From 10c8cba547a212aac504df4bf75bf1270f75fef4 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 09:45:56 -0800 Subject: [PATCH] Keep Swatches list drags in the document that started them --- crates/ui-egui/src/panel_drag.rs | 2 + crates/ui-egui/src/panels/swatches.rs | 9 ++-- crates/ui-egui/src/panels/swatches/dnd.rs | 7 +-- crates/ui-egui/src/panels/swatches/tests.rs | 56 ++++++++++++++++++++- 4 files changed, 66 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/panel_drag.rs b/crates/ui-egui/src/panel_drag.rs index 83ebf46070..27cab1fe2c 100644 --- a/crates/ui-egui/src/panel_drag.rs +++ b/crates/ui-egui/src/panel_drag.rs @@ -41,6 +41,8 @@ pub enum PanelDrag { /// panel order. #[derive(Clone, Debug, PartialEq)] pub struct SwatchRows { + /// The document whose rows the drag started on. + pub document: u64, pub grabbed: String, pub names: Vec, /// The names are colour groups. diff --git a/crates/ui-egui/src/panels/swatches.rs b/crates/ui-egui/src/panels/swatches.rs index 58f24ed3f5..bb8eaa994f 100644 --- a/crates/ui-egui/src/panels/swatches.rs +++ b/crates/ui-egui/src/panels/swatches.rs @@ -409,8 +409,8 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) { let dragged_doc = Arc::clone(&thumbs.doc); let document = st.uid; let shown_app: &VectorcraftApp = app; - let out = SwatchesPanel::new(&data, Bind::bound(&mut model.state), Srgba::rgb(0.0, 0.0, 0.0)) - .drag_payload(move |drag: &ListDrag| Some(dnd::drag_of(&dragged_doc, &drag.grabbed, dragged_rows(drag)))) + let browser = SwatchesPanel::new(&data, Bind::bound(&mut model.state), Srgba::rgb(0.0, 0.0, 0.0)) + .drag_payload(move |drag: &ListDrag| Some(dnd::drag_of(&dragged_doc, document, &drag.grabbed, dragged_rows(drag)))) .thumb(thumbs, thumb) .row_icons(|key| match key { SwatchKey::Swatch(name) => row_icons(shown_app, name), @@ -422,8 +422,9 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) { .select(SelectMode::Multi) .search(model.show_find) .reveal(focus.map(SwatchKey::Swatch)) - .footer(None) - .show(ui); + .footer(None); + // A list drag is keyed by its browser ID. Each document has its own browser. + let out = ui.push_id(("swatch-rows", document), |ui| browser.show(ui)).inner; app.ui.swatches_panel = model; let shown = shown_keys(&data); if let Some((at, _, alt)) = out.used diff --git a/crates/ui-egui/src/panels/swatches/dnd.rs b/crates/ui-egui/src/panels/swatches/dnd.rs index 2151357979..f4d100f2d6 100644 --- a/crates/ui-egui/src/panels/swatches/dnd.rs +++ b/crates/ui-egui/src/panels/swatches/dnd.rs @@ -4,9 +4,10 @@ use craft::color_ui::ui::SwatchKey; use craft::keymap::Mods; use craft::ui::contract::{DropAt, ReorderMove}; -/// Swatches take paint payloads and artwork from the current document's canvas. +/// Swatches take paints, and take swatch rows or canvas art from the current document. pub(super) fn accepts(drag: &PanelDrag, document: u64) -> bool { match drag { + PanelDrag::Paint { rows: Some(rows), .. } => rows.document == document, PanelDrag::Paint { .. } => true, PanelDrag::Art { document: source, .. } => *source == document, _ => false, @@ -53,7 +54,7 @@ impl Drop { /// The drag a row of `key` starts, moving rows `names`. Colour groups have no paint of their own /// (the chip under the pointer draws Registration's mark for Registration). -pub(super) fn drag_of(d: &Document, key: &SwatchKey, names: Vec) -> PanelDrag { +pub(super) fn drag_of(d: &Document, document: u64, key: &SwatchKey, names: Vec) -> PanelDrag { let name = name_of(key); let (paint, params) = match key { SwatchKey::Swatch(_) if name == REGISTRATION => (Paint::registration(), swatch_params(d, name)), @@ -63,7 +64,7 @@ pub(super) fn drag_of(d: &Document, key: &SwatchKey, names: Vec) -> Pane }, _ => (Paint::None, Value::Null), }; - let rows = SwatchRows { grabbed: name.to_string(), names, groups: matches!(key, SwatchKey::Group(_)) }; + let rows = SwatchRows { document, grabbed: name.to_string(), names, groups: matches!(key, SwatchKey::Group(_)) }; PanelDrag::Paint { paint, params, rows: Some(rows), library: None } } diff --git a/crates/ui-egui/src/panels/swatches/tests.rs b/crates/ui-egui/src/panels/swatches/tests.rs index c770f5094a..0e5387e547 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -96,7 +96,61 @@ fn dropped_below(app: &mut VectorcraftApp, drag: PanelDrag, mods: Mods) { /// The drag the row `name` starts, carrying rows `names`. fn rows_drag(app: &VectorcraftApp, name: &str, names: &[&str]) -> PanelDrag { - dnd::drag_of(&doc(app), &key_of(app, name), names.iter().map(|n| n.to_string()).collect()) + let document = app.session.active().unwrap().uid; + dnd::drag_of(&doc(app), document, &key_of(app, name), names.iter().map(|n| n.to_string()).collect()) +} + +#[test] +fn a_swatch_row_drag_reorders_only_its_source_document() { + use crate::tests::panel_harness::Panel; + use craft::ui::contract::{ListDrag, Role}; + use craft::ui::runtime::drag; + use egui::{Key, Modifiers}; + + let mut app = app(); + let source = app.session.active().unwrap().uid; + let before_source = names_of(&app, None); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let destination = app.session.active().unwrap().uid; + let before_destination = names_of(&app, None); + app.run("window.previousDocument", json!({})).unwrap(); + assert_eq!(app.session.active().unwrap().uid, source); + + let mut panel = Panel::new(app, show); + let drag_red = |panel: &mut Panel| { + let red = "Red, #ed1c24"; + let from = panel.rect(red).center(); + let to = panel.rect("Green, #39b54a").center(); + panel.drag_hold_role(red, Role::LIST_ITEM, to - from); + let grabbed = drag::active::>(panel.ctx()).map(|d| d.grabbed.clone()); + assert_eq!(grabbed, Some(SwatchKey::Swatch("Red".into()))); + }; + drag_red(&mut panel); + panel.key(Key::F6, Modifiers::COMMAND); + assert_eq!(panel.app().session.active().unwrap().uid, destination); + panel.release(); + assert_eq!(names_of(&panel.app(), None), before_destination); + + panel.key(Key::F6, Modifiers::COMMAND); + assert_eq!(panel.app().session.active().unwrap().uid, source); + assert_eq!(names_of(&panel.app(), None), before_source); + drag_red(&mut panel); + panel.release(); + assert_ne!(names_of(&panel.app(), None), before_source); + panel.app().run("edit.undo", json!({})).unwrap(); + assert_eq!(names_of(&panel.app(), None), before_source); +} + +#[test] +fn a_foreign_swatch_row_payload_cannot_move_or_replace_local_swatches() { + let mut app = app(); + let drag = rows_drag(&app, "Red", &["Red"]); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let before = doc(&app); + assert!(!dnd::accepts(&drag, app.session.active().unwrap().uid)); + dropped_below(&mut app, drag.clone(), none()); + dropped(&mut app, drag, "Green", alt()); + assert_eq!(doc(&app).swatches, before.swatches); } /// Art `ids` dragged off the canvas.