PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Dropdowns: type a letter to jump to an item, Enter to choose (#1481) (#2948)
David Mann509b596805cf
diff --git a/crates/ui-egui/src/blend_dropdown_keys_tests.rs b/crates/ui-egui/src/blend_dropdown_keys_tests.rsindex 64d71e136..86f24cbdb 100644--- a/crates/ui-egui/src/blend_dropdown_keys_tests.rs+++ b/crates/ui-egui/src/blend_dropdown_keys_tests.rs@@ -45,3 +45,49 @@ fn arrow_keys_step_the_open_blend_mode_dropdown_without_nudging_the_layer() {h.run_steps(2);assert_eq!(layer(&h).0, BlendMode::Normal);}++/// #1481: with the Blend Mode list open, a letter picks the next mode starting with it (as in+/// Photoshop's pop-up menus) instead of reaching the tool shortcuts, and ↩ closes the list.+#[test]+fn letters_pick_blend_modes_in_the_open_list_without_switching_tools() {+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_max_steps(64).build_eframe(|cc| {+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 64, "height": 64})).unwrap();+ app.run("layer.new.layer", json!({})).unwrap();+ app.ui.tool = Tool::Move;+ app+ });+ h.run_steps(6);+ let blend = |h: &Harness<'_, PhotocraftApp>| {+ let st = h.state().session.active().unwrap();+ st.doc.layer(st.active_layer.unwrap()).unwrap().blend+ };+ let type_key = |h: &mut Harness<'_, PhotocraftApp>, key: egui::Key, text: &str| {+ h.key_press(key);+ h.event(egui::Event::Text(text.into()));+ h.run_steps(2);+ };+ let named = |n: &egui_kittest::Node<'_>| {+ let a = n.accesskit_node();+ a.value().as_deref() == Some("Normal") || a.label().as_deref() == Some("Normal")+ };+ h.query_all_by_role(Role::ComboBox).find(|n| named(n)).expect("the Layers panel's Blend Mode dropdown").click();+ h.run_steps(2);+ type_key(&mut h, egui::Key::M, "m");+ assert_eq!(blend(&h), BlendMode::Multiply, "M picks Multiply");+ assert_eq!(h.state().ui.tool, Tool::Move, "M doesn't also pick the Marquee tool");+ type_key(&mut h, egui::Key::D, "d");+ assert_eq!(blend(&h), BlendMode::DarkerColor, "D picks the next mode starting with D");+ type_key(&mut h, egui::Key::D, "d");+ assert_eq!(blend(&h), BlendMode::Difference, "D again picks the one after");+ h.key_press(egui::Key::Enter);+ h.run_steps(3);+ assert!(!crate::widgets::dropdown_list_open(&h.ctx), "↩ closes the list");+ assert_eq!(blend(&h), BlendMode::Difference, "↩ keeps the mode");+ // With the list closed the letters are the tools' again.+ h.key_press(egui::Key::M);+ h.event(egui::Event::Text("m".into()));+ h.run_steps(2);+ assert!(h.state().ui.tool != Tool::Move, "M picks a marquee tool once the list is closed");+}diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rsindex 622b2d485..e88cf2a45 100644--- a/crates/ui-egui/src/shortcuts.rs+++ b/crates/ui-egui/src/shortcuts.rs@@ -306,8 +306,9 @@ pub fn handle(app: &mut PhotocraftApp, ctx: &egui::Context) {if app.camera_raw.is_some() {return;}- // An open menu owns the keyboard (arrows, ↩, Esc), like a native menu.- if crate::menu_nav::is_open(ctx) {+ // An open menu owns the keyboard (arrows, ↩, Esc), like a native menu; so does an open+ // dropdown list, whose letters pick an option rather than a tool (#1481).+ if crate::menu_nav::is_open(ctx) || crate::widgets::dropdown_list_open(ctx) {return;}let symmetry_editing = app.ui.symmetry_transform.is_some();diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rsindex ccd7877e1..303e79b6d 100644--- a/crates/ui-egui/src/widgets.rs+++ b/crates/ui-egui/src/widgets.rs@@ -7,6 +7,8 @@ use crate::theme::{self, Tokens};mod color_count;pub use color_count::color_count_row;+mod type_ahead;+pub use type_ahead::list_open as dropdown_list_open;/// Match the Spectrum application-menu padding and blue/white hover state in/// secondary menus (status bar, dock hamburger, layer context menus; #2187).@@ -996,7 +998,7 @@ fn dropdown_with<T: PartialEq + Clone>(ui: &mut Ui, id: &str, current: &mut T, o// The wheel's notches add up over the button and the open list alike.let wheel_id = ui.make_persistent_id(id).with("wheel");let wheel = wheel && ui.is_enabled();- let (mut over_list, mut wheeled) = (false, false);+ let (mut over_list, mut wheeled, mut keyed) = (false, false, false);// A wheel step scrolls the open list to the new value: at once over the list, the next frame// after a step over the button.let reveal_id = wheel_id.with("reveal");@@ -1006,6 +1008,15 @@ fn dropdown_with<T: PartialEq + Clone>(ui: &mut Ui, id: &str, current: &mut T, olet response =egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).close_behavior(close).show_ui(ui, |ui| {fit_widest_option(ui, options.iter().map(|(_, l)| tl!(l)));+ // A typed letter jumps to the next option starting with it, ↩ closes the list (#1481).+ let labels: Vec<&str> = options.iter().map(|(_, l)| tl!(l)).collect();+ let shown = options.iter().position(|(v, _)| v == current);+ if let Some((v, _)) = type_ahead::keys(ui, ui.make_persistent_id(id), shown, &labels).and_then(|i| options.get(i)) {+ *current = v.clone();+ chosen.push(v.clone());+ reveal = true;+ keyed = true;+ }// Over the open list the wheel steps the value too, instead of scrolling the list.over_list = wheel && ui.rect_contains_pointer(ui.clip_rect());if over_list {@@ -1054,7 +1065,8 @@ fn dropdown_with<T: PartialEq + Clone>(ui: &mut Ui, id: &str, current: &mut T, o}chosen.extend(stepped);}- if wheel && wheel_rests(ui, wheel_id, wheeled) {+ // Nor is the option under a pointer resting where the keyboard picked another.+ if wheel_rests(ui, wheel_id, wheeled || keyed) {hovered = None;}(chosen, hovered)@@ -1193,6 +1205,12 @@ pub fn dropdown_with_tooltips<T: PartialEq + Clone>(ui: &mut Ui, id: &str, currelet response =egui::ComboBox::from_id_salt(id).selected_text(label).width(width).height(420.0).icon(chevron_icon).close_behavior(close).show_ui(ui, |ui| {fit_widest_option(ui, options.iter().map(|(_, l, _)| tl!(l)));+ let labels: Vec<&str> = options.iter().map(|(_, l, _)| tl!(l)).collect();+ let shown = options.iter().position(|(v, _, _)| v == current);+ if let Some((v, _, _)) = type_ahead::keys(ui, ui.make_persistent_id(id), shown, &labels).and_then(|i| options.get(i)) {+ *current = v.clone();+ changed = true;+ }for (v, l, tip) in options {let item = ui.selectable_label(v == current, tl!(l)).on_hover_text(tl!(tip));if press.chosen(ui, &item) {diff --git a/crates/ui-egui/src/widgets/type_ahead.rs b/crates/ui-egui/src/widgets/type_ahead.rsnew file mode 100644index 000000000..288b33b02--- /dev/null+++ b/crates/ui-egui/src/widgets/type_ahead.rs@@ -0,0 +1,178 @@+//! Keyboard choice in an open dropdown list, as in Photoshop's pop-up menus (#1481): typing a+//! letter jumps to the next option that starts with it (the same letter again cycles through+//! them), letters typed in quick succession match a longer prefix, and ↩ closes the list on the+//! option it shows.++use egui::{Id, Key, Modifiers, Ui};++/// How long a pause starts a new prefix instead of adding to the letters typed before.+const PAUSE: f64 = 1.0;++/// Where the frame number of the last frame an open dropdown list was drawn is kept.+fn open_id() -> Id {+ Id::new("pc-dropdown-list-open")+}++/// Is a dropdown list open? It then owns the keyboard, like an open menu: a letter picks an+/// option instead of a tool, ↩ closes the list instead of committing something on the canvas.+pub fn list_open(ctx: &egui::Context) -> bool {+ let pass = ctx.cumulative_pass_nr();+ ctx.data(|d| d.get_temp::<u64>(open_id())).is_some_and(|seen| seen.saturating_add(2) >= pass)+}++/// The keys of the open list of dropdown `id`, called while drawing the list, before its rows:+/// the index of the option this frame's typing picks (its text events are consumed), if any. ↩+/// closes the list. `current` is the option the list now shows; `labels` are as displayed.+pub fn keys(ui: &mut Ui, id: Id, current: Option<usize>, labels: &[&str]) -> Option<usize> {+ let pass = ui.ctx().cumulative_pass_nr();+ ui.data_mut(|d| d.insert_temp(open_id(), pass));+ let key = id.with("type-ahead");+ // A list opened anew starts with no letters typed.+ let shown_id = key.with("pass");+ ui.data_mut(|d| {+ if d.get_temp::<u64>(shown_id).is_none_or(|seen| seen.saturating_add(2) < pass) {+ d.remove::<(String, f64)>(key);+ }+ d.insert_temp(shown_id, pass);+ });+ if ui.input_mut(|i| i.consume_key(Modifiers::NONE, Key::Enter)) {+ ui.close();+ }+ let (typed, now) = ui.input_mut(|i| {+ let mut typed = String::new();+ i.events.retain(|e| match e {+ egui::Event::Text(t) => {+ typed.push_str(t);+ false+ }+ _ => true,+ });+ (typed, i.time)+ });+ if typed.is_empty() {+ return None;+ }+ let (mut prefix, last) = ui.data(|d| d.get_temp::<(String, f64)>(key)).unwrap_or_default();+ // A pause (or a clock that went backwards) starts over.+ if !(now >= last && now - last <= PAUSE) {+ prefix.clear();+ }+ let lower: Vec<String> = labels.iter().map(|l| l.to_lowercase()).collect();+ let mut pick = None;+ for c in typed.chars().filter(|c| !c.is_control()) {+ // A space only counts inside a prefix ("linear b"), never to start one.+ if c.is_whitespace() && prefix.is_empty() {+ continue;+ }+ prefix.extend(c.to_lowercase());+ let from = pick.or(current);+ let mut found = find(&lower, &prefix, from);+ // A letter that leads nowhere after the others starts a new prefix ("m" then "d" is D).+ if found.is_none() {+ prefix.clear();+ if !c.is_whitespace() {+ prefix.extend(c.to_lowercase());+ found = find(&lower, &prefix, from);+ }+ }+ pick = found.or(pick);+ }+ ui.data_mut(|d| d.insert_temp(key, (prefix, now)));+ pick+}++/// The option `prefix` (lower case) picks, searching on from `from` and wrapping. A prefix of one+/// repeated letter ("d", "dd") moves to the next option starting with that letter, so pressing it+/// again cycles through them; a longer prefix keeps the option shown while it still matches.+fn find(lower: &[String], prefix: &str, from: Option<usize>) -> Option<usize> {+ let n = lower.len();+ if n == 0 {+ return None;+ }+ let mut chars = prefix.chars();+ let first = chars.next()?;+ let (needle, skip) = if chars.all(|c| c == first) { (first.to_string(), 1) } else { (prefix.to_string(), 0) };+ // Without a shown option the search starts at the top, the first match included.+ let start = from.map_or(0, |f| f.saturating_add(skip)) % n;+ (0..n).map(|k| start.saturating_add(k) % n).find(|&i| lower.get(i).is_some_and(|l| l.starts_with(&needle)))+}++#[cfg(test)]+mod tests {+ use super::find;++ fn labels(l: &[&str]) -> Vec<String> {+ l.iter().map(|s| s.to_lowercase()).collect()+ }++ #[test]+ fn a_letter_cycles_and_a_prefix_narrows() {+ let l = labels(&["Normal", "Dissolve", "Darken", "Multiply", "Darker Color", "Lighten", "Linear Burn"]);+ assert_eq!(find(&l, "d", Some(0)), Some(1));+ assert_eq!(find(&l, "dd", Some(1)), Some(2));+ assert_eq!(find(&l, "ddd", Some(2)), Some(4));+ assert_eq!(find(&l, "dddd", Some(4)), Some(1), "wraps to the first match");+ assert_eq!(find(&l, "da", Some(1)), Some(2));+ assert_eq!(find(&l, "darke", Some(2)), Some(2), "keeps the shown option while it matches");+ assert_eq!(find(&l, "darker", Some(2)), Some(4));+ assert_eq!(find(&l, "linear b", Some(5)), Some(6));+ assert_eq!(find(&l, "x", Some(0)), None);+ assert_eq!(find(&l, "d", None), Some(1));+ assert_eq!(find(&[], "d", Some(0)), None);+ assert_eq!(find(&l, "", Some(0)), None);+ assert_eq!(find(&l, "n", Some(usize::MAX)), Some(0), "an index past the end doesn't panic");+ }++ /// Press `key` the way a keyboard does: a key event and the text it types.+ fn type_key<S>(h: &egui_kittest::Harness<'_, S>, key: egui::Key, text: &str) {+ h.key_press(key);+ h.event(egui::Event::Text(text.into()));+ }++ #[test]+ fn letters_pick_options_of_the_open_list_and_enter_closes_it() {+ use egui::accesskit::Role;+ use egui_kittest::{Harness, kittest::Queryable};++ let mut h = Harness::builder().with_size(egui::vec2(300.0, 400.0)).build_ui_state(+ |ui, selected: &mut usize| {+ let options = [(0, "Normal"), (1, "Dissolve"), (2, "Darken"), (3, "Multiply"), (4, "Darker Color")];+ crate::widgets::dropdown(ui, "blend-mode", selected, &options, 120.0);+ },+ 0,+ );+ // Typing with the list closed does nothing.+ type_key(&h, egui::Key::D, "d");+ h.run();+ assert_eq!(*h.state(), 0);+ h.get_by_role(Role::ComboBox).click();+ h.run();+ type_key(&h, egui::Key::D, "D");+ h.run();+ assert_eq!(*h.state(), 1, "D picks the first option starting with it");+ type_key(&h, egui::Key::D, "d");+ h.run();+ assert_eq!(*h.state(), 2, "D again picks the next one");+ assert!(super::list_open(&h.ctx), "the list stays open while typing");+ h.key_press(egui::Key::Enter);+ h.run();+ h.run();+ assert_eq!(*h.state(), 2, "↩ keeps the option");+ assert!(!super::list_open(&h.ctx), "↩ closes the list");+ // Opened again, the letters typed before are forgotten: M picks Multiply.+ h.get_by_role(Role::ComboBox).click();+ h.run();+ type_key(&h, egui::Key::M, "m");+ h.run();+ assert_eq!(*h.state(), 3);+ }++ #[test]+ fn case_and_scripts_beyond_ascii_match() {+ let l = labels(&["Normal", "Éclaircir", "Ébène", "Нормальный", "Затемнение"]);+ assert_eq!(find(&l, "é", Some(0)), Some(1));+ assert_eq!(find(&l, "éé", Some(1)), Some(2));+ assert_eq!(find(&l, "з", Some(0)), Some(4));+ assert_eq!(find(&l, "но", Some(4)), Some(3));+ }+}