From b57a4ff927546eb04fe819b4c0ecbbb7de47fe2e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 06:20:07 -0800 Subject: [PATCH] Document Tools: Flood Fill Artboards keeps an RGB current fill in a CMYK document Two fixes for review threads on this pull request. The dialog sent an RGB current fill's working components without keepModel, so in a CMYK document the engine converted the fill to CMYK. While the shown color is left unchanged, the dialog now also sends keepModel: true. An edited color converts to the document's model as before. With New Art Has Basic Appearance off and nothing selected, a preview that drew new art took its new-art paints from that art, and the next preview then put back the paints the interaction began with. A second rectangle preview after a fill preview was drawn with the starting fill instead of the previewed one. The interaction now keeps the paints from before the inheritance and the next preview starts from them. A fill chosen during the drag updates them too. After a failed preview, a cancel or a commit puts back the paints the interaction began with. --- .../engine/src/cmd/addons/document/tests.rs | 61 ++++++++++++++ crates/engine/src/cmd/newart.rs | 8 +- crates/engine/src/interaction.rs | 5 +- crates/engine/src/session/edit.rs | 80 ++++++++++++------- .../engine/src/tests/interaction_commands.rs | 19 +++++ crates/ui-egui/src/addons/document.rs | 10 ++- crates/ui-egui/src/addons/document/tests.rs | 20 +++++ docs/mcp.md | 3 +- 8 files changed, 167 insertions(+), 39 deletions(-) diff --git a/crates/engine/src/cmd/addons/document/tests.rs b/crates/engine/src/cmd/addons/document/tests.rs index c0e2e0e894..3114142ba8 100644 --- a/crates/engine/src/cmd/addons/document/tests.rs +++ b/crates/engine/src/cmd/addons/document/tests.rs @@ -644,6 +644,67 @@ fn flood_fill_previews_without_color_use_the_fill_the_interaction_began_with() { assert_eq!(fill, red, "the fill the interaction began with, not the previewed color"); } +/// With New Art Has Basic Appearance off and nothing selected, rectangle previews that follow a fill +/// preview each draw with that preview's fill, not the fill the interaction began with. +#[test] +fn rectangle_previews_after_a_fill_preview_keep_its_fill() { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + let red = s.paint.fill.clone(); + s.begin_interaction("Rectangle").unwrap(); + s.preview("paint.setFill", &json!({"color": "#0000ff"})).unwrap(); + let blue = s.paint.fill.clone(); + assert_ne!(blue, red); + for n in 0..2 { + let r = s.preview("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + let id = NodeId(r["id"].as_u64().unwrap()); + let fill = s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint(); + assert_eq!(fill, blue, "rectangle preview {n} draws with the previewed fill"); + } + s.commit_interaction().unwrap(); +} + +/// A cancel or a commit after a fill preview, a rectangle preview and a failed rectangle preview +/// puts back the fill the interaction began with. +#[test] +fn cancel_or_commit_after_a_failed_rectangle_preview_restores_the_starting_fill() { + for commit in [false, true] { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + let before = s.paint.clone(); + s.begin_interaction("Rectangle").unwrap(); + s.preview("paint.setFill", &json!({"color": "#0000ff"})).unwrap(); + s.preview("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + assert!(s.preview("shape.rectangle", &json!({"x": 10, "y": 10, "width": "wide", "height": 20})).is_err()); + if commit { + s.commit_interaction().unwrap(); + } else { + s.cancel_interaction().unwrap(); + } + assert_eq!(s.paint, before, "commit {commit}"); + } +} + +/// A failed fill preview after a fill preview and a rectangle preview leaves the starting fill for +/// the next rectangle preview. +#[test] +fn rectangle_preview_after_a_failed_fill_preview_uses_the_starting_fill() { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + let red = s.paint.fill.clone(); + s.begin_interaction("Rectangle").unwrap(); + s.preview("paint.setFill", &json!({"color": "#0000ff"})).unwrap(); + s.preview("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + assert!(s.preview("paint.setFill", &json!({"color": "not a color"})).is_err()); + let r = s.preview("shape.rectangle", &json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); + let id = NodeId(r["id"].as_u64().unwrap()); + assert_eq!(s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint(), red); + s.cancel_interaction().unwrap(); +} + #[test] fn paint_swap_previews_from_an_empty_selection_commit_one_swap() { let mut s = boards(); diff --git a/crates/engine/src/cmd/newart.rs b/crates/engine/src/cmd/newart.rs index 09c56e1473..c10fe26c53 100644 --- a/crates/engine/src/cmd/newart.rs +++ b/crates/engine/src/cmd/newart.rs @@ -236,10 +236,12 @@ impl Session { return; } if self.previewing - && let Some(it) = self.active_mut().and_then(|st| st.interaction.as_mut()) - && it.selection.is_empty() + && self.active().and_then(|st| st.interaction.as_ref()).is_some_and(|it| it.selection.is_empty() && it.inherited_preview_paint.is_none()) { - it.inherited_preview_paint = true; + let before = self.paint.clone(); + if let Some(it) = self.active_mut().and_then(|st| st.interaction.as_mut()) { + it.inherited_preview_paint = Some(before); + } } let NewArtSource { appearance, opacity, blend, style, .. } = source; let kept_fill = fill.then(|| self.paint.fill.clone()); diff --git a/crates/engine/src/interaction.rs b/crates/engine/src/interaction.rs index 171f2adba0..d3b39bf831 100644 --- a/crates/engine/src/interaction.rs +++ b/crates/engine/src/interaction.rs @@ -23,8 +23,9 @@ pub struct Interaction { /// A Flood Fill Artboards preview ran: a cancel puts `paint` back and does not take the /// selection's appearance as the new-art paints. pub(crate) preserve_paint_on_cancel: bool, - /// The preview inherited new-art paints from a selection absent at interaction start. - pub(crate) inherited_preview_paint: bool, + /// The preview inherited new-art paints from a selection absent at interaction start: the + /// new-art paints from before that, which the next preview starts from. + pub(crate) inherited_preview_paint: Option, /// An accepted batch preview uses these final paints after changing the document color mode. pub(crate) preview_paint: Option, /// Conversion of new-art paints waits until a color-mode preview is accepted. diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index 025cbb5258..5195b2df14 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -394,32 +394,34 @@ impl Session { // A change the command made to the new-art paints stays when the next preview or a cancel // puts back the paints the interaction began with (the Live Paint Bucket's colors); the // parts it left alone keep the interaction's. The pattern lists every field, so a field - // added to `PaintDefaults` does not compile until it is handled here. + // added to `PaintDefaults` does not compile until it is handled here. The paints a preview + // took its new-art paints over from take the change as well. let vectorcraft_tools::PaintDefaults { fill, stroke, stroke_width, appearance, opacity, blend, style, inherited } = &self.paint; - let start = &mut it.paint; - if *fill != paint.fill { - start.fill.clone_from(fill); - } - if *stroke != paint.stroke { - start.stroke.clone_from(stroke); - } - if *stroke_width != paint.stroke_width { - start.stroke_width = *stroke_width; - } - if *appearance != paint.appearance { - start.appearance.clone_from(appearance); - } - if *opacity != paint.opacity { - start.opacity = *opacity; - } - if *blend != paint.blend { - start.blend = *blend; - } - if *style != paint.style { - start.style.clone_from(style); - } - if *inherited != paint.inherited { - start.inherited = *inherited; + for start in std::iter::once(&mut it.paint).chain(it.inherited_preview_paint.as_mut()) { + if *fill != paint.fill { + start.fill.clone_from(fill); + } + if *stroke != paint.stroke { + start.stroke.clone_from(stroke); + } + if *stroke_width != paint.stroke_width { + start.stroke_width = *stroke_width; + } + if *appearance != paint.appearance { + start.appearance.clone_from(appearance); + } + if *opacity != paint.opacity { + start.opacity = *opacity; + } + if *blend != paint.blend { + start.blend = *blend; + } + if *style != paint.style { + start.style.clone_from(style); + } + if *inherited != paint.inherited { + start.inherited = *inherited; + } } let active = self.active().is_some_and(|d| d.uid == uid); let Some(st) = self.docs.iter_mut().find(|d| d.uid == uid) else { return }; @@ -550,7 +552,7 @@ impl Session { paint, used_defaults_only: false, preserve_paint_on_cancel: false, - inherited_preview_paint: false, + inherited_preview_paint: None, preview_paint: None, color_mode_paints: None, tool_paints: false, @@ -580,7 +582,7 @@ impl Session { // `defaultsOnly: true`, and one after it. let tool_paints = self.tool_paints(false) || self.tool_paints(true); let new_art_basic = self.prefs.new_art_basic; - let (paint, restore_paint) = { + let (paint, restore_paint, inherited) = { let menu = self.menu.clone(); let st = self.doc_mut()?; let Some(it) = &mut st.interaction else { return Err(EngineError::Other("no interaction in progress".into())) }; @@ -602,10 +604,11 @@ impl Session { || it.used_defaults_only || it.preview.as_ref().is_some_and(|(id, p)| id == "command.batch" || cmd::asks_defaults_only(p)) || it.color_mode_paints.is_some() - || it.inherited_preview_paint || (!new_art_basic && it.selection.is_empty() && it.preview.is_some() && cmd.starts_with("paint.")); let paint = it.paint.clone(); - it.inherited_preview_paint = false; + // A preview that took the new-art paints from the art it drew puts back the paints it + // started from, which an earlier paint preview may have set. + let inherited = it.inherited_preview_paint.take(); it.preview_paint = None; it.color_mode_paints = None; it.deferred_color_mode = None; @@ -622,10 +625,12 @@ impl Session { // undone with it. st.restore_isolation(isolation, &undone); cmd::globaledit::follow_history(st); - (paint, restore_paint) + (paint, restore_paint, inherited) }; if restore_paint { self.paint = paint.clone(); + } else if let Some(before) = &inherited { + self.paint.clone_from(before); } self.pending_paint = None; // The previous preview's Assign Profile is undone with it. @@ -651,6 +656,10 @@ impl Session { // A failed preview leaves the document at the snapshot, so nothing is left to commit // (not even an earlier successful preview, which the reset above already undid). it.preview = r.is_ok().then(|| (cmd.to_string(), params)); + // After a failed preview a cancel still puts back the paints the interaction began with. + if r.is_err() && !restore_paint && it.inherited_preview_paint.is_none() { + it.inherited_preview_paint.clone_from(&inherited); + } if it.color_mode_paints.is_some() { it.preview_paint = batch_paint.clone(); } @@ -679,6 +688,15 @@ impl Session { if let Some(paint) = self.active().and_then(|st| st.interaction.as_ref()).and_then(|it| it.preview_paint.clone()) { self.paint = paint; } + // A failed last preview leaves nothing to commit, so the paints an earlier preview set go too. + if let Some(paint) = self + .active() + .and_then(|st| st.interaction.as_ref()) + .filter(|it| it.preview.is_none() && it.inherited_preview_paint.is_some()) + .map(|it| it.paint.clone()) + { + self.paint = paint; + } self.remember_pending_paint(); cmd::colormgmt::commit_new_art_colors(self)?; let paint = self.paint.clone(); @@ -788,7 +806,7 @@ impl Session { || it.preserve_paint_on_cancel || it.preview.as_ref().is_some_and(|(id, p)| id == "command.batch" || cmd::asks_defaults_only(p)) || it.color_mode_paints.is_some() - || it.inherited_preview_paint + || it.inherited_preview_paint.is_some() || (!self.prefs.new_art_basic && it.preview.is_some() && it.selection.is_empty()) }) .map(|it| it.paint.clone()) diff --git a/crates/engine/src/tests/interaction_commands.rs b/crates/engine/src/tests/interaction_commands.rs index d0d12cce94..00588ecf0c 100644 --- a/crates/engine/src/tests/interaction_commands.rs +++ b/crates/engine/src/tests/interaction_commands.rs @@ -808,6 +808,25 @@ fn a_fill_chosen_during_a_bucket_drag_stays() { } } +/// With New Art Has Basic Appearance off, a fill chosen during a rectangle drag from an empty +/// selection is the fill the drag's next preview and its release draw with. +#[test] +fn a_fill_chosen_during_a_rectangle_drag_draws_the_rectangle() { + for real_time in [true, false] { + let at = format!("real time {real_time}"); + let (mut s, _) = session(real_time); + s.prefs.new_art_basic = false; + pointer(&mut s, PointerKind::Down, 100.0, 100.0); + pointer(&mut s, PointerKind::Drag, 150.0, 150.0); + s.execute("paint.setFill", &json!({"color": "#0000ff", "ids": []})).unwrap(); + let blue = s.paint.fill.clone(); + pointer(&mut s, PointerKind::Drag, 200.0, 180.0); + pointer(&mut s, PointerKind::Up, 200.0, 180.0); + let drawn = s.doc().unwrap().selection.objects[0]; + assert_eq!(s.doc().unwrap().doc.node(drawn).unwrap().appearance.fill_paint(), blue, "{at}"); + } +} + /// A command run during a Quick Pen press still runs inside the press, so it records no undo /// step of its own while the press is held. #[test] diff --git a/crates/ui-egui/src/addons/document.rs b/crates/ui-egui/src/addons/document.rs index 33b96eefe1..64833ab120 100644 --- a/crates/ui-egui/src/addons/document.rs +++ b/crates/ui-egui/src/addons/document.rs @@ -88,11 +88,17 @@ fn flood_params(fields: &DialogFields) -> Value { if fields.text("fill") == Some("color") { let picked = fields.color("color").unwrap_or(GRAY); // The opening fill, while its shown color is unchanged, keeps its own working components, also - // those outside the displayable sRGB range. + // those outside the displayable sRGB range, and stays RGB in a CMYK document. let start = (fields.color("startShown") == Some(picked)) .then(|| fields.text("startColor").and_then(|t| serde_json::from_str::(t).ok())) .flatten(); - p["color"] = start.unwrap_or_else(|| working_rgb(picked)); + match start { + Some(start) => { + p["color"] = start; + p["keepModel"] = json!(true); + } + None => p["color"] = working_rgb(picked), + } } p["layer"] = json!(fields.text("layer").unwrap_or(FLOOD_LAYER)); p diff --git a/crates/ui-egui/src/addons/document/tests.rs b/crates/ui-egui/src/addons/document/tests.rs index 6ceed3da2b..b8ef41e65b 100644 --- a/crates/ui-egui/src/addons/document/tests.rs +++ b/crates/ui-egui/src/addons/document/tests.rs @@ -379,6 +379,26 @@ fn flood_fill_color_keeps_an_out_of_srgb_current_fill() { }); } +/// In a CMYK document, an RGB current fill kept by picking Color without editing it stays RGB. +#[test] +fn flood_fill_color_keeps_an_rgb_current_fill_in_a_cmyk_document() { + use vectorcraft_color::Color; + let mut app = boards(); + app.run("file.documentColorMode", json!({"mode": "cmyk"})).unwrap(); + app.run("paint.setFill", json!({"color": [0.6, 0.5, 0.4], "keepModel": true})).unwrap(); + click(&mut app, "Flood Fill Artboards…"); + let id = host::dialog_id(&app); + host::set(&app, id, "fill", json!("color")); + assert!(host::confirm(&app, id)); + host::frame(&mut app); + let art = bottom_art(&app); + assert_eq!(art.len(), 1); + let Some(Color::Rgb { r, g, b }) = art[0].1.color() else { panic!("an RGB fill: {:?}", art[0].1) }; + for (got, want) in [(r, 0.6), (g, 0.5), (b, 0.4)] { + assert!((got - want).abs() < 1e-3, "{:?} is the current fill [0.6, 0.5, 0.4]", [r, g, b]); + } +} + #[test] fn flood_fill_layer_name_limit_counts_characters_after_trimming() { let mut app = boards(); diff --git a/docs/mcp.md b/docs/mcp.md index cb9092a33a..cdf564d439 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -17528,7 +17528,8 @@ The desktop app's Flood Fill Artboards… dialog picks the active artboard or al current fill or a color, and the layer, and previews on the canvas. The dialog disables OK for an empty or overlong trimmed layer name, a destination layer that holds a clipping mask, or Current Fill when there is no current fill. Color opens on the current fill's color; for an RGB current fill, while that color is left unchanged, the dialog sends -the fill's own working RGB components, also those outside the sRGB gamut. +the fill's own working RGB components, also those outside the sRGB gamut, with `keepModel: true`, so the fill stays RGB +in a CMYK document. → `{ids: [rectangle id…], layer, created}` ```json