renderer

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package renderer decouples a miniui UI from the backend that draws it.

miniui itself never draws anything: each frame it produces a list of draw commands (filled rectangles, text, icons, clip regions). This package turns that command list into calls on a small Renderer interface, so the same UI code runs unchanged on any backend — a terminal, a GPU canvas, a web canvas, and so on. Swapping the engine is swapping the Renderer.

The flow is:

term, _ := renderer.NewTerminal()      // pick a backend (the engine)
renderer.Run(term, func(ctx *miniui.Context) {
    // build the UI; it only ever sees *miniui.Context
    if ctx.BeginWindow("Hi", miniui.Rect{X: 1, Y: 1, W: 30, H: 8}) != 0 {
        ctx.Button("OK")
        ctx.EndWindow()
    }
})

Run owns the frame loop: it pumps input through the Driver, calls your UI builder between Begin and End, then hands the finished command list to Paint, which dispatches every command to the Renderer. Because the UI builder takes only a *miniui.Context, the exact same function works with any Driver.

Index

Constants

This section is empty.

Variables

View Source
var DefaultBackground = microui.RGBA(40, 44, 52, 255)

DefaultBackground is the color empty cells are cleared to. Terminal.BG starts here and may be changed at any time (e.g. from a slider in the UI).

Functions

func Connect

func Connect(ctx *miniui.Context, r Renderer)

Connect wires a renderer's text metrics into ctx as its layout callbacks. Run does this for you; call it directly only when driving the loop yourself.

func Paint

func Paint(r Renderer, ctx *miniui.Context, bg miniui.Color)

Paint renders one finished miniui frame through r and presents it. Call it after ctx.End(). This is the bridge between miniui's command list and a Renderer: every draw command becomes one method call on r.

func Run

func Run(d Driver, ui func(ctx *miniui.Context)) error

Run drives a Driver's frame loop until the user quits, then closes it.

Each frame it polls input into a fresh context, calls ui to build the frame, then paints and presents it. The ui builder receives only a *miniui.Context, so it is completely independent of which Driver is running — that is what makes the engine swappable.

Types

type Driver

type Driver interface {
	Renderer

	// Style returns the style this backend wants the context to use (metrics
	// tuned for its units). Run installs it before the first frame.
	Style() miniui.Style

	// Background returns the color the surface is cleared to each frame. It is
	// read every frame, so a UI may change it live.
	Background() miniui.Color

	// Poll drains pending platform input into ctx and reports whether the user
	// asked to quit (closed the surface, pressed Ctrl-C, reached EOF). It must
	// not block, so Run can keep a steady frame rate.
	Poll(ctx *miniui.Context) (quit bool)

	// Close restores any platform state. It is safe to call more than once.
	Close() error
}

Driver is a Renderer that can also run a live UI: it supplies the style and background to use, pumps platform input into the context, and tears itself down. Run drives it. Concrete drivers (e.g. Terminal) live in this package; adding a new engine means implementing this interface.

type Renderer

type Renderer interface {
	// TextWidth and TextHeight measure rendered text in the surface's own units
	// (pixels, character cells, ...). They are wired into the context as its
	// layout callbacks by Connect.
	TextWidth(font miniui.Font, str string) int
	TextHeight(font miniui.Font) int

	// Size reports the drawable area in surface units.
	Size() (w, h int)

	// Clear begins a frame: it fills the whole surface with bg and resets the
	// clip region to the full surface.
	Clear(bg miniui.Color)

	// SetClip restricts following draws to rect (intersected with the surface).
	SetClip(rect miniui.Rect)

	// FillRect fills rect with a solid color.
	FillRect(rect miniui.Rect, color miniui.Color)

	// DrawText draws str with its top-left corner at pos.
	DrawText(font miniui.Font, str string, pos miniui.Vec2, color miniui.Color)

	// DrawIcon draws a built-in icon (miniui.IconClose, IconCheck, ...) inside
	// rect.
	DrawIcon(id int, rect miniui.Rect, color miniui.Color)

	// Present flushes the finished frame to the display.
	Present()
}

Renderer is a drawing backend for miniui. Paint walks a frame's command list and calls these methods in order; a backend only has to know how to measure text and paint the four primitives onto its surface.

Clipping is modelled as a scissor rectangle: SetClip sets the region that subsequent FillRect / DrawText / DrawIcon calls must stay within, and Clear resets it to the whole surface. (miniui already pre-clips rectangles, but it relies on the scissor for text and icons.)

type Terminal

type Terminal struct {
	// BG is the background color used to clear the surface each frame. Exported
	// so a UI can recolor the backdrop live; defaults to DefaultBackground.
	BG microui.Color
	// contains filtered or unexported fields
}

Terminal is the default microui Driver: it renders the command list to an ANSI terminal (truecolor when advertised via COLORTERM, 256-color otherwise), treating each character cell as one "pixel", drawing text in the terminal font, and reading input from SGR mouse reporting plus the keyboard. It uses only the standard library (raw mode is set via stty), so there is no cgo and no third-party dependency.

Create one with NewTerminal and drive it with Run. When stdout/stdin is not a terminal, NewTerminal returns a headless Terminal that paints to an in-memory surface and prints a single plain-text frame — handy for piping and tests.

func NewHeadless

func NewHeadless(w, h int) *Terminal

NewHeadless creates a non-interactive terminal renderer drawing to an in-memory w*h surface. Present writes the surface as plain text. It is used for tests and for piping a single rendered frame.

func NewTerminal

func NewTerminal() (*Terminal, error)

NewTerminal creates a terminal renderer. If stdin/stdout is a real terminal it switches to raw mode, enables the alternate screen and mouse reporting, and reads live input. Otherwise it returns a headless 80x24 renderer that emits a single plain-text frame (see Run / Present).

func (*Terminal) Background

func (t *Terminal) Background() microui.Color

Background returns the current clear color.

func (*Terminal) Clear

func (t *Terminal) Clear(bg microui.Color)

Clear fills the whole surface with bg and resets the clip region.

func (*Terminal) Close

func (t *Terminal) Close() error

Close restores the terminal to its original state. Safe to call repeatedly.

func (*Terminal) DrawIcon

func (t *Terminal) DrawIcon(id int, rect microui.Rect, color microui.Color)

DrawIcon draws a single glyph centered within rect.

func (*Terminal) DrawText

func (t *Terminal) DrawText(_ microui.Font, str string, pos microui.Vec2, color microui.Color)

DrawText writes str starting at pos, one rune per cell, on top of whatever background is already there.

func (*Terminal) FillRect

func (t *Terminal) FillRect(rect microui.Rect, color microui.Color)

FillRect paints rect with color as the cell background.

func (*Terminal) Poll

func (t *Terminal) Poll(ctx *microui.Context) bool

Poll drains pending input into ctx (handling resize) and reports whether to quit. In headless mode it returns true once a single frame has been drawn.

func (*Terminal) Present

func (t *Terminal) Present()

Present flushes the frame: an ANSI string in interactive mode (truecolor when the terminal supports it, 256-color otherwise), or a plain-text dump.

func (*Terminal) SetClip

func (t *Terminal) SetClip(rect microui.Rect)

SetClip restricts following draws to rect (intersected with the surface).

func (*Terminal) Size

func (t *Terminal) Size() (w, h int)

Size reports the surface size in cells.

func (*Terminal) Style

func (t *Terminal) Style() microui.Style

Style returns a microui style with metrics tuned for character-cell units.

func (*Terminal) TextHeight

func (t *Terminal) TextHeight(_ microui.Font) int

TextHeight returns the height of a line of text: one cell.

func (*Terminal) TextWidth

func (t *Terminal) TextWidth(_ microui.Font, s string) int

TextWidth returns the number of cells a string occupies (one per rune).

Jump to

Keyboard shortcuts

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