From 4020b59c5a2eaf6247a695b916a73cfd545a1420 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 08:17:05 -0800 Subject: [PATCH] Reject Swatches pattern drops from another document --- crates/ui-egui/src/panels/swatches.rs | 13 ++++------- crates/ui-egui/src/panels/swatches/dnd.rs | 12 ++++++++++ crates/ui-egui/src/panels/swatches/tests.rs | 25 ++++++++++++++++++++- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/crates/ui-egui/src/panels/swatches.rs b/crates/ui-egui/src/panels/swatches.rs index 9995752ec1..58f24ed3f5 100644 --- a/crates/ui-egui/src/panels/swatches.rs +++ b/crates/ui-egui/src/panels/swatches.rs @@ -14,7 +14,7 @@ use craft::color_ui::palette::{SwatchColor, SwatchEntry, SwatchGroup, SwatchKind use craft::color_ui::ui::{SwatchKey, SwatchesPanel, SwatchesState}; use craft::theme::Srgba; use craft::ui::choice::SwatchFace; -use craft::ui::contract::{Bind, DropAt, DropPlace, ListDrag, MemoProps, SelectMode}; +use craft::ui::contract::{Bind, DropPlace, ListDrag, MemoProps, SelectMode}; use craft::ui::contract::{Entry, Props as _, Show as _, ShowContainer as _, Trigger}; use craft::ui::menu::{Menu, MenuItem}; use craft::ui::patterns::presets::{PresetEvent, PresetView, ThumbInfo}; @@ -407,6 +407,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) { doc: Arc::clone(&st.doc), }; 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)))) @@ -416,8 +417,8 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) { _ => vec![], }) .row_items(|key| row_items(shown_app, key)) - .takes_drop::(DropPlace::Between, takes) - .takes_drop::(DropPlace::Onto, takes) + .takes_drop::(DropPlace::Between, |drag, _| dnd::accepts(drag, document)) + .takes_drop::(DropPlace::Onto, |drag, _| dnd::accepts(drag, document)) .select(SelectMode::Multi) .search(model.show_find) .reveal(focus.map(SwatchKey::Swatch)) @@ -467,12 +468,6 @@ fn on_event(app: &mut VectorcraftApp, shown: &[SwatchKey], event: PresetEvent) -> bool { - matches!(d, PanelDrag::Paint { .. } | PanelDrag::Art { .. }) -} - /// A double click or Enter: Swatch Options for a swatch, Edit or Apply Color Group for a group. fn activate(app: &mut VectorcraftApp, key: &SwatchKey) { match key { diff --git a/crates/ui-egui/src/panels/swatches/dnd.rs b/crates/ui-egui/src/panels/swatches/dnd.rs index dd3aff50f9..2151357979 100644 --- a/crates/ui-egui/src/panels/swatches/dnd.rs +++ b/crates/ui-egui/src/panels/swatches/dnd.rs @@ -4,6 +4,15 @@ 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. +pub(super) fn accepts(drag: &PanelDrag, document: u64) -> bool { + match drag { + PanelDrag::Paint { .. } => true, + PanelDrag::Art { document: source, .. } => *source == document, + _ => false, + } +} + /// A drag released on the panel. pub(super) enum Drop { /// Swatches (or colour groups) dropped before or `after` row `target`, into folder `target`, or @@ -75,6 +84,9 @@ fn place(shown: &[SwatchKey], at: &DropAt) -> (Option, bool) /// `drag` dropped onto a swatch or between rows of the `shown` list (ap.AP2 `Dropped`); a gap with no /// row under the pointer is the end of the list. pub(super) fn on_dropped(app: &mut VectorcraftApp, shown: &[SwatchKey], at: &DropAt, drag: &PanelDrag, mods: Mods) { + if !app.session.active().is_some_and(|st| accepts(drag, st.uid)) { + return; + } let (target, after) = place(shown, at); if let Some(drop) = Drop::of(drag, target, after, mods) { 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 b638b289f5..c770f5094a 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -2,7 +2,7 @@ use super::*; use crate::dialogs::swatch_options::KIND; use crate::tests::picks::{answer_dialog, cancel_dialog, draw_dialogs, open_dialogs}; use craft::keymap::Mods; -use craft::ui::contract::ReorderMove; +use craft::ui::contract::{DropAt, ReorderMove}; use craft::ui::dialog::DialogId; use egui::Pos2; use vectorcraft_color::Color; @@ -602,6 +602,29 @@ fn art_dropped_on_the_panel_becomes_a_pattern_swatch() { assert_eq!(doc(&app).node(vectorcraft_doc::NodeId(id)), before.node(vectorcraft_doc::NodeId(id)), "the art stays as it was"); } +#[test] +fn swatches_ignore_artwork_from_another_document_with_the_same_object_id() { + let mut app = app(); + let shape = json!({"x": 10, "y": 10, "width": 20, "height": 20}); + let source = app.run("shape.rectangle", shape.clone()).unwrap()["id"].as_u64().unwrap(); + let drag = art_drag(&app, vec![vectorcraft_doc::NodeId(source)]); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let local = app.run("shape.rectangle", shape).unwrap()["id"].as_u64().unwrap(); + assert_eq!(source, local); + + dropped_below(&mut app, drag.clone(), none()); + assert!(doc(&app).patterns.is_empty(), "foreign artwork cannot create a pattern"); + + let star = app.run("shape.star", json!({"cx": 50, "cy": 50, "radius1": 20, "radius2": 9, "points": 5})).unwrap()["id"].as_u64().unwrap(); + let local_drag = art_drag(&app, vec![vectorcraft_doc::NodeId(star)]); + dropped_below(&mut app, local_drag, none()); + assert_eq!(doc(&app).patterns.len(), 1, "the same document's artwork can create a pattern"); + let before = doc(&app).patterns[0].clone(); + let name = before.name.clone(); + dropped(&mut app, drag, &name, alt()); + assert_eq!(doc(&app).patterns[0].art, before.art, "foreign artwork cannot replace an existing pattern"); +} + /// Art released with Alt held over a pattern swatch becomes that pattern's tile art in one undo /// step: no pattern or swatch is added and the art stays. With Alt over a color swatch, or without /// Alt over the pattern swatch, the art becomes a new pattern swatch.