From 7bb0970511c2023229d11c88abe3a1522adbb2fd Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Fri, 6 Nov 2026 05:22:14 +0100 Subject: [PATCH] :bug: Store the turn a transform decomposes into when a turn flips it Turning a subtree to match another orientation stores, for each shape, its old rotation plus the turn, or minus it when only that one fits its flips. A turn that also drops or adds a flip leaves neither fitting: a switch from an unflipped variant to a flipped one kept the copy's unflipped turn in the transform but stored the flipped main's rotation. When neither fits, the shape stores the rotation its transform decomposes into with its flips. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | world | baseline | now | ratio | |------------------------------------+----------+---------+-------| | deepest-edit-through-three-levels | 35.215 | 19.641 | 0.56 | | edit-through-a-chain-of-dependents | 68.931 | 38.794 | 0.56 | | library-update-into-copies | 83.083 | 46.743 | 0.56 | | main-edit-among-many-shapes | 4.837 | 2.355 | 0.49 | | main-edit-into-10-copies | 1.79 | 0.997 | 0.56 | | main-edit-into-100-copies | 15.415 | 9.669 | 0.63 | | main-edit-into-1000-copies | 153.086 | 97.248 | 0.64 | | variant-switch-into-copies | 32.052 | 21.73 | 0.68 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/libraries.cljc | 14 +++++++++++--- .../common_tests/logic/composable_gen_test.cljc | 10 ++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 8884f512dc..527e782ced 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -2104,7 +2104,8 @@ "The rotation that `shape`, just turned by `angle` with its subtree, stores: `rotation` plus `angle`, or minus it when only that one decomposes its transform with its flips. A mirror that flips after its turn turns the - other way." + other way. When neither does, as when the turn changes its flips, the + rotation its transform decomposes into." [shape rotation angle] (let [{:keys [a b c d]} (or (:transform shape) (gmt/matrix)) mirrored (not= (boolean (:flip-x shape)) (boolean (:flip-y shape))) @@ -2117,9 +2118,16 @@ (and (:flip-x shape) (:flip-y shape) (same? value (+ turn 180))))) plus (mod (+ rotation angle) 360) minus (mod (- rotation angle) 360)] - (if (and (= mirrored (neg? (- (* a d) (* b c)))) - (not (stores? plus)) (stores? minus)) + (cond + (and (= mirrored (neg? (- (* a d) (* b c)))) + (not (stores? plus)) (stores? minus)) minus + + ;; a turn that drops or adds a flip leaves neither; the transform decides + (and (not (stores? plus)) (not (stores? minus))) + (mod turn 360) + + :else plus))) (defn orient-like diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 78f9c420d6..2e55f5d5a1 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1230,3 +1230,13 @@ [:swap 2 0 0 0] [:duplicate 4 0 0 0] [:delete 8] [:restore 8] [:undo 0 0 0 0] [:text-style 4 0 1] [:delete 0 0 0 0] [:nest 21 0 0 0] [:duplicate 0 0 0 0] [:swap 16 0 0 0] [:switch 0 0 0 0]]})))) + +(t/deftest generated-switch-to-a-flipped-variant-stores-the-turn-its-transform-has + ;; a switch to a variant whose main is flipped keeps the copy's unflipped + ;; turn; the new head stores the rotation its transform decomposes into + (t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library-variants true :stale-libraries true + :layout nil :layout-item-margins false :grid-square false :library-depth 2 + :library true :rich-variants true :layout-children false :library-text true + :grid-empty false :layout-copies true :text true}] + :ops [[:library-transform 9 3 4 0] [:delete 5] [:restore 5] [:library-transform 9 4 0 0] + [:library-transform 4 3 0 0] [:library-sync 0 0 0 0] [:switch 13 0 0 0]]}))))