Reject Swatches pattern drops from another document

This commit is contained in:
dotCooCoo
2026-11-26 08:19:45 -08:00
parent 2a1b573a78
commit 4020b59c5a
3 changed files with 40 additions and 10 deletions
+4 -9
View File
@@ -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<SwatchKey>| 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::<PanelDrag>(DropPlace::Between, takes)
.takes_drop::<PanelDrag>(DropPlace::Onto, takes)
.takes_drop::<PanelDrag>(DropPlace::Between, |drag, _| dnd::accepts(drag, document))
.takes_drop::<PanelDrag>(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<Sw
}
}
/// What a swatch's drag or row menu may carry: paints and art dragged off the canvas, not the Appearance panel's
/// thumbnail.
fn takes(d: &PanelDrag, _at: &DropAt<SwatchKey>) -> 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 {
+12
View File
@@ -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<SwatchKey>) -> (Option<String>, 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<SwatchKey>, 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);
+24 -1
View File
@@ -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.