diff --git a/crates/ui-canvas/src/canvas/momentary.rs b/crates/ui-canvas/src/canvas/momentary.rs index 5b664ee3e..cb7631a42 100644 --- a/crates/ui-canvas/src/canvas/momentary.rs +++ b/crates/ui-canvas/src/canvas/momentary.rs @@ -80,7 +80,7 @@ pub struct Momentary { impl Momentary { /// The momentary tools under `options`. The Zoom tool reads the document's size from the canvas's space. pub fn new(options: NavigationOptions) -> Self { - Momentary { held: None, latched: None, hand: HandTool::new(options).lent(), zoom: ZoomTool::new(options), hand_selected: false } + Momentary { held: None, latched: None, hand: HandTool::new(options).lent(), zoom: ZoomTool::new(options).lent(), hand_selected: false } } /// Tells the set whether the selected tool is the Hand: with the Hand selected and no Space held, Cmd and Alt at a diff --git a/crates/ui-canvas/src/canvas/tools.rs b/crates/ui-canvas/src/canvas/tools.rs index 00f095bf2..07f067022 100644 --- a/crates/ui-canvas/src/canvas/tools.rs +++ b/crates/ui-canvas/src/canvas/tools.rs @@ -368,15 +368,17 @@ impl CanvasTool for HandTool { /// The Zoom tool: a click steps the ladder about the clicked point (Alt: out; Zoom Clicked Point to Center centres /// it), a press held still zooms continuously ([`HoldZoom`], Animated Zoom only), a drag scrubs or draws a zoom -/// rectangle ([`ZoomDrag`], Scrubby Zoom). The cursor is the magnifier, with a minus while Alt is held or `out` is -/// set (Zoom Out held with Alt+Space). At a zoom limit the view stays put. The tool reads the document's size from +/// rectangle ([`ZoomDrag`], Scrubby Zoom). The cursor is the magnifier, with a minus while the tool zooms out. At a +/// zoom limit the view stays put. The tool reads the document's size from /// the canvas's [`CanvasSpace`](super::CanvasSpace) (the size given to [`Canvas::document`](super::Canvas::document)); /// on a canvas given no document it does nothing. #[derive(Clone, Debug)] #[non_exhaustive] pub struct ZoomTool { - /// Zoom out instead of in (Alt+Space held); Alt flips it. + /// Zoom out instead of in (the tool's zoom-out mode); a held Alt flips it on the selected tool. pub out: bool, + /// Lent by the momentary set: the held keys chose the direction, Alt included, so Alt flips nothing. + lent: bool, /// The navigation choices the tool reads. pub options: NavigationOptions, hold: Option, @@ -392,16 +394,28 @@ pub struct ZoomTool { impl ZoomTool { /// A Zoom tool under `options`. pub fn new(options: NavigationOptions) -> Self { - Self { out: false, options, hold: None, drag: None, press: None, last: Pos2::ZERO, dragged: false } + Self { out: false, lent: false, options, hold: None, drag: None, press: None, last: Pos2::ZERO, dragged: false } + } + + /// This tool as the momentary set lends it: its direction is final. + #[must_use] + pub(super) fn lent(mut self) -> Self { + self.lent = true; + self + } + + /// Whether the tool zooms out with `mods` held. + fn zooms_out(&self, mods: Mods) -> bool { + self.out != (mods.alt && !self.lent) } /// One pass of a press in progress, with the pointer at `at`: the hold zooms once past its delay (and then the /// drag has nothing to do), else the drag scrubs or moves the rectangle's corner. fn advance(&mut self, at: Pos2, mods: Mods, time: f64, cx: &mut ToolContext<'_>) { + let out = self.zooms_out(mods); let (Some(drag), Some(space)) = (self.drag.as_mut(), cx.space()) else { return }; let Some(size) = space.document() else { return }; let ppp = space.pixels_per_point; - let out = self.out != mods.alt; let mut view = space.view; let before = view; let mut waiting = false; @@ -483,7 +497,7 @@ impl CanvasTool for ZoomTool { let hold = self.hold.take(); let Some(size) = space.document() else { return }; let ppp = space.pixels_per_point; - let out = self.out != pointer.mods.alt; + let out = self.zooms_out(pointer.mods); // A hold that zoomed has already changed the view; the release is no click. if hold.is_some_and(|hold| hold.zooming()) { return; @@ -523,7 +537,7 @@ impl CanvasTool for ZoomTool { } fn cursor(&self, cx: &ToolContext<'_>) -> Cursor { - PaintedCursor::zoom(self.out != cx.mods.alt).into() + PaintedCursor::zoom(self.zooms_out(cx.mods)).into() } fn autoscrolls(&self) -> bool { diff --git a/crates/ui-canvas/tests/it/canvas/tool.rs b/crates/ui-canvas/tests/it/canvas/tool.rs index d1207d3c1..e593a9b84 100644 --- a/crates/ui-canvas/tests/it/canvas/tool.rs +++ b/crates/ui-canvas/tests/it/canvas/tool.rs @@ -1012,6 +1012,29 @@ fn a_queued_space_held_over_a_press_pans_with_the_hand_and_the_press_after_its_r assert!(!calls.iter().any(|call| call == "main drag"), "the Hand's drag never reaches the selected tool: {calls:?}"); } +/// The momentary Zoom's direction comes from the keys that chose it: a click with Alt and Space held zooms out, and the +/// Alt that turned the Hand into Zoom Out does not flip it back to Zoom In. +#[trial] +fn a_click_under_alt_space_zooms_out() { + let log = Log::default(); + let (mut fixture, view, canvas) = momentary_canvas(Probe::new("main", &log)); + let at = fixture.by_role(Role::CANVAS)[0].rect.center(); + let before = view.get().expect("the canvas ran").zoom; + let space = craft_keymap::Key::Space; + for input in [ + ToolInput::hold(space), + ToolInput::down(PointerButton::Primary, at).mods(Mods::ALT), + ToolInput::up(at).mods(Mods::ALT), + ToolInput::let_go(space), + ] { + ToolHost::inject(fixture.ctx(), canvas.get(), input); + } + fixture.run_injected(); + let after = view.get().expect("the canvas ran").zoom; + assert!(after < before, "Alt+Space and a click zoom out: {before} -> {after}"); + assert!(log.borrow().is_empty(), "the selected tool sees nothing: {:?}", log.borrow()); +} + /// A held Space pans over a session member on the canvas's body: a drag on a crop corner pans the view with the Hand /// and leaves the crop as it was; without Space the same drag moves the corner and leaves the view. #[trial]