XU XUv0.13.1
中文

UI / WIDGETS / Text & status

Badge

Attach a count or status to content

Run the Xu example

The example executes in the browser using the Xu/WASM runtime.

What to try

Click the button to increment the badge count.

Usage notes

Counts, dots and text badges have different roles. Attach the badge to the content it describes and retain an accessible explanation of the state.

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.badge("3") or >>
    screen.place(id,24.0,24.0,320.0,80.0) or >>
    << id
}

($&Screen) badge

XuAPI
pub fn (s $&Screen) badge(text string) int

Purpose

Create Badge 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.

(&Badge) variant

XuAPI
pub fn (badge &Badge) variant() int

Purpose

Execute variant on &Badge.

Parameters

badge &Badge
The Badge instance used by this call (borrow).

Returns

int — a value of this type

($&Badge) set_variant

XuAPI
pub fn (badge $&Badge) set_variant(variant int) int

Purpose

Operation on $&Badge: set variant. Throws on failure; handle the error or propagate it to the caller.

Parameters

badge $&Badge
The Badge instance used by this call (exclusive borrow).
variant int
variant (int)

Returns

int — a value of this type

(&Badge) clone

XuAPI
pub fn (value &Badge) clone() Badge

Purpose

Copy the control or style snapshot into an independent Badge value.

Parameters

value &Badge
The Badge instance used by this call (borrow).

Returns

Badge — a value of this type

struct Badge

XuAPI
pub struct Badge {

}

Purpose

Badge context or state structure.

Public fields

This type has no public fields.

See usage example

(&Badge) is_selectable

XuAPI
pub fn (bg &Badge) is_selectable() bool

Purpose

Operation on &Badge: whether it is selectable.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

($&Badge) set_selectable

XuAPI
pub fn (bg $&Badge) set_selectable(selectable bool)

Purpose

Operation on $&Badge: set selectable.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).
selectable bool
selectable (bool)

Returns

This call does not return a value.

(&Badge) bounds

XuAPI
pub fn (bg &Badge) bounds() layout.Rect

Purpose

Return the bounding Rectangle of the control or geometry.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

layout.Rect — a value of this type

($&Badge) set_bounds

XuAPI
pub fn (bg $&Badge) set_bounds(bounds &layout.Rect) int

Purpose

Operation on $&Badge: set bounds. Throws on failure; handle the error or propagate it to the caller.

Parameters

bg $&Badge
The Badge 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

(&Badge) needs_paint

XuAPI
pub fn (bg &Badge) needs_paint() bool

Purpose

Return whether the control needs repainting; call painted after painting to clear the flag.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

($&Badge) painted

XuAPI
pub fn (bg $&Badge) painted()

Purpose

Acknowledge the painted frame and clear the dirty flag; this is not a query.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).

Returns

This call does not return a value.

($&Badge) invalidate

XuAPI
pub fn (bg $&Badge) invalidate()

Purpose

Invalidate the control area, requesting a repaint on next frame.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).

Returns

This call does not return a value.

(&Badge) is_enabled

XuAPI
pub fn (bg &Badge) is_enabled() bool

Purpose

Return whether the control is currently enabled.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

($&Badge) set_enabled

XuAPI
pub fn (bg $&Badge) set_enabled(enabled bool)

Purpose

Operation on $&Badge: set enabled.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).
enabled bool
enabled (bool)

Returns

This call does not return a value.

(&Badge) is_selected

XuAPI
pub fn (bg &Badge) is_selected() bool

Purpose

Operation on &Badge: whether it is selected.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

($&Badge) set_selected

XuAPI
pub fn (bg $&Badge) set_selected(selected bool)

Purpose

Operation on $&Badge: set selected.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).
selected bool
selected (bool)

Returns

This call does not return a value.

(&Badge) is_invalid

XuAPI
pub fn (bg &Badge) is_invalid() bool

Purpose

Operation on &Badge: whether it is invalid.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

($&Badge) set_invalid

XuAPI
pub fn (bg $&Badge) set_invalid(invalid bool)

Purpose

Operation on $&Badge: set invalid.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).
invalid bool
invalid (bool)

Returns

This call does not return a value.

(&Badge) is_focused

XuAPI
pub fn (bg &Badge) is_focused() bool

Purpose

Return whether the control currently has input focus.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

($&Badge) set_focus

XuAPI
pub fn (bg $&Badge) set_focus(focused bool)

Purpose

Operation on $&Badge: set focus.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).
focused bool
focused (bool)

Returns

This call does not return a value.

(&Badge) is_hovered

XuAPI
pub fn (bg &Badge) is_hovered() bool

Purpose

Operation on &Badge: whether it is hovered.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

($&Badge) set_hovered

XuAPI
pub fn (bg $&Badge) set_hovered(hovered bool)

Purpose

Operation on $&Badge: set hovered.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).
hovered bool
hovered (bool)

Returns

This call does not return a value.

(&Badge) visual_state

XuAPI
pub fn (bg &Badge) visual_state() int

Purpose

Operation on &Badge: visual state.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

int — a value of this type

(&Badge) has_style

XuAPI
pub fn (bg &Badge) has_style() bool

Purpose

Operation on &Badge: whether it has style.

Parameters

bg &Badge
The Badge instance used by this call (borrow).

Returns

bool — true or false

(&Badge) style_or

XuAPI
pub fn (bg &Badge) style_or(styles &Styles) ControlStyle

Purpose

Operation on &Badge: style or.

Parameters

bg &Badge
The Badge 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

($&Badge) set_style

XuAPI
pub fn (bg $&Badge) set_style(style &ControlStyle) int

Purpose

Operation on $&Badge: set style. Throws on failure; handle the error or propagate it to the caller.

Parameters

bg $&Badge
The Badge 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

($&Badge) clear_style

XuAPI
pub fn (bg $&Badge) clear_style()

Purpose

Operation on $&Badge: clear style.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).

Returns

This call does not return a value.

(&Badge) paint

XuAPI
pub fn (bg &Badge) 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

bg &Badge
The Badge 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

(&Badge) measure_styled

XuAPI
pub fn (bg &Badge) measure_styled(label $&StyledLabel,styles &Styles) layout.Size

Purpose

Operation on &Badge: measure styled. Throws on failure; handle the error or propagate it to the caller.

Parameters

bg &Badge
The Badge 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

layout.Size — a value of this type

(&Badge) paint_styled

XuAPI
pub fn (bg &Badge) paint_styled(frame $&draw.Frame,label $&StyledLabel,styles &Styles) int

Purpose

Operation on &Badge: paint styled. Throws on failure; handle the error or propagate it to the caller.

Parameters

bg &Badge
The Badge 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

int — a value of this type

($&Badge) set_tone

XuAPI
pub fn (bg $&Badge) set_tone(tone int) int

Purpose

Operation on $&Badge: set tone. Throws on failure; handle the error or propagate it to the caller.

Parameters

bg $&Badge
The Badge instance used by this call (exclusive borrow).
tone int
tone (int)

Returns

int — a value of this type

Complete native example

Component Workshop · Back to Widgets