From 64fc59266d2823b249c892f9483bc36090a1bb20 Mon Sep 17 00:00:00 2001 From: qvest-ssels <214787280+qvest-ssels@users.noreply.github.com> Date: Sat, 28 Nov 2026 17:02:52 +0100 Subject: [PATCH] Smart Guides: with View > Snap off, a Move drag still shows exact alignments (#16857) With Snap off the Move tool drew no smart guides at all, because the lines only came out of snapping. Now, with Snap off and Smart Guides on, an edge or centre of the moved box that lines up exactly (at the whole-pixel offset the drag commits) with another layer's draws its line, with no pull. Smart Guides off or the suspend key held draws nothing; Snap on is unchanged. --- .../src/canvas/snap_off_lines_tests.rs | 97 +++++++++++++++++++ crates/ui-egui/src/canvas/snap_ui.rs | 2 +- crates/ui-egui/src/canvas/tools/move_tool.rs | 27 +++++- 3 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/src/canvas/snap_off_lines_tests.rs diff --git a/crates/ui-egui/src/canvas/snap_off_lines_tests.rs b/crates/ui-egui/src/canvas/snap_off_lines_tests.rs new file mode 100644 index 00000000000..af9d9cb6dea --- /dev/null +++ b/crates/ui-egui/src/canvas/snap_off_lines_tests.rs @@ -0,0 +1,97 @@ +//! #16857: with View > Snap off and Smart Guides on, a Move-tool drag that lines an edge or centre up exactly with +//! another layer's still draws that smart guide, with no pull; Smart Guides off draws nothing, and Snap on keeps +//! snapping. + +use craft::behavior::Axis; +use craft::behavior::geometry::SnapLine; +use serde_json::json; + +use crate::canvas::{ToolEvent, tool_event, tool_event_response}; +use crate::state::Tool; +use crate::{PhotocraftApp, Services}; + +/// The epic's test document: 1000 x 800, white Background, A red (100,100)-(300,260), C green (400,100)-(500,200) +/// and B blue (600,400)-(750,520) on top and active; the Move tool without Auto-Select, the view fitted (the test +/// window is smaller than the document at 100 %; an exact coincidence does not depend on the zoom). +fn test_document(snap: bool, smart_guides: bool) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default()); + app.run("file.new", json!({"width": 1000, "height": 800})).unwrap(); + app.sync_views(); + for (name, rect, color) in [("A", [100, 100, 200, 160], "#ff0000"), ("C", [400, 100, 100, 100], "#00ff00"), ("B", [600, 400, 150, 120], "#0000ff")] { + app.run("layer.new.layer", json!({"name": name})).unwrap(); + app.run("select.rect", json!({"x": rect[0], "y": rect[1], "width": rect[2], "height": rect[3]})).unwrap(); + app.run("edit.fill", json!({"color": color})).unwrap(); + } + app.run("select.deselect", json!({})).unwrap(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_auto_select = false; + app.ui.extras.snap = snap; + app.ui.view.show.smart_guides = smart_guides; + app +} + +/// B's bounds. +fn b_left(app: &PhotocraftApp) -> i32 { + let st = app.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds().x0 +} + +/// Drags B from its centre (675, 460) by `dx` and returns the snap lines of the last move; B's left edge after the +/// release. +fn drag_b(app: &mut PhotocraftApp, dx: f64) -> (Vec, i32) { + let m = egui::Modifiers::NONE; + tool_event(app, ToolEvent::Down { x: 675.0, y: 460.0, pressure: 1.0 }, m); + tool_event(app, ToolEvent::Move { x: 675.0 + dx / 2.0, y: 460.0, pressure: 1.0 }, m); + let lines = tool_event_response(app, ToolEvent::Move { x: 675.0 + dx, y: 460.0, pressure: 1.0 }, m).snapped; + tool_event(app, ToolEvent::Up { x: 675.0 + dx, y: 460.0 }, m); + (lines, b_left(app)) +} + +fn vertical_at(lines: &[SnapLine], x: f64) -> bool { + lines.iter().any(|l| l.along == Axis::Y && l.position == x && l.kind.is_smart()) +} + +#[test] +fn snap_off_draws_a_smart_guide_where_an_edge_lines_up_exactly() { + let mut app = test_document(false, true); + let (lines, left) = drag_b(&mut app, -500.0); + assert_eq!(left, 100, "B's left edge on A's"); + assert!(vertical_at(&lines, 100.0), "x=100 (A's left) is drawn: {lines:?}"); +} + +#[test] +fn snap_off_draws_nothing_and_does_not_pull_four_pixels_away() { + let mut app = test_document(false, true); + let (lines, left) = drag_b(&mut app, -496.0); + assert_eq!(left, 104, "no pull with Snap off"); + assert!(!vertical_at(&lines, 100.0), "nothing for A's left 4 px away: {lines:?}"); +} + +#[test] +fn snap_off_and_smart_guides_off_draw_nothing() { + let mut app = test_document(false, false); + let (lines, left) = drag_b(&mut app, -500.0); + assert_eq!(left, 100); + assert!(lines.is_empty(), "{lines:?}"); +} + +#[test] +fn snap_off_ctrl_draws_nothing() { + let mut app = test_document(false, true); + let m = egui::Modifiers::NONE; + let ctrl = if cfg!(target_os = "macos") { egui::Modifiers::CTRL } else { egui::Modifiers::COMMAND }; + tool_event(&mut app, ToolEvent::Down { x: 675.0, y: 460.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Move { x: 425.0, y: 460.0, pressure: 1.0 }, ctrl); + let lines = tool_event_response(&mut app, ToolEvent::Move { x: 175.0, y: 460.0, pressure: 1.0 }, ctrl).snapped; + tool_event(&mut app, ToolEvent::Up { x: 175.0, y: 460.0 }, ctrl); + assert_eq!(b_left(&app), 100); + assert!(lines.is_empty(), "the suspend key hides the lines too: {lines:?}"); +} + +#[test] +fn snap_on_keeps_snapping_and_its_line() { + let mut app = test_document(true, true); + let (lines, left) = drag_b(&mut app, -496.0); + assert_eq!(left, 100, "Snap on pulls B's left onto A's"); + assert!(vertical_at(&lines, 100.0), "{lines:?}"); +} diff --git a/crates/ui-egui/src/canvas/snap_ui.rs b/crates/ui-egui/src/canvas/snap_ui.rs index 7fc08b6327c..8158c56b5a6 100644 --- a/crates/ui-egui/src/canvas/snap_ui.rs +++ b/crates/ui-egui/src/canvas/snap_ui.rs @@ -57,7 +57,7 @@ fn selection_moves(app: &PhotocraftApp) -> bool { /// group does not snap to its own layers' starting edges), the type layer being edited (a paragraph-box handle drag /// does not snap to the text it resizes, #6819), the layers of the open Free Transform and the shape layers the shape /// scale box or a corner widget is pressing. -fn moving_set(app: &PhotocraftApp) -> Vec { +pub(crate) fn moving_set(app: &PhotocraftApp) -> Vec { let mut set = Vec::new(); if moves_as_move_tool(app) && let Some(st) = app.session.active() diff --git a/crates/ui-egui/src/canvas/tools/move_tool.rs b/crates/ui-egui/src/canvas/tools/move_tool.rs index 80c634d3819..6c49cb207c3 100644 --- a/crates/ui-egui/src/canvas/tools/move_tool.rs +++ b/crates/ui-egui/src/canvas/tools/move_tool.rs @@ -11,7 +11,7 @@ use super::{FamilyTool, ToolScope}; use crate::PhotocraftApp; use crate::canvas::document_display::CanvasHost; use crate::state::Tool; -use craft::behavior::geometry::{ConstrainStyle, Directions, MoveGesture}; +use craft::behavior::geometry::{ConstrainStyle, Directions, MoveGesture, SnapKinds}; use craft::behavior::kurbo::{self, Point}; use craft::behavior::{CancelReason, Cursor, CursorIcon, PaintedCursor, PointerGlyph}; use craft::keymap::{Chord, Key, Mods}; @@ -267,6 +267,24 @@ fn press_at(app: &mut PhotocraftApp, p: [f64; 2], mods: Mods, style: ConstrainSt (ToolAction::Claimed, Some(run)) } +/// How far apart two coordinates may be and still line up exactly: the moved box sits on whole pixels. +const EXACT: f64 = 1e-6; + +/// With View › Snap off and Smart Guides on, the smart guides of the edges and centres of `moved` (the box at the +/// offset the drag commits) that line up exactly with another layer's, as in the reference application: nothing +/// pulls, the line only marks the coincidence (#16857). Snap on draws its lines through `snap_rect`; Smart Guides off +/// or the suspend key held draws none. +fn exact_lines(app: &PhotocraftApp, cx: &mut ToolContext<'_>, moved: kurbo::Rect, mods: Mods) { + let Some(idx) = app.session.active_index() else { return }; + let options = crate::canvas::snap_ui::snap(app, idx).options(); + if options.on || !options.smart_guides || craft::ui::canvas::snap_suspended(mods, craft::keymap::Platform::current(), options.suspend, false) { + return; + } + let targets = crate::canvas::snap_ui::targets(app, idx, &crate::canvas::snap_ui::moving_set(app)); + let (_, lines) = targets.snap_rect_of(moved, EXACT, SnapKinds::ALL); + cx.add_snapped(lines); +} + /// The drag to `pointer`: the ⇧ lock, the snap of the moving box, the ⌥ duplicate and, while the button is down /// (`readout`), the readout; the release applies its last sample without one. /// A pass applies only its own pointer sample, so a second pass of a frame moves the offset to @@ -293,6 +311,10 @@ fn follow(run: &mut Run, app: &mut PhotocraftApp, pointer: &ToolPointer, cx: &mu run.offset = (dx, dy); run.delta = [f64::from(dx), f64::from(dy)]; run.moved = true; + if let Some(b) = run.box_rect { + let moved = kurbo::Rect::new(b[0], b[1], b[2], b[3]) + kurbo::Vec2::new(run.delta[0], run.delta[1]); + exact_lines(app, cx, moved, pointer.mods); + } if readout { let x_arrow = if dx < 0 { "←" } else { "→" }; let y_arrow = if dy < 0 { "↑" } else { "↓" }; @@ -466,3 +488,6 @@ impl CanvasTool for MoveRun<'_> { #[cfg(test)] #[path = "move_cursor_cross_tests.rs"] mod move_cursor_cross_tests; +#[cfg(test)] +#[path = "../snap_off_lines_tests.rs"] +mod snap_off_lines_tests;