fwstub-2: a page view zoom by the wheel or a pinch anchors at the point the canvas zoomed about (CanvasSpace::zoom_anchor), not one rebuilt from the change of centre: the canvas's clamp keeps pages no wider than the view centred, so the rebuilt point was the middle of the view, in the gutter, and a reflow from two columns to three anchored page 2 and drew page 3, under the pointer, a row off; trial: Ctrl+wheel out from page 3 just right of the gutter through three reflows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Ball
2026-11-19 19:47:14 +00:00
co-authored by Claude Opus 5.5
parent 37b862c663
commit 99d40aba4d
5 changed files with 97 additions and 21 deletions
+12 -2
View File
@@ -712,8 +712,18 @@ fn run_pass(ui: &mut Ui, cx: &mut BodyCx<'_>, held: &mut Held<'_, PageViewState>
let mut want = state.offset;
let mut wheel_left = 0.0;
if zooming {
let fixed = (target.center - given.center) * (given.zoom * target.zoom / (target.zoom - given.zoom));
let pointer = rect.center() + Vec2::new(fixed.x as f32, fixed.y as f32);
// The point that stayed still, in the given view's frame: the canvas places the given view in its own rect (inside
// the rulers) about the same centre. The wheel's and a pinch's point is the canvas's own: its clamp of the
// zoomed view (a document no wider than the view stays centred) moves the centre, so the change of centre
// would put the point elsewhere, across a gutter on another page that reflows into another row. A view
// command's zoom keeps the centre.
let pointer = match space.zoom_anchor() {
Some(at) => at + (rect.center() - target.rect.center()),
None => {
let fixed = (target.center - given.center) * (given.zoom * target.zoom / (target.zoom - given.zoom));
rect.center() + Vec2::new(fixed.x as f32, fixed.y as f32)
}
};
geo = zoom_about(build, &mut state, &geo, rect, pointer, target.zoom / given.zoom);
let (xs, ys) = (offset_range(geo.doc.x0, geo.doc.x1, vw), offset_range(geo.doc.y0, geo.doc.y1, vh));
state.offset = (state.offset.0.clamp(xs.0, xs.1), state.offset.1.clamp(ys.0, ys.1));
+40 -14
View File
@@ -170,6 +170,24 @@ impl ZoomSeen {
/// pass through a gutter between columns under the pointer on the way.
#[trial]
fn a_ctrl_wheel_zoom_keeps_the_point_under_the_pointer_and_settles_in_its_own_frame() {
// Far enough down that zooming out never meets the top of the document, whose clamp would move the place.
ctrl_wheel_zoom_out(vec2(1280.0, 760.0), 40.4 * 1056.0, 30, egui::Vec2::ZERO, 5);
}
/// The WordCraft app's case (#1340): a few pages down, the pointer just inside page 3, right of the gutter in the
/// middle of the view, when the pages reflow from two columns to three, which moves page 3 down a row. The canvas
/// zooms about the pointer, then keeps the pages (no wider than the view) centred: the place under the pointer, not
/// the middle of the view, keeps its place, on page 3 and not page 2 across the gutter. Then three columns to four and
/// five, before the top of the document stops the view.
#[trial]
fn a_ctrl_wheel_zoom_keeps_the_place_when_the_page_under_the_pointer_moves_down_a_row() {
ctrl_wheel_zoom_out(vec2(1280.0, 690.0), 3.2 * 1056.0, 3, vec2(7.0, 0.0), 4);
}
/// `notches` Ctrl + wheel notches out, 8 frames apart, in a page view `room` big from `top` (screen points at 100 %)
/// with the pointer `nudge` from the middle of the area: every frame keeps the point under the pointer at the notch,
/// which starts on page `least` or later.
fn ctrl_wheel_zoom_out(room: egui::Vec2, top: f64, least: usize, nudge: egui::Vec2, notches: usize) {
let state = Rc::new(RefCell::new(state_with(|s| {
s.fit = Fit::None;
s.zoom = 1.0;
@@ -193,27 +211,35 @@ fn a_ctrl_wheel_zoom_keeps_the_point_under_the_pointer_and_settles_in_its_own_fr
*kept.borrow_mut() = now;
});
fixture.ctx().options_mut(|options| options.input_options.zoom_modifier = Modifiers::NONE);
fixture.room(vec2(1280.0, 760.0));
fixture.room(room);
fixture.settle();
// Far enough down that zooming out never meets the top of the document, whose clamp would move the place.
state.borrow_mut().offset.1 = 40.4 * 1056.0;
state.borrow_mut().offset.1 = top;
fixture.settle();
let pointer = seen.borrow().area.center();
let pointer = seen.borrow().area.center() + nudge;
fixture.frame(vec![egui::Event::PointerMoved(pointer)]).settle();
let start = seen.borrow().clone();
let (page, _) = *start
.pages
.iter()
.min_by(|(_, a), (_, b)| a.distance_to_pos(pointer).total_cmp(&b.distance_to_pos(pointer)))
.expect("pages show");
assert!(page >= 30, "many pages down: page {page}");
let under = |seen: &ZoomSeen| seen.rect(page).map(|r| (pointer - r.min) / seen.scale as f32);
let at = under(&start).expect("the page shows");
// The page under the pointer, else the nearest; a reflow can put another page there, so each notch asks again.
let page_under = |seen: &ZoomSeen| {
let (page, _) = *seen
.pages
.iter()
.min_by(|(_, a), (_, b)| a.distance_to_pos(pointer).total_cmp(&b.distance_to_pos(pointer)))
.expect("pages show");
page
};
let mut page = page_under(&start);
assert!(page >= least, "pages down: page {page}");
let under = |seen: &ZoomSeen, page: usize| seen.rect(page).map(|r| (pointer - r.min) / seen.scale as f32);
let mut at = under(&start, page).expect("the page shows");
let ctrl = Modifiers { ctrl: true, command: true, ..Modifiers::NONE };
fixture.frame(vec![egui::Event::ModifiersChanged(ctrl)]);
let mut last = seen.borrow().clone();
for n in 0..40 {
for n in 0..notches * 8 {
let notch = n % 8 == 0;
if notch {
page = page_under(&last);
at = under(&last, page).expect("the page under the pointer shows");
}
let events = if notch {
vec![egui::Event::MouseWheel {
unit: egui::MouseWheelUnit::Line,
@@ -227,7 +253,7 @@ fn a_ctrl_wheel_zoom_keeps_the_point_under_the_pointer_and_settles_in_its_own_fr
};
fixture.frame(events);
let now = seen.borrow().clone();
let here = under(&now).unwrap_or_else(|| panic!("frame {n}: page {page} shows"));
let here = under(&now, page).unwrap_or_else(|| panic!("frame {n}: page {page} shows"));
let off = (here - at) * now.scale as f32;
// Across, the place holds while the pages stay one a row; in rows they fit the window side by side.
let across = now.columns() > start.columns() || off.x.abs() < 2.0;
+18 -5
View File
@@ -277,6 +277,8 @@ struct Settled {
mark: Option<Pos2>,
/// A press that stopped an autoscroll is down: it reaches no tool.
swallowing: bool,
/// The screen point a wheel or pinch zoom of this pass kept still ([`CanvasSpace::zoom_anchor`]).
zoom_anchor: Option<Pos2>,
}
impl<'a> Canvas<'a> {
@@ -425,6 +427,8 @@ impl<'a> Canvas<'a> {
}
}
let mut jumped = false;
// The screen point a wheel or pinch zoom of this pass kept still, before the clamp below can move the centre.
let mut zoom_anchor = None;
let wheel = cx.take_wheel();
let wheeled = wheel.is_some();
if let Some(wheel) = wheel {
@@ -433,7 +437,11 @@ impl<'a> Canvas<'a> {
// The pointer the wheel came at: the canvas's own widget is not hovered where an app widget in its body
// covers it.
let at = wheel.at.unwrap_or_else(|| view.rect.center());
let (anchor, zoom) = (WheelNavigation::zoom_anchor(&view, action, at, size), view.zoom);
jumped |= WheelNavigation::apply(&mut view, action, at, size, pixels_per_point);
if view.zoom != zoom {
zoom_anchor = anchor;
}
}
// A trackpad rotation or a twist of two fingers turns the view; a mirrored view turns the other way.
if let Some(degrees) = WheelNavigation::rotation(&WheelInput::of(&wheel, WheelNavigation::LINE, os), &self.navigation) {
@@ -489,6 +497,10 @@ impl<'a> Canvas<'a> {
}
let before = view;
if let Some((next, motion)) = self.view_commands(cx, Some(view)) {
// A view command's zoom keeps its own point (the centre), not the wheel's.
if next.zoom != view.zoom {
zoom_anchor = None;
}
view = next;
match motion {
CommandMotion::Ease if self.navigation.animated_zoom => {
@@ -516,7 +528,7 @@ impl<'a> Canvas<'a> {
let easing = motion.animation.running();
let mark = motion.autoscroll.mark();
motion.restore(&ctx, id);
Settled { target, shown, easing, keys, mark, swallowing }
Settled { target, shown, easing, keys, mark, swallowing, zoom_anchor }
}
/// One pass of the middle button's autoscroll over `view`: a middle press on the background starts it under
@@ -596,11 +608,12 @@ impl<'a> Canvas<'a> {
/// The space the body and the tool see in this pass; `caps_lock` is whether Caps Lock is on in it.
fn space(&self, id: egui::Id, pixels_per_point: f32, background: BackgroundPress, caps_lock: bool) -> Option<CanvasSpace> {
let (shown, target, easing, keys) = match self.settled {
Some(settled) => (settled.shown, settled.target, settled.easing, settled.keys),
None => (self.given?, self.given?, false, BodyKeys::default()),
let (shown, target, easing, keys, zoom_anchor) = match self.settled {
Some(settled) => (settled.shown, settled.target, settled.easing, settled.keys, settled.zoom_anchor),
None => (self.given?, self.given?, false, BodyKeys::default(), None),
};
let mut space = CanvasSpace::new(target, pixels_per_point).with_follows_pointer(self.follows_pointer);
let mut space =
CanvasSpace::new(target, pixels_per_point).with_follows_pointer(self.follows_pointer).with_zoom_anchor(zoom_anchor);
space.set_view(target);
if easing {
space.ease_through(shown);
@@ -220,4 +220,14 @@ impl WheelNavigation {
}
*view != before
}
/// The screen point [`apply`](Self::apply) keeps still when `action` zooms `view` with the pointer at `pointer`, for
/// a document of `size`; `None` for a pan.
pub fn zoom_anchor(view: &CanvasView, action: WheelAction, pointer: Pos2, size: Size) -> Option<Pos2> {
match action {
WheelAction::Zoom(_) => Some(zoom::wheel_anchor(view, pointer, size)),
WheelAction::SnapZoom(_) => Some(pointer),
WheelAction::Pan(_) => None,
}
}
}
+17
View File
@@ -40,6 +40,7 @@ pub struct CanvasSpace {
/// The canvas's picture slot for this pass, which [`paint_picture`](Self::paint_picture) appends to; `None` in a
/// measuring pass.
picture: Option<PaintSlot>,
zoom_anchor: Option<Pos2>,
}
impl CanvasSpace {
@@ -60,6 +61,7 @@ impl CanvasSpace {
pasteboard: PasteboardColor::Default,
precise: false,
picture: None,
zoom_anchor: None,
}
}
@@ -211,6 +213,21 @@ impl CanvasSpace {
self.easing
}
/// The screen point the wheel or a pinch zoomed about in this pass, in the canvas root's screen space; `None` when
/// neither zoomed (a view command zooms about the centre). A body that keeps its own view state anchors its zoom
/// here: the canvas clamps the zoomed view to the document after, which can move the centre (a document no wider
/// than the view stays centred), so the change of centre no longer tells which point stayed still.
pub fn zoom_anchor(&self) -> Option<Pos2> {
self.zoom_anchor
}
/// This space with the point a zoom of this pass kept still ([`zoom_anchor`](Self::zoom_anchor)).
#[must_use]
pub fn with_zoom_anchor(mut self, anchor: Option<Pos2>) -> Self {
self.zoom_anchor = anchor;
self
}
/// The page and arrow keys the canvas left to its body in this pass ([`Canvas::keys_to_body`](super::Canvas)).
pub fn keys(&self) -> BodyKeys {
self.keys