Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Crop default mode: moving the image under the box snaps (#8580) (#8622)

Khawar Zamman Wani41599c591c4f
diff --git a/crates/ui-egui/src/crop_mode.rs b/crates/ui-egui/src/crop_mode.rs
index ee684517b..36bc4d659 100644
--- a/crates/ui-egui/src/crop_mode.rs
+++ b/crates/ui-egui/src/crop_mode.rs
@@ -3,8 +3,9 @@
//! - **Default mode** (Photoshop's default, Use Classic Mode off): the crop box stays upright on
//! screen and the image turns behind it. A drag outside the box turns the image about the box's
//! centre (⇧ in 15° steps; the readout beside the pointer is the image's turn, clockwise
-//! positive), a drag inside moves the image under the box (the box stays put), the arrow keys
-//! nudge the image, and the handles resize the box on screen as usual. With **Auto Center
+//! positive), a drag inside moves the image under the box (the box stays put; with View › Snap
+//! on, the upright frame's edges and centre snap as a Classic Mode frame's do, #8580), the arrow
+//! keys nudge the image, and the handles resize the box on screen as usual. With **Auto Center
//! Preview** on (the default), a box being resized keeps its centre where it is on screen (also
//! after a Space pan) while the handle follows the pointer, a drawn box moves to the middle of
//! the canvas on release, and X re-centres it. A drag inside never re-centres the box
@@ -425,6 +426,100 @@ mod tests {
assert!(r.iter().zip([20.0, 30.0, 80.0, 70.0]).all(|(a, b)| (a - b).abs() < 1e-3), "{r:?}");
}
+ /// Moves the image under the box from document point `from` by `d` (both in the view at the
+ /// press), checks the snap lines before release, and returns where the box's centre started
+ /// on screen.
+ fn image_drag(app: &mut PhotocraftApp, from: [f32; 2], d: [f32; 2], line: Option<(bool, f64)>) -> Pos2 {
+ let (a, b) = (xf(app).to_screen(from[0], from[1]), xf(app).to_screen(from[0] + d[0], from[1] + d[1]));
+ let mid = mid_of(screen_box(app));
+ let p = xf(app).to_doc(a);
+ tool_event(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, NONE);
+ assert_eq!(app.prefs_rt.snap.as_ref().map(|s| &s.gesture), Some(&crate::snap_ui::Gesture::CropImage));
+ for t in [0.5, 1.0] {
+ let p = xf(app).to_doc(a + (b - a) * t);
+ tool_event(app, ToolEvent::Move { x: p[0], y: p[1], pressure: 1.0 }, NONE);
+ }
+ if let Some((vertical, pos)) = line {
+ assert!(app.prefs_rt.snap_lines.iter().any(|l| l.vertical == vertical && l.pos == pos), "{:?}", app.prefs_rt.snap_lines);
+ }
+ let p = xf(app).to_doc(b);
+ tool_event(app, ToolEvent::Up { x: p[0], y: p[1] }, NONE);
+ mid
+ }
+
+ fn assert_frame(app: &PhotocraftApp, want: [f64; 4], what: &str) {
+ let r = app.ui.crop_rect.unwrap();
+ assert!(r.iter().zip(want).all(|(a, b)| (a - b).abs() < 1e-3), "{what}: {r:?}, want {want:?}");
+ }
+
+ /// #8580: moving the image under the box snaps the frame's edges and centre with View › Snap
+ /// on, so the content can be centred in it without leaving the Crop tool. The box stays put.
+ #[test]
+ fn moving_the_image_snaps_the_frame_to_the_document_centre() {
+ for (snap, zoom, flip) in [(false, 1.0, false), (true, 1.0, false), (true, 2.0, false), (true, 1.0, true)] {
+ let mut app = app();
+ app.ui.extras.snap = snap;
+ app.ui.views[0].zoom = zoom;
+ app.ui.view.flip_horizontal = flip;
+ with_frame(&mut app);
+ // The frame [40, 30, 100, 70] goes 27 px right over the image: its centre lands 3 px
+ // short of the document's (100, 50), inside the snap radius at either zoom.
+ let mid = image_drag(&mut app, [70.0, 50.0], [-27.0, 0.0], snap.then_some((true, 100.0)));
+ let what = format!("snap {snap}, zoom {zoom}, flip {flip}");
+ assert_frame(&app, if snap { [70.0, 30.0, 130.0, 70.0] } else { [67.0, 30.0, 127.0, 70.0] }, &what);
+ assert!(mid_of(screen_box(&app)).distance(mid) < 0.01, "{what}: the box moved on screen");
+ assert!(app.prefs_rt.snap.is_none() && app.prefs_rt.snap_lines.is_empty());
+ }
+ }
+
+ /// The issue's case: a box larger than the canvas (to extend it) centres on the content's
+ /// layer, both ways. A turned box doesn't snap (its edges don't line up with anything).
+ #[test]
+ fn moving_the_image_centres_a_larger_box_on_a_layer() {
+ let mut app = app();
+ app.ui.extras.snap = true;
+ app.run("layer.new.layer", json!({"name": "logo"})).unwrap();
+ app.run("select.rect", json!({"x": 20, "y": 20, "width": 40, "height": 40})).unwrap();
+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ app.run("select.deselect", json!({})).unwrap();
+ crate::crop_ui::ensure_frame(&mut app);
+ app.ui.crop_rect = Some([-40.0, -60.0, 140.0, 120.0]);
+ app.crop.default_frame = false;
+ // Its centre (50, 30) goes to (42, 38): 2 px from the logo's centre (40, 40).
+ image_drag(&mut app, [50.0, 30.0], [8.0, -8.0], Some((false, 40.0)));
+ assert_frame(&app, [-50.0, -50.0, 130.0, 130.0], "centred on the logo");
+
+ app.ui.crop_angle = 30.0;
+ sync(&mut app, None);
+ let p = xf(&app).to_doc(mid_of(screen_box(&app)));
+ tool_event(&mut app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, NONE);
+ assert!(matches!(app.crop.drag, Some(CropDrag::MoveImage { .. })));
+ assert!(app.prefs_rt.snap.is_none(), "a turned box doesn't snap");
+ tool_event(&mut app, ToolEvent::Up { x: p[0], y: p[1] }, NONE);
+ }
+
+ /// With View › Snap To › Layers off, smart guides still centre the box on a layer, although
+ /// the document's bounds are snap targets that aren't within reach.
+ #[test]
+ fn moving_the_image_snaps_to_smart_guides_beside_out_of_reach_targets() {
+ for smart in [false, true] {
+ let mut app = app();
+ app.ui.extras.snap = true;
+ app.ui.view.snap_to.layers = false;
+ app.ui.view.show.smart_guides = smart;
+ app.run("layer.new.layer", json!({"name": "logo"})).unwrap();
+ app.run("select.rect", json!({"x": 20, "y": 20, "width": 40, "height": 40})).unwrap();
+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ app.run("select.deselect", json!({})).unwrap();
+ crate::crop_ui::ensure_frame(&mut app);
+ app.ui.crop_rect = Some([-40.0, -60.0, 140.0, 120.0]);
+ app.crop.default_frame = false;
+ image_drag(&mut app, [50.0, 30.0], [8.0, -8.0], smart.then_some((false, 40.0)));
+ let want = if smart { [-50.0, -50.0, 130.0, 130.0] } else { [-48.0, -52.0, 132.0, 128.0] };
+ assert_frame(&app, want, &format!("smart guides {smart}"));
+ }
+ }
+
/// #7088: the box's own Undo/Redo of a move (or a turn) in default mode puts the frame back
/// and moves the image, not the box: it stays put on screen (Auto Center Preview off) or goes
/// to the middle of the canvas (on).
diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs
index b83646af2..4bead9828 100644
--- a/crates/ui-egui/src/snap_ui.rs
+++ b/crates/ui-egui/src/snap_ui.rs
@@ -36,6 +36,9 @@ pub enum Gesture {
DrawMove { rect: [f64; 4] },
/// A guide being moved with the Move tool.
Guide,
+ /// Default crop mode, a drag inside an upright box: the image moves under it, so the frame
+ /// moves the other way over the document and its edges and centre snap ([`crop_image`]).
+ CropImage,
}
/// Snapping state of the drag in progress.
@@ -238,12 +241,21 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) {
// its edges.
let gesture = if app.crop.default_frame { Gesture::Point } else { Gesture::Move { rect } };
Some((gesture, Vec::new()))
+ } else if tool == Tool::Crop
+ && crate::crop_mode::moves_image(app)
+ && !app.crop.default_frame
+ && crate::crop_ui::angle(app) == 0.0
+ && app.ui.crop_rect.is_some_and(|r| crate::crop_ui::hit(r, p, crop_tol) == crate::crop_ui::Hit::Inside)
+ {
+ // The default mode moves the image under an upright box: the frame's edges and centre
+ // snap, as an edited Classic Mode frame's do, so content can be centred in it (#8580).
+ Some((Gesture::CropImage, Vec::new()))
} else if tool == Tool::Crop
&& (crate::crop_ui::turns_at(app, p)
|| app.ui.crop_rect.is_some_and(|r| crate::crop_ui::hit_turned(r, crate::crop_ui::angle(app), p, crop_tol) == crate::crop_ui::Hit::Inside))
{
// Turning the frame, moving a turned one (its edges don't line up with anything), or in
- // the default mode moving or turning the image under the box: no snapping.
+ // the default mode turning the image or moving it under a turned box: no snapping.
None
} else if let Some(cut) = crate::canvas::selection_drag_kind(app, tool, p, mods) {
// A marquee or lasso inside the selection moves the outline (⌘: cuts its pixels into a
@@ -276,7 +288,11 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) {
// A moving selection never snaps back to where it started.
targets = targets.filtered(|k| k != SnapKind::Selection);
}
- let smart = if snap_on(app) && matches!(gesture, Gesture::Move { .. }) && smart_on(app) { build(app, &exclude, true) } else { SnapTargets::default() };
+ let smart = if snap_on(app) && matches!(gesture, Gesture::Move { .. } | Gesture::CropImage) && smart_on(app) {
+ build(app, &exclude, true)
+ } else {
+ SnapTargets::default()
+ };
app.prefs_rt.snap = Some(ActiveSnap { gesture, start: p, targets, smart, disabled: false });
}
@@ -324,6 +340,12 @@ fn apply(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) -> [f64; 2
let (adj, lines) = set.snap_rect(moved, tol);
([p[0] + adj[0], p[1] + adj[1]], lines)
}
+ Gesture::CropImage => {
+ // Smart guides still snap when nothing in View › Snap To is within reach: the
+ // document's bounds alone keep `targets` from ever being empty.
+ let snapped = |set| crop_image(app, p, set, tol).filter(|(_, lines)| !lines.is_empty());
+ snapped(&snap.targets).or_else(|| snapped(&snap.smart)).unwrap_or((p, Vec::new()))
+ }
};
app.prefs_rt.snap_lines = lines;
out
@@ -413,6 +435,22 @@ fn crop_handle(app: &PhotocraftApp, p: [f64; 2], mods: egui::Modifiers, targets:
Some((view.map_or(q, |view| crate::crop_mode::from_press(app, view, q)), lines))
}
+/// Snap the image being moved under the crop box (default mode): the frame moves by the image's
+/// travel the other way over the document, so its edges and centre snap and the pointer moves by
+/// the frame's correction the other way too (#8580). As `crop_ui` reads this drag, the pointer is
+/// read in the camera at the press, since the view pans with the frame meanwhile.
+fn crop_image(app: &PhotocraftApp, p: [f64; 2], targets: &SnapTargets, tol: f64) -> Option<([f64; 2], Vec<SnapLine>)> {
+ let crate::crop_ui::CropDrag::MoveImage { start, rect, view } = app.crop.drag? else { return None };
+ let q = crate::crop_mode::to_press(app, view, p);
+ let d = [q[0] - start[0], q[1] - start[1]];
+ let (adj, lines) = targets.snap_rect([rect[0] - d[0], rect[1] - d[1], rect[2] - d[0], rect[3] - d[1]], tol);
+ if adj == [0.0, 0.0] {
+ return Some((p, lines));
+ }
+ let q = [q[0] - adj[0], q[1] - adj[1]];
+ Some((view.map_or(q, |view| crate::crop_mode::from_press(app, view, q)), lines))
+}
+
/// The bounds `[x0, y0, x1, y1]` of what drag `d` draws so far, for the tools whose drag Space
/// repositions and that snap (`None` for the rest, or a shape still too small to draw).
fn drawn_rect(app: &PhotocraftApp, d: &crate::canvas::Drag) -> Option<[f64; 4]> {
diff --git a/docs/ui-parity.md b/docs/ui-parity.md
index 8afece06d..f1ed065ce 100644
--- a/docs/ui-parity.md
+++ b/docs/ui-parity.md
@@ -1,6 +1,6 @@
# UI parity: tools, handles, modifiers and feel
-> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-21 · **Change:** minor (View › Extras hides the Move transform controls, #6699; Hand tool ⌘ / ⌥ are Zoom In / Zoom Out while held, #6434; the wheel steps value fields, #5473; a dragged guide shows its X or Y beside the pointer, #7680; Type options bar "T" icon scrubs the font size, #6437; Move tool: dragged selected pixels take their own outline as the selection, #7538; artboard backgrounds remain below child Free Transform previews; ⌫/Delete removes the selected smart filter, #4751; Object Finder foreground/background discrimination (#6548); rounded triangle, polygon and star corners; shape Fill/Stroke popovers accept hex RGB values, #6176; a guide let go over either ruler is removed, #6286; Crop rows corrected: saved crop presets and Delete Cropped Pixels off are done, #6562; marching ants in device pixels, #7489; Animated Zoom: holding the Zoom tool zooms continuously, #6210; shape Density and Feather, TOOL-214-12, #5846; Pen Auto Add/Delete on the path being drawn, #6140; Show Transform Controls frames every selected layer; guide keys and clicks; menu bar from the keyboard, #4988; Crop tool Content-Aware Fill; on-canvas corner radius widgets, TOOL-214-12; Curvature Pen edits the active path; Clip Thumbnails to Layer Bounds; Pen ⇧ 45°; last selected tool restored at launch; Layers panel filter-type dropdown with Kind, Name and Mode, #5048; Free Transform box and marching ants follow the selection, #5297; History: the trash deletes the selected snapshot, rows drag onto the trash, #5324; smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows; stale missing-tool rows corrected: Color Replacement, Red Eye and the Add/Delete Anchor Point pair are live toolbar tools, Frame landed; no percentage changed; selection-modifier and ⌥-wheel rows match their tests) · **Target:** Adobe Photoshop 2026
+> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-22 · **Change:** minor (Crop default mode: moving the image under the box snaps the frame's edges and centre, #8580; View › Extras hides the Move transform controls, #6699; Hand tool ⌘ / ⌥ are Zoom In / Zoom Out while held, #6434; the wheel steps value fields, #5473; a dragged guide shows its X or Y beside the pointer, #7680; Type options bar "T" icon scrubs the font size, #6437; Move tool: dragged selected pixels take their own outline as the selection, #7538; artboard backgrounds remain below child Free Transform previews; ⌫/Delete removes the selected smart filter, #4751; Object Finder foreground/background discrimination (#6548); rounded triangle, polygon and star corners; shape Fill/Stroke popovers accept hex RGB values, #6176; a guide let go over either ruler is removed, #6286; Crop rows corrected: saved crop presets and Delete Cropped Pixels off are done, #6562; marching ants in device pixels, #7489; Animated Zoom: holding the Zoom tool zooms continuously, #6210; shape Density and Feather, TOOL-214-12, #5846; Pen Auto Add/Delete on the path being drawn, #6140; Show Transform Controls frames every selected layer; guide keys and clicks; menu bar from the keyboard, #4988; Crop tool Content-Aware Fill; on-canvas corner radius widgets, TOOL-214-12; Curvature Pen edits the active path; Clip Thumbnails to Layer Bounds; Pen ⇧ 45°; last selected tool restored at launch; Layers panel filter-type dropdown with Kind, Name and Mode, #5048; Free Transform box and marching ants follow the selection, #5297; History: the trash deletes the selected snapshot, rows drag onto the trash, #5324; smart filter reordering by drag; smart object perspective row corrected to done, #426; Free Transform H/V skew fields; toolbar tool shortcuts can be reassigned; ⇧-dragged guides land on ruler ticks; layer and selection keys; tool keys; spring-loaded tool letters; type formatting keys; Pen Auto Add/Delete, #4144; smart filter rows; stale missing-tool rows corrected: Color Replacement, Red Eye and the Add/Delete Anchor Point pair are live toolbar tools, Frame landed; no percentage changed; selection-modifier and ⌥-wheel rows match their tests) · **Target:** Adobe Photoshop 2026
How PhotoCraft's direct manipulation compares with Photoshop 2026 (27.11.0): the toolbox, the
handles and modifier keys of each gesture, nudging, snapping, numeric precision, navigation,
@@ -148,6 +148,7 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme
| Overlays (thirds, grid, diagonal, triangle, golden ratio/spiral), O / ⇧O | done | TOOL-214-20 |
| Crop shield, Show Cropped Area (H) | done | TOOL-214-22 |
| Default mode: the image turns and moves behind an upright box; Auto Center Preview; Use Classic Mode | done | TOOL-214-23 |
+| Default mode: moving the image under an upright box snaps the frame's edges and centre (View › Snap), so content centres in the box without the Move tool | done | `crop_mode.rs::moving_the_image_snaps_the_frame_to_the_document_centre`, `crop_mode.rs::moving_the_image_centres_a_larger_box_on_a_layer` (#8580, 2026-10-22) |
| W × H × Resolution with units, built-in size presets, Front Image | done | TOOL-214-9 (`crop_size.rs`) |
| Saved custom crop presets (New / Delete Crop Preset, kept with the preferences; moving them to another computer is not offered) | done | TOOL-214-9 (`crop_presets.rs`, `tool.presets.*` with tool `crop`, #2723) |
| Fill: Content-Aware Fill: a turned or extending crop fills the uncovered corners and strips from the image (each piece with its own sampling window), on the Background or else the active pixel layer, in the crop's one undo step; Generative Expand not offered | done | `tests/it/crop_content_aware.rs`, `crop_ui.rs::commit_passes_the_fill_choice` (#3823, 2026-10-12) |
@@ -240,6 +241,7 @@ Paragraph; the rest open as in-app floating windows.
| Date | Change | Summary |
|---|---|---|
+| 2026-10-22 | minor | Crop default mode: a drag inside an upright box snaps the frame's edges and centre to the document, layers, guides and grid, as a Classic Mode frame's move does (#8580); no percentage changed |
| 2026-10-19 | minor | A guide being moved or dragged out of a ruler shows an `X:` / `Y:` position readout beside the pointer, from the zero point in the ruler unit (#7680) |
| 2026-10-17 | minor | The wheel steps value fields: a focused field takes it wherever the pointer is (the canvas and panels don't scroll), an unfocused one only a gesture that starts over it (#5473, #4061); no percentage changed |
| 2026-10-17 | minor | Object Finder foreground/background discrimination (#6548) |