Merge pull request #2631 from davidmann4/fix/scrollbar-arrows-pointer-only

Canvas scroll bars: thumb drag follows every move; arrows take no keyboard focus
This commit is contained in:
David Mann authored and GitHub committed 2026-11-20 22:16:12 +01:00
commit 50e00dd493
2 files changed
+80 -1

No files matched your search

+18 -1
View File
@@ -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 };
@@ -390,7 +404,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 {
@@ -272,3 +272,65 @@ 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());
}
}
/// 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<Cell<CanvasView>>) {
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");
}
}