From e6e7d389a47e898c62a309388d7568c7e1c32958 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 08:25:36 -0800 Subject: [PATCH] Reject foreign-document artwork in 3D and Materials --- crates/ui-egui/src/dialogs/three_d.rs | 3 ++ .../ui-egui/src/dialogs/three_d_materials.rs | 5 +- crates/ui-egui/src/panels/tests_three_d.rs | 48 +++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/dialogs/three_d.rs b/crates/ui-egui/src/dialogs/three_d.rs index 8345aad401..1bf8a87680 100644 --- a/crates/ui-egui/src/dialogs/three_d.rs +++ b/crates/ui-egui/src/dialogs/three_d.rs @@ -55,6 +55,8 @@ pub(crate) struct Data { pub bevels_file: bool, /// The active document, for the symbol pictures. pub doc: Option>, + /// The document that owns the object IDs accepted by the Graphics drop target. + pub document_uid: Option, /// The active document's version. pub revision: u64, /// Whether the active document has a selection. @@ -77,6 +79,7 @@ pub(crate) fn data(app: &VectorcraftApp, effect: Option<&str>, params: &Map Option { Text::new(tl!("Graphics")).show(ui); let mut action = None; - let (Some(doc), true) = (data.doc.clone(), true) else { return action }; + let (Some(doc), Some(uid)) = (data.doc.clone(), data.document_uid) else { return action }; let names: Vec = doc.symbols.iter().map(|s| s.name.clone()).collect(); let keys = crate::panels::symbols::document_symbols(&doc, data.revision); let source = keyed(&names[..], |name: &String| name.clone()).with_text(|name| Cow::Borrowed(name.as_str())); - let mut dropped = DropAction::::new(|drag| matches!(drag, PanelDrag::Art { .. })).show(ui, |ui, _| { + let drop_art = DropAction::::new(move |drag| matches!(drag, PanelDrag::Art { document, .. } if *document == uid)); + let mut dropped = drop_art.show(ui, |ui, _| { TileGrid::new(tl!("Graphics")).activate(ActivateOn::Click).show_items(ui, &source, &mut |name, _| { let tip = if apply { name.clone() } else { format!("{name}: {}", tl!("Select a 3D object to map the graphic onto it.")) }; let (doc, keys, name) = (doc.clone(), keys.clone(), name.clone()); diff --git a/crates/ui-egui/src/panels/tests_three_d.rs b/crates/ui-egui/src/panels/tests_three_d.rs index e13cf649a6..cc1c1593da 100644 --- a/crates/ui-egui/src/panels/tests_three_d.rs +++ b/crates/ui-egui/src/panels/tests_three_d.rs @@ -686,6 +686,54 @@ fn art_can_be_added_as_a_graphic_before_any_3d_effect() { assert!(painted_text(&mut app, show).contains("Add as Single Graphic")); } +#[test] +fn three_d_graphics_accepts_artwork_only_from_its_document() { + use crate::panel_drag::PanelDrag; + use crate::tests::panel_harness::Panel; + use craft::ui::contract::{ListDrag, ShowCollection as _}; + use craft::ui::list::{List, ListRow}; + use craft::ui::runtime::drag; + + fn with_artwork(app: &mut VectorcraftApp, ui: &mut egui::Ui) { + let st = app.session.active().unwrap(); + let payload = PanelDrag::Art { document: st.uid, ids: st.selection.objects.clone() }; + ui.allocate_ui(egui::vec2(ui.available_width(), 80.0), |ui| { + let keys = craft::behavior::keyed(&[0_usize], |key: &usize| *key); + List::::new("Artwork") + .drag_payload(move |_: &ListDrag| Some(payload.clone())) + .show_items(ui, &keys, &mut |_, _| ListRow::new("Dragged artwork")); + }); + show(app, ui); + } + + let mut app = app(); + let source = app.session.active().unwrap().uid; + let first = app.session.active().unwrap().selection.objects[0]; + let add_existing = |app: &mut VectorcraftApp| app.run("threeD.addGraphic", json!({"name": "Existing Graphic"})).unwrap(); + add_existing(&mut app); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + let second = app.run("shape.rectangle", json!({"x": 30, "y": 30, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap(); + add_existing(&mut app); + assert_eq!(first.0, second, "the two documents have different artwork with the same local ID"); + assert!(app.session.set_active(0)); + + let mut p = Panel::new(app, with_artwork); + let from = p.rect("Dragged artwork").center(); + let to = p.rect("Existing Graphic").center(); + p.drag_hold_role("Dragged artwork", craft::ui::contract::Role::LIST_ITEM, to - from); + assert!(drag::mark(p.ctx()).is_some(), "Graphics accepts the artwork before switching documents"); + assert!(p.app().session.set_active(1)); + p.frame(); + let carried = drag::active::(p.ctx()); + assert!(matches!(carried.as_deref(), Some(PanelDrag::Art { document, .. }) if *document == source)); + assert!(drag::mark(p.ctx()).is_none(), "Graphics withdraws the drop indicator when the artwork is foreign"); + p.release(); + assert_eq!(p.app().session.active().unwrap().doc.symbols.len(), 1, "the foreign drop adds no symbol from the active document's same-ID artwork"); + + p.drag("Dragged artwork", "Existing Graphic", egui::Modifiers::NONE); + assert_eq!(p.app().session.active().unwrap().doc.symbols.len(), 2, "a same-document drop still makes a graphic"); +} + #[test] fn a_graphics_handle_drags_it_over_its_face_in_one_undo_step() { let (mut app, _) = materials_app();