Skip to content
Development

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:

CONCEPT
319#[model]
320#[derive(Default)]
321pub struct MiniSwitch {
322 #[observe]
323 pub checked: bool,
324 #[observe]
325 pub taps: u32,
326}
327
328#[model]
329impl MiniSwitch {
330 pub fn flip(&mut self) {
331 self.checked = !self.checked;
332 self.taps = self.taps.saturating_add(1);
333 }
334}
335
336#[compose]
337fn mini_state_scene(state: model!(MiniSwitch)) {
338 ui! {
339 Column (
340 grow: 1.0,
341 padding: Padding::all(20),
342 justify: JustifyContent::SpaceBetween,
343 text_color: ColorToken::OnSurface
344 ) {
345 Text (
346 text: ${ format_args!("Status: {} ({} taps)", if state.checked() { "Done" } else { "Open" }, state.taps()) },
347 capacity: 36,
348 height: 26,
349 font_size: 16
350 )
351 Button (
352 text: ${ if state.checked() { "✓ Completed — tap to undo" } else { "Tap to mark complete" } },
353 height: 44,
354 border_radius: 10,
355 normal_color: ${ if state.checked() { ColorToken::Primary } else { ColorToken::SurfaceVariant } },
356 pressed_color: ColorToken::Outline,
357 text_color: ${ if state.checked() { ColorToken::OnPrimary } else { ColorToken::OnSurface } }
358 ) on Tap { state.flip(); }
359 }
360 };
361}
Loading preview…

Tap the button on the right: the button label, background color, and tap counter all react to state.flip() without manual widget mutation.


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:

CHAPTER 02 · MODEL & SUBSCRIBER UMLTasks → ModelList<Task> ownership & reactive ${...} dependency graph

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 item
02 / 07
model.rs · interface.rs · lib.rs Added in this step157 lines
⋯model.rs:7
7#[model(persistence)]
8#[derive(Default)]
9pub struct Task {
10 #[persist]
11 pub title: String,
12 #[observe]
13 #[persist]
14 pub done: bool,
15}
16
17#[model]
18impl Task {
19 pub fn toggle(&mut self) {
20 self.done = !self.done;
21 }
22}
⋯model.rs:41
41#[model(persistence(key = "first_app_tasks"))]
42pub struct Tasks {
43 #[persist]
44 pub items: model!(owned ModelList<Task>),
45 #[observe]
46 pub(crate) filter: Filter,
47 #[persist]
48 sequence: u32,
49}
⋯model.rs:72
72#[model]
73impl Tasks {
74 #[observe]
75 pub fn completed(&self) -> usize {
76 self.items.iter().filter(|task| task.done()).count()
77 }
78
79 #[observe]
80 pub fn total(&self) -> usize {
81 self.items.len()
82 }
⋯model.rs:98
98}
⋯interface.rs:51
51#[compose]
52fn task_row(
53 tasks: model!(Tasks),
54 task: model!(Task),
55 draft: model!(TextDocument),
56 key: ModelItemId,
57 chapter: Chapter,
58) -> Entity {
59 let title = task.read(|task| task.title.clone());
60 ui! {
61 Column (visible: ${ tasks.filter().includes(task.done()) }, height: 49, shrink: 0.0) {
62 Row (height: 48, align: AlignItems::Center) {
63 Button (
64 grow: 1.0,
65 min_width: 0,
66 height: 44,
67 clip_children: true,
68 normal_color: ColorToken::Surface,
69 pressed_color: ColorToken::SurfaceVariant,
70 class: ${
71 (
72 chapter.has_presets().then_some("task"),
73 (chapter.has_presets() && task.done()).then_some("task.complete"),
74 )
75 }
76 ) {
77 Row (grow: 1.0, min_width: 0, align: AlignItems::Center, column_gap: 12) {
78 Column (
79 width: 20,
80 height: 20,
81 shrink: 0.0,
82 border_width: 1,
83 border_radius: 6,
84 align: AlignItems::Center,
85 justify: JustifyContent::Center,
86 bg_color: ${ if task.done() { ColorToken::Primary } else { ColorToken::Surface } },
87 border_color: ${ if task.done() { ColorToken::Primary } else { ColorToken::OnSurfaceVariant } }
88 ) {
89 Icon (
90 visible: ${ task.done() },
91 path: ICON_CHECK,
92 width: 14,
93 height: 14,
94 color: ColorToken::OnPrimary
95 )
96 }
97 Text (
98 text: title,
99 grow: 1.0,
100 min_width: 0,
101 height: 22,
102 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
103 )
104 }
105 } on Tap { task.toggle(); }
106 if chapter.has_list() {
107 Button (
108 "Remove",
109 width: 54,
110 height: 44,
111 font_size: 12,
112 normal_color: ColorToken::Surface,
113 pressed_color: ColorToken::SurfaceVariant,
114 text_color: ColorToken::OnSurfaceVariant
115 ) on Tap(edit_tasks::callback(Edit::Remove(key), tasks, draft))
116 }
117 }
118 View (height: 1, bg_color: ColorToken::Outline)
119 }
120 }
121}
⋯interface.rs:237
237#[compose]
238pub(crate) fn task_list(
239 tasks: model!(Tasks),
240 draft: model!(TextDocument),
241 chapter: Chapter,
242 reduced: bool,
243) {
244 let start = progress_ratio(tasks.completed(), tasks.total());
245 ui! {
246 Column (
247 grow: 1.0,
248 min_height: 0,
249 padding: Padding::all(18),
250 row_gap: 9,
251 text_color: ColorToken::OnSurface,
252 font_size: 14
253 ) {
254 Column (
255 height: if chapter.has_motion() { 102 } else { 92 },
256 padding: Padding::all(14),
257 row_gap: 6,
258 shrink: 0.0,
259 bg_color: ColorToken::Secondary,
260 border_radius: 12
261 ) {
262 Text ("Task list", font_size: 30, height: 39, text_color: ColorToken::OnSecondary)
263 Text (
264 text: ${ format_args!("{} of {} complete", tasks.completed(), tasks.total()) },
265 capacity: 32,
266 font_size: 13,
267 height: 19,
268 text_color: ColorToken::OnSecondary
269 )
270 if chapter.has_motion() {
271 View (height: 4, bg_color: ColorToken::OnSurfaceVariant, border_radius: 2) [
272 Progress(start),
273 ProgressTarget(start),
274 ]
275 }
276 }
⋯interface.rs:317
317 }
318 };
319}
⋯lib.rs:85
85 let tasks = app.add_model(Tasks::default()).finish();
⋯lib.rs:93
93 let draft = app.add_model(TextDocument::new()).finish();
94 app.compose(
95 root,
96 interface::task_list(tasks.clone(), draft, chapter, reduced),
97 );
Loading preview…

Tap Connect the display: its checkmark appears and the header count steps from 1 of 3 to 2 of 3. Tap again to undo.

What changed in this step ​

  1. 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.
  2. Always call the generated getter: Inside ${...}, call task.done() rather than reading task.done directly so the reactive scope records the subscription.

Run on your machine ​

sh
cargo run -p mirui-first-app --features desktop --example desktop -- 2