From fe6da87e8b3bd82efb6a52032d842ac780009ed2 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 10:10:41 +0000 Subject: [PATCH] Canvas view commands: queued app commands, 20 pt Fit on Screen, one implementation for full-window views - Fit on Screen leaves 20 points on each side (PhotoCraft and the old framework) and fits up to the target's fit ceiling: 100 % for a document canvas, 400 % for a full-window dialog. - Canvas::queued: view commands an app asks of a document's canvas run in the canvas's loop before the fired ones, eased the same way, and never in a measuring run. - FullWindowView runs Zoom In, Zoom Out, Fit on Screen and 100% through ViewCommands::apply. --- .../src/canvas/full_window_parts/view.rs | 32 ++++++++----- crates/ui-canvas/src/canvas/mod.rs | 21 ++++++++- crates/ui-canvas/src/canvas/tools.rs | 18 ++++++-- crates/ui-canvas/tests/it/canvas/navigate.rs | 45 ++++++++++++++++++- 4 files changed, 99 insertions(+), 17 deletions(-) diff --git a/crates/ui-canvas/src/canvas/full_window_parts/view.rs b/crates/ui-canvas/src/canvas/full_window_parts/view.rs index 839ac6f13..d1fee8615 100644 --- a/crates/ui-canvas/src/canvas/full_window_parts/view.rs +++ b/crates/ui-canvas/src/canvas/full_window_parts/view.rs @@ -1,7 +1,7 @@ //! `FullWindowView`: the view of a full-window canvas as plain data. The canvas view commands //! ([`ViewCommands`](crate::canvas::ViewCommands)) act on it. -use crate::canvas::ViewCommands; +use crate::canvas::{ViewCommands, ViewTarget}; use craft_behavior::canvas::zoom; use craft_behavior::kurbo::{Point, Size}; use craft_behavior::{CanvasView, PixelSize, ZoomLadder}; @@ -20,7 +20,8 @@ const FIT_MAX: f64 = 4.0; /// view is fitted. The canvas settles it on its rect each pass (so a window resize refits a fitted view) and reports /// the next view in its output; the zoom bar steps from the same value. /// -/// - Fitted: the whole image shows centred, magnified up to 400 % (`zoom::fit_up_to`). +/// - Fitted: the whole image shows centred, magnified up to 400 % ([`ViewCommands::FitOnScreen`] with a fit ceiling of +/// 400 %). /// - A zoom (wheel, pinch, commands, the Zoom tool, the zoom bar) or a pan (Space, the middle button, the Hand tool) /// by the user ends the fit; [`ViewCommands::FitOnScreen`] returns to it. /// - Zooms stay inside `ZoomLadder`'s range for the image's size. @@ -81,10 +82,15 @@ impl FullWindowView { self } + /// What the view commands act on: the image, fitted up to [`FIT_MAX`]. + fn target(&self) -> ViewTarget { + ViewTarget::new(self.size).fit_ceiling(FIT_MAX) + } + /// Fits the view to the rect and image size the value holds, while it is fitted. fn refit(&mut self) { if self.fit && self.size.width > 0.0 && self.size.height > 0.0 { - zoom::fit_up_to(&mut self.view, self.size, self.pixels_per_point, FIT_MAX, 0.0); + ViewCommands::FitOnScreen.apply(&mut self.view, &self.target(), None, self.pixels_per_point); } } @@ -101,8 +107,8 @@ impl FullWindowView { /// Whether a zoom step in (`dir` > 0) or out would change the zoom. pub fn can_step(&self, dir: i32) -> bool { - let mut probe = self.view; - zoom::step(&mut probe, dir, self.size, self.pixels_per_point) + let command = if dir > 0 { ViewCommands::ZoomIn } else { ViewCommands::ZoomOut }; + command.available(&self.view, &self.target(), None, self.pixels_per_point) } /// Sets the device zoom (kept inside the ladder's range) about the view's centre and ends the fit. Returns whether @@ -116,14 +122,14 @@ impl FullWindowView { changed } - /// Applies a view command: the zoom commands (zoom in and out, fit on screen, 100 %). The rest (fill screen and - /// the rotations) leave the view as it is. Returns whether the view changed. Fit on screen refits on the rect and - /// image size of the last settled pass, so the zoom it reports is the fitted one at once. + /// Applies a view command: the zoom commands (zoom in and out, fit on screen, 100 %), as + /// [`ViewCommands::apply`] runs them on the image. The rest (fill screen, the other zooms and the rotations) leave + /// the view as it is. Returns whether the view changed. Fit on screen refits on the rect and image size of the last + /// settled pass, so the zoom it reports is the fitted one at once. pub fn apply(&mut self, command: ViewCommands) -> bool { match command { ViewCommands::ZoomIn | ViewCommands::ZoomOut => { - let dir = if command == ViewCommands::ZoomIn { 1 } else { -1 }; - let changed = zoom::step(&mut self.view, dir, self.size, self.pixels_per_point); + let changed = command.apply(&mut self.view, &self.target(), None, self.pixels_per_point); self.fit &= !changed; changed } @@ -133,7 +139,11 @@ impl FullWindowView { self.refit(); *self != before } - ViewCommands::ActualPixels => self.set_device_zoom(1.0), + ViewCommands::ActualPixels => { + let changed = command.apply(&mut self.view, &self.target(), None, self.pixels_per_point) || self.fit; + self.fit = false; + changed + } _ => false, } } diff --git a/crates/ui-canvas/src/canvas/mod.rs b/crates/ui-canvas/src/canvas/mod.rs index 5aa243703..238af633f 100644 --- a/crates/ui-canvas/src/canvas/mod.rs +++ b/crates/ui-canvas/src/canvas/mod.rs @@ -177,6 +177,8 @@ pub struct Canvas<'a> { /// disabled for this canvas. #[prop] layer_bounds: Option>, + /// The view commands the app asks of this canvas ([`queued`](Self::queued)). + queued: &'a [ViewCommands], /// 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] @@ -252,6 +254,7 @@ impl<'a> Canvas<'a> { given: None, document: None, layer_bounds: None, + queued: &[], navigation: NavigationOptions::default(), show_rulers: false, keys_to_body: false, @@ -313,6 +316,16 @@ impl<'a> Canvas<'a> { self } + /// 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. + #[must_use] + pub fn queued(mut self, commands: &'a [ViewCommands]) -> Self { + self.queued = commands; + 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] @@ -321,8 +334,8 @@ impl<'a> Canvas<'a> { self } - /// Publishes the view commands' state for this canvas and applies the ones routed to it, in order, to `view`: - /// the view they ask for, and whether they zoom. + /// 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)> { self.props.command_scope?; let pixels_per_point = cx.env.scale().0; @@ -333,6 +346,10 @@ impl<'a> Canvas<'a> { } let (view, document) = target?; let (mut next, mut zoomed) = (view, 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(); + } while let Some((command, _)) = cx.take_command::() { zoomed |= command.apply(&mut next, &document, self.layer_bounds, pixels_per_point) && command.zooms(); } diff --git a/crates/ui-canvas/src/canvas/tools.rs b/crates/ui-canvas/src/canvas/tools.rs index 07f067022..ee1d7507f 100644 --- a/crates/ui-canvas/src/canvas/tools.rs +++ b/crates/ui-canvas/src/canvas/tools.rs @@ -29,6 +29,9 @@ const ARTBOARD_MARGIN: Vec2 = Vec2::new(30.0, 40.0); // design: allow[literal-si /// The room Fit Layers on Screen leaves around the layers' bounds, in points. const LAYERS_MARGIN: Vec2 = Vec2::new(20.0, 20.0); // design: allow[literal-size/vec2](view-fit room in screen points, not a look) +/// The room Fit on Screen leaves around the document on each side, in points. +const FIT_MARGIN: f64 = 20.0; + /// Photoshop's default Screen Resolution (Preferences, Units & Rulers), in pixels per inch. const SCREEN_PPI: f64 = 72.0; @@ -48,12 +51,15 @@ pub struct ViewTarget { /// The screen resolution Print Size shows the document at, in pixels per inch (Preferences, Units & Rulers). /// Default: 72. pub screen_ppi: f64, + /// The highest device zoom Fit on Screen reaches (1 = 100 %). Default: 1, so a document window never opens a small + /// document magnified; a full-window dialog magnifies a small image to fill its canvas. + pub fit_ceiling: f64, } impl ViewTarget { /// A document of `size` with no artboard and no resolution. pub fn new(document: Size) -> Self { - Self { document, artboard: None, ppi: None, screen_ppi: SCREEN_PPI } + Self { document, artboard: None, ppi: None, screen_ppi: SCREEN_PPI, fit_ceiling: 1.0 } } /// The artboard Fit Artboard on Screen shows. @@ -77,6 +83,13 @@ impl ViewTarget { self } + /// The highest device zoom Fit on Screen reaches. + #[must_use] + pub fn fit_ceiling(mut self, device: f64) -> Self { + self.fit_ceiling = device; + self + } + /// Print Size's device zoom: the screen resolution over the document's. fn print_zoom(&self) -> Option { self.ppi.filter(|ppi| ppi.is_finite() && *ppi > 0.0).map(|ppi| self.screen_ppi / ppi).filter(|zoom| zoom.is_finite() && *zoom > 0.0) @@ -179,8 +192,7 @@ impl ViewCommands { ViewCommands::ZoomOut => { zoom::step(view, -1, size, pixels_per_point); } - // Photoshop fits the image to the view, with no margin. - ViewCommands::FitOnScreen => zoom::fit(view, size, pixels_per_point, 0.0), + ViewCommands::FitOnScreen => zoom::fit_up_to(view, size, pixels_per_point, target.fit_ceiling, FIT_MARGIN), ViewCommands::FillScreen => zoom::fill(view, size, pixels_per_point), ViewCommands::ActualPixels => zoom_about_centre(view, 1.0), ViewCommands::TwoHundredPercent => zoom_about_centre(view, 2.0), diff --git a/crates/ui-canvas/tests/it/canvas/navigate.rs b/crates/ui-canvas/tests/it/canvas/navigate.rs index 4a8de912c..4dbb3ea4f 100644 --- a/crates/ui-canvas/tests/it/canvas/navigate.rs +++ b/crates/ui-canvas/tests/it/canvas/navigate.rs @@ -1,7 +1,8 @@ //! Navigation rules (wheel, page keys, flick, autoscroll), the zoom motion and the momentary tool order. +use craft_behavior::canvas::zoom; use craft_behavior::kurbo::{Point, Size}; -use craft_behavior::{Axis, CanvasView, GESTURE_GAP, MotionMode, Wheel, WheelPhase, WheelUnit}; +use craft_behavior::{Axis, CanvasView, GESTURE_GAP, MotionMode, Wheel, WheelPhase, WheelUnit, ZoomLadder}; use craft_keymap::{Mods, Platform}; use craft_ui::canvas::{ Autoscroll, Canvas, CanvasSpace, Guide, GuideKey, Momentary, MomentaryCommands, MomentaryTool, NavigationOptions, TouchpadGlide, ViewCommands, @@ -311,6 +312,48 @@ fn the_view_commands_a_canvas_runs_are_in_the_frame_output_whatever_fired_them() ); } +#[trial] +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 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)); + // As an app does: the queue is dropped once a pass showed the canvas. + asked.borrow_mut().clear(); + }); + fixture.settle(); + 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); + fixture.frame(vec![]); + let fitted = view.get(); + 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. + let start = zoom::device(&fitted, ppp); + queue.borrow_mut().extend([ViewCommands::ZoomIn, ViewCommands::ZoomIn]); + assert!(commands::fire(fixture.ctx(), ViewCommands::ZoomOut, FireSource::Menu)); + fixture.frame(vec![]); + let one_in = ZoomLadder::step(start, 1, DOCUMENT); + assert!((zoom::device(&view.get(), ppp) / one_in - 1.0).abs() < 1e-6, "in, in, out: one rung in"); + let delivered: Vec<_> = frame::take_output(fixture.ctx()).delivered.into_iter().map(|fired| fired.command).collect(); + assert_eq!(delivered, [CommandRef::from(ViewCommands::ZoomOut)], "a queued command is the app's, not delivered"); + + // A dropped queue runs nothing more. + let settled = view.get(); + fixture.frame(vec![]); + assert_eq!(view.get().zoom, settled.zoom); +} + #[trial] fn a_wheel_zoom_keeps_the_point_under_the_pointer_where_an_app_widget_covers_the_body() { let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0)));