DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge storytold/main into PR #2055
Brandon Thomas3372e3f3cf34
diff --cc crates/ui-egui/src/dialogs.rsindex a3448e23a,f8532d9cc..b8e149eb3--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@@ -3566,20 -4056,18 +4077,22 @@@ fn format_settings(app: &mut DesignAppd.fields.remove(FORMAT_EDIT);return;};- let mut fd =- Dialog { id: "changeFormat".into(), fields: d.fields.get(FORMAT_EDIT).and_then(Value::as_object).cloned().unwrap_or_default(), unit: d.unit };+ let mut fd = Dialog {+ id: "changeFormat".into(),+ fields: d.fields.get(FORMAT_EDIT).and_then(Value::as_object).cloned().unwrap_or_default(),+ unit: d.unit,+ vertical_unit: None,+ };let base = json!({});let cf = CharFields { base: &base, sparse: true };+ let find = fd.s("for") == "find";let popup_open = egui::Popup::is_any_open(ctx);let mut result = None;- let host = egui::Id::unique("change_format_settings");+ let host = egui::Id::unique("format_settings");crate::modal_drag::modal(ctx, host, host).backdrop_color(egui::Color32::TRANSPARENT).show(ctx, |ui| {let title_top = ui.cursor().top();- crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&lang, "Change Format Settings")).font(semibold(16.0)));+ let title = if find { "Find Format Settings" } else { "Change Format Settings" };+ crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&lang, title)).font(semibold(16.0)));let margin = egui::Frame::popup(ui.style()).inner_margin;let title_row = egui::Rect::from_min_max(egui::pos2(ui.max_rect().left() - margin.leftf(), title_top - margin.topf()),diff --cc crates/ui-egui/src/dialogs/tests.rsindex 98b0f7044,b253b1bc9..b77774570--- a/crates/ui-egui/src/dialogs/tests.rs+++ b/crates/ui-egui/src/dialogs/tests.rs@@@ -4337,85 -4679,138 +4679,221 @@@ fn on_a_page_without_a_section_the_windassert!(!labels.iter().any(|(t, _, _)| t == "New Section"));}+ /// Find/Change's Change and Change/Find, as InDesign's: Find Next selects a match, Change/Find+ /// changes it and selects the next, Change changes that one and keeps it selected. With no+ /// text selected they're off.+ #[test]+ fn change_and_change_find_change_the_found_text() {+ use egui_kittest::Harness;+ use egui_kittest::kittest::{NodeT, Queryable};+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let f = app.run("frame.create", json!({"rect": [36, 36, 400, 300], "content": "text", "text": "red red | red"})).unwrap();+ let sid = designcraft_doc::StoryId(f["story"].as_u64().unwrap());+ app.run("text.select", json!({"story": sid.0, "anchor": 0, "focus": 0})).unwrap();+ app.run("app.findChange", json!({})).unwrap();+ if let Some(d) = app.ui.dialog.as_mut() {+ d.fields.insert("find".into(), json!("red"));+ d.fields.insert("change".into(), json!("blue"));+ }+ let mut h = Harness::builder().with_size(egui::vec2(1200.0, 900.0)).build_ui_state(+ |ui, app: &mut DesignApp| {+ let key = egui::Id::unique("change_find_test_fonts");+ if ui.data(|d| d.get_temp::<bool>(key)).is_none() {+ crate::theme::install_fonts(ui.ctx(), "");+ ui.data_mut(|d| d.insert_temp(key, true));+ return;+ }+ show(app, ui.ctx());+ },+ app,+ );+ h.run_steps(4);+ assert!(h.query_by_value("Document").is_some(), "the Search choices are InDesign's");+ assert!(h.get_by_label("Change").accesskit_node().is_disabled(), "nothing found yet");+ let text = |h: &Harness<'_, DesignApp>| h.state().session.doc().unwrap().doc.story(sid).unwrap().text.clone();+ let selected = |h: &Harness<'_, DesignApp>| h.state().session.doc().unwrap().selection.text.unwrap().range();+ h.get_by_label("Find Next").click();+ h.run_steps(3);+ assert_eq!(selected(&h), 0..3);+ h.get_by_label("Change/Find").click();+ h.run_steps(3);+ assert_eq!((text(&h), selected(&h)), ("blue red | red".to_string(), 5..8));+ h.get_by_label("Change").click();+ h.run_steps(3);+ assert_eq!((text(&h), selected(&h)), ("blue blue | red".to_string(), 5..9));+ assert_eq!(h.state().ui.dialog.as_ref().unwrap().fields["status"], json!("1 replacement(s) made"));+ }++ /// Insert Table: the dimensions as typed, and the Table Style chosen is applied to the new+ /// table; [Basic Table] (the default) leaves it unstyled.+ #[test]+ fn insert_table_applies_its_dimensions_and_table_style() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("style.table.create", json!({"name": "Grid"})).unwrap();+ for (style, expected) in [("Grid", "Grid"), (designcraft_doc::BASIC_TABLE, "")] {+ app.run("frame.create", json!({"rect": [72, 72, 372, 372], "content": "text"})).unwrap();+ assert_eq!(app.run("app.insertTableDialog", json!({})).unwrap(), Value::Null);+ for (field, value) in [("bodyRows", json!(5)), ("columns", json!(3)), ("headerRows", json!(1)), ("tableStyle", json!(style))] {+ ctl(&mut app, "ui.dialog.set", json!({"field": field, "value": value}));+ }+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ let t = app.run("table.get", json!({})).unwrap();+ let len = |k: &str| t[k].as_array().map_or(0, Vec::len);+ assert_eq!((len("rows"), len("columns"), t["headerRows"].as_u64()), (6, 3, Some(1)), "{t}");+ assert_eq!(t["style"].as_str().unwrap_or(""), expected, "{t}");+ // One Insert Table, one undo.+ app.run("edit.undo", json!({})).unwrap();+ assert!(app.run("table.get", json!({})).is_err(), "the table is gone");+ }+ // A null style is no style: the table is made, unstyled.+ app.run("frame.create", json!({"rect": [72, 72, 372, 372], "content": "text"})).unwrap();+ app.run("app.insertTableDialog", json!({})).unwrap();+ ctl(&mut app, "ui.dialog.set", json!({"field": "tableStyle", "value": null}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ assert_eq!(app.run("table.get", json!({})).unwrap()["style"].as_str().unwrap_or(""), "");+ app.run("edit.undo", json!({})).unwrap();+ // A style that no longer exists changes nothing.+ let tables = |app: &DesignApp| app.session.active().unwrap().doc.stories.values().map(|s| s.tables.len()).sum::<usize>();+ app.run("frame.create", json!({"rect": [72, 72, 372, 372], "content": "text"})).unwrap();+ app.run("app.insertTableDialog", json!({})).unwrap();+ ctl(&mut app, "ui.dialog.set", json!({"field": "tableStyle", "value": "Gone"}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], false);+ assert_eq!(tables(&app), 0);+ let d = app.ui.dialog.as_ref().expect("the dialog stays open to be corrected");+ assert_eq!((d.id.as_str(), d.s("tableStyle")), ("insertTable", "Gone".to_string()));+ // So does a style that isn't a name: the command refuses it, rather than making the table+ // unstyled.+ ctl(&mut app, "ui.dialog.set", json!({"field": "tableStyle", "value": 42}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], false);+ assert_eq!(tables(&app), 0);+ assert_eq!(app.ui.dialog.as_ref().map(|d| d.id.as_str()), Some("insertTable"), "the dialog stays open");+ }++ /// Convert Text to Table: available with text selected (not with a caret alone); its+ /// separators and Number of Columns shape the table. Convert Table to Text takes it back with+ /// the separators chosen there.+ #[test]+ fn convert_dialogs_use_their_separators() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let text = "Name:\nAddress:\nCity:\nState:";+ let sid = app.run("frame.create", json!({"rect": [72, 72, 372, 372], "content": "text", "text": text})).unwrap()["story"].clone();+ app.run("text.select", json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();+ assert!(!crate::menus::menu_enabled(&app, "app.convertTextToTableDialog"), "a caret alone");+ assert!(!crate::menus::menu_enabled(&app, "app.convertTableToTextDialog"), "no table");+ app.run("text.select", json!({"story": sid, "anchor": 0, "focus": text.len()})).unwrap();+ assert!(crate::menus::menu_enabled(&app, "app.convertTextToTableDialog"));+ app.run("app.convertTextToTableDialog", json!({})).unwrap();+ assert_eq!(app.ui.dialog.as_ref().map(|d| d.id.as_str()), Some("convertTextToTable"));+ // "comma" and a typed "," are the same separator, so Number of Columns applies; a count of+ // 0 is refused, not taken as 1, and the dialog stays open.+ ctl(&mut app, "ui.dialog.set", json!({"field": "columnSeparator", "value": "comma"}));+ ctl(&mut app, "ui.dialog.set", json!({"field": "rowSeparator", "value": ","}));+ assert!(same_separators(app.ui.dialog.as_ref().unwrap()));+ ctl(&mut app, "ui.dialog.set", json!({"field": "columns", "value": 0}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], false);+ assert_eq!(app.ui.dialog.as_ref().map(|d| d.id.as_str()), Some("convertTextToTable"));+ // An emptied separator: the command refuses it and the dialog stays open, as typed.+ ctl(&mut app, "ui.dialog.set", json!({"field": "rowSeparator", "value": ""}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], false);+ assert_eq!(app.ui.dialog.as_ref().map(|d| (d.id.as_str(), d.s("rowSeparator"))), Some(("convertTextToTable", String::new())));+ for (field, value) in [("columnSeparator", json!("paragraph")), ("rowSeparator", json!("paragraph")), ("columns", json!(2))] {+ ctl(&mut app, "ui.dialog.set", json!({"field": field, "value": value}));+ }+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ let t = app.run("table.get", json!({})).unwrap();+ let len = |k: &str| t[k].as_array().map_or(0, Vec::len);+ assert_eq!((len("rows"), len("columns")), (2, 2), "{t}");+ assert!(crate::menus::menu_enabled(&app, "app.convertTableToTextDialog"));+ app.run("app.convertTableToTextDialog", json!({})).unwrap();+ ctl(&mut app, "ui.dialog.set", json!({"field": "columnSeparator", "value": ", "}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ let story = app.session.active().unwrap().doc.story(designcraft_doc::StoryId(sid.as_u64().unwrap())).unwrap().text.clone();+ // (The empty paragraph after a table that ends its story stays after the text.)+ assert_eq!(story.trim_end(), "Name:, Address:\nCity:, State:");+ }+++/// The Rectangle dialog a frame-tool click opens shows the width in the horizontal ruler unit and+/// the height in the vertical one, not always picas, and reads bare numbers the same way (#1964).+#[test]+fn frame_size_dialog_follows_the_ruler_units() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("document.preferences", json!({"horizontalUnits": "inches", "verticalUnits": "millimeters"})).unwrap();+ // As the frame tool asks: a click at (36, 36), 72 pt by 72 pt.+ app.session.ui_requests.push(designcraft_engine::UiRequest::Dialog {+ id: "frameSize".into(),+ params: json!({"spread": 0, "shape": "rectangle", "content": "graphic", "x": 36.0, "y": 36.0, "width": 72.0, "height": 72.0}),+ });+ app.after_engine();+ let d = app.ui.dialog.as_ref().expect("the Rectangle dialog");+ assert_eq!((d.id.as_str(), d.unit), ("frameSize", Unit::Inches));+ assert_eq!(d.s("width"), format_measure(72.0, Unit::Inches));+ assert_eq!(d.s("height"), format_measure(72.0, Unit::Millimeters));+ assert!(!d.s("width").contains('p') && !d.s("height").contains('p'), "no picas: {} x {}", d.s("width"), d.s("height"));+ // Bare numbers typed in each field are in that field's unit: 2 in by 50 mm.+ let d = app.ui.dialog.as_mut().unwrap();+ d.fields.insert("width".into(), json!("2"));+ d.fields.insert("height".into(), json!("50"));+ let before = app.session.doc().unwrap().doc.clone();+ confirm(&mut app).unwrap();+ let after = &app.session.doc().unwrap().doc;+ let added: Vec<_> = after.spreads[0].items.iter().filter(|i| !before.spreads[0].items.iter().any(|b| b.id == i.id)).collect();+ assert_eq!(added.len(), 1, "one frame");+ let b = added[0].bounds();+ assert!((b.width() - 144.0).abs() < 0.01 && (b.height() - 50.0 / 25.4 * 72.0).abs() < 0.01, "{b:?}");+ // The properties panel's link opens it in the same units.+ let d = frame_size_dialog(&app, json!({"width": 36.0, "height": 36.0}));+ assert_eq!((d.s("width"), d.s("height")), (format_measure(36.0, Unit::Inches), format_measure(36.0, Unit::Millimeters)));+ // Every ruler unit: the shown size reads back as the size, so OK leaves a 72 pt frame 72 pt.+ let units = serde_json::to_value(Unit::ALL).unwrap();+ for (unit, name) in Unit::ALL.iter().zip(units.as_array().unwrap()) {+ app.run("document.preferences", json!({"horizontalUnits": name, "verticalUnits": name})).unwrap();+ let d = frame_size_dialog(&app, json!({"width": 72.0, "height": 145.0}));+ assert_eq!(d.unit, *unit);+ for (k, pt) in [("width", 72.0), ("height", 145.0)] {+ let back = d.m_in(k, *unit).unwrap_or(f64::NAN);+ assert!((back - pt).abs() < 0.05, "{name}: {k} shows {:?}, reads back {back}", d.s(k));+ }+ }+}++/// #2055 review: the Rectangle dialog reads the height in the vertical unit it showed when it+/// opened. Rulers switched while it is open (the control channel) leave a typed 50 at 50 mm.+#[test]+fn frame_size_dialog_keeps_its_units_while_open() {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("document.preferences", json!({"horizontalUnits": "millimeters", "verticalUnits": "millimeters"})).unwrap();+ app.session.ui_requests.push(designcraft_engine::UiRequest::Dialog {+ id: "frameSize".into(),+ params: json!({"spread": 0, "shape": "rectangle", "content": "graphic", "x": 36.0, "y": 36.0, "width": 72.0, "height": 72.0}),+ });+ app.after_engine();+ let d = app.ui.dialog.as_mut().expect("the Rectangle dialog");+ d.fields.insert("width".into(), json!("50"));+ d.fields.insert("height".into(), json!("50"));+ app.run("document.preferences", json!({"horizontalUnits": "inches", "verticalUnits": "inches"})).unwrap();+ let before = app.session.doc().unwrap().doc.clone();+ confirm(&mut app).unwrap();+ let after = &app.session.doc().unwrap().doc;+ let added: Vec<_> = after.spreads[0].items.iter().filter(|i| !before.spreads[0].items.iter().any(|b| b.id == i.id)).collect();+ assert_eq!(added.len(), 1, "one frame");+ let b = added[0].bounds();+ let mm50 = 50.0 / 25.4 * 72.0;+ assert!((b.width() - mm50).abs() < 0.01 && (b.height() - mm50).abs() < 0.01, "50 mm by 50 mm (about 141.73 pt): {b:?}");+ // A dialog made without the vertical unit (`Dialog::new`) reads the height in its own unit.+ let mut d = Dialog::new("frameSize", json!({"width": "2", "height": "3"}));+ d.unit = Unit::Inches;+ assert_eq!(d.vertical_unit, None);+ app.ui.dialog = Some(d);+ let before = app.session.doc().unwrap().doc.clone();+ confirm(&mut app).unwrap();+ let after = &app.session.doc().unwrap().doc;+ let added: Vec<_> = after.spreads[0].items.iter().filter(|i| !before.spreads[0].items.iter().any(|b| b.id == i.id)).collect();+ assert_eq!(added.len(), 1, "one frame");+ let b = added[0].bounds();+ assert!((b.width() - 144.0).abs() < 0.01 && (b.height() - 216.0).abs() < 0.01, "{b:?}");+}