DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge #264: UI: the shortcut recorder waits for the key; Interactive for PDF and Digital Publishing open their panels
aiongg24ab4a1f6bd2
diff --cc crates/ui-egui/src/dialogs.rsindex a671fa21d,cf3988b17..0b7529c7f--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@@ -3648,140 -2688,18 +3654,155 @@@ mod tests}}+ use designcraft_doc::{CharAttrs, CharacterStyle};++ /// A document with character styles Base (bold) and Emphasis (italic) on the first two letters.+ fn styled_app() -> DesignApp {+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ app.run("style.character.create", json!({"name": "Base", "chars": {"fontStyle": "Bold"}})).unwrap();+ app.run("style.character.create", json!({"name": "Emphasis", "chars": {"fontStyle": "Italic"}})).unwrap();+ let r = app.run("frame.create", json!({"rect": [72, 72, 300, 200], "content": "text", "text": "Hi there"})).unwrap();+ app.run("text.select", json!({"story": r["story"], "anchor": 0, "focus": 2})).unwrap();+ app.run("style.character.apply", json!({"name": "Emphasis"})).unwrap();+ app+ }++ /// One control-channel request, as the control port or MCP would send it.+ fn ctl(app: &mut DesignApp, method: &str, params: Value) -> Value {+ let (req, _reply) = crate::control::ControlRequest::new(method, params);+ match crate::control::handle(app, &egui::Context::default(), &req) {+ crate::control::Outcome::Done(v) => v,+ crate::control::Outcome::Screenshot { .. } => Value::Null,+ }+ }++ fn char_style(app: &DesignApp, name: &str) -> Option<CharacterStyle> {+ app.session.active().and_then(|st| st.doc.styles.char_style(name).cloned())+ }++ #[test]+ fn character_style_options_set_exactly_the_edited_attributes() {+ let mut app = styled_app();+ assert_eq!(ctl(&mut app, "ui.dialog.open", json!({"id": "characterStyleOptions", "fields": {"name": "Emphasis"}}))["ok"], true);+ ctl(&mut app, "ui.dialog.set", json!({"field": "c.size", "value": 14}));+ ctl(&mut app, "ui.dialog.set", json!({"field": "c.fill", "value": "[Paper]"}));+ ctl(&mut app, "ui.dialog.set", json!({"field": "basedOn", "value": "Base"}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ assert!(app.ui.dialog.is_none());+ let st = char_style(&app, "Emphasis").unwrap();+ assert_eq!(st.based_on.as_deref(), Some("Base"));+ assert_eq!(st.chars, CharAttrs { font_style: Some("Italic".into()), size: Some(14.0), fill: Some("[Paper]".into()), ..Default::default() });+ }++ #[test]+ fn character_style_options_unset_cleared_fields_and_rename_every_use() {+ let mut app = styled_app();+ ctl(&mut app, "ui.dialog.open", json!({"id": "characterStyleOptions", "fields": {"name": "Emphasis"}}));+ ctl(&mut app, "ui.dialog.set", json!({"field": "c.fontStyle", "value": null}));+ ctl(&mut app, "ui.dialog.set", json!({"field": "rename", "value": "Strong"}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ assert!(char_style(&app, "Emphasis").is_none());+ assert!(char_style(&app, "Strong").unwrap().chars.is_empty());+ let st = app.session.active().unwrap();+ let story = st.doc.stories.values().next().unwrap();+ assert!(story.chars.iter().any(|r| r.format.style == "Strong"));+ // A taken name keeps the dialog open with the reason.+ ctl(&mut app, "ui.dialog.open", json!({"id": "characterStyleOptions", "fields": {"name": "Strong"}}));+ ctl(&mut app, "ui.dialog.set", json!({"field": "rename", "value": "Base"}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], false);+ assert!(app.ui.dialog.as_ref().is_some_and(|d| !d.s("status").is_empty()));+ assert!(char_style(&app, "Strong").is_some());+ }++ #[test]+ fn new_character_style_opens_its_options_and_creates_the_style() {+ let mut app = styled_app();+ crate::menus::activate(&mut app, "style.character.create", &Value::Null);+ let d = app.ui.dialog.clone().unwrap();+ assert_eq!((d.id.as_str(), d.b("new"), d.s("rename").as_str()), ("characterStyleOptions", true, "Character Style 1"));+ ctl(&mut app, "ui.dialog.set", json!({"field": "c.size", "value": 9}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ let st = char_style(&app, "Character Style 1").unwrap();+ assert_eq!((st.based_on, st.chars), (None, CharAttrs { size: Some(9.0), ..Default::default() }));+ crate::menus::activate(&mut app, "style.character.create", &Value::Null);+ assert_eq!(app.ui.dialog.as_ref().unwrap().s("rename"), "Character Style 2");+ }++ #[test]+ fn new_paragraph_style_opens_its_options_and_creates_the_style() {+ let mut app = styled_app();+ crate::menus::activate(&mut app, "style.paragraph.create", &Value::Null);+ let d = app.ui.dialog.clone().unwrap();+ assert_eq!((d.id.as_str(), d.b("new"), d.s("rename").as_str()), ("paragraphStyleOptions", true, "Paragraph Style 1"));+ ctl(&mut app, "ui.dialog.set", json!({"field": "c.size", "value": 20}));+ ctl(&mut app, "ui.dialog.set", json!({"field": "basedOn", "value": designcraft_doc::BASIC_PARAGRAPH}));+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);+ let st = app.session.active().unwrap().doc.styles.para("Paragraph Style 1").cloned().unwrap();+ assert_eq!(st.based_on.as_deref(), Some(designcraft_doc::BASIC_PARAGRAPH));+ assert_eq!(st.chars, CharAttrs { size: Some(20.0), ..Default::default() });+ }++ #[test]+ fn style_rows_open_their_options_except_none() {+ let mut app = styled_app();+ open_style_options(&mut app, false, "Emphasis");+ let d = app.ui.dialog.take().unwrap();+ assert_eq!((d.id.as_str(), d.s("name").as_str()), ("characterStyleOptions", "Emphasis"));+ open_style_options(&mut app, false, designcraft_doc::NO_CHAR_STYLE);+ assert!(app.ui.dialog.is_none());+ open_style_options(&mut app, true, designcraft_doc::BASIC_PARAGRAPH);+ assert_eq!(app.ui.dialog.take().unwrap().id, "paragraphStyleOptions");+ }++ #[test]+ fn style_options_sections_draw() {+ let mut app = styled_app();+ let ctx = egui::Context::default();+ let frame = |app: &mut DesignApp| {+ for _ in 0..2 {+ let raw = egui::RawInput {+ screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1440.0, 900.0))),+ max_texture_side: Some(8192),+ ..Default::default()+ };+ let mut out = ctx.run_ui(raw, |ui| {+ app.logic(&ui.ctx().clone());+ app.ui(ui);+ });+ out.textures_delta.clear();+ }+ };+ for lang in ["", "ar"] {+ app.run("app.language", json!({"lang": lang})).unwrap();+ for fields in [json!({"name": "Emphasis"}), json!({"new": true}), json!({"name": "Emphasis", "c.size": null, "c.underline": false})] {+ for section in ["general", "chars", "advanced", "color", "openType", "underline", "strikethrough"] {+ let mut f = fields.clone();+ f["section"] = json!(section);+ app.ui.dialog = Some(Dialog::new("characterStyleOptions", f));+ frame(&mut app);+ assert!(app.ui.dialog.is_some(), "{section} stays open");+ }+ }+ for section in ["general", "chars", "color"] {+ app.ui.dialog = Some(Dialog::new("paragraphStyleOptions", json!({"new": true, "section": section})));+ frame(&mut app);+ }+ }+ }+++ #[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");+ }}