From 8326e69c21cad729b78a271e4366160e44b76081 Mon Sep 17 00:00:00 2001 From: Martin Ball <98639962+benandez@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:15:24 +0000 Subject: [PATCH 1/3] Merge pull request #3218 from mocenigo/page-view-fit-width pages: no horizontal scroll bar while the pages fit the view's width (cherry picked from commit 58acd23cdc96a491d5f735cc0823adb993fba6e9) --- crates/pages/src/ui/view.rs | 4 + crates/pages/tests/it/views.rs | 76 +++++++++++++++++++ crates/ui-canvas/src/canvas/scrollbars/mod.rs | 14 +++- 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/crates/pages/src/ui/view.rs b/crates/pages/src/ui/view.rs index 2e344d73a6..46522ae8e2 100644 --- a/crates/pages/src/ui/view.rs +++ b/crates/pages/src/ui/view.rs @@ -1200,6 +1200,10 @@ fn run_pass( Label::from(words.page_of.fill(&[("page", label.as_str()), ("count", total.as_str())])) }; let bars = if count > 0 { bars.tip(&tip) } else { bars }; + // Pages that fit the view's width can't scroll sideways, so there is no horizontal bar. The layout drawn + // decides, as the bars measure it. + let (x_lo, x_hi) = offset_range(drawn.doc.x0, drawn.doc.x1, vw); + let bars = if x_lo >= x_hi { bars.vertical_only() } else { bars }; // Full screen's edge-to-edge view (`PageView::fill_screen`) has no scroll bars: nothing is drawn and nothing takes a press // along the window's edges. let bar_moved = if build.fill_screen { diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index e4e60c79ac..f0ec0c2e8e 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -107,6 +107,82 @@ impl PageSource for SmallPages { } } +/// Whether the view shows a horizontal scroll bar at `zoom` in a window 900 wide. +fn has_horizontal_bar(zoom: f64) -> bool { + let (fixture, _, _) = layout_rig( + 3, + state_with(|s| { + s.fit = Fit::None; + s.zoom = zoom; + }), + ); + horizontal_bar(&fixture).is_some() +} + +#[trial] +fn the_horizontal_bar_shows_only_when_the_pages_are_wider_than_the_view() { + assert!(!has_horizontal_bar(0.5), "pages that fit the width need no horizontal bar"); + assert!(has_horizontal_bar(3.0), "pages wider than the view scroll sideways"); +} + +/// The horizontal scroll bar the fixture shows, if any. +fn horizontal_bar(fixture: &Fixture) -> Option { + fixture.by_role(Role::SCROLL_BAR).iter().map(|bar| bar.rect).find(|rect| rect.width() > rect.height()) +} + +#[trial] +fn a_horizontal_thumb_held_while_the_pages_come_to_fit_keeps_its_bar_until_the_release() { + let (mut fixture, state, _) = layout_rig( + 3, + state_with(|s| { + s.fit = Fit::None; + s.zoom = 3.0; + }), + ); + let bar = horizontal_bar(&fixture).expect("pages wider than the view have a horizontal bar"); + // The view starts at the left edge, so the thumb is at the start of the track. + let from = pos2(bar.left() + 4.0, bar.center().y); + fixture.drag_hold_at(from, vec2(20.0, 0.0)); + state.borrow_mut().zoom = 0.5; + fixture.idle(3); + assert!(horizontal_bar(&fixture).is_some(), "the held thumb keeps its bar, so its release reaches it"); + fixture.release_at(from + vec2(20.0, 0.0)).idle(2); + assert!(horizontal_bar(&fixture).is_none(), "after the release, pages that fit have no horizontal bar"); +} + +#[trial] +fn a_horizontal_thumb_released_while_the_bars_are_not_shown_leaves_no_bar_behind() { + let state = Rc::new(RefCell::new(state_with(|s| { + s.fit = Fit::None; + s.zoom = 3.0; + }))); + let full_screen = Rc::new(Cell::new(false)); + let (kept, edge_to_edge) = (Rc::clone(&state), Rc::clone(&full_screen)); + let pages = DemoPages::new(3); + let mut fixture = Fixture::new(move |ui| { + let mut now = kept.borrow().clone(); + PageView::new("Pages", &pages, &mut now, PAPER).options(LayoutOptions::ACROBAT).fill_screen(edge_to_edge.get()).show(ui, |_, _| ()); + *kept.borrow_mut() = now; + }); + fixture.room(vec2(900.0, 600.0)); + fixture.settle(); + let bar = horizontal_bar(&fixture).expect("pages wider than the view have a horizontal bar"); + let from = pos2(bar.left() + 4.0, bar.center().y); + fixture.drag_hold_at(from, vec2(20.0, 0.0)); + // Full screen mid-drag: the bars go, and the release can't reach them. + full_screen.set(true); + fixture.idle(2); + fixture.release_at(from + vec2(20.0, 0.0)).idle(2); + full_screen.set(false); + { + let mut state = state.borrow_mut(); + state.fit = Fit::None; + state.zoom = 0.5; + } + fixture.idle(3); + assert!(horizontal_bar(&fixture).is_none(), "a press that ended unseen leaves no horizontal bar over pages that fit"); +} + #[trial] fn go_to_a_narrow_page_brings_it_back_into_horizontal_view() { let Rig { mut fixture, state, out, .. } = rig_over( diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index f0d190d33b..f4e30e5e3f 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -477,7 +477,9 @@ impl<'a> CanvasScrollbars<'a> { } fn bars(&self, view: &CanvasView, rect: Rect, style: &ScrollbarsStyle, held: Option<&HeldThumb>) -> Bars { - Bars::of(view, self.document, self.overscroll, !self.vertical_only, rect, style, held) + // A horizontal thumb held keeps its bar until the press ends, so its release closes the drag. + let horizontal = !self.vertical_only || held.is_some_and(|held| held.on(Axis::X)); + Bars::of(view, self.document, self.overscroll, horizontal, rect, style, held) } } @@ -547,6 +549,16 @@ impl Component for CanvasScrollbars<'_> { } let Some(view) = cx.value().copied() else { return }; let mut held = cx.press_slot::().clone(); + // A press that ended while the bars weren't shown (a full-screen view drops them) never reached them: its thumb + // is idle again, and the extent kept for it is stale. + let stale = held.as_ref().is_some_and(|held| { + let thumb = thumb_of(if held.horizontal { HORIZONTAL } else { VERTICAL }); + matches!(cx.part(thumb).press.phase, PressPhase::Idle | PressPhase::Hover) + }); + if stale { + *cx.press_slot::() = None; + held = None; + } let bars = self.bars(&view, cx.rect, cx.style, held.as_ref()); let mut ended = false; let rtl = cx.flow.is_rtl(); From 3837e0b64dac9f536e0dac6c168e3341e624af7f Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 30 Nov 2026 12:35:16 +0100 Subject: [PATCH 2/3] pages: paint the anchored zoom immediately and support floating scrollbars --- crates/pages/src/ui/view.rs | 54 +++++++++++++++---- crates/pages/tests/it/views.rs | 48 +++++++++++++++++ crates/ui-canvas/src/canvas/scrollbars/mod.rs | 13 +++-- 3 files changed, 100 insertions(+), 15 deletions(-) diff --git a/crates/pages/src/ui/view.rs b/crates/pages/src/ui/view.rs index 46522ae8e2..907046fc58 100644 --- a/crates/pages/src/ui/view.rs +++ b/crates/pages/src/ui/view.rs @@ -478,6 +478,7 @@ pub struct PageView<'a> { reach: f32, go_to_place: GoToPlace, fill_screen: bool, + floating_scrollbars: bool, } /// How far from the view, in view heights above and below it (and one view width either side), the page view keeps a @@ -508,6 +509,7 @@ impl<'a> PageView<'a> { reach: PICTURE_REACH, go_to_place: GoToPlace::AfterRowAbove, fill_screen: false, + floating_scrollbars: false, } } @@ -528,6 +530,13 @@ impl<'a> PageView<'a> { self } + /// Use the theme's floating scroll indicators over the page edges. + #[must_use] + pub fn floating_scrollbars(mut self) -> Self { + self.floating_scrollbars = true; + self + } + /// The colour round the pages: the theme's by default; production's full screen view is black. #[must_use] pub fn pasteboard(mut self, colour: PasteboardColor) -> Self { @@ -696,6 +705,7 @@ impl<'a> PageView<'a> { reach, go_to_place, fill_screen, + floating_scrollbars, } = self; let mut spacing = PageMetrics::new().show_at(ui, Rect::from_min_size(rect.min, Vec2::ZERO)).spacing; // Filling the screen, the pages keep no margin above or below and no gutter beside them, and draw no border. @@ -752,8 +762,20 @@ impl<'a> PageView<'a> { let mut held = Held::new(Bind::bound(state)); let mut seen: Option = None; let mut canvas_body = |ui: &mut Ui, cx: &mut BodyCx<'_>| { - let inputs = - Inputs { source, describer, build: &build, rect, given, paper, navigation, border, keep, reach, strips: strips_clip.map(|_| full) }; + let inputs = Inputs { + source, + describer, + build: &build, + rect, + given, + paper, + navigation, + border, + keep, + reach, + floating_scrollbars, + strips: strips_clip.map(|_| full), + }; let (mut next, pending) = run_pass(ui, cx, &mut held, &inputs, |ui, page| before(ui, page, session))?; let pass = tool_pass(&cx.env); if let Some(clip) = strips_clip { @@ -817,6 +839,7 @@ struct Inputs<'a> { keep: &'a [usize], /// `PageView::picture_reach`. reach: f32, + floating_scrollbars: bool, /// With `PageView::rulers`, the view's whole rect, strips included; the canvas shows inside the strips. strips: Option, } @@ -986,7 +1009,7 @@ fn run_pass( inputs: &Inputs<'_>, mut before: impl FnMut(&mut Ui, &PageBody<'_>) -> Option, ) -> Option<(Seen, Pending)> { - let Inputs { source, describer, build, rect, given, paper, navigation, border, keep, reach, strips: _ } = *inputs; + let Inputs { source, describer, build, rect, given, paper, navigation, border, keep, reach, floating_scrollbars, strips: _ } = *inputs; let mut state = held.read(cx)?; let started = state.clone(); state.normalise(); @@ -1004,8 +1027,9 @@ fn run_pass( // A display, continuous, cover, fit or rotation other than the layout placed last keeps the current page in // view. Writing a field to the value it has changes nothing, and the first pass has no layout to differ from. let previous = mem.layout.take(); - if state.go_to.is_none() && previous.as_ref().is_some_and(|(key, _)| key.display_differs(&state)) { - state.go_to = Some(GoTo::Top { page: state.current, fraction: 0.0 }); + let reposition = state.go_to.is_none() && previous.as_ref().is_some_and(|(key, _)| key.display_differs(&state)); + if reposition { + state.go_to(state.current); } // The layout the canvas was given its view for: what this pass draws, whatever the pass makes of the state. let drawn = build.of(&state); @@ -1028,6 +1052,11 @@ fn run_pass( state.offset = offset; } } + // Switching fit or display also places the current page at the top. Use that same + // destination for this paint, before the normal go-to processing commits it. + if reposition && let Some(top) = state.go_to { + state.offset = go_to_offset(top, &geo, state.continuous, state.offset, (vw, vh), &build.spacing, build.go_to_place); + } state.zoom = geo.scale / SCREEN_POINTS_PER_POINT; // The canvas folded the wheel, a pinch and the view commands into the view it settled on: a different zoom is the @@ -1066,13 +1095,15 @@ fn run_pass( } } let mut scrolled = wheel.is_some_and(|wheel| !zooming && wheel.scroll != Vec2::ZERO); - // A zoom that does not ease draws the layout it made in its own pass, through the view of the anchored offset: the - // pages reflow and the point under the pointer stays in the pass the zoom happens, and the next pass, given that - // view, moves nothing. (Drawing the layout the pass started with, scaled, put the reflow and the spacing that does - // not scale one pass late.) An ease draws the layout it started from, scaled, until it lands. - let (drawn, drawn_frame, mut shown) = if zooming && !space.easing() { + // Wheel zooms and external scale changes both compute an anchored offset above. Paint + // through it immediately so the new page size and its placement change in the same frame. + // An animated canvas keeps drawing its easing view until it lands. + let anchored = state.offset != started.offset; + let (drawn, drawn_frame, mut shown) = if (zooming || anchored) && !space.easing() { let frame = Frame::of(&geo); - let mut view = frame.view(rect, state.offset, geo.scale / SCREEN_POINTS_PER_POINT); + let (xs, ys) = (offset_range(geo.doc.x0, geo.doc.x1, vw), geo.y_range(vh)); + let offset = (want.0.clamp(xs.0, xs.1), want.1.clamp(ys.0, ys.1)); + let mut view = frame.view(rect, offset, geo.scale / SCREEN_POINTS_PER_POINT); // As the canvas places a given view: in its own rect (inside the rulers), about the same centre. view.rect = target.rect; let view = if shown == target { view } else { pixel_aligned(view, ui.ctx().pixels_per_point()) }; @@ -1200,6 +1231,7 @@ fn run_pass( Label::from(words.page_of.fill(&[("page", label.as_str()), ("count", total.as_str())])) }; let bars = if count > 0 { bars.tip(&tip) } else { bars }; + let bars = if floating_scrollbars { bars.floating() } else { bars }; // Pages that fit the view's width can't scroll sideways, so there is no horizontal bar. The layout drawn // decides, as the bars measure it. let (x_lo, x_hi) = offset_range(drawn.doc.x0, drawn.doc.x1, vw); diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index f0ec0c2e8e..d725ac1d51 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -125,6 +125,31 @@ fn the_horizontal_bar_shows_only_when_the_pages_are_wider_than_the_view() { assert!(has_horizontal_bar(3.0), "pages wider than the view scroll sideways"); } +#[trial] +fn floating_page_scrollbars_use_narrower_tracks_and_keep_the_same_page_layout() { + let measure = |floating: bool| { + let pages = DemoPages::new(4); + let mut state = state_with(|s| { + s.fit = Fit::None; + s.zoom = 2.0; + }); + let page = Rc::new(RefCell::new(egui::Rect::NOTHING)); + let drawn = Rc::clone(&page); + let mut fixture = Fixture::new(move |ui| { + let view = PageView::new("Pages", &pages, &mut state, PAPER); + let shown = if floating { view.floating_scrollbars() } else { view }.show(ui, |_, _| {}); + *drawn.borrow_mut() = shown.pages[0].rect; + }); + fixture.room(vec2(900.0, 600.0)).idle(3); + let bar = horizontal_bar(&fixture).expect("wide pages scroll sideways"); + (bar.height(), *page.borrow()) + }; + let (floating, float_page) = measure(true); + let (docked, dock_page) = measure(false); + assert!(floating < docked); + assert_eq!(float_page, dock_page); +} + /// The horizontal scroll bar the fixture shows, if any. fn horizontal_bar(fixture: &Fixture) -> Option { fixture.by_role(Role::SCROLL_BAR).iter().map(|bar| bar.rect).find(|rect| rect.width() > rect.height()) @@ -489,6 +514,29 @@ fn assert_centre_kept(out: &Out, before: (usize, kurbo::Point), what: &str) { ); } +/// A zoom entered in a toolbar draws the page in its final position immediately. A pass at +/// the new scale with the old offset makes the page jump sideways and vertically on the next frame. +#[trial] +fn a_written_zoom_draws_the_anchored_page_on_its_first_frame() { + let mut view = rig( + 5, + state_with(|s| { + s.fit = Fit::None; + s.zoom = 1.0; + }), + ); + for (fit, zoom) in [(Fit::None, 2.0), (Fit::None, 1.0), (Fit::Width, 1.0), (Fit::None, 2.0)] { + view.state.borrow_mut().fit = fit; + view.state.borrow_mut().zoom = zoom; + view.fixture.idle(1); + let first = view.out.borrow().pages[0].rect; + view.fixture.idle(2); + let settled = view.out.borrow().pages[0].rect; + assert!((first.min - settled.min).length() < 1.0, "at {zoom}: first {first:?}, settled {settled:?}"); + assert!((first.size() - settled.size()).length() < 1.0); + } +} + /// A written zoom keeps the point at the centre; a new room (a panel opened beside the view) refits and keeps the /// current page's top where it was, as production did. #[trial] diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index f4e30e5e3f..1b2ea363e9 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -1,6 +1,6 @@ //! `CanvasScrollbars`: the scroll bars of a canvas (20.6): a horizontal bar along the bottom of the view and a vertical -//! one along the right, always shown, in the docked look (`ScrollBarKind::Docked`): the thumb has the track's whole -//! thickness at rest and under the pointer. Document chrome uses inset thumbs, step buttons and a corner. +//! one along the right. Docked bars have full-width thumbs at rest. Floating bars use the theme's narrower resting +//! indicators. Document chrome uses inset thumbs, step buttons and a corner. //! The thumb is the visible fraction of //! the scrollable extent (the document, plus wherever the view has been scrolled past it); drag it to scroll, or press //! the track to page by one screenful. With overscroll on, the extent also takes in the pasteboard around the document, @@ -402,6 +402,10 @@ pub struct CanvasScrollbars<'a> { /// horizontal bar or corner is shown. #[prop(flag)] vertical_only: bool, + /// Use floating indicators with a narrow thumb at rest. Document chrome takes precedence. + #[prop(flag)] + #[style_key] + floating: bool, /// Wider bars with inset thumbs, step arrows and a corner for a document window. #[prop(flag)] #[style_key] @@ -424,6 +428,7 @@ impl<'a> CanvasScrollbars<'a> { overscroll: false, vertical_only: false, document_chrome: false, + floating: false, tip: None, held: None, } @@ -490,8 +495,8 @@ impl Component for CanvasScrollbars<'_> { type Output = CanvasScrollbarsOutput; fn style(cx: &StyleCx<'_>, key: CanvasScrollbarsStyleKey) -> ScrollbarsStyle { - let mut look = cx.scroll_bar_of(ScrollBarKind::Docked); - look.thickness = cx.tokens.size.icon.md; + let kind = if key.floating && !key.document_chrome { ScrollBarKind::Floating } else { ScrollBarKind::Docked }; + let mut look = cx.scroll_bar_of(kind); let margin = if key.document_chrome { 0.0 } else { cx.tokens.space.gap.tight }; let document = key.document_chrome.then(|| { let color = &cx.tokens.color; From 6afe03f5b6de3c288ddcd3dbf0bb0320a2b20ca8 Mon Sep 17 00:00:00 2001 From: Codex Date: Mon, 30 Nov 2026 12:41:37 +0100 Subject: [PATCH 3/3] pages: retain docked scrollbar sizing and exercise both floating thumbs --- crates/pages/tests/it/views.rs | 30 +++++++++++++++---- crates/ui-canvas/src/canvas/scrollbars/mod.rs | 3 ++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/pages/tests/it/views.rs b/crates/pages/tests/it/views.rs index d725ac1d51..f039e4cdb5 100644 --- a/crates/pages/tests/it/views.rs +++ b/crates/pages/tests/it/views.rs @@ -141,12 +141,32 @@ fn floating_page_scrollbars_use_narrower_tracks_and_keep_the_same_page_layout() *drawn.borrow_mut() = shown.pages[0].rect; }); fixture.room(vec2(900.0, 600.0)).idle(3); - let bar = horizontal_bar(&fixture).expect("wide pages scroll sideways"); - (bar.height(), *page.borrow()) + let horizontal = horizontal_bar(&fixture).expect("wide pages scroll sideways"); + let vertical = fixture + .by_role(Role::SCROLL_BAR) + .into_iter() + .map(|node| node.rect) + .find(|rect| rect.height() > rect.width()) + .expect("long documents scroll vertically"); + let initial = *page.borrow(); + if floating { + // Hover the thumb at the start of each track, then drag from inside its hover area. + for (grab, delta) in [ + (pos2(horizontal.left() + 4.0, horizontal.center().y), vec2(35.0, 0.0)), + (pos2(vertical.center().x, vertical.top() + 4.0), vec2(0.0, 35.0)), + ] { + fixture.frame(vec![craft_ui::testing::pointer::moved(grab)]); + let before = page.borrow().min; + fixture.drag_hold_at(grab, delta).release_at(grab + delta).idle(2); + let moved = before - page.borrow().min; + assert!(moved.dot(delta) > 1.0, "the thumb drag must scroll its own axis: {delta:?} -> {moved:?}"); + } + } + (horizontal.height(), vertical.width(), initial) }; - let (floating, float_page) = measure(true); - let (docked, dock_page) = measure(false); - assert!(floating < docked); + let (float_h, float_v, float_page) = measure(true); + let (dock_h, dock_v, dock_page) = measure(false); + assert!(float_h < dock_h && float_v < dock_v); assert_eq!(float_page, dock_page); } diff --git a/crates/ui-canvas/src/canvas/scrollbars/mod.rs b/crates/ui-canvas/src/canvas/scrollbars/mod.rs index 1b2ea363e9..61a6127d3f 100644 --- a/crates/ui-canvas/src/canvas/scrollbars/mod.rs +++ b/crates/ui-canvas/src/canvas/scrollbars/mod.rs @@ -497,6 +497,9 @@ impl Component for CanvasScrollbars<'_> { fn style(cx: &StyleCx<'_>, key: CanvasScrollbarsStyleKey) -> ScrollbarsStyle { let kind = if key.floating && !key.document_chrome { ScrollBarKind::Floating } else { ScrollBarKind::Docked }; let mut look = cx.scroll_bar_of(kind); + if kind == ScrollBarKind::Docked { + look.thickness = cx.tokens.size.icon.md; + } let margin = if key.document_chrome { 0.0 } else { cx.tokens.space.gap.tight }; let document = key.document_chrome.then(|| { let color = &cx.tokens.color;