diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 9d90f445ec..ba7b747b0f 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -269,6 +269,10 @@ mod layer_edges_tests; #[path = "canvas/move_readout_arrows_tests.rs"] mod move_readout_arrows_tests; +#[cfg(test)] +#[path = "canvas/move_reference_point_drag_tests.rs"] +mod move_reference_point_drag_tests; + #[cfg(test)] mod live_frame_tests; diff --git a/crates/ui-egui/src/canvas/move_reference_point_drag_tests.rs b/crates/ui-egui/src/canvas/move_reference_point_drag_tests.rs new file mode 100644 index 0000000000..854bab53db --- /dev/null +++ b/crates/ui-egui/src/canvas/move_reference_point_drag_tests.rs @@ -0,0 +1,100 @@ +//! Move › Show Transform Controls: a drag from the box's centre moves the reference point in a Free Transform session +//! instead of the layer (#18161). + +use egui::Modifiers; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::testing::{key, move_to, press, release}; +use crate::state::Tool; + +/// A 400 × 300 document at 100 % with one layer filled at (100..300, 100..220), so the Move box's centre is (200, 160), +/// the Move tool active with Show Transform Controls `controls`. +fn fixture(controls: bool) -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.ui.views[0].zoom = 1.0; + app.ui.views[0].clear_pending(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 120})).unwrap(); + app.run("edit.fill", json!({"color": "#0000ff"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + app.ui.tool_options.move_auto_select = false; + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = controls; + app +} + +fn layer_bounds(app: &PhotocraftApp) -> Rect { + let st = app.session.active().unwrap(); + photocraft_engine::transform_cmds::transform_bounds(&st.doc, st.doc.layer(st.active_layer.unwrap()).unwrap()) +} + +fn steps(app: &PhotocraftApp) -> usize { + app.session.active().unwrap().history.past_len() +} + +fn near(a: [f64; 2], b: [f64; 2], within: f64) -> bool { + (a[0] - b[0]).abs() <= within && (a[1] - b[1]).abs() <= within +} + +/// Drags from `from` to `to` and checks the drag moved the reference point of a Free Transform session on `rect`, with +/// the box, the layer and the history as they were; Escape then closes the session with nothing changed. +fn assert_reference_point_drag(app: &mut PhotocraftApp, rect: [f64; 4], from: [f64; 2], to: [f64; 2]) { + let before = app.session.active().unwrap().doc.clone(); + let (bounds, history) = (layer_bounds(app), steps(app)); + press(app, from, Modifiers::NONE); + move_to(app, [(from[0] + to[0]) / 2.0, (from[1] + to[1]) / 2.0], Modifiers::NONE); + move_to(app, to, Modifiers::NONE); + let session = &app.ui.transform.as_ref().expect("a centre drag opens Free Transform").session; + let r = session.rect(); + assert_eq!([r.x0, r.y0, r.x1, r.y1], rect, "the session frames what the Move box framed"); + let pivot = [session.pivot().x, session.pivot().y]; + assert!(near(pivot, to, 1.0), "the reference point follows the pointer: {pivot:?}"); + let quad = session.quad().map(|p| [p.x, p.y]); + assert_eq!(quad, [[rect[0], rect[1]], [rect[2], rect[1]], [rect[2], rect[3]], [rect[0], rect[3]]], "the box stays put"); + release(app, to, Modifiers::NONE); + assert!(app.ui.transform.is_some(), "the release ends the drag, not the session"); + assert_eq!(layer_bounds(app), bounds, "the layer does not move"); + key(app, egui::Key::Escape, Modifiers::NONE); + assert!(app.ui.transform.is_none(), "Escape cancels Free Transform"); + assert_eq!(app.session.active().unwrap().doc, before, "nothing changed"); + assert_eq!(steps(app), history, "no history step"); +} + +#[test] +fn a_drag_from_the_box_centre_moves_the_reference_point_not_the_layer() { + let mut app = fixture(true); + // A few pixels off the exact centre still grabs the point. + assert_reference_point_drag(&mut app, [100.0, 100.0, 300.0, 220.0], [202.0, 161.0], [250.0, 190.0]); +} + +/// With a selection the box is the selection's bounds, and its centre is the point to drag. +#[test] +fn a_drag_from_the_selection_box_centre_moves_the_reference_point() { + let mut app = fixture(true); + app.run("select.rect", json!({"x": 60, "y": 80, "width": 100, "height": 120})).unwrap(); + assert_reference_point_drag(&mut app, [60.0, 80.0, 160.0, 200.0], [110.0, 140.0], [150.0, 120.0]); +} + +/// A drag that starts inside the box away from its centre, or anywhere with the controls off, moves the layer. +#[test] +fn off_centre_or_without_controls_the_drag_still_moves_the_layer() { + for (controls, from) in [(true, [130.0, 120.0]), (false, [200.0, 160.0])] { + let mut app = fixture(controls); + let (bounds, history) = (layer_bounds(&app), steps(&app)); + let to = [from[0] + 20.0, from[1] + 10.0]; + press(&mut app, from, Modifiers::NONE); + move_to(&mut app, [from[0] + 10.0, from[1] + 5.0], Modifiers::NONE); + move_to(&mut app, to, Modifiers::NONE); + release(&mut app, to, Modifiers::NONE); + let what = format!("controls {controls}, press {from:?}"); + assert!(app.ui.transform.is_none(), "{what}: no Free Transform"); + assert_eq!(layer_bounds(&app), bounds.translate(20, 10), "{what}: the layer moves with the drag"); + assert_eq!(steps(&app), history + 1, "{what}: one Move step"); + } +} diff --git a/crates/ui-egui/src/canvas/transform_controls.rs b/crates/ui-egui/src/canvas/transform_controls.rs index ebd8c34b26..cfd75b6bea 100644 --- a/crates/ui-egui/src/canvas/transform_controls.rs +++ b/crates/ui-egui/src/canvas/transform_controls.rs @@ -2,7 +2,7 @@ use super::PhotocraftApp; use super::tools::selection::floating_outline; use crate::state::Tool; use craft::behavior::EditEvent; -use craft::behavior::geometry::quad::{BoxRules, BoxState, TransformMode}; +use craft::behavior::geometry::quad::{BoxPart, BoxRules, BoxState, TransformMode}; use craft::behavior::kurbo::{self, BezPath}; use craft::draw::ui::transform::{RotateZone, TransformBox, TransformEdit}; use craft::ui::contract::{Bind, Props as _, Show as _}; @@ -137,7 +137,8 @@ pub(crate) fn begin_controls(app: &mut PhotocraftApp, ctx: &egui::Context) -> Co const CONTROLS_LABEL: &str = "Transform controls"; /// Move › Show Transform Controls over the canvas body `rect`: a `TransformBox` on [`frame`] whose inside and outside -/// (past the corner rotate bands) and arrow keys stay the Move tool's. Its first `Begin` runs [`begin_controls`]; once +/// (past the corner rotate bands) and arrow keys stay the Move tool's. Its centre is the reference point: a drag there +/// moves the point in a Free Transform session, not the layer (measured, #18161). Its first `Begin` runs [`begin_controls`]; once /// a session starts, this pass's box events go to the session, and from the next pass the session's /// `TransformOverlay`, salted with the same [`BOX_ID_SALT`](crate::transform_tool::BOX_ID_SALT), carries the drag on. pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: egui::Rect, session_box_shown: bool) { @@ -152,7 +153,7 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: egui::Rect, let out = TransformBox::new(CONTROLS_LABEL, Bind::given(BoxState::from_rect(start)), start, 0.0) .rules(rules) .rotate_zone(RotateZone::Band) - .reference_point(false) + .reference_point(true) .inside_moves(false) .session_arrows(false) .id_salt(crate::transform_tool::BOX_ID_SALT) @@ -165,6 +166,11 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: egui::Rect, } let now = ui.input(|i| i.time); if let Some(t) = app.ui.transform.as_mut() { + // The session's box hides its reference point unless the setting shows it, and a hidden point takes no part + // of the press: show it, so the session's box carries a drag that began on the centre (#18161). + if out.held == Some(BoxPart::Pivot) { + t.session.apply(TransformEdit::ReferencePoint(true), now); + } for event in out.edit.events.iter().cloned() { t.session.apply(TransformEdit::Box(event), now); }