Skip to content

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

fix(ui): Ctrl/Cmd-drag in the Navigator zooms to the drawn rectangle, as in Photoshop (#6872) (#7140)

Korg1ch295a28a50be0
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 6fc8e2d8f..10476013e 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -1939,13 +1939,42 @@ fn navigator(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ui.painter().image(tex, rect, Rect::from_min_max(egui::Pos2::ZERO, pos2(1.0, 1.0)), Color32::WHITE);
}
let s = rect.width() / size.width.max(1) as f32;
- if (resp.clicked() || resp.dragged())
+ // ⌘/Ctrl-drag over the preview draws a rectangle the view then zooms to, as in Photoshop
+ // (#6872); a plain click or drag moves the view.
+ let command = crate::workspace_ui::sticky_mods(app, ctx.input(|i| i.modifiers)).command;
+ if resp.drag_started() && command {
+ let start = ctx.input(|i| i.pointer.press_origin()).or(resp.interact_pointer_pos());
+ ui.data_mut(|d| match start {
+ Some(p) => {
+ d.insert_temp(navigator_zoom_drag_id(), p);
+ }
+ None => d.remove::<Pos2>(navigator_zoom_drag_id()),
+ });
+ }
+ let zoom_from = ui.data(|d| d.get_temp::<Pos2>(navigator_zoom_drag_id()));
+ if let Some(from) = zoom_from {
+ if let Some(to) = resp.interact_pointer_pos() {
+ let r = Rect::from_two_pos(from, to).intersect(rect);
+ crate::tool_feedback::draw_ants(ui.painter(), &[r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom()], true);
+ if resp.drag_stopped() {
+ let doc_rect = Rect::from_min_max(((r.min - rect.min) / s).to_pos2(), ((r.max - rect.min) / s).to_pos2());
+ let (canvas, ppp, aspect) = (app.last_canvas_rect, app.canvas_ppp(), app.ui.view.display_aspect());
+ navigator_zoom_to(&mut app.ui.views[idx], doc_rect, [size.width, size.height], canvas.size(), ppp, aspect);
+ }
+ }
+ if resp.drag_stopped() || !resp.dragged() {
+ ui.data_mut(|d| d.remove::<Pos2>(navigator_zoom_drag_id()));
+ }
+ } else if (resp.clicked() || resp.dragged())
&& let Some(pp) = resp.interact_pointer_pos()
{
let d = (pp - rect.min) / s;
app.ui.views[idx].center = [d.x.clamp(0.0, size.width as f32), d.y.clamp(0.0, size.height as f32)];
app.ui.views[idx].fit_pending = false;
}
+ if command && resp.hovered() {
+ ctx.set_cursor_icon(egui::CursorIcon::ZoomIn);
+ }
// The Navigator has its own zoom target: an unmodified wheel over the preview
// should zoom even when the document canvas is configured to scroll on wheel.
// Use the canvas wheel classifier to honour one ×1.1 step per notch without
@@ -2024,6 +2053,42 @@ fn navigator_fit(frame: Rect, w: u32, h: u32) -> Rect {
Rect::from_center_size(frame.center(), vec2(w * s, h * s))
}
+/// Where a ⌘/Ctrl-drag over the Navigator's preview started, while it lasts.
+fn navigator_zoom_drag_id() -> egui::Id {
+ egui::Id::new("navigator-zoom-drag")
+}
+
+/// Zoom `view` so the document rectangle `doc_rect` (pixels) fills a canvas of `canvas` points,
+/// centred on it, as a Navigator ⌘/Ctrl-drag does (#6872). `doc` is the document's size in
+/// pixels (the view's own copy is only set once the canvas has been drawn), `ppp` the canvas's
+/// device pixels per point and `aspect` the pixel aspect ratio shown; the zoom stays within the
+/// zoom range. A rectangle under 2 document pixels across zooms one step in on its centre.
+fn navigator_zoom_to(view: &mut crate::state::View, doc_rect: Rect, doc: [u32; 2], canvas: egui::Vec2, ppp: f32, aspect: f32) {
+ // Checked before clipping: `min`/`max` would turn a NaN edge into a document edge.
+ if ![doc_rect.min.x, doc_rect.min.y, doc_rect.max.x, doc_rect.max.y].iter().all(|v| v.is_finite()) {
+ return;
+ }
+ let (w, h) = (doc[0] as f32, doc[1] as f32);
+ let r = doc_rect.intersect(Rect::from_min_size(Pos2::ZERO, vec2(w, h)));
+ if r.width() < 0.0 || r.height() < 0.0 {
+ return;
+ }
+ let aspect = if aspect.is_finite() && aspect > 0.0 { aspect } else { 1.0 };
+ let zoom = if r.width() < 2.0 || r.height() < 2.0 || canvas.x <= 0.0 || canvas.y <= 0.0 {
+ crate::zoom_levels::step(view.zoom, 1, view.doc_size)
+ } else {
+ // Document columns shown across the canvas are `canvas.x / (point_zoom * aspect)`.
+ let point_zoom = (canvas.x / (r.width() * aspect)).min(canvas.y / r.height());
+ point_zoom * ppp
+ };
+ if !zoom.is_finite() {
+ return;
+ }
+ view.zoom = crate::zoom_levels::clamp(zoom, view.doc_size);
+ view.center = [r.center().x, r.center().y];
+ view.fit_pending = false;
+}
+
fn navigator_preview_id() -> egui::Id {
egui::Id::new("navigator-preview-rect")
}
@@ -2086,6 +2151,101 @@ mod navigator_wheel_tests {
assert!((app.ui.views[0].zoom - 1.0).abs() < 1e-4, "wheel outside preview leaves zoom unchanged");
}
+ /// #6872: the zoom a Navigator ⌘/Ctrl-drag sets: the rectangle fills the canvas, centred.
+ #[test]
+ fn navigator_zoom_to_fits_the_rectangle_in_the_canvas() {
+ let fresh = || crate::state::View { doc_size: [200, 100], zoom: 1.0, center: [100.0, 50.0], ..Default::default() };
+ let canvas = vec2(800.0, 600.0);
+ let r = Rect::from_min_max(pos2(50.0, 25.0), pos2(100.0, 50.0));
+ // 50 × 25 px on 800 × 600 pt: the width limits it, 16 points per pixel.
+ let mut v = fresh();
+ navigator_zoom_to(&mut v, r, [200, 100], canvas, 1.0, 1.0);
+ assert_eq!((v.zoom, v.center, v.fit_pending), (crate::zoom_levels::clamp(16.0, [200, 100]), [75.0, 37.5], false));
+ // Device pixels per point and a 2:1 pixel aspect scale it as the canvas shows pixels.
+ let mut v = fresh();
+ navigator_zoom_to(&mut v, r, [200, 100], canvas, 2.0, 2.0);
+ assert_eq!(v.zoom, crate::zoom_levels::clamp(16.0, [200, 100]), "8 points per pixel across, at 2 device pixels per point");
+ // Off the document: only the part on it counts.
+ let mut v = fresh();
+ navigator_zoom_to(&mut v, Rect::from_min_max(pos2(150.0, 50.0), pos2(400.0, 300.0)), [200, 100], canvas, 1.0, 1.0);
+ assert_eq!(v.center, [175.0, 75.0]);
+ // Under 2 px across: one step in on its centre.
+ let mut v = fresh();
+ navigator_zoom_to(&mut v, Rect::from_min_max(pos2(10.0, 10.0), pos2(11.0, 40.0)), [200, 100], canvas, 1.0, 1.0);
+ assert_eq!((v.zoom, v.center), (crate::zoom_levels::step(1.0, 1, [200, 100]), [10.5, 25.0]));
+ // Not a number, or wholly off the document: nothing changes.
+ for bad in [Rect::from_min_max(pos2(f32::NAN, 0.0), pos2(10.0, 10.0)), Rect::from_min_max(pos2(300.0, 300.0), pos2(400.0, 400.0))] {
+ let mut v = fresh();
+ navigator_zoom_to(&mut v, bad, [200, 100], canvas, 1.0, 1.0);
+ assert_eq!((v.zoom, v.center), (1.0, [100.0, 50.0]), "{bad:?}");
+ }
+ }
+
+ /// One frame of the Navigator with pointer `events` and `modifiers` held.
+ fn frame_with(app: &mut PhotocraftApp, ctx: &egui::Context, modifiers: Modifiers, mut events: Vec<Event>) {
+ events.insert(0, Event::ModifiersChanged(modifiers));
+ let input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(430.0, 460.0))), events, ..Default::default() };
+ ctx.run_ui(input, |ui| navigator(app, ui)).textures_delta.clear();
+ }
+
+ /// A drag over the Navigator's preview between document points `from` and `to`.
+ fn drag(app: &mut PhotocraftApp, ctx: &egui::Context, modifiers: Modifiers, from: [f32; 2], to: [f32; 2]) {
+ let preview = last_navigator_preview(ctx).expect("the preview");
+ let s = preview.width() / 200.0;
+ let at = |p: [f32; 2]| preview.min + vec2(p[0], p[1]) * s;
+ let (a, b) = (at(from), at(to));
+ let button = |pos, pressed| Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers };
+ frame_with(app, ctx, modifiers, vec![Event::PointerMoved(a), button(a, true)]);
+ frame_with(app, ctx, modifiers, vec![Event::PointerMoved(a.lerp(b, 0.5))]);
+ frame_with(app, ctx, modifiers, vec![Event::PointerMoved(b)]);
+ frame_with(app, ctx, modifiers, vec![button(b, false)]);
+ frame_with(app, ctx, Modifiers::NONE, vec![]);
+ }
+
+ /// #6872: ⌘/Ctrl-dragging a rectangle over the preview zooms the view to it, as in
+ /// Photoshop; a plain drag still only moves the view.
+ #[test]
+ fn navigator_command_drag_zooms_to_the_rectangle() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
+ app.sync_views();
+ app.ui.views[0].zoom = 1.0;
+ app.ui.views[0].fit_pending = false;
+ let ctx = egui::Context::default();
+ PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);
+ // The preview settles once the zoom row under it has been measured.
+ for _ in 0..3 {
+ frame(&mut app, &ctx, pos2(5.0, 5.0), 0.0);
+ }
+ // A plain drag pans and keeps the zoom.
+ drag(&mut app, &ctx, Modifiers::NONE, [50.0, 25.0], [100.0, 50.0]);
+ assert_eq!(app.ui.views[0].zoom, 1.0);
+ let c = app.ui.views[0].center;
+ assert!((c[0] - 100.0).abs() < 1.0 && (c[1] - 50.0).abs() < 1.0, "the view follows the pointer: {c:?}");
+ // ⌘/Ctrl-drag: the rectangle fills the canvas (`last_canvas_rect`, 800 × 600 pt).
+ drag(&mut app, &ctx, Modifiers::COMMAND, [50.0, 25.0], [100.0, 50.0]);
+ let mut expected = app.ui.views[0].clone();
+ navigator_zoom_to(
+ &mut expected,
+ Rect::from_min_max(pos2(50.0, 25.0), pos2(100.0, 50.0)),
+ [200, 100],
+ vec2(800.0, 600.0),
+ app.canvas_ppp(),
+ app.ui.view.display_aspect(),
+ );
+ let v = &app.ui.views[0];
+ assert!((v.zoom - expected.zoom).abs() / expected.zoom < 0.05, "{} vs {}", v.zoom, expected.zoom);
+ assert!(
+ (v.center[0] - 75.0).abs() < 1.0 && (v.center[1] - 37.5).abs() < 1.0,
+ "centred on the rectangle: {:?} (zoom {}, expected {:?})",
+ v.center,
+ v.zoom,
+ (expected.zoom, expected.center)
+ );
+ assert!(v.zoom > 1.0 && !v.fit_pending);
+ assert!(ctx.data(|d| d.get_temp::<Pos2>(navigator_zoom_drag_id())).is_none(), "the drag is over");
+ }
+
/// #4840: under the preview, one row as in Photoshop: the zoom in percent, Zoom Out, the
/// slider and Zoom In. The buttons step the zoom like View › Zoom In / Out.
#[test]