diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index 2f08cf61a5..9e7a0296c8 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -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. diff --git a/common/src/app/common/logic/libraries.cljc b/common/src/app/common/logic/libraries.cljc index 50ffef6c31..c4cc9d37e1 100644 --- a/common/src/app/common/logic/libraries.cljc +++ b/common/src/app/common/logic/libraries.cljc @@ -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 diff --git a/common/src/app/common/logic/placement.cljc b/common/src/app/common/logic/placement.cljc index cc34d75cf9..b4c3219d77 100644 --- a/common/src/app/common/logic/placement.cljc +++ b/common/src/app/common/logic/placement.cljc @@ -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 diff --git a/common/src/app/common/logic/shapes.cljc b/common/src/app/common/logic/shapes.cljc index edd7463d8d..7be005ea44 100644 --- a/common/src/app/common/logic/shapes.cljc +++ b/common/src/app/common/logic/shapes.cljc @@ -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 diff --git a/common/src/app/common/logic/swap.cljc b/common/src/app/common/logic/swap.cljc index 9d6538b58e..98532b13b0 100644 --- a/common/src/app/common/logic/swap.cljc +++ b/common/src/app/common/logic/swap.cljc @@ -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 diff --git a/common/src/app/common/test_helpers/composable/oracle.cljc b/common/src/app/common/test_helpers/composable/oracle.cljc index 097a9cb6f0..f3596911b8 100644 --- a/common/src/app/common/test_helpers/composable/oracle.cljc +++ b/common/src/app/common/test_helpers/composable/oracle.cljc @@ -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))) diff --git a/common/src/app/common/types/shape/radius.cljc b/common/src/app/common/types/shape/radius.cljc index fd7d492d23..cfbacd1226 100644 --- a/common/src/app/common/types/shape/radius.cljc +++ b/common/src/app/common/types/shape/radius.cljc @@ -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] diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index 7174bf2a27..8454196a6f 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -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}] diff --git a/common/test/common_tests/logic/token_apply_test.cljc b/common/test/common_tests/logic/token_apply_test.cljc index dce46d9dca..f2308de4e4 100644 --- a/common/test/common_tests/logic/token_apply_test.cljc +++ b/common/test/common_tests/logic/token_apply_test.cljc @@ -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"