Skip to content

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

Merge #603: document tabs shorten long names so the close button stays visible

aiongge3f444ad10a4
diff --cc crates/ui-egui/src/chrome.rs
index 3ebd2db6b,c2f1cb48e..68de574f7
--- a/crates/ui-egui/src/chrome.rs
+++ b/crates/ui-egui/src/chrome.rs
@@@ -758,113 -805,31 +784,134 @@@ mod tests
app
}
+ /// A document with a rectangle selected, so the object controls are enabled.
+ fn app_with_selection() -> crate::DesignApp {
+ let mut app = app();
+ app.run("file.new", serde_json::json!({})).unwrap();
+ app.run("frame.create", serde_json::json!({"rect": [72, 72, 300, 200]})).unwrap();
+ app.run("edit.selectAll", serde_json::json!({})).unwrap();
+ app
+ }
+
+ /// The rect of an object Control panel group.
+ fn group(h: &egui_kittest::Harness<'static, test_window::Window>, id: &str) -> egui::Rect {
+ h.ctx.read_response(egui::Id::new(("object_control", id))).unwrap().rect
+ }
+
+ /// The rect of the node with this label, preferring the one in the Control bar's row: the
+ /// Properties panel has buttons with the same names.
fn rect(h: &egui_kittest::Harness<'static, test_window::Window>, label: &str) -> egui::Rect {
- h.get_by_label(label).rect()
+ let bar = test_window::panel_rect(h, "control_bar");
+ h.query_all_by_label(label).map(|n| n.rect()).find(|r| bar.y_range().contains(r.center().y)).unwrap_or_else(|| h.get_by_label(label).rect())
+ }
+
+ /// The application bar's search field (German interface).
+ fn search_node<'h>(h: &'h egui_kittest::Harness<'static, test_window::Window>) -> egui_kittest::Node<'h> {
+ h.get_by(|n| n.role() == egui::accesskit::Role::TextInput && n.placeholder() == Some("Suchen"))
+ }
+
+ /// A document with two frames, the Selection tool, German menus, and the search field clicked.
+ fn searching() -> egui_kittest::Harness<'static, test_window::Window> {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.ui.language = "de".into();
+ let mut h = test_window::open(app, vec2(1440.0, 900.0));
+ let app = &mut h.state_mut().app;
+ app.run("frame.create", serde_json::json!({"rect": [36, 36, 200, 200]})).unwrap();
+ app.run("frame.create", serde_json::json!({"rect": [236, 36, 400, 200]})).unwrap();
+ app.run("edit.deselectAll", serde_json::json!({})).unwrap();
+ h.run_steps(4);
+ let field = search_node(&h).rect();
+ test_window::click_at(&mut h, field.center());
+ assert!(h.ctx.memory(|m| m.has_focus(super::search_id())), "a click gives the field the keyboard");
+ h
+ }
+
+ /// A match in the search's list, whose accessible label ends in its shortcut.
+ fn row<'h>(h: &'h egui_kittest::Harness<'static, test_window::Window>, label: &'static str) -> Option<egui_kittest::Node<'h>> {
+ h.query_by(move |n| n.label().is_some_and(|l| l == label || l.starts_with(&format!("{label} "))))
+ }
+
+ fn items(h: &egui_kittest::Harness<'static, test_window::Window>) -> (usize, usize) {
+ let d = h.state().app.session.active().unwrap();
+ (d.doc.spreads.iter().flat_map(|sp| &sp.items).count(), d.selection.items.len())
+ }
+
+ #[test]
+ fn the_search_field_finds_and_runs_menu_commands() {
+ let mut h = searching();
+ h.event(egui::Event::Text("gruppier".into()));
+ h.run_steps(4);
+ assert!(row(&h, "Gruppieren").is_some(), "the German label is listed");
+ assert!(row(&h, "Gruppierung aufheben").is_some());
+ // Nothing is selected, so Group is off and Return doesn't run it.
+ assert!(row(&h, "Gruppieren").is_some_and(|n| n.accesskit_node().is_disabled()));
+ h.key_press(egui::Key::Enter);
+ h.run_steps(4);
+ assert_eq!(items(&h), (2, 0), "a disabled command doesn't run");
+ // Select both frames, then ↓ ↑ Return runs the first match, as Object › Group would.
+ h.state_mut().app.run("edit.selectAll", serde_json::json!({})).unwrap();
+ h.key_press(egui::Key::ArrowDown);
+ h.run_steps(2);
+ h.key_press(egui::Key::ArrowUp);
+ h.run_steps(2);
+ h.key_press(egui::Key::Enter);
+ h.run_steps(4);
+ assert_eq!(items(&h).0, 1, "the two frames are grouped");
+ assert!(row(&h, "Gruppieren").is_none(), "the matches closed");
+ assert!(!h.ctx.memory(|m| m.has_focus(super::search_id())), "the field gave the keyboard back");
+ }
+
+ #[test]
+ fn keys_typed_in_the_search_field_stay_out_of_the_document() {
+ let mut h = searching();
+ h.state_mut().app.run("edit.selectAll", serde_json::json!({})).unwrap();
+ // Escape would switch this tool back to the Selection tool.
+ h.state_mut().app.run("tool.select", serde_json::json!({"tool": "rectangleFrame"})).unwrap();
+ h.run_steps(2);
+ // Tool keys, Delete and Backspace edit the query; the tool and the frames stay.
+ for key in [egui::Key::P, egui::Key::T, egui::Key::M, egui::Key::Delete, egui::Key::Backspace] {
+ h.key_press(key);
+ h.run_steps(2);
+ }
+ h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
+ h.run_steps(2);
+ assert_eq!(h.state().app.session.tool_id(), "rectangleFrame");
+ assert_eq!(items(&h), (2, 2), "nothing was deleted or deselected");
+ h.event(egui::Event::Text("zz-no-such-command".into()));
+ h.run_steps(4);
+ assert!(h.query_by_label("Keine passenden Befehle").is_some());
+ // Escape closes the matches and clears the field without touching the document.
+ h.key_press(egui::Key::Escape);
+ h.run_steps(4);
+ assert!(h.query_by_label("Keine passenden Befehle").is_none());
+ assert!(!h.ctx.text_edit_focused());
+ assert_eq!(h.state().app.session.tool_id(), "rectangleFrame");
+ assert_eq!(items(&h), (2, 2));
+ let field = search_node(&h);
+ assert_eq!(field.value().unwrap_or_default(), "", "the query is cleared");
}
+ /// A very long document name made its tab grow past the window, hiding the close button
+ /// and the tabs after it (#603).
+ #[test]
+ fn long_document_names_keep_tabs_short_and_close_buttons_visible() {
+ let latin = "Quarterly report with a very long file name ".repeat(6);
+ let hebrew = "דוח רבעוני עם שם קובץ ארוך מאוד ".repeat(6);
+ let mut app = app();
+ for title in [latin.as_str(), hebrew.as_str(), "Short"] {
+ app.run("file.new", serde_json::json!({ "title": title })).unwrap();
+ }
+ let h = test_window::open(app, vec2(1200.0, 600.0));
+ for title in [latin.as_str(), hebrew.as_str()] {
+ let tab = rect(&h, title);
+ assert!(tab.width() <= super::DOC_TAB_MAX_W, "{title:?} tab is {} wide", tab.width());
+ let close = rect(&h, &format!("Close {title}"));
+ assert!(tab.contains_rect(close), "close button {close:?} outside its tab {tab:?}");
+ }
+ let (first, short) = (rect(&h, &latin), rect(&h, "Short"));
+ assert!(short.min.x - first.min.x <= 2.0 * super::DOC_TAB_MAX_W, "the last tab {short:?} starts too far right");
+ }
+
#[test]
fn a_narrow_window_scrolls_the_control_bar() {
let w = 700.0;