Skip to content
Development

Save and restore the list ​

A task list shouldn't lose your work when the window closes. Persistence in mirui is declarative: mark durable fields with #[persist], install PersistencePlugin, and the model restores its snapshot as soon as it is registered.

1. Minimal demo: #[persist] and simulated relaunch ​

Here is a minimal persistent counter: SavedCount opts into #[model(persistence(key = "..."))] and marks value with #[persist].

Tap + 1 (Autosaves) a few times, wait 0.2 seconds, and click Relaunch ↻ at the bottom-right of the workspace—it destroys the running App instance and mounts a brand-new one from storage:

CONCEPT
537#[model(persistence(key = "mirui_mini_saved_count"))]
538pub struct SavedCount {
539 #[observe]
540 #[persist]
541 pub value: i32,
542}
543
544impl Default for SavedCount {
545 fn default() -> Self {
546 Self { value: 1 }
547 }
548}
549
550#[model]
551impl SavedCount {
552 pub fn add(&mut self, delta: i32) {
553 self.value += delta;
554 }
555
556 pub fn reset(&mut self) {
557 self.value = 1;
558 }
559}
560
561#[compose]
562fn mini_storage_scene(counter: model!(SavedCount)) {
563 ui! {
564 Column (
565 grow: 1.0,
566 padding: Padding::all(20),
567 justify: JustifyContent::SpaceBetween,
568 text_color: ColorToken::OnSurface
569 ) {
570 Row (height: 28, justify: JustifyContent::SpaceBetween, align: AlignItems::Center) {
571 Text (
572 "#[persist] snapshot",
573 grow: 1.0,
574 height: 20,
575 font_size: 13,
576 text_color: ColorToken::OnSurfaceVariant,
577 paragraph: ParagraphStyle::label().with_align(TextAlign::Start)
578 )
579 Text (
580 text: ${ format_args!("Saved: {}", counter.value()) },
581 capacity: 20,
582 grow: 1.0,
583 font_size: 20,
584 height: 28,
585 paragraph: ParagraphStyle::label().with_align(TextAlign::End)
586 )
587 }
588 Row (height: 40, column_gap: 8) {
589 Button (
590 "+ 1 (Autosaves)",
591 grow: 1.0,
592 height: 40,
593 border_radius: 9,
594 normal_color: ColorToken::Primary,
595 pressed_color: ColorToken::Secondary,
596 text_color: ColorToken::OnPrimary
597 ) on Tap { counter.add(1); }
598 Button (
599 "Reset",
600 width: 80,
601 height: 40,
602 border_radius: 9,
603 normal_color: ColorToken::SurfaceVariant,
604 pressed_color: ColorToken::Outline,
605 text_color: ColorToken::OnSurface
606 ) on Tap { counter.reset(); }
607 }
608 }
609 };
610}
Loading preview…

Increment the counter, pause briefly for autosave, then click Relaunch ↻ in the workspace footer to verify the new App instance restores the saved count.


2. In the task list: autosave and startup restoration ​

Now we annotate Task and Tasks in model.rs and install PersistencePlugin before app.add_model(...) in lib.rs:

CHAPTER 06 · PERSISTENCE & PLUGIN ORDER UMLPersistencePlugin registration order & selective #[persist] archive schema

`lib.rs` installs `PersistencePlugin::new(storage).autosave(500)` **before** calling `app.add_model(Tasks::default()).finish()`.

app.add_plugin(PersistencePlugin::new(storage).autosave(500));Plugin order contract enforced
06 / 07
model.rs · lib.rs Added in this step31 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}
⋯lib.rs:69
69 if chapter.has_storage()
70 && let Some(storage) = storage
71 {
72 app.add_plugin(PersistencePlugin::new(storage).autosave(500));
73 }
⋯lib.rs:85
85 let tasks = app.add_model(Tasks::default()).finish();
Loading preview…

Add or toggle a task, wait half a second for autosave, then click Relaunch ↻ in the footer (or refresh the browser) to see your list restored.

What changed in this step ​

  1. Install the plugin before registering models: app.add_model(Tasks::default()).finish() queries PersistencePlugin immediately during registration, so the very first composed frame already reflects the restored data.
  2. Persist domain data, not transient UI state: Notice filter on Tasks is marked #[observe] but not #[persist]. Active filter tabs and in-flight animations reset cleanly on launch while tasks and completion states endure.

Run on your machine ​

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