desktop

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 24 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 desktop is the desktop shell's logic, kept apart from the desktop runtime: what the app does with the services' events, how the menu bar icon is kept current, the background work that runs while the app is open, and the order the app shuts down in.

It imports no desktop runtime. The desktop bootstrap (cmd/nilbox, built with the desktop tag) implements the small interfaces declared here over the runtime: the page that hears window events, the window, the menu bar icon, and the app itself. So everything here builds and is tested on any OS.

Index

Constants

View Source
const (
	// DefaultStopTimeout bounds how long Stop waits for the background work in flight,
	// such as a refresh, before it closes the data anyway.
	DefaultStopTimeout = 5 * time.Second

	// DefaultMeasureTimeout bounds a startup measurement whose window never reports its
	// first paint.
	DefaultMeasureTimeout = time.Minute

	// DeferredFallback is how long after launch the deferred work starts when the window
	// never reports its first paint.
	DeferredFallback = 10 * time.Second
)
View Source
const TextSizeParam = "textSize"

TextSizeParam is the query parameter of the page's URL that carries the text size. The page reads it before it first renders (frontend/src/lib/textSize.ts), so its first frame already shows text at the stored size, and writes it back on every change, so a reload keeps the size the user chose since.

Variables

View Source
var ErrNoFirstPaint = errors.New("the window did not report its first paint in time")

ErrNoFirstPaint is reported when a startup measurement timed out.

Functions

func BannerClicked

func BannerClicked(data map[string]any) event.RevealItem

BannerClicked reads the data a clicked banner carried, as BannerData wrote it, into the request to reveal its item, or the run it is about, and to mark its notification read. A banner that stands for many items names none. The data comes back through JSON, so an id is a float64; an id that is not a number, or a URL or a run's start that is not a string, reads as none.

func BannerData

func BannerData(b notify.Banner) map[string]any

BannerData is the data a banner carries for b, which the system hands back when the banner is clicked: the item it is for and the item's URL, or the page or the run of an entry about no item, with when the run started, and the notification it stands for.

func MarshalError

func MarshalError(err error) []byte

MarshalError encodes the cause the desktop runtime sends with an error a bound method returned: {"message": "..."}, holding what the user is told about it (see uierr.Message), and "detail" when an error in its chain is a Detailer. The runtime still sends the error's full text as the failure's own message, for the developer console; the window shows this one. It returns nil, which keeps the runtime's own encoding, only if the cause cannot be encoded.

func PageURL

func PageURL(textSize settings.TextSize) string

PageURL is the URL the window opens its page at: the app's root, carrying the text size the page shows from its first frame.

func WithinArea

func WithinArea(window, area Rect) (x, y int)

WithinArea returns where a window with the given bounds moves to so it sits inside area: left and up, where needed, until its right and bottom edges are inside area, but never past area's left and top edges, so a window larger than area keeps its top-left corner on it. A window already inside area stays where it is.

Types

type App

type App interface {
	// Quit quits the app.
	Quit()
}

App is the running app.

type Authorizer

type Authorizer interface {
	Authorize() (bool, error)
}

Authorizer asks the user, the first time, to allow notification banners. Every notify.Poster implements it.

type Config

type Config struct {
	Frontend Frontend
	Window   Window
	Tray     Tray
	App      App

	Inbox    Inbox
	Settings Settings

	// Sessions are the agent sessions the menu bar lists first. Optional: without it, the
	// menu lists the inbox alone.
	Sessions Sessions

	// Workers run in the background from Start until Stop, started in this order.
	Workers []Worker

	// Deferred is work kept off the cold start: it starts once, when the window reports
	// its first paint, or DeferredFallback after launch when it never does, and never in
	// a startup measurement. Each Run returns when its work is done or its context ends,
	// and Stop waits for it as for the workers.
	Deferred []Worker

	// Data is what Stop closes last, once the background work is done: the database.
	Data io.Closer

	// Banners is asked at launch to allow notifications. Nil means this build cannot
	// show them, and nothing is asked.
	Banners Authorizer

	// QuitGuard holds a quit while it reports work going, until the user confirms. Nil
	// means nothing holds a quit.
	QuitGuard QuitGuard

	// Clock times the menu bar's snooze ends. It defaults to the system clock.
	Clock clock.Clock

	// Stdout gets a startup measurement's timing line. It defaults to discarding.
	Stdout io.Writer

	// Logger receives what the shell cannot report as an error: a setting it could not
	// read and fell back from, a failed refresh or menu bar redraw, and a startup
	// measurement that never painted. Without it they are not logged.
	Logger *slog.Logger

	// MeasureStartup makes this run a startup measurement: the first paint is printed to
	// Stdout and quits the app, a run that never paints quits failing after
	// MeasureTimeout, and nothing asks to allow banners.
	MeasureStartup bool

	// StopTimeout and MeasureTimeout default to DefaultStopTimeout and
	// DefaultMeasureTimeout.
	StopTimeout    time.Duration
	MeasureTimeout time.Duration
}

Config holds a Shell's dependencies. Frontend, Window, Tray, App, Inbox, and Settings are required; the rest are optional.

type Detailer

type Detailer interface {
	error
	Detail() any
}

Detailer is an error that carries what the window needs to answer it, which the desktop runtime sends beside its message.

type Failure

type Failure struct {
	// Title is the dialog's title.
	Title string

	// Message is its text: what went wrong, and what to do about it where that is known.
	Message string
}

Failure is what the error dialog says when the app cannot start.

func StartupFailure

func StartupFailure(err error) Failure

StartupFailure words err, which stopped the app from starting, for the error dialog shown before the app quits: nobody sees the standard error of an app opened from the Finder. A database saved by a newer build asks for an update. A database another process holds locked, and data the app may not read or write, say what to do. Each gives what the user is told about the error itself (see uierr.Message).

type Frontend

type Frontend interface {
	Emit(e event.WindowEvent)
}

Frontend is the window's page. Emit passes a window event on to it, and must not wait for the page.

type Inbox

type Inbox interface {
	Snapshot(ctx context.Context) (inbox.Snapshot, error)
	Refresh(ctx context.Context) (inbox.Snapshot, error)
}

Inbox is the stored inbox the menu bar shows, and the refresh its menu asks for. *inbox.Service implements it.

type QuitGuard

type QuitGuard interface {
	Active() bool
}

QuitGuard says whether quitting now would stop work the user is asked about first: runs going in the app. *scriptrun.Engine implements it.

type Rect

type Rect struct {
	X, Y, Width, Height int
}

Rect is a rectangle on screen, in points, from its top-left corner.

type Sessions

type Sessions interface {
	// Waiting returns the running sessions waiting for the user, the longest waiting
	// first.
	Waiting(ctx context.Context) ([]sessions.Session, error)

	// Focus brings forward the terminal tab a running session is in.
	Focus(ctx context.Context, id string) error
}

Sessions are the agent sessions the menu bar lists, and the terminal a click on one brings forward. *sessions.Ledger and *sessions.Service do, which the core joins.

type Settings

type Settings interface {
	Theme(ctx context.Context) (settings.Theme, error)
	MenuBar(ctx context.Context) (bool, error)
	WindowSize(ctx context.Context) (settings.WindowSize, error)
	TextSize(ctx context.Context) (settings.TextSize, error)
}

Settings are the settings the shell reads when the app launches. core.ShellSettings implements it.

type Shell

type Shell struct {
	// contains filtered or unexported fields
}

Shell connects the services to the desktop runtime. It is the services' event.Sink: window events go on to the page, an inbox change redraws the menu bar, a theme or menu bar change reaches the window and the icon, and the first paint ends a startup measurement. It runs the background work, tracked so Stop can wait for it, and keeps the menu bar icon current with one redraw at a time.

The desktop runtime calls Start once the app starts, Launched once it has launched, and Stop when it shuts down.

func New

func New(cfg Config) *Shell

New returns a Shell over cfg that has not started.

func (*Shell) Emit

func (s *Shell) Emit(e event.Event)

Emit acts on e, until the shell stops: a window event goes on to the page, an inbox change also redraws the menu bar, and a sessions change does when the sessions waiting for the user read otherwise than the menu shows them, a request to reveal an item brings the window forward, a theme or menu bar change reaches the window or the icon, the first paint ends a startup measurement or starts the deferred work, and the user's choice to stop the runs and quit quits. It never waits for a redraw or the quit.

func (*Shell) FocusSession

func (s *Shell) FocusSession(id string, itemID int64, url string)

FocusSession brings forward the terminal tab of a session waiting for the user, as its row in the menu bar asks, in the background; Stop waits for it. When the tab cannot be shown, the session's item is shown in the window instead.

func (*Shell) HidesOnClose

func (s *Shell) HidesOnClose() bool

HidesOnClose reports whether closing the window only hides it, keeping the app running in the menu bar: while the icon shows and the app is not quitting.

func (*Shell) Launched

func (s *Shell) Launched()

Launched opens the window in the stored theme, at the stored size and text size, shows the menu bar icon when it is on, asks once to allow banners, arms the deferred work's fallback, and starts a startup measurement's timeout. The desktop runtime calls it once the app has launched, when the system appearance the System theme follows is known. A setting that cannot be read falls back to its default, with a note on Stderr.

func (*Shell) Quit

func (s *Shell) Quit()

Quit quits the app.

func (*Shell) RefreshNow

func (s *Shell) RefreshNow()

RefreshNow refreshes the inbox from GitHub in the background, as the menu bar's Refresh Now asks; Stop waits for it. The refresh says when it is stored, and records a failure for the window and the menu bar to show, which is also logged.

func (*Shell) ShouldQuit

func (s *Shell) ShouldQuit() bool

ShouldQuit answers the desktop runtime's question whether the app may quit now, which every quit asks: the menu's, the menu bar's, and the system's at a logout. It says yes unless the quit guard reports runs going in the app and the user hasn't chosen to stop them; then it brings the window forward, asks the page to ask the user, and says no. It runs on the main thread, so it answers at once and does the rest in the background.

func (*Shell) ShowWindow

func (s *Shell) ShowWindow()

ShowWindow brings the window forward, unless the shell is stopping.

func (*Shell) Start

func (s *Shell) Start(ctx context.Context)

Start starts the background work: the menu bar's redraws, then each worker in order. It runs once; a later call, or one after Stop, does nothing. When ctx ends, the work ends with it, as it does when the shell stops.

func (*Shell) Stop

func (s *Shell) Stop() error

Stop shuts the shell down, once, in this order: it drops every later event, stops the snooze, measurement, and deferred-work timers, ends the background work and waits for it, up to the stop timeout, and then closes the data. It returns the error closing the data returned, on this and every later call. It is safe to call without Start.

func (*Shell) Stopping

func (s *Shell) Stopping() bool

Stopping reports whether Stop has begun. Work the desktop runtime queued before then checks it, so nothing is drawn once the app is shutting down.

func (*Shell) TimedOut

func (s *Shell) TimedOut() bool

TimedOut reports whether a startup measurement gave up waiting for the first paint; the run then exits with a failure status.

type Tray

type Tray interface {
	// Show puts the icon in the menu bar, if it is not there.
	Show()

	// Hide takes the icon out of the menu bar.
	Hide()

	// Draw sets the icon's count and its menu.
	Draw(sum menubar.Summary)
}

Tray is the menu bar icon. Its methods must not wait for anything Stop waits for: Stop can run on the thread the icon is drawn on.

type Window

type Window interface {
	// Open opens the window at size, shrunk to fit the screen, in the native appearance
	// and on the background of theme, with its page showing text at textSize from the
	// first frame.
	Open(theme settings.Theme, size settings.WindowSize, textSize settings.TextSize)

	// Show brings the window forward: shown if it was closed into the menu bar, restored
	// if it was minimized, and the app made active.
	Show()

	// SetTheme pins the native appearance to theme and repaints the window's background.
	SetTheme(theme settings.Theme)

	// SetTextSize sets the smallest the window can be to suit textSize, growing the window
	// if it is smaller; the page shows the size itself.
	SetTextSize(textSize settings.TextSize)
}

Window is the app's one window and its native appearance.

type Worker

type Worker interface {
	Run(ctx context.Context)
}

Worker is background work that runs while the app is open, such as the inbox's scheduled refreshes. Run returns once ctx is canceled and the work in flight has returned. *inbox.Scheduler and worktrees.Sweeper implement it.

Jump to

Keyboard shortcuts

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