Your first app

The mental model behind Yoga, and a first window.

Mental model#

Yoga is retained app state + a declarative tree rebuilt every frame (the SwiftUI @State analogue):

  1. Your app is a struct that implements yoga.App — one method, Body(c *ui.Ctx) ui.View.
  2. Body runs on every drawn frame. There is no framework-level widget cache.
  3. Durable state (strings, slices, editors, tables) lives on the app struct.
  4. Micro-state (hover, caret, scroll, open menus) lives in c.Widget(id, …), keyed by a unique id.
  5. ui.View is Layout(c *ui.Ctx) *layout.Element. The DSL (Column, Button, …) returns *ui.Node, which implements View.

Do not keep *ui.Ctx after Body returns. The runtime owns it and resets it each pass.

The event loop is idle until input or an animation request, so idle CPU stays near zero. Each drawn frame builds the tree twice: once to hit-test input against fresh geometry, then again so paint reflects state those events changed.

First window#

yoga.Run creates the window and text engine, then calls build. Construct anything that measures text (editors, tables) inside build, never before Run.

package main

import (
	"github.com/yogaui/yoga"
	"github.com/yogaui/yoga/ui"
)

type App struct {
	name string
}

func Build() *App { return &App{} }

func (a *App) Body(c *ui.Ctx) ui.View {
	th := c.Theme()
	return ui.Column(
		ui.Title("Hello"),
		ui.TextField("name", a.name).
			Placeholder("Your name").
			OnChange(func(s string) { a.name = s }).
			DefaultFocus().
			Grow(1),
		ui.Button("ok", ui.Text("OK")).Primary().OnClick(func() {}),
	).Gap(th.Spacing.M).Padding(th.Spacing.L).Grow(1).Background(ui.TokenSurface)
}

func main() {
	cfg := yoga.Config{
		Title:  "Hello",
		Width:  640,
		Height: 480,
	}
	if err := yoga.Run(cfg, Build); err != nil {
		panic(err)
	}
}

Custom title bar (VS Code-style): set CustomTitleBar: true in yoga.Config, then put menus and tools in ui.TitleBar(...) at the top of Body. macOS keeps native traffic lights; Windows and Linux get framework min/max/close buttons. Empty title-bar area drags the window; double-click toggles maximize. See example/catalog.

cfg := yoga.Config{
	Title:          "My App",
	Width:          1100,
	Height:         720,
	CustomTitleBar: true,
}

func (a *App) Body(c *ui.Ctx) ui.View {
	return ui.Column(
		ui.TitleBar(
			ui.Dropdown("file", "File", fileItems),
			ui.Spacer(),
			ui.TextField("q", a.query).Placeholder("Search…").Width(240),
		),
		mainContent(c).Grow(1),
	).Grow(1).Background(ui.TokenSurface)
}

Child widgets inside TitleBar auto-size to th.Metrics.TitleBarControlHeight (26px) and sit vertically centered in the 36px bar — no need to set .Height() on each control.

Ship a GPU main (//go:build !nogpu) and a headless main (//go:build nogpu) in the same package, as the demos do.

Optional capabilities (detected by type assertion):

  • Close() — stop workers / close files when the window closes.
  • OnKey(c *ui.Ctx, k input.KeyEvent) bool (yoga.KeyHook) — app-global shortcuts before focus routing; return true to consume.
Edit this page on GitHub
esc