⚡ Re-measure auto-grow texts only on size changes

On the WebGL renderer, a sync from a main component re-measures every
auto-grow text it changes on the page. The sync runs on every edit to
a main component, so an edit that cannot change a text size, such as
opacity, shadow or text color, still re-measures and re-lays out every
synced text.

The sync now re-measures a text only when the change sets an attribute
that can change its size: :grow-type, :width, :height,
:layout-item-h-sizing, :layout-item-v-sizing, or :content that differs
in more than its fills. A text color change reaches copies as a
:content change, so fills are removed from both contents before
comparing. Sync emits operations only for attributes that changed, so
the operation attributes are enough. Moves (x and y only) do not
re-measure.

Tests: frontend-tests.data.workspace-sync-text-wasm-test adds cases for
a fill change and an opacity change on the main text (no re-measure),
and for a font size change and a width change on an auto-height text
(re-measure).

Relates to #12721

AI-assisted-by: claude-opus-5-5
This commit is contained in:
Pablo Alba 2026-11-17 11:56:11 +01:00 committed by Pablo Alba
parent 0e103fb215
commit 2964e88a8c
2 changed files with 95 additions and 12 deletions

View File

@ -27,6 +27,7 @@
[app.common.types.file :as ctf]
[app.common.types.library :as ctl]
[app.common.types.shape.layout :as ctsl]
[app.common.types.text :as txt]
[app.common.types.typography :as ctt]
[app.common.uuid :as uuid]
[app.config :as cf]
@ -1230,19 +1231,43 @@
(remove uuid/zero?))
(:redo-changes changes)))
(def ^:private text-size-attrs
"Shape attributes whose change can change the size of an auto-grow text."
#{:grow-type :width :height :layout-item-h-sizing :layout-item-v-sizing})
(defn- without-fills
"Text `content` without its fills, which do not change its layout."
[content]
(txt/transform-nodes #(apply dissoc % :fills txt/text-fill-attrs) content))
(defn- text-size-change?
"True when `change` can change the size of the text `shape`: it sets a
size attribute, or content that differs in more than its fills. Sync only
emits operations for attributes that changed."
[shape {:keys [type operations]}]
(and (= :mod-obj type)
(some (fn [{:keys [attr val] :as op}]
(and (= :set (:type op))
(or (contains? text-size-attrs attr)
(and (= :content attr)
(not= (without-fills (:content shape))
(without-fills val))))))
operations)))
(defn- sync-auto-grow-text-ids-on-page
"Auto-grow text ids from sync redo-changes on `page-id`, resolved against
pre-commit `objects`. Off-page, non-text, and :fixed ids are excluded."
"Auto-grow text ids from sync redo-changes on `page-id` that can change
the text size, resolved against pre-commit `objects`. Off-page, non-text
and :fixed ids are excluded."
[changes page-id objects]
(into []
(comp
(filter #(= page-id (:page-id %)))
(keep :id)
(remove uuid/zero?)
(filter (fn [id]
(filter (fn [{:keys [id] :as change}]
(when-let [shape (get objects id)]
(and (cfh/text-shape? shape)
(not= :fixed (:grow-type shape))))))
(not= :fixed (:grow-type shape))
(text-size-change? shape change)))))
(map :id)
(distinct))
(:redo-changes changes)))

View File

@ -10,12 +10,14 @@
:skip-component-sync? so box size heals without marking :geometry-group."
(:require
[app.common.files.changes-builder :as pcb]
[app.common.geom.shapes :as gsh]
[app.common.logic.shapes :as cls]
[app.common.test-helpers.components :as cthc]
[app.common.test-helpers.compositions :as ctho]
[app.common.test-helpers.files :as cthf]
[app.common.test-helpers.ids-map :as cthi]
[app.common.test-helpers.shapes :as cths]
[app.common.types.modifiers :as ctm]
[app.common.uuid :as uuid]
[app.main.data.workspace.libraries :as dwl]
[app.main.data.workspace.wasm-text :as dwwt]
@ -51,21 +53,35 @@
(cthc/instantiate-component :component1 :copy-root
{:children-labels [:copy-child]})))
(defn- bump-main-font-size
[file]
(defn- update-main-text
[file update-fn]
(let [page (cthf/current-page file)
main-child (cths/get-shape file :main-child)
changes (cls/generate-update-shapes
(pcb/empty-changes nil (:id page))
#{(:id main-child)}
(fn [shape]
(assoc-in shape
[:content :children 0 :children 0 :children 0 :font-size]
"32"))
update-fn
(:objects page)
{})]
(cthf/apply-changes file changes)))
(defn- bump-main-font-size
[file]
(update-main-text file #(assoc-in % [:content :children 0 :children 0 :children 0 :font-size] "32")))
(defn- recolor-main-text
[file]
(update-main-text file #(assoc-in % [:content :children 0 :children 0 :children 0 :fills]
[{:fill-color "#ff0000" :fill-opacity 1}])))
(defn- fade-main-text
[file]
(update-main-text file #(assoc % :opacity 0.5)))
(defn- widen-main-text
[file]
(update-main-text file #(gsh/transform-shape % (ctm/change-dimensions-modifiers % :width 300))))
(defn- collect-layout-updates
"Subscribe before emit; accumulates deref'd `:layout/update` payloads."
[store]
@ -125,3 +141,45 @@
(fn [_new-state]
(t/is (empty? @resize-calls)
"fixed-grow texts must not be scheduled for WASM remasure"))))))
(defn- sync-remeasures
"Sync the copy after `change-main` and call `check` with the re-measure
requests."
[done change-main check]
(let [file (change-main (setup-file :grow-type :auto-height))
store (ths/setup-store file)
events [(dwl/sync-file (:id file) (:id file)
:components (cthi/id :component1))]]
(ths/run-store store done events (fn [_] (check @resize-calls)))))
(t/deftest test-sync-file-skips-remasure-for-fill-change
(t/async
done
(sync-remeasures done recolor-main-text
(fn [calls]
(t/is (empty? calls)
"a text color change does not change its size")))))
(t/deftest test-sync-file-skips-remasure-for-opacity-change
(t/async
done
(sync-remeasures done fade-main-text
(fn [calls]
(t/is (empty? calls)
"an opacity change does not change the text size")))))
(t/deftest test-sync-file-remasures-auto-height-text-on-font-change
(t/async
done
(sync-remeasures done bump-main-font-size
(fn [calls]
(t/is (= 1 (count calls))
"a font change can change the text size")))))
(t/deftest test-sync-file-remasures-auto-height-text-on-width-change
(t/async
done
(sync-remeasures done widen-main-text
(fn [calls]
(t/is (= 1 (count calls))
"a wider auto-height text can wrap into fewer lines")))))