Keep Layers selection-square drags in their source document

This commit is contained in:
dotCooCoo committed 2026-11-26 09:01:24 -08:00
1 parent f040538a39
commit 4b5d4464f2
3 files changed
+57 -14

No files matched your search

+15 -12
View File
@@ -33,10 +33,12 @@ const SQUARE_SIDE: f32 = 7.0;
/// The side of the square of a row that holds selected art.
const HELD_SIDE: f32 = 4.0;
/// The selected art a row's selection square carries to another row: the selected objects when the
/// drag started. Only the Layers rows take it.
/// The selected art a row's selection square carries to another row in the same document.
#[derive(Clone, Debug)]
pub(super) struct ArtDrag(pub(super) Vec<NodeId>);
pub(super) struct ArtDrag {
pub(super) document: u64,
pub(super) ids: Vec<NodeId>,
}
/// What a cell reports.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -204,6 +206,7 @@ impl Component for TargetCircle {
pub(super) struct SelectionSquare {
#[common]
common: Common,
document: u64,
id: Option<NodeId>,
selected: bool,
holds: bool,
@@ -214,13 +217,13 @@ pub(super) struct SelectionSquare {
}
impl SelectionSquare {
fn new(id: Option<NodeId>, selected: bool, holds: bool, color: DataColor, art: Vec<NodeId>) -> Self {
Self { common: Common::new(), id, selected, holds, color, art, pick: None }
fn new(document: u64, id: Option<NodeId>, selected: bool, holds: bool, color: DataColor, art: Vec<NodeId>) -> 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(Some(NodeId(1)), true, false, DataColor::new(Color32::from_rgb(79, 128, 255)), vec![NodeId(1)]).label("Select art")
Self::new(1, Some(NodeId(1)), true, false, DataColor::new(Color32::from_rgb(79, 128, 255)), vec![NodeId(1)]).label("Select art")
}
}
@@ -253,7 +256,7 @@ impl Component for SelectionSquare {
self.pick = Some(CellPick::SelectArt(activation.mods));
}
if (self.selected || self.holds) && !self.art.is_empty() && primary_drag_start(cx) {
cx.start_drag(ArtDrag(self.art.clone()), DragPreview::None);
cx.start_drag(ArtDrag { document: self.document, ids: self.art.clone() }, DragPreview::None);
}
}
@@ -369,18 +372,18 @@ pub(super) fn build(tree: &DocLayers<'_>, row: &RowId, info: &LayerInfo) -> Cell
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(None, false, false, color, vec![])) };
let Some(n) = tree.doc().node(id) else { return Cells::new(None, SelectionSquare::new(st.uid, 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(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)
let square = SelectionSquare::new(st.uid, 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(None, tree.guides().contains(&i), false, color, vec![])),
RowId::Spine(blend) => Cells::new(None, SelectionSquare::new(None, selected(blend), false, color, vec![])),
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![])),
}
}
@@ -410,6 +413,6 @@ 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| {
position == LayerPosition::Into && matches!(*row, RowId::Node(id) if tree.lands_in(id, &art.0, true))
art.document == tree.state().uid && position == LayerPosition::Into && matches!(*row, RowId::Node(id) if tree.lands_in(id, &art.ids, true))
}
}
+2 -2
View File
@@ -463,8 +463,8 @@ fn drop_command(tree: &DocLayers<'_>, mv: &LayerMove<RowId>) -> 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::<ArtDrag>()?;
let ids: Vec<u64> = st.doc.paint_order(art.0.iter().copied()).iter().map(|i| i.0).collect();
let art = payload.get::<ArtDrag>().filter(|art| art.document == st.uid)?;
let ids: Vec<u64> = 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})))
}
+40
View File
@@ -524,6 +524,46 @@ fn name_row(app: &mut VectorcraftApp, id: NodeId, name: &str) {
app.session.execute("layer.setProps", &json!({"ids": [id.0], "name": name})).unwrap();
}
#[test]
fn a_selection_square_drop_after_next_document_leaves_the_new_artwork_in_place() {
use crate::tests::panel_harness::Panel;
use craft::ui::contract::Role;
use craft::ui::runtime::drag;
use egui::{Key, Modifiers};
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let mut setup = |x| {
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 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 (first_doc, first, _, _) = setup(10);
let (second_doc, second, source, destination) = setup(60);
assert_eq!(first, second, "the documents have different rectangles with the same local ID");
assert!(app.session.set_active(0));
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 artwork in its own document");
panel.key(Key::F6, Modifiers::COMMAND);
assert_eq!(panel.app().session.active().unwrap().uid, second_doc);
let carried = drag::active::<cells::ArtDrag>(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");
panel.release();
let layer = |app: &VectorcraftApp| app.session.active().unwrap().doc.layer_of(second);
assert_eq!(layer(&panel.app()), Some(source), "the same-ID rectangle in this document has not moved");
panel.drag("Select art of Source", "Destination", Modifiers::NONE);
assert_eq!(layer(&panel.app()), Some(destination), "a same-document selection-square drag still moves the rectangle");
}
/// The target and the selected objects, sorted.
fn targeted(app: &VectorcraftApp) -> (Option<NodeId>, Vec<NodeId>) {
let st = app.session.active().unwrap();