PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Font field: ↑/↓ step through the fonts after a pick, as in Photoshop (#7407) (#7426)
Korg1cha4436074c903
diff --git a/crates/ui-egui/src/type_font_picker_tests.rs b/crates/ui-egui/src/type_font_picker_tests.rsindex 4aa25c750..6f05ed61a 100644--- a/crates/ui-egui/src/type_font_picker_tests.rs+++ b/crates/ui-egui/src/type_font_picker_tests.rs@@ -239,6 +239,88 @@ fn arrows_apply_fonts_without_closing_and_enter_accepts() {assert!(popup_rect(&h).is_none());}+/// #7407: as in Photoshop, the field keeps the focus once a font is picked (click or Enter),+/// and ↑ / ↓ on it step through the families with the menu closed. Escape ends it.+#[test]+fn arrows_on_the_field_step_through_the_families_after_a_pick() {+ let mut h = harness();+ open(&mut h);+ h.get_by_label("Family 03").click();+ h.run();+ assert_eq!(h.state().current, "Family 03");+ assert!(popup_rect(&h).is_none(), "picking closes the menu");+ let button = h.state().button;+ assert!(h.ctx.memory(|m| m.has_focus(button)), "the field keeps the focus");+ let press = |h: &mut Harness<'static, State>, key: Key| {+ h.key_press(key);+ h.run();+ assert!(popup_rect(h).is_none(), "{key:?} leaves the menu closed");+ assert!(h.ctx.memory(|m| m.has_focus(button)), "{key:?} keeps the focus on the field");+ h.state().current.clone()+ };+ assert_eq!(press(&mut h, Key::ArrowDown), "Family 04");+ assert_eq!(press(&mut h, Key::ArrowDown), "Family 05");+ assert_eq!(press(&mut h, Key::ArrowUp), "Family 04");+ // Enter in the open menu is a pick too.+ open(&mut h);+ h.key_press(Key::ArrowDown);+ h.key_press(Key::Enter);+ h.run();+ assert_eq!(h.state().current, "Family 05");+ assert_eq!(press(&mut h, Key::ArrowDown), "Family 06");+ // Escape takes the focus away; the arrows then leave the font alone.+ h.key_press(Key::Escape);+ h.run();+ assert!(!h.ctx.memory(|m| m.has_focus(button)));+ h.key_press(Key::ArrowDown);+ h.run();+ assert_eq!(h.state().current, "Family 06");+}++/// The first arrow right after a pick (in the very next frame) steps the font and keeps the+/// focus on the field, also between focusable widgets: the field's lock filter only holds from+/// the frame after it got the focus, and egui would read that arrow as a move to "Below".+#[test]+fn the_first_arrow_after_a_pick_keeps_the_focus_between_neighbours() {+ for (enter, picked, stepped) in [(false, "Family 05", "Family 06"), (true, "Family 04", "Family 05")] {+ let families: Vec<String> = (0..20).map(|i| format!("Family {i:02}")).collect();+ let state = State { current: "Family 03".into(), families, button: egui::Id::NULL, popup: egui::Id::NULL, preview: FontPreview::Off };+ let mut h = Harness::builder().with_size(vec2(600.0, 900.0)).build_ui_state(+ |ui, s: &mut State| {+ let _ = ui.button("Above");+ s.button = ui.make_persistent_id(egui::IdSalt::new("type-font"));+ s.popup = s.button.with("popup");+ let fams = s.families.clone();+ font_picker_in(ui, &mut s.current, 170.0, &fams, s.preview);+ let _ = ui.button("Below");+ },+ state,+ );+ h.run();+ open(&mut h);+ if enter {+ h.key_press(Key::ArrowDown);+ h.key_press(Key::Enter);+ } else {+ h.get_by_label("Family 05").click();+ }+ // Frame by frame up to the pick, then the arrow in the next frame.+ for _ in 0..8 {+ if h.state().current == picked && popup_rect(&h).is_none() {+ break;+ }+ h.step();+ }+ assert_eq!(h.state().current, picked, "enter: {enter}");+ h.key_press(Key::ArrowDown);+ h.step();+ h.run();+ assert_eq!(h.state().current, stepped, "enter: {enter}");+ let button = h.state().button;+ assert!(h.ctx.memory(|m| m.has_focus(button)), "enter: {enter}: the arrow moved the focus off the field");+ }+}+#[test]fn arrows_follow_the_filtered_list_and_handle_no_matches() {let mut h = harness();diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex 29882cd7d..325e5122b 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -1451,6 +1451,8 @@ pub(crate) fn family_picker_in(preview: photocraft_engine::prefs::FontPreview,) -> bool {let mut changed = false;+ // A font picked from the open menu (click or Enter): the field keeps the focus afterwards.+ let mut picked = false;let search_id = ui.id().with(("font-search", salt));let focus_id = search_id.with("focus");// The list opens on the press, so a press-drag-release picks a font too (#2735).@@ -1508,6 +1510,7 @@ pub(crate) fn family_picker_in(}}let accept = enter && rows.iter().any(|(f, _)| *f == current);+ picked |= accept;if accept {ui.data_mut(|d| d.remove::<String>(search_id));ui.close();@@ -1543,6 +1546,7 @@ pub(crate) fn family_picker_in(// A served family not fetched yet: start now, before any text needs it.photocraft_text::served::request(f);changed = true;+ picked = true;ui.data_mut(|d| d.remove::<String>(search_id));hovered = None;ui.close();@@ -1565,6 +1569,38 @@ pub(crate) fn family_picker_in(}let hovered_font = response.inner.unwrap_or_default();+ // As in Photoshop, the field keeps the keyboard focus once a font is picked, and ↑ / ↓ on the+ // focused, closed field step to the previous / next family, applying each (#7407). The lock+ // filter keeps egui from using those arrows to move the focus to another widget.+ let field = response.response.id;+ if picked {+ ui.memory_mut(|m| m.request_focus(field));+ }+ let open = egui::ComboBox::is_open(ui.ctx(), field);+ if !open && ui.is_enabled() && ui.memory(|m| m.has_focus(field)) {+ ui.memory_mut(|m| m.set_focus_lock_filter(field, egui::EventFilter { vertical_arrows: true, ..Default::default() }));+ let down = ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::ArrowDown));+ let up = ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::ArrowUp));+ if down || up {+ // The lock filter only holds from the frame after the field got the focus: an arrow+ // right after a pick was already read by egui as a focus move, applied at the end of+ // the frame. Cancel it, so the arrow only steps the font.+ ui.memory_mut(|m| m.move_focus(egui::FocusDirection::None));+ }+ // The wheel's step: a negative notch is the next family.+ let notches = f32::from(u8::from(up)) - f32::from(u8::from(down));+ let rows = font_menu_rows(families, labels, "");+ if let Some(i) = rows.iter().position(|(f, _)| *f == current)+ && let Some(n) = wheel_family_index(i, rows.len(), notches)+ && let Some((f, _)) = rows.get(n)+ && **f != *current+ {+ current.clone_from(f);+ photocraft_text::served::request(f);+ changed = true;+ }+ }+// The wheel over the closed button steps through the families, a notch each, like Photoshop's// options-bar font menu: no need to open the list (#3350).if !egui::ComboBox::is_open(ui.ctx(), response.response.id) && ui.is_enabled() {