canvas: ViewTarget::scroll(ScrollBounds::FitMargin) lets a view with Overscroll off scroll the fit inset past a larger document's edge

The Overscroll-off clamp stopped every canvas's view at the document's edges. A slide editor keeps its fit margin
beside a zoomed-in slide's edge, so a zoom out about a point near the edge kept the point; stopped at the edge, the
clamp moved it. ScrollBounds::Document stays the default (Photoshop's).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Dylan M. TaylorandClaude Opus 5.5 committed 2026-11-28 22:54:16 +00:00
1 parent 74cd72a551
commit 4e2e30f1eb
5 files changed
+132 -24

No files matched your search

+18 -12
View File
@@ -76,7 +76,7 @@ pub use navigate::{
WheelInput, WheelNavigation, WheelScheme, pan, wheel_turn,
};
pub use rotate::{RotateView, RotateViewOutput, RotateViewStyle, ViewTurn};
pub use tools::{HandTool, LayerBounds, RotateViewTool, ViewCommands, ViewFit, ViewRequest, ViewTarget, ZoomTool};
pub use tools::{HandTool, LayerBounds, RotateViewTool, ScrollBounds, ViewCommands, ViewFit, ViewRequest, ViewTarget, ZoomTool};
pub use zoom_motion::{HoldZoom, ZoomAnimation, ZoomDrag, easing_view};
pub(crate) mod rulers;
@@ -457,6 +457,11 @@ impl<'a> Canvas<'a> {
self.document.map(|target| target.document)
}
/// The document's size and the room, in points, the view may scroll past its edges with Overscroll off.
fn scroll_bounds(&self, cx: &Cx<'_, Self>) -> Option<(Size, f64)> {
self.target(cx).map(|target| (target.document, target.scroll_margin()))
}
/// The view of this pass from the app's `given` view: this pass's rect, the first fit, the wheel with its ease and
/// the touchpad glide, a middle-button pan or autoscroll, the view commands, the Overscroll clamp, and the zoom
/// ease.
@@ -605,7 +610,7 @@ impl<'a> Canvas<'a> {
_ => {}
}
}
let target = clamped(view, self.document_size(), &self.navigation);
let target = clamped(view, self.scroll_bounds(cx), &self.navigation);
if glide != Vec2::ZERO {
motion.glide.moved(target != before_glide);
}
@@ -854,7 +859,7 @@ impl<'a> Canvas<'a> {
space,
view: self.settled.map(|settled| settled.target).filter(|target| Some(*target) != self.given),
snap: self.snapped(cx.env.shell().os),
document: self.document_size(),
scroll_bounds: self.scroll_bounds(cx),
navigation: self.navigation,
editor_input: self.editor_input.clone(),
editor: cx.editor_handle(),
@@ -927,7 +932,7 @@ pub struct ToolPass {
/// The canvas's view of this pass, when it differs from the view the app gave, as [`CanvasOutput::view`].
pub view: Option<CanvasView>,
pub(crate) snap: Option<Snap>,
pub(crate) document: Option<Size>,
pub(crate) scroll_bounds: Option<(Size, f64)>,
pub(crate) navigation: NavigationOptions,
/// The editor part's inputs of this pass, in the order they arrived; the body applies them to its own document.
pub editor_input: Arc<[EditorInput]>,
@@ -960,10 +965,10 @@ impl ToolPass {
}
}
/// `view` as the canvas keeps it: inside the document's edges with Overscroll off. The tool host passes a tool's
/// view through it.
/// `view` as the canvas keeps it: inside the document's scroll bounds with Overscroll off. The tool host passes a
/// tool's view through it.
pub(crate) fn clamped(&self, view: CanvasView) -> CanvasView {
clamped(view, self.document, &self.navigation)
clamped(view, self.scroll_bounds, &self.navigation)
}
/// Hides the pointer for this pass while it is over the canvas, over every other request, links and the tool's
@@ -1139,15 +1144,16 @@ impl Component for Canvas<'_> {
impl Container for Canvas<'_> {}
/// `view` inside the document's edges with Overscroll off: the one clamp of a canvas's view, the canvas's own and a
/// tool's alike. A view with no rect yet, or a canvas without a document, is left as it is.
fn clamped(mut view: CanvasView, document: Option<Size>, navigation: &NavigationOptions) -> CanvasView {
if let Some(document) = document
/// `view` inside the document's scroll bounds with Overscroll off: the one clamp of a canvas's view, the canvas's own
/// and a tool's alike. `bounds` is the document's size and the room, in points, the view may scroll past its edges
/// ([`ViewTarget::scroll`]). A view with no rect yet, or a canvas without a document, is left as it is.
fn clamped(mut view: CanvasView, bounds: Option<(Size, f64)>, navigation: &NavigationOptions) -> CanvasView {
if let Some((document, margin)) = bounds
&& !navigation.overscroll
&& view.rect.width() > 0.0
&& view.rect.height() > 0.0
{
CanvasScrollbars::clamp(&mut view, DocRect::from_origin_size(Point::ZERO, document));
CanvasScrollbars::clamp_with_margin(&mut view, DocRect::from_origin_size(Point::ZERO, document), margin);
}
view
}
+15 -7
View File
@@ -342,8 +342,9 @@ fn moved(view: &CanvasView, doc: DocRect, axis: Axis, range: &Range<f64>) -> Can
/// one smaller than the view (or exactly its size) is centred in it, so zooming out around the pointer keeps it in the
/// middle. Photoshop 25.4 was measured leaving a smaller document where zooming put it; reports of Photoshop itself
/// say it stays centred with Overscroll off, which is what turning Overscroll off asks for. Overscroll on keeps the
/// free view.
fn clamp_axis(center: f64, len: f64, size: f64, zoom: f64) -> f64 {
/// free view. A larger document may scroll `margin` screen points past each edge
/// ([`ScrollBounds::FitMargin`](crate::canvas::ScrollBounds::FitMargin)).
fn clamp_axis(center: f64, len: f64, size: f64, zoom: f64, margin: f64) -> f64 {
if !(zoom.is_finite() && zoom > 0.0 && len.is_finite() && len > 0.0 && size.is_finite() && size > 0.0) {
return center;
}
@@ -354,7 +355,8 @@ fn clamp_axis(center: f64, len: f64, size: f64, zoom: f64) -> f64 {
if 2.0 * half >= len {
return len / 2.0;
}
center.clamp(half, len - half)
let reach = margin / zoom;
center.clamp(half - reach, len - half + reach)
}
/// Whether `host`, a view the app wrote back while a drag proposed `proposed`, is that proposal as the canvas settled
@@ -452,6 +454,12 @@ impl<'a> CanvasScrollbars<'a> {
/// Applies overscroll off to `view` over the document `doc`: the view can't leave the document's edges, and a
/// document smaller than the view is centred in it. True when the view moved (then repaint).
pub fn clamp(view: &mut CanvasView, doc: DocRect) -> bool {
Self::clamp_with_margin(view, doc, 0.0)
}
/// [`clamp`](Self::clamp), with a document larger than the view free to scroll `margin` screen points past each
/// edge ([`ScrollBounds::FitMargin`](crate::canvas::ScrollBounds::FitMargin)).
pub(crate) fn clamp_with_margin(view: &mut CanvasView, doc: DocRect, margin: f64) -> bool {
if !(doc.width() > 0.0 && doc.height() > 0.0) || !view.zoom.is_finite() || view.zoom <= 0.0 {
return false;
}
@@ -460,8 +468,8 @@ impl<'a> CanvasScrollbars<'a> {
let zoom = view.zoom;
let local = if view.rotation.abs() < 1e-8 {
[
clamp_axis(view.center.x - doc.x0, w, f64::from(size.x) / aspect(view), zoom),
clamp_axis(view.center.y - doc.y0, h, f64::from(size.y), zoom),
clamp_axis(view.center.x - doc.x0, w, f64::from(size.x) / aspect(view), zoom, margin / aspect(view)),
clamp_axis(view.center.y - doc.y0, h, f64::from(size.y), zoom, margin),
]
} else {
if !size.is_finite() || size.x <= 0.0 || size.y <= 0.0 {
@@ -478,8 +486,8 @@ impl<'a> CanvasScrollbars<'a> {
let offset = view.rect.center() - image.min;
let (ox, oy) = (f64::from(offset.x), f64::from(offset.y));
let wanted = Vec2::new(
(clamp_axis(ox, f64::from(image.width()), f64::from(size.x), 1.0) - ox) as f32,
(clamp_axis(oy, f64::from(image.height()), f64::from(size.y), 1.0) - oy) as f32,
(clamp_axis(ox, f64::from(image.width()), f64::from(size.x), 1.0, margin) - ox) as f32,
(clamp_axis(oy, f64::from(image.height()), f64::from(size.y), 1.0, margin) - oy) as f32,
);
let d = level.unmap_vec(wanted);
[view.center.x + d.x - doc.x0, view.center.y + d.y - doc.y0]
+19 -2
View File
@@ -1,8 +1,8 @@
//! Stories of the canvas family.
use super::{
Canvas, CanvasTool, ContextSource, CropOverlay, MiddleButton, NavigationOptions, NoTool, ToolAction, ToolContext, ToolHost, ToolPath,
ToolPointer, ViewFit, ViewTarget, WheelScheme, tool_pass,
Canvas, CanvasTool, ContextSource, CropOverlay, MiddleButton, NavigationOptions, NoTool, ScrollBounds, ToolAction, ToolContext, ToolHost,
ToolPath, ToolPointer, ViewFit, ViewTarget, WheelScheme, tool_pass,
};
use crate::buttons::Button;
use crate::canvas::testing::canvas_story::{StoryDocument, StoryPicture, StoryShape, accent, paint_shapes, show_document};
@@ -60,6 +60,23 @@ fn edge_to_edge_fit(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
fitted_document(ui, data, ViewFit::EdgeToEdge)
}
/// A slide editor's canvas ([`ScrollBounds::FitMargin`]) zoomed in and scrolled to the document's right edge: the fit
/// inset shows past the edge.
#[story(Canvas, covers = [Default])]
fn fit_margin_scroll(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let room = stage(ui);
let size = Size::new(640.0, 400.0);
let (view, view_at) = data.value_at("view", CanvasView::new(room, Point::new(size.width, size.height / 2.0), 2.0));
let shapes = [StoryShape::filled(&DocRect::new(40.0, 40.0, 600.0, 360.0), accent(ui, 0.6))];
let picture = StoryPicture::new(640, 400, |p| paint_shapes(&shapes, Color32::TRANSPARENT, p));
Canvas::new("Document")
.document(ViewTarget::new(size).scroll(ScrollBounds::FitMargin))
.view(view_at.bind(view))
.show_at(ui, room, |ui, body| show_document(ui, body.rect, &picture))
.output
.into()
}
#[story(Canvas, covers = [Default])]
fn zoomed(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
let room = stage(ui);
+41 -1
View File
@@ -47,6 +47,20 @@ pub enum ViewFit {
EdgeToEdge,
}
/// How far a view with Overscroll off scrolls a document larger than it. A document smaller than the view is centred
/// either way.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ScrollBounds {
/// Up to the document's edges, as Photoshop's.
#[default]
Document,
/// Up to the room editor fitting leaves past each edge, as a slide or page editor's, so a zoomed-in document's
/// edge can still show with that room beside it, and a zoom out about a point near an edge keeps the point.
/// [`ViewFit::EdgeToEdge`] leaves no room.
FitMargin,
}
/// What the view commands act on: the document's size and the facts some commands need. The app gives it to
/// [`Canvas::document`](super::Canvas::document); a command whose fact is absent is disabled
/// ([`ViewCommands::available`]). Fit Layers reads the layers' bounds from [`LayerBounds`] instead, which only the
@@ -69,6 +83,8 @@ pub struct ViewTarget {
pub fit_ceiling: f64,
/// The policy shared by initial fitting and Fit on Screen.
pub fit: ViewFit,
/// How far a view with Overscroll off scrolls past the document's edges. Default: [`ScrollBounds::Document`].
pub scroll: ScrollBounds,
/// The room editor fitting leaves around the document at each side, in points: the theme's `space.inset.fit` in a
/// canvas.
pub(crate) fit_margin: f64,
@@ -84,7 +100,31 @@ 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, fit_ceiling: 1.0, fit: ViewFit::Editor, fit_margin: FIT_MARGIN }
Self {
document,
artboard: None,
ppi: None,
screen_ppi: SCREEN_PPI,
fit_ceiling: 1.0,
fit: ViewFit::Editor,
scroll: ScrollBounds::Document,
fit_margin: FIT_MARGIN,
}
}
/// How far a view with Overscroll off scrolls past the document's edges.
#[must_use]
pub fn scroll(mut self, scroll: ScrollBounds) -> Self {
self.scroll = scroll;
self
}
/// The room, in points, a view with Overscroll off scrolls past each edge of a document larger than it.
pub(crate) fn scroll_margin(&self) -> f64 {
match (self.scroll, self.fit) {
(ScrollBounds::FitMargin, ViewFit::Editor) => self.fit_margin,
_ => 0.0,
}
}
/// The artboard Fit Artboard on Screen shows.
+39 -2
View File
@@ -2,12 +2,13 @@
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::Arc;
use craft_behavior::kurbo::{Point, Rect as DocRect, Size};
use craft_behavior::{Axis, CancelReason, CanvasView, PartKey, PointerButton};
use craft_ui::canvas::{
Canvas, CanvasScrollbars, CanvasTool, NavigationOptions, Rulers, ShownBars, ToolAction, ToolContext, ToolHost, ToolPath, ToolPointer,
canvas_space, tool_pass,
Canvas, CanvasScrollbars, CanvasTool, NavigationOptions, Rulers, ScrollBounds, ShownBars, ToolAction, ToolContext, ToolHost, ToolPath,
ToolPointer, ViewTarget, canvas_space, tool_pass,
};
use craft_ui::contract::{Bind, Show as _, ShowContainer as _, THUMB};
use craft_ui::primitives::Draw;
@@ -60,6 +61,42 @@ fn overscroll_off_below_fit_keeps_the_image_centred() {
assert!((image.center().y - VIEW.center().y).abs() < TOLERANCE, "the short axis is centred: {image:?}");
}
/// A slide editor's canvas ([`ScrollBounds::FitMargin`]) scrolls a document larger than its view up to the fit inset
/// past an edge with Overscroll off, as production DeckCraft's slide scrolled, so a zoom out about a point near that
/// edge keeps the point under the pointer; the default stops at the edge.
#[trial]
fn fit_margin_bounds_let_a_larger_document_scroll_the_fit_inset_past_its_edge() {
const INSET: f32 = 40.0;
let document = Size::new(1600.0, 900.0);
let room = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0));
// The room the shown document's right edge leaves inside the view's, for a view given `past` points past that edge.
let gap = |scroll: ScrollBounds, past: f64| {
let mut sizes = craft_theme::AppSizes::default();
sizes.all.insert("space.inset.fit".to_owned(), craft_theme::SizeValue::Number(INSET));
let mut root = craft_ui::runtime::env::RootEnv::default();
root.sizes = Arc::new(sizes);
let center = Point::new(document.width - f64::from(room.width()) / 2.0 + past, document.height / 2.0);
let view = Rc::new(Cell::new(CanvasView::new(room, center, 1.0)));
let shown = Rc::clone(&view);
let mut fixture = Fixture::new(move |ui| {
let given = shown.get();
let target = ViewTarget::new(document).scroll(scroll);
let output = Canvas::new("Document").document(target).view(given).show_at(ui, room, |_, _| {}).output;
shown.set(output.view.unwrap_or(given));
});
fixture.root(root).room(room.size()).settle();
let view = view.get();
view.rect.right() - view.to_screen(Point::new(document.width, document.height / 2.0)).x
};
let near = f64::from(INSET) / 2.0;
let stopped = gap(ScrollBounds::Document, near);
assert!(stopped.abs() < 0.5, "by default the view stops at the document's edge: {stopped}");
let kept = gap(ScrollBounds::FitMargin, near);
assert!((kept - INSET / 2.0).abs() < 0.5, "a view inside the fit inset stays where it was: {kept}");
let far = gap(ScrollBounds::FitMargin, 3.0 * f64::from(INSET));
assert!(far > INSET / 2.0 && far < INSET + 0.5, "a view past the fit inset stops at it: {far}");
}
const FITTING_DOCUMENT: Size = Size::new(400.0, 300.0);
const DOCUMENT_WINDOW: Rect = Rect::from_min_max(Pos2::new(12.0, 12.0), Pos2::new(732.0, 512.0));