1use gpui::{
4 AnyElement, App, Div, ElementId, InteractiveElement as _, IntoElement, ParentElement,
5 RenderOnce, Role, SharedString, Stateful, StatefulInteractiveElement as _, StyleRefinement,
6 Styled, Window, div,
7};
8
9use crate::TestSupportExt as _;
10
11#[derive(IntoElement)]
18pub struct Breadcrumb {
19 base: crate::ObservedElement<Stateful<Div>>,
20 label: SharedString,
21}
22
23impl Breadcrumb {
24 pub fn new(id: impl Into<ElementId>) -> Self {
25 Self {
26 base: div().id(id).test_support(),
27 label: "Breadcrumb".into(),
28 }
29 }
30
31 pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
33 self.label = label.into();
34 self
35 }
36}
37
38impl Styled for Breadcrumb {
39 fn style(&mut self) -> &mut StyleRefinement {
40 self.base.style()
41 }
42}
43
44impl ParentElement for Breadcrumb {
45 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
46 self.base.extend(elements);
47 }
48}
49
50impl RenderOnce for Breadcrumb {
51 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
52 self.base.role(Role::Navigation).aria_label(self.label)
53 }
54}
55
56#[derive(IntoElement)]
60pub struct BreadcrumbList {
61 base: crate::ObservedElement<Stateful<Div>>,
62}
63
64impl BreadcrumbList {
65 pub fn new(id: impl Into<ElementId>) -> Self {
66 Self {
67 base: div().id(id).test_support(),
68 }
69 }
70}
71
72impl Styled for BreadcrumbList {
73 fn style(&mut self) -> &mut StyleRefinement {
74 self.base.style()
75 }
76}
77
78impl ParentElement for BreadcrumbList {
79 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
80 self.base.extend(elements);
81 }
82}
83
84impl RenderOnce for BreadcrumbList {
85 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
86 self.base.role(Role::List)
87 }
88}
89
90#[derive(IntoElement)]
96pub struct BreadcrumbItem {
97 base: crate::ObservedElement<Stateful<Div>>,
98 current: bool,
99}
100
101impl BreadcrumbItem {
102 pub fn new(id: impl Into<ElementId>) -> Self {
103 Self {
104 base: div().id(id).test_support(),
105 current: false,
106 }
107 }
108
109 pub fn current(mut self, current: bool) -> Self {
114 self.current = current;
115 self
116 }
117}
118
119impl Styled for BreadcrumbItem {
120 fn style(&mut self) -> &mut StyleRefinement {
121 self.base.style()
122 }
123}
124
125impl ParentElement for BreadcrumbItem {
126 fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
127 self.base.extend(elements);
128 }
129}
130
131impl RenderOnce for BreadcrumbItem {
132 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
133 crate::a11y_current::current_page(self.base.role(Role::ListItem), self.current)
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use gpui::{Element as _, TestAppContext, accesskit};
140
141 use super::*;
142
143 fn node_of(element: impl IntoElement, role: Role) -> accesskit::Node {
144 let element = element.into_element();
145 assert_eq!(element.a11y_role(), Some(role));
146 let mut node = accesskit::Node::new(role);
147 element.write_a11y_info(&mut node);
148 assert!(!node.supports_action(accesskit::Action::Focus));
149 assert!(!node.supports_action(accesskit::Action::Click));
150 node
151 }
152
153 #[gpui::test]
154 fn native_path_names_current_page_without_structural_focus(cx: &mut TestAppContext) {
155 let window = cx.add_empty_window();
156 window.update(|window, cx| {
157 let root = node_of(
158 Breadcrumb::new("path")
159 .accessibility_label("Session path")
160 .render(window, cx),
161 Role::Navigation,
162 );
163 assert_eq!(root.label(), Some("Session path"));
164 assert_eq!(root.aria_current(), None);
165
166 let list = node_of(
167 BreadcrumbList::new("path-list").render(window, cx),
168 Role::List,
169 );
170 assert_eq!(list.aria_current(), None);
171
172 for current in [false, true] {
173 let item = node_of(
174 BreadcrumbItem::new("path-item")
175 .current(current)
176 .child("Keyboard guide")
177 .render(window, cx),
178 Role::ListItem,
179 );
180 assert_eq!(
181 item.aria_current(),
182 current.then_some(accesskit::AriaCurrent::Page)
183 );
184 }
185 });
186 }
187}