tauri-plugin-widgets 0.8.0

Tauri plugin for App Widgets on Android, iOS, and macOS (WidgetKit); Windows Widgets Board (Adaptive Cards) + desktop webview; Linux desktop webview with X11 DESKTOP pin.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Cross-platform render receipts (diagnostics, not a critical path).
//!
//! Renderers write after paint; host reads when it wants. Separate from the
//! config map so receipt writes never bump `__meta_nonce__`.
//!
//! History is a ring per instance (cap 32) so intermittent failures stay
//! visible after the latest upsert.

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use crate::store::now_ms;

/// Prefs / file basename for the receipt bag (outside config DataMap).
pub const RECEIPTS_STORE_NAME: &str = "__tauri_widget_receipts__";
/// On-disk filename under the app data / widgets dir.
pub const RECEIPTS_FILE_NAME: &str = "widget_receipts.json";
const HISTORY_CAP: usize = 32;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
/// Element the renderer could not paint (capability / parse).
pub struct SkippedElement {
    /// IR type name (`chart`, `canvas`, …).
    #[serde(rename = "type")]
    pub type_name: String,
    /// Why it was skipped.
    pub reason: String,
}

/// What a renderer actually painted for one widget instance.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WidgetRenderReceipt {
    /// Logical widget id from JS / IR.
    pub widget_id: String,
    /// App Group / prefs group.
    pub group: String,
    /// `appWidgetId` | WidgetFamily | window label
    pub instance: String,
    /// Config-map nonce that was rendered (0 if unknown).
    pub nonce: u64,
    /// Family / slot size label when known.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub size: Option<String>,
    /// Light / dark when reported.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub theme: Option<String>,
    /// IR schema version the renderer understands.
    #[serde(default = "default_schema")]
    pub schema: u32,
    /// `prefs` | `state` | `appgroup` | `defaults` | `container` | `push` | `pull`
    pub source: String,
    /// Why this paint ran: `reload` | `timeline` | `action` | `added` | `resize` | `snapshot`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trigger: Option<String>,
    /// IR type names that painted successfully.
    #[serde(default)]
    pub rendered: Vec<String>,
    /// Elements skipped with reasons.
    #[serde(default)]
    pub skipped: Vec<SkippedElement>,
    /// Unix ms when this receipt was written.
    pub ts: u64,
}

fn default_schema() -> u32 {
    1
}

impl WidgetRenderReceipt {
    /// Fill `ts` with [`now_ms`] if still zero.
    pub fn touch_ts(mut self) -> Self {
        if self.ts == 0 {
            self.ts = now_ms();
        }
        self
    }
}

/// In-memory + optional disk bag of receipts, keyed by group → instance → history.
#[derive(Default)]
pub struct ReceiptStore {
    by_group: Mutex<HashMap<String, HashMap<String, VecDeque<WidgetRenderReceipt>>>>,
}

impl ReceiptStore {
    /// Empty in-memory store.
    pub fn new() -> Self {
        Self::default()
    }

    /// Append a receipt to the per-instance history ring.
    pub fn upsert(&self, receipt: WidgetRenderReceipt) {
        let receipt = receipt.touch_ts();
        let mut guard = self.by_group.lock().unwrap();
        let map = guard.entry(receipt.group.clone()).or_default();
        let q = map.entry(receipt.instance.clone()).or_default();
        q.push_back(receipt);
        while q.len() > HISTORY_CAP {
            q.pop_front();
        }
    }

    /// Latest receipt per instance (newest first).
    pub fn list(&self, group: &str) -> Vec<WidgetRenderReceipt> {
        self.by_group
            .lock()
            .unwrap()
            .get(group)
            .map(|m| {
                let mut v: Vec<_> = m.values().filter_map(|q| q.back().cloned()).collect();
                v.sort_by_key(|r| std::cmp::Reverse(r.ts));
                v
            })
            .unwrap_or_default()
    }

    /// Full history for a group (oldest → newest), capped by ring.
    pub fn history(&self, group: &str) -> Vec<WidgetRenderReceipt> {
        self.by_group
            .lock()
            .unwrap()
            .get(group)
            .map(|m| {
                let mut v: Vec<_> = m.values().flat_map(|q| q.iter().cloned()).collect();
                v.sort_by_key(|r| r.ts);
                v
            })
            .unwrap_or_default()
    }

    /// Instance ids with a recent receipt for `widget_id` (age ≤ `max_age_ms`).
    pub fn live_instances(&self, group: &str, widget_id: &str, max_age_ms: u64) -> Vec<String> {
        let now = now_ms();
        self.list(group)
            .into_iter()
            .filter(|r| r.widget_id == widget_id && now.saturating_sub(r.ts) <= max_age_ms)
            .map(|r| r.instance)
            .collect()
    }

    /// Persist all groups to JSON at `path`.
    pub fn save_to_path(&self, path: &Path) -> crate::Result<()> {
        let all: HashMap<String, Vec<WidgetRenderReceipt>> = self
            .by_group
            .lock()
            .unwrap()
            .iter()
            .map(|(g, m)| {
                let mut list: Vec<_> = m.values().flat_map(|q| q.iter().cloned()).collect();
                list.sort_by_key(|r| r.ts);
                (g.clone(), list)
            })
            .collect();
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let json = serde_json::to_string_pretty(&all)?;
        let tmp = path.with_extension("tmp");
        fs::write(&tmp, json.as_bytes())?;
        fs::rename(&tmp, path)?;
        Ok(())
    }

    /// Load receipts from disk (best-effort; ignores corrupt files).
    pub fn load_from_path(&self, path: &Path) {
        let Ok(raw) = fs::read_to_string(path) else {
            return;
        };
        let Ok(all): Result<HashMap<String, Vec<WidgetRenderReceipt>>, _> =
            serde_json::from_str(&raw)
        else {
            return;
        };
        for (_group, list) in all {
            for r in list {
                self.upsert(r);
            }
        }
    }
}

/// Default disk path under an app data dir.
pub fn receipts_path(app_data: &Path) -> PathBuf {
    app_data.join("widgets").join(RECEIPTS_FILE_NAME)
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    use proptest_state_machine::{prop_state_machine, ReferenceStateMachine, StateMachineTest};
    use std::collections::BTreeMap;

    const GROUPS: [&str; 2] = ["group.a", "group.b"];
    const INSTANCES: [&str; 2] = ["1", "2"];

    fn sample(group: &str, instance: &str, nonce: u64, ts: u64) -> WidgetRenderReceipt {
        WidgetRenderReceipt {
            widget_id: "weather".into(),
            group: group.into(),
            instance: instance.into(),
            nonce,
            size: Some("small".into()),
            theme: None,
            schema: 1,
            source: "prefs".into(),
            trigger: Some("timeline".into()),
            rendered: vec!["text".into()],
            skipped: vec![],
            ts,
        }
    }

    #[derive(Clone, Debug)]
    enum Op {
        /// A renderer reports a paint (ts strictly increasing, so ordering is total).
        Upsert {
            group: usize,
            instance: usize,
            nonce: u64,
            ts: u64,
        },
        /// Persist, then load into a fresh store (app restart).
        SaveAndRestart,
        /// A corrupt file on disk is ignored by load.
        LoadCorrupt,
    }

    /// Model: (group, instance) → history of (nonce, ts), oldest first, capped.
    #[derive(Clone, Debug, Default)]
    struct Model {
        rings: BTreeMap<(usize, usize), Vec<(u64, u64)>>,
        next_ts: u64,
    }

    impl ReferenceStateMachine for Model {
        type State = Model;
        type Transition = Op;

        fn init_state() -> BoxedStrategy<Model> {
            Just(Model {
                next_ts: 1,
                ..Default::default()
            })
            .boxed()
        }

        fn transitions(state: &Model) -> BoxedStrategy<Op> {
            let ts = state.next_ts;
            prop_oneof![
                // Most upserts hit one placement, so its ring passes the cap of 32 within
                // ~55 steps (uniform over 4 placements needed ~150).
                10 => (prop_oneof![8 => Just((0, 0)), 3 => (0..GROUPS.len(), 0..INSTANCES.len())], 0u64..1000)
                    .prop_map(move |((group, instance), nonce)| Op::Upsert { group, instance, nonce, ts }),
                1 => Just(Op::SaveAndRestart),
                1 => Just(Op::LoadCorrupt),
            ]
            .boxed()
        }

        fn apply(mut state: Model, op: &Op) -> Model {
            if let Op::Upsert {
                group,
                instance,
                nonce,
                ts,
            } = *op
            {
                let ring = state.rings.entry((group, instance)).or_default();
                ring.push((nonce, ts));
                if ring.len() > HISTORY_CAP {
                    ring.remove(0);
                }
                state.next_ts = ts + 1;
            }
            state
        }
    }

    struct Sut {
        store: ReceiptStore,
        dir: tempfile::TempDir,
    }

    struct ReceiptMachine;

    impl StateMachineTest for ReceiptMachine {
        type SystemUnderTest = Sut;
        type Reference = Model;

        fn init_test(_: &Model) -> Sut {
            Sut {
                store: ReceiptStore::new(),
                dir: tempfile::tempdir().unwrap(),
            }
        }

        fn apply(mut sut: Sut, _: &Model, op: Op) -> Sut {
            let path = receipts_path(sut.dir.path());
            match op {
                Op::Upsert {
                    group,
                    instance,
                    nonce,
                    ts,
                } => sut
                    .store
                    .upsert(sample(GROUPS[group], INSTANCES[instance], nonce, ts)),
                Op::SaveAndRestart => {
                    sut.store.save_to_path(&path).unwrap();
                    assert!(
                        !path.with_extension("tmp").exists(),
                        "temp file left behind"
                    );
                    let fresh = ReceiptStore::new();
                    fresh.load_from_path(&path);
                    sut.store = fresh;
                }
                Op::LoadCorrupt => {
                    let bad = sut.dir.path().join("corrupt.json");
                    fs::write(&bad, "{not json").unwrap();
                    sut.store.load_from_path(&bad);
                    sut.store
                        .load_from_path(&sut.dir.path().join("missing.json"));
                }
            }
            sut
        }

        fn check_invariants(sut: &Sut, model: &Model) {
            for (g, group) in GROUPS.iter().enumerate() {
                // history: every kept paint of the group, oldest → newest.
                let mut want: Vec<(u64, u64)> = model
                    .rings
                    .iter()
                    .filter(|((mg, _), _)| *mg == g)
                    .flat_map(|(_, r)| r.iter().copied())
                    .collect();
                want.sort_by_key(|&(_, ts)| ts);
                let got: Vec<(u64, u64)> = sut
                    .store
                    .history(group)
                    .iter()
                    .map(|r| (r.nonce, r.ts))
                    .collect();
                assert_eq!(got, want, "history({group})");
                // list: latest paint per instance, newest first.
                let mut latest: Vec<(String, u64, u64)> = model
                    .rings
                    .iter()
                    .filter(|((mg, _), _)| *mg == g)
                    .filter_map(|((_, i), r)| {
                        r.last().map(|&(n, ts)| (INSTANCES[*i].to_string(), n, ts))
                    })
                    .collect();
                latest.sort_by_key(|&(_, _, ts)| std::cmp::Reverse(ts));
                let got: Vec<(String, u64, u64)> = sut
                    .store
                    .list(group)
                    .into_iter()
                    .map(|r| (r.instance, r.nonce, r.ts))
                    .collect();
                assert_eq!(got, latest, "list({group})");
            }
            assert!(sut.store.list("group.none").is_empty());
        }
    }

    prop_state_machine! {
        #![proptest_config(ProptestConfig { cases: 64, ..ProptestConfig::default() })]
        /// Receipt bag vs a model: per-instance rings capped at 32, `list` = latest per
        /// instance newest first, `history` = all oldest first, restart via disk keeps both,
        /// corrupt / missing files are ignored.
        #[test]
        fn receipt_store_follows_the_model(sequential 1..80 => ReceiptMachine);
    }

    /// Liveness: a receipt without `ts` is stamped now → `live_instances` keeps recent
    /// paints of that widget only (old paints and other widgets are dropped).
    #[test]
    fn untimed_receipts_are_stamped_and_count_as_live() {
        let store = ReceiptStore::new();
        store.upsert(sample("g", "fresh", 1, 0));
        store.upsert(sample("g", "stale", 2, 1));
        let mut other = sample("g", "other", 3, 0);
        other.widget_id = "clock".into();
        store.upsert(other);
        let fresh = store
            .list("g")
            .into_iter()
            .find(|r| r.instance == "fresh")
            .unwrap();
        assert!(
            now_ms().saturating_sub(fresh.ts) < 60_000,
            "ts {}",
            fresh.ts
        );
        assert_eq!(store.live_instances("g", "weather", 60_000), ["fresh"]);
        assert_eq!(sample("g", "x", 0, 7).touch_ts().ts, 7);
    }
}