spiffy

package module
v0.0.0-...-6c2c960 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

README

spiffy

spiffy is a small retained-mode UI layer for gctx2d and GoGPU. Build each UI once, update its retained widgets when state changes, and call Draw wherever that UI belongs in a scene.

Creating and switching UIs

var (
	menuUI *spiffy.UserInterface = spiffy.New(ctx)
	gameUI *spiffy.UserInterface = spiffy.New(ctx).SetActive(false).SetVisible(false)
)

menuUI.SetClipboard(app)

var name *spiffy.TextInput = menuUI.NewTextInput(
	spiffy.Rect{Width: 240, Height: 44},
	"Player name",
	func(input *spiffy.TextInput, value string) {
		// Persist the new value.
	},
)

var play *spiffy.Button = menuUI.NewButton(
	spiffy.Rect{Width: 240, Height: 44},
	"Play",
	func(button *spiffy.Button) {
		menuUI.SetActive(false).SetVisible(false)
		gameUI.SetActive(true).SetVisible(true)
	},
)

var column *spiffy.Container = menuUI.Column(spiffy.Rect{X: 40, Y: 40, Width: 240, Height: 104})
column.Gap = 16
column.Align = spiffy.AlignStretch
column.Add(name, 0).Add(play, 0)

Draw explicitly during the scene:

menuUI.Draw()
gameUI.Draw()

Use DrawTo(ctx) to render the same retained UI through a different gctx2d.Context. Use Migrate(ctx) when that context should become the UI's new default; retained widgets, layout, and values are preserved, and text measurements are refreshed.

Animated windows and popups

Show and Hide animate a complete retained UI as one window. The default is a macOS-inspired centered zoom, slight vertical lift, opacity fade, and soft overshooting settle. Closing is shorter and eases away cleanly:

popup.SetVisible(false)

popup.Show()
popup.Hide(func() {
	// Restore the underlying screen after the close animation finishes.
})

Input is enabled after opening finishes and disabled as soon as closing begins. The UI remains visible while closing so callers should continue drawing it until the Hide callback runs. Customize the motion per UI:

popup.SetWindowAnimation(spiffy.WindowAnimationStyle{
	Open: spiffy.TransitionStyle{
		Duration: 280 * time.Millisecond,
		Easing:   spiffy.EaseOutBack,
	},
	Close: spiffy.TransitionStyle{
		Duration: 170 * time.Millisecond,
		Easing:   spiffy.EaseIn,
	},
	MinScale: 0.95,
	OffsetY:  12,
	Fade:     true,
})

Zero open or close durations make that direction immediate. SetVisible remains the immediate visibility API when no lifecycle animation is desired.

Windows can also be dragged from any non-interactive layout item, such as a retained title label. An optional drag area keeps the full window onscreen:

popup.SetWindowDragHandle(title)
popup.SetWindowDragArea(spiffy.Rect{Width: screenWidth, Height: screenHeight})

Dragging translates drawing and pointer input together without mutating retained widget or layout bounds. Use WindowOffset to read the position, SetWindowOffset to restore a saved position, and ResetWindowOffset to return to the layout-defined location. ClearWindowDragArea removes containment.

Input hookup

The package deliberately does not register itself with app.EventSource(). GoGPU stores one callback per event type, so the application should own those callbacks and route them to whichever UI is active:

var events gpucontext.EventSource = app.EventSource()

events.OnMouseMove(func(x, y float64) {
	activeUI.OnMouseMove(x, y)
})

events.OnMousePress(func(button gpucontext.MouseButton, x, y float64) {
	activeUI.OnMousePress(button, x, y)
})

events.OnMouseRelease(func(button gpucontext.MouseButton, x, y float64) {
	activeUI.OnMouseRelease(button, x, y)
})

events.OnKeyPress(func(key gpucontext.Key, modifiers gpucontext.Modifiers) {
	activeUI.OnKeyPress(key, modifiers)
})

events.OnKeyRelease(func(key gpucontext.Key, modifiers gpucontext.Modifiers) {
	activeUI.OnKeyRelease(key, modifiers)
})

events.OnTextInput(func(text string) {
	activeUI.OnTextInput(text)
})

Input methods return whether the UI handled the event, allowing an application-level router to stop propagation when appropriate.

Text inputs use gctx2d's active font atlas for exact variable-width glyph advances. Caret placement, horizontal clipping, mouse drag selection, and selection highlighting therefore use the same metrics as rendering. Keyboard editing supports Shift selection, Ctrl/Alt word movement and deletion, Ctrl/Command+A, and clipboard copy/cut/paste when SetClipboard is configured.

Caret motion, blinking, and selection highlighting are independently tunable:

input.Animation = spiffy.TextInputAnimationStyle{
	Cursor: spiffy.TransitionStyle{
		Duration: 90 * time.Millisecond,
		Easing:   spiffy.EaseOut,
	},
	CursorBlink:     1200 * time.Millisecond,
	CursorBlinkFade: 140 * time.Millisecond,
	Highlight: spiffy.TransitionStyle{
		Duration: 160 * time.Millisecond,
		Easing:   spiffy.EaseInOut,
	},
}

Cursor animates caret movement through the measured glyph positions. CursorBlink is the complete visible/hidden cycle, and CursorBlinkFade controls the soft edge around each half-cycle. A zero blink duration keeps the caret steadily visible. Highlight controls selection fade-in and fade-out. Assigning TextInputAnimationStyle{} disables all text-input animation.

Styling and layout

Each widget owns value-copy Style and TextStyle fields, so changing one widget does not mutate global defaults. The initial colors, borders, and hover/press mixing match the original Latticefall render UI.

Interactive styles also own animation settings. Transition animates visual value changes while the control's logical value and callbacks update immediately. A zero duration disables the transition. Click supports scale, pulse, bounce, or no activation feedback:

toggle.Style.Transition = spiffy.TransitionStyle{
	Duration: 220 * time.Millisecond,
	Easing:   spiffy.EaseInOut,
}

radio.Style.Transition = spiffy.TransitionStyle{
	Duration: 180 * time.Millisecond,
	Easing:   spiffy.EaseOut,
	Mode:     spiffy.TransitionCrossFade,
}

button.Style.Click = spiffy.ClickAnimationStyle{
	Type:      spiffy.ClickAnimationBounce,
	Duration:  160 * time.Millisecond,
	Easing:    spiffy.EaseOut,
	Scale:     0.95,
	MixAmount: 0.18,
}

Checkboxes, toggles, radios, radio groups, sliders, switchers, and dropdown selection changes use Style.Transition. Buttons use the scale click animation by default, while compact controls use a pulse. Assign TransitionStyle{} or ClickAnimationStyle{} to disable either effect.

Radio controls default to interpolation: standalone indicators grow or shrink, and radio-group indicators slide between choices. Set Mode to TransitionCrossFade to fade the old indicator out in place while the new one fades in. The same mode works for standalone radios by fading indicator opacity.

Sliders reserve a value column to the right of the track by default. Adjust ValueWidth and ValueGap when a longer formatted value needs more room. Slider[T] preserves integer or floating-point values and callback types. Use the package constructor for type inference; the UI method remains a convenient float32-compatible API:

var volume *spiffy.Slider[int] = spiffy.NewSlider(
	ui,
	spiffy.Rect{Width: 240, Height: 40},
	"Volume",
	0,
	100,
	75,
	func(slider *spiffy.Slider[int], value int) {
		// Persist value.
	},
)

volume.SetStep(5)        // Snap to increments of five.
volume.SetStep(5, false) // Keep the step without quantizing to it.
volume.SetValueFormat("%d%%")

SetValueFormat is fluent and panics immediately when its single numeric target is incompatible with T. Integer sliders accept integer verbs such as d and x; floating sliders accept verbs such as f, e, and g. Use %% for a literal percent sign. Normal flags, widths, and precisions are supported.

Kerning is disabled in the default TextStyle because some system fonts contain unusually aggressive UI kerning pairs. Set widget.TextStyle.Kerning = true when typographic kerning is desired; measurement and rendering remain synchronized either way.

Text borders are optional and disabled by default. Set TextStyle.BorderWidth to a positive value, then customize BorderColor, BorderCap, and BorderJoin as needed. Borders are drawn before the text fill so the fill remains crisp.

Row and Column containers support padding, gaps, cross-axis alignment, primary-axis justification, fixed-size items, and grow weights. Tracked containers are reapplied before each draw; calling SetBounds also lays them out immediately for resize handling.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MixColors

func MixColors(first, second gctx2d.Color, amount float32) (color gctx2d.Color)

MixColors linearly blends two colors and clamps the blend amount.

Types

type Align

type Align uint8

Align controls placement on the cross axis.

const (
	AlignStart Align = iota
	AlignCenter
	AlignEnd
	AlignStretch
)

type Axis

type Axis uint8

Axis selects the primary direction of a stack layout.

const (
	Horizontal Axis = iota
	Vertical
)

type Button

type Button struct {
	*Element
	Text      string
	TextStyle TextStyle
	OnClick   func(self *Button)
}

Button is a retained clickable button.

func (*Button) SetDisabled

func (button *Button) SetDisabled(disabled bool) *Button

SetDisabled updates the button's disabled state.

func (*Button) SetText

func (button *Button) SetText(text string) *Button

SetText updates a button label.

type CheckState

type CheckState uint8

CheckState is the exact state of a two- or three-state checkbox.

const (
	CheckUnchecked CheckState = iota
	CheckChecked
	CheckIndeterminate
)

type Checkbox

type Checkbox struct {
	*Element
	Label                                            string
	TextStyle                                        TextStyle
	UncheckedStyle, CheckedStyle, IndeterminateStyle Style
	CheckColor                                       gctx2d.Color
	State                                            CheckState
	Checked, ThreeState                              bool
	OnChange                                         func(self *Checkbox, checked bool)
	OnStateChange                                    func(self *Checkbox, state CheckState)
	// contains filtered or unexported fields
}

Checkbox is a retained boolean input with an optional label.

func (*Checkbox) SetChecked

func (checkbox *Checkbox) SetChecked(checked bool) *Checkbox

SetChecked updates the checkbox and notifies its change handler.

func (*Checkbox) SetState

func (checkbox *Checkbox) SetState(state CheckState) *Checkbox

SetState updates the exact checkbox state and notifies its handlers.

func (*Checkbox) SetThreeState

func (checkbox *Checkbox) SetThreeState(enabled bool) *Checkbox

SetThreeState controls whether pointer activation cycles through indeterminate.

type ClickAnimation

type ClickAnimation uint8

ClickAnimation selects the visual feedback produced by activation.

const (
	ClickAnimationNone ClickAnimation = iota
	ClickAnimationScale
	ClickAnimationPulse
	ClickAnimationBounce
)

type ClickAnimationStyle

type ClickAnimationStyle struct {
	Type      ClickAnimation
	Duration  time.Duration
	Easing    Easing
	Scale     float32
	MixAmount float32
}

ClickAnimationStyle controls press and activation feedback.

func DefaultClickAnimationStyle

func DefaultClickAnimationStyle() (style ClickAnimationStyle)

DefaultClickAnimationStyle returns the standard button press animation.

type Clipboard

type Clipboard interface {
	ClipboardRead() (text string, err error)
	ClipboardWrite(text string) error
}

Clipboard is implemented by gogpu.App and gpucontext platform providers.

type Container

type Container struct {
	Bounds    Rect
	Direction Axis
	Padding   Insets
	Gap       float32
	Align     Align
	Justify   Justify
	// contains filtered or unexported fields
}

Container applies a lightweight row or column layout to retained items.

func NewColumn

func NewColumn(bounds Rect) (container *Container)

NewColumn creates an untracked vertical container.

func NewRow

func NewRow(bounds Rect) (container *Container)

NewRow creates an untracked horizontal container.

func (*Container) Add

func (container *Container) Add(item LayoutItem, grow float32) *Container

Add appends an item; positive grow values divide remaining primary-axis space.

func (*Container) Clear

func (container *Container) Clear() *Container

Clear removes all items while retaining the container allocation.

func (*Container) Count

func (container *Container) Count() int

Count returns the number of layout items.

func (*Container) GetBounds

func (container *Container) GetBounds() Rect

GetBounds returns the container bounds for nesting.

func (*Container) Layout

func (container *Container) Layout()

Layout positions all items without allocating.

func (*Container) Remove

func (container *Container) Remove(item LayoutItem) *Container

Remove detaches an item from this layout.

func (*Container) SetBounds

func (container *Container) SetBounds(bounds Rect)

SetBounds updates the container and immediately reapplies its layout.

type Cursor

type Cursor interface {
	SetCursor(cursor gpucontext.CursorShape)
}

Cursor changes the platform pointer shape; gogpu.App implements it.

type Dropdown struct {
	*Element
	Options      []string
	Selected     int
	Expanded     bool
	TextStyle    TextStyle
	OptionHeight float32
	OnChange     func(self *Dropdown, selected int, value string)
	// contains filtered or unexported fields
}

Dropdown is an expandable fixed-option selection input.

func (dropdown *Dropdown) SetSelected(selected int) *Dropdown

SetSelected updates the dropdown selection and notifies its handler.

func (dropdown *Dropdown) Value() (value string)

Value returns the currently selected dropdown option.

type Easing

type Easing uint8

Easing controls how an animated value advances through a transition.

const (
	EaseLinear Easing = iota
	EaseIn
	EaseOut
	EaseInOut
	EaseOutBack
)

type Element

type Element struct {
	Bounds                         Rect
	Style                          Style
	Visible, Disabled, Interactive bool
	Hovered, Pressed               bool
	// contains filtered or unexported fields
}

Element holds the state shared by every interactive element.

func (*Element) BringToFront

func (element *Element) BringToFront()

BringToFront moves the element to the top of draw and hit-test order.

func (*Element) ContainsPoint

func (element *Element) ContainsPoint(x, y float32) (contains bool)

ContainsPoint performs a rounded-rectangle hit test.

func (*Element) GetBounds

func (element *Element) GetBounds() Rect

GetBounds returns the current bounds for layout use.

func (*Element) Remove

func (element *Element) Remove()

Remove detaches an element from its UI.

func (*Element) SetBounds

func (element *Element) SetBounds(bounds Rect)

SetBounds updates an element's position and size.

func (*Element) SetDisabled

func (element *Element) SetDisabled(disabled bool) *Element

SetDisabled updates the common disabled state.

func (*Element) SetPosition

func (element *Element) SetPosition(x, y float32) *Element

SetPosition updates an element's position without changing its size.

func (*Element) SetSize

func (element *Element) SetSize(width, height float32) *Element

SetSize updates an element's size without changing its position.

type Form

type Form struct {
	Bounds      Rect
	LabelSide   LabelSide
	LabelWidth  float32
	RowHeight   float32
	Gap, RowGap float32
	// contains filtered or unexported fields
}

Form aligns corresponding labels and controls into consistent rows.

func (*Form) Add

func (form *Form) Add(label *Label, control LayoutItem) *Form

Add appends a corresponding label and control row.

func (*Form) GetBounds

func (form *Form) GetBounds() Rect

GetBounds returns the form bounds for nesting.

func (*Form) Layout

func (form *Form) Layout()

Layout aligns every form label and control without allocating.

func (*Form) SetBounds

func (form *Form) SetBounds(bounds Rect)

SetBounds updates the form and immediately reapplies its layout.

type Insets

type Insets struct {
	Top, Right, Bottom, Left float32
}

Insets describes spacing on each edge of a rectangle.

type Justify

type Justify uint8

Justify controls placement on the primary axis.

const (
	JustifyStart Justify = iota
	JustifyCenter
	JustifyEnd
	JustifySpaceBetween
)

type Label

type Label struct {
	*Element
	Text      string
	TextStyle TextStyle
}

Label is retained text that can participate in layouts.

type LabelSide

type LabelSide uint8

LabelSide selects which side of a form row contains its label.

const (
	LabelLeading LabelSide = iota
	LabelTrailing
)

type LayoutItem

type LayoutItem interface {
	GetBounds() Rect
	SetBounds(bounds Rect)
}

LayoutItem can be positioned by a Container.

type Numeric

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64
}

Numeric is the set of integer and floating-point values supported by Slider.

type Panel

type Panel struct {
	*Element
}

Panel is a retained non-interactive background surface.

type Radio

type Radio struct {
	*Element
	Label     string
	Group     string
	TextStyle TextStyle
	Checked   bool
	OnChange  func(self *Radio, checked bool)
	// contains filtered or unexported fields
}

Radio is a retained mutually exclusive choice within a named group.

func (*Radio) SetChecked

func (radio *Radio) SetChecked(checked bool) *Radio

SetChecked updates this radio and enforces its group exclusivity.

type RadioGroup

type RadioGroup struct {
	*Element
	Label                 string
	Choices               []string
	Selected              int
	LabelStyle, TextStyle TextStyle
	OnChange              func(self *RadioGroup, selected int, value string)
	// contains filtered or unexported fields
}

RadioGroup owns a labeled, mutually exclusive set of radio options.

func (*RadioGroup) SetSelected

func (group *RadioGroup) SetSelected(selected int) *RadioGroup

SetSelected updates the selected radio option and notifies its handler.

func (*RadioGroup) Value

func (group *RadioGroup) Value() (value string)

Value returns the currently selected radio option.

type Rect

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

Rect describes an element's position and size in pixels.

type Select

type Select struct {
	*Element
	Label, ValueSeparator string
	Options               []string
	Selected              int
	TextStyle             TextStyle
	OnChange              func(self *Select, selected int, value string)
	// contains filtered or unexported fields
}

Select is a retained input that cycles through a fixed set of options.

func (*Select) SetSelected

func (selectInput *Select) SetSelected(selected int) *Select

SetSelected updates the selected option and notifies its change handler.

func (*Select) Value

func (selectInput *Select) Value() (value string)

Value returns the currently selected option.

type Slider

type Slider[T Numeric] struct {
	*Element
	Label                     string
	TextStyle                 TextStyle
	Min, Max, Step, Value     T
	SnapStep                  bool
	TrackColor, ProgressColor gctx2d.Color
	TrackWidth, KnobRadius    float32
	ValueWidth, ValueGap      float32
	OnChange                  func(self *Slider[T], value T)
	// contains filtered or unexported fields
}

Slider is a retained numeric input controlled by pointer dragging.

func NewSlider

func NewSlider[T Numeric](ui *UserInterface, bounds Rect, label string, minimum, maximum, value T, onChange func(self *Slider[T], value T)) (slider *Slider[T])

NewSlider adds a bounded slider whose values and callback retain numeric type T.

func (*Slider[T]) SetStep

func (slider *Slider[T]) SetStep(step T, snaps ...bool) *Slider[T]

SetStep sets the value interval and optionally disables pointer snapping.

func (*Slider[T]) SetValue

func (slider *Slider[T]) SetValue(value T) *Slider[T]

SetValue clamps and optionally snaps the slider value, then notifies its handler.

func (*Slider[T]) SetValueFormat

func (slider *Slider[T]) SetValueFormat(format string) *Slider[T]

SetValueFormat applies a type-compatible numeric format or panics.

func (*Slider[T]) ValueFormat

func (slider *Slider[T]) ValueFormat() string

ValueFormat returns the format used for the displayed slider value.

type Style

type Style struct {
	FillColor, BorderColor, HoverMixColor, DisabledMixColor gctx2d.Color
	FocusBorderColor                                        gctx2d.Color
	BorderWidth, BorderRadius                               float32
	HoverMixAmount, PressMixAmount, DisabledMixAmount       float32
	Transition                                              TransitionStyle
	Click                                                   ClickAnimationStyle
}

Style controls the shared box styling for interactive elements.

func DefaultButtonStyle

func DefaultButtonStyle() (style Style)

DefaultButtonStyle returns the original render-package button styling.

func DefaultCheckboxStyles

func DefaultCheckboxStyles() (unchecked, checked, indeterminate Style)

DefaultCheckboxStyles returns distinct visual styles for every checkbox state.

func DefaultControlStyle

func DefaultControlStyle() (style Style)

DefaultControlStyle returns a transparent style for standalone controls.

func DefaultPanelStyle

func DefaultPanelStyle() (style Style)

DefaultPanelStyle returns a dark modal-surface style.

func DefaultTextInputStyle

func DefaultTextInputStyle() (style Style)

DefaultTextInputStyle returns a button-compatible text input style.

type Switcher

type Switcher = Select

Switcher is the descriptive name for the cycling Select control.

type TextInput

type TextInput struct {
	*Element
	Value, Placeholder                            string
	TextStyle                                     TextStyle
	Animation                                     TextInputAnimationStyle
	PlaceholderColor, CursorColor, SelectionColor gctx2d.Color
	Padding                                       Insets
	MaxLength                                     int
	OnChange                                      func(self *TextInput, value string)
	OnSubmit                                      func(self *TextInput, value string)
	// contains filtered or unexported fields
}

TextInput is a retained, single-line editable text field.

func (*TextInput) Blur

func (input *TextInput) Blur() *TextInput

Blur removes keyboard focus from this input.

func (*TextInput) Cursor

func (input *TextInput) Cursor() int

Cursor returns the insertion point as a rune index.

func (*TextInput) Focus

func (input *TextInput) Focus() *TextInput

Focus gives this input keyboard and text input focus.

func (*TextInput) Focused

func (input *TextInput) Focused() bool

Focused reports whether this input currently receives keyboard events.

func (*TextInput) Select

func (input *TextInput) Select(start, end int) *TextInput

Select sets a rune-indexed selection with the caret at end.

func (*TextInput) SelectAll

func (input *TextInput) SelectAll() *TextInput

SelectAll selects the complete input value.

func (*TextInput) SelectedText

func (input *TextInput) SelectedText() (text string)

SelectedText returns the currently selected text.

func (*TextInput) Selection

func (input *TextInput) Selection() (start, end int)

Selection returns the ordered rune range of the current selection.

func (*TextInput) SetCursor

func (input *TextInput) SetCursor(cursor int) *TextInput

SetCursor moves the insertion point and clears the selection.

func (*TextInput) SetDisabled

func (input *TextInput) SetDisabled(disabled bool) *TextInput

SetDisabled updates the input's disabled state.

func (*TextInput) SetMaxLength

func (input *TextInput) SetMaxLength(maxLength int) *TextInput

SetMaxLength limits the value by rune count; zero means unlimited.

func (*TextInput) SetValue

func (input *TextInput) SetValue(value string) *TextInput

SetValue replaces the input value and places the cursor at the end.

type TextInputAnimationStyle

type TextInputAnimationStyle struct {
	Cursor          TransitionStyle
	CursorBlink     time.Duration
	CursorBlinkFade time.Duration
	Highlight       TransitionStyle
}

TextInputAnimationStyle controls caret and selection-highlight animation.

func DefaultTextInputAnimationStyle

func DefaultTextInputAnimationStyle() (style TextInputAnimationStyle)

DefaultTextInputAnimationStyle returns smooth caret and highlight animation.

type TextStyle

type TextStyle struct {
	Color       gctx2d.Color
	BorderColor gctx2d.Color
	Font        *gctx2d.FontHandle
	FontSize    float64
	BorderWidth float32
	Weight      gctx2d.FontWeight
	Align       gctx2d.TextAlign
	Baseline    gctx2d.TextBaseline
	BorderCap   gctx2d.LineCap
	BorderJoin  gctx2d.LineJoin
	Kerning     bool
}

TextStyle controls font, fill, and optional border styling.

func DefaultButtonTextStyle

func DefaultButtonTextStyle() (style TextStyle)

DefaultButtonTextStyle returns centered blue button text styling.

func DefaultInputTextStyle

func DefaultInputTextStyle() (style TextStyle)

DefaultInputTextStyle returns left-aligned black input text styling.

type Toggle

type Toggle struct {
	*Element
	Label     string
	TextStyle TextStyle
	Checked   bool
	OnChange  func(self *Toggle, checked bool)
	// contains filtered or unexported fields
}

Toggle is a retained boolean input rendered as an on/off switch.

func (*Toggle) SetChecked

func (toggle *Toggle) SetChecked(checked bool) *Toggle

SetChecked updates the toggle and notifies its change handler.

type TransitionMode

type TransitionMode uint8

TransitionMode selects how controls with multiple visual states animate.

const (
	TransitionInterpolate TransitionMode = iota
	TransitionCrossFade
)

type TransitionStyle

type TransitionStyle struct {
	Duration time.Duration
	Easing   Easing
	Mode     TransitionMode
}

TransitionStyle controls the duration and easing of visual state changes.

func DefaultTransitionStyle

func DefaultTransitionStyle() (style TransitionStyle)

DefaultTransitionStyle returns the standard control-state transition.

type UserInterface

type UserInterface struct {
	WindowAnimation WindowAnimationStyle

	Active, Visible bool
	// contains filtered or unexported fields
}

UserInterface owns a reusable, independently switchable set of widgets.

func New

func New(ctx *gctx2d.Context) (ui *UserInterface)

New creates an active UI that draws to ctx.

func NewUserInterface

func NewUserInterface(ctx *gctx2d.Context) (ui *UserInterface)

NewUserInterface is the descriptive alias for New.

func (*UserInterface) Clear

func (ui *UserInterface) Clear()

Clear removes every widget and layout while retaining the UI allocation.

func (*UserInterface) ClearWindowDragArea

func (ui *UserInterface) ClearWindowDragArea() *UserInterface

ClearWindowDragArea removes window-position containment.

func (*UserInterface) Column

func (ui *UserInterface) Column(bounds Rect) (container *Container)

Column creates a vertical container that is laid out before every UI draw.

func (*UserInterface) Context

func (ui *UserInterface) Context() (ctx *gctx2d.Context)

Context returns the context used by Draw.

func (*UserInterface) Draw

func (ui *UserInterface) Draw()

Draw emits this UI at the current point in the caller's scene draw.

func (*UserInterface) DrawTo

func (ui *UserInterface) DrawTo(ctx *gctx2d.Context)

DrawTo emits this UI to ctx without changing its retained state.

func (*UserInterface) FocusedInput

func (ui *UserInterface) FocusedInput() *TextInput

FocusedInput returns the text input that currently owns keyboard focus.

func (*UserInterface) Form

func (ui *UserInterface) Form(bounds Rect) (form *Form)

Form creates a tracked label/control layout.

func (*UserInterface) Hide

func (ui *UserInterface) Hide(onHidden ...func()) *UserInterface

Hide disables input and begins closing, then invokes onHidden after dismissal.

func (*UserInterface) Migrate

func (ui *UserInterface) Migrate(ctx *gctx2d.Context) *UserInterface

Migrate changes the default draw context while preserving the retained UI.

func (*UserInterface) NewButton

func (ui *UserInterface) NewButton(bounds Rect, text string, onClick func(self *Button)) (button *Button)

NewButton adds a button to the top of this UI's draw and hit-test order.

func (*UserInterface) NewCheckbox

func (ui *UserInterface) NewCheckbox(bounds Rect, label string, checked bool, onChange func(self *Checkbox, checked bool)) (checkbox *Checkbox)

NewCheckbox adds a boolean input to this UI.

func (*UserInterface) NewDropdown

func (ui *UserInterface) NewDropdown(bounds Rect, options []string, selected int, onChange func(self *Dropdown, selected int, value string)) (dropdown *Dropdown)

NewDropdown adds an expandable fixed-option input.

func (*UserInterface) NewLabel

func (ui *UserInterface) NewLabel(bounds Rect, text string) (label *Label)

NewLabel adds non-interactive text that can participate in layouts.

func (*UserInterface) NewPanel

func (ui *UserInterface) NewPanel(bounds Rect) (panel *Panel)

NewPanel adds a non-interactive retained background surface.

func (*UserInterface) NewRadio

func (ui *UserInterface) NewRadio(bounds Rect, label string, group string, checked bool, onChange func(self *Radio, checked bool)) (radio *Radio)

NewRadio adds a mutually exclusive choice to this UI.

func (*UserInterface) NewRadioGroup

func (ui *UserInterface) NewRadioGroup(bounds Rect, label string, choices []string, selected int, onChange func(self *RadioGroup, selected int, value string)) (group *RadioGroup)

NewRadioGroup adds a labeled set of mutually exclusive options.

func (*UserInterface) NewSelect

func (ui *UserInterface) NewSelect(bounds Rect, label string, options []string, selected int, onChange func(self *Select, selected int, value string)) (selectInput *Select)

NewSelect adds a cycling fixed-option input for backwards compatibility.

func (*UserInterface) NewSlider

func (ui *UserInterface) NewSlider(bounds Rect, label string, minimum, maximum, value float32, onChange func(self *Slider[float32], value float32)) (slider *Slider[float32])

NewSlider adds a float32 slider using the UI method compatibility API.

func (*UserInterface) NewSwitcher

func (ui *UserInterface) NewSwitcher(bounds Rect, options []string, selected int, onChange func(self *Switcher, selected int, value string)) (switcher *Switcher)

NewSwitcher adds a cycling fixed-option input.

func (*UserInterface) NewTextInput

func (ui *UserInterface) NewTextInput(bounds Rect, placeholder string, onChange func(self *TextInput, value string)) (input *TextInput)

NewTextInput adds a single-line text input to this UI.

func (*UserInterface) NewToggle

func (ui *UserInterface) NewToggle(bounds Rect, label string, checked bool, onChange func(self *Toggle, checked bool)) (toggle *Toggle)

NewToggle adds an on/off switch to this UI.

func (*UserInterface) OnKeyPress

func (ui *UserInterface) OnKeyPress(key gpucontext.Key, mods gpucontext.Modifiers) (handled bool)

OnKeyPress edits the focused input and handles focus traversal.

func (*UserInterface) OnKeyRelease

func (ui *UserInterface) OnKeyRelease(key gpucontext.Key, mods gpucontext.Modifiers) (handled bool)

OnKeyRelease reports keyboard capture while a text input is focused.

func (*UserInterface) OnMouseMove

func (ui *UserInterface) OnMouseMove(x, y float64) (handled bool)

OnMouseMove updates hover and captured-press state.

func (*UserInterface) OnMousePress

func (ui *UserInterface) OnMousePress(button gpucontext.MouseButton, x, y float64) (handled bool)

OnMousePress captures the topmost enabled widget under the pointer.

func (*UserInterface) OnMouseRelease

func (ui *UserInterface) OnMouseRelease(button gpucontext.MouseButton, x, y float64) (handled bool)

OnMouseRelease activates a captured widget when released over the same widget.

func (*UserInterface) OnScroll

func (ui *UserInterface) OnScroll(deltaX, deltaY float64) bool

OnScroll is reserved for scrollable widgets and currently reports no handling.

func (*UserInterface) OnTextInput

func (ui *UserInterface) OnTextInput(text string) (handled bool)

OnTextInput inserts composed text into the focused field.

func (*UserInterface) ResetWindowOffset

func (ui *UserInterface) ResetWindowOffset() *UserInterface

ResetWindowOffset returns the window to its retained layout position.

func (*UserInterface) Row

func (ui *UserInterface) Row(bounds Rect) (container *Container)

Row creates a horizontal container that is laid out before every UI draw.

func (*UserInterface) SetActive

func (ui *UserInterface) SetActive(active bool) *UserInterface

SetActive controls whether the UI accepts input.

func (*UserInterface) SetClipboard

func (ui *UserInterface) SetClipboard(clipboard Clipboard) *UserInterface

SetClipboard enables copy, cut, and paste shortcuts using a gogpu clipboard provider.

func (*UserInterface) SetContext

func (ui *UserInterface) SetContext(ctx *gctx2d.Context) *UserInterface

SetContext changes the default draw context without rebuilding the UI.

func (*UserInterface) SetCursor

func (ui *UserInterface) SetCursor(cursor Cursor) *UserInterface

SetCursor enables automatic pointer-shape updates for this UI.

func (*UserInterface) SetVisible

func (ui *UserInterface) SetVisible(visible bool) *UserInterface

SetVisible controls whether Draw emits this UI.

func (*UserInterface) SetWindowAnimation

func (ui *UserInterface) SetWindowAnimation(style WindowAnimationStyle) *UserInterface

SetWindowAnimation changes the popup transition used by Show and Hide.

func (*UserInterface) SetWindowDragArea

func (ui *UserInterface) SetWindowDragArea(area Rect) *UserInterface

SetWindowDragArea constrains the complete window to an on-screen rectangle.

func (*UserInterface) SetWindowDragHandle

func (ui *UserInterface) SetWindowDragHandle(handle LayoutItem) *UserInterface

SetWindowDragHandle enables dragging from an otherwise non-interactive layout item.

func (*UserInterface) SetWindowOffset

func (ui *UserInterface) SetWindowOffset(x, y float32) *UserInterface

SetWindowOffset positions the window relative to its retained layout.

func (*UserInterface) Show

func (ui *UserInterface) Show() *UserInterface

Show makes the UI visible and begins its configured opening transition.

func (*UserInterface) WidgetCount

func (ui *UserInterface) WidgetCount() int

WidgetCount returns the number of retained widgets.

func (*UserInterface) WindowOffset

func (ui *UserInterface) WindowOffset() (x, y float32)

WindowOffset returns the current drag translation.

func (*UserInterface) WindowState

func (ui *UserInterface) WindowState() WindowState

WindowState returns whether the UI is hidden, opening, open, or closing.

type WindowAnimationStyle

type WindowAnimationStyle struct {
	Open, Close TransitionStyle
	MinScale    float32
	OffsetY     float32
	Fade        bool
}

WindowAnimationStyle controls UI-level popup opening and closing.

func DefaultWindowAnimationStyle

func DefaultWindowAnimationStyle() (style WindowAnimationStyle)

DefaultWindowAnimationStyle returns a clean scale, lift, and fade transition.

type WindowState

type WindowState uint8

WindowState describes the current UI-level window transition.

const (
	WindowHidden WindowState = iota
	WindowOpening
	WindowOpen
	WindowClosing
)

Jump to

Keyboard shortcuts

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