Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<WebView>` 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).
Expand Down
21 changes: 21 additions & 0 deletions core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} {
Expand Down
63 changes: 63 additions & 0 deletions core/environment_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
19 changes: 15 additions & 4 deletions core/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading