Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Help › Search: ← / → and Space edit the field instead of driving the menu

Higgs32584a008842a853c
diff --git a/crates/ui-egui/src/menu_nav.rs b/crates/ui-egui/src/menu_nav.rs
index 822fcae2b..879bd4397 100644
--- a/crates/ui-egui/src/menu_nav.rs
+++ b/crates/ui-egui/src/menu_nav.rs
@@ -336,7 +336,12 @@ impl Nav {
let Some(top) = self.top.filter(|_| !tops.is_empty()) else { return };
let level = self.level.min(self.rows.len().saturating_sub(1));
self.level = level;
- let press = |k: Key| ctx.input_mut(|i| i.consume_key(Modifiers::NONE, k));
+ // A text field in the menu (Help › Search) keeps the keys that edit it: ← / → move its
+ // cursor and Space types a space.
+ let typing = ctx.text_edit_focused();
+ let press = |k: Key| {
+ !(typing && matches!(k, Key::ArrowLeft | Key::ArrowRight | Key::Space)) && ctx.input_mut(|i| i.consume_key(Modifiers::NONE, k))
+ };
let current = self.highlighted(level).and_then(|i| self.rows.get(level)?.get(i)).cloned();
let switch = |by: usize| {
let n = tops.len();
diff --git a/crates/ui-egui/src/menu_nav_tests.rs b/crates/ui-egui/src/menu_nav_tests.rs
index 1643e2765..659a794b3 100644
--- a/crates/ui-egui/src/menu_nav_tests.rs
+++ b/crates/ui-egui/src/menu_nav_tests.rs
@@ -265,6 +265,41 @@ fn left_and_right_switch_menus_and_shortcuts_wait_while_one_is_open() {
assert!(!is_open(&h.ctx), "shortcuts work again");
}
+/// In Help › Search, ← / → move the field's cursor instead of switching to Window / File, and
+/// Space types a space instead of running the lit row.
+#[test]
+fn help_search_keeps_the_arrows_and_space_that_edit_it() {
+ let mut h = harness((1280.0, 720.0, 1.0));
+ open(&mut h, "Help");
+ let field = egui::Id::new("help-menu-search").with("field");
+ assert_eq!(h.ctx.memory(|m| m.focused()), Some(field), "the search field has the keyboard");
+ let text = |h: &Harness<'static, PhotocraftApp>| h.ctx.data(|d| d.get_temp::<String>(egui::Id::new("help-menu-search"))).unwrap_or_default();
+ let typing = |h: &mut Harness<'static, PhotocraftApp>, s: &str| {
+ h.event(egui::Event::Text(s.to_string()));
+ h.run_steps(3);
+ };
+ typing(&mut h, "levls");
+ key(&mut h, egui::Key::ArrowLeft);
+ assert!(h.query_by_label_contains("About PhotoCraft").is_some(), "← keeps Help open");
+ key(&mut h, egui::Key::ArrowLeft);
+ typing(&mut h, "e");
+ assert_eq!(text(&h), "levels", "← moved the cursor");
+ key(&mut h, egui::Key::ArrowRight);
+ assert!(h.query_by_label_contains("About PhotoCraft").is_some(), "→ keeps Help open");
+ // The pointer resting on a result lights it; a space typed then is text, not a click.
+ let levels = h.get_by_label_contains("› Levels").rect().center();
+ h.hover_at(levels);
+ h.run_steps(3);
+ assert!(Nav::current(&h.ctx).highlighted(0).is_some(), "a result is lit");
+ h.event(egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE });
+ h.event(egui::Event::Text(" ".into()));
+ h.event(egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::NONE });
+ h.run_steps(3);
+ assert!(is_open(&h.ctx) && h.query_by_label_contains("About PhotoCraft").is_some(), "Space keeps Help open");
+ assert!(h.state().ui.dialogs.is_empty(), "Space ran no command: {:?}", h.state().ui.dialogs);
+ assert_eq!(text(&h), "levels ");
+}
+
/// A level's visible height (its view) and the full height of its rows: equal means no scrolling.
fn fits(h: &Harness<'static, PhotocraftApp>, level: usize) -> (f32, f32) {
let nav = Nav::current(&h.ctx);