From 1c339ddcca8288051104f9a18127c5ead4923268 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Sat, 14 Nov 2026 14:56:38 +0100 Subject: [PATCH] :bug: Send the WASM engine the paddings and margins the layout uses A `:simple` padding or margin type repeats the top and right values on the bottom and left, as `ctl/paddings` and `ctl/child-margins` read them, but the WASM engine received the four stored values. A board whose type was switched to simple after editing its sides laid out its children with the stale bottom and left values in the WASM engine only, so moving one child shifted all of them and marked copies as touched. Both the shape payload and the per-attribute calls now send the values the layout functions return. The benchmark covers JVM code; this change is ClojureScript only, so it is not run. AI-assisted-by: claude-opus-5-5 --- .../app/common/render_wasm/api/upload.cljs | 12 +-- frontend/src/app/render_wasm/api.cljs | 18 +--- .../render_wasm/layout_data_test.cljs | 82 +++++++++++++++++++ frontend/test/frontend_tests/runner.cljs | 2 + 4 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 frontend/test/frontend_tests/render_wasm/layout_data_test.cljs diff --git a/common/src/app/common/render_wasm/api/upload.cljs b/common/src/app/common/render_wasm/api/upload.cljs index 8f53145b05..e9555d12dc 100644 --- a/common/src/app/common/render_wasm/api/upload.cljs +++ b/common/src/app/common/render_wasm/api/upload.cljs @@ -149,11 +149,7 @@ justify-items (-> (get shape :layout-justify-items) sr/translate-layout-justify-items) justify-content (-> (get shape :layout-justify-content) sr/translate-layout-justify-content) wrap-type (-> (get shape :layout-wrap-type) sr/translate-layout-wrap-type) - padding (get shape :layout-padding) - padding-top (get padding :p1 0) - padding-right (get padding :p2 0) - padding-bottom (get padding :p3 0) - padding-left (get padding :p4 0)] + [padding-top padding-right padding-bottom padding-left] (ctl/paddings shape)] (buf/write-u8 dview offset dir) (buf/write-u8 dview (+ offset 1) align-items) (buf/write-u8 dview (+ offset 2) align-content) @@ -170,11 +166,7 @@ (defn- write-layout-item! [dview offset shape] - (let [margins (get shape :layout-item-margin) - margin-top (get margins :m1 0) - margin-right (get margins :m2 0) - margin-bottom (get margins :m3 0) - margin-left (get margins :m4 0) + (let [[margin-top margin-right margin-bottom margin-left] (ctl/child-margins shape) h-sizing (-> (get shape :layout-item-h-sizing) sr/translate-layout-sizing) v-sizing (-> (get shape :layout-item-v-sizing) sr/translate-layout-sizing) align-self (-> (get shape :layout-item-align-self) sr/translate-align-self) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index aab9321c6c..8fb66cf7f9 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -1167,11 +1167,7 @@ justify-content (-> (get shape :layout-justify-content) sr/translate-layout-justify-content) wrap-type (-> (get shape :layout-wrap-type) sr/translate-layout-wrap-type) - padding (get shape :layout-padding) - padding-top (get padding :p1 0) - padding-right (get padding :p2 0) - padding-bottom (get padding :p3 0) - padding-left (get padding :p4 0)] + [padding-top padding-right padding-bottom padding-left] (ctl/paddings shape)] (h/call wasm/internal-module "_set_flex_layout_data" @@ -1201,11 +1197,7 @@ justify-items (-> (get shape :layout-justify-items) sr/translate-layout-justify-items) justify-content (-> (get shape :layout-justify-content) sr/translate-layout-justify-content) - padding (get shape :layout-padding) - padding-top (get padding :p1 0) - padding-right (get padding :p2 0) - padding-bottom (get padding :p3 0) - padding-left (get padding :p4 0)] + [padding-top padding-right padding-bottom padding-left] (ctl/paddings shape)] (h/call wasm/internal-module "_set_grid_layout_data" @@ -1302,11 +1294,7 @@ (defn set-layout-data [shape] - (let [margins (get shape :layout-item-margin) - margin-top (get margins :m1 0) - margin-right (get margins :m2 0) - margin-bottom (get margins :m3 0) - margin-left (get margins :m4 0) + (let [[margin-top margin-right margin-bottom margin-left] (ctl/child-margins shape) h-sizing (-> (get shape :layout-item-h-sizing) sr/translate-layout-sizing) v-sizing (-> (get shape :layout-item-v-sizing) sr/translate-layout-sizing) diff --git a/frontend/test/frontend_tests/render_wasm/layout_data_test.cljs b/frontend/test/frontend_tests/render_wasm/layout_data_test.cljs new file mode 100644 index 0000000000..3a86392cf3 --- /dev/null +++ b/frontend/test/frontend_tests/render_wasm/layout_data_test.cljs @@ -0,0 +1,82 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; Copyright (c) KALEIDOS SUBSIDIARY SL + +(ns frontend-tests.render-wasm.layout-data-test + "The paddings and margins the WASM engine receives are those the layout + uses: a `:simple` type repeats the top and right values on the bottom + and left." + (:require + [app.common.render-wasm.api.upload :as upload] + [app.common.render-wasm.wasm :as wasm] + [app.common.uuid :as uuid] + [app.render-wasm.api :as wasm.api] + [cljs.test :as t :include-macros true])) + +(defn- calls-of + "The arguments of each call to `fn-name` of the WASM module while `f` runs." + [fn-name f] + (let [calls (atom []) + module (js-obj fn-name (fn [& args] (swap! calls conj (vec args)) nil))] + (with-redefs [wasm/internal-module module] + (f)) + @calls)) + +(def ^:private padding + {:p1 3 :p2 6 :p3 9 :p4 12}) + +(t/deftest flex-layout-sends-simple-paddings + (let [[args] (calls-of "_set_flex_layout_data" + #(wasm.api/set-flex-layout {:layout :flex + :layout-padding-type :simple + :layout-padding padding}))] + (t/is (= [3 6 3 6] (subvec args 8 12))))) + +(t/deftest flex-layout-sends-multiple-paddings + (let [[args] (calls-of "_set_flex_layout_data" + #(wasm.api/set-flex-layout {:layout :flex + :layout-padding-type :multiple + :layout-padding padding}))] + (t/is (= [3 6 9 12] (subvec args 8 12))))) + +(t/deftest grid-layout-sends-simple-paddings + (let [[args] (calls-of "_set_grid_layout_data" + #(wasm.api/set-grid-layout-data {:layout :grid + :layout-padding-type :simple + :layout-padding padding}))] + (t/is (= [3 6 3 6] (take-last 4 args))))) + +(t/deftest layout-item-sends-simple-margins + (let [margin {:m1 3 :m2 6 :m3 9 :m4 12} + [simple] (calls-of "_set_layout_data" + #(wasm.api/set-layout-data {:layout-item-margin-type :simple + :layout-item-margin margin})) + [untyped] (calls-of "_set_layout_data" + #(wasm.api/set-layout-data {:layout-item-margin margin})) + [multiple] (calls-of "_set_layout_data" + #(wasm.api/set-layout-data {:layout-item-margin-type :multiple + :layout-item-margin margin}))] + (t/is (= [3 6 3 6] (subvec simple 0 4))) + (t/is (= [3 6 3 6] (subvec untyped 0 4))) + (t/is (= [3 6 9 12] (subvec multiple 0 4))))) + +(defn- float-runs + "Every run of four little-endian floats in the first `end` bytes of `dview`." + [dview end] + (into #{} + (map (fn [offset] (mapv #(.getFloat32 dview (+ offset (* 4 %)) true) (range 4)))) + (range 0 (- end 15)))) + +(t/deftest shape-payload-sends-simple-paddings-and-margins + (let [dview (js/DataView. (js/ArrayBuffer. 4096)) + shape {:id uuid/zero :type :frame :layout :flex + :layout-padding-type :simple :layout-padding padding + :layout-item-margin-type :simple :layout-item-margin {:m1 1 :m2 2 :m3 7 :m4 8}} + end (upload/write-shape-payload! dview 0 shape [] nil nil {:include-layout? true}) + runs (float-runs dview end)] + (t/is (contains? runs [3 6 3 6])) + (t/is (not (contains? runs [3 6 9 12]))) + (t/is (contains? runs [1 2 1 2])) + (t/is (not (contains? runs [1 2 7 8]))))) diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 33b521e82c..a09ed546f9 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -125,6 +125,7 @@ [frontend-tests.plugins.utils-test] [frontend-tests.plugins.value-objects-test] [frontend-tests.render-dimensions-test] + [frontend-tests.render-wasm.layout-data-test] [frontend-tests.render-wasm.process-objects-test] [frontend-tests.render-wasm.serialization-test] [frontend-tests.render-wasm.shape-proxy-test] @@ -348,6 +349,7 @@ 'frontend-tests.plugins.user-test 'frontend-tests.plugins.utils-test 'frontend-tests.plugins.value-objects-test + 'frontend-tests.render-wasm.layout-data-test 'frontend-tests.render-wasm.process-objects-test 'frontend-tests.render-wasm.serialization-test 'frontend-tests.render-wasm.shape-proxy-test