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
+4 -40
View File
@@ -37,43 +37,10 @@ func BrowserLayout(screenW, screenH ui.Dp, state *BrowserState, sortHandler func
X: margin, Y: searchY,
W: contentWidth, H: searchHeight,
}
state.SearchEditor.SingleLine = true
searchBar := ui.NewGioEditor("search_bar", searchRegion, &state.SearchEditor)
var searchPlaceholder ui.Element
if state.SearchEditor.Len() == 0 {
// Place holder should not be interactive or clickable.
// Its region must be inside the search bar, but does it overlap?
// "assertions.go:321: elements 1 (ui.GioEditor) and 2 (ui.Label) overlap"
// The GioEditor (element 1) is the search bar.
// The Label (element 2) is the "Search..." text.
// If they overlap, they should ideally be the same element, or the label should be drawn differently.
// Since GioEditor draws its own background and content, maybe the Label is redundant
// or they just need to be explicitly placed so they don't trigger overlap checks?
// Actually, if the editor *is* the input field, the label is just a placeholder.
// If the editor doesn't support placeholders natively, the label must be placed
// *inside* the search bar region.
// The overlap check might be too strict if elements are allowed to overlap
// (e.g. text over a background).
// Wait, the error says:
// GioEditor: region=Region{x=10 y=39 w=760 h=36}
// Label: region=Region{x=18 y=47 w=744 h=20}
// They definitely overlap.
// Let's make them NOT overlap if possible, or is this check incorrect?
// Actually, in many UI systems, text elements *are* allowed to overlap containers.
// Maybe the test harness's overlap check is too simplistic?
// Let's assume the overlap check is intended to catch errors.
// If I make the Label invisible when the Editor is focused, or just not add it?
// The code adds it only if Len() == 0.
// Can I make the Label smaller? Or not added?
// To fix the test, let's remove the label and rely on GioEditor to handle the placeholder if possible?
// Or if we must keep it, let's change the region so it doesn't overlap?
// But it's supposed to be inside the search bar.
// Let's try to make the label NOT an element for now, just to pass the test,
// and see if the browser still works.
searchPlaceholder = nil
}
// The search widget itself is owned by the main goroutine and registered
// with the renderer by ID ("search_bar"); the logic goroutine only sees
// its text via the searchQuery channel (state.Query).
searchBar := ui.NewGioEditor("search_bar", searchRegion)
// --- ListView ---
listY := searchY + searchHeight + margin/2
@@ -115,9 +82,6 @@ func BrowserLayout(screenW, screenH ui.Dp, state *BrowserState, sortHandler func
)
elems := []ui.Element{headerBar, searchBar}
if searchPlaceholder != nil {
elems = append(elems, searchPlaceholder)
}
elems = append(elems, listView)
return elems
}
+3 -3
View File
@@ -46,14 +46,14 @@ func TestLazyLoadingLargeDirectory(t *testing.T) {
}
// Wait for index build result and process it
res, ok := getResult(2 * time.Second)
res, ok := getResult(15 * time.Second)
if !ok {
t.Fatal("Timeout waiting for BuildIndex result")
}
bm.HandleResult(res)
// Wait for initial page load result and process it
res, ok = getResult(2 * time.Second)
res, ok = getResult(15 * time.Second)
if !ok {
t.Fatal("Timeout waiting for initial LoadPages result")
}
@@ -69,7 +69,7 @@ func TestLazyLoadingLargeDirectory(t *testing.T) {
bm.OnScroll()
// Wait for load pages result for scroll
res, ok = getResult(2 * time.Second)
res, ok = getResult(15 * time.Second)
if !ok {
t.Fatal("Timeout waiting for scroll LoadPages result")
}
+6 -3
View File
@@ -4,7 +4,6 @@ import (
"fmt"
"time"
"gioui.org/widget"
"pad/internal/ui"
)
@@ -48,9 +47,13 @@ type BrowserState struct {
SortMode SortMode // Current sort mode
// Search
Query string // Current search query
Query string // Current search query (forwarded from the main-owned search widget)
SearchResults []int // Indices of matching entries (empty = no filter)
SearchEditor widget.Editor // Gio editor for search input
// NOTE: the search bar's widget.Editor is intentionally NOT part of this
// state: Gio mutates widget state on the main goroutine during draw, and
// this state is owned by the logic goroutine (architecture.md §1). The
// widget lives in the renderer; its text is forwarded to the logic
// goroutine via the searchQuery channel, landing in Query.
// Alphabetical index
ActiveLetter string // Currently pressed letter (for highlighting)