diff --git a/crates/ui-egui/src/panels/layers/cells.rs b/crates/ui-egui/src/panels/layers/cells.rs index 4e3316fe84..ce0de11247 100644 --- a/crates/ui-egui/src/panels/layers/cells.rs +++ b/crates/ui-egui/src/panels/layers/cells.rs @@ -34,9 +34,10 @@ const SQUARE_SIDE: f32 = 7.0; const HELD_SIDE: f32 = 4.0; /// The selected art a row's selection square carries to another row in the same document. +/// `document` is the document's uid and revert count: File › Revert keeps the uid but replaces the drawing. #[derive(Clone, Debug)] pub(super) struct ArtDrag { - pub(super) document: u64, + pub(super) document: (u64, u64), pub(super) ids: Vec, } @@ -208,7 +209,7 @@ impl Component for TargetCircle { pub(super) struct SelectionSquare { #[common] common: Common, - document: u64, + document: (u64, u64), id: Option, selected: bool, holds: bool, @@ -219,13 +220,13 @@ pub(super) struct SelectionSquare { } impl SelectionSquare { - fn new(document: u64, id: Option, selected: bool, holds: bool, color: DataColor, art: Vec) -> Self { + fn new(document: (u64, u64), id: Option, selected: bool, holds: bool, color: DataColor, art: Vec) -> Self { Self { common: Common::new(), document, id, selected, holds, color, art, pick: None } } /// A selected row's square, for the suite. fn sample() -> Self { - Self::new(1, Some(NodeId(1)), true, false, DataColor::new(Color32::from_rgb(79, 128, 255)), vec![NodeId(1)]).label("Select art") + Self::new((1, 0), Some(NodeId(1)), true, false, DataColor::new(Color32::from_rgb(79, 128, 255)), vec![NodeId(1)]).label("Select art") } } @@ -370,22 +371,23 @@ pub(super) fn square_marks(tree: &DocLayers<'_>, n: &Node) -> (bool, bool) { /// The cells of row `row` (shown as `info`) of the panel over `tree`. pub(super) fn build(tree: &DocLayers<'_>, row: &RowId, info: &LayerInfo) -> Cells { let st = tree.state(); + let document = (st.uid, st.reverts); let color = info.color.unwrap_or_else(|| DataColor::new(Color32::TRANSPARENT)); let selected = |id: NodeId| tree.is_selected(id); match *row { RowId::Node(id) => { - let Some(n) = tree.doc().node(id) else { return Cells::new(None, SelectionSquare::new(st.uid, None, false, false, color, vec![])) }; + let Some(n) = tree.doc().node(id) else { return Cells::new(None, SelectionSquare::new(document, None, false, false, color, vec![])) }; 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((st.uid, st.reverts), 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(st.uid, Some(id), full, holds, color, art) + let square = SelectionSquare::new(document, Some(id), full, holds, color, art) .label(crate::i18n::fmt(tl!("Select art of {row}"), &[("row", info.name.as_str())])); Cells::new(Some(circle), square) } - RowId::Guide(i) => Cells::new(None, SelectionSquare::new(st.uid, None, tree.guides().contains(&i), false, color, vec![])), - RowId::Spine(blend) => Cells::new(None, SelectionSquare::new(st.uid, None, selected(blend), false, color, vec![])), + RowId::Guide(i) => Cells::new(None, SelectionSquare::new(document, None, tree.guides().contains(&i), false, color, vec![])), + RowId::Spine(blend) => Cells::new(None, SelectionSquare::new(document, None, selected(blend), false, color, vec![])), } } @@ -415,6 +417,8 @@ pub(super) fn output( /// layer or group that takes the objects ([`DocLayers::lands_in`]). pub(super) fn accepts_art<'a>(tree: &'a DocLayers<'a>) -> impl Fn(&ArtDrag, &RowId, LayerPosition) -> bool + 'a { move |art: &ArtDrag, row: &RowId, position: LayerPosition| { - art.document == tree.state().uid && position == LayerPosition::Into && matches!(*row, RowId::Node(id) if tree.lands_in(id, &art.ids, true)) + art.document == (tree.state().uid, tree.state().reverts) + && position == LayerPosition::Into + && matches!(*row, RowId::Node(id) if tree.lands_in(id, &art.ids, true)) } } diff --git a/crates/ui-egui/src/panels/layers/rows.rs b/crates/ui-egui/src/panels/layers/rows.rs index 523e9653ed..f1a84af022 100644 --- a/crates/ui-egui/src/panels/layers/rows.rs +++ b/crates/ui-egui/src/panels/layers/rows.rs @@ -463,7 +463,7 @@ fn drop_command(tree: &DocLayers<'_>, mv: &LayerMove) -> Option<(String, /// The selected art carried by a row's selection square, dropped on `target`: the selection's top /// objects, as Arrange and Group take them, move there (Alt copies them). fn art_drop(st: &DocState, payload: &DropPayload, target: NodeId, position: LayerPosition, copy: bool) -> Option<(String, Value)> { - let art = payload.get::().filter(|art| art.document == st.uid)?; + let art = payload.get::().filter(|art| art.document == (st.uid, st.reverts))?; let ids: Vec = st.doc.paint_order(art.ids.iter().copied()).iter().map(|i| i.0).collect(); Some(("layer.move".into(), json!({"ids": ids, "target": target.0, "place": place(position), "copy": copy}))) } diff --git a/crates/ui-egui/src/panels/layers/tests.rs b/crates/ui-egui/src/panels/layers/tests.rs index acce526a3b..6ad6d1217c 100644 --- a/crates/ui-egui/src/panels/layers/tests.rs +++ b/crates/ui-egui/src/panels/layers/tests.rs @@ -590,7 +590,8 @@ fn a_selection_square_drop_after_next_document_leaves_the_new_artwork_in_place() let id = app.run("shape.rectangle", json!({"x": x, "y": 20, "width": 40, "height": 30})).unwrap()["id"].as_u64().unwrap(); let dst = app.run("layer.new", json!({"name": "Destination"})).unwrap()["id"].as_u64().unwrap(); app.run("select.set", json!({"ids": [id]})).unwrap(); - (app.session.active().unwrap().uid, NodeId(id), source, NodeId(dst)) + let st = app.session.active().unwrap(); + ((st.uid, st.reverts), NodeId(id), source, NodeId(dst)) }; let (first_doc, first, _, _) = setup(10); let (second_doc, second, source, destination) = setup(60); @@ -603,7 +604,11 @@ fn a_selection_square_drop_after_next_document_leaves_the_new_artwork_in_place() panel.drag_hold_role("Select art of Source", Role::BUTTON, to - from); assert!(drag::mark(panel.ctx()).is_some(), "the destination accepts artwork in its own document"); panel.key(Key::F6, Modifiers::COMMAND); - assert_eq!(panel.app().session.active().unwrap().uid, second_doc); + { + let app = panel.app(); + let st = app.session.active().unwrap(); + assert_eq!((st.uid, st.reverts), second_doc); + } let carried = drag::active::(panel.ctx()).expect("the shortcut leaves the drag held"); assert_eq!((carried.document, carried.ids.as_slice()), (first_doc, &[first][..])); assert!(drag::mark(panel.ctx()).is_none(), "the destination rejects the foreign artwork"); @@ -615,6 +620,57 @@ fn a_selection_square_drop_after_next_document_leaves_the_new_artwork_in_place() assert_eq!(layer(&panel.app()), Some(destination), "a same-document selection-square drag still moves the rectangle"); } +#[test] +fn a_selection_square_drop_after_file_revert_leaves_the_restored_artwork_in_place() { + use crate::tests::panel_harness::Panel; + use craft::ui::contract::Role; + use craft::ui::runtime::drag; + use egui::Modifiers; + + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + let source = app.session.active().unwrap().doc.layers[0].id; + name_row(&mut app, source, "Source"); + let rect = NodeId(app.run("shape.rectangle", json!({"x": 10, "y": 20, "width": 40, "height": 30})).unwrap()["id"].as_u64().unwrap()); + let destination = NodeId(app.run("layer.new", json!({"name": "Destination"})).unwrap()["id"].as_u64().unwrap()); + let folder = vectorcraft_testkit::TempDir::new("layers-art-drag-revert"); + let path = folder.join("drawing.vectorcraft"); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|err| err.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|err| err.to_string()))); + app.run("file.saveAs", json!({"path": path.to_string_lossy()})).unwrap(); + app.run("shape.rectangle", json!({"x": 120, "y": 30, "width": 30, "height": 30})).unwrap(); + app.run("select.set", json!({"ids": [rect.0]})).unwrap(); + + let mut panel = Panel::new(app, show); + let from = panel.rect_role("Select art of Source", Role::BUTTON).center(); + let to = panel.rect("Destination").center(); + panel.drag_hold_role("Select art of Source", Role::BUTTON, to - from); + assert!(drag::mark(panel.ctx()).is_some(), "the destination accepts the selected artwork before the revert"); + let opening = { + let app = panel.app(); + let st = app.session.active().unwrap(); + (st.uid, st.reverts) + }; + + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + panel.frame(); + { + let app = panel.app(); + let st = app.session.active().unwrap(); + assert_eq!((st.uid, st.reverts), (opening.0, opening.1 + 1)); + } + assert!(drag::active::(panel.ctx()).is_some(), "the revert leaves the drag held"); + assert!(drag::mark(panel.ctx()).is_none(), "the destination rejects artwork carried from before the revert"); + panel.release(); + let layer = |app: &VectorcraftApp| app.session.active().unwrap().doc.layer_of(rect); + assert_eq!(layer(&panel.app()), Some(source), "the restored rectangle with the same ID has not moved"); + + panel.app().run("select.set", json!({"ids": [rect.0]})).unwrap(); + panel.frame(); + panel.drag("Select art of Source", "Destination", Modifiers::NONE); + assert_eq!(layer(&panel.app()), Some(destination), "a selection-square drag started after the revert still moves the rectangle"); +} + /// The target and the selected objects, sorted. fn targeted(app: &VectorcraftApp) -> (Option, Vec) { let st = app.session.active().unwrap();