veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

ui/widgets

lib/ui/widgets.vk

lib/ui/widgets.vk Pure-Veko immediate-mode UI widget, layout, and chart toolkit built on ui/canvas, ui/font, and ui/layout. Zero capabilities required; renders 28+ crisp desktop-grade Light & Dark widgets directly into an RGBA32 Canvas.

Interactive Wasm 3.0 Framebuffer Preview (<canvas>)Mounting Wasm 3.0 surface...
Rect struct
signature
Rect = struct { x i64, y i64, w i64, h i64 }

Axis-aligned integer bounding box.

Theme struct
signature
Theme = struct { bg cv/Color, surface cv/Color, surface_hover cv/Color, border cv/Color, text cv/Color, muted cv/Color, accent cv/Color, accent_text cv/Color, success cv/Color, warning cv/Color, danger cv/Color }

Color theme palette for immediate-mode widgets.

UiState struct
signature
UiState = struct { mouse_x i64 = ..., mouse_y i64 = ..., mouse_down bool = ..., mouse_clicked bool = ..., mouse_rclicked bool = ..., wheel_dy i64 = ..., key_code i64 = ..., text_char i64 = ..., hot_id str = ..., active_id str = ..., focused_id str = ..., selected_id str = ..., open_popup_id str = ..., clipboard str = ..., clipboard_dirty bool = ..., dpi_scale i64 = ..., clip_stack block[Rect] = ..., key_mods i64 = ..., shift_down bool = ..., ctrl_down bool = ..., alt_down bool = ..., key_consumed bool = ..., focus_order block[str] = ..., next_focus_order block[str] = ..., focus_scope_prefix str = ... }

Per-frame pointer, wheel, keyboard, focus, popup, and clipboard state for immediate-mode widgets.

signature
MOD-SHIFT = fn [] i64

Keyboard modifier & virtual key constants (mirroring ui/MOD-* and ui/KEY-* with zero .ui capability).

signature
MOD-CTRL = fn [] i64
signature
MOD-ALT = fn [] i64
signature
KEY-BACKSPACE = fn [] i64
signature
KEY-TAB = fn [] i64
signature
KEY-ENTER = fn [] i64
signature
KEY-SHIFT = fn [] i64
signature
KEY-CTRL = fn [] i64
signature
KEY-ALT = fn [] i64
signature
KEY-ESC = fn [] i64
signature
KEY-SPACE = fn [] i64
signature
KEY-PAGE-UP = fn [] i64
signature
KEY-PAGE-DOWN = fn [] i64
signature
KEY-END = fn [] i64
signature
KEY-HOME = fn [] i64
signature
KEY-LEFT = fn [] i64
signature
KEY-UP = fn [] i64
signature
KEY-RIGHT = fn [] i64
signature
KEY-DOWN = fn [] i64
signature
KEY-DELETE = fn [] i64
signature
SplitResult = struct { first Rect, second Rect, ratio_pct i64 }

Result returned by split-pane-state carrying both pane Rects and updated mouse-dragged ratio_pct.

EditorSnap struct
signature
EditorSnap = struct { text str, cursor i64, anchor i64 }

Undo/redo history snapshot for EditorState.

signature
EditorState = struct { text str = ..., cursor i64 = ..., sel_anchor i64 = ..., pref_col i64 = ..., scroll_line i64 = ..., scroll_col i64 = ..., undo_stack block[EditorSnap] = ..., redo_stack block[EditorSnap] = ..., tab_width i64 = ..., syntax_vk bool = ... }

Persistent state for code-editor (multi-line cursor, selection, scroll, 64-step undo/redo, Veko syntax highlighting).

signature
CommandItem = struct { id str, label str, category str = ..., shortcut str = ... }

Action entry displayed in command-palette.

signature
PaletteState = struct { open bool = ..., query str = ..., selected i64 = ..., scroll i64 = ... }

Persistent state for command-palette.

FileEntry struct
signature
FileEntry = struct { name str, is_dir bool = ..., size i64 = ... }

Pure-data directory or file entry displayed in file-picker.

signature
FilePickerState = struct { open bool = ..., dir str = ..., filename str = ..., ext_filter str = ..., filter_on bool = ..., selected i64 = ..., scroll_y i64 = ... }

Persistent state for file-picker.

signature
FilePickerResult = struct { action enum, path str = ... }

Result returned each frame by file-picker (action in .none, .selected, .cancelled, .chdir).

TreeNode struct
signature
TreeNode = struct { id str, label str, depth i64 = ..., expanded bool = ..., has_children bool = ..., badge str = ... }

Hierarchical node descriptor for tree-view.

signature
ModalResult = struct { card Rect, body Rect, closed bool }

Result returned by modal carrying the outer dialog card, inner content Rect, and close-button hit state.

signature
ChartSeries = struct { name str, values block, color cv/Color }

Multi-series data descriptor for line-chart.

signature
dark-theme = fn [] Theme

Default dark theme (Slate 900 / Cyan 400).

signature
light-theme = fn [] Theme

Default crisp studio light theme (Slate 50 / Sky 600).

signature
new-state = fn [] UiState

Creates a fresh UiState.

signature
register-focusable = fn [mut st UiState, id str] bool

Registers id in the current frame's Tab-focus ring (respecting st.focus_scope_prefix) and returns true when id is focused.

signature
cycle-focus = fn [mut st UiState, reverse bool] str

Advances (reverse == false) or reverses (reverse == true) st.focused_id along st.focus_order.

signature
draw-focus-ring = fn [mut c cv/Canvas, r Rect, radius i64, th Theme]

Draws a soft keyboard focus halo + border ring around r.

signature
apply-key = fn [mut st UiState, key i64, text_ch i64, sh bool, ct bool, al bool]

Injects a deterministic keyboard event (key, text_ch, and optional /shift, /ctrl, /alt modifiers) for scripted UI frames & tests.

signature
apply-mouse = fn [mut st UiState, x i64, y i64, down bool, clicked bool]

Updates pointer coordinates and click state for the current frame.

signature
apply-events = fn [mut st UiState, events block] bool

Feeds a frame batch of ui/UiEvent structs (or maps with .kind, .x, .y, .key, .btn) into st.

signature
rect-contains? = fn [r Rect, px i64, py i64] bool

Returns true if point (px, py) lies inside r.

signature
inset = fn [r Rect, pad i64] Rect

Shrinks r uniformly on all 4 sides by pad pixels.

signature
inset-edges = fn [r Rect, top i64, right i64, bottom i64, left i64] Rect

Insets r asymmetrically by (top, right, bottom, left).

signature
vstack = fn [bounds Rect, heights block, gap i64] block[Rect]

Splits bounds vertically into stacked Rects with fixed heights and spacing gap.

Executable Example
veko
use [w = ui/widgets]
rows = w/vstack (w/Rect { x = 10, y = 10, w = 200, h = 100 }) [20 30] 8
if (len rows) != 2 or rows[1].y != 38 [ err 'vstack' ] else [ .ok ]
signature
hstack = fn [bounds Rect, widths block, gap i64] block[Rect]

Splits bounds horizontally into side-by-side Rects with fixed widths and spacing gap.

px fn
signature
px = fn [pixels i64] lay/Track

Creates a fixed-pixel flex track (lay/Track).

flex fn
signature
flex = fn [weight i64] lay/Track

Creates a proportional flex-grow track (lay/Track).

signature
flex-row = fn [bounds Rect, tracks block[lay/Track], gap i64] block[Rect]

Solves horizontal flex columns inside bounds using w/px and w/flex tracks.

signature
flex-col = fn [bounds Rect, tracks block[lay/Track], gap i64] block[Rect]

Solves vertical flex rows inside bounds using w/px and w/flex tracks.

grid fn
signature
grid = fn [bounds Rect, rows i64, cols i64, gap i64] block[Rect]

Solves a rows x cols uniform 2D grid inside bounds (row-major).

signature
push-clip = fn [mut st UiState, mut c cv/Canvas, r Rect]

Pushes a nested clipping rectangle onto st and activates it on c.

signature
pop-clip = fn [mut st UiState, mut c cv/Canvas]

Pops the top clipping rectangle from st and restores the previous clip on c.

signature
panel = fn [mut c cv/Canvas, r Rect, title str, th Theme]

Widget 1: Draws a rounded surface card with a 1px border and optional header title band.

signature
panel-body = fn [r Rect, has_title bool, pad i64] Rect

Returns the usable inner body Rect of a panel below its header band.

signature
label = fn [mut c cv/Canvas, x i64, y i64, text str, scale i64, col cv/Color]

Widget 2: Draws mixed-case proportional UI text at (x, y).

signature
heading = fn [mut c cv/Canvas, x i64, y i64, text str, scale i64, col cv/Color]

Draws semi-bold heading text at (x, y).

signature
button = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, text str, th Theme, is_primary bool] bool

Widget 3: Draws an interactive rounded button and returns true when clicked in this frame. Supports /primary true for high-emphasis accent fill.

Executable Example
veko
use [cv = ui/canvas, w = ui/widgets]
mut c = cv/new-canvas 120 40
mut st = w/new-state
w/apply-mouse mut st 30 20 true true
th = w/light-theme
hit = w/button mut c mut st 'go' (w/Rect { x = 10, y = 8, w = 80, h = 24 }) 'Run Build' th
if not hit [ err 'button click missed' ] else [ .ok ]
signature
icon-button = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, icon str, th Theme] bool

Widget 4: Draws a compact vector icon button ('plus', 'check', 'close', 'search', 'refresh', 'play', 'folder', 'settings').

signature
badge = fn [mut c cv/Canvas, x i64, y i64, text str, kind str, th Theme] i64

Widget 5: Draws a semantic pill badge (kind in 'ok', 'warn', 'err', 'info', 'neutral') and returns its width.

signature
stat-card = fn [mut c cv/Canvas, r Rect, title str, metric str, delta str, kind str, th Theme]

Widget 6: Draws a KPI metric summary card with muted label, bold scale-2 metric, and delta status badge.

signature
text-input = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, val str, placeholder str, th Theme, masked bool] str

Widget 7: Interactive single-line text input with placeholder, focus ring, keyboard editing, and optional /secret bullet masking.

secret-input fn .secret
signature
secret-input = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, mut secret_buf secret bytes, bullet_count i64, placeholder str, th Theme] i64

.secret-safe masked input widget operating directly on a mut secret bytes buffer without converting to str.

signature
text-area = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, val str, th Theme] str

Widget 8: Multi-line editable text area with line numbers and clipped viewport.

signature
checkbox = fn [mut c cv/Canvas, mut st UiState, id str, x i64, y i64, text str, checked bool, th Theme] bool

Widget 9: Interactive checkbox with crisp vector checkmark and clickable label; returns updated checked state.

signature
toggle-switch = fn [mut c cv/Canvas, mut st UiState, id str, x i64, y i64, text str, on bool, th Theme] bool

Widget 10: Interactive pill toggle switch with sliding circular thumb; returns updated on state.

signature
radio-group = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, options block, selected i64, th Theme] i64

Widget 11: Interactive radio button group (options list) stacked vertically inside r; returns selected index.

signature
slider = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, val i64, min_v i64, max_v i64, th Theme] i64

Widget 12: Horizontal range slider (min_v..max_v) with filled track, circular thumb, and value label; returns updated value.

signature
progress-bar = fn [mut c cv/Canvas, r Rect, pct i64, th Theme, show_pct bool]

Widget 13: Draws a horizontal progress bar inside r filled to pct (0..100).

signature
dropdown = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, options block, selected i64, th Theme] i64

Widget 14: Select dropdown trigger + popup menu (st.open_popup_id == id controls open state); returns selected index.

signature
chip-group = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, labels block, active_flags block, th Theme] i64

Widget 15: Horizontal filter/tag chip group (active_flags bool list); toggles clicked chip and returns clicked index (or -1).

tabs fn
signature
tabs = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, labels block, active i64, th Theme] i64

Widget 16: Segmented single-view tab bar (labels); returns the active tab index.

signature
begin-scroll = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, content_h i64, scroll_y i64, th Theme] i64

Widget 17: Begins a clipped scrollable viewport with scrollbar track/thumb and mouse-wheel scrolling; returns updated scroll_y.

signature
end-scroll = fn [mut st UiState, mut c cv/Canvas]

Ends a begin-scroll viewport and restores the parent clip rectangle.

signature
list-view = fn [mut c cv/Canvas, r Rect, items block, selected i64, th Theme]

Widget 18: Draws a selectable list view inside r (backward-compatible 5-arg form).

signature
select-list = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, items block, selected i64, th Theme] i64

Interactive list view returning the newly selected row index on click or Up/Down arrow keys.

signature
data-table = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, headers block, col_tracks block[lay/Track], rows block, selected_row i64, th Theme] i64

Widget 19: Multi-column data table (headers str list, col_tracks lay/Track list, rows 2D block) with zebra rows & row selection.

signature
tree-view = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, nodes block[TreeNode], selected_id str, th Theme] str

Widget 20: Hierarchical tree view (nodes of TreeNode) with disclosure triangles, folder/item icons, and optional badges.

signature
split-pane = fn [mut c cv/Canvas, mut st UiState, id str, bounds Rect, vertical bool, ratio_pct i64, th Theme] block[Rect]

Widget 21: Resizable horizontal or vertical split pane; draws interactive grip handle and returns [first_rect, second_rect].

signature
split-pane-state = fn [mut c cv/Canvas, mut st UiState, id str, bounds Rect, vertical bool, ratio_pct i64, th Theme] SplitResult

Stateful mouse-draggable variant of split-pane returning SplitResult { first, second, ratio_pct }.

signature
menu-bar = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, brand str, items block, active_idx i64, th Theme] i64

Widget 22: Top application menu bar (items str list); returns clicked menu index (or -1).

signature
context-menu = fn [mut c cv/Canvas, mut st UiState, id str, x i64, y i64, w_px i64, items block, th Theme] i64

Widget 23: Floating context menu card (items of [label shortcut] or str, '-' for divider); returns clicked item index (or -1).

signature
modal = fn [mut c cv/Canvas, mut st UiState, id str, viewport Rect, dialog_w i64, dialog_h i64, title str, th Theme] ModalResult

Widget 24: Modal lightbox overlay over viewport; draws dimmed backdrop + centered card + close button, returning ModalResult.

signature
toast = fn [mut c cv/Canvas, r Rect, title str, message str, kind str, th Theme]

Widget 25: Non-blocking toast notification card (kind in 'ok', 'warn', 'err', 'info').

signature
tooltip = fn [mut c cv/Canvas, x i64, y i64, text str, th Theme]

Widget 26: Floating high-contrast tooltip callout anchored at (x, y).

signature
sparkline = fn [mut c cv/Canvas, r Rect, vals block, col cv/Color, th Theme]

Widget 27: Draws an anti-aliased sparkline with soft area tint inside r normalized to [min(vals)..max(vals)].

signature
bar-chart = fn [mut c cv/Canvas, r Rect, categories block, values block, bar_col cv/Color, th Theme]

Widget 28: Labeled vertical column chart with gridlines, Y-axis scale ticks, value labels, and category axis.

signature
line-chart = fn [mut c cv/Canvas, r Rect, x_labels block, series_list block[ChartSeries], th Theme]

Widget 29: Multi-series anti-aliased line chart (series_list of ChartSeries) with gridlines, markers, X labels, and legend.

signature
new-editor = fn [initial str] EditorState

Creates a fresh EditorState initialized with initial source text and cursor at the end.

signature
code-editor = fn [mut c cv/Canvas, mut st UiState, id str, r Rect, mut ed EditorState, th Theme] str

Widget 30: Multi-line Veko studio code editor with line numbers, click/drag & Shift selection, clipboard (Ctrl+A/C/X/V), 64-step Ctrl+Z/Ctrl+Y undo/redo, auto-indent, and Veko syntax highlighting.

signature
fuzzy-score = fn [query str, candidate str] i64

Pure-Veko subsequence + word-boundary + consecutive-run fuzzy scorer (-1 if query does not match candidate).

signature
command-palette = fn [mut c cv/Canvas, mut st UiState, id str, viewport Rect, mut ps PaletteState, items block[CommandItem], th Theme] str

Widget 31: Fuzzy-searchable Studio Command Palette overlay (Ctrl+K / Ctrl+P style). Returns the executed CommandItem.id (and closes ps.open = false), or '' if still open / dismissed.

signature
entries-from-list = fn [items block] block[FileEntry]

Converts { name = str, is_dir = bool, size = i64 } maps (e.g. from fs/list-entries) into typed FileEntry values.

signature
file-picker = fn [mut c cv/Canvas, mut st UiState, id str, viewport Rect, mut fp FilePickerState, entries block[FileEntry], th Theme, is_save bool] FilePickerResult

Widget 32: Pure-Veko zero-capability modal File Picker (entries of FileEntry, /save true for Save dialog). Returns FilePickerResult with action in .none, .selected, .cancelled, or .chdir.