use std::{cell::RefCell, rc::Rc}; use gpui::{ Context, DefiniteLength, IntoElement, Modifiers, MouseButton, ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window, WindowHandle, div, point, px, }; use gpui_native_core::menu::{Menu, MenuItem}; use gpui_native_theme::Theme; struct Harness { open: bool, min_width: Option, changes: Rc>>, activated: Rc>>, } impl Render for Harness { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let changes = self.changes.clone(); let view = cx.entity(); let activated = self.activated.clone(); let mut menu = Menu::new("test-menu") .open(self.open) .on_change(move |next, _, cx| { changes.borrow_mut().push(next); view.update(cx, |harness, cx| { harness.open = next; cx.notify(); }); }) .trigger(|_, _| { div() .w(px(100.)) .h(px(32.)) .bg(gpui::black()) .into_any_element() }) .items(move |_, _| { vec![ cut_item(activated.clone()), MenuItem::separator(), copy_item(activated.clone()), MenuItem::new("Unavailable").disabled(true).handler({ let activated = activated.clone(); move |_, _| record(&activated, "unavailable") }), delete_item(activated.clone()), ] }); if let Some(width) = self.min_width { menu = menu.min_width(width); } div().size_full().child(menu) } } fn cut_item(activated: Rc>>) -> MenuItem { MenuItem::new("Cut") .shortcut("cmd-x") .handler(move |_, _| record(&activated, "cut")) } fn copy_item(activated: Rc>>) -> MenuItem { MenuItem::new("Copy").handler(move |_, _| record(&activated, "copy")) } fn delete_item(activated: Rc>>) -> MenuItem { MenuItem::new("Delete") .destructive(true) .handler(move |_, _| record(&activated, "delete")) } fn action_item( label: &'static str, key: &'static str, activated: Rc>>, ) -> MenuItem { MenuItem::new(label).handler(move |_, _| record(&activated, key)) } fn submenu_item(activated: Rc>>) -> MenuItem { MenuItem::new("Export").submenu(vec![ action_item("PNG", "png", activated.clone()), action_item("PDF", "pdf", activated.clone()), ]) } /// A menu with an Export row whose flyout holds PNG and PDF actions. struct SubmenuHarness { open: bool, changes: Rc>>, activated: Rc>>, } impl Render for SubmenuHarness { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let changes = self.changes.clone(); let view = cx.entity(); let activated = self.activated.clone(); div().size_full().child( Menu::new("test-submenu") .open(self.open) .on_change(move |next, _, cx| { changes.borrow_mut().push(next); view.update(cx, |harness, cx| { harness.open = next; cx.notify(); }); }) .trigger(|_, _| { div() .w(px(100.)) .h(px(32.)) .bg(gpui::black()) .into_any_element() }) .items(move |_, _| { vec![ action_item("Cut", "cut", activated.clone()), submenu_item(activated.clone()), action_item("Delete", "delete", activated.clone()), ] }), ) } } fn record(sink: &Rc>>, what: &'static str) { sink.borrow_mut().push(what); } type Changes = Rc>>; type Activated = Rc>>; fn menu_window( open: bool, ) -> ( TestAppContext, VisualTestContext, WindowHandle, Changes, Activated, ) { menu_window_with(open, None) } fn menu_window_with( open: bool, min_width: Option, ) -> ( TestAppContext, VisualTestContext, WindowHandle, Changes, Activated, ) { let mut cx = TestAppContext::single(); cx.update(Theme::init); let changes = Rc::new(RefCell::new(Vec::new())); let activated = Rc::new(RefCell::new(Vec::new())); let harness_changes = changes.clone(); let harness_activated = activated.clone(); let window = cx.add_window(move |_, _| Harness { open, min_width, changes: harness_changes, activated: harness_activated, }); let mut visual = VisualTestContext::from_window(window.into(), &cx); visual.update(|window, cx| { window.draw(cx).clear(cx); }); (cx, visual, window, changes, activated) } fn submenu_window(open: bool) -> (TestAppContext, VisualTestContext, Changes, Activated) { let mut cx = TestAppContext::single(); cx.update(Theme::init); let changes = Rc::new(RefCell::new(Vec::new())); let activated = Rc::new(RefCell::new(Vec::new())); let harness_changes = changes.clone(); let harness_activated = activated.clone(); let window = cx.add_window(move |_, _| SubmenuHarness { open, changes: harness_changes, activated: harness_activated, }); let mut visual = VisualTestContext::from_window(window.into(), &cx); visual.update(|window, cx| { window.draw(cx).clear(cx); }); (cx, visual, changes, activated) } fn draw(visual: &mut VisualTestContext) { visual.update(|window, cx| { window.draw(cx).clear(cx); }); } fn click(visual: &mut VisualTestContext, position: gpui::Point) { visual.simulate_mouse_down(position, MouseButton::Left, Modifiers::default()); visual.simulate_mouse_up(position, MouseButton::Left, Modifiers::default()); } fn hover(visual: &mut VisualTestContext, position: gpui::Point) { visual.simulate_mouse_move(position, None, Modifiers::default()); } fn center(bounds: gpui::Bounds) -> gpui::Point { point( bounds.origin.x + bounds.size.width / 2., bounds.origin.y + bounds.size.height / 2., ) } /// Every action row renders at the platform's stated menu row height, /// whatever its content — the metric is the whole row, not a floor under /// padding. Item 1 of the harness menu is a separator, so it is skipped. #[test] fn rows_render_at_the_stated_menu_row_height() { let (cx, mut visual, _window, _changes, _activated) = menu_window(true); let stated = cx.update(|cx| Theme::global(cx).sizes.medium.menu_item_height); for index in [0, 2, 3, 4] { let selector: &'static str = Box::leak(format!("menu-item-{index}").into_boxed_str()); let row = visual .debug_bounds(selector) .unwrap_or_else(|| panic!("row {index} should have been laid out")); // Painted bounds are snapped to device pixels; allow half a pixel. assert!( (row.size.height - stated).abs() <= px(0.5), "row {index} is {:?} tall, stated menu row height is {stated:?}", row.size.height ); } } #[test] fn arrow_navigation_skips_separators_and_disabled_items() { let (_cx, mut visual, _window, changes, activated) = menu_window(true); // Nothing is highlighted when the menu opens; down arms the first // selectable item, the next down crosses the separator to Copy, and // enter activates it, closing the menu. visual.simulate_keystrokes("down down enter"); draw(&mut visual); assert_eq!(*activated.borrow(), vec!["copy"]); assert_eq!(*changes.borrow(), vec![false]); // Reopen: pointer opens highlight nothing again, and end jumps straight // to the last selectable item, over the separator and the disabled row. click(&mut visual, point(px(10.), px(16.))); draw(&mut visual); visual.simulate_keystrokes("end"); visual.simulate_keystrokes("enter"); assert_eq!(*activated.borrow(), vec!["copy", "delete"]); assert_eq!(*changes.borrow(), vec![false, true, false]); } #[test] fn enter_without_navigation_activates_nothing() { let (_cx, mut visual, _window, changes, activated) = menu_window(true); // A pointer-opened menu arms no row, so enter cannot fire one and the // menu stays open. visual.simulate_keystrokes("enter"); draw(&mut visual); assert!(activated.borrow().is_empty()); assert!(changes.borrow().is_empty(), "the menu stays open"); } #[test] fn escape_dismisses_without_activating() { let (_cx, mut visual, _window, changes, activated) = menu_window(true); visual.simulate_keystrokes("escape"); assert_eq!(*changes.borrow(), vec![false]); assert!(activated.borrow().is_empty()); // A closed menu is inert. draw(&mut visual); visual.simulate_keystrokes("escape"); assert_eq!(*changes.borrow(), vec![false]); } #[test] fn clicking_outside_dismisses_and_swallows_the_click() { let (_cx, mut visual, _window, changes, activated) = menu_window(true); click(&mut visual, point(px(600.), px(600.))); assert_eq!(*changes.borrow(), vec![false]); assert!(activated.borrow().is_empty()); draw(&mut visual); click(&mut visual, point(px(600.), px(600.))); assert_eq!(*changes.borrow(), vec![false]); } #[test] fn clicking_an_item_activates_and_closes() { let (_cx, mut visual, _window, changes, activated) = menu_window(true); // The first row hangs just below the trigger's panel padding. click(&mut visual, point(px(60.), px(50.))); assert_eq!(*activated.borrow(), vec!["cut"]); assert_eq!(*changes.borrow(), vec![false]); } #[test] fn right_arrow_opens_the_submenu_and_enter_within_activates_through() { let (_cx, mut visual, changes, activated) = submenu_window(true); // down arms Cut, the next down reaches Export, right opens its flyout // with the first row armed. visual.simulate_keystrokes("down down right"); draw(&mut visual); assert!(visual.debug_bounds("submenu-item-0").is_some()); assert!(changes.borrow().is_empty(), "the menu stays open"); assert!(activated.borrow().is_empty()); // Enter inside the flyout fires PNG and closes the whole menu. visual.simulate_keystrokes("enter"); draw(&mut visual); assert_eq!(*activated.borrow(), vec!["png"]); assert_eq!(*changes.borrow(), vec![false]); assert!(visual.debug_bounds("submenu-item-0").is_none()); } #[test] fn enter_on_a_submenu_row_opens_it_and_navigates_within() { let (_cx, mut visual, changes, activated) = submenu_window(true); visual.simulate_keystrokes("down down enter"); draw(&mut visual); assert!(visual.debug_bounds("submenu-item-0").is_some()); assert!( changes.borrow().is_empty(), "enter opens, it does not close" ); // The flyout armed PNG; down reaches PDF and enter fires it. visual.simulate_keystrokes("down enter"); draw(&mut visual); assert_eq!(*activated.borrow(), vec!["pdf"]); assert_eq!(*changes.borrow(), vec![false]); } #[test] fn left_closes_the_submenu_and_returns_to_the_menu() { let (_cx, mut visual, changes, activated) = submenu_window(true); visual.simulate_keystrokes("down down right"); draw(&mut visual); visual.simulate_keystrokes("left"); draw(&mut visual); assert!(visual.debug_bounds("submenu-item-0").is_none()); assert!(changes.borrow().is_empty(), "the menu stays open"); assert!(activated.borrow().is_empty()); // Navigation resumes in the parent: down reaches Delete, enter fires it. visual.simulate_keystrokes("down enter"); draw(&mut visual); assert_eq!(*activated.borrow(), vec!["delete"]); assert_eq!(*changes.borrow(), vec![false]); } #[test] fn escape_closes_the_submenu_then_the_menu() { let (_cx, mut visual, changes, activated) = submenu_window(true); visual.simulate_keystrokes("down down right"); draw(&mut visual); visual.simulate_keystrokes("escape"); draw(&mut visual); assert!(visual.debug_bounds("submenu-item-0").is_none()); assert!( changes.borrow().is_empty(), "the first escape only closes the flyout" ); visual.simulate_keystrokes("escape"); draw(&mut visual); assert_eq!(*changes.borrow(), vec![false]); assert!(activated.borrow().is_empty()); } #[test] fn clicking_a_submenu_row_opens_the_flyout_and_a_flyout_action_closes_the_menu() { let (_cx, mut visual, changes, activated) = submenu_window(true); let row = visual .debug_bounds("menu-item-1") .unwrap_or_else(|| panic!("the Export row should have been laid out")); click( &mut visual, point( row.origin.x + row.size.width / 2., row.origin.y + row.size.height / 2., ), ); draw(&mut visual); assert!(visual.debug_bounds("submenu-item-0").is_some()); assert!(changes.borrow().is_empty()); let png = visual .debug_bounds("submenu-item-0") .unwrap_or_else(|| panic!("the PNG row should have been laid out")); click( &mut visual, point( png.origin.x + png.size.width / 2., png.origin.y + png.size.height / 2., ), ); draw(&mut visual); assert_eq!(*activated.borrow(), vec!["png"]); assert_eq!(*changes.borrow(), vec![false]); } #[test] fn a_click_inside_the_flyout_padding_does_not_dismiss_the_menu() { let (_cx, mut visual, changes, _activated) = submenu_window(true); let row = visual.debug_bounds("menu-item-1").unwrap(); click( &mut visual, point( row.origin.x + row.size.width / 2., row.origin.y + row.size.height / 2., ), ); draw(&mut visual); // The flyout's own padding belongs to the flyout, not to the dismissal // listener of the panel behind it. let panel = visual.debug_bounds("submenu-panel").unwrap(); click( &mut visual, point( panel.origin.x + px(4.), panel.size.height + panel.origin.y - px(2.), ), ); draw(&mut visual); assert!(changes.borrow().is_empty(), "the flyout stays open"); assert!(visual.debug_bounds("submenu-item-0").is_some()); // A click outside everything still closes the whole menu. click(&mut visual, point(px(600.), px(600.))); assert_eq!(*changes.borrow(), vec![false]); } #[test] fn hovering_a_submenu_row_opens_its_flyout_and_an_action_row_closes_it() { let (_cx, mut visual, changes, _activated) = submenu_window(true); let export = visual.debug_bounds("menu-item-1").unwrap(); hover(&mut visual, center(export)); draw(&mut visual); assert!( visual.debug_bounds("submenu-item-0").is_some(), "hover opens the flyout" ); assert!(changes.borrow().is_empty(), "the menu stays open"); let cut = visual.debug_bounds("menu-item-0").unwrap(); hover(&mut visual, center(cut)); draw(&mut visual); assert!( visual.debug_bounds("submenu-item-0").is_none(), "hovering an action row closes the flyout" ); assert!(changes.borrow().is_empty(), "the menu stays open"); } #[test] fn the_flyout_overlaps_the_parent_from_the_rows_right_edge() { let (_cx, mut visual, _changes, _activated) = submenu_window(true); let export = visual.debug_bounds("menu-item-1").unwrap(); hover(&mut visual, center(export)); draw(&mut visual); let parent = visual.debug_bounds("menu-panel").unwrap(); let flyout = visual.debug_bounds("submenu-panel").unwrap(); let parent_right = parent.origin.x + parent.size.width; let row_right = export.origin.x + export.size.width; // macOS style: the flyout covers the parent's trailing padding, starting // at the row's right edge. assert!( flyout.origin.x < parent_right - px(1.), "the flyout starts at {:?}, inside the parent edge at {:?}", flyout.origin.x, parent_right ); assert!( flyout.origin.x >= row_right - px(1.), "the flyout starts at {:?}, at or after the row's right edge {:?}", flyout.origin.x, row_right ); } #[test] fn a_custom_min_width_overrides_the_default_panel_width() { let (_cx, mut visual, _window, _changes, _activated) = menu_window(true); let default_width = visual.debug_bounds("menu-panel").unwrap().size.width; let (_cx, mut visual, _window, _changes, _activated) = menu_window_with(true, Some(px(320.).into())); let width = visual.debug_bounds("menu-panel").unwrap().size.width; // Painted bounds are snapped to device pixels; allow half a pixel. assert!( (width - px(320.)).abs() <= px(0.5), "the panel is {width:?} wide, expected the 320px minimum" ); assert!( width > default_width, "the default panel is {:?} wide and should be narrower", default_width ); }