Tutorial

Tutorial 3: Forms, input and accessibility

Chapter 3 of 5. The complete example is on this page; every line of code compiles as shown.

Keep application state in Store, and derive the UI from it. The text field and switch bind to named store keys. The Apply action changes saved; the next screen build reads that value and shows a confirmation. There is no disk or network persistence in this example.

Aster's authoring kit in this revision does not export a generic stock button function. The example builds a labelled, focusable element with role button and an on_click action. The text field has an explicit text label for assistive technology, and the switch uses a visible label. Keep stable element IDs when the UI rebuilds: input routing and diagnostics use them.

Use Tab to move focus, type into the name field, and use the keyboard activation supported by each control. The native adapter supplies input routing; App can also receive the same interactions in a headless script. The --headless path types a synthetic name, applies it, prints the UI-tree accessibility audit, and saves the resulting image. An audit is a diagnostic, not a substitute for screen-reader and keyboard testing. Platform accessibility exposure depends on the chosen host.

Complete example

Source: tutorial_forms_input.rs native/ui-host/examples/tutorial_forms_input.rs.

Rust
//! Run from native/: cargo run --locked -p aster-ui-host --example tutorial_forms_input
use aster_blueprint::ui::prelude::*;
use aster_native::{Options, RunWindow};

fn screen(s: &Store) -> El {
    col([
        text("Workspace preferences").style(Type::Headline),
        text("Display name").style(Type::Label),
        text_field(s, "name", "Choose a display name").a11y_label("Display name"),
        switch(s, "reminders", "Enable reminders"),
        text("Apply preferences").pad(3).fill(Role::Accent).color(Role::OnAccent)
            .radius(Radius::Md).id("apply").role("button")
            .a11y_label("Apply preferences").focusable()
            .on_click(Action::set("saved", true)),
        text(if s.bool("saved") { "Applied in this demo session." } else { "Nothing applied yet." }),
    ]).gap(3).pad(6)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let store = Store::new().with("name", "").with("reminders", true).with("saved", false);
    let mut app = App::new(&Theme::paper_dark(), store).size(640, 440).screen(screen)?;
    if std::env::args().any(|a| a == "--headless") {
        app.click_id("name")?;
        app.type_text("Sample workspace")?;
        app.click_id("apply")?;
        app.settle()?;
        println!("{}", app.tree().audit_a11y());
        app.write_png("tutorial_forms_input.png")?;
        return Ok(());
    }
    app.run_window_with(Options {
        title: "Aster preferences".into(), ..Options::default()
    }.from_env())
}

Run it

From the repository root:

Shell
cd native
cargo check --locked -p aster-ui-host --example tutorial_forms_input
cargo run --locked -p aster-ui-host --example tutorial_forms_input

Without a desktop display, run the image-producing path instead:

Shell
cargo run --locked -p aster-ui-host --example tutorial_forms_input -- --headless

PNG files are written in the command's current directory. For the animation example, append --reduced-motion after -- to compare the reduced-motion policy.

What you will see

A name field, reminder switch and Apply control appear. Applying changes shows “Applied in this demo session.” Closing and reopening starts with a new in-memory store.

Next steps

Add validation before connecting a persistence layer. Keep labels and stable IDs, audit the tree, and test keyboard focus on your target host. Continue with animation.