From 433a02695c1bd072dae72f202e96671557c7e988 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 11:13:18 -0600 Subject: [PATCH 1/3] Free Transform: relative positioning toggle between X and Y (#11679) --- crates/ui-egui/src/lib.rs | 3 + crates/ui-egui/src/state/tool_options.rs | 5 + crates/ui-egui/src/tools/tool_reset.rs | 3 +- .../src/tools/transform_tool/options_bar.rs | 69 ++++++++- .../ui-egui/src/tools/transform_tool/tests.rs | 2 +- .../src/transform_relative_position_tests.rs | 138 ++++++++++++++++++ 6 files changed, 213 insertions(+), 7 deletions(-) create mode 100644 crates/ui-egui/src/transform_relative_position_tests.rs diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 81b981fa55..2229e7ae30 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -855,6 +855,9 @@ mod transform_type_tests; #[cfg(test)] mod transform_reference_point_tests; +#[cfg(test)] +mod transform_relative_position_tests; + #[cfg(test)] mod transform_several_tests; diff --git a/crates/ui-egui/src/state/tool_options.rs b/crates/ui-egui/src/state/tool_options.rs index 228d8bac38..573e9ce7f1 100644 --- a/crates/ui-egui/src/state/tool_options.rs +++ b/crates/ui-egui/src/state/tool_options.rs @@ -168,6 +168,10 @@ pub struct ToolOptions { /// transforms still turn and scale about it (#11098). #[serde(default)] pub transform_reference_point: bool, + /// Transform options bar: X and Y show the reference point's offset from where the box started + /// instead of its position (Photoshop's relative positioning, off by default, #11679). + #[serde(default)] + pub transform_relative_position: bool, /// Last interpolation selected in the Transform options bar; unset follows General's default. #[serde(default)] pub transform_interpolation: Option, @@ -478,6 +482,7 @@ impl Default for ToolOptions { move_expand_group_on_click: true, move_show_layers_hover_bounds: true, transform_reference_point: false, + transform_relative_position: false, transform_interpolation: None, crop_ratio: String::new(), crop_custom_ratio_width: 0.0, diff --git a/crates/ui-egui/src/tools/tool_reset.rs b/crates/ui-egui/src/tools/tool_reset.rs index 6d8fac5bbd..dc6b1c85cd 100644 --- a/crates/ui-egui/src/tools/tool_reset.rs +++ b/crates/ui-egui/src/tools/tool_reset.rs @@ -171,7 +171,8 @@ pub fn option_keys(tool: Tool) -> &'static [&'static str] { move_expand_group_on_click, move_show_layers_hover_bounds, transform_interpolation, - transform_reference_point + transform_reference_point, + transform_relative_position ), Tool::RectMarquee | Tool::EllipseMarquee => reset!(feather, anti_alias, marquee_style, marquee_width, marquee_height, marquee_unit), // The single-pixel marquees are never anti-aliased: Feather is their only option. diff --git a/crates/ui-egui/src/tools/transform_tool/options_bar.rs b/crates/ui-egui/src/tools/transform_tool/options_bar.rs index 19b46634e6..a9f642c5fe 100644 --- a/crates/ui-egui/src/tools/transform_tool/options_bar.rs +++ b/crates/ui-egui/src/tools/transform_tool/options_bar.rs @@ -270,6 +270,64 @@ fn reference_point_toggle(ui: &mut egui::Ui, on: &mut bool) -> bool { *on } +/// Where the reference point was when the session opened: the point of the original box that the +/// box's transform has carried to the reference point now. A box that only scales or turns about +/// its reference point leaves it in place; a moved box carries it along. +fn reference_point_origin(rect: [f64; 4], quad: [[f64; 2]; 4], pivot: [f64; 2]) -> [f64; 2] { + if let Some(h) = Homography::rect_to_quad(rect, quad).and_then(|h| h.inverse()) { + let (x, y) = h.apply(pivot[0], pivot[1]); + if x.is_finite() && y.is_finite() { + return [x, y]; + } + } + // A flattened box has no inverse: count how far its top-left corner moved. + [pivot[0] - (quad[0][0] - rect[0]), pivot[1] - (quad[0][1] - rect[1])] +} + +fn shown_at(rect: [f64; 4], quad: [[f64; 2]; 4], pivot: [f64; 2], relative: bool) -> [f64; 2] { + if relative { + let o = reference_point_origin(rect, quad, pivot); + [pivot[0] - o[0], pivot[1] - o[1]] + } else { + pivot + } +} + +/// X and Y as the options bar shows them: the reference point's position, or with relative +/// positioning on (#11679) its offset from where it was when the session opened. +pub(crate) fn position_shown(t: &TransformSession, relative: bool) -> [f64; 2] { + shown_at(t.rect, t.quad, t.pivot, relative) +} + +/// Moves the box (and its reference point) so X and Y read `xy` (see [`position_shown`]). +pub(crate) fn set_position(s: &mut TransformSession, relative: bool, xy: [f64; 2]) { + let now = position_shown(s, relative); + let (dx, dy) = (xy[0] - now[0], xy[1] - now[1]); + s.quad = s.quad.map(|q| [q[0] + dx, q[1] + dy]); + s.pivot = [s.pivot[0] + dx, s.pivot[1] + dy]; +} + +/// Use Relative Positioning for Reference Point: the triangle button Photoshop puts between X and +/// Y (#11679). On, X and Y are the reference point's offset from where the box started. +fn relative_position_toggle(ui: &mut egui::Ui, on: &mut bool) { + let tk = crate::theme::Tokens::get(ui.ctx()); + let (rect, resp) = ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::click()); + if resp.clicked() { + *on = !*on; + } + let painter = ui.painter(); + if *on || resp.hovered() { + painter.rect_filled(rect, 3.0, if *on { tk.accent.gamma_multiply(0.35) } else { tk.card_border.gamma_multiply(0.5) }); + } + let c = rect.center(); + let tri = vec![egui::pos2(c.x, c.y - 4.5), egui::pos2(c.x + 5.0, c.y + 4.0), egui::pos2(c.x - 5.0, c.y + 4.0)]; + let color = if *on { tk.accent } else { tk.text_dim }; + painter.add(egui::Shape::closed_line(tri, egui::Stroke::new(1.2, color))); + let label = tl!("Use relative positioning for reference point"); + resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, *on, label)); + resp.on_hover_text(label); +} + fn anchor_grid_widget(ui: &mut egui::Ui, current_anchor: Option, enabled: bool) -> Option { let size = egui::vec2(16.0, 16.0); // Greyed and inert while Toggle Reference Point is off, as in Photoshop (#11098). @@ -314,18 +372,19 @@ pub(crate) fn position_size_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, t pivot = new_p; } ui.add_space(4.0); - let mut px = pivot[0] as f32; - let mut py = pivot[1] as f32; + let relative = app.ui.tool_options.transform_relative_position; + let shown = shown_at(t.rect, t.quad, pivot, relative); + let mut px = shown[0] as f32; + let mut py = shown[1] as f32; lbl(ui, "X:"); let rx = crate::widgets::value_field(ui, &mut px, -POSITION_LIMIT_PX..=POSITION_LIMIT_PX, "px", 72.0).accessible_name("X"); + relative_position_toggle(ui, &mut app.ui.tool_options.transform_relative_position); lbl(ui, "Y:"); let ry = crate::widgets::value_field(ui, &mut py, -POSITION_LIMIT_PX..=POSITION_LIMIT_PX, "px", 72.0).accessible_name("Y"); if (rx.changed() || ry.changed()) && let Some(s) = app.ui.transform.as_mut() { - let (dx, dy) = (px as f64 - s.pivot[0], py as f64 - s.pivot[1]); - s.quad = s.quad.map(|q| [q[0] + dx, q[1] + dy]); - s.pivot = [px as f64, py as f64]; + set_position(s, relative, [px as f64, py as f64]); } crate::widgets::vline(ui, 22.0); // W and H in the unit picked from their right-click menu: the scale in percent, or the box's diff --git a/crates/ui-egui/src/tools/transform_tool/tests.rs b/crates/ui-egui/src/tools/transform_tool/tests.rs index 3721fdeb2a..96ef000f36 100644 --- a/crates/ui-egui/src/tools/transform_tool/tests.rs +++ b/crates/ui-egui/src/tools/transform_tool/tests.rs @@ -329,7 +329,7 @@ fn bar_harness(setup: impl FnOnce(&mut PhotocraftApp)) -> egui_kittest::Harness< let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); app.ui.transform = Some(session()); setup(&mut app); - let mut h = Harness::builder().with_size(vec2(900.0, 360.0)).build_ui_state( + let mut h = Harness::builder().with_size(vec2(1000.0, 360.0)).build_ui_state( |ui, app: &mut PhotocraftApp| { if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { return; diff --git a/crates/ui-egui/src/transform_relative_position_tests.rs b/crates/ui-egui/src/transform_relative_position_tests.rs new file mode 100644 index 0000000000..da6f6a7bf4 --- /dev/null +++ b/crates/ui-egui/src/transform_relative_position_tests.rs @@ -0,0 +1,138 @@ +//! Relative positioning in the transform options bar (#11679): a toggle between X and Y, off by +//! default; on, X and Y show the reference point's offset from where the box started and a typed +//! value moves the box by that much; off, they show its position again. + +use egui::accesskit::Role; +use egui::vec2; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::ToolEvent; + +const TOGGLE: &str = "Use relative positioning for reference point"; + +/// A 200 x 120 document with a layer filled at 40,20 (60 x 40) and Free Transform open on it: +/// the reference point starts at the box's centre, 70,40. +fn transforming() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 120})).unwrap(); + app.sync_views(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 40, "y": 20, "width": 60, "height": 40})).unwrap(); + app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + crate::transform_tool::begin(&mut app, &egui::Context::default()).unwrap(); + app +} + +fn harness(app: PhotocraftApp) -> Harness<'static, PhotocraftApp> { + let mut h = Harness::builder().with_size(vec2(1600.0, 200.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::panels::options_bar(app, ui); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio); + h.run_steps(4); + h +} + +/// What the X and Y fields show. +fn shown(h: &Harness<'_, PhotocraftApp>) -> (f64, f64) { + let read = |name: &str| h.get_by_role_and_label(Role::SpinButton, name).accesskit_node().numeric_value().unwrap(); + (read("X"), read("Y")) +} + +/// Types `text` into the X field and commits it with Tab, as a user does. +fn type_x(h: &mut Harness<'_, PhotocraftApp>, text: &str) { + h.get_by_role_and_label(Role::SpinButton, "X").click(); + h.run_steps(1); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.run_steps(1); + for ch in text.chars() { + h.event(egui::Event::Text(ch.to_string())); + h.run_steps(1); + } + h.key_press(egui::Key::Tab); + h.run_steps(3); +} + +fn click_toggle(h: &mut Harness<'_, PhotocraftApp>) { + h.get_by_label(TOGGLE).click(); + h.run_steps(3); +} + +fn quad(h: &Harness<'_, PhotocraftApp>) -> [[f64; 2]; 4] { + h.state().ui.transform.as_ref().unwrap().quad +} + +fn moved(q: [[f64; 2]; 4], dx: f64) -> [[f64; 2]; 4] { + q.map(|[x, y]| [x + dx, y]) +} + +fn same(a: [[f64; 2]; 4], b: [[f64; 2]; 4]) -> bool { + a.iter().flatten().zip(b.iter().flatten()).all(|(a, b)| (a - b).abs() < 1e-6) +} + +#[test] +fn relative_positioning_is_off_by_default() { + let app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + assert!(!app.ui.tool_options.transform_relative_position); + // An older preferences file without the field reads it as off. + let o: crate::state::ToolOptions = serde_json::from_value(json!({})).unwrap(); + assert!(!o.transform_relative_position); +} + +#[test] +fn the_toggle_sits_between_x_and_y_and_turns_a_fresh_box_into_zero() { + let mut h = harness(transforming()); + let x = h.get_by_role_and_label(Role::SpinButton, "X").rect(); + let y = h.get_by_role_and_label(Role::SpinButton, "Y").rect(); + let toggle = h.get_by_label(TOGGLE).rect(); + assert!(x.max.x <= toggle.min.x && toggle.max.x <= y.min.x, "X {x:?}, toggle {toggle:?}, Y {y:?}"); + assert_eq!(shown(&h), (70.0, 40.0), "off: the reference point's position"); + click_toggle(&mut h); + assert!(h.state().ui.tool_options.transform_relative_position); + assert_eq!(shown(&h), (0.0, 0.0), "on: no offset yet"); +} + +#[test] +fn a_typed_x_moves_the_box_by_that_much_and_commits_as_free_transform() { + let mut h = harness(transforming()); + click_toggle(&mut h); + let before = quad(&h); + type_x(&mut h, "20"); + assert!(same(quad(&h), moved(before, 20.0)), "the box moved 20 px right: {:?}", quad(&h)); + assert_eq!(shown(&h), (20.0, 0.0)); + // Typing the same offset again doesn't move it further. + type_x(&mut h, "20"); + assert!(same(quad(&h), moved(before, 20.0)), "still 20 px: {:?}", quad(&h)); + let app = h.state_mut(); + crate::transform_tool::commit(app); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds().x0, 60, "the layer moved 20 px"); + assert_eq!(st.history.entries().last().map(ToString::to_string).as_deref(), Some("Free Transform")); +} + +#[test] +fn a_drag_updates_the_offset_and_turning_it_off_shows_the_position_again() { + let mut h = harness(transforming()); + click_toggle(&mut h); + { + let app = h.state_mut(); + let none = egui::Modifiers::NONE; + crate::transform_tool::pointer(app, ToolEvent::Down { x: 50.0, y: 30.0, pressure: 1.0 }, none); + crate::transform_tool::pointer(app, ToolEvent::Move { x: 55.0, y: 33.0, pressure: 1.0 }, none); + crate::transform_tool::pointer(app, ToolEvent::Up { x: 55.0, y: 33.0 }, none); + } + h.run_steps(3); + assert_eq!(shown(&h), (5.0, 3.0), "the offset follows the drag"); + click_toggle(&mut h); + assert!(!h.state().ui.tool_options.transform_relative_position); + assert_eq!(shown(&h), (75.0, 43.0), "off again: the reference point's position"); +} From 4b3eb2ffd1164997f90fc64ef805e88551a6d106 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 12:39:37 -0600 Subject: [PATCH 2/3] Free Transform: typing X or Y leaves the other axis at full precision (CodeRabbit review on options_bar.rs:308) Co-Authored-By: Claude Opus 5.5 --- .../src/tools/transform_tool/options_bar.rs | 6 +++++- .../src/transform_relative_position_tests.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/tools/transform_tool/options_bar.rs b/crates/ui-egui/src/tools/transform_tool/options_bar.rs index a9f642c5fe..5789d8df02 100644 --- a/crates/ui-egui/src/tools/transform_tool/options_bar.rs +++ b/crates/ui-egui/src/tools/transform_tool/options_bar.rs @@ -384,7 +384,11 @@ pub(crate) fn position_size_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, t if (rx.changed() || ry.changed()) && let Some(s) = app.ui.transform.as_mut() { - set_position(s, relative, [px as f64, py as f64]); + // An axis the user didn't edit keeps its full-precision value: the field's f32 would + // nudge the box by its rounding. + let x = if rx.changed() { f64::from(px) } else { shown[0] }; + let y = if ry.changed() { f64::from(py) } else { shown[1] }; + set_position(s, relative, [x, y]); } crate::widgets::vline(ui, 22.0); // W and H in the unit picked from their right-click menu: the scale in percent, or the box's diff --git a/crates/ui-egui/src/transform_relative_position_tests.rs b/crates/ui-egui/src/transform_relative_position_tests.rs index da6f6a7bf4..9d7abb13cf 100644 --- a/crates/ui-egui/src/transform_relative_position_tests.rs +++ b/crates/ui-egui/src/transform_relative_position_tests.rs @@ -136,3 +136,21 @@ fn a_drag_updates_the_offset_and_turning_it_off_shows_the_position_again() { assert!(!h.state().ui.tool_options.transform_relative_position); assert_eq!(shown(&h), (75.0, 43.0), "off again: the reference point's position"); } + +/// Typing X leaves Y exactly where it was, even when Y is a fraction the field's f32 can't hold +/// (CodeRabbit review on options_bar.rs:308): the field's rounding must not nudge the box. +#[test] +fn a_typed_x_leaves_a_fractional_y_exactly_where_it_was() { + let mut app = transforming(); + let t = app.ui.transform.as_mut().unwrap(); + t.quad = t.quad.map(|[x, y]| [x, y + 0.1]); + t.pivot[1] += 0.1; + let mut h = harness(app); + let before = quad(&h); + let pivot_y = h.state().ui.transform.as_ref().unwrap().pivot[1]; + type_x(&mut h, "90"); + let after = quad(&h); + assert!(same(after, moved(before, 20.0)), "the box moved 20 px right: {after:?}"); + assert_eq!(after.map(|p| p[1]), before.map(|p| p[1]), "Y is untouched"); + assert_eq!(h.state().ui.transform.as_ref().unwrap().pivot[1], pivot_y); +} From 5252735c97438917b3c56eba60cf1a333bbf7e71 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sat, 7 Nov 2026 13:22:29 -0600 Subject: [PATCH 3/3] Free Transform: a flattened box reads its reference point's own offset (CodeRabbit pre-merge check on #12139) With no inverse map, the offset now comes from the pseudo-inverse of the box map's linear part, not from how far the top-left corner moved. Co-Authored-By: Claude Opus 5.5 --- .../src/tools/transform_tool/options_bar.rs | 27 +++++++++++++++++-- .../src/transform_relative_position_tests.rs | 19 +++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/tools/transform_tool/options_bar.rs b/crates/ui-egui/src/tools/transform_tool/options_bar.rs index 5789d8df02..e9ee695815 100644 --- a/crates/ui-egui/src/tools/transform_tool/options_bar.rs +++ b/crates/ui-egui/src/tools/transform_tool/options_bar.rs @@ -280,8 +280,31 @@ fn reference_point_origin(rect: [f64; 4], quad: [[f64; 2]; 4], pivot: [f64; 2]) return [x, y]; } } - // A flattened box has no inverse: count how far its top-left corner moved. - [pivot[0] - (quad[0][0] - rect[0]), pivot[1] - (quad[0][1] - rect[1])] + // A flattened box has no inverse. Fit the map's linear part about the box's centre and use + // its pseudo-inverse: the collapsed direction adds nothing, so a box squashed about its + // reference point reads no offset and one squashed toward an edge reads how far it moved. + let (w, h) = (rect[2] - rect[0], rect[3] - rect[1]); + let centre = [(rect[0] + rect[2]) / 2.0, (rect[1] + rect[3]) / 2.0]; + let mid = [quad.iter().map(|q| q[0]).sum::() / 4.0, quad.iter().map(|q| q[1]).sum::() / 4.0]; + let d = [pivot[0] - mid[0], pivot[1] - mid[1]]; + if !(w.abs() > f64::EPSILON && h.abs() > f64::EPSILON) { + return [centre[0] + d[0], centre[1] + d[1]]; + } + // Columns: where a unit step along the box's width and height goes. + let ex = [(quad[1][0] - quad[0][0] + quad[2][0] - quad[3][0]) / (2.0 * w), (quad[1][1] - quad[0][1] + quad[2][1] - quad[3][1]) / (2.0 * w)]; + let ey = [(quad[3][0] - quad[0][0] + quad[2][0] - quad[1][0]) / (2.0 * h), (quad[3][1] - quad[0][1] + quad[2][1] - quad[1][1]) / (2.0 * h)]; + let det = ex[0] * ey[1] - ey[0] * ex[1]; + let norm2 = ex[0] * ex[0] + ex[1] * ex[1] + ey[0] * ey[0] + ey[1] * ey[1]; + let back = if det.abs() > 1e-9 * norm2.max(1e-12) { + [(ey[1] * d[0] - ey[0] * d[1]) / det, (ex[0] * d[1] - ex[1] * d[0]) / det] + } else if norm2 > 1e-12 { + // Rank one: the pseudo-inverse is the transpose over the squared norm. + [(ex[0] * d[0] + ex[1] * d[1]) / norm2, (ey[0] * d[0] + ey[1] * d[1]) / norm2] + } else { + [0.0, 0.0] + }; + let o = [centre[0] + back[0], centre[1] + back[1]]; + if o[0].is_finite() && o[1].is_finite() { o } else { pivot } } fn shown_at(rect: [f64; 4], quad: [[f64; 2]; 4], pivot: [f64; 2], relative: bool) -> [f64; 2] { diff --git a/crates/ui-egui/src/transform_relative_position_tests.rs b/crates/ui-egui/src/transform_relative_position_tests.rs index 9d7abb13cf..bd6ed4d21f 100644 --- a/crates/ui-egui/src/transform_relative_position_tests.rs +++ b/crates/ui-egui/src/transform_relative_position_tests.rs @@ -154,3 +154,22 @@ fn a_typed_x_leaves_a_fractional_y_exactly_where_it_was() { assert_eq!(after.map(|p| p[1]), before.map(|p| p[1]), "Y is untouched"); assert_eq!(h.state().ui.transform.as_ref().unwrap().pivot[1], pivot_y); } + +/// A box squashed flat has no inverse map, so the offset comes from the map's pseudo-inverse: a +/// box squashed to zero width about its reference point reads 0 / 0, and one squashed onto its +/// left edge, carrying the reference point with it, reads -30 / 0 (CodeRabbit pre-merge check). +#[test] +fn a_flattened_box_reads_the_reference_points_own_offset() { + let mut app = transforming(); + app.ui.tool_options.transform_relative_position = true; + let t = app.ui.transform.as_mut().unwrap(); + t.quad = [[70.0, 20.0], [70.0, 20.0], [70.0, 60.0], [70.0, 60.0]]; + t.pivot = [70.0, 40.0]; + let mut h = harness(app); + assert_eq!(shown(&h), (0.0, 0.0), "squashed about the reference point"); + let t = h.state_mut().ui.transform.as_mut().unwrap(); + t.quad = [[40.0, 20.0], [40.0, 20.0], [40.0, 60.0], [40.0, 60.0]]; + t.pivot = [40.0, 40.0]; + h.run_steps(2); + assert_eq!(shown(&h), (-30.0, 0.0), "squashed onto the left edge"); +}