From 6b16a5555e713a53c2820e5397c94f26732e6cd7 Mon Sep 17 00:00:00 2001 From: feeiyu <158308373+feeiyu@users.noreply.github.com> Date: Tue, 4 Mar 2025 21:06:44 +0800 Subject: [PATCH] Fix lost focus when navigating back in project search result (#22483) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #22447 When navigate forward/back, the focus moves from the ProjectSearchView's result editor to the Pane, and then move to the ProjectSearchView, but the event `on_focus_in` not triggered for ProjectSearchView, causing the result editor to lose focus eventually. https://github.com/zed-industries/zed/blob/f6dabadaf79bd29c89c8d55a1e9f1d33236f736e/crates/workspace/src/workspace.rs#L1372 https://github.com/zed-industries/zed/blob/f6dabadaf79bd29c89c8d55a1e9f1d33236f736e/crates/workspace/src/workspace.rs#L1385 Considering that the navigation might be triggered again in the next frame, so use `on_next_frame` in `on_focus` event to move focus to result editor. Next frame: - the blur event triggered for result editor. - focus move from ProjectSearchView to result editor in `on_focus` event for ProjectSearchView - navigate again, focus moves from result editor to Pane then move back to ProjectSearchView - the focus not change during this frame, so no focus event happened for ProjectSearchView. ![fix lost focus1229](https://github.com/user-attachments/assets/bfaac839-7bcf-40e7-b3b4-1423d0510594) Release Notes: - Fix lost focus when navigate back in project search result Co-authored-by: Kirill Bulatov --- crates/search/src/project_search.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index 1ff04a8153b50184ea4a992a0440c5b2d58b64ad..e8629cbb1d53e836eadff4e33a0d4d1655fd69e6 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -785,14 +785,16 @@ impl ProjectSearchView { ); let focus_handle = cx.focus_handle(); - subscriptions.push(cx.on_focus_in(&focus_handle, window, |this, window, cx| { - if this.focus_handle.is_focused(window) { - if this.has_matches() { - this.results_editor.focus_handle(cx).focus(window); - } else { - this.query_editor.focus_handle(cx).focus(window); + subscriptions.push(cx.on_focus(&focus_handle, window, |_, window, cx| { + cx.on_next_frame(window, |this, window, cx| { + if this.focus_handle.is_focused(window) { + if this.has_matches() { + this.results_editor.focus_handle(cx).focus(window); + } else { + this.query_editor.focus_handle(cx).focus(window); + } } - } + }); })); let languages = project.read(cx).languages().clone();