🐛 Keep radius tokens on the corners a mirrored copy shows

A radius token names a corner as its shape shows it, but a copy mirrored
unlike its main inherited the main's corners as they were. A token value
edit then rounded the corner the main keeps square: the copy's tokens
named its other corners.

A flip now moves a shape's radius tokens with the corners they apply
to, and a transform writes them without making them an override. Sync,
reset, update main and a switch carry tokens into the orientation the
destination ends with (`ctsr/tokens-in-orientation`,
`cll/oriented-by`). The composable oracle compares a copy's radius
group with its main's in the copy's orientation.

Tests: `token-apply-test/a-flip-moves-a-radius-token-with-its-corner`,
generated `a-token-edit-keeps-the-square-corner-of-a-mirrored-copy`.

Benchmark (bench-components, alone):

|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |  22.352 |  23.145 |   0.63 |
| edit-through-a-chain-of-dependents |    68.931 |  44.184 |  45.036 |   0.64 |
|         library-update-into-copies |    83.083 |  52.481 |  54.229 |   0.63 |
|        main-edit-among-many-shapes |     4.837 |   2.354 |   2.498 |   0.49 |
|           main-edit-into-10-copies |      1.79 |   1.031 |     1.1 |   0.58 |
|          main-edit-into-100-copies |    15.415 |  10.729 |  10.946 |    0.7 |
|         main-edit-into-1000-copies |   153.086 | 103.496 | 105.968 |   0.68 |
|         variant-switch-into-copies |    32.052 |  23.207 |    23.7 |   0.72 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-15 20:42:16 +01:00
parent 4b404af324
commit 645223d90c
9 changed files with 112 additions and 20 deletions

View File

@ -15,7 +15,8 @@
[app.common.geom.shapes.common :as gco]
[app.common.math :as mth]
[app.common.types.modifiers :as ctm]
[app.common.types.path :as path]))
[app.common.types.path :as path]
[app.common.types.shape.radius :as ctsr]))
#?(:clj (set! *warn-on-reflection* true))
@ -343,6 +344,10 @@
flip-y?
(update :flip-y not)
;; the radius tokens keep to the corners they apply to
(and (or flip-x? flip-y?) (seq (:applied-tokens shape)))
(update :applied-tokens ctsr/mirror-token-corners flip-x? flip-y?)
;; Negate rotation only when an odd number of axes are flipped,
;; since flipping both axes is equivalent to a 180° rotation and
;; two negations would cancel each other out.

View File

@ -35,6 +35,7 @@
[app.common.types.shape :as cts]
[app.common.types.shape-tree :as ctst]
[app.common.types.shape.layout :as ctsl]
[app.common.types.shape.radius :as ctsr]
[app.common.types.text :as txt]
[app.common.types.token :as cto]
[app.common.types.typography :as cty]
@ -1802,13 +1803,22 @@
[(set-remote-synced remote-synced?)]
[(set-remote-synced (:remote-synced shape))])))))
(defn oriented-by
"`shape` with the flips the `:set` operations `roperations` leave on it."
[shape roperations]
(reduce (fn [shape {:keys [type attr val]}]
(cond-> shape (and (= :set type) (#{:flip-x :flip-y} attr)) (assoc attr val)))
shape roperations))
(defn- update-tokens
"Token synchronization algorithm. Copy the applied tokens that have changed
in the origin shape to the dest shape (applying or removing as necessary).
Only the given token attributes are synced."
[changes container dest-shape orig-shape token-attrs]
(let [orig-tokens (get orig-shape :applied-tokens {})
Only the given token attributes are synced. A radius token names a corner
as its shape shows it, so the tokens of both shapes take the orientation
of `oriented`, the dest shape with the flips the sync leaves on it."
[changes container dest-shape orig-shape token-attrs oriented]
(let [orig-tokens (ctsr/tokens-in-orientation (get orig-shape :applied-tokens {}) orig-shape oriented)
dest-tokens (get dest-shape :applied-tokens {})
dest-tokens' (reduce (fn [dest-tokens' token-attr]
(let [orig-token (get orig-tokens token-attr)
@ -1818,7 +1828,7 @@
(if (nil? orig-token)
(dissoc dest-tokens' token-attr)
(assoc dest-tokens' token-attr orig-token)))))
dest-tokens
(ctsr/tokens-in-orientation dest-tokens dest-shape oriented)
token-attrs)
set-tokens (fn [tokens]
{:type :mod-obj
@ -1845,8 +1855,9 @@
"Copy into `dest-shape` the tokens `origin-shape` applies to the attributes
it syncs: with `omit-touched?`, only those whose group is not in `touched`;
with `valid-attrs`, only those attributes (all when nil). Nothing to copy
or remove when neither shape applies tokens."
[changes container dest-shape origin-shape touched omit-touched? valid-attrs]
or remove when neither shape applies tokens. `oriented` is the dest shape
with the flips the sync leaves on it."
[changes container dest-shape origin-shape touched omit-touched? valid-attrs oriented]
(if (and (empty? (:applied-tokens origin-shape))
(empty? (:applied-tokens dest-shape)))
changes
@ -1869,7 +1880,7 @@
token-attrs-by-sync-attr)]
(cond-> changes
(seq applied-tokens)
(update-tokens container dest-shape origin-shape applied-tokens)))))
(update-tokens container dest-shape origin-shape applied-tokens oriented)))))
(def ^:private derived-geometry-attrs
"The geometry a group or bool takes from its children."
@ -2118,7 +2129,9 @@
[roperations uoperations]
(attr-operations dest-shape
#(when-not (and content-only? (contains? placement-attrs %))
(sync-attr-operations dest-shape origin-shape touched omit-touched? %)))]
(sync-attr-operations dest-shape origin-shape touched omit-touched? %)))
oriented (oriented-by dest-shape roperations)]
(cond-> changes
(seq roperations)
@ -2128,7 +2141,7 @@
:always
(check-swapped-main dest-shape origin-shape)
:always
(generate-update-tokens container dest-shape origin-shape touched omit-touched? valid-attrs))))
(generate-update-tokens container dest-shape origin-shape touched omit-touched? valid-attrs oriented))))
(defn- propagate-attrs
"Helper that puts the origin attributes (attrs) into dest but only if

View File

@ -64,7 +64,9 @@
:layout-grid-columns
:layout-grid-rows
:fills
:fill-image})
:fill-image
;; a flip moves the radius tokens with their corners
:applied-tokens})
(defn- check-delta
"If the shape is a component instance, check whether the transformation is a

View File

@ -110,9 +110,14 @@
(fn [changes id]
;; a shape the transform leaves untouched changes only
;; as a consequence of it: no attribute touches
(let [opts {:attrs attrs
(let [ignore (or (true? (get ignore-tree id)) ignore-touched)
opts {:attrs attrs
:ignore-geometry? (get ignore-tree id)
:ignore-touched (or (true? (get ignore-tree id)) ignore-touched)
;; the radius tokens a flip moves with their
;; corners are no override
:ignore-touched (if (and (not ignore) (contains? attrs :applied-tokens))
#{:applied-tokens}
ignore)
:with-objects? with-objects?}]
(pcb/update-shapes changes [id] update-fn (d/without-nils opts))))
(cond-> changes

View File

@ -410,7 +410,8 @@
(cond-> changes
(> (count roperations) 1)
(-> (cll/add-update-attr-changes current-shape container roperations uoperations)
(cll/generate-update-tokens container current-shape previous-shape touched false updated-attrs)))))
(cll/generate-update-tokens container current-shape previous-shape touched false updated-attrs
(cll/oriented-by current-shape roperations))))))
(defn- generate-keep-orientation
"Give the new head of a swap, and its subtree, the flips and the rotation

View File

@ -29,6 +29,7 @@
[app.common.types.file :as ctf]
[app.common.types.modifiers :as ctm]
[app.common.types.shape.layout :as ctl]
[app.common.types.shape.radius :as ctsr]
[app.common.types.text :as txt]
[clojure.walk :as walk]))
@ -649,11 +650,18 @@
:shape (:name actual) :expected expected :actual actual})))
(defn- comparable-values
"The values of `group` in `shape`, as the shape `context` would hold them:
radius tokens name the corners as `context` shows them."
[shape context group]
(let [shape (if (= group :constraints-group)
(let [shape (case group
:constraints-group
(cond-> (update shape :fixed-scroll boolean)
(nil? (:constraints-h shape)) (assoc :constraints-h (gsh/default-constraints-h context))
(nil? (:constraints-v shape)) (assoc :constraints-v (gsh/default-constraints-v context)))
:radius-group
(update shape :applied-tokens ctsr/tokens-in-orientation shape context)
shape)]
(shadow/values shape group)))

View File

@ -44,16 +44,44 @@
(can-get-border-radius? shape)
(assoc :r1 value :r2 value :r3 value :r4 value)))
(defn mirror-corner
"The corner `attr` mirrored along the axes `flip-x?` and `flip-y?` name."
[attr flip-x? flip-y?]
(cond->> attr
flip-x?
(get {:r1 :r2 :r2 :r1 :r3 :r4 :r4 :r3})
flip-y?
(get {:r1 :r4 :r2 :r3 :r3 :r2 :r4 :r1})))
(defn shown-corner
"The corner `attr` of `shape` as the shape shows it, mirrored by its flips;
the same mirroring takes a shown corner to the one it stores."
[shape attr]
(cond->> attr
(:flip-x shape)
(get {:r1 :r2 :r2 :r1 :r3 :r4 :r4 :r3})
(mirror-corner attr (:flip-x shape) (:flip-y shape)))
(:flip-y shape)
(get {:r1 :r4 :r2 :r3 :r3 :r2 :r4 :r1})))
(def ^:private corners #{:r1 :r2 :r3 :r4})
(defn mirror-token-corners
"The `applied-tokens` of a shape with the corners its radius tokens name
mirrored along the axes `flip-x?` and `flip-y?` name. A radius token
names a corner as the shape shows it, so a flip that mirrors the shape
moves the token with the corner it applies to."
[applied-tokens flip-x? flip-y?]
(if (and (or flip-x? flip-y?) (some corners (keys applied-tokens)))
(into {}
(map (fn [[attr token]]
[(cond-> attr (corners attr) (mirror-corner flip-x? flip-y?)) token]))
applied-tokens)
applied-tokens))
(defn tokens-in-orientation
"The `applied-tokens` of a shape oriented as `from`, with the corners its
radius tokens name as a shape oriented as `to` shows them."
[applied-tokens from to]
(mirror-token-corners applied-tokens
(not= (boolean (:flip-x from)) (boolean (:flip-x to)))
(not= (boolean (:flip-y from)) (boolean (:flip-y to)))))
(defn set-radius-to-single-corner
[shape attr value]

View File

@ -2469,6 +2469,15 @@
:layout-copies false}]
:ops [[:transform 0 10 0 8] [:apply-token 12 11] [:style 12 3 0 1] [:token-value 0 2]]}))))
(t/deftest a-token-edit-keeps-the-square-corner-of-a-mirrored-copy
;; a copy mirrored horizontally takes its main's radius tokens on the
;; corners it shows them on, so a token edit leaves the corner its main
;; keeps square by hand alone
(t/is (map? (cg/run {:world [2 0 {:tokens true :variants true :rich-variants true :text true :layout nil
:layout-copies true}]
:ops [[:apply-token 2 5] [:token-value 0 5 1] [:apply-token 0 0] [:style 0 3 0 1]
[:transform 0 4 0 3] [:apply-token 0 2] [:token-value 0 2 1]]}))))
(t/deftest switching-a-copy-carries-the-radius-tokens-it-applied
(t/is (map? (cg/run {:world [1 0 {:tokens true :variants true :rich-variants false :text false :layout :flex
:layout-copies false}]

View File

@ -8,6 +8,9 @@
(:require
[app.common.data :as d]
[app.common.files.changes-builder :as pcb]
[app.common.geom.point :as gpt]
[app.common.geom.shapes :as gsh]
[app.common.logic.placement :as clp]
[app.common.logic.shapes :as cls]
[app.common.test-helpers.compositions :as tho]
[app.common.test-helpers.files :as thf]
@ -15,6 +18,7 @@
[app.common.test-helpers.shapes :as ths]
[app.common.test-helpers.tokens :as tht]
[app.common.types.container :as ctn]
[app.common.types.modifiers :as ctm]
[app.common.types.text :as txt]
[app.common.types.token :as cto]
[app.common.types.tokens-lib :as ctob]
@ -379,6 +383,23 @@
(t/is (= 8 (:r2 rect')))
(t/is (empty? (:applied-tokens rect')))))
(t/deftest a-flip-moves-a-radius-token-with-its-corner
(let [file (-> (thf/sample-file :file1)
(ths/add-sample-shape :rect :type :rect :r1 8
:applied-tokens {:r1 "radius.s"}))
page (thf/current-page file)
rect (ths/get-shape file :rect)
flip #(gsh/transform-shape % (ctm/resize-modifiers (gpt/point -1 1) (gsh/shape->center %)))
changes (-> (pcb/empty-changes nil)
(pcb/with-page page)
(pcb/with-objects (:objects page))
(cls/generate-update-shapes [(:id rect)] flip (:objects page) {:attrs clp/transform-attrs}))
rect' (ths/get-shape (thf/apply-changes file changes) :rect)]
;; the corner it stores as the first shows second once flipped
(t/is (true? (:flip-x rect')))
(t/is (= 8 (:r1 rect')))
(t/is (= {:r2 "radius.s"} (:applied-tokens rect')))))
(t/deftest unapply-only-changed-per-side-token
(let [file (-> (setup-file)
(tht/apply-token-to-shape :frame1 "token-stroke-width"