Skip to content

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

Merge remote-tracking branch 'origin/main' into zydvc26s67-web-paragraph-styles-save-delete

zydvc26s67-web206bc0da742e
diff --cc crates/ui-egui/src/dialogs.rs
index 646e80dc7,44edebb0c..121ab6dec
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@@ -2931,83 -2774,18 +3023,98 @@@ mod tests
}
}
+ fn test_app() -> crate::DesignApp {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.session.execute("file.new", &json!({"pages": 1})).unwrap();
+ app
+ }
+
+ #[test]
+ fn paragraph_style_options_dialog_saves_edits() {
+ let mut app = test_app();
+ let name = app.session.execute("style.paragraph.create", &json!({"name": "Body Copy"})).unwrap()["name"].as_str().unwrap().to_string();
+ app.ui.dialog = Some(Dialog::new("paragraphStyleOptions", json!({"name": name, "p.align": "center"})));
+ confirm(&mut app).expect("OK saves the style edit");
+ let st = app.session.active().unwrap();
+ assert_eq!(st.doc.styles.para(&name).unwrap().para.align, Some(designcraft_doc::Align::Center));
+ }
+
+ #[test]
+ fn delete_paragraph_style_dialog_removes_the_style_and_reassigns_text() {
+ let mut app = test_app();
+ let name = app.session.execute("style.paragraph.create", &json!({"name": "Caption"})).unwrap()["name"].as_str().unwrap().to_string();
+ let sid = app.session.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap()["story"].as_u64().unwrap();
+ app.session.execute("text.insert", &json!({"text": "Hello"})).unwrap();
+ app.session.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 0})).unwrap();
+ app.session.execute("style.paragraph.apply", &json!({"name": name})).unwrap();
+ app.ui.dialog = Some(Dialog::new("deleteParagraphStyle", json!({"name": name, "replaceWith": designcraft_doc::BASIC_PARAGRAPH})));
+ confirm(&mut app).expect("OK deletes the style");
+ let st = app.session.active().unwrap();
+ assert!(st.doc.styles.para(&name).is_none(), "style removed");
+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).unwrap();
+ assert_eq!(story.paras[0].style, designcraft_doc::BASIC_PARAGRAPH, "text reassigned to the replacement style");
+ }
+
+ #[test]
+ fn delete_paragraph_style_dialog_defaults_replacement_to_basic_paragraph() {
+ let mut app = test_app();
+ let name = app.session.execute("style.paragraph.create", &json!({"name": "Caption"})).unwrap()["name"].as_str().unwrap().to_string();
+ // No `replaceWith` set — the dialog's default field.
+ app.ui.dialog = Some(Dialog::new("deleteParagraphStyle", json!({"name": name})));
+ confirm(&mut app).expect("OK deletes the style with the default replacement");
+ let st = app.session.active().unwrap();
+ assert!(st.doc.styles.para(&name).is_none());
+ }
+
+ #[test]
+ fn character_style_options_dialog_saves_edits() {
+ let mut app = test_app();
+ let name = app.session.execute("style.character.create", &json!({"name": "Key Term"})).unwrap()["name"].as_str().unwrap().to_string();
+ app.ui.dialog = Some(Dialog::new("characterStyleOptions", json!({"name": name, "c.tracking": 20.0})));
+ confirm(&mut app).expect("OK saves the style edit");
+ let st = app.session.active().unwrap();
+ assert_eq!(st.doc.styles.char_style(&name).unwrap().chars.tracking, Some(20.0));
+ }
+
+ #[test]
+ fn delete_character_style_dialog_removes_the_style_and_reassigns_text() {
+ let mut app = test_app();
+ let name = app.session.execute("style.character.create", &json!({"name": "Key Term"})).unwrap()["name"].as_str().unwrap().to_string();
+ let sid = app.session.execute("frame.create", &json!({"rect": [36, 36, 300, 200], "content": "text"})).unwrap()["story"].as_u64().unwrap();
+ app.session.execute("text.insert", &json!({"text": "Hello"})).unwrap();
+ app.session.execute("text.select", &json!({"story": sid, "anchor": 0, "focus": 5})).unwrap();
+ app.session.execute("style.character.apply", &json!({"name": name})).unwrap();
+ app.ui.dialog = Some(Dialog::new("deleteCharacterStyle", json!({"name": name, "replaceWith": designcraft_doc::NO_CHAR_STYLE})));
+ confirm(&mut app).expect("OK deletes the style");
+ let st = app.session.active().unwrap();
+ assert!(st.doc.styles.char_style(&name).is_none(), "style removed");
+ let story = st.doc.stories.get(&designcraft_doc::StoryId(sid)).unwrap();
+ assert_eq!(story.chars[0].format.style, designcraft_doc::NO_CHAR_STYLE, "text reassigned to the replacement style");
+ }
+
+ #[test]
+ fn delete_character_style_dialog_defaults_replacement_to_none() {
+ let mut app = test_app();
+ let name = app.session.execute("style.character.create", &json!({"name": "Key Term"})).unwrap()["name"].as_str().unwrap().to_string();
+ // No `replaceWith` set — the dialog's default field.
+ app.ui.dialog = Some(Dialog::new("deleteCharacterStyle", json!({"name": name})));
+ confirm(&mut app).expect("OK deletes the style with the default replacement");
+ let st = app.session.active().unwrap();
+ assert!(st.doc.styles.char_style(&name).is_none());
+ }
++
+ #[test]
+ fn shortcut_recorder_waits_for_the_key_after_its_modifiers() {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ let mut d = Dialog::new("keyboardShortcuts", json!({"query": "", "recording": "app.palette"}));
+ let ctx = egui::Context::default();
+ let key = |key, modifiers| egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers };
+ // Holding Ctrl reports Ctrl itself as a key press, then Ctrl+K arrives.
+ for event in [key(egui::Key::ControlLeft, egui::Modifiers::COMMAND), key(egui::Key::K, egui::Modifiers::COMMAND)] {
+ let input = egui::RawInput { events: vec![event], ..Default::default() };
+ ctx.run_ui(input, |ui| keyboard_shortcuts(&mut app, ui, &mut d)).textures_delta.clear();
+ }
+ assert_eq!(crate::menus::shortcut_of(&app, "app.palette").as_deref(), Some("Cmd+K"));
+ assert_eq!(d.s("recording"), "", "recording ends with the recorded shortcut");
+ }
}