chore: stabilize element ids and overlay focus tracking
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
//! A popover takes focus on open and gives it back to what held it on close.
|
||||
|
||||
use gpui::{
|
||||
Context, FocusHandle, InteractiveElement as _, IntoElement, Modifiers, ParentElement as _,
|
||||
Render, Styled as _, TestAppContext, VisualTestContext, Window, div, px, size,
|
||||
};
|
||||
use ui::common::v_flex;
|
||||
use ui::popover::Popover;
|
||||
use ui::theme::Theme;
|
||||
|
||||
const WINDOW_WIDTH: f32 = 800.0;
|
||||
const WINDOW_HEIGHT: f32 = 600.0;
|
||||
|
||||
/// A popover over a focusable trigger, with its open state held by the view.
|
||||
struct PopoverHarness {
|
||||
open: bool,
|
||||
trigger_focus: FocusHandle,
|
||||
}
|
||||
|
||||
impl Render for PopoverHarness {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let harness = cx.entity();
|
||||
let trigger_focus = self.trigger_focus.clone();
|
||||
|
||||
v_flex().size_full().p_4().child(
|
||||
Popover::new("focus-popover")
|
||||
.open(self.open)
|
||||
.on_change(move |open, _window, cx| {
|
||||
harness.update(cx, |harness, cx| {
|
||||
harness.open = open;
|
||||
cx.notify();
|
||||
});
|
||||
})
|
||||
.trigger(move |_, _| {
|
||||
div()
|
||||
.w(px(200.))
|
||||
.h(px(40.))
|
||||
.track_focus(&trigger_focus)
|
||||
.debug_selector(|| "focus-popover-trigger".to_string())
|
||||
.into_any_element()
|
||||
})
|
||||
.content(|_, _| {
|
||||
div()
|
||||
.h(px(120.))
|
||||
.debug_selector(|| "focus-popover-content".to_string())
|
||||
.into_any_element()
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The focused handle, whether it is on screen, and the trigger the popover opened over.
|
||||
fn focus_state(cx: &mut VisualTestContext) -> Option<(FocusHandle, bool)> {
|
||||
cx.update(|window, cx| {
|
||||
window
|
||||
.focused(cx)
|
||||
.map(|focused| (focused.clone(), focused.is_rendered(window)))
|
||||
})
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn closing_a_popover_restores_the_previous_focus(cx: &mut TestAppContext) {
|
||||
cx.update(Theme::init);
|
||||
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, cx| {
|
||||
PopoverHarness {
|
||||
open: false,
|
||||
trigger_focus: cx.focus_handle(),
|
||||
}
|
||||
});
|
||||
let mut cx = VisualTestContext::from_window(window.into(), cx);
|
||||
cx.run_until_parked();
|
||||
|
||||
let harness = window.root(&mut cx).expect("harness view");
|
||||
let trigger_focus = cx.update(|_, cx| harness.read(cx).trigger_focus.clone());
|
||||
let trigger = cx
|
||||
.debug_bounds("focus-popover-trigger")
|
||||
.expect("trigger laid out");
|
||||
|
||||
cx.update(|window, cx| window.focus(&trigger_focus, cx));
|
||||
assert_eq!(
|
||||
focus_state(&mut cx),
|
||||
Some((trigger_focus.clone(), true)),
|
||||
"the trigger holds focus before the popover opens"
|
||||
);
|
||||
|
||||
cx.simulate_click(trigger.center(), Modifiers::default());
|
||||
cx.run_until_parked();
|
||||
|
||||
assert!(
|
||||
cx.debug_bounds("popover-panel").is_some(),
|
||||
"clicking the trigger opens the popover"
|
||||
);
|
||||
let (opened_focus, opened_rendered) = focus_state(&mut cx).expect("the panel takes focus");
|
||||
assert!(opened_rendered, "the open panel's focus is on screen");
|
||||
assert_ne!(
|
||||
opened_focus, trigger_focus,
|
||||
"the panel holds focus while open"
|
||||
);
|
||||
|
||||
cx.simulate_keystrokes("escape");
|
||||
cx.run_until_parked();
|
||||
|
||||
assert!(
|
||||
cx.debug_bounds("popover-panel").is_none(),
|
||||
"escape closes the popover"
|
||||
);
|
||||
assert_eq!(
|
||||
focus_state(&mut cx),
|
||||
Some((trigger_focus, true)),
|
||||
"focus returns to the trigger"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
//! A covering overlay swallows scrollbar presses instead of dragging the thumb
|
||||
//! behind it.
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
Context, InteractiveElement as _, IntoElement, Modifiers, MouseButton, ParentElement as _,
|
||||
Render, ScrollHandle, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px,
|
||||
size,
|
||||
};
|
||||
use ui::scroll_area::ScrollArea;
|
||||
use ui::theme::Theme;
|
||||
|
||||
const WINDOW_WIDTH: f32 = 800.0;
|
||||
const WINDOW_HEIGHT: f32 = 600.0;
|
||||
const VIEWPORT_HEIGHT: f32 = 100.0;
|
||||
const ROW_HEIGHT: f32 = 40.0;
|
||||
const ROWS: usize = 50;
|
||||
/// The track's thickness, matching the theme's scrollbar width.
|
||||
const TRACK_WIDTH: f32 = 8.0;
|
||||
|
||||
/// A scroll area over a full-window cover the test toggles.
|
||||
struct ScrollHarness {
|
||||
handle: ScrollHandle,
|
||||
covered: bool,
|
||||
}
|
||||
|
||||
impl Render for ScrollHarness {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.relative()
|
||||
.size_full()
|
||||
.child(
|
||||
ScrollArea::new("scroll-test")
|
||||
.handle(self.handle.clone())
|
||||
.w_full()
|
||||
.h(px(VIEWPORT_HEIGHT))
|
||||
.children(
|
||||
(0..ROWS).map(|row| {
|
||||
div().w_full().h(px(ROW_HEIGHT)).child(format!("row {row}"))
|
||||
}),
|
||||
),
|
||||
)
|
||||
.when(self.covered, |this| {
|
||||
this.child(div().absolute().inset_0().occlude())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// A press and release in the middle of the scrollbar's track.
|
||||
fn press_track(cx: &mut VisualTestContext) {
|
||||
let track = point(
|
||||
px(WINDOW_WIDTH - TRACK_WIDTH / 2.0),
|
||||
px(VIEWPORT_HEIGHT * 0.8),
|
||||
);
|
||||
cx.simulate_mouse_down(track, MouseButton::Left, Modifiers::default());
|
||||
cx.simulate_mouse_up(track, MouseButton::Left, Modifiers::default());
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_covering_overlay_blocks_scrollbar_drags(cx: &mut TestAppContext) {
|
||||
cx.update(Theme::init);
|
||||
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
|
||||
ScrollHarness {
|
||||
handle: ScrollHandle::new(),
|
||||
covered: true,
|
||||
}
|
||||
});
|
||||
let mut cx = VisualTestContext::from_window(window.into(), cx);
|
||||
cx.run_until_parked();
|
||||
|
||||
let harness = window.root(&mut cx).expect("harness view");
|
||||
let handle = cx.update(|_, cx| harness.read(cx).handle.clone());
|
||||
|
||||
press_track(&mut cx);
|
||||
assert_eq!(
|
||||
handle.offset().y,
|
||||
px(0.),
|
||||
"the cover swallows the press on the track behind it"
|
||||
);
|
||||
|
||||
cx.update(|_, cx| {
|
||||
harness.update(cx, |harness, cx| {
|
||||
harness.covered = false;
|
||||
cx.notify();
|
||||
});
|
||||
});
|
||||
cx.run_until_parked();
|
||||
|
||||
press_track(&mut cx);
|
||||
assert!(
|
||||
handle.offset().y < px(0.),
|
||||
"an uncovered press on the track scrolls the content"
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user