From 17539d9f0e84974cb385da0e32cea6a562733007 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Thu, 12 Nov 2026 12:16:29 +0100 Subject: [PATCH] :bug: Keep as an override the place a copy shape's old constraint gave it A constraint applies to the resizes after it, so changing the constraint of a copy shape moves nothing. But a later sync placed the shape from its main through the new constraint, as if it had applied to the copy's earlier resize too, so the same copy showed two places depending on the sync. A copy shape whose constraint changes in a board sized apart from its main's along that axis now authors its geometry: it keeps its place, and later resizes of the board move it through the new constraint. The frontend's shape update runs the check after any update that marks overrides. Tests: centring-a-nested-copy-after-its-copy-resized-keeps-its-children-in-place (the stale gate program of seed 151006440); a-constraint-a-copy-sets-after-a-resize-moves-nothing-at-sync expects the override now. Benchmark (scripts/bench-components, ms, lower 2.5% quantile), measured with the previous commit's change in the tree as well: | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 21.443 | 22.755 | 0.61 | | edit-through-a-chain-of-dependents | 68.931 | 40.138 | 42.216 | 0.58 | | library-update-into-copies | 83.083 | 51.127 | 55.671 | 0.62 | | main-edit-among-many-shapes | 4.837 | 2.516 | 3.029 | 0.52 | | main-edit-into-10-copies | 1.79 | 1.11 | 1.148 | 0.62 | | main-edit-into-100-copies | 15.415 | 9.991 | 10.514 | 0.65 | | main-edit-into-1000-copies | 153.086 | 97.388 | 114.808 | 0.64 | | variant-switch-into-copies | 32.052 | 21.907 | 23.988 | 0.68 | AI-assisted-by: claude-opus-5-5 --- common/src/app/common/logic/libraries.cljc | 32 +++++++++++++++++++ .../test_helpers/composable/comp/nodes.cljc | 6 ++++ .../common/test_helpers/composable/gen.cljc | 18 +++++++++++ .../logic/comp_expected_geometry_test.cljc | 4 +-- .../logic/composable_gen_test.cljc | 7 ++++ .../src/app/main/data/workspace/shapes.cljs | 6 ++++ 6 files changed, 71 insertions(+), 2 deletions(-) diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index acafb8fb99..bb7a9e5c8e 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -863,6 +863,38 @@ (contains? (ctn/held-component-ids (cfh/get-children (:objects container) shape-id) libraries) component-id))) +(defn generate-author-constraint-places + "A copy shape of `ids` whose constraint an update changed, from the shapes + `before` to those of `changes`, keeps its place as an override where its + board has another size than its main's along the changed axis: its place + came through the old constraint, and the new one applies only to the + resizes after it. A sync would otherwise place it through the new one." + [changes container libraries before ids] + (let [after (pcb/get-objects changes) + authored + (filter + (fn [id] + (let [old (get before id) + shape (get after id) + frame (get after (:frame-id shape)) + h? (not= (:constraints-h old) (:constraints-h shape)) + v? (not= (:constraints-v old) (:constraints-v shape))] + (when (and (or h? v?) + (:shape-ref shape) + (:shape-ref frame) + (not (ctk/touched-group? shape :geometry-group)) + (not (and (ctsl/any-layout-immediate-child? after shape) + (not (ctsl/position-absolute? shape))))) + (when-let [main-frame (ctf/find-ref-shape nil (assoc container :objects after) libraries frame + :include-deleted? true)] + (or (and h? (not (mth/close? (:width frame) (:width main-frame) 0.01))) + (and v? (not (mth/close? (:height frame) (:height main-frame) 0.01)))))))) + ids)] + (cond-> changes + (seq authored) + (pcb/update-shapes authored #(update % :touched ctk/set-touched-group :geometry-group) + {:ignore-touched true})))) + (defn generate-sync-shape-inverse "Generate changes to update the component a shape is linked to, from the values in the shape and all its children. An update that would nest diff --git a/common/src/app/common/test_helpers/composable/comp/nodes.cljc b/common/src/app/common/test_helpers/composable/comp/nodes.cljc index 033c864507..5d5ece463c 100644 --- a/common/src/app/common/test_helpers/composable/comp/nodes.cljc +++ b/common/src/app/common/test_helpers/composable/comp/nodes.cljc @@ -221,6 +221,12 @@ (fn [shape] (set-property shape property value')) (:objects page) {}) + ;; as the frontend's update-shapes: a copy shape keeps the place + ;; its old constraint gave it + :always + (cll/generate-author-constraint-places (ctn/make-container page :page) + (cf/libraries situation) + (:objects page) #{shape-id}) (and (= :layout-item-absolute property) (ctl/grid-layout? (get-in page [:objects layout-id]))) (pcb/update-shapes [layout-id] ctl/assign-cells diff --git a/common/src/app/common/test_helpers/composable/gen.cljc b/common/src/app/common/test_helpers/composable/gen.cljc index a0d39c84e8..6bd624f363 100644 --- a/common/src/app/common/test_helpers/composable/gen.cljc +++ b/common/src/app/common/test_helpers/composable/gen.cljc @@ -21,6 +21,7 @@ [app.common.logic.placement :as clp] [app.common.logic.shapes :as cls] [app.common.logic.variants :as clv] + [app.common.math :as mth] [app.common.schema.test :as smt] [app.common.test-helpers.components :as thc] [app.common.test-helpers.composable.comp.files :as cf] @@ -1014,6 +1015,23 @@ (:shape-ref shape) (not (:layout-item-absolute shape))) (assoc-in [id :geometry-group] (shadow/author-geometry (get (all-objects after) id shape) nil)) + ;; a copy shape keeps the place its old constraint gave it in a board + ;; whose size differs from its main's along that axis + (and (#{:constraints-h :constraints-v} property) + (:shape-ref shape) + (not (get-in model [id :geometry-group])) + (not= (get shape property) (get (get (all-objects after) id) property)) + (not (and (ctl/any-layout-immediate-child? objects shape) + (not (ctl/position-absolute? shape)))) + (let [frame (get objects (:frame-id shape)) + main-frame (get objects (:shape-ref frame)) + size (if (= property :constraints-h) :width :height)] + (and (:shape-ref frame) main-frame + (not (mth/close? (get frame size) (get main-frame size) 0.01))))) + (assoc-in [id :geometry-group] + (shadow/author-geometry (get (all-objects after) id shape) + (get (all-objects after) (:frame-id shape)))) + ;; a constraint resizes the shape only with its parent's later resizes (and (#{:constraints-h :constraints-v} property) (get-in model [id :geometry-group])) (update-in [id :geometry-group] shadow/anchor-position (get (all-objects after) id shape) diff --git a/common/test/common_tests/logic/comp_expected_geometry_test.cljc b/common/test/common_tests/logic/comp_expected_geometry_test.cljc index 923805a955..0ba2a39617 100644 --- a/common/test/common_tests/logic/comp_expected_geometry_test.cljc +++ b/common/test/common_tests/logic/comp_expected_geometry_test.cljc @@ -517,7 +517,7 @@ (t/deftest a-constraint-a-copy-sets-after-a-resize-moves-nothing-at-sync ;; A constraint the copy sets on a child applies to later resizes, so the - ;; next sync keeps the child as the earlier resize left it. + ;; child keeps the place the earlier resize left it, as an override. (let [file (-> (thf/sample-file :file1) (tho/add-simple-component :c :c-root :c-child :root-params {:width 100 :height 100} @@ -532,7 +532,7 @@ (t/is (mth/close? 20 (:height (tm/resolve-shape resized :copy-child)))) (t/is (= "#dc2626" (get-in child [:fills 0 :fill-color]))) (t/is (mth/close? 20 (:height child))) - (t/is (not (contains? (:touched child) :geometry-group))))) + (t/is (contains? (:touched child) :geometry-group)))) (t/deftest generated-constraint-a-copy-sets-after-a-resize-moves-nothing ;; a copy resized from its 0.01 main sets a child's vertical constraint to diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 5f8f6b0f12..292d776ab9 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -2353,6 +2353,13 @@ (t/is (empty? (cg/coverage-errors @coverage cg/variant-coverage-kinds)) (pr-str (cg/coverage-errors @coverage cg/variant-coverage-kinds))))) +(t/deftest centring-a-nested-copy-after-its-copy-resized-keeps-its-children-in-place + (t/is (map? (cg/run {:world [1 0 {:variants true :text-styles false :library-variants false :stale-libraries false + :layout :grid :layout-item-margins false :grid-square false :library-depth 1 + :library false :rich-variants true :layout-children false :library-text false + :grid-empty false :layout-copies false :text false}] + :ops [[:nest 4] [:resize 1] [:style 15 9 3]]})))) + (t/deftest unmasking-in-a-flex-main-leaves-its-layout-as-the-unmask-event-does (t/is (map? (cg/run {:world [2 0 {:variants false :rich-variants false :text true :layout :flex :layout-copies false :layout-children true}] diff --git a/frontend/src/app/main/data/workspace/shapes.cljs b/frontend/src/app/main/data/workspace/shapes.cljs index 32c0fbc856..c611b0eef8 100644 --- a/frontend/src/app/main/data/workspace/shapes.cljs +++ b/frontend/src/app/main/data/workspace/shapes.cljs @@ -254,6 +254,12 @@ :with-objects? with-objects? :translation? translation? :skip-grid-reassignment? skip-grid-reassignment?}) + ;; a copy shape keeps the place its old constraint gave it + (cond-> (not ignore-touched) + (cll/generate-author-constraint-places + (ctn/make-container (dsh/lookup-page state page-id) :page) + (dsh/lookup-libraries state) + objects ids)) (cond-> undo-group (pcb/set-undo-group undo-group)) (pcb/set-translation? translation?)