Skip to content

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

Merge table-cell-strokes: edits start from the drawn edge, bounded dash patterns, Cell Options fixes, closed corners

aionggb6b5d4c705f0
diff --cc crates/ui-egui/src/dialogs.rs
index 7d0c8a6f2,89b84a2d7..33d48c818
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@@ -4905,291 -3885,33 +4918,318 @@@ mod tests
assert!(t.cells.iter().all(|c| c.strokes.iter().all(|e| e.weight == 1.0)), "unchanged values are not applied");
}
+ #[test]
+ fn cell_options_edge_changes_reseed_every_stroke_field_and_survive_a_bad_seed() {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ app.run("frame.create", json!({"rect": [72, 72, 400, 400], "content": "text", "caret": true})).unwrap();
+ let tid = app.run("table.insert", json!({"rows": 2, "cols": 2})).unwrap()["table"].as_u64().unwrap();
+ app.run("table.select", json!({"what": "table"})).unwrap();
+ app.run("table.setCell", json!({"stroke": {"weight": 3, "edges": "innerHorizontal"}})).unwrap();
+ app.run("app.cellOptionsDialog", json!({})).unwrap();
+ let mut d = app.ui.dialog.take().unwrap();
+ d.fields.insert("seed".into(), json!(5));
+ let outer = [true, true, true, true, false, false];
+ cell_options_edges_changed(&mut app, &mut d, &outer);
+ assert_eq!((&d.fields["weight"], &d.fields["seed"]["weight"]), (&json!(1.0), &json!(1.0)));
+ // A weight typed for the outer edges, then the inner horizontal edges (3 pt) chosen
+ // and 1 pt typed: 1 pt differs from what those edges show, so OK applies it.
+ d.fields.insert("weight".into(), json!(5.0));
+ cell_options_edges_changed(&mut app, &mut d, &[false, false, false, false, true, false]);
+ assert_eq!((&d.fields["weight"], &d.fields["seed"]["weight"]), (&json!(5.0), &json!(3.0)));
+ d.fields.insert("weight".into(), json!(1.0));
+ app.ui.dialog = Some(d);
+ confirm(&mut app).unwrap();
+ let st = app.session.active().unwrap();
+ let t = st.doc.stories.values().find_map(|s| s.tables.get(&tid)).unwrap();
+ assert_eq!([t.cell(0, 0).unwrap().strokes[2].weight, t.cell(1, 1).unwrap().strokes[0].weight], [1.0; 2]);
+ }
+
+ #[test]
+ fn numbering_and_section_options_keep_their_actions_in_view() {
+ for language in ["en", "ar"] {
+ for size in [egui::vec2(1178.0, 814.0), egui::vec2(392.0, 271.0)] {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.ui.language = language.into();
+ app.run("file.new", json!({"pages": 2})).unwrap();
+ crate::menus::activate(&mut app, "layout.section", &Value::Null);
+ let ctx = dialog_context(language);
+ let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, size);
+ for _ in 0..3 {
+ dialog_frame(&mut app, &ctx, screen, vec![]);
+ }
+ let labels = dialog_frame(&mut app, &ctx, screen, vec![]);
+ let tr = |s| crate::i18n::tr(language, s);
+ for expected in [tr("Numbering & Section Options…").trim_end_matches('…'), tr("OK"), tr("Cancel"), tr("Start Section")] {
+ visible_label(&labels, screen, expected);
+ }
+ if size.x > 1000.0 {
+ // Every option fits without scrolling, and choosing one then OK applies it.
+ visible_label(&labels, screen, &format!("{} {}", tr("Book Name:"), tr("N/A")));
+ click_dialog(&mut app, &ctx, screen, visible_label(&labels, screen, tr("Automatic Page Numbering")).center());
+ let labels = dialog_frame(&mut app, &ctx, screen, vec![]);
+ click_dialog(&mut app, &ctx, screen, visible_label(&labels, screen, tr("OK")).center());
+ assert!(app.ui.dialog.is_none(), "{language}: OK closes the dialog");
+ let doc = &app.session.active().unwrap().doc;
+ assert!(doc.sections.iter().all(|s| s.start != 0 || s.start_number.is_none()), "{language}: page 1 numbers automatically");
+ }
+ }
+ }
+ }
+
+ fn draw(app: &mut DesignApp, ctx: &egui::Context) {
+ let input =
+ egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0))), ..Default::default() };
+ let mut out = ctx.run_ui(input, |ui| {
+ app.logic(&ui.ctx().clone());
+ app.ui(ui);
+ });
+ out.textures_delta.clear();
+ }
+
+ /// A paragraph styled "Ruled Child", whose Rule Below comes from its parent "Ruled".
+ fn ruled_paragraph() -> (DesignApp, Value) {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ let rule = json!({"on": true, "weight": 3.0, "color": "[Black]", "tint": 0.5, "columnWidth": false, "offset": 4.0, "leftIndent": 6.0, "rightIndent": 2.0});
+ app.run("style.paragraph.create", json!({"name": "Ruled", "para": {"ruleBelow": rule}})).unwrap();
+ app.run("style.paragraph.create", json!({"name": "Ruled Child", "basedOn": "Ruled"})).unwrap();
+ let r = app.run("frame.create", json!({"rect": [72, 72, 300, 400], "content": "text", "text": "One"})).unwrap();
+ app.run("text.select", json!({"story": r["story"], "anchor": 0, "focus": 0})).unwrap();
+ app.run("style.paragraph.apply", json!({"name": "Ruled Child"})).unwrap();
+ (app, rule)
+ }
+
+ fn undo_steps(app: &DesignApp) -> usize {
+ app.session.doc().map_or(0, |st| st.history.undo.len())
+ }
+
+ #[test]
+ fn paragraph_rules_dialog_shows_the_rule_and_applies_one_field() {
+ let (mut app, rule) = ruled_paragraph();
+ let ctx = egui::Context::default();
+ app.run("app.paragraphRulesDialog", json!({"rule": "ruleBelow"})).unwrap();
+ draw(&mut app, &ctx);
+ let d = app.ui.dialog.clone().unwrap();
+ assert_eq!(d.id, "paragraphRules");
+ let current = d.fields["current"].clone();
+ assert_eq!(shown_rule(&d, "", "ruleBelow", &current), rule, "the inherited rule");
+ assert_eq!(shown_rule(&d, "", "ruleAbove", &current)["on"], false);
+ // Pick a colour, as the Color menu does.
+ app.ui.dialog.as_mut().unwrap().fields.insert("ruleBelow.color".into(), json!("[Registration]"));
+ draw(&mut app, &ctx);
+ let before = undo_steps(&app);
+ confirm(&mut app).unwrap();
+ assert_eq!(undo_steps(&app), before + 1, "one undo step");
+ let mut want = rule.clone();
+ want["color"] = json!("[Registration]");
+ assert_eq!(app.run("type.selectionAttrs", json!({})).unwrap()["para"]["ruleBelow"], want);
+ app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(app.run("type.selectionAttrs", json!({})).unwrap()["para"]["ruleBelow"], rule);
+ }
+
+ #[test]
+ fn paragraph_style_options_edit_one_rule_field() {
+ let (mut app, rule) = ruled_paragraph();
+ let ctx = egui::Context::default();
+ app.ui.dialog = Some(Dialog::new("paragraphStyleOptions", json!({"name": "Ruled Child", "section": "rules", "rule": "ruleBelow"})));
+ draw(&mut app, &ctx);
+ let resolved = |app: &DesignApp| serde_json::to_value(app.session.doc().unwrap().doc.styles.resolve_para_style("Ruled Child").0).unwrap();
+ let d = app.ui.dialog.clone().unwrap();
+ assert_eq!(shown_rule(&d, "p.", "ruleBelow", &resolved(&app)), rule, "inherited from the parent style");
+ app.ui.dialog.as_mut().unwrap().fields.insert("p.ruleBelow.weight".into(), json!(1.5));
+ draw(&mut app, &ctx);
+ let before = undo_steps(&app);
+ confirm(&mut app).unwrap();
+ assert_eq!(undo_steps(&app), before + 1, "one undo step");
+ let mut want = rule.clone();
+ want["weight"] = json!(1.5);
+ assert_eq!(resolved(&app)["ruleBelow"], want);
+ assert_eq!(app.run("type.selectionAttrs", json!({})).unwrap()["para"]["ruleBelow"], want, "the paragraph follows its style");
+ app.run("edit.undo", json!({})).unwrap();
+ assert_eq!(resolved(&app)["ruleBelow"], rule);
+ }
+
+ /// Opens Paragraph Style Options on "Ruled Child" at each of `sections`, sets `fields` and
+ /// clicks OK; returns the resolved attributes of the paragraph styled with it.
+ fn style_section_edit(sections: &[&str], fields: Value) -> Value {
+ let (mut app, _) = ruled_paragraph();
+ let ctx = egui::Context::default();
+ app.ui.dialog = Some(Dialog::new("paragraphStyleOptions", json!({"name": "Ruled Child"})));
+ for section in sections {
+ app.ui.dialog.as_mut().unwrap().fields.insert("section".into(), json!(section));
+ draw(&mut app, &ctx);
+ }
+ for (k, v) in fields.as_object().unwrap() {
+ app.ui.dialog.as_mut().unwrap().fields.insert(k.clone(), v.clone());
+ }
+ draw(&mut app, &ctx);
+ let before = undo_steps(&app);
+ confirm(&mut app).unwrap();
+ assert_eq!(undo_steps(&app), before + 1, "one undo step");
+ let style = app.session.doc().unwrap().doc.styles.para("Ruled Child").cloned().unwrap();
+ let own = serde_json::to_value(&style.para).unwrap();
+ for (k, v) in fields.as_object().unwrap() {
+ assert_eq!(own[k.trim_start_matches("p.")], *v, "the style sets {k}");
+ }
+ app.run("type.selectionAttrs", json!({})).unwrap()["para"].clone()
+ }
+
+ #[test]
+ fn paragraph_style_options_keep_and_border_sections_reach_the_styled_paragraph() {
+ let para = style_section_edit(
+ &["keep", "border"],
+ json!({"p.keepWithNext": 2, "p.keepLinesTogether": true, "p.keepAllLines": false, "p.keepFirst": 3,
+ "p.startParagraph": "nextPage", "p.borderOn": true, "p.borderWeights": [2.0, 0.0, 2.0, 0.0]}),
+ );
+ assert_eq!(para["keepWithNext"], 2);
+ assert_eq!(para["keepLinesTogether"], true);
+ assert_eq!(para["keepAllLines"], false);
+ assert_eq!(para["keepFirst"], 3);
+ assert_eq!(para["startParagraph"], "nextPage");
+ assert_eq!(para["borderOn"], true);
+ assert_eq!(para["borderWeights"], json!([2.0, 0.0, 2.0, 0.0]));
+ // Fields not edited keep the resolved value.
+ assert_eq!(para["keepLast"], 2);
+ assert_eq!(para["ruleBelow"]["weight"], 3.0);
+ }
+
+ #[test]
+ fn paragraph_style_options_bullets_and_span_sections_reach_the_styled_paragraph() {
+ let para = style_section_edit(
+ &["bullets", "span"],
+ json!({"p.listType": "numbers", "p.numberStyle": "upperRoman", "p.numberExpression": "^#)^t", "p.startAt": 3,
+ "p.spanColumns": {"kind": "split", "columns": 3}}),
+ );
+ assert_eq!(para["listType"], "numbers");
+ assert_eq!(para["numberStyle"], "upperRoman");
+ assert_eq!(para["numberExpression"], "^#)^t");
+ assert_eq!(para["startAt"], 3);
+ assert_eq!(para["spanColumns"], json!({"kind": "split", "columns": 3}));
+ let props: designcraft_doc::ParaProps = serde_json::from_value(para).unwrap();
+ assert_eq!(props.number_label(3), "III)\t");
+ }
+
+ fn command_app(id: &str, fields: Value) -> DesignApp {
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.ui.dialog = Some(Dialog::new(&format!("cmd:{id}"), fields));
+ app
+ }
+
+ #[test]
+ fn generic_new_document_preserves_typed_values_and_text_entry() {
+ for pages in [json!(2), json!(" 2 ")] {
+ let mut app = command_app(
+ "file.new",
+ json!({
+ "preset": "A4", "pages": pages, "facingPages": false, "gutter": 0,
+ "margins": {"top": 10, "bottom": 20, "inside": 30, "outside": 40},
+ "title": " ", "status": "internal", "primaryTextFrame": true
+ }),
+ );
+ confirm(&mut app).unwrap();
+ let doc = &app.session.active().unwrap().doc;
+ assert_eq!(doc.page_count(), 2);
+ assert!(!doc.settings.facing_pages);
+ assert!(doc.title.starts_with("Untitled-"));
+ for page in doc.spreads.iter().flat_map(|s| &s.pages) {
+ assert!((page.width - 595.2755905511812).abs() < 0.01);
+ assert!((page.height - 841.8897637795277).abs() < 0.01);
+ assert_eq!(page.columns.gutter, 0.0);
+ assert_eq!(page.margins.top, 10.0);
+ assert_eq!(page.margins.outside, 40.0);
+ }
+ assert!(doc.spreads.iter().all(|s| s.items.is_empty()), "undocumented fields stay excluded");
+ assert!(app.ui.dialog.is_none());
+ }
+ }
+
+ #[test]
+ fn numbering_commands_document_their_fields() {
+ let names = |id: &str| command_fields(designcraft_engine::find_command(id).unwrap().params).into_iter().map(|f| f.key).collect::<Vec<_>>();
+ assert_eq!(names("layout.section"), ["page", "startNumber", "style", "prefix", "includePrefix", "marker", "remove"]);
+ assert_eq!(names("layout.chapterNumbering"), ["style", "start", "source"]);
+ }
+
+ #[test]
+ fn generic_command_preserves_array_geometry() {
+ let mut app = command_app("file.new", json!({}));
+ confirm(&mut app).unwrap();
+ app.ui.dialog = Some(Dialog::new("cmd:frame.create", json!({"rect": [10, 20, 110, 220]})));
+ confirm(&mut app).unwrap();
+ let doc = &app.session.active().unwrap().doc;
+ let item = doc.spreads.iter().flat_map(|s| &s.items).next().unwrap();
+ assert_eq!(item.bounds(), designcraft_geom::Rect::new(10.0, 20.0, 110.0, 220.0));
+ }
+
+ #[test]
+ fn generic_explicit_null_uses_the_commands_existing_semantics() {
+ // file.new accepts null width by using the selected preset's width.
+ for width in [Value::Null, json!("null")] {
+ let mut app = command_app("file.new", json!({"preset": "A4", "width": width, "title": " Sample layout "}));
+ confirm(&mut app).unwrap();
+ let doc = &app.session.active().unwrap().doc;
+ assert_eq!(doc.title, "Sample layout");
+ assert!((doc.settings.page_width - 595.2755905511812).abs() < 0.01);
+ }
+ }
+
+ #[test]
+ fn generic_invalid_number_retains_dialog_without_creating_document() {
+ let mut app = command_app("file.new", json!({"width": 0}));
+ assert!(confirm(&mut app).is_err());
+ assert!(app.session.active().is_none());
+ let dialog = app.ui.dialog.as_ref().unwrap();
+ assert_eq!(dialog.fields["width"], 0);
+ assert!(dialog.fields["status"].as_str().unwrap().contains("page size out of range"));
+ }
+
+ #[test]
+ fn text_frame_options_change_only_what_was_edited() {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ let r = app.run("frame.create", json!({"rect": [36, 36, 336, 236], "content": "text", "text": "Hello"})).unwrap();
+ let id = designcraft_doc::ItemId(r["id"].as_u64().unwrap());
+ app.run("object.textFrameOptions", json!({"ids": [id.0], "columns": 2, "inset": [1, 2, 3, 4]})).unwrap();
+ let opts = |app: &DesignApp| app.session.doc().unwrap().doc.item(id).unwrap().text_frame().unwrap().options.clone();
+ let before = opts(&app);
+ let open = |app: &mut DesignApp, fields: &[(&str, Value)]| {
+ app.ui.dialog = Some(Dialog::new("textFrameOptions", json!({})));
+ let mut d = app.ui.dialog.take().unwrap();
+ seed_text_frame_options(app, &mut d);
+ for (k, v) in fields {
+ d.fields.insert((*k).into(), v.clone());
+ }
+ app.ui.dialog = Some(d);
+ confirm(app)
+ };
+ // OK without edits changes nothing (no undo step either).
+ assert_eq!(open(&mut app, &[]).unwrap(), Value::Null);
+ assert_eq!(opts(&app), before);
+ // The rule's weight is in points; unequal insets the user didn't touch stay.
+ open(&mut app, &[("columnRule", json!(true)), ("columnRuleWeight", json!("6"))]).unwrap();
+ let o = opts(&app);
+ assert!(o.column_rule && o.column_rule_weight == 6.0, "{o:?}");
+ assert_eq!((o.inset, o.columns, o.gutter), (before.inset, before.columns, before.gutter));
+ // One of the rule's insets edited: the other rule settings stay.
+ open(&mut app, &[("columnRuleBottomInset", json!("0p9")), ("columnRuleTint", json!("40"))]).unwrap();
+ let o = opts(&app);
+ assert_eq!((o.column_rule_bottom_inset, o.column_rule_top_inset, o.column_rule_offset, o.column_rule_weight), (9.0, 0.0, 0.0, 6.0));
+ assert!((o.column_rule_tint - 0.4).abs() < 1e-6 && o.column_rule);
+ // One inset side edited: the others keep their values.
+ open(&mut app, &[("insetLeft", json!("1p0"))]).unwrap();
+ assert_eq!(opts(&app).inset, [1.0, 12.0, 3.0, 4.0]);
+ // An unknown swatch is an error and the dialog stays open.
+ let e = open(&mut app, &[("columnRuleColor", json!("Mauve"))]).unwrap_err();
+ assert!(e.contains("Mauve") && app.ui.dialog.is_some(), "{e}");
+ assert_eq!(opts(&app).column_rule_color, designcraft_color::swatch::BLACK);
+ // Each OK is one undo step.
+ for _ in 0..3 {
+ app.run("edit.undo", json!({})).unwrap();
+ }
+ assert_eq!(opts(&app), before);
+ }
+
fn dialog_context(language: &str) -> egui::Context {
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx, language);