Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge font-search: Return and Escape in the font menu stay in the menu, never reach the dialog under it

aionggedcb3d20365a
diff --cc crates/ui-egui/src/panels/mod.rs
index 2b12d6d48,581f6747e..322f359e3
--- a/crates/ui-egui/src/panels/mod.rs
+++ b/crates/ui-egui/src/panels/mod.rs
@@@ -659,90 -607,94 +684,180 @@@ mod font_menu_ui_tests
assert_eq!(story_text(&mut h, story), before);
assert!(h.state().app.session.active().unwrap().selection.text.is_some(), "the text is still selected");
}
+
+ /// The font fields showing `family`, left to right and top to bottom.
+ fn fields(h: &Harness<'static, Window>, family: &str) -> Vec<egui::Rect> {
+ let mut r: Vec<egui::Rect> =
+ h.query_all_by(|n| n.role() == egui::accesskit::Role::ComboBox && n.value().as_deref() == Some(family)).map(|n| n.rect()).collect();
+ r.sort_by(|a, b| a.min.x.total_cmp(&b.min.x).then(a.min.y.total_cmp(&b.min.y)));
+ r
+ }
+
+ /// Opens the font menu of the field at `at`, clicks its search field, types, checks the menu
+ /// stays open and lists only the matches, then picks the first with ↓ and Return.
+ fn search_in_menu(h: &mut Harness<'static, Window>, at: egui::Rect, what: &str) {
+ click_at(h, at.center());
+ let field = h.query_by(|n| n.role() == egui::accesskit::Role::TextInput && n.is_focused()).map(|n| n.rect());
+ let field = field.unwrap_or_else(|| panic!("{what}: the menu opens with the search field focused"));
+ click_at(h, field.center());
+ assert!(h.ctx.text_edit_focused(), "{what}: a click on the search field keeps the menu open");
+ h.event(egui::Event::Text("SOURCE SA".into()));
+ h.run_steps(4);
+ assert_eq!(search(h), "SOURCE SA", "{what}: the typing went to the search");
+ assert!(h.query_by_label("Source Sans 3").is_some(), "{what}: a match is listed");
+ assert!(h.query_by_label(designcraft_fonts::DEFAULT_FAMILY).is_none(), "{what}: families that don't match aren't");
+ h.key_press(egui::Key::ArrowDown);
+ h.run_steps(2);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(4);
+ assert!(!h.ctx.text_edit_focused(), "{what}: the menu closed");
+ }
+
+ /// Every font field over selected text (Control bar, Contextual Task Bar, Properties ›
+ /// Character, Character panel) searches, in the workspaces that show them and in a small
+ /// window with the macOS window chrome.
+ #[test]
+ fn every_font_field_searches_in_every_workspace() {
+ for (name, size, mac, panel) in [
+ ("Advanced", vec2(1440.0, 900.0), false, None),
+ ("Printing and Proofing", vec2(1440.0, 900.0), false, None),
+ ("Essentials", vec2(1440.0, 900.0), false, Some("character")),
+ ("Printing and Proofing", vec2(900.0, 560.0), true, None),
+ ] {
+ let (mut h, story) = typing_on(name, size, mac);
+ if panel.is_some() {
+ h.state_mut().app.ui.open_panel = panel.map(str::to_string);
+ h.run_steps(4);
+ }
+ let name = &format!("{name} {size:?} {panel:?}");
+ let before = story_text(&mut h, story);
+ let count = fields(&h, designcraft_fonts::DEFAULT_FAMILY).len();
+ assert_eq!(count, 3, "{name}: the font fields are shown");
+ for k in 0..count {
+ h.state_mut().app.run("type.char", json!({"attrs": {"fontFamily": designcraft_fonts::DEFAULT_FAMILY}})).unwrap();
+ h.run_steps(4);
+ let at = fields(&h, designcraft_fonts::DEFAULT_FAMILY)[k];
+ search_in_menu(&mut h, at, &format!("{name}, field {k}"));
+ assert_eq!(family(&mut h), "Source Sans 3", "{name}, field {k}");
+ assert_eq!(story_text(&mut h, story), before, "{name}, field {k}");
+ }
+ }
+ }
+
+ /// The font fields away from the text: the Glyphs panel, Paragraph Style Options › Basic
+ /// Character Formats and Find/Replace Font's Replace With.
+ #[test]
+ fn font_fields_in_panels_and_dialogs_search() {
+ let paragraph = json!({"name": designcraft_doc::BASIC_PARAGRAPH, "section": "chars"});
+ for (what, panel, dialog, shown) in [
+ ("Glyphs", Some("glyphs"), None, designcraft_fonts::DEFAULT_FAMILY),
+ ("Paragraph Style Options", None, Some(("paragraphStyleOptions", paragraph)), designcraft_fonts::DEFAULT_FAMILY),
+ ("Find/Replace Font", None, Some(("findFont", json!({}))), "—"),
+ ] {
+ let app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let mut h = test_window::open(app, vec2(1440.0, 900.0));
+ let dialog_id = dialog.as_ref().map(|(id, _)| id.to_string());
+ h.state_mut().app.ui.open_panel = panel.map(str::to_string);
+ h.state_mut().app.ui.dialog = dialog.map(|(id, fields)| crate::dialogs::Dialog::new(id, fields));
+ h.run_steps(4);
+ let at = *fields(&h, shown).first().unwrap_or_else(|| panic!("{what}: the font field is shown"));
+ search_in_menu(&mut h, at, what);
+ assert_eq!(h.state().app.ui.dialog.as_ref().map(|d| d.id.clone()), dialog_id, "{what}: Return in the menu leaves the dialog open");
+ assert!(!fields(&h, "Source Sans 3").is_empty(), "{what}: the field shows the family picked");
+ // Escape closes the menu only.
+ let at = fields(&h, "Source Sans 3")[0];
+ click_at(&mut h, at.center());
+ assert!(h.ctx.text_edit_focused(), "{what}: the menu opened");
+ h.key_press(egui::Key::Escape);
+ h.run_steps(4);
+ assert!(!h.ctx.text_edit_focused(), "{what}: Escape closed the menu");
+ assert_eq!(h.state().app.ui.dialog.as_ref().map(|d| d.id.clone()), dialog_id, "{what}: Escape in the menu leaves the dialog open");
+ }
+ }
}
+
+#[cfg(test)]
+mod swatch_list_tests {
+ use super::*;
+
+ fn rects(shapes: &[egui::epaint::ClippedShape]) -> Vec<(egui::Rect, egui::Color32)> {
+ fn walk(s: &egui::Shape, out: &mut Vec<(egui::Rect, egui::Color32)>) {
+ match s {
+ egui::Shape::Rect(r) => out.push((r.rect, r.fill)),
+ egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, out)),
+ _ => {}
+ }
+ }
+ let mut out = vec![];
+ shapes.iter().for_each(|s| walk(&s.shape, &mut out));
+ out
+ }
+
+ fn texts(shapes: &[egui::epaint::ClippedShape]) -> Vec<(String, egui::Rect)> {
+ fn walk(s: &egui::Shape, out: &mut Vec<(String, egui::Rect)>) {
+ match s {
+ egui::Shape::Text(t) => out.push((t.galley.job.text.clone(), t.visual_bounding_rect())),
+ egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, out)),
+ _ => {}
+ }
+ }
+ let mut out = vec![];
+ shapes.iter().for_each(|s| walk(&s.shape, &mut out));
+ out
+ }
+
+ #[test]
+ fn the_swatch_under_the_pointer_is_highlighted_and_names_clear_the_scroll_bar() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", serde_json::json!({})).unwrap();
+ // A long name, as process swatches have.
+ app.run("swatch.create", serde_json::json!({"name": "C=15 M=100 Y=100 K=0 Long Name", "color": {"c": 15, "m": 100, "y": 100, "k": 0}}))
+ .unwrap();
+ let long = "C=15 M=100 Y=100 K=0 Long Name";
+ let ctx = egui::Context::default();
+ crate::theme::install_fonts(&ctx, "");
+ let tokens = Tokens::for_brightness(crate::theme::Brightness::MediumDark);
+ crate::theme::apply(&ctx, &tokens);
+ let mut time = 0.0;
+ let mut frame = |app: &mut DesignApp, events: Vec<egui::Event>| {
+ time += 0.1;
+ let raw = egui::RawInput {
+ screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 700.0))),
+ time: Some(time),
+ events,
+ ..Default::default()
+ };
+ let mut out = ctx.run_ui(raw, |ui| swatch_picker(app, ui, "list", Some("[Black]".into()), |_, _| {}));
+ out.textures_delta.clear();
+ out.shapes
+ };
+ frame(&mut app, vec![]);
+ let chip = egui::pos2(17.0, 17.0);
+ for pressed in [true, false] {
+ frame(
+ &mut app,
+ vec![
+ egui::Event::PointerMoved(chip),
+ egui::Event::PointerButton { pos: chip, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() },
+ ],
+ );
+ }
+ let shapes = frame(&mut app, vec![]);
+ let (_, name) = texts(&shapes).into_iter().find(|(t, _)| t == "[Registration]").expect("the list is open");
+ let pointer = name.center();
+ frame(&mut app, vec![egui::Event::PointerMoved(pointer)]);
+ let shapes = frame(&mut app, vec![]);
+ let all = rects(&shapes);
+ let (row, _) = *all.iter().find(|(r, c)| *c == tokens.accent_strong && r.contains(pointer)).expect("the hovered row is highlighted");
+ let (list, _) = *all
+ .iter()
+ .filter(|(r, c)| *c == tokens.panel && r.contains_rect(row))
+ .min_by(|a, b| a.0.area().total_cmp(&b.0.area()))
+ .expect("the popup");
+ let scroll = ctx.global_style().spacing.scroll;
+ assert!(row.right() + scroll.bar_width <= list.right(), "rows {row:?} stop short of the scroll bar in {list:?}");
+ let (_, long_rect) = texts(&shapes).into_iter().find(|(t, _)| t == long).unwrap();
+ assert!(long_rect.right() <= row.right(), "the longest name {long_rect:?} fits its row {row:?}");
+ assert!(texts(&shapes).iter().any(|(t, r)| t == "[Black]" && r.right() <= row.right()));
+ }
+}