diff --git a/common/src/app/common/geom/align.cljc b/common/src/app/common/geom/align.cljc index 959cf18018..385ca87876 100644 --- a/common/src/app/common/geom/align.cljc +++ b/common/src/app/common/geom/align.cljc @@ -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)))) diff --git a/common/test/common_tests/geom_align_test.cljc b/common/test/common_tests/geom_align_test.cljc index 5d71bf6030..751fcb9585 100644 --- a/common/test/common_tests/geom_align_test.cljc +++ b/common/test/common_tests/geom_align_test.cljc @@ -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))))) diff --git a/frontend/playwright/ui/specs/workspace.spec.js-snapshots/BUG-13822---Problems-with-z-index-1.png b/frontend/playwright/ui/specs/workspace.spec.js-snapshots/BUG-13822---Problems-with-z-index-1.png index ec9aed513d..ad93681c73 100644 Binary files a/frontend/playwright/ui/specs/workspace.spec.js-snapshots/BUG-13822---Problems-with-z-index-1.png and b/frontend/playwright/ui/specs/workspace.spec.js-snapshots/BUG-13822---Problems-with-z-index-1.png differ diff --git a/frontend/src/app/main/constants.cljs b/frontend/src/app/main/constants.cljs index 1c401c9558..c5abf7630c 100644 --- a/frontend/src/app/main/constants.cljs +++ b/frontend/src/app/main/constants.cljs @@ -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) diff --git a/frontend/src/app/main/data/workspace/viewport.cljs b/frontend/src/app/main/data/workspace/viewport.cljs index f7570534e3..51f881999b 100644 --- a/frontend/src/app/main/data/workspace/viewport.cljs +++ b/frontend/src/app/main/data/workspace/viewport.cljs @@ -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 diff --git a/frontend/src/app/main/data/workspace/zoom.cljs b/frontend/src/app/main/data/workspace/zoom.cljs index bf53b9462f..c06845c4f6 100644 --- a/frontend/src/app/main/data/workspace/zoom.cljs +++ b/frontend/src/app/main/data/workspace/zoom.cljs @@ -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