diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 037308010d..8730d94018 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -105,6 +105,8 @@ (declare add-shape-to-instance) (declare add-shape-to-main) (declare axes-matrix) +(declare layout-skewed?) +(declare unskewed) (declare remove-shape) (declare move-shape) (declare reorder-main-children) @@ -345,7 +347,10 @@ main-parent (get-in main-container [:objects (:parent-id shape-main)]) swapped? (and (not reset?) (ctk/swapped? shape-inst)) origin (if swapped? shape-main main-parent) - target (if swapped? shape-inst parent) + ;; a head that the layout skews takes the turn it skews + target (if swapped? + (cond-> shape-inst (layout-skewed? (:objects container) shape-inst) (unskewed)) + parent) ;; the axes only: a tiny shape's transform carries its rounding relative (gmt/multiply (axes-matrix target) (gmt/inverse (axes-matrix origin)))] (if (or (nil? target) (and (:shape-ref parent) (not reset?) (not swapped?)) @@ -599,6 +604,37 @@ vertical (gpt/unit (gpt/point c d))] (gmt/matrix (:x horizontal) (:y horizontal) (:x vertical) (:y vertical) 0 0))) +(defn- skewed? + "Whether the axes of the transform of `shape` are not perpendicular." + [shape] + (let [{:keys [a b c d]} (axes-matrix shape)] + (not (mth/close? (+ (* a c) (* b d)) 0 0.0001)))) + +(defn- unskewed + "`shape` with the transform that its rotation and flips give it, without + the skew a layout gives a turned child that it resizes along its parent's + axes. Of a turn before or after the flips, the one nearer its axes." + [shape] + (let [turn (gmt/rotate-matrix (d/nilv (:rotation shape) 0)) + flips (gmt/scale-matrix (gpt/point (if (:flip-x shape) -1 1) (if (:flip-y shape) -1 1))) + axes (axes-matrix shape) + ;; how close the axes of `m` are to those of the shape + closeness (fn [m] (+ (* (:a m) (:a axes)) (* (:b m) (:b axes)) + (* (:c m) (:c axes)) (* (:d m) (:d axes)))) + transform (max-key closeness (gmt/multiply turn flips) (gmt/multiply flips turn))] + (assoc shape :transform transform :transform-inverse (gmt/inverse transform)))) + +(defn- layout-skewed? + "Whether the layout of its parent skews the copy shape `shape` of + `objects`: it fills in a direction, it does not author its geometry, and + the layout resizes it along the parent's axes." + [objects shape] + (and (not (ctk/touched-group? shape :geometry-group)) + (some #(= :fill (get shape %)) [:layout-item-h-sizing :layout-item-v-sizing]) + (ctsl/any-layout-immediate-child? objects shape) + (not (ctsl/position-absolute? shape)) + (skewed? shape))) + (defn- sync-descendant-orientation "A parent turned or moved by sync carries its whole subtree, including geometry overrides. Resize remains the job of constraints, not matrix @@ -1861,19 +1897,32 @@ "Synchronizes a swapped copy head with the main shape it replaces, in the attributes that place it in its parent. A head with untouched geometry that stops filling its parent in a direction is synced with its own - component, as the layout no longer sizes it." + component, as the layout no longer sizes it. One that fills where the + layout skews that shape takes the lengths of its own component and the + turn of that shape, which the layout then skews as it skewed the shape." [changes {:keys [root-inst root-main container file libraries]} shape-inst shape-main] (let [objects (:objects container) - changes (generate-take-placement changes - shape-inst - (reposition-shape shape-main root-main root-inst) - objects - (and (ctsl/any-layout-immediate-child? objects shape-inst) - (not (ctsl/position-absolute? shape-inst)))) + placed (reposition-shape shape-main root-main root-inst) sizings [:layout-item-h-sizing :layout-item-v-sizing] ;; whether the layout may have sized the head filling? (and (not (ctk/touched-group? shape-inst :geometry-group)) - (some #(= :fill (get shape-inst %)) sizings))] + (some #(= :fill (get shape-inst %)) sizings)) + layout-placed? (and (ctsl/any-layout-immediate-child? objects shape-inst) + (not (ctsl/position-absolute? shape-inst))) + skew? (and filling? layout-placed? (skewed? placed)) + changes (if skew? + (let [changes (pcb/apply-changes-local changes)] + (-> (generate-sync-shape-direct changes file libraries + (assoc container :objects (pcb/get-objects changes)) + (:id shape-inst) false) + (pcb/apply-changes-local))) + changes) + objects (if skew? (pcb/get-objects changes) objects) + changes (generate-take-placement changes + (get objects (:id shape-inst)) + (cond-> placed skew? (unskewed)) + objects + layout-placed?)] (if filling? (let [changes (pcb/apply-changes-local changes) objects (pcb/get-objects changes) diff --git a/common/test/common_tests/logic/comp_sync_layout_items_test.cljc b/common/test/common_tests/logic/comp_sync_layout_items_test.cljc index 39bc833108..8f0cccbbbf 100644 --- a/common/test/common_tests/logic/comp_sync_layout_items_test.cljc +++ b/common/test/common_tests/logic/comp_sync_layout_items_test.cljc @@ -7,9 +7,13 @@ (ns common-tests.logic.comp-sync-layout-items-test (:require [app.common.files.changes-builder :as pcb] + [app.common.geom.point :as gpt] [app.common.geom.shapes :as gsh] [app.common.logic.libraries :as cll] [app.common.test-helpers.components :as thc] + [app.common.test-helpers.composable.comp.nodes :as n] + [app.common.test-helpers.composable.comp.runner :as r] + [app.common.test-helpers.composable.core :as tm] [app.common.test-helpers.compositions :as tho] [app.common.test-helpers.files :as thf] [app.common.test-helpers.ids-map :as thi] @@ -254,3 +258,34 @@ (t/is (= :fix (:layout-item-h-sizing copy-big'))) (t/is (== 140 (:width copy-big'))) (t/is (== 140 (-> copy-big' :selrect :width))))) + +(t/deftest swapped-child-turned-to-fill-takes-the-skew-of-its-main + ;; the main turns a child that fills the height of its row, and the layout + ;; skews it: a swapped copy of it in an identical row shows the same shape + (let [file (-> (thf/sample-file :file1) + (tho/add-frame :frame-a :width 100 :height 100) + (thc/make-component :a :frame-a) + (tho/add-frame :frame-b :width 100 :height 100) + (thc/make-component :b :frame-b) + (tho/add-frame :row-main {:layout :flex :layout-flex-dir :row :width 100 :height 100}) + (thc/instantiate-component :a :nested :parent-label :row-main) + (tho/change-shape :nested #(assoc % :layout-item-v-sizing :fill)) + (thc/make-component :row :row-main) + (thc/instantiate-component :row :plain :children-labels [:plain-nested]) + (thc/instantiate-component :row :swapped :children-labels [:swapped-nested]) + (tho/swap-component-in-shape :swapped-nested :b :new-shape-label :swapped-b)) + settled (-> (tm/make-situation file) + (r/step (n/rotate :nested 45)) + r/settle) + resynced (-> (tm/file settled) + (as-> file (:file (r/sync-cascade file {} (keys (get-in file [:data :components]))))) + (tm/make-situation) + r/settle-layout) + ;; the corners of the child of the row `row`, from the row's corner + corners (fn [situation row child] + (let [origin (-> (tm/resolve-shape situation row) :points first)] + (mapv #(gpt/subtract % origin) (:points (tm/resolve-shape situation child)))))] + (t/is (gsh/close-attrs? :points (corners settled :plain :plain-nested) + (corners settled :swapped :swapped-b) 0.01)) + (t/is (gsh/close-attrs? :points (corners settled :swapped :swapped-b) + (corners resynced :swapped :swapped-b) 0.01)))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 9f6379bb7f..46becccf7c 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1971,6 +1971,15 @@ :grid-empty false :layout-copies true :text true}] :ops [[:nest 7 0 0 0] [:transform 3 3 0 0] [:switch 1 0 0 0] [:style 3 29 1]]})))) +(t/deftest a-swapped-copy-turned-to-fill-a-row-settles-like-its-main + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :flex :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:delete 0] [:restore 0] [:add-variant 0 0 0 0] [:swap 6 0 0 0] [:combine 5 0 0 0] + [:delete 12 0 0 0] [:combine 9 0 0 0] [:nest 0 0 0 0] [:restore 0 0 0 0] + [:combine 8] [:switch 3] [:style 0 30 1] [:detach 1 0 0 0] [:combine 0] + [:switch 7] [:nest 4 0 0 0] [:rotate 2 0 0 0] [:combine 0] [:switch 0]]})))) + (t/deftest switching-a-grid-copy-places-its-kept-swap-in-a-cell (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :grid :variant-diff false :layout-item-margins false :grid-square false :rich-variants true