PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Font menu keeps its height and focuses its search field (#1369) (#1505)
David Mann62fd1342dd9a
diff --git a/crates/ui-egui/src/type_font_picker_tests.rs b/crates/ui-egui/src/type_font_picker_tests.rsnew file mode 100644index 000000000..117a9aca3--- /dev/null+++ b/crates/ui-egui/src/type_font_picker_tests.rs@@ -0,0 +1,91 @@+//! The Type tool's searchable font menu (#1369): it keeps its height and focuses its search field.++use super::font_picker_in;+use egui::accesskit::Role;+use egui::{Key, Modifiers, Rect, vec2};+use egui_kittest::Harness;+use egui_kittest::kittest::Queryable;++struct State {+ current: String,+ families: Vec<String>,+ popup: egui::Id,+}++fn harness() -> 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 };+ let mut h = Harness::builder().with_size(vec2(600.0, 900.0)).build_ui_state(+ |ui, s: &mut State| {+ // `ComboBox::from_id_salt` wraps its salt in an `IdSalt`; the popup id is the button's + "popup".+ s.popup = ui.make_persistent_id(egui::IdSalt::new("type-font")).with("popup");+ let fams = s.families.clone();+ font_picker_in(ui, &mut s.current, 170.0, &fams);+ },+ state,+ );+ h.run();+ h+}++fn popup_rect(h: &Harness<'static, State>) -> Option<Rect> {+ let id = h.state().popup;+ let open = egui::Popup::is_id_open(&h.ctx, id);+ if open { h.ctx.memory(|m| m.area_rect(id)) } else { None }+}++fn open(h: &mut Harness<'static, State>) {+ h.get_by_role(Role::ComboBox).click();+ h.run();+}++#[test]+fn clearing_the_query_restores_the_menu_height() {+ let mut h = harness();+ open(&mut h);+ let full = popup_rect(&h).expect("menu open").height();+ h.get_by_role(Role::TextInput).type_text("zeta");+ h.run();+ assert!(h.query_by_label("Family 01").is_none(), "the query filters the list");+ let searching = popup_rect(&h).expect("still open").height();+ for _ in 0.."zeta".len() {+ h.key_press(Key::Backspace);+ }+ h.run();+ assert!(h.query_by_label("Family 01").is_some(), "the cleared query lists every family");+ let cleared = popup_rect(&h).expect("still open").height();+ assert!((cleared - full).abs() < 0.5, "height after clearing {cleared} != original {full}");+ assert!((searching - full).abs() < 0.5, "the menu keeps its height while searching: {searching} vs {full}");+}++#[test]+fn reopening_focuses_the_search_field_and_clicking_it_keeps_the_menu_open() {+ let mut h = harness();+ open(&mut h);+ 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.run();+ assert!(popup_rect(&h).is_none(), "Escape closes the menu");+ open(&mut h);+ 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");+ // Clicking the search field (to edit the query) must not close the menu.+ h.get_by_role(Role::TextInput).click();+ h.run();+ assert!(popup_rect(&h).is_some(), "clicking the search field keeps the menu open");+ h.key_press_modifiers(Modifiers::COMMAND, Key::A);+ h.get_by_role(Role::TextInput).type_text("Zeta S");+ h.run();+ assert!(popup_rect(&h).is_some());+ // Picking a font still closes the menu.+ h.get_by_label("Zeta Sans").click();+ h.run();+ assert_eq!(h.state().current, "Zeta Sans");+ assert!(popup_rect(&h).is_none(), "choosing a font closes the menu");+}diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex 25555e9ea..e0c9f9667 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -726,23 +726,62 @@ pub fn style_label(style: &str) -> String {/// Searchable font-family combo box.fn font_picker(ui: &mut egui::Ui, current: &mut String, width: f32) -> bool {+ font_picker_in(ui, current, width, families())+}++/// Maximum height of the font menu.+const FONT_MENU_HEIGHT: f32 = 460.0;++/// [`font_picker`] over a given family list (tests pass their own).+fn font_picker_in(ui: &mut egui::Ui, current: &mut String, width: f32, families: &[String]) -> bool {let mut changed = false;let search_id = ui.id().with("font-search");- egui::ComboBox::from_id_salt("type-font").selected_text(current.as_str()).width(width).height(460.0).icon(crate::widgets::chevron_icon).show_ui(ui, |ui| {+ let combo = egui::ComboBox::from_id_salt("type-font")+ .selected_text(current.as_str())+ .width(width)+ .height(FONT_MENU_HEIGHT)+ .icon(crate::widgets::chevron_icon)+ // Clicks inside the menu (the search field, the scroll bar) must not close it (#1369);+ // picking a font closes it explicitly below.+ .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside);+ 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 mut q: String = ui.data(|d| d.get_temp(search_id)).unwrap_or_default();let r = ui.add(egui::TextEdit::singleline(&mut q).hint_text(tl!("Search fonts")).desired_width(200.0));- if !r.has_focus() && q.is_empty() {+ if r.has_focus() {+ focus = false;+ } else if focus {r.request_focus();}- ui.data_mut(|d| d.insert_temp(search_id, q.clone()));+ ui.data_mut(|d| {+ d.insert_temp(pass_id, pass);+ d.insert_temp(focus_id, focus);+ d.insert_temp(search_id, q.clone());+ });let ql = q.to_lowercase();- for f in families().iter().filter(|f| ql.is_empty() || f.to_lowercase().contains(&ql)) {+ for f in families.iter().filter(|f| ql.is_empty() || f.to_lowercase().contains(&ql)) {if ui.selectable_label(f == current, f).clicked() {*current = f.clone();changed = true;ui.data_mut(|d| d.remove::<String>(search_id));+ ui.close();}}+ // egui measures a popup only when it opens and never lets it grow back, so a filtered+ // list would leave the menu short after the query is cleared (#1369). Keep the unfiltered+ // height while searching.+ if ql.is_empty() {+ let h = ui.min_rect().height();+ ui.data_mut(|d| d.insert_temp(height_id, h));+ } else if let Some(h) = ui.data(|d| d.get_temp::<f32>(height_id)) {+ ui.set_min_height(h.min(FONT_MENU_HEIGHT));+ }});changed}@@ -1393,6 +1432,10 @@ pub fn cancel(app: &mut PhotocraftApp) {#[path = "type_tool_tests.rs"]mod canvas_tests;+#[cfg(test)]+#[path = "type_font_picker_tests.rs"]+mod font_picker_tests;+#[cfg(test)]mod tests {use super::*;