diff --git a/crates/engine/src/cmd/addons/document.rs b/crates/engine/src/cmd/addons/document.rs index b149a44da2..1fa433c692 100644 --- a/crates/engine/src/cmd/addons/document.rs +++ b/crates/engine/src/cmd/addons/document.rs @@ -67,13 +67,25 @@ pub fn specs() -> Vec { "Flood Fill Artboards", ["Object", "Plug-ins", "Document Tools"], None, - "{artboards?: [index…] (default: the active artboard), all?: bool (true: every artboard; not with artboards), color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused, as is a component too large to be finite; default: the current fill, using the live fill after earlier batch steps or the fill the preview began with in a Flood Fill preview, an error when that is None), layer?: name (default: \"Flood Fill\"; trimmed, nonempty, at most 255 characters after trimming)} add an editable rectangle exactly the size of each artboard, painted with the color and no stroke, on top of the top-level layer of that name, made at the bottom of the layers when there is none. A destination layer that holds a clipping mask is rejected before any edit. A layer that already exists keeps its lock and visibility; the rectangles are selected unless that layer is locked or hidden, and the current layer stays as it was. One undo step. Every value given is checked: null and values of the wrong type are errors. Refused while an opacity mask is edited → {ids: [rectangle id…], layer: layer id, created: bool (a new layer was made)}", - super::super::maskedit::outside_mask_editing, + "{artboards?: [index…] (default: the active artboard), all?: bool (true: every artboard; not with artboards), color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused, as is a component too large to be finite; default: the current fill, using the live fill after earlier batch steps or the fill the preview began with in a Flood Fill preview, an error when that is None), layer?: name (default: \"Flood Fill\"; trimmed, nonempty, at most 255 characters after trimming)} add an editable rectangle exactly the size of each artboard, painted with the color and no stroke, on top of the top-level layer of that name, made at the bottom of the layers when there is none. A destination layer that holds a clipping mask is rejected before any edit. A layer that already exists keeps its lock and visibility; the rectangles are selected unless that layer is locked or hidden, and the current layer stays as it was. One undo step. Params must be an object or null (omitted options). Every option given is checked: null and values of the wrong type are errors. Refused while a symbol, pattern or opacity mask is edited → {ids: [rectangle id…], layer: layer id, created: bool (a new layer was made)}", + flood_fill_enabled, flood_fill::flood_fill_artboards ), ] } +fn flood_fill_enabled(s: &Session) -> std::result::Result<(), String> { + super::super::maskedit::outside_mask_editing(s)?; + let st = s.active().ok_or("no document open")?; + if st.doc.symbol_edit.is_some() { + return Err("finish editing the symbol before filling artboards".into()); + } + if st.doc.pattern_edit.is_some() { + return Err("finish editing the pattern before filling artboards".into()); + } + Ok(()) +} + /// A document is open and the time can be told. fn has_clock(s: &Session) -> std::result::Result<(), String> { has_doc(s)?; diff --git a/crates/engine/src/cmd/addons/document/flood_fill.rs b/crates/engine/src/cmd/addons/document/flood_fill.rs index 3a698aae94..b0d63420bb 100644 --- a/crates/engine/src/cmd/addons/document/flood_fill.rs +++ b/crates/engine/src/cmd/addons/document/flood_fill.rs @@ -24,6 +24,12 @@ const MAX_NAME: usize = 255; pub(super) fn flood_fill_artboards(s: &mut Session, p: &Value) -> Result { let st = s.doc()?; let d = &st.doc; + if d.symbol_edit.is_some() { + return Err(bad(COMMAND, "finish editing the symbol before filling artboards")); + } + if d.pattern_edit.is_some() { + return Err(bad(COMMAND, "finish editing the pattern before filling artboards")); + } if d.artboards.is_empty() { return Err(bad(COMMAND, "the document has no artboards")); } diff --git a/crates/engine/src/cmd/addons/document/tests.rs b/crates/engine/src/cmd/addons/document/tests.rs index 038bffa653..c6ce4e6241 100644 --- a/crates/engine/src/cmd/addons/document/tests.rs +++ b/crates/engine/src/cmd/addons/document/tests.rs @@ -322,6 +322,95 @@ fn flood_fill_rejects_a_destination_layer_with_a_clipping_mask_without_an_edit() assert_eq!(s.paint, before.3); } +#[test] +fn flood_fill_refuses_symbol_and_pattern_editing_without_an_edit() { + for symbol in [true, false] { + let mut s = boards(); + if symbol { + s.execute("symbol.new", &json!({"name": "Flood Fill"})).unwrap(); + s.execute("symbol.edit", &json!({"name": "Flood Fill"})).unwrap(); + } else { + s.execute("object.pattern.make", &json!({"name": "Tile"})).unwrap(); + } + let st = s.doc().unwrap(); + let layer = if symbol { st.doc.symbol_edit.as_ref().unwrap().layer } else { st.doc.pattern_edit.as_ref().unwrap().layer }; + let name = st.doc.node(layer).unwrap().name.clone().unwrap(); + let before = (st.doc.clone(), st.selection.clone(), st.history.undo.len(), s.paint.clone()); + for params in [json!({}), json!({"layer": name}), json!({"layer": "Paper"})] { + let error = s.execute(FLOOD, ¶ms).unwrap_err().to_string(); + assert!(error.contains(if symbol { "finish editing the symbol" } else { "finish editing the pattern" }), "{error}"); + let st = s.doc().unwrap(); + assert_eq!(*st.doc, *before.0); + assert_eq!(st.selection, before.1); + assert_eq!(st.history.undo.len(), before.2); + assert_eq!(s.paint, before.3); + } + } +} + +#[test] +fn flood_fill_is_disabled_while_editing_symbols_or_patterns() { + let spec = crate::find_command(FLOOD).unwrap(); + for symbol in [true, false] { + let mut s = boards(); + assert!(spec.info(&s).enabled); + if symbol { + s.execute("symbol.new", &json!({"name": "Flood Fill"})).unwrap(); + s.execute("symbol.edit", &json!({"name": "Flood Fill"})).unwrap(); + } else { + s.execute("object.pattern.make", &json!({"name": "Tile"})).unwrap(); + } + let info = spec.info(&s); + assert!(!info.enabled); + assert!(info.disabled_reason.unwrap().contains(if symbol { "finish editing the symbol" } else { "finish editing the pattern" })); + } +} + +#[test] +fn flood_fill_action_batch_rejects_non_object_params_at_preflight() { + let mut s = boards(); + let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len(), s.paint.clone()); + for params in [json!([]), json!([{}]), json!(5), json!(true), json!("all")] { + let batch = json!({"commands": [{"command": FLOOD, "params": params}]}); + for error in [s.check_params("action.batch", &batch).unwrap_err(), s.execute("action.batch", &batch).unwrap_err()] { + let error = error.to_string(); + assert!(error.contains("action step 1 (`addon.document.floodFillArtboards`)"), "{error}"); + assert!(error.contains("params must be an object or null"), "{error}"); + } + assert_eq!(*s.doc().unwrap().doc, *before.0); + assert_eq!(s.doc().unwrap().history.undo.len(), before.1); + assert_eq!(s.paint, before.2); + } +} + +#[test] +fn flood_fill_requires_object_or_null_params_directly_and_in_a_batch() { + for batch in [false, true] { + let mut s = boards(); + for params in [json!([]), json!([{}]), json!(5), json!(true), json!("all")] { + let st = s.doc().unwrap(); + let before = (st.doc.clone(), st.selection.clone(), st.history.undo.len(), s.paint.clone()); + let result = if batch { + s.execute("command.batch", &json!({"commands": [{"command": FLOOD, "params": params}]})) + } else { + s.execute(FLOOD, ¶ms) + }; + assert!(result.is_err(), "batch: {batch}, params: {params}"); + let st = s.doc().unwrap(); + assert_eq!(*st.doc, *before.0); + assert_eq!(st.selection, before.1); + assert_eq!(st.history.undo.len(), before.2); + assert_eq!(s.paint, before.3); + } + let result = if batch { + s.execute("command.batch", &json!({"commands": [{"command": FLOOD}]})).unwrap()["results"][0].clone() + } else { + s.execute(FLOOD, &Value::Null).unwrap() + }; + assert_eq!(result["ids"].as_array().unwrap().len(), 1); + } +} + #[test] fn flood_fill_refuses_bad_params_and_a_none_fill() { let mut s = boards(); @@ -378,6 +467,82 @@ 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"); } +#[test] +fn paint_swap_previews_from_an_empty_selection_commit_one_swap() { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + s.execute("paint.setStroke", &json!({"color": "#00ff00"})).unwrap(); + let before = s.paint.clone(); + assert_ne!(before.fill, before.stroke); + s.begin_interaction("Swap").unwrap(); + for _ in 0..2 { + s.preview("paint.swap", &json!({})).unwrap(); + assert_eq!((&s.paint.fill, &s.paint.stroke), (&before.stroke, &before.fill)); + } + s.commit_interaction().unwrap(); + assert_eq!((&s.paint.fill, &s.paint.stroke), (&before.stroke, &before.fill)); +} + +#[test] +fn paint_preview_replacement_from_an_empty_selection_uses_the_starting_defaults() { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + let before = s.paint.clone(); + s.begin_interaction("Paint").unwrap(); + s.preview("paint.setFill", &json!({"color": "#0000ff"})).unwrap(); + assert_ne!(s.paint.fill, before.fill); + s.preview("paint.swap", &json!({})).unwrap(); + assert_eq!((&s.paint.fill, &s.paint.stroke), (&before.stroke, &before.fill)); + s.commit_interaction().unwrap(); + assert_eq!((&s.paint.fill, &s.paint.stroke), (&before.stroke, &before.fill)); +} + +#[test] +fn paint_previews_from_an_empty_selection_restore_defaults_on_cancel() { + for (command, params) in [ + ("paint.swap", json!({})), + ("paint.setFill", json!({"color": "#0000ff"})), + ("paint.setStroke", json!({"color": "#0000ff"})), + ] { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + let before = s.paint.clone(); + s.begin_interaction("Paint").unwrap(); + s.preview(command, ¶ms).unwrap(); + assert_ne!(s.paint, before, "{command}"); + s.cancel_interaction().unwrap(); + assert_eq!(s.paint, before, "{command}"); + } +} + +#[test] +fn paint_preview_from_an_empty_selection_keeps_its_fill_for_the_next_step() { + for cancel in [false, true] { + 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("Fill").unwrap(); + s.preview("paint.setFill", &json!({"color": "#0000ff"})).unwrap(); + let blue = s.paint.fill.clone(); + assert_ne!(blue, red); + assert!(s.doc().unwrap().selection.is_empty()); + if cancel { + s.cancel_interaction().unwrap(); + assert_eq!(s.paint.fill, red); + } else { + let result = s.preview("shape.rectangle", &json!({"x": 40, "y": 40, "width": 10, "height": 10})).unwrap(); + let id = NodeId(result["id"].as_u64().unwrap()); + assert_eq!(s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint(), blue); + assert_eq!(s.paint.fill, blue); + s.commit_interaction().unwrap(); + } + } +} + /// With New Art Has Basic Appearance off and nothing selected, a canceled preview in a color puts /// back the new-art fill that its selected rectangles had given their color to. #[test] diff --git a/crates/engine/src/cmd/newart.rs b/crates/engine/src/cmd/newart.rs index f43221120c..09c56e1473 100644 --- a/crates/engine/src/cmd/newart.rs +++ b/crates/engine/src/cmd/newart.rs @@ -235,6 +235,12 @@ impl Session { if matches!(&self.defaults_only_ran, Some(Some(ran)) if *ran == source) { return; } + if self.previewing + && let Some(it) = self.active_mut().and_then(|st| st.interaction.as_mut()) + && it.selection.is_empty() + { + it.inherited_preview_paint = true; + } let NewArtSource { appearance, opacity, blend, style, .. } = source; let kept_fill = fill.then(|| self.paint.fill.clone()); let kept_stroke = stroke.then(|| (self.paint.stroke.clone(), self.paint.stroke_width)); diff --git a/crates/engine/src/cmd/params.rs b/crates/engine/src/cmd/params.rs index dcc3eade04..6600530719 100644 --- a/crates/engine/src/cmd/params.rs +++ b/crates/engine/src/cmd/params.rs @@ -1089,6 +1089,9 @@ pub fn check_params(id: &str, params: &Value) -> Result<()> { /// `command.batch` or `action.batch` step aren't checked (running it reports that), however deep they go. fn check_params_in(id: &str, params: &Value, in_batch: bool) -> Result<()> { let Some(spec) = find_command(id) else { return Ok(()) }; + if id == "addon.document.floodFillArtboards" && !params.is_null() && !params.is_object() { + return Err(bad(id, "params must be an object or null")); + } if let ("effect.apply" | "appearance.addEffect", Some(effect), Some(given)) = (id, super::effectcmd::requested_effect(params), params.get("params")) { diff --git a/crates/engine/src/interaction.rs b/crates/engine/src/interaction.rs index a2b9417461..5b28071e55 100644 --- a/crates/engine/src/interaction.rs +++ b/crates/engine/src/interaction.rs @@ -20,6 +20,8 @@ pub struct Interaction { /// A preview of the interaction gave `defaultsOnly: true`: every later preview and a cancel put /// `paint` back, whichever preview is the latest. pub(crate) used_defaults_only: bool, + /// The preview inherited new-art paints from a selection absent at interaction start. + pub(crate) inherited_preview_paint: bool, /// 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 61e62d0dd8..b7cfe25e0e 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -549,6 +549,7 @@ impl Session { tool: false, paint, used_defaults_only: false, + inherited_preview_paint: false, preview_paint: None, color_mode_paints: None, tool_paints: false, @@ -577,7 +578,7 @@ impl Session { // preview starts from the colors the interaction began with; so does a preview with // `defaultsOnly: true`, and one after it. let tool_paints = self.tool_paints(false) || self.tool_paints(true); - let inherits = !self.prefs.new_art_basic; + let new_art_basic = self.prefs.new_art_basic; let (paint, restore_paint) = { let menu = self.menu.clone(); let st = self.doc_mut()?; @@ -599,8 +600,10 @@ 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() - || (inherits && it.preview.is_some() && it.selection.is_empty()); + || 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; it.preview_paint = None; it.color_mode_paints = None; it.deferred_color_mode = None; @@ -772,7 +775,6 @@ impl Session { // With New Art Has Basic Appearance off, a preview that selected new art from an empty // selection gave that art's appearance to the new-art paints; after the undo nothing is // selected to take them from, so the cancel puts back the paints the interaction began with. - let inherits = !self.prefs.new_art_basic; if let Some(paint) = self .active() .and_then(|st| st.interaction.as_ref()) @@ -782,7 +784,8 @@ 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() - || (inherits && it.preview.is_some() && it.selection.is_empty()) + || it.inherited_preview_paint + || (!self.prefs.new_art_basic && it.preview.is_some() && it.selection.is_empty()) }) .map(|it| it.paint.clone()) { diff --git a/crates/ui-egui/src/addons/document.rs b/crates/ui-egui/src/addons/document.rs index 0c1d84b015..eedb5ef60a 100644 --- a/crates/ui-egui/src/addons/document.rs +++ b/crates/ui-egui/src/addons/document.rs @@ -97,6 +97,7 @@ fn flood_params(fields: &DialogFields) -> Value { pub(crate) fn open_flood_fill(app: &mut VectorcraftApp) -> Result { let board = app.view().map_or(0, |v| v.artboard); let fill = &app.session.paint.fill; + let no_current_fill = fill.is_none(); let color = fill.color().map_or(GRAY, |c| c.to_rgb().map(|v| v.clamp(0.0, 1.0))); let fields = DialogFields::from_pairs([ ("artboards", FieldValue::Text("active".into())), @@ -121,6 +122,8 @@ pub(crate) fn open_flood_fill(app: &mut VectorcraftApp) -> Result Some(tl!("Use at most 255 characters for the layer name.")) } else if layer_clips.get(name) == Some(&true) { Some(tl!("The layer holds a clipping mask.")) + } else if no_current_fill && fields.text("fill") == Some("current") { + Some(tl!("There is no current fill.")) } else { None }; diff --git a/crates/ui-egui/src/addons/document/tests.rs b/crates/ui-egui/src/addons/document/tests.rs index 01e23b70f0..4f71e746a6 100644 --- a/crates/ui-egui/src/addons/document/tests.rs +++ b/crates/ui-egui/src/addons/document/tests.rs @@ -398,6 +398,28 @@ fn flood_fill_rejects_a_layer_with_a_clipping_mask_and_keeps_the_dialog_open() { assert_eq!(layer_names(&app)[0], "Paper"); } +#[test] +fn flood_fill_current_fill_without_a_fill_keeps_the_dialog_open() { + let mut app = boards(); + app.run("paint.setFill", json!({"none": true})).unwrap(); + let before = app.session.doc().unwrap().history.undo.len(); + click(&mut app, "Flood Fill Artboards…"); + let id = host::dialog_id(&app); + host::set(&app, id, "layer", json!("Paper")); + host::set(&app, id, "fill", json!("current")); + assert!(!host::confirm(&app, id)); + host::frame(&mut app); + assert!(app.any_dialog_open()); + assert_eq!(host::read(&app, id, |f| f.text("layer").map(str::to_owned)), Some("Paper".to_owned())); + assert_eq!(app.session.doc().unwrap().history.undo.len(), before); + host::set(&app, id, "fill", json!("color")); + assert!(host::confirm(&app, id)); + host::frame(&mut app); + assert!(!app.any_dialog_open()); + assert_eq!(layer_names(&app)[0], "Paper"); + assert_eq!(app.session.doc().unwrap().history.undo.len(), before + 1); +} + #[test] fn flood_fill_opens_on_color_without_a_fill_and_needs_a_layer_name() { let mut app = boards(); diff --git a/docs/mcp.md b/docs/mcp.md index 62dbfc6ba9..0776548e6a 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -17518,10 +17518,12 @@ colors the paint commands take (`#rrggbbaa` and `#rgba` are refused, as is a com (in a Flood Fill preview, the fill the preview began with; in a batch step, the live fill after preceding steps), and a current fill of None is an error. The rectangles go on top of the top-level layer named `layer` (`Flood Fill` by default, trimmed and nonempty, at most 255 characters after trimming), which is made at the bottom of the layers when the document has none. A destination layer that holds a clipping mask is rejected before any edit. A layer that already exists keeps its lock and visibility. The rectangles are selected unless that layer is locked or hidden, and -the current layer stays as it was. Null and values of the wrong type are errors; the command is refused while an -opacity mask is edited. The desktop app's Flood Fill Artboards… dialog picks the active artboard or all of them, the +the current layer stays as it was. Params must be an object or null (omitted options). Null option values and values +of the wrong type are errors. The command is refused while a symbol, pattern or opacity mask is edited. +The desktop app's Flood Fill Artboards… dialog picks the active artboard or all of them, the 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. → `{ids: [rectangle id…], layer, created}` +trimmed layer name, a destination layer that holds a clipping mask, or Current Fill when there is no current fill. +→ `{ids: [rectangle id…], layer, created}` ```json {"name":"run_command","arguments":{"command":"addon.document.floodFillArtboards","params":{}}}