From f4b50a47349f49db57067b0e19cbbc07c7686d46 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:44:50 -0800 Subject: [PATCH 1/2] Refuse panel artwork drags from before File Revert --- crates/ui-egui/src/canvas/drops.rs | 3 +- crates/ui-egui/src/canvas/drops/tests.rs | 68 +++++++++++++++++++ crates/ui-egui/src/dialogs/three_d.rs | 4 +- .../ui-egui/src/dialogs/three_d_materials.rs | 6 +- crates/ui-egui/src/panel_drag.rs | 12 +++- crates/ui-egui/src/panels/asset_export.rs | 9 ++- crates/ui-egui/src/panels/brushes.rs | 21 +++--- crates/ui-egui/src/panels/brushes/tests.rs | 8 +-- crates/ui-egui/src/panels/graphic_styles.rs | 19 +++--- crates/ui-egui/src/panels/libraries.rs | 21 ++++-- crates/ui-egui/src/panels/libraries/tests.rs | 3 +- crates/ui-egui/src/panels/swatches.rs | 5 +- crates/ui-egui/src/panels/swatches/dnd.rs | 8 +-- crates/ui-egui/src/panels/swatches/tests.rs | 6 +- crates/ui-egui/src/panels/symbols.rs | 22 +++--- crates/ui-egui/src/panels/symbols/tests.rs | 4 +- .../ui-egui/src/panels/tests_asset_export.rs | 2 +- crates/ui-egui/src/panels/tests_three_d.rs | 2 +- 18 files changed, 156 insertions(+), 67 deletions(-) diff --git a/crates/ui-egui/src/canvas/drops.rs b/crates/ui-egui/src/canvas/drops.rs index e432b42759..bb7dc54ebb 100644 --- a/crates/ui-egui/src/canvas/drops.rs +++ b/crates/ui-egui/src/canvas/drops.rs @@ -279,8 +279,9 @@ pub(crate) fn released_on_pane(app: &VectorcraftApp, art: CarriedArt, at: Pos2, /// to the drag session. Moves of anchors or segments stay reshapes. `covered`: another window is over the canvas at the /// pointer. → whether a carry runs. pub(crate) fn carry_out(app: &mut VectorcraftApp, cx: &mut ToolContext<'_>, pointer: &ToolPointer, covered: bool) -> bool { + let Some(reverts) = app.session.active().map(|st| st.reverts) else { return false }; let Some(art) = carried(app, cx, pointer, covered) else { return false }; - let panel = PanelDrag::Art { document: art.document, ids: art.ids.clone() }; + let panel = PanelDrag::Art { document: art.document, reverts, ids: art.ids.clone() }; cx.start_drag_also(art, Arc::new(panel), DragPreview::None); true } diff --git a/crates/ui-egui/src/canvas/drops/tests.rs b/crates/ui-egui/src/canvas/drops/tests.rs index 6060043515..1e47cc9133 100644 --- a/crates/ui-egui/src/canvas/drops/tests.rs +++ b/crates/ui-egui/src/canvas/drops/tests.rs @@ -388,6 +388,74 @@ fn button(pos: Pos2, pressed: bool) -> Event { Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Default::default() } } +#[test] +fn a_canvas_art_drag_cannot_make_a_graphic_style_after_file_revert() { + use crate::tests::panel_harness::Panel; + use craft::ui::runtime::drag; + + struct SavedFile(std::path::PathBuf); + impl Drop for SavedFile { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } + } + + fn canvas_and_styles(app: &mut VectorcraftApp, ui: &mut egui::Ui) { + egui::Panel::top("audit-styles").exact_size(220.0).show(ui, |ui| crate::panels::graphic_styles::show(app, ui)); + canvas::show(app, ui); + } + + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let id = NodeId(app.run("shape.rectangle", json!({"x": 50, "y": 50, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap()); + app.run("paint.setFill", json!({"color": "#ff0000", "ids": [id.0]})).unwrap(); + app.run("graphicStyle.new", json!({"name": "Drop Target"})).unwrap(); + let path = SavedFile(std::env::temp_dir().join(format!("vc-ui-artwork-drop-revert-{}.vectorcraft", std::process::id()))); + app.run("file.saveAs", json!({"path": path.0})).unwrap(); + assert!(path.0.is_file()); + app.run("paint.setFill", json!({"color": "#00ff00", "ids": [id.0]})).unwrap(); + app.run("select.none", json!({})).unwrap(); + app.select_tool("selection"); + let mut panel = Panel::new(app, canvas_and_styles); + let carry_to_style = |panel: &mut Panel| { + let at = panel.app().canvas_view().unwrap().to_screen(Point::new(100.0, 100.0)); + let target = panel.rect("Drop Target").center(); + panel.frame_with(vec![Event::PointerMoved(at), button(at, true)]); + panel.frame_with(vec![Event::PointerMoved(at + egui::vec2(20.0, 0.0))]); + assert!(panel.app().session.active().unwrap().interaction.is_some(), "the Selection tool starts a move"); + panel.frame_with(vec![Event::PointerMoved(target)]); + panel.frame(); + let carried = drag::active::(panel.ctx()); + assert!(matches!(carried.as_deref(), Some(PanelDrag::Art { ids, .. }) if ids == &[id]), "the canvas starts the panel payload"); + assert!(drag::mark(panel.ctx()).is_some(), "Graphic Styles accepts artwork dragged from the current contents"); + }; + carry_to_style(&mut panel); + + let (uid, reverts) = { + let app = panel.app(); + let state = app.session.active().unwrap(); + (state.uid, state.reverts) + }; + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + let styles = |app: &VectorcraftApp| app.session.active().unwrap().doc.graphic_styles.len(); + let count = { + let app = panel.app(); + let state = app.session.active().unwrap(); + assert_eq!((state.uid, state.reverts), (uid, reverts + 1)); + assert!(state.doc.node(id).is_some(), "the restored object keeps the same ID"); + styles(&app) + }; + panel.frame(); + assert!(drag::mark(panel.ctx()).is_none()); + panel.release(); + assert_eq!(styles(&panel.app()), count, "a drag from discarded art cannot create a style from restored art"); + carry_to_style(&mut panel); + panel.release(); + assert_eq!(styles(&panel.app()), count + 1, "a fresh drag can still create a style from the restored artwork"); +} + /// A Selection, Direct Selection or Group Selection move that leaves the canvas hands the art to the drag session /// with the ids the move started with, and the move waits where it was last. #[test] diff --git a/crates/ui-egui/src/dialogs/three_d.rs b/crates/ui-egui/src/dialogs/three_d.rs index 1bf8a87680..385c928350 100644 --- a/crates/ui-egui/src/dialogs/three_d.rs +++ b/crates/ui-egui/src/dialogs/three_d.rs @@ -56,7 +56,7 @@ pub(crate) struct Data { /// 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, + pub document: Option<(u64, u64)>, /// The active document's version. pub revision: u64, /// Whether the active document has a selection. @@ -79,7 +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), Some(uid)) = (data.doc.clone(), data.document_uid) else { return action }; + let (Some(doc), Some((uid, reverts))) = (data.doc.clone(), data.document) 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 drop_art = DropAction::::new(move |drag| matches!(drag, PanelDrag::Art { document, .. } if *document == uid)); + let drop_art = DropAction::::new(move |drag| drag.artwork_for(uid, reverts).is_some()); 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.")) }; @@ -199,7 +199,7 @@ fn library(ui: &mut Ui, data: &Data, apply: bool) -> Option { action = Some(Action::Map(name)); } } - if let Some(PanelDrag::Art { ids, .. }) = dropped.output.dropped { + if let Some(ids) = dropped.output.dropped.as_ref().and_then(|drag| drag.artwork_for(uid, reverts)) { action = Some(Action::Add(Some(ids.iter().map(|id| id.0).collect()))); } if clicked(&Button::new(tl!("Add as Single Graphic")).enabled(data.selected).show(ui)) { diff --git a/crates/ui-egui/src/panel_drag.rs b/crates/ui-egui/src/panel_drag.rs index 7cc1efffd5..5d024e27b5 100644 --- a/crates/ui-egui/src/panel_drag.rs +++ b/crates/ui-egui/src/panel_drag.rs @@ -26,8 +26,8 @@ pub enum PanelDrag { /// (`graphicStyle.new`), or with Alt held replaces the style it is dropped on with that /// object's look (`graphicStyle.redefine`), the Symbols panel a symbol of it all /// (`symbol.new`), the Swatches panel a pattern swatch of a copy (`object.pattern.make`) and - /// the Brushes panel an Art brush (`brush.new`). - Art { document: u64, ids: Vec }, + /// the Brushes panel an Art brush (`brush.new`). `reverts` identifies the contents it came from. + Art { document: u64, reverts: u64, ids: Vec }, /// A Libraries panel graphic (`item`: its id in `library`): the canvas places a copy centred /// where it is dropped (`library.use`); the panel moves it to where it is dropped. LibraryGraphic { library: String, item: String }, @@ -53,6 +53,14 @@ pub struct SwatchRows { } impl PanelDrag { + /// The artwork IDs while the destination still shows the contents the drag came from. + pub(crate) fn artwork_for(&self, uid: u64, reload_count: u64) -> Option<&[vectorcraft_doc::NodeId]> { + match self { + Self::Art { document, reverts, ids } if (*document, *reverts) == (uid, reload_count) => Some(ids), + _ => None, + } + } + /// `paint` dragged from a Fill/Stroke proxy, a Recent Colors chip, a Color Guide color or the /// Gradient panel's thumbnail. A gradient carries no placement: it fits the art it lands on /// (freeform points are placed afresh there, coloured like the dragged ones). diff --git a/crates/ui-egui/src/panels/asset_export.rs b/crates/ui-egui/src/panels/asset_export.rs index 36df059b59..043918e3b5 100644 --- a/crates/ui-egui/src/panels/asset_export.rs +++ b/crates/ui-egui/src/panels/asset_export.rs @@ -509,6 +509,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { return; }; let uid = st.uid; + let reverts = st.reverts; let model = app.ui.asset_export_panel.docs.entry(uid).or_default(); model.selection.items.retain(|id| st.doc.asset(*id).is_some()); let grid_id = ui.scope_id().with(("asset-export-grid", uid)); @@ -553,7 +554,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { footer = Some(bar); }); let zone = footed.show(ui, |ui, _| { - let drop_art = DropAction::::new(move |p| matches!(p, PanelDrag::Art { document, .. } if *document == uid)); + let drop_art = DropAction::::new(move |p| p.artwork_for(uid, reverts).is_some()); drop_art.sizing(Sizing::Fill).show(ui, |ui, _| { if st.doc.assets.is_empty() { Notice::new(Tone::Neutral, tl!("Drag art here, or select it and click +. Hold Alt for one asset of several objects.")) @@ -571,8 +572,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } }) }); - if let Some(PanelDrag::Art { ids, .. }) = zone.inner.and_then(|zone| zone.output.dropped) { - seen.dropped = Some((ids.into_iter().map(|id| id.0).collect(), alt)); + if let Some(payload) = zone.inner.and_then(|zone| zone.output.dropped) + && let Some(ids) = payload.artwork_for(uid, reverts) + { + seen.dropped = Some((ids.iter().map(|id| id.0).collect(), alt)); } for event in events { match event { diff --git a/crates/ui-egui/src/panels/brushes.rs b/crates/ui-egui/src/panels/brushes.rs index 6f670ae2fd..205a18ac05 100644 --- a/crates/ui-egui/src/panels/brushes.rs +++ b/crates/ui-egui/src/panels/brushes.rs @@ -380,12 +380,9 @@ fn takes_art(ty: &str) -> bool { matches!(ty, "scatter" | "art" | "pattern") } -/// The ids of art dragged off the canvas of the document `doc` (its uid). -fn art_of(payload: &PanelDrag, doc: u64) -> Option> { - match payload { - PanelDrag::Art { document, ids, .. } if *document == doc => Some(ids.iter().map(|id| id.0).collect()), - _ => None, - } +/// The ids of art dragged from the displayed drawing. +fn art_of(payload: &PanelDrag, uid: u64, reverts: u64) -> Option> { + payload.artwork_for(uid, reverts).map(|ids| ids.iter().map(|id| id.0).collect()) } /// The library and name of a brush dragged out of a brush library panel; a folder or another kind's item is none. @@ -547,7 +544,7 @@ fn keys_of(sel: &Selection, doc: u64) -> Selection { pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let (list, current) = brushes(app); let doc = app.session.active().map(|st| st.doc.clone()); - let (uid, revision) = app.session.active().map_or((0, 0), |st| (st.uid, st.revision)); + let (uid, revision, reverts) = app.session.active().map_or((0, 0, 0), |st| (st.uid, st.revision, st.reverts)); let memo = app.session.active().map(document_brushes).unwrap_or_default(); let first = by_name(&memo); let multi = picked(app, &list, current.as_deref()); @@ -584,7 +581,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { // the pass that drops and the held ones in a rerun pass, which takes no drop. let art_onto = PresetDrop::new::(DropPlace::Onto, move |payload: &PanelDrag, at: &DropAt| { let DropAt::Onto(key) = at else { return false }; - art_of(payload, uid).is_some() + payload.artwork_for(uid, reverts).is_some() && !key.group && type_of(first, key).is_some_and(takes_art) && craft::ui::runtime::pointer::frame(&ctx).pick_mods.alt @@ -607,7 +604,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let mut footed = None; let shown = Stack::new().label(tl!("Brushes")).fill().footer(|ui, _| footed = Some(footer(app, ui, on))).show(ui, |ui, _| { let library = DropAction::>::new(|drag| library_brush(drag).is_some()).sizing(Sizing::Fill).show(ui, |ui, _| { - let art = DropAction::::new(move |payload| art_of(payload, uid).is_some()).sizing(Sizing::Fill).show(ui, |ui, _| browser(ui)); + let art = DropAction::::new(move |payload| payload.artwork_for(uid, reverts).is_some()) + .sizing(Sizing::Fill) + .show(ui, |ui, _| browser(ui)); (art.output.dropped, art.inner.unwrap_or_default()) }); (library.output.dropped, library.inner.unwrap_or_default()) @@ -642,7 +641,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { PresetEvent::Menu { target, command, .. } => item_command(app, &target, &command, multi.as_ref()), PresetEvent::Reorder(edit) => move_brush(app, &rows(uid, &memo, &hidden), first, &edit), PresetEvent::Dropped { at, payload, .. } => { - if let (DropAt::Onto(key), Some(ids)) = (&at, payload.get::().and_then(|p| art_of(p, uid))) { + if let (DropAt::Onto(key), Some(ids)) = (&at, payload.get::().and_then(|p| art_of(p, uid, reverts))) { crate::dialogs::brush_replace_art::replace(app, &key.name, &ids); } } @@ -650,7 +649,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } } // Art dropped on the list opens New Brush for that art, as the New Brush button opens it for the selection. - if let Some(ids) = art_drop.as_ref().and_then(|p| art_of(p, uid)) + if let Some(ids) = art_drop.as_ref().and_then(|p| art_of(p, uid, reverts)) && let Err(e) = app.run("ui.newBrush", json!({ "ids": ids })) { app.status(e); diff --git a/crates/ui-egui/src/panels/brushes/tests.rs b/crates/ui-egui/src/panels/brushes/tests.rs index 7d74f0834b..501bd051cd 100644 --- a/crates/ui-egui/src/panels/brushes/tests.rs +++ b/crates/ui-egui/src/panels/brushes/tests.rs @@ -258,8 +258,8 @@ fn drops_on_the_panel_run_their_commands() { let star = t.run("shape.star", json!({"cx": 50, "cy": 50, "radius1": 20, "radius2": 9, "points": 5}))["id"].as_u64().unwrap(); // Nothing selected: what New Brush makes comes from the dropped art. t.run("select.none", json!({})); - let document = t.app().session.active().unwrap().uid; - t.drags(PanelDrag::Art { document, ids: vec![NodeId(star)] }); + let (document, reverts) = t.app().session.active().map(|st| (st.uid, st.reverts)).unwrap(); + t.drags(PanelDrag::Art { document, reverts, ids: vec![NodeId(star)] }); let (lib, before) = (t.run("brush.list", json!({})), brushes(&t.app()).0); let at = t.target(on).center(); t.drop_at(at, modifiers); @@ -275,8 +275,8 @@ fn drops_on_the_panel_run_their_commands() { for (name, part) in [("Dots", "art"), ("Arrow", "art"), ("Chain", "side")] { let mut t = Trial::new(); let id = t.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 30, "height": 12}))["id"].as_u64().unwrap(); - let document = t.app().session.active().unwrap().uid; - t.drags(PanelDrag::Art { document, ids: vec![NodeId(id)] }); + let (document, reverts) = t.app().session.active().map(|st| (st.uid, st.reverts)).unwrap(); + t.drags(PanelDrag::Art { document, reverts, ids: vec![NodeId(id)] }); let steps = undo_count(&t.app()); let at = t.target(name).center(); t.drop_at(at, Modifiers::ALT); diff --git a/crates/ui-egui/src/panels/graphic_styles.rs b/crates/ui-egui/src/panels/graphic_styles.rs index cf42d21f00..b220eb2529 100644 --- a/crates/ui-egui/src/panels/graphic_styles.rs +++ b/crates/ui-egui/src/panels/graphic_styles.rs @@ -467,7 +467,7 @@ const AT_START: usize = 2; pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { // The document is shared (an `Arc`): holding it while drawing copies nothing. - let Some((uid, doc)) = app.session.active().map(|d| (d.uid, d.doc.clone())) else { + let Some((uid, reverts, doc)) = app.session.active().map(|d| (d.uid, d.reverts, d.doc.clone())) else { super::empty_state(ui, "dc-graphic-styles", tl!("No document"), tl!("Open a document to see its graphic styles.")); return; }; @@ -484,7 +484,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { // The browser's own state (an open rename field, the scroll position) is kept per document. let browser_id = ui.make_persistent_id(("graphic-styles-browser", uid)); let list = PresetBrowser::::list_id(browser_id); - let takes_object = move |drag: &PanelDrag, _: &DropAt| dropped_object(drag, uid).is_some(); + let takes_object = move |drag: &PanelDrag, _: &DropAt| dropped_object(drag, uid, reverts).is_some(); // A style dragged from this panel's list. let ours = move |drag: &ListDrag| drag.list == list; let svg = |name: &'static str| IconSpec::svg(IconId::new(name)); @@ -610,9 +610,9 @@ 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. -fn dropped_object(drag: &PanelDrag, uid: u64) -> Option { +fn dropped_object(drag: &PanelDrag, uid: u64, reverts: u64) -> Option { match drag { - PanelDrag::Art { document, ids } if *document == uid => ids.first().copied(), + PanelDrag::Art { .. } => drag.artwork_for(uid, reverts)?.first().copied(), PanelDrag::Appearance(id) => Some(*id), _ => None, } @@ -624,7 +624,7 @@ fn dropped(app: &mut VectorcraftApp, at: &DropAt, payload: &DropPayloa DropAt::Onto(name) => Some(name.as_str()), _ => None, }; - let object = app.session.active().and_then(|st| dropped_object(payload.get::()?, st.uid)); + let object = app.session.active().and_then(|st| dropped_object(payload.get::()?, st.uid, st.reverts)); let style = payload.get::>().map(|drag| drag.grabbed.as_str()); drop_on(app, onto, object, style, alt); } @@ -1347,9 +1347,10 @@ mod tests { app.run("paint.setFill", json!({"color": "#00ff00", "ids": [oval.0]})).unwrap(); 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)] { - drop_on(&mut app, Some("Red"), dropped_object(&payload, document), None, true); + let st = app.session.active().unwrap(); + let (document, reverts) = (st.uid, st.reverts); + for payload in [PanelDrag::Art { document, reverts, ids: vec![oval] }, PanelDrag::Appearance(oval)] { + drop_on(&mut app, Some("Red"), dropped_object(&payload, document, reverts), None, true); let d = doc(&app); assert_eq!(d.graphic_style("Red").unwrap().appearance.fill_paint(), green); assert_eq!(d.node(oval).unwrap().extras().graphic_style, d.graphic_style("Red").map(|g| g.id)); @@ -1375,7 +1376,7 @@ mod tests { fn with_artwork(app: &mut VectorcraftApp, ui: &mut Ui) { let st = app.session.active().unwrap(); - let payload = PanelDrag::Art { document: st.uid, ids: st.selection.objects.clone() }; + let payload = PanelDrag::Art { document: st.uid, reverts: st.reverts, 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") diff --git a/crates/ui-egui/src/panels/libraries.rs b/crates/ui-egui/src/panels/libraries.rs index 5d1955810a..8b44ba7d83 100644 --- a/crates/ui-egui/src/panels/libraries.rs +++ b/crates/ui-egui/src/panels/libraries.rs @@ -190,7 +190,7 @@ fn swatch_add(drag: &PanelDrag, library: &str) -> Option<(ItemKind, Value)> { /// which keep their test, can read it: what a drop into a group, or out of every group, does. struct Membership { library: String, - active_document: Option, + active_document: Option<(u64, u64)>, /// The group of each grouped item, by its index in the library's groups. index: HashMap<(ItemKind, String), usize>, /// The groups' names, in the library's order. @@ -198,7 +198,7 @@ struct Membership { } impl Membership { - fn of(library: &str, lib: &Library, active_document: Option) -> Self { + fn of(library: &str, lib: &Library, active_document: Option<(u64, u64)>) -> Self { let index = lib.group_index().into_iter().map(|((kind, item), group)| ((kind, item.to_string()), group)).collect(); Membership { library: library.to_string(), active_document, index, names: lib.groups.iter().map(|g| g.name.clone()).collect() } } @@ -210,8 +210,9 @@ impl Membership { PanelDrag::Paint { library: Some((l, item)), .. } if l == library => Some(Grab::Item(ItemKind::Color, item.clone())), PanelDrag::LibraryGraphic { library: l, item } if l == library => Some(Grab::Item(ItemKind::Graphic, item.clone())), PanelDrag::LibraryItem { library: l, kind, item } if l == library => Some(Grab::Item(*kind, item.clone())), - PanelDrag::Art { document, ids } if Some(*document) == self.active_document => { - let ids: Vec = ids.iter().map(|id| id.0).collect(); + PanelDrag::Art { .. } => { + let (uid, reverts) = self.active_document?; + let ids: Vec = drag.artwork_for(uid, reverts)?.iter().map(|id| id.0).collect(); Some(Grab::New(ItemKind::Graphic, json!({ "kind": "graphic", "ids": ids }))) } PanelDrag::Paint { .. } => swatch_add(drag, library).map(|(kind, add)| Grab::New(kind, add)), @@ -305,7 +306,15 @@ struct View<'a> { } impl<'a> View<'a> { - fn new(library: &'a str, lib: &'a Library, query: &str, list: bool, fill_active: bool, active_document: Option, can_apply: bool) -> Self { + fn new( + library: &'a str, + lib: &'a Library, + query: &str, + list: bool, + fill_active: bool, + active_document: Option<(u64, u64)>, + can_apply: bool, + ) -> Self { let membership = Membership::of(library, lib, active_document); let query = query.trim().to_lowercase(); let searching = !query.is_empty(); @@ -776,7 +785,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { return; }; let acts = RefCell::new(Vec::new()); - let active_document = app.session.active().map(|st| st.uid); + let active_document = app.session.active().map(|st| (st.uid, st.reverts)); let has_doc = active_document.is_some(); { // PLAN: assumed `UiState::{libraries_list_view: bool, library_search: String, library_groups_closed: HashSet<(String, String)>}` (ap.AS17, hub-w1). diff --git a/crates/ui-egui/src/panels/libraries/tests.rs b/crates/ui-egui/src/panels/libraries/tests.rs index 305e8eadbf..a4a8c21201 100644 --- a/crates/ui-egui/src/panels/libraries/tests.rs +++ b/crates/ui-egui/src/panels/libraries/tests.rs @@ -71,7 +71,8 @@ fn art(app: &mut VectorcraftApp, x: f64, name: Option<&str>) -> Vec PanelDrag { let ids = art(app, x, None); - PanelDrag::Art { document: app.session.active().unwrap().uid, ids } + let state = app.session.active().unwrap(); + PanelDrag::Art { document: state.uid, reverts: state.reverts, ids } } fn graphics(app: &mut VectorcraftApp) -> Vec { diff --git a/crates/ui-egui/src/panels/swatches.rs b/crates/ui-egui/src/panels/swatches.rs index efd5ed9533..cb3ab044d8 100644 --- a/crates/ui-egui/src/panels/swatches.rs +++ b/crates/ui-egui/src/panels/swatches.rs @@ -408,6 +408,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) { }; let dragged_doc = Arc::clone(&thumbs.doc); let document = st.uid; + let reverts = st.reverts; let shown_app: &VectorcraftApp = app; 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)))) @@ -417,8 +418,8 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui) { _ => vec![], }) .row_items(|key| row_items(shown_app, key)) - .takes_drop::(DropPlace::Between, |drag, _| dnd::accepts(drag, document)) - .takes_drop::(DropPlace::Onto, |drag, _| dnd::accepts(drag, document)) + .takes_drop::(DropPlace::Between, |drag, _| dnd::accepts(drag, document, reverts)) + .takes_drop::(DropPlace::Onto, |drag, _| dnd::accepts(drag, document, reverts)) .select(SelectMode::Multi) .search(model.show_find) .reveal(focus.map(SwatchKey::Swatch)) diff --git a/crates/ui-egui/src/panels/swatches/dnd.rs b/crates/ui-egui/src/panels/swatches/dnd.rs index 9b492e06f5..6ff3420571 100644 --- a/crates/ui-egui/src/panels/swatches/dnd.rs +++ b/crates/ui-egui/src/panels/swatches/dnd.rs @@ -5,12 +5,11 @@ use craft::keymap::Mods; use craft::ui::contract::{DropAt, ReorderMove}; /// Swatches take paints, and take swatch rows or canvas art from the current document. -pub(super) fn accepts(drag: &PanelDrag, document: u64) -> bool { +pub(super) fn accepts(drag: &PanelDrag, document: u64, reverts: u64) -> bool { match drag { PanelDrag::Paint { rows: Some(rows), .. } => rows.document == document, PanelDrag::Paint { .. } => true, - PanelDrag::Art { document: source, .. } => *source == document, - _ => false, + _ => drag.artwork_for(document, reverts).is_some(), } } @@ -45,7 +44,6 @@ impl Drop { Some(Drop::Move { names: r.names.clone(), target, after, with }) } PanelDrag::Paint { paint, .. } => Some(Drop::New { paint: paint.clone(), target, replace: mods.alt, spot: mods.cmd }), - // PLAN: assumed `Art { document: u64, ids: Vec }` (widgets-w13). PanelDrag::Art { ids, .. } => Some(Drop::Pattern { ids: ids.iter().map(|id| id.0).collect(), target, replace: mods.alt }), _ => None, } @@ -88,7 +86,7 @@ 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)) { + if !app.session.active().is_some_and(|st| accepts(drag, st.uid, st.reverts)) { return; } let (target, after) = place(shown, at); diff --git a/crates/ui-egui/src/panels/swatches/tests.rs b/crates/ui-egui/src/panels/swatches/tests.rs index 596914f8a3..540eb2c458 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -163,7 +163,8 @@ fn a_foreign_swatch_row_payload_cannot_move_or_replace_local_swatches() { 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)); + let state = app.session.active().unwrap(); + assert!(!dnd::accepts(&drag, state.uid, state.reverts)); dropped_below(&mut app, drag.clone(), none()); dropped(&mut app, drag, "Green", alt()); assert_eq!(doc(&app).swatches, before.swatches); @@ -171,7 +172,8 @@ fn a_foreign_swatch_row_payload_cannot_move_or_replace_local_swatches() { /// Art `ids` dragged off the canvas. fn art_drag(app: &VectorcraftApp, ids: Vec) -> PanelDrag { - PanelDrag::Art { document: app.session.active().map(|st| st.uid).unwrap_or_default(), ids } + let (document, reverts) = app.session.active().map_or((0, 0), |st| (st.uid, st.reverts)); + PanelDrag::Art { document, reverts, ids } } /// The browser's reorder committing `names` before row `before` (a colour group: into it). diff --git a/crates/ui-egui/src/panels/symbols.rs b/crates/ui-egui/src/panels/symbols.rs index 321a0a7c6d..a3ed7d9528 100644 --- a/crates/ui-egui/src/panels/symbols.rs +++ b/crates/ui-egui/src/panels/symbols.rs @@ -530,7 +530,9 @@ const AT_START: usize = 4; /// list becomes a symbol (Shift at the release keeps the art as ordinary art); dropped on a symbol /// with Alt it redefines that symbol in one undo step. pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { - let Some((doc, revision, uid, has_sel)) = app.session.active().map(|st| (st.doc.clone(), st.revision, st.uid, !st.selection.is_empty())) else { + let Some((doc, revision, uid, reverts, has_sel)) = + app.session.active().map(|st| (st.doc.clone(), st.revision, st.uid, st.reverts, !st.selection.is_empty())) + else { return; }; let keys = document_symbols(&doc, revision); @@ -575,7 +577,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { .show(ui, |ui, _| { // Art dropped anywhere on the list becomes a symbol; dropped on a symbol, the browser's // own target takes it. - DropAction::::new(move |drag| art_of(drag, uid).is_some()).show(ui, |ui, _| { + DropAction::::new(move |drag| art_of(drag, uid, reverts).is_some()).show(ui, |ui, _| { let mut state = PresetState::new(); state.zoom = zoom; PresetBrowser::new(tl!("Symbols"), Bind::bound(&mut state), &source) @@ -587,7 +589,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { .row_menu(PresetRowMenu::Host) .row_items(|key: &SymbolKey| item_rows(&linked, &key.name)) .takes_drop(PresetDrop::new::(DropPlace::Onto, move |drag: &PanelDrag, _: &DropAt| { - art_of(drag, uid).is_some() + art_of(drag, uid, reverts).is_some() })) .empty(tl!("No symbols in this document")) .show(ui) @@ -605,7 +607,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } if let Some((dropped, events)) = taken { if let Some((drag, mods)) = dropped - && let Some(ids) = art_of(&drag, uid) + && let Some(ids) = art_of(&drag, uid, reverts) { new_symbol(app, ids, mods.shift); } @@ -630,7 +632,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } } PresetEvent::Dropped { at, payload, mods, .. } => { - let Some(ids) = payload.get::().and_then(|drag| art_of(drag, uid)) else { continue }; + let Some(ids) = payload.get::().and_then(|drag| art_of(drag, uid, reverts)) else { continue }; if !mods.alt { new_symbol(app, ids, mods.shift); } else if let Some(target) = at.key() @@ -683,13 +685,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } } -/// The ids of art dragged off the canvas of document `uid`; `None` for another drag or art of -/// another document. -fn art_of(drag: &PanelDrag, uid: u64) -> Option<&[NodeId]> { - match drag { - PanelDrag::Art { document, ids, .. } if *document == uid && !ids.is_empty() => Some(ids), - _ => None, - } +/// The ids of the nonempty artwork dragged from the displayed drawing. +fn art_of(drag: &PanelDrag, uid: u64, reverts: u64) -> Option<&[NodeId]> { + drag.artwork_for(uid, reverts).filter(|ids| !ids.is_empty()) } /// Whether the art `ids` can redefine a symbol of `doc`: every object is still there and none is a diff --git a/crates/ui-egui/src/panels/symbols/tests.rs b/crates/ui-egui/src/panels/symbols/tests.rs index 3f52c73fc8..a4223322b0 100644 --- a/crates/ui-egui/src/panels/symbols/tests.rs +++ b/crates/ui-egui/src/panels/symbols/tests.rs @@ -92,7 +92,7 @@ fn panel(app: VectorcraftApp) -> Panel { fn art_and_symbols(app: &mut VectorcraftApp, ui: &mut Ui) { use craft::ui::contract::{ListDrag, ShowCollection as _}; if let Some(st) = app.session.active() { - let document = st.uid; + let (document, reverts) = (st.uid, st.reverts); let mut named = Vec::new(); st.doc.walk(|n| { if let Some(name) = &n.name { @@ -104,7 +104,7 @@ fn art_and_symbols(app: &mut VectorcraftApp, ui: &mut Ui) { let ids: Vec = named.iter().map(|(_, id)| *id).collect(); ui.allocate_ui(vec2(ui.available_width(), 200.0), |ui| { let _ = craft::ui::list::List::::new("Art off the canvas") - .drag_payload(move |drag: &ListDrag| ids.get(drag.grabbed).map(|id| PanelDrag::Art { document, ids: vec![*id] })) + .drag_payload(move |drag: &ListDrag| ids.get(drag.grabbed).map(|id| PanelDrag::Art { document, reverts, ids: vec![*id] })) .show_items(ui, &rows, &mut |key, _| craft::ui::list::ListRow::new(named[*key].0.clone())); }); } diff --git a/crates/ui-egui/src/panels/tests_asset_export.rs b/crates/ui-egui/src/panels/tests_asset_export.rs index e14e6c0ea3..369006b699 100644 --- a/crates/ui-egui/src/panels/tests_asset_export.rs +++ b/crates/ui-egui/src/panels/tests_asset_export.rs @@ -62,7 +62,7 @@ fn art_and_assets(app: &mut VectorcraftApp, ui: &mut egui::Ui) { use craft::ui::list::{List, ListRow}; let st = app.session.active().unwrap(); - let payload = crate::panel_drag::PanelDrag::Art { document: st.uid, ids: st.selection.objects.clone() }; + let payload = crate::panel_drag::PanelDrag::Art { document: st.uid, reverts: st.reverts, 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") diff --git a/crates/ui-egui/src/panels/tests_three_d.rs b/crates/ui-egui/src/panels/tests_three_d.rs index cc1c1593da..03bed26984 100644 --- a/crates/ui-egui/src/panels/tests_three_d.rs +++ b/crates/ui-egui/src/panels/tests_three_d.rs @@ -696,7 +696,7 @@ fn three_d_graphics_accepts_artwork_only_from_its_document() { 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() }; + let payload = PanelDrag::Art { document: st.uid, reverts: st.reverts, 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") From b9267890871820d527f7ffd1a148593fdb55acc2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 21:45:43 -0800 Subject: [PATCH 2/2] Reject Graphics drops from a reverted 3D drawing --- crates/ui-egui/src/app.rs | 1 + crates/ui-egui/src/dialogs/three_d.rs | 9 ++- .../ui-egui/src/dialogs/three_d_materials.rs | 79 ++++++++++++++++++- 3 files changed, 85 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index c5a97a940a..f5b6f6a216 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -387,6 +387,7 @@ impl VectorcraftApp { } font_menu::end_previews(self, &out.ended_previews); edits::end_removed(self, &out.removed_gestures); + dialogs::three_d::sync_graphics_source(self); let lang = self.ui_language(); if self.host.lang() != lang { self.host = Arc::new(self.host.with_lang(lang)); diff --git a/crates/ui-egui/src/dialogs/three_d.rs b/crates/ui-egui/src/dialogs/three_d.rs index f8fa3b6577..2d3497ebe5 100644 --- a/crates/ui-egui/src/dialogs/three_d.rs +++ b/crates/ui-egui/src/dialogs/three_d.rs @@ -63,7 +63,7 @@ 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. + /// The opening document generation while it can still supply artwork to the Graphics drop target. pub document: Option<(u64, u64)>, /// The active document's version. pub revision: u64, @@ -352,6 +352,13 @@ fn surfaces(app: &mut VectorcraftApp, s: &Session, fields: &DialogFields, params Some(listed) } +/// Refresh drop eligibility before the dialog can receive a release in this frame. +pub(crate) fn sync_graphics_source(app: &VectorcraftApp) { + let Some(open) = &app.three_d_dialog else { return }; + let source = open.source.filter(|&(uid, reverts)| app.session.documents().iter().any(|d| (d.uid, d.reverts) == (uid, reverts))); + open.data.write().unwrap_or_else(PoisonError::into_inner).document = source; +} + /// Once per frame: refreshes the rows' data, runs the preview, and, with Preview off, tries the values so the dialog /// still says why OK would fail. `refuse` is called only from a result this pump produced: an idle pump leaves a /// refusal standing until the values change. diff --git a/crates/ui-egui/src/dialogs/three_d_materials.rs b/crates/ui-egui/src/dialogs/three_d_materials.rs index b31fdc67bf..a55055fe4d 100644 --- a/crates/ui-egui/src/dialogs/three_d_materials.rs +++ b/crates/ui-egui/src/dialogs/three_d_materials.rs @@ -174,11 +174,12 @@ fn body(ui: &mut Ui, fields: &mut DialogFields, cx: &mut CustomContext, data: &D fn library(ui: &mut Ui, data: &Data, apply: bool) -> Option { Text::new(tl!("Graphics")).show(ui); let mut action = None; - let (Some(doc), Some((uid, reverts))) = (data.doc.clone(), data.document) else { return action }; + let Some(doc) = data.doc.clone() else { return action }; + let source_drawing = data.document; 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 drop_art = DropAction::::new(move |drag| drag.artwork_for(uid, reverts).is_some()); + let drop_art = DropAction::::new(move |drag| source_drawing.is_some_and(|(uid, reverts)| drag.artwork_for(uid, reverts).is_some())); 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.")) }; @@ -199,7 +200,9 @@ fn library(ui: &mut Ui, data: &Data, apply: bool) -> Option { action = Some(Action::Map(name)); } } - if let Some(ids) = dropped.output.dropped.as_ref().and_then(|drag| drag.artwork_for(uid, reverts)) { + if let Some((uid, reverts)) = source_drawing + && let Some(ids) = dropped.output.dropped.as_ref().and_then(|drag| drag.artwork_for(uid, reverts)) + { action = Some(Action::Add(Some(ids.iter().map(|id| id.0).collect()))); } if clicked(&Button::new(tl!("Add as Single Graphic")).enabled(data.selected).show(ui)) { @@ -207,3 +210,73 @@ fn library(ui: &mut Ui, data: &Data, apply: bool) -> Option { } action } + +#[cfg(test)] +mod tests { + use super::*; + use crate::VectorcraftApp; + use crate::tests::panel_harness::Panel; + use craft::ui::contract::{ListDrag, Role}; + use craft::ui::list::{List, ListRow}; + use craft::ui::runtime::drag; + + fn draw_graphics(app: &mut VectorcraftApp, ui: &mut Ui) { + let st = app.session.active().unwrap(); + let payload = PanelDrag::Art { document: st.uid, reverts: st.reverts, ids: st.selection.objects.clone() }; + ui.allocate_ui(egui::vec2(ui.available_width(), 80.0), |ui| { + let keys = 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")); + }); + if let Some(open) = &app.three_d_dialog { + let data = open.data.read().unwrap(); + let _ = library(ui, &data, true); + } + } + + #[test] + fn held_graphics_drag_is_refused_after_the_opening_drawing_reverts() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + let dir = vectorcraft_testkit::TempDir::new("three-d-graphics-revert"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + let shape = app.run("shape.rectangle", json!({"x": 20, "y": 20, "width": 50, "height": 50})).unwrap()["id"].clone(); + app.run("threeD.addGraphic", json!({"name": "Opening Graphic"})).unwrap(); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + app.run("shape.rectangle", json!({"x": 120, "y": 90, "width": 45, "height": 40})).unwrap(); + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + assert!(app.session.set_active(0)); + app.run("select.set", json!({"ids": [shape]})).unwrap(); + + let mut panel = Panel::new(app, draw_graphics); + panel.room(egui::vec2(800.0, 820.0)); + super::super::three_d::open(&mut panel.app(), &json!({"effect": INFLATE})).unwrap(); + panel.frame(); + let from = panel.rect("Dragged artwork").center(); + let to = panel.rect("Opening Graphic").center(); + panel.drag_hold_role("Dragged artwork", Role::LIST_ITEM, to - from); + assert!(drag::mark(panel.ctx()).is_some(), "the opening drawing's drag is accepted"); + assert!(panel.app().session.set_active(1)); + panel.frame(); + assert!(drag::mark(panel.ctx()).is_some(), "switching drawings retains the opening drawing's drag"); + + { + let mut app = panel.app(); + assert!(app.session.set_active(0)); + let source = app.session.active().unwrap(); + let (uid, generation) = (source.uid, source.reverts); + app.run("file.revert", json!({"confirmed": true})).unwrap(); + let restored = app.session.active().unwrap(); + assert_eq!(restored.uid, uid); + assert_ne!(restored.reverts, generation); + assert!(app.session.set_active(1)); + } + panel.frame(); + assert!(drag::active::(panel.ctx()).is_some(), "the same drag is still in progress"); + assert!(drag::mark(panel.ctx()).is_none(), "the drop stops accepting artwork from the reverted generation"); + let _ = panel.rect("Opening Graphic"); + panel.release(); + } +}