From 1da982bbd7a6dbdafce82763870131d1ecbd8fc2 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sun, 8 Nov 2026 14:57:34 +0100 Subject: [PATCH] :bug: Compare a switched copy's child with its old main in one frame A turned copy holds a nested variant copy whose child is flipped. When the nested copy switches to a variant whose child agrees with the old one, the switch compared the old main's child with the new copy's child as they stand, and the copy's turn made their transforms disagree while their flips agreed. It carried the flip flag without the flipped transform and points, so the child stored a flip it did not show. Before carrying, the switch turns the old main's shapes as the copy turns its new main, so both variants are compared in one frame. Tests: test-switch-of-a-turned-copy-carries-the-flip-of-its-child and the regression program switching-a-turned-copy-keeps-the-flip-its-child-inherits. Full common suite: 2353 tests, 0 failures. Benchmark (scripts/bench-components, ms, lower 2.5% quantile): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 19.783 | 20.636 | 0.56 | | edit-through-a-chain-of-dependents | 68.931 | 42.981 | 44.139 | 0.62 | | library-update-into-copies | 83.083 | 49.497 | 51.013 | 0.6 | | main-edit-among-many-shapes | 4.837 | 2.119 | 2.197 | 0.44 | | main-edit-into-10-copies | 1.79 | 0.976 | 1.056 | 0.55 | | main-edit-into-100-copies | 15.415 | 9.563 | 9.784 | 0.62 | | main-edit-into-1000-copies | 153.086 | 95.722 | 103.356 | 0.63 | | variant-switch-into-copies | 32.052 | 21.712 | 22.187 | 0.68 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/variants.cljc | 13 +++++++++- .../logic/composable_gen_test.cljc | 7 +++++ .../logic/variants_switch_test.cljc | 26 +++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index d5bd2b1298..0bbcca249c 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -7,6 +7,7 @@ [app.common.files.variant :as cfv] [app.common.geom.point :as gpt] [app.common.geom.rect :as grc] + [app.common.geom.shapes :as gsh] [app.common.logic.duplicate :as cld] [app.common.logic.libraries :as cll] [app.common.logic.shapes :as cls] @@ -513,11 +514,21 @@ ;; A swapped child is kept as it is, and the others carry their ;; touched attributes into their new child + new-head (get objects (:id new-shape)) + new-base-ref-shape (ctf/find-remote-shape (assoc container :objects objects) libraries new-head) + ;; the old main's shapes turned as the copy turns its new main, so + ;; the switch compares them with the new copy's in one frame + orient-ref (or (when new-base-ref-shape + (cll/orient-like new-base-ref-shape new-head + (gsh/shape->center new-base-ref-shape))) + identity) + changes (reduce (fn [changes {:keys [old old-ref new swapped?]}] (if swapped? (keep-swapped-item changes new old ldata page libraries) - (clsw/update-attrs-on-switch changes new old new-shape original-shape old-ref container))) + (clsw/update-attrs-on-switch changes new old new-shape original-shape + (orient-ref old-ref) container))) changes pairs) diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 7616c042db..1efe6d3322 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1834,6 +1834,13 @@ :layout-children false :grid-empty false :layout-copies true :text true}] :ops [[:text 4 1 1 0] [:text-style 0 8 1] [:switch 1 0 0 0] [:switch 0 0 0 0]]})))) +(t/deftest switching-a-turned-copy-keeps-the-flip-its-child-inherits + (t/is (map? (cg/run {:world [3 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 [[:nest 17 0 0 0] [:duplicate 9 0 0 0] [:make-variant 21 0 0 0] + [:transform 6 16 0 18] [:rotate 15 0 0 0] [:switch 14 0 0 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_switch_test.cljc b/common/test/common_tests/logic/variants_switch_test.cljc index 9a3dca2fa5..af97ef4764 100644 --- a/common/test/common_tests/logic/variants_switch_test.cljc +++ b/common/test/common_tests/logic/variants_switch_test.cljc @@ -2394,6 +2394,32 @@ (t/is (= (:transform flipped) (:transform rect02'))) (t/is (gsh/close-attrs? :points (:points flipped) (:points rect02'))))) +(t/deftest test-switch-of-a-turned-copy-carries-the-flip-of-its-child + ;; the variants agree on the child, whose flip carries though the copy's + ;; turn sets its child apart from the main's + (let [file (-> (thf/sample-file :file1) + (thv/add-variant-with-child + :v01 :c01 :m01 :c02 :m02 :r01 :r02 + {:child1-params {:width 100 :height 50} + :child2-params {:width 100 :height 50}}) + (thc/instantiate-component :c01 :copy01 :children-labels [:copy-r01])) + update (fn [file ids f] + (let [page (thf/current-page file)] + (thf/apply-changes file (cls/generate-update-shapes (pcb/empty-changes nil (:id page)) + ids f (:objects page) {})))) + file (update file #{(thi/id :copy-r01)} + #(gsh/transform-shape % (ctm/resize-modifiers (gpt/point -1 1) (gsh/shape->center %)))) + center (gsh/shape->center (ths/get-shape file :copy01)) + file (update file #{(thi/id :copy01) (thi/id :copy-r01)} + #(gsh/transform-shape % (ctm/rotation-modifiers % center 45))) + turned (ths/get-shape file :copy-r01) + file' (tho/swap-component-in-shape file :copy01 :c02 {:new-shape-label :copy02 :keep-touched? true}) + rect02' (ths/get-shape-by-id file' (-> (ths/get-shape file' :copy02) :shapes first))] + (t/is (true? (:flip-x turned))) + (t/is (true? (:flip-x rect02'))) + (t/is (gsh/close-attrs? :transform (:transform turned) (:transform rect02'))) + (t/is (gsh/close-attrs? :points (:points turned) (:points rect02'))))) + (t/deftest test-switch-with-rotation-different-variants (let [;; ==== Setup file (-> (thf/sample-file :file1)