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.
This commit is contained in:
aiongg
2026-11-17 10:59:06 +00:00
parent d046011855
commit 0bfa3ca65a
4 changed files with 80 additions and 24 deletions
+46 -17
View File
@@ -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<LayerBounds<'a>>,
/// 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<crate::primitives::PaintSlot>,
}
/// 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<CanvasView>) -> 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<CanvasView>) -> 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::<ViewCommands>() {
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);
+18
View File
@@ -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)]
+13 -7
View File
@@ -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::<ViewCommands>::new()));
let (shown, asked) = (Rc::clone(&view), Rc::clone(&queue));
let on_screen = Rc::new(Cell::new(None::<CanvasView>));
let queue = Rc::new(RefCell::new(Vec::<ViewRequest>::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);
@@ -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)]);