🐛 Sync a swapped head that its layout skews like the shape it replaces

A layout skews a turned child that fills along its parent's axes. A
swapped copy head keeps the lengths of its own component and turned them
to the skewed axes of the main shape it replaces, so the layout squashed
it into another shape than that main; each later sync started from the
squashed head and moved it again. A 100px square turned 45 degrees in a
100px high row gave a 141x100 rhombus for the main and an ordinary copy,
and 163x100, then 156x100, for the swapped copy.

A swapped head that its layout skews now takes the turn its rotation and
flips give, without the skew: its content main is oriented with that
turn, and its placement resets it from its own component and turns it
like the replaced shape's unskewed turn. The layout then skews it as it
skews the main.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):

|                             :world | :baseline |   :now |   :mean | :ratio |
|------------------------------------+-----------+--------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |  21.39 |   22.34 |   0.61 |
| edit-through-a-chain-of-dependents |    68.931 | 42.155 |  43.846 |   0.61 |
|         library-update-into-copies |    83.083 | 47.467 |   51.34 |   0.57 |
|        main-edit-among-many-shapes |     4.837 |  2.026 |   2.265 |   0.42 |
|           main-edit-into-10-copies |      1.79 |  1.025 |   1.065 |   0.57 |
|          main-edit-into-100-copies |    15.415 |  9.538 |  10.051 |   0.62 |
|         main-edit-into-1000-copies |   153.086 | 96.813 | 102.667 |   0.63 |
|         variant-switch-into-copies |    32.052 | 21.011 |  22.247 |   0.66 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-09 22:13:26 +01:00
parent 72304d023b
commit 777b489381
3 changed files with 102 additions and 9 deletions

View File

@ -105,6 +105,8 @@
(declare add-shape-to-instance)
(declare add-shape-to-main)
(declare axes-matrix)
(declare layout-skewed?)
(declare unskewed)
(declare remove-shape)
(declare move-shape)
(declare reorder-main-children)
@ -345,7 +347,10 @@
main-parent (get-in main-container [:objects (:parent-id shape-main)])
swapped? (and (not reset?) (ctk/swapped? shape-inst))
origin (if swapped? shape-main main-parent)
target (if swapped? shape-inst parent)
;; a head that the layout skews takes the turn it skews
target (if swapped?
(cond-> shape-inst (layout-skewed? (:objects container) shape-inst) (unskewed))
parent)
;; the axes only: a tiny shape's transform carries its rounding
relative (gmt/multiply (axes-matrix target) (gmt/inverse (axes-matrix origin)))]
(if (or (nil? target) (and (:shape-ref parent) (not reset?) (not swapped?))
@ -599,6 +604,37 @@
vertical (gpt/unit (gpt/point c d))]
(gmt/matrix (:x horizontal) (:y horizontal) (:x vertical) (:y vertical) 0 0)))
(defn- skewed?
"Whether the axes of the transform of `shape` are not perpendicular."
[shape]
(let [{:keys [a b c d]} (axes-matrix shape)]
(not (mth/close? (+ (* a c) (* b d)) 0 0.0001))))
(defn- unskewed
"`shape` with the transform that its rotation and flips give it, without
the skew a layout gives a turned child that it resizes along its parent's
axes. Of a turn before or after the flips, the one nearer its axes."
[shape]
(let [turn (gmt/rotate-matrix (d/nilv (:rotation shape) 0))
flips (gmt/scale-matrix (gpt/point (if (:flip-x shape) -1 1) (if (:flip-y shape) -1 1)))
axes (axes-matrix shape)
;; how close the axes of `m` are to those of the shape
closeness (fn [m] (+ (* (:a m) (:a axes)) (* (:b m) (:b axes))
(* (:c m) (:c axes)) (* (:d m) (:d axes))))
transform (max-key closeness (gmt/multiply turn flips) (gmt/multiply flips turn))]
(assoc shape :transform transform :transform-inverse (gmt/inverse transform))))
(defn- layout-skewed?
"Whether the layout of its parent skews the copy shape `shape` of
`objects`: it fills in a direction, it does not author its geometry, and
the layout resizes it along the parent's axes."
[objects shape]
(and (not (ctk/touched-group? shape :geometry-group))
(some #(= :fill (get shape %)) [:layout-item-h-sizing :layout-item-v-sizing])
(ctsl/any-layout-immediate-child? objects shape)
(not (ctsl/position-absolute? shape))
(skewed? shape)))
(defn- sync-descendant-orientation
"A parent turned or moved by sync carries its whole subtree, including
geometry overrides. Resize remains the job of constraints, not matrix
@ -1861,19 +1897,32 @@
"Synchronizes a swapped copy head with the main shape it replaces, in the
attributes that place it in its parent. A head with untouched geometry
that stops filling its parent in a direction is synced with its own
component, as the layout no longer sizes it."
component, as the layout no longer sizes it. One that fills where the
layout skews that shape takes the lengths of its own component and the
turn of that shape, which the layout then skews as it skewed the shape."
[changes {:keys [root-inst root-main container file libraries]} shape-inst shape-main]
(let [objects (:objects container)
changes (generate-take-placement changes
shape-inst
(reposition-shape shape-main root-main root-inst)
objects
(and (ctsl/any-layout-immediate-child? objects shape-inst)
(not (ctsl/position-absolute? shape-inst))))
placed (reposition-shape shape-main root-main root-inst)
sizings [:layout-item-h-sizing :layout-item-v-sizing]
;; whether the layout may have sized the head
filling? (and (not (ctk/touched-group? shape-inst :geometry-group))
(some #(= :fill (get shape-inst %)) sizings))]
(some #(= :fill (get shape-inst %)) sizings))
layout-placed? (and (ctsl/any-layout-immediate-child? objects shape-inst)
(not (ctsl/position-absolute? shape-inst)))
skew? (and filling? layout-placed? (skewed? placed))
changes (if skew?
(let [changes (pcb/apply-changes-local changes)]
(-> (generate-sync-shape-direct changes file libraries
(assoc container :objects (pcb/get-objects changes))
(:id shape-inst) false)
(pcb/apply-changes-local)))
changes)
objects (if skew? (pcb/get-objects changes) objects)
changes (generate-take-placement changes
(get objects (:id shape-inst))
(cond-> placed skew? (unskewed))
objects
layout-placed?)]
(if filling?
(let [changes (pcb/apply-changes-local changes)
objects (pcb/get-objects changes)

View File

@ -7,9 +7,13 @@
(ns common-tests.logic.comp-sync-layout-items-test
(:require
[app.common.files.changes-builder :as pcb]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.logic.libraries :as cll]
[app.common.test-helpers.components :as thc]
[app.common.test-helpers.composable.comp.nodes :as n]
[app.common.test-helpers.composable.comp.runner :as r]
[app.common.test-helpers.composable.core :as tm]
[app.common.test-helpers.compositions :as tho]
[app.common.test-helpers.files :as thf]
[app.common.test-helpers.ids-map :as thi]
@ -254,3 +258,34 @@
(t/is (= :fix (:layout-item-h-sizing copy-big')))
(t/is (== 140 (:width copy-big')))
(t/is (== 140 (-> copy-big' :selrect :width)))))
(t/deftest swapped-child-turned-to-fill-takes-the-skew-of-its-main
;; the main turns a child that fills the height of its row, and the layout
;; skews it: a swapped copy of it in an identical row shows the same shape
(let [file (-> (thf/sample-file :file1)
(tho/add-frame :frame-a :width 100 :height 100)
(thc/make-component :a :frame-a)
(tho/add-frame :frame-b :width 100 :height 100)
(thc/make-component :b :frame-b)
(tho/add-frame :row-main {:layout :flex :layout-flex-dir :row :width 100 :height 100})
(thc/instantiate-component :a :nested :parent-label :row-main)
(tho/change-shape :nested #(assoc % :layout-item-v-sizing :fill))
(thc/make-component :row :row-main)
(thc/instantiate-component :row :plain :children-labels [:plain-nested])
(thc/instantiate-component :row :swapped :children-labels [:swapped-nested])
(tho/swap-component-in-shape :swapped-nested :b :new-shape-label :swapped-b))
settled (-> (tm/make-situation file)
(r/step (n/rotate :nested 45))
r/settle)
resynced (-> (tm/file settled)
(as-> file (:file (r/sync-cascade file {} (keys (get-in file [:data :components])))))
(tm/make-situation)
r/settle-layout)
;; the corners of the child of the row `row`, from the row's corner
corners (fn [situation row child]
(let [origin (-> (tm/resolve-shape situation row) :points first)]
(mapv #(gpt/subtract % origin) (:points (tm/resolve-shape situation child)))))]
(t/is (gsh/close-attrs? :points (corners settled :plain :plain-nested)
(corners settled :swapped :swapped-b) 0.01))
(t/is (gsh/close-attrs? :points (corners settled :swapped :swapped-b)
(corners resynced :swapped :swapped-b) 0.01))))

View File

@ -1971,6 +1971,15 @@
:grid-empty false :layout-copies true :text true}]
:ops [[:nest 7 0 0 0] [:transform 3 3 0 0] [:switch 1 0 0 0] [:style 3 29 1]]}))))
(t/deftest a-swapped-copy-turned-to-fill-a-row-settles-like-its-main
(t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :flex :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 [[:delete 0] [:restore 0] [:add-variant 0 0 0 0] [:swap 6 0 0 0] [:combine 5 0 0 0]
[:delete 12 0 0 0] [:combine 9 0 0 0] [:nest 0 0 0 0] [:restore 0 0 0 0]
[:combine 8] [:switch 3] [:style 0 30 1] [:detach 1 0 0 0] [:combine 0]
[:switch 7] [:nest 4 0 0 0] [:rotate 2 0 0 0] [:combine 0] [:switch 0]]}))))
(t/deftest switching-a-grid-copy-places-its-kept-swap-in-a-cell
(t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :grid :variant-diff false
:layout-item-margins false :grid-square false :rich-variants true