Merge pull request #4488 from storytold/fix/page-zoom-first-frame

pages: keep the zoom anchor on the first frame and offer floating scrollbars
This commit is contained in:
FredApps authored and GitHub committed 2026-11-30 13:14:21 +01:00
commit 1e321ca65c
3 files changed
+123 -15

No files matched your search

+43 -11
View File
@@ -477,6 +477,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
@@ -507,6 +508,7 @@ impl<'a> PageView<'a> {
reach: PICTURE_REACH,
go_to_place: GoToPlace::AfterRowAbove,
fill_screen: false,
floating_scrollbars: false,
}
}
@@ -527,6 +529,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 {
@@ -695,6 +704,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.
@@ -751,8 +761,20 @@ impl<'a> PageView<'a> {
let mut held = Held::new(Bind::bound(state));
let mut seen: Option<Seen> = 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 {
@@ -816,6 +838,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<Rect>,
}
@@ -985,7 +1008,7 @@ fn run_pass(
inputs: &Inputs<'_>,
mut before: impl FnMut(&mut Ui, &PageBody<'_>) -> Option<CanvasView>,
) -> 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();
@@ -1003,8 +1026,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);
@@ -1027,6 +1051,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
@@ -1065,13 +1094,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()) };
@@ -1199,6 +1230,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);
+68
View File
@@ -125,6 +125,51 @@ 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 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 (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);
}
/// The horizontal scroll bar the fixture shows, if any.
fn horizontal_bar(fixture: &Fixture) -> Option<egui::Rect> {
fixture.by_role(Role::SCROLL_BAR).iter().map(|bar| bar.rect).find(|rect| rect.width() > rect.height())
@@ -489,6 +534,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]
+12 -4
View File
@@ -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,
@@ -409,6 +409,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]
@@ -431,6 +435,7 @@ impl<'a> CanvasScrollbars<'a> {
overscroll: false,
vertical_only: false,
document_chrome: false,
floating: false,
tip: None,
held: None,
}
@@ -510,9 +515,12 @@ impl Component for CanvasScrollbars<'_> {
type Output = CanvasScrollbarsOutput;
fn style(cx: &StyleCx<'_>, key: CanvasScrollbarsStyleKey) -> ScrollbarsStyle {
let mut look = cx.scroll_bar_of(ScrollBarKind::Docked);
let kind = if key.floating && !key.document_chrome { ScrollBarKind::Floating } else { ScrollBarKind::Docked };
let mut look = cx.scroll_bar_of(kind);
let (thickness, margin) = bar_size(cx.tokens, key.document_chrome);
look.thickness = thickness;
if kind == ScrollBarKind::Docked {
look.thickness = thickness;
}
let document = key.document_chrome.then(|| {
let color = &cx.tokens.color;
look.track = Stateful::new(cx.color(color.surface.sunken));