From 11863ded30bcc3fb5b945572715baee50eda24a5 Mon Sep 17 00:00:00 2001 From: Evan Hu Date: Fri, 20 Nov 2026 19:52:17 +0900 Subject: [PATCH] fix(core): avoid full resolves for window environment reports --- CHANGELOG.md | 1 + core/bench_test.go | 21 +++++++++++++ core/environment_test.go | 63 +++++++++++++++++++++++++++++++++++++++ core/tree.go | 19 +++++++++--- docs/architecture/core.md | 2 +- 5 files changed, 101 insertions(+), 5 deletions(-) create mode 100644 core/environment_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf93b000..2e9957e7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] - Add the `macos_universal` build target, which joins the arm64 and x64 apps, libraries and widgets into one direct-channel disk image and Sparkle feed (#265 by @houko). +- Stop resolving and emitting the whole tree when the platform reports only a new window frame, maximized state, lifecycle or title bar, as on every step of a window drag; what reads them through `Env` still updates (#178 by @houko). - Add `TabStyle` and `IndicatorStyle` to `ui.Tabs` to restyle its tabs, the selected tab and the indicator (#958 by @houko). - Reload a TSX `` in place with the new script when `injectedJavaScriptBeforeContentLoaded` changes, as `core.WebView.InjectedJavaScriptBeforeContentFunc` does, rather than replacing the web view (#895 by @houko). - Keep partial borders above Apple backdrop, material and glass layers while leaving app children above the border (#935 by @houko). diff --git a/core/bench_test.go b/core/bench_test.go index 1f2d67b40..0bccd74a5 100644 --- a/core/bench_test.go +++ b/core/bench_test.go @@ -58,6 +58,27 @@ func BenchmarkViewTick(b *testing.B) { commitEach(b, tree, func(i int) { tick.Set(i + 1) }) } +// BenchmarkWindowMove reports a new window frame before each commit over trees of static rows, as a window drag does: its cost stays the same whatever the tree holds (#178). +func BenchmarkWindowMove(b *testing.B) { + for _, rows := range []int{300, 3000} { + b.Run(fmt.Sprint(rows), func(b *testing.B) { + tree := benchTree(b, Size{Width: 400, Height: 800}, func(*Scope) Node { + kids := make([]Node, rows) + for i := range kids { + kids[i] = View{Style: Style{Direction: Row, Padding: All(8), Gap: 8}, Children: []Node{ + View{Style: Style{Width: Pt(20), Height: Pt(20), Background: RGB(0x3366ff)}}, + Text{Content: fmt.Sprintf("row %d", i), Style: Style{FontSize: 14}}, + }} + } + return View{Children: kids} + }) + commitEach(b, tree, func(i int) { + tree.SetEnvironment(Environment{WindowFrame: WindowFrame{X: Points(i + 1), Y: 40, Width: 400, Height: 800}}) + }) + }) + } +} + // BenchmarkMountChildren checks that mounting children stays linear in their count: plain ones whether or not a positional class is in another branch (#584), and striped ones that each follow their place. func BenchmarkMountChildren(b *testing.B) { for _, count := range []int{1000, 4000} { diff --git a/core/environment_test.go b/core/environment_test.go new file mode 100644 index 000000000..6b54dccfa --- /dev/null +++ b/core/environment_test.go @@ -0,0 +1,63 @@ +package core + +import ( + "fmt" + "testing" +) + +// A report that changes only the window's frame, maximized state, lifecycle or title bar commits without a full round, and a tree that reads none of them sends nothing. +func TestWindowEnvironmentReportsStayIncremental(t *testing.T) { + h := newHost(t, Size{Width: 200, Height: 100}, func(*Scope) Node { + return View{TestID: "static"} + }) + h.commit() + h.commit() + for _, env := range []Environment{ + {WindowFrame: WindowFrame{X: 20, Y: 30}}, + {Maximized: true}, + {Lifecycle: LifecycleBackground}, + {TitleBar: Edges{Top: 24}}, + } { + h.tree.SetEnvironment(env) + if h.tree.full { + t.Fatalf("environment %+v forced a full resolve", env) + } + if batch := h.commit(); batch != nil && len(batch.Items) > 0 { + t.Fatalf("environment %+v emitted items for a tree that reads none of its signals: %+v", env, batch.Items) + } + } +} + +// What reads the window's frame, maximized state, lifecycle or title bar through Env follows each report without a full round. +func TestWindowEnvironmentReportsReachTheirReaders(t *testing.T) { + h := newHost(t, Size{Width: 200, Height: 100}, func(cx *Scope) Node { + e := cx.Env() + return View{Children: []Node{ + Text{TestID: "frame", ContentFunc: func() string { return fmt.Sprint(e.WindowFrame().X) }}, + Text{TestID: "maximized", ContentFunc: func() string { return fmt.Sprint(e.Maximized()) }}, + Text{TestID: "lifecycle", ContentFunc: func() string { return e.Lifecycle().String() }}, + Text{TestID: "titlebar", ContentFunc: func() string { return fmt.Sprint(e.TitleBar().Top) }}, + }} + }) + h.commit() + env := Environment{WindowFrame: WindowFrame{X: 20}, Maximized: true, Lifecycle: LifecycleBackground, TitleBar: Edges{Top: 24}} + h.tree.SetEnvironment(env) + if h.tree.full { + t.Fatal("the window's report forced a full resolve") + } + h.commit() + for id, want := range map[string]string{"frame": "20", "maximized": "true", "lifecycle": LifecycleBackground.String(), "titlebar": "24"} { + if got := h.find(id).text.content; got != want { + t.Errorf("%s shows %q, want %q", id, got, want) + } + } +} + +func TestEnvironmentReportsThatAffectResolutionStayFull(t *testing.T) { + if !environmentNeedsFullRound(Environment{}, Environment{Dark: true}) { + t.Fatal("dark mode did not request a full resolve") + } + if environmentNeedsFullRound(Environment{}, Environment{WindowFrame: WindowFrame{X: 1}}) { + t.Fatal("window frame requested a full resolve") + } +} diff --git a/core/tree.go b/core/tree.go index da574702f..ab5109657 100644 --- a/core/tree.go +++ b/core/tree.go @@ -846,9 +846,9 @@ func (t *Tree) reportFit() { }) } -// SetEnvironment records what the platform reports about where the app -// runs. +// SetEnvironment records what the platform reports about where the app runs. func (t *Tree) SetEnvironment(env Environment) { + previous := t.reported t.reported = env e := t.root.Env() safe, headers := e.safe.GetUntracked(), e.headers @@ -858,8 +858,19 @@ func (t *Tree) SetEnvironment(env Environment) { if e.safe.GetUntracked() != safe || e.headers != headers { t.markLayout(t.view) } - // Images pick their scales, screens their headers from it. - t.markAll() + // Images pick their scales, screens their headers from the rest of it, which resolving and emitting read; the window's frame, maximized state, lifecycle and title bar reach only what reads their signals. + if environmentNeedsFullRound(previous, env) { + t.markAll() + } +} + +// environmentNeedsFullRound reports whether env changes from previous in a field that resolving or emitting the tree reads directly: any but WindowFrame, Maximized, Lifecycle and TitleBar, which only their Env signals carry (#178). +func environmentNeedsFullRound(previous, env Environment) bool { + previous.WindowFrame = env.WindowFrame + previous.Maximized = env.Maximized + previous.Lifecycle = env.Lifecycle + previous.TitleBar = env.TitleBar + return !previous.Equal(env) } // Size returns the surface size. diff --git a/docs/architecture/core.md b/docs/architecture/core.md index 378cffb29..e63d6c0a5 100644 --- a/docs/architecture/core.md +++ b/docs/architecture/core.md @@ -85,7 +85,7 @@ A component is any type whose `Render(cx) Node` returns its nodes, like `http.Ha ## Commit -`Tree.Commit` runs up to eight rounds. Its batch holds a copy of the items; `Tree.CommitShared` hands a platform that applies the batch within `Mount` the tree's own, until the next commit (`engine.SharedMounter`: the web, where copying a busy table's half megabyte a commit fed Go's collector on the page's thread). `OnLayout` handlers' changes go into the same batch, and so does the layout audit's drawing of every view's box while it shows: a round that moved, resized, hid, showed or removed a view, restyled what layout reads of one, such as its padding, or followed a scroll draws it again after the `OnLayout` handlers (`bumpAudit`). A commit costs what changed, not what the tree is: each change marks its node, `mark` for its parts and `markResolve` for its styles or content, and the walks below reach the marked nodes only, through the views they are emitted in (`dirtyKids`) and their shadow parents (`resolveKids`). What every node reads (the environment, the layout direction, the font scale, an accessory's ID) marks them all. +`Tree.Commit` runs up to eight rounds. Its batch holds a copy of the items; `Tree.CommitShared` hands a platform that applies the batch within `Mount` the tree's own, until the next commit (`engine.SharedMounter`: the web, where copying a busy table's half megabyte a commit fed Go's collector on the page's thread). `OnLayout` handlers' changes go into the same batch, and so does the layout audit's drawing of every view's box while it shows: a round that moved, resized, hid, showed or removed a view, restyled what layout reads of one, such as its padding, or followed a scroll draws it again after the `OnLayout` handlers (`bumpAudit`). A commit costs what changed, not what the tree is: each change marks its node, `mark` for its parts and `markResolve` for its styles or content, and the walks below reach the marked nodes only, through the views they are emitted in (`dirtyKids`) and their shadow parents (`resolveKids`). What every node reads (the environment, the layout direction, the font scale, an accessory's ID) marks them all, except a report that changes only the window's frame, maximized state, lifecycle or title bar, which reaches only what reads them through `Env`, so a window drag costs nothing per view (`environmentNeedsFullRound`). 1. **Resolve.** Inherit text styles into the marked nodes, and into every node under one whose inherited fields changed; prepare text whose content or style changed. 2. **Layout.** CSS block, flexbox and grid from `internal/layout` over each view's native children; overlays and modals are layout roots of their own. Results stay cached until a node changes, and frames are rounded to the environment's device pixels as Taffy rounds to whole ones, an overlay's or fixed view's origin too: a pointer's position holds fractions, and a box off the pixel grid draws its border and fill apart. Texts round up to a pixel, so at Android's 2.625 a text of 56 pixels is not laid out 58 tall. A new pixel ratio lays everything out again.