diff --git a/crates/engine/src/view_behaviour_tests.rs b/crates/engine/src/view_behaviour_tests.rs index 974c9f44f..9c77314e0 100644 --- a/crates/engine/src/view_behaviour_tests.rs +++ b/crates/engine/src/view_behaviour_tests.rs @@ -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. diff --git a/crates/engine/src/view_state.rs b/crates/engine/src/view_state.rs index bfbb36f66..212bb93b4 100644 --- a/crates/engine/src/view_state.rs +++ b/crates/engine/src/view_state.rs @@ -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;