diff --git a/common/src/app/common/test_helpers/composable/comp/undo_check.cljc b/common/src/app/common/test_helpers/composable/comp/undo_check.cljc index 81ef0740d1..e51ca46f23 100644 --- a/common/src/app/common/test_helpers/composable/comp/undo_check.cljc +++ b/common/src/app/common/test_helpers/composable/comp/undo_check.cljc @@ -38,6 +38,7 @@ [app.common.geom.point :as gpt] [app.common.math :as mth] [app.common.test-helpers.composable.core :as tm] + [app.common.test-helpers.composable.geometry :as geom] [app.common.test-helpers.ids-map :as thi] [app.common.test-helpers.normalize :as thn] [clojure.test :as t] @@ -170,10 +171,10 @@ #{} (constantly nil))))))) (defn- corner-order - "The corners of a bool by their rounded position: a bool computes them - again from its children, in any order." + "The corners of a bool, which it computes again from its children in any + order (`geom/ordered-corners`)." [points] - (sort-by (juxt #(Math/round (* 100.0 (:x %))) #(Math/round (* 100.0 (:y %)))) points)) + (geom/ordered-corners points)) (defn same-state? "Compare exact data with an absolute tolerance only in shape geometry. @@ -198,6 +199,10 @@ ;; a bool computes its segments from its children, in an order ;; that depends on how it computed them; its children are compared (and (= :content key) (= :bool (:type expected)) (contains? expected :parent-id)) + ;; fitting a group or bool to its children clears its flip flags, + ;; which its transform and corners still show + (and (#{:flip-x :flip-y} key) (#{:group :bool} (:type expected)) + (contains? expected :parent-id)) (same-state? (cond-> (get expected key) (and (= :points key) (= :bool (:type expected)) (contains? expected :parent-id)) corner-order) diff --git a/common/src/app/common/test_helpers/composable/gen.cljc b/common/src/app/common/test_helpers/composable/gen.cljc index 7e8d2dbd64..7d2d60d034 100644 --- a/common/src/app/common/test_helpers/composable/gen.cljc +++ b/common/src/app/common/test_helpers/composable/gen.cljc @@ -814,13 +814,18 @@ (n/edit-grid target :cell (assoc (select-keys cell [:row :column]) :props props))) (:transform :library-transform) (let [action (nth [:move :width :height :rotate :flip-x :flip-y] (mod (or (nth tuple 2 nil) 0) 6)) - value (or (nth tuple 3 nil) 0)] - (n/transform-shape target action + value (or (nth tuple 3 nil) 0) + shape (:shape selected)] + ;; resizing a copy bool stays out until its + ;; stored orientation is reliable (F518) + (when-not (and (#{:width :height} action) + (= :bool (:type shape)) (:shape-ref shape)) + (n/transform-shape target action (case action :move [(* 5 (inc (mod value 4))) (* -3 (inc (mod value 3)))] (:width :height) (+ 80 (* 20 (mod value 6))) :rotate (* 45 (inc (mod value 4))) - nil))) + nil)))) :library-edit (n/change-property target property value) :library-add-child (n/add-child target (keyword (str "library-generated-" (count (:applied situation)))) :width 20 :height 20 :x 10 :y 10) diff --git a/common/src/app/common/test_helpers/composable/geometry.cljc b/common/src/app/common/test_helpers/composable/geometry.cljc index f415d25f91..b5bcc22447 100644 --- a/common/src/app/common/test_helpers/composable/geometry.cljc +++ b/common/src/app/common/test_helpers/composable/geometry.cljc @@ -173,3 +173,20 @@ (gsh/transform-shape child (gsh/calc-child-modifiers parent child modifiers false (:points child) (:points parent) (delay (:points resized)))))))) + +(defn ordered-corners + "The corners `points` of a group or bool, which keeps them in any order, + by their angle around their centre: it holds whatever the shape's size, + turn or noise, where an order by position swaps the corners of a tiny or + turned shape." + [points] + (when (seq points) + (let [cx (/ (reduce + (map :x points)) (count points)) + cy (/ (reduce + (map :y points)) (count points)) + ;; angles to the ten-thousandth of a radian, rounded before they wrap, + ;; so a corner level with the centre sorts first whichever side the + ;; single precision noise of a bool puts it + turn (Math/round (* 2e4 Math/PI))] + (sort-by (fn [{:keys [x y]}] + (mod (Math/round (* 1e4 (Math/atan2 (- y cy) (- x cx)))) turn)) + points)))) diff --git a/common/src/app/common/test_helpers/composable/oracle.cljc b/common/src/app/common/test_helpers/composable/oracle.cljc index 6060d348f3..a451d0d3b3 100644 --- a/common/src/app/common/test_helpers/composable/oracle.cljc +++ b/common/src/app/common/test_helpers/composable/oracle.cljc @@ -155,11 +155,6 @@ node)) content)) -;; the order of a bool's corners: by position, to the hundredth of a pixel, -;; so noise in one coordinate does not swap two corners -(def ^:private corner-order - (juxt #(Math/round (* 100.0 (:x %))) #(Math/round (* 100.0 (:y %))))) - (defn- without-unreferenced-deleted "The snapshot `files` without the deleted components no shape outside their own objects refers to, as an undone creation: only redo brings one @@ -209,9 +204,9 @@ (and (= :path (:type value)) (some? (:content value))) (update :content #(into [] %)) ;; a bool computes its segments from its children, which the design ;; holds, in an order that depends on how it was computed - (= :bool (:type value)) (-> (dissoc :content) - ;; and its corners in their order - (update :points #(some->> % (sort-by corner-order) vec))) + (= :bool (:type value)) (dissoc :content) + ;; a group or bool takes its corners from its children, in any order + (#{:group :bool} (:type value)) (update :points #(some-> % geom/ordered-corners vec)) ;; the transform carries the turn, whichever rotation the shape ;; stores for it :always (dissoc :rotation)) @@ -550,10 +545,12 @@ (not= (boolean (:flip-y shape)) (boolean (:flip-y expected))) (geom/decomposes? shape)) ;; a group or bool takes its corners from its children: at its - ;; expected corners, its turn and flips say nothing more - derived (and (#{:group :bool} (:type shape)) - (gsh/close-attrs? :points (sort-by corner-order (:points shape)) - (sort-by corner-order (:points expected)) 0.01))] + ;; expected corners, its turn and flips say nothing more; nor do those + ;; of its children, whose orientation it stores unreliably + in-group? (#{:group :bool} (:type (get objects (:parent-id ownership)))) + derived (and (or (#{:group :bool} (:type shape)) in-group?) + (gsh/close-attrs? :points (geom/ordered-corners (:points shape)) + (geom/ordered-corners (:points expected)) 0.01))] (cond-> #{} ;; Parents may store equivalent decompositions, so a rotation that ;; decomposes the shape's own transform is its expected turn too, as @@ -576,9 +573,9 @@ [(first (:points expected))] 0.01))) (conj :position) (and (not layout-child) (not auto-width) (not auto-height) ;; a bool computes its corners from its segments, in their order - (not (if (= :bool (:type shape)) - (gsh/close-attrs? :points (sort-by corner-order (:points shape)) - (sort-by corner-order (:points expected)) 0.01) + (not (if (or (= :bool (:type shape)) in-group?) + (gsh/close-attrs? :points (geom/ordered-corners (:points shape)) + (geom/ordered-corners (:points expected)) 0.01) (gsh/close-attrs? :points (:points shape) (:points expected) 0.01)))) (conj :points))))) (defn check-requested-geometry @@ -631,8 +628,9 @@ source (cond-> source (and source-parent kept-parent (= (:id source-parent) (:id kept-parent)) - (mth/close? (:width source-parent) (:width kept-parent) 0.01) - (mth/close? (:height source-parent) (:height kept-parent) 0.01)) + ;; a bool has no size but its selrect's + (mth/close? (-> source-parent :selrect :width) (-> kept-parent :selrect :width) 0.01) + (mth/close? (-> source-parent :selrect :height) (-> kept-parent :selrect :height) 0.01)) (gsh/move (gpt/subtract (first (:points kept-parent)) (first (:points source-parent))))) attrs (geometry-differences source-objects kept source source)] diff --git a/common/src/app/common/test_helpers/composable/shadow.cljc b/common/src/app/common/test_helpers/composable/shadow.cljc index 32506ac0ac..c849b527c1 100644 --- a/common/src/app/common/test_helpers/composable/shadow.cljc +++ b/common/src/app/common/test_helpers/composable/shadow.cljc @@ -193,13 +193,13 @@ ;; a group or bool takes its corners from its children: at its ;; expected corners, its turn and flips say nothing more derived (and (#{:group :bool} (:type shape)) - (let [order (partial sort-by (juxt #(Math/round (* 100.0 (:x %))) - #(Math/round (* 100.0 (:y %)))))] - (gsh/close-attrs? :points (order (:points shape)) (order (:points expected)) 0.01))) + (or (nil? (:points expected)) + (gsh/close-attrs? :points (geom/ordered-corners (:points shape)) + (geom/ordered-corners (:points expected)) 0.01))) ;; an unskewed transform carries the turn and the mirror: an ;; expected rotation and flips match when they decompose it, ;; whichever rotation and flips the shape stores for it - oriented (when (and (not placed) (:transform shape) (geom/rigid? shape) + oriented (when (and (not placed) (not derived) (:transform shape) (geom/rigid? shape) (number? (:rotation expected)) (contains? expected :flip-x) (contains? expected :flip-y)) (geom/decomposes? (merge shape (select-keys expected [:rotation :flip-x :flip-y])))) @@ -215,9 +215,17 @@ (update :rotation + 180)) expected)] (and (if placed - ;; a size the layout gives keeps the first corner - (if (seq sized) + (cond + ;; a group or bool keeps its corners in any order + (#{:group :bool} (:type shape)) + (gsh/close-attrs? :points (geom/ordered-corners (:points shape)) + (geom/ordered-corners (:points placed)) 0.01) + + ;; a size the layout gives keeps the first corner + (seq sized) (gsh/close-attrs? :points [(first (:points shape))] [(first (:points placed))] 0.01) + + :else (gsh/close-attrs? :points (:points shape) (:points placed) 0.01)) true) (if (some? oriented) oriented true) @@ -678,6 +686,9 @@ (if (and (contains? groups :swap-group) (not (contains? groups :geometry-group)) (resized? id) + ;; a layout sizes and places its children, not their constraints + (not (and (ctl/any-layout-immediate-child? after shape) + (not (ctl/position-absolute? shape)))) (some? origin) (:shape-ref origin) (not (contains? (get model (:id origin)) :geometry-group))) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 28932efbce..65626fec29 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2535,6 +2535,44 @@ :layout-copies false :layout-children false}] :ops [[:transform 0 5 0 3] [:structure 0 5] [:update-main]]})))) +(t/deftest an-empty-bool-in-a-copy-keeps-its-corners-in-one-order + (t/is (map? (cg/run {:world [3 1 {:variants true :rich-variants true :text true :layout nil + :layout-copies true :layout-children false}] + :ops [[:add-child 7] [:structure 0 0 0] [:add-child 7] [:structure 3 4 3] [:add-child 0] + [:structure 3 0 0] [:add-child 7] [:structure 7 5 7]]})))) + +(t/deftest a-copy-bool-and-its-shapes-are-compared-by-their-corners + (t/is (map? (cg/run {:world [2 1 {:variants true :rich-variants true :text true :layout :flex + :layout-copies false :layout-children true}] + :ops [[:transform 6 17 0 9] [:add-child 16] [:structure] [:remove-child 11] [:structure 6 6] + [:update-main] [:transform 0 13 0 17]]})))) + +(t/deftest undoing-a-text-style-refits-a-flipped-group-to-the-same-corners + (t/is (map? (cg/run {:world [1 0 {:variants true :rich-variants true :text false :layout nil + :layout-copies false :layout-children false}] + :ops [[:delete] [:transform 0 22 0 18] [:structure 0 0 5] [:text-style 0 0 1]]})))) + +(t/deftest a-turned-copy-shape-in-a-group-orders-a-corner-level-with-its-centre-once + (t/is (map? (cg/run {:world [1 1 {:variants true :rich-variants true :text true :layout :grid + :layout-copies true :layout-children true}] + :ops [[:add-child] [:structure 11 4 5] [:swap 6] [:rotate] [:style 8 11]]})))) + +(t/deftest updating-a-main-from-a-shape-in-a-copy-bool-reads-the-bool-size + (t/is (map? (cg/run {:world [3 1 {:variants true :rich-variants false :text true :layout nil + :layout-copies false :layout-children false}] + :ops [[:add-child 5] [:remove-child] [:undo] [:add-child] [:structure 17] [:structure 16 7] + [:transform 18 0 0 17] [:update-main]]})))) + +(t/deftest a-bool-made-in-a-variant-main-orders-the-corners-of-its-turned-copies + (t/is (map? (cg/run {:world [3 0 {:variants true :rich-variants true :text true :layout nil + :layout-copies false :layout-children false}] + :ops [[:rotate 12] [:swap 2] [:swap 19] [:add-child] [:structure] [:structure 3 5 21]]})))) + +(t/deftest switching-a-head-a-grid-places-authors-no-size-when-the-grid-grows + (t/is (map? (cg/run {:world [1 1 {:variants true :rich-variants true :text true :layout :grid + :layout-copies true :layout-children false}] + :ops [[:style 8 29 1] [:nest 6] [:switch 9]]})))) + (t/deftest settling-a-bool-in-a-grid-copy-twice-keeps-its-shapes-in-place (t/is (map? (cg/run {:world [3 0 {:variants true :rich-variants true :text true :layout :grid :layout-copies true :layout-children true}]