diff --git a/common/src/app/common/logic/swap.cljc b/common/src/app/common/logic/swap.cljc index ec3c8fff9a..3bf1217733 100644 --- a/common/src/app/common/logic/swap.cljc +++ b/common/src/app/common/logic/swap.cljc @@ -332,18 +332,28 @@ "Give the new head of a swap, and its subtree, the flips and the rotation of the shape it replaces, around the head's own center. They come from its parent, as when the parent is rotated, and are not overrides. The - turned head takes the place of the old one at their first corner, so it - turns rigidly with its parent." + turned or flipped head takes the place of the old one at their first + corner, so it turns rigidly with its parent, also when it already has + the old one's orientation." [changes new-shape shape] - (if-let [orient (cll/orient-like new-shape shape (gsh/shape->center new-shape))] - (let [ids (cfh/get-children-ids-with-self (pcb/get-objects changes) (:id new-shape)) - delta (gpt/subtract (first (:points shape)) (first (:points (orient new-shape))))] + (let [orient (cll/orient-like new-shape shape (gsh/shape->center new-shape)) + ids (cfh/get-children-ids-with-self (pcb/get-objects changes) (:id new-shape)) + delta (gpt/subtract (first (:points shape)) + (first (:points (cond-> new-shape (some? orient) (orient)))))] + (cond + (some? orient) (pcb/update-shapes changes ids (cond->> orient (not (gpt/almost-zero? delta)) (comp #(gsh/move % delta))) - {:ignore-touched true})) - changes)) + {:ignore-touched true}) + + (and (not (gmt/unit? (or (:transform shape) (gmt/matrix)))) + (not (gpt/almost-zero? delta))) + (pcb/update-shapes changes ids #(gsh/move % delta) {:ignore-touched true}) + + :else + changes))) (defn- generate-swap-in-copy "A swap inside a copy keeps the position: the new head takes the place of diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 1a50c9db95..1ac612eba3 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1160,3 +1160,12 @@ :layout-item-margins false :grid-square false :grid-empty false}] :ops [[:delete 0] [:restore 0] [:undo 0 0 0 0] [:delete 11] [:restore 11] [:delete 1 0 0 0] [:delete 3 0 0 0] [:rotate 4 0 0 0] [:detach 11 0 0 0]]})))) + +(t/deftest generated-switch-keeps-a-flipped-heads-first-corner + ;; both variant mains are flipped; a flipped nested copy switches to the + ;; wider variant and keeps its first corner, its top right + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:transform 1 4 0 0] [:delete 5 0 0 0] [:delete 0] [:restore 0] + [:transform 1 4 0 0] [:switch 0 0 0 0]]}))))