ui-canvas: Space moves the Zoom tool's rectangle while held, once let go since the press, and the release zooms to the moved rectangle

This commit is contained in:
bflatastic
2026-11-23 09:26:44 +07:00
parent 0a33cd9217
commit 8242da0a92
3 changed files with 63 additions and 8 deletions
+12 -3
View File
@@ -17,7 +17,7 @@ use craft_behavior::canvas::zoom;
use craft_behavior::geometry::crop::normalized_angle;
use craft_behavior::kurbo::{Rect, Size, Vec2};
use craft_behavior::{CancelReason, CanvasView, Cursor, PaintedCursor, ZoomScheme};
use craft_keymap::Mods;
use craft_keymap::{Key, Mods};
use egui::Pos2;
/// The rotation step of the Rotate View commands, in degrees.
@@ -417,7 +417,8 @@ 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 the tool zooms out. At a
/// rectangle ([`ZoomDrag`], Scrubby Zoom), which Space moves while held. Space held since the press (Cmd+Space lends
/// the tool) sizes the rectangle until it is let go. 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.
@@ -438,12 +439,14 @@ pub struct ZoomTool {
last: Pos2,
/// The host called `drag` in this press: the pointer moved past the arena's slop.
dragged: bool,
/// Space moves the rectangle while held: it was up at the press, or let go since.
space_moves: bool,
}
impl ZoomTool {
/// A Zoom tool under `options`.
pub fn new(options: NavigationOptions) -> Self {
Self { out: false, lent: 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, space_moves: false }
}
/// This tool as the momentary set lends it: its direction is final.
@@ -532,6 +535,7 @@ impl CanvasTool for ZoomTool {
self.press = Some((view, pointer.time, pointer.pos));
self.last = pointer.pos;
self.dragged = false;
self.space_moves = !cx.held(Key::Space);
self.hold = self.options.animated_zoom.then(|| HoldZoom::press(pointer.pos, view.to_doc(pointer.pos), pointer.time));
self.drag = Some(ZoomDrag::begin(&view, pointer.pos, self.options.scrubby_zoom, space.pixels_per_point));
if self.hold.is_some() {
@@ -541,6 +545,11 @@ impl CanvasTool for ZoomTool {
}
fn drag(&mut self, pointer: &ToolPointer, cx: &mut ToolContext<'_>) {
let space = cx.held(Key::Space);
self.space_moves |= !space;
if let (true, Some(drag), Some(view)) = (space && self.space_moves, self.drag.as_mut(), cx.space().map(|space| space.view)) {
drag.shift(&view, pointer.pos - self.last);
}
self.last = pointer.pos;
self.dragged = true;
// A hold that has not started zooming is a drag now.
+13 -3
View File
@@ -12,7 +12,7 @@ use crate::runtime::memory::{global, with_session};
use craft_behavior::canvas::zoom;
use craft_behavior::kurbo::{Point, Size};
use craft_behavior::{CanvasView, ZoomScheme};
use egui::{Context, Id, Pos2, Rect};
use egui::{Context, Id, Pos2, Rect, Vec2};
/// How far the app's view may differ from the zoom anchor, in document units, before the anchor is dropped.
const ANCHOR_TOLERANCE: f64 = 0.01;
@@ -232,8 +232,9 @@ impl HoldZoom {
}
/// A Zoom tool drag. With Scrubby Zoom on (Photoshop's default) the drag zooms continuously, right in and left out,
/// [`DOUBLING`](Self::DOUBLING) points per doubling, about the press point. Off, it draws a rectangle and the release
/// zooms to fit it (Alt: out, so the view steps one rung out about the press point). Whether a press is a drag or a
/// [`DOUBLING`](Self::DOUBLING) points per doubling, about the press point. Off, it draws a rectangle, which Space moves
/// as a marquee ([`shift`](Self::shift)), and the release zooms to fit it (Alt: out, so the view steps one rung out
/// about the press point). Whether a press is a drag or a
/// click is the host's call (`drag` reached the tool or not), not the drag's.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ZoomDrag {
@@ -266,6 +267,15 @@ impl ZoomDrag {
}
}
/// Moves a rectangle by `by` on `view`, keeping its size, as Space held during the drag moves a marquee. A rectangle
/// with no size yet stays at the press point, so the drag sizes it; a scrub has no rectangle to move.
pub fn shift(&mut self, view: &CanvasView, by: Vec2) {
if self.corner.is_some_and(|corner| corner != self.anchor) {
self.anchor += by;
self.doc = view.to_doc(self.anchor);
}
}
/// The button came up: a rectangle zooms `view` to fit it, or with `out` steps the zoom out about the press point;
/// a scrub has nothing left to do. A rectangle narrower or shorter than `least` points is too small to zoom to:
/// the view stays.
+38 -2
View File
@@ -24,6 +24,7 @@ use craft_ui::runtime::drag;
use craft_ui::runtime::frame;
use craft_ui::runtime::windows::WindowHost;
use craft_ui::style::{DataColor, StyleCx};
use craft_ui::testing::focus::key_release;
use craft_ui::testing::input::key_press;
use craft_ui::testing::layout::in_region;
use craft_ui::testing::{Fixture, MemoRun, ToolFixture, last_frame_memo_runs, pointer, trial};
@@ -1182,11 +1183,20 @@ fn momentary_canvas(main: Probe) -> CanvasRig<Id> {
}
/// `momentary_canvas` with `body` as the canvas's body.
fn momentary_canvas_with(main: Probe, mut body: impl FnMut(&mut egui::Ui, &mut craft_ui::contract::BodyCx<'_>) + 'static) -> CanvasRig<Id> {
fn momentary_canvas_with(main: Probe, body: impl FnMut(&mut egui::Ui, &mut craft_ui::contract::BodyCx<'_>) + 'static) -> CanvasRig<Id> {
momentary_canvas_under(NavigationOptions::default(), main, body)
}
/// `momentary_canvas_with` under the navigation choices `options`.
fn momentary_canvas_under(
options: NavigationOptions,
main: Probe,
mut body: impl FnMut(&mut egui::Ui, &mut craft_ui::contract::BodyCx<'_>) + 'static,
) -> CanvasRig<Id> {
let main = RefCell::new(main);
let (view, canvas) = (Rc::new(Cell::new(None::<CanvasView>)), Rc::new(Cell::new(Id::NULL)));
let (shown_view, shown_canvas) = (Rc::clone(&view), Rc::clone(&canvas));
let mut set = Momentary::new(NavigationOptions::default());
let mut set = Momentary::new(options);
let mut fixture = Fixture::new(move |ui| {
let room = ui.available_rect_before_wrap();
let current = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(600.0, 450.0), 4.0));
@@ -1230,6 +1240,32 @@ 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 zoom rectangle of a momentary Zoom that Cmd+Space started keeps being sized by the drag once Cmd is let go, and
/// moves with Space once Space is let go and pressed again, as a marquee does: the release zooms to the moved
/// rectangle. Space held since the press sizes it until let go.
#[trial]
fn space_pressed_again_moves_a_cmd_space_zoom_rectangle() {
let log = Log::default();
let mut options = NavigationOptions::default();
options.scrubby_zoom = false;
options.animated_zoom = false;
let (mut fixture, view, _) = momentary_canvas_under(options, Probe::new("main", &log), |_, _| {});
let room = fixture.by_role(Role::CANVAS)[0].rect;
let at = room.center() - Vec2::new(180.0, 90.0);
let before = view.get().expect("the canvas ran").zoom;
let (none, by) = (Modifiers::NONE, |x: f32| at + Vec2::new(x, x / 2.0));
fixture.hold(Modifiers::COMMAND).frame(vec![pointer::moved(at), key_press(Key::Space, Modifiers::COMMAND)]);
fixture.frame(vec![pointer::press(at)]).frame(vec![pointer::moved(by(100.0))]);
fixture.hold(none).frame(vec![pointer::moved(by(200.0))]);
fixture.frame(vec![key_release(Key::Space, none)]);
fixture.frame(vec![key_press(Key::Space, none), pointer::moved(by(240.0))]).frame(vec![pointer::moved(by(280.0))]);
fixture.frame(vec![key_release(Key::Space, none), pointer::moved(by(360.0))]).frame(vec![pointer::release(by(360.0))]).settle();
let zoomed = view.get().expect("the canvas ran").zoom / before;
// Moved by Space, the rectangle spans 280 x 140 points; never moved, it would span 360 x 180.
let fit = |w: f32, h: f32| f64::from((room.width() / w).min(room.height() / h));
assert!((zoomed - fit(280.0, 140.0)).abs() < (zoomed - fit(360.0, 180.0)).abs(), "zoomed by {zoomed}: to the moved rectangle");
}
/// 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]