Skip to main content

ui

Macro ui 

Source
macro_rules! ui {
    ( $ui:expr => window $root:ident ( $($attrs:tt)* ) : $title:expr => { $($children:tt)* } ) => { ... };
    ( $ui:expr => window $root:ident : $title:expr => { $($children:tt)* } ) => { ... };
}
Expand description

Builds and attaches widgets imperatively from a terse declarative block.

The macro expands to Ui::create + Ui::attach calls (plus set_text where text is given), so it adds no runtime machinery: the widget ids it binds are ordinary WidgetId values in the caller’s scope, creation runs depth-first, and failures panic with a ui!: prefixed message (creation of an unknown kind is a programming error, not a runtime condition).

Grammar:

  • window <id>: <title-expr> => { ...children... } — the root window; the title may be any expression.
  • label|button|checkbox|text_input|text_area <id>: "<literal>"
  • progress_bar|slider <id>
  • container <id>: { ...children... }
  • any <id>: [<WidgetKind expr>] — any widget kind without text.

Any node may carry an optional parenthesized attribute group right after its id. Attributes are applied through the ordinary Ui::set_* calls and map one-to-one onto them:

A setter that rejects the widget kind panics with a ui!: <attribute> message, the same way an unknown kind does: a wrong attribute is a programming error, not a runtime condition. gap/padding are uniform flow-container spacing; in dynamic trees, Node::gap and Node::padding forward flow spacing to backend container properties while configuring flexbox layout styles.

§ui! versus Node

ui! builds a static tree once — the widgets an app always has — and binds their ids for later updates and event matching. Node with DeclarativeUi::render is the dynamic path: it re-renders a tree that can vary per call and carries flexbox style (gap, padding, growth, wrapping). Use ui! for the app skeleton and Node for content that depends on runtime data.

use native_gui_mock::MockBackend;
use native_gui_core::{Color, Ui, WidgetKind};

let mut ui = Ui::new(MockBackend::default());

native_gui_core::ui! { ui =>
    window root: "Demo" => {
        label greeting: "Hello",
        button go: "Click",
        container form: {
            text_input name: "Ada",
            progress_bar progress,
        },
        label status (tooltip: "Shows progress", size: (240, 24)): "Ready",
        container panel (direction: horizontal, gap: 6, padding: 10, bg: Color::rgb(24, 28, 34)): {
            slider zoom (value: 7),
        },
    }
}

assert_eq!(ui.widget_count(), 9);
assert_eq!(ui.widget(go).and_then(|spec| spec.text.as_deref()), Some("Click"));
assert_eq!(ui.widget(progress).and_then(|spec| spec.parent), Some(form));
assert_eq!(ui.widget(status).and_then(|spec| spec.tooltip.as_deref()), Some("Shows progress"));
assert_eq!(ui.widget(zoom).and_then(|spec| spec.value), Some(7));
assert_eq!(ui.widget(panel).and_then(|spec| spec.container_gap), Some(6));
assert_eq!(ui.widget(panel).and_then(|spec| spec.container_padding), Some(10));