Make state single-owner and stabilize race detector

Frame now carries view state (scale/focus/query) to the main goroutine,
which reads only the frame-receiver-stored snapshot. Renderer owns Gio
widget editors (registered by ID) and the draw scale. Autosave timer
sends a token to the logic goroutine instead of touching state;
Shutdown waits for the owner to exit before FlushAll.

Tests access state only through owner-side Inspect/WithState helpers
(harness + in-package). Fixed double-Harness.Run race in two e2e tests,
made TestWorkerPool_PriorityPreemption deterministic, and raised the
lazy-loading test timeout that was too short under -race.

go test -race ./... is now green.
This commit is contained in:
gmp
2026-08-16 01:32:27 -04:00
parent 7240b62a61
commit 58725a5e6c
20 changed files with 723 additions and 369 deletions
+13 -9
View File
@@ -12,7 +12,6 @@ import (
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/io/key"
)
@@ -642,13 +641,14 @@ func NewSpacer(height Dp) Spacer {
}
}
// GioEditor wraps a Gio widget.Editor for single-line or multiline text input.
// GioEditor is a reference to a main-owned widget.Editor (see
// Renderer.RegisterGioEditor). The element itself is pure data: the mutable
// widget state lives in the renderer, which is owned by the main goroutine.
type GioEditor struct {
id string
region Region
visible bool
interactions []Interaction
Editor *widget.Editor
}
func (ge GioEditor) Type() string { return "gioeditor" }
@@ -662,9 +662,13 @@ func (ge GioEditor) String() string {
return fmt.Sprintf("GioEditor[%s] region=%+v", ge.id, ge.region)
}
// Draw renders the Gio Editor widget.
// Draw renders the Gio Editor widget registered for this element's ID.
func (ge GioEditor) Draw(gtx layout.Context, r *Renderer) {
ge.Editor.SingleLine = true
ed, ok := r.GioEditor(ge.id)
if !ok {
return
}
ed.SingleLine = true
// Position and clip to the editor's region
stack := op.Offset(image.Pt(int(r.toPx(ge.region.X)), int(r.toPx(ge.region.Y)))).Push(gtx.Ops)
@@ -696,16 +700,16 @@ func (ge GioEditor) Draw(gtx layout.Context, r *Renderer) {
}
r.RegisterClick(gtx, ge.id, ge.region, tapHandler)
ge.Editor.Layout(gtx, r.shp, font.Font{}, r.theme.FontSize, textColor, selectionColor)
ed.Layout(gtx, r.shp, font.Font{}, r.theme.FontSize, textColor, selectionColor)
}
// NewGioEditor creates a GioEditor element wrapping a widget.Editor.
func NewGioEditor(id string, region Region, editor *widget.Editor) GioEditor {
// NewGioEditor creates a GioEditor element referencing the widget.Editor
// registered with the renderer under id.
func NewGioEditor(id string, region Region) GioEditor {
return GioEditor{
id: id,
region: region,
visible: true,
Editor: editor,
}
}
+39 -21
View File
@@ -22,6 +22,7 @@ import (
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
"golang.org/x/image/math/fixed"
)
@@ -31,11 +32,6 @@ const maxInt32 = 1<<31 - 1
//go:embed icons/*.png
var iconFS embed.FS
// ScaleProvider provides access to the current scale factor.
type ScaleProvider interface {
Scale() float32
}
// clickReg pairs a gesture.Click with its handler.
type clickReg struct {
click *gesture.Click
@@ -54,34 +50,52 @@ type scrollReg struct {
}
// Renderer consumes a slice of elements and draws them.
//
// The Renderer is owned by the main goroutine. It is the home of any state
// that Gio mutates during draw (e.g. the search bar's widget.Editor): such
// state must not live in the logic goroutine's State (architecture.md §1).
type Renderer struct {
theme Theme
shp *text.Shaper
scale ScaleProvider
scale float32 // px-per-Dp for the current draw pass; set in Draw
icons map[string]image.Image
clicks map[string]clickReg
Keys map[string]keyReg // Exported Keys map
scrolls map[string]scrollReg
gioEditors map[string]*widget.Editor // main-owned widget editors by element ID
displayLineCount int // number of display lines from last drawWrappedText
lastLineY Dp // last line baseline offset from text origin, in Dp (derived from GlyphLayout)
glyphLayout GlyphLayout // captured per-glyph layout from last drawWrappedText
}
// New creates a new Renderer.
func New(th Theme, shp *text.Shaper, scale ScaleProvider) *Renderer {
func New(th Theme, shp *text.Shaper) *Renderer {
r := &Renderer{
theme: th,
shp: shp,
scale: scale,
icons: make(map[string]image.Image),
clicks: make(map[string]clickReg),
Keys: make(map[string]keyReg),
scrolls: make(map[string]scrollReg),
theme: th,
shp: shp,
icons: make(map[string]image.Image),
clicks: make(map[string]clickReg),
Keys: make(map[string]keyReg),
scrolls: make(map[string]scrollReg),
gioEditors: make(map[string]*widget.Editor),
}
r.loadIcons()
return r
}
// RegisterGioEditor attaches a main-owned widget.Editor to an element ID.
// GioEditor elements with that ID render the widget during draw. Must be
// called from the main goroutine before the first frame.
func (r *Renderer) RegisterGioEditor(id string, ed *widget.Editor) {
r.gioEditors[id] = ed
}
// GioEditor returns the main-owned widget editor registered for id, if any.
func (r *Renderer) GioEditor(id string) (*widget.Editor, bool) {
ed, ok := r.gioEditors[id]
return ed, ok
}
// loadIcons loads PNG icons from the embedded filesystem.
func (r *Renderer) loadIcons() {
for _, name := range []string{"back", "cut", "copy", "paste"} {
@@ -104,16 +118,20 @@ func (r *Renderer) icon(name string) image.Image {
// toPx converts Dp to physical pixels using State's scale.
func (r *Renderer) toPx(dp Dp) Px {
return ToPx(dp, r.scale.Scale())
return ToPx(dp, r.scale)
}
// toDp converts physical pixels to Dp using State's scale.
func (r *Renderer) toDp(px Px) Dp {
return ToDp(px, r.scale.Scale())
return ToDp(px, r.scale)
}
// Draw iterates elements and draws each in slice order (back-to-front).
func (r *Renderer) Draw(gtx layout.Context, elems []Element) {
// Draw renders the given elements. scale is the px-per-Dp factor for this
// draw pass, taken from the frame's view-state snapshot (the renderer never
// reads logic state directly).
func (r *Renderer) Draw(gtx layout.Context, elems []Element, scale float32) {
r.scale = scale
// Gio sets constraints to layout.Exact(windowSize), so Min==Max. Use (0,0) as Min.
winW := gtx.Constraints.Max.X
winH := gtx.Constraints.Max.Y
@@ -374,7 +392,7 @@ func (r *Renderer) drawText(gtx layout.Context, str string, size unit.Sp, reg Re
for g, ok := r.shp.NextGlyph(); ok; g, ok = r.shp.NextGlyph() {
totalAdvance += g.Advance
}
textW := Dp(float32(totalAdvance>>6) / r.scale.Scale())
textW := Dp(float32(totalAdvance>>6) / r.scale)
// Compute aligned X position
var drawX Dp
@@ -503,9 +521,9 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
// g.X is in fixed.Int26_6 — shift >> 6 for device pixels, divide by scale for Dp.
// g.Y is the baseline in device pixels.
layout.ByteOffsets = append(layout.ByteOffsets, byteOffset)
layout.X = append(layout.X, Dp(float32(g.X>>6)/r.scale.Scale()))
layout.Y = append(layout.Y, Dp(float32(g.Y)/r.scale.Scale()))
layout.Advance = append(layout.Advance, Dp(float32(g.Advance>>6)/r.scale.Scale()))
layout.X = append(layout.X, Dp(float32(g.X>>6)/r.scale))
layout.Y = append(layout.Y, Dp(float32(g.Y)/r.scale))
layout.Advance = append(layout.Advance, Dp(float32(g.Advance>>6)/r.scale))
// Advance byteOffset by g.Runes.
for i := uint16(0); i < g.Runes; i++ {