Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Free Transform: the Hand tool no longer steals a resize drag (#2302) (#2327)

Max Murphy8587c55c78e8
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 8c1c0efa8..5e5390845 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2551,7 +2551,13 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
crate::color_range_ui::canvas_eyedropper(app, &ctx, p, press);
}
}
- if tool == Tool::Hand && response.dragged() {
+ // The selected Hand yields to an open Free Transform box in this (primary) window: the box
+ // owns the pointer, so the tool behind it cannot steal the resize drag (#2302). The held Hand
+ // (Space) and a middle-button drag still pan, and a secondary window's Hand never stops, as the
+ // box is only drawn and driven on the primary canvas.
+ let transform_owns_pointer = primary && app.ui.transform.is_some() && temporary.is_none() && !middle;
+ let hand_pan = tool == Tool::Hand && !transform_owns_pointer;
+ if hand_pan && response.dragged() {
let d = xf.unmap_vec(response.drag_delta()) / (view.zoom / ppp);
view.center[0] -= d.x;
view.center[1] -= d.y;
@@ -2628,8 +2634,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
{
crate::canvas_tool_menu::open(app, tool, [p.x, p.y]);
}
- // The (temporary) Hand pans above; its gestures never reach the tool underneath.
- if tool == Tool::Hand {
+ // The (temporary) Hand pans above; its gestures never reach the tool underneath. While a
+ // Free Transform box is open the Hand does not pan, so the box gets these events (#2302).
+ if hand_pan {
(buttons.started, buttons.dragged, buttons.stopped) = (false, false, false);
}
// Rotate View: drag around the view centre. Compass clicks are not a rotate drag.
@@ -5341,6 +5348,50 @@ mod tests {
assert!((step.x - 0.5).abs() < 1e-3, "{step:?}");
}
+ #[test]
+ fn a_secondary_canvas_hand_pans_while_a_transform_is_open() {
+ // #2302 review: `app.ui.transform` is global, but only the primary canvas draws and drives
+ // the box. A secondary window's Hand must still pan while the main window transforms.
+ let ctx = egui::Context::default();
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
+ app.run("layer.new.layer", json!({})).unwrap();
+ app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap();
+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
+ app.run("select.deselect", json!({})).unwrap();
+ app.run("view.actualPixels", json!({})).unwrap();
+ app.ui.tool = Tool::Hand;
+ app.sync_views();
+ crate::transform_tool::begin(&mut app, &ctx).expect("a transform starts");
+ let mut view0 = app.ui.views[0].clone();
+ view0.zoom = 1.0;
+ view0.center = [200.0, 150.0];
+ view0.fit_pending = false;
+ view0.fill_pending = false;
+ let center0 = view0.center;
+ let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0));
+ let frame = |app: &mut PhotocraftApp, events: Vec<egui::Event>| -> View {
+ let input = egui::RawInput { screen_rect: Some(rect), events, ..Default::default() };
+ let mut seen = view0.clone();
+ let mut out = ctx.run_ui(input, |ui| {
+ seen = canvas_view(app, ui, 0, rect, view0.clone(), false);
+ });
+ out.textures_delta.clear();
+ seen
+ };
+ let start = Pos2::new(200.0, 150.0);
+ let press =
+ |pos: Pos2, pressed: bool| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE };
+ frame(&mut app, vec![egui::Event::PointerMoved(start)]);
+ frame(&mut app, vec![press(start, true)]);
+ for dx in [10.0, 20.0, 40.0] {
+ frame(&mut app, vec![egui::Event::PointerMoved(start + vec2(dx, 0.0))]);
+ }
+ let dragged = frame(&mut app, vec![egui::Event::PointerMoved(start + vec2(60.0, 0.0))]);
+ frame(&mut app, vec![press(start + vec2(60.0, 0.0), false)]);
+ assert_ne!(dragged.center, center0, "a secondary window's Hand pans while transforming");
+ }
+
#[test]
fn brush_drags_paint_no_stand_in_shape_over_the_canvas() {
// #189: v0.1 drew the stroke being dragged as a foreground-coloured egui polyline as wide
diff --git a/crates/ui-egui/src/transform_undo_tests.rs b/crates/ui-egui/src/transform_undo_tests.rs
index 7e290f890..9b8bcbbd8 100644
--- a/crates/ui-egui/src/transform_undo_tests.rs
+++ b/crates/ui-egui/src/transform_undo_tests.rs
@@ -13,6 +13,11 @@ use crate::state::Tool;
/// A 400 × 300 document with a 100 × 60 red rectangle on its own layer at (100, 100), the Move
/// tool active, at 100 %.
fn harness() -> Harness<'static, PhotocraftApp> {
+ harness_tool(Tool::Move)
+}
+
+/// The same document with a chosen tool selected (e.g. Hand, #2302).
+fn harness_tool(tool: Tool) -> Harness<'static, PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
app.run("layer.new.layer", json!({})).unwrap();
@@ -21,7 +26,7 @@ fn harness() -> Harness<'static, PhotocraftApp> {
app.run("select.deselect", json!({})).unwrap();
app.sync_views();
app.ui.extras.rulers = false;
- app.ui.tool = Tool::Move;
+ app.ui.tool = tool;
let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
let ctx = ui.ctx().clone();
@@ -205,6 +210,43 @@ fn a_press_just_off_a_corner_scales_rather_than_rotates() {
}
}
+/// With the Hand tool selected, a Free Transform drag resizes the box rather than panning the view
+/// away: the open box owns the pointer (#2302).
+#[test]
+fn the_hand_tool_does_not_steal_a_free_transform_drag() {
+ let mut h = harness_tool(Tool::Hand);
+ begin(&mut h);
+ let q0 = quad(&h);
+ let center0 = h.state().ui.views[0].center;
+ drag(&mut h, q0[2], [300.0, 220.0]);
+ let q1 = quad(&h);
+ assert!(!close(q1, q0), "the drag scaled the box, not the view: {q1:?}");
+ assert_eq!(h.state().ui.views[0].center, center0, "the Hand did not pan");
+ key(&mut h, Modifiers::NONE, Key::Enter);
+ assert!(h.state().ui.transform.is_none(), "the transform committed");
+ assert!(width(&h) > 150, "the committed resize applied: {}", width(&h));
+}
+
+/// Holding Space (the temporary Hand, #249) still pans the view while a Free Transform box is open,
+/// rather than resizing it (#2302 review): only the selected Hand yields to the box.
+#[test]
+fn holding_space_still_pans_while_transforming() {
+ let mut h = harness();
+ begin(&mut h);
+ let q0 = quad(&h);
+ let center0 = h.state().ui.views[0].center;
+ let space = |h: &mut Harness<'static, PhotocraftApp>, down: bool| {
+ h.event(egui::Event::Key { key: Key::Space, physical_key: None, pressed: down, repeat: false, modifiers: Modifiers::NONE });
+ h.run_steps(1);
+ };
+ space(&mut h, true);
+ drag(&mut h, q0[2], [300.0, 220.0]);
+ space(&mut h, false);
+ h.run_steps(2);
+ assert!(close(quad(&h), q0), "the space-drag did not touch the box: {:?}", quad(&h));
+ assert_ne!(h.state().ui.views[0].center, center0, "the view panned instead");
+}
+
/// A transform started by dragging a Move-tool control: that first drag is undoable too.
#[test]
fn the_first_drag_from_the_move_tools_controls_undoes() {