state

package
v0.1.0-alpha.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 5 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package state is the browser's in-memory model: windows, tabs, per-window active tab, ordering, the closed-tab stack and per-tab navigation history. It is pure data plus a mutex, with no I/O and no engine dependency, and it renders to the JSON snapshot / RFC 7386 merge patches that the chrome page consumes as the `state` and `state.patch` messages.

Snapshot schema (what the chrome page receives):

{
  "window":   {"id":1,"w":1280,"h":800,"scale":1,"incognito":false,"focused":true},
  "tabs":     [{"id","url","title","favicon","loading","progress","canBack","canForward",
                "security":{"level","https","certValid","issuer"},"audio","pinned","muted",
                "incognito","discarded","blocked"}, ...],   // visual order, pinned first
  "active":   "tab-id",
  "url","title","loading","canBack","canForward","security","bookmarked","incognito",
                                       // mirrors of the active tab for the toolbar
  "theme":    "light|dark|system",
  "sidebar":  {"open":false,"width":360},
  "settings": {...},
  "find":     {"open":false,"query":"","matches":0,"index":0},
  "closedTabs": 0
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(target, patch map[string]any) map[string]any

Apply applies a merge patch to target in place and returns it.

func Diff

func Diff(old, new map[string]any) map[string]any

Diff returns the RFC 7386 JSON merge patch that turns old into new. Both must be normalised (see Normalize). Arrays are replaced wholesale, as the RFC requires. The result is nil when nothing changed.

func Normalize

func Normalize(v map[string]any) map[string]any

Normalize round-trips v through JSON so numbers/bools/maps have canonical types; Diff expects normalised inputs.

Types

type ClosedTab

type ClosedTab struct {
	URL       string    `json:"url"`
	Title     string    `json:"title"`
	History   []string  `json:"history,omitempty"`
	HIdx      int       `json:"hidx"`
	Scroll    int       `json:"scroll"`
	Pinned    bool      `json:"pinned"`
	Index     int       `json:"index"`
	Window    int       `json:"window"`
	Incognito bool      `json:"-"`
	At        time.Time `json:"at"`
}

ClosedTab is an entry of the reopen stack.

type Find

type Find struct {
	Open    bool   `json:"open"`
	Query   string `json:"query"`
	Matches int    `json:"matches"`
	Index   int    `json:"index"`
}

Find is the find-in-page bar state.

type Model

type Model struct {
	Theme    string
	Sidebar  Sidebar
	Find     Find
	Settings map[string]any
	// Bookmarked answers "is this URL bookmarked" for the toolbar star.
	Bookmarked func(url string) bool
	// contains filtered or unexported fields
}

Model is the whole browser state. All methods are safe for concurrent use.

func New

func New() *Model

New returns an empty model.

func (*Model) Activate

func (m *Model) Activate(id string) bool

Activate makes id the active tab of its window.

func (*Model) Active

func (m *Model) Active(win int) string

Active returns the active tab id of a window ("" if none).

func (*Model) AddTab

func (m *Model) AddTab(win int, t Tab, index int, activate bool)

AddTab inserts a tab. index < 0 appends (after pinned tabs for unpinned). The tab becomes active when activate is true.

func (*Model) AddWindow

func (m *Model) AddWindow(w, h int, scale float64, incognito bool) int

AddWindow creates a window and returns its id.

func (*Model) CloseTab

func (m *Model) CloseTab(id string) (next string, ok bool)

CloseTab removes a tab, pushes it on the reopen stack (not for incognito tabs, which must leave no trace) and returns the tab that became active ("" if the window is now empty).

func (*Model) ClosedCount

func (m *Model) ClosedCount() int

ClosedCount is the size of the reopen stack.

func (*Model) Move

func (m *Model) Move(id string, to int) bool

Move reorders a tab to index `to` (clamped; pinned tabs stay first).

func (*Model) Navigated

func (m *Model) Navigated(id, url string, replace bool)

Navigated records a committed navigation in the tab's history. replace overwrites the current entry (redirects, reload); traverse moves the index (back/forward) without adding.

func (*Model) Order

func (m *Model) Order(win int) []string

Order returns the tab ids of a window in visual order.

func (*Model) PopClosed

func (m *Model) PopClosed() (ClosedTab, bool)

PopClosed returns the most recently closed tab.

func (*Model) Rev

func (m *Model) Rev() uint64

Rev increments on every mutation; consumers use it to skip diffing.

func (*Model) SetFocused

func (m *Model) SetFocused(win int, f bool)

SetFocused records window focus.

func (*Model) SetWindowSize

func (m *Model) SetWindowSize(win, w, h int, scale float64)

SetWindowSize records the logical size and device scale.

func (*Model) Snapshot

func (m *Model) Snapshot(win int) map[string]any

Snapshot renders the full state for one window as a JSON-ready map.

func (*Model) Tab

func (m *Model) Tab(id string) (Tab, bool)

Tab returns a copy of the tab.

func (*Model) TabCount

func (m *Model) TabCount() int

TabCount counts tabs (optionally all windows).

func (*Model) Traverse

func (m *Model) Traverse(id string, delta int) (string, bool)

Traverse moves delta entries through history and returns the target url.

func (*Model) Update

func (m *Model) Update(id string, fn func(t *Tab)) bool

Update applies fn to the tab under the lock.

func (*Model) WindowInfo

func (m *Model) WindowInfo(win int) (Window, bool)

WindowInfo returns a copy of a window.

type Security

type Security struct {
	Level     string `json:"level"` // "secure" | "insecure" | "internal" | "error" | "none"
	HTTPS     bool   `json:"https"`
	CertValid bool   `json:"certValid"`
	Issuer    string `json:"issuer,omitempty"`
}

Security describes the connection security shown by the lock icon.

type Sidebar struct {
	Open  bool `json:"open"`
	Width int  `json:"width"`
}

Sidebar is the agent sidebar geometry.

type Tab

type Tab struct {
	ID         string   `json:"id"`
	URL        string   `json:"url"`
	Title      string   `json:"title"`
	Favicon    string   `json:"favicon,omitempty"`
	Loading    bool     `json:"loading"`
	Progress   float64  `json:"progress"`
	CanBack    bool     `json:"canBack"`
	CanForward bool     `json:"canForward"`
	Security   Security `json:"security"`
	Audio      bool     `json:"audio"`   // legacy name of Audible
	Crashed    bool     `json:"crashed"` // the tab's engine worker panicked
	Pinned     bool     `json:"pinned"`
	Muted      bool     `json:"muted"`
	Incognito  bool     `json:"incognito"`
	Discarded  bool     `json:"discarded"`
	Blocked    int      `json:"blocked"`         // trackers blocked on this tab
	Popup      bool     `json:"popup,omitempty"` // opened by a page's window.open / link target (the tab strip marks it)
	Cam        bool     `json:"-"`               // the page is using the camera
	Mic        bool     `json:"-"`               // ...the microphone
	Share      bool     `json:"-"`               // ...is sharing a tab, window or screen

	// not sent to the chrome page
	Window  int       `json:"-"`
	History []string  `json:"-"` // navigation entries (urls)
	HIdx    int       `json:"-"`
	Scroll  int       `json:"-"` // device-independent scroll offset, for session restore
	Opened  time.Time `json:"-"`
	Opener  string    `json:"-"` // id of the tab whose page opened this popup
}

Tab is one browser tab as the chrome page sees it.

type Window

type Window struct {
	ID        int
	Order     []string // tab ids, visual order
	Active    string
	Incognito bool
	W, H      int
	Scale     float64
	Focused   bool
}

Window is a browser window.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL