From 3cae29abfababf4a97d34ac8a4b46c7eaf5a7a2b Mon Sep 17 00:00:00 2001 From: Nigel Ihza Firdausy <106657905+Ni-zav@users.noreply.github.com> Date: Sat, 10 Oct 2026 02:27:30 +0700 Subject: [PATCH] Grid: percentage spacing and snapping use each canvas axis (#1914) A percentage grid now spaces vertical lines by the document width and horizontal lines by its height, so rectangular canvases get proportional cells, and grid snapping uses the same per-axis steps. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/snap.rs | 38 ++++++++++++++++++++------- crates/ui-egui/src/rulers.rs | 48 +++++++++++++++++++++++++---------- crates/ui-egui/src/snap_ui.rs | 27 +++++++++++++++++--- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/crates/engine/src/snap.rs b/crates/engine/src/snap.rs index 7c1c9a2a756..85745b0395e 100644 --- a/crates/engine/src/snap.rs +++ b/crates/engine/src/snap.rs @@ -59,13 +59,13 @@ pub struct SnapOptions { pub layers: bool, pub document: bool, pub selection: bool, - /// Grid spacing (document px) used when `grid` is on: the subdivision step. - pub grid_step: f64, + /// Grid subdivision spacing in document pixels, independently for x and y. + pub grid_step: [f64; 2], } impl Default for SnapOptions { fn default() -> Self { - Self { guides: true, grid: false, layers: true, document: true, selection: true, grid_step: 18.0 } + Self { guides: true, grid: false, layers: true, document: true, selection: true, grid_step: [18.0; 2] } } } @@ -76,8 +76,8 @@ pub struct SnapTargets { pub x: Vec, /// Horizontal lines (y positions). pub y: Vec, - /// Regular grid step (document px), when snapping to the grid. - pub grid: Option, + /// Grid subdivision spacing on the x and y axes (document px). + pub grid: Option<[f64; 2]>, /// Extent used for grid lines and guides ([x0, y0, x1, y1]). pub extent: [f64; 4], } @@ -113,7 +113,7 @@ impl SnapTargets { t.y.push(Target { pos: *g as f64, kind: SnapKind::Guide, span: (0.0, w) }); } } - if opts.grid && opts.grid_step > 0.0 { + if opts.grid && opts.grid_step.iter().all(|step| step.is_finite() && *step > 0.0) { t.grid = Some(opts.grid_step); } if opts.layers { @@ -185,7 +185,8 @@ impl SnapTargets { } }; list.iter().copied().for_each(&mut consider); - if let Some(step) = self.grid { + if let Some(steps) = self.grid { + let step = steps[usize::from(!vertical)]; let g = (v / step).round() * step; let span = if vertical { (self.extent[1], self.extent[3]) } else { (self.extent[0], self.extent[2]) }; consider(Target { pos: g, kind: SnapKind::Grid, span }); @@ -234,7 +235,12 @@ impl SnapTargets { let list = if vertical { &self.x } else { &self.y }; let hit = list.iter().copied().filter(|t| (t.pos - moved).abs() < 1e-6).min_by_key(|t| u8::from(t.kind != SnapKind::Guide)); let hit = hit.or_else(|| { - self.grid.filter(|s| ((moved / s).round() * s - moved).abs() < 1e-6).map(|_| Target { pos: moved, kind: SnapKind::Grid, span: other }) + self.grid + .filter(|steps| { + let step = steps[axis]; + ((moved / step).round() * step - moved).abs() < 1e-6 + }) + .map(|_| Target { pos: moved, kind: SnapKind::Grid, span: other }) }); if let Some(t) = hit { lines.push(line(vertical, t, other.0, other.1)); @@ -297,11 +303,25 @@ mod tests { #[test] fn grid_snaps_to_nearest_line() { - let t = SnapTargets { grid: Some(25.0), extent: [0.0, 0.0, 100.0, 100.0], ..Default::default() }; + let t = SnapTargets { grid: Some([25.0; 2]), extent: [0.0, 0.0, 100.0, 100.0], ..Default::default() }; assert_eq!(t.snap_point([48.0, 61.0], 3.0).0, [50.0, 61.0]); assert_eq!(t.nearest(false, 74.0, 3.0).unwrap().kind, SnapKind::Grid); } + #[test] + fn rectangular_grid_snaps_independently_on_both_axes() { + // A 1280×720 document, 20% major spacing and two subdivisions: + // vertical lines every 128 px, horizontal lines every 72 px. + let t = SnapTargets { grid: Some([128.0, 72.0]), extent: [0.0, 0.0, 1280.0, 720.0], ..Default::default() }; + assert_eq!(t.snap_point([127.0, 71.0], 2.0).0, [128.0, 72.0]); + assert_eq!(t.nearest(true, 255.0, 2.0).map(|t| t.pos), Some(256.0)); + assert_eq!(t.nearest(false, 143.0, 2.0).map(|t| t.pos), Some(144.0)); + let (delta, lines) = t.snap_rect([126.0, 70.0, 160.0, 100.0], 3.0); + assert_eq!(delta, [2.0, 2.0]); + assert!(lines.iter().any(|l| l.vertical && l.pos == 128.0 && l.kind == SnapKind::Grid)); + assert!(lines.iter().any(|l| !l.vertical && l.pos == 72.0 && l.kind == SnapKind::Grid)); + } + #[test] fn rect_snaps_by_closest_anchor() { let t = targets(); diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs index 46dde0c5382..84b5cfa4aa7 100644 --- a/crates/ui-egui/src/rulers.rs +++ b/crates/ui-egui/src/rulers.rs @@ -4,7 +4,7 @@ use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, pos2, vec2}; use photocraft_doc::Document; -use photocraft_engine::prefs::Unit; +use photocraft_engine::prefs::{GuidesGridAndSlices, Unit}; use serde_json::json; use crate::PhotocraftApp; @@ -106,29 +106,39 @@ fn tick_step(zoom: f32) -> f64 { nice_step(60.0 / zoom.max(1e-4) as f64, true) } +/// Grid spacing in document pixels. Percentages use each axis's own extent. +pub(crate) fn grid_major_px(g: &GuidesGridAndSlices, dpi: f64, size: [f64; 2], ppi: f64) -> [f64; 2] { + size.map(|extent| g.major_px(dpi, extent, ppi)) +} + /// View › Show › Grid, from Preferences › Guides, Grid & Slices (spacing, subdivisions, colour, /// style). Photoshop's default is a gridline every inch with 4 subdivisions. pub fn draw_grid(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, doc: &Document) { let g = &app.session.prefs().guides_grid_and_slices; let ppi = app.session.prefs().units_and_rulers.point_size.per_inch(); - let major = g.major_px(doc.resolution_dpi.max(1.0) as f64, doc.size.width as f64, ppi); - let minor = major / g.subdivisions.max(1) as f64; - let base = pref_color(&g.grid_color, Color32::from_gray(140)); let (w, h) = (doc.size.width as f64, doc.size.height as f64); - for (step, alpha) in [(minor, 0.45f32), (major, 1.0)] { - if step * (xf.zoom as f64) < 6.0 || (alpha < 1.0 && g.subdivisions <= 1) { + let major = grid_major_px(g, doc.resolution_dpi.max(1.0) as f64, [w, h], ppi); + let minor = major.map(|step| step / g.subdivisions.max(1) as f64); + let base = pref_color(&g.grid_color, Color32::from_gray(140)); + for (steps, alpha) in [(minor, 0.45f32), (major, 1.0)] { + if alpha < 1.0 && g.subdivisions <= 1 { continue; } let st = Stroke::new(1.0, base.gamma_multiply(alpha)); - let mut x = step; - while x < w { - styled_line(painter, xf.to_screen(x as f32, 0.0), xf.to_screen(x as f32, h as f32), st, g.grid_style); - x += step; + let [step_x, step_y] = steps; + if step_x * (xf.zoom as f64) >= 6.0 { + let mut x = step_x; + while x < w { + styled_line(painter, xf.to_screen(x as f32, 0.0), xf.to_screen(x as f32, h as f32), st, g.grid_style); + x += step_x; + } } - let mut y = step; - while y < h { - styled_line(painter, xf.to_screen(0.0, y as f32), xf.to_screen(w as f32, y as f32), st, g.grid_style); - y += step; + if step_y * (xf.zoom as f64) >= 6.0 { + let mut y = step_y; + while y < h { + styled_line(painter, xf.to_screen(0.0, y as f32), xf.to_screen(w as f32, y as f32), st, g.grid_style); + y += step_y; + } } } } @@ -313,6 +323,16 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: & mod tests { use super::*; + #[test] + fn percentage_grid_spacing_uses_both_canvas_dimensions() { + let mut g = GuidesGridAndSlices { grid_unit: Unit::Percent, gridline_every: 20.0, subdivisions: 2, ..Default::default() }; + assert_eq!(grid_major_px(&g, 72.0, [1280.0, 720.0], 72.0), [256.0, 144.0]); + // Absolute units remain equal on both axes, irrespective of aspect ratio. + g.grid_unit = Unit::Pixels; + g.gridline_every = 100.0; + assert_eq!(grid_major_px(&g, 72.0, [1280.0, 720.0], 72.0), [100.0, 100.0]); + } + #[test] fn unit_steps_allow_fractions() { assert_eq!(nice_step(0.3, false), 0.5); diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs index d33a5e0635b..07c843a6fae 100644 --- a/crates/ui-egui/src/snap_ui.rs +++ b/crates/ui-egui/src/snap_ui.rs @@ -45,10 +45,11 @@ pub struct ActiveSnap { pub fn options(app: &PhotocraftApp) -> SnapOptions { let st = &app.ui.view.snap_to; let e = &app.ui.extras; - let grid_step = app.session.active().map_or(18.0, |d| { + let grid_step = app.session.active().map_or([18.0; 2], |d| { let g = &app.session.prefs().guides_grid_and_slices; let ppi = app.session.prefs().units_and_rulers.point_size.per_inch(); - g.major_px(d.doc.resolution_dpi as f64, d.doc.size.width as f64, ppi) / g.subdivisions.max(1) as f64 + crate::rulers::grid_major_px(g, d.doc.resolution_dpi.max(1.0) as f64, [d.doc.size.width as f64, d.doc.size.height as f64], ppi) + .map(|step| step / g.subdivisions.max(1) as f64) }); SnapOptions { guides: st.guides && e.guides, grid: st.grid && e.grid, layers: st.layers, document: st.document_bounds, selection: true, grid_step } } @@ -69,7 +70,8 @@ fn snap_on(app: &PhotocraftApp) -> bool { /// Targets of the active document (smart = layer alignments only). fn build(app: &PhotocraftApp, exclude: &[LayerId], smart: bool) -> SnapTargets { let Some(st) = app.session.active() else { return SnapTargets::default() }; - let opts = if smart { SnapOptions { guides: false, grid: false, layers: true, document: true, selection: false, grid_step: 0.0 } } else { options(app) }; + let opts = + if smart { SnapOptions { guides: false, grid: false, layers: true, document: true, selection: false, grid_step: [0.0; 2] } } else { options(app) }; let t = SnapTargets::from_document(&st.doc, &opts, exclude); if smart { t.filtered(SnapKind::is_smart) } else { t } } @@ -456,6 +458,23 @@ mod tests { assert_eq!(snap_guide(&mut app, true, 297.0), 297.0); } + #[test] + fn percentage_grid_snaps_at_separate_axis_intervals() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 1280, "height": 720})).unwrap(); + app.ui.extras.grid = true; + app.ui.view.snap_to.grid = true; + app.run( + "prefs.set", + json!({"values": {"guidesGridAndSlices.gridlineEvery": 20, "guidesGridAndSlices.gridUnit": "percent", "guidesGridAndSlices.subdivisions": 2}}), + ) + .unwrap(); + let opts = options(&app); + assert_eq!(opts.grid_step, [128.0, 72.0]); + let targets = SnapTargets::from_document(&app.session.active().unwrap().doc, &opts, &[]); + assert_eq!(targets.snap_point([127.0, 71.0], 2.0).0, [128.0, 72.0]); + } + #[test] fn grid_snapping_uses_preferences() { let mut app = app_with_box(); @@ -465,7 +484,7 @@ mod tests { json!({"values": {"guidesGridAndSlices.gridlineEvery": 100, "guidesGridAndSlices.gridUnit": "pixels", "guidesGridAndSlices.subdivisions": 4}}), ) .unwrap(); - assert_eq!(options(&app).grid_step, 25.0); + assert_eq!(options(&app).grid_step, [25.0; 2]); app.ui.tool = Tool::RectMarquee; let m = egui::Modifiers::NONE; crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 152.0, y: 127.0, pressure: 1.0 }, m);