From bdf0c6db0b0b1e3d628aab800e0c0bc9040de019 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 13 Nov 2026 13:56:28 +0000 Subject: [PATCH] CanvasSpace::set_view: the target and the shown view by one rule for the canvas and the tool host; easing_view reads a canvas's zoom ease outside its pass --- crates/ui-canvas/src/canvas/mod.rs | 11 ++++--- crates/ui-canvas/src/canvas/space.rs | 32 +++++++++++++++----- crates/ui-canvas/src/canvas/tool.rs | 2 +- crates/ui-canvas/src/canvas/tool_host.rs | 2 +- crates/ui-canvas/src/canvas/zoom_motion.rs | 12 ++++++++ crates/ui-canvas/tests/it/canvas/navigate.rs | 14 +++++++++ 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index ad734b548..fa7251b36 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -78,7 +78,7 @@ pub use navigate::{ }; pub use rotate::{RotateView, RotateViewOutput, RotateViewStyle, ViewTurn}; pub use tools::{HandTool, RotateViewTool, ViewCommands, ViewTarget, ZoomTool}; -pub use zoom_motion::{HoldZoom, ZoomAnimation, ZoomDrag}; +pub use zoom_motion::{HoldZoom, ZoomAnimation, ZoomDrag, easing_view}; mod rulers; mod scrollbars; @@ -420,9 +420,12 @@ impl<'a> Canvas<'a> { Some(settled) => (settled.shown, settled.target, settled.easing, settled.keys), None => (self.given?, self.given?, false, BodyKeys::default()), }; - 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)) + let mut space = CanvasSpace::new(target, pixels_per_point).with_follows_pointer(self.follows_pointer); + space.set_view(target); + if easing { + space.ease_through(shown); + } + Some(space.with_keys(keys).with_document(self.document_size()).with_rulers(self.rulers).with_background(background).with_canvas(id)) } /// The press on the background part as the canvas reports it, and the pan of a middle-button drag: a middle press diff --git a/crates/ui-canvas/src/canvas/space.rs b/crates/ui-canvas/src/canvas/space.rs index e7c30b575..75f8bf07b 100644 --- a/crates/ui-canvas/src/canvas/space.rs +++ b/crates/ui-canvas/src/canvas/space.rs @@ -26,6 +26,7 @@ pub struct CanvasSpace { pub pixels_per_point: f32, target: CanvasView, easing: bool, + follows_pointer: bool, keys: BodyKeys, document: Option, rulers: Option, @@ -43,6 +44,7 @@ impl CanvasSpace { pixels_per_point, target: view, easing: false, + follows_pointer: false, keys: BodyKeys::default(), document: None, rulers: None, @@ -51,18 +53,32 @@ impl CanvasSpace { } } - /// This space with `target`, the view the canvas settled on in the pass, which `view` eases toward. + /// This space for a pass in which the app's tool moves the view with the pointer + /// ([`Canvas::view_follows_pointer`](super::Canvas::view_follows_pointer)): [`set_view`](Self::set_view) then shows + /// a view as it is, not [`pixel_aligned`]. #[must_use] - pub fn with_target(mut self, target: CanvasView) -> Self { - self.target = target; + pub fn with_follows_pointer(mut self, follows: bool) -> Self { + self.follows_pointer = follows; self } - /// This space with `view` part way to the target while a zoom eases (`easing`). - #[must_use] - pub fn with_easing(mut self, easing: bool) -> Self { - self.easing = easing; - self + /// Sets the view of this pass: [`target`](Self::target) is `view`, and the view on screen is `view` + /// [`pixel_aligned`], or `view` as it is while the pass follows the pointer. The canvas sets the view it settled + /// on, and the tool host each view a tool sets, so the calls after it start from what the canvas will show. + pub fn set_view(&mut self, view: CanvasView) { + self.target = view; + self.view = self.on_screen(view); + self.easing = false; + } + + /// Shows `shown`, part way to the target while a zoom eases, by the same rule as [`set_view`](Self::set_view). + pub(crate) fn ease_through(&mut self, shown: CanvasView) { + self.view = self.on_screen(shown); + self.easing = true; + } + + fn on_screen(&self, view: CanvasView) -> CanvasView { + if self.follows_pointer { view } else { pixel_aligned(view, self.pixels_per_point) } } /// This space with the keys the canvas left to its body in the pass. diff --git a/crates/ui-canvas/src/canvas/tool.rs b/crates/ui-canvas/src/canvas/tool.rs index 936833649..47d310a9a 100644 --- a/crates/ui-canvas/src/canvas/tool.rs +++ b/crates/ui-canvas/src/canvas/tool.rs @@ -367,7 +367,7 @@ impl<'a> ToolContext<'a> { self.requests.view = Some(view); self.requests.ease = None; if let Some(space) = self.space.as_mut() { - space.view = view; + space.set_view(view); } } diff --git a/crates/ui-canvas/src/canvas/tool_host.rs b/crates/ui-canvas/src/canvas/tool_host.rs index ccc240de5..fd4935c46 100644 --- a/crates/ui-canvas/src/canvas/tool_host.rs +++ b/crates/ui-canvas/src/canvas/tool_host.rs @@ -633,7 +633,7 @@ impl<'a> Run<'a> { self.requests.repaint |= asked.repaint; if let Some(view) = asked.view { if let Some(space) = self.space.as_mut() { - space.view = super::space::pixel_aligned(view, space.pixels_per_point); + space.set_view(view); } self.response.view = Some(view); self.requests.ease = asked.ease; diff --git a/crates/ui-canvas/src/canvas/zoom_motion.rs b/crates/ui-canvas/src/canvas/zoom_motion.rs index 00dfb1527..7766af2f7 100644 --- a/crates/ui-canvas/src/canvas/zoom_motion.rs +++ b/crates/ui-canvas/src/canvas/zoom_motion.rs @@ -101,6 +101,11 @@ impl ZoomAnimation { self.run.is_some() } + /// While a zoom eases, the view drawn last: the start of the ease until its first pass. + pub fn drawn(&self) -> Option { + self.run.map(|run| run.shown) + } + /// Stops easing: the next [`shown`](Self::shown) is the target (a pan or a scroll took over). pub fn stop(&mut self) { self.run = None; @@ -147,6 +152,13 @@ impl ViewMotion { } } +/// While the zoom of the canvas `canvas` (its id, `CanvasOutput::response.id`) eases, the view on screen: the one its +/// ease drew in the canvas's latest pass. `None` while no zoom eases. For an app that maps through the canvas's view +/// outside the canvas's pass (its tool input before the canvas shows, a panel), so it lands where the picture is. +pub fn easing_view(ctx: &Context, canvas: Id) -> Option { + with_session(ctx, ViewMotion::id(canvas), |motion: &mut ViewMotion| motion.animation.drawn()) +} + /// Asks the canvas `canvas` (its id, `CanvasOutput::response.id`) to ease its next view or to stop easing: the /// tool host for a tool's view, the Navigator for a preview zoom. The canvas applies it in its next pass. pub(crate) fn steer(ctx: &Context, canvas: Id, steer: Steer) { diff --git a/crates/ui-canvas/tests/it/canvas/navigate.rs b/crates/ui-canvas/tests/it/canvas/navigate.rs index 7b2a6e9cb..e98a00540 100644 --- a/crates/ui-canvas/tests/it/canvas/navigate.rs +++ b/crates/ui-canvas/tests/it/canvas/navigate.rs @@ -147,4 +147,18 @@ fn at_a_whole_device_zoom_with_no_rotation_the_shown_view_puts_document_pixels_o 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"); + + // A view the tool host sets in the pass is the target, shown by the same rule. + let mut set = following; + let mut moved = following.target(); + moved.center = Point::new(140.6, 90.2); + set.set_view(moved); + assert_eq!((set.view, set.target()), (moved, moved), "a view set while following the pointer is shown as it is"); + let mut set = space_at(2.0, 0.0, false); + let mut moved = set.target(); + moved.center = Point::new(140.6, 90.2); + set.set_view(moved); + assert_eq!(set.target(), moved, "the target follows the view set"); + let p = set.view.to_screen(Point::new(17.0, 29.0)) * set.pixels_per_point; + assert!((p.x - p.x.round()).abs() < 1e-3 && (p.y - p.y.round()).abs() < 1e-3, "a view set at a whole zoom is shown aligned: {p:?}"); }