From 956ac0bb578b327987a7d2337290aae083aebc89 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 08:31:32 -0800 Subject: [PATCH] Keep appearance drags in their source document --- crates/ui-egui/src/canvas/drops.rs | 8 ++- crates/ui-egui/src/canvas/drops/tests.rs | 30 ++++++++++- crates/ui-egui/src/panel_drag.rs | 2 +- crates/ui-egui/src/panels/appearance.rs | 4 +- crates/ui-egui/src/panels/graphic_styles.rs | 56 ++++++++++++++++++-- crates/ui-egui/src/panels/layers/cells.rs | 23 ++++---- crates/ui-egui/src/panels/layers/controls.rs | 6 ++- 7 files changed, 107 insertions(+), 22 deletions(-) diff --git a/crates/ui-egui/src/canvas/drops.rs b/crates/ui-egui/src/canvas/drops.rs index f2ae87a7a8..732984f606 100644 --- a/crates/ui-egui/src/canvas/drops.rs +++ b/crates/ui-egui/src/canvas/drops.rs @@ -170,7 +170,11 @@ impl Component for CanvasDrops<'_> { } if self.kind == BodyKind::Live { // The art of this canvas's own document is not accepted as a panel payload: its move resumes. - let panel = Target::new(rect, view, covered, |p: &PanelDrag| (!matches!(p, PanelDrag::Art { .. })).then(|| Payload::Panel(p.clone()))); + let panel = Target::new(rect, view, covered, |p: &PanelDrag| match p { + PanelDrag::Art { .. } => None, + PanelDrag::Appearance { document, .. } if *document != self.document => None, + _ => Some(Payload::Panel(p.clone())), + }); let style = Target::new(rect, view, covered, |d: &ListDrag| Some(Payload::Style(d.grabbed.clone()))); let symbol = Target::new(rect, view, covered, |d: &ListDrag| Some(Payload::Symbol(d.grabbed.clone()))); let brush = Target::new(rect, view, covered, |d: &ListDrag| Some(Payload::Brush(d.grabbed.clone()))); @@ -370,7 +374,7 @@ pub(crate) fn apply_drop(app: &mut VectorcraftApp, drop: &Dropped) -> Result<(), } run(app, cmd, params) } - Payload::Panel(PanelDrag::Appearance(source)) => { + Payload::Panel(PanelDrag::Appearance { document, source }) if app.session.active().is_some_and(|st| st.uid == *document) => { let Some(hit) = hit() else { return Ok(()) }; let target = top_object(app, &hit)?; if target == source.0 { diff --git a/crates/ui-egui/src/canvas/drops/tests.rs b/crates/ui-egui/src/canvas/drops/tests.rs index 6fdaf1b7af..869737b188 100644 --- a/crates/ui-egui/src/canvas/drops/tests.rs +++ b/crates/ui-egui/src/canvas/drops/tests.rs @@ -119,12 +119,14 @@ fn a_drop_runs_the_effect_of_its_payload() { drop_at(&mut app, Payload::Panel(PanelDrag::paint(Paint::Gradient(Box::new(gradient)))), on_b, Move).unwrap(); assert!(matches!(fill(&app), Paint::Gradient(g) if g.geom.is_none()), "a gradient fits the object it lands on"); app.run("transparency.set", json!({"opacity": 40})).unwrap(); - drop_at(&mut app, Payload::Panel(PanelDrag::Appearance(a)), on_b, Move).unwrap(); + let document = app.session.active().unwrap().uid; + let appearance = |source| Payload::Panel(PanelDrag::Appearance { document, source }); + drop_at(&mut app, appearance(a), on_b, Move).unwrap(); let doc = &app.session.active().unwrap().doc; let (na, nb) = (doc.node(a).unwrap(), doc.node(b).unwrap()); assert_eq!((&nb.appearance, nb.opacity), (&na.appearance, na.opacity), "the appearance is copied"); let n = undo_len(&app); - drop_at(&mut app, Payload::Panel(PanelDrag::Appearance(b)), on_b, Move).unwrap(); + drop_at(&mut app, appearance(b), on_b, Move).unwrap(); assert_eq!(undo_len(&app), n, "dropped on its own source: nothing changes"); let path = NodeId(app.run("path.create", json!({"anchors": [{"x": 50, "y": 200}, {"x": 350, "y": 200}]})).unwrap()["id"].as_u64().unwrap()); @@ -292,3 +294,27 @@ fn an_anchor_dragged_off_the_canvas_is_not_carried() { frame(&mut app, &ctx, vec![]); assert!(craft::ui::runtime::drag::active::(&ctx).is_none()); } + +#[test] +fn a_foreign_appearance_drop_never_copies_a_matching_local_object() { + let (mut app, ctx) = app_with_canvas(); + let add = |app: &mut VectorcraftApp, x| { + NodeId(app.run("shape.rectangle", json!({"x": x, "y": 20, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap()) + }; + let source = add(&mut app, 20); + let document = app.session.active().unwrap().uid; + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let other = add(&mut app, 20); + assert_eq!(source, other, "both documents have different objects with the same local ID"); + app.run("paint.setFill", json!({"color": "#ed1c24"})).unwrap(); + let target = add(&mut app, 200); + app.run("paint.setFill", json!({"color": "#0000ff"})).unwrap(); + frame(&mut app, &ctx, vec![]); + + let fill = |app: &VectorcraftApp| app.session.active().unwrap().doc.node(target).unwrap().appearance.fill_paint(); + let before = fill(&app); + let history = undo_len(&app); + drop_at(&mut app, Payload::Panel(PanelDrag::Appearance { document, source }), Point::new(250.0, 70.0), DropOperation::Move).unwrap(); + assert_eq!(fill(&app), before); + assert_eq!(undo_len(&app), history, "a foreign appearance drop makes no edit"); +} diff --git a/crates/ui-egui/src/panel_drag.rs b/crates/ui-egui/src/panel_drag.rs index 83ebf46070..5f4f6f0a1f 100644 --- a/crates/ui-egui/src/panel_drag.rs +++ b/crates/ui-egui/src/panel_drag.rs @@ -19,7 +19,7 @@ pub enum PanelDrag { /// The Appearance panel's thumbnail: art it is dropped on takes object `0`'s appearance /// (`appearance.copyFrom`); the Graphic Styles panel makes a style of it, or with Alt held /// replaces the style it is dropped on with it (`graphicStyle.redefine`). - Appearance(vectorcraft_doc::NodeId), + Appearance { document: u64, source: vectorcraft_doc::NodeId }, /// The selected art, dragged off the canvas with the Selection tool (`document`: the uid of the /// document it came from): the Graphic Styles panel makes a style of the first object /// (`graphicStyle.new`), or with Alt held replaces the style it is dropped on with that diff --git a/crates/ui-egui/src/panels/appearance.rs b/crates/ui-egui/src/panels/appearance.rs index 2ae612ca36..154f9e55f5 100644 --- a/crates/ui-egui/src/panels/appearance.rs +++ b/crates/ui-egui/src/panels/appearance.rs @@ -799,8 +799,8 @@ fn object_row<'r>(cx: &'r RowCx<'_>) -> ListRow<'r> { None => cx.app.session.paint.fill.clone(), }; let row = row.thumb(ThumbSize::Small, move |painter, rect| paint_preview::paint_rect(painter, rect, &fill)); - match cx.node.filter(|_| cx.mix.is_none()) { - Some(n) => row.part_drag(RowDragSpot::Thumb(ThumbTarget::First), PanelDrag::Appearance(n.id)), + match cx.node.filter(|_| cx.mix.is_none()).zip(cx.app.session.active()) { + Some((n, st)) => row.part_drag(RowDragSpot::Thumb(ThumbTarget::First), PanelDrag::Appearance { document: st.uid, source: n.id }), None => row, } } diff --git a/crates/ui-egui/src/panels/graphic_styles.rs b/crates/ui-egui/src/panels/graphic_styles.rs index 380a605cd2..bb87966d80 100644 --- a/crates/ui-egui/src/panels/graphic_styles.rs +++ b/crates/ui-egui/src/panels/graphic_styles.rs @@ -601,11 +601,11 @@ fn reorder(app: &mut VectorcraftApp, edit: EditEvent>) { } /// The object whose look a dropped payload carries: the first of the art dragged from this document, or the -/// Appearance panel's object. +/// Appearance panel's object from this document. fn dropped_object(drag: &PanelDrag, uid: u64) -> Option { match drag { PanelDrag::Art { document, ids } if *document == uid => ids.first().copied(), - PanelDrag::Appearance(id) => Some(*id), + PanelDrag::Appearance { document, source } if *document == uid => Some(*source), _ => None, } } @@ -1322,7 +1322,7 @@ mod tests { let (red, green) = (doc(&app).graphic_style("Red").unwrap().appearance.fill_paint(), doc(&app).node(oval).unwrap().appearance.fill_paint()); let n = names(&app); let document = app.session.active().unwrap().uid; - for payload in [PanelDrag::Art { document, ids: vec![oval] }, PanelDrag::Appearance(oval)] { + for payload in [PanelDrag::Art { document, ids: vec![oval] }, PanelDrag::Appearance { document, source: oval }] { drop_on(&mut app, Some("Red"), dropped_object(&payload, document), None, true); let d = doc(&app); assert_eq!(d.graphic_style("Red").unwrap().appearance.fill_paint(), green); @@ -1388,6 +1388,56 @@ mod tests { assert_eq!(names(&p.app()).len(), before.len() + 1, "a same-document drop still makes a style"); } + #[test] + fn graphic_styles_rejects_a_foreign_appearance_after_next_document() { + use crate::tests::panel_harness::Panel as PanelHarness; + use craft::ui::contract::ShowCollection as _; + use craft::ui::list::{List, ListRow}; + use craft::ui::runtime::drag; + + fn with_appearance(app: &mut VectorcraftApp, ui: &mut Ui) { + let st = app.session.active().unwrap(); + let source = st.selection.objects.first().copied(); + let document = st.uid; + ui.allocate_ui(egui::vec2(ui.available_width(), 80.0), |ui| { + let keys = craft::behavior::keyed(&[0_usize], |key: &usize| *key); + List::::new("Appearance source") + .drag_payload(move |_: &ListDrag| source.map(|source| PanelDrag::Appearance { document, source })) + .show_items(ui, &keys, &mut |_, _| ListRow::new("Dragged appearance")); + }); + show(app, ui); + } + + let mut app = app(); + app.run("graphicStyle.new", json!({"name": "Drop Target"})).unwrap(); + let first = app.session.active().unwrap(); + let (source_document, source_object) = (first.uid, first.selection.objects[0]); + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 50, "height": 50})).unwrap(); + app.run("graphicStyle.new", json!({"name": "Drop Target"})).unwrap(); + let second = app.session.active().unwrap(); + assert_eq!(source_object, second.selection.objects[0], "both documents have distinct objects with the same local ID"); + let target_document = second.uid; + let before = names(&app); + assert!(app.session.set_active(0)); + + let mut p = PanelHarness::new(app, with_appearance); + let from = p.rect("Dragged appearance").center(); + let to = p.rect("Drop Target").center(); + p.drag_hold_role("Dragged appearance", craft::ui::contract::Role::LIST_ITEM, to - from); + assert!(drag::mark(p.ctx()).is_some()); + p.key(egui::Key::F6, egui::Modifiers::COMMAND); + assert_eq!(p.app().session.active().unwrap().uid, target_document, "Next Document switches tabs during the drag"); + let carried = drag::active::(p.ctx()); + assert_eq!(carried.as_deref(), Some(&PanelDrag::Appearance { document: source_document, source: source_object })); + assert!(drag::mark(p.ctx()).is_none(), "the target withdraws its drop indicator for the foreign appearance"); + p.release(); + assert_eq!(names(&p.app()), before, "the foreign drop creates no style from the current document's same-ID object"); + + p.drag("Dragged appearance", "Drop Target", egui::Modifiers::NONE); + assert_eq!(names(&p.app()).len(), before.len() + 1, "a same-document appearance still makes a style"); + } + /// A style dropped on another with Alt takes that style's look; on itself it does nothing. #[test] fn a_style_dropped_on_another_with_alt_replaces_that_style() { diff --git a/crates/ui-egui/src/panels/layers/cells.rs b/crates/ui-egui/src/panels/layers/cells.rs index 1b9bdadd43..130e16c2c3 100644 --- a/crates/ui-egui/src/panels/layers/cells.rs +++ b/crates/ui-egui/src/panels/layers/cells.rs @@ -92,6 +92,7 @@ impl Outcome for CellOutput { pub(super) struct TargetCircle { #[common] common: Common, + document: u64, id: NodeId, targeted: bool, styled: bool, @@ -101,18 +102,19 @@ pub(super) struct TargetCircle { } impl TargetCircle { - fn new(id: NodeId, targeted: bool, styled: bool) -> Self { - Self { common: Common::new(), id, targeted, styled, held: false, pick: None } + fn new(document: u64, id: NodeId, targeted: bool, styled: bool) -> Self { + Self { common: Common::new(), document, id, targeted, styled, held: false, pick: None } } /// A targeted circle with an appearance, for the suite. fn sample() -> Self { - Self::new(NodeId(1), true, true).label("Target") + Self::new(1, NodeId(1), true, true).label("Target") } } /// The circle as a drop target: it takes another row's appearance. struct TakesAppearance { + document: u64, own: NodeId, rect: Rect, } @@ -122,7 +124,9 @@ impl DropTarget for TakesAppearance { fn accept(&self, payload: &PanelDrag, _: Pos2, op: DropOperation) -> Option<(NodeId, bool)> { match payload { - PanelDrag::Appearance(source) if *source != self.own => Some((*source, op == DropOperation::Copy)), + PanelDrag::Appearance { document, source } if *document == self.document && *source != self.own => { + Some((*source, op == DropOperation::Copy)) + } _ => None, } } @@ -165,13 +169,12 @@ impl Component for TargetCircle { self.pick = Some(CellPick::Target(activation.mods)); } if primary_drag_start(cx) { - cx.start_drag(PanelDrag::Appearance(self.id), DragPreview::None); + cx.start_drag(PanelDrag::Appearance { document: self.document, source: self.id }, DragPreview::None); } let rect = cx.rect; - let own = self.id; - self.held = - cx.dragging::().is_some_and(|(drag, at)| rect.contains(at) && matches!(*drag, PanelDrag::Appearance(source) if source != own)); - if let Some((source, copy)) = cx.drop_target::(&TakesAppearance { own, rect }) { + let target = TakesAppearance { document: self.document, own: self.id, rect }; + self.held = cx.dragging::().is_some_and(|(drag, at)| rect.contains(at) && target.accept(&drag, at, DropOperation::Move).is_some()); + if let Some((source, copy)) = cx.drop_target::(&target) { self.pick = Some(CellPick::TakeAppearance { source, copy }); } } @@ -373,7 +376,7 @@ pub(super) fn build(tree: &DocLayers<'_>, row: &RowId, info: &LayerInfo) -> Cell let is_sel = selected(id); let (full, holds) = square_marks(tree, n); let art = if full || holds { st.selection.objects.clone() } else { vec![] }; - let circle = TargetCircle::new(id, shows_targeted(tree, n, is_sel), has_styled_target(n)) + let circle = TargetCircle::new(st.uid, id, shows_targeted(tree, n, is_sel), has_styled_target(n)) .label(crate::i18n::fmt(tl!("Target {row}"), &[("row", info.name.as_str())])); let square = SelectionSquare::new(Some(id), full, holds, color, art) .label(crate::i18n::fmt(tl!("Select art of {row}"), &[("row", info.name.as_str())])); diff --git a/crates/ui-egui/src/panels/layers/controls.rs b/crates/ui-egui/src/panels/layers/controls.rs index b337508c90..df30e7e731 100644 --- a/crates/ui-egui/src/panels/layers/controls.rs +++ b/crates/ui-egui/src/panels/layers/controls.rs @@ -464,8 +464,10 @@ pub(crate) fn footer(tree: &DocLayers, ui: &mut Ui, out: &mut Vec<(String, Value if let Some(drop) = &output.dropped { let run = if let Some(d) = drop.payload::>>() { footer_drop(tree, drop.index, FooterPayload::Rows(&d.keys)) - } else if let Some(PanelDrag::Appearance(id)) = drop.payload::() { - footer_drop(tree, drop.index, FooterPayload::Appearance(id.0)) + } else if let Some(PanelDrag::Appearance { document, source }) = drop.payload::() + && *document == st.uid + { + footer_drop(tree, drop.index, FooterPayload::Appearance(source.0)) } else { None };