Remove duplicate browser state and fix sort mode toggle

- Remove currentBrowserState global and SetBrowserState from browser package
- Browser now receives sortHandler as a parameter instead of using global state
- Fix sort mode toggle: reload pages after clearing cache (was showing 'Loading...')
- Add String() methods to all UI element types for debugging
- Add e2e tests for sort mode functionality (5 tests)

The editor now owns all state; the browser is a pure function receiving
state and handlers through parameters.
This commit is contained in:
gmp
2026-05-31 23:20:21 +00:00
parent 86328bab5b
commit ce8dd82bb0
5 changed files with 495 additions and 22 deletions
+100
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"image"
"image/color"
"strings"
"gioui.org/font"
"gioui.org/layout"
@@ -21,12 +22,18 @@ type Region struct {
W, H Dp
}
// String returns a string representation of the Region.
func (r Region) String() string {
return fmt.Sprintf("Region{x=%g y=%g w=%g h=%g}", r.X, r.Y, r.W, r.H)
}
// Element is the base interface for all UI elements.
// Elements know how to draw themselves when given a Renderer.
type Element interface {
Region() Region
Visible() bool
Draw(gtx layout.Context, r *Renderer)
String() string
}
// Container holds child elements and draws them within its bounds.
@@ -48,6 +55,20 @@ func (c Container) Draw(gtx layout.Context, r *Renderer) {
}
}
// String returns a string representation of the Container and all children.
func (c Container) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("Container[%s] region=%+v bg=%#v children=%d", c.id, c.region, c.background, len(c.children)))
for i, child := range c.children {
if str, ok := any(child).(fmt.Stringer); ok {
sb.WriteString(fmt.Sprintf("\n [%d] %s", i, str.String()))
} else {
sb.WriteString(fmt.Sprintf("\n [%d] %T region=%+v", i, child, child.Region()))
}
}
return sb.String()
}
// NewContainer creates a Container with the given region, background, and children.
func NewContainer(region Region, bg Color, children []Element) Container {
return Container{
@@ -87,6 +108,11 @@ func (l Label) Draw(gtx layout.Context, r *Renderer) {
}
func (l Label) ID() string { return l.id }
// String returns a string representation of the Label.
func (l Label) String() string {
return fmt.Sprintf("Label[%s] text=%q region=%+v align=%d fontSize=%g", l.id, l.Text, l.region, l.Align, l.FontSize)
}
// NewLabel creates a visible Label element.
func NewLabel(text string, fontSize unit.Sp, region Region, align TextAlign, id string, interactions []Interaction) Label {
return Label{
@@ -129,6 +155,11 @@ func (i Icon) Draw(gtx layout.Context, r *Renderer) {
}
func (i Icon) ID() string { return i.id }
// String returns a string representation of the Icon.
func (i Icon) String() string {
return fmt.Sprintf("Icon[%s] name=%q region=%+v size=%g", i.id, i.Name, i.region, i.Size)
}
// NewIcon creates a visible Icon element.
func NewIcon(name string, region Region, size Dp, interactions []Interaction) Icon {
return Icon{
@@ -162,6 +193,11 @@ func (tf TextField) Visible() bool { return tf.visible }
func (tf TextField) ID() string { return tf.id }
func (tf TextField) Interactions() []Interaction { return tf.interactions }
// String returns a string representation of the TextField.
func (tf TextField) String() string {
return fmt.Sprintf("TextField[%s] region=%+v len=%d multiline=%v", tf.id, tf.region, len(tf.Value), tf.Multiline)
}
// Draw renders the TextField. For multiline text, it shapes with word wrap
// and draws display lines inline — one LayoutString call, no double-shaping.
func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
@@ -217,6 +253,16 @@ func (lv ListView) Interactions() []Interaction {
return filtered
}
// String returns a string representation of the ListView.
func (lv ListView) String() string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("ListView[%s] region=%+v items=%d selected=%d", lv.id, lv.region, len(lv.Items), lv.Selected))
for i, item := range lv.Items {
sb.WriteString(fmt.Sprintf("\n [%d] %s", i, item.String()))
}
return sb.String()
}
// Draw renders each list item as a row of text.
// The scroll gesture is registered via the normal interaction path so it
// is clipped to the list region by the element's clip context.
@@ -304,6 +350,15 @@ type ListItem struct {
Selected bool
}
// String returns a string representation of the ListItem.
func (li ListItem) String() string {
selected := ""
if li.Selected {
selected = " *"
}
return fmt.Sprintf("ListItem text=%q subtext=%q%s", li.Text, li.Subtext, selected)
}
// AlphaIndex displays an alphabetical index for quick navigation.
type AlphaIndex struct {
id string
@@ -319,6 +374,11 @@ func (ai AlphaIndex) Visible() bool { return ai.visible }
func (ai AlphaIndex) ID() string { return ai.id }
func (ai AlphaIndex) Interactions() []Interaction { return ai.interactions }
// String returns a string representation of the AlphaIndex.
func (ai AlphaIndex) String() string {
return fmt.Sprintf("AlphaIndex[%s] region=%+v letters=%v active=%q", ai.id, ai.region, ai.Letters, ai.ActiveLetter)
}
// Draw renders letters vertically along the right edge.
func (ai AlphaIndex) Draw(gtx layout.Context, r *Renderer) {
letterHeight := ai.region.H / Dp(len(ai.Letters))
@@ -366,6 +426,11 @@ func (b Button) Draw(gtx layout.Context, r *Renderer) {
}
func (b Button) ID() string { return b.id }
// String returns a string representation of the Button.
func (b Button) String() string {
return fmt.Sprintf("Button[%s] text=%q region=%+v enabled=%v", b.id, b.Text, b.region, b.Enabled)
}
// NewButton creates a visible Button element.
func NewButton(text string, enabled bool, primary bool, region Region) Button {
return Button{
@@ -394,6 +459,11 @@ func (sb SearchBar) Visible() bool { return sb.visible }
func (sb SearchBar) ID() string { return sb.id }
func (sb SearchBar) Interactions() []Interaction { return sb.interactions }
// String returns a string representation of the SearchBar.
func (sb SearchBar) String() string {
return fmt.Sprintf("SearchBar[%s] region=%+v query=%q match=%d/%d", sb.id, sb.region, sb.Query, sb.Match, sb.Total)
}
// NewSearchBar creates a visible SearchBar element.
func NewSearchBar(region Region, query string, match, total int, forward bool) SearchBar {
return SearchBar{
@@ -423,6 +493,11 @@ func (c Cursor) Visible() bool { return c.visible }
func (c Cursor) ID() string { return c.id }
func (c Cursor) Interactions() []Interaction { return c.interactions }
// String returns a string representation of the Cursor.
func (c Cursor) String() string {
return fmt.Sprintf("Cursor[%s] region=%+v line=%d col=%d", c.id, c.region, c.Line, c.Column)
}
// Selection represents a text selection range.
type Selection struct {
StartLine, StartCol int
@@ -449,6 +524,11 @@ func (mh MergeHunk) Visible() bool { return mh.visible }
func (mh MergeHunk) ID() string { return mh.id }
func (mh MergeHunk) Interactions() []Interaction { return mh.interactions }
// String returns a string representation of the MergeHunk.
func (mh MergeHunk) String() string {
return fmt.Sprintf("MergeHunk[%s] region=%+v hunk=%d/%d lineRange=%s resolution=%d", mh.id, mh.region, mh.HunkNumber, mh.TotalHunks, mh.LineRange, mh.Resolution)
}
// HunkResolution represents the resolution state of a merge hunk.
type HunkResolution int
@@ -474,6 +554,11 @@ func (t Toast) Visible() bool { return t.visible }
func (t Toast) ID() string { return t.id }
func (t Toast) Interactions() []Interaction { return t.interactions }
// String returns a string representation of the Toast.
func (t Toast) String() string {
return fmt.Sprintf("Toast[%s] text=%q region=%+v timeout=%d", t.id, t.Text, t.region, t.Timeout)
}
// NewToast creates a visible Toast element.
func NewToast(region Region, text string, timeout int) Toast {
return Toast{
@@ -497,6 +582,11 @@ func (s Spacer) Visible() bool { return s.visible }
func (s Spacer) ID() string { return s.id }
func (s Spacer) Interactions() []Interaction { return s.interactions }
// String returns a string representation of the Spacer.
func (s Spacer) String() string {
return fmt.Sprintf("Spacer[%s] region=%+v", s.id, s.region)
}
// NewSpacer creates a visible Spacer element.
func NewSpacer(height Dp) Spacer {
return Spacer{
@@ -519,6 +609,11 @@ func (ge GioEditor) Visible() bool { return ge.visible }
func (ge GioEditor) ID() string { return ge.id }
func (ge GioEditor) Interactions() []Interaction { return ge.interactions }
// String returns a string representation of the GioEditor.
func (ge GioEditor) String() string {
return fmt.Sprintf("GioEditor[%s] region=%+v", ge.id, ge.region)
}
// Draw renders the Gio Editor widget.
func (ge GioEditor) Draw(gtx layout.Context, r *Renderer) {
ge.Editor.SingleLine = true
@@ -569,6 +664,11 @@ type Color struct {
R, G, B, A uint8
}
// String returns a string representation of the Color.
func (c Color) String() string {
return fmt.Sprintf("Color{R:%d G:%d B:%d A:%d}", c.R, c.G, c.B, c.A)
}
// Theme holds styling defaults for the UI.
type Theme struct {
FontSize unit.Sp // Gio's shaper requires unit.Sp for font sizes