1use gpui::{
4 Anchor, AnyElement, App, ElementId, Entity, InteractiveElement as _, IntoElement, KeyBinding,
5 Pixels, RenderOnce, SharedString, StyleRefinement, Styled, Window, px,
6};
7
8use crate::{
9 Button, MenuState, Popover, StyledExt as _,
10 actions::{SelectDown, SelectUp},
11};
12
13const TRIGGER_CONTEXT: &str = "DropdownMenuTrigger";
14
15pub(crate) fn init(cx: &mut App) {
16 cx.bind_keys([
17 KeyBinding::new("down", SelectDown, Some(TRIGGER_CONTEXT)),
18 KeyBinding::new("up", SelectUp, Some(TRIGGER_CONTEXT)),
19 ]);
20}
21
22#[derive(IntoElement)]
29pub struct DropdownMenu {
30 id: ElementId,
31 state: Entity<MenuState>,
32 trigger: Button,
33 content: Option<AnyElement>,
34 label: Option<SharedString>,
35 anchor: Anchor,
36 offset: Pixels,
37 style: StyleRefinement,
38}
39
40impl DropdownMenu {
41 pub fn new(id: impl Into<ElementId>, state: &Entity<MenuState>) -> Self {
42 let id = id.into();
43 Self {
44 trigger: Button::new(id.clone()),
45 id,
46 state: state.clone(),
47 content: None,
48 label: None,
49 anchor: Anchor::TopLeft,
50 offset: px(0.),
51 style: StyleRefinement::default(),
52 }
53 }
54
55 pub fn trigger(mut self, build: impl FnOnce(Button) -> Button) -> Self {
56 self.trigger = build(self.trigger);
57 self
58 }
59
60 pub fn content(mut self, content: impl IntoElement) -> Self {
61 self.content = Some(content.into_any_element());
62 self
63 }
64
65 pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
67 self.label = Some(label.into());
68 self
69 }
70
71 pub fn anchor(mut self, anchor: impl Into<Anchor>) -> Self {
72 self.anchor = anchor.into();
73 self
74 }
75
76 pub fn offset(mut self, offset: Pixels) -> Self {
77 self.offset = offset;
78 self
79 }
80}
81
82impl Styled for DropdownMenu {
83 fn style(&mut self) -> &mut StyleRefinement {
84 &mut self.style
85 }
86}
87
88impl RenderOnce for DropdownMenu {
89 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
90 let state = self.state.read(cx);
91 let popover = state.popover.clone();
92 let trigger_down = self.state.clone();
93 let trigger_up = self.state.clone();
94 let content = crate::menu::menu_content(&self.state, self.label, self.content, window, cx);
95
96 Popover::new(self.id, &popover)
97 .content_role(None)
98 .anchor(self.anchor)
99 .offset(self.offset)
100 .trigger(|_| {
101 self.trigger
102 .key_context(TRIGGER_CONTEXT)
103 .on_action(move |_: &SelectDown, window, cx| {
104 trigger_down.update(cx, |state, cx| state.open_at(false, window, cx));
105 })
106 .on_action(move |_: &SelectUp, window, cx| {
107 trigger_up.update(cx, |state, cx| state.open_at(true, window, cx));
108 })
109 })
110 .content(content)
111 .refine_style(&self.style)
112 }
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118 use crate::{
119 Dialog, DialogBackdrop, DialogPopup, DialogState, MenuEvent, MenuItem, MenuOption,
120 PopoverState, Root,
121 };
122 use gpui::{
123 AppContext as _, Context, FocusHandle, KeyDownEvent, KeyUpEvent, Keystroke, Modifiers,
124 MouseButton, ParentElement as _, Render, Subscription, TestAppContext, VisualTestContext,
125 div,
126 };
127
128 struct Harness {
129 menu: Entity<MenuState>,
130 dialog: Entity<DialogState>,
131 outer: Entity<PopoverState>,
132 before: FocusHandle,
133 after: FocusHandle,
134 outside: FocusHandle,
135 nested: bool,
136 commands: Vec<SharedString>,
137 _subscriptions: Vec<Subscription>,
138 }
139
140 impl Render for Harness {
141 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
142 let options = self
143 .menu
144 .read(cx)
145 .items()
146 .iter()
147 .enumerate()
148 .map(|(index, item)| {
149 MenuOption::new(&self.menu, index).h(px(28.)).child(
150 div()
151 .debug_selector(move || format!("command-{index}").into())
152 .size_full()
153 .child(item.label().clone()),
154 )
155 });
156 let controls = div()
157 .debug_selector(|| "outer-panel".into())
158 .w(px(180.))
159 .p(px(8.))
160 .flex()
161 .flex_col()
162 .gap(px(8.))
163 .child(
164 Button::new("before-menu")
165 .debug_selector(|| "before-menu".into())
166 .track_focus(&self.before)
167 .h(px(28.))
168 .child("Before"),
169 )
170 .child(
171 DropdownMenu::new("menu-trigger", &self.menu)
172 .accessibility_label("Commands")
173 .trigger(|button| {
174 button
175 .debug_selector(|| "menu-trigger".into())
176 .accessibility_label("Open commands")
177 .h(px(28.))
178 .child("Commands")
179 })
180 .content(
181 div()
182 .w(px(164.))
183 .p(px(4.))
184 .flex()
185 .flex_col()
186 .children(options),
187 ),
188 )
189 .child(
190 Button::new("after-menu")
191 .track_focus(&self.after)
192 .h(px(28.))
193 .child("After"),
194 );
195 let controls = if self.nested {
196 Popover::new("outer-trigger", &self.outer)
197 .accessibility_label("Parent popover")
198 .trigger(|button| button.h(px(28.)).child("Parent"))
199 .content(controls)
200 .into_any_element()
201 } else {
202 controls.into_any_element()
203 };
204 div()
205 .size_full()
206 .child(
207 Button::new("outside-dialog")
208 .track_focus(&self.outside)
209 .h(px(28.))
210 .child("Outside"),
211 )
212 .child(
213 Dialog::new(&self.dialog)
214 .accessibility_label("Parent dialog")
215 .backdrop(DialogBackdrop::new().size_full())
216 .popup(
217 DialogPopup::new()
218 .w(px(360.))
219 .h(px(380.))
220 .p(px(16.))
221 .child(controls),
222 ),
223 )
224 }
225 }
226
227 fn items() -> Vec<MenuItem> {
228 vec![
229 MenuItem::new("alpha", "Alpha").disabled(true),
230 MenuItem::new("open", "Open"),
231 MenuItem::new("save", "Save").disabled(true),
232 MenuItem::new("share", "Share"),
233 MenuItem::new("search", "Search"),
234 MenuItem::new("quit", "Quit"),
235 MenuItem::new("zip", "Zip").disabled(true),
236 ]
237 }
238
239 fn draw(cx: &mut VisualTestContext) {
240 cx.update(|window, cx| window.draw(cx).clear(cx));
241 }
242
243 fn settle(cx: &mut VisualTestContext) {
244 draw(cx);
245 cx.run_until_parked();
246 draw(cx);
247 }
248
249 fn press(cx: &mut VisualTestContext, key: &str) {
250 let keystroke =
251 cx.update(|_, cx| Keystroke::parse_with_os(key, cx.platform_info().os).unwrap());
252 cx.simulate_event(KeyDownEvent {
253 keystroke: keystroke.clone(),
254 is_held: false,
255 prefer_character_input: false,
256 });
257 cx.simulate_event(KeyUpEvent { keystroke });
258 settle(cx);
259 }
260
261 fn harness(
262 cx: &mut TestAppContext,
263 items: Vec<MenuItem>,
264 ) -> (&mut VisualTestContext, Entity<Harness>) {
265 cx.update(crate::init);
266 let (root, cx) = cx.add_window_view(move |window, cx| {
267 let view = cx.new(|cx| {
268 let before = cx.focus_handle();
269 let menu = cx.new(|cx| MenuState::new(items, cx));
270 let dialog = cx.new(|cx| DialogState::new(cx).with_initial_focus(&before));
271 let outer = cx.new(PopoverState::new);
272 let subscriptions = vec![
273 cx.observe(&menu, |_, _, cx| cx.notify()),
274 cx.observe(&dialog, |_, _, cx| cx.notify()),
275 cx.observe(&outer, |_, _, cx| cx.notify()),
276 cx.subscribe_in(
277 &menu,
278 window,
279 |this: &mut Harness, menu, event, window, cx| {
280 let MenuEvent::Action(value) = event;
281 assert!(!menu.read(cx).is_open(cx));
282 assert_eq!(menu.read(cx).highlighted_index(), None);
283 assert!(menu.read(cx).focus_handle().is_focused(window));
284 this.commands.push(value.clone());
285 cx.notify();
286 },
287 ),
288 ];
289 Harness {
290 menu,
291 dialog,
292 outer,
293 before,
294 after: cx.focus_handle(),
295 outside: cx.focus_handle(),
296 nested: false,
297 commands: Vec::new(),
298 _subscriptions: subscriptions,
299 }
300 });
301 Root::new(view, window, cx)
302 });
303 let view = root
304 .read_with(cx, |root, _| root.view().clone())
305 .downcast::<Harness>()
306 .unwrap();
307 cx.update(|window, cx| {
308 window.activate_window();
309 window.draw(cx).clear(cx);
310 let outside = view.read(cx).outside.clone();
311 outside.focus(window, cx);
312 let dialog = view.read(cx).dialog.clone();
313 dialog.update(cx, |state, cx| state.open(window, cx));
314 });
315 settle(cx);
316 (cx, view)
317 }
318
319 fn open(cx: &mut VisualTestContext, view: &Entity<Harness>, key: &str) {
320 cx.update(|window, cx| {
321 let focus = view.read(cx).menu.read(cx).focus_handle().clone();
322 focus.focus(window, cx);
323 });
324 draw(cx);
325 press(cx, key);
326 cx.update(|window, cx| {
327 let state = view.read(cx).menu.read(cx);
328 assert!(state.is_open(cx));
329 assert!(state.content_focus_handle().is_focused(window));
330 });
331 }
332
333 fn assert_highlight(cx: &mut VisualTestContext, view: &Entity<Harness>, index: usize) {
334 cx.update(|window, cx| {
335 let state = view.read(cx).menu.read(cx);
336 assert_eq!(state.highlighted_index(), Some(index));
337 assert!(state.content_focus_handle().is_focused(window));
338 });
339 }
340
341 #[gpui::test]
342 fn native_trigger_and_repeated_command_activation_close_before_events(cx: &mut TestAppContext) {
343 let (cx, view) = harness(cx, items());
344 open(cx, &view, "enter");
345 assert_highlight(cx, &view, 1);
346 press(cx, "enter");
347 cx.update(|_, cx| {
348 let view = view.read(cx);
349 assert_eq!(view.commands, vec![SharedString::from("open")]);
350 assert!(!view.menu.read(cx).is_open(cx));
351 });
352 open(cx, &view, "space");
353 assert_highlight(cx, &view, 1);
354 press(cx, "space");
355 cx.update(|window, cx| {
356 let view = view.read(cx);
357 assert_eq!(view.commands, vec![SharedString::from("open"); 2]);
358 assert!(!view.menu.read(cx).is_open(cx));
359 assert!(view.menu.read(cx).focus_handle().is_focused(window));
360 assert!(view.dialog.read(cx).is_open());
361 });
362 }
363
364 #[gpui::test]
365 fn long_menu_reveals_keyboard_commands_inside_a_short_window(cx: &mut TestAppContext) {
366 let commands = ('A'..='Z')
367 .map(|letter| MenuItem::new(letter.to_string(), letter.to_string()))
368 .collect();
369 let (cx, view) = harness(cx, commands);
370 cx.simulate_resize(gpui::size(px(400.), px(220.)));
371 open(cx, &view, "up");
372 for (key, index, selector) in [
373 ("end", 25, "command-25"),
374 ("t", 19, "command-19"),
375 ("home", 0, "command-0"),
376 ] {
377 press(cx, key);
378 assert_highlight(cx, &view, index);
379 let row = cx.debug_bounds(selector).unwrap();
380 let window_height = cx.update(|window, _| window.viewport_size().height);
381 assert!(
382 row.top() >= px(0.) && row.bottom() <= window_height,
383 "{key}: {row:?}"
384 );
385 }
386 let first = cx.debug_bounds("command-0").unwrap();
387 cx.simulate_event(gpui::ScrollEvent {
388 position: first.center(),
389 delta: gpui::ScrollDelta::Pixels(gpui::point(px(0.), px(-130.))),
390 ..Default::default()
391 });
392 settle(cx);
393 assert!(cx.debug_bounds("command-0").unwrap().bottom() < px(0.));
394 press(cx, "home");
395 assert!(cx.debug_bounds("command-0").unwrap().top() >= px(0.));
396 press(cx, "enter");
397 cx.update(|window, cx| {
398 let view = view.read(cx);
399 assert_eq!(view.commands, vec![SharedString::from("A")]);
400 assert!(view.menu.read(cx).focus_handle().is_focused(window));
401 });
402 }
403
404 #[gpui::test]
405 fn arrows_edges_and_single_character_typeahead_skip_disabled_commands(cx: &mut TestAppContext) {
406 let (cx, view) = harness(cx, items());
407 open(cx, &view, "up");
408 assert_highlight(cx, &view, 5);
409 press(cx, "down");
410 assert_highlight(cx, &view, 1);
411 press(cx, "down");
412 assert_highlight(cx, &view, 3);
413 press(cx, "home");
414 assert_highlight(cx, &view, 1);
415 press(cx, "up");
416 assert_highlight(cx, &view, 5);
417 press(cx, "home");
418 press(cx, "s");
419 assert_highlight(cx, &view, 3);
420 press(cx, "s");
421 assert_highlight(cx, &view, 4);
422 press(cx, "S");
423 assert_highlight(cx, &view, 3);
424 press(cx, "ctrl-s");
425 press(cx, "alt-s");
426 press(cx, "x");
427 assert_highlight(cx, &view, 3);
428 press(cx, "t->q");
430 assert_highlight(cx, &view, 5);
431 press(cx, "home");
432 press(cx, "end");
433 assert_highlight(cx, &view, 5);
434 cx.update(|_, cx| assert!(view.read(cx).commands.is_empty()));
435 }
436
437 #[gpui::test]
438 fn pointer_hover_and_activation_share_the_cursor_and_disabled_contract(
439 cx: &mut TestAppContext,
440 ) {
441 let (cx, view) = harness(cx, items());
442 open(cx, &view, "down");
443 let disabled = cx.debug_bounds("command-2").unwrap().center();
444 cx.simulate_click(disabled, Modifiers::default());
445 settle(cx);
446 assert_highlight(cx, &view, 1);
447 cx.update(|_, cx| assert!(view.read(cx).commands.is_empty()));
448
449 let target = cx.debug_bounds("command-4").unwrap().center();
450 cx.simulate_mouse_move(target, None, Modifiers::default());
451 settle(cx);
452 assert_highlight(cx, &view, 4);
453 cx.simulate_click(target, Modifiers::default());
454 settle(cx);
455 cx.update(|_, cx| {
456 let view = view.read(cx);
457 assert_eq!(view.commands, vec![SharedString::from("search")]);
458 assert!(!view.menu.read(cx).is_open(cx));
459 });
460
461 let trigger = cx.debug_bounds("menu-trigger").unwrap().center();
462 cx.simulate_click(trigger, Modifiers::default());
463 settle(cx);
464 assert_highlight(cx, &view, 1);
465 cx.simulate_mouse_down(trigger, MouseButton::Left, Modifiers::default());
466 settle(cx);
467 cx.simulate_mouse_up(trigger, MouseButton::Left, Modifiers::default());
468 settle(cx);
469 cx.update(|_, cx| assert!(!view.read(cx).menu.read(cx).is_open(cx)));
470 }
471
472 #[gpui::test]
473 fn outside_dismissal_and_escape_clear_cursor_before_reopening(cx: &mut TestAppContext) {
474 let (cx, view) = harness(cx, items());
475 open(cx, &view, "up");
476 let before = cx.debug_bounds("before-menu").unwrap().center();
477 cx.simulate_click(before, Modifiers::default());
478 settle(cx);
479 cx.update(|window, cx| {
480 let view = view.read(cx);
481 assert!(!view.menu.read(cx).is_open(cx));
482 assert_eq!(view.menu.read(cx).highlighted_index(), None);
483 assert!(view.before.is_focused(window));
484 assert!(view.commands.is_empty());
485 });
486 open(cx, &view, "enter");
487 assert_highlight(cx, &view, 1);
488 press(cx, "end");
489 press(cx, "escape");
490 cx.update(|window, cx| {
491 let view = view.read(cx);
492 assert!(!view.menu.read(cx).is_open(cx));
493 assert_eq!(view.menu.read(cx).highlighted_index(), None);
494 assert!(view.menu.read(cx).focus_handle().is_focused(window));
495 assert!(view.dialog.read(cx).is_open());
496 });
497 open(cx, &view, "space");
498 assert_highlight(cx, &view, 1);
499 }
500
501 #[gpui::test]
502 fn tab_and_shift_tab_leave_command_content(cx: &mut TestAppContext) {
503 let (cx, view) = harness(cx, items());
504 open(cx, &view, "down");
505 press(cx, "tab");
506 cx.update(|window, cx| {
507 let view = view.read(cx);
508 assert!(!view.menu.read(cx).is_open(cx));
509 assert!(view.after.is_focused(window));
510 });
511 open(cx, &view, "down");
512 press(cx, "shift-tab");
513 cx.update(|window, cx| {
514 let view = view.read(cx);
515 assert!(!view.menu.read(cx).is_open(cx));
516 assert!(view.menu.read(cx).focus_handle().is_focused(window));
517 assert!(view.commands.is_empty());
518 });
519 }
520
521 #[gpui::test]
522 fn nested_menu_pointer_and_escape_preserve_the_parent_popover_and_dialog(
523 cx: &mut TestAppContext,
524 ) {
525 let (cx, view) = harness(cx, items());
526 view.update(cx, |view, cx| {
527 view.nested = true;
528 cx.notify();
529 });
530 settle(cx);
531 cx.update(|window, cx| {
532 let outer = view.read(cx).outer.clone();
533 outer.update(cx, |state, cx| state.open(window, cx));
534 });
535 settle(cx);
536 open(cx, &view, "down");
537 let target = cx.debug_bounds("command-5").unwrap().center();
538 assert!(!cx.debug_bounds("outer-panel").unwrap().contains(&target));
539 cx.simulate_click(target, Modifiers::default());
540 settle(cx);
541 cx.update(|_, cx| {
542 let view = view.read(cx);
543 assert_eq!(view.commands, vec![SharedString::from("quit")]);
544 assert!(!view.menu.read(cx).is_open(cx));
545 assert!(view.outer.read(cx).is_open());
546 assert!(view.dialog.read(cx).is_open());
547 });
548 open(cx, &view, "down");
549 press(cx, "escape");
550 cx.update(|_, cx| {
551 assert!(!view.read(cx).menu.read(cx).is_open(cx));
552 assert!(view.read(cx).outer.read(cx).is_open());
553 });
554 press(cx, "escape");
555 cx.update(|_, cx| {
556 assert!(!view.read(cx).outer.read(cx).is_open());
557 assert!(view.read(cx).dialog.read(cx).is_open());
558 });
559 press(cx, "escape");
560 cx.update(|window, cx| {
561 assert!(!view.read(cx).dialog.read(cx).is_open());
562 assert!(view.read(cx).outside.is_focused(window));
563 });
564 }
565
566 #[gpui::test]
567 fn disabling_an_open_menu_blocks_activation_but_preserves_tab_exit(cx: &mut TestAppContext) {
568 let (cx, view) = harness(cx, items());
569 open(cx, &view, "down");
570 cx.update(|window, cx| {
571 let menu = view.read(cx).menu.clone();
572 menu.update(cx, |state, cx| state.set_disabled(true, window, cx));
573 });
574 settle(cx);
575 press(cx, "enter");
576 press(cx, "space");
577 press(cx, "down");
578 press(cx, "up");
579 let trigger = cx.debug_bounds("menu-trigger").unwrap().center();
580 cx.simulate_click(trigger, Modifiers::default());
581 settle(cx);
582 cx.update(|window, cx| {
583 let menu = view.read(cx).menu.clone();
584 menu.update(cx, |state, cx| state.open(window, cx));
585 assert!(!menu.read(cx).is_open(cx));
586 assert!(menu.read(cx).is_disabled(cx));
587 assert_eq!(menu.read(cx).highlighted_index(), None);
588 assert!(menu.read(cx).focus_handle().is_focused(window));
589 assert!(view.read(cx).commands.is_empty());
590 });
591 press(cx, "tab");
592 cx.update(|window, cx| assert!(view.read(cx).after.is_focused(window)));
593 }
594
595 #[gpui::test]
596 fn empty_disabled_and_single_command_collections_have_defined_navigation(
597 cx: &mut TestAppContext,
598 ) {
599 for commands in [
600 Vec::new(),
601 vec![MenuItem::new("disabled", "Disabled").disabled(true)],
602 ] {
603 let (cx, view) = harness(cx, commands);
604 open(cx, &view, "enter");
605 for key in ["down", "up", "home", "end", "d", "enter", "space"] {
606 press(cx, key);
607 }
608 cx.update(|_, cx| {
609 assert!(view.read(cx).menu.read(cx).is_open(cx));
610 assert_eq!(view.read(cx).menu.read(cx).highlighted_index(), None);
611 assert!(view.read(cx).commands.is_empty());
612 });
613 press(cx, "tab");
614 cx.update(|window, cx| {
615 assert!(!view.read(cx).menu.read(cx).is_open(cx));
616 assert!(view.read(cx).after.is_focused(window));
617 });
618 }
619
620 let (cx, view) = harness(cx, vec![MenuItem::new("only", "Only")]);
621 open(cx, &view, "up");
622 for key in ["down", "up", "home", "end", "o"] {
623 press(cx, key);
624 assert_highlight(cx, &view, 0);
625 }
626 press(cx, "enter");
627 cx.update(|_, cx| assert_eq!(view.read(cx).commands, vec![SharedString::from("only")]));
628 }
629
630 #[cfg(feature = "test-support")]
631 #[gpui::test]
632 fn accessibility_exposes_expanded_button_and_menu_commands_without_selection(
633 cx: &mut TestAppContext,
634 ) {
635 use gpui::{Element as _, Role, accesskit, prelude::FluentBuilder as _};
636
637 let (cx, view) = harness(cx, items());
638 cx.update(|window, _| {
639 let trigger = crate::test_support::find(window, &[], &"menu-trigger".into()).unwrap();
640 assert_eq!(trigger.role(), Some(Role::Button));
641 assert_eq!(trigger.label(), Some("Open commands"));
642 assert_eq!(trigger.expanded(), Some(false));
643 });
644 open(cx, &view, "down");
645 cx.update(|window, cx| {
646 let menu = view.read(cx).menu.clone();
647 let trigger = crate::test_support::find(window, &[], &"menu-trigger".into()).unwrap();
648 assert_eq!(trigger.expanded(), Some(true));
649 let scope =
650 crate::test_support::find(window, &[], &("menu", menu.entity_id()).into()).unwrap();
651 assert_eq!(scope.role(), Some(Role::Menu));
652 assert_eq!(scope.label(), Some("Commands"));
653 assert_eq!(scope.focused(), Some(true));
654 for (index, item) in menu.read(cx).items().iter().enumerate() {
655 let option =
656 crate::test_support::find(window, &[], &("menu-option", index).into()).unwrap();
657 assert_eq!(option.role(), Some(Role::MenuItem));
658 assert_eq!(option.label(), Some(item.label().as_ref()));
659 assert_eq!(option.selected(), None);
660 assert_eq!(option.checked(), None);
661 assert_eq!(option.focused(), None);
662 assert_eq!(option.disabled(), item.is_disabled().then_some(true));
663 }
664 for (index, disabled) in [(0, true), (1, false)] {
665 for checked in [None, Some(false), Some(true)] {
666 let element = MenuOption::new(&menu, index)
667 .when_some(checked, |option, checked| option.checked(checked))
668 .render(window, cx)
669 .into_element();
670 let role = if checked.is_some() {
671 Role::MenuItemCheckBox
672 } else {
673 Role::MenuItem
674 };
675 assert_eq!(element.a11y_role(), Some(role));
676 let mut node = accesskit::Node::new(role);
677 element.write_a11y_info(&mut node);
678 assert_eq!(
679 node.label(),
680 Some(menu.read(cx).items()[index].label().as_ref())
681 );
682 assert_eq!(
683 node.toggled(),
684 checked.map(|checked| if checked {
685 accesskit::Toggled::True
686 } else {
687 accesskit::Toggled::False
688 })
689 );
690 assert_eq!(node.is_disabled(), disabled);
691 assert_eq!(node.is_selected(), None);
692 assert!(!node.supports_action(accesskit::Action::Focus));
693 assert_eq!(node.supports_action(accesskit::Action::Click), !disabled);
694 }
695 for (option, role, toggled) in [
696 (
697 MenuOption::new(&menu, index)
698 .check_state(crate::CheckboxState::Indeterminate),
699 Role::MenuItemCheckBox,
700 accesskit::Toggled::Mixed,
701 ),
702 (
703 MenuOption::new(&menu, index).radio_checked(false),
704 Role::MenuItemRadio,
705 accesskit::Toggled::False,
706 ),
707 (
708 MenuOption::new(&menu, index).radio_checked(true),
709 Role::MenuItemRadio,
710 accesskit::Toggled::True,
711 ),
712 ] {
713 let element = option.render(window, cx).into_element();
714 assert_eq!(element.a11y_role(), Some(role));
715 let mut node = accesskit::Node::new(role);
716 element.write_a11y_info(&mut node);
717 assert_eq!(node.toggled(), Some(toggled));
718 assert_eq!(node.is_disabled(), disabled);
719 assert_eq!(node.is_selected(), None);
720 assert!(!node.supports_action(accesskit::Action::Focus));
721 }
722 }
723 let dialogs = crate::test_support::snapshots(window)
724 .into_iter()
725 .filter(|node| node.role() == Some(Role::Dialog))
726 .count();
727 assert_eq!(
728 dialogs, 1,
729 "The menu wrapper must not advertise another Dialog"
730 );
731 });
732 }
733}