Skip to content

Search

Documentação

UI

Balaur UI: widget components on scene nodes, saved with the scene, edited in the inspector, laid out by the engine and painted with egui.

Nesta página

A UI is widget components on scene nodes: saved with the scene, edited in the inspector, previewed in the viewport. A script fills them and answers their events. It never draws.

The editor's shell, docks, inspector and tab rows are widget nodes in editor/scenes/main.toml.

Widgets: game HUDs as scene nodes

[[nodes]]
name = "Score"

[nodes.widget]
kind = "label"
text = "Score 0"
anchor = "top_left"
x = 20
y = 16
font_size = 22
text_color = "#2b2b2b"
  • One widget component. kind says what it draws: label, button or panel. Every component with variants selects one with kind.
  • Widgets are scene nodes: saved with the scene, edited in the editor, updated through the component handle.

The UI workspace: examples/angrynerds' Score, Birds, Hint and Restart widgets in the tree, the HUD over the safe area, and the Restart button in the inspector

A button's click calls a method through a bindings row:

[[nodes]]
name = "Start"
script = { source = "scripts/start.bs" }
bindings = { rows = [{ event = "pointer_click", action = "call", value = "start_pressed" }] }

[nodes.widget]
kind = "button"
text = "Start"
pub class Start {
    node: Node,
}

impl Start {
    pub fn start_pressed() {
        log::info("start");
    }
}
// scripts/restart_button.bs, on the Restart node
pub class RestartButton {
    node: Node,
}

impl RestartButton {
    pub fn on_restart() {
        self.node.get_node("../Game").call("restart");
    }
}
  • on_click names a method on the node's script, or on the nearest ancestor whose script declares it. A button acting on another node forwards the call.
self.node.widget.text = "Score 10";
  • set_component builds the component from the given map over the schema's defaults. To change one property, write its field.
  • Panels size to their content when width or height is 0.
  • visible hides a widget and keeps its state. ui::set_widget_layer scopes and toggles the layer.

The kinds

Group Kinds
Text label, text_field, text_area, code
Controls button, checkbox, switch, dropdown, slider, number_field, color_picker, progress_bar, menu
Data list, tree, table
Boxes panel, row, column, grid, scroll, tabs, fold, dialog, window, stack, viewport
Other image, separator

A row with wrap_children = "wrap" starts a new line when the row is full.

  • list — rows from options; text is the one picked. A row splits on U+001F into an icon, a label, a trailing note and an #rrggbb. With columns above one, rows flow into a grid of cards (ItemList in icon mode) and row_height is the card's height.
  • tree — a list whose rows nest. One leading tab is one level. The fold caret is the kind's own.
  • table — one row per line, one cell per column. titles names the columns and says how many there are; a name ending in > draws its column against the right edge. A drag on the seam in the header moves a column's width, and a row may carry an #rrggbb past its last cell.
  • multi_select — lets a list, tree or table hold more than one row. The platform's command key toggles a row and shift takes the run from the last one clicked. selection is what the widget holds either way, one row or many, so nothing reading it back branches; text is the row last clicked. on_change hears the whole set where the widget holds many, and the row where it holds one.
  • widths — a share per table column, normalised: ["3", "1"] is three quarters and a quarter, and empty divides the width evenly. A drag on a seam in the header writes the shares back, so the scene keeps them.
  • header — off, a table draws no strip of column names. The columns are still titles', and a table that names none keeps its first row.
  • sort — the column the rows are drawn in the order of; reverse turns it round, and sortable lets a click on a header ask for either and writes both back. A cell that starts with a number sorts as one, so 12 KB follows 3 KB.
  • reorderable — a drag moves a row of a list or a tree. The kind draws where the row would land and calls on_move with the row moved, the row it landed on, and before, after or into. The rows themselves are the script's to reorder.
  • code — an editor with gutter, syntax colours and caret.
  • menu — a button that drops its child rows. A row that is itself a menu is a submenu. placement says where it opens.
  • viewport — the screen for the widgets under it. Each is placed against its box as a root is against the window and cut off at its edge. A dialog in it dims only the viewport, its popups open inside it, and its class tables read its size.

Only the rows on screen are built, in all three. A long list costs its viewport rather than its length. A secondary click picks the row it lands on, so a context menu acts on what it opened over; a row already picked keeps the set it is in.

A list with two rows picked, the same entries as a folding tree, and again as a table with named columns

A screen of nodes: a card grid, a control row and a wrapping chip flow

Layout

A container hands its children rects.

  • grow — the share of leftover space this box takes along the container's direction. Zero takes only its own size.
  • gap — space between children. padding is space inside the container's edge.
  • align — where children sit across the container's direction.
  • justify — how they spread along it: start, center, end, between, around or evenly.
  • width — the box, with height, min_width and min_height. Zero hugs the content.
  • handle — the grab width of the seams between children. A drag writes the new size onto the neighbour that states one.

Layout is taffy, the layout engine Dioxus and Blitz use. It runs over the whole tree before drawing. ui::widget_rect reads a rect back in the same frame.

Themes and roles

A widget_theme asset says how each kind looks. A widget takes the theme of the nearest ancestor that names one.

type = "widget_theme"

[colors]
background = "#0e1116"
primary = "#6fa4d8"

[button]
fill = "bg_control"
corner_radius = "radius"
padding_x = 10

[button.hover]
fill = "primary_bg"

[roles.primary]
fill = "primary_fill"
text_color = "text_on_primary"
font_weight = 700
  • [colors] states the source colours: background, foreground, primary, secondary, success, warning and danger. Every other token is derived from them, and stating one overrides its rule.
  • dark follows from the background. Each ink is walked toward the foreground until it reads at WCAG AA on the three surfaces text lands on.
  • [sizes] states font_size, radius, control_height and stroke_width, and the rest derive from those four. A number in any table may name a size instead.
  • [<kind>] is that kind's look. A kind the file leaves out keeps the built-in one.
  • [roles.<name>] is the same table under a name. A widget takes it with role = "primary", over its kind's.
  • hover, active, focus, disabled and checked tables say how it looks in each state. checked dresses a checkbox, a switch or a toggle button that is on.
  • A widget's own fill, stroke, corner_radius, text_color, font_size and font win over the theme.
  • [<kind>] image with slice makes the kind's background a nine-patch, on a row view as on a panel.
Token What it paints
bg_app, bg_panel the ground behind everything, and a sheet on it
bg_control, bg_control_hover a control at rest, and under the pointer
border_default every outline
text_default, text_muted, text_subtle three levels of text
primary_fill, primary_fill_hover a solid fill, and the same under the pointer
primary_text, primary_bg ink in the colour, and a tint of it behind a control that is on
text_on_primary ink on primary_fill
font_size_small, font_size_large, font_size_title from font_size
radius_small, radius_large from radius
control_height_small, control_height_large, control_height_touch from control_height

secondary, success, warning and danger take the same five colour tokens as primary.

A list, tree or table paints more than one plate, so the parts it draws are [colors] of their own. Each takes the built-in look where the theme names none, and a colour with no alpha draws nothing.

Token What it paints
row_selected, row_selected_text a picked row, and the ink on it
row_hover, row_active what a row takes under the pointer, and while it is held
row_stripe a table's every other row
table_header its header's plate
table_rule the lines down its columns, and the one under its header
tree_guide the lines down a tree's indent

These are the rows, not the box: [list.hover] still dresses the whole view under the pointer, the way it does any other kind. So table_rule = "#00000000" is a table with no lines, and row_stripe the same is one with no striping. padding_x on the kind is the air either side of a cell, and gap is the space between a list's cards.

Also on every widget: tooltip, icon (a glyph from the theme's icon family, drawn before the caption), enabled and interactive. Both are on unless turned off: enabled = false greys the widget out and swallows its clicks, and interactive = false lets the pointer through to the scene.

image is the picture an image widget or a button draws, sheet the picture a list cuts its card faces from, and language what a code widget highlights.

Three more say what a widget answers to. context names a menu a right click or a long press opens at the pointer. shortcut is a chord that clicks the widget from anywhere, as cmd+shift+s or f5. selectable lets a drag over a label select its text for the platform's copy key.

The ui module

What a script asks of the UI and tells it.

pub class Hud {
    node: Node,
}

impl Hud {
    // on_click = "pressed" on any widget under this node
    pub fn pressed(value: any, widget: Node) {
        if widget.name() == "Copy" {
            ui::set_clipboard("score 120");
        }
    }
}
  • A widget's on_click finds its method on the nearest node up whose script declares it, so one script answers a whole panel. The method gets the value, then the widget's node.
  • ui::widget_rect(node) is where a widget node drew, in design pixels. ui::tab_rect(node) is a tabs page's button.
  • ui::set_focus and ui::focus_next move focus from a pad, and ui::click(ui::focused_widget()) presses what has it. ui::click and ui::edit(node, value) act on a node as a reader would.
  • ui::fill_strip makes, reuses and hides one widget node per entry of a list, and ui::fill_slot fills one of them, so a long list writes only the rows on screen.
  • ui::width_class and ui::height_class say how much room the screen has.
  • ui::set_theme takes the document a widget_theme file holds. ui::complete_theme answers it with every token derived, and ui::contrast measures two colours the way the theme does.
  • ui::set_clipboard copies at the next pass. ui::pasted_text is what the last pass saw pasted.
  • [window] low_processor = true runs a pass only when something asks: input, a widget written, ui::request_repaint.
  • Fonts load from <project>/fonts/*.ttf, with heading, ui, mono and icon always available.
  • All dimensions are design pixels. ui::set_scale(f) multiplies every metric and the query functions divide back. HiDPI is separate and automatic.

Example

examples/interface is a whole screen built from widgets: a card grid, an outline, a table, a row of every control, and a theme with roles and hover states. One script fills the three views; the layout is in the scene.

Three more scenes sit beside it. scenes/rows.toml is the three row views over one set of entries. scenes/menus.toml is a menu bar and a context menu; scenes/text.toml is a label with links and hints, a selectable line, and numbers with arrows and units. Open any of them with --scene.

Tem certeza?