From 552ce4a80302d244944cd5edede1bfa15263e7f3 Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 20 Nov 2026 19:58:58 +0100 Subject: [PATCH 1/2] Canvas scroll bar arrows take no keyboard focus The document chrome's step arrows were focusable stops. Tab stopped on them, and when the canvas's focused part went away (a crop that shrank the document) focus fell through to an arrow, which then took the Escape that cancels the next crop frame. The canvas scrolls with its own keys (arrows, Page Up/Down, Home and End), and in the reference application the scroll bar arrows take no keyboard focus, so the arrows are pointer-only, like the corner. --- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 5 ++++- .../ui-canvas/tests/it/canvas/scrollbars.rs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 6c2bb011c2..710e398169 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -390,7 +390,10 @@ impl Component for CanvasScrollbars<'_> { for (key, axis, bar) in bars.each() { cx.scroll(key, bar.track, axis, bar.span.clone()); for (part, rect, _) in bar.arrows(key, axis) { - cx.part(part, rect).sense(Sense::CLICK | Sense::FOCUS).focusable(); + // As in the reference application the step arrows take no keyboard focus: the canvas scrolls with its own + // keys (the arrows, Page Up/Down, Home and End), and a focused arrow would take the Escape that ends a + // tool's edit on the canvas. + cx.part(part, rect).sense(Sense::CLICK).pointer_only("the canvas scrolls with its own keys"); } } if let Some(corner) = bars.corner { diff --git a/crates/ui-canvas/tests/it/canvas/scrollbars.rs b/crates/ui-canvas/tests/it/canvas/scrollbars.rs index a7b6e1d683..bbecbcc7d1 100644 --- a/crates/ui-canvas/tests/it/canvas/scrollbars.rs +++ b/crates/ui-canvas/tests/it/canvas/scrollbars.rs @@ -272,3 +272,22 @@ fn a_dragged_vertical_thumb_shows_the_tip_beside_it_and_nothing_when_it_is_not_d assert_eq!(fixture.texts().len(), at_rest, "bars with no tip show none"); assert_eq!(shows_tip(&fixture), None); } + +/// The step arrows scroll under the pointer but take no keyboard focus: Tab never stops on them, so the canvas keeps +/// its own keys and an Escape reaches what the canvas shows (a tool's edit), as in the reference application. +#[trial] +fn the_document_scroll_arrows_take_no_keyboard_focus() { + let (mut fixture, view, _) = document_scrollbars(); + let start = view.get().center; + let arrow = fixture.by_label("Scroll right").expect("the right arrow").center(); + fixture.frame(vec![pointer::moved(arrow)]).frame(vec![pointer::press(arrow)]).frame(vec![pointer::release(arrow)]); + assert!(view.get().center.x > start.x, "the arrow still steps"); + for step in 0..8 { + fixture.key(egui::Key::Tab, egui::Modifiers::NONE).settle(); + let on_arrow = fixture + .focused_stop() + .and_then(|(_, part)| part) + .is_some_and(|part| matches!(format!("{:?}", part.key).as_str(), "PartKey(\"vertical\")" | "PartKey(\"horizontal\")")); + assert!(!on_arrow, "Tab {step} stopped on a scroll bar part: {:?}", fixture.focused_stop()); + } +} From 6d3d29331cdf81e2e3703eb07c10f11e6f6fc657 Mon Sep 17 00:00:00 2001 From: David Mann Date: Fri, 20 Nov 2026 20:51:50 +0100 Subject: [PATCH 2/2] Canvas scroll bars: a thumb drag follows every move The canvas settles the view it is given to whole device pixels, so a thumb drag's first proposal came back with its centre a fraction of a pixel off. The value step read that as the app changing the view under the gesture and cancelled it (HostChanged); the cancelled gesture refused every later proposal until the release, so the view moved once and then stayed (photocraft#16006). The bars now take a host view with the same zoom, turn, mirroring and aspect and a centre within a point as their own value (same_value), as the number fields do with rounded values. --- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 14 ++++++ .../ui-canvas/tests/it/canvas/scrollbars.rs | 43 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 710e398169..1ef7a84f97 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -383,6 +383,20 @@ impl Component for CanvasScrollbars<'_> { Some(self.view.reborrow()) } + /// The canvas settles a view it is given to whole device pixels and to its own rect, so a thumb drag's proposal + /// comes back with its centre moved by under a point (photocraft#16006): that is the drag's own value, not the app + /// changing the view under it, and the drag goes on. A different zoom, turn, mirroring or aspect, or a centre a + /// point or more away, is a change. + fn same_value(&self, proposed: &CanvasView, host: &CanvasView) -> bool { + let reach = 1.0 / proposed.zoom.max(f64::MIN_POSITIVE); + proposed.zoom == host.zoom + && proposed.rotation == host.rotation + && proposed.flip_x == host.flip_x + && proposed.aspect == host.aspect + && (proposed.center.x - host.center.x).abs() < reach + && (proposed.center.y - host.center.y).abs() < reach + } + /// The bars: `PartsCx::scroll` runs the thumb drag and the page press in the pipeline. fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) { let Some(view) = cx.value().copied() else { return }; diff --git a/crates/ui-canvas/tests/it/canvas/scrollbars.rs b/crates/ui-canvas/tests/it/canvas/scrollbars.rs index bbecbcc7d1..c601ecd185 100644 --- a/crates/ui-canvas/tests/it/canvas/scrollbars.rs +++ b/crates/ui-canvas/tests/it/canvas/scrollbars.rs @@ -291,3 +291,46 @@ fn the_document_scroll_arrows_take_no_keyboard_focus() { assert!(!on_arrow, "Tab {step} stopped on a scroll bar part: {:?}", fixture.focused_stop()); } } + +/// Document scroll bars over a document larger than the window, at 100%, with or without the document chrome. +fn large_document_scrollbars(chrome: bool) -> (Fixture, Rc>) { + let large = Size::new(4000.0, 3000.0); + let view = Rc::new(Cell::new(CanvasView::new(DOCUMENT_WINDOW, Point::new(2000.0, 1500.0), 1.0))); + let observed = Rc::clone(&view); + let mut fixture = Fixture::new(move |ui| { + let mut next = observed.get(); + Canvas::new("Document").document(large).view(next).show_at(ui, DOCUMENT_WINDOW, |_, body| { + if let Some(space) = canvas_space(&body.env) { + next = space.view; + } + }); + let doc = DocRect::from_origin_size(Point::ZERO, large); + let bars = CanvasScrollbars::new("Scroll bars", Bind::bound(&mut next), doc); + let bars = if chrome { bars.document_chrome() } else { bars }; + let _ = bars.show_at(ui, DOCUMENT_WINDOW); + observed.set(next); + }); + fixture.room(vec2(760.0, 560.0)).settle(); + (fixture, view) +} + +/// photocraft#16006: a drag on a thumb moves the view on every pointer move until the release, not only on the first. +#[trial] +fn a_thumb_drag_follows_every_pointer_move() { + for chrome in [false, true] { + let (mut fixture, view) = large_document_scrollbars(chrome); + let track = fixture.by_label("Horizontal scroll bar").expect("the horizontal track").rect; + let start = pos2(track.center().x, track.center().y); + let before = view.get().center; + fixture.frame(vec![pointer::moved(start)]).frame(vec![pointer::press(start)]); + let mut seen = vec![view.get().center.x]; + for step in 1..=10 { + fixture.frame(vec![pointer::moved(start + vec2(20.0 * step as f32, 0.0))]); + seen.push(view.get().center.x); + } + fixture.frame(vec![pointer::release(start + vec2(200.0, 0.0))]).settle(); + let moves = seen.windows(2).filter(|pair| pair[1] > pair[0]).count(); + assert!(moves >= 9, "chrome {chrome}: the view follows each of the ten moves: {seen:?} from {before:?}"); + assert_eq!(view.get().center.y, before.y, "chrome {chrome}: a horizontal drag keeps the vertical position"); + } +}