215 lines
7.2 KiB
Rust
215 lines
7.2 KiB
Rust
use std::{cell::RefCell, rc::Rc};
|
|
|
|
use gpui::{
|
|
Context, InteractiveElement as _, IntoElement, Modifiers, MouseButton, ParentElement as _,
|
|
Render, StatefulInteractiveElement as _, Styled as _, TestAppContext, VisualTestContext,
|
|
Window, WindowHandle, div, point, px, rgba,
|
|
};
|
|
use gpui_native_core::popover::Popover;
|
|
use gpui_native_theme::Theme;
|
|
|
|
struct Harness {
|
|
open: bool,
|
|
changes: Rc<RefCell<Vec<bool>>>,
|
|
target_clicks: Rc<RefCell<usize>>,
|
|
}
|
|
|
|
impl Render for Harness {
|
|
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let changes = self.changes.clone();
|
|
let target_clicks = self.target_clicks.clone();
|
|
let view = cx.entity();
|
|
div()
|
|
.size_full()
|
|
.child(
|
|
div().absolute().left(px(400.)).top(px(300.)).child(
|
|
div()
|
|
.id("popover-target")
|
|
.size(px(40.))
|
|
.bg(rgba(0x000000ff))
|
|
.on_click(move |_, _, cx| {
|
|
*target_clicks.borrow_mut() += 1;
|
|
cx.stop_propagation();
|
|
}),
|
|
),
|
|
)
|
|
.child(
|
|
Popover::new("test-popover")
|
|
.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()
|
|
.debug_selector(|| "popover-trigger".to_string())
|
|
.w(px(100.))
|
|
.h(px(32.))
|
|
.bg(rgba(0x000000ff))
|
|
.into_any_element()
|
|
})
|
|
.content(|_, _| {
|
|
div()
|
|
.w(px(200.))
|
|
.h(px(80.))
|
|
.child("Popover content")
|
|
.into_any_element()
|
|
}),
|
|
)
|
|
}
|
|
}
|
|
|
|
/// A window whose popover trigger sits in the top-left corner: the panel
|
|
/// hangs below it around `(0..200, 36..116)`, so clicks at `(10, 16)` land
|
|
/// on the trigger, `(100, 70)` on the panel, and anything past `(300, 300)`
|
|
/// is outside both.
|
|
type PopoverWindow = (
|
|
TestAppContext,
|
|
VisualTestContext,
|
|
WindowHandle<Harness>,
|
|
Rc<RefCell<Vec<bool>>>,
|
|
Rc<RefCell<usize>>,
|
|
);
|
|
|
|
fn popover_window(open: bool) -> PopoverWindow {
|
|
let mut cx = TestAppContext::single();
|
|
cx.update(Theme::init);
|
|
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
let harness_changes = changes.clone();
|
|
let target_clicks = Rc::new(RefCell::new(0));
|
|
let harness_clicks = target_clicks.clone();
|
|
let window = cx.add_window(move |_, _| Harness {
|
|
open,
|
|
changes: harness_changes,
|
|
target_clicks: harness_clicks,
|
|
});
|
|
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
visual.update(|window, cx| {
|
|
window.draw(cx).clear(cx);
|
|
});
|
|
(cx, visual, window, changes, target_clicks)
|
|
}
|
|
|
|
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 clicking_outside_dismisses_and_reports_closed() {
|
|
let (_cx, mut visual, _window, changes, _clicks) = popover_window(true);
|
|
|
|
click(&mut visual, point(px(600.), px(600.)));
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
|
|
// The dismissal swallowed the click and the owner closed the popover:
|
|
// the redraw renders without a panel, and nothing further fires.
|
|
draw(&mut visual);
|
|
click(&mut visual, point(px(600.), px(600.)));
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
}
|
|
|
|
#[test]
|
|
fn clicking_the_trigger_toggles_and_clicking_the_panel_does_nothing() {
|
|
let (_cx, mut visual, _window, changes, _clicks) = popover_window(true);
|
|
|
|
// On the open panel's trigger: toggles closed.
|
|
click(&mut visual, point(px(10.), px(16.)));
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
|
|
// On the panel itself: nothing.
|
|
click(&mut visual, point(px(100.), px(70.)));
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
}
|
|
|
|
#[test]
|
|
fn clicking_the_trigger_opens_a_closed_popover() {
|
|
let (_cx, mut visual, _window, changes, _clicks) = popover_window(false);
|
|
|
|
click(&mut visual, point(px(10.), px(16.)));
|
|
assert_eq!(*changes.borrow(), vec![true]);
|
|
}
|
|
|
|
#[test]
|
|
fn escape_dismisses_while_the_panel_holds_focus() {
|
|
let (_cx, mut visual, _window, changes, _clicks) = popover_window(true);
|
|
draw(&mut visual);
|
|
|
|
// The panel claims focus on open, so escape reaches its key listener.
|
|
visual.simulate_keystrokes("escape");
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
|
|
// A closed popover is inert: no further dismissal events.
|
|
draw(&mut visual);
|
|
visual.simulate_keystrokes("escape");
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
}
|
|
|
|
#[test]
|
|
fn the_controlled_loop_reopens_from_the_app_side() {
|
|
let (mut cx, mut visual, window, changes, _clicks) = popover_window(true);
|
|
|
|
// Dismiss once.
|
|
click(&mut visual, point(px(600.), px(600.)));
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
draw(&mut visual);
|
|
|
|
// Reopen through the harness view: the app-driven half of the loop
|
|
// reports no event of its own.
|
|
window
|
|
.update(&mut cx, |harness, _, cx| {
|
|
harness.open = true;
|
|
cx.notify();
|
|
})
|
|
.unwrap();
|
|
draw(&mut visual);
|
|
|
|
// The panel is back and holds focus again: escape dismisses once more.
|
|
visual.simulate_keystrokes("escape");
|
|
assert_eq!(*changes.borrow(), vec![false, false]);
|
|
}
|
|
|
|
/// Clicking another control while the popover is open dismisses it and still
|
|
/// acts on that control: dismissing does not claim the event.
|
|
#[test]
|
|
fn clicking_another_control_dismisses_and_reaches_it() {
|
|
let (_cx, mut visual, _window, changes, clicks) = popover_window(true);
|
|
|
|
click(&mut visual, point(px(420.), px(320.)));
|
|
|
|
assert_eq!(*changes.borrow(), vec![false]);
|
|
assert_eq!(*clicks.borrow(), 1);
|
|
}
|
|
|
|
/// The panel hangs one spacing unit below the trigger: the anchor gap is
|
|
/// theme-owned, and rides the anchored element so the window-edge clamp
|
|
/// stays intact.
|
|
#[test]
|
|
fn the_panel_hangs_one_spacing_unit_below_the_trigger() {
|
|
let (cx, mut visual, _window, _changes, _clicks) = popover_window(true);
|
|
|
|
let trigger = visual
|
|
.debug_bounds("popover-trigger")
|
|
.expect("trigger rendered");
|
|
let panel = visual
|
|
.debug_bounds("popover-panel")
|
|
.expect("panel rendered");
|
|
|
|
let expected = cx.update(|cx| Theme::global(cx).spacing.unit);
|
|
let gap = panel.top() - trigger.bottom();
|
|
// Painted bounds are snapped to device pixels; allow half a pixel.
|
|
assert!(
|
|
(gap - expected).abs() <= px(0.5),
|
|
"the anchor gap is {gap:?}, expected {expected:?}"
|
|
);
|
|
}
|