PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Move tool: transform controls and snapping frame the moved pixels too (#7538)
Hector Villarreal5c7ca36f0775
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex d5501fbc5..d2a1b0a4e 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -4838,6 +4838,9 @@ fn transform_controls_rect(app: &PhotocraftApp, xf: &ViewXform) -> Option<Rect>.map(|l| photocraft_engine::transform_cmds::transform_frame(&st.doc, l)).filter(|b| !b.is_empty()).reduce(|a, b| a.union(&b))?;+ // Floating selected pixels are framed by their outline, as their ants are (#7538), not by the+ // selection they were cut with.+ let b = app.session.active_index().and_then(|idx| floating_outline(app, idx)).map_or(b, |o| o.content_bounds());let (dx, dy) = crate::move_ui::floating_offset(app).unwrap_or((0, 0));let b = b.translate(dx, dy);if b.is_empty() {@@ -5478,6 +5481,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)app.ui.status_error = true;return;}+ crate::snap_ui::follow_cut(app);let mut d = Drag::new(tool, [x, y], vec![[x, y, pressure as f64]], mods, false);d.sel_move = Some(cut);app.drag = Some(d);@@ -8380,6 +8384,32 @@ mod transform_controls_tests {use super::*;use serde_json::json;+ /// #7538: as in Photoshop, the box frames a loose selection until its pixels are moved, then+ /// the pixels themselves, while they float and once dropped.+ #[test]+ fn transform_controls_frame_the_moved_pixels_not_the_marquee() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", json!({"width": 200, "height": 200, "background": "transparent"})).unwrap();+ app.session.execute("select.rect", json!({"x": 40, "y": 40, "width": 50, "height": 40})).unwrap();+ app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap();+ app.session.execute("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap();+ app.sync_views();+ app.ui.tool = Tool::Move;+ app.ui.tool_options.move_show_transform = true;+ let xf =+ ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 };+ let frame = |app: &PhotocraftApp| {+ let r = transform_controls_rect(app, &xf).unwrap();+ let (lt, rb) = (xf.to_doc(r.left_top()), xf.to_doc(r.right_bottom()));+ [lt[0], lt[1], rb[0], rb[1]].map(|v| v.round() as i32)+ };+ assert_eq!(frame(&app), [20, 20, 120, 110], "the selection, before anything moves");+ app.run("select.float", json!({"dx": 10, "dy": 5})).unwrap();+ assert_eq!(frame(&app), [50, 45, 100, 85], "the floating pixels");+ app.run("select.drop", json!({})).unwrap();+ assert_eq!(frame(&app), [50, 45, 100, 85], "dropped");+ }+#[test]fn several_selected_layers_share_one_frame_without_handles() {// As in Photoshop: Show Transform Controls frames every selected layer. Free Transformdiff --git a/crates/ui-egui/src/move_ui.rs b/crates/ui-egui/src/move_ui.rsindex 347151660..86e567521 100644--- a/crates/ui-egui/src/move_ui.rs+++ b/crates/ui-egui/src/move_ui.rs@@ -39,7 +39,10 @@ pub(crate) fn float_selected(app: &mut PhotocraftApp, copy: bool, dx: f64, dy: flet floating = app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some());let dropped = if copy && floating { app.run("select.drop", json!({})) } else { Ok(json!(null)) };match dropped.and_then(|_| app.run("select.float", json!({"dx": dx, "dy": dy, "copy": copy}))) {- Ok(_) => true,+ Ok(_) => {+ crate::snap_ui::follow_cut(app);+ true+ }Err(e) => {app.ui.status = e;app.ui.status_error = true;diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rsindex f8d9c0bbf..6c3717960 100644--- a/crates/ui-egui/src/snap_ui.rs+++ b/crates/ui-egui/src/snap_ui.rs@@ -101,29 +101,30 @@ fn moving_rect(app: &PhotocraftApp, tool: Tool) -> Option<[f64; 4]> {union(st.selected_layers().into_iter().filter_map(|id| layer_rect(&st.doc, id)))}-/// The bounds of the pixels a selection moves, where they are shown (#7538): its floating piece's-/// outline at its offset. Before a drag first cuts the piece, the selection's bounds over the-/// active layer's pixels, which hold the outline (exactly when the pixels lie in the selection).-/// A targeted mask, an opaque layer or no pixels selected: the selection's own bounds.+/// The bounds of the pixels a selection moves, where they are shown: its floating piece's outline+/// at its offset (#7538), else (nothing cut yet, or a mask or empty piece) the selection's bounds.fn selection_rect(app: &PhotocraftApp) -> Option<[f64; 4]> {let st = app.session.active()?;let sel = st.doc.selection.as_ref()?;let r = match photocraft_engine::float_cmds::floating(st) {Some(f) => f.parts.outline().unwrap_or(sel).content_bounds().translate(f.offset.0, f.offset.1),- None => {- let b = sel.content_bounds();- let pixels = st- .active_layer- .filter(|&id| !st.doc.is_background(id) && crate::canvas::paint_target(app) == "pixels")- .and_then(|id| st.doc.layer(id)?.surface())- .filter(|s| s.format().alpha)- .map(|s| b.intersect(&s.content_bounds()));- pixels.filter(|p| !p.is_empty()).unwrap_or(b)- }+ None => sel.content_bounds(),};(!r.is_empty()).then(|| [r.x0, r.y0, r.x1, r.y1].map(f64::from))}+/// A press cut the selected pixels after snapping began (`filter_event` runs before the tools):+/// the move now snaps the cut piece's outline, not the selection it was cut with (#7538).+pub(crate) fn follow_cut(app: &mut PhotocraftApp) {+ let Some(rect) = selection_rect(app) else { return };+ if let Some(snap) = app.prefs_rt.snap.as_mut() {+ match &mut snap.gesture {+ Gesture::Move { rect: r } | Gesture::SelectionMove { rect: r } => *r = rect,+ _ => {}+ }+ }+}+fn is_point_tool(t: Tool) -> bool {matches!(t,@@ -570,21 +571,41 @@ mod tests {app}- /// #7538: moving selected pixels snaps (and frames) the pixels, not a loose marquee round them:- /// the box hugs the moved pixels before the first drag cuts them and while they float.+ /// #7538: moving selected pixels snaps (and frames) the pixels, not a loose marquee round them.+ /// Snapping starts before the press cuts them, so the cut retargets it to their outline.#[test]fn moving_selected_pixels_snaps_their_bounds_not_the_marquee() {+ for alt in [false, true] {+ let mut app = app_with_box();+ app.ui.tool = Tool::Move;+ app.run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap();+ let m = egui::Modifiers { alt, ..Default::default() };+ crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m);+ let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone());+ assert_eq!(gesture, Some(Gesture::Move { rect: [40.0, 40.0, 90.0, 80.0] }), "alt {alt}: the blue box, not the marquee");+ // 263 px right puts the box's left edge 3 px from the target's (the marquee's 20 px off).+ crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 323.0, y: 100.0, pressure: 1.0 }, m);+ crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m);+ app.run("select.drop", json!({})).unwrap();+ let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds().x0);+ assert_eq!(sel, Some(300), "alt {alt}: snapped to the target's edge");+ }+ // A marquee's ⌘-drag cuts them the same way.let mut app = app_with_box();- app.ui.tool = Tool::Move;+ app.ui.tool = Tool::RectMarquee;app.run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap();- assert_eq!(moving_rect(&app, Tool::Move), Some([40.0, 40.0, 90.0, 80.0]), "before the cut");- app.run("select.float", json!({"dx": 10, "dy": 5})).unwrap();- assert_eq!(moving_rect(&app, Tool::Move), Some([50.0, 45.0, 100.0, 85.0]), "floating");- app.run("select.drop", json!({})).unwrap();+ let cmd = egui::Modifiers { mac_cmd: true, command: true, ..Default::default() };+ crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, cmd);+ let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone());+ assert_eq!(gesture, Some(Gesture::SelectionMove { rect: [40.0, 40.0, 90.0, 80.0] }), "⌘-drag with the marquee");// The Background has no transparency to shrink to.+ let mut app = app_with_box();+ app.ui.tool = Tool::Move;app.run("layer.select", json!({"name": "Background"})).unwrap();app.run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap();- assert_eq!(moving_rect(&app, Tool::Move), Some([20.0, 20.0, 120.0, 110.0]), "the Background");+ 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: [20.0, 20.0, 120.0, 110.0] }), "the Background");}fn mover_bounds(app: &PhotocraftApp) -> photocraft_geom::Rect {diff --git a/docs/ui-parity.md b/docs/ui-parity.mdindex b5f960e50..1068cc812 100644--- a/docs/ui-parity.md+++ b/docs/ui-parity.md@@ -94,7 +94,7 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme| Arrow nudge of path points with Direct Selection | missing | no arrow handling in `direct_select.rs` || ⌘-arrow nudges the layer from any tool | missing | #2474 || ⌥-drag duplicates, ⇧ constrains to 45° | done | `move_mods.rs` |-| Dragging selected pixels shrinks the selection to them (the selection times the layer's transparency) as the drag starts, also with ⌥; the move box and snapping use the moved pixels' bounds | done | #7538, `canvas.rs` `floating_outline`, `snap_ui.rs` `selection_rect`, engine `float_cmds.rs` `CutParts::outline` |+| Dragging selected pixels shrinks the selection to them (the selection times the layer's transparency) as the drag starts, also with ⌥; the move box, snapping and Show Transform Controls use the moved pixels' bounds | done | #7538, `canvas.rs` `floating_outline` and `transform_controls_rect`, `snap_ui.rs` `follow_cut`, engine `float_cmds.rs` `CutParts::outline` || Auto-Select with hover outline, box-select layers, double-click fit | missing | TOOL-214-4 || Auto-Select bounding box and snapping follow the picked layer | partial | follow the previous layer (#2666) || Several layers selected (⇧-click adds, box-select): Show Transform Controls frames them all, with every selected shape's outline | partial: the shared frame has no handles, since Free Transform works on one layer (`canvas.rs` `transform_controls_layers`, `several_selected_layers_share_one_frame_without_handles`; `move_auto_select_tests.rs`) | |