diff --git a/common/src/app/common/geom/shapes/flex_layout/params.cljc b/common/src/app/common/geom/shapes/flex_layout/params.cljc index 0d7d67ba1b..e409ad0b9b 100644 --- a/common/src/app/common/geom/shapes/flex_layout/params.cljc +++ b/common/src/app/common/geom/shapes/flex_layout/params.cljc @@ -18,10 +18,9 @@ (calculate-params objects shapes nil)) ([objects shapes parent] - (when (d/not-empty? shapes) - (let [shapes (->> shapes (remove :hidden)) - - points + ;; hidden shapes take no part in the layout + (when-let [shapes (seq (remove :hidden shapes))] + (let [points (->> shapes (map :id) (ctt/sort-z-index objects) diff --git a/common/src/app/common/geom/shapes/grid_layout/params.cljc b/common/src/app/common/geom/shapes/grid_layout/params.cljc index e310dfadd1..e9ebc2e8e5 100644 --- a/common/src/app/common/geom/shapes/grid_layout/params.cljc +++ b/common/src/app/common/geom/shapes/grid_layout/params.cljc @@ -106,62 +106,66 @@ [params true]))] params)) +(defn- calculate-params* + "The grid parameters for the visible `shapes`." + [_objects shapes parent] + (if (empty? shapes) + (-> {:layout-grid-columns [ctl/default-track-value ctl/default-track-value] + :layout-grid-rows [ctl/default-track-value ctl/default-track-value]} + (ctl/create-cells [1 1 2 2])) + + (let [all-shapes-rect (gco/shapes->rect shapes) + shapes+bounds + (->> shapes + (map #(vector % (grc/points->rect (get % :points))))) + + shapes-by-x + (->> shapes+bounds + (sort-by (comp :x second))) + + shapes-by-y + (->> shapes+bounds + (sort-by (comp :y second))) + + cols (calculate-tracks :x shapes-by-x) + rows (calculate-tracks :y shapes-by-y) + + num-cols (count cols) + num-rows (count rows) + + total-cols-width (->> cols (reduce #(+ %1 (:size %2)) 0)) + total-rows-height (->> rows (reduce #(+ %1 (:size %2)) 0)) + + column-gap + (if (= num-cols 1) + 0 + (/ (- (:width all-shapes-rect) total-cols-width) (dec num-cols))) + + row-gap + (if (= num-rows 1) + 0 + (/ (- (:height all-shapes-rect) total-rows-height) (dec num-rows))) + + layout-grid-rows (mapv (constantly ctl/default-track-value) rows) + layout-grid-columns (mapv (constantly ctl/default-track-value) cols) + + parent-childs-vector (gpt/to-vec (gpo/origin (:points parent)) (gpt/point all-shapes-rect)) + p-left (max 0 (:x parent-childs-vector)) + p-top (max 0 (:y parent-childs-vector))] + + (-> {:layout-grid-columns layout-grid-columns + :layout-grid-rows layout-grid-rows + :layout-gap {:row-gap row-gap + :column-gap column-gap} + :layout-padding {:p1 p-top :p2 p-left :p3 p-top :p4 p-left} + :layout-grid-dir (if (> num-cols num-rows) :row :column)} + (ctl/create-cells [1 1 num-cols num-rows]) + (assign-shape-cells rows cols))))) + (defn calculate-params "Given the shapes calculate its grid parameters (horizontal vs vertical, gaps, etc)" ([objects shapes] (calculate-params objects shapes nil)) - ([_objects shapes parent] - (if (empty? shapes) - (-> {:layout-grid-columns [ctl/default-track-value ctl/default-track-value] - :layout-grid-rows [ctl/default-track-value ctl/default-track-value]} - (ctl/create-cells [1 1 2 2])) - - (let [shapes (->> shapes (remove :hidden)) - all-shapes-rect (gco/shapes->rect shapes) - shapes+bounds - (->> shapes - (map #(vector % (grc/points->rect (get % :points))))) - - shapes-by-x - (->> shapes+bounds - (sort-by (comp :x second))) - - shapes-by-y - (->> shapes+bounds - (sort-by (comp :y second))) - - cols (calculate-tracks :x shapes-by-x) - rows (calculate-tracks :y shapes-by-y) - - num-cols (count cols) - num-rows (count rows) - - total-cols-width (->> cols (reduce #(+ %1 (:size %2)) 0)) - total-rows-height (->> rows (reduce #(+ %1 (:size %2)) 0)) - - column-gap - (if (= num-cols 1) - 0 - (/ (- (:width all-shapes-rect) total-cols-width) (dec num-cols))) - - row-gap - (if (= num-rows 1) - 0 - (/ (- (:height all-shapes-rect) total-rows-height) (dec num-rows))) - - layout-grid-rows (mapv (constantly ctl/default-track-value) rows) - layout-grid-columns (mapv (constantly ctl/default-track-value) cols) - - parent-childs-vector (gpt/to-vec (gpo/origin (:points parent)) (gpt/point all-shapes-rect)) - p-left (max 0 (:x parent-childs-vector)) - p-top (max 0 (:y parent-childs-vector))] - - (-> {:layout-grid-columns layout-grid-columns - :layout-grid-rows layout-grid-rows - :layout-gap {:row-gap row-gap - :column-gap column-gap} - :layout-padding {:p1 p-top :p2 p-left :p3 p-top :p4 p-left} - :layout-grid-dir (if (> num-cols num-rows) :row :column)} - (ctl/create-cells [1 1 num-cols num-rows]) - (assign-shape-cells rows cols)))))) + ([objects shapes parent] + (calculate-params* objects (remove :hidden shapes) parent))) diff --git a/common/test/common_tests/geom_flex_layout_test.cljc b/common/test/common_tests/geom_flex_layout_test.cljc index 160b4abd02..3638df0bf9 100644 --- a/common/test/common_tests/geom_flex_layout_test.cljc +++ b/common/test/common_tests/geom_flex_layout_test.cljc @@ -200,3 +200,8 @@ (t/is (= [[{:min-size 10 :max-size 10 :size 10}] 5] (fld/add-space-to-items :size :min-size :max-size 5 [{:min-size 10 :max-size 10 :size 10}])))) + +(t/deftest calculate-params-of-hidden-children-only-is-nil + ;; hidden children take no part, so a board of only hidden ones has + ;; nothing to derive the layout from, as a board with none + (t/is (nil? (params-for-child {:x 10 :y 20 :width 50 :height 50 :hidden true})))) diff --git a/common/test/common_tests/geom_grid_layout_test.cljc b/common/test/common_tests/geom_grid_layout_test.cljc index 68a9ffaa5b..1ba115d73d 100644 --- a/common/test/common_tests/geom_grid_layout_test.cljc +++ b/common/test/common_tests/geom_grid_layout_test.cljc @@ -443,3 +443,12 @@ (t/deftest grid-calculate-params-clamps-overflowing-child-padding (let [params (grid-params-for-children {:x -10 :y -5 :width 150 :height 150})] (t/is (= {:p1 0 :p2 0 :p3 0 :p4 0} (:layout-padding params))))) + +(t/deftest grid-calculate-params-of-hidden-children-only-is-the-empty-grid + ;; hidden children take no part, as in a board with none + (let [empty (glparams/calculate-params nil [] nil) + params (grid-params-for-children {:x 10 :y 20 :width 30 :height 30 :hidden true}) + shape (fn [params] (-> params + (update :layout-grid-cells #(map (fn [cell] (dissoc cell :id)) (vals %))) + (update :layout-grid-cells (partial sort-by (juxt :row :column)))))] + (t/is (= (shape empty) (shape params))))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 28faf9e86f..8f78d03cbb 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1764,6 +1764,12 @@ :ops [[:make-variant 2 0 0 0] [:make-variant 14 0 0 0] [:make-variant 14 0 0 0] [:style 13 27 1] [:make-variant 0] [:switch 18]]})))) +(t/deftest making-a-variant-of-a-hidden-main-lays-out-its-board + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:style 0 1 1] [:make-variant 0 0 0 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)]