chore: audit all components (#2)
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use gpui::{
|
||||
Context, IntoElement, Modifiers, MouseButton, ParentElement as _, Render, Styled as _,
|
||||
TestAppContext, VisualTestContext, Window, WindowHandle, div, point, px, rgba,
|
||||
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;
|
||||
@@ -10,37 +11,54 @@ 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(
|
||||
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()
|
||||
.size_full()
|
||||
.child(
|
||||
div().absolute().left(px(400.)).top(px(300.)).child(
|
||||
div()
|
||||
.w(px(100.))
|
||||
.h(px(32.))
|
||||
.id("popover-target")
|
||||
.size(px(40.))
|
||||
.bg(rgba(0x000000ff))
|
||||
.into_any_element()
|
||||
})
|
||||
.content(|_, _| {
|
||||
div()
|
||||
.w(px(200.))
|
||||
.h(px(80.))
|
||||
.child("Popover content")
|
||||
.into_any_element()
|
||||
}),
|
||||
)
|
||||
.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()
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,27 +66,31 @@ impl Render for Harness {
|
||||
/// 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.
|
||||
fn popover_window(
|
||||
open: bool,
|
||||
) -> (
|
||||
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)
|
||||
(cx, visual, window, changes, target_clicks)
|
||||
}
|
||||
|
||||
fn draw(visual: &mut VisualTestContext) {
|
||||
@@ -84,7 +106,7 @@ fn click(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
||||
|
||||
#[test]
|
||||
fn clicking_outside_dismisses_and_reports_closed() {
|
||||
let (_cx, mut visual, _window, changes) = popover_window(true);
|
||||
let (_cx, mut visual, _window, changes, _clicks) = popover_window(true);
|
||||
|
||||
click(&mut visual, point(px(600.), px(600.)));
|
||||
assert_eq!(*changes.borrow(), vec![false]);
|
||||
@@ -98,7 +120,7 @@ fn clicking_outside_dismisses_and_reports_closed() {
|
||||
|
||||
#[test]
|
||||
fn clicking_the_trigger_toggles_and_clicking_the_panel_does_nothing() {
|
||||
let (_cx, mut visual, _window, changes) = popover_window(true);
|
||||
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.)));
|
||||
@@ -111,7 +133,7 @@ fn clicking_the_trigger_toggles_and_clicking_the_panel_does_nothing() {
|
||||
|
||||
#[test]
|
||||
fn clicking_the_trigger_opens_a_closed_popover() {
|
||||
let (_cx, mut visual, _window, changes) = popover_window(false);
|
||||
let (_cx, mut visual, _window, changes, _clicks) = popover_window(false);
|
||||
|
||||
click(&mut visual, point(px(10.), px(16.)));
|
||||
assert_eq!(*changes.borrow(), vec![true]);
|
||||
@@ -119,7 +141,7 @@ fn clicking_the_trigger_opens_a_closed_popover() {
|
||||
|
||||
#[test]
|
||||
fn escape_dismisses_while_the_panel_holds_focus() {
|
||||
let (_cx, mut visual, _window, changes) = popover_window(true);
|
||||
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.
|
||||
@@ -134,7 +156,7 @@ fn escape_dismisses_while_the_panel_holds_focus() {
|
||||
|
||||
#[test]
|
||||
fn the_controlled_loop_reopens_from_the_app_side() {
|
||||
let (mut cx, mut visual, window, changes) = popover_window(true);
|
||||
let (mut cx, mut visual, window, changes, _clicks) = popover_window(true);
|
||||
|
||||
// Dismiss once.
|
||||
click(&mut visual, point(px(600.), px(600.)));
|
||||
@@ -155,3 +177,38 @@ fn the_controlled_loop_reopens_from_the_app_side() {
|
||||
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:?}"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user