quickgui 0.1.6

A damage-driven, GPU-accelerated desktop GUI foundation
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
use std::sync::Arc;

use crate::{
    AccessibilityOrientation, AccessibilityRole, Element, ElementId, FocusHandle, KeyBinding,
    StateAccessor, ViewContext, div,
};

/// Maximum items retained in one toolbar declaration.
///
/// Roving focus scans the caller's ordered item list rather than a registry, so the bound keeps
/// one keypress a bounded walk even when application data drives the toolbar.
pub const MAX_TOOLBAR_ITEMS: usize = 256;

const TOOLBAR_HORIZONTAL_KEY_CONTEXT: &str = "ToolbarHorizontal";
const TOOLBAR_VERTICAL_KEY_CONTEXT: &str = "ToolbarVertical";
const TOOLBAR_ITEM_ID_TAG: u64 = 0x9f14_c6b7_2a05_d8e3;

/// Move toolbar focus to the next enabled item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ToolbarNext;
/// Move toolbar focus to the previous enabled item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ToolbarPrevious;
/// Move toolbar focus to the first enabled item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ToolbarFirst;
/// Move toolbar focus to the final enabled item.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ToolbarLast;

/// Contextual bindings used by [`ToolbarEntry::key_with`].
///
/// A horizontal toolbar answers Left and Right; a vertical toolbar answers Up and Down. The other
/// axis is left to the surrounding application, which matches the tab-list contract.
pub fn toolbar_key_bindings() -> [KeyBinding; 8] {
    [
        KeyBinding::new("right", ToolbarNext, Some(TOOLBAR_HORIZONTAL_KEY_CONTEXT)),
        KeyBinding::new(
            "left",
            ToolbarPrevious,
            Some(TOOLBAR_HORIZONTAL_KEY_CONTEXT),
        ),
        KeyBinding::new("home", ToolbarFirst, Some(TOOLBAR_HORIZONTAL_KEY_CONTEXT)),
        KeyBinding::new("end", ToolbarLast, Some(TOOLBAR_HORIZONTAL_KEY_CONTEXT)),
        KeyBinding::new("down", ToolbarNext, Some(TOOLBAR_VERTICAL_KEY_CONTEXT)),
        KeyBinding::new("up", ToolbarPrevious, Some(TOOLBAR_VERTICAL_KEY_CONTEXT)),
        KeyBinding::new("home", ToolbarFirst, Some(TOOLBAR_VERTICAL_KEY_CONTEXT)),
        KeyBinding::new("end", ToolbarLast, Some(TOOLBAR_VERTICAL_KEY_CONTEXT)),
    ]
}

/// Layout and keyboard axis for one toolbar.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ToolbarOrientation {
    #[default]
    Horizontal,
    Vertical,
}

impl ToolbarOrientation {
    const fn accessibility(self) -> AccessibilityOrientation {
        match self {
            Self::Horizontal => AccessibilityOrientation::Horizontal,
            Self::Vertical => AccessibilityOrientation::Vertical,
        }
    }

    const fn key_context(self) -> &'static str {
        match self {
            Self::Horizontal => TOOLBAR_HORIZONTAL_KEY_CONTEXT,
            Self::Vertical => TOOLBAR_VERTICAL_KEY_CONTEXT,
        }
    }
}

/// Allocation-free controlled roving-focus value for one toolbar.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ToolbarState {
    active: Option<ElementId>,
}

impl ToolbarState {
    pub fn new(active: impl Into<ElementId>) -> Self {
        Self {
            active: Some(active.into()),
        }
    }

    pub const fn empty() -> Self {
        Self { active: None }
    }

    pub const fn active(&self) -> Option<ElementId> {
        self.active
    }

    /// Replace the roving tab stop, returning whether it changed.
    pub fn set_active(&mut self, active: Option<ElementId>) -> bool {
        if self.active == active {
            false
        } else {
            self.active = active;
            true
        }
    }

    pub fn focus(&mut self, value: impl Into<ElementId>) -> bool {
        self.set_active(Some(value.into()))
    }

    pub fn clear(&mut self) -> bool {
        self.set_active(None)
    }
}

/// One caller-declared toolbar item.
///
/// The application owns the item's content and behavior. QuickGUI needs only its stable value and
/// whether it participates in keyboard navigation.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ToolbarItem {
    value: ElementId,
    disabled: bool,
    focusable_when_disabled: bool,
}

impl ToolbarItem {
    pub fn new(value: impl Into<ElementId>) -> Self {
        Self {
            value: value.into(),
            disabled: false,
            focusable_when_disabled: true,
        }
    }

    #[must_use]
    pub const fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    pub const fn value(self) -> ElementId {
        self.value
    }

    /// Choose whether this item stays reachable by keyboard while it is disabled.
    ///
    /// This is Base UI's `focusableWhenDisabled`, and a toolbar defaults it to `true`: skipping an
    /// unavailable item entirely hides it from keyboard users, who then cannot discover that the
    /// command exists at all. The item still reports as disabled and still refuses pointer focus.
    pub const fn focusable_when_disabled(mut self, focusable: bool) -> Self {
        self.focusable_when_disabled = focusable;
        self
    }

    pub const fn is_disabled(self) -> bool {
        self.disabled
    }

    /// Whether a disabled item keeps its place in the keyboard sequence.
    pub const fn is_focusable_when_disabled(self) -> bool {
        self.focusable_when_disabled
    }
}

/// A controlled, unstyled toolbar descriptor.
///
/// The application owns every item, separator, icon, label, and visual state. QuickGUI supplies
/// the Toolbar role and orientation, one roving Tab stop, bounded arrow/Home/End navigation, and
/// disabled-item skipping. The descriptor borrows the caller's ordered item list and retains no
/// registry, task, timer, observer, or idle scheduler source.
#[derive(Clone, Copy, Debug)]
#[must_use = "a Toolbar descriptor has no effect until its parts are mounted"]
pub struct Toolbar<'a> {
    root_id: ElementId,
    items: &'a [ToolbarItem],
    active: Option<ElementId>,
    orientation: ToolbarOrientation,
    loop_focus: bool,
}

impl<'a> Toolbar<'a> {
    /// Create a toolbar over the caller's ordered items.
    ///
    /// # Panics
    ///
    /// Panics with more than [`MAX_TOOLBAR_ITEMS`] items or duplicate item values.
    pub fn new(
        root_id: impl Into<ElementId>,
        state: &ToolbarState,
        items: &'a [ToolbarItem],
    ) -> Self {
        assert_toolbar_items(items);
        Self {
            root_id: root_id.into(),
            items,
            active: state.active(),
            orientation: ToolbarOrientation::Horizontal,
            loop_focus: true,
        }
    }

    pub const fn orientation(mut self, orientation: ToolbarOrientation) -> Self {
        self.orientation = orientation;
        self
    }

    pub const fn vertical(self) -> Self {
        self.orientation(ToolbarOrientation::Vertical)
    }

    /// Wrap from the last item to the first. The default is `true`.
    pub const fn loop_focus(mut self, loop_focus: bool) -> Self {
        self.loop_focus = loop_focus;
        self
    }

    pub const fn root_id(self) -> ElementId {
        self.root_id
    }

    pub const fn items(self) -> &'a [ToolbarItem] {
        self.items
    }

    pub const fn axis(self) -> ToolbarOrientation {
        self.orientation
    }

    pub fn item_id(self, value: impl Into<ElementId>) -> ElementId {
        derived_toolbar_id(self.root_id, value.into())
    }

    /// The item that currently owns the toolbar's single Tab stop.
    ///
    /// This is the controlled active value while it names an enabled mounted item, and otherwise
    /// the first enabled item, matching the composite-widget convention.
    pub fn roving_value(self) -> Option<ElementId> {
        let active = self.active.filter(|active| {
            self.items
                .iter()
                .any(|item| item.value == *active && !item.disabled)
        });
        active.or_else(|| {
            self.items
                .iter()
                .find(|item| !item.disabled)
                .map(|item| item.value)
        })
    }

    /// Decorate an application-owned toolbar root without adding layout or appearance.
    pub fn root_with(self, root: Element) -> Element {
        root.id(self.root_id)
            .accessibility_role(AccessibilityRole::Toolbar)
            .accessibility_orientation(self.orientation.accessibility())
            .app_region_no_drag()
    }
    /// Create the unstyled root part. Use [`Self::root_with`] to supply an existing element.
    pub fn root(self) -> Element {
        self.root_with(crate::div())
    }

    /// Decorate a caller-owned group of related toolbar items, Base UI's Toolbar.Group.
    ///
    /// The group is a labelling and structural unit only: every item inside it keeps taking part
    /// in the toolbar's single roving Tab stop, so grouping never creates a second focus scope.
    pub fn group_with(self, group: Element) -> Element {
        group
            .accessibility_role(AccessibilityRole::Group)
            .accessibility_orientation(self.orientation.accessibility())
            .app_region_no_drag()
    }
    /// Create the unstyled group part. Use [`Self::group_with`] to supply an existing element.
    pub fn group(self) -> Element {
        self.group_with(crate::div())
    }

    /// Decorate a caller-owned divider between toolbar groups, Base UI's Toolbar.Separator.
    ///
    /// A separator carries the Separator role across the toolbar's cross axis and never takes
    /// focus, so arrow navigation steps straight past it.
    pub fn separator_with(self, separator: Element) -> Element {
        separator
            .accessibility_role(AccessibilityRole::Separator)
            .accessibility_orientation(match self.orientation {
                ToolbarOrientation::Horizontal => AccessibilityOrientation::Vertical,
                ToolbarOrientation::Vertical => AccessibilityOrientation::Horizontal,
            })
            .app_region_no_drag()
            .user_select_none()
    }
    /// Create the unstyled separator part. Use [`Self::separator_with`] to supply an existing element.
    pub fn separator(self) -> Element {
        self.separator_with(crate::div())
    }

    /// Describe one declared item.
    pub fn item(self, value: impl Into<ElementId>) -> Option<ToolbarEntry<'a>> {
        let value = value.into();
        let item = self
            .items
            .iter()
            .copied()
            .find(|item| item.value == value)?;
        Some(ToolbarEntry {
            toolbar: self,
            item,
            roving: self.roving_value() == Some(value),
        })
    }
}

/// A copyable declaration for one mounted toolbar item.
#[derive(Clone, Copy, Debug)]
#[must_use = "a ToolbarEntry descriptor has no effect until its part is mounted"]
pub struct ToolbarEntry<'a> {
    toolbar: Toolbar<'a>,
    item: ToolbarItem,
    roving: bool,
}

impl<'a> ToolbarEntry<'a> {
    pub const fn value(self) -> ElementId {
        self.item.value
    }

    pub const fn is_disabled(self) -> bool {
        self.item.disabled
    }

    /// Whether this item owns the toolbar's single Tab stop.
    pub const fn is_roving_stop(self) -> bool {
        self.roving
    }

    pub fn item_id(self) -> ElementId {
        self.toolbar.item_id(self.item.value)
    }

    /// Decorate an application-owned item without adding appearance.
    ///
    /// Exactly one enabled item is in the window's normal Tab sequence; the rest are reachable
    /// with the toolbar's arrow keys.
    pub fn item_with(self, item: Element) -> Element {
        let disabled = self.item.disabled || item.accessibility.disabled;
        let item = item
            .id(self.item_id())
            .focusable()
            .tab_index(if self.roving { 0 } else { -1 })
            .key_context(self.toolbar.orientation.key_context())
            .cursor_default()
            .app_region_no_drag()
            .user_select_none()
            .disabled(disabled);
        if disabled && self.item.focusable_when_disabled {
            item.focusable_when_disabled()
        } else {
            item
        }
    }
    /// Create the unstyled item part. Use [`Self::item_with`] to supply an existing element.
    pub fn item(self) -> Element {
        self.item_with(crate::div())
    }

    /// Decorate an application-owned toolbar button, Base UI's Toolbar.Button.
    ///
    /// This is [`Self::item_with`] plus the Button role and click activation.
    pub fn button_with(self, button: Element) -> Element {
        self.item_with(button.accessibility_role(AccessibilityRole::Button))
    }
    /// Create the unstyled button part. Use [`Self::button_with`] to supply an existing element.
    pub fn button(self) -> Element {
        self.button_with(crate::button())
    }

    /// Decorate an application-owned toolbar link, Base UI's Toolbar.Link.
    pub fn link_with(self, link: Element) -> Element {
        self.item_with(link.accessibility_role(AccessibilityRole::Link))
    }
    /// Create the unstyled link part. Use [`Self::link_with`] to supply an existing element.
    pub fn link(self) -> Element {
        self.link_with(crate::div())
    }

    /// Decorate an application-owned toolbar input, Base UI's Toolbar.Input.
    ///
    /// The element keeps whatever role it already declares — a [`crate::text_input`] is already a
    /// text field — and gains the toolbar's roving Tab stop and arrow key context.
    pub fn input_with(self, input: Element) -> Element {
        self.item_with(input)
    }
    /// Create the unstyled input part. Use [`Self::input_with`] to supply an existing element.
    pub fn input(self) -> Element {
        self.input_with(crate::text_input(""))
    }

    /// Attach QuickGUI's typed toolbar navigation to this item.
    ///
    /// Each item answers its own arrow keys, so the focused item is always the one that moves.
    /// Install [`toolbar_key_bindings`] once on the application keymap.
    pub fn key_with<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        item: Element,
        access: fn(&mut V) -> &mut ToolbarState,
    ) -> Element {
        self.key_with_accessor(cx, item, StateAccessor::from(access))
    }
    /// Create the unstyled key part. Use [`Self::key_with`] to supply an existing element.
    pub fn key<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        access: fn(&mut V) -> &mut ToolbarState,
    ) -> Element {
        self.key_with(cx, crate::div(), access)
    }

    /// Attach the typed toolbar navigation against a per-instance state accessor.
    ///
    /// A host that renders many declared toolbars through one view passes an accessor that
    /// captures which [`ToolbarState`] this item belongs to.
    pub fn key_with_accessor<V: 'static>(
        self,
        cx: &mut ViewContext<'_, V>,
        item: Element,
        access_source: StateAccessor<V, ToolbarState>,
    ) -> Element {
        let id = self.item_id();
        let root = self.toolbar.root_id;
        let value = self.item.value;
        let loop_focus = self.toolbar.loop_focus;
        let items: Arc<[ToolbarItem]> = Arc::from(self.toolbar.items);

        let next_items = items.clone();
        let access = access_source.clone();
        let next = cx.action_listener(id, move |view, _: &ToolbarNext, cx| {
            move_focus(
                view,
                cx,
                &access,
                root,
                neighbor_value(&next_items, value, true, loop_focus),
            );
        });
        let previous_items = items.clone();
        let access = access_source.clone();
        let previous = cx.action_listener(id, move |view, _: &ToolbarPrevious, cx| {
            move_focus(
                view,
                cx,
                &access,
                root,
                neighbor_value(&previous_items, value, false, loop_focus),
            );
        });
        let first_items = items.clone();
        let access = access_source.clone();
        let first = cx.action_listener(id, move |view, _: &ToolbarFirst, cx| {
            move_focus(view, cx, &access, root, edge_value(&first_items, false));
        });
        let access = access_source;
        let last = cx.action_listener(id, move |view, _: &ToolbarLast, cx| {
            move_focus(view, cx, &access, root, edge_value(&items, true));
        });

        item.on_action(next)
            .on_action(previous)
            .on_action(first)
            .on_action(last)
    }
}

fn move_focus<V: 'static>(
    view: &mut V,
    cx: &mut crate::EventContext,
    access: &StateAccessor<V, ToolbarState>,
    root: ElementId,
    target: Option<ElementId>,
) {
    let Some(target) = target else {
        return;
    };
    let changed = access.get(view).focus(target);
    cx.focus(FocusHandle::new(derived_toolbar_id(root, target)));
    if changed {
        cx.invalidate();
    }
}

fn neighbor_value(
    items: &[ToolbarItem],
    from: ElementId,
    forward: bool,
    loop_focus: bool,
) -> Option<ElementId> {
    let position = items.iter().position(|item| item.value == from)?;
    let count = items.len();
    let mut index = position;
    for _ in 0..count {
        index = if forward {
            if index + 1 == count {
                if !loop_focus {
                    return None;
                }
                0
            } else {
                index + 1
            }
        } else if index == 0 {
            if !loop_focus {
                return None;
            }
            count - 1
        } else {
            index - 1
        };
        if !items[index].disabled {
            return Some(items[index].value);
        }
    }
    None
}

fn edge_value(items: &[ToolbarItem], last: bool) -> Option<ElementId> {
    let mut enabled = items.iter().filter(|item| !item.disabled);
    if last {
        enabled.next_back().map(|item| item.value)
    } else {
        enabled.next().map(|item| item.value)
    }
}

fn assert_toolbar_items(items: &[ToolbarItem]) {
    assert!(
        items.len() <= MAX_TOOLBAR_ITEMS,
        "a toolbar retains at most {MAX_TOOLBAR_ITEMS} items"
    );
    for (index, item) in items.iter().enumerate() {
        assert!(
            !items[..index]
                .iter()
                .any(|earlier| earlier.value == item.value),
            "toolbar item values must be unique"
        );
    }
}

/// Create an unstyled semantic toolbar root.
///
/// This shorthand is equivalent to `Toolbar::new(id, state, items).root_with(div())`.
pub fn toolbar(id: impl Into<ElementId>, state: &ToolbarState, items: &[ToolbarItem]) -> Element {
    Toolbar::new(id, state, items).root_with(div())
}

fn derived_toolbar_id(scope: ElementId, value: ElementId) -> ElementId {
    let mut hash = scope
        .as_u64()
        .rotate_left(13)
        .wrapping_add(value.as_u64().rotate_right(29))
        ^ TOOLBAR_ITEM_ID_TAG;
    hash ^= hash >> 30;
    hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
    hash ^= hash >> 27;
    hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
    hash ^= hash >> 31;
    if hash == 0 || hash == u64::MAX || hash == scope.as_u64() || hash == value.as_u64() {
        hash ^= TOOLBAR_ITEM_ID_TAG.rotate_left(19);
    }
    ElementId::new(hash)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        AppRegion, Application, Color, CursorStyle, IntoElement, UserSelect, View, WindowOptions,
        button, text,
    };

    fn items() -> [ToolbarItem; 4] {
        [
            ToolbarItem::new("bold"),
            ToolbarItem::new("italic").disabled(true),
            ToolbarItem::new("underline"),
            ToolbarItem::new("link"),
        ]
    }

    #[test]
    fn roving_stop_and_parts_are_exact_and_unstyled() {
        let items = items();
        let state = ToolbarState::empty();
        let toolbar = Toolbar::new("format", &state, &items);
        assert_eq!(toolbar.roving_value(), Some("bold".into()));

        let focused = ToolbarState::new("underline");
        let toolbar = Toolbar::new("format", &focused, &items);
        assert_eq!(toolbar.roving_value(), Some("underline".into()));

        // A disabled or absent active value falls back to the first enabled item.
        let disabled_active = ToolbarState::new("italic");
        assert_eq!(
            Toolbar::new("format", &disabled_active, &items).roving_value(),
            Some("bold".into())
        );
        let missing = ToolbarState::new("missing");
        assert_eq!(
            Toolbar::new("format", &missing, &items).roving_value(),
            Some("bold".into())
        );

        let root = toolbar.root_with(div().gap_2().bg(Color::rgb8(1, 2, 3)));
        assert_eq!(root.explicit_id, Some("format".into()));
        assert_eq!(root.accessibility.role, AccessibilityRole::Toolbar);
        assert_eq!(
            root.accessibility.orientation,
            Some(AccessibilityOrientation::Horizontal)
        );
        assert_eq!(root.app_region, Some(AppRegion::NoDrag));
        assert_eq!(root.visual.background, Some(Color::rgb8(1, 2, 3)));
        assert!(!root.focusable);

        let underline = toolbar.item("underline").expect("underline entry");
        assert!(underline.is_roving_stop());
        let element = underline.item_with(button().child("Underline"));
        assert_eq!(element.explicit_id, Some(toolbar.item_id("underline")));
        assert_eq!(element.tab_index, 0);
        assert!(element.focusable);
        assert_eq!(element.cursor_style, Some(CursorStyle::Arrow));
        assert_eq!(element.user_select, UserSelect::None);

        let bold = toolbar.item("bold").expect("bold entry");
        assert!(!bold.is_roving_stop());
        assert_eq!(bold.item_with(div()).tab_index, -1);

        let italic = toolbar.item("italic").expect("italic entry");
        assert!(italic.is_disabled());
        assert!(italic.item_with(div()).accessibility.disabled);
        assert!(toolbar.item("absent").is_none());

        let vertical = Toolbar::new("sidebar", &focused, &items).vertical();
        assert_eq!(
            vertical.root_with(div()).accessibility.orientation,
            Some(AccessibilityOrientation::Vertical)
        );

        let ids = [
            toolbar.root_id(),
            toolbar.item_id("bold"),
            toolbar.item_id("italic"),
            toolbar.item_id("underline"),
        ];
        for (index, id) in ids.iter().enumerate() {
            assert!(!ids[..index].contains(id));
        }
    }

    #[test]
    fn navigation_skips_disabled_items_and_honors_looping() {
        let items = items();
        assert_eq!(
            neighbor_value(&items, "bold".into(), true, true),
            Some("underline".into())
        );
        assert_eq!(
            neighbor_value(&items, "link".into(), true, true),
            Some("bold".into())
        );
        assert_eq!(neighbor_value(&items, "link".into(), true, false), None);
        assert_eq!(
            neighbor_value(&items, "bold".into(), false, true),
            Some("link".into())
        );
        assert_eq!(neighbor_value(&items, "bold".into(), false, false), None);
        assert_eq!(neighbor_value(&items, "absent".into(), true, true), None);
        assert_eq!(edge_value(&items, false), Some("bold".into()));
        assert_eq!(edge_value(&items, true), Some("link".into()));

        let all_disabled = [ToolbarItem::new("a").disabled(true)];
        assert_eq!(edge_value(&all_disabled, false), None);
        assert_eq!(neighbor_value(&all_disabled, "a".into(), true, true), None);
    }

    #[derive(Default)]
    struct ToolbarView {
        toolbar: ToolbarState,
        clicked: Option<ElementId>,
    }

    impl ToolbarView {
        fn toolbar(view: &mut Self) -> &mut ToolbarState {
            &mut view.toolbar
        }
    }

    impl View for ToolbarView {
        fn render(&mut self, cx: &mut ViewContext<'_, Self>) -> impl IntoElement {
            let items = items();
            let toolbar = Toolbar::new("format", &self.toolbar, &items);
            let mut root = toolbar.root_with(div());
            for item in items {
                let entry = toolbar.item(item.value()).expect("declared entry");
                let value = item.value();
                let clicked = cx.listener(entry.item_id(), move |view: &mut Self, cx| {
                    view.clicked = Some(value);
                    view.toolbar.focus(value);
                    cx.invalidate();
                });
                root = root.child(entry.key_with(
                    cx,
                    entry.item_with(div().child(text("Item")).on_click(clicked)),
                    Self::toolbar,
                ));
            }
            div()
                .child(button().id("before").child("Before"))
                .child(root)
        }
    }

    #[test]
    fn keyboard_focus_and_accessibility_paths_stay_deterministic() {
        let (mut cx, view) = Application::new()
            .bind_keys(toolbar_key_bindings())
            .into_test_context(WindowOptions::default(), ToolbarView::default())
            .unwrap();
        let window = view.window_handle();
        let state = ToolbarState::empty();
        let items = items();
        let toolbar = Toolbar::new("format", &state, &items);

        cx.simulate_keystrokes(window, "tab tab").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(toolbar.item_id("bold")));
        cx.simulate_keystrokes(window, "right").unwrap();
        assert_eq!(
            cx.focused(window).unwrap(),
            Some(toolbar.item_id("underline"))
        );
        assert_eq!(
            cx.read(view, |view| view.toolbar.active()).unwrap(),
            Some("underline".into())
        );
        cx.simulate_keystrokes(window, "right right").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(toolbar.item_id("bold")));
        cx.simulate_keystrokes(window, "left").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(toolbar.item_id("link")));
        cx.simulate_keystrokes(window, "home").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(toolbar.item_id("bold")));
        cx.simulate_keystrokes(window, "end").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(toolbar.item_id("link")));
        // The cross axis is left to the application.
        cx.simulate_keystrokes(window, "down").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(toolbar.item_id("link")));
        assert!(cx.click(window, toolbar.item_id("italic")).is_err());

        // Only the roving item is in the normal Tab sequence.
        cx.focus(window, "before").unwrap();
        cx.simulate_keystrokes(window, "tab").unwrap();
        assert_eq!(cx.focused(window).unwrap(), Some(toolbar.item_id("link")));

        let update = cx.accessibility_update(window).unwrap();
        let node = |id: ElementId| {
            update
                .nodes
                .iter()
                .find_map(|(node_id, node)| (node_id.0 == id.as_u64()).then_some(node))
                .expect("toolbar accessibility node")
        };
        assert_eq!(node("format".into()).role(), accesskit::Role::Toolbar);
        assert_eq!(
            node("format".into()).orientation(),
            Some(accesskit::Orientation::Horizontal)
        );
        assert!(node(toolbar.item_id("italic")).is_disabled());

        let renders = cx.render_count(window).unwrap();
        cx.run_until_idle().unwrap();
        assert_eq!(cx.render_count(window).unwrap(), renders);
    }

    #[test]
    fn bindings_are_contextual_and_complete() {
        let bindings = toolbar_key_bindings();
        assert_eq!(bindings.len(), 8);
        assert!(bindings.iter().all(|binding| {
            binding.context_predicate().is_some_and(|context| {
                context
                    .depth_of(&[crate::KeyContext::parse(TOOLBAR_HORIZONTAL_KEY_CONTEXT).unwrap()])
                    .is_some()
                    || context
                        .depth_of(
                            &[crate::KeyContext::parse(TOOLBAR_VERTICAL_KEY_CONTEXT).unwrap()],
                        )
                        .is_some()
            })
        }));
    }

    #[test]
    #[should_panic(expected = "toolbar item values must be unique")]
    fn duplicate_item_values_are_rejected() {
        let items = [ToolbarItem::new("same"), ToolbarItem::new("same")];
        let _ = Toolbar::new("format", &ToolbarState::empty(), &items);
    }

    #[test]
    fn shorthand_is_a_semantic_unstyled_root() {
        let items = items();
        let element = toolbar("format", &ToolbarState::empty(), &items);
        assert_eq!(element.accessibility.role, AccessibilityRole::Toolbar);
        assert!(element.children.is_empty());
        assert_eq!(element.visual.background, None);
    }

    #[test]
    fn base_ui_toolbar_parts_add_roles_without_a_second_focus_scope() {
        let state = ToolbarState::new("bold");
        let items = [
            ToolbarItem::new("bold"),
            ToolbarItem::new("italic").disabled(true),
            ToolbarItem::new("link"),
            ToolbarItem::new("search"),
        ];
        let toolbar = Toolbar::new("format", &state, &items);

        let button = toolbar
            .item("bold")
            .expect("declared item")
            .button_with(div());
        assert_eq!(button.accessibility.role, AccessibilityRole::Button);
        assert_eq!(button.tab_index, 0);
        assert_eq!(button.visual.background, None);

        let link = toolbar
            .item("link")
            .expect("declared item")
            .link_with(div());
        assert_eq!(link.accessibility.role, AccessibilityRole::Link);
        assert_eq!(link.tab_index, -1);

        // An input keeps whatever role it already declares and only joins the roving Tab stop.
        let input = toolbar
            .item("search")
            .expect("declared item")
            .input_with(crate::text_input("query"));
        assert_eq!(input.accessibility.role, AccessibilityRole::TextInput);
        assert_eq!(input.tab_index, -1);
        assert_eq!(input.key_context, button.key_context);

        // A disabled toolbar item stays reachable by keyboard so it can still be discovered.
        let unavailable = toolbar
            .item("italic")
            .expect("declared item")
            .button_with(div());
        assert!(unavailable.accessibility.disabled);
        assert!(unavailable.is_keyboard_focusable());
        assert!(ToolbarItem::new("x").is_focusable_when_disabled());

        let skipped_items = [ToolbarItem::new("bold")
            .disabled(true)
            .focusable_when_disabled(false)];
        let skipped = Toolbar::new("format", &state, &skipped_items)
            .item("bold")
            .expect("declared item")
            .item_with(div());
        assert!(!skipped.is_keyboard_focusable());

        let group = toolbar.group_with(div().bg(Color::rgb8(1, 2, 3)));
        assert_eq!(group.accessibility.role, AccessibilityRole::Group);
        assert_eq!(
            group.accessibility.orientation,
            Some(AccessibilityOrientation::Horizontal)
        );
        assert_eq!(group.visual.background, Some(Color::rgb8(1, 2, 3)));
        assert!(!group.focusable);

        // A separator crosses the toolbar's axis and never takes focus.
        let separator = toolbar.separator_with(div());
        assert_eq!(separator.accessibility.role, AccessibilityRole::Separator);
        assert_eq!(
            separator.accessibility.orientation,
            Some(AccessibilityOrientation::Vertical)
        );
        assert!(!separator.focusable);
        assert_eq!(separator.visual.background, None);
        assert_eq!(
            toolbar
                .vertical()
                .separator_with(div())
                .accessibility
                .orientation,
            Some(AccessibilityOrientation::Horizontal)
        );
    }
}