From 78b64ed2fc0e97ef5d84427c7f90f53faff772cd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 24 Oct 2026 20:22:00 -0700 Subject: [PATCH 1/8] Format: OK in the Font dialog keeps a preset or system text color The Font dialog got a preset text color as its bare name, which dropped its transforms, and a system color as its last RGB value, which dropped the system color. Pressing OK then replaced the run's color: a preset "red" at 50% luminance came back as plain red. `color_command_value` now writes `{"preset": name}` and `{"system": name, "last": "#RRGGBB"}` with the transforms, and `color_value` reads both forms. The dialog's Color field shows the preset's name or the system color's last value. --- crates/engine/src/cmd/format.rs | 29 ++++++++++++++++++++++-- crates/engine/src/cmd/mod.rs | 7 ++++++ crates/ui-egui/src/dialogs.rs | 2 +- crates/ui-egui/src/font_dialog_tests.rs | 30 +++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index a3b2c40a4..31ab7290d 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -597,8 +597,15 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { o.insert("scheme".into(), json!(slot.xml_name())); Value::Object(o) } - ColorBase::Preset { name } => json!(name), - ColorBase::System { last, .. } => json!(format!("#{}", last.hex())), + ColorBase::Preset { name } => { + o.insert("preset".into(), json!(name)); + Value::Object(o) + } + ColorBase::System { name, last } => { + o.insert("system".into(), json!(name)); + o.insert("last".into(), json!(format!("#{}", last.hex()))); + Value::Object(o) + } } } @@ -1497,6 +1504,24 @@ mod tests { assert!(r.size.is_none() && r.fill.is_none()); } + #[test] + fn font_dialog_ok_keeps_preset_and_system_colors() { + let preset = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::LumMod(50_000)); + let system = + ColorRef { base: ColorBase::System { name: "windowText".into(), last: deckcraft_color::Rgba::rgb(0x12, 0x34, 0x56) }, mods: vec![] } + .with(ColorTransform::Alpha(60_000)); + for color in [preset, system] { + let mut s = shape(); + let fill = Fill::Solid { color: color.clone() }; + format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap(); + s.ui_requests.clear(); + s.execute("format.fontDialog", &json!({})).unwrap(); + let Some(UiRequest::Dialog { params, .. }) = s.ui_requests.last().cloned() else { panic!("dialog request") }; + s.execute("format.fontDialog", &json!({"color": params["color"]})).unwrap(); + assert_eq!(props(&s).fill, Some(Fill::Solid { color }), "{}", params["color"]); + } + } + #[test] fn font_dialog_with_no_fields_asks_the_ui_to_open() { let mut s = shape(); diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index fa3f64598..e2442025c 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -371,6 +371,8 @@ pub(crate) fn ok() -> Result { } /// Parse a colour param: `"#RRGGBB"`, `"accent1"`…, or `{"scheme":"accent2","lumMod":75000}`. +/// A preset or system colour keeps its base: `{"preset":"red"}`, +/// `{"system":"windowText","last":"#000000"}`. pub(crate) fn color_param(p: &Value, key: &str) -> Option { let v = p.get(key)?; color_value(v) @@ -391,6 +393,11 @@ pub(crate) fn color_value(v: &Value) -> Option { Value::Object(o) => { let mut c = if let Some(s) = o.get("scheme").and_then(Value::as_str) { ColorRef::scheme(deckcraft_color::SchemeSlot::from_xml(s)?) + } else if let Some(name) = o.get("preset").and_then(Value::as_str) { + ColorRef { base: deckcraft_model::ColorBase::Preset { name: name.to_string() }, mods: vec![] } + } else if let Some(name) = o.get("system").and_then(Value::as_str) { + let last = deckcraft_color::Rgba::from_hex(o.get("last").and_then(Value::as_str)?)?; + ColorRef { base: deckcraft_model::ColorBase::System { name: name.to_string(), last }, mods: vec![] } } else { ColorRef::rgb(deckcraft_color::Rgba::from_hex(o.get("rgb").and_then(Value::as_str)?)?) }; diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index e0fa02e95..e6a70f168 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -2798,7 +2798,7 @@ fn json_text(v: Option<&Value>, default: &str) -> String { fn color_label(v: &Value) -> String { match v { Value::String(s) => s.clone(), - Value::Object(o) => o.get("scheme").or_else(|| o.get("rgb")).and_then(Value::as_str).unwrap_or("").to_string(), + Value::Object(o) => ["scheme", "rgb", "preset", "last"].iter().find_map(|k| o.get(*k)).and_then(Value::as_str).unwrap_or("").to_string(), _ => String::new(), } } diff --git a/crates/ui-egui/src/font_dialog_tests.rs b/crates/ui-egui/src/font_dialog_tests.rs index 06ccfc024..e3076c62a 100644 --- a/crates/ui-egui/src/font_dialog_tests.rs +++ b/crates/ui-egui/src/font_dialog_tests.rs @@ -63,3 +63,33 @@ fn command_t_opens_the_font_dialog_and_cancel_keeps_the_text() { assert!(h.state().dialog.is_none()); assert_eq!(bold_of(&h), None); } + +#[test] +fn preset_and_system_colors_show_in_the_dialog_and_ok_keeps_them() { + for (color, shown) in + [(json!({"preset": "red", "lumMod": 50000}), "red"), (json!({"system": "windowText", "last": "#123456", "alpha": 60000}), "#123456")] + { + let mut h = harness(); + h.state_mut().session.execute("format.fontDialog", &json!({ "color": color })).unwrap(); + let before = fill_of(&h); + assert!(before.is_some()); + h.get_by_label("Font…").click(); + h.run_steps(2); + let d = h.state().dialog.as_ref().expect("font dialog"); + assert_eq!(d.fields.get("color").map(String::as_str), Some(shown)); + h.get_by_label("OK").click_accesskit(); + h.run_steps(1); + assert!(h.state().dialog.is_none(), "status {:?}", h.state().status); + assert_eq!(fill_of(&h), before, "{shown}"); + } +} + +fn fill_of(h: &Harness<'_, SlideApp>) -> Option { + let st = h.state().session.active().unwrap(); + st.selected_shapes() + .first() + .and_then(|sh| sh.text.as_ref()) + .and_then(|t| t.paragraphs.first()) + .and_then(|p| p.runs.first()) + .and_then(|r| r.props.fill.clone()) +} From aa0afa1462cb933b08663001d7a8d0fcdc0b26d2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 24 Oct 2026 21:14:43 -0700 Subject: [PATCH 2/8] Format: the Font dialog's color keeps inv, comp and gray, and a color's alpha A transform without a value (inv, comp, gray) is written as `true`, and an RGB or a system color's last value that is not opaque is written as `#RRGGBBAA`, so OK in the Font dialog keeps both. --- crates/engine/src/cmd/format.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index 31ab7290d..b1e01ae93 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -583,14 +583,15 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { use deckcraft_model::ColorBase; let mut o = serde_json::Map::new(); for m in &c.mods { - if let Some(v) = m.value() { - o.insert(m.xml_name().to_string(), json!(v)); - } + // `inv`, `comp` and `gray` have no value. + o.insert(m.xml_name().to_string(), m.value().map_or(json!(true), |v| json!(v))); } + // `#RRGGBB`, or `#RRGGBBAA` for a colour that is not opaque. + let hex = |c: &deckcraft_color::Rgba| if c.a == 255 { format!("#{}", c.hex()) } else { format!("#{}{:02X}", c.hex(), c.a) }; match &c.base { - ColorBase::Rgb { rgb } if o.is_empty() => json!(format!("#{}", rgb.hex())), + ColorBase::Rgb { rgb } if o.is_empty() => json!(hex(rgb)), ColorBase::Rgb { rgb } => { - o.insert("rgb".into(), json!(format!("#{}", rgb.hex()))); + o.insert("rgb".into(), json!(hex(rgb))); Value::Object(o) } ColorBase::Scheme { slot } => { @@ -603,7 +604,7 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { } ColorBase::System { name, last } => { o.insert("system".into(), json!(name)); - o.insert("last".into(), json!(format!("#{}", last.hex()))); + o.insert("last".into(), json!(hex(last))); Value::Object(o) } } @@ -1505,12 +1506,16 @@ mod tests { } #[test] - fn font_dialog_ok_keeps_preset_and_system_colors() { + fn font_dialog_ok_keeps_the_text_color() { + use deckcraft_color::Rgba; let preset = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::LumMod(50_000)); - let system = - ColorRef { base: ColorBase::System { name: "windowText".into(), last: deckcraft_color::Rgba::rgb(0x12, 0x34, 0x56) }, mods: vec![] } - .with(ColorTransform::Alpha(60_000)); - for color in [preset, system] { + let system = ColorRef { base: ColorBase::System { name: "windowText".into(), last: Rgba::rgb(0x12, 0x34, 0x56) }, mods: vec![] } + .with(ColorTransform::Alpha(60_000)); + // A transform without a value, and a colour that is not opaque. + let inverted = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::Inv); + let translucent = ColorRef { base: ColorBase::System { name: "windowText".into(), last: Rgba::rgba(0x12, 0x34, 0x56, 0x80) }, mods: vec![] }; + let rgb = ColorRef::rgb(Rgba::rgba(0x12, 0x34, 0x56, 0x80)); + for color in [preset, system, inverted, translucent, rgb] { let mut s = shape(); let fill = Fill::Solid { color: color.clone() }; format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap(); From 36bf5591c1b4f83bafed23539ef28faf4a8a89b4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 25 Oct 2026 08:57:11 -0700 Subject: [PATCH 3/8] Format: spell color the American way in the new comments --- crates/engine/src/cmd/format.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index 7fba5ce04..dcb4ba3b8 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -594,7 +594,7 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { // `inv`, `comp` and `gray` have no value. o.insert(m.xml_name().to_string(), m.value().map_or(json!(true), |v| json!(v))); } - // `#RRGGBB`, or `#RRGGBBAA` for a colour that is not opaque. + // `#RRGGBB`, or `#RRGGBBAA` for a color that is not opaque. let hex = |c: &deckcraft_color::Rgba| if c.a == 255 { format!("#{}", c.hex()) } else { format!("#{}{:02X}", c.hex(), c.a) }; match &c.base { ColorBase::Rgb { rgb } if o.is_empty() => json!(hex(rgb)), @@ -1552,7 +1552,7 @@ mod tests { let preset = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::LumMod(50_000)); let system = ColorRef { base: ColorBase::System { name: "windowText".into(), last: Rgba::rgb(0x12, 0x34, 0x56) }, mods: vec![] } .with(ColorTransform::Alpha(60_000)); - // A transform without a value, and a colour that is not opaque. + // A transform without a value, and a color that is not opaque. let inverted = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::Inv); let translucent = ColorRef { base: ColorBase::System { name: "windowText".into(), last: Rgba::rgba(0x12, 0x34, 0x56, 0x80) }, mods: vec![] }; let rgb = ColorRef::rgb(Rgba::rgba(0x12, 0x34, 0x56, 0x80)); From 37ac38cac784fb60fbff0358d539fdaaed629d97 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 25 Oct 2026 13:26:55 -0700 Subject: [PATCH 4/8] Format: the Font dialog keeps the order of a color's transforms The dialog's color parameter lists transforms in order under `mods`, in the form the model stores them, so OK keeps repeats such as [inv, inv] and transforms that are not in alphabetical order. Color parameters accept `mods` alongside the keyed form. --- crates/engine/src/cmd/format.rs | 11 +++++++---- crates/engine/src/cmd/mod.rs | 11 ++++++++++- crates/ui-egui/src/font_dialog_tests.rs | 9 ++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index dcb4ba3b8..8b8785aa8 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -590,9 +590,9 @@ fn font_edits(p: &Value) -> Result { fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { use deckcraft_model::ColorBase; let mut o = serde_json::Map::new(); - for m in &c.mods { - // `inv`, `comp` and `gray` have no value. - o.insert(m.xml_name().to_string(), m.value().map_or(json!(true), |v| json!(v))); + // An ordered list, so the order of the transforms and repeated ones such as `[inv, inv]` are kept. + if !c.mods.is_empty() { + o.insert("mods".into(), json!(c.mods)); } // `#RRGGBB`, or `#RRGGBBAA` for a color that is not opaque. let hex = |c: &deckcraft_color::Rgba| if c.a == 255 { format!("#{}", c.hex()) } else { format!("#{}{:02X}", c.hex(), c.a) }; @@ -1556,7 +1556,10 @@ mod tests { let inverted = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::Inv); let translucent = ColorRef { base: ColorBase::System { name: "windowText".into(), last: Rgba::rgba(0x12, 0x34, 0x56, 0x80) }, mods: vec![] }; let rgb = ColorRef::rgb(Rgba::rgba(0x12, 0x34, 0x56, 0x80)); - for color in [preset, system, inverted, translucent, rgb] { + // A repeated transform, and two transforms in an order that is not alphabetical. + let twice = inverted.clone().with(ColorTransform::Inv); + let ordered = ColorRef::scheme(SchemeSlot::Accent2).with(ColorTransform::LumOff(20_000)).with(ColorTransform::LumMod(50_000)); + for color in [preset, system, inverted, translucent, rgb, twice, ordered] { let mut s = shape(); let fill = Fill::Solid { color: color.clone() }; format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap(); diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index f0f1141fb..9c6aadd2f 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -372,7 +372,8 @@ pub(crate) fn ok() -> Result { /// Parse a colour param: `"#RRGGBB"`, `"accent1"`…, or `{"scheme":"accent2","lumMod":75000}`. /// A preset or system colour keeps its base: `{"preset":"red"}`, -/// `{"system":"windowText","last":"#000000"}`. +/// `{"system":"windowText","last":"#000000"}`. `"mods"` lists transforms in order: +/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`. pub(crate) fn color_param(p: &Value, key: &str) -> Option { let v = p.get(key)?; color_value(v) @@ -412,6 +413,10 @@ pub(crate) fn color_value(v: &Value) -> Option { c.mods.push(t); } } + // `mods` lists transforms in order, repeats included: `[{"op":"inv"},{"op":"lumMod","val":50000}]`. + if let Some(list) = o.get("mods") { + c.mods.extend(serde_json::from_value::>(list.clone()).ok()?); + } Some(c) } _ => None, @@ -669,5 +674,9 @@ mod tests { assert!(color_value(&json!({"preset": name})).is_some(), "{name}"); } assert!(color_value(&json!({"system": "3dDkShadow", "last": "#000000"})).is_some()); + let listed = color_value(&json!({"preset": "red", "mods": [{"op": "inv"}, {"op": "lumOff", "val": 20_000}, {"op": "inv"}]})); + let mods = vec![ColorTransform::Inv, ColorTransform::LumOff(20_000), ColorTransform::Inv]; + assert_eq!(listed, Some(ColorRef { base: ColorBase::Preset { name: "red".into() }, mods })); + assert_eq!(color_value(&json!({"preset": "red", "mods": [{"op": "notATransform"}]})), None); } } diff --git a/crates/ui-egui/src/font_dialog_tests.rs b/crates/ui-egui/src/font_dialog_tests.rs index e3076c62a..60b18f02d 100644 --- a/crates/ui-egui/src/font_dialog_tests.rs +++ b/crates/ui-egui/src/font_dialog_tests.rs @@ -66,9 +66,12 @@ fn command_t_opens_the_font_dialog_and_cancel_keeps_the_text() { #[test] fn preset_and_system_colors_show_in_the_dialog_and_ok_keeps_them() { - for (color, shown) in - [(json!({"preset": "red", "lumMod": 50000}), "red"), (json!({"system": "windowText", "last": "#123456", "alpha": 60000}), "#123456")] - { + for (color, shown) in [ + (json!({"preset": "red", "lumMod": 50000}), "red"), + (json!({"system": "windowText", "last": "#123456", "alpha": 60000}), "#123456"), + (json!({"preset": "red", "mods": [{"op": "inv"}, {"op": "inv"}]}), "red"), + (json!({"scheme": "accent2", "mods": [{"op": "lumOff", "val": 20000}, {"op": "lumMod", "val": 50000}]}), "accent2"), + ] { let mut h = harness(); h.state_mut().session.execute("format.fontDialog", &json!({ "color": color })).unwrap(); let before = fill_of(&h); From 2427c605116fa29484f6169270b1bcec842ffe74 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 3 Nov 2026 20:30:46 -0800 Subject: [PATCH 5/8] Format: test color mods lists with two mods, unknown ops, empty lists and keyed transforms Adds tests for the `mods` list of a color parameter: a preset with two transforms round-trips through the Font dialog, an unknown op makes the color invalid and the dialog returns an error without editing, an empty list gives the color with no transforms, and a scheme color with a keyed lumMod and a list keeps both, the keyed one first. --- crates/engine/src/cmd.rs | 22 ++++++++++++++++++++++ crates/engine/src/cmd/format.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 042733555..95800655d 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -688,4 +688,26 @@ mod tests { assert_eq!(listed, Some(ColorRef { base: ColorBase::Preset { name: "red".into() }, mods })); assert_eq!(color_value(&json!({"preset": "red", "mods": [{"op": "notATransform"}]})), None); } + + #[test] + fn color_mods_lists_follow_keyed_transforms() { + use deckcraft_color::{ColorTransform, SchemeSlot}; + use deckcraft_model::{ColorBase, ColorRef}; + use serde_json::json; + let red = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }; + assert_eq!(color_value(&json!({"preset": "red", "mods": []})), Some(red.clone()), "an empty list adds no transforms"); + let two = color_value(&json!({"preset": "red", "mods": [{"op": "lumMod", "val": 75_000}, {"op": "lumOff", "val": 10_000}]})); + assert_eq!(two, Some(red.with(ColorTransform::LumMod(75_000)).with(ColorTransform::LumOff(10_000)))); + // The keyed `lumMod` comes first, then the list in order. + let both = + color_value(&json!({"scheme": "accent2", "lumMod": 75_000, "mods": [{"op": "lumOff", "val": 10_000}, {"op": "lumMod", "val": 50_000}]})); + let want = ColorRef::scheme(SchemeSlot::Accent2) + .with(ColorTransform::LumMod(75_000)) + .with(ColorTransform::LumOff(10_000)) + .with(ColorTransform::LumMod(50_000)); + assert_eq!(both, Some(want)); + // An unknown op anywhere in the list, or a `mods` value that is not a list, makes the color invalid. + assert_eq!(color_value(&json!({"scheme": "accent2", "lumMod": 75_000, "mods": [{"op": "lumOff", "val": 10_000}, {"op": "sparkle"}]})), None); + assert_eq!(color_value(&json!({"preset": "red", "mods": {"op": "inv"}})), None); + } } diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index 47314a800..0336dc41f 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -422,6 +422,36 @@ mod tests { } } + #[test] + fn font_dialog_color_mods_round_trip_and_unknown_ops_are_errors() { + let two = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] } + .with(ColorTransform::LumMod(75_000)) + .with(ColorTransform::LumOff(10_000)); + let mut s = shape(); + let fill = Fill::Solid { color: two.clone() }; + format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap(); + s.ui_requests.clear(); + s.execute("format.fontDialog", &json!({})).unwrap(); + let Some(UiRequest::Dialog { params, .. }) = s.ui_requests.last().cloned() else { panic!("dialog request") }; + assert_eq!(params["color"], json!({"preset": "red", "mods": [{"op": "lumMod", "val": 75_000}, {"op": "lumOff", "val": 10_000}]})); + s.execute("format.fontDialog", &json!({"color": params["color"]})).unwrap(); + assert_eq!(props(&s).fill, Some(Fill::Solid { color: two.clone() })); + // An unknown op is an error, and the run keeps its color. + let n = s.doc().unwrap().history.undo.len(); + let err = s.execute("format.fontDialog", &json!({"color": {"preset": "red", "mods": [{"op": "sparkle"}]}})).unwrap_err(); + assert!(err.to_string().contains("color"), "{err}"); + assert_eq!(s.doc().unwrap().history.undo.len(), n); + assert_eq!(props(&s).fill, Some(Fill::Solid { color: two })); + // An empty list gives the color with no transforms. + s.execute("format.fontDialog", &json!({"color": {"preset": "red", "mods": []}})).unwrap(); + assert_eq!(props(&s).fill, Some(Fill::Solid { color: ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] } })); + // A scheme color with a keyed `lumMod` and a list keeps both, the keyed one first. + s.execute("format.fontDialog", &json!({"color": {"scheme": "accent2", "lumMod": 75_000, "mods": [{"op": "lumOff", "val": 10_000}]}})) + .unwrap(); + let scheme = ColorRef::scheme(SchemeSlot::Accent2).with(ColorTransform::LumMod(75_000)).with(ColorTransform::LumOff(10_000)); + assert_eq!(props(&s).fill, Some(Fill::Solid { color: scheme })); + } + #[test] fn font_dialog_with_no_fields_asks_the_ui_to_open() { let mut s = shape(); From c3aab3ac1ed3c0e1e8ddfaeb09fca184475b0d8f Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 4 Nov 2026 07:51:35 -0800 Subject: [PATCH 6/8] Format: a color's mods list holds at most 256 transforms color_value reads a mods list element by element instead of cloning it, and a list longer than MAX_COLOR_MODS (256) makes the color invalid. --- crates/engine/src/cmd.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 95800655d..e68dce576 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -378,12 +378,16 @@ pub(crate) fn ok() -> Result { /// Parse a colour param: `"#RRGGBB"`, `"accent1"`…, or `{"scheme":"accent2","lumMod":75000}`. /// A preset or system colour keeps its base: `{"preset":"red"}`, /// `{"system":"windowText","last":"#000000"}`. `"mods"` lists transforms in order: -/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`. +/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`. A list longer than [`MAX_COLOR_MODS`] +/// makes the color invalid. pub(crate) fn color_param(p: &Value, key: &str) -> Option { let v = p.get(key)?; color_value(v) } +/// Most transforms a color param's `"mods"` list may hold. +pub(crate) const MAX_COLOR_MODS: usize = 256; + pub(crate) fn color_value(v: &Value) -> Option { use deckcraft_model::ColorRef; match v { @@ -420,7 +424,13 @@ pub(crate) fn color_value(v: &Value) -> Option { } // `mods` lists transforms in order, repeats included: `[{"op":"inv"},{"op":"lumMod","val":50000}]`. if let Some(list) = o.get("mods") { - c.mods.extend(serde_json::from_value::>(list.clone()).ok()?); + let list = list.as_array()?; + if list.len() > MAX_COLOR_MODS { + return None; + } + for m in list { + c.mods.push(::deserialize(m).ok()?); + } } Some(c) } @@ -710,4 +720,13 @@ mod tests { assert_eq!(color_value(&json!({"scheme": "accent2", "lumMod": 75_000, "mods": [{"op": "lumOff", "val": 10_000}, {"op": "sparkle"}]})), None); assert_eq!(color_value(&json!({"preset": "red", "mods": {"op": "inv"}})), None); } + + #[test] + fn color_mods_lists_longer_than_the_cap_are_invalid() { + use deckcraft_color::ColorTransform; + use serde_json::json; + let at_cap = color_value(&json!({"preset": "red", "mods": vec![json!({"op": "inv"}); MAX_COLOR_MODS]})); + assert_eq!(at_cap.map(|c| c.mods), Some(vec![ColorTransform::Inv; MAX_COLOR_MODS])); + assert_eq!(color_value(&json!({"preset": "red", "mods": vec![json!({"op": "inv"}); MAX_COLOR_MODS + 1]})), None); + } } From d379334ea5d461b57081a9108a128d0b9208fb7e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 4 Nov 2026 08:04:50 -0800 Subject: [PATCH 7/8] Format: the Font dialog gives an unknown preset or system name as the color it draws A preset or system color name kept from an opened file that the color parser does not know is given to the dialog as its drawn RGB (black for a preset, the stored last color for a system color), so OK no longer rejects the color when another field changes. --- crates/engine/src/cmd/format.rs | 18 ++++++++++++++++++ crates/engine/src/cmd/format/font.rs | 9 ++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index 0336dc41f..83ec891b3 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -422,6 +422,24 @@ mod tests { } } + #[test] + fn font_dialog_ok_accepts_preset_and_system_names_it_does_not_know() { + use deckcraft_color::Rgba; + let last = Rgba::rgb(0x12, 0x34, 0x56); + let preset = ColorRef { base: ColorBase::Preset { name: "notAColor".into() }, mods: vec![] }.with(ColorTransform::LumMod(50_000)); + let system = ColorRef { base: ColorBase::System { name: "notAColor".into(), last }, mods: vec![] }; + for (color, drawn) in [(preset, ColorRef::rgb(Rgba::BLACK).with(ColorTransform::LumMod(50_000))), (system, ColorRef::rgb(last))] { + let mut s = shape(); + let fill = Fill::Solid { color }; + format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap(); + s.ui_requests.clear(); + s.execute("format.fontDialog", &json!({})).unwrap(); + let Some(UiRequest::Dialog { params, .. }) = s.ui_requests.last().cloned() else { panic!("dialog request") }; + s.execute("format.fontDialog", &json!({"color": params["color"], "size": 20.0})).unwrap(); + assert_eq!(props(&s).fill, Some(Fill::Solid { color: drawn }), "{}", params["color"]); + } + } + #[test] fn font_dialog_color_mods_round_trip_and_unknown_ops_are_errors() { let two = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] } diff --git a/crates/engine/src/cmd/format/font.rs b/crates/engine/src/cmd/format/font.rs index 57fd81c34..82d47568e 100644 --- a/crates/engine/src/cmd/format/font.rs +++ b/crates/engine/src/cmd/format/font.rs @@ -396,7 +396,14 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { } // `#RRGGBB`, or `#RRGGBBAA` for a color that is not opaque. let hex = |c: &deckcraft_color::Rgba| if c.a == 255 { format!("#{}", c.hex()) } else { format!("#{}{:02X}", c.hex(), c.a) }; - match &c.base { + // A preset or system name that `color_value` does not know (kept from an opened file) is given as + // the color it is drawn with, so OK does not reject it. + let base = match &c.base { + ColorBase::Preset { name } if !deckcraft_color::is_preset_name(name) => ColorBase::Rgb { rgb: deckcraft_color::Rgba::BLACK }, + ColorBase::System { name, last } if !deckcraft_color::is_system_name(name) => ColorBase::Rgb { rgb: *last }, + b => b.clone(), + }; + match &base { ColorBase::Rgb { rgb } if o.is_empty() => json!(hex(rgb)), ColorBase::Rgb { rgb } => { o.insert("rgb".into(), json!(hex(rgb))); From 25dc8b9dcfeb7ca211d4c5e490245f6b09408245 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 22:30:26 -0800 Subject: [PATCH 8/8] Format: OK with the Font dialog's Color untouched keeps the stored color The Font dialog's OK sent the `color` it was opened with back to `format.fontDialog`. For a preset or system name that `color_value` does not know, that color is the RGB it is drawn with, so OK replaced the name even when only another field changed. A color with more than 256 transforms made OK fail with "invalid `color`", so the other edits were not applied either. OK now sends no `color` when the Color field is untouched, and the text keeps its color reference as stored. An edited Color field is sent as before. Tests: a UI test opens the dialog on an unknown preset with 257 transforms and on an unknown system color, ticks Bold and presses OK; an engine test sends the dialog's fields without `color` and checks that the stored color, size and bold are kept. --- crates/engine/src/cmd/format.rs | 20 +++++++--- crates/engine/src/cmd/format/font.rs | 3 +- crates/ui-egui/src/dialogs/font.rs | 8 +--- .../ui-egui/src/dialogs/font_dialog_tests.rs | 37 +++++++++++++++++++ 4 files changed, 55 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index 7cee895ee..b00da2f89 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -431,20 +431,28 @@ mod tests { } #[test] - fn font_dialog_ok_accepts_preset_and_system_names_it_does_not_know() { + fn font_dialog_ok_without_color_keeps_colors_a_command_cannot_give() { use deckcraft_color::Rgba; let last = Rgba::rgb(0x12, 0x34, 0x56); let preset = ColorRef { base: ColorBase::Preset { name: "notAColor".into() }, mods: vec![] }.with(ColorTransform::LumMod(50_000)); let system = ColorRef { base: ColorBase::System { name: "notAColor".into(), last }, mods: vec![] }; - for (color, drawn) in [(preset, ColorRef::rgb(Rgba::BLACK).with(ColorTransform::LumMod(50_000))), (system, ColorRef::rgb(last))] { + // More transforms than a command's `mods` list may carry, as an opened file can hold. + let many = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![ColorTransform::Inv; crate::cmd::MAX_COLOR_MODS + 1] }; + for color in [preset, system, many] { let mut s = shape(); - let fill = Fill::Solid { color }; + let fill = Fill::Solid { color: color.clone() }; format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap(); s.ui_requests.clear(); s.execute("format.fontDialog", &json!({})).unwrap(); - let Some(UiRequest::Dialog { params, .. }) = s.ui_requests.last().cloned() else { panic!("dialog request") }; - s.execute("format.fontDialog", &json!({"color": params["color"], "size": 20.0})).unwrap(); - assert_eq!(props(&s).fill, Some(Fill::Solid { color: drawn }), "{}", params["color"]); + let Some(UiRequest::Dialog { mut params, .. }) = s.ui_requests.last().cloned() else { panic!("dialog request") }; + // OK with the Color field untouched and another field edited, as the dialog sends it. + params.as_object_mut().unwrap().remove("color"); + params["size"] = json!(20.0); + params["bold"] = json!(true); + s.execute("format.fontDialog", ¶ms).unwrap(); + let r = props(&s); + assert_eq!((r.size, r.bold), (Some(20.0), Some(true))); + assert_eq!(r.fill, Some(Fill::Solid { color })); } } diff --git a/crates/engine/src/cmd/format/font.rs b/crates/engine/src/cmd/format/font.rs index d5c604f10..13dcd5cd0 100644 --- a/crates/engine/src/cmd/format/font.rs +++ b/crates/engine/src/cmd/format/font.rs @@ -450,7 +450,8 @@ fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { // `#RRGGBB`, or `#RRGGBBAA` for a color that is not opaque. let hex = |c: &deckcraft_color::Rgba| if c.a == 255 { format!("#{}", c.hex()) } else { format!("#{}{:02X}", c.hex(), c.a) }; // A preset or system name that `color_value` does not know (kept from an opened file) is given as - // the color it is drawn with, so OK does not reject it. + // the color it is drawn with, so a caller that sends this `color` back gets a valid color. The + // Font dialog sends no `color` when its Color field is untouched, so the run keeps the stored name. let base = match &c.base { ColorBase::Preset { name } if !deckcraft_color::is_preset_name(name) => ColorBase::Rgb { rgb: deckcraft_color::Rgba::BLACK }, ColorBase::System { name, last } if !deckcraft_color::is_system_name(name) => ColorBase::Rgb { rgb: *last }, diff --git a/crates/ui-egui/src/dialogs/font.rs b/crates/ui-egui/src/dialogs/font.rs index a163e7bce..994f15126 100644 --- a/crates/ui-egui/src/dialogs/font.rs +++ b/crates/ui-egui/src/dialogs/font.rs @@ -147,13 +147,9 @@ pub(super) fn font_dialog(app: &mut SlideApp, ui: &mut Ui, d: &mut Dialog) -> bo "caps": d.get("caps", "none"), "spacing": spacing, }); + // An untouched Color field sends no `color`, so the text keeps its color reference as stored. let color = d.get("color", ""); - let seed = d.get("colorSeed", ""); - if color == seed { - if let Some(c) = d.params.get("color").filter(|v| !v.is_null()) { - params["color"] = c.clone(); - } - } else if !color.trim().is_empty() { + if color != d.get("colorSeed", "") && !color.trim().is_empty() { params["color"] = json!(color.trim()); } let _ = app.run("format.fontDialog", params); diff --git a/crates/ui-egui/src/dialogs/font_dialog_tests.rs b/crates/ui-egui/src/dialogs/font_dialog_tests.rs index 60b18f02d..b342223fd 100644 --- a/crates/ui-egui/src/dialogs/font_dialog_tests.rs +++ b/crates/ui-egui/src/dialogs/font_dialog_tests.rs @@ -87,6 +87,43 @@ fn preset_and_system_colors_show_in_the_dialog_and_ok_keeps_them() { } } +#[test] +fn ok_with_color_untouched_keeps_a_color_the_dialog_cannot_send_back() { + use deckcraft_color::{ColorTransform, Rgba}; + use deckcraft_model::{ColorBase, ColorRef, Fill}; + // Names that `color_value` does not know, as an opened file can hold, and more transforms than a + // command's `mods` list may carry. + let unknown_preset = ColorRef { base: ColorBase::Preset { name: "notAColor".into() }, mods: vec![ColorTransform::Inv; 257] }; + let unknown_system = ColorRef { base: ColorBase::System { name: "notAColor".into(), last: Rgba::rgb(0x12, 0x34, 0x56) }, mods: vec![] }; + for color in [unknown_preset, unknown_system] { + let mut h = harness(); + set_fill(&mut h, Fill::Solid { color }); + let before = fill_of(&h); + h.get_by_label("Font…").click(); + h.run_steps(2); + assert_eq!(h.state().dialog.as_ref().map(|d| d.id.as_str()), Some("font")); + h.get_by_label("Bold").click_accesskit(); + h.run_steps(1); + h.get_by_label("OK").click_accesskit(); + h.run_steps(1); + assert!(h.state().dialog.is_none(), "status {:?}", h.state().status); + assert_eq!(bold_of(&h), Some(true), "status {:?}", h.state().status); + assert_eq!(fill_of(&h), before); + } +} + +/// Sets the first run's fill directly, so the test can hold a color that a command would reject. +fn set_fill(h: &mut Harness<'_, SlideApp>, fill: deckcraft_model::Fill) { + let st = h.state_mut().session.active_mut().unwrap(); + let id = st.selection.shapes[0]; + let slide = st.selection.slide; + let doc = std::sync::Arc::make_mut(&mut st.doc); + let slide = std::sync::Arc::make_mut(&mut doc.slides[slide]); + let shape = slide.shapes.iter_mut().find(|sh| sh.id == id).unwrap(); + let run = shape.text.as_mut().and_then(|t| t.paragraphs.first_mut()).and_then(|p| p.runs.first_mut()).unwrap(); + run.props.fill = Some(fill); +} + fn fill_of(h: &Harness<'_, SlideApp>) -> Option { let st = h.state().session.active().unwrap(); st.selected_shapes()