M8: Zoom to selection in a short canvas keeps the selection above the toolbar (#2139 review)

The inset was clamped to leave 100 points and the fit kept at least 50, so in a canvas 130
points high the selection ended behind the toolbar. The real inset is kept and the margin
shrinks to a quarter of the area when 40 points a side does not fit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Model-Effort: Opus 5.5 drove and wrote
This commit is contained in:
Trevor BrownandClaude Opus 5.5 committed 2026-11-09 00:33:53 -05:00
1 parent fceb606314
commit 5d529269ce
2 files changed
+24 -5

No files matched your search

+16
View File
@@ -808,6 +808,22 @@ fn zoom_to_selection_without_a_toolbar_inset_uses_the_whole_height() {
assert!((v.zoom - (903.0 - 80.0) / 100.0).abs() < 1e-6, "{}", v.zoom);
}
#[test]
fn zoom_to_selection_in_a_short_canvas_still_ends_above_the_toolbar() {
// A canvas 130 points high with the 60-point toolbar strip leaves 70 above it: too short for
// 40 points a side, so the margin shrinks and the selection stays clear of the toolbar.
let mut s = Session::new();
let r = rect(&mut s, 0.0, 0.0, 100.0, 100.0, None);
ex(&mut s, "view.set", json!({"vw": 1389, "vh": 130, "bottomInset": 60}));
ex(&mut s, "select.set", json!({"ids": [r]}));
ex(&mut s, "view.zoomSelection", json!({}));
let v = view(&s);
let (_, top) = v.page_to_screen(0.0, 0.0);
let (_, bottom) = v.page_to_screen(100.0, 100.0);
assert!(top >= 0.0 && bottom <= 70.0 + 1e-6, "top {top}, bottom {bottom}");
assert!((top - (70.0 - bottom)).abs() < 1e-6, "centred above the toolbar: top {top}, bottom {bottom}");
}
#[test]
fn zoom_to_fit_is_unchanged() {
// Left alone on purpose: its 64-point margin is not measured against Figma yet.
+8 -5
View File
@@ -118,11 +118,14 @@ impl ViewState {
self.cy = r.center().y;
return;
}
let margin = 40.0;
let inset = if self.bottom_inset.is_finite() { self.bottom_inset.clamp(0.0, (self.vh - 100.0).max(0.0)) } else { 0.0 };
let area_h = self.vh - inset;
let zx = (self.vw - margin * 2.0).max(50.0) / r.width().max(1e-6);
let zy = (area_h - margin * 2.0).max(50.0) / r.height().max(1e-6);
let inset = if self.bottom_inset.is_finite() { self.bottom_inset.clamp(0.0, (self.vh - 1.0).max(0.0)) } else { 0.0 };
let area_h = (self.vh - inset).max(1.0);
// 40 points as measured; a canvas too small for that keeps a quarter of its size on each
// side instead, so the selection still ends above the toolbar.
let mx = (self.vw / 4.0).clamp(0.0, 40.0);
let my = (area_h / 4.0).clamp(0.0, 40.0);
let zx = (self.vw - mx * 2.0).max(1.0) / r.width().max(1e-6);
let zy = (area_h - my * 2.0).max(1.0) / r.height().max(1e-6);
self.zoom = zx.min(zy).clamp(Self::MIN_ZOOM, max_zoom);
// The rect's centre lands at screen y = area_h / 2, the page centre `(vh - area_h) / 2` below it.
self.cx = r.center().x;