🐛 Keep fitted content clear of the rulers (#13069)

* 🐛 Keep fitted content clear of the rulers

Fit actions pass padding in document units, so the margin on screen
shrinks with the zoom. On large designs the margin became smaller than
the fixed 22px ruler overlay and part of the content was hidden under
it.

Add a :min-screen-padding option to adjust-to-viewport. The existing
design-space padding is kept when it already leaves enough margin on
screen; otherwise the viewport is fitted again with a fixed screen
margin. Use it in fit-to-all, fit-to-selected and fit-to-shapes so all
fit paths behave the same at any zoom level.

Closes #12609

AI-assisted-by: deepseek-v4.1-flash

* 🐛 Keep page-open fits clear of rulers

Pass the minimum screen padding when initializing the viewport, and cover all
four fit margins for wide and tall content with the call-site minimum zoom.

AI-assisted-by: gpt-6-luna
This commit is contained in:
Luis de Dios 2026-11-23 23:32:13 +01:00 committed by GitHub
parent 3a1f17834e
commit 0b4bb4b832
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 121 additions and 13 deletions

View File

@ -9,7 +9,8 @@
[app.common.geom.point :as gpt]
[app.common.geom.rect :as grc]
[app.common.geom.shapes :as gsh]
[app.common.geom.shapes.points :as gpo]))
[app.common.geom.shapes.points :as gpo]
[app.common.math :as mth]))
;; --- Alignment
@ -122,9 +123,25 @@
;; Adjust to viewport
(defn- fit-rect
"Returns a viewport-sized rect (in document coordinates) that scales
`srect` with `zoom` and centers it in the viewport."
[viewport srect zoom]
(let [width (/ (:width viewport) zoom)
height (/ (:height viewport) zoom)
cx (+ (:x srect) (/ (:width srect) 2))
cy (+ (:y srect) (/ (:height srect) 2))]
(-> srect
(assoc :x (- cx (/ width 2))
:y (- cy (/ height 2))
:width width
:height height)
(grc/update-rect :position))))
(defn adjust-to-viewport
([viewport srect] (adjust-to-viewport viewport srect nil))
([viewport srect {:keys [padding min-zoom] :or {padding 0 min-zoom nil}}]
([viewport srect {:keys [padding min-zoom min-screen-padding]
:or {padding 0 min-zoom nil min-screen-padding 0}}]
(let [gprop (/ (:width viewport)
(:height viewport))
srect-padded (-> srect
@ -154,13 +171,14 @@
(grc/update-rect :position)))
:else
(grc/update-rect srect-padded :position))]
;; If min-zoom is specified and the resulting zoom would be below it,
;; return a rect with the original top-left corner centered in the viewport
;; instead of using the aspect-ratio-adjusted rect (which can push coords
;; extremely far with extreme aspect ratios).
(if (and (some? min-zoom)
(< (/ (:width viewport) (:width adjusted-rect)) min-zoom))
(grc/update-rect srect-padded :position))
zoom (/ (:width viewport) (:width adjusted-rect))]
(cond
;; If min-zoom is specified and the resulting zoom would be below it,
;; return a rect with the original top-left corner centered in the
;; viewport instead of using the aspect-ratio-adjusted rect (which can
;; push coords extremely far with extreme aspect ratios).
(and (some? min-zoom) (< zoom min-zoom))
(let [anchor-x (:x srect)
anchor-y (:y srect)
vbox-width (/ (:width viewport) min-zoom)
@ -171,4 +189,25 @@
:width vbox-width
:height vbox-height)
(grc/update-rect :position)))
;; The design-space padding shrinks on screen as the zoom decreases. If
;; it becomes smaller than the requested minimum, fit again with a fixed
;; screen margin so the content never sits under fixed overlays (such as
;; the rulers).
(and (pos? min-screen-padding)
(< (* padding zoom) min-screen-padding))
(let [padding (mth/min min-screen-padding
(/ (dec (mth/min (:width viewport)
(:height viewport)))
2))
zoom (mth/min (/ (- (:width viewport) (* 2 padding))
(:width srect))
(/ (- (:height viewport) (* 2 padding))
(:height srect)))
zoom (if (some? min-zoom)
(mth/max zoom min-zoom)
zoom)]
(fit-rect viewport srect zoom))
:else
adjusted-rect))))

View File

@ -94,3 +94,55 @@
srect {:x 0 :y 0 :width 100 :height 100}
result (gal/adjust-to-viewport viewport srect {:min-zoom 0.5})]
(t/is (some? result)))))
(t/deftest adjust-to-viewport-min-screen-padding-test
(t/testing "Fitted wide content keeps the minimum margin on every edge"
(let [viewport {:width 1000 :height 800}
srect {:x 0 :y 0 :width 8000 :height 6000}
result (gal/adjust-to-viewport viewport srect
{:padding 40
:min-zoom 0.01
:min-screen-padding 40})
zoom (/ (:width viewport) (:width result))
left-gap (* (- (:x srect) (:x result)) zoom)
right-gap (* (- (+ (:x result) (:width result))
(+ (:x srect) (:width srect)))
zoom)
top-gap (* (- (:y srect) (:y result)) zoom)
bottom-gap (* (- (+ (:y result) (:height result))
(+ (:y srect) (:height srect)))
zoom)]
(t/is (mth/close? 40 left-gap))
(t/is (mth/close? 40 right-gap))
(t/is (<= 40 top-gap))
(t/is (<= 40 bottom-gap))))
(t/testing "Fitted tall content keeps the minimum margin on every edge"
(let [viewport {:width 1000 :height 800}
srect {:x 0 :y 0 :width 6000 :height 8000}
result (gal/adjust-to-viewport viewport srect
{:padding 40
:min-zoom 0.01
:min-screen-padding 40})
zoom (/ (:width viewport) (:width result))
left-gap (* (- (:x srect) (:x result)) zoom)
right-gap (* (- (+ (:x result) (:width result))
(+ (:x srect) (:width srect)))
zoom)
top-gap (* (- (:y srect) (:y result)) zoom)
bottom-gap (* (- (+ (:y result) (:height result))
(+ (:y srect) (:height srect)))
zoom)]
(t/is (<= 40 left-gap))
(t/is (<= 40 right-gap))
(t/is (mth/close? 40 top-gap))
(t/is (mth/close? 40 bottom-gap))))
(t/testing "Design-space padding is kept when it is larger on screen"
(let [viewport {:width 1000 :height 800}
srect {:x 0 :y 0 :width 200 :height 100}
expected (gal/adjust-to-viewport viewport srect {:padding 40})
result (gal/adjust-to-viewport viewport srect
{:padding 40
:min-screen-padding 40})]
(t/is (= expected result)))))

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

@ -9,6 +9,12 @@
(def viewport-width 4000)
(def viewport-height 4000)
(def ^:const viewport-fit-min-padding
"Minimum margin, in screen pixels, kept around the content when fitting the
viewport to it. Keeps the content clear of fixed overlays such as the
rulers."
40)
(def frame-start-x 1200)
(def frame-start-y 1200)

View File

@ -14,6 +14,7 @@
[app.common.geom.rect :as gpr]
[app.common.geom.shapes :as gsh]
[app.common.math :as mth]
[app.main.constants :as mconst]
[app.main.data.event :as ev]
[app.main.data.helpers :as dsh]
[app.main.data.workspace.viewport-wasm :as dwvw]
@ -61,7 +62,10 @@
(or (> (:width srect) width)
(> (:height srect) height))
(let [srect (gal/adjust-to-viewport size srect {:padding 40 :min-zoom 0.01})
(let [srect (gal/adjust-to-viewport size srect
{:padding 40
:min-zoom 0.01
:min-screen-padding mconst/viewport-fit-min-padding})
zoom (/ (:width size) (:width srect))]
(-> local

View File

@ -14,6 +14,7 @@
[app.common.geom.point :as gpt]
[app.common.geom.rect :as grc]
[app.common.geom.shapes :as gsh]
[app.main.constants :as mconst]
[app.main.data.event :as ev]
[app.main.data.helpers :as dsh]
[app.main.data.workspace.viewport-wasm :as dwvw]
@ -124,7 +125,9 @@
state
(update state :workspace-local
(fn [{:keys [vport] :as local}]
(let [srect (gal/adjust-to-viewport vport srect {:padding 160 :min-zoom 0.01})
(let [srect (gal/adjust-to-viewport vport srect {:padding 160
:min-zoom 0.01
:min-screen-padding mconst/viewport-fit-min-padding})
zoom (/ (:width vport) (:width srect))]
(-> local
(assoc :zoom zoom)
@ -177,7 +180,9 @@
(gsh/shapes->rect))]
(update state :workspace-local
(fn [{:keys [vport] :as local}]
(let [srect (gal/adjust-to-viewport vport srect {:padding 40 :min-zoom 0.01})
(let [srect (gal/adjust-to-viewport vport srect {:padding 40
:min-zoom 0.01
:min-screen-padding mconst/viewport-fit-min-padding})
zoom (/ (:width vport) (:width srect))]
(-> local
(assoc :zoom zoom)
@ -205,7 +210,9 @@
(fn [{:keys [vport] :as local}]
(let [srect (gal/adjust-to-viewport
vport srect
{:padding 40 :min-zoom 0.01})
{:padding 40
:min-zoom 0.01
:min-screen-padding mconst/viewport-fit-min-padding})
zoom (/ (:width vport)
(:width srect))]
(-> local