Skip to main content

Button

widget.Button is a focusable action trigger rendered as [ label ]. It fires a callback when the user presses Enter or Space.

Basic usage

btn := widget.NewButton("Save", func() {
// handle the press
})

Builder options

MethodDescription
WithStyle(s latte.Style)Override the display style
WithID(id string)Set a stable identifier for Canvas.GetValue(id)

With an ID

Assign an ID to retrieve the button's label later via Canvas.GetValue(id):

btn := widget.NewButton("Delete", onDelete).WithID("delete-btn")

Custom style

btn := widget.NewButton("Danger", onDelete).
WithStyle(latte.Style{FG: latte.ColorRed, Bold: true})

The focus style (reversed colours by default) is always applied on top of Style when the button is focused. Use WithStyle to control the unfocused appearance only; the theme supplies focus colours.

In a button row

The typical pattern is an HBox with HFill to push buttons to the right:

btnRow := layout.NewHBox()
btnRow.AddChild(layout.NewHFill()) // pushes buttons rightward
btnRow.AddChild(cancelBtn)
btnRow.AddChild(layout.NewHFill().WithMaxSize(2)) // 2-cell gap between buttons
btnRow.AddChild(okBtn)

Reading the value

Button implements oat.ValueGetter. Canvas.GetValue(id) returns the button's label as a string.

Keyboard behavior

KeyAction
EnterFires the press callback
SpaceFires the press callback
Tab / Shift+TabMove focus to next / previous widget