From 79cfa2b65aeefc5164558ea359c2ec20a0470403 Mon Sep 17 00:00:00 2001 From: n-delic Date: Mon, 12 Oct 2026 11:22:58 +0200 Subject: [PATCH] Guides: Shift-drag out of a ruler lands on the nearest ruler tick (#3770) (#3796) As in Photoshop, holding Shift while dragging a new guide out of a ruler snaps it to the tick marks the ruler draws (tenths of the labelled step), in the current ruler unit and zoom. Without Shift the guide snaps to the document, layers, selection and grid as before. The tick spacing draw_rulers computed inline moves to ruler_scale, shared by the renderer and the new snap_to_tick, so the snap always matches the ticks on screen. Co-authored-by: Claude Opus 5.5 Co-authored-by: Brandon Thomas --- crates/ui-egui/src/rulers.rs | 79 +++++++++++++++++++++++++++++++----- docs/ui-parity.md | 4 +- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs index 58bcf04cf5..cc427758fe 100644 --- a/crates/ui-egui/src/rulers.rs +++ b/crates/ui-egui/src/rulers.rs @@ -268,6 +268,29 @@ fn ruler_pos(xf: &ViewXform, vertical: bool, u: f64, px_per_unit: f64) -> f32 { if vertical { rxf.to_screen(0.0, px).y } else { rxf.to_screen(px, 0.0).x } } +/// Document pixels per ruler unit and the labelled tick step (in ruler units) of the top ruler +/// (`y_axis` false) or the left one, from Preferences › Units & Rulers and the zoom. Minor ticks +/// sit at tenths of the step. +fn ruler_scale(app: &PhotocraftApp, xf: &ViewXform, y_axis: bool) -> (f64, f64) { + let ur = &app.session.prefs().units_and_rulers; + let (unit, ppi) = (ur.rulers, ur.point_size.per_inch()); + let (dpi, size) = + app.session.active().map_or((72.0, [1.0, 1.0]), |d| (d.doc.resolution_dpi.max(1.0) as f64, [d.doc.size.width as f64, d.doc.size.height as f64])); + let px_per_unit = unit.to_px(1.0, dpi, if y_axis { size[1] } else { size[0] }, ppi).max(1e-9); + // The top ruler runs along document x, which pixel aspect correction stretches. + let zoom = if y_axis { xf.zoom } else { xf.zoom_x() }; + (px_per_unit, nice_step(60.0 / (zoom as f64 * px_per_unit).max(1e-6), unit == Unit::Pixels)) +} + +/// `pos` (document px) moved to the nearest tick mark the ruler draws: where a guide lands when +/// Shift is held while dragging it out of a ruler, as in Photoshop (#3770). +fn snap_to_tick(app: &PhotocraftApp, xf: &ViewXform, vertical: bool, pos: f64) -> f64 { + // A vertical guide sits at an x position, which the top ruler measures. + let (px_per_unit, step) = ruler_scale(app, xf, !vertical); + let tick = step / 10.0 * px_per_unit; + if tick.is_finite() && tick > 0.0 { (pos / tick).round() * tick } else { pos } +} + /// The number a ruler prints at the tick `v` (in ruler units). Like Photoshop, rulers show the /// distance from the origin, so values left of / above it read without a minus sign. fn ruler_label(v: f64, step: f64, whole: bool) -> String { @@ -292,17 +315,11 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: & let font = egui::FontId::proportional(9.0); let tick = Stroke::new(1.0, t.text_faint); // Preferences › Units & Rulers: labels in the ruler unit (percent of the document's side). - let ur = &app.session.prefs().units_and_rulers; - let (unit, ppi) = (ur.rulers, ur.point_size.per_inch()); - let (dpi, size) = - app.session.active().map_or((72.0, [1.0, 1.0]), |d| (d.doc.resolution_dpi.max(1.0) as f64, [d.doc.size.width as f64, d.doc.size.height as f64])); - let whole = unit == photocraft_engine::prefs::Unit::Pixels; - for (vertical, extent) in [(false, size[0]), (true, size[1])] { + let unit = app.session.prefs().units_and_rulers.rulers; + let whole = unit == Unit::Pixels; + for vertical in [false, true] { let p = p.with_clip_rect(if vertical { left } else { top }); - let px_per_unit = unit.to_px(1.0, dpi, extent, ppi).max(1e-9); - // The top ruler runs along document x, which pixel aspect correction stretches. - let zoom = if vertical { xf.zoom } else { xf.zoom_x() }; - let step = nice_step(60.0 / (zoom as f64 * px_per_unit).max(1e-6), whole); + let (px_per_unit, step) = ruler_scale(app, xf, vertical); let (u0, u1) = if vertical { ruler_range(xf, left.left_top(), left.left_bottom(), 1, px_per_unit) } else { @@ -352,7 +369,8 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: & } if let Some(pos) = resp.interact_pointer_pos().filter(|_| resp.dragged() || resp.drag_started()) { let d = xf.to_doc(pos); - let pos = crate::snap_ui::snap_guide(app, vertical, if vertical { d[0] } else { d[1] }); + let along = if vertical { d[0] } else { d[1] }; + let pos = if ui.input(|i| i.modifiers.shift) { snap_to_tick(app, xf, vertical, along) } else { crate::snap_ui::snap_guide(app, vertical, along) }; app.guide_drag = Some(GuideDrag { vertical, index: None, pos }); } if resp.drag_stopped() @@ -661,4 +679,43 @@ mod tests { assert!(u1 - u0 > 0.0, "flip={flip}: range is empty"); } } + + /// #3770: a guide Shift-dragged out of a ruler lands on the nearest tick the ruler draws + /// (tenths of the labelled step, in the ruler unit); without Shift it lands where it's dropped. + #[test] + fn shift_dragged_guides_land_on_ruler_ticks() { + let full = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max); + let xf = ViewXform { rect, zoom: 1.0, center: [128.0, 64.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let new_app = || { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + app + }; + let drag_guide = |modifiers: egui::Modifiers| { + let builder = egui_kittest::Harness::builder().with_size(vec2(400.0, 300.0)); + let mut h = builder.build_ui_state(move |ui, app| draw_rulers(app, ui, full, &xf), new_app()); + // Out of the left ruler: a vertical guide, dropped at document x = 37. + let (from, to) = (pos2(RULER / 2.0, 100.0), xf.to_screen(37.0, 64.0)); + h.event_modifiers(egui::Event::PointerMoved(from), modifiers); + h.event_modifiers(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers }, modifiers); + h.run_steps(1); + h.event_modifiers(egui::Event::PointerMoved(to), modifiers); + h.run_steps(1); + h.event_modifiers(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers }, modifiers); + h.run_steps(2); + h.state().session.active().unwrap().doc.guides.vertical.clone() + }; + assert_eq!(drag_guide(egui::Modifiers::NONE), vec![37.0]); + // Pixels at 100%: labels every 100 px, ticks every 10 px. + assert_eq!(drag_guide(egui::Modifiers::SHIFT), vec![40.0]); + + let mut app = new_app(); + // At 200% the labels are every 50 px, so the ticks every 5 px. + assert_eq!(snap_to_tick(&app, &ViewXform { zoom: 2.0, ..xf }, true, 37.0), 35.0); + // Percent of each side: ticks every 2.5% of 256 px across, every 5% of 128 px down. + app.run("prefs.set", json!({"path": "unitsAndRulers.rulers", "value": "percent"})).unwrap(); + assert!((snap_to_tick(&app, &xf, true, 37.0) - 38.4).abs() < 1e-9); + assert!((snap_to_tick(&app, &xf, false, 37.0) - 38.4).abs() < 1e-9); + } } diff --git a/docs/ui-parity.md b/docs/ui-parity.md index 1471255196..d168db4dff 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-11 · **Change:** minor (History snapshots) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-11 · **Change:** minor (⇧-dragged guides land on ruler ticks) · **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, @@ -149,6 +149,7 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme | Smart guides (magenta alignment lines) | done | | | Smart guide distance and spacing labels | missing | | | Ruler units px/in/cm/mm/pt/pica/% | done | | +| ⇧-drag a guide out of a ruler: it lands on the nearest ruler tick | done | `rulers.rs` `snap_to_tick` (#3770) | | Status bar follows the ruler unit | partial | #2602 | | Ruler corner and gaps drawn as Photoshop | partial | #2600, #2601, #2551 | | Guides stored as f64 | missing | `Vec` (UI-217-20) | @@ -213,3 +214,4 @@ Paragraph; the rest open as in-app floating windows. | 2026-10-10 | minor | Fixed Crop live resize snapping feedback and outdated-camera rendering, applied ratios immediately to centred inscribed previews, and removed two-point options-bar overflow; added pointer, rendering, dropdown/Enter, Swap/X and scrolling regressions | | 2026-10-11 | minor | Flick panning done: releasing a moving Hand drag (tool, Space or middle button) glides and decays, gated by Enable Flick Panning | | 2026-10-11 | minor | History snapshots: the camera button takes one, snapshot rows return to it as a new state, rename and delete it (#2966) | +| 2026-10-11 | minor | ⇧-drag a guide out of a ruler lands on the nearest ruler tick (#3770) |