A Fusuma app is an Elm-style program. One state, one place where it changes, and a screen that is a function of the state. Views are plain values; there are no widget handles to keep, no observers, and no callbacks that capture the state.
The trait
pub trait Program {
type State: 'static;
type Msg: Copy;
fn init() -> Self::State;
fn update(state: &mut Self::State, msg: Self::Msg) -> Task<Self::Msg>;
fn view<'a>(state: &'a Self::State, env: &Env) -> impl Children<Self::Msg> + 'a;
// Optional:
fn theme(state: &Self::State, env: &Env) -> Option<Theme>;
fn on_screen(env: &Env) -> Option<Self::Msg>;
fn subscription(state: &Self::State) -> Sub<Self::Msg>;
fn presets() -> &'static [Preset<Self::State>];
}init | the state to start from |
update | the one place the state changes; returns what should happen next |
view | the children of the page's root column for this state and this Env |
theme | the program's own theme, for one that keeps its colours or design in its state (the gallery's theme selector does) |
on_screen | a message for a change of the screen a layout cares about: the form factor, the size class, the orientation. Not every pixel of a resize |
subscription | what the program wants to hear while its state is this: a timer, a shortcut. Nothing by default, and the program sleeps until an input comes |
presets | named starting states, for a demo, a screenshot or a test |
Messages
Msg is Copy. That is a rule with a purpose: a task, a subscription and a log can hold one, a test can send one, and a recorded session can be replayed to the same pixels. An event that carries a value (a slider, a calendar day) sends a message made by a function, usually a variant:
slider(state.volume).on_change(Msg::Volume) // fn(u16) -> Msg
calendar(year, month, Some(day), Msg::Day, Msg::MonthBy)
button("Save").primary().on_press(Msg::Save)A value too big for a message, such as the text of a field or an answer from a server, stays in the state or in an Inbox, and the message says only that it changed.
Views
A view is a function of the state that returns a tuple of views. A container takes a tuple of its children:
use fusuma::{CrossAlign, Length, button, card, column, row, spacer, text};
fn header<M: Clone + 'static>(title: &str) -> impl fusuma::View<M> + '_ {
row((
text(title).title(),
spacer(),
button("Edit").ghost(),
))
.gap(8)
.align(CrossAlign::Center)
}Every view function is on its own page in Components, with its methods and an example that runs as a test.
The tree is the previous state. Nothing is kept between frames but the widget tree. A view is built on the stack, synced into the tree node by node, and dropped. Every setter compares first, so a node whose text, style and handlers are the same changes nothing and asks for no layout and no drawing.
Parts that come and go. An Option<V> among the children is a node that is there or not. show(on, view) and Either, Either3, Either4 choose between views of different types, and swapping one for the other replaces the nodes.
lazy(&key, || view) makes a big part again only when its key changes. The key must hold everything the part shows. The gallery's sidebar and its dropdowns are lazy, which is why dragging a slider does not rebuild them.
Long lists. virtual_rows keeps a pool of nodes and fills it from a function: the market page of the showcase shows 2,000 symbols in a tree of a few dozen nodes.
The environment
view receives an Env: what the view is made for besides the state.
env.screen: the width, height, scale, shape (rectangle, round), class (watch, phone, tablet, desktop), the inputs that exist (pointer, keys, touch) and the insets of the safe area. It dereferences, soenv.widthandenv.class()work.env.text_scale: the person's font-size setting in percent, 50 to 400.env.localeandenv.t(key): the language and its strings.env.dark: whether the dark appearance is on.
When any of them changes, the view is made again. A layout that depends on the size, one column or two, is therefore an if in the view, not a media query. The toolkit also ships responsive rules (env.responsive(): navigation as sidebar or bar, margins, sidebar width) so an app can follow them.
Work that takes time
With the async feature, update can return Task::perform(id). The program's perform(state, id) answers with a future that ends in a message, made from a copy of what it needs. The runner keeps the future and polls it on the app's own clock, so there is no runtime to link and no thread unless you ask for one with blocking.
fn perform(state: &State, _id: u16) -> Work<Msg> {
let (page, answer) = (state.page, state.answer.clone());
work(async move {
answer.put(fetch(page).await);
Msg::Loaded
})
}Task::cancel(id) drops the work. A session with loads replays when the answers are recorded (Inbox::recording()) and fed back (Inbox::replaying(takes)). The Load page of the showcase shows it, and the feature builds for boards.
Testing a program
fusuma-test runs a program the way a person would, without a window: find a widget by the text it shows, its name or its role, click it, activate it from the keyboard, type, hover, let time pass, read what is on the screen and the messages handled, and take a screenshot. It uses the semantic tree, so a widget a test cannot find is a widget an assistive tool cannot find either. (Msg needs Debug for it.) A program is a plain State and update, so most tests do not need it at all.
use fusuma_test::Sim;
let mut app = Sim::<Counter, 64, 16>::new(320, 240);
app.click("Up")?;
assert!(app.shows("Count: 1"));Every component's example in these docs is run the same way, and its picture is drawn by the toolkit on the CPU renderer.
Next: architecture for what happens after view, or layout.