diff --git a/common/src/app/common/geom/modifiers.cljc b/common/src/app/common/geom/modifiers.cljc index 8668c332d4..49cdd21a72 100644 --- a/common/src/app/common/geom/modifiers.cljc +++ b/common/src/app/common/geom/modifiers.cljc @@ -110,8 +110,10 @@ (letfn [(apply-modifiers [bounds child] (when-let [child-bounds (get bounds (:id child))] + ;; the child's own corners stay with its bounds in the grid's axes [(-> @child-bounds - (gpo/parent-coords-bounds @transformed-parent-bounds)) + (gpo/parent-coords-bounds @transformed-parent-bounds) + (vary-meta assoc :app.common.geom.shapes.grid-layout.positions/own-points @child-bounds)) child])) (set-child-modifiers [modif-tree grid-data cell-data [child-bounds child]] diff --git a/common/src/app/common/geom/shapes/grid_layout/positions.cljc b/common/src/app/common/geom/shapes/grid_layout/positions.cljc index 3b818a56d2..07b54d40b9 100644 --- a/common/src/app/common/geom/shapes/grid_layout/positions.cljc +++ b/common/src/app/common/geom/shapes/grid_layout/positions.cljc @@ -53,6 +53,22 @@ (gpt/add $ (vv (* row-gap (dec (count span-row-tracks))))))] [p1 p2 p3 p4])))) +(defn- turned-min-scale + "The smallest scale a fill applies along the grid's axes to a child turned + in it, from the child's own corners `own-points`: one that keeps it at + least 1px thick in its own axes, as the skew narrows it. Nil for a child + that lines up with the grid's axes or is already thinner." + [own-points [g1 g2]] + (when-let [[p1 p2 _ p4] (seq own-points)] + (let [own-x (gpt/to-vec p1 p2) + width (gpt/length own-x) + height (gpt/length (gpt/to-vec p1 p4)) + ux (gpt/unit own-x) + gx (gpt/unit (gpt/to-vec g1 g2)) + sin (mth/abs (- (* (:x ux) (:y gx)) (* (:y ux) (:x gx))))] + (when (and (> (min width height) 1) (> sin 1e-4) (< sin (- 1 1e-4))) + (/ 1 (min width height)))))) + (defn calc-fill-width-data "Calculates the size and modifiers for the width of an auto-fill child" [_parent @@ -60,11 +76,13 @@ transform-inverse child child-origin child-width - cell-bounds] + cell-bounds + min-scale] (let [target-width (max (- (gpo/width-points cell-bounds) (ctl/child-width-margin child)) 0.01) max-width (max (ctl/child-max-width child) 0.01) - target-width (mth/clamp target-width (ctl/child-min-width child) max-width) + target-width (cond-> (mth/clamp target-width (ctl/child-min-width child) max-width) + (some? min-scale) (max (* min-scale child-width))) fill-scale (/ target-width child-width)] {:width target-width :modifiers (ctm/resize-modifiers (gpt/point fill-scale 1) child-origin transform transform-inverse)})) @@ -75,10 +93,12 @@ transform transform-inverse child child-origin child-height - cell-bounds] + cell-bounds + min-scale] (let [target-height (max (- (gpo/height-points cell-bounds) (ctl/child-height-margin child)) 0.01) max-height (max (ctl/child-max-height child) 0.01) - target-height (mth/clamp target-height (ctl/child-min-height child) max-height) + target-height (cond-> (mth/clamp target-height (ctl/child-min-height child) max-height) + (some? min-scale) (max (* min-scale child-height))) fill-scale (/ target-height child-height)] {:height target-height :modifiers (ctm/resize-modifiers (gpt/point 1 fill-scale) child-origin transform transform-inverse)})) @@ -95,13 +115,18 @@ (when (or (ctl/fill-width? child) (ctl/fill-height? child)) (gtr/calculate-geometry @parent-bounds)) + ;; a child turned in the grid fills along the grid's axes, which skews it + min-scale + (when (or (ctl/fill-width? child) (ctl/fill-height? child)) + (turned-min-scale (::own-points (meta child-bounds)) @parent-bounds)) + fill-width (when (ctl/fill-width? child) - (calc-fill-width-data parent transform transform-inverse child child-origin child-width cell-bounds)) + (calc-fill-width-data parent transform transform-inverse child child-origin child-width cell-bounds min-scale)) fill-height (when (ctl/fill-height? child) - (calc-fill-height-data parent transform transform-inverse child child-origin child-height cell-bounds)) + (calc-fill-height-data parent transform transform-inverse child child-origin child-height cell-bounds min-scale)) child-width (or (:width fill-width) child-width) child-height (or (:height fill-height) child-height)] diff --git a/common/test/common_tests/geom_modifiers_test.cljc b/common/test/common_tests/geom_modifiers_test.cljc index 4d99a5fa42..af3232cd89 100644 --- a/common/test/common_tests/geom_modifiers_test.cljc +++ b/common/test/common_tests/geom_modifiers_test.cljc @@ -226,3 +226,41 @@ (t/is (gpt/close? (gpt/scale inner-h (/ 1 3)) cell-h)) (t/is (gpt/close? (gpt/scale inner-v (/ 1 3)) cell-v)) (t/is (gpt/close? (first (:points inner)) (first (:points cell))))))) + +(t/deftest grid-fill-keeps-a-turned-child-at-least-one-pixel-thick + ;; a fill child turned in its grid fills along the grid's axes, which skews + ;; it; a fixed sibling leaves its column no width, and the child narrows + ;; only until it is 1px thick in its own axes + (let [file (-> (thf/sample-file :file) + (ths/add-sample-shape :grid {:type :frame :name "Grid" :width 100 :height 100 + :layout :grid :layout-grid-dir :row + :layout-grid-rows [{:type :flex :value 1}] + :layout-grid-columns [{:type :flex :value 1} {:type :flex :value 1} + {:type :flex :value 1}] + :layout-gap {:row-gap 5 :column-gap 9} + :layout-padding {:p1 8 :p2 8 :p3 8 :p4 8}}) + (ths/add-sample-shape :child {:type :frame :name "Child" :parent-label :grid + :width 100 :height 100 :layout-item-h-sizing :fill}) + (ths/add-sample-shape :rect {:type :rect :name "Rect" :parent-label :grid + :width 70 :height 30})) + page (thf/current-page file) + objects (:objects page) + child (get objects (thi/id :child)) + turned (gsh/transform-shape child (ctm/rotation-modifiers child (gsh/shape->center child) 45)) + grid (-> (get objects (thi/id :grid)) + (assoc :shapes [(thi/id :child) (thi/id :rect)]) + (assoc :layout-grid-cells + {(thi/new-id! :cell-1) {:id (thi/id :cell-1) :row 1 :column 1 :row-span 1 :column-span 1 + :position :manual :shapes [(thi/id :child)]} + (thi/new-id! :cell-2) {:id (thi/id :cell-2) :row 1 :column 2 :row-span 1 :column-span 1 + :position :manual :shapes [(thi/id :rect)]} + (thi/new-id! :cell-3) {:id (thi/id :cell-3) :row 1 :column 3 :row-span 1 :column-span 1 + :position :auto :shapes []}})) + objects (assoc objects (thi/id :child) turned (thi/id :grid) grid) + tree (gm/set-objects-modifiers {(thi/id :grid) {:modifiers (ctm/reflow-modifiers)}} objects) + result (gsh/transform-shape turned (get-in tree [(thi/id :child) :modifiers])) + [p1 p2 _ p4] (:points result) + e1 (gpt/to-vec p1 p2) + e2 (gpt/to-vec p1 p4) + area (abs (- (* (:x e1) (:y e2)) (* (:y e1) (:x e2))))] + (t/is (>= (/ area (max (gpt/length e1) (gpt/length e2))) 0.999))))