taim 0.15.0-rc.2

Taim.rs: a GPU-native, fine-grained reactive GUI framework for Rust, for desktop, mobile and the web. By theaimartco.
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! The Phase 13 exit-gate clause: **all Taim components have semantic
//! snapshots/actions**.
//!
//! `taim-design`'s `ComponentSpec` already declares each component's
//! `role()` and `actions()`, and `tests/design/components.rs` proves that
//! declaration is self-consistent. This suite proves the next step: that
//! the declaration survives being turned into a real
//! `taim_core::semantics::Node`, retained in an
//! `taim_accessibility::Tree`, and queried the way a platform adapter
//! would.
//!
//! The gap that would otherwise hide here: a component can declare a
//! perfect role and action set and still be useless to a screen reader if
//! nothing ever builds a node from it. So every assertion below goes
//! through the retained tree, not the spec.

use taim_accessibility::actions::{Dispatch, Rejection, Router};
use taim_accessibility::live_region::{LiveRegions, Refusal};
use taim_accessibility::tree::Tree;

use taim_design::components::app_bar::{AppBar, AppBarKind};
use taim_design::components::bottom_sheet::BottomSheet;
use taim_design::components::button::{Button, ButtonKind};
use taim_design::components::card::Card;
use taim_design::components::dialog::Dialog;
use taim_design::components::icon_button::IconButton;
use taim_design::components::list_tile::{ListLines, ListTile};
use taim_design::components::menu::Menu;
use taim_design::components::navigation::{Navigation, NavigationMode};
use taim_design::components::notification::{Notification, Privacy};
use taim_design::components::progress::Progress;
use taim_design::components::selection::{Selection, SelectionKind};
use taim_design::components::slider::Slider;
use taim_design::components::text_field::TextField;
use taim_design::components::toast::Toast;
use taim_design::components::tooltip::Tooltip;
use taim_design::components::window_chrome::WindowChrome;
use taim_design::components::{ComponentSpec, Density};
use taim_design::icon::Icon;

use taim_core::semantics::{Actions, Live, Node, Role};
use taim_core::widget::Id;
use taim_core::{Point, Rectangle};

/// Builds the semantic node a component would contribute, exactly the way
/// a widget is expected to: role and actions come straight from the
/// component's own contract, and the accessible name from the author.
///
/// This is the bridge under test. If a component's declaration cannot
/// produce a usable node, that shows up here rather than in production.
fn snapshot(id: &Id, spec: &dyn ComponentSpec, name: &str) -> Node {
    let size = spec.min_size(Density::Comfortable);

    let node = Node::new(id.clone(), Rectangle::new(Point::ORIGIN, size), spec.role())
        .with_actions(spec.actions());

    if name.is_empty() {
        node
    } else {
        node.with_name(name)
    }
}

/// Every component, named, in its ordinary configuration.
fn every_component() -> Vec<(&'static str, Box<dyn ComponentSpec>)> {
    vec![
        (
            "app_bar",
            Box::new(AppBar {
                kind: AppBarKind::Standard,
                navigation_action: true,
            }),
        ),
        (
            "bottom_sheet",
            Box::new(BottomSheet {
                expanded: 0.5,
                dismissible: true,
            }),
        ),
        (
            "button",
            Box::new(Button {
                kind: ButtonKind::Filled,
                loading: false,
                disabled: false,
            }),
        ),
        (
            "card",
            Box::new(Card {
                interactive: true,
                selected: false,
            }),
        ),
        (
            "dialog",
            Box::new(Dialog {
                dismissible: true,
                focus_trapped: true,
            }),
        ),
        (
            "icon_button",
            Box::new(IconButton {
                icon: Icon::new("close", 24, false),
                tooltip: Some("Close"),
                disabled: false,
            }),
        ),
        (
            "list_tile",
            Box::new(ListTile {
                lines: ListLines::Two,
                interactive: true,
            }),
        ),
        (
            "menu",
            Box::new(Menu {
                expanded: false,
                has_submenu: true,
            }),
        ),
        (
            "navigation",
            Box::new(Navigation {
                mode: NavigationMode::Rail,
                selected: false,
            }),
        ),
        (
            "notification",
            Box::new(Notification {
                privacy: Privacy::Public,
                actionable: true,
                group_count: 1,
            }),
        ),
        (
            "progress",
            Box::new(Progress {
                value: Some(0.4),
                circular: false,
            }),
        ),
        (
            "selection",
            Box::new(Selection {
                kind: SelectionKind::Checkbox,
                checked: false,
                disabled: false,
            }),
        ),
        (
            "slider",
            Box::new(Slider {
                value: 0.5,
                range: (0.0, 1.0),
                rtl: false,
                disabled: false,
            }),
        ),
        (
            "text_field",
            Box::new(TextField {
                secure: false,
                multiline: false,
                error: false,
                disabled: false,
            }),
        ),
        (
            "toast",
            Box::new(Toast {
                actionable: true,
                dismissible: true,
            }),
        ),
        (
            "tooltip",
            Box::new(Tooltip {
                focus_enabled: true,
                long_press_enabled: true,
            }),
        ),
        (
            "window_chrome",
            Box::new(WindowChrome {
                desktop: true,
                system_gestures_reserved: true,
            }),
        ),
    ]
}

// ---------------------------------------------------------------------
// Every component produces a usable snapshot
// ---------------------------------------------------------------------

#[test]
fn every_component_produces_a_retained_semantic_node() {
    // The gate clause, literally: each component must survive the trip
    // into the retained tree with its role and actions intact.
    for (name, spec) in every_component() {
        let id = Id::unique();
        let tree = Tree::from_forest(&[snapshot(&id, spec.as_ref(), name)]);

        let entry = tree
            .get(&id)
            .unwrap_or_else(|| panic!("{name}: contributed no semantic node at all"));

        assert_eq!(entry.role, spec.role(), "{name}: role was lost in transit");
        assert_eq!(
            entry.actions,
            spec.actions(),
            "{name}: actions were lost in transit"
        );
    }
}

#[test]
fn every_interactive_component_is_focusable_in_the_retained_tree() {
    // `is_focusable` is what focus traversal actually consults. A
    // component whose actions say FOCUS but whose entry says otherwise
    // would be skipped by Tab despite claiming otherwise.
    for (name, spec) in every_component() {
        if !spec.actions().contains(Actions::FOCUS) {
            continue;
        }

        let id = Id::unique();
        let tree = Tree::from_forest(&[snapshot(&id, spec.as_ref(), name)]);

        assert!(
            tree.get(&id).unwrap().is_focusable(),
            "{name}: claims FOCUS but the retained entry is not focusable"
        );
    }
}

#[test]
fn every_component_that_claims_an_action_will_actually_route_it() {
    // A declared action is a promise the router has to keep. If a
    // component advertises ACTIVATE and the router rejects it, assistive
    // technology sees a dead control.
    let router = Router::new();

    for (name, spec) in every_component() {
        let id = Id::unique();
        let tree = Tree::from_forest(&[snapshot(&id, spec.as_ref(), name)]);

        for action in spec.actions().iter() {
            assert_eq!(
                router.route(
                    &tree,
                    &Dispatch {
                        target: id.clone(),
                        action,
                    },
                ),
                Ok(id.clone()),
                "{name}: advertises {action:?} but the router refuses it"
            );
        }
    }
}

#[test]
fn no_component_routes_an_action_it_never_claimed() {
    // The converse: an action nobody offered must be refused, or a
    // platform could invoke behavior the component never agreed to.
    let router = Router::new();

    for (name, spec) in every_component() {
        let id = Id::unique();
        let tree = Tree::from_forest(&[snapshot(&id, spec.as_ref(), name)]);

        let unclaimed = Actions::all() - spec.actions();

        for action in unclaimed.iter() {
            assert_eq!(
                router.route(
                    &tree,
                    &Dispatch {
                        target: id.clone(),
                        action,
                    },
                ),
                Err(Rejection::NotOffered),
                "{name}: routed {action:?}, which it never claimed"
            );
        }
    }
}

// ---------------------------------------------------------------------
// Per-component semantic contracts
// ---------------------------------------------------------------------

#[test]
fn a_secure_field_snapshot_never_carries_a_value() {
    // The disclosure boundary, enforced end-to-end: the spec reports
    // SecureField, the node refuses the value, and the retained entry
    // holds none.
    let spec = TextField {
        secure: true,
        multiline: false,
        error: false,
        disabled: false,
    };
    assert_eq!(spec.role(), Role::SecureField);

    let id = Id::unique();
    let node = snapshot(&id, &spec, "Password")
        .with_value(taim_core::semantics::Value::Text("hunter2".into()));

    let tree = Tree::from_forest(&[node]);
    let entry = tree.get(&id).unwrap();

    assert_eq!(entry.value, None, "a secure field must disclose no value");
    assert!(
        entry.name.is_some(),
        "...but must keep its accessible name, or it is unusable"
    );
    assert!(
        !format!("{entry:?}").contains("hunter2"),
        "the secret must not survive in the retained entry's debug output"
    );
}

#[test]
fn a_secure_field_can_never_announce_through_a_live_region() {
    let spec = TextField {
        secure: true,
        multiline: false,
        error: false,
        disabled: false,
    };

    let id = Id::unique();
    let tree = Tree::from_forest(&[snapshot(&id, &spec, "Password")]);
    let entry = tree.get(&id).unwrap();

    let mut regions = LiveRegions::default();

    assert_eq!(
        regions.announce(entry, "hunter2", false),
        Err(Refusal::Secure),
        "a secure field must never reach a live region"
    );
}

#[test]
fn a_disabled_component_offers_nothing_to_assistive_technology() {
    let disabled: Vec<(&str, Box<dyn ComponentSpec>)> = vec![
        (
            "button",
            Box::new(Button {
                kind: ButtonKind::Filled,
                loading: false,
                disabled: true,
            }),
        ),
        (
            "selection",
            Box::new(Selection {
                kind: SelectionKind::Checkbox,
                checked: false,
                disabled: true,
            }),
        ),
        (
            "slider",
            Box::new(Slider {
                value: 0.5,
                range: (0.0, 1.0),
                rtl: false,
                disabled: true,
            }),
        ),
        (
            "text_field",
            Box::new(TextField {
                secure: false,
                multiline: false,
                error: false,
                disabled: true,
            }),
        ),
    ];

    let router = Router::new();

    for (name, spec) in disabled {
        let id = Id::unique();
        let tree = Tree::from_forest(&[snapshot(&id, spec.as_ref(), name)]);

        assert!(
            tree.get(&id).unwrap().actions.is_empty(),
            "{name}: a disabled control must advertise no actions"
        );

        assert_eq!(
            router.route(
                &tree,
                &Dispatch {
                    target: id,
                    action: Actions::ACTIVATE,
                },
            ),
            Err(Rejection::NotOffered),
            "{name}: a disabled control must reject activation"
        );
    }
}

#[test]
fn a_dialog_offers_dismissal_to_assistive_technology() {
    let spec = Dialog {
        dismissible: true,
        focus_trapped: true,
    };

    let id = Id::unique();
    let tree = Tree::from_forest(&[snapshot(&id, &spec, "Confirm")]);

    assert_eq!(tree.get(&id).unwrap().role, Role::Window);
    assert_eq!(
        Router::new().route(
            &tree,
            &Dispatch {
                target: id.clone(),
                action: Actions::DISMISS,
            },
        ),
        Ok(id),
        "Escape must be reachable through the action path too"
    );
}

#[test]
fn a_slider_offers_both_stepping_and_direct_setting() {
    // A slider that only offers SET_VALUE is unusable from a keyboard;
    // one that only steps is unusable from a screen reader's value setter.
    let spec = Slider {
        value: 0.5,
        range: (0.0, 1.0),
        rtl: false,
        disabled: false,
    };

    let id = Id::unique();
    let tree = Tree::from_forest(&[snapshot(&id, &spec, "Volume")]);
    let actions = tree.get(&id).unwrap().actions;

    for required in [
        Actions::FOCUS,
        Actions::SET_VALUE,
        Actions::INCREMENT,
        Actions::DECREMENT,
    ] {
        assert!(
            actions.contains(required),
            "a slider must offer {required:?}"
        );
    }
}

#[test]
fn a_menu_with_a_submenu_offers_expansion() {
    let spec = Menu {
        expanded: false,
        has_submenu: true,
    };

    let id = Id::unique();
    let tree = Tree::from_forest(&[snapshot(&id, &spec, "File")]);
    let entry = tree.get(&id).unwrap();

    assert_eq!(entry.role, Role::MenuItem);
    assert!(entry.actions.contains(Actions::TOGGLE_EXPANDED));
}

#[test]
fn a_toast_announces_politely_and_never_takes_focus() {
    // A toast interrupts to inform, not to grab. Stealing focus from
    // whatever the user was doing to announce a transient message is
    // hostile.
    let spec = Toast {
        actionable: true,
        dismissible: true,
    };

    let id = Id::unique();
    let node = snapshot(&id, &spec, "Message sent").with_live(Live::Polite);
    let tree = Tree::from_forest(&[node]);
    let entry = tree.get(&id).unwrap();

    let mut regions = LiveRegions::default();
    assert_eq!(regions.announce(entry, "Message sent", false), Ok(()));

    let drained = regions.drain();
    assert_eq!(drained.len(), 1);
    assert_eq!(drained[0].message, "Message sent");
}

#[test]
fn a_private_notification_body_never_reaches_a_live_region() {
    // `Privacy` decides what a locked screen may reveal; a live region is
    // exactly the channel that would speak it out loud regardless.
    let spec = Notification {
        privacy: Privacy::Secret,
        actionable: false,
        group_count: 1,
    };

    let id = Id::unique();
    let node = snapshot(&id, &spec, "Message").with_live(Live::Polite);
    let tree = Tree::from_forest(&[node]);
    let entry = tree.get(&id).unwrap();

    let mut regions = LiveRegions::default();

    // The caller consults `Privacy` and marks it private on a locked
    // screen; the region refuses it.
    let locked = true;
    let private = !spec.privacy.shows_body(locked);
    assert!(
        private,
        "a Secret notification withholds its body when locked"
    );

    assert_eq!(
        regions.announce(entry, "Bank code 1234", private),
        Err(Refusal::Private)
    );
    assert!(regions.is_empty());
}

#[test]
fn a_progress_indicator_reports_but_is_never_operated() {
    let spec = Progress {
        value: Some(0.4),
        circular: false,
    };

    let id = Id::unique();
    let tree = Tree::from_forest(&[snapshot(&id, &spec, "Loading")]);
    let entry = tree.get(&id).unwrap();

    assert!(
        entry.actions.is_empty(),
        "progress reports; it must never be operated"
    );
    assert!(!entry.is_focusable());
}

#[test]
fn a_component_snapshot_is_stable_across_rebuilds() {
    // "Semantic IDs remain stable across ordinary view reconstruction":
    // rebuilding the same component with the same id must produce no
    // changes at all, or a screen reader would re-announce it every frame.
    let spec = Button {
        kind: ButtonKind::Filled,
        loading: false,
        disabled: false,
    };
    let id = Id::unique();

    let first = Tree::from_forest(&[snapshot(&id, &spec, "Save")]);
    let second = Tree::from_forest(&[snapshot(&id, &spec, "Save")]);

    assert!(
        first.diff(&second).is_empty(),
        "an unchanged component must produce no semantic churn"
    );
}