Complete a task
In chapter 1 the rows were static labels. Now we introduce reactive state (#[model]) so tapping a row flips its checkmark and updates the header tally on the same frame.
1. Minimal demo: #[model] and ${...} bindings
Annotate a struct with #[model] and mark observable fields with #[observe]. The macro generates a getter method for each observed field (state.checked(), state.taps()).
Calling those getters inside ${...} subscribes the widget attribute to the field; mutating the model inside on Tap updates every subscriber automatically:
2. In the task list: syncing rows and the header count
Now bring the same pattern into the task list. In model.rs we mark done with #[observe] and add a derived completed() query on Tasks; in interface.rs each row wraps its content in Button ... on Tap { task.toggle(); }.
With the eye button on, the source pane gathers the focused regions across model.rs · interface.rs · lib.rs (including the Task and Tasks struct definitions and #[model] impl Tasks) and highlights the lines added in this step (+). Click any file tab or turn the eye off to read the full files:
Tapping a row runs `on Tap { task.toggle(); }`, flipping `task.done` on that specific `Task` item inside `Tasks.items`.
pub fn toggle(&mut self) { self.done = !self.done; }Mutates only the clicked Task itemWhat changed in this step
- Single source of truth: The header's
format_args!("{} of {} complete", tasks.completed(), tasks.total())counts completed items directly from the list. Because there is no second counter to maintain, the row checkmarks and header tally can never drift apart. - Always call the generated getter: Inside
${...}, calltask.done()rather than readingtask.donedirectly so the reactive scope records the subscription.
Run on your machine
cargo run -p mirui-first-app --features desktop --example desktop -- 2