Skip to content

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

Focus font search when opening the picker (#7222)

Carter Smithe9036c72c656
diff --git a/crates/ui-egui/src/type_font_picker_tests.rs b/crates/ui-egui/src/type_font_picker_tests.rs
index 176e683a9..3dfe34ded 100644
--- a/crates/ui-egui/src/type_font_picker_tests.rs
+++ b/crates/ui-egui/src/type_font_picker_tests.rs
@@ -10,6 +10,7 @@ use photocraft_engine::prefs::FontPreview;
struct State {
current: String,
families: Vec<String>,
+ button: egui::Id,
popup: egui::Id,
preview: FontPreview,
}
@@ -22,10 +23,11 @@ fn harness_with(preview: FontPreview) -> Harness<'static, State> {
// Enough families that the unfiltered menu hits its maximum height and scrolls.
let mut families: Vec<String> = (0..60).map(|i| format!("Family {i:02}")).collect();
families.push("Zeta Sans".into());
- let state = State { current: "Family 00".into(), families, popup: egui::Id::NULL, preview };
+ let state = State { current: "Family 00".into(), families, button: egui::Id::NULL, popup: egui::Id::NULL, preview };
let mut h = Harness::builder().with_size(vec2(600.0, 900.0)).build_ui_state(
|ui, s: &mut State| {
- s.popup = ui.make_persistent_id(egui::IdSalt::new("type-font")).with("popup");
+ 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);
},
@@ -46,6 +48,16 @@ fn open(h: &mut Harness<'static, State>) {
h.run();
}
+/// A native click can leave the opening button held across several rendered frames.
+fn hold_open<S>(h: &mut Harness<'_, S>, button: egui::Pos2) {
+ h.hover_at(button);
+ h.run();
+ h.event(egui::Event::PointerButton { pos: button, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
+ h.run_steps(4);
+ h.event(egui::Event::PointerButton { pos: button, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
+ h.run();
+}
+
#[test]
fn hovering_a_font_row_sets_preview_and_leaving_clears_it() {
let mut h = harness();
@@ -93,15 +105,18 @@ fn clearing_the_query_restores_the_menu_height() {
#[test]
fn reopening_focuses_the_search_field_and_clicking_it_keeps_the_menu_open() {
let mut h = harness();
- open(&mut h);
+ let button = h.query_all_by_role(Role::ComboBox).next().unwrap().rect().center();
+ hold_open(&mut h, button);
assert!(h.get_by_role(Role::TextInput).is_focused(), "search is focused on first open");
- h.get_by_role(Role::TextInput).type_text("zeta");
- h.run();
- // Close without choosing, then reopen: the query stays and the field is focused again.
- h.key_press(Key::Escape);
+ h.event(egui::Event::Text("zeta".into()));
h.run();
+ assert_eq!(h.get_by_role(Role::TextInput).value().as_deref(), Some("zeta"), "typing goes straight to the search field");
+ // Close and reopen on consecutive passes: the query stays and the field is focused again.
+ h.input_mut().events.push(egui::Event::Key { key: Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
+ h.step();
assert!(popup_rect(&h).is_none(), "Escape closes the menu");
- open(&mut h);
+ h.query_all_by_role(Role::ComboBox).next().unwrap().click_accesskit();
+ h.run();
let search = h.get_by_role(Role::TextInput);
assert_eq!(search.value().as_deref(), Some("zeta"));
assert!(search.is_focused(), "search is focused on reopen with a query");
@@ -120,6 +135,64 @@ fn reopening_focuses_the_search_field_and_clicking_it_keeps_the_menu_open() {
assert!(popup_rect(&h).is_none(), "choosing a font closes the menu");
}
+#[test]
+fn opening_click_release_returns_focus_to_search_before_typing() {
+ let mut h = harness();
+ let button = h.query_all_by_role(Role::ComboBox).next().unwrap().rect().center();
+ h.hover_at(button);
+ h.run();
+ h.event(egui::Event::PointerButton { pos: button, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
+ h.run_steps(4);
+ assert!(h.get_by_role(Role::TextInput).is_focused());
+ // A native opening click can hand focus back to its button when it ends, after the
+ // search has already been focused for several passes. Model that platform handoff.
+ let id = h.state().button;
+ h.ctx.memory_mut(|m| m.request_focus(id));
+ h.event(egui::Event::PointerButton { pos: button, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
+ h.run();
+ assert!(h.get_by_role(Role::TextInput).is_focused(), "the opening release leaves the search focused");
+ h.event(egui::Event::Text("zeta".into()));
+ h.run();
+ assert_eq!(h.get_by_role(Role::TextInput).value().as_deref(), Some("zeta"));
+ assert!(h.query_by_label("Family 01").is_none());
+}
+
+#[test]
+fn first_options_bar_open_searches_without_changing_the_tool_or_edited_text() {
+ use crate::{PhotocraftApp, Services};
+ use photocraft_doc::LayerContent;
+ use serde_json::json;
+
+ for editing in [false, true] {
+ let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).with_max_steps(64).build_eframe(move |cc| {
+ PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default());
+ app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
+ app.run("type.create", json!({"text": "Sample", "font": "Inter", "size": 18})).unwrap();
+ app.ui.tool = crate::state::Tool::Type;
+ if editing {
+ super::edit_active(&mut app).unwrap();
+ }
+ app.sync_views();
+ app
+ });
+ h.run_steps(8);
+ let button = h.query_all_by_role(Role::ComboBox).find(|n| n.value().as_deref() == Some("Inter")).unwrap().rect().center();
+ hold_open(&mut h, button);
+ // Native typing carries key events as well as text; an unfocused search would let
+ // the first key switch tools, or replace the selected inline text instead.
+ h.event(egui::Event::Key { key: Key::J, physical_key: Some(Key::J), pressed: true, repeat: false, modifiers: Modifiers::NONE });
+ h.event(egui::Event::Text("JetBrains".into()));
+ h.run();
+ let search = h.query_all_by_role(Role::TextInput).find(|n| n.is_focused()).expect("font search focused on first open");
+ assert_eq!(search.value().as_deref(), Some("JetBrains"));
+ assert_eq!(h.state().ui.tool, crate::state::Tool::Type);
+ let st = h.state().session.active().unwrap();
+ let LayerContent::Text(text) = &st.doc.layer(st.active_layer.unwrap()).unwrap().content else { panic!("type layer") };
+ assert_eq!(text.text, "Sample");
+ }
+}
+
#[test]
fn arrows_apply_fonts_without_closing_and_enter_accepts() {
let mut h = harness();
@@ -411,6 +484,7 @@ fn localized_font_names_label_the_menu_but_pick_the_family() {
let state = State {
current: "Arial".into(),
families: vec!["Arial".into(), "MS Gothic".into(), "Zeta Sans".into()],
+ button: egui::Id::NULL,
popup: egui::Id::NULL,
preview: FontPreview::Medium,
};
@@ -519,8 +593,8 @@ fn options_bar_hover_previews_with_the_character_picker_also_shown() {
};
let pickers: Vec<_> = h.query_all_by_role(Role::ComboBox).filter(|n| n.value().as_deref() == Some("Inter")).collect();
assert_eq!(pickers.len(), 2, "options bar and Character section both show the font picker");
- pickers[0].click();
- h.run();
+ let button = pickers[0].rect().center();
+ hold_open(&mut h, button);
h.query_all_by_role(Role::TextInput).find(|n| n.is_focused()).expect("search field focused").type_text("JetBrains Mono");
h.run();
h.get_by_label("JetBrains Mono").hover();
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index a8125201d..68c33b78c 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -1447,6 +1447,7 @@ pub(crate) fn family_picker_in(
) -> bool {
let mut changed = 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).
let (press, close) = crate::press_menu::PressCombo::before(ui, salt);
let labels: Option<Arc<HashMap<String, String>>> = ui.ctx().data(|d| d.get_temp(font_labels_id()));
@@ -1462,26 +1463,25 @@ pub(crate) fn family_picker_in(
.close_behavior(if matches!(close, egui::PopupCloseBehavior::IgnoreClicks) { close } else { egui::PopupCloseBehavior::CloseOnClickOutside });
let response = combo.show_ui(ui, |ui| {
- let (pass_id, focus_id, height_id) = (search_id.with("pass"), search_id.with("focus"), search_id.with("height"));
- let pass = ui.ctx().cumulative_pass_nr();
- let last_pass: Option<u64> = ui.data(|d| d.get_temp(pass_id));
- // Freshly opened (not drawn last pass): focus the search field whatever the query, and keep
- // asking until it has focus (the first pass is egui's invisible sizing pass) (#1369).
- let opened = last_pass.is_none_or(|p| p.saturating_add(1) < pass);
- let mut focus = opened || ui.data(|d| d.get_temp(focus_id)).unwrap_or(false);
+ let height_id = search_id.with("height");
+ // Each opening focuses the search, including a reopen on the very next pass. Keep
+ // asking through the invisible sizing pass, until the field receives focus.
+ let mut focus = ui.data(|d| d.get_temp(focus_id)).unwrap_or(true);
let mut q: String = ui.data(|d| d.get_temp(search_id)).unwrap_or_default();
// Consume these before TextEdit and the canvas can interpret them as caret movement.
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));
let enter = ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));
- let r = ui.add(egui::TextEdit::singleline(&mut q).hint_text(tl!("Search fonts")).desired_width(200.0));
- if r.has_focus() {
- focus = false;
- } else if focus {
- r.request_focus();
+ if focus {
+ ui.memory_mut(|m| m.request_focus(search_id.with("input")));
+ }
+ let r = ui.add(egui::TextEdit::singleline(&mut q).id(search_id.with("input")).hint_text(tl!("Search fonts")).desired_width(200.0));
+ if focus {
+ // The opening click can release after several focused frames. Keep the request
+ // pending through that release, which can hand focus back to the combo button.
+ focus = !r.has_focus() || ui.input(|i| i.pointer.primary_down() || i.pointer.primary_released());
}
ui.data_mut(|d| {
- d.insert_temp(pass_id, pass);
d.insert_temp(focus_id, focus);
d.insert_temp(search_id, q.clone());
});
@@ -1551,6 +1551,9 @@ pub(crate) fn family_picker_in(
if accept { None } else { hovered }
});
press.after(&response.response);
+ if !egui::ComboBox::is_open(ui.ctx(), response.response.id) {
+ ui.data_mut(|d| d.remove::<bool>(focus_id));
+ }
let hovered_font = response.inner.unwrap_or_default();
// The wheel over the closed button steps through the families, a notch each, like Photoshop's