Merge pull request #12102 from Bobderto/fix/12085-zoom-menu-centre

Zoom tool menu: keep the view centre for Zoom In and Out
This commit is contained in:
David Mann authored and GitHub committed 2026-11-08 16:51:19 +01:00
commit 2ffe5d1008
4 files changed
+206 -10

No files matched your search

@@ -1000,8 +1000,8 @@ pub fn open_view(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32;
true
}
/// A view menu row. Zoom In and Zoom Out step about the right-clicked point, as a Zoom tool click
/// does; the other rows run their View menu command.
/// A view menu row. The non-temporary Zoom tool's steps keep the view centre unless clicked-point
/// centring is enabled. Other view-tool cases retain their pressed-point anchoring.
fn choose_view(app: &mut PhotocraftApp, ctx: &Context, menu: &CanvasToolMenu, command: &str) -> Result<(), String> {
let i = app.session.active_index().ok_or("no document")?;
if command == FILL_SCREEN {
@@ -1019,6 +1019,9 @@ fn choose_view(app: &mut PhotocraftApp, ctx: &Context, menu: &CanvasToolMenu, co
}
let (Some((zoom0, c)), Some(d)) = (before, menu.anchor) else { return Ok(()) };
let center_on_point = app.session.prefs().tools.zoom_clicked_point_to_center;
if menu.tool == Tool::Zoom && app.ui.tool == Tool::Zoom && app.ui.spring.is_none() && crate::hold_keys::held_tool(app, ctx).is_none() && !center_on_point {
return Ok(());
}
let Some(v) = app.ui.views.get_mut(i) else { return Ok(()) };
let d = [d[0] as f32, d[1] as f32];
// The view scales about the centre, so the point keeps its screen position when its offset
@@ -2,6 +2,9 @@ use super::*;
use egui::{Modifiers, PointerButton, Pos2, vec2};
use egui_kittest::{Harness, kittest::Queryable};
#[path = "zoom_menu_anchor_tests.rs"]
mod zoom_menu_anchor_tests;
fn app() -> PhotocraftApp {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 32, "height": 32})).unwrap();
@@ -548,11 +551,12 @@ fn zoom_and_hand_right_click_opens_the_view_menu() {
}
#[test]
fn view_menu_zoom_in_keeps_the_clicked_point_under_the_pointer() {
fn view_menu_zoom_in_keeps_the_view_centre() {
let mut app = zoom_app(400, 300);
app.ui.tool = Tool::Zoom;
let mut h = harness(app);
let zoom = h.state().ui.views[0].zoom;
let center = h.state().ui.views[0].center;
let doc = [300.0, 100.0];
let p = screen_of(h.state(), doc);
right_click(&mut h, p, Modifiers::NONE);
@@ -562,8 +566,7 @@ fn view_menu_zoom_in_keeps_the_clicked_point_under_the_pointer() {
assert!(h.state().ui.canvas_tool_menu.is_none());
let after = h.state().ui.views[0].zoom;
assert_eq!(after, crate::zoom_levels::step(zoom, 1, [400, 300]), "one Zoom In step");
let s = screen_of(h.state(), doc);
assert!((s - p).length() < 1.0, "the right-clicked point stays put: {s:?} vs {p:?}");
assert_eq!(h.state().ui.views[0].center, center);
assert_eq!(h.state().session.journal.last().map(|(id, _)| id.as_str()), Some("view.zoomIn"));
// Zoom Out returns to where it was.
right_click(&mut h, p, Modifiers::NONE);
@@ -574,12 +577,13 @@ fn view_menu_zoom_in_keeps_the_clicked_point_under_the_pointer() {
}
#[test]
fn animated_zoom_off_view_menu_keeps_the_anchor_without_a_transition() {
fn animated_zoom_off_view_menu_keeps_the_centre_without_a_transition() {
for command in ["view.zoomIn", "view.zoomOut", FILL_SCREEN] {
let mut app = zoom_app(400, 300);
app.ui.tool = Tool::Zoom;
app.run("prefs.set", json!({"values": {"general.animatedZoom": false, "tools.zoomClickedPointToCenter": false}})).unwrap();
let mut h = harness(app);
let center = h.state().ui.views[0].center;
let doc = [300.0, 100.0];
let p = screen_of(h.state(), doc);
right_click(&mut h, p, Modifiers::NONE);
@@ -592,17 +596,18 @@ fn animated_zoom_off_view_menu_keeps_the_anchor_without_a_transition() {
assert_eq!((xf.zoom, xf.center), (v.zoom, v.center), "{command}");
assert!(v.zoom_anchor.is_none() && v.zoom_transition.is_none(), "{command}");
if command != FILL_SCREEN {
assert!(screen_of(h.state(), doc).distance(p) < 1.0, "{command}: preserve the clicked point");
assert_eq!(v.center, center, "{command}: preserve the view centre");
}
}
}
#[test]
fn view_menu_zoom_keeps_its_anchor_during_the_animation() {
fn view_menu_zoom_keeps_its_centre_during_the_animation() {
let mut app = zoom_app(400, 300);
app.ui.tool = Tool::Zoom;
let mut h = harness(app);
let zoom = h.state().ui.views[0].zoom;
let center = h.state().ui.views[0].center;
let doc = [300.0, 100.0];
let p = screen_of(h.state(), doc);
right_click(&mut h, p, Modifiers::NONE);
@@ -614,7 +619,7 @@ fn view_menu_zoom_keeps_its_anchor_during_the_animation() {
h.step();
let displayed = crate::canvas::ViewXform::active(h.state()).unwrap();
assert!(displayed.zoom > zoom && displayed.zoom < h.state().ui.views[0].zoom);
assert!(displayed.to_screen(doc[0], doc[1]).distance(p) < 1.0);
assert!(displayed.to_screen(center[0], center[1]).distance(displayed.rect.center()) < 1.0);
h.input_mut().time = Some(ctx.input(|i| i.time) + 0.3);
h.step();
assert!(!h.state().ui.views[0].zoom_animating);
@@ -0,0 +1,188 @@
use super::*;
#[test]
fn zoom_menu_steps_keep_the_view_centre_and_history() {
for animated in [false, true] {
for offset in [vec2(-160.0, -100.0), vec2(140.0, 80.0)] {
let mut app = zoom_app(2400, 1800);
app.ui.tool = Tool::Zoom;
app.run("prefs.set", json!({"values": {"general.animatedZoom": animated, "tools.zoomClickedPointToCenter": false}})).unwrap();
let mut h = harness(app);
let v = &mut h.state_mut().ui.views[0];
v.zoom = 1.0;
v.center = [928.0, 686.0];
v.request_zoom_animation(false);
h.step();
let center = h.state().ui.views[0].center;
let st = h.state().session.active().unwrap();
let history = (st.history.past_len(), st.history.can_redo(), st.revision);
for (label, zoom) in [("Zoom In", 2.0), ("Zoom Out", 1.0), ("Zoom Out", 2.0 / 3.0)] {
let p = crate::canvas::ViewXform::active(h.state()).unwrap().rect.center() + offset;
right_click(&mut h, p, Modifiers::NONE);
h.get_by_label(label).click();
h.run_steps(3);
let v = &h.state().ui.views[0];
assert_eq!(v.zoom, zoom, "{label}: one ladder stop");
assert_eq!(v.center, center, "{label}: the right-click must not move the view centre");
let xf = crate::canvas::ViewXform::active(h.state()).unwrap();
assert!(xf.to_screen(center[0], center[1]).distance(xf.rect.center()) < 1.0);
assert!(h.state().ui.canvas_tool_menu.is_none());
let st = h.state().session.active().unwrap();
assert_eq!((st.history.past_len(), st.history.can_redo(), st.revision), history);
}
}
}
}
#[test]
fn clicked_point_preference_and_hand_menu_keep_their_existing_anchors() {
for (tool, center_on_point) in [(Tool::Zoom, true), (Tool::Hand, true), (Tool::Hand, false)] {
for (command, direction) in [("view.zoomIn", 1), ("view.zoomOut", -1)] {
let mut app = zoom_app(400, 300);
app.ui.tool = tool;
app.run("prefs.set", json!({"values": {"tools.zoomClickedPointToCenter": center_on_point, "tools.overscroll": true}})).unwrap();
let mut h = harness(app);
let before = h.state().ui.views[0].clone();
let p = screen_of(h.state(), [300.0, 100.0]);
right_click(&mut h, p, Modifiers::NONE);
let doc = h.state().ui.canvas_tool_menu.as_ref().unwrap().anchor.unwrap().map(|n| n as f32);
let ctx = h.ctx.clone();
choose(h.state_mut(), &ctx, command);
let v = &h.state().ui.views[0];
assert_eq!(v.zoom, crate::zoom_levels::step(before.zoom, direction, [400, 300]));
let expected = if center_on_point { doc } else { std::array::from_fn(|i| doc[i] - (doc[i] - before.center[i]) * before.zoom / v.zoom) };
assert_eq!(v.center, expected, "{tool:?}: {command}");
assert!(v.zoom_animating);
assert_eq!(v.zoom_anchor.is_some(), !center_on_point);
}
}
}
#[test]
fn zoom_click_and_alt_click_still_anchor_at_the_pointer() {
for animated in [false, true] {
for (modifiers, direction) in [(Modifiers::NONE, 1), (Modifiers::ALT, -1)] {
let mut app = zoom_app(400, 300);
app.ui.tool = Tool::Zoom;
app.run("prefs.set", json!({"values": {"general.animatedZoom": animated, "tools.zoomClickedPointToCenter": false, "tools.overscroll": true}}))
.unwrap();
let mut h = harness(app);
let zoom = h.state().ui.views[0].zoom;
let doc = [300.0, 100.0];
let p = screen_of(h.state(), doc);
h.event(egui::Event::ModifiersChanged(modifiers));
h.event(egui::Event::PointerMoved(p));
h.step();
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers });
h.step();
}
h.input_mut().time = Some(h.ctx.input(|i| i.time) + 0.3);
h.step();
assert_eq!(h.state().ui.views[0].zoom, crate::zoom_levels::step(zoom, direction, [400, 300]));
assert!(screen_of(h.state(), doc).distance(p) < 1.0);
assert!(h.state().ui.canvas_tool_menu.is_none());
assert_eq!(h.state().session.active().unwrap().history.past_len(), 0);
}
}
}
#[test]
fn held_zoom_keys_still_open_no_view_menu() {
for tool in [Tool::Brush, Tool::Hand] {
for modifiers in [Modifiers::COMMAND, Modifiers::ALT] {
let mut app = zoom_app(400, 300);
app.ui.tool = tool;
let mut h = harness(app);
let modifiers =
Modifiers { mac_cmd: cfg!(target_os = "macos") && modifiers.command, ctrl: !cfg!(target_os = "macos") && modifiers.command, ..modifiers };
h.event(egui::Event::ModifiersChanged(modifiers));
h.event(egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed: true, repeat: false, modifiers });
h.step();
assert!(matches!(crate::hold_keys::held_tool(h.state(), &h.ctx), Some(crate::hold_keys::Temporary::ZoomIn | crate::hold_keys::Temporary::ZoomOut)));
let before = h.state().ui.views[0].clone();
let p = screen_of(h.state(), [300.0, 100.0]);
right_click(&mut h, p, modifiers);
assert!(h.state().ui.canvas_tool_menu.is_none());
assert_eq!(h.state().ui.tool, tool);
let v = &h.state().ui.views[0];
assert_eq!((v.zoom, v.center), (before.zoom, before.center));
}
}
}
#[test]
fn held_zoom_with_zoom_selected_keeps_the_menu_pressed_point_anchor() {
for animated in [false, true] {
for (command, direction) in [("view.zoomIn", 1), ("view.zoomOut", -1)] {
let mut app = zoom_app(400, 300);
app.ui.tool = Tool::Zoom;
app.run("prefs.set", json!({"values": {"general.animatedZoom": animated, "tools.zoomClickedPointToCenter": false, "tools.overscroll": true}}))
.unwrap();
let mut h = harness(app);
h.event(egui::Event::ModifiersChanged(Modifiers::ALT));
h.event(egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::ALT });
h.step();
assert_eq!(crate::hold_keys::held_tool(h.state(), &h.ctx), Some(crate::hold_keys::Temporary::ZoomOut));
let zoom = h.state().ui.views[0].zoom;
let center = h.state().ui.views[0].center;
let doc = [300.0, 100.0];
let p = screen_of(h.state(), doc);
h.event(egui::Event::PointerMoved(p));
h.step();
for pressed in [true, false] {
h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed, modifiers: Modifiers::ALT });
h.step();
}
assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.tool == Tool::Zoom));
assert_eq!(crate::hold_keys::held_tool(h.state(), &h.ctx), Some(crate::hold_keys::Temporary::ZoomOut));
let ctx = h.ctx.clone();
choose(h.state_mut(), &ctx, command);
assert_eq!(h.state().ui.views[0].zoom, crate::zoom_levels::step(zoom, direction, [400, 300]));
assert_ne!(h.state().ui.views[0].center, center, "{command}: preserve the held tool's pressed-point behavior");
h.step();
h.input_mut().time = Some(ctx.input(|i| i.time) + 0.11);
h.step();
assert!(screen_of(h.state(), doc).distance(p) < 1.0, "{command}: anchor during animation");
h.input_mut().time = Some(ctx.input(|i| i.time) + 0.3);
h.step();
assert!(screen_of(h.state(), doc).distance(p) < 1.0, "{command}: settled anchor");
assert_eq!(h.state().ui.tool, Tool::Zoom);
assert_eq!(h.state().session.active().unwrap().history.past_len(), 0);
}
}
}
#[test]
fn held_z_menu_keeps_its_existing_pressed_point_anchor() {
for label in ["Zoom In", "Zoom Out"] {
let mut app = zoom_app(400, 300);
app.ui.tool = Tool::Brush;
let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
|ui, app: &mut PhotocraftApp| {
let ctx = ui.ctx().clone();
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
return;
}
crate::shortcuts::handle(app, &ctx);
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
},
app,
);
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default());
h.run_steps(4);
h.event(egui::Event::Key { key: egui::Key::Z, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
h.step();
assert_eq!(h.state().ui.tool, Tool::Zoom);
assert!(h.state().ui.spring.as_ref().is_some_and(|s| s.previous == Tool::Brush));
let doc = [300.0, 100.0];
let p = screen_of(h.state(), doc);
right_click(&mut h, p, Modifiers::NONE);
h.get_by_label(label).click();
h.run_steps(3);
assert!(screen_of(h.state(), doc).distance(p) < 1.0, "{label}: a held Z keeps today's anchor");
h.event(egui::Event::Key { key: egui::Key::Z, physical_key: None, pressed: false, repeat: false, modifiers: Modifiers::NONE });
h.run_steps(2);
assert_eq!(h.state().ui.tool, Tool::Brush);
}
}
+1 -1
View File
@@ -86,7 +86,7 @@ The transport is `apps/photocraft/src/control_server.rs`, and the handlers are i
- A pixel tool pressed on a type, shape, Smart Object or fill layer (e.g. through `ui.pointer`) opens the "Rasterize?" prompt instead of painting: a dialog with `__rasterize` (`type|shape|smartObject|fill`), `message`, `layer`, `tool` and `at`. `ui.dialog.confirm` runs the `layer.rasterize.*` command and then paints at `at` (two history states, `{"rasterized", "painted"}`); `ui.dialog.cancel` does nothing.
- Edit › Transform › Warp on editable type (`ui.menu.invoke {"id":"edit.transform.warp"}`, or the warp toggle of an open transform box) opens Warp mode with no prompt, as in Photoshop: the mesh can't be dragged or split, only the options bar's warp style, Bend, H and V apply, and the commit sets the layer's Warp Text in one step, so the type stays editable. Directly: `engine.execute {"command":"edit.transform.warp","params":{"style":"flag","bend":40}}` on a type layer. Distort and Perspective stay greyed for type.
- `ui.window.open {document?}` / `ui.window.close {window}`: extra document windows
- `ui.pointer {events: [{kind: down|move|up, x, y, pressure?, tiltX?, tiltY?, rotation?}], tool?, modifiers?, button?}`: drive the active tool in document coordinates (pressure 0..1, tilt in degrees -90..90, barrel rotation 0..360: a simulated pen). Modifier flags (`shift`, `alt`, `command`, `ctrl`, `space`) go either at the top level or grouped under `modifiers`; when `modifiers` is present, top-level flags are ignored. `space: true` holds Space (the Crop frame, marquee, lasso or shape being drawn then moves instead of growing). With the Crop tool in its default mode (Use Classic Mode off) a drag inside the box moves the image and one outside turns it, the view panning and turning with it; a drag's `move`/`up` points are read as the view showed the document at its `down`, so an `up` where the last `move` was changes nothing more. `command: true` with a painting, retouching, eraser, gradient, eyedropper or other non-selection tool makes the gesture the Move tool's, as holding ⌘ (Ctrl off the Mac) does in the app: a drag moves the active layer (the selected pixels when there is a selection), with `alt: true` as well it duplicates the layer first, and a click picks the layer under the point; the tool is unchanged afterwards. The Hand, Rotate View, Zoom, Crop, Slice, Path Selection, Pen, shape, Type and selection tools keep `command` for themselves. `ctrl: true` with `alt: true` and a brush tool is the on-canvas brush resize instead (in the app, Ctrl+Alt-drag off the Mac). `button: "right"` (or `"secondary"`) is the right button: with the Move tool (or `command: true` with any tool) it opens the canvas layer menu (`layerMenu` in `ui.inspect` lists the layers there, topmost first; select one with `layer.select`); with Marquee, Lasso, Magic Wand, Object Selection, or Pen, or with a Type tool on text being edited or on committed type, it opens a tool context menu (`canvasToolMenu` in `ui.inspect`, with ordered command ids, separators, and enabled states); with Zoom or Hand it opens the view menu there (Zoom In and Zoom Out step about the pressed point; `view.fillScreen` is its Fill Screen row); with a painting tool it opens the Brush Preset picker, or erases with Preferences › Tools › Right-click with painting tools = erase. For a painting tool's Shift+right-click, see the Mode menu exception below. The menus open at the screen position of the pressed document point. Use `ui.context.choose {id}` for enabled tool-context actions. The Pen menu is documented in `docs/context-menu-parity.md`. `button` is `left` (default), `right`, `secondary` or `middle`, and an event's `kind` is `down`, `move` (default) or `up`. With a Color Picker as the top dialog, `down` and `move` sample the image into its new colour instead (its eyedropper, like a click on the canvas) and the tool is not driven. A `down`/`up` at the same point with the Rectangle, Ellipse, Triangle or Polygon tool opens Create Rectangle / Ellipse / Triangle / Polygon (Photoshop's click-to-size dialog); set its `width` and `height` (document pixels), `fromCenter`, `radii` [tl, tr, br, bl] (Rectangle) or `sides` and `starRatio` (percent, Polygon) with `ui.dialog.set`, then `ui.dialog.confirm` runs `shape.create` at the clicked point.
- `ui.pointer {events: [{kind: down|move|up, x, y, pressure?, tiltX?, tiltY?, rotation?}], tool?, modifiers?, button?}`: drive the active tool in document coordinates (pressure 0..1, tilt in degrees -90..90, barrel rotation 0..360: a simulated pen). Modifier flags (`shift`, `alt`, `command`, `ctrl`, `space`) go either at the top level or grouped under `modifiers`; when `modifiers` is present, top-level flags are ignored. `space: true` holds Space (the Crop frame, marquee, lasso or shape being drawn then moves instead of growing). With the Crop tool in its default mode (Use Classic Mode off) a drag inside the box moves the image and one outside turns it, the view panning and turning with it; a drag's `move`/`up` points are read as the view showed the document at its `down`, so an `up` where the last `move` was changes nothing more. `command: true` with a painting, retouching, eraser, gradient, eyedropper or other non-selection tool makes the gesture the Move tool's, as holding ⌘ (Ctrl off the Mac) does in the app: a drag moves the active layer (the selected pixels when there is a selection), with `alt: true` as well it duplicates the layer first, and a click picks the layer under the point; the tool is unchanged afterwards. The Hand, Rotate View, Zoom, Crop, Slice, Path Selection, Pen, shape, Type and selection tools keep `command` for themselves. `ctrl: true` with `alt: true` and a brush tool is the on-canvas brush resize instead (in the app, Ctrl+Alt-drag off the Mac). `button: "right"` (or `"secondary"`) is the right button: with the Move tool (or `command: true` with any tool) it opens the canvas layer menu (`layerMenu` in `ui.inspect` lists the layers there, topmost first; select one with `layer.select`); with Marquee, Lasso, Magic Wand, Object Selection, or Pen, or with a Type tool on text being edited or on committed type, it opens a tool context menu (`canvasToolMenu` in `ui.inspect`, with ordered command ids, separators, and enabled states); with Zoom or Hand it opens the view menu there (Zoom In and Zoom Out keep the view centre with the non-temporary Zoom tool and Zoom Clicked Point to Center off; other cases retain their pressed-point behaviour; `view.fillScreen` is its Fill Screen row); with a painting tool it opens the Brush Preset picker, or erases with Preferences › Tools › Right-click with painting tools = erase. For a painting tool's Shift+right-click, see the Mode menu exception below. The menus open at the screen position of the pressed document point. Use `ui.context.choose {id}` for enabled tool-context actions. The Pen menu is documented in `docs/context-menu-parity.md`. `button` is `left` (default), `right`, `secondary` or `middle`, and an event's `kind` is `down`, `move` (default) or `up`. With a Color Picker as the top dialog, `down` and `move` sample the image into its new colour instead (its eyedropper, like a click on the canvas) and the tool is not driven. A `down`/`up` at the same point with the Rectangle, Ellipse, Triangle or Polygon tool opens Create Rectangle / Ellipse / Triangle / Polygon (Photoshop's click-to-size dialog); set its `width` and `height` (document pixels), `fromCenter`, `radii` [tl, tr, br, bl] (Rectangle) or `sides` and `starRatio` (percent, Polygon) with `ui.dialog.set`, then `ui.dialog.confirm` runs `shape.create` at the clicked point.
- Magnetic Lasso (`tool: "magneticLasso"`): a `down`/`up` pair is a click. The first sets the first fastening point (its modifiers set the selection mode); later clicks fasten a point, or close the border on the first point. `move` events trace the border with or without a button held, fastening points by themselves; with `alt: true` a click draws a straight segment and a drag a freehand one. `ui.inspect` shows the border in progress under `magnetic` (`path`, `anchors`, `live`, `mode`). `ui.key` Enter closes it along the edges, Backspace removes the last fastening point, Escape cancels. To select along edges in one call instead, use the `select.magneticLasso` command with rough points around the shape.
- Image › Adjustments › Curves on top, no eyedropper armed (#7325): a `down` with `command: true` adds a point on the curve the dialog shows (its Channel popup), at the pixel's tone (RGB: Rec. 709 luma; Gray: the mean) and at the curve's current output there, so the preview does not change; with `shift: true` as well, one point on each of Red, Green and Blue at that channel's value. It samples the merged image like the eyedroppers, never moves an existing point (a point already within 2 levels is selected instead, a 16-point curve takes no more), and the tool is not driven. Nothing is added with a pencil-drawn curve, in Lab or CMYK, or with a mask, saved channel or Quick Mask targeted.
- Levels or Curves adjustment layer, Properties eyedropper armed (`layerPicker` in `ui.inspect`: `{layer, point, tool}`, set by the panel's Set Black/Neutral Gray/White Point buttons): a `down` sets that point from the image below the layer as one `layer.setAdjustment {"layer", "eyedropper": {"point", "at": [x, y], "size"}}` (send that command directly to skip the UI) and the tool is not driven; Esc, another tool or another layer disarms it.