window

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 18 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 window runs a Flatte app in a GUI surface instead of a terminal. The app is unchanged — the same State, Handle, and View — and the host is selected with a flatte.Run option:

flatte.Run(ctx, app)                                    // terminal
flatte.Run(ctx, app, window.Options{})                  // native window
flatte.Run(ctx, app, window.Options{}, flatte.WithMouse(flatte.MouseModeCellMotion))
flatte.Run(ctx, app, media.Controls{})                  // macOS terminal media controls

On macOS, Options selects an AppKit window. On js/wasm, the same option selects a browser canvas; without it, Run keeps the cell-grid page.

Run passes the window driver a *flatte.Session. Native callbacks send events and frame requests through its non-blocking Step method; the driver keeps point layout and AppKit painting on its main-thread surfaces. Custom drivers implement flatte.Host in the root package; see the quick reference's "Writing a host" section.

The window draws the app's Frame.Root as real components: Labels in the theme's font by text role, Boxes as rounded strokes (an overlay's box as a raised surface, a titled border's title in its stroke), Inputs as outlined text fields (multi-line ones as text areas) with input-method support, and scroll bars as thin thumbs. Layout runs in points (see layout.LayoutScene): sizes are columns and lines of the UI font, padding is spacing steps, and a border takes a band with its stroke down the middle, as a terminal border takes a cell — so a layout written for the terminal keeps its proportions without terminal chunkiness. Nodes the window does not know — a Text of ANSI output, a container's Chrome, a frame with no Root — show as a monospace cell grid (line-drawing, block, and Powerline characters drawn as shapes that fill their cells), so every existing app runs.

The look is a Theme: colors for what the app does not color itself, the UI and fixed-pitch font families and sizes, line spacing, and corner radius. With no Options.Theme the window follows the system's light or dark appearance as it changes; set one (Dark or Light with fields changed) to fix the look:

th := window.Dark()
th.Accent, th.Radius = color.RGBA{0xff, 0x80, 0x00, 0xff}, -1 // square
flatte.Run(ctx, app, window.Options{Theme: &th})

Geometry reaches the app in cells, as in a terminal: ResizeEvent and LayoutEvent sizes are whole columns and lines, and MouseEvent coordinates are cells whose offset from TargetRect is exact inside the target.

The native backend is macOS-only and uses no cgo: system frameworks are bound at run time through purego. The js/wasm backend paints LayoutScene operations into a 2D canvas, measures text with Canvas, loads Options.Fonts through FontFace before the first frame, and scales the backing store for device pixel ratio. It uses a hidden textarea for keyboard, IME, paste, and caret input. Images are encoded from the app's image.Image source into a Blob and decoded asynchronously with createImageBitmap; failed decodes show a subtle music-note placeholder. Source fetching still follows the browser's CORS rules.

In a page, Options.Theme and Fonts apply. Title is the fallback document title. Desktop, Width, Height, MinWidth, MinHeight, Autosave, HideTitleBar, and Translucent are ignored. Browser Alerts use alert/confirm; file panels, desktop services, menu presentation, and secondary windows are unavailable. On other native platforms Options installs no host and flatte.Run falls back to the terminal. Supported is true on macOS and js/wasm. The separate github.com/lunguini/flatte/window/media package provides media.Controls for terminal runs. It is a fallback host, so it can be passed with Options; the window host already connects media when App.NowPlaying is set. Other genuinely conflicting hosts return flatte.ErrMultipleHosts.

Menus: App.Menus is the native menu bar, between the application menu (Quit, ⌘Q) and the Window menu (Close, ⌘W); an item's first ⌘ shortcut is its native key equivalent, and its other shortcuts are routed by the host. App.ContextMenu shows as a native popup on a right-click or Control-click. Choices arrive as MenuEvents. An item with the ⌘C shortcut copies selected Label text instead, when there is some. The browser canvas routes App.Menus shortcuts as MenuEvents, but does not draw the menu bar or context popups.

Windows: App.Windows declares secondary windows, each opened as its own native window drawn by View (RenderContext.Window); the close button sends a WindowCloseRequest, and key changes a WindowFocused. Options.MinWidth, MinHeight, and Autosave set the main window's minimum size and saved frame; the Window menu minimizes, zooms, and enters full screen. The browser canvas does not open secondary windows.

Dialogs: OpenFile, SaveFile, and Alert show the system panels and alerts, run after the handler that asked returns. Files or text dropped on the window arrive as a DropEvent. In a browser, Alert uses alert/confirm, file panels report ErrNoDialogs, and drops include file names and plain text.

Editing: the clipboard effects use the system pasteboard (ReadClipboard answers with a ClipboardEvent), ⌘V delivers a PasteEvent unless a menu item claims it, and ⌘ chords no menu item claims arrive as keys with ModSuper — except ⌘← and ⌘→, which arrive as Home and End (with Shift when held). Pointer events over an Input carry the caret under the pointer, and ↑/↓ in a focused multi-line Input carry the caret a visual row away, both in the window's own text layout (see MouseEvent.Caret, KeyEvent.Caret). The caret blinks.

Differences from the terminal host: Ctrl-C is delivered to Handle (quit with ⌘Q or by closing the window); Effects.Print, Suspend, and Exec are no-ops; Frame.Cursor shows only for frames without a Root (a Root frame's cursor is the focused Input's caret).

Index

Constants

View Source
const (
	TitleBarHeight       = 28
	TitleBarButtonsWidth = 78
)

The window buttons' corner of a window with HideTitleBar, in points.

Variables

View Source
var ErrUnsupported = errors.New("window: not supported on this platform")

ErrUnsupported is returned by Snapshot on platforms without a window backend. Window Options fall back to Run's default host on those platforms.

Functions

func Snapshot

func Snapshot[S any](app flatte.App[S], opts Options, scale float64) (image.Image, error)

Snapshot renders app's first frame — after Init, the first ResizeEvent, and layout settling — offscreen at scale pixels per point, for visual tests and screenshots. It needs no display and may be called from any goroutine.

func Supported

func Supported() bool

Types

type Options

type Options struct {
	// Desktop opts into a background status-menu utility. Its main window
	// starts hidden, floats when shown, and hides instead of quitting on
	// close. It uses the same semantic renderer and native input as a normal
	// window. Nil keeps ordinary window behavior unchanged.
	Desktop *flatte.Desktop
	// Title is the window title until the app's Frame.Title sets one.
	Title string
	// Width and Height are the initial content size in columns and lines of
	// the UI font. Zero means 100×32.
	Width, Height int
	// Theme is the window's look. Nil follows the system's appearance,
	// switching between Dark and Light as it changes; Snapshot draws Dark.
	Theme *Theme
	// Appearance fixes the design system's appearance (AppearanceLight or
	// AppearanceDark) instead of following the system; Unknown follows it
	// (Snapshot draws Dark). It applies when Theme is nil.
	Appearance flatte.Appearance
	// MinWidth and MinHeight are the smallest content size in columns and
	// lines (at least 20×4).
	MinWidth, MinHeight int
	// Autosave, when set, names the window's saved frame: its position and
	// size are restored from it at launch and saved as they change.
	Autosave string
	// HideTitleBar extends the content under the title bar: the frame fills
	// the whole window and the window's buttons float over its top-left
	// corner, which the app leaves clear (TitleBarHeight tall,
	// TitleBarButtonsWidth wide). The strip still drags the window.
	HideTitleBar bool
	// TitleBarHeight, with HideTitleBar, is the strip's height in points
	// when the app draws a taller one (a toolbar, a tab strip): the whole
	// strip drags the window and the window buttons sit in its middle.
	// Zero or less than the system's TitleBarHeight keeps the system's.
	TitleBarHeight int
	// Translucent shows the desktop, blurred, through the window wherever
	// the frame draws no background of its own — for good; a frame's
	// Frame.Translucent turns it on frame by frame instead.
	Translucent bool
	// Fonts are font files (TrueType or OpenType) for this process to use
	// before the window draws, so Theme.Font can name a family the system
	// does not have — an app embeds its font and names it.
	Fonts [][]byte
}

Options configures the window.

func (Options) ConfigureRun

func (Options) ConfigureRun(*flatte.RunConfig)

type Theme

type Theme struct {
	Background, Surface color.Color
	Text, Subtle, Title color.Color
	Border, Field       color.Color
	// Accent marks focus: field outlines, carets, input-method text, and
	// meters without a Fill. Selection highlights selected rows.
	Accent, Selection color.Color
	// Track is a meter's unfilled bar.
	Track color.Color

	// Font is the UI font family and MonoFont the family of terminal cell
	// grids ("": the system UI font and the user's fixed-pitch font).
	Font, MonoFont string
	// FontSize is the body and cell-grid size and TitleSize the title size,
	// in points; LineSpacing is a line's height over its font's. Columns and
	// lines are measured in this type, so a larger size makes a larger
	// window for the same Options.Width and Height.
	FontSize, TitleSize, LineSpacing float64
	// ControlHeight is the minimum Auto height of regular controls in points.
	// Zero inherits the base theme (32pt); negative disables the minimum.
	// Explicit node heights and Bare/multiline fields are unaffected.
	ControlHeight float64
	// Radius is a plain border's corner radius in points (a rounded border
	// is twice as round); negative draws square corners.
	Radius float64
	// ANSI is the palette cell grids draw the 16 ANSI colors in (0–7
	// normal, 8–15 bright), as a terminal profile does: program output
	// asks for "blue", and the theme decides which blue reads on its
	// background. Nil entries take the base theme's. Colors 16–255 and
	// true colors are drawn as given.
	ANSI [16]color.Color
	// contains filtered or unexported fields
}

Theme is a window's look: the colors of everything the app does not color itself — the window, raised surfaces, text roles, borders, fields, and the accent used for focus, carets, and meters — and the type and corner shape the layout is measured and drawn in. Start from Dark or Light and change fields; zero fields take the base theme's value (Dark's, for a Theme built from scratch).

func Dark

func Dark() Theme

Dark is the default dark theme: theme.Default()'s dark palette.

func Light

func Light() Theme

Light is the default light theme: theme.Default()'s light palette.

Directories

Path Synopsis
internal
appkit
Package appkit contains the shared AppKit media backend.
Package appkit contains the shared AppKit media backend.
Package media adds native system media controls to a terminal Run.
Package media adds native system media controls to a terminal Run.

Jump to

Keyboard shortcuts

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