From 0bfa3ca65a33e8abd02ff93933f659c7635a4ca1 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 10:59:06 +0000 Subject: [PATCH] Canvas::queued takes ViewRequest: user commands ease, app placements jump A new or cropped document's fit and a new arrangement's fits place the view, which jumps as PhotoCraft's placements did; a menu row or an action step eases under Animated Zoom as its chord does. The full-window wheel trial turns Overscroll on: after the 20-point fit one notch leaves the image smaller than the canvas, which Overscroll off centres. --- crates/ui-canvas/src/canvas/mod.rs | 63 ++++++++++++++----- crates/ui-canvas/src/canvas/tools.rs | 18 ++++++ crates/ui-canvas/tests/it/canvas/navigate.rs | 20 +++--- .../ui-canvas/tests/it/full_window_parts.rs | 3 + 4 files changed, 80 insertions(+), 24 deletions(-) diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index 238af633f..1777123e8 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -75,7 +75,7 @@ pub use navigate::{ Autoscroll, FlickPan, NavigateBar, NavigateBarOutput, NavigationOptions, PageKey, TouchpadGlide, WheelAction, WheelInput, WheelNavigation, pan, }; pub use rotate::{RotateView, RotateViewOutput, RotateViewStyle, ViewTurn}; -pub use tools::{HandTool, LayerBounds, RotateViewTool, ViewCommands, ViewTarget, ZoomTool}; +pub use tools::{HandTool, LayerBounds, RotateViewTool, ViewCommands, ViewRequest, ViewTarget, ZoomTool}; pub use zoom_motion::{HoldZoom, ZoomAnimation, ZoomDrag, easing_view}; mod rulers; @@ -178,7 +178,7 @@ pub struct Canvas<'a> { #[prop] layer_bounds: Option>, /// The view commands the app asks of this canvas ([`queued`](Self::queued)). - queued: &'a [ViewCommands], + queued: &'a [ViewRequest], /// The user's navigation options: how the wheel and a pinch move the view ([`WheelNavigation`]), whether a zoom /// eases (Animated Zoom) and whether the view may leave the document (Overscroll). #[prop] @@ -229,6 +229,17 @@ pub struct Canvas<'a> { picture: Option, } +/// How a pass's view commands move the view. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum CommandMotion { + /// No command zoomed or placed it (a turn). + Keep, + /// A command zoomed it: the zoom eases under Animated Zoom. + Ease, + /// The app placed it: the view jumps. + Jump, +} + /// A canvas's view in one pass. #[derive(Clone, Copy, Debug)] struct Settled { @@ -317,11 +328,12 @@ impl<'a> Canvas<'a> { } /// View commands the app asks of this canvas's view (a menu row or an action step it runs, a new document's fit): - /// the canvas runs them in this pass, in order and before the ones fired to it, as it runs those, eases included. - /// A measuring run runs none. The app asks for them until a pass that is not a measuring run showed the canvas, - /// then drops them. + /// the canvas runs them in this pass, in order and before the ones fired to it, as it runs those. A + /// [`ViewRequest::Command`] eases as a fired command does; a [`ViewRequest::Place`] that moves the view makes the + /// pass's view jump. A measuring run runs none. The app asks for them until a pass that is not a measuring run + /// showed the canvas, then drops them. #[must_use] - pub fn queued(mut self, commands: &'a [ViewCommands]) -> Self { + pub fn queued(mut self, commands: &'a [ViewRequest]) -> Self { self.queued = commands; self } @@ -335,8 +347,8 @@ impl<'a> Canvas<'a> { } /// Publishes the view commands' state for this canvas and applies the ones the app queued, then the ones routed to - /// it, in order, to `view`: the view they ask for, and whether they zoom. - fn view_commands(&self, cx: &mut Cx<'_, Self>, view: Option) -> Option<(CanvasView, bool)> { + /// it, in order, to `view`: the view they ask for, and how the pass moves to it. + fn view_commands(&self, cx: &mut Cx<'_, Self>, view: Option) -> Option<(CanvasView, CommandMotion)> { self.props.command_scope?; let pixels_per_point = cx.env.scale().0; let target = view.zip(self.document); @@ -345,15 +357,27 @@ impl<'a> Canvas<'a> { cx.publish(command, CommandState::new().enabled(enabled)); } let (view, document) = target?; - let (mut next, mut zoomed) = (view, false); + let (mut next, mut zoomed, mut placed) = (view, false, false); let queued = if measure::active(&cx.core.ctx) { &[][..] } else { self.queued }; - for command in queued { - zoomed |= command.apply(&mut next, &document, self.layer_bounds, pixels_per_point) && command.zooms(); + for request in queued { + let command = request.command(); + let changed = command.apply(&mut next, &document, self.layer_bounds, pixels_per_point); + match request { + ViewRequest::Command(_) => zoomed |= changed && command.zooms(), + ViewRequest::Place(_) => placed |= changed, + } } while let Some((command, _)) = cx.take_command::() { zoomed |= command.apply(&mut next, &document, self.layer_bounds, pixels_per_point) && command.zooms(); } - (next != view).then_some((next, zoomed)) + let motion = if placed { + CommandMotion::Jump + } else if zoomed { + CommandMotion::Ease + } else { + CommandMotion::Keep + }; + (next != view).then_some((next, motion)) } /// The document's size, from the target the app gave. @@ -436,12 +460,17 @@ impl<'a> Canvas<'a> { steer = Some(Steer::Stop); } let before = view; - if let Some((next, zoomed)) = self.view_commands(cx, Some(view)) { + if let Some((next, motion)) = self.view_commands(cx, Some(view)) { view = next; - if zoomed && self.navigation.animated_zoom { - // A zoom that keeps the centre eases about it; one that moves the centre (Fit on Screen) eases freely. - let anchor = (next.center == before.center).then_some((before.center, before.rect.center())); - steer = Some(Steer::Ease { from: before, anchor }); + match motion { + CommandMotion::Ease if self.navigation.animated_zoom => { + // A zoom that keeps the centre eases about it; one that moves the centre (Fit on Screen) eases + // freely. + let anchor = (next.center == before.center).then_some((before.center, before.rect.center())); + steer = Some(Steer::Ease { from: before, anchor }); + } + CommandMotion::Jump => steer = Some(Steer::Stop), + _ => {} } } let target = clamped(view, self.document_size(), &self.navigation); diff --git a/crates/ui-canvas/src/canvas/tools.rs b/crates/ui-canvas/src/canvas/tools.rs index ee1d7507f..87ddf1ac5 100644 --- a/crates/ui-canvas/src/canvas/tools.rs +++ b/crates/ui-canvas/src/canvas/tools.rs @@ -240,6 +240,24 @@ impl ViewCommands { } } +/// A view command an app asks of its canvas ([`Canvas::queued`](super::Canvas::queued)). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ViewRequest { + /// A command the user gave (a menu row, an action step): it eases under Animated Zoom, as its chord does. + Command(ViewCommands), + /// The app placing the view (a new or cropped document's fit, a new arrangement's fits): the view jumps there. + Place(ViewCommands), +} + +impl ViewRequest { + /// The view command asked for. + pub fn command(self) -> ViewCommands { + match self { + ViewRequest::Command(command) | ViewRequest::Place(command) => command, + } + } +} + /// The Hand tool: a drag pans the view with the document under the pointer; a release while still moving glides on /// with [`FlickPan`] when Flick Panning is on; Escape during the drag puts the view back. It does not autoscroll. #[derive(Clone, Debug)] diff --git a/crates/ui-canvas/tests/it/canvas/navigate.rs b/crates/ui-canvas/tests/it/canvas/navigate.rs index 4dbb3ea4f..3bab71e46 100644 --- a/crates/ui-canvas/tests/it/canvas/navigate.rs +++ b/crates/ui-canvas/tests/it/canvas/navigate.rs @@ -6,7 +6,7 @@ use craft_behavior::{Axis, CanvasView, GESTURE_GAP, MotionMode, Wheel, WheelPhas use craft_keymap::{Mods, Platform}; use craft_ui::canvas::{ Autoscroll, Canvas, CanvasSpace, Guide, GuideKey, Momentary, MomentaryCommands, MomentaryTool, NavigationOptions, TouchpadGlide, ViewCommands, - WheelNavigation, + ViewRequest, WheelNavigation, }; use craft_ui::canvas_controls::Guides; use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _}; @@ -316,14 +316,16 @@ fn the_view_commands_a_canvas_runs_are_in_the_frame_output_whatever_fired_them() fn a_canvas_runs_the_view_commands_its_app_queues_before_the_fired_ones_and_reports_only_the_fired() { const DOCUMENT: Size = Size::new(4000.0, 4000.0); let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0))); - let queue = Rc::new(RefCell::new(Vec::::new())); - let (shown, asked) = (Rc::clone(&view), Rc::clone(&queue)); + let on_screen = Rc::new(Cell::new(None::)); + let queue = Rc::new(RefCell::new(Vec::::new())); + let (shown, seen, asked) = (Rc::clone(&view), Rc::clone(&on_screen), Rc::clone(&queue)); let mut fixture = Fixture::new(move |ui| { let mut given = shown.get(); given.rect = ui.available_rect_before_wrap(); let commands = asked.borrow().clone(); let canvas = Canvas::new("Document").document(DOCUMENT).view(given).queued(&commands).show(ui, |_, _| {}); shown.set(canvas.output.view.unwrap_or(given)); + seen.set(canvas.output.space.map(|space| space.view)); // As an app does: the queue is dropped once a pass showed the canvas. asked.borrow_mut().clear(); }); @@ -331,16 +333,20 @@ fn a_canvas_runs_the_view_commands_its_app_queues_before_the_fired_ones_and_repo frame::take_output(fixture.ctx()); let ppp = fixture.ctx().pixels_per_point(); - // Fit on Screen leaves room around the whole document. - queue.borrow_mut().push(ViewCommands::FitOnScreen); + // The app placing the view jumps there, also while a zoom the user asked for eases, and Fit on Screen leaves room + // around the document. + queue.borrow_mut().push(ViewRequest::Command(ViewCommands::ZoomIn)); + fixture.frame(vec![]); + queue.borrow_mut().push(ViewRequest::Place(ViewCommands::FitOnScreen)); fixture.frame(vec![]); let fitted = view.get(); + assert_eq!(on_screen.get().map(|shown| (shown.zoom, shown.center)), Some((fitted.zoom, fitted.center)), "a placed view jumps"); let extent = fitted.extent(craft_behavior::kurbo::Rect::from_origin_size(Point::ORIGIN, DOCUMENT)); assert!(extent.x < fitted.rect.width() && extent.y < fitted.rect.height(), "the fit leaves room on every side"); - // Two queued Zoom Ins and a fired Zoom Out: the queue runs first, in order, then the fired command. + // Two asked Zoom Ins and a fired Zoom Out: the queue runs first, in order, then the fired command. let start = zoom::device(&fitted, ppp); - queue.borrow_mut().extend([ViewCommands::ZoomIn, ViewCommands::ZoomIn]); + queue.borrow_mut().extend([ViewRequest::Command(ViewCommands::ZoomIn), ViewRequest::Command(ViewCommands::ZoomIn)]); assert!(commands::fire(fixture.ctx(), ViewCommands::ZoomOut, FireSource::Menu)); fixture.frame(vec![]); let one_in = ZoomLadder::step(start, 1, DOCUMENT); diff --git a/crates/ui-canvas/tests/it/full_window_parts.rs b/crates/ui-canvas/tests/it/full_window_parts.rs index 91b6b16f8..a803f723d 100644 --- a/crates/ui-canvas/tests/it/full_window_parts.rs +++ b/crates/ui-canvas/tests/it/full_window_parts.rs @@ -114,6 +114,9 @@ mod canvas_navigation { let mut navigation = NavigationOptions::default(); navigation.zoom_with_wheel = true; navigation.animated_zoom = false; + // Overscroll on: the fit leaves room around the image, and with Overscroll off an image still smaller than the + // canvas after one notch is centred again, whatever the pointer. + navigation.overscroll = true; let mut zooming = rig(navigation); let fitted = zooming.zoom(); zooming.fixture.frame(vec![pointer::moved(OVER)]);