Skip to content

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

Ctrl+Space and Alt+Space zoom the image under an open dialog (fixes #6460)

Marco Arnulfo3c63b2007171
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 57d357511..4afedd01d 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3353,7 +3353,23 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
let shadow_drag =
!picking && app.ui.dialogs.last().is_some_and(|d| d.kind == crate::state::DialogKind::LayerStyle && crate::layer_style::drags_shadow(&d.fields));
let hand = app.ui.tool == Tool::Hand && !picking && !range_picking && !refine_brushing && !pinning && !shadow_drag;
- if let Some(d) = crate::dialogs::pan_delta(&ctx, rect, hand) {
+ // ⌘Space / ⌥Space zoom under the dialog as they do without one, before Space pans (#6460).
+ let held_zoom = !middle && matches!(temporary, Some(crate::hold_keys::Temporary::ZoomIn | crate::hold_keys::Temporary::ZoomOut));
+ if held_zoom {
+ let alt = ctx.input(|i| i.modifiers.alt);
+ if free_hover {
+ ctx.set_cursor_icon(crate::tool_cursor::zoom(&ctx, zoom_out(alt)));
+ }
+ let b = crate::dialogs::free_buttons(&ctx, rect);
+ let pointer = ctx.input(|i| i.pointer.latest_pos());
+ crate::zoom_tool::drag(app, &ctx, &mut view, &xf, &b, pointer);
+ if b.clicked
+ && let Some(p) = pointer
+ {
+ let nz = crate::zoom_levels::step(view.zoom, if zoom_out(alt) { -1 } else { 1 }, view.doc_size);
+ zoom_about(&mut view, &xf, p, nz, app.session.prefs().tools.zoom_clicked_point_to_center, ppp);
+ }
+ } else if let Some(d) = crate::dialogs::pan_delta(&ctx, rect, hand) {
let d = xf.unmap_vec(d) / (view.zoom / ppp);
view.center[0] -= d.x;
view.center[1] -= d.y;
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 013c79e82..7daef4ee2 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -43,6 +43,24 @@ pub fn pan_delta(ctx: &egui::Context, canvas: egui::Rect, hand: bool) -> Option<
(panning && canvas.contains(origin) && !rects.iter().any(|r| r.contains(origin))).then_some(delta)
}
+/// The primary button's drag and click on the free canvas under an open dialog, as the canvas
+/// response would report them if it weren't inert: for the held Zoom (⌘Space / ⌥Space, #6460).
+/// A drag counts from egui's drag threshold, like `drag_started`.
+pub fn free_buttons(ctx: &egui::Context, canvas: egui::Rect) -> crate::paint_mouse::Buttons {
+ let id = egui::Id::new("pc-dialog-free-drag");
+ let was = ctx.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
+ let rects = rects(ctx);
+ let open_popup = egui::Popup::is_any_open(ctx);
+ let free = |p: egui::Pos2| !open_popup && canvas.contains(p) && !rects.iter().any(|r| r.contains(p));
+ let (down, dragging, clicked) = ctx.input(|i| {
+ let p = &i.pointer;
+ let dragging = p.primary_down() && p.is_decidedly_dragging() && (was || p.press_origin().is_some_and(free));
+ (p.primary_down(), dragging, p.primary_clicked() && p.interact_pos().is_some_and(free))
+ });
+ ctx.data_mut(|d| d.insert_temp(id, dragging));
+ crate::paint_mouse::Buttons { started: dragging && !was, dragged: dragging, stopped: was && !down, clicked }
+}
+
/// A click or drag with the primary button started on the free canvas under an open dialog, Space
/// not held: where the pointer is this frame. The press itself counts even when it is released in
/// the same frame (a quick click, `ui.click`); the drag only while it stays on the free canvas.
diff --git a/crates/ui-egui/src/input_tests.rs b/crates/ui-egui/src/input_tests.rs
index 3754e821f..2266c6f3d 100644
--- a/crates/ui-egui/src/input_tests.rs
+++ b/crates/ui-egui/src/input_tests.rs
@@ -194,6 +194,56 @@ fn clicking_outside_a_dialog_keeps_it_open_and_the_canvas_pans_and_zooms() {
assert!(h.state().ui.dialogs.is_empty());
}
+/// ⌘Space (Ctrl+Space off the Mac) and ⌥Space are the temporary Zoom under an open dialog too, as
+/// in Photoshop: a drag on the free canvas is a scrubby zoom about the press point, not a pan, and
+/// a click steps the zoom (#6460).
+#[test]
+fn temporary_zoom_works_under_a_dialog() {
+ let mut h = harness();
+ h.state_mut().ui.extras.rulers = false;
+ open_dialog(&mut h);
+ let p = free_canvas(&h);
+ h.hover_at(p);
+ h.run_steps(1);
+ let (z0, doc0) = (zoom(&h), doc_at(&h, p));
+ let held = |h: &mut Harness<'static, PhotocraftApp>, pressed: bool, m: Modifiers| {
+ h.event(egui::Event::ModifiersChanged(m));
+ h.event(egui::Event::Key { key: Key::Space, physical_key: None, pressed, repeat: false, modifiers: m });
+ h.run_steps(1);
+ };
+ let button = |h: &mut Harness<'static, PhotocraftApp>, pos: Pos2, pressed: bool, m: Modifiers| {
+ h.event_modifiers(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: m }, m);
+ h.run_steps(1);
+ };
+ held(&mut h, true, Modifiers::COMMAND);
+ button(&mut h, p, true, Modifiers::COMMAND);
+ for i in 1..=6 {
+ h.hover_at(p + vec2(10.0 * i as f32, 0.0));
+ h.run_steps(1);
+ }
+ button(&mut h, p + vec2(60.0, 0.0), false, Modifiers::COMMAND);
+ held(&mut h, false, Modifiers::COMMAND);
+ h.event(egui::Event::ModifiersChanged(Modifiers::NONE));
+ h.run_steps(2);
+ let z1 = zoom(&h);
+ let size = h.state().ui.views[0].doc_size;
+ assert!((z1 - crate::zoom_tool::scrub_zoom(z0, 60.0, size)).abs() < 1e-4, "⌘Space-drag zooms in: {z0} -> {z1}");
+ let doc1 = doc_at(&h, p);
+ assert!((doc1[0] - doc0[0]).abs() < 0.5 && (doc1[1] - doc0[1]).abs() < 0.5, "the pressed point stays put: {doc0:?} -> {doc1:?}");
+
+ // ⌥Space-click steps out.
+ h.hover_at(p);
+ h.run_steps(1);
+ held(&mut h, true, Modifiers::ALT);
+ button(&mut h, p, true, Modifiers::ALT);
+ button(&mut h, p, false, Modifiers::ALT);
+ held(&mut h, false, Modifiers::ALT);
+ h.event(egui::Event::ModifiersChanged(Modifiers::NONE));
+ h.run_steps(2);
+ assert_eq!(zoom(&h), crate::zoom_levels::step(z1, -1, size));
+ assert_eq!(h.state().ui.dialogs.len(), 1);
+}
+
fn press(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, pressed: bool) {
h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
h.run_steps(1);