From 25dc8b9dcfeb7ca211d4c5e490245f6b09408245 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 22:30:26 -0800 Subject: [PATCH] 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()