From c2e61ac55e6eb4f6152a90c14930d7885d422e7a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 16:50:09 -0800 Subject: [PATCH 1/4] Swatches: a swatch replaced during a drag is not duplicated or deleted on drop New Swatch and Delete Swatch took the dragged rows by name. When a dragged swatch was deleted and a new one was made under its name before the drop, the drop duplicated or deleted the new swatch, which was never dragged. The panel now records each dragged swatch or color group when the drag starts and, until the drop, checks it against the states the document's undo steps keep and against the current state. A row that one of them does not hold unchanged is left out of the drop, also when the new row has the same name and paint. A row that stays unchanged is duplicated or deleted as before. --- crates/ui-egui/src/panels/swatches/actions.rs | 106 ++++++++++++++++-- crates/ui-egui/src/panels/swatches/body.rs | 4 +- crates/ui-egui/src/panels/swatches/tests.rs | 42 ++++++- 3 files changed, 138 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/panels/swatches/actions.rs b/crates/ui-egui/src/panels/swatches/actions.rs index dc26c78550..7ba6fda3dd 100644 --- a/crates/ui-egui/src/panels/swatches/actions.rs +++ b/crates/ui-egui/src/panels/swatches/actions.rs @@ -1,8 +1,13 @@ +use std::sync::{Arc, Weak}; + +use vectorcraft_color::SwatchGroup; +use vectorcraft_engine::DocState; + use super::dnd::{group_of, selection}; use super::libraries::SwatchLibraries; use super::{ - Kind, LibraryKind, PanelDrag, Response, Ui, Value, VectorcraftApp, View, active_paint, json, library_panel, menu_item, pstate, set_pstate, - widgets, + Document, Kind, LibraryKind, PanelDrag, Response, Swatch, Ui, Value, VectorcraftApp, View, active_paint, json, library_panel, menu_item, pstate, + set_pstate, widgets, }; /// The selected swatches and groups that can be deleted: every color group in the document and @@ -117,9 +122,81 @@ pub(super) fn bottom(app: &mut VectorcraftApp, ui: &mut Ui, sel: &[String]) { /// The panel state key of the document (`uid`) the swatches being dragged from the list belong to. pub(super) const DRAG_DOC: &str = "swatch-drag-doc"; +/// The panel state key of the rows being dragged from the list ([`HeldRows`]). +const DRAG_ROWS: &str = "swatch-drag-rows"; + +/// A row dragged from the list as it was when the drag started: its swatch or its color group. +#[derive(Clone, PartialEq)] +enum HeldRow { + Swatch(Swatch), + Group(SwatchGroup), +} + +impl HeldRow { + /// Row `name` of `d`: its swatch, else the color group of that name. + fn of(d: &Document, name: &str) -> Option { + match d.swatch(name) { + Some(w) => Some(HeldRow::Swatch(w.clone())), + None => d.swatch_groups.iter().find(|g| g.name == name).cloned().map(HeldRow::Group), + } + } + /// Whether `d` holds this row unchanged. + fn unchanged_in(&self, d: &Document) -> bool { + match self { + HeldRow::Swatch(w) => d.swatch(&w.name) == Some(w), + HeldRow::Group(g) => d.swatch_groups.iter().any(|x| x == g), + } + } +} + +/// The rows being dragged from the list, in the order of the drag's names, and the document they +/// were last checked against. A row is `None` once a state of the document since the drag started +/// did not hold it unchanged: deleted, edited, or deleted and made again under its name, also with +/// the same paint. +#[derive(Clone, Default)] +struct HeldRows { + doc: Weak, + rows: Vec<(String, Option)>, +} + +/// Records the document and the rows a drag from the list started with this frame, so that New +/// Swatch and Delete Swatch act only on rows that stay unchanged until it is dropped. +pub(super) fn drag_started(app: &VectorcraftApp, ctx: &egui::Context) { + let st = app.session.active(); + set_pstate(ctx, DRAG_DOC, st.map(|st| st.uid)); + let held = match (st, egui::DragAndDrop::payload::(ctx).as_deref()) { + (Some(st), Some(PanelDrag::Paint { rows: Some(r), .. })) => { + HeldRows { doc: Arc::downgrade(&st.doc), rows: r.names.iter().map(|n| (n.clone(), HeldRow::of(&st.doc, n))).collect() } + } + _ => HeldRows::default(), + }; + set_pstate(ctx, DRAG_ROWS, held); +} + +/// The dragged rows ([`HeldRows`]) checked against every state document `st` passed through since +/// they were last checked: the states its undo steps kept since then, and the current one. When +/// the last checked state is not among them (an undo, or steps dropped past the history's limits), +/// only the current state is checked. +fn held_rows(st: &DocState, ctx: &egui::Context) -> HeldRows { + let mut held: HeldRows = pstate(ctx, DRAG_ROWS); + if std::ptr::eq(held.doc.as_ptr(), Arc::as_ptr(&st.doc)) { + return held; + } + let since = st.history.undo.iter().rposition(|e| std::ptr::eq(held.doc.as_ptr(), Arc::as_ptr(&e.doc))); + let between = since.and_then(|i| st.history.undo.get(i + 1..)).unwrap_or_default(); + for (_, row) in &mut held.rows { + if row.as_ref().is_some_and(|r| !(between.iter().all(|e| r.unchanged_in(&e.doc)) && r.unchanged_in(&st.doc))) { + *row = None; + } + } + held.doc = Arc::downgrade(&st.doc); + set_pstate(ctx, DRAG_ROWS, held.clone()); + held +} + /// The swatches and color groups of the active document being dragged from the list that Delete -/// Swatch can delete: neither None nor Registration, and still in the document. Empty for any other -/// drag, and for a drag started in another document. +/// Swatch can delete: neither None nor Registration, and still in the document unchanged. Empty for +/// any other drag, and for a drag started in another document. fn dragged_deletable(app: &VectorcraftApp, ctx: &egui::Context) -> Vec { let Some(st) = app.session.active() else { return vec![] }; let d = &st.doc; @@ -127,16 +204,25 @@ fn dragged_deletable(app: &VectorcraftApp, ctx: &egui::Context) -> Vec { } /// The names of the swatches or color groups of the active document being dragged from the list, -/// in panel order. Empty for any other drag, and for a drag started in another document. +/// in panel order, that the document has held unchanged since the drag started ([`held_rows`]). +/// Empty for any other drag, and for a drag started in another document. fn dragged_rows(app: &VectorcraftApp, ctx: &egui::Context) -> Vec { let Some(st) = app.session.active() else { return vec![] }; if pstate::>(ctx, DRAG_DOC) != Some(st.uid) { return vec![]; } - match egui::DragAndDrop::payload::(ctx).as_deref() { - Some(PanelDrag::Paint { rows: Some(r), .. }) => r.names.clone(), - _ => vec![], + let drag = egui::DragAndDrop::payload::(ctx); + let Some(PanelDrag::Paint { rows: Some(r), .. }) = drag.as_deref() else { return vec![] }; + let held = held_rows(st, ctx); + if held.rows.len() != r.names.len() { + return vec![]; } + r.names + .iter() + .zip(&held.rows) + .filter(|(n, (h, row))| *n == h && row.as_ref().is_some_and(|row| row.unchanged_in(&st.doc))) + .map(|(n, _)| n.clone()) + .collect() } /// Swatches or color groups (`names`) dragged onto Delete Swatch (`button`): the button is @@ -157,8 +243,8 @@ fn drop_on_delete(app: &mut VectorcraftApp, ui: &Ui, button: &Response, names: V } /// The swatches of the active document being dragged from the list that New Swatch can duplicate, -/// in panel order: the dragged rows ([`dragged_rows`]) that are swatches still in the document, -/// other than None and Registration. +/// in panel order: the dragged rows ([`dragged_rows`]) that are swatches still in the document +/// unchanged, other than None and Registration. fn dragged_swatches(app: &VectorcraftApp, ctx: &egui::Context) -> Vec { let Some(st) = app.session.active() else { return vec![] }; dragged_rows(app, ctx).into_iter().filter(|n| st.doc.swatch(n).is_some_and(|w| !w.is_reserved())).collect() diff --git a/crates/ui-egui/src/panels/swatches/body.rs b/crates/ui-egui/src/panels/swatches/body.rs index 944a245b9e..4ec269d5dd 100644 --- a/crates/ui-egui/src/panels/swatches/body.rs +++ b/crates/ui-egui/src/panels/swatches/body.rs @@ -1,4 +1,4 @@ -use super::actions::{DRAG_DOC, bottom, edit_group}; +use super::actions::{bottom, drag_started, edit_group}; use super::dnd::{TileEvents, apply_drop, click_selection, highlighted, selection, tile_input, zone_input}; use super::tiles::{ draw_folder, draw_registration, folder_tile, global_mark, list_icons, list_row, list_row_name, pattern_thumb, tile_grid, tile_id, @@ -133,7 +133,7 @@ pub(super) fn body(app: &mut VectorcraftApp, ui: &mut Ui, salt: &'static str) { None => {} } if ev.started { - set_pstate(ui.ctx(), DRAG_DOC, app.session.active().map(|st| st.uid)); + drag_started(app, ui.ctx()); } if let Some(drop) = ev.drop { apply_drop(app, drop); diff --git a/crates/ui-egui/src/panels/swatches/tests.rs b/crates/ui-egui/src/panels/swatches/tests.rs index a3e7120c7a..58e767817c 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -788,7 +788,8 @@ fn swatches_dropped_on_delete_swatch_are_deleted_after_asking() { /// Delete Swatch takes only swatches dragged from the active document's list: a paint dragged from /// elsewhere, art, and a swatch dragged from another document whose name the active document also /// has delete nothing, ask nothing and are not outlined. A secondary button released while a swatch -/// is held over the button ends the drag without asking. +/// is held over the button ends the drag without asking, and a swatch deleted while it is dragged +/// leaves alone a new swatch made under its name. #[test] fn delete_swatch_ignores_other_drags() { let mut app = app(); @@ -848,6 +849,23 @@ fn delete_swatch_ignores_other_drags() { assert!(app.session.doc().unwrap().doc.swatch("Orange").is_some() && undo_steps(&app) == undo_new, "the other document keeps Orange"); assert!(app.session.set_active(0)); unchanged(&app, "the first document"); + // A swatch deleted while it is dragged, and a new swatch made under its name before the drop. + frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 5.0); + let from = tile_center(&ctx, "Orange"); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(from), press(from, PointerButton::Primary, true)], Modifiers::NONE, 5.1); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(from + vec2(6.0, 4.0))], Modifiers::NONE, 5.2); + assert!(egui::DragAndDrop::payload::(&ctx).is_some(), "Orange is dragged"); + let orange = app.session.doc().unwrap().doc.swatch("Orange").cloned(); + app.run("swatch.delete", json!({"names": ["Orange"]})).unwrap(); + app.run("swatch.new", json!({"name": "Orange", "color": "#f7931e"})).unwrap(); + let (replaced, undo_replaced) = (app.session.doc().unwrap().doc.clone(), undo_steps(&app)); + assert_eq!(replaced.swatch("Orange").cloned(), orange, "the new swatch is the same as the one deleted"); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 5.3); + let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 5.4); + assert!(!outlined(&held, trash), "the new swatch of that name is not outlined"); + frame_out(&mut app, &ctx, vec![release(trash.center())], Modifiers::NONE, 5.5); + assert_eq!(dialog_message(&app), None, "the new swatch of that name asks nothing"); + assert_eq!((app.session.doc().unwrap().doc.clone(), undo_steps(&app)), (replaced, undo_replaced), "the new swatch of that name is kept"); } /// Swatches dragged from the list onto New Swatch are duplicated without a dialog, in thumbnail @@ -920,7 +938,8 @@ fn swatches_dropped_on_new_swatch_are_duplicated() { /// New Swatch duplicates only swatches dragged from the active document's list that are still in /// it: a paint dragged from elsewhere, art, a swatch dragged from another document, and a swatch -/// deleted while it is dragged duplicate nothing. A secondary button released while a swatch is +/// deleted while it is dragged duplicate nothing, nor does a new swatch made under the name of a +/// swatch deleted while it is dragged. A secondary button released while a swatch is /// held over the button ends the drag without duplicating, and a click still opens New Swatch. #[test] fn new_swatch_ignores_other_drags() { @@ -977,6 +996,25 @@ fn new_swatch_ignores_other_drags() { frame_out(&mut app, &ctx, vec![release(new.center())], Modifiers::NONE, 4.5); assert_eq!((app.session.doc().unwrap().doc.clone(), undo_steps(&app)), (gone, undo_gone), "a deleted swatch is not duplicated"); app.run("edit.undo", json!({})).unwrap(); + // A swatch deleted while it is dragged, and a new swatch made under its name before the drop. + hold_orange(&mut app, 4.6); + let orange = app.session.doc().unwrap().doc.swatch("Orange").cloned(); + app.run("swatch.delete", json!({"names": ["Orange"]})).unwrap(); + app.run("swatch.new", json!({"name": "Orange", "color": "#f7931e"})).unwrap(); + let (replaced, undo_replaced) = (app.session.doc().unwrap().doc.clone(), undo_steps(&app)); + assert_eq!(replaced.swatch("Orange").cloned(), orange, "the new swatch is the same as the one deleted"); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(new.center())], Modifiers::NONE, 4.85); + let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(new.center())], Modifiers::NONE, 4.9); + assert!(!outlined(&held, new), "the new swatch of that name is not outlined"); + frame_out(&mut app, &ctx, vec![release(new.center())], Modifiers::NONE, 4.95); + assert!(app.ui.dialog.is_none(), "the new swatch of that name opens no dialog"); + assert_eq!( + (app.session.doc().unwrap().doc.clone(), undo_steps(&app)), + (replaced, undo_replaced), + "the new swatch of that name is not duplicated" + ); + app.run("edit.undo", json!({})).unwrap(); + app.run("edit.undo", json!({})).unwrap(); // A swatch dragged from this document while another document with the same swatches becomes // active. hold_orange(&mut app, 5.0); From 98535b70d478342c341e616e1c8988db22ce26db Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 02:07:39 -0800 Subject: [PATCH 2/4] Swatches: a swatch undone during a drag is not duplicated or deleted on drop When the drag's last checked state was gone from the undo history, the panel checked the dragged rows against the current document only. An undo of the step that made a dragged swatch, followed by a new swatch of the same name and paint before the panel was drawn again (for example two control requests handled in one frame), left the new swatch eligible for New Swatch and Delete Swatch. A dragged swatch deleted and brought back by an undo before the next check was eligible too, although the panel leaves it out when it sees the deletion. The panel now checks the dragged rows after every command the app runs, not only when it is drawn. For steps inside one command it also records the newest undo step, the one under it and the newest redo steps with the rows: - After an undo of the newest step, a row is kept only when the state the undo gave back, the states of the steps made since and the current state all hold it unchanged. - A row is also left out when a state that an undo left (kept by the redo steps made since) does not hold it unchanged. - When more than one step was undone, or steps were dropped past the history's limits, every row is left out of the drop. An undo of a step that left a dragged swatch alone still lets it be duplicated or deleted. HistoryEntry::serial exposes the step number that History::step already reports for the newest steps. --- crates/engine/src/history.rs | 6 + crates/ui-egui/src/app/commands.rs | 4 +- crates/ui-egui/src/panels/swatches.rs | 1 + crates/ui-egui/src/panels/swatches/actions.rs | 98 +++++++++++++--- crates/ui-egui/src/panels/swatches/tests.rs | 111 +++++++++++++++++- 5 files changed, 196 insertions(+), 24 deletions(-) diff --git a/crates/engine/src/history.rs b/crates/engine/src/history.rs index d472ec760c..51c38e8e09 100644 --- a/crates/engine/src/history.rs +++ b/crates/engine/src/history.rs @@ -109,6 +109,12 @@ impl HistoryEntry { self.named.as_ref().filter(|n| n.label() == self.label) } + /// A number no other step takes ([`History::step`]). Undo and Redo make the step they move + /// with a new number, and so does an edit merged into the newest step. + pub fn serial(&self) -> u64 { + self.serial + } + /// The step that undoing (redoing) the step `costs` came from makes: it keeps `doc`, the /// document before the undo, whose next state is now `now` (that step's document). Its figures /// are that step's the other way round when `doc` is still the document they were taken diff --git a/crates/ui-egui/src/app/commands.rs b/crates/ui-egui/src/app/commands.rs index 042e8b37a5..b52addfe41 100644 --- a/crates/ui-egui/src/app/commands.rs +++ b/crates/ui-egui/src/app/commands.rs @@ -275,9 +275,11 @@ impl VectorcraftApp { } // A renamed spot color swatch stays the Color Guide's base; a deleted one ends the link. panels::color_guide::after_command(self, id, ¶ms, v, spot_base); - // A renamed color group stays selected in the Swatches panel. + // A renamed color group stays selected in the Swatches panel, and the rows dragged + // from its list are checked against what the command left. if let Some(ctx) = self.ui_ctx.clone() { panels::swatches::after_command(&ctx, id, ¶ms, v); + panels::swatches::follow_drag(self, &ctx); } if id == "file.new" { self.status(""); diff --git a/crates/ui-egui/src/panels/swatches.rs b/crates/ui-egui/src/panels/swatches.rs index 41b42bfd60..0f6c0b1fe5 100644 --- a/crates/ui-egui/src/panels/swatches.rs +++ b/crates/ui-egui/src/panels/swatches.rs @@ -30,6 +30,7 @@ mod dnd; mod libraries; mod tiles; +pub(crate) use actions::follow_drag; pub use actions::menu; #[cfg(test)] use actions::{deletable, delete, mergeable, new_swatch, selected_group}; diff --git a/crates/ui-egui/src/panels/swatches/actions.rs b/crates/ui-egui/src/panels/swatches/actions.rs index 7ba6fda3dd..5572e4f778 100644 --- a/crates/ui-egui/src/panels/swatches/actions.rs +++ b/crates/ui-egui/src/panels/swatches/actions.rs @@ -1,7 +1,7 @@ use std::sync::{Arc, Weak}; use vectorcraft_color::SwatchGroup; -use vectorcraft_engine::DocState; +use vectorcraft_engine::{DocState, History, HistoryEntry}; use super::dnd::{group_of, selection}; use super::libraries::SwatchLibraries; @@ -149,14 +149,37 @@ impl HeldRow { } } -/// The rows being dragged from the list, in the order of the drag's names, and the document they -/// were last checked against. A row is `None` once a state of the document since the drag started -/// did not hold it unchanged: deleted, edited, or deleted and made again under its name, also with -/// the same paint. +/// The rows being dragged from the list, in the order of the drag's names, and the document and +/// undo history they were last checked against. A row is `None` once a state of the document since +/// the drag started did not hold it unchanged: deleted, edited, or deleted and made again under its +/// name, also with the same paint. #[derive(Clone, Default)] struct HeldRows { doc: Weak, + /// The newest undo step of the document and the one under it, and the next redo step and the + /// one under it ([`steps`]). + steps: [u64; 4], rows: Vec<(String, Option)>, + /// For each row, whether the document undoing the newest step gives back (that step's + /// document) holds it unchanged. + back: Vec, +} + +/// The newest undo step of `st` and the one under it, and the next redo step and the one under it +/// ([`HistoryEntry::serial`]), 0 for none. +fn steps(st: &DocState) -> [u64; 4] { + let serial = |list: &[HistoryEntry], back: usize| list.len().checked_sub(back).and_then(|i| list.get(i)).map_or(0, HistoryEntry::serial); + let h = &st.history; + [serial(&h.undo, 1), serial(&h.undo, 2), serial(&h.redo, 1), serial(&h.redo, 2)] +} + +impl HeldRows { + /// `rows`, checked against document `st` as it is now. + fn at(st: &DocState, rows: Vec<(String, Option)>) -> Self { + let last = st.history.undo.last(); + let back = rows.iter().map(|(_, r)| r.as_ref().zip(last).is_some_and(|(r, e)| r.unchanged_in(&e.doc))).collect(); + HeldRows { doc: Arc::downgrade(&st.doc), steps: steps(st), rows, back } + } } /// Records the document and the rows a drag from the list started with this frame, so that New @@ -166,7 +189,7 @@ pub(super) fn drag_started(app: &VectorcraftApp, ctx: &egui::Context) { set_pstate(ctx, DRAG_DOC, st.map(|st| st.uid)); let held = match (st, egui::DragAndDrop::payload::(ctx).as_deref()) { (Some(st), Some(PanelDrag::Paint { rows: Some(r), .. })) => { - HeldRows { doc: Arc::downgrade(&st.doc), rows: r.names.iter().map(|n| (n.clone(), HeldRow::of(&st.doc, n))).collect() } + HeldRows::at(st, r.names.iter().map(|n| (n.clone(), HeldRow::of(&st.doc, n))).collect()) } _ => HeldRows::default(), }; @@ -174,26 +197,65 @@ pub(super) fn drag_started(app: &VectorcraftApp, ctx: &egui::Context) { } /// The dragged rows ([`HeldRows`]) checked against every state document `st` passed through since -/// they were last checked: the states its undo steps kept since then, and the current one. When -/// the last checked state is not among them (an undo, or steps dropped past the history's limits), -/// only the current state is checked. +/// they were last checked, and the current one. Those states are the ones the redo steps made +/// since then keep (the states an undo left), and: +/// - While the newest undo step of then is still in the history, the states the undo steps made +/// since then keep. +/// - After an undo of that step, or an edit merged into it, the state before that step and the +/// states the undo steps made since then keep. +/// +/// Otherwise (more than one step undone, or steps dropped past the history's limits) the states in +/// between are not known, and every row is left out of the drop. The panel checks the rows after +/// every command the app runs ([`follow_drag`]), so only the steps of one command go unseen. One +/// undo step is one change: the states between the commands of a `command.batch` are not kept, and +/// a row the batch leaves as it found it stays in the drop. fn held_rows(st: &DocState, ctx: &egui::Context) -> HeldRows { - let mut held: HeldRows = pstate(ctx, DRAG_ROWS); - if std::ptr::eq(held.doc.as_ptr(), Arc::as_ptr(&st.doc)) { + let held: HeldRows = pstate(ctx, DRAG_ROWS); + let now = steps(st); + let same = std::ptr::eq(held.doc.as_ptr(), Arc::as_ptr(&st.doc)); + if same && held.steps == now { return held; } - let since = st.history.undo.iter().rposition(|e| std::ptr::eq(held.doc.as_ptr(), Arc::as_ptr(&e.doc))); - let between = since.and_then(|i| st.history.undo.get(i + 1..)).unwrap_or_default(); - for (_, row) in &mut held.rows { - if row.as_ref().is_some_and(|r| !(between.iter().all(|e| r.unchanged_in(&e.doc)) && r.unchanged_in(&st.doc))) { - *row = None; + let (undo, redo) = (&st.history.undo, &st.history.redo); + // Where the steps made after the step with `serial` start in `list`: 0 for none. + let after = |list: &[HistoryEntry], serial: u64| { + if serial == 0 { Some(0) } else { list.iter().rposition(|e| e.serial() == serial).map(|i| i + 1) } + }; + let [newest, below, next, further] = held.steps; + // The redo steps made since: above the next redo step of then, or above the one under it once a + // redo took that step. + let left = redo.get(after(redo, next).or_else(|| after(redo, further)).unwrap_or(0)..).unwrap_or_default(); + // The states in between, and whether they start with the state an undo of the newest step gave back. + let (between, undone) = match after(undo, newest) { + // With no step before it, the first step keeps the last checked state, unless steps were + // dropped past the history's limits, which leave at least that many steps. + Some(0) => { + let kept = same + || undo.len() < st.history.limit.min(History::MIN_STEPS) + || undo.first().is_none_or(|e| std::ptr::eq(held.doc.as_ptr(), Arc::as_ptr(&e.doc))); + (kept.then_some(undo.as_slice()), false) } - } - held.doc = Arc::downgrade(&st.doc); + Some(i) => (undo.get(i..), false), + None => (after(undo, below).and_then(|i| undo.get(i..)), true), + }; + let unchanged = |r: &HeldRow, b: &[HistoryEntry]| b.iter().chain(left).all(|e| r.unchanged_in(&e.doc)) && r.unchanged_in(&st.doc); + let rows = held + .rows + .into_iter() + .zip(held.back) + .map(|((name, row), back)| (name, row.filter(|r| (back || !undone) && between.is_some_and(|b| unchanged(r, b))))) + .collect(); + let held = HeldRows::at(st, rows); set_pstate(ctx, DRAG_ROWS, held.clone()); held } +/// Checks the rows being dragged from the list against the active document after a command ran +/// ([`held_rows`]), so that the commands after it cannot replace one unseen. +pub(crate) fn follow_drag(app: &VectorcraftApp, ctx: &egui::Context) { + dragged_rows(app, ctx); +} + /// The swatches and color groups of the active document being dragged from the list that Delete /// Swatch can delete: neither None nor Registration, and still in the document unchanged. Empty for /// any other drag, and for a drag started in another document. diff --git a/crates/ui-egui/src/panels/swatches/tests.rs b/crates/ui-egui/src/panels/swatches/tests.rs index 58e767817c..81b4096bd5 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -788,8 +788,9 @@ fn swatches_dropped_on_delete_swatch_are_deleted_after_asking() { /// Delete Swatch takes only swatches dragged from the active document's list: a paint dragged from /// elsewhere, art, and a swatch dragged from another document whose name the active document also /// has delete nothing, ask nothing and are not outlined. A secondary button released while a swatch -/// is held over the button ends the drag without asking, and a swatch deleted while it is dragged -/// leaves alone a new swatch made under its name. +/// is held over the button ends the drag without asking, and a swatch deleted or undone while it is +/// dragged leaves alone a new swatch made under its name, and the swatch an undo brings back. A +/// swatch dragged while an undo takes back a step that left it alone is still deleted. #[test] fn delete_swatch_ignores_other_drags() { let mut app = app(); @@ -866,6 +867,50 @@ fn delete_swatch_ignores_other_drags() { frame_out(&mut app, &ctx, vec![release(trash.center())], Modifiers::NONE, 5.5); assert_eq!(dialog_message(&app), None, "the new swatch of that name asks nothing"); assert_eq!((app.session.doc().unwrap().doc.clone(), undo_steps(&app)), (replaced, undo_replaced), "the new swatch of that name is kept"); + // A swatch the newest undo step made, undone while it is dragged, and a new swatch made under + // its name before the drop, both inside one command (run on the session, so the panel does + // not check the rows between them). + let hold_orange = |app: &mut VectorcraftApp, time: f64| { + frame_out(app, &ctx, vec![], Modifiers::NONE, time); + let from = tile_center(&ctx, "Orange"); + frame_out(app, &ctx, vec![Event::PointerMoved(from), press(from, PointerButton::Primary, true)], Modifiers::NONE, time + 0.1); + frame_out(app, &ctx, vec![Event::PointerMoved(from + vec2(6.0, 4.0))], Modifiers::NONE, time + 0.2); + let drag = egui::DragAndDrop::payload::(&ctx); + assert!(matches!(drag.as_deref(), Some(PanelDrag::Paint { rows: Some(r), .. }) if r.names == ["Orange"]), "Orange is dragged"); + }; + hold_orange(&mut app, 6.0); + app.session.execute("edit.undo", &json!({})).unwrap(); + app.session.execute("swatch.new", &json!({"name": "Orange", "color": "#f7931e"})).unwrap(); + let (replaced, undo_replaced) = (app.session.doc().unwrap().doc.clone(), undo_steps(&app)); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 6.3); + let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 6.4); + assert!(!outlined(&held, trash), "the new swatch made after the undo is not outlined"); + frame_out(&mut app, &ctx, vec![release(trash.center())], Modifiers::NONE, 6.5); + assert_eq!(dialog_message(&app), None, "the new swatch made after the undo asks nothing"); + assert_eq!( + (app.session.doc().unwrap().doc.clone(), undo_steps(&app)), + (replaced.clone(), undo_replaced), + "the new swatch made after the undo is kept" + ); + // A swatch deleted while it is dragged and brought back by an undo, inside one command. + hold_orange(&mut app, 6.6); + app.session.execute("swatch.delete", &json!({"names": ["Orange"]})).unwrap(); + app.session.execute("edit.undo", &json!({})).unwrap(); + assert_eq!(app.session.doc().unwrap().doc.clone(), replaced, "the undo brings Orange back"); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 6.85); + let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 6.9); + assert!(!outlined(&held, trash), "a swatch brought back by an undo is not outlined"); + frame_out(&mut app, &ctx, vec![release(trash.center())], Modifiers::NONE, 6.95); + assert_eq!(dialog_message(&app), None, "a swatch brought back by an undo asks nothing"); + // A swatch dragged while an undo takes back a step that left it alone is still deleted. + app.run("shape.rectangle", json!({"x": 60, "y": 0, "width": 20, "height": 20})).unwrap(); + hold_orange(&mut app, 7.0); + app.session.execute("edit.undo", &json!({})).unwrap(); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 7.3); + let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(trash.center())], Modifiers::NONE, 7.4); + assert!(outlined(&held, trash), "a swatch an undo left alone is outlined"); + frame_out(&mut app, &ctx, vec![release(trash.center())], Modifiers::NONE, 7.5); + assert!(dialog_message(&app).is_some(), "a swatch an undo left alone is deleted after asking"); } /// Swatches dragged from the list onto New Swatch are duplicated without a dialog, in thumbnail @@ -939,7 +984,8 @@ fn swatches_dropped_on_new_swatch_are_duplicated() { /// New Swatch duplicates only swatches dragged from the active document's list that are still in /// it: a paint dragged from elsewhere, art, a swatch dragged from another document, and a swatch /// deleted while it is dragged duplicate nothing, nor does a new swatch made under the name of a -/// swatch deleted while it is dragged. A secondary button released while a swatch is +/// swatch deleted or undone while it is dragged, or the swatch an undo brings back after deleting it +/// while it is dragged. A secondary button released while a swatch is /// held over the button ends the drag without duplicating, and a click still opens New Swatch. #[test] fn new_swatch_ignores_other_drags() { @@ -1028,14 +1074,69 @@ fn new_swatch_ignores_other_drags() { assert_eq!((app.session.doc().unwrap().doc.clone(), undo_steps(&app)), (other, undo_new), "the other document duplicates nothing"); assert!(app.session.set_active(0)); unchanged(&app, "the first document"); + // A swatch the newest undo step made, undone while it is dragged, and a new swatch made under + // its name before the drop, both inside one command (run on the session, so the panel does + // not check the rows between them). + let glass = json!({"name": "Sea Glass", "color": "#00a99d"}); + let steps = json!([{"command": "swatch.new", "params": glass}, {"command": "swatch.move", "params": {"name": "Sea Glass", "to": 2}}]); + app.run("command.batch", json!({"label": "New Swatch", "commands": steps})).unwrap(); + let made = app.session.doc().unwrap().doc.swatch("Sea Glass").cloned(); + assert!(made.is_some(), "Sea Glass is made"); + frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 5.6); + frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 5.65); + let from = tile_center(&ctx, "Sea Glass"); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(from), press(from, PointerButton::Primary, true)], Modifiers::NONE, 5.7); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(from + vec2(6.0, 4.0))], Modifiers::NONE, 5.8); + let drag = egui::DragAndDrop::payload::(&ctx); + assert!(matches!(drag.as_deref(), Some(PanelDrag::Paint { rows: Some(r), .. }) if r.names == ["Sea Glass"]), "Sea Glass is dragged"); + app.session.execute("edit.undo", &json!({})).unwrap(); + assert!(app.session.doc().unwrap().doc.swatch("Sea Glass").is_none(), "the undo takes Sea Glass away"); + app.session.execute("swatch.new", &glass).unwrap(); + let (replaced, undo_replaced) = (app.session.doc().unwrap().doc.clone(), undo_steps(&app)); + assert_eq!(replaced.swatch("Sea Glass").cloned(), made, "the new swatch is the same as the one undone"); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(new.center())], Modifiers::NONE, 5.85); + let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(new.center())], Modifiers::NONE, 5.9); + assert!(!outlined(&held, new), "the new swatch made after the undo is not outlined"); + frame_out(&mut app, &ctx, vec![release(new.center())], Modifiers::NONE, 5.95); + assert!(app.ui.dialog.is_none(), "the new swatch made after the undo opens no dialog"); + assert_eq!( + (app.session.doc().unwrap().doc.clone(), undo_steps(&app)), + (replaced, undo_replaced), + "the new swatch made after the undo is not duplicated" + ); + app.run("edit.undo", json!({})).unwrap(); + unchanged(&app, "after the undo case"); + // A swatch deleted while it is dragged and brought back by an undo, then an edit that clears + // the redo steps, each a command of its own before the panel is drawn again. The app checks the + // rows after each command it runs with the window's context. + app.ui_ctx = Some(ctx.clone()); + hold_orange(&mut app, 6.0); + app.run("swatch.delete", json!({"names": ["Orange"]})).unwrap(); + app.run("edit.undo", json!({})).unwrap(); + app.run("shape.rectangle", json!({"x": 60, "y": 0, "width": 20, "height": 20})).unwrap(); + let (restored, undo_restored) = (app.session.doc().unwrap().doc.clone(), undo_steps(&app)); + assert!(restored.swatch("Orange").is_some(), "the undo brings Orange back"); + frame_out(&mut app, &ctx, vec![Event::PointerMoved(new.center())], Modifiers::NONE, 6.3); + let held = frame_out(&mut app, &ctx, vec![Event::PointerMoved(new.center())], Modifiers::NONE, 6.4); + assert!(!outlined(&held, new), "a swatch brought back by an undo is not outlined"); + frame_out(&mut app, &ctx, vec![release(new.center())], Modifiers::NONE, 6.5); + assert!(app.ui.dialog.is_none(), "a swatch brought back by an undo opens no dialog"); + assert_eq!( + (app.session.doc().unwrap().doc.clone(), undo_steps(&app)), + (restored, undo_restored), + "a swatch brought back by an undo is not duplicated" + ); + app.run("edit.undo", json!({})).unwrap(); + app.ui_ctx = None; + unchanged(&app, "after the restored case"); // A click still opens New Swatch. - frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 6.0); + frame_out(&mut app, &ctx, vec![], Modifiers::NONE, 7.0); let click = vec![ Event::PointerMoved(new.center()), press(new.center(), PointerButton::Primary, true), press(new.center(), PointerButton::Primary, false), ]; - frame_out(&mut app, &ctx, click, Modifiers::NONE, 6.1); + frame_out(&mut app, &ctx, click, Modifiers::NONE, 7.1); assert!(app.ui.dialog.is_some(), "a click opens the New Swatch dialog"); } From 49c049117082d7c1b1d3b6e106793738fb4939c4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 20:28:18 -0800 Subject: [PATCH 3/4] Swatches: test the drag check after each command with a running drag The new test starts a row drag through the drag session and runs Delete, Undo and an unrelated edit with no pass of the panel in between. The check that runs after every command then keeps New Swatch from duplicating the restored swatch and Delete Swatch from deleting it. --- crates/ui-egui/src/panels/swatches/tests.rs | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/crates/ui-egui/src/panels/swatches/tests.rs b/crates/ui-egui/src/panels/swatches/tests.rs index 9d5606a2b5..123f5be08b 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -665,6 +665,52 @@ fn new_swatch_skips_rows_replaced_during_the_drag() { assert_eq!(undo_steps(&app), undo + 1); } +/// The rows of a drag running in the drag session are checked after every command the app runs: +/// a swatch deleted and brought back by an undo, then an edit that clears the redo steps, all with +/// no pass of the panel in between, is neither duplicated by New Swatch nor deleted by Delete +/// Swatch. +#[test] +fn rows_dragged_in_the_session_are_checked_after_every_command() { + use craft::behavior::keyed; + use craft::ui::contract::ShowCollection as _; + use craft::ui::list::{List, ListRow}; + use craft::ui::testing::Fixture; + + let mut app = app(); + let payload = rows_drag(&app, "Orange", &["Orange"]); + // A list row that starts the drag through the drag session, as a row of the panel does. + let mut fixture = Fixture::new(move |ui| { + let payload = payload.clone(); + let _ = List::::new("Drag source").drag_payload(move |_: &ListDrag| Some(payload.clone())).show_items( + ui, + &keyed(&[0u32], |key: &u32| *key), + &mut |_, _| ListRow::new("Dragged"), + ); + }); + fixture.room(vec2(240.0, 120.0)); + fixture.settle(); + app.ui_ctx = Some(fixture.ctx().clone()); + let from = fixture.by_label("Dragged").expect("the drag source").center(); + let key = crate::dialogs::confirm::KIND; + fixture.drag_at_pausing(from, vec2(0.0, 60.0), |f| { + let drag = craft::ui::runtime::drag::active::(f.ctx()).expect("the rows drag"); + for (id, params) in [ + ("swatch.delete", json!({"names": ["Orange"]})), + ("edit.undo", json!({})), + ("shape.rectangle", json!({"x": 60, "y": 0, "width": 20, "height": 20})), + ] { + app.run(id, params).unwrap(); + } + let restored = doc_and_steps(&app); + assert!(restored.0.swatch("Orange").is_some(), "the undo brings Orange back"); + dnd::on_footer_drop(&mut app, dnd::FooterTarget::New, &drag); + assert_eq!(doc_and_steps(&app), restored, "a swatch brought back by an undo is not duplicated"); + dnd::on_footer_drop(&mut app, dnd::FooterTarget::Delete, &drag); + assert!(!dialog_open(&app, key), "a swatch brought back by an undo asks nothing"); + assert_eq!(doc_and_steps(&app), restored, "a swatch brought back by an undo is kept"); + }); +} + /// A selected swatch dragged onto New Swatch or Delete Swatch carries the selected swatches that /// the find field hides, which the browser leaves out of the drag, as a click on Delete Swatch /// takes them too. The copies are selected in panel order. A grabbed swatch outside the selection From 8d48a0d2754d0ea249548b52e4685467bd59a789 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 20:54:24 -0800 Subject: [PATCH 4/4] =?UTF-8?q?Swatches:=20a=20row=20drag=20started=20befo?= =?UTF-8?q?re=20File=20=E2=80=BA=20Revert=20drops=20nothing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File › Revert keeps the document's uid and clears its history, so a drag started before it could still duplicate or delete a swatch of the reverted document that has the same name and paint. The drag now also records the document's revert count, and New Swatch and Delete Swatch take no rows when it has changed. --- crates/ui-egui/src/panels/swatches/dnd.rs | 35 +++++++++++++-------- crates/ui-egui/src/panels/swatches/tests.rs | 19 +++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/panels/swatches/dnd.rs b/crates/ui-egui/src/panels/swatches/dnd.rs index c22726a967..beff034708 100644 --- a/crates/ui-egui/src/panels/swatches/dnd.rs +++ b/crates/ui-egui/src/panels/swatches/dnd.rs @@ -96,11 +96,13 @@ fn steps(st: &DocState) -> [u64; 4] { [serial(&h.undo, 1), serial(&h.undo, 2), serial(&h.redo, 1), serial(&h.redo, 2)] } -/// A document as a drag from the list finds it: its `uid`, its state, its newest steps ([`steps`]) -/// and the state undoing the newest undo step gives back (that step's document). +/// A document as a drag from the list finds it: its `uid` and its count of File › Revert +/// ([`DocState::reverts`]), its state, its newest steps ([`steps`]) and the state undoing the newest +/// undo step gives back (that step's document). #[derive(Clone)] pub(super) struct DragStart { uid: u64, + reverts: u64, doc: Arc, steps: [u64; 4], back: Option>, @@ -109,24 +111,25 @@ pub(super) struct DragStart { impl DragStart { pub(super) fn of(st: &DocState) -> Self { let back = st.history.undo.last().map(|e| Arc::clone(&e.doc)); - DragStart { uid: st.uid, doc: Arc::clone(&st.doc), steps: steps(st), back } + DragStart { uid: st.uid, reverts: st.reverts, doc: Arc::clone(&st.doc), steps: steps(st), back } } /// Rows `names` as this document holds them. fn rows(&self, names: &[String]) -> HeldRows { let rows: Vec<_> = names.iter().map(|n| (n.clone(), HeldRow::of(&self.doc, n))).collect(); let back = rows.iter().map(|(_, r)| r.as_ref().zip(self.back.as_deref()).is_some_and(|(r, d)| r.unchanged_in(d))).collect(); - HeldRows { uid: self.uid, doc: Arc::downgrade(&self.doc), steps: self.steps, rows, back } + HeldRows { uid: self.uid, reverts: self.reverts, doc: Arc::downgrade(&self.doc), steps: self.steps, rows, back } } } -/// The rows a drag from the list carries, in the order of its names, and the document (`uid`) and -/// the state and undo history they were last checked against. A row is `None` once a state of the -/// document since the drag started did not hold it unchanged: deleted, edited, or deleted and made -/// again under its name, also with the same paint. +/// The rows a drag from the list carries, in the order of its names, and the document (`uid`, and +/// its count of File › Revert) and the state and undo history they were last checked against. A +/// row is `None` once a state of the document since the drag started did not hold it unchanged: +/// deleted, edited, or deleted and made again under its name, also with the same paint. #[derive(Clone)] pub(crate) struct HeldRows { uid: u64, + reverts: u64, doc: Weak, /// The newest undo step of the document and the one under it, and the next redo step and the /// one under it ([`steps`]). @@ -139,7 +142,12 @@ pub(crate) struct HeldRows { impl PartialEq for HeldRows { fn eq(&self, other: &Self) -> bool { - self.uid == other.uid && self.doc.ptr_eq(&other.doc) && self.steps == other.steps && self.rows == other.rows && self.back == other.back + self.uid == other.uid + && self.reverts == other.reverts + && self.doc.ptr_eq(&other.doc) + && self.steps == other.steps + && self.rows == other.rows + && self.back == other.back } } @@ -200,15 +208,16 @@ fn checked(held: HeldRows, st: &DocState) -> HeldRows { .collect(); let last = undo.last(); let back = rows.iter().map(|(_, r)| r.as_ref().zip(last).is_some_and(|(r, e)| r.unchanged_in(&e.doc))).collect(); - HeldRows { uid: held.uid, doc: Arc::downgrade(&st.doc), steps: now, rows, back } + HeldRows { uid: held.uid, reverts: held.reverts, doc: Arc::downgrade(&st.doc), steps: now, rows, back } } /// The names of drag `r` that the active document has held unchanged since the drag started -/// ([`checked`]), in the drag's order. Empty when the drag started in another document. The panel -/// keeps what it found for the drag, so the next check starts from there. +/// ([`checked`]), in the drag's order. Empty when the drag started in another document, or before +/// File › Revert replaced the document. The panel keeps what it found for the drag, so the next +/// check starts from there. pub(super) fn follow(app: &mut VectorcraftApp, r: &SwatchRows) -> Vec { let Some(st) = app.session.active() else { return vec![] }; - if st.uid != r.held.uid { + if st.uid != r.held.uid || st.reverts != r.held.reverts { return vec![]; } let kept = &mut app.ui.swatches_panel.drag; diff --git a/crates/ui-egui/src/panels/swatches/tests.rs b/crates/ui-egui/src/panels/swatches/tests.rs index 123f5be08b..479f0a5f55 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -665,6 +665,25 @@ fn new_swatch_skips_rows_replaced_during_the_drag() { assert_eq!(undo_steps(&app), undo + 1); } +/// A drag started before File › Revert replaced the document duplicates and deletes nothing, also +/// when the reverted document holds the same swatches. +#[test] +fn rows_dragged_before_a_revert_are_left_alone() { + let mut app = app(); + let key = crate::dialogs::confirm::KIND; + let drag = rows_drag(&app, "Orange", &["Orange"]); + let at = app.session.active_index().expect("a document"); + let copy = doc(&app); + assert!(app.session.replace_document(at, copy)); + let reverted = doc_and_steps(&app); + assert!(reverted.0.swatch("Orange").is_some(), "the reverted document has Orange"); + dnd::on_footer_drop(&mut app, dnd::FooterTarget::New, &drag); + assert_eq!(doc_and_steps(&app), reverted, "the reverted document's Orange is not duplicated"); + dnd::on_footer_drop(&mut app, dnd::FooterTarget::Delete, &drag); + assert!(!dialog_open(&app, key), "the reverted document asks nothing"); + assert_eq!(doc_and_steps(&app), reverted, "the reverted document keeps Orange"); +} + /// The rows of a drag running in the drag session are checked after every command the app runs: /// a swatch deleted and brought back by an undo, then an edit that clears the redo steps, all with /// no pass of the panel in between, is neither duplicated by New Swatch nor deleted by Delete