From 4a8fff263d3aa91be72153614788d921c32fc810 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 04:52:12 +0700 Subject: [PATCH] ui-canvas: a tool drag held in the band inside the canvas edge scrolls the view, under the one edge rule hovers already use --- crates/ui-canvas/src/canvas/navigate/mod.rs | 6 +-- crates/ui-canvas/src/canvas/tool.rs | 4 +- crates/ui-canvas/src/canvas/tool_host.rs | 55 +++++++++++---------- crates/ui-canvas/tests/it/canvas/tool.rs | 35 +++++++++++-- 4 files changed, 64 insertions(+), 36 deletions(-) diff --git a/crates/ui-canvas/src/canvas/navigate/mod.rs b/crates/ui-canvas/src/canvas/navigate/mod.rs index 4e4cd8d23..becefe43e 100644 --- a/crates/ui-canvas/src/canvas/navigate/mod.rs +++ b/crates/ui-canvas/src/canvas/navigate/mod.rs @@ -256,9 +256,9 @@ impl TouchpadGlide { } } -/// Autoscroll while a tool drag runs past the canvas edge (marquee, lasso, brush, crop), or while a tool that -/// autoscrolls on hover hovers in the band inside it (a Polygonal Lasso's rubber band): the view scrolls toward the -/// pointer, faster the further past the edge, and stops at the image's edge (an image that fits is never scrolled +/// Autoscroll while a tool drag (marquee, lasso, brush, crop), or a hover of a tool that autoscrolls on hover (a +/// Polygonal Lasso's rubber band), runs in the band inside the canvas edge or past it: the view scrolls toward the +/// pointer, faster the further toward the edge, and stops at the image's edge (an image that fits is never scrolled /// into empty pasteboard). The tool host reports each pass's step in /// [`ToolHostResponse::scroll`](crate::canvas::ToolHostResponse); the app runs `Autoscroll::scroll(&mut view, step, /// size)` and asks for the next pass while the view moves. diff --git a/crates/ui-canvas/src/canvas/tool.rs b/crates/ui-canvas/src/canvas/tool.rs index 856f31108..01dcfcf7a 100644 --- a/crates/ui-canvas/src/canvas/tool.rs +++ b/crates/ui-canvas/src/canvas/tool.rs @@ -754,8 +754,8 @@ pub trait CanvasTool { button == PointerButton::Primary } - /// Whether a drag past the canvas edge asks the app to scroll the view (marquees, lassos, brushes, crop: yes; - /// the Hand and Zoom tools: no). Default: `true`. + /// Whether a drag into the band inside the canvas edge or past it asks the app to scroll the view (marquees, + /// lassos, brushes, crop: yes; the Hand and Zoom tools: no). Default: `true`. fn autoscrolls(&self) -> bool { true } diff --git a/crates/ui-canvas/src/canvas/tool_host.rs b/crates/ui-canvas/src/canvas/tool_host.rs index 124a6c78d..047f601e0 100644 --- a/crates/ui-canvas/src/canvas/tool_host.rs +++ b/crates/ui-canvas/src/canvas/tool_host.rs @@ -23,8 +23,8 @@ //! A tool that claims the nudges has AccessKit Increment and Decrement on its node, and they reach `key` as the //! arrows toward and away from the reading end. //! 6. The tool paints over the canvas and requests its cursor at tool priority, within the view on the canvas's layer. -//! 7. A drag past the canvas edge ([`CanvasTool::autoscrolls`]), and a hover in the band inside it -//! ([`CanvasTool::autoscrolls_on_hover`]), report [`ToolHostResponse::past_edge`] and the pass's +//! 7. A drag ([`CanvasTool::autoscrolls`]) or a hover ([`CanvasTool::autoscrolls_on_hover`]) in the band inside the +//! canvas edge or past it reports [`ToolHostResponse::past_edge`] and the pass's //! [`ToolHostResponse::scroll`]; the app moves its view by it with [`Autoscroll::scroll`]. //! //! The host keeps its state under the canvas's id ([`ToolPass::id`]), so two canvases never share a @@ -270,8 +270,8 @@ pub struct ToolHostResponse { pub ignored_press: Option, /// The key the tool took this pass. pub took_key: Option, - /// How far past the edge the pointer is, in screen points: past the canvas edge while a gesture of a tool that - /// autoscrolls runs, or into the band inside the view's edge (`size.hit.edge`) while a tool that autoscrolls on + /// How far toward the edge the pointer is, in screen points: into the band inside the view's edge + /// (`size.hit.edge`) or past it, while a gesture of a tool that autoscrolls runs or a tool that autoscrolls on /// hover hovers there. pub past_edge: Option, /// The autoscroll of this pass, in screen points: the [`Autoscroll`] speed for [`past_edge`](Self::past_edge) over @@ -1128,17 +1128,28 @@ impl<'a, 't: 'a> Run<'a, 't> { }) } - /// The one past-edge rule: how far `pos` lies past the canvas edge, for a drag and for a canvas control that asks to - /// scroll. An empty canvas (a window shrunk to nothing) has no edge to scroll past. + /// Reports [`edge_depth`](Self::edge_depth) at `pos` as the pass's edge overshoot. fn scroll_past(&mut self, pos: Pos2) { - if self.rect.is_positive() { - let past = pos - self.rect.clamp(pos); - if past != Vec2::ZERO { - self.response.past_edge = Some(past); - } + if let Some(past) = self.edge_depth(pos) { + self.response.past_edge = Some(past); } } + /// The one edge rule, for a drag, a hover of a tool that autoscrolls on hover and a canvas control that asks to + /// scroll: how far into the band inside the view's edge (`size.hit.edge`, at most a quarter of the view's shorter + /// side) or past it `pos` is, toward the edge. A canvas that fills the screen leaves no room past its edge. The + /// view's rect leaves out the rulers; a canvas without a view measures from its own rect, and an empty one (a + /// window shrunk to nothing) has no edge. + fn edge_depth(&self, pos: Pos2) -> Option { + let rect = self.space.map_or(self.rect, |space| space.view.rect); + if !rect.is_positive() { + return None; + } + let band = self.env.tokens.size.hit.edge.min(rect.width().min(rect.height()) / 4.0); + let past = pos - rect.shrink(band).clamp(pos); + (past != Vec2::ZERO).then_some(past) + } + /// Advances the glide of a momentary Hand that is not the tool of this pass. fn glide(&mut self) { if self.momentary == Some(MomentaryTool::Hand) { @@ -1226,10 +1237,10 @@ impl<'a, 't: 'a> Run<'a, 't> { if self.state.gesture.is_none() { return; } - // A pass delivers only the moves it was handed: a rerun pass has none. A held pointer resting past the edge - // keeps one drag per pass, since the view scrolls under it. + // A pass delivers only the moves it was handed: a rerun pass has none. A held pointer resting in the edge band + // or past it keeps one drag per pass, since the view scrolls under it. let stirred = pointer::with_pass_pointer(self.ctx, |pass| pass.stirred); - if !stirred && pos == self.rect.clamp(pos) { + if !stirred && self.edge_depth(pos).is_none() { return; } if !self.every_move(start, press) { @@ -1335,22 +1346,14 @@ impl<'a, 't: 'a> Run<'a, 't> { fn hover(&mut self, pointer: ToolPointer) { self.call(Some(pointer), |tool, cx| tool.hover(&pointer, cx)); self.hovered = true; - if let Some(past) = self.hover_edge(pointer.pos) { - self.response.past_edge = Some(past); + if self.hover_scrolls { + self.scroll_past(pointer.pos); } } - /// How far into the band inside the view's edge (`size.hit.edge`, at most a quarter of the view's shorter side) a - /// hover at `pos` is, toward the edge, for a tool that autoscrolls on hover. The view's rect leaves out the - /// rulers; a canvas without a view measures from its own rect. + /// The edge depth of a hover at `pos`, for a tool that autoscrolls on hover. fn hover_edge(&self, pos: Pos2) -> Option { - let rect = self.space.map_or(self.rect, |space| space.view.rect); - if !self.hover_scrolls || !rect.is_positive() { - return None; - } - let band = self.env.tokens.size.hit.edge.min(rect.width().min(rect.height()) / 4.0); - let past = pos - rect.shrink(band).clamp(pos); - (past != Vec2::ZERO).then_some(past) + self.edge_depth(pos).filter(|_| self.hover_scrolls) } /// The canvas background's press phase of this pass. A pass carries one phase, so a press that moved or diff --git a/crates/ui-canvas/tests/it/canvas/tool.rs b/crates/ui-canvas/tests/it/canvas/tool.rs index 142d3f3c7..d66e22635 100644 --- a/crates/ui-canvas/tests/it/canvas/tool.rs +++ b/crates/ui-canvas/tests/it/canvas/tool.rs @@ -791,15 +791,17 @@ fn a_queued_space_covers_the_moves_queued_after_it_and_not_those_after_its_relea let at = rig.centre(); let (ctx, canvas) = (rig.fixture.ctx().clone(), rig.canvas.get()); let space = craft_keymap::Key::Space; + // Short steps keep every move clear of the edge band, where a resting pointer gets a drag in each pass. + let step = BY / 2.0; for input in [ ToolInput::down(PointerButton::Primary, at), - ToolInput::moved(at + BY), + ToolInput::moved(at + step), ToolInput::hold(space), - ToolInput::moved(at + BY * 2.0), - ToolInput::moved(at + BY * 3.0), + ToolInput::moved(at + step * 2.0), + ToolInput::moved(at + step * 3.0), ToolInput::let_go(space), - ToolInput::moved(at + BY * 4.0), - ToolInput::up(at + BY * 4.0), + ToolInput::moved(at + step * 4.0), + ToolInput::up(at + step * 4.0), ] { ToolHost::inject(&ctx, canvas, input); } @@ -1618,6 +1620,29 @@ fn a_drag_past_the_canvas_edge_scrolls_the_view_and_a_turned_view_scrolls_on_the } } +/// A drag held still in the band inside the canvas edge scrolls the view: a canvas that fills the screen leaves the +/// pointer no room past its edge. A drag held away from the edge leaves the view. +#[trial] +fn a_drag_held_in_the_band_inside_the_canvas_edge_scrolls_the_view() { + let (mut fixture, view) = scroll_canvas(Rc::new(RefCell::new(Recorder::default())), 0.0); + let canvas = fixture.by_role(Role::CANVAS)[0].rect; + let centre = || view.get().expect("the canvas ran").center; + let start = canvas.center(); + fixture.frame(vec![pointer::press(start)]); + fixture.frame(vec![pointer::moved(start + Vec2::new(30.0, 0.0))]); + let held = centre(); + fixture.idle(3); + assert_eq!(centre(), held, "a drag held away from the edge leaves the view"); + + let band = Pos2::new(canvas.right() - 4.0, start.y); + fixture.frame(vec![pointer::moved(band)]); + let entered = centre(); + fixture.idle(3); + let rested = centre(); + assert!(rested.x > entered.x && rested.y == entered.y, "resting in the band scrolls toward the edge: {entered:?} -> {rested:?}"); + fixture.frame(vec![pointer::release(band)]); +} + /// A canvas whose app reads the window's intents after the tool host, last in its UI: returns the fixture and how many /// Cancels the window took. fn window_reading_canvas(tool: impl CanvasTool + 'static) -> (Fixture, Rc>) {