box

package
v1.0.31-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 23 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

Index

Constants

View Source
const LongPressDuration = 500 * time.Millisecond

LongPressDuration is how long a touch must be held still for OnLongPress.

Variables

This section is empty.

Functions

func PlayLongPressFeedback

func PlayLongPressFeedback()

PlayLongPressFeedback plays the long-press feedback: a haptic tap, as the app sets it. Elements that start something with a long press (a touch drag) call it.

func SetLongPressFeedback

func SetLongPressFeedback(fn func())

SetLongPressFeedback sets what plays when OnLongPress fires - the app sets it to a haptic tap; component authors don't need to.

Types

type BoxElement

type BoxElement struct {
	// Contextual gives it a context menu (Context).
	base.Contextual
	// contains filtered or unexported fields
}

BoxElement is a container element: a flex container and a containing block for absolutely positioned children.

func Box

func Box(st styles.Styles, children ...base.Element) *BoxElement

Box is a container element: children are laid along FlexDirection with Align, Justify, Gap and Wrap. Chain OnHover to add hover styles.

func Grid

func Grid(st styles.Styles, children ...base.Element) *BoxElement

Grid lays children out on a grid: Box with Display DisplayGrid. st's GridColumns sizes the columns ("200 1fr", "repeat(3, 1fr)", "repeat(auto-fill, minmax(220, 1fr))"), Gap and RowGap space them, and each child's GridColumn and GridRow place it (Span, Cell), or it takes the next free cells in reading order.

func Row

func Row(st styles.Styles, children ...base.Element) *BoxElement

Row lays children out horizontally. It is Box with flex direction forced to row.

func Stack

func Stack(st styles.Styles, children ...base.Element) *BoxElement

Stack lays children out vertically. It is Box with flex direction forced to column. Note that this is not Gio's layout.Stack.

func (*BoxElement) Animate

func (b *BoxElement) Animate(key any) *BoxElement

Animate makes the box animate every change of its styles, under key: when the styles it's built with differ from the last frame's - a state changed, a row was selected, a panel expanded - it moves to the new ones over the styles' Transition (Duration and Easing), from wherever it was. A box is rebuilt every frame, so key (any comparable value, unique among animated boxes: a row's ID) keeps its animation between frames.

giode.Box(giode.Styles{
	Height:     ifThen(open, 240, 48),
	Background: ifThen(selected, "theme.primary/0.1", "theme.surface"),
	Transition: giode.StyleTransition{Duration: 200 * time.Millisecond},
}, children...).Animate("panel-" + id)

Colors, Opacity, Transform, BorderRadius, BorderWidth, the shadow, Padding, Gap, and fixed Width and Height interpolate; other properties change at once. With reduced motion every change is immediate. It returns b for chaining.

func (*BoxElement) Animation

func (b *BoxElement) Animation(a *KeyframeAnimation) *BoxElement

Animation plays a keyframe animation on the box (see Keyframes): its frames' styles are merged over the box's. It returns b for chaining.

func (*BoxElement) CacheSafe

func (b *BoxElement) CacheSafe() bool

CacheSafe reports whether the box's output depends only on things a cache watches: its own styles, which don't change after it's built, and children that are CacheSafe themselves.

func (*BoxElement) Describe

func (b *BoxElement) Describe() string

Describe names the box for diagnostics: its kind and the first text inside it, which is usually enough to find it in the code.

func (*BoxElement) FlexPercent

func (b *BoxElement) FlexPercent(axis layout.Axis) int

func (*BoxElement) FlexWeight

func (b *BoxElement) FlexWeight(axis layout.Axis) int

func (*BoxElement) GridPlacement

func (b *BoxElement) GridPlacement() (col, row properties.GridSpan)

func (*BoxElement) Hovered

func (b *BoxElement) Hovered() bool

Hovered reports whether the pointer was over the element on the last frame.

func (*BoxElement) Layout

func (b *BoxElement) Layout(gtx layout.Context) layout.Dimensions

Layout resolves the box size and paints the background stack (shadow, fill, gradient, image, border) with the content replayed on top.

A box with OnHover or OnClick is kept and laid out every frame as the same element; when all its content is CacheSafe it replays the output recorded last time until its hover or press state, its content's state, its constraints or its direction change.

func (*BoxElement) OnClick

func (b *BoxElement) OnClick(fn func(e ClickEvent)) *BoxElement

OnClick makes the box clickable, as if wrapped in a Pressable: the pointer cursor shows on hover, Enter/Space activate it when focused, and clickable children keep their own clicks. Its text stays selectable, like a clickable element on the web: in a SelectionArea, or Selectable text, a mouse drag selects instead of clicking. Like OnHover, it needs a box created once and reused every frame (or looked up with Keyed), since press state lives in the element.

func (*BoxElement) OnHover

func (b *BoxElement) OnHover(st styles.Styles) *BoxElement

OnHover sets styles applied while the pointer is over the element, merged on top of its base styles for both layout and painting:

card := giode.Box(base, children...).OnHover(giode.Styles{
	Background: "#1e40af",
	Cursor:     giode.CursorPointer,
})

Set Transition on the base styles to animate the change.

Hover state lives in the element, so create it once and reuse it every frame; rebuilding it in the view function resets the hover on each frame.

func (*BoxElement) Pressable

func (b *BoxElement) Pressable() *Pressable

Pressable returns the Pressable behind OnClick, to set a label, double-click handlers or disable it; nil before OnClick.

func (*BoxElement) ZIndex

func (b *BoxElement) ZIndex() int

type ClickEvent

type ClickEvent struct {
	// Clicks is 1 for a single click, 2 for a double click, and so on.
	Clicks int
	// Button is the mouse button; MousePrimary for keyboard and
	// programmatic clicks.
	Button MouseButton
	// Modifiers holds the keys held down (key.ModCtrl, key.ModShift,
	// ...); see Ctrl and Shift.
	Modifiers key.Modifiers
	// Source says how the click was produced.
	Source ClickSource
	// Position is where the pointer was, relative to the Pressable,
	// in pixels; zero for keyboard and programmatic clicks.
	Position image.Point
	// PositionDp is Position in dp, the unit of Styles.
	PositionDp f32.Point
	// WindowPosition is where the pointer was in the window, in pixels,
	// so a row can open a menu where it was right-clicked (see
	// Menu.OpenAtClick). For keyboard and programmatic clicks it's the
	// Pressable's bottom-left corner, as of the last frame.
	WindowPosition image.Point
}

ClickEvent describes a click.

func (ClickEvent) Ctrl

func (e ClickEvent) Ctrl() bool

Ctrl reports whether Ctrl (or Command on macOS) was held.

func (ClickEvent) Shift

func (e ClickEvent) Shift() bool

Shift reports whether Shift was held.

type ClickSource

type ClickSource int

ClickSource says how a click was produced.

const (
	// ClickPointer is a mouse, pen or touch click.
	ClickPointer ClickSource = iota
	// ClickKeyboard is Enter or Space on a focused Pressable.
	ClickKeyboard
	// ClickProgrammatic is a call to Pressable.Click (or automation).
	ClickProgrammatic
)

type DragEvent

type DragEvent struct {
	// Position is where the pointer is, relative to the Pressable, in
	// pixels.
	Position image.Point
	// PositionDp is Position in dp, the unit of Styles.
	PositionDp f32.Point
	// WindowPosition is where the pointer is in the window, in pixels.
	WindowPosition image.Point
	// DeltaDp is how far the pointer has moved since the press, in dp,
	// measured in the window: it stays right while the Pressable itself
	// moves with the drag (a tab being reordered, a dragged card).
	DeltaDp f32.Point
	// Button is the mouse button held; MousePrimary for touch.
	Button MouseButton
	// Modifiers holds the keys held down.
	Modifiers key.Modifiers
	// Touch is set for a finger, unset for a mouse or pen.
	Touch bool
	// Cancelled is set on OnDragEnd when the drag didn't end with a
	// release: the system took the pointer (another gesture won, the
	// window lost it) or the Pressable was disabled. Treat it as no
	// drop.
	Cancelled bool
}

DragEvent describes a drag on a Pressable: a press that moved further than a click may, from the start of the drag to its end.

type Keyframe

type Keyframe struct {
	At     float32
	Styles styles.Styles
}

Keyframe is a step of a keyframe animation: Styles at At, a fraction of the animation from 0 to 1. Its styles are merged over the box's, so a keyframe sets only what it animates.

type KeyframeAnimation

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

KeyframeAnimation plays styles through keyframes on a Box (BoxElement.Animation), like CSS @keyframes: a pulse, a shake, an entrance. It keeps its own time, so create it once. With reduced motion a repeating animation doesn't play, and a finite one jumps to its end.

func Keyframes

func Keyframes(d time.Duration, frames ...Keyframe) *KeyframeAnimation

Keyframes returns an animation through frames over d, played once. Frames are sorted by At; without one at 0 or 1 the box's own styles stand in at that end.

func (*KeyframeAnimation) Alternate

func (a *KeyframeAnimation) Alternate(on bool) *KeyframeAnimation

Alternate plays every other repetition backwards. It returns a for chaining.

func (*KeyframeAnimation) Delay

Delay waits d before playing. It returns a for chaining.

func (*KeyframeAnimation) Done

func (a *KeyframeAnimation) Done() bool

Done reports whether it has finished playing.

func (*KeyframeAnimation) Easing

Easing eases each step between two keyframes (EaseInOut by default). It returns a for chaining.

func (*KeyframeAnimation) KeepEnd

func (a *KeyframeAnimation) KeepEnd(on bool) *KeyframeAnimation

KeepEnd keeps the last keyframe's styles once it has played (the default); off, the box goes back to its own styles. It returns a for chaining.

func (*KeyframeAnimation) OnDone

func (a *KeyframeAnimation) OnDone(fn func()) *KeyframeAnimation

OnDone sets a function run when it finishes playing. It returns a for chaining.

func (*KeyframeAnimation) Pause

func (a *KeyframeAnimation) Pause()

Pause holds it where it is; Resume goes on from there.

func (*KeyframeAnimation) Repeat

func (a *KeyframeAnimation) Repeat(n int) *KeyframeAnimation

Repeat plays it n times; 0 repeats it forever. It returns a for chaining.

func (*KeyframeAnimation) Restart

func (a *KeyframeAnimation) Restart()

Restart plays it again from the start, on the next frame.

func (*KeyframeAnimation) Resume

func (a *KeyframeAnimation) Resume()

Resume: see Pause.

type MouseButton

type MouseButton int

MouseButton identifies which button produced a click.

const (
	MousePrimary MouseButton = iota
	MouseSecondary
	MouseMiddle
)

type PressState

type PressState struct {
	Hovered  bool
	Pressed  bool
	Focused  bool
	Disabled bool
	// Dragging is set while a press that moved is being dragged (see
	// Pressable.OnDragStart).
	Dragging bool
}

PressState is the interaction state a Pressable's content is built with, so it can restyle itself.

type Pressable

type Pressable struct {
	// Contextual gives it a context menu (Context).
	base.Contextual
	// contains filtered or unexported fields
}

Pressable makes any content clickable. Its content is rebuilt every frame with the current PressState, so it can restyle itself:

card := giode.Pressable(func(s giode.PressState) giode.Element {
	bg := "theme.surface"
	if s.Hovered {
		bg = "theme.border"
	}
	return giode.Box(giode.Styles{Background: bg, Padding: giode.UniformInset(12)},
		giode.Text("Open project"))
}).OnClick(func(e giode.ClickEvent) { open() })

The hit area sits beneath the content, like Gio's widget.Clickable, so clickable children (an expand arrow, a delete button) keep their own clicks. It shows a pointer cursor on hover, can be focused with Tab and activated with Enter or Space, stays clickable inside a frameless window's drag area, and appears to automation (the MCP server) as a button.

Like every stateful component, create it once and reuse it every frame - or, for items of a list that changes, look it up by key with Keyed.

func NewPressable

func NewPressable(content func(s PressState) base.Element, st ...styles.Styles) *Pressable

NewPressable returns a Pressable around content. The styles argument is optional and styles a box wrapped around the content: padding, background, FlexGrow, Width, ... and Cursor (default pointer).

func (*Pressable) Click

func (p *Pressable) Click()

Click runs the click handlers as a programmatic click. Call it on the UI goroutine (from a handler, or through App.Post).

func (*Pressable) Content

func (p *Pressable) Content(content func(s PressState) base.Element) *Pressable

Content replaces the content builder. It returns p for chaining.

func (*Pressable) Describe

func (p *Pressable) Describe() string

Describe names the pressable for diagnostics.

func (*Pressable) Disabled

func (p *Pressable) Disabled(v bool) *Pressable

Disabled turns input off: no events, no focus, default cursor, and PressState.Disabled set for the content. It returns p for chaining.

func (*Pressable) DragThreshold

func (p *Pressable) DragThreshold(dp float32) *Pressable

DragThreshold sets how far, in dp, a press moves before it's a drag (OnDragStart); 6dp by default. 0 starts the drag on the first move, as a text selection does. It returns p for chaining.

func (*Pressable) Dragging

func (p *Pressable) Dragging() bool

Dragging reports whether a drag is in progress.

func (*Pressable) FlexPercent

func (p *Pressable) FlexPercent(axis layout.Axis) int

func (*Pressable) FlexWeight

func (p *Pressable) FlexWeight(axis layout.Axis) int

func (*Pressable) Focusable

func (p *Pressable) Focusable(v bool) *Pressable

Focusable controls whether Tab can focus it (default true). It returns p for chaining.

func (*Pressable) Focused

func (p *Pressable) Focused() bool

Focused reports whether it has keyboard focus.

func (*Pressable) Hovered

func (p *Pressable) Hovered() bool

Hovered reports whether the pointer is over it.

func (*Pressable) Label

func (p *Pressable) Label(label string) *Pressable

Label sets the accessible name reported to assistive technology and automation. It returns p for chaining.

func (*Pressable) Layout

func (p *Pressable) Layout(gtx layout.Context) layout.Dimensions

Layout lays out the content over the hit area.

func (*Pressable) OnClick

func (p *Pressable) OnClick(fn func(e ClickEvent)) *Pressable

OnClick sets the handler for clicks of any button and count (see OnPrimaryClick for the primary button alone). It returns p for chaining.

func (*Pressable) OnDoubleClick

func (p *Pressable) OnDoubleClick(fn func(e ClickEvent)) *Pressable

OnDoubleClick sets a handler for primary-button double clicks, run in addition to OnClick. It returns p for chaining.

func (*Pressable) OnDragEnd

func (p *Pressable) OnDragEnd(fn func(e DragEvent)) *Pressable

OnDragEnd sets the handler for the end of a drag: the release, or a cancellation (DragEvent.Cancelled). It returns p for chaining.

func (*Pressable) OnDragMove

func (p *Pressable) OnDragMove(fn func(e DragEvent)) *Pressable

OnDragMove sets the handler for each move of the pointer during a drag, the first one right after OnDragStart. The drag goes on when the pointer leaves the Pressable, and once it has started, an enclosing Scroll or frameless window doesn't take it. (A finger moving along a Scroll's axis scrolls it before it moves far enough to start a drag.) It returns p for chaining.

func (*Pressable) OnDragStart

func (p *Pressable) OnDragStart(fn func(e DragEvent)) *Pressable

OnDragStart sets the handler for the start of a drag: a press that moves more than a few dp with any button, mouse or finger. Once a press becomes a drag, its release is not a click, so a draggable tab or card still clicks when it's only tapped. The event's Position is where the press was. Setting any drag handler keeps the content's text from being selected by a drag. It returns p for chaining.

func (*Pressable) OnLongPress

func (p *Pressable) OnLongPress(fn func(e ClickEvent)) *Pressable

OnLongPress sets the handler for a touch held on the element for LongPressDuration without sliding off it - the touch screen's way to open context actions. The release that follows isn't a click. With a mouse, use OnSecondaryClick. It returns p for chaining.

func (*Pressable) OnPress

func (p *Pressable) OnPress(fn func(e ClickEvent)) *Pressable

OnPress sets a handler for a pointer pressing down on it, any button, before it's released: where it pressed, which button, and how many presses in a row (Clicks 2 for the second press of a double click). An editor places its caret on the press, as text editors do, rather than on the release; the release still fires OnClick (unless the press became a drag). It returns p for chaining.

func (*Pressable) OnPrimaryClick

func (p *Pressable) OnPrimaryClick(fn func(e ClickEvent)) *Pressable

OnPrimaryClick sets a handler for clicks of the primary button (and keyboard and programmatic clicks), run in addition to OnClick: the handler for a plain click that a right or middle click shouldn't trigger. It returns p for chaining.

func (*Pressable) OnSecondaryClick

func (p *Pressable) OnSecondaryClick(fn func(e ClickEvent)) *Pressable

OnSecondaryClick sets a handler for right clicks, run in addition to OnClick. It returns p for chaining.

func (*Pressable) Pressed

func (p *Pressable) Pressed() bool

Pressed reports whether a button is held down on it.

func (*Pressable) Semantics

func (p *Pressable) Semantics(fn func(n *base.SemanticNode)) *Pressable

Semantics sets a function adjusting the node the Pressable reports to automation and assistive technology: its role (a tree item, a list item, a tab), states (selected, expanded) and description. It returns p for chaining.

func (*Pressable) State

func (p *Pressable) State() PressState

State returns the current PressState.

func (*Pressable) Styles

func (p *Pressable) Styles(st styles.Styles) *Pressable

Styles replaces the styles of the box around the content, from the next frame. It returns p for chaining.

func (*Pressable) ZIndex

func (p *Pressable) ZIndex() int

Jump to

Keyboard shortcuts

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