From fc9d22f16e2537166ab84539e4cc604c80eb6056 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 13 Nov 2026 13:36:57 +0000 Subject: [PATCH] Canvas: view_follows_pointer leaves the shown view unaligned while the app's tool moves the view with the pointer --- crates/ui-canvas/src/canvas/mod.rs | 15 ++++++++++++++- crates/ui-canvas/tests/it/canvas/navigate.rs | 19 +++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index a218535c1..ad734b548 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -186,6 +186,8 @@ pub struct Canvas<'a> { context_key: bool, /// The ease the app asked for from the view it drew ([`ease_from`](Self::ease_from)). ease: Option, + /// The app's tool moves the view with the pointer this pass ([`view_follows_pointer`](Self::view_follows_pointer)). + follows_pointer: bool, } /// A canvas's view in one pass. @@ -220,6 +222,7 @@ impl<'a> Canvas<'a> { settled: None, context_key: false, ease: None, + follows_pointer: false, } .label(label) .ltr_axis() @@ -268,6 +271,16 @@ impl<'a> Canvas<'a> { self } + /// The app's tool moves the view with the pointer this pass (an image-moving crop slides the view by fractional + /// pointer distances under a frame that stays put): the shown view stays as given, not [`pixel_aligned`], so the + /// picture and the frame do not jitter by part of a pixel as the pointer moves. P-15: the tool host passes the + /// framework `CropTool`'s image mode. + #[must_use] + pub fn view_follows_pointer(mut self, follows: bool) -> Self { + self.follows_pointer = follows; + self + } + /// Leaves the view commands to the instance around the canvas that owns its view (a full-window dialog's): the /// canvas is no instance of them, so the chords reach that instance while the canvas has the focus. #[must_use] @@ -407,7 +420,7 @@ impl<'a> Canvas<'a> { Some(settled) => (settled.shown, settled.target, settled.easing, settled.keys), None => (self.given?, self.given?, false, BodyKeys::default()), }; - let shown = space::pixel_aligned(shown, pixels_per_point); + let shown = if self.follows_pointer { shown } else { space::pixel_aligned(shown, pixels_per_point) }; let space = CanvasSpace::new(shown, pixels_per_point).with_target(target).with_easing(easing).with_keys(keys); Some(space.with_document(self.document_size()).with_rulers(self.rulers).with_background(background).with_canvas(id)) } diff --git a/crates/ui-canvas/tests/it/canvas/navigate.rs b/crates/ui-canvas/tests/it/canvas/navigate.rs index 22b1f1706..7b2a6e9cb 100644 --- a/crates/ui-canvas/tests/it/canvas/navigate.rs +++ b/crates/ui-canvas/tests/it/canvas/navigate.rs @@ -107,8 +107,8 @@ fn cmd_or_ctrl_with_page_down_on_a_focused_canvas_control_scrolls_the_canvas_sid } /// The space a canvas gives its body for a view of `device` device pixels per document unit turned `rotation` -/// degrees, centred off the pixel grid. -fn space_at(device: f64, rotation: f64) -> craft_ui::canvas::CanvasSpace { +/// degrees, centred off the pixel grid, with the app's tool moving the view with the pointer or not. +fn space_at(device: f64, rotation: f64, follows_pointer: bool) -> craft_ui::canvas::CanvasSpace { let space = Rc::new(Cell::new(None)); let seen = Rc::clone(&space); let mut fixture = Fixture::new(move |ui| { @@ -117,7 +117,12 @@ fn space_at(device: f64, rotation: f64) -> craft_ui::canvas::CanvasSpace { let view = CanvasView::new(room, Point::new(100.3, 77.7), zoom).rotation(rotation); let mut navigation = NavigationOptions::default(); navigation.overscroll = true; - let shown = Canvas::new("Document").document(Size::new(400.0, 300.0)).navigation(navigation).view(view).show(ui, |_, _| {}); + let shown = Canvas::new("Document") + .document(Size::new(400.0, 300.0)) + .navigation(navigation) + .view_follows_pointer(follows_pointer) + .view(view) + .show(ui, |_, _| {}); seen.set(shown.output.space); }); fixture.settle(); @@ -125,9 +130,9 @@ fn space_at(device: f64, rotation: f64) -> craft_ui::canvas::CanvasSpace { } #[trial] -fn at_a_whole_device_zoom_with_no_rotation_the_shown_view_puts_document_pixels_on_device_pixels() { +fn at_a_whole_device_zoom_with_no_rotation_the_shown_view_puts_document_pixels_on_device_pixels_unless_it_follows_the_pointer() { for device in [2.0, 12.0] { - let space = space_at(device, 0.0); + let space = space_at(device, 0.0, false); let ppp = space.pixels_per_point; for doc in [Point::ZERO, Point::new(17.0, 29.0)] { let p = space.view.to_screen(doc) * ppp; @@ -138,6 +143,8 @@ fn at_a_whole_device_zoom_with_no_rotation_the_shown_view_puts_document_pixels_o let moved = (space.view.to_screen(Point::ZERO) - space.target().to_screen(Point::ZERO)) * ppp; assert!(moved.x.abs() <= 0.501 && moved.y.abs() <= 0.501, "{device}x: moved {moved:?}"); } - let turned = space_at(2.0, 30.0); + let turned = space_at(2.0, 30.0, false); assert_eq!(turned.view.center, turned.target().center, "a turned view is shown as it is"); + let following = space_at(2.0, 0.0, true); + assert_eq!(following.view.center, following.target().center, "a view moving with the pointer is shown as it is"); }