fix(search): select the query ⌘F reopens with

The find box keeps the last query on purpose — reopening on the word you
just looked for is most of what a find bar is for. But the caret was
wrong for it in both directions, so the kept query behaved like a prefix
or a suffix instead of like a value you are about to replace.

Reopened after closing, `default_value` left the caret at offset 0: ⌘F
then "beta" over a kept "alpha" gave "betaalpha", 0/0, and no clue why.
Re-focused while still open, the caret was at the end and typing appended
instead — "alphaZZ". Every other find bar on the platform selects the
text, and the earlier rename-box fix reached the same wall and settled
for caret-at-end because `InputState::select_all` is `pub(super)` in the
UI crate.

It is reachable after all, just not as a method: the input binds ⌘A to a
public `SelectAll` action, and a `FocusHandle` can be handed an action
directly. Aiming it at the box's own handle rather than at whatever
happens to be focused keeps it from selecting something else if focus
moved. It goes through `on_next_frame` because actions route along the
last drawn frame's dispatch tree, and a box created this turn is not in
it yet.

Verified in a dev window across all three paths: closed and reopened,
re-focused while open, and empty (no selection to make, so nothing
happens). The frame requirement is also why there is no unit test — the
test harness never paints, so the callback never fires there.
This commit is contained in:
l0ng-ai
2026-08-09 09:16:34 +07:00
parent 12cb07d008
commit fd8ddedfea
2 changed files with 34 additions and 0 deletions
+30
View File
@@ -60,6 +60,35 @@ impl SearchState {
}
}
/// Select everything already in the find box, so ⌘F followed by typing
/// replaces the last query instead of growing it.
///
/// The box keeps the previous query on purpose — stepping back through it is
/// most of what a find bar is for — but that only works if the caret does not
/// treat it as a prefix. It did: `default_value` leaves the caret at offset 0,
/// so reopening and typing "beta" over "alpha" produced "betaalpha", and
/// re-focusing an open bar appended instead.
///
/// `InputState::select_all` is not public, so this goes through the action the
/// input binds ⌘A to, aimed at the input's own focus handle rather than at
/// whatever is focused. It waits a frame because the dispatch tree an action is
/// routed through is the one from the last frame drawn — a box that has just
/// been created is not in it yet. That frame is also why this has no unit
/// test: the test harness never paints, so the callback never runs there.
fn select_whole_query(
input: &Entity<InputState>,
window: &mut Window,
cx: &mut Context<TerminalView>,
) {
if input.read(cx).value().is_empty() {
return;
}
let handle = gpui::Focusable::focus_handle(input.read(cx), cx);
window.on_next_frame(move |window, cx| {
handle.dispatch_action(&gpui_component::input::SelectAll, window, cx);
});
}
impl TerminalView {
pub fn open_search(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let fresh = self.search.is_none();
@@ -82,6 +111,7 @@ impl TerminalView {
}
if let Some(input) = self.search.as_ref().map(|s| s.input.clone()) {
input.update(cx, |state, cx| state.focus(window, cx));
select_whole_query(&input, window, cx);
}
if fresh {
self.recompute_matches(cx);
+4
View File
@@ -7098,6 +7098,10 @@ mod gpui_tests {
*out.borrow_mut() = Some(view.clone());
gpui_component::Root::new(view, window, cx)
});
window
.update(cx, |_, window, _| window.activate_window())
.unwrap();
cx.background_executor.run_until_parked();
let view = built.borrow_mut().take().expect("the pane was built");
(window, view, daemon_side)
}