app

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 39 Imported by: 1 module, 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 app runs a jamb application: it opens native windows, creates the GPU renderer and drives one ui runtime per window.

func main() {
    app.New().Run(app.Window{
        Title: "Hello", Width: 800, Height: 600,
        Root: func(ctx *ui.Context) ui.Element { return ui.Text("Hello") },
    })
}

Further windows open with Open, from event handlers or from goroutines. All windows share the UI thread, one text system and one GPU device; each has its own ui runtime, theme, zoom, focus and key bindings.

Example

A one-window application. It is compiled, not run, by the tests.

package main

import (
	"github.com/tyler-builds/jamb/app"
	"github.com/tyler-builds/jamb/ui"
)

func main() {
	app.New().Run(app.Window{
		Title: "Hello", Width: 800, Height: 600,
		Root: func(ctx *ui.Context) ui.Element { return ui.Text("Hello") },
	})
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRecentDocument

func AddRecentDocument(path string)

AddRecentDocument adds the file at path to the Recent category of the app's jump list, which the app shows when JumpList.Recent is set. Call it for each file the app opens or saves. A relative path is made absolute first, because Windows needs the full path. Windows keeps the list and drops old entries itself. It does nothing on platforms other than Windows.

func ClearNotifications

func ClearNotifications() error

ClearNotifications returns an error; see Notify.

func Notify

func Notify(Notification) error

Notify returns an error: OS notifications are Windows toasts.

func Open

func Open(w Window) ui.Window

Open opens another window. It is safe from event handlers and from goroutines: the window is opened on the UI thread. The returned ui.Window controls it; actions on it before the window exists run once it is open, and Closed reports whether it closed or failed to open. The app keeps running while the window is open (see Window.QuitWhenClosed).

func OpenDockWindow

func OpenDockWindow(w ui.DockWindow) ui.Window

OpenDockWindow opens the window of a floating dock group with Open. It is the opener that ui.Dock.Floating takes:

ui.Dock(layout, panels).Floating(app.OpenDockWindow)

The window takes the group's title, position and size, and its close and geometry handlers are the dock's, so the layout records the window's place as it moves and when it closes, and the group's panels return to the dock when the window closes. A window that opens at a place off its monitor is moved in from the monitor's edges (on Windows). The window is not quit with the app's last window: the main window should set QuitWhenClosed, so that closing it closes the floating windows too.

func RemoveNotification

func RemoveNotification(string) error

RemoveNotification returns an error; see Notify.

Types

type App

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

App is an application instance. A process has one App: its windows share the UI thread, the text system and the GPU device.

func New

func New() *App

New creates an application.

func (*App) AppID

func (a *App) AppID(id string) *App

AppID sets the app's Application User Model ID, the identifier that Windows uses to group the app's windows on the taskbar, and to find its jump list, its Recent documents and its single instance. It is set when the app starts, before the first window opens. Use a stable identifier in the form CompanyName.ProductName, such as "JambExamples.Notes": it is letters, digits, '.', '-' and '_', starts with a letter or digit, and is at most 128 characters. Keep it the same across versions. A packaged (MSIX) app has an identity from its package, and the package's ID is used instead; see docs/os-integration.md, Jump lists and single instance section.

func (*App) DevTools

func (a *App) DevTools(on bool) *App

DevTools turns the visual inspector on in a release build as well, for every window: Ctrl+Shift+I opens an inspector window for the window that has the focus, and closes it again. The inspector is on by default under `jamb dev` and with JAMB_INSPECT=1, and it shows the elements, styles, accessibility and frame timings of a window; see docs/tools.md, "Tools for humans and coding agents".

func (*App) Font

func (a *App) Font(family string, weight ui.FontWeight, data []byte) *App

Font registers a font file (TrueType or OpenType data, such as a //go:embed file) as the upright face of family at weight, for every window of the app. Text uses it with Font(family) or Style.FontFamily; a weight the family has no face for uses the nearest one, and italic text uses the upright face. A family named like a built-in one ("" for the UI font, "mono") replaces that face.

app.New().
	Font("Inter", ui.FontRegular, interRegular).
	Font("Inter", ui.FontBold, interBold).
	Run(app.Window{...})

The fonts are parsed while the first window opens. Data that is not a font makes RunE return an error, and Run print it and exit.

Example

Fonts are registered before Run, and apply to every window of the app.

package main

import (
	"github.com/tyler-builds/jamb/app"
	"github.com/tyler-builds/jamb/ui"
)

func main() {
	var interRegular, interBold []byte // the font files, for example from go:embed
	app.New().
		Font("Inter", ui.FontRegular, interRegular).
		Font("Inter", ui.FontBold, interBold).
		Run(app.Window{Title: "Fonts", Width: 640, Height: 480,
			Root: func(ctx *ui.Context) ui.Element { return ui.Text("Fonts") }})
}

func (*App) JumpList

func (a *App) JumpList(j JumpList) *App

JumpList sets the app's jump list, which is committed when the app starts. It needs an AppID. Each task starts the program's executable, and a program run with go run has an executable that is removed when the run ends, so its tasks stop working then. See the Jump lists and single instance section of docs/os-integration.md.

func (*App) OnNotification

func (a *App) OnNotification(fn func(n NotificationActivation)) *App

OnNotification sets the function that is called on the UI thread when the user clicks a notification that the app sent, on the body or on a button. It needs SingleInstance and an AppID: a click on a notification starts the app's program with the click's data, and SingleInstance hands that to the running app, which then calls fn in place of starting a second window. A click after the app has closed starts the app and calls fn once its window is open.

The data of a click is passed through the notification system, which any program of the user can also send. fn must treat Action, Tag and Args as input from outside the app, and check them before it acts on them.

func (*App) Run

func (a *App) Run(w Window)

Run opens the window and blocks until the app quits: when the last window closes, or when a QuitWhenClosed window closes. On failure it prints the error and exits with status 1. Use RunE to handle errors yourself. A later launch of a SingleInstance app returns at once, without a window, after its command line has been handed to the running app.

func (*App) RunE

func (a *App) RunE(w Window) error

RunE is Run returning an error instead of exiting. Call it once per process.

func (*App) SingleInstance

func (a *App) SingleInstance(onArgs func(args []string, dir string)) *App

SingleInstance makes the app a single instance per user and session. A later launch hands its command line to the first instance and exits with status 0, without opening a window. The first instance calls onArgs on its UI thread with the arguments after the program name, as os.Args[1:] gives them, and the working directory of the later launch, so a relative path can be resolved with filepath.Join(dir, arg). Before onArgs runs, the first instance's first window is brought to the front. onArgs may be nil.

The first launch keeps its own arguments in os.Args, as usual. SingleInstance needs an AppID, which names the instance. A later launch whose hand-off fails runs on its own, with its own window and state. It does not depend on JumpList: a jump list task starts a new process, and a single instance app receives that process's arguments through its hand-off.

type Chrome

type Chrome uint8

Chrome selects who draws a window's title bar.

const (
	// ChromeNative keeps the operating system's title bar and frame.
	ChromeNative Chrome = iota
	// ChromeCustom removes the operating system's title bar, and the app
	// draws its own. The window keeps its native resize borders, Aero Snap,
	// shadow and maximize behavior. The app marks its drag and button areas
	// with ui.DivElement.WindowArea; ui.TitleBarButtons draws the usual
	// buttons.
	ChromeCustom
)

type JumpList

type JumpList struct {
	// Tasks are listed under Tasks, in order. Windows may show only the first few.
	Tasks []JumpTask
	// Recent shows the Recent category, which lists the documents given to
	// AddRecentDocument.
	Recent bool
}

JumpList is the app's Windows jump list: the tasks in its taskbar menu, and whether the menu has a Recent category. The app sets it with JumpList, when it starts, under its AppID. Nothing is listed unless the app sets it, and the framework adds no tasks of its own.

Each task starts the app's own executable with Args, as a new process. Without SingleInstance, that process opens its own window and keeps its own state. With SingleInstance, the new process hands its command line to the running app, which receives it in onArgs. Use SingleInstance when a task should act in the running app, such as opening a window in it, rather than start another one.

type JumpTask

type JumpTask struct {
	// Title is the text the menu shows. It is required.
	Title string
	// Args is the command line after the executable, such as "--new-note". Windows
	// takes it as one string, so an argument with spaces needs quotes, as in a
	// shell.
	Args string
	// Description is shown as the tooltip of the task.
	Description string
	// Icon is an optional path and icon index, as "path,index", such as
	// "C:\\app\\icons.dll,2". A comma followed by a number is read as the index;
	// otherwise the whole string is the path, with index 0. Empty uses the
	// executable's own icon.
	Icon string
}

JumpTask is one task of a JumpList.

type Notification

type Notification struct {
	// Title is the first line, in bold. It is required.
	Title string
	// Body is the text below the title. It may be empty.
	Body string
	// Image is shown as a hero image: a banner above the text, which Windows scales
	// to fit the notification. It is saved as a PNG file in the temporary folder, as
	// Windows reads images from files; the file is named by a digest of its contents,
	// so the same image is saved once.
	Image image.Image
	// Actions are the buttons, in order: at most five. A click on a button reports
	// the button's ID to OnNotification as NotificationActivation.Action.
	Actions []NotificationAction
	// Tag identifies the notification. A later notification with the same tag
	// replaces the one that is shown, and RemoveNotification removes it. It is at
	// most 64 characters, and empty for none.
	Tag string
	// Args is returned unchanged to OnNotification as NotificationActivation.Args
	// when the notification is activated. Put an ID or a short string in it, not a
	// secret: it is stored in the notification's activation data.
	Args string
	// Silent shows the notification without its sound.
	Silent bool
}

Notification is an operating system notification: a Windows toast, shown in the Notification Center. Notify shows it; see the OS notifications section of docs/os-integration.md.

Write Title and Body as complete sentences. Windows reads them aloud with screen readers, so "3 warnings in the build" is read as written, and "3 warnings" alone is not a sentence a listener can place.

type NotificationAction

type NotificationAction struct {
	// ID is what OnNotification receives when the button is clicked. It is required
	// and unique within the notification.
	ID string
	// Label is the text of the button. It is required.
	Label string
}

NotificationAction is a button of a Notification.

type NotificationActivation

type NotificationActivation struct {
	// Action is the ID of the button that was clicked, or empty for a click on the
	// body of the notification.
	Action string
	// Tag and Args are the Tag and Args of the notification.
	Tag string
	// Args is the Args of the notification, as Tag describes.
	Args string
}

NotificationActivation is a click on a notification, reported to OnNotification.

type Tray

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

Tray is a notification area icon: the icon at the edge of the taskbar, which the user can click and right click. Make one with NewTray and add it with Show.

The methods are safe from any goroutine. They run on the UI thread in the order they are called, so a call before the app runs waits for it to start.

A tray icon needs no window. The icon, its tooltip and its menu are Windows' own: the menu follows the system theme, and assistive technology reads it as the system's menus. Windows keeps the user's choice to show or hide an icon by its identity: with an AppID, the identity is derived from the AppID and the icon's place in the order of NewTray calls, so keep that order from run to run. Windows also ties that identity to the executable's path, and an icon whose executable moved is added by its ID instead.

func NewTray

func NewTray(o TrayOptions) *Tray

NewTray returns a notification area icon. It is not shown until Show is called.

func (*Tray) Hide

func (t *Tray) Hide()

Hide takes the icon out of the notification area, and Show adds it again. An app's icons are also removed when the app quits. It is safe from any goroutine.

func (*Tray) SetIcon

func (t *Tray) SetIcon(img image.Image)

SetIcon sets the image of the icon; nil is the executable's icon. It is safe from any goroutine.

func (*Tray) SetTooltip

func (t *Tray) SetTooltip(s string)

SetTooltip sets the tooltip of the icon. It is safe from any goroutine.

func (*Tray) Show

func (t *Tray) Show()

Show adds the icon to the notification area. Showing an icon that is shown does nothing. A failure, such as a refused icon, is printed, and the icon is not shown; Show may be called again. It is safe from any goroutine.

func (*Tray) Shown added in v0.2.0

func (t *Tray) Shown() bool

Shown reports whether the icon is in the notification area: Show has added it, and Hide has not taken it out. An icon stays shown when Explorer restarts, as it is added to the new notification area again. It is safe from any goroutine: from another goroutine, it waits for the UI thread to run the calls made before it, and it reports false once the app has quit.

type TrayItem

type TrayItem struct {
	// Label is the text of the item.
	Label string
	// Disabled shows the item, but it cannot be chosen.
	Disabled bool
	// Checked shows a check mark beside the label.
	Checked bool
	// Submenu opens a menu of its items to the right, instead of running OnClick.
	Submenu []TrayItem
	// OnClick runs when the item is chosen.
	OnClick func()
	// contains filtered or unexported fields
}

TrayItem is an item of a tray menu. A tray menu is Windows' native menu, so it is a subset of what the menus of ui can show: an item has a label, a disabled state, a check mark and a submenu, but no icon, shortcut or action. Its OnClick runs on the UI thread after the menu closes, when the user chooses the item. A separator is made by TraySeparator.

func TraySeparator

func TraySeparator() TrayItem

TraySeparator returns a rule between groups of menu items.

type TrayOptions

type TrayOptions struct {
	// Icon is the image of the icon, drawn at the small icon size of the current
	// DPI. Nil uses the executable's icon, which jamb package embeds, or the
	// standard application icon when the executable has none.
	Icon image.Image
	// Tooltip is the text shown when the pointer rests on the icon. Windows shows
	// at most 127 UTF-16 units of it.
	Tooltip string
	// OnClick runs on the UI thread for a click, and for Enter or Space when the
	// icon has the keyboard focus. A double click runs OnClick for its first click,
	// as Windows reports both, and then OnDoubleClick. An app that wants different
	// actions for the two should make OnClick idempotent, or rely on OnDoubleClick
	// alone and leave OnClick nil.
	OnClick func()
	// OnDoubleClick runs on the UI thread for a double click.
	OnDoubleClick func()
	// Menu returns the menu, which is shown on a right click, and with the context
	// menu key or Shift+F10. It runs on the UI thread each time the menu opens, so
	// its checked items show the app's state. Nil means the icon has no menu.
	Menu func() []TrayItem
}

TrayOptions describes a notification area icon; see NewTray.

type Window

type Window struct {
	// Title is the title of the window, which its title bar and taskbar button show.
	Title  string
	Width  int // logical pixels; default 1024
	Height int // logical pixels; default 720
	// MinWidth is the smallest width that the user can resize the window to, in logical pixels.
	MinWidth int
	// MinHeight is the smallest height that the user can resize the window to, in logical pixels.
	MinHeight int
	// Theme pins the window to one theme. Without it, the window follows the
	// system's app mode (light or dark), live: it uses DarkTheme while Windows
	// is in dark mode and LightTheme otherwise, by default ui.DarkTheme() and
	// ui.LightTheme(). ctx.SetTheme also pins one theme.
	Theme *ui.Theme
	// LightTheme is the theme used in light mode; by default it is ui.LightTheme().
	LightTheme *ui.Theme
	// DarkTheme is the theme used in dark mode; by default it is ui.DarkTheme().
	DarkTheme *ui.Theme
	// Strings is the text that the built-in controls show and announce. Without it,
	// the window uses the display language of Windows (see ui.SystemStrings), and
	// English where that language has no translation or cannot be read. Give
	// ui.StringsFor(tag) to choose a language, or start from ui.EnglishStrings() and
	// set the messages to change. A field left empty shows English (see ui.Strings).
	Strings *ui.Strings
	// Root renders the whole window. It is a component: it re-runs when a
	// signal it reads changes.
	Root func(ctx *ui.Context) ui.Element
	// Zoom is the initial UI zoom (0 = 100%). Users change it with Ctrl+=,
	// Ctrl+-, Ctrl+0 and Ctrl+wheel; OnZoom is called after each change so
	// the app can remember it.
	Zoom float32
	// OnZoom is called after each change of the zoom, with the new zoom.
	OnZoom func(zoom float32)
	// Bindings maps keys to actions for the whole window; see ui.Bind.
	Bindings []ui.Binding
	// KeymapFile is a JSON keymap whose bindings come after Bindings, so they
	// override them; see keymap.Parse. A missing file is fine. The file is
	// reloaded when it changes, and the app keeps running with the bindings it
	// had if the change does not parse. Problems are printed to stderr, and
	// the valid bindings still apply. Actions lists the actions the file may
	// name.
	KeymapFile string
	// Actions lists the actions that KeymapFile may name.
	Actions []*ui.Action
	// OnKeymapLoad is called on the UI thread after the startup load and after
	// each reload that changed the bindings, with nil on success or the
	// problems. Use it to show errors in the app.
	OnKeymapLoad func(err error)

	// Chrome selects who draws the title bar: the operating system (the
	// default) or the app; see ChromeCustom.
	Chrome Chrome
	// X and Y place the window's top-left corner in logical screen pixels.
	// Both zero centers the first window, and cascades a later one 32 pixels
	// from the last window opened.
	X, Y int
	// OnClose is called when the window is asked to close, by the user, by Close on
	// the ui.Window that Open returns, or by the app. Return false to keep the window
	// open, for example to ask about unsaved changes first.
	OnClose func() bool
	// OnClosed is called on the UI thread after the window has closed, or
	// after Open failed to open it.
	OnClosed func()
	// OnGeometry is called on the UI thread as the window is moved, resized or
	// given another scale, with its position and content size in logical pixels,
	// as X, Y, Width and Height are. It is called for each step of a move or a
	// resize, so an app that saves the geometry should wait until it is still.
	OnGeometry func(x, y, width, height int)

	// QuitWhenClosed quits the app when this window closes, even while other
	// windows are open. Before it closes, the OnClose of every other window is
	// asked; if one returns false, nothing closes. Then the other windows close
	// too, each without being asked again. By default the app quits when the last
	// window closes.
	QuitWhenClosed bool
	// CloseToTray, while it holds true, hides the window when the user closes it,
	// instead of closing it, and the app keeps running. The window's taskbar button
	// goes with it. Show (ui.Window's Show) brings the window back, which a tray
	// icon's OnClick does for a window the user hid. The app owns the signal, so it
	// can offer the choice as a setting: a Switch bound to the signal takes effect at
	// the next close. The window hides only while one of the app's tray icons is
	// shown, since without one the user could not show it again; otherwise it closes
	// as usual. OnClose is not asked while the window hides, and the window handle's
	// Close still closes the window, asking OnClose as usual. Nil is false.
	CloseToTray *ui.Signal[bool]
	// contains filtered or unexported fields
}

Window describes an application window and its content.

Jump to

Keyboard shortcuts

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