What to try
Click the button, or focus it with Tab and press Enter, to increment the count.
Usage notes
Name the action with a clear verb and handle ACTION_ACTIVATE. Disabled buttons must not perform the action; styling controls icons and text.
Create through Screen
The Screen constructor returns a node ID. Pass that ID to Screen layout, state and event APIs. It is separate from the standalone control object documented below.
Native Xu setup fragment: call configure during initialization of an existing Screen. See the complete example for window creation, event dispatch and cleanup.
#ui/widgets
fn configure(screen $&widgets.Screen) int {
id := screen.button("Save") or >>
screen.place(id,24.0,24.0,320.0,80.0) or >>
<< id
}
($&Screen) button
pub fn (s $&Screen) button(text string) int
Purpose
Create Button in Screen and return the component node ID; throw on creation failure.
Parameters
s $&Screen- The
Screen instance used by this call (exclusive borrow).
text string- text (string)
Returns
int — component node ID for place and other Screen methods.
Placement, state and events
The application owns the component. Screen.place positions nodes; Screen.set_enabled and Screen.set_visible control availability and visibility. Present again after state changes, handle or errors, and keep topology changes outside paint callbacks. ToastHost uses its own queue API.
Action · Styles · UI lifecycle and styling guide
Screen.place · Screen.set_enabled · Screen.set_visible
Standalone control object API
These types and methods support applications that manage control objects, input and painting directly. Their receiver is a control object, not a Screen node ID. For Screen usage, use the constructor above and the Screen methods in the Widgets module.
struct Button
Purpose
Button context or state structure.
Public fields
This type has no public fields.
See usage example
(&Button) id
pub fn (button &Button) id() int
Purpose
Return the unique integer ID of the element or control.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
int — a value of this type
(&Button) is_closed
pub fn (button &Button) is_closed() bool
Purpose
Operation on &Button: whether it is closed.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
($&Button) close
pub fn (button $&Button) close()
Purpose
Operation on $&Button: close the resource.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
Returns
This call does not return a value.
(&Button) identity
pub fn (button &Button) identity() ui.CanvasEvent
Purpose
Execute identity on &Button.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
ui.CanvasEvent — a value of this type
(&Button) needs_paint
pub fn (button &Button) needs_paint() bool
Purpose
Return whether the control needs repainting; call painted after painting to clear the flag.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) is_focused
pub fn (button &Button) is_focused() bool
Purpose
Return whether the control currently has input focus.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) is_enabled
pub fn (button &Button) is_enabled() bool
Purpose
Return whether the control is currently enabled.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) is_selectable
pub fn (button &Button) is_selectable() bool
Purpose
Operation on &Button: whether it is selectable.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
($&Button) set_selectable
pub fn (button $&Button) set_selectable(selectable bool)
Purpose
Operation on $&Button: set selectable.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
selectable bool- selectable (bool)
Returns
This call does not return a value.
(&Button) bounds
pub fn (button &Button) bounds() layout.Rect
Purpose
Return the bounding Rectangle of the control or geometry.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
layout.Rect — a value of this type
($&Button) painted
pub fn (button $&Button) painted()
Purpose
Acknowledge the painted frame and clear the dirty flag; this is not a query.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
Returns
This call does not return a value.
($&Button) invalidate
pub fn (button $&Button) invalidate()
Purpose
Invalidate the control area, requesting a repaint on next frame.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
Returns
This call does not return a value.
($&Button) cancel
pub fn (button $&Button) cancel()
Purpose
Execute cancel on $&Button.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
Returns
This call does not return a value.
($&Button) set_enabled
pub fn (button $&Button) set_enabled(enabled bool)
Purpose
Operation on $&Button: set enabled.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
enabled bool- enabled (bool)
Returns
This call does not return a value.
($&Button) set_focus
pub fn (button $&Button) set_focus(focused bool)
Purpose
Operation on $&Button: set focus.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
focused bool- focused (bool)
Returns
This call does not return a value.
($&Button) set_bounds
pub fn (button $&Button) set_bounds(bounds &layout.Rect) int
Purpose
Operation on $&Button: set bounds. Throws on failure; handle the error or propagate it to the caller.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
bounds &layout.Rect- bounds (read-only borrow; caller keeps the original; type &layout.Rect)
Returns
int — a value of this type
($&Button) input
pub fn (button $&Button) input(input &ui.InputEvent) Action
Purpose
Process button presses, releases and keyboard activation; perform the business action on a completed activation.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
input &ui.InputEvent- Input event for the same window, canvas and generation, with coordinates in the control's canvas space.
Returns
An Action: kind identifies the action, consumed marks a handled event, request_focus requests focus, and needs_paint requests repainting.
Usage notes
- Pressing and releasing inside, or pressing and releasing Enter/Space while focused, yields
ACTION_ACTIVATE; read-only buttons do not activate.
($&Button) event
pub fn (button $&Button) event(event &ui.Event) Action
Purpose
Handle and respond to window or system lifecycle and interaction events. Throws on failure; handle the error or propagate it to the caller.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
event &ui.Event- event (read-only borrow; caller keeps the original; type &ui.Event)
Returns
Action — input result with kind, id, repaint and focus requests.
(&Button) paint
pub fn (button &Button) paint(frame $&draw.Frame,label &draw.TextLayout,theme &Theme) int
Purpose
Paint the control visually onto the target canvas. Throws on failure; handle the error or propagate it to the caller.
Parameters
button &Button- The
Button instance used by this call (borrow).
frame $&draw.Frame- frame (exclusive borrow; may modify the original; type $&draw.Frame)
label &draw.TextLayout- label (read-only borrow; caller keeps the original; type &draw.TextLayout)
theme &Theme- theme (read-only borrow; caller keeps the original; type &Theme)
Returns
int — a value of this type
(&Button) measure_icon_styled
pub fn (button &Button) measure_icon_styled(label $&StyledLabel,styles &Styles) layout.Size
Purpose
Measure the icon, label, gap and padding. icon_size=0 removes icon spacing.
Parameters
button &Button- The
Button instance used by this call (borrow).
label $&StyledLabel- label (exclusive borrow; may modify the original; type $&StyledLabel)
styles &Styles- styles (read-only borrow; caller keeps the original; type &Styles)
Returns
intrinsic logical size
(&Button) paint_icon_styled
pub fn (button &Button) paint_icon_styled(frame $&draw.Frame,label $&StyledLabel,image &draw.Image,styles &Styles) int
Purpose
Paint an image icon and label using styles. The icon preserves aspect ratio; the caller owns the Image.
Parameters
button &Button- The
Button instance used by this call (borrow).
frame $&draw.Frame- frame (exclusive borrow; may modify the original; type $&draw.Frame)
label $&StyledLabel- label (exclusive borrow; may modify the original; type $&StyledLabel)
image &draw.Image- image (read-only borrow; caller keeps the original; type &draw.Image)
styles &Styles- styles (read-only borrow; caller keeps the original; type &Styles)
Returns
0 on success; throws on failure
(&Button) clone
pub fn (b &Button) clone() Button
Purpose
Copy the control or style snapshot into an independent Button value.
Parameters
b &Button- The
Button instance used by this call (borrow).
Returns
Button — a value of this type
(&Button) visual_state
pub fn (button &Button) visual_state() int
Purpose
Operation on &Button: visual state.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
int — a value of this type
(&Button) has_style
pub fn (button &Button) has_style() bool
Purpose
Operation on &Button: whether it has style.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) style_or
pub fn (button &Button) style_or(styles &Styles) ControlStyle
Purpose
Operation on &Button: style or.
Parameters
button &Button- The
Button instance used by this call (borrow).
styles &Styles- styles (read-only borrow; caller keeps the original; type &Styles)
Returns
ControlStyle — a value of this type
($&Button) set_style
pub fn (button $&Button) set_style(style &ControlStyle) int
Purpose
Operation on $&Button: set style. Throws on failure; handle the error or propagate it to the caller.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
style &ControlStyle- style (read-only borrow; caller keeps the original; type &ControlStyle)
Returns
int — a value of this type
($&Button) clear_style
pub fn (button $&Button) clear_style()
Purpose
Operation on $&Button: clear style.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
Returns
This call does not return a value.
(&Button) is_invalid
pub fn (button &Button) is_invalid() bool
Purpose
Operation on &Button: whether it is invalid.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) is_selected
pub fn (button &Button) is_selected() bool
Purpose
Operation on &Button: whether it is selected.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) is_read_only
pub fn (button &Button) is_read_only() bool
Purpose
Operation on &Button: whether it is read only.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) is_hovered
pub fn (button &Button) is_hovered() bool
Purpose
Operation on &Button: whether it is hovered.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
(&Button) is_pressed
pub fn (button &Button) is_pressed() bool
Purpose
Operation on &Button: whether it is pressed.
Parameters
button &Button- The
Button instance used by this call (borrow).
Returns
bool — true or false
($&Button) set_read_only
pub fn (button $&Button) set_read_only(read_only bool)
Purpose
Operation on $&Button: set read only.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
read_only bool- read_only (bool)
Returns
This call does not return a value.
(&Button) measure_styled
pub fn (button &Button) measure_styled(label $&StyledLabel,styles &Styles) layout.Size
Purpose
Measure Button with the effective font, density, content and minimum size.
Parameters
button &Button- The
Button instance used by this call (borrow).
label $&StyledLabel- label (exclusive borrow; may modify the original; type $&StyledLabel)
styles &Styles- styles (read-only borrow; caller keeps the original; type &Styles)
Returns
intrinsic logical size; throws if native shaping fails
(&Button) paint_styled
pub fn (button &Button) paint_styled(frame $&draw.Frame,label $&StyledLabel,styles &Styles) int
Purpose
Paint Button using the complete theme; control bounds and Frame use the same local coordinates.
Parameters
button &Button- The
Button instance used by this call (borrow).
frame $&draw.Frame- frame (exclusive borrow; may modify the original; type $&draw.Frame)
label $&StyledLabel- label (exclusive borrow; may modify the original; type $&StyledLabel)
styles &Styles- styles (read-only borrow; caller keeps the original; type &Styles)
Returns
0 on success; throws on failure
($&Button) set_invalid
pub fn (button $&Button) set_invalid(invalid bool)
Purpose
Operation on $&Button: set invalid.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
invalid bool- invalid (bool)
Returns
This call does not return a value.
($&Button) set_selected
pub fn (button $&Button) set_selected(selected bool)
Purpose
Set a button's persistent selected appearance, for example a toolbar mode toggle.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
selected bool- Whether to display selection; this does not simulate a click or emit activation.
Returns
No return value; changed state marks repainting.
($&Button) route
pub fn (button $&Button) route(kind int,input &ui.InputEvent) Action
Purpose
Route and dispatch the input event to matching targets. Throws on failure; handle the error or propagate it to the caller.
Parameters
button $&Button- The
Button instance used by this call (exclusive borrow).
kind int- kind tag (int)
input &ui.InputEvent- input (read-only borrow; caller keeps the original; type &ui.InputEvent)
Returns
Action — input result with kind, id, repaint and focus requests.
Complete native example
Component Workshop · Back to Widgets