diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs index 0b6140aa3a..e18213b398 100644 --- a/crates/ui-egui/src/chrome.rs +++ b/crates/ui-egui/src/chrome.rs @@ -820,6 +820,9 @@ fn parse_scale(text: &str) -> Option { /// Global keyboard shortcuts. pub fn shortcuts(app: &mut RustGisApp, ctx: &egui::Context) { + if app.ui.command_search.is_some() { + return; + } // Behind a dialog (Save changes?, Recover unsaved work?, Command Search…) the keys are the // dialog's: Escape answers it rather than switching tools, and Ctrl+N can't start what it // asks about. @@ -1298,6 +1301,45 @@ fn command_matches(id: &str, label: &str, location: &str, ql: &str) -> bool { || crate::i18n::location_raw(location).to_lowercase().contains(ql) } +/// Keeps an Escape press owned by the palette until release, even after it closes. +#[derive(Default)] +struct CommandSearchEscape { + open: bool, + held: bool, + presses: Vec<(usize, egui::Event)>, +} + +impl egui::Plugin for CommandSearchEscape { + fn debug_name(&self) -> &'static str { + "Command search Escape" + } + + fn input_hook(&mut self, _ctx: &egui::Context, input: &mut egui::RawInput) { + // egui stops drags on Escape before the application draws. Give palette + // presses only to the palette, in order relative to its IME events. + self.presses.clear(); + let mut position = 0; + input.events.retain(|event| { + match event { + egui::Event::Key { key: Key::Escape, pressed: true, repeat, .. } if self.open || self.held => { + if !self.held && !repeat { + self.presses.push((position, event.clone())); + position += 1; + } + self.held = true; + return false; + } + egui::Event::Key { key: Key::Escape, pressed: false, .. } | egui::Event::WindowFocused(false) => { + self.held = false; + } + _ => {} + } + position += 1; + true + }); + } +} + /// The row the arrow keys have picked in Command Search, kept between frames while the palette /// stays open on the same query. #[derive(Clone, Default)] @@ -1327,7 +1369,102 @@ pub(crate) const NUMBER_KEYS: [Key; 10] = /// The command palette: type to filter every command, Up and Down pick a row, Enter runs it, and /// Ctrl (⌘) with 1 … 9 and 0 runs one of the first ten. pub fn command_search(app: &mut RustGisApp, ctx: &egui::Context) { - let Some(mut q) = app.ui.command_search.take() else { return }; + let Some(mut q) = app.ui.command_search.take() else { + app.ui.command_search_composing = false; + return; + }; + if ctx.plugin_opt::().is_none() { + ctx.add_plugin(CommandSearchEscape::default()); + } + let edit_id = egui::Id::unique("command_search_input"); + // Enter and Escape may arrive in the same frame that begins or ends a composition. + // Leave that frame's keys to the IME as well as keys during an ongoing preedit. + let focused = ctx.memory(|m| m.has_focus(edit_id)); + app.ui.command_search_composing &= focused; + let escape_presses = ctx.with_plugin::(|input| std::mem::take(&mut input.presses)).unwrap_or_default(); + let (mut enter, mut escape, deleting_preedit, after_delete, ime_owns_keys) = ctx.input(|i| { + // Raw events preserve their positions even if another widget consumed a key. + let mut events = std::borrow::Cow::Borrowed(i.raw.events.as_slice()); + for (position, event) in escape_presses { + let events = events.to_mut(); + events.insert(position.min(events.len()), event); + } + let mut accept_keys = !app.ui.command_search_composing; + let mut ime_owns_frame = false; + let mut enter = false; + let mut escape = false; + let mut deleting_preedit = false; + let mut after_delete = (false, false); + for event in events.iter() { + match event { + egui::Event::Ime(egui::ImeEvent::Preedit { text, .. } | egui::ImeEvent::Commit(text)) + if focused && deleting_preedit && text.is_empty() => + { + // The IME can report the deletion itself before the next key arrives. + deleting_preedit = false; + after_delete = (false, false); + ime_owns_frame = false; + accept_keys = true; + enter = false; + escape = false; + app.ui.command_search_composing = false; + } + egui::Event::Ime(egui::ImeEvent::Preedit { text, .. }) if focused => { + deleting_preedit = false; + after_delete = (false, false); + ime_owns_frame |= app.ui.command_search_composing || !text.is_empty(); + app.ui.command_search_composing = !text.is_empty(); + } + egui::Event::Ime(egui::ImeEvent::Commit(_)) if focused => { + deleting_preedit = false; + after_delete = (false, false); + // A standalone commit can carry ordinary text. + ime_owns_frame |= app.ui.command_search_composing; + app.ui.command_search_composing = false; + } + egui::Event::Paste(text) | egui::Event::Text(text) + if focused + && !text.is_empty() + && !matches!(event, egui::Event::Text(text) if !app.ui.command_search_composing || text == "\n" || text == "\r") => + { + // TextEdit replaces the preedit. Keys after the replacement can act + // on the replacement, even if the preedit started in this frame. + if ime_owns_frame { + enter = false; + escape = false; + } + ime_owns_frame = false; + app.ui.command_search_composing = false; + accept_keys = true; + deleting_preedit = false; + after_delete = (false, false); + } + egui::Event::Cut if app.ui.command_search_composing => { + deleting_preedit = true; + } + egui::Event::Key { key: Key::Backspace | Key::Delete, pressed: true, .. } if app.ui.command_search_composing => { + deleting_preedit = true; + } + egui::Event::Key { key, pressed: true, repeat: false, .. } => { + if accept_keys && !ime_owns_frame { + enter |= *key == Key::Enter; + escape |= *key == Key::Escape; + } else if deleting_preedit { + after_delete.0 |= *key == Key::Enter; + after_delete.1 |= *key == Key::Escape; + } + } + _ => {} + } + } + ( + enter && !ime_owns_frame, + escape && !ime_owns_frame, + deleting_preedit, + after_delete, + ime_owns_frame || app.ui.command_search_composing, + ) + }); let t = Tokens::get(ctx); let mut keep = true; let mut chosen: Option = None; @@ -1335,9 +1472,14 @@ pub fn command_search(app: &mut RustGisApp, ctx: &egui::Context) { let pass = ctx.cumulative_pass_nr(); let cursor: Option = ctx.data(|d| d.get_temp(cursor_id)); // The arrows are the list's, taken before the text box sees them (it would move its caret). - let (down, up) = ctx.input_mut(|i| (i.consume_key(Modifiers::NONE, Key::ArrowDown), i.consume_key(Modifiers::NONE, Key::ArrowUp))); + let (down, up) = if ime_owns_keys { + (false, false) + } else { + ctx.input_mut(|i| (i.consume_key(Modifiers::NONE, Key::ArrowDown), i.consume_key(Modifiers::NONE, Key::ArrowUp))) + }; // Ctrl (⌘) and a digit run one of the first ten rows that can run: 1 the first, 0 the tenth. - let jump = ctx.input_mut(|i| NUMBER_KEYS.iter().position(|&key| i.consume_key(Modifiers::COMMAND, key))); + let jump = + if ime_owns_keys { None } else { ctx.input_mut(|i| NUMBER_KEYS.iter().position(|&key| i.consume_key(Modifiers::COMMAND, key))) }; // Fits small screens: never wider or taller than the window allows. let screen = ctx.content_rect(); let width = (screen.width() - 48.0).clamp(220.0, 520.0); @@ -1347,13 +1489,34 @@ pub fn command_search(app: &mut RustGisApp, ctx: &egui::Context) { ui.horizontal(|ui| { let (r, _) = ui.allocate_exact_size(vec2(18.0, 18.0), Sense::hover()); icons::paint(ui.painter(), r, "search", t.text_dim, t.accent); - let te = ui.add( - egui::TextEdit::singleline(&mut q) - .hint_text(tl!("Search commands — e.g. buffer, basemap, labels")) - .desired_width(width - 56.0) - .frame(egui::Frame::NONE), - ); - te.request_focus(); + let edit = egui::TextEdit::singleline(&mut q) + .id(edit_id) + .return_key(None) + .event_filter(egui::EventFilter { horizontal_arrows: true, vertical_arrows: true, escape: true, ..Default::default() }) + .hint_text(tl!("Search commands — e.g. buffer, basemap, labels")) + .desired_width(width - 56.0) + .frame(egui::Frame::NONE) + .show(ui); + // A deletion may remove the preedit without a corresponding IME event. + // Cursor movement alone can also collapse the range, so require a text edit. + if deleting_preedit && edit.response.response.changed() && edit.state.cursor.char_range().is_some_and(|range| range.is_empty()) + { + enter |= after_delete.0; + escape |= after_delete.1; + app.ui.command_search_composing = false; + } + let edit = edit.response.response; + // Clicking or dragging in TextEdit switches its cursor to a regular + // selection, even when no IME event arrives to clear the preedit. + if edit.clicked() || edit.dragged() || (edit.hovered() && ui.input(|i| i.pointer.any_pressed())) { + app.ui.command_search_composing = false; + } + // The modal starts with an invisible sizing pass. Request focus only when + // the edit is interactive and unfocused, since a request can interrupt IME. + if edit.enabled() && !ctx.memory(|m| m.has_focus(edit_id)) { + app.ui.command_search_composing = false; + edit.request_focus(); + } ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { let (r, resp) = ui.allocate_exact_size(vec2(22.0, 22.0), Sense::click()); if resp.hovered() { @@ -1473,10 +1636,8 @@ pub fn command_search(app: &mut RustGisApp, ctx: &egui::Context) { }; let row = step_row(row, runnable, down, up); ui.ctx().data_mut(|d| d.insert_temp(cursor_id, SearchCursor { query: q.clone(), row, pass })); - // The modal's area starts from its last size, so after a short result list the scroll - // area would get only that room back (egui's 64 px floor): ask for the full height - // whenever the list scrolls. A few hits still shrink the palette to fit. - egui::ScrollArea::vertical().max_height(list_h).min_scrolled_height(list_h).show(ui, |ui| { + // Keep the search field and IME candidate anchor stable as results change. + egui::ScrollArea::vertical().max_height(list_h).min_scrolled_height(list_h).auto_shrink([true, false]).show(ui, |ui| { let row_w = ui.available_width(); // The number keys' column: as wide as the widest, so the hints line up. let number_w = NUMBER_KEYS @@ -1547,13 +1708,12 @@ pub fn command_search(app: &mut RustGisApp, ctx: &egui::Context) { app.ui.shortcut_editor.filter = id.clone(); keep = false; } - let enter = picked && ui.input(|i| i.key_pressed(Key::Enter)); - if resp.clicked() || enter || jumped { + if resp.clicked() || (picked && enter) || jumped { chosen = Some(id.clone()); } } }); - if ui.input(|i| i.key_pressed(Key::Escape)) { + if escape { keep = false; } }); @@ -1586,8 +1746,12 @@ pub fn command_search(app: &mut RustGisApp, ctx: &egui::Context) { let _ = app.run_by_user(&id, Value::Null); } } + ctx.with_plugin::(|input| input.open = keep); if keep { app.ui.command_search = Some(q); + } else { + ctx.memory_mut(|m| m.surrender_focus(edit_id)); + app.ui.command_search_composing = false; } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index dd31c685bb..5ec6c4f474 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -991,6 +991,7 @@ pub struct UiState { /// A minimized ribbon temporarily opened from its tab strip. pub ribbon_peek: bool, pub command_search: Option, + command_search_composing: bool, /// A ribbon button being shown by a task step's Show Me. pub show_me: Option, pub about: bool, @@ -1257,6 +1258,7 @@ impl Default for UiState { ribbon_minimized: false, ribbon_peek: false, command_search: None, + command_search_composing: false, show_me: None, about: false, shortcuts: false, diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs index 0325a9162f..f654072e73 100644 --- a/crates/ui-egui/tests/smoke.rs +++ b/crates/ui-egui/tests/smoke.rs @@ -13,7 +13,11 @@ mod remembered_service; mod service_logins; /// Builds a headless World Atlas UI for interaction tests. fn harness() -> egui_kittest::Harness<'static, RustGisApp> { - egui_kittest::Harness::builder().with_size(egui::vec2(1400.0, 860.0)).with_max_steps(64).build_ui_state( + harness_with_size(egui::vec2(1400.0, 860.0)) +} + +fn harness_with_size(size: egui::Vec2) -> egui_kittest::Harness<'static, RustGisApp> { + egui_kittest::Harness::builder().with_size(size).with_max_steps(64).build_ui_state( |ui, app: &mut RustGisApp| { let ctx = ui.ctx().clone(); app.logic(&ctx); @@ -23,6 +27,386 @@ fn harness() -> egui_kittest::Harness<'static, RustGisApp> { ) } +fn ime_and_key(h: &mut egui_kittest::Harness<'_, RustGisApp>, ime: egui::ImeEvent, key: egui::Key) { + event_and_key(h, egui::Event::Ime(ime), key); +} + +fn event_and_key(h: &mut egui_kittest::Harness<'_, RustGisApp>, event: egui::Event, key: egui::Key) { + // Queue both events before stepping the harness. + h.input_mut().events.push(event); + h.input_mut().events.extend([true, false].map(|pressed| egui::Event::Key { + key, + physical_key: None, + pressed, + repeat: false, + modifiers: egui::Modifiers::NONE, + })); +} + +#[test] +fn layer_filter_keeps_ime_anchor_during_preedit() { + use egui::{Event, ImeEvent}; + + let mut h = harness(); + // This tests IME state, not whether the host has a CJK font. + h.ctx.set_missing_glyph_policy(egui::MissingGlyphPolicy::Tofu); + h.run_steps(3); + h.get_by(|node| node.placeholder() == Some("Filter layers")).focus(); + h.run_steps(3); + let ime = h.output().platform_output.ime.unwrap(); + h.event(Event::Ime(ImeEvent::Preedit { text: "huanchong".into(), active_range_chars: None })); + h.run_steps(3); + assert_eq!(h.state().ui.layer_filter, "huanchong"); + let composing = h.output().platform_output.ime.unwrap(); + assert_eq!(composing.rect.top(), ime.rect.top()); + assert_eq!(composing.cursor_rect.top(), ime.cursor_rect.top()); + h.event(Event::Ime(ImeEvent::Commit("缓冲".into()))); + h.run_steps(3); + assert_eq!(h.state().ui.layer_filter, "缓冲"); + h.event(Event::Ime(ImeEvent::Preedit { text: "qu".into(), active_range_chars: None })); + h.run_steps(3); + h.event(Event::Ime(ImeEvent::Preedit { text: String::new(), active_range_chars: None })); + h.run_steps(3); + assert_eq!(h.state().ui.layer_filter, "缓冲"); + assert_eq!(h.output().platform_output.ime.unwrap().rect.top(), ime.rect.top()); +} + +#[test] +fn command_search_keeps_ime_anchor_and_accepts_committed_text() { + use egui::{Event, ImeEvent, Key, Modifiers}; + + for width in [960.0, 320.0] { + let mut h = harness_with_size(egui::vec2(width, 480.0)); + // This tests IME state, not whether the host has a CJK font. + h.ctx.set_missing_glyph_policy(egui::MissingGlyphPolicy::Tofu); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + let ime = h.output().platform_output.ime.unwrap(); + assert!(ime.rect.left() >= 0.0 && ime.rect.right() <= width); + + let preedit = |text: &str| ImeEvent::Preedit { text: text.into(), active_range_chars: None }; + h.event(Event::Ime(preedit("huanchong"))); + h.run_steps(4); + assert_eq!(h.state().ui.command_search.as_deref(), Some("huanchong")); + let composing = h.output().platform_output.ime.unwrap(); + assert_eq!(composing.rect.top(), ime.rect.top()); + assert_eq!(composing.cursor_rect.top(), ime.cursor_rect.top()); + assert!(!composing.should_interrupt_composition); + + ime_and_key(&mut h, ImeEvent::Commit("缓冲".into()), Key::Enter); + h.run_steps(3); + assert_eq!(h.state().ui.command_search.as_deref(), Some("缓冲")); + + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.key_press(Key::Backspace); + h.run_steps(4); + assert_eq!(h.state().ui.command_search.as_deref(), Some("")); + assert_eq!(h.output().platform_output.ime.unwrap().rect.top(), ime.rect.top()); + + // An Enter delivered alongside preedit must not run the matching Buffer tool. + ime_and_key(&mut h, preedit("buffer"), Key::Enter); + h.run_steps(3); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + ime_and_key(&mut h, preedit(""), Key::Escape); + h.run_steps(3); + assert_eq!(h.state().ui.command_search.as_deref(), Some("")); + assert!(h.output().platform_output.ime.is_some()); + + h.key_press(Key::Escape); + h.run_steps(3); + assert!(h.state().ui.command_search.is_none()); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(3); + h.event(Event::Text("buffer".into())); + h.run_steps(3); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + h.key_press(Key::Enter); + h.run_steps(3); + assert!(h.state().ui.command_search.is_none()); + } +} + +#[test] +fn command_search_escape_works_after_paste_replaces_preedit() { + use egui::{Event, ImeEvent, Key}; + + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Ime(ImeEvent::Preedit { text: "huanchong".into(), active_range_chars: None })); + h.run_steps(2); + h.event(Event::Paste("buffer".into())); + h.run_steps(2); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + h.key_press(Key::Escape); + h.run_steps(2); + assert!(h.state().ui.command_search.is_none()); +} + +#[test] +fn command_search_handles_keys_after_replacing_preedit_in_the_same_frame() { + use egui::{Event, ImeEvent, Key}; + + for (key, same_frame_preedit) in [(Key::Enter, false), (Key::Escape, false), (Key::Enter, true), (Key::Escape, true)] { + for replacement in [Event::Paste("buffer".into()), Event::Text("buffer".into())] { + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + let preedit = Event::Ime(ImeEvent::Preedit { text: "huanchong".into(), active_range_chars: None }); + if same_frame_preedit { + h.input_mut().events.push(preedit); + } else { + h.event(preedit); + h.run_steps(2); + assert_eq!(h.state().ui.command_search.as_deref(), Some("huanchong")); + } + + event_and_key(&mut h, replacement, key); + h.run_steps(2); + assert!(h.state().ui.command_search.is_none(), "{key:?} should act on the replacement query"); + } + } +} + +#[test] +fn command_search_ignores_enter_held_after_ime_commit() { + use egui::{Event, ImeEvent, Key, Modifiers}; + + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Ime(ImeEvent::Preedit { text: "buffer".into(), active_range_chars: None })); + h.step(); + + let enter = |pressed| Event::Key { key: Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE }; + h.input_mut().events.extend([Event::Ime(ImeEvent::Commit("buffer".into())), enter(true)]); + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + + h.event(enter(true)); // egui marks it as a repeat while the key is still down. + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + + h.event(enter(false)); + h.step(); + h.key_press(Key::Enter); + h.step(); + assert!(h.state().ui.command_search.is_none()); +} + +#[test] +fn command_search_handles_escape_after_deleting_preedit_in_same_frame() { + use egui::{Event, ImeEvent, Key, Modifiers}; + + for delete_first in [false, true] { + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Ime(ImeEvent::Preedit { text: "buffer".into(), active_range_chars: None })); + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + + let mut keys = [Key::Escape, Key::Backspace]; + if delete_first { + keys.reverse(); + } + for key in keys { + h.input_mut().events.extend([true, false].map(|pressed| Event::Key { + key, + physical_key: None, + pressed, + repeat: false, + modifiers: Modifiers::NONE, + })); + } + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), if delete_first { None } else { Some("") }); + } +} + +#[test] +fn command_search_handles_keys_after_an_ime_reports_cleared_preedit() { + use egui::{Event, ImeEvent, Key}; + + for (notification, key) in [ + (ImeEvent::Preedit { text: String::new(), active_range_chars: None }, Key::Enter), + (ImeEvent::Preedit { text: String::new(), active_range_chars: None }, Key::Escape), + (ImeEvent::Commit(String::new()), Key::Enter), + ] { + for clear_first in [true, false] { + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Text("buffer".into())); + h.step(); + h.event(Event::Ime(ImeEvent::Preedit { text: "x".into(), active_range_chars: None })); + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("bufferx")); + + let key_events = |key| { + [true, false].map(|pressed| Event::Key { + key, + physical_key: None, + pressed, + repeat: false, + modifiers: egui::Modifiers::NONE, + }) + }; + let events = &mut h.input_mut().events; + events.extend(key_events(Key::Backspace)); + if clear_first { + events.push(Event::Ime(notification.clone())); + events.extend(key_events(key)); + } else { + events.extend(key_events(key)); + events.push(Event::Ime(notification.clone())); + } + h.step(); + assert_eq!( + h.state().ui.command_search.as_deref(), + if clear_first { None } else { Some("buffer") }, + "{key:?}, clear_first={clear_first}" + ); + assert_eq!(h.state().gp.tool, (clear_first && key == Key::Enter).then_some("buffer")); + } + } +} + +#[test] +fn command_search_keeps_ime_key_ownership_when_the_caret_moves() { + use egui::{Event, ImeEvent, Key}; + + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Ime(ImeEvent::Preedit { text: "buffer".into(), active_range_chars: None })); + h.step(); + h.key_press(Key::ArrowLeft); + h.step(); + h.key_press(Key::Enter); + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + assert!(h.state().gp.tool.is_none()); + + h.event(Event::Ime(ImeEvent::Commit(String::new()))); + h.step(); + h.key_press(Key::Escape); + h.step(); + assert!(h.state().ui.command_search.is_none()); +} + +#[test] +fn command_search_keys_work_after_clicking_during_preedit() { + use egui::{Event, ImeEvent, Key, Modifiers, PointerButton}; + + for key in [Key::Enter, Key::Escape] { + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Ime(ImeEvent::Preedit { text: "buffer".into(), active_range_chars: None })); + h.step(); + + let pos = h.output().platform_output.ime.unwrap().rect.center(); + h.event(Event::PointerMoved(pos)); + for pressed in [true, false] { + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.step(); + } + h.key_press(key); + h.step(); + assert!(h.state().ui.command_search.is_none(), "{key:?} should act after the click"); + assert_eq!(h.state().gp.tool, (key == Key::Enter).then_some("buffer")); + } +} + +#[test] +fn command_search_handles_cut_during_preedit() { + use egui::{Event, ImeEvent, Key}; + for key in [Key::Enter, Key::Escape] { + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Ime(ImeEvent::Preedit { text: "buffer".into(), active_range_chars: None })); + h.step(); + h.event(Event::Cut); + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("")); + h.key_press(key); + h.step(); + assert!(h.state().ui.command_search.is_none(), "{key:?} should work after cut"); + } + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.event(Event::Ime(ImeEvent::Preedit { text: "buffer".into(), active_range_chars: None })); + h.step(); + h.key_press(Key::ArrowLeft); + h.step(); + h.event(Event::Cut); + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); + h.key_press(Key::Enter); + h.step(); + assert_eq!(h.state().ui.command_search.as_deref(), Some("buffer")); +} + +#[test] +fn command_search_handles_click_and_preedit_in_same_frame() { + use egui::{Event, ImeEvent, Key, Modifiers, PointerButton}; + for key in [Key::Enter, Key::Escape] { + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + let pos = h.output().platform_output.ime.unwrap().rect.center(); + h.input_mut().events.extend([ + Event::Ime(ImeEvent::Preedit { text: "buffer".into(), active_range_chars: None }), + Event::PointerMoved(pos), + Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }, + Event::PointerButton { pos, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }, + ]); + h.step(); + h.key_press(key); + h.step(); + assert!(h.state().ui.command_search.is_none(), "{key:?} should work after click"); + } +} + +#[test] +fn command_search_runs_after_standalone_ime_commit_and_enter() { + use egui::{ImeEvent, Key}; + + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + ime_and_key(&mut h, ImeEvent::Commit("buffer".into()), Key::Enter); + h.run_steps(2); + assert!(h.state().ui.command_search.is_none()); +} + +#[test] +fn command_search_accepts_typing_after_clicking_empty_results() { + use egui::{Event, Modifiers, PointerButton}; + + let mut h = harness(); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + let query = "zzzxq-no-matching-command"; + h.event(Event::Text(query.into())); + h.run_steps(3); + assert_eq!(h.state().ui.command_search.as_deref(), Some(query)); + + let edit = h.output().platform_output.ime.unwrap().rect; + let pos = egui::pos2(edit.center().x, edit.bottom() + 60.0); + h.event(Event::PointerMoved(pos)); + for pressed in [true, false] { + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.run_steps(2); + } + h.event(Event::Text("a".into())); + h.run_steps(3); + assert_eq!(h.state().ui.command_search.as_deref(), Some(format!("{query}a").as_str())); +} + /// Runs a layer command through Command Search and checks that the search closes. fn search_layer_command(h: &mut egui_kittest::Harness<'_, RustGisApp>, command: &str) { h.state_mut().ui.command_search = Some(command.into()); @@ -4839,6 +5223,14 @@ fn a_selected_text_is_dragged_on_the_map() { h.input_mut().events.push(egui::Event::PointerMoved(egui::pos2(x, 500.0))); h.step(); } + let undo = h.state().session.undo_label().map(str::to_owned); + h.state_mut().run("ui.commandSearch", serde_json::Value::Null).unwrap(); + h.run_steps(4); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().ui.command_search.is_none()); + assert_eq!(anchor(&h), before); + assert_eq!(h.state().session.undo_label(), undo.as_deref()); h.input_mut().events.push(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary,