Move: frame and snap a dragged group (#13418) (#13429)

* Move: frame and snap a dragged group (#13418)

A group dragged with the Move tool showed only the arrow: the moving box
came from snap::layer_rect, which skips groups, so a selected group got no
Move gesture, no box, no snapping and no Smart Guides.

Measure what the move carries instead: the move targets, a group as the
layers in it (whole, even with a selection), an artboard as its board.
Keep the targets and everything inside them out of the snap targets, so a
group doesn't snap to its own layers' starting edges.

* Rename the group drag test helper so it doesn't clash with drag()

* Move: keep linked layers as snap targets while selected pixels move
This commit is contained in:
Khawar Zamman Wani authored and GitHub committed 2026-11-12 17:58:41 +01:00
1 parent dbde10cfb1
commit f88fde3db0
4 files changed
+135 -15

No files matched your search

+1 -1
View File
@@ -298,7 +298,7 @@ fn shape_reach(l: &photocraft_doc::Layer, canvas: Rect) -> Option<Rect> {
}
/// `ids` and every layer inside them (groups move their contents).
fn with_descendants(doc: &Document, ids: &[LayerId]) -> Vec<LayerId> {
pub(crate) fn with_descendants(doc: &Document, ids: &[LayerId]) -> Vec<LayerId> {
let walk = doc.walk();
let roots: Vec<_> = walk.iter().filter(|(_, _, l)| ids.contains(&l.id)).map(|(p, _, _)| p.as_slice()).collect();
walk.iter().filter(|(p, _, _)| roots.iter().any(|r| p.starts_with(r))).map(|(_, _, l)| l.id).collect()
+30 -2
View File
@@ -8,6 +8,7 @@
use egui::{Color32, Stroke, pos2};
use photocraft_doc::LayerId;
use photocraft_engine::layer_multi_cmds::move_targets;
use photocraft_engine::snap::{SnapKind, SnapLine, SnapOptions, SnapTargets, layer_rect, union};
use crate::PhotocraftApp;
@@ -104,7 +105,24 @@ fn moving_rect(app: &PhotocraftApp, tool: Tool) -> Option<[f64; 4]> {
if crate::move_ui::moves_selected_pixels_with(app, tool) {
return selection_rect(app);
}
union(st.selected_layers().into_iter().filter_map(|id| layer_rect(&st.doc, id)))
// What moves, not what is selected: a group carries its layers and linked layers go along.
union(move_targets(&st.doc, &st.selected_layers()).into_iter().filter_map(|id| tree_rect(&st.doc, id)))
}
/// The bounds of layer `id` as the Move tool moves and snaps it. `snap::layer_rect` skips groups,
/// which hold no pixels of their own: a group is the layers in it, whole even with a selection
/// (it moves them whole, so clipping its pixel layers to the selection would frame less than
/// moves). An artboard is its board, as the snap targets take it, since the whole board moves.
pub(crate) fn tree_rect(doc: &photocraft_doc::Document, id: LayerId) -> Option<[f64; 4]> {
let l = doc.layer(id)?;
let b = if let Some(a) = l.artboard() {
a.rect
} else if l.is_group() {
photocraft_engine::transform_cmds::transform_bounds(doc, l)
} else {
return layer_rect(doc, id);
};
(!b.is_empty()).then(|| [b.x0, b.y0, b.x1, b.y1].map(f64::from))
}
/// The bounds of the pixels a selection moves, where they are shown: its floating piece's outline
@@ -231,7 +249,17 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) {
} else if tool == Tool::Move && crate::rulers::guide_at(app, p[0], p[1]).is_some() {
Some((Gesture::Guide, Vec::new()))
} else if tool == Tool::Move || moves_layer {
let exclude = app.session.active().map(|s| s.selected_layers()).unwrap_or_default();
// The targets and everything inside them stay out of the snap targets: a moving group
// must not snap to its own layers' starting edges. Selected pixels float off the active
// layer alone, so its linked layers stay put and stay targets.
let pixels = crate::move_ui::moves_selected_pixels_with(app, tool);
let exclude = app.session.active().map_or_else(Vec::new, |s| {
let mut ids = s.selected_layers();
if !pixels {
ids.extend(crate::move_ui::with_descendants(&s.doc, &move_targets(&s.doc, &ids)));
}
ids
});
moving_rect(app, tool).map(|rect| (Gesture::Move { rect }, exclude))
} else if straightens {
// A straighten line (`crop_straighten::pointer` takes the press before the frame's drags)
+100
View File
@@ -984,3 +984,103 @@ fn a_path_box_snaps_to_a_stationary_selection_edge() {
crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 143.0, y: 110.0 }, m);
assert_eq!(app.ui.transform.as_ref().unwrap().quad[0][0], 123.0);
}
/// A group of two layers (40..90 x 40..80 and 100..140 x 100..130) selected, as one layer.
fn app_with_group() -> PhotocraftApp {
let mut app = app_with_box();
app.run("layer.new.layer", json!({"name": "second"})).unwrap();
app.run("select.rect", json!({"x": 100, "y": 100, "width": 40, "height": 30})).unwrap();
app.run("edit.fill", json!({"color": "#00ff00"})).unwrap();
app.run("select.deselect", json!({})).unwrap();
app.run("layer.select", json!({"name": "mover"})).unwrap();
app.run("layer.select", json!({"name": "second", "mode": "add"})).unwrap();
app.run("layer.groupLayers", json!({"name": "G"})).unwrap();
app.ui.tool = Tool::Move;
app.ui.tool_options.move_auto_select = false;
app
}
fn child_bounds(app: &PhotocraftApp, name: &str) -> photocraft_geom::Rect {
let st = app.session.active().unwrap();
let (_, _, l) = st.doc.walk().into_iter().find(|(_, _, l)| l.name == name).unwrap();
l.surface().unwrap().content_bounds()
}
/// A plain Move drag from `from` to `to`, released there.
fn move_drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2]) {
let m = egui::Modifiers::NONE;
crate::canvas::tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, m);
crate::canvas::tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, m);
crate::canvas::tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, m);
}
/// #13418: a selected group had no Move gesture at all (`layer_rect` skips groups), so no blue box
/// and no Smart Guides; the box is the union of the layers it carries.
#[test]
fn dragging_a_group_frames_the_layers_inside_it() {
for bbox in [false, true] {
let mut app = app_with_group();
app.session.edit_prefs(|p| p.interface.show_bounding_box_when_dragging_layer = bbox);
crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, egui::Modifiers::NONE);
let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone());
assert_eq!(gesture, Some(Gesture::Move { rect: [40.0, 40.0, 140.0, 130.0] }), "bounding box pref {bbox}");
}
}
/// #13418: a group moves its layers whole with a selection too, so the box isn't cut to the
/// selection as a pixel layer's is.
#[test]
fn a_group_box_is_not_cut_to_the_selection() {
let mut app = app_with_group();
app.run("select.rect", json!({"x": 50, "y": 50, "width": 20, "height": 20})).unwrap();
crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, egui::Modifiers::NONE);
let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone());
assert_eq!(gesture, Some(Gesture::Move { rect: [40.0, 40.0, 140.0, 130.0] }));
}
/// #13418: the group's edge snaps to another layer's edge as a single layer's does.
#[test]
fn a_dragged_group_snaps_to_other_layers() {
let mut app = app_with_group();
// 72 px down puts the group's bottom edge (130 -> 202) 2 px past the target's top (200); no
// other edge or centre is within reach.
move_drag(&mut app, [60.0, 60.0], [80.0, 132.0]);
let (m, s) = (child_bounds(&app, "mover"), child_bounds(&app, "second"));
assert_eq!((m.x0, m.y0, s.y0), (60, 110, 170), "snapped 2 px up, the layers kept their spacing");
}
/// #13418: a moving group's layers are not snap targets, or it would snap to where it started.
#[test]
fn a_dragged_group_does_not_snap_to_its_own_layers() {
let mut app = app_with_group();
// +46, +3: the group's left edge (86) is 4 px from where the mover's right edge was (90) and
// its top (43) 3 px from where the mover's top was (40); nothing else is in reach.
move_drag(&mut app, [60.0, 60.0], [106.0, 63.0]);
let b = child_bounds(&app, "mover");
assert_eq!((b.x0, b.y0), (86, 43), "moved by the raw offset");
}
/// Selected pixels float off the active layer alone: a layer linked to it stays put, so the
/// pixels still snap to it.
#[test]
fn moving_selected_pixels_snaps_to_a_linked_layer() {
let mut app = app_with_box();
app.run("layer.new.layer", json!({"name": "linked"})).unwrap();
app.run("select.rect", json!({"x": 150, "y": 120, "width": 40, "height": 20})).unwrap();
app.run("edit.fill", json!({"color": "#00ff00"})).unwrap();
app.run("select.deselect", json!({})).unwrap();
app.run("layer.select", json!({"name": "mover"})).unwrap();
app.run("layer.select", json!({"name": "linked", "mode": "add"})).unwrap();
app.run("layer.linkLayers", json!({"linked": true})).unwrap();
app.run("layer.select", json!({"name": "mover"})).unwrap();
app.ui.tool = Tool::Move;
app.ui.tool_options.move_auto_select = false;
app.run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap();
// 37 px down puts the pixels' bottom edge (80 -> 117) 3 px above the linked layer's top (120);
// no other edge or centre is within reach.
move_drag(&mut app, [60.0, 60.0], [60.0, 97.0]);
app.run("select.drop", json!({})).unwrap();
let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds().y0);
assert_eq!(sel, Some(80), "snapped to the linked layer's top");
assert_eq!(child_bounds(&app, "linked").y0, 120, "the linked layer stayed put");
}
+4 -12
View File
@@ -11,8 +11,8 @@
//! so a pointer resting on the canvas costs nothing per frame.
use egui::{Color32, Pos2, Stroke, pos2, vec2};
use photocraft_doc::{DocId, Document, LayerId};
use photocraft_engine::snap::{layer_rect, union};
use photocraft_doc::{DocId, LayerId};
use photocraft_engine::snap::union;
use crate::PhotocraftApp;
use crate::canvas::ViewXform;
@@ -82,14 +82,6 @@ pub(crate) fn measure(a: [f64; 4], b: [f64; 4]) -> Overlay {
out
}
/// The bounds of layer `id` and, for a group, of the layers in it (`snap::layer_rect`: what the
/// Move tool moves and snaps).
fn tree_rect(doc: &Document, id: LayerId) -> Option<[f64; 4]> {
let rows = doc.walk();
let (prefix, _, _) = rows.iter().find(|(_, _, l)| l.id == id)?;
union(rows.iter().filter(|(p, _, _)| p.starts_with(prefix)).filter_map(|(_, _, l)| layer_rect(doc, l.id)))
}
/// The last answer: the boxes measured with the pointer at `at` on that revision and selection.
pub(crate) struct Cache {
doc: DocId,
@@ -115,13 +107,13 @@ fn boxes(app: &mut PhotocraftApp, at: [f64; 2]) -> Option<([f64; 4], [f64; 4])>
return c.boxes;
}
let d = &st.doc;
let a = union(selected.iter().filter(|id| !d.is_background(**id)).filter_map(|id| tree_rect(d, *id)));
let a = union(selected.iter().filter(|id| !d.is_background(**id)).filter_map(|id| crate::snap_ui::tree_rect(d, *id)));
let canvas = [0.0, 0.0, d.size.width as f64, d.size.height as f64];
let hovered = photocraft_engine::pick_cmds::move_auto_select_target(d, at[0], at[1], group).filter(|id| {
// The selection itself (or a layer in a selected group) isn't another layer.
!d.is_background(*id) && !selected.iter().any(|s| *s == *id || d.path_of(*id).zip(d.path_of(*s)).is_some_and(|(p, q)| p.starts_with(&q)))
});
let boxes = a.map(|a| (a, hovered.and_then(|id| tree_rect(d, id)).unwrap_or(canvas)));
let boxes = a.map(|a| (a, hovered.and_then(|id| crate::snap_ui::tree_rect(d, id)).unwrap_or(canvas)));
app.move_distance = Some(Cache { doc, revision, at, group, selected, boxes });
boxes
}