bevy_gooey 0.35.0

Game interfaces made easy for Bevy: a skin, ready-made game widgets (buttons, panels, menus, HUD meters, tooltips, modal dialogs) and keyboard and gamepad navigation, on Bevy's own headless widgets.
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! The accessibility tree: what a screen reader hears of every widget.
//!
//! Bevy gives a widget an `AccessibilityNode` with the right role when it is a `Button`,
//! `Checkbox`, `Slider`, tab or text field, and keeps its `toggled`, `selected` and `disabled`
//! state. It never names them, and it knows nothing of a heading, a meter, a toast, a table or a
//! splitter. [`update_accessibility`] fills that in from what the widget already draws: the name
//! is the words on screen, so a game writes nothing twice. [`spec`] is one exhaustive `match` over
//! [`Role`]: a new role does not compile until someone says what an assistive technology sees.
//!
//! The system returns at once unless `bevy::a11y::AccessibilityRequested` is set (an assistive
//! technology connected). It reads each node through `Deref` and writes only a difference, so a
//! settled interface marks nothing changed.
//!
//! The same pass declares what an assistive technology may ask of each widget (press, focus, step,
//! set); [`crate::access_actions`] carries the requests out as the widget's own events.

use accesskit::{Action, HasPopup, Node as Node_, Role as Ak, Toggled};
use bevy::a11y::{AccessibilityNode, AccessibilityRequested};
use bevy::ecs::query::QueryData;
use bevy::ecs::system::SystemParam;
use bevy::prelude::*;
use bevy::ui::{Checked, InteractionDisabled, Selected};

use crate::access_actions::{Handles, MANAGED, TREE_BRANCH, actions_of};
use crate::binding::Capturing;
use crate::choice::ChoiceValue;
use crate::color::ColorSwatch;
use crate::cooldown::Cooldown;
use crate::menu::{MenuItem, MenuOpen};
use crate::meter::Meter;
use crate::model::cooldown_words;
use crate::progress::Progress;
use crate::section::{SectionHeader, SectionOpen};
use crate::skin::Role;
use crate::split::{SplitDivider, SplitFraction};
use crate::table::{Table, TableRow};
use crate::text::{AreaField, TextInputOf, TextInputValue};
use crate::toast::{Toast, ToastKind};
use crate::tooltip::Tooltip;
use crate::tree::{Tree, TreeView};

/// What a role is to an assistive technology.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Spec {
    /// Decorative, or its words belong to the widget around it: no node of its own, and a node
    /// Bevy added (an `ImageNode`'s) is hidden.
    Hidden,
    /// A container with no meaning of its own. It keeps the tree nested, so the screen reader
    /// reads in layout order and not in the order the entities are stored.
    Group,
    /// A node the widget's own arm of [`describe`] fills in.
    Node,
}

/// The exhaustive table: every [`Role`] is one of three things.
fn spec(role: Role) -> Spec {
    match role {
        Role::Screen
        | Role::Panel
        | Role::Surface(_)
        | Role::Tone(_)
        | Role::Hovered
        | Role::Pressed
        | Role::Held
        | Role::HeldGround
        | Role::Unchecked
        | Role::Card
        | Role::PreviewEdge
        | Role::Menu
        | Role::Popover
        | Role::ToastStack
        | Role::ToastBody
        | Role::Prompts
        | Role::Prompt
        | Role::ShortcutLegend
        | Role::List
        | Role::ListView
        | Role::ListRows
        | Role::TableView
        | Role::TableRows
        | Role::TableHeader
        | Role::Split
        | Role::SplitPane
        | Role::Dock
        | Role::DockBody
        | Role::Tabs
        | Role::TabList
        | Role::TabPages
        | Role::TextInput
        | Role::TextArea
        | Role::TextCell
        | Role::NumberPart
        | Role::NumberParts
        | Role::ColorStripBox
        | Role::Section
        | Role::SectionBody
        | Role::StatusGroup
        | Role::StatusItem
        | Role::StatusBar
        | Role::Inspector
        | Role::NotificationCenter
        | Role::Palette
        | Role::PaletteField
        | Role::SlotGrid
        | Role::FieldMessage => Spec::Group,
        Role::Heading
        | Role::Label
        | Role::Button
        | Role::Meter
        | Role::Tooltip
        | Role::Scrim
        | Role::Toggle
        | Role::Slider
        | Role::Choice
        | Role::Binding
        | Role::PromptKey
        | Role::PromptButton
        | Role::Toast
        | Role::ToastAction
        | Role::Table
        | Role::TableRow
        | Role::TableCell
        | Role::TableHeaderCell
        | Role::TableEdge
        | Role::Tab
        | Role::TabPage
        | Role::TextField
        | Role::ColorSwatch
        | Role::ColorArea
        | Role::Viewport
        | Role::SplitDivider
        | Role::MenuBar
        | Role::MenuPanel
        | Role::ToggleGroup
        | Role::MenuTitle
        | Role::MenuItem
        | Role::MenuNote
        | Role::PaletteRow
        | Role::PaletteCount
        | Role::Progress
        | Role::Spinner
        | Role::Toolbar
        | Role::GroupOption
        | Role::Slot
        | Role::SlotEmpty
        | Role::Radial
        | Role::SectionNote => Spec::Node,
        Role::ButtonText
        | Role::MenuTitleText
        | Role::MeterFill
        | Role::MeterTrail
        | Role::ToggleTrack
        | Role::ToggleKnob
        | Role::SliderHeader
        | Role::SliderTrack
        | Role::SliderRail
        | Role::SliderFill
        | Role::SliderGap
        | Role::SliderThumb
        | Role::SliderReadout
        | Role::ControlText
        | Role::ChoiceArrow
        | Role::ChoiceBack
        | Role::ChoiceForward
        | Role::ChoiceCell
        | Role::ChoiceOption
        | Role::BindingCap
        | Role::BindingText
        | Role::BindingPrompt
        | Role::ShortcutBadge
        | Role::ToastMark
        | Role::ToastText
        | Role::ListRail
        | Role::ListThumb
        | Role::TableSortUp
        | Role::TableSortDown
        | Role::TableCellText
        | Role::TreeTwisty
        | Role::TreeClosed
        | Role::TreeOpen
        | Role::SplitGrip
        | Role::DockDrop
        | Role::DockGhost
        | Role::DragGhost
        | Role::DragGhostRefused
        | Role::DropMark
        | Role::DropMarkRefused
        | Role::TabText
        | Role::TextHint
        | Role::MenuMark
        | Role::MenuCheck
        | Role::MenuDot
        | Role::MenuText
        | Role::MenuShortcut
        | Role::MenuChevron
        | Role::ColorSwatchHalf
        | Role::ColorAreaMarker
        | Role::ColorAreaMarkerInner
        | Role::ColorRail
        | Role::Icon
        | Role::FieldMark
        | Role::ProgressLine
        | Role::StatusMark
        | Role::MenuSeparator
        | Role::ToolbarSeparator
        | Role::RadialTrack
        | Role::SlotName
        | Role::SlotCount
        | Role::SlotIcon
        | Role::Cooldown
        | Role::CooldownText
        | Role::HoldTrack
        | Role::HoldFill
        | Role::RadialIcon
        | Role::RadialSlot
        | Role::RadialText
        | Role::RadialCentre
        | Role::ToastActionText => Spec::Hidden,
    }
}

/// The roles whose text is a widget's own name: a button's, a tab's, a menu row's, a table cell's,
/// a toast button's. (Label, heading and control text are read by their own arms.)
fn names(role: Role) -> bool {
    matches!(
        role,
        Role::ButtonText
            | Role::MenuTitleText
            | Role::TabText
            | Role::MenuText
            | Role::TableCellText
            | Role::ToastActionText
    )
}

/// What a node should say, as far as the crate owns it. `None` fields are left as they are.
#[derive(Default, Debug, PartialEq)]
struct Wanted {
    /// Set only where Bevy has not set the role already.
    role: Option<Ak>,
    name: Option<String>,
    value: Option<String>,
    /// `(value, min, max)`.
    numeric: Option<(f64, f64, f64)>,
    toggled: Option<bool>,
    selected: Option<bool>,
    expanded: Option<bool>,
    /// A tree row that can be opened and shut: it declares Expand and Collapse.
    tree_branch: bool,
    /// A tree row with no children: it is not expandable, so a state its pooled entity said while
    /// it was another node's must go.
    end_node: bool,
    /// A tree row's depth, from 0 as AccessKit counts (the platform adapters add the 1).
    level: Option<usize>,
    /// A tree row's place among the rows shown, from 0.
    position_in_set: Option<usize>,
    /// A tree's rows shown: the platform reads a row's set size from its container.
    size_of_set: Option<usize>,
    /// A table row's index among all rows, from 0.
    row_index: Option<usize>,
    row_count: Option<usize>,
    column_count: Option<usize>,
    popup: bool,
    modal: bool,
    hidden: bool,
    disabled: bool,
    /// What an assistive technology may ask of the widget ([`actions_of`]).
    actions: &'static [Action],
}

/// The name a crate part gives an unlabelled control, English like the crate's other words
/// ("Dismiss", "More"); a host's own [`AccessibleLabel`] replaces it.
const DIVIDER: &str = "Divider";
const COLUMN_EDGE: &str = "Resize column";
const EMPTY_SLOT: &str = "Empty slot";
const COLOUR_AREA: &str = "Saturation and brightness";
const COMMAND_FIELD: &str = "Command";

/// Everything one entity says about itself, read once.
#[derive(QueryData)]
pub(crate) struct Facts {
    label: Option<&'static AccessibleLabel>,
    seen: Option<&'static InheritedVisibility>,
    node: Option<&'static Node>,
    disabled: Has<InteractionDisabled>,
    selected: Has<Selected>,
    checked: Has<Checked>,
    open: Has<MenuOpen>,
    capturing: Has<Capturing>,
    header: Has<SectionHeader>,
    area: Has<AreaField>,
    item: Option<&'static MenuItem>,
    toast: Option<&'static Toast>,
    meter: Option<&'static Meter>,
    progress: Option<&'static Progress>,
    row: Option<&'static TableRow>,
    field: Option<&'static TextInputOf>,
    swatch: Option<&'static ColorSwatch>,
    choice: Option<&'static ChoiceValue>,
    divider: Option<&'static SplitDivider>,
    cooldown: Option<&'static Cooldown>,
    handles: Handles,
}

/// The queries a name or a value is read through.
#[derive(SystemParam)]
pub(crate) struct Look<'w, 's> {
    texts: Query<'w, 's, &'static Text>,
    roles: Query<'w, 's, &'static Role>,
    children: Query<'w, 's, &'static Children>,
    parents: Query<'w, 's, &'static ChildOf>,
    values: Query<'w, 's, &'static TextInputValue>,
    sections: Query<'w, 's, &'static SectionOpen>,
    fractions: Query<'w, 's, &'static SplitFraction>,
    trees: Query<'w, 's, (), With<TreeView>>,
    tables: Query<'w, 's, (&'static Table, Has<InteractionDisabled>)>,
    tooltips: Query<'w, 's, &'static Tooltip>,
}

impl Look<'_, '_> {
    /// The words of every descendant of `root` whose role `want` accepts, in layout order, joined
    /// by a space. A popup, a tooltip and a section's body are another widget's words and are
    /// not entered.
    fn words(&self, root: Entity, want: impl Fn(Role) -> bool + Copy) -> Option<String> {
        let mut out = String::new();
        self.collect(root, want, &mut out);
        let trimmed = out.trim();
        (!trimmed.is_empty()).then(|| trimmed.to_owned())
    }

    fn collect(&self, from: Entity, want: impl Fn(Role) -> bool + Copy, out: &mut String) {
        let Ok(children) = self.children.get(from) else {
            return;
        };
        for child in children {
            let Ok(&role) = self.roles.get(*child) else {
                self.collect(*child, want, out);
                continue;
            };
            if matches!(
                role,
                Role::MenuPanel | Role::Popover | Role::Tooltip | Role::SectionBody
            ) {
                continue;
            }
            if want(role)
                && let Ok(text) = self.texts.get(*child)
                && !text.0.is_empty()
            {
                if !out.is_empty() {
                    out.push(' ');
                }
                out.push_str(&text.0);
            }
            self.collect(*child, want, out);
        }
    }

    /// The row's label: a toggle's, a slider's, a text field's.
    fn control_text(&self, root: Entity) -> Option<String> {
        self.words(root, |role| role == Role::ControlText)
    }

    /// A button's name: its text, else its tooltip (an icon button has no other).
    fn name_of(&self, root: Entity) -> Option<String> {
        self.words(root, names).or_else(|| {
            self.tooltips
                .get(root)
                .ok()
                .map(|tip| tip.text.clone())
                .filter(|text| !text.is_empty())
        })
    }

    fn own(&self, entity: Entity) -> Option<String> {
        self.texts
            .get(entity)
            .ok()
            .map(|text| text.0.clone())
            .filter(|text| !text.is_empty())
    }

    /// The option of a choice that is selected: the `index`th of its options, in layout order. It is
    /// read by index, not by what is shown, so a choice on a page that is hidden keeps its value.
    fn selected_option(&self, root: Entity, index: usize) -> Option<String> {
        let mut options = Vec::new();
        self.options(root, &mut options);
        options.get(index).and_then(|option| self.own(*option))
    }

    fn options(&self, from: Entity, out: &mut Vec<Entity>) {
        for child in self.children.get(from).into_iter().flat_map(|c| c.iter()) {
            if self.roles.get(child) == Ok(&Role::ChoiceOption) {
                out.push(child);
            }
            self.options(child, out);
        }
    }

    fn parent(&self, entity: Entity) -> Option<Entity> {
        self.parents.get(entity).ok().map(ChildOf::parent)
    }
}

/// What the entity of `role` says, `None` when it is decorative after all (a meter's track inside a
/// progress bar, a spinner nobody named).
fn describe(entity: Entity, role: Role, facts: &FactsItem, look: &Look) -> Option<Wanted> {
    let mut wanted = Wanted::default();
    match role {
        Role::Heading => {
            wanted.role = Some(Ak::Heading);
            wanted.name = look.own(entity);
        }
        Role::Label
        | Role::PromptKey
        | Role::PromptButton
        | Role::MenuNote
        | Role::PaletteCount
        | Role::SectionNote => {
            wanted.role = Some(Ak::Label);
            wanted.value = look.own(entity);
        }
        Role::Button => {
            wanted.name = look.name_of(entity);
            if facts.header
                && let Some(parent) = look.parent(entity)
                && let Ok(open) = look.sections.get(parent)
            {
                wanted.expanded = Some(open.0);
            }
        }
        Role::Meter => {
            let meter = facts.meter?;
            wanted.role = Some(Ak::Meter);
            wanted.numeric = Some((f64::from(meter.value), 0.0, f64::from(meter.max)));
        }
        Role::Tooltip => wanted.role = Some(Ak::Tooltip),
        Role::Scrim => {
            wanted.role = Some(Ak::Dialog);
            wanted.modal = true;
            wanted.name = look.words(entity, |role| role == Role::Heading);
        }
        Role::Toggle => wanted.name = look.control_text(entity),
        Role::Slider => {
            wanted.name = look.control_text(entity).or_else(|| {
                // A colour strip's label is the text above it, in the box that holds both.
                let parent = look.parent(entity)?;
                (look.roles.get(parent) == Ok(&Role::ColorStripBox))
                    .then(|| look.words(parent, |role| role == Role::Label))
                    .flatten()
            });
            wanted.value = look.words(entity, |role| role == Role::SliderReadout);
        }
        Role::Choice => {
            wanted.role = Some(Ak::SpinButton);
            wanted.name = look.control_text(entity);
            wanted.value = facts
                .choice
                .and_then(|value| look.selected_option(entity, value.0));
        }
        // A wheel is read as a spin button whose value is the highlighted item's name, the one
        // in its middle; the host names it with `AccessibleLabel`.
        Role::Radial => {
            wanted.role = Some(Ak::SpinButton);
            wanted.value = look.words(entity, |role| role == Role::RadialCentre);
        }
        // A slot is its item's name, and its count where there is more than one; an empty one
        // says so.
        Role::Slot => {
            wanted.name = look.words(entity, |role| role == Role::SlotName);
            wanted.value = look.words(entity, |role| role == Role::SlotCount);
        }
        Role::SlotEmpty => wanted.name = Some(EMPTY_SLOT.to_owned()),
        Role::Binding => {
            wanted.name = look.control_text(entity);
            wanted.value = if facts.capturing {
                look.words(entity, |role| role == Role::BindingPrompt)
            } else {
                look.words(entity, |role| role == Role::BindingText)
            };
        }
        Role::Toast => {
            let alert = facts
                .toast
                .is_some_and(|toast| toast.kind() == ToastKind::Alert);
            wanted.role = Some(if alert { Ak::Alert } else { Ak::Status });
            wanted.name = look.words(entity, |role| role == Role::ToastText);
        }
        Role::ToastAction => wanted.name = look.words(entity, names),
        // A table's rows are a pool of a dozen entities rebound while scrolling, so the whole
        // set's size and each row's place are said here: a reader announces "row 5 of 10,000",
        // never "row 5 of 12".
        Role::Table => {
            let rows = look.tables.get(entity).ok().map(|(table, _)| table.len());
            if look.trees.contains(entity) {
                wanted.role = Some(Ak::Tree);
                wanted.size_of_set = rows;
            } else {
                wanted.role = Some(Ak::Table);
                wanted.row_count = rows;
                wanted.column_count = look
                    .tables
                    .get(entity)
                    .ok()
                    .map(|(table, _)| table.columns().len());
            }
        }
        Role::TableRow => {
            let tree = facts.row.is_some_and(|row| look.trees.contains(row.table));
            wanted.role = Some(if tree { Ak::TreeItem } else { Ak::Row });
            wanted.selected = Some(facts.selected);
            if let Some(row) = facts.row {
                if tree {
                    wanted.position_in_set = Some(row.index);
                    tree_place(&mut wanted, row, look);
                } else {
                    wanted.row_index = Some(row.index);
                }
            }
        }
        Role::TableCell => {
            wanted.role = Some(Ak::Cell);
            wanted.name = look.words(entity, names);
        }
        Role::TableHeaderCell | Role::Tab => wanted.name = look.words(entity, names),
        Role::TableEdge => {
            wanted.role = Some(Ak::Splitter);
            wanted.name = Some(COLUMN_EDGE.to_owned());
        }
        Role::TabPage => wanted.role = Some(Ak::TabPanel),
        Role::TextField => {
            if facts.area {
                wanted.role = Some(Ak::MultilineTextInput);
            }
            let row = facts.field?.0;
            wanted.name = look.control_text(row).or_else(|| {
                (look.roles.get(row) == Ok(&Role::PaletteField)).then(|| COMMAND_FIELD.to_owned())
            });
            wanted.value = look.values.get(row).ok().map(|value| value.0.clone());
        }
        Role::ColorSwatch => {
            let row = facts.swatch?.0;
            wanted.role = Some(Ak::ColorWell);
            wanted.name = look.control_text(row);
            wanted.value = look.values.get(row).ok().map(|value| value.0.clone());
        }
        Role::ColorArea => {
            wanted.role = Some(Ak::Group);
            wanted.name = Some(COLOUR_AREA.to_owned());
        }
        Role::Viewport => wanted.role = Some(Ak::Pane),
        Role::SplitDivider => {
            facts.divider?;
            wanted.role = Some(Ak::Splitter);
            wanted.name = Some(DIVIDER.to_owned());
            let fraction = look
                .parent(entity)
                .and_then(|parent| look.fractions.get(parent).ok());
            wanted.numeric = fraction.map(|fraction| (f64::from(fraction.0), 0.0, 1.0));
        }
        Role::MenuBar => wanted.role = Some(Ak::MenuBar),
        Role::MenuPanel => wanted.role = Some(Ak::Menu),
        Role::MenuTitle => {
            wanted.role = Some(Ak::MenuItem);
            wanted.name = look.words(entity, names);
            wanted.popup = true;
            wanted.expanded = Some(facts.open);
        }
        Role::MenuItem => {
            let (role, popup) = facts.item.map_or((Ak::MenuItem, false), MenuItem::access);
            wanted.role = Some(role);
            wanted.name = look.words(entity, names);
            wanted.popup = popup;
            if popup {
                wanted.expanded = Some(facts.open);
            }
            if matches!(role, Ak::MenuItemCheckBox | Ak::MenuItemRadio) {
                wanted.toggled = Some(facts.checked);
            }
        }
        Role::PaletteRow => {
            wanted.role = Some(Ak::ListBoxOption);
            wanted.name = look.words(entity, names);
            wanted.selected = Some(facts.selected);
        }
        Role::Progress => {
            let progress = facts.progress?;
            wanted.role = Some(Ak::ProgressIndicator);
            wanted.numeric = Some((f64::from(progress.0), 0.0, 1.0));
            wanted.value = look.words(entity, |role| role == Role::ProgressLine);
        }
        Role::Spinner => {
            facts.label?;
            wanted.role = Some(Ak::ProgressIndicator);
        }
        Role::Toolbar => wanted.role = Some(Ak::Toolbar),
        Role::ToggleGroup => wanted.role = Some(Ak::RadioGroup),
        Role::GroupOption => {
            wanted.role = Some(Ak::RadioButton);
            wanted.name = look.name_of(entity);
            wanted.toggled = Some(facts.checked);
        }
        _ => {}
    }
    if let Some(label) = facts.label {
        wanted.name = Some(label.0.clone());
    }
    // A cooldown running is part of what the widget is: its seconds follow the name, the host's
    // own label included.
    if let Some(cooldown) = facts.cooldown
        && !cooldown.is_ready()
        && let Some(name) = wanted.name.as_mut()
    {
        name.push_str(", ");
        name.push_str(&cooldown_words(cooldown.remaining));
    }
    wanted.disabled = facts.disabled;
    wanted.actions = if wanted.tree_branch {
        TREE_BRANCH
    } else {
        actions_of(&facts.handles)
    };
    wanted.hidden = facts.seen.is_some_and(|seen| !seen.get())
        || facts.node.is_some_and(|node| node.display == Display::None);
    Some(wanted)
}

/// A tree row's depth and whether it opens, read from the [`Tree`] by the row's key: a row is a
/// pooled entity, so the node it shows changes under it. A branch that cannot be opened (the
/// tree is disabled) still says its state, and declares no action.
fn tree_place(wanted: &mut Wanted, row: &TableRow, look: &Look) {
    let Ok((table, disabled)) = look.tables.get(row.table) else {
        return;
    };
    let Some(outline) = table
        .source::<Tree>()
        .and_then(|tree| tree.outline_of(row.key))
    else {
        return;
    };
    wanted.level = Some(outline.depth);
    match outline.open {
        Some(open) => {
            wanted.expanded = Some(open);
            if !disabled {
                wanted.tree_branch = true;
            }
        }
        None => wanted.end_node = true,
    }
}

/// Whether `node` already says everything `wanted` asks.
fn says(node: &Node_, wanted: &Wanted) -> bool {
    wanted.role.is_none_or(|role| node.role() == role)
        && node.label() == wanted.name.as_deref()
        && node.value() == wanted.value.as_deref()
        && wanted.numeric.is_none_or(|(value, min, max)| {
            node.numeric_value() == Some(value)
                && node.min_numeric_value() == Some(min)
                && node.max_numeric_value() == Some(max)
        })
        && wanted
            .toggled
            .is_none_or(|on| node.toggled() == Some(Toggled::from(on)))
        && wanted
            .selected
            .is_none_or(|selected| node.is_selected() == Some(selected))
        && wanted
            .expanded
            .is_none_or(|expanded| node.is_expanded() == Some(expanded))
        && (!wanted.end_node || node.is_expanded().is_none())
        && wanted.level.is_none_or(|level| node.level() == Some(level))
        && wanted
            .position_in_set
            .is_none_or(|at| node.position_in_set() == Some(at))
        && wanted
            .size_of_set
            .is_none_or(|size| node.size_of_set() == Some(size))
        && wanted
            .row_index
            .is_none_or(|at| node.row_index() == Some(at))
        && wanted
            .row_count
            .is_none_or(|count| node.row_count() == Some(count))
        && wanted
            .column_count
            .is_none_or(|count| node.column_count() == Some(count))
        && (!wanted.popup || node.has_popup() == Some(HasPopup::Menu))
        && node.is_modal() == wanted.modal
        && node.is_hidden() == wanted.hidden
        && node.is_disabled() == wanted.disabled
        && MANAGED
            .iter()
            .all(|action| node.supports_action(*action) == wanted.actions.contains(action))
}

/// Writes what `wanted` asks, field by field.
fn write(node: &mut Node_, wanted: &Wanted) {
    if let Some(role) = wanted.role {
        node.set_role(role);
    }
    match &wanted.name {
        Some(name) => node.set_label(name.as_str()),
        None => node.clear_label(),
    }
    match &wanted.value {
        Some(value) => node.set_value(value.as_str()),
        None => node.clear_value(),
    }
    if let Some((value, min, max)) = wanted.numeric {
        node.set_numeric_value(value);
        node.set_min_numeric_value(min);
        node.set_max_numeric_value(max);
    }
    if let Some(on) = wanted.toggled {
        node.set_toggled(Toggled::from(on));
    }
    if let Some(selected) = wanted.selected {
        node.set_selected(selected);
    }
    if let Some(expanded) = wanted.expanded {
        node.set_expanded(expanded);
    }
    if wanted.end_node {
        node.clear_expanded();
    }
    if let Some(level) = wanted.level {
        node.set_level(level);
    }
    if let Some(at) = wanted.position_in_set {
        node.set_position_in_set(at);
    }
    if let Some(size) = wanted.size_of_set {
        node.set_size_of_set(size);
    }
    if let Some(at) = wanted.row_index {
        node.set_row_index(at);
    }
    if let Some(count) = wanted.row_count {
        node.set_row_count(count);
    }
    if let Some(count) = wanted.column_count {
        node.set_column_count(count);
    }
    if wanted.popup {
        node.set_has_popup(HasPopup::Menu);
    }
    flag(wanted.modal, node, Node_::set_modal, Node_::clear_modal);
    flag(wanted.hidden, node, Node_::set_hidden, Node_::clear_hidden);
    flag(
        wanted.disabled,
        node,
        Node_::set_disabled,
        Node_::clear_disabled,
    );
    for action in MANAGED {
        if wanted.actions.contains(&action) {
            node.add_action(action);
        } else {
            node.remove_action(action);
        }
    }
}

fn flag(on: bool, node: &mut Node_, set: fn(&mut Node_), clear: fn(&mut Node_)) {
    if on {
        set(node);
    } else {
        clear(node);
    }
}

/// Gives every widget its accessibility node and keeps its name, value and state true, while an
/// assistive technology is connected (`AccessibilityRequested`); a game without one pays for one
/// resource read. Runs last in [`crate::GooeySystems::Style`].
pub(crate) fn update_accessibility(
    requested: Option<Res<AccessibilityRequested>>,
    mut commands: Commands,
    mut nodes: Query<(Entity, &Role, Option<&mut AccessibilityNode>, Facts)>,
    look: Look,
) {
    if requested.is_some_and(|requested| !requested.get()) {
        return;
    }
    for (entity, &role, node, facts) in &mut nodes {
        let kind = spec(role);
        let wanted = match kind {
            Spec::Hidden => None,
            Spec::Group => {
                let mut group = Wanted {
                    role: Some(Ak::GenericContainer),
                    ..default()
                };
                group.name = facts.label.map(|label| label.0.clone());
                group.hidden = facts.seen.is_some_and(|seen| !seen.get())
                    || facts.node.is_some_and(|node| node.display == Display::None);
                Some(group)
            }
            Spec::Node => describe(entity, role, &facts, &look),
        };
        match (node, wanted) {
            (Some(mut node), Some(wanted)) => {
                if !says(&node, &wanted) {
                    write(&mut node, &wanted);
                }
            }
            (Some(mut node), None) => {
                if !node.is_hidden() {
                    node.set_hidden();
                }
            }
            (None, Some(wanted)) => {
                let mut node = Node_::new(wanted.role.unwrap_or(Ak::GenericContainer));
                write(&mut node, &wanted);
                commands
                    .entity(entity)
                    .insert(AccessibilityNode::from(node));
                // Bevy sets a node's bounds when its layout changes; a node added now has not
                // had one, so ask for it.
                commands.queue(move |world: &mut World| {
                    if let Some(mut computed) = world.get_mut::<ComputedNode>(entity) {
                        computed.set_changed();
                    }
                });
            }
            (None, None) => {}
        }
    }
}