From 764804a914a7bc082a5d927fe351487755e84b81 Mon Sep 17 00:00:00 2001 From: Pablo Alba Date: Fri, 27 Nov 2026 11:02:58 +0100 Subject: [PATCH] :bug: Keep fill children of hug boards from collapsing to 0.01 px (#13358) * :bug: Keep fill children of hug boards from collapsing to 0.01 px A flex or grid board that hugs an axis has no free space for fill children on that axis: along a flex direction any fill child, across it only when every child is fill, and in a grid any fill child. The layout shrank them to the 0.01 px minimum and the commit saved it. In component copies, the new render engine then wrapped their text one character per line. The rule lives in common as fix-child-sizing, with tests, and now covers grid boards. Every action that reaches this state applies it: - Resize board to fit content fixes, in the temporary objects only, the fill children that the hug would collapse. - The hug toggle changes the direct children only. It fixed fill sizing on all descendants, which froze collapsed grandchildren as overrides. Setting a child to fill inside a hug grid turns the grid to fix. - A fill shape that arrives in a hug board by paste, move, canvas drag or create variant turns fix at its size, and the board keeps hugging. A move used to turn the board fix instead, which still collapsed the shape when the board fitted its children. - When delete, hide, show or make absolute leaves only fill children, the board turns fix at its size, as moving a child out already did. This covers all the hide and show paths and deletes on other pages. - The plugin API sizing, hidden and visible setters go through the same events as the design panel, so plugins and MCP get the same guards. Files saved with collapsed sizes are not repaired by this change. Closes #13353 AI-assisted-by: claude-opus-5-5 AI-assisted-by: claude-sonnet-5-5 * :bug: Repair collapsed layout children Add migration 0040 to restore collapsed layout children from exact sizes and reflow the repaired boards. Guard singleton space-between layouts and require all collapsed descendants to be repaired before a board can hug. Closes #13353 AI-assisted-by: gpt-6-luna * :sparkles: Fix MR findings --- common/src/app/common/files/migrations.cljc | 19 +- .../geom/shapes/flex_layout/layout_data.cljc | 19 +- .../app/common/logic/collapsed_layout.cljc | 306 ++++++++++++ common/src/app/common/logic/shapes.cljc | 28 +- common/src/app/common/types/shape/layout.cljc | 87 +++- .../files_migrations_0040_test.cljc | 171 +++++++ .../geom_flex_layout_reflow_test.cljc | 160 ++++++ .../logic/collapsed_layout_test.cljc | 460 ++++++++++++++++++ .../common_tests/logic/move_shapes_test.cljc | 132 ++++- common/test/common_tests/runner.cljc | 6 + .../common_tests/types/shape_layout_test.cljc | 157 +++++- .../app/main/data/workspace/clipboard.cljs | 5 + .../app/main/data/workspace/modifiers.cljs | 46 +- .../app/main/data/workspace/shape_layout.cljs | 57 +-- .../src/app/main/data/workspace/shapes.cljs | 42 +- .../app/main/data/workspace/transforms.cljs | 15 +- .../src/app/main/data/workspace/variants.cljs | 3 + .../sidebar/options/menus/layer.cljs | 4 +- frontend/src/app/plugins/flex.cljs | 6 +- frontend/src/app/plugins/grid.cljs | 4 +- frontend/src/app/plugins/shape.cljs | 8 +- .../data/workspace_fit_content_test.cljs | 180 +++++++ .../data/workspace_hug_parents_test.cljs | 369 ++++++++++++++ .../data/workspace_shape_layout_test.cljs | 154 +++++- .../data/workspace_variants_test.cljs | 46 ++ .../logic/move_shapes_to_frame_test.cljs | 96 ++++ .../logic/pasting_in_containers_test.cljs | 54 +- .../frontend_tests/plugins/flex_test.cljs | 34 +- .../frontend_tests/plugins/grid_test.cljs | 19 + frontend/test/frontend_tests/runner.cljs | 4 + 30 files changed, 2578 insertions(+), 113 deletions(-) create mode 100644 common/src/app/common/logic/collapsed_layout.cljc create mode 100644 common/test/common_tests/files_migrations_0040_test.cljc create mode 100644 common/test/common_tests/geom_flex_layout_reflow_test.cljc create mode 100644 common/test/common_tests/logic/collapsed_layout_test.cljc create mode 100644 frontend/test/frontend_tests/data/workspace_fit_content_test.cljs create mode 100644 frontend/test/frontend_tests/data/workspace_hug_parents_test.cljs diff --git a/common/src/app/common/files/migrations.cljc b/common/src/app/common/files/migrations.cljc index 51520c888d..23e31b6d3a 100644 --- a/common/src/app/common/files/migrations.cljc +++ b/common/src/app/common/files/migrations.cljc @@ -21,6 +21,7 @@ [app.common.geom.shapes :as gsh] [app.common.geom.shapes.text :as gsht] [app.common.logging :as l] + [app.common.logic.collapsed-layout :as clc] [app.common.math :as mth] [app.common.path-names :as cpn] [app.common.schema :as sm] @@ -2500,6 +2501,21 @@ (concat (map :objects (vals (:pages-index data))) (map :objects (vals (:components data))))))) +;; Layout children saved at the 0.01 px minimum (issue 13353) get back an +;; exact size: the sizing of their main, the stored layout of a text, or a +;; hug board whose content got one of those. The layout then sizes them. +;; Every other shape stays as it is. +(defmethod migrate-data "0040-repair-collapsed-layout-children" + [data _] + (let [file {:id (:id data) :data data} + libs (some-> (:libs data) deref)] + (update data :pages-index + d/update-vals + (fn [page] + (let [{:keys [objects ids]} (clc/repair-page file page libs)] + (cond-> page + (seq ids) (assoc :objects objects))))))) + (def available-migrations (into (d/ordered-set) ["legacy-2" @@ -2594,4 +2610,5 @@ "0036-clear-nil-keys-in-objects" "0037-fix-shape-svg-attrs-and-defs" "0038-sanitize-variant-property-text" - "0039-align-variant-component-names"])) + "0039-align-variant-component-names" + "0040-repair-collapsed-layout-children"])) diff --git a/common/src/app/common/geom/shapes/flex_layout/layout_data.cljc b/common/src/app/common/geom/shapes/flex_layout/layout_data.cljc index dfdd8cd094..56f1c03206 100644 --- a/common/src/app/common/geom/shapes/flex_layout/layout_data.cljc +++ b/common/src/app/common/geom/shapes/flex_layout/layout_data.cljc @@ -60,10 +60,15 @@ (persistent! (cond-> result (some? line-data) (conj! line-data))) (let [[child-bounds child] (first children) + ;; No line yet on the first child: the accumulators start at 0 + ;; (JS reads nil as 0 in arithmetic, the JVM throws) {:keys [line-min-width line-min-height line-max-width line-max-height num-children - children-data]} line-data + children-data] + :or {line-min-width 0 line-min-height 0 + line-max-width 0 line-max-height 0 + num-children 0}} line-data child-width (gpo/width-points child-bounds) child-height (gpo/height-points child-bounds) @@ -141,7 +146,8 @@ (defn add-space-to-items ;; Distributes the remainder space between the lines [prop prop-min prop-max to-share items] - (let [num-items (->> items (remove #(mth/close? (get % prop) (get % prop-max))) count) + (let [;; A line without a value for `prop` counts as 0 (JS reads nil as 0) + num-items (->> items (remove #(mth/close? (or (get % prop) 0) (get % prop-max))) count) per-line-target (/ to-share num-items)] (loop [items (seq items) @@ -222,13 +228,14 @@ num-lines (count layout-lines) ;; When align-items is stretch we need to adjust the main axis size to grow for the full content + ;; (with no lines there is nothing to adjust: the JVM throws on the division by 0, JS gets NaN) stretch-width-fix - (if (and col? (ctl/content-stretch? parent) (not auto-width?)) + (if (and col? (ctl/content-stretch? parent) (not auto-width?) (pos? num-lines)) (/ (- layout-width (* layout-gap-col (dec num-lines)) total-max-width) num-lines) 0) stretch-height-fix - (if (and row? (ctl/content-stretch? parent) (not auto-height?)) + (if (and row? (ctl/content-stretch? parent) (not auto-height?) (pos? num-lines)) (/ (- layout-height (* layout-gap-row (dec num-lines)) total-max-height) num-lines) 0) @@ -359,7 +366,7 @@ (and row? space-around?) (/ (max layout-gap-col (/ (- width line-width) num-children)) 2) - (and row? space-between? (not auto-width?)) + (and row? space-between? (not auto-width?) (> num-children 1)) (max layout-gap-col (/ (- width line-width) (dec num-children))) :else @@ -372,7 +379,7 @@ (and col? space-around?) (/ (max layout-gap-row (/ (- height line-height) num-children)) 2) - (and col? space-between? (not auto-height?)) + (and col? space-between? (not auto-height?) (> num-children 1)) (max layout-gap-row (/ (- height line-height) (dec num-children))) :else diff --git a/common/src/app/common/logic/collapsed_layout.cljc b/common/src/app/common/logic/collapsed_layout.cljc new file mode 100644 index 0000000000..069188c22c --- /dev/null +++ b/common/src/app/common/logic/collapsed_layout.cljc @@ -0,0 +1,306 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns app.common.logic.collapsed-layout + "Repairs layout children saved at the 0.01 px minimum size (issue 13353). + It only gives back a size that is known exactly: the sizing of the main of + a copy, the stored layout of a text, or a hug board whose content got one + of those. Anything else stays as it is." + (:require + [app.common.data :as d] + [app.common.files.helpers :as cfh] + [app.common.geom.modifiers :as gm] + [app.common.geom.shapes :as gsh] + [app.common.math :as mth] + [app.common.types.component :as ctk] + [app.common.types.file :as ctf] + [app.common.types.modifiers :as ctm] + [app.common.types.shape.layout :as ctl])) + +(def ^:private min-size + "Largest size that still counts as the 0.01 px minimum." + 0.011) + +(def ^:private narrow-size + "A text or layout board narrower than this (px) under a restored shape + counts as damaged: a word needs more, so it is a leftover of the collapse." + 8) + +(def ^:private axes + [:h :v]) + +(def ^:private axis-attrs + {:h {:sizing :layout-item-h-sizing :size :width} + :v {:sizing :layout-item-v-sizing :size :height}}) + +(defn- layout-child? + [objects shape] + (and (ctl/any-layout-immediate-child? objects shape) + (not (ctl/position-absolute? shape)))) + +(defn- small? + [shape axis] + (let [size (get shape (get-in axis-attrs [axis :size]))] + (and (number? size) (<= size min-size)))) + +(defn- measurable? + "Texts and layout boards size themselves, so a `:fix` at the minimum is + damage. Any other shape can be thin on purpose (dividers, spacers)." + [shape] + (or (cfh/text-shape? shape) + (and (cfh/frame-shape? shape) (ctl/any-layout? shape)))) + +(defn- collapsed-axis? + "With `any-fix?` a `:fix` shape of any type counts: a copy whose main + fills proves the damage." + [objects shape axis any-fix?] + (let [sizing (get-in axis-attrs [axis :sizing])] + (and (small? shape axis) + (case (get shape sizing) + :fix (or any-fix? (measurable? shape)) + ;; A fill child of a hug parent in a collapsing configuration + :fill (not= :fill (get (ctl/fix-child-sizing objects {} shape) sizing)) + false)))) + +(defn- stuck-axes + "Axes on which the layout child `shape` is stuck at the minimum size." + [objects shape any-fix?] + (when (layout-child? objects shape) + (into #{} (filter #(collapsed-axis? objects shape % any-fix?)) axes))) + +(defn- depth + [objects shape] + (count (cfh/get-parent-ids objects (:id shape)))) + +(defn- add-repaired + [acc id shape-axes] + (update-in acc [:repaired id] (fnil into #{}) shape-axes)) + +(defn- resize + [shape axis value] + (let [attr (get-in axis-attrs [axis :size])] + (gsh/transform-shape shape (ctm/change-dimensions-modifiers shape attr value {:ignore-lock? true})))) + +;; Copies: exact restore from the main shape + +(defn- frozen-axis? + "The override of a sizing at `:fix`." + [shape axis] + (let [sizing (get-in axis-attrs [axis :sizing])] + (and (= :fix (get shape sizing)) + (contains? (:touched shape) sizing)))) + +(defn- collapsed-lines? + "A stored layout of more than one line, each with one character." + [position-data] + (and (> (count position-data) 1) + (every? #(some-> (:text %) count (<= 1)) position-data))) + +(defn- damaged-frozen? + "A frozen override that shows the collapse: a text or layout board narrower + than `narrow-size` on the axis, or a text whose stored layout is collapsed. + Any other shape, or a wider one, may keep its override on purpose." + [shape axis] + (and (measurable? shape) + (or (< (get shape (get-in axis-attrs [axis :size])) narrow-size) + (and (cfh/text-shape? shape) + (collapsed-lines? (:position-data shape)))))) + +(defn- copy-restores + "Subset of `shape-axes` where giving the copy `shape` the sizing of its + `ref-shape` repairs it: the main fills or hugs there and the parent of the + copy does not collapse that sizing. The siblings are checked against the + restored shape." + [objects shape ref-shape shape-axes] + (into #{} + (filter (fn [axis] + (let [sizing (get-in axis-attrs [axis :sizing]) + value (get ref-shape sizing) + candidate (assoc shape sizing value)] + (and (contains? #{:fill :auto} value) + (= value (get (ctl/fix-child-sizing (assoc objects (:id shape) candidate) {} candidate) + sizing)))))) + shape-axes)) + +(defn- restore-axes + [shape ref-shape shape-axes] + (let [shape (reduce (fn [shape axis] + (let [sizing (get-in axis-attrs [axis :sizing]) + size-attr (get-in axis-attrs [axis :size])] + ;; A collapsed shape also gets the size of the main, so + ;; the layout that follows starts from a valid geometry: + ;; a fill child of a board at 0.01 would get a negative size + (cond-> (-> shape + (assoc sizing (get ref-shape sizing)) + (d/update-when :touched disj sizing)) + (and (small? shape axis) + (> (get ref-shape size-attr) min-size)) + (resize axis (get ref-shape size-attr))))) + shape + shape-axes)] + (cond-> shape + (empty? (:touched shape)) (dissoc :touched)))) + +(defn- restore-copies + "Restores the sizing of the copies stuck at the minimum, and of the damaged + frozen shapes inside a restored one. Goes top-down so the shapes inside a + restored one see their new parent." + [file page libs {:keys [objects] :as acc}] + (let [copies (->> (vals objects) + (filter #(and (ctk/in-component-copy? %) + (layout-child? objects %) + (some (fn [axis] (or (small? % axis) + (and (frozen-axis? % axis) + (damaged-frozen? % axis)))) + axes))) + (sort-by #(depth objects %)))] + (reduce + (fn [{:keys [objects repaired] :as acc} copy] + (let [shape (get objects (:id copy)) + inherited (into #{} (mapcat repaired) (cfh/get-parent-ids objects (:id shape))) + stuck (into (stuck-axes objects shape true) + (filter #(and (frozen-axis? shape %) + (contains? inherited %) + (damaged-frozen? shape %))) + axes) + ref-shape (when (seq stuck) + (ctf/find-ref-shape file page libs shape)) + restorable (when ref-shape + (copy-restores objects shape ref-shape stuck))] + (if (empty? restorable) + acc + (-> acc + (assoc-in [:objects (:id shape)] (restore-axes shape ref-shape restorable)) + (add-repaired (:id shape) restorable))))) + acc + copies))) + +;; Texts: the width of the stored layout + +(defn- position-data-width + "Width of the lines in the stored layout of the text `shape`. Nil when + there is none, or when it is a collapsed layout (one character per line)." + [{:keys [position-data]}] + (when (seq position-data) + (let [min-x (transduce (map :x) min ##Inf position-data) + max-x (transduce (map #(+ (:x %) (:width %))) max ##-Inf position-data) + width (- max-x min-x)] + (when (and (> width min-size) (not (collapsed-lines? position-data))) + width)))) + +(defn- fix-texts + "Gives the text that is not a copy and is stuck at the minimum width the + width of its stored layout, with one px of margin." + [{:keys [objects] :as acc}] + (reduce + (fn [acc text] + (let [width (when (contains? (stuck-axes objects text false) :h) + (position-data-width text))] + (if (some? width) + (-> acc + (assoc-in [:objects (:id text)] + (-> (resize text :h (inc (mth/ceil width))) + (assoc :layout-item-h-sizing :fix))) + (add-repaired (:id text) #{:h})) + acc))) + acc + (filter #(and (cfh/text-shape? %) + (not (ctk/in-component-copy? %)) + (small? % :h)) + (vals objects)))) + +;; Boards: hug the content that got an exact size + +(defn- hug-axes + "Subset of the `stuck` axes of the board `board` where it can hug: it has + collapsed descendants, each one repaired by now, and no direct child still + fills that axis (a hug board would collapse it again)." + [original {:keys [objects repaired]} board stuck] + (letfn [(layout-descendants [shape] + (mapcat (fn [child] + (cons child (layout-descendants child))) + (remove ctl/position-absolute? + (keep #(get objects %) (:shapes shape)))))] + (let [children (->> (:shapes board) + (keep #(get objects %)) + (remove ctl/position-absolute?))] + (into #{} + (filter (fn [axis] + (let [sizing (get-in axis-attrs [axis :sizing]) + collapsed (filter #(contains? (stuck-axes original (get original (:id %)) true) axis) + (layout-descendants board))] + (and (seq collapsed) + (every? #(contains? (get repaired (:id %)) axis) collapsed) + (not-any? #(= :fill (get % sizing)) children))))) + stuck)))) + +(defn- hug-boards + "Sets a board that is not a copy and is stuck at the minimum to hug, only + when everything collapsed inside got an exact size. Goes bottom-up, so a + board that starts to hug counts for its parent." + [original {:keys [objects] :as acc}] + (reduce + (fn [{:keys [objects] :as acc} board] + (let [board (get objects (:id board)) + stuck (stuck-axes objects board false) + to-hug (when (seq stuck) + (hug-axes original acc board stuck))] + (if (empty? to-hug) + acc + (-> acc + (assoc-in [:objects (:id board)] + (reduce #(assoc %1 (get-in axis-attrs [%2 :sizing]) :auto) board to-hug)) + (add-repaired (:id board) to-hug))))) + acc + (->> (vals objects) + (filter #(and (ctl/any-layout? %) + (not (ctk/in-component-copy? %)) + (some (fn [axis] (small? % axis)) axes))) + (sort-by #(depth objects %) >)))) + +;; Layout + +(defn- layout-roots + "Top-most layout boards among the repaired `ids` and their parents." + [objects ids] + (let [boards (into #{} + (mapcat (fn [id] + (let [shape (get objects id) + parent (get objects (:parent-id shape))] + (cond-> [] + (ctl/any-layout? shape) (conj id) + (ctl/any-layout? parent) (conj (:id parent)))))) + ids)] + (remove #(some boards (cfh/get-parent-ids objects %)) boards))) + +(defn- layout-repaired + "Runs the layout of the repaired boards, so the shapes get the sizes and + positions the layout gives them." + [objects ids] + (let [roots (layout-roots objects ids)] + (if (empty? roots) + objects + (let [modif-tree (gm/set-objects-modifiers + (into {} (map #(vector % {:modifiers (ctm/reflow-modifiers)})) roots) + objects)] + (gsh/apply-objects-modifiers objects modif-tree))))) + +(defn repair-page + "Repairs the layout children of `page` stuck at the minimum size, with the + `libs` of its file to find the main of the copies. Returns the new + `:objects`, the very same instance when nothing needed repair, and the + `:ids` of the repaired shapes." + [file page libs] + (let [objects (:objects page) + acc (->> {:objects objects :repaired {}} + (restore-copies file page libs) + (fix-texts) + (hug-boards objects)) + ids (into #{} (keys (:repaired acc)))] + (if (empty? ids) + {:objects objects :ids ids} + {:objects (layout-repaired (:objects acc) ids) + :ids ids}))) diff --git a/common/src/app/common/logic/shapes.cljc b/common/src/app/common/logic/shapes.cljc index 5193b028ce..fd612aea57 100644 --- a/common/src/app/common/logic/shapes.cljc +++ b/common/src/app/common/logic/shapes.cljc @@ -471,6 +471,9 @@ parent (get objects parent-id) all-parents (into #{parent-id} (map #(cfh/get-parent-id objects %)) ids) parents (if ignore-parents? #{parent-id} all-parents) + old-parents (disj parents parent-id) + + arriving-ids (remove #(= parent-id (cfh/get-parent-id objects %)) ids) children-ids (mapcat #(cfh/get-children-ids-with-self objects %) ids) @@ -645,17 +648,22 @@ :constraints-v (gsh/default-constraints-v moved-shape)))) {:ignore-touched true}) - ;; Fix the sizing when moving a shape - (pcb/update-shapes parents - (fn [parent] - (if (ctl/flex-layout? parent) - (cond-> parent - (ctl/change-h-sizing? (:id parent) objects (:shapes parent)) - (assoc :layout-item-h-sizing :fix) + ;; A hug parent cannot give room to a fill shape that arrives, so + ;; the shape is fixed and the parent keeps hugging + (pcb/update-shapes arriving-ids + (fn [shape objects] + (ctl/fix-child-sizing objects {} shape)) + {:with-objects? true}) - (ctl/change-v-sizing? (:id parent) objects (:shapes parent)) - (assoc :layout-item-v-sizing :fix)) - parent))) + ;; A hug parent that loses the children that sized it stops hugging + (pcb/update-shapes old-parents + (fn [parent] + (cond-> parent + (ctl/change-h-sizing? (:id parent) objects (:shapes parent)) + (assoc :layout-item-h-sizing :fix) + + (ctl/change-v-sizing? (:id parent) objects (:shapes parent)) + (assoc :layout-item-v-sizing :fix)))) ;; Change the grid cell in a grid layout (cond-> (ctl/grid-layout? objects parent-id) diff --git a/common/src/app/common/types/shape/layout.cljc b/common/src/app/common/types/shape/layout.cljc index e6e776d8a8..686402d39a 100644 --- a/common/src/app/common/types/shape/layout.cljc +++ b/common/src/app/common/types/shape/layout.cljc @@ -568,25 +568,33 @@ (defn change-h-sizing? [frame-id objects children-ids] - (and (flex-layout? objects frame-id) - (auto-width? objects frame-id) - (or (and (col? objects frame-id) - (->> children-ids - (remove (partial position-absolute? objects)) - (every? (partial fill-width? objects)))) - (and (row? objects frame-id) - (->> children-ids - (remove (partial position-absolute? objects)) - (some (partial fill-width? objects))))))) + (let [grid? (grid-layout? objects frame-id) + flex? (flex-layout? objects frame-id) + children-ids (remove (partial position-absolute? objects) children-ids)] + (and (or grid? flex?) + (auto-width? objects frame-id) + (or (and grid? + (some (partial fill-width? objects) children-ids)) + (and flex? + (or (and (col? objects frame-id) + (every? (partial fill-width? objects) children-ids)) + (and (row? objects frame-id) + (some (partial fill-width? objects) children-ids)))))))) (defn change-v-sizing? [frame-id objects children-ids] - (and (flex-layout? objects frame-id) - (auto-height? objects frame-id) - (or (and (col? objects frame-id) - (some (partial fill-height? objects) children-ids)) - (and (row? objects frame-id) - (every? (partial fill-height? objects) children-ids))))) + (let [grid? (grid-layout? objects frame-id) + flex? (flex-layout? objects frame-id) + children-ids (remove (partial position-absolute? objects) children-ids)] + (and (or grid? flex?) + (auto-height? objects frame-id) + (or (and grid? + (some (partial fill-height? objects) children-ids)) + (and flex? + (or (and (col? objects frame-id) + (some (partial fill-height? objects) children-ids)) + (and (row? objects frame-id) + (every? (partial fill-height? objects) children-ids)))))))) (defn remove-layout-container-data [shape] @@ -1760,3 +1768,50 @@ (assoc :layout-item-h-sizing :fix) (= (:layout-item-v-sizing shape) :fill) (assoc :layout-item-v-sizing :fix))) + +(defn fix-child-sizing + "Fixes the fill sizing of `shape` that a hug parent (merged with + `parent-changes`) would collapse." + [objects parent-changes shape] + + (let [parent (-> (cfh/get-parent objects (:id shape)) + (d/deep-merge parent-changes)) + + auto-width? (auto-width? parent) + auto-height? (auto-height? parent) + grid? (grid-layout? parent) + col? (and (not grid?) (col? parent)) + row? (and (not grid?) (row? parent)) + + all-children (->> parent + :shapes + (map (d/getf objects)) + (remove position-absolute?))] + + (cond-> shape + ;; A hug grid cannot give room to fill children on that axis + (and grid? auto-width? (fill-width? shape)) + (assoc :layout-item-h-sizing :fix) + + (and grid? auto-height? (fill-height? shape)) + (assoc :layout-item-v-sizing :fix) + + ;; If the parent is hug width and the direction column + ;; change to fixed when ALL children are fill + (and col? auto-width? (every? fill-width? all-children)) + (assoc :layout-item-h-sizing :fix) + + ;; If the parent is hug height and the direction is column + ;; change to fixed when ANY children is fill + (and col? auto-height? (fill-height? shape)) + (assoc :layout-item-v-sizing :fix) + + ;; If the parent is hug width and the direction row + ;; change to fixed when ANY children is fill + (and row? auto-width? (fill-width? shape)) + (assoc :layout-item-h-sizing :fix) + + ;; If the parent is hug height and the direction row + ;; change to fixed when ALL children are fill + (and row? auto-height? (every? fill-height? all-children)) + (assoc :layout-item-v-sizing :fix)))) diff --git a/common/test/common_tests/files_migrations_0040_test.cljc b/common/test/common_tests/files_migrations_0040_test.cljc new file mode 100644 index 0000000000..0507fe5802 --- /dev/null +++ b/common/test/common_tests/files_migrations_0040_test.cljc @@ -0,0 +1,171 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns common-tests.files-migrations-0040-test + (:require + [app.common.data :as d] + [app.common.files.migrations :as cfm] + [app.common.files.validate :as cfv] + [app.common.geom.shapes :as gsh] + [app.common.test-helpers.components :as thc] + [app.common.test-helpers.compositions :as tho] + [app.common.test-helpers.files :as thf] + [app.common.test-helpers.ids-map :as thi] + [app.common.types.file :as ctf] + [app.common.types.modifiers :as ctm] + [app.common.types.pages-list :as ctpl] + [app.common.types.shape-tree :as ctst] + [clojure.test :as t])) + +(t/use-fixtures :each thi/test-fixture) + +;; ====================================================================== +;; 0040-repair-collapsed-layout-children +;; Layout children saved at the 0.01 px minimum (issue 13353) get back the +;; sizing of their main, and the layout gives them their size. +;; ====================================================================== + +(def ^:private migration-id + "0040-repair-collapsed-layout-children") + +(def ^:private flex-defaults + {:layout :flex + :layout-flex-dir :row + :layout-padding-type :simple + :layout-padding {:p1 0 :p2 0 :p3 0 :p4 0} + :layout-gap-type :multiple + :layout-gap {:row-gap 0 :column-gap 0} + :layout-align-items :start + :layout-align-content :stretch + :layout-justify-items :start + :layout-justify-content :start + :layout-wrap-type :nowrap + :width 100 + :height 50}) + +(defn- raw-update + [file label f] + (let [page (thf/current-page file)] + (update file :data + (fn [fdata] + (ctpl/update-page fdata (:id page) + #(ctst/set-shape % (f (ctst/get-shape % (thi/id label))))))))) + +(defn- resize + [shape attr value] + (gsh/transform-shape shape (ctm/change-dimensions-modifiers shape attr value {:ignore-lock? true}))) + +(defn- add-main + "A component whose main is a row (67 px, padding 8, gap 4) with a 16 px icon + and a fill board (padding 2) that holds a fill text." + [file] + (-> file + (tho/add-frame :main-row (merge flex-defaults {:width 67 + :layout-padding {:p1 8 :p2 8 :p3 8 :p4 8} + :layout-gap {:row-gap 0 :column-gap 4}})) + (tho/add-frame :main-board (merge flex-defaults {:parent-label :main-row + :layout-flex-dir :column + :layout-item-h-sizing :fill + :layout-padding {:p1 0 :p2 2 :p3 0 :p4 2}})) + (tho/add-text :main-text "Input" + :text-params {:parent-label :main-board :grow-type :fixed :width 50 :height 16 + :layout-item-h-sizing :fill}) + (tho/add-rect :main-icon :parent-label :main-row :width 16 :height 16) + (thc/make-component :component1 :main-row))) + +(defn- add-copy + "A copy of the component of `library` with the board and the text stored + collapsed, as the issue shows." + [file library] + (let [touched #{:geometry-group :layout-item-h-sizing} + file (thc/instantiate-component file :component1 :copy-row + :library library + :children-labels [:c1 :c2 :c3]) + copy-of (fn [file main-label copy-label] + (let [copy (d/seek #(= (thi/id main-label) (:shape-ref %)) + (vals (:objects (thf/current-page file))))] + (thi/set-id! copy-label (:id copy)) + file))] + (-> file + (copy-of :main-board :copy-board) + (copy-of :main-text :copy-text) + (raw-update :copy-row #(assoc % :layout-item-h-sizing :fix)) + (raw-update :copy-board #(-> (resize % :width 1) + (resize :width 0.01) + (assoc :layout-item-h-sizing :fix :touched touched))) + (raw-update :copy-text #(-> (resize % :width 3.99) + (assoc :layout-item-h-sizing :fix + :touched (into touched [:content-group :text-content-attribute :text-content-text]))))))) + +(defn- migrate + "Runs the migration on the data of `file`; `libs` are the libraries of its file." + [file libs] + (cfm/migrate-data (assoc (ctf/file-data file) :id (:id file) :libs (delay libs)) migration-id)) + +(defn- migrated-file + [file libs] + (let [data (dissoc (migrate file libs) :libs)] + (assoc file :data data))) + +(defn- shape + [file label] + (get (:objects (thf/current-page file)) (thi/id label))) + +(t/deftest migration-0040-repairs-the-copy-of-a-main-in-the-same-file + (let [file (-> (thf/sample-file :file1) + (add-main) + (add-copy nil)) + file' (migrated-file file {(:id file) file}) + board (shape file' :copy-board) + text (shape file' :copy-text)] + (t/is (<= (:width (shape file :copy-board)) 0.011)) + (t/is (= [:fill 31.0] [(:layout-item-h-sizing board) (double (:width board))])) + (t/is (= [:fill 27.0] [(:layout-item-h-sizing text) (double (:width text))])) + (t/is (= #{:geometry-group} (:touched board))) + (t/is (some? (cfv/validate-file-schema! file'))))) + +(t/deftest migration-0040-repairs-the-copy-of-a-main-in-a-library + (let [library (-> (thf/sample-file :lib :page-label :lib-page) + (add-main)) + file (-> (thf/sample-file :file1) + (add-copy library)) + libs {(:id library) library} + file' (migrated-file file libs) + board (shape file' :copy-board) + text (shape file' :copy-text)] + (t/is (= [:fill 31.0] [(:layout-item-h-sizing board) (double (:width board))])) + (t/is (= [:fill 27.0] [(:layout-item-h-sizing text) (double (:width text))])) + (t/is (some? (cfv/validate-file-schema! file'))))) + +(t/deftest migration-0040-leaves-library-copies-without-the-libraries + (let [library (-> (thf/sample-file :lib :page-label :lib-page) + (add-main)) + file (-> (thf/sample-file :file1) + (add-copy library)) + data (ctf/file-data file) + data' (dissoc (migrate file nil) :libs)] + (t/is (= (dissoc data :libs) data')) + (t/is (identical? (get-in data [:pages-index (:id (thf/current-page file)) :objects]) + (get-in data' [:pages-index (:id (thf/current-page file)) :objects]))))) + +(t/deftest migration-0040-keeps-a-healthy-file-as-it-is + (let [file (-> (thf/sample-file :file1) + (add-main)) + data (ctf/file-data file) + data' (migrate file {(:id file) file}) + page #(get-in % [:pages-index (:id (thf/current-page file))])] + (t/is (= (dissoc data' :libs) data)) + (t/is (identical? (:objects (page data)) (:objects (page data')))))) + +(t/deftest migration-0040-repairs-once + (let [file (-> (thf/sample-file :file1) + (add-main) + (add-copy nil)) + file' (migrated-file file {(:id file) file}) + data' (ctf/file-data file') + data'' (migrate file' {(:id file') file'}) + page #(get-in % [:pages-index (:id (thf/current-page file))])] + (t/is (identical? (:objects (page data')) (:objects (page data'')))))) diff --git a/common/test/common_tests/geom_flex_layout_reflow_test.cljc b/common/test/common_tests/geom_flex_layout_reflow_test.cljc new file mode 100644 index 0000000000..d8dbbf325e --- /dev/null +++ b/common/test/common_tests/geom_flex_layout_reflow_test.cljc @@ -0,0 +1,160 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns common-tests.geom-flex-layout-reflow-test + "A flex reflow gives the same numbers on the JVM and in JS." + (:require + [app.common.geom.modifiers :as gm] + [app.common.geom.shapes :as gsh] + [app.common.math :as mth] + [app.common.test-helpers.compositions :as tho] + [app.common.test-helpers.files :as thf] + [app.common.test-helpers.ids-map :as thi] + [app.common.types.modifiers :as ctm] + [clojure.test :as t])) + +(t/use-fixtures :each thi/test-fixture) + +;; ---- Setup + +(def ^:private flex-defaults + {:layout :flex + :layout-flex-dir :row + :layout-padding-type :simple + :layout-padding {:p1 0 :p2 0 :p3 0 :p4 0} + :layout-gap-type :multiple + :layout-gap {:row-gap 0 :column-gap 0} + :layout-align-items :start + :layout-align-content :stretch + :layout-justify-items :start + :layout-justify-content :start + :layout-wrap-type :nowrap + :width 100 + :height 50}) + +(defn- add-flex + [file label & {:as params}] + (tho/add-frame file label (merge flex-defaults params))) + +(defn- add-rect + [file label parent-label width height & {:as params}] + (tho/add-rect file label (merge {:parent-label parent-label :width width :height height} params))) + +(defn- reflow + "The objects of the page after the layout of the frame `label`." + [file label] + (let [objects (:objects (thf/current-page file)) + modif-tree (gm/set-objects-modifiers {(thi/id label) {:modifiers (ctm/reflow-modifiers)}} objects)] + (gsh/apply-objects-modifiers objects modif-tree))) + +(defn- rect-of + "[x y width height] of the shape `label`." + [objects label] + (let [{:keys [x y width height]} (get objects (thi/id label))] + (mapv #(mth/precision % 2) [x y width height]))) + +;; ---- Tests + +(t/deftest row-places-children-with-padding-and-gap + (let [file (-> (thf/sample-file :file1) + (add-flex :row :width 200 :layout-padding {:p1 10 :p2 10 :p3 10 :p4 10} + :layout-gap {:row-gap 0 :column-gap 5}) + (add-rect :a :row 20 20) + (add-rect :b :row 30 20)) + objects (reflow file :row)] + ;; A row lays out its children from the last one in `:shapes` + (t/is (= [10.0 10.0 30.0 20.0] (rect-of objects :b))) + (t/is (= [45.0 10.0 20.0 20.0] (rect-of objects :a))))) + +(t/deftest column-hug-sizes-the-board + (let [file (-> (thf/sample-file :file1) + (add-flex :col :layout-flex-dir :column :layout-item-h-sizing :auto + :layout-item-v-sizing :auto + :layout-padding {:p1 5 :p2 5 :p3 5 :p4 5} + :layout-gap {:row-gap 3 :column-gap 0}) + (add-rect :a :col 20 10) + (add-rect :b :col 30 10)) + objects (reflow file :col)] + (t/is (= [0.0 0.0 40.0 33.0] (rect-of objects :col))) + (t/is (= [5.0 5.0 30.0 10.0] (rect-of objects :b))) + (t/is (= [5.0 18.0 20.0 10.0] (rect-of objects :a))))) + +(t/deftest fill-child-takes-the-remaining-space + (let [file (-> (thf/sample-file :file1) + (add-flex :row :layout-gap {:row-gap 0 :column-gap 10}) + (add-rect :a :row 20 20) + (add-rect :b :row 10 20 :layout-item-h-sizing :fill)) + objects (reflow file :row)] + (t/is (= [0.0 0.0 70.0 20.0] (rect-of objects :b))) + (t/is (= [80.0 0.0 20.0 20.0] (rect-of objects :a))))) + +(t/deftest space-between-spreads-the-children + (let [file (-> (thf/sample-file :file1) + (add-flex :row :layout-justify-content :space-between) + (add-rect :a :row 20 20) + (add-rect :b :row 20 20) + (add-rect :c :row 20 20)) + objects (reflow file :row)] + (t/is (= [80.0 40.0 0.0] (mapv #(first (rect-of objects %)) [:a :b :c]))))) + +(t/deftest space-between-keeps-the-base-gap-with-one-child + (let [file (-> (thf/sample-file :file1) + (add-flex :row :layout-justify-content :space-between + :layout-gap {:row-gap 0 :column-gap 5}) + (add-rect :a :row 20 20)) + objects (reflow file :row)] + (t/is (= [0.0 0.0 20.0 20.0] (rect-of objects :a))))) + +(t/deftest space-between-keeps-the-base-gap-with-one-child-in-a-column + (let [file (-> (thf/sample-file :file1) + (add-flex :column :layout-flex-dir :column + :layout-justify-content :space-between + :layout-gap {:row-gap 5 :column-gap 0}) + (add-rect :a :column 20 20)) + objects (reflow file :column)] + (t/is (= [0.0 0.0 20.0 20.0] (rect-of objects :a))))) + +(t/deftest space-around-and-evenly-spread-the-children + (let [around (-> (thf/sample-file :file1) + (add-flex :row :layout-justify-content :space-around) + (add-rect :a :row 20 20) + (add-rect :b :row 20 20) + (reflow :row)) + evenly (-> (thf/sample-file :file2) + (add-flex :row2 :layout-justify-content :space-evenly) + (add-rect :c :row2 20 20) + (add-rect :d :row2 20 20) + (reflow :row2))] + (t/is (= [65.0 15.0] (mapv #(first (rect-of around %)) [:a :b]))) + (t/is (= [60.0 20.0] (mapv #(first (rect-of evenly %)) [:c :d]))))) + +(t/deftest wrap-moves-the-overflow-to-a-new-line + (let [file (-> (thf/sample-file :file1) + (add-flex :row :width 50 :layout-wrap-type :wrap + :layout-gap {:row-gap 4 :column-gap 0}) + (add-rect :a :row 30 10) + (add-rect :b :row 30 10)) + objects (reflow file :row)] + ;; The two lines share the 50 px of height, 4 px apart + (t/is (= [0.0 0.0] (subvec (rect-of objects :b) 0 2))) + (t/is (= [0.0 27.0] (subvec (rect-of objects :a) 0 2))))) + +(t/deftest nested-fill-boards-share-the-row + ;; Issue 13353: a fixed row holds an icon and a fill board that holds a + ;; fill rect. The row is 67 wide, 8 px of padding, 4 px of gap, 16 px icon. + (let [file (-> (thf/sample-file :file1) + (add-flex :row :width 67 :layout-padding {:p1 8 :p2 8 :p3 8 :p4 8} + :layout-gap {:row-gap 0 :column-gap 4}) + (add-flex :board :parent-label :row :layout-flex-dir :column + :layout-item-h-sizing :fill :width 60 + :layout-padding {:p1 0 :p2 2 :p3 0 :p4 2}) + (add-rect :inner :board 10 16 :layout-item-h-sizing :fill) + (add-rect :icon :row 16 16)) + objects (reflow file :row)] + (t/is (= 31.0 (nth (rect-of objects :board) 2))) + (t/is (= 27.0 (nth (rect-of objects :inner) 2))) + (t/is (= 28.0 (first (rect-of objects :board)))) + (t/is (= 8.0 (first (rect-of objects :icon)))))) diff --git a/common/test/common_tests/logic/collapsed_layout_test.cljc b/common/test/common_tests/logic/collapsed_layout_test.cljc new file mode 100644 index 0000000000..dfcb30ef60 --- /dev/null +++ b/common/test/common_tests/logic/collapsed_layout_test.cljc @@ -0,0 +1,460 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns common-tests.logic.collapsed-layout-test + (:require + [app.common.data :as d] + [app.common.geom.shapes :as gsh] + [app.common.logic.collapsed-layout :as clc] + [app.common.math :as mth] + [app.common.test-helpers.components :as thc] + [app.common.test-helpers.compositions :as tho] + [app.common.test-helpers.files :as thf] + [app.common.test-helpers.ids-map :as thi] + [app.common.test-helpers.shapes :as ths] + [app.common.types.modifiers :as ctm] + [app.common.types.pages-list :as ctpl] + [app.common.types.shape-tree :as ctst] + [clojure.test :as t])) + +(t/use-fixtures :each thi/test-fixture) + +;; ---- Setup + +(defn- raw-update + "Updates the shape directly, to build data the editor would not produce." + [file label f] + (let [page (thf/current-page file)] + (update file :data + (fn [fdata] + (ctpl/update-page fdata (:id page) + #(ctst/set-shape % (f (ctst/get-shape % (thi/id label))))))))) + +(defn- resized + "Resizes in two steps: a modifier that shrinks a shape by a factor below + 1/1000 is ignored." + [shape attr value] + (let [resize #(gsh/transform-shape %1 (ctm/change-dimensions-modifiers %1 attr %2 {:ignore-lock? true}))] + (cond-> shape + (< value 1) (resize 1) + :always (resize value)))) + +(defn- collapse + "Leaves the shape at `size` on the axis, with the given sizing and touched." + [file label attr sizing size & {:keys [touched]}] + (raw-update file label + (fn [shape] + (cond-> (-> (resized shape attr size) + (assoc (if (= attr :width) :layout-item-h-sizing :layout-item-v-sizing) sizing)) + (some? touched) (assoc :touched touched))))) + +(defn- set-sizing + [file label & {:as attrs}] + (raw-update file label #(merge % attrs))) + +(defn- copy-of + "Labels the copy shape that points at the main shape `main-label`." + [file main-label copy-label] + (let [main-id (thi/id main-label) + copy (d/seek #(= main-id (:shape-ref %)) (vals (:objects (thf/current-page file))))] + (thi/set-id! copy-label (:id copy)) + file)) + +(defn- repair + "The result of repairing the current page of `file`." + ([file] + (repair file {(:id file) file})) + ([file libs] + (clc/repair-page file (thf/current-page file) libs))) + +(defn- shape + [result label] + (get (:objects result) (thi/id label))) + +(defn- repaired? + [result label] + (contains? (:ids result) (thi/id label))) + +(def ^:private flex-defaults + {:layout :flex + :layout-padding-type :simple + :layout-padding {:p1 0 :p2 0 :p3 0 :p4 0} + :layout-gap-type :multiple + :layout-gap {:row-gap 0 :column-gap 0} + :layout-align-items :start + :layout-align-content :stretch + :layout-justify-items :start + :layout-justify-content :start + :layout-wrap-type :nowrap + :width 100 + :height 50}) + +(defn- row-frame + [file label & {:as params}] + (tho/add-frame file label (merge flex-defaults {:layout-flex-dir :row} params))) + +(defn- column-frame + [file label & {:as params}] + (tho/add-frame file label (merge flex-defaults {:layout-flex-dir :column} params))) + +(defn- add-text + [file label parent-label & {:as params}] + (tho/add-text file label "Input" + :text-params (merge {:parent-label parent-label :grow-type :fixed :width 50 :height 16} params))) + +(defn- issue-file + "The data of issue 13353: a copy row (fix 67, padding 8, gap 4, 16 px icon) + with a fill board (padding 2) that holds a fill text, both stored as :fix at + the minimum and touched; the main has :fill." + [] + (let [touched #{:geometry-group :layout-item-h-sizing}] + (-> (thf/sample-file :file1) + (row-frame :main-row :width 67 :layout-padding {:p1 8 :p2 8 :p3 8 :p4 8} + :layout-gap {:row-gap 0 :column-gap 4} + :layout-item-h-sizing :auto) + (column-frame :main-board :parent-label :main-row :layout-item-h-sizing :fill + :layout-padding {:p1 0 :p2 2 :p3 0 :p4 2}) + (add-text :main-text :main-board :layout-item-h-sizing :fill) + (tho/add-rect :main-rect :parent-label :main-board :width 40 :height 20) + (column-frame :main-inner :parent-label :main-board :width 60 :height 30) + (tho/add-rect :main-icon :parent-label :main-row :width 16 :height 16) + (thc/make-component :component1 :main-row) + (row-frame :container :width 400) + (thc/instantiate-component :component1 :copy-row + :parent-label :container + :children-labels [:c1 :c2 :c3 :c4 :c5]) + (copy-of :main-board :copy-board) + (copy-of :main-text :copy-text) + (copy-of :main-rect :copy-rect) + (copy-of :main-inner :copy-inner) + (set-sizing :copy-row :layout-item-h-sizing :fix) + (raw-update :copy-row #(resized % :width 67)) + (collapse :copy-board :width :fix 0.01 :touched touched) + (collapse :copy-text :width :fix 3.99 :touched (into touched [:content-group :text-content-attribute :text-content-text]))))) + +;; ---- Copies: restore the main sizing + +(t/deftest restores-the-frozen-overrides-of-a-copy + (let [result (repair (issue-file)) + board (shape result :copy-board) + text (shape result :copy-text)] + (t/is (= :fill (:layout-item-h-sizing board))) + (t/is (= #{:geometry-group} (:touched board))) + (t/is (= :fill (:layout-item-h-sizing text))) + (t/is (= #{:geometry-group :content-group :text-content-attribute :text-content-text} (:touched text))))) + +(t/deftest keeps-an-intended-fix-override-inside-a-restored-board + (let [result (-> (issue-file) + (collapse :copy-text :width :fix 120 :touched #{:layout-item-h-sizing}) + (repair))] + (t/is (repaired? result :copy-board)) + (t/is (not (repaired? result :copy-text))) + (t/is (= :fix (:layout-item-h-sizing (shape result :copy-text)))))) + +(t/deftest restores-a-hug-axis-of-a-copy + (let [result (-> (issue-file) + (set-sizing :main-board :layout-item-h-sizing :auto) + (set-sizing :copy-board :layout-item-h-sizing :fix) + (collapse :copy-board :width :fix 0.01 :touched #{:layout-item-h-sizing}) + (repair))] + (t/is (= :auto (:layout-item-h-sizing (shape result :copy-board)))) + (t/is (nil? (:touched (shape result :copy-board)))))) + +(t/deftest keeps-an-intended-fix-override-of-a-copy + (let [file (-> (issue-file) + (collapse :copy-board :width :fix 40 :touched #{:geometry-group :layout-item-h-sizing}) + (collapse :copy-text :width :fix 30 :touched #{:layout-item-h-sizing})) + result (repair file)] + (t/is (empty? (:ids result))) + (t/is (identical? (:objects (thf/current-page file)) (:objects result))))) + +(t/deftest does-not-restore-a-copy-under-a-hug-parent + (let [file (-> (issue-file) + (set-sizing :copy-row :layout-item-h-sizing :auto)) + result (repair file)] + (t/is (empty? (:ids result))) + (t/is (= :fix (:layout-item-h-sizing (shape result :copy-board)))) + (t/is (identical? (:objects (thf/current-page file)) (:objects result))))) + +(t/deftest does-not-restore-a-copy-whose-fill-siblings-would-collapse-it + (let [result (-> (thf/sample-file :file1) + (column-frame :main-col) + (add-text :main-a :main-col :layout-item-h-sizing :fill) + (tho/add-rect :main-b :parent-label :main-col :width 40 :height 20 + :layout-item-h-sizing :fill) + (thc/make-component :component1 :main-col) + (thc/instantiate-component :component1 :copy-col :children-labels [:c1 :c2]) + (copy-of :main-a :copy-a) + (set-sizing :copy-col :layout-item-h-sizing :auto) + (collapse :copy-a :width :fix 0.01 :touched #{:layout-item-h-sizing}) + (repair))] + (t/is (empty? (:ids result))) + (t/is (= :fix (:layout-item-h-sizing (shape result :copy-a)))))) + +(t/deftest keeps-a-healthy-frozen-shape-inside-a-restored-board + (doseq [[label size] [[:copy-rect 40] [:copy-inner 200]]] + (let [result (-> (issue-file) + (collapse label :width :fix size :touched #{:layout-item-h-sizing}) + (repair))] + (t/is (repaired? result :copy-board)) + (t/is (not (repaired? result label)))))) + +(t/deftest keeps-a-frozen-text-with-a-healthy-layout + (let [data {:x 0 :y 0 :width 100 :height 16 :fills [] :text "Hello there world"} + result (-> (issue-file) + (collapse :copy-text :width :fix 120 :touched #{:layout-item-h-sizing}) + (raw-update :copy-text #(assoc % :position-data [data])) + (repair))] + (t/is (not (repaired? result :copy-text))))) + +(t/deftest restores-a-thin-rect-copy + (let [result (-> (issue-file) + (set-sizing :main-icon :layout-item-h-sizing :fill) + (copy-of :main-icon :copy-icon) + (collapse :copy-icon :width :fix 0.01 :touched #{:layout-item-h-sizing}) + (repair))] + (t/is (= :fill (:layout-item-h-sizing (shape result :copy-icon)))))) + +(t/deftest leaves-a-copy-whose-main-is-in-a-missing-library + (let [library (-> (thf/sample-file :lib :page-label :lib-page) + (row-frame :main-row :layout-padding {:p1 8 :p2 8 :p3 8 :p4 8}) + (column-frame :main-board :parent-label :main-row :layout-item-h-sizing :fill) + (thc/make-component :component1 :main-row)) + file (-> (thf/sample-file :file1) + (thc/instantiate-component :component1 :copy-row :library library + :children-labels [:c1]) + (copy-of :main-board :copy-board) + (set-sizing :copy-row :layout-item-h-sizing :fix) + (collapse :copy-board :width :fix 0.01 :touched #{:layout-item-h-sizing}))] + (t/is (empty? (:ids (repair file {})))) + (t/is (= #{(thi/id :copy-board)} (:ids (repair file {(:id library) library})))))) + +;; ---- Nothing else is touched + +(t/deftest keeps-a-thin-rect-made-on-purpose + (let [result (-> (thf/sample-file :file1) + (column-frame :col) + (tho/add-rect :divider :parent-label :col :width 300 :height 50 + :strokes [{:stroke-color "#000000" :stroke-opacity 1 :stroke-width 1 + :stroke-style :solid :stroke-alignment :inner}]) + (collapse :divider :height :fix 0.01) + (repair))] + (t/is (empty? (:ids result))))) + +(t/deftest keeps-a-thin-board-without-layout + (let [result (-> (thf/sample-file :file1) + (row-frame :row) + (tho/add-frame :spacer :parent-label :row :width 20 :height 20) + (collapse :spacer :width :fix 0.01) + (repair))] + (t/is (empty? (:ids result))))) + +(t/deftest ignores-shapes-that-are-not-collapsed-layout-children + (let [result (-> (thf/sample-file :file1) + (row-frame :narrow :width 10 :layout-item-h-sizing :fix) + (tho/add-rect :fill-child :parent-label :narrow :width 5 :height 5 + :layout-item-h-sizing :fill) + (tho/add-frame :plain :width 100 :height 100) + (tho/add-rect :loose :parent-label :plain :width 0.01 :height 20) + (repair))] + (t/is (empty? (:ids result))))) + +(t/deftest ignores-a-healthy-page + (let [file (-> (issue-file) + (collapse :copy-board :width :fill 31) + (collapse :copy-text :width :fill 27)) + result (repair file)] + (t/is (empty? (:ids result))) + (t/is (identical? (:objects (thf/current-page file)) (:objects result))))) + +;; ---- Non-copy texts and boards + +(defn- hug-row-with-fill-board + [text-params] + (-> (thf/sample-file :file1) + (row-frame :row :layout-item-h-sizing :auto) + (column-frame :inner :parent-label :row :layout-item-h-sizing :fill) + (add-text :text :inner text-params) + (collapse :inner :width :fill 0.01) + (collapse :text :width :fix 0.01))) + +(t/deftest fixes-a-text-at-the-width-of-its-stored-layout + (let [data {:x 0 :y 0 :width 14 :height 16 :fills [] :text "Input"} + result (repair (hug-row-with-fill-board {:position-data [data]})) + text (shape result :text)] + (t/is (= :fix (:layout-item-h-sizing text))) + (t/is (= 15.0 (double (:width text)))) + (t/is (= :auto (:layout-item-h-sizing (shape result :inner)))) + (t/is (= 1 (count (:position-data text)))))) + +(t/deftest lays-out-a-collapsed-grid-board-from-stored-text-layout + (let [data {:x 0 :y 0 :width 14 :height 16 :fills [] :text "Input"} + file (-> (thf/sample-file :file1) + (row-frame :row) + (tho/add-frame :grid + :parent-label :row + :width 60 :height 30 + :layout :grid + :layout-grid-dir :row + :layout-grid-columns [{:type :flex :value 1}] + :layout-grid-rows [{:type :flex :value 1}] + :layout-grid-cells {} + :layout-padding-type :simple + :layout-padding {:p1 0 :p2 0 :p3 0 :p4 0} + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (add-text :text :grid :position-data [data]) + (raw-update :grid + (fn [shape] + (let [cell-id (thi/new-id! :grid-cell)] + (assoc shape :layout-grid-cells + {cell-id {:id cell-id + :row 1 :column 1 + :row-span 1 :column-span 1 + :position :auto + :align-self :auto :justify-self :auto + :shapes [(thi/id :text)]}})))) + (collapse :grid :width :fix 0.01) + (collapse :text :width :fix 0.01)) + result (repair file) + board (shape result :grid) + text (shape result :text)] + (t/is (repaired? result :grid)) + (t/is (= [:auto 15.0] [(:layout-item-h-sizing board) (double (:width board))])) + (t/is (= [:fix 15.0] [(:layout-item-h-sizing text) (double (:width text))])))) + +(t/deftest keeps-the-text-and-the-board-when-the-stored-layout-is-collapsed + (let [chars (mapv (fn [i] {:x 0 :y (* i 16) :width 8 :height 16 :fills [] :text "I"}) (range 5)) + result (repair (hug-row-with-fill-board {:position-data chars}))] + (t/is (empty? (:ids result))))) + +(t/deftest keeps-the-text-and-the-board-without-a-stored-layout + (let [result (repair (hug-row-with-fill-board {:position-data nil}))] + (t/is (empty? (:ids result))) + (t/is (= :fill (:layout-item-h-sizing (shape result :inner)))))) + +(t/deftest keeps-a-board-with-another-collapsed-shape-inside + (let [data {:x 0 :y 0 :width 14 :height 16 :fills [] :text "Input"} + result (-> (hug-row-with-fill-board {:position-data [data]}) + (tho/add-rect :rect :parent-label :inner :width 20 :height 16 + :layout-item-h-sizing :fill) + (collapse :rect :width :fill 0.01) + (repair))] + (t/is (repaired? result :text)) + (t/is (not (repaired? result :rect))) + (t/is (not (repaired? result :inner))) + (t/is (= :fill (:layout-item-h-sizing (shape result :inner)))))) + +(t/deftest keeps-a-collapsed-board-when-a-descendant-is-unrepairable + (let [file (-> (thf/sample-file :file1) + (column-frame :main-board :layout-item-h-sizing :auto) + (tho/add-rect :main-rect :parent-label :main-board :width 40 :height 20 + :layout-item-h-sizing :fix) + (thc/make-component :component1 :main-board) + (row-frame :container :width 400) + (column-frame :outer :parent-label :container :layout-item-h-sizing :fix) + (thc/instantiate-component :component1 :copy-board :parent-label :outer + :children-labels [:copy-rect]) + (collapse :copy-board :width :fix 0.01 :touched #{:layout-item-h-sizing}) + (collapse :copy-rect :width :fix 0.01 :touched #{:layout-item-h-sizing}) + (collapse :outer :width :fix 0.01)) + result (repair file)] + (t/is (repaired? result :copy-board)) + (t/is (not (repaired? result :copy-rect))) + (t/is (not (repaired? result :outer))) + (t/is (= :fix (:layout-item-h-sizing (shape result :outer)))) + (t/is (= :fix (:layout-item-h-sizing (shape result :copy-rect)))))) + +(t/deftest repairs-twice-nothing + (let [data {:x 0 :y 0 :width 14 :height 16 :fills [] :text "Input"} + file (hug-row-with-fill-board {:position-data [data]}) + result (repair file) + file' (update file :data + (fn [fdata] + (ctpl/update-page fdata (:id (thf/current-page file)) + #(assoc % :objects (:objects result))))) + result' (repair file')] + (t/is (seq (:ids result))) + (t/is (empty? (:ids result'))) + (t/is (identical? (:objects result) (:objects result'))))) + +;; ---- Layout of the repaired boards + +(t/deftest lays-out-the-restored-boards-of-the-issue + (let [result (repair (issue-file)) + board (shape result :copy-board) + text (shape result :copy-text)] + (t/is (= 31.0 (double (:width board)))) + (t/is (= 27.0 (double (:width text)))) + (t/is (pos? (:a (:transform board)))) + (t/is (pos? (:a (:transform text)))) + (t/is (not (:flip-x board))) + (t/is (not (:flip-x text))))) + +(t/deftest lays-out-a-board-that-starts-to-hug + (let [data {:x 0 :y 0 :width 14 :height 16 :fills [] :text "Input"} + result (repair (hug-row-with-fill-board {:position-data [data]}))] + (t/is (= 15.0 (double (:width (shape result :inner))))) + (t/is (= 15.0 (double (:width (shape result :row))))))) + +(t/deftest lays-out-the-hug-boards-above-a-repaired-board + (let [data {:x 0 :y 0 :width 14 :height 16 :fills [] :text "Input"} + file (-> (thf/sample-file :file1) + (column-frame :outer :layout-item-h-sizing :auto :layout-item-v-sizing :auto) + (row-frame :row :parent-label :outer :layout-item-h-sizing :auto) + (column-frame :inner :parent-label :row :layout-item-h-sizing :fill) + (add-text :text :inner :position-data [data]) + (collapse :inner :width :fill 0.01) + (collapse :text :width :fix 0.01)) + result (repair file)] + (t/is (= [:auto :fix] [(:layout-item-h-sizing (shape result :inner)) + (:layout-item-h-sizing (shape result :text))])) + (t/is (= [15.0 15.0 15.0] (mapv #(double (:width (shape result %))) [:inner :row :outer]))))) + +(t/deftest lays-out-both-axes-of-a-copy + (let [result (-> (issue-file) + (set-sizing :main-board :layout-item-v-sizing :fill) + (set-sizing :copy-board :layout-item-v-sizing :fix) + (collapse :copy-board :height :fix 0.01 + :touched #{:geometry-group :layout-item-h-sizing :layout-item-v-sizing}) + (repair)) + board (shape result :copy-board)] + (t/is (= [:fill :fill] [(:layout-item-h-sizing board) (:layout-item-v-sizing board)])) + (t/is (= #{:geometry-group} (:touched board))) + (t/is (= [31.0 34.0] (mapv #(mth/precision % 2) [(:width board) (:height board)]))))) + +(t/deftest leaves-the-shapes-outside-the-repaired-boards-as-they-are + (let [file (-> (issue-file) + (tho/add-rect :bystander :width 30 :height 30)) + result (repair file)] + (t/is (identical? (get (:objects (thf/current-page file)) (thi/id :bystander)) + (shape result :bystander))) + (t/is (identical? (get (:objects (thf/current-page file)) (thi/id :container)) + (shape result :container))))) + +(t/deftest moves-the-stored-layout-with-the-text + (let [data {:x 0 :y 0 :width 14 :height 16 :fills [] :text "Input"} + result (repair (hug-row-with-fill-board {:position-data [data]})) + text (shape result :text) + line (first (:position-data text))] + (t/is (= 1 (count (:position-data text)))) + (t/is (= 14.0 (double (:width line)))) + (t/is (= (double (:x text)) (double (:x line)))))) + +(t/deftest keeps-a-valid-size-for-a-restored-copy-in-a-collapsed-board + (let [result (-> (thf/sample-file :file1) + (row-frame :main-row :layout-item-h-sizing :fill :width 60) + (thc/make-component :component1 :main-row) + (row-frame :box) + (thc/instantiate-component :component1 :copy-row :parent-label :box) + (set-sizing :copy-row :layout-item-h-sizing :fix) + (collapse :copy-row :width :fix 0.01 :touched #{:layout-item-h-sizing}) + (collapse :box :width :fix 0.01) + (repair)) + board (shape result :copy-row)] + (t/is (= :fill (:layout-item-h-sizing board))) + (t/is (>= (:width board) 0)) + (t/is (pos? (:a (:transform board)))))) diff --git a/common/test/common_tests/logic/move_shapes_test.cljc b/common/test/common_tests/logic/move_shapes_test.cljc index 98bf21f6f7..a4f287e6c5 100644 --- a/common/test/common_tests/logic/move_shapes_test.cljc +++ b/common/test/common_tests/logic/move_shapes_test.cljc @@ -216,4 +216,134 @@ (t/is (nil? (:layout-item-max-w circle'))) (t/is (nil? (:layout-item-min-w circle'))) (t/is (nil? (:layout-item-absolute circle'))) - (t/is (nil? (:layout-item-z-index circle'))))) \ No newline at end of file + (t/is (nil? (:layout-item-z-index circle'))))) +(defn- add-row-board + [file label & {:as attrs}] + (tho/add-frame file label + (merge {:layout :flex + :layout-flex-dir :row + :layout-gap-type :multiple + :layout-gap {:row-gap 0 :column-gap 0} + :layout-align-items :start + :layout-justify-content :start + :layout-align-content :stretch + :layout-wrap-type :nowrap + :layout-padding-type :simple + :layout-padding {:p1 0 :p2 0 :p3 0 :p4 0}} + attrs))) + +(defn- relocate + [file parent-id to-index ids] + (let [page (thf/current-page file) + changes (cls/generate-relocate (-> (pcb/empty-changes nil) + (pcb/with-page-id (:id page)) + (pcb/with-objects (:objects page))) + parent-id + to-index + ids)] + (thf/apply-changes file changes))) + +(t/deftest test-relocate-fill-shape-into-hug-board-fixes-the-shape + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (add-row-board :hug + :width 32 :height 40 + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (ths/add-sample-shape :inside :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (ths/add-sample-shape :arriving + :width 32 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + + ;; ==== Action + file' (relocate file (thi/id :hug) 0 #{(thi/id :arriving)}) + + ;; ==== Check + arriving' (ths/get-shape file' :arriving) + hug' (ths/get-shape file' :hug)] + + (t/is (= (thi/id :hug) (:parent-id arriving'))) + (t/is (= :fix (:layout-item-h-sizing arriving'))) + (t/is (= 32 (:width arriving'))) + (t/is (= :auto (:layout-item-h-sizing hug'))))) + +(t/deftest test-relocate-fill-shape-into-fixed-board-keeps-the-fill + (let [file (-> (thf/sample-file :file1) + (add-row-board :fixed + :width 100 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (ths/add-sample-shape :arriving + :width 32 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + file' (relocate file (thi/id :fixed) 0 #{(thi/id :arriving)})] + + (t/is (= :fill (:layout-item-h-sizing (ths/get-shape file' :arriving)))) + (t/is (= :fix (:layout-item-h-sizing (ths/get-shape file' :fixed)))))) + +(t/deftest test-relocate-last-sizing-child-out-of-hug-board-fixes-the-board + (let [file (-> (thf/sample-file :file1) + (add-row-board :hug + :width 64 :height 40 + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (ths/add-sample-shape :leaving :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (ths/add-sample-shape :staying :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + file' (relocate file uuid/zero 0 #{(thi/id :leaving)})] + + (t/is (= :fix (:layout-item-h-sizing (ths/get-shape file' :hug)))) + (t/is (= :fill (:layout-item-h-sizing (ths/get-shape file' :staying)))))) + +(t/deftest test-relocate-last-sizing-child-out-of-hug-grid-fixes-the-board + (let [file (-> (thf/sample-file :file1) + (tho/add-frame :hug + :x 0 :y 0 :width 64 :height 40 + :layout :grid + :layout-grid-dir :row + :layout-grid-columns [{:type :flex :value 1} + {:type :flex :value 1}] + :layout-grid-rows [{:type :flex :value 1}] + :layout-grid-cells {} + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (ths/add-sample-shape :leaving :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (ths/add-sample-shape :staying :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + file' (relocate file uuid/zero 0 #{(thi/id :leaving)})] + (t/is (= :fix (:layout-item-h-sizing (ths/get-shape file' :hug)))) + (t/is (= :fill (:layout-item-h-sizing (ths/get-shape file' :staying)))))) + +(t/deftest test-relocate-inside-the-same-hug-board-keeps-the-sizing + (let [file (-> (thf/sample-file :file1) + (add-row-board :hug + :width 64 :height 40 + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (ths/add-sample-shape :first :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (ths/add-sample-shape :second :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix)) + file' (relocate file (thi/id :hug) 2 #{(thi/id :first)})] + + (t/is (= :auto (:layout-item-h-sizing (ths/get-shape file' :hug)))) + (t/is (= :fix (:layout-item-h-sizing (ths/get-shape file' :first)))))) diff --git a/common/test/common_tests/runner.cljc b/common/test/common_tests/runner.cljc index 41056d8cb8..6eede262fd 100644 --- a/common/test/common_tests/runner.cljc +++ b/common/test/common_tests/runner.cljc @@ -29,6 +29,7 @@ [common-tests.files-migrations-0037-test] [common-tests.files-migrations-0038-test] [common-tests.files-migrations-0039-test] + [common-tests.files-migrations-0040-test] [common-tests.files-migrations-test] [common-tests.files.changes-builder-test] [common-tests.files.comp-processors-test] @@ -43,6 +44,7 @@ [common-tests.geom-align-test] [common-tests.geom-bounds-layout-nil-test] [common-tests.geom-bounds-map-test] + [common-tests.geom-flex-layout-reflow-test] [common-tests.geom-flex-layout-test] [common-tests.geom-grid-layout-test] [common-tests.geom-grid-test] @@ -68,6 +70,7 @@ [common-tests.helpers-test] [common-tests.logging-test] [common-tests.logic.chained-propagation-test] + [common-tests.logic.collapsed-layout-test] [common-tests.logic.comp-creation-test] [common-tests.logic.comp-detach-with-nested-test] [common-tests.logic.comp-flex-interactions-test] @@ -153,6 +156,7 @@ 'common-tests.files-migrations-0037-test 'common-tests.files-migrations-0038-test 'common-tests.files-migrations-0039-test + 'common-tests.files-migrations-0040-test 'common-tests.files-migrations-test 'common-tests.files.changes-builder-test 'common-tests.files.comp-processors-test @@ -168,6 +172,7 @@ 'common-tests.geom-bounds-layout-nil-test 'common-tests.geom-bounds-map-test 'common-tests.geom-flex-layout-test + 'common-tests.geom-flex-layout-reflow-test 'common-tests.geom-grid-layout-test 'common-tests.geom-grid-test 'common-tests.geom-image-bounds-resize-test @@ -192,6 +197,7 @@ 'common-tests.helpers-test 'common-tests.logging-test 'common-tests.logic.chained-propagation-test + 'common-tests.logic.collapsed-layout-test 'common-tests.logic.comp-creation-test 'common-tests.logic.comp-detach-with-nested-test 'common-tests.logic.comp-flex-interactions-test diff --git a/common/test/common_tests/types/shape_layout_test.cljc b/common/test/common_tests/types/shape_layout_test.cljc index 3aa0b08b95..3de6e7a8e0 100644 --- a/common/test/common_tests/types/shape_layout_test.cljc +++ b/common/test/common_tests/types/shape_layout_test.cljc @@ -834,6 +834,120 @@ (t/is (= :fix (:layout-item-h-sizing result))) (t/is (= :auto (:layout-item-v-sizing result)))))) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; fix-child-sizing +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn- sizing-objects + "Builds an objects map with a parent and children given as + `[h-sizing v-sizing]` pairs or attr maps. Returns `[objects parent children]`." + [parent-attrs children-attrs] + (let [parent-id (uuid/next) + children (mapv (fn [attrs] + (let [attrs (if (vector? attrs) + {:layout-item-h-sizing (first attrs) + :layout-item-v-sizing (second attrs)} + attrs)] + (assoc attrs :id (uuid/next) :parent-id parent-id))) + children-attrs) + parent (assoc parent-attrs + :id parent-id + :shapes (mapv :id children))] + [(into {parent-id parent} (map (juxt :id identity)) children) + parent + children])) + +(defn- fixed-sizing + "Returns `[h v]` of the first child after `fix-child-sizing`." + [parent-attrs children-attrs & [changes]] + (let [[objects _ children] (sizing-objects parent-attrs children-attrs) + result (layout/fix-child-sizing objects changes (first children))] + [(:layout-item-h-sizing result) (:layout-item-v-sizing result)])) + +(t/deftest fix-child-sizing-test + (t/testing "row, hug width: a fill-width child becomes fix" + (t/is (= [:fix :fix] + (fixed-sizing {:layout-flex-dir :row :layout-item-h-sizing :auto} + [[:fill :fix]]))) + (t/is (= [:fix :fix] + (fixed-sizing {:layout-flex-dir :row :layout-item-h-sizing :auto} + [[:fill :fix] [:fix :fix]])))) + + (t/testing "row, hug height: fix only when all children are fill-height" + (t/is (= [:fix :fix] + (fixed-sizing {:layout-flex-dir :row :layout-item-v-sizing :auto} + [[:fix :fill] [:fix :fill]]))) + (t/is (= [:fix :fill] + (fixed-sizing {:layout-flex-dir :row :layout-item-v-sizing :auto} + [[:fix :fill] [:fix :fix]])))) + + (t/testing "column, hug width: fix only when all children are fill-width" + (t/is (= [:fix :fix] + (fixed-sizing {:layout-flex-dir :column :layout-item-h-sizing :auto} + [[:fill :fix] [:fill :fix]]))) + (t/is (= [:fill :fix] + (fixed-sizing {:layout-flex-dir :column :layout-item-h-sizing :auto} + [[:fill :fix] [:fix :fix]])))) + + (t/testing "column, hug height: a fill-height child becomes fix" + (t/is (= [:fix :fix] + (fixed-sizing {:layout-flex-dir :column :layout-item-v-sizing :auto} + [[:fix :fill] [:fix :fix]])))) + + (t/testing "a fixed parent leaves the child alone" + (t/is (= [:fill :fill] + (fixed-sizing {:layout-flex-dir :row + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix} + [[:fill :fill]])))) + + (t/testing "absolute and hidden children are ignored by the all-children check" + (t/is (= [:fix :fix] + (fixed-sizing {:layout-flex-dir :row :layout-item-v-sizing :auto} + [[:fix :fill] + {:layout-item-absolute true :layout-item-v-sizing :fix} + {:hidden true :layout-item-v-sizing :fix}])))) + + (t/testing "parent-changes can supply the hug" + (t/is (= [:fill :fill] + (fixed-sizing {:layout-flex-dir :row + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix} + [[:fill :fill] [:fix :fix]]))) + (t/is (= [:fix :fill] + (fixed-sizing {:layout-flex-dir :row + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix} + [[:fill :fill] [:fix :fix]] + {:layout-item-h-sizing :auto})))) + + (t/testing "grid, hug width: a fill-width child becomes fix" + (t/is (= [:fix :fill] + (fixed-sizing {:type :frame :layout :grid :layout-item-h-sizing :auto} + [[:fill :fill] [:fix :fix]])))) + + (t/testing "grid, hug height: a fill-height child becomes fix" + (t/is (= [:fill :fix] + (fixed-sizing {:type :frame :layout :grid :layout-item-v-sizing :auto} + [[:fill :fill] [:fix :fix]])))) + + (t/testing "grid, fixed: the child is left alone" + (t/is (= [:fill :fill] + (fixed-sizing {:type :frame + :layout :grid + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix} + [[:fill :fill]])))) + + (t/testing "grid, parent-changes can supply the hug" + (t/is (= [:fix :fill] + (fixed-sizing {:type :frame + :layout :grid + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix} + [[:fill :fill]] + {:layout-item-h-sizing :auto}))))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Grid track defaults ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -1450,7 +1564,48 @@ :layout-item-v-sizing :fix) child-1 (make-shape :layout-item-v-sizing :fill) objects {(:id frame) frame (:id child-1) child-1}] - (t/is (not (layout/change-v-sizing? (:id frame) objects [(:id child-1)])))))) + (t/is (not (layout/change-v-sizing? (:id frame) objects [(:id child-1)]))))) + + (t/testing "change-v-sizing? ignores absolute and hidden children" + (let [frame (make-flex-frame :layout-flex-dir :row + :layout-item-v-sizing :auto) + child-1 (make-shape :layout-item-v-sizing :fill) + child-2 (make-shape :layout-item-v-sizing :fix + :layout-item-absolute true) + child-3 (make-shape :layout-item-v-sizing :fix + :hidden true) + objects {(:id frame) frame + (:id child-1) child-1 + (:id child-2) child-2 + (:id child-3) child-3}] + (t/is (layout/change-v-sizing? (:id frame) objects + [(:id child-1) (:id child-2) (:id child-3)]))))) + +(t/deftest grid-change-sizing-tests + (t/testing "a hug grid stops hugging when any visible child fills an axis" + (let [frame (make-grid-frame :layout-item-h-sizing :auto + :layout-item-v-sizing :auto) + filled (make-shape :layout-item-h-sizing :fill + :layout-item-v-sizing :fill) + fixed (make-shape :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + objects {(:id frame) frame + (:id filled) filled + (:id fixed) fixed} + children [(:id filled) (:id fixed)]] + (t/is (layout/change-h-sizing? (:id frame) objects children)) + (t/is (layout/change-v-sizing? (:id frame) objects children)))) + + (t/testing "hidden fill children do not stop a hug grid from hugging" + (let [frame (make-grid-frame :layout-item-h-sizing :auto + :layout-item-v-sizing :auto) + hidden (make-shape :layout-item-h-sizing :fill + :layout-item-v-sizing :fill + :hidden true) + objects {(:id frame) frame + (:id hidden) hidden}] + (t/is (not (layout/change-h-sizing? (:id frame) objects [(:id hidden)]))) + (t/is (not (layout/change-v-sizing? (:id frame) objects [(:id hidden)])))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; get-cell-by-position diff --git a/frontend/src/app/main/data/workspace/clipboard.cljs b/frontend/src/app/main/data/workspace/clipboard.cljs index 787b6b1bc2..d19c242d19 100644 --- a/frontend/src/app/main/data/workspace/clipboard.cljs +++ b/frontend/src/app/main/data/workspace/clipboard.cljs @@ -1013,6 +1013,11 @@ (pcb/update-shapes [parent-id] #(ctl/add-children-to-cell % selected all-objects drop-cell))) + ;; A hug parent cannot give room to a pasted fill shape + changes (pcb/update-shapes changes selected + #(ctl/fix-child-sizing %2 {} %1) + {:with-objects? true}) + add-component-to-variant? (and ;; Any of the shapes is a head (some ctk/instance-head? orig-shapes) diff --git a/frontend/src/app/main/data/workspace/modifiers.cljs b/frontend/src/app/main/data/workspace/modifiers.cljs index 82e3d9cc72..46e0931f05 100644 --- a/frontend/src/app/main/data/workspace/modifiers.cljs +++ b/frontend/src/app/main/data/workspace/modifiers.cljs @@ -429,7 +429,17 @@ target-flex-layout? (ctl/flex-layout? target-frame) target-grid-layout? (ctl/grid-layout? target-frame) - children-ids (concat (:shapes target-frame) selected) + ;; Shapes that change frame, as seen from the target frame + arriving-ids (->> selected + (remove #(= target-frame-id (get-in objects [% :frame-id]))) + (filterv #(contains? objects %))) + + arriving-objects + (-> objects + (update-in [target-frame-id :shapes] into arriving-ids) + (into (map (fn [id] + [id (assoc (get objects id) :parent-id target-frame-id)])) + arriving-ids)) set-parent-ids (fn [modif-tree shapes target-frame-id] @@ -454,10 +464,8 @@ (filterv #(contains? child-set %))) children-ids (->> (dm/get-in objects [original-frame :shapes]) (remove (set selected))) - h-sizing? (and (ctl/flex-layout? objects original-frame) - (ctl/change-h-sizing? original-frame objects children-ids)) - v-sizing? (and (ctl/flex-layout? objects original-frame) - (ctl/change-v-sizing? original-frame objects children-ids))] + h-sizing? (ctl/change-h-sizing? original-frame objects children-ids) + v-sizing? (ctl/change-v-sizing? original-frame objects children-ids)] (cond-> modif-tree (not= original-frame target-frame-id) (-> (modifier-remove-from-parent objects shapes) @@ -473,20 +481,24 @@ ;; Add the object to the cell target-grid-layout? - (update-in [target-frame-id :modifiers] add-grid-children-modifiers target-frame-id shapes objects cell-data))))] + (update-in [target-frame-id :modifiers] add-grid-children-modifiers target-frame-id shapes objects cell-data)))) - (as-> modif-tree $ - (reduce update-frame-modifiers $ origin-frame-ids) - (cond-> $ - ;; Set fix position to target frame (horizontal) - (and (ctl/flex-layout? objects target-frame-id) - (ctl/change-h-sizing? target-frame-id objects children-ids)) - (update-in [target-frame-id :modifiers] ctm/change-property :layout-item-h-sizing :fix) + fix-arriving-sizing + (fn [modif-tree id] + (let [shape (get arriving-objects id) + fixed (ctl/fix-child-sizing arriving-objects {} shape)] + (reduce (fn [modif-tree attr] + (cond-> modif-tree + (not= (get shape attr) (get fixed attr)) + (update-in [id :modifiers] ctm/change-property attr (get fixed attr)))) + modif-tree + [:layout-item-h-sizing :layout-item-v-sizing])))] - ;; Set fix position to target frame (vertical) - (and (ctl/flex-layout? objects target-frame-id) - (ctl/change-v-sizing? target-frame-id objects children-ids)) - (update-in [target-frame-id :modifiers] ctm/change-property :layout-item-v-sizing :fix))))) + (cond-> (reduce update-frame-modifiers modif-tree origin-frame-ids) + ;; A hug target cannot give room to the fill shapes that arrive, so + ;; they are fixed and the target keeps hugging + (ctl/any-layout? target-frame) + (as-> $ (reduce fix-arriving-sizing $ arriving-ids))))) (defn modif->js [modif-tree objects] diff --git a/frontend/src/app/main/data/workspace/shape_layout.cljs b/frontend/src/app/main/data/workspace/shape_layout.cljs index dd62a7d3bc..388b1c0a7b 100644 --- a/frontend/src/app/main/data/workspace/shape_layout.cljs +++ b/frontend/src/app/main/data/workspace/shape_layout.cljs @@ -587,50 +587,14 @@ (ptk/data-event :layout/update {:ids ids}) (dwu/commit-undo-transaction undo-id)))))) -(defn fix-child-sizing - [objects parent-changes shape] - - (let [parent (-> (cfh/get-parent objects (:id shape)) - (d/deep-merge parent-changes)) - - auto-width? (ctl/auto-width? parent) - auto-height? (ctl/auto-height? parent) - col? (ctl/col? parent) - row? (ctl/row? parent) - - all-children (->> parent - :shapes - (map (d/getf objects)) - (remove ctl/position-absolute?))] - - (cond-> shape - ;; If the parent is hug width and the direction column - ;; change to fixed when ALL children are fill - (and col? auto-width? (every? ctl/fill-width? all-children)) - (assoc :layout-item-h-sizing :fix) - - ;; If the parent is hug height and the direction is column - ;; change to fixed when ANY children is fill - (and col? auto-height? (ctl/fill-height? shape)) - (assoc :layout-item-v-sizing :fix) - - ;; If the parent is hug width and the direction row - ;; change to fixed when ANY children is fill - (and row? auto-width? (ctl/fill-width? shape)) - (assoc :layout-item-h-sizing :fix) - - ;; If the parent is hug height and the direction row - ;; change to fixed when ALL children are fill - (and row? auto-height? (every? ctl/fill-height? all-children)) - (assoc :layout-item-v-sizing :fix)))) - (defn fix-parent-sizing [parent objects ids-set changes] (let [auto-width? (ctl/auto-width? parent) auto-height? (ctl/auto-height? parent) - col? (ctl/col? parent) - row? (ctl/row? parent) + grid? (ctl/grid-layout? parent) + col? (and (not grid?) (ctl/col? parent)) + row? (and (not grid?) (ctl/row? parent)) all-children (->> parent :shapes @@ -638,9 +602,18 @@ (map (fn [shape] (if (contains? ids-set (:id shape)) (d/deep-merge shape changes) - shape))))] + shape))) + (remove ctl/position-absolute?))] (cond-> parent + ;; Grid layout and parent is hug if any children is fill on that axis + ;; change parent to fixed + (and grid? auto-width? (some ctl/fill-width? all-children)) + (assoc :layout-item-h-sizing :fix) + + (and grid? auto-height? (some ctl/fill-height? all-children)) + (assoc :layout-item-v-sizing :fix) + ;; Col layout and parent is hug-width if all children are fill-width ;; change parent to fixed (and col? auto-width? (every? ctl/fill-width? all-children)) @@ -669,7 +642,7 @@ (let [page-id (or (get options :page-id) (get state :current-page-id)) objects (dsh/lookup-page-objects state page-id) - children-ids (->> ids (mapcat #(cfh/get-children-ids objects %))) + children-ids (->> ids (mapcat #(get-in objects [% :shapes]))) parent-ids (->> ids (map #(cfh/get-parent-id objects %))) undo-id (js/Symbol) margin-attrs (-> (get attrs :layout-item-margin) @@ -680,7 +653,7 @@ (cond-> options (seq margin-attrs) (assoc :changed-sub-attr margin-attrs))) - (dwsh/update-shapes children-ids (partial fix-child-sizing objects attrs) options) + (dwsh/update-shapes children-ids (partial ctl/fix-child-sizing objects attrs) options) (dwsh/update-shapes parent-ids (fn [parent objects] diff --git a/frontend/src/app/main/data/workspace/shapes.cljs b/frontend/src/app/main/data/workspace/shapes.cljs index 82691dfad9..aba64c6cc0 100644 --- a/frontend/src/app/main/data/workspace/shapes.cljs +++ b/frontend/src/app/main/data/workspace/shapes.cljs @@ -17,6 +17,7 @@ [app.common.types.container :as ctn] [app.common.types.shape :as cts] [app.common.types.shape-tree :as ctst] + [app.common.types.shape.layout :as ctl] [app.main.data.changes :as dch] [app.main.data.comments :as dc] [app.main.data.event :as ev] @@ -371,6 +372,27 @@ (rx/of (dch/commit-changes changes)) (rx/empty)))))) +(defn fix-hug-parents-sizing + "Turns each flex parent in `parent-ids` to fix on the axes where its + remaining children would collapse because of the hug, so it stops hugging + at its current size. `page-id` nil means the open page." + [parent-ids page-id] + (ptk/reify ::fix-hug-parents-sizing + ptk/WatchEvent + (watch [_ state _] + (let [page-id (or page-id (:current-page-id state)) + objects (dsh/lookup-page-objects state page-id) + fix (fn [{:keys [id shapes] :as parent}] + (if (seq (remove (partial ctl/position-absolute? objects) shapes)) + (cond-> parent + (ctl/change-h-sizing? id objects shapes) + (assoc :layout-item-h-sizing :fix) + + (ctl/change-v-sizing? id objects shapes) + (assoc :layout-item-v-sizing :fix)) + parent))] + (rx/of (update-shapes (filter objects parent-ids) fix {:page-id page-id})))))) + (declare update-shape-flags) (defn delete-shapes @@ -402,6 +424,7 @@ (rx/of (dwu/start-undo-transaction undo-id) (dc/detach-comment-thread ids) (dch/commit-changes changes) + (fix-hug-parents-sizing all-parents page-id) (ptk/data-event :layout/update {:ids all-parents :undo-group (:undo-group options)}) (dwu/commit-undo-transaction undo-id))))))) @@ -536,15 +559,28 @@ ids (if (boolean? blocked) (into ids (->> ids (mapcat #(cfh/get-children-ids objects %)))) ids)] - (rx/of (update-shapes ids update-fn {:attrs #{:blocked :hidden} :undo-group undo-group}))))))) + (if (boolean? hidden) + (let [undo-id (js/Symbol) + parent-ids (into #{} (keep #(cfh/get-parent-id objects %)) ids)] + (rx/of (dwu/start-undo-transaction undo-id) + (update-shapes ids update-fn {:attrs #{:blocked :hidden} :undo-group undo-group}) + (fix-hug-parents-sizing parent-ids nil) + (dwu/commit-undo-transaction undo-id))) + (rx/of (update-shapes ids update-fn {:attrs #{:blocked :hidden} :undo-group undo-group})))))))) (defn toggle-visibility-selected [] (ptk/reify ::toggle-visibility-selected ptk/WatchEvent (watch [_ state _] - (let [selected (dsh/lookup-selected state)] - (rx/of (update-shapes selected #(update % :hidden not))))))) + (let [selected (dsh/lookup-selected state) + objects (dsh/lookup-page-objects state) + parent-ids (into #{} (keep #(cfh/get-parent-id objects %)) selected) + undo-id (js/Symbol)] + (rx/of (dwu/start-undo-transaction undo-id) + (update-shapes selected #(update % :hidden not)) + (fix-hug-parents-sizing parent-ids nil) + (dwu/commit-undo-transaction undo-id)))))) (defn toggle-lock-selected [] diff --git a/frontend/src/app/main/data/workspace/transforms.cljs b/frontend/src/app/main/data/workspace/transforms.cljs index 3e5f8ced3a..f8c7e221df 100644 --- a/frontend/src/app/main/data/workspace/transforms.cljs +++ b/frontend/src/app/main/data/workspace/transforms.cljs @@ -1626,11 +1626,22 @@ (defn fit-layout-modifiers [objects frame] - ;; Set temporaly the auto flag and calculate a reflow to resize and position + ;; Set temporaly the auto flag and calculate a reflow to resize and position. + ;; Only the fill children that the hug would collapse are fixed in the + ;; temporary objects, so they keep their current size. (let [objects (-> objects (assoc-in [(:id frame) :layout-item-h-sizing] :auto) - (assoc-in [(:id frame) :layout-item-v-sizing] :auto))] + (assoc-in [(:id frame) :layout-item-v-sizing] :auto)) + + fix-child + (if (ctl/flex-layout? frame) + (partial ctl/fix-child-sizing objects {}) + ctl/toggle-fix-if-auto) + + objects + (->> (:shapes frame) + (reduce #(update %1 %2 fix-child) objects))] (gm/set-objects-modifiers {(:id frame) {:modifiers (ctm/reflow-modifiers)}} objects))) (defn selected-fit-content diff --git a/frontend/src/app/main/data/workspace/variants.cljs b/frontend/src/app/main/data/workspace/variants.cljs index eb9b08e349..9543302c3b 100644 --- a/frontend/src/app/main/data/workspace/variants.cljs +++ b/frontend/src/app/main/data/workspace/variants.cljs @@ -461,6 +461,9 @@ (cl/remove-all-fills variant-vec {:color clr/black :opacity 1}) (when flex? (dwsl/create-layout-from-id variant-id :flex)) (dwsh/update-shapes variant-vec #(merge % style-props)) + ;; The container hugs the main, which cannot be fill inside it + (when flex? + (dwsh/update-shapes [main-instance-id] ctsl/toggle-fix-if-auto)) (cl/add-stroke variant-vec stroke-props)) (rx/of (define-variant-container variant-id main-instance-id component-id name)) diff --git a/frontend/src/app/main/ui/workspace/sidebar/options/menus/layer.cljs b/frontend/src/app/main/ui/workspace/sidebar/options/menus/layer.cljs index eb8f59542b..86a1c0058d 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/options/menus/layer.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/options/menus/layer.cljs @@ -159,14 +159,14 @@ (mf/deps ids) (fn [_] (st/emit! (dw/trigger-bounding-box-cloaking ids)) - (on-change ids :hidden true))) + (st/emit! (dw/update-shape-flags ids {:hidden true})))) handle-set-visible (mf/use-fn (mf/deps ids) (fn [_] (st/emit! (dw/trigger-bounding-box-cloaking ids)) - (on-change ids :hidden false))) + (st/emit! (dw/update-shape-flags ids {:hidden false})))) handle-set-blocked (mf/use-fn diff --git a/frontend/src/app/plugins/flex.cljs b/frontend/src/app/plugins/flex.cljs index 0b5f9243b2..7350a48790 100644 --- a/frontend/src/app/plugins/flex.cljs +++ b/frontend/src/app/plugins/flex.cljs @@ -385,7 +385,7 @@ (u/not-valid plugin-id :horizontalSizing "Cannot modify a page that is not currently active") :else - (st/emit! (dwsl/update-layout #{id} {:layout-item-h-sizing value})))))} + (st/emit! (dwsl/update-layout-child #{id} {:layout-item-h-sizing value})))))} :verticalSizing {:this true @@ -404,7 +404,7 @@ (u/not-valid plugin-id :verticalSizing "Cannot modify a page that is not currently active") :else - (st/emit! (dwsl/update-layout #{id} {:layout-item-v-sizing value})))))})) + (st/emit! (dwsl/update-layout-child #{id} {:layout-item-v-sizing value})))))})) (defn layout-child-proxy? [p] (obj/type-of? p "LayoutChildProxy")) @@ -433,7 +433,7 @@ (u/not-valid plugin-id :absolute "Cannot modify a page that is not currently active") :else - (st/emit! (dwsl/update-layout #{id} {:layout-item-absolute value}))))} + (st/emit! (dwsl/update-layout-child #{id} {:layout-item-absolute value}))))} :zIndex {:this true diff --git a/frontend/src/app/plugins/grid.cljs b/frontend/src/app/plugins/grid.cljs index 8860aa5d58..eb60d66d15 100644 --- a/frontend/src/app/plugins/grid.cljs +++ b/frontend/src/app/plugins/grid.cljs @@ -87,7 +87,7 @@ (u/not-valid plugin-id :horizontalSizing "Cannot modify a page that is not currently active") :else - (st/emit! (dwsl/update-layout #{id} {:layout-item-h-sizing value})))))} + (st/emit! (dwsl/update-layout-child #{id} {:layout-item-h-sizing value})))))} :verticalSizing {:this true @@ -106,7 +106,7 @@ (u/not-valid plugin-id :verticalSizing "Cannot modify a page that is not currently active") :else - (st/emit! (dwsl/update-layout #{id} {:layout-item-v-sizing value})))))} + (st/emit! (dwsl/update-layout-child #{id} {:layout-item-v-sizing value})))))} :alignItems {:this true diff --git a/frontend/src/app/plugins/shape.cljs b/frontend/src/app/plugins/shape.cljs index 3e771b5852..ab21507f0c 100644 --- a/frontend/src/app/plugins/shape.cljs +++ b/frontend/src/app/plugins/shape.cljs @@ -436,7 +436,7 @@ :else (let [id (obj/get self "$id")] - (st/emit! (dwsh/update-shapes [id] #(assoc % :hidden value))))))} + (st/emit! (dwsh/update-shape-flags [id] {:hidden value})))))} :visible {:this true @@ -455,7 +455,7 @@ :else (let [id (obj/get self "$id")] - (st/emit! (dwsh/update-shapes [id] #(assoc % :hidden (not value)))))))} + (st/emit! (dwsh/update-shape-flags [id] {:hidden (not value)})))))} :proportionLock {:this true @@ -2071,7 +2071,7 @@ (u/not-valid plugin-id :horizontalSizing "Cannot modify a page that is not currently active") :else - (st/emit! (dwsl/update-layout #{id} {:layout-item-h-sizing value})))))} + (st/emit! (dwsl/update-layout-child #{id} {:layout-item-h-sizing value})))))} {:name "verticalSizing" :get #(-> % u/proxy->shape :layout-item-v-sizing (d/nilv :fix) d/name) @@ -2090,7 +2090,7 @@ (u/not-valid plugin-id :verticalSizing "Cannot modify a page that is not currently active") :else - (st/emit! (dwsl/update-layout #{id} {:layout-item-v-sizing value})))))} + (st/emit! (dwsl/update-layout-child #{id} {:layout-item-v-sizing value})))))} {:name "variants" :enumerable false diff --git a/frontend/test/frontend_tests/data/workspace_fit_content_test.cljs b/frontend/test/frontend_tests/data/workspace_fit_content_test.cljs new file mode 100644 index 0000000000..8ed6c314af --- /dev/null +++ b/frontend/test/frontend_tests/data/workspace_fit_content_test.cljs @@ -0,0 +1,180 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.data.workspace-fit-content-test + "Tests that `Resize board to fit content` keeps fill children at their + current size instead of collapsing them." + (:require + [app.common.geom.shapes :as gsh] + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] + [app.common.test-helpers.shapes :as cths] + [app.main.data.workspace.transforms :as dwt] + [cljs.test :as t :include-macros true])) + +(defn- fit-content + "Return the shapes of the page `:board` after fit content, by label." + [file labels] + (let [objects (:objects (cthf/current-page file)) + board (get objects (cthi/id :board)) + modif (dwt/fit-layout-modifiers objects board) + transform (fn [label] + (let [id (cthi/id label)] + (gsh/transform-shape (get objects id) + (get-in modif [id :modifiers]))))] + (into {} (map (juxt identity transform)) labels))) + +(defn- add-row-board + [file] + (ctho/add-frame file :board + :x 0 :y 0 :width 96 :height 40 + :layout :flex + :layout-flex-dir :row + :layout-gap-type :multiple + :layout-gap {:row-gap 4 :column-gap 4} + :layout-padding-type :multiple + :layout-padding {:p1 8 :p2 8 :p3 8 :p4 8})) + +(defn- add-icon + [file] + (cths/add-sample-shape file :icon + :type :rect + :parent-label :board + :width 16 :height 16 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix)) + +(t/deftest fit-content-keeps-a-fill-child-size + (let [file (-> (cthf/sample-file :file1) + (add-row-board) + (add-icon) + (cths/add-sample-shape :fill-child + :type :rect + :parent-label :board + :width 60 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + result (fit-content file [:board :fill-child])] + (t/is (= 60 (:width (:fill-child result)))) + (t/is (= 96 (:width (:board result)))))) + +(t/deftest fit-content-keeps-a-fill-grandchild-inside-a-fill-board + (let [file (-> (cthf/sample-file :file1) + (add-row-board) + (add-icon) + (ctho/add-frame :inner + :parent-label :board + :width 60 :height 16 + :layout :flex + :layout-flex-dir :column + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix) + (cths/add-sample-shape :leaf + :type :rect + :parent-label :inner + :width 60 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + result (fit-content file [:inner :leaf])] + (t/is (= 60 (:width (:inner result)))) + (t/is (= 60 (:width (:leaf result)))))) + +(t/deftest fit-content-still-fits-fix-children + (let [file (-> (cthf/sample-file :file1) + (add-row-board) + (add-icon)) + result (fit-content file [:board])] + (t/is (= 32 (:width (:board result)))))) + +(defn- add-child + [file label sizing-h sizing-v] + (cths/add-sample-shape file label + :type :rect + :parent-label :board + :width 30 :height 24 + :layout-item-h-sizing sizing-h + :layout-item-v-sizing sizing-v)) + +(t/deftest fit-content-fits-the-cross-axis-next-to-a-fixed-sibling + (let [file (-> (cthf/sample-file :file1) + (add-row-board) + (add-icon) + (add-child :child :fix :fill)) + result (fit-content file [:board :child])] + (t/is (= 32 (:height (:board result)))) + (t/is (= 16 (:height (:child result)))))) + +(t/deftest fit-content-fits-a-fill-both-axes-child-next-to-a-fixed-sibling + (let [file (-> (cthf/sample-file :file1) + (add-row-board) + (add-icon) + (add-child :child :fill :fill)) + result (fit-content file [:board :child])] + (t/is (= 66 (:width (:board result)))) + (t/is (= 32 (:height (:board result)))) + (t/is (= 30 (:width (:child result)))) + (t/is (= 16 (:height (:child result)))))) + +(t/deftest fit-content-keeps-heights-when-all-children-are-fill-height + (let [file (-> (cthf/sample-file :file1) + (add-row-board) + (add-child :a :fix :fill) + (add-child :b :fix :fill)) + result (fit-content file [:board :a :b])] + (t/is (= 24 (:height (:a result)))) + (t/is (= 24 (:height (:b result)))) + (t/is (= 40 (:height (:board result)))))) + +(t/deftest fit-content-keeps-a-grid-fill-child-size-next-to-a-fixed-sibling + (let [file (-> (cthf/sample-file :file1) + (ctho/add-frame :board + :x 0 :y 0 :width 200 :height 40 + :layout :grid + :layout-grid-dir :row + :layout-grid-columns [{:type :auto} + {:type :auto}] + :layout-grid-rows [{:type :auto}] + :layout-grid-cells {} + :layout-padding-type :simple + :layout-padding {:p1 0 :p2 0 :p3 0 :p4 0} + :layout-gap-type :multiple + :layout-gap {:row-gap 0 :column-gap 0} + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :fill-child + :type :rect + :parent-label :board + :width 60 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix) + (cths/add-sample-shape :fixed-sibling + :type :rect + :parent-label :board + :width 16 :height 16 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/update-shape + :board + :layout-grid-cells + (let [fill-cell-id (cthi/new-id! :fill-cell) + fixed-cell-id (cthi/new-id! :fixed-cell)] + {fill-cell-id {:id fill-cell-id + :row 1 :column 1 + :row-span 1 :column-span 1 + :position :auto + :align-self :auto :justify-self :auto + :shapes [(cthi/id :fill-child)]} + fixed-cell-id {:id fixed-cell-id + :row 1 :column 2 + :row-span 1 :column-span 1 + :position :auto + :align-self :auto :justify-self :auto + :shapes [(cthi/id :fixed-sibling)]}}))) + result (fit-content file [:board :fill-child :fixed-sibling])] + (t/is (= 60 (:width (:fill-child result)))) + (t/is (= 16 (:width (:fixed-sibling result)))) + (t/is (= 76 (:width (:board result)))))) diff --git a/frontend/test/frontend_tests/data/workspace_hug_parents_test.cljs b/frontend/test/frontend_tests/data/workspace_hug_parents_test.cljs new file mode 100644 index 0000000000..b10a9f70d3 --- /dev/null +++ b/frontend/test/frontend_tests/data/workspace_hug_parents_test.cljs @@ -0,0 +1,369 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.data.workspace-hug-parents-test + "A hug board that loses the child that sized it must stop hugging, or its + fill children collapse to the minimum size." + (:require + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] + [app.common.test-helpers.shapes :as cths] + [app.main.data.workspace.selection :as dws] + [app.main.data.workspace.shape-layout :as dwsl] + [app.main.data.workspace.shapes :as dwsh] + [app.main.data.workspace.undo :as dwu] + [app.main.store :as st] + [app.plugins.api :as api] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.state :as ths] + [frontend-tests.helpers.wasm :as thw] + [frontend-tests.tokens.helpers.state :as tohs])) + +(t/use-fixtures :each {:before cthi/reset-idmap!}) + +(def ^:private plugin-id "00000000-0000-0000-0000-000000000000") + +(defn- setup-file + "A column board hugging its width, with a fixed rect and a fill rect. + `extra?` adds a second fixed rect, so the board keeps a sizing child." + [extra?] + (cond-> (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + :x 0 :y 0 :width 80 :height 200 + :layout :flex + :layout-flex-dir :column + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :fixed + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/add-sample-shape :filled + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + extra? + (cths/add-sample-shape :other + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix))) + +(defn- setup-grid-file + [filled-hidden? filled-sizing] + (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + :x 0 :y 0 :width 80 :height 40 + :layout :grid + :layout-grid-dir :row + :layout-grid-columns [{:type :flex :value 1} + {:type :flex :value 1}] + :layout-grid-rows [{:type :flex :value 1}] + :layout-grid-cells {} + :layout-padding-type :simple + :layout-padding {:p1 0 :p2 0 :p3 0 :p4 0} + :layout-gap-type :multiple + :layout-gap {:row-gap 0 :column-gap 0} + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :fixed + :type :rect + :parent-label :frame + :width 40 :height 20 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/add-sample-shape :filled + :type :rect + :parent-label :frame + :width 40 :height 20 + :hidden filled-hidden? + :layout-item-h-sizing filled-sizing + :layout-item-v-sizing :fix) + (cths/update-shape + :frame + :layout-grid-cells + (let [fixed-cell-id (cthi/new-id! :fixed-cell) + filled-cell-id (cthi/new-id! :filled-cell)] + {fixed-cell-id {:id fixed-cell-id + :row 1 :column 1 + :row-span 1 :column-span 1 + :position :auto + :align-self :auto :justify-self :auto + :shapes [(cthi/id :fixed)]} + filled-cell-id {:id filled-cell-id + :row 1 :column 2 + :row-span 1 :column-span 1 + :position :auto + :align-self :auto :justify-self :auto + :shapes [(cthi/id :filled)]}})))) + +(defn- run + "Runs `events` on a store holding `file` and calls `check` with the file + read back from the final state." + [file events check] + (t/async + done + (let [store (ths/setup-store file)] + (ths/run-store store done events + (fn [state] + (check (ths/get-file-from-state state))))))) + +(defn- hug-stopped? + [file'] + (and (= :fix (:layout-item-h-sizing (cths/get-shape file' :frame))) + (= :fill (:layout-item-h-sizing (cths/get-shape file' :filled))))) + +(t/deftest delete-the-sizing-child-fixes-the-hug-board + (run (setup-file false) + [(dwsh/delete-shapes #{(cthi/id :fixed)})] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest delete-the-sizing-child-fixes-the-hug-grid + (run (setup-grid-file false :fill) + [(dwsh/delete-shapes #{(cthi/id :fixed)})] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest delete-keeps-the-hug-while-another-child-sizes-the-board + (run (setup-file true) + [(dwsh/delete-shapes #{(cthi/id :fixed)})] + (fn [file'] + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :frame))))))) + +(t/deftest delete-the-only-child-keeps-the-hug + (run (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + :x 0 :y 0 :width 80 :height 200 + :layout :flex + :layout-flex-dir :column + :layout-item-h-sizing :auto) + (cths/add-sample-shape :only + :type :rect + :parent-label :frame + :layout-item-h-sizing :fix)) + [(dwsh/delete-shapes #{(cthi/id :only)})] + (fn [file'] + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :frame))))))) + +(t/deftest delete-is-undone-with-the-sizing-change + (run (setup-file false) + [(tohs/watch-undo-stack) + (dwsh/delete-shapes #{(cthi/id :fixed)}) + dwu/undo] + (fn [file'] + (t/is (some? (cths/get-shape file' :fixed))) + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :frame))))))) + +(t/deftest hide-the-sizing-child-fixes-the-hug-board + (run (setup-file false) + [(dwsh/update-shape-flags [(cthi/id :fixed)] {:hidden true})] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest hide-the-sizing-child-fixes-the-hug-grid + (run (setup-grid-file false :fill) + [(dwsh/update-shape-flags [(cthi/id :fixed)] {:hidden true})] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest hide-is-undone-with-the-sizing-change + (run (setup-file false) + [(tohs/watch-undo-stack) + (dwsh/update-shape-flags [(cthi/id :fixed)] {:hidden true}) + dwu/undo] + (fn [file'] + (t/is (not (:hidden (cths/get-shape file' :fixed)))) + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :frame))))))) + +(t/deftest lock-only-keeps-the-hug + (run (setup-file false) + [(dwsh/update-shape-flags [(cthi/id :fixed)] {:blocked true})] + (fn [file'] + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :frame))))))) + +(t/deftest toggle-visibility-of-the-sizing-child-fixes-the-hug-board + (run (setup-file false) + [(dws/select-shape (cthi/id :fixed)) + (dwsh/toggle-visibility-selected)] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest toggle-visibility-is-undone-with-the-sizing-change + (run (setup-file false) + [(tohs/watch-undo-stack) + (dws/select-shape (cthi/id :fixed)) + (dwsh/toggle-visibility-selected) + dwu/undo] + (fn [file'] + (t/is (not (:hidden (cths/get-shape file' :fixed)))) + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :frame))))))) + +(t/deftest plugin-hidden-setter-fixes-the-hug-board + (thw/with-wasm-mocks* + (fn [] + (let [file (setup-file false) + store (ths/setup-store file) + ^js context (api/create-context plugin-id) + _ (set! st/state store) + ^js page (.-currentPage context) + ^js shape (.getShapeById page (str (cthi/id :fixed)))] + (set! (.-hidden shape) true) + (t/is (hug-stopped? (ths/get-file-from-state @store))))))) + +;; Showing a hidden fill child + +(t/deftest show-a-fill-child-of-an-empty-hug-board-fixes-the-board + (run (setup-file false) + [(dwsh/update-shape-flags [(cthi/id :filled)] {:hidden true}) + (dwsh/update-shape-flags [(cthi/id :fixed)] {:hidden true}) + (dwsh/update-shape-flags [(cthi/id :filled)] {:hidden false})] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest toggle-visibility-shows-a-fill-child-of-an-empty-hug-board-fixed + (run (setup-file false) + [(dwsh/update-shape-flags [(cthi/id :filled)] {:hidden true}) + (dwsh/update-shape-flags [(cthi/id :fixed)] {:hidden true}) + (dws/select-shape (cthi/id :filled)) + (dwsh/toggle-visibility-selected)] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest show-is-undone-with-the-sizing-change + (run (setup-file false) + [(tohs/watch-undo-stack) + (dwsh/update-shape-flags [(cthi/id :filled)] {:hidden true}) + (dwsh/update-shape-flags [(cthi/id :fixed)] {:hidden true}) + (dwsh/update-shape-flags [(cthi/id :filled)] {:hidden false}) + dwu/undo] + (fn [file'] + (t/is (true? (:hidden (cths/get-shape file' :filled)))) + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :frame))))))) + +(t/deftest show-a-child-set-to-fill-while-hidden-fixes-the-hug-board + (run (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + :x 0 :y 0 :width 80 :height 40 + :layout :flex + :layout-flex-dir :row + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :visible + :type :rect + :parent-label :frame + :width 40 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/add-sample-shape :hidden + :type :rect + :parent-label :frame + :width 40 :height 40 + :hidden true + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix)) + [(dwsl/update-layout-child [(cthi/id :hidden)] {:layout-item-h-sizing :fill}) + (dwsh/update-shape-flags [(cthi/id :hidden)] {:hidden false})] + (fn [file'] + (t/is (= :fix (:layout-item-h-sizing (cths/get-shape file' :frame)))) + (t/is (= :fill (:layout-item-h-sizing (cths/get-shape file' :hidden))))))) + +(t/deftest show-a-child-set-to-fill-while-hidden-fixes-the-hug-grid + (run (setup-grid-file true :fix) + [(dwsl/update-layout-child [(cthi/id :filled)] {:layout-item-h-sizing :fill}) + (dwsh/update-shape-flags [(cthi/id :filled)] {:hidden false})] + (fn [file'] + (t/is (hug-stopped? file'))))) + +(t/deftest plugin-visible-setter-fixes-the-hug-board + (thw/with-wasm-mocks* + (fn [] + (let [file (setup-file false) + store (ths/setup-store file) + ^js context (api/create-context plugin-id) + _ (set! st/state store) + ^js page (.-currentPage context) + ^js shape (.getShapeById page (str (cthi/id :fixed)))] + (set! (.-visible shape) false) + (t/is (hug-stopped? (ths/get-file-from-state @store))))))) + +(t/deftest plugin-layout-child-absolute-setter-fixes-the-hug-board + (thw/with-wasm-mocks* + (fn [] + (let [file (setup-file false) + store (ths/setup-store file) + ^js context (api/create-context plugin-id) + _ (set! st/state store) + ^js page (.-currentPage context) + ^js shape (.getShapeById page (str (cthi/id :fixed))) + ^js child (.-layoutChild shape)] + (set! (.-absolute child) true) + (let [file' (ths/get-file-from-state @store) + filled (cths/get-shape file' :filled)] + (t/is (hug-stopped? file')) + (t/is (true? (:layout-item-absolute (cths/get-shape file' :fixed)))) + (t/is (= 80 (:width filled)))))))) + +;; Deleting more than the direct child + +(t/deftest delete-a-grouped-child-fixes-the-hug-board + (run (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + :x 0 :y 0 :width 80 :height 200 + :layout :flex + :layout-flex-dir :column + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (ctho/add-group :group :parent-label :frame) + (cths/add-sample-shape :grouped + :type :rect + :parent-label :group + :width 80 :height 20) + (cths/add-sample-shape :filled + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + [(dwsh/delete-shapes #{(cthi/id :grouped)})] + (fn [file'] + (t/is (nil? (cths/get-shape file' :group))) + (t/is (hug-stopped? file'))))) + +(t/deftest delete-on-another-page-fixes-the-hug-board + (let [file (-> (cthf/sample-file :file1 :page-label :page-a) + (cthf/add-sample-page :page-b) + (cthf/switch-to-page :page-b) + (ctho/add-frame :frame + :x 0 :y 0 :width 80 :height 200 + :layout :flex + :layout-flex-dir :column + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :fixed + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/add-sample-shape :filled + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix) + (cthf/switch-to-page :page-a))] + (run file + [(dwsh/delete-shapes (cthi/id :page-b) #{(cthi/id :fixed)})] + (fn [file'] + (t/is (= :fix (:layout-item-h-sizing (cths/get-shape file' :frame :page-label :page-b)))) + (t/is (= :fill (:layout-item-h-sizing (cths/get-shape file' :filled :page-label :page-b)))))))) diff --git a/frontend/test/frontend_tests/data/workspace_shape_layout_test.cljs b/frontend/test/frontend_tests/data/workspace_shape_layout_test.cljs index ac91e826f9..a316566eb2 100644 --- a/frontend/test/frontend_tests/data/workspace_shape_layout_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_shape_layout_test.cljs @@ -7,9 +7,14 @@ (ns frontend-tests.data.workspace-shape-layout-test (:require [app.common.schema :as sm] + [app.common.test-helpers.compositions :as ctho] + [app.common.test-helpers.files :as cthf] + [app.common.test-helpers.ids-map :as cthi] + [app.common.test-helpers.shapes :as cths] [app.common.types.shape.layout :as ctl] [app.main.data.workspace.shape-layout :as dwsl] - [cljs.test :as t :include-macros true])) + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.state :as ths])) (def ^:private valid-layout-padding? (sm/lazy-validator ctl/schema:layout-attrs)) @@ -60,3 +65,150 @@ result (dwsl/update-shape-layout shape {:layout-gap {:row-gap 4}})] (t/is (= {:p1 12 :p2 0 :p3 0 :p4 0} (:layout-padding result))) (t/is (valid-layout-padding? (select-keys result [:layout-padding]))))) + +(defn- setup-nested-fill-file + "A row board with a fill column board that holds a fill rect." + [] + (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + :x 0 :y 0 :width 100 :height 40 + :layout :flex + :layout-flex-dir :row) + (ctho/add-frame :inner + :parent-label :frame + :width 60 :height 40 + :layout :flex + :layout-flex-dir :column + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix) + (cths/add-sample-shape :child + :type :rect + :parent-label :inner + :width 60 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix))) + +(t/deftest update-layout-child-hug-fixes-only-direct-fill-children + (t/async + done + (let [file (setup-nested-fill-file) + store (ths/setup-store file) + event (dwsl/update-layout-child [(cthi/id :frame)] + {:layout-item-h-sizing :auto})] + (ths/run-store + store done [event] + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + inner (cths/get-shape file' :inner) + child (cths/get-shape file' :child)] + (t/is (= :fix (:layout-item-h-sizing inner)) + "the fill direct child of the hug board becomes fix") + (t/is (= :fill (:layout-item-h-sizing child)) + "the fill grandchild keeps its sizing"))))))) + +(defn- setup-hug-column-file + "A column board hugging its width, with a fixed rect and a fill rect. + `extra?` adds a second fixed rect." + [extra?] + (cond-> (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + :x 0 :y 0 :width 80 :height 200 + :layout :flex + :layout-flex-dir :column + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :fixed + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/add-sample-shape :filled + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + extra? + (cths/add-sample-shape :other + :type :rect + :parent-label :frame + :width 80 :height 20 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix))) + +(t/deftest update-layout-child-absolute-fixes-a-hug-board-left-with-fill-children + (t/async + done + (let [file (setup-hug-column-file false) + store (ths/setup-store file) + event (dwsl/update-layout-child [(cthi/id :fixed)] + {:layout-item-absolute true})] + (ths/run-store + store done [event] + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + frame (cths/get-shape file' :frame) + filled (cths/get-shape file' :filled)] + (t/is (= :fix (:layout-item-h-sizing frame)) + "the board stops hugging") + (t/is (= :fill (:layout-item-h-sizing filled)) + "the remaining child keeps its fill"))))))) + +(t/deftest update-layout-child-absolute-keeps-hug-when-another-child-sizes-the-board + (t/async + done + (let [file (setup-hug-column-file true) + store (ths/setup-store file) + event (dwsl/update-layout-child [(cthi/id :fixed)] + {:layout-item-absolute true})] + (ths/run-store + store done [event] + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + frame (cths/get-shape file' :frame)] + (t/is (= :auto (:layout-item-h-sizing frame))))))))) + +(defn- setup-hug-grid-file + "A grid board hugging its width, with one fix child. `extra-attrs` are + merged into the board." + [extra-attrs] + (-> (cthf/sample-file :file1) + (ctho/add-frame :frame + (merge {:x 0 :y 0 :width 80 :height 80 + :layout :grid + :layout-grid-dir :row + :layout-grid-rows [{:type :flex :value 1}] + :layout-grid-columns [{:type :flex :value 1}] + :layout-grid-cells {} + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix} + extra-attrs)) + (cths/add-sample-shape :child + :type :rect + :parent-label :frame + :width 80 :height 80 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix))) + +(defn- check-grid-child-fill + [file] + (t/async + done + (let [store (ths/setup-store file) + event (dwsl/update-layout-child [(cthi/id :child)] + {:layout-item-h-sizing :fill})] + (ths/run-store + store done [event] + (fn [new-state] + (let [file' (ths/get-file-from-state new-state)] + (t/is (= :fix (:layout-item-h-sizing (cths/get-shape file' :frame))) + "the grid stops hugging") + (t/is (= :fill (:layout-item-h-sizing (cths/get-shape file' :child))) + "the child keeps its fill"))))))) + +(t/deftest update-layout-child-fill-fixes-a-hug-grid + (check-grid-child-fill (setup-hug-grid-file {}))) + +(t/deftest update-layout-child-fill-fixes-a-hug-grid-with-leftover-flex-direction + (check-grid-child-fill (setup-hug-grid-file {:layout-flex-dir :column}))) diff --git a/frontend/test/frontend_tests/data/workspace_variants_test.cljs b/frontend/test/frontend_tests/data/workspace_variants_test.cljs index 0c80611d03..24af59e800 100644 --- a/frontend/test/frontend_tests/data/workspace_variants_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_variants_test.cljs @@ -414,6 +414,52 @@ (t/is (= variant-id (:variant-id comp'))))) after-commits-idle)))) +(defn- fill-main-in-board-file + "A main component that is a fill child of a flex board." + [sizing] + (-> (cthf/sample-file :file1) + (tho/add-frame :board + :x 0 :y 0 :width 300 :height 100 + :layout :flex + :layout-flex-dir :row) + (tho/add-simple-component :c01 :m01 :r01 + :root-params {:parent-label :board + :width 100 :height 80 + :layout-item-h-sizing sizing + :layout-item-v-sizing :fix}))) + +(t/deftest transform-in-variant-keeps-a-fill-main-at-its-size + (t/async + done + (let [store (ths/setup-store (fill-main-in-board-file :fill)) + id (cthi/id :m01) + vid (uuid/next)] + (ths/run-store + store done + [(dwv/transform-in-variant id vid nil [] false true true)] + (fn [state] + (let [objects (page-objects (ths/get-file-from-state state)) + mains (map objects (:shapes (get objects vid)))] + (t/is (= 2 (count mains))) + (t/is (every? #(= :fix (:layout-item-h-sizing %)) mains)) + (t/is (every? #(= 100 (:width %)) mains)))) + after-commits-idle)))) + +(t/deftest transform-in-variant-keeps-the-sizing-of-a-fixed-main + (t/async + done + (let [store (ths/setup-store (fill-main-in-board-file :fix)) + id (cthi/id :m01) + vid (uuid/next)] + (ths/run-store + store done + [(dwv/transform-in-variant id vid nil [] false true true)] + (fn [state] + (let [objects (page-objects (ths/get-file-from-state state)) + mains (map objects (:shapes (get objects vid)))] + (t/is (every? #(= :fix (:layout-item-h-sizing %)) mains)))) + after-commits-idle)))) + (t/deftest transform-in-variant-path-name-validates-after-every-commit (t/async done diff --git a/frontend/test/frontend_tests/logic/move_shapes_to_frame_test.cljs b/frontend/test/frontend_tests/logic/move_shapes_to_frame_test.cljs index 7dab714ad3..bbf9d7c079 100644 --- a/frontend/test/frontend_tests/logic/move_shapes_to_frame_test.cljs +++ b/frontend/test/frontend_tests/logic/move_shapes_to_frame_test.cljs @@ -11,8 +11,10 @@ [app.common.test-helpers.files :as cthf] [app.common.test-helpers.ids-map :as cthi] [app.common.test-helpers.shapes :as cths] + [app.common.types.modifiers :as ctm] [app.common.uuid :as uuid] [app.main.data.changes :as dch] + [app.main.data.workspace.modifiers :as dwm] [app.main.data.workspace.transforms :as dwt] [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] @@ -63,3 +65,97 @@ (fn [commits file'] (t/is (= 1 commits)) (t/is (= (:id frame) (:parent-id (cths/get-shape file' :rect1))))))))) + +(defn- hug-row-file + "A row board hugging its width at the width of its only child, and a fill + rect outside of it." + [] + (-> (cthf/sample-file :file1) + (ctho/add-frame :hug + :x 0 :y 0 :width 32 :height 40 + :layout :flex + :layout-flex-dir :row + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :inside + :type :rect + :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/add-sample-shape :arriving + :type :rect + :x 300 :y 0 :width 32 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix))) + +(defn- hug-grid-file + "A grid board hugging its width, with a fixed child and a fill sibling." + [] + (-> (cthf/sample-file :file1) + (ctho/add-frame :hug + :x 0 :y 0 :width 64 :height 40 + :layout :grid + :layout-grid-dir :row + :layout-grid-columns [{:type :flex :value 1} + {:type :flex :value 1}] + :layout-grid-rows [{:type :flex :value 1}] + :layout-grid-cells {} + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix) + (cths/add-sample-shape :leaving + :type :rect + :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix) + (cths/add-sample-shape :staying + :type :rect + :parent-label :hug + :width 32 :height 40 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix))) + +;; Scenario: a move ends over a row board that hugs its width at the width of +;; its children. Proves: the arriving fill shape turns fix, and the board keeps +;; hugging. +(t/deftest moving-a-fill-shape-into-a-hug-board-fixes-the-shape + (t/async + done + (count-commits done (hug-row-file) + (dwt/move-shapes-to-frame [(cthi/id :arriving)] (cthi/id :hug) nil nil) + (fn [_ file'] + (t/is (= :fix (:layout-item-h-sizing (cths/get-shape file' :arriving)))) + (t/is (= :auto (:layout-item-h-sizing (cths/get-shape file' :hug)))))))) + +;; Scenario: the drag preview of the same move. Proves: it fixes the arriving +;; shape and leaves the board alone, as the commit does. +(t/deftest dragging-a-fill-shape-over-a-hug-board-previews-the-fixed-shape + (let [file (hug-row-file) + objects (-> (cthf/current-page file) :objects) + arriving (cthi/id :arriving) + hug (cthi/id :hug) + modifiers (fn [tree id] + (->> (get-in tree [id :modifiers :structure-parent]) + (filter #(= :change-property (:type %))) + (map (juxt :property :value)) + set)) + tree (-> (dwm/create-modif-tree [arriving] (ctm/empty)) + (dwm/build-change-frame-modifiers objects [arriving] hug 0 nil))] + (t/is (contains? (modifiers tree arriving) [:layout-item-h-sizing :fix])) + (t/is (not-any? (comp #{:layout-item-h-sizing :layout-item-v-sizing} first) + (modifiers tree hug))))) + +(t/deftest dragging-a-fixed-child-out-of-a-hug-grid-previews-a-fixed-parent + (let [file (hug-grid-file) + objects (-> (cthf/current-page file) :objects) + leaving (cthi/id :leaving) + hug (cthi/id :hug) + modifiers (fn [tree id] + (->> (get-in tree [id :modifiers :structure-parent]) + (filter #(= :change-property (:type %))) + (map (juxt :property :value)) + set)) + tree (-> (dwm/create-modif-tree [leaving] (ctm/empty)) + (dwm/build-change-frame-modifiers objects [leaving] uuid/zero 0 nil))] + (t/is (contains? (modifiers tree hug) [:layout-item-h-sizing :fix])))) diff --git a/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs b/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs index 3a14c80b4b..984e517ff3 100644 --- a/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs +++ b/frontend/test/frontend_tests/logic/pasting_in_containers_test.cljs @@ -1202,4 +1202,56 @@ ;; ==== Check (t/is (= "#ff0000" (-> rect1' :fills first :fill-color))))) - stopper)))) \ No newline at end of file + stopper)))) +(defn- paste-fill-rect + "Pastes a fill-width rect into a board built with `board-attrs` and + calls `check` with the pasted rect and the board read back from the file." + [board-attrs check] + (t/async + done + (let [file (-> (cthf/sample-file :file1) + (ctho/add-frame :board (merge {:x 0 :y 0 :width 100 :height 40} + board-attrs)) + (cths/add-sample-shape :src + :type :rect + :x 300 :y 0 :width 32 :height 16 + :layout-item-h-sizing :fill + :layout-item-v-sizing :fix)) + store (ths/setup-store file) + page (cthf/current-page file) + pdata (thp/simulate-copy-shape #{(cthi/id :src)} (:objects page) + {(:id file) file} page file #{} 67)] + (ths/run-store + store done + [(dws/select-shape (cthi/id :board)) + (dw/paste-shapes pdata)] + (fn [new-state] + (let [file' (ths/get-file-from-state new-state) + board' (cths/get-shape file' :board) + pasted' (->> (:shapes board') + (map #(get-in (cthf/current-page file') [:objects %])) + first)] + (check pasted' board'))))))) + +(t/deftest paste-fill-shape-into-hug-board-keeps-its-size + (paste-fill-rect {:layout :flex + :layout-flex-dir :row + :layout-item-h-sizing :auto + :layout-item-v-sizing :fix} + (fn [pasted board] + (t/is (= :fix (:layout-item-h-sizing pasted))) + (t/is (= 32 (:width pasted))) + (t/is (= :auto (:layout-item-h-sizing board)))))) + +(t/deftest paste-fill-shape-into-fixed-board-keeps-fill + (paste-fill-rect {:layout :flex + :layout-flex-dir :row + :layout-item-h-sizing :fix + :layout-item-v-sizing :fix} + (fn [pasted _board] + (t/is (= :fill (:layout-item-h-sizing pasted)))))) + +(t/deftest paste-fill-shape-into-board-without-layout-keeps-sizing + (paste-fill-rect {} + (fn [pasted _board] + (t/is (= :fill (:layout-item-h-sizing pasted)))))) diff --git a/frontend/test/frontend_tests/plugins/flex_test.cljs b/frontend/test/frontend_tests/plugins/flex_test.cljs index 9cf5e68442..c62a695dac 100644 --- a/frontend/test/frontend_tests/plugins/flex_test.cljs +++ b/frontend/test/frontend_tests/plugins/flex_test.cljs @@ -6,15 +6,21 @@ (ns frontend-tests.plugins.flex-test (:require + [app.common.test-helpers.files :as cthf] [app.common.types.shape.layout :as ctl] [app.common.uuid :as uuid] + [app.main.data.workspace.shape-layout :as dwsl] [app.main.store :as st] + [app.plugins.api :as api] [app.plugins.flex :as flex] [app.plugins.register :as r] [app.plugins.shape :as shape] [app.plugins.utils :as u] [cljs.test :as t :include-macros true] - [frontend-tests.helpers.mock :as mock])) + [frontend-tests.helpers.mock :as mock] + [frontend-tests.helpers.state :as ths] + [frontend-tests.helpers.wasm :as thw] + [potok.v2.core :as ptk])) ;; --------------------------------------------------------------------------- ;; Permission checks (T9-F-05) @@ -38,3 +44,29 @@ ;; TODO: flex-append-child-checks-permission test requires more complex mocking ;; of u/locate-objects, u/locate-shape, ctl/reverse?, etc. The permission check ;; is in place at flex.cljs line 358. + +(defn- emitted-types + "Runs `thunk` and returns the `ptk/type` of every event emitted through + `st/emit!`." + [thunk] + (let [events (atom [])] + (with-redefs [st/emit! (mock/stub (fn [event] (swap! events conj event)))] + (thunk)) + (mapv ptk/type @events))) + +(t/deftest container-sizing-setters-emit-update-layout-child + ;; Setting hug on a container must go through the guard that fixes its + ;; fill children, like the design panel does. + (thw/with-wasm-mocks* + (fn [] + (let [store (ths/setup-store (cthf/sample-file :file1 :page-label :page1)) + _ (set! st/state store) + _ (set! st/stream (ptk/input-stream store)) + ^js context (api/create-context "00000000-0000-0000-0000-000000000000") + ^js board (.createBoard context) + ^js flex (.addFlexLayout board) + expected [::dwsl/update-layout-child]] + (t/is (= expected (emitted-types #(set! (.-horizontalSizing flex) "auto")))) + (t/is (= expected (emitted-types #(set! (.-verticalSizing flex) "auto")))) + (t/is (= expected (emitted-types #(set! (.-horizontalSizing board) "auto")))) + (t/is (= expected (emitted-types #(set! (.-verticalSizing board) "auto")))))))) diff --git a/frontend/test/frontend_tests/plugins/grid_test.cljs b/frontend/test/frontend_tests/plugins/grid_test.cljs index 66e5f51a48..6d3f37595e 100644 --- a/frontend/test/frontend_tests/plugins/grid_test.cljs +++ b/frontend/test/frontend_tests/plugins/grid_test.cljs @@ -7,10 +7,12 @@ (ns frontend-tests.plugins.grid-test (:require [app.common.test-helpers.files :as cthf] + [app.main.data.workspace.shape-layout :as dwsl] [app.main.store :as st] [app.plugins.api :as api] [app.plugins.utils :as u] [cljs.test :as t :include-macros true] + [frontend-tests.helpers.mock :as mock] [frontend-tests.helpers.state :as ths] [frontend-tests.helpers.wasm :as thw] [potok.v2.core :as ptk])) @@ -62,3 +64,20 @@ :layout-grid-dir) (set! (.-dir grid) "row") (t/is (= "row" (.-dir grid))))))) + +(defn- emitted-types + "Runs `thunk` and returns the `ptk/type` of every event emitted through + `st/emit!`." + [thunk] + (let [events (atom [])] + (with-redefs [st/emit! (mock/stub (fn [event] (swap! events conj event)))] + (thunk)) + (mapv ptk/type @events))) + +(t/deftest grid-sizing-setters-emit-update-layout-child + (thw/with-wasm-mocks* + (fn [] + (let [{:keys [^js grid]} (setup-grid) + expected [::dwsl/update-layout-child]] + (t/is (= expected (emitted-types #(set! (.-horizontalSizing grid) "auto")))) + (t/is (= expected (emitted-types #(set! (.-verticalSizing grid) "auto")))))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index c8d2240821..6649d80d2f 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -39,8 +39,10 @@ [frontend-tests.data.workspace-colors-test] [frontend-tests.data.workspace-comments-test] [frontend-tests.data.workspace-context-menu-test] + [frontend-tests.data.workspace-fit-content-test] [frontend-tests.data.workspace-focus-test] [frontend-tests.data.workspace-heal-undo-test] + [frontend-tests.data.workspace-hug-parents-test] [frontend-tests.data.workspace-interactions-test] [frontend-tests.data.workspace-layout-other-page-test] [frontend-tests.data.workspace-libraries-load-test] @@ -257,8 +259,10 @@ 'frontend-tests.data.workspace-colors-test 'frontend-tests.data.workspace-comments-test 'frontend-tests.data.workspace-context-menu-test + 'frontend-tests.data.workspace-fit-content-test 'frontend-tests.data.workspace-focus-test 'frontend-tests.data.workspace-heal-undo-test + 'frontend-tests.data.workspace-hug-parents-test 'frontend-tests.data.workspace-interactions-test 'frontend-tests.data.workspace-layout-other-page-test 'frontend-tests.data.workspace-libraries-load-test