🐛 Keep turned copies and swaps at their first corner

A sync lined up a copy with its main by their untransformed top-left.
Once the main is turned or flipped, a width change moves that point, so
every copy that follows the main drifted about 7 px off the corner the
resize keeps, and a child it overrides left its place. A swapped head of
another size than its position did the same when its board turned: it
was lined up by the untransformed boxes and turned about its own centre.

An untouched copy root turned or flipped like its main now lines up
with it at their first corner, and a swapped head turned like its
position takes its place at that corner, so both turn rigidly with
their board. The oracle resizes an expected turned head about its first
corner in the same way.

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

| world                              | baseline |    now | ratio |
|------------------------------------+----------+--------+-------|
| deepest-edit-through-three-levels  |   35.215 | 18.808 |  0.53 |
| edit-through-a-chain-of-dependents |   68.931 | 38.155 |  0.55 |
| library-update-into-copies         |   83.083 | 46.798 |  0.56 |
| main-edit-among-many-shapes        |    4.837 |  2.193 |  0.45 |
| main-edit-into-10-copies           |     1.79 |  0.962 |  0.54 |
| main-edit-into-100-copies          |   15.415 |  9.315 |  0.60 |
| main-edit-into-1000-copies         |  153.086 | 93.184 |  0.61 |
| variant-switch-into-copies         |   32.052 |  20.55 |  0.64 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-01 17:48:14 +01:00
parent e10ce2dba9
commit 79b909a0bd
3 changed files with 68 additions and 10 deletions

View File

@ -1670,15 +1670,21 @@
{}
placement-attrs)
orient (when (untouched? :rotation)
(orient-like shape-inst placed (gsh/shape->center shape-inst)))
oriented (cond-> shape-inst (some? orient) (orient))
;; turned alike, both take their place at their first corner
turned? (and (some? (:transform placed))
(not (gmt/unit? (:transform placed)))
(gmt/close? (:transform placed) (or (:transform oriented) (gmt/matrix))))
position (fn [shape]
(gpt/point (dm/get-in shape [:selrect :x])
(dm/get-in shape [:selrect :y])))
delta (gpt/subtract (position placed) (position shape-inst))
(or (when turned? (first (:points shape)))
(gpt/point (dm/get-in shape [:selrect :x])
(dm/get-in shape [:selrect :y]))))
delta (gpt/subtract (position placed) (position oriented))
move? (and (untouched? :x)
(not layout-placed?)
(not (gpt/almost-zero? delta)))
orient (when (untouched? :rotation)
(orient-like shape-inst placed (gsh/shape->center shape-inst)))]
(not (gpt/almost-zero? delta)))]
(cond-> changes
(seq placement)
(pcb/update-shapes [(:id shape-inst)]
@ -1878,22 +1884,32 @@
translation by the roots' (untransformed) position delta suffices — position is
free per-instance placement.
A turned or flipped instance lines up with the origin at their first
corner, the one a resize keeps, so it follows a resize in its own frame.
If the dest root's geometry IS overridden (e.g. the user rotated the copy as a
whole), the instance keeps its own placement transform, so the origin shape is
additionally transformed by the roots' relative transformation (rotation /
flips) around the dest root center — geometric changes then land expressed in
the dest instance's own frame instead of wiping its placement."
[shape origin-root dest-root]
(let [shape-pos (fn [shape]
(gpt/point (dm/get-in shape [:selrect :x])
(dm/get-in shape [:selrect :y])))
(let [touched? (ctk/touched-group? dest-root :geometry-group)
oriented? (and (not touched?)
(some? (:transform dest-root))
(not (gmt/unit? (:transform dest-root)))
(gmt/close? (or (:transform origin-root) (gmt/matrix))
(:transform dest-root)))
shape-pos (fn [shape]
(or (when oriented? (first (:points shape)))
(gpt/point (dm/get-in shape [:selrect :x])
(dm/get-in shape [:selrect :y]))))
origin-root-pos (shape-pos origin-root)
dest-root-pos (shape-pos dest-root)
delta (gpt/subtract dest-root-pos origin-root-pos)
shape (gsh/move shape delta)]
(if-not (ctk/touched-group? dest-root :geometry-group)
(if-not touched?
shape
(if-let [turn (orient-like origin-root dest-root
(grc/rect->center (:selrect dest-root)))]

View File

@ -459,3 +459,28 @@
(t/is (mth/close? 80 (:height after)))
(t/is (mth/close? 100 (:width after)))
(t/is (not (contains? (:touched after) :geometry-group)))))
(t/deftest a-resized-turned-main-keeps-its-copies-at-their-first-corner
;; a width change keeps a shape's first corner; a copy oriented like its
;; main keeps its own first corner, whether the turn or flip is the main's
;; or a parent's
(doseq [case [:turned :flipped :parent-turned]]
(let [file (-> (thf/sample-file :file)
(tho/add-simple-component :a :main-a :rect-a :root-params {:width 100 :height 100})
(cond-> (= case :parent-turned)
(-> (tho/add-frame :main-b {:width 200 :height 200})
(thc/instantiate-component :a :b-a :parent-label :main-b)
(thc/make-component :b :main-b))))
before (-> (tm/make-situation file)
(r/step (n/rotate (if (= case :parent-turned) :main-b :main-a) 45)) r/settle
(cond-> (= case :flipped) (-> (r/step (n/transform-shape :main-a :flip-x nil)) r/settle)))
before (r/settle (tm/with-file before
(thc/instantiate-component (tm/file before) (if (= case :parent-turned) :b :a)
:copy :children-labels [:copy-a])))
label (if (= case :parent-turned) :copy-a :copy)
after (-> before (r/step (n/transform-shape :main-a :width 80)) r/settle)
copy (ths/get-shape (tm/file after) label)]
(t/is (mth/close? 80 (:width copy) 0.001) (str case))
(t/is (gpt/close? (first (:points (ths/get-shape (tm/file before) label)))
(first (:points copy)))
(str case)))))

View File

@ -925,3 +925,20 @@
(t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout :grid :rich-variants true
:layout-children true :grid-empty true :layout-copies true :text true}]
:ops [[:style 4 29 1] [:detach 0 0 0 0] [:rotate 5 0 0 0]]}))))
(t/deftest generated-resized-main-keeps-a-turned-copy-at-its-first-corner
;; a main is narrowed while a copy of it is turned by a detached parent;
;; the copy keeps its first corner, and a child it overrides keeps its
;; place against it
(t/is (map? (cg/run {:world [2 2 {:text true :text-styles true}]
:ops [[:detach 6 0 0 0] [:delete 0] [:restore 0] [:duplicate 2 0 0 0] [:edit 0 0 0 0]
[:transform 8 0 0 2] [:delete 0 0 0 0] [:rotate 8 0 0 0] [:delete 0] [:restore 0]
[:edit 7 0 1 1] [:update-main 7] [:transform 7 7 0 0]]}))))
(t/deftest generated-turned-swap-of-another-size-keeps-the-corner-of-its-position
;; a nested head switched to a wider variant is turned 45 degrees with the
;; chain of copies it is in; it keeps the corner of its narrower position,
;; and a reset lands on it
(t/is (map? (cg/run {:world [2 0 {:variants true :text-styles true :library true :rich-variants true
:layout-copies true :text true}]
:ops [[:switch 6 0 0 0] [:rotate 0 0 0 0] [:reset 5 0 0 0]]}))))