Document Tools: a failed preview puts back the fill an earlier preview set

With nothing selected and New Art Has Basic Appearance off, a fill
preview followed by a failed Flood Fill preview left the previewed fill
in the new-art paints, and a commit kept it. The interaction now keeps
the paints from before the previews that build on each other and the
paints the last of them left. A failed preview puts back the first, with
the fields a command outside the previews changed kept as that command
left them.
This commit is contained in:
dotCooCoo committed 2026-11-30 10:17:11 -08:00
1 parent a36c8ce509
commit 91469a3d0e
3 files changed
+89 -2

No files matched your search

@@ -868,6 +868,33 @@ fn flood_fill_cancel_after_a_rejected_preview_keeps_the_new_art_fill() {
assert!(s.doc().unwrap().selection.is_empty());
}
/// A commit after a fill preview with nothing selected and a failed flood fill preview puts back
/// the fill the interaction began with.
#[test]
fn flood_fill_commit_after_a_fill_preview_and_a_rejected_preview_restores_the_new_art_fill() {
for stroke_first 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("Flood Fill Artboards").unwrap();
if stroke_first {
s.execute("paint.setStroke", &json!({"color": "#00ff00"})).unwrap();
}
s.preview("paint.setFill", &json!({"color": "#0000ff"})).unwrap();
assert_ne!(s.paint.fill, red);
if !stroke_first {
s.execute("paint.setStroke", &json!({"color": "#00ff00"})).unwrap();
}
let stroke = s.paint.stroke.clone();
assert!(s.preview(FLOOD, &json!({"color": [1e39, 0, 0]})).is_err());
assert_eq!(s.paint.fill, red, "stroke first {stroke_first}");
s.commit_interaction().unwrap();
assert_eq!(s.paint.fill, red, "stroke first {stroke_first}");
assert_eq!(s.paint.stroke, stroke, "a command run outside the previews keeps its change (stroke first {stroke_first})");
}
}
/// #4920: Export All Open Documents writes an SVG of each open document; a folder that can't be
/// written lists every document in `failed`, and either way the active document stays active.
#[test]
+4
View File
@@ -29,6 +29,10 @@ pub struct Interaction {
/// A successful preview started from `inherited_preview_paint`: the new-art paints changed
/// during the interaction, so a cancel, or a commit after a failed preview, puts `paint` back.
pub(crate) inherited_preview_paint_used: bool,
/// The new-art paints before the successful previews that built on each other, with the
/// changes made outside the previews, and the paints the last of them left. A failed preview
/// puts back the first where a field still holds what the previews left.
pub(crate) last_preview_paint: Option<(vectorcraft_tools::PaintDefaults, vectorcraft_tools::PaintDefaults)>,
/// An accepted batch preview uses these final paints after changing the document color mode.
pub(crate) preview_paint: Option<vectorcraft_tools::PaintDefaults>,
/// Conversion of new-art paints waits until a color-mode preview is accepted.
+58 -2
View File
@@ -554,6 +554,7 @@ impl Session {
preserve_paint_on_cancel: false,
inherited_preview_paint: None,
inherited_preview_paint_used: false,
last_preview_paint: None,
preview_paint: None,
color_mode_paints: None,
tool_paints: false,
@@ -583,7 +584,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, inherited) = {
let (paint, restore_paint, inherited, last_preview_paint) = {
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())) };
@@ -607,6 +608,7 @@ impl Session {
|| it.color_mode_paints.is_some()
|| (!new_art_basic && it.selection.is_empty() && it.preview.is_some() && cmd.starts_with("paint."));
let paint = it.paint.clone();
let last_preview_paint = it.last_preview_paint.take().filter(|_| it.preview.is_some());
// 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();
@@ -626,13 +628,25 @@ impl Session {
// undone with it.
st.restore_isolation(isolation, &undone);
cmd::globaledit::follow_history(st);
(paint, restore_paint, inherited)
(paint, restore_paint, inherited, last_preview_paint)
};
// The paints before the previews that build on each other: a preview that starts from the
// interaction's paints (or from the inherited ones) starts a new run of them. A field that
// no longer holds what the last preview left was changed outside the previews.
let base = if restore_paint || inherited.is_some() {
None
} else {
last_preview_paint.map(|(mut base, left)| {
take_changed_paints(&mut base, &left, &self.paint);
base
})
};
if restore_paint {
self.paint = paint.clone();
} else if let Some(before) = &inherited {
self.paint.clone_from(before);
}
let base = base.unwrap_or_else(|| self.paint.clone());
self.pending_paint = None;
// The previous preview's Assign Profile is undone with it.
self.sync_front_profiles();
@@ -651,12 +665,20 @@ impl Session {
}
let params = self.noted(params);
let batch_paint = (r.is_ok() && cmd == "command.batch").then(|| self.paint.clone());
// A failed preview leaves the document at the snapshot, so the new-art paints that earlier
// previews changed (a `paint.*` preview with nothing selected) go back to the paints from
// before them as well. A change made outside the previews stays.
if r.is_err() {
self.paint.clone_from(&base);
}
let after_paint = r.is_ok().then(|| (base, self.paint.clone()));
let st = self.doc_mut()?;
if let Some(it) = &mut st.interaction {
it.used_defaults_only |= ran_defaults_only;
// 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));
it.last_preview_paint = after_paint;
// 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);
@@ -839,6 +861,40 @@ impl Session {
}
}
/// Copy into `target` each field of `now` that differs from `left`. The pattern lists every field,
/// so a field added to `PaintDefaults` does not compile until it is handled here.
fn take_changed_paints(
target: &mut vectorcraft_tools::PaintDefaults,
left: &vectorcraft_tools::PaintDefaults,
now: &vectorcraft_tools::PaintDefaults,
) {
let vectorcraft_tools::PaintDefaults { fill, stroke, stroke_width, appearance, opacity, blend, style, inherited } = now;
if left.fill != *fill {
target.fill.clone_from(fill);
}
if left.stroke != *stroke {
target.stroke.clone_from(stroke);
}
if left.stroke_width != *stroke_width {
target.stroke_width = *stroke_width;
}
if left.appearance != *appearance {
target.appearance.clone_from(appearance);
}
if left.opacity != *opacity {
target.opacity = *opacity;
}
if left.blend != *blend {
target.blend = *blend;
}
if left.style != *style {
target.style.clone_from(style);
}
if left.inherited != *inherited {
target.inherited = *inherited;
}
}
/// The selection a drag began with, without the objects that a command run during it removed,
/// hid or locked in `doc`.
fn drag_selection(began: &Selection, doc: &Document) -> Selection {