192 lines
6.2 KiB
Rust
192 lines
6.2 KiB
Rust
use std::{cell::RefCell, rc::Rc};
|
|
|
|
use gpui::{
|
|
Context, 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,
|
|
changes: Rc<RefCell<Vec<bool>>>,
|
|
activated: Rc<RefCell<Vec<&'static str>>>,
|
|
}
|
|
|
|
impl Render for Harness {
|
|
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let changes = self.changes.clone();
|
|
let view = cx.entity();
|
|
let activated = self.activated.clone();
|
|
div().size_full().child(
|
|
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()),
|
|
]
|
|
}),
|
|
)
|
|
}
|
|
}
|
|
|
|
fn cut_item(activated: Rc<RefCell<Vec<&'static str>>>) -> MenuItem {
|
|
MenuItem::new("Cut")
|
|
.shortcut("cmd-x")
|
|
.handler(move |_, _| record(&activated, "cut"))
|
|
}
|
|
|
|
fn copy_item(activated: Rc<RefCell<Vec<&'static str>>>) -> MenuItem {
|
|
MenuItem::new("Copy").handler(move |_, _| record(&activated, "copy"))
|
|
}
|
|
|
|
fn delete_item(activated: Rc<RefCell<Vec<&'static str>>>) -> MenuItem {
|
|
MenuItem::new("Delete")
|
|
.destructive(true)
|
|
.handler(move |_, _| record(&activated, "delete"))
|
|
}
|
|
|
|
fn record(sink: &Rc<RefCell<Vec<&'static str>>>, what: &'static str) {
|
|
sink.borrow_mut().push(what);
|
|
}
|
|
|
|
type Changes = Rc<RefCell<Vec<bool>>>;
|
|
type Activated = Rc<RefCell<Vec<&'static str>>>;
|
|
|
|
fn menu_window(
|
|
open: bool,
|
|
) -> (
|
|
TestAppContext,
|
|
VisualTestContext,
|
|
WindowHandle<Harness>,
|
|
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,
|
|
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 draw(visual: &mut VisualTestContext) {
|
|
visual.update(|window, cx| {
|
|
window.draw(cx).clear(cx);
|
|
});
|
|
}
|
|
|
|
fn click(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
|
visual.simulate_mouse_down(position, MouseButton::Left, Modifiers::default());
|
|
visual.simulate_mouse_up(position, MouseButton::Left, Modifiers::default());
|
|
}
|
|
|
|
#[test]
|
|
fn arrow_navigation_skips_separators_and_disabled_items() {
|
|
let (_cx, mut visual, _window, changes, activated) = menu_window(true);
|
|
|
|
// Selection starts on the first item; down moves to the next
|
|
// selectable item (Copy, past the separator), and enter activates it,
|
|
// closing the menu.
|
|
visual.simulate_keystrokes("down");
|
|
visual.simulate_keystrokes("enter");
|
|
draw(&mut visual);
|
|
assert_eq!(*activated.borrow(), vec!["copy"]);
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
|
|
// Reopen: the selection starts over on the first item, 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 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_the_trigger_toggles() {
|
|
let (_cx, mut visual, _window, changes, _activated) = menu_window(false);
|
|
|
|
click(&mut visual, point(px(10.), px(16.)));
|
|
assert_eq!(*changes.borrow(), vec![true]);
|
|
}
|
|
|
|
#[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 enter_activates_the_selected_item_and_closes() {
|
|
let (_cx, mut visual, _window, changes, activated) = menu_window(true);
|
|
|
|
visual.simulate_keystrokes("enter");
|
|
assert_eq!(*activated.borrow(), vec!["cut"]);
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
}
|