chore: clean up all tests
This commit is contained in:
@@ -1,70 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, InteractiveElement as _, IntoElement, Modifiers,
|
|
||||||
ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window, div, point,
|
|
||||||
px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::alert::{Alert, AlertSeverity};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
severity: AlertSeverity,
|
|
||||||
dismissed: Rc<RefCell<bool>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut gpui::Context<Self>) -> impl IntoElement {
|
|
||||||
let dismissed = self.dismissed.clone();
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
div().debug_selector(|| "test-alert".to_string()).child(
|
|
||||||
Alert::new("test-alert")
|
|
||||||
.title("Heads up")
|
|
||||||
.severity(self.severity)
|
|
||||||
.message("Secondary line.")
|
|
||||||
.on_dismiss(move |_| *dismissed.borrow_mut() = true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The alert stretches across the top of the content area; its close button
|
|
||||||
/// sits in the banner's top-right corner.
|
|
||||||
fn alert_window(
|
|
||||||
severity: AlertSeverity,
|
|
||||||
) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<bool>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let dismissed = Rc::new(RefCell::new(false));
|
|
||||||
let harness_dismissed = dismissed.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
severity,
|
|
||||||
dismissed: harness_dismissed,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(
|
|
||||||
VisualTestContext::from_window(window, &cx),
|
|
||||||
window,
|
|
||||||
dismissed,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clicking the close button is how the user acts on the alert; the banner
|
|
||||||
/// reports the intent instead of hiding itself.
|
|
||||||
#[test]
|
|
||||||
fn closing_the_alert_reports_dismissal() {
|
|
||||||
let (mut cx, _window, dismissed) = alert_window(AlertSeverity::Danger);
|
|
||||||
|
|
||||||
let bounds = cx.debug_bounds("test-alert").expect("alert rendered");
|
|
||||||
cx.simulate_click(
|
|
||||||
point(bounds.right() - px(14.), bounds.top() + px(14.)),
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert!(*dismissed.borrow(), "the close button reported dismissal");
|
|
||||||
}
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{AppContext as _, TestAppContext};
|
|
||||||
use gpui_native_core::alert::{Alert, AlertSeverity, Alerts};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
fn alerts_stack() -> (TestAppContext, gpui::Entity<Alerts>) {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let alerts = cx.update(|cx| cx.new(|_| Alerts::default()));
|
|
||||||
(cx, alerts)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn pushed_alerts_stack_up_and_dismiss_by_id() {
|
|
||||||
let (cx, alerts) = alerts_stack();
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
alerts.update(cx, |alerts, cx| {
|
|
||||||
alerts.push(
|
|
||||||
Alert::new("a").title("First").severity(AlertSeverity::Info),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
alerts.push(
|
|
||||||
Alert::new("b")
|
|
||||||
.title("Second")
|
|
||||||
.severity(AlertSeverity::Warning),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
assert_eq!(cx.update(|cx| alerts.read(cx).count()), 2);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
alerts.update(cx, |alerts, cx| alerts.dismiss(0, cx));
|
|
||||||
});
|
|
||||||
assert_eq!(cx.update(|cx| alerts.read(cx).count()), 1);
|
|
||||||
|
|
||||||
// Duplicate ids are fine: the stack re-keys each entry to its own id.
|
|
||||||
cx.update(|cx| {
|
|
||||||
alerts.update(cx, |alerts, cx| {
|
|
||||||
alerts.push(Alert::new("b").title("Third"), cx);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
assert_eq!(cx.update(|cx| alerts.read(cx).count()), 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_close_button_routes_through_the_store_and_runs_the_handler() {
|
|
||||||
let (cx, alerts) = alerts_stack();
|
|
||||||
let dismissed = Rc::new(std::cell::RefCell::new(false));
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
let handler = dismissed.clone();
|
|
||||||
alerts.update(cx, |alerts, cx| {
|
|
||||||
alerts.push(
|
|
||||||
Alert::new("a").title("Sync failed").on_dismiss(move |_| {
|
|
||||||
*handler.borrow_mut() = true;
|
|
||||||
}),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// The pushed alert's close button calls the store's dismissal, which
|
|
||||||
// then runs the handler.
|
|
||||||
cx.update(|cx| {
|
|
||||||
alerts.update(cx, |alerts, cx| alerts.dismiss(0, cx));
|
|
||||||
});
|
|
||||||
assert!(*dismissed.borrow());
|
|
||||||
assert_eq!(cx.update(|cx| alerts.read(cx).count()), 0);
|
|
||||||
}
|
|
||||||
@@ -1,102 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke,
|
|
||||||
Modifiers, ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window,
|
|
||||||
div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::button::Button;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
clicks: Rc<RefCell<usize>>,
|
|
||||||
disabled: bool,
|
|
||||||
loading: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let clicks = self.clicks.clone();
|
|
||||||
div().size_full().flex().flex_col().child(
|
|
||||||
Button::new("test-button")
|
|
||||||
.label("Save")
|
|
||||||
.disabled(self.disabled)
|
|
||||||
.loading(self.loading)
|
|
||||||
.on_click(move |_, _, _| *clicks.borrow_mut() += 1),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window whose button stretches across the top band, so
|
|
||||||
/// `point(px(60.), px(14.))` always lands on it.
|
|
||||||
fn button_window(
|
|
||||||
disabled: bool,
|
|
||||||
loading: bool,
|
|
||||||
) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<usize>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let clicks = Rc::new(RefCell::new(0));
|
|
||||||
let harness_clicks = clicks.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
clicks: harness_clicks,
|
|
||||||
disabled,
|
|
||||||
loading,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(VisualTestContext::from_window(window, &cx), window, clicks)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(cx: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn pointer_click_fires_on_click() {
|
|
||||||
let (mut cx, _window, clicks) = button_window(false, false);
|
|
||||||
cx.simulate_click(point(px(60.), px(14.)), Modifiers::default());
|
|
||||||
assert_eq!(*clicks.borrow(), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_button_ignores_clicks() {
|
|
||||||
let (mut cx, _window, clicks) = button_window(true, false);
|
|
||||||
cx.simulate_click(point(px(60.), px(14.)), Modifiers::default());
|
|
||||||
assert_eq!(*clicks.borrow(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn loading_button_ignores_clicks() {
|
|
||||||
let (mut cx, _window, clicks) = button_window(false, true);
|
|
||||||
cx.simulate_click(point(px(60.), px(14.)), Modifiers::default());
|
|
||||||
assert_eq!(*clicks.borrow(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_and_space_fire_on_click_while_focused() {
|
|
||||||
let (mut cx, window, clicks) = button_window(false, false);
|
|
||||||
cx.simulate_click(point(px(60.), px(14.)), Modifiers::default());
|
|
||||||
assert_eq!(*clicks.borrow(), 1, "the pointer click itself activates");
|
|
||||||
|
|
||||||
// Repaint with the button focused so its Enter/Space handlers register.
|
|
||||||
draw(&mut cx, window);
|
|
||||||
|
|
||||||
for key in ["enter", "space"] {
|
|
||||||
cx.simulate_event(KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
});
|
|
||||||
cx.simulate_event(KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*clicks.borrow(), 3, "Enter and Space each fire on_click");
|
|
||||||
}
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke,
|
|
||||||
Modifiers, ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window,
|
|
||||||
div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::checkbox::Checkbox;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
changes: Rc<RefCell<Vec<bool>>>,
|
|
||||||
checked: bool,
|
|
||||||
disabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().flex().flex_col().child(
|
|
||||||
Checkbox::new("test-checkbox")
|
|
||||||
.label("Telemetry")
|
|
||||||
.checked(self.checked)
|
|
||||||
.disabled(self.disabled)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.checked = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window whose checkbox sits in the top-left corner, so
|
|
||||||
/// `point(px(8.), px(10.))` lands on it.
|
|
||||||
fn checkbox_window(
|
|
||||||
checked: bool,
|
|
||||||
disabled: bool,
|
|
||||||
) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<Vec<bool>>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
changes: harness_changes,
|
|
||||||
checked,
|
|
||||||
disabled,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(VisualTestContext::from_window(window, &cx), window, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(cx: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_reports_the_next_value_through_the_controlled_loop() {
|
|
||||||
let (mut cx, _window, changes) = checkbox_window(false, false);
|
|
||||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![true]);
|
|
||||||
|
|
||||||
// The second click must report `false`: only if the owner applied the
|
|
||||||
// first change does the checkbox know it is now checked.
|
|
||||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_checkbox_does_not_report_changes() {
|
|
||||||
let (mut cx, _window, changes) = checkbox_window(false, true);
|
|
||||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_and_space_report_the_next_value_while_focused() {
|
|
||||||
let (mut cx, window, changes) = checkbox_window(false, false);
|
|
||||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![true],
|
|
||||||
"the pointer click itself toggles"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Repaint with the checkbox focused so its Enter/Space handlers register.
|
|
||||||
draw(&mut cx, window);
|
|
||||||
|
|
||||||
for key in ["enter", "space"] {
|
|
||||||
cx.simulate_event(KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
});
|
|
||||||
cx.simulate_event(KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false, true]);
|
|
||||||
}
|
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, IntoElement, Modifiers, ParentElement as _, Render,
|
|
||||||
Styled as _, TestAppContext, VisualTestContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::clipboard::{COPIED_DURATION, ClipboardButton};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
const VALUE: &str = "https://example.com/repo.git";
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
copies: Rc<RefCell<usize>>,
|
|
||||||
disabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let copies = self.copies.clone();
|
|
||||||
div().size_full().flex().flex_col().child(
|
|
||||||
ClipboardButton::new("test-clipboard", VALUE)
|
|
||||||
.disabled(self.disabled)
|
|
||||||
.on_copied(move |_| *copies.borrow_mut() += 1),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window whose button stretches across the top control row, so
|
|
||||||
/// `point(px(60.), px(14.))` always lands on it.
|
|
||||||
fn clipboard_window(
|
|
||||||
disabled: bool,
|
|
||||||
) -> (
|
|
||||||
TestAppContext,
|
|
||||||
VisualTestContext,
|
|
||||||
AnyWindowHandle,
|
|
||||||
Rc<RefCell<usize>>,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let copies = Rc::new(RefCell::new(0));
|
|
||||||
let harness_copies = copies.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
copies: harness_copies,
|
|
||||||
disabled,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
(cx, visual, window, copies)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(visual: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
visual
|
|
||||||
.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn clipboard_text(visual: &mut VisualTestContext) -> String {
|
|
||||||
visual.update(|_window, cx| {
|
|
||||||
cx.read_from_clipboard()
|
|
||||||
.and_then(|item| item.text())
|
|
||||||
.unwrap_or_default()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn click(visual: &mut VisualTestContext) {
|
|
||||||
let selector = if visual.debug_bounds("clipboard").is_some() {
|
|
||||||
"clipboard"
|
|
||||||
} else {
|
|
||||||
"clipboard-copied"
|
|
||||||
};
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds(selector)
|
|
||||||
.expect("clipboard button should be rendered");
|
|
||||||
visual.simulate_click(bounds.center(), Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn click_copies_the_value_and_fires_on_copied() {
|
|
||||||
let (_cx, mut visual, _window, copies) = clipboard_window(false);
|
|
||||||
click(&mut visual);
|
|
||||||
assert_eq!(clipboard_text(&mut visual), VALUE);
|
|
||||||
assert_eq!(*copies.borrow(), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_button_does_not_copy() {
|
|
||||||
let (_cx, mut visual, _window, copies) = clipboard_window(true);
|
|
||||||
click(&mut visual);
|
|
||||||
assert_eq!(clipboard_text(&mut visual), "");
|
|
||||||
assert_eq!(*copies.borrow(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn copied_confirmation_shows_then_reverts() {
|
|
||||||
let (cx, mut visual, window, _copies) = clipboard_window(false);
|
|
||||||
click(&mut visual);
|
|
||||||
draw(&mut visual, window);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("clipboard-copied").is_some(),
|
|
||||||
"the check mark state should render right after the click"
|
|
||||||
);
|
|
||||||
assert!(visual.debug_bounds("clipboard").is_none());
|
|
||||||
|
|
||||||
cx.executor().advance_clock(COPIED_DURATION);
|
|
||||||
visual.run_until_parked();
|
|
||||||
draw(&mut visual, window);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("clipboard").is_some(),
|
|
||||||
"the copy icon should return once the confirmation window passes"
|
|
||||||
);
|
|
||||||
assert!(visual.debug_bounds("clipboard-copied").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_second_click_within_the_window_keeps_the_confirmation_up() {
|
|
||||||
let (cx, mut visual, window, _copies) = clipboard_window(false);
|
|
||||||
click(&mut visual);
|
|
||||||
cx.executor().advance_clock(COPIED_DURATION / 2);
|
|
||||||
click(&mut visual);
|
|
||||||
cx.executor().advance_clock(COPIED_DURATION / 2);
|
|
||||||
visual.run_until_parked();
|
|
||||||
draw(&mut visual, window);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("clipboard-copied").is_some(),
|
|
||||||
"the first click's stale revert must not clear the second click's confirmation"
|
|
||||||
);
|
|
||||||
|
|
||||||
cx.executor().advance_clock(COPIED_DURATION);
|
|
||||||
visual.run_until_parked();
|
|
||||||
draw(&mut visual, window);
|
|
||||||
assert!(visual.debug_bounds("clipboard").is_some());
|
|
||||||
}
|
|
||||||
@@ -1,367 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, MouseButton, ParentElement as _, Render,
|
|
||||||
SharedString, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::combobox::{Combobox, ComboboxOption, ComboboxState};
|
|
||||||
use gpui_native_core::init;
|
|
||||||
|
|
||||||
fn option(label: &'static str) -> ComboboxOption<SharedString> {
|
|
||||||
(SharedString::from(label), SharedString::from(label))
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
combobox: Entity<ComboboxState<SharedString>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().p_4().child(self.combobox.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Recorder = Rc<RefCell<Vec<Option<SharedString>>>>;
|
|
||||||
type CreatedRecorder = Rc<RefCell<Vec<SharedString>>>;
|
|
||||||
|
|
||||||
struct ComboHost {
|
|
||||||
cx: TestAppContext,
|
|
||||||
state: Entity<ComboboxState<SharedString>>,
|
|
||||||
changes: Recorder,
|
|
||||||
created: CreatedRecorder,
|
|
||||||
visual: VisualTestContext,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn combo_host(keep_unmatched: bool, create: bool) -> ComboHost {
|
|
||||||
combo_host_with(|builder, created| {
|
|
||||||
let builder = if keep_unmatched {
|
|
||||||
builder.keep_unmatched_text()
|
|
||||||
} else {
|
|
||||||
builder
|
|
||||||
};
|
|
||||||
if create {
|
|
||||||
builder.on_create(move |text, _, _| created.borrow_mut().push(text))
|
|
||||||
} else {
|
|
||||||
builder
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Builds a host whose combobox the caller configures; the created-recorder
|
|
||||||
/// is handed over for `on_create` wiring.
|
|
||||||
fn combo_host_with(
|
|
||||||
configure: impl FnOnce(Combobox<SharedString>, CreatedRecorder) -> Combobox<SharedString>,
|
|
||||||
) -> ComboHost {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
// `init` registers the input bindings and then the combobox's, in the
|
|
||||||
// load-bearing order.
|
|
||||||
cx.update(init);
|
|
||||||
let changes: Recorder = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let created: Rc<RefCell<Vec<SharedString>>> = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<ComboboxState<SharedString>>>>> =
|
|
||||||
Rc::new(RefCell::new(None));
|
|
||||||
let window = cx.update(|cx| {
|
|
||||||
let changes_for_builder = changes.clone();
|
|
||||||
let created_for_builder = created.clone();
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
cx.open_window(gpui::WindowOptions::default(), |window, cx| {
|
|
||||||
let builder = configure(
|
|
||||||
Combobox::new(
|
|
||||||
"test-combo",
|
|
||||||
"Fruit",
|
|
||||||
vec![option("main"), option("maintain"), option("release")],
|
|
||||||
)
|
|
||||||
.on_change(move |value, _, _| changes_for_builder.borrow_mut().push(value)),
|
|
||||||
created_for_builder,
|
|
||||||
);
|
|
||||||
let combobox = cx.new(|cx| ComboboxState::new(builder, window, cx));
|
|
||||||
cell.borrow_mut().replace(combobox.clone());
|
|
||||||
cx.new(|_| Host { combobox })
|
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
draw(&mut visual);
|
|
||||||
ComboHost {
|
|
||||||
cx,
|
|
||||||
state,
|
|
||||||
changes,
|
|
||||||
created,
|
|
||||||
visual,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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, gpui::Modifiers::default());
|
|
||||||
visual.simulate_mouse_up(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clicking into a valueless combobox field opens the options right away:
|
|
||||||
/// no keystroke is needed to see the choices.
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_empty_field_opens_the_popup() {
|
|
||||||
let mut host = combo_host(false, false);
|
|
||||||
|
|
||||||
// Inside the field, which sits at the top of the padded host.
|
|
||||||
click(&mut host.visual, point(px(100.), px(27.)));
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
host.state.read_with(&host.cx, |state, _| state.is_open()),
|
|
||||||
"clicking the empty field should show the options"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
host.visual.debug_bounds("test-combo-panel").is_some(),
|
|
||||||
"the options panel should be rendered"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn focus(host: &mut ComboHost) {
|
|
||||||
let state = host.state.clone();
|
|
||||||
host.visual.update(|window, cx| {
|
|
||||||
state.update(cx, |state, cx| state.focus(window, cx));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn type_text(host: &mut ComboHost, text: &str) {
|
|
||||||
focus(host);
|
|
||||||
for letter in text.chars() {
|
|
||||||
host.visual.simulate_keystrokes(&letter.to_string());
|
|
||||||
}
|
|
||||||
draw(&mut host.visual);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typing_opens_the_picker_and_filters_the_options() {
|
|
||||||
let mut host = combo_host(false, false);
|
|
||||||
type_text(&mut host, "mai");
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-option-0").is_some());
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-option-1").is_some());
|
|
||||||
assert!(
|
|
||||||
host.visual.debug_bounds("test-combo-option-2").is_none(),
|
|
||||||
"release must not match \"mai\""
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_commits_the_highlighted_option() {
|
|
||||||
let mut host = combo_host(false, false);
|
|
||||||
type_text(&mut host, "mai");
|
|
||||||
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
host.visual.run_until_parked();
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
*host.changes.borrow(),
|
|
||||||
vec![Some(SharedString::from("main"))],
|
|
||||||
"the first surviving option starts highlighted"
|
|
||||||
);
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-panel").is_none());
|
|
||||||
let text = host.cx.update(|cx| host.state.read(cx).text(cx));
|
|
||||||
assert_eq!(text, "main");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typing_clears_the_value() {
|
|
||||||
let mut host = combo_host(false, false);
|
|
||||||
type_text(&mut host, "main");
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
assert_eq!(
|
|
||||||
*host.changes.borrow(),
|
|
||||||
vec![Some(SharedString::from("main"))]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Typing again first clears the value it no longer names.
|
|
||||||
host.visual.simulate_keystrokes("r");
|
|
||||||
host.visual.run_until_parked();
|
|
||||||
draw(&mut host.visual);
|
|
||||||
assert_eq!(
|
|
||||||
*host.changes.borrow(),
|
|
||||||
vec![Some(SharedString::from("main")), None]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn escape_closes_without_committing() {
|
|
||||||
let mut host = combo_host(false, false);
|
|
||||||
type_text(&mut host, "mai");
|
|
||||||
|
|
||||||
host.visual.simulate_keystrokes("escape");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-panel").is_none());
|
|
||||||
assert!(host.changes.borrow().is_empty(), "escape must not commit");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn blur_reverts_unmatched_text_to_the_empty_selection() {
|
|
||||||
let mut host = combo_host(false, false);
|
|
||||||
type_text(&mut host, "nonsense");
|
|
||||||
assert!(host.changes.borrow().is_empty());
|
|
||||||
|
|
||||||
host.visual.update(|window, cx| window.blur(cx));
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert!(host.changes.borrow().is_empty());
|
|
||||||
let text = host.cx.update(|cx| host.state.read(cx).text(cx));
|
|
||||||
assert_eq!(text, "", "revert with no value empties the field");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn keep_unmatched_leaves_the_text() {
|
|
||||||
let mut host = combo_host(true, false);
|
|
||||||
type_text(&mut host, "nonsense");
|
|
||||||
|
|
||||||
host.visual.update(|window, cx| window.blur(cx));
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
let text = host.cx.update(|cx| host.state.read(cx).text(cx));
|
|
||||||
assert_eq!(text, "nonsense", "keep leaves the text in the field");
|
|
||||||
// The picker started with no value, so typing cleared nothing and there
|
|
||||||
// was nothing to report.
|
|
||||||
assert!(host.changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn create_hands_unmatched_text_to_the_handler() {
|
|
||||||
let mut host = combo_host(false, true);
|
|
||||||
type_text(&mut host, "feature");
|
|
||||||
|
|
||||||
host.visual.update(|window, cx| window.blur(cx));
|
|
||||||
host.visual.run_until_parked();
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
*host.created.borrow(),
|
|
||||||
vec![SharedString::from("feature")],
|
|
||||||
"create hands the text over on blur"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_row_commits_it() {
|
|
||||||
let mut host = combo_host(false, false);
|
|
||||||
type_text(&mut host, "rel");
|
|
||||||
|
|
||||||
let row = host
|
|
||||||
.visual
|
|
||||||
.debug_bounds("test-combo-option-0")
|
|
||||||
.expect("release row missing");
|
|
||||||
host.visual
|
|
||||||
.simulate_click(row.center(), gpui::Modifiers::default());
|
|
||||||
host.visual.run_until_parked();
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
*host.changes.borrow(),
|
|
||||||
vec![Some(SharedString::from("release"))]
|
|
||||||
);
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-panel").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Regression: handlers used to run inside an update of the combobox
|
|
||||||
/// entity, so a handler that touched its own state re-entered and panicked.
|
|
||||||
/// Both handlers now run on the executor and may update the state freely.
|
|
||||||
#[test]
|
|
||||||
fn handlers_may_update_their_own_combobox() {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
cx.update(init);
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<ComboboxState<SharedString>>>>> =
|
|
||||||
Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.update(|cx| {
|
|
||||||
cx.open_window(gpui::WindowOptions::default(), |window, cx| {
|
|
||||||
let builder = Combobox::new(
|
|
||||||
"self-updating-combo",
|
|
||||||
"Label",
|
|
||||||
vec![option("bug"), option("docs")],
|
|
||||||
)
|
|
||||||
.on_create(move |text: SharedString, window, cx| {
|
|
||||||
let state = cell.borrow().clone().expect("state captured");
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
let mut options = state.options_snapshot();
|
|
||||||
if !options.iter().any(|(value, _)| value == &text) {
|
|
||||||
options.push((text.clone(), text.clone()));
|
|
||||||
state.set_options(options, cx);
|
|
||||||
}
|
|
||||||
state.set_selected(Some(text), window, cx);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
let combobox = cx.new(|cx| ComboboxState::new(builder, window, cx));
|
|
||||||
state_cell.borrow_mut().replace(combobox.clone());
|
|
||||||
cx.new(|_| Host { combobox })
|
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
|
|
||||||
// Typing an unmatched name and blurring hands it to `on_create`, which
|
|
||||||
// updates this very combobox — the crash the deferral fixes.
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
state.update(cx, |state, cx| state.focus(window, cx));
|
|
||||||
});
|
|
||||||
for letter in "feature".chars() {
|
|
||||||
visual.simulate_keystrokes(&letter.to_string());
|
|
||||||
}
|
|
||||||
visual.update(|window, cx| window.blur(cx));
|
|
||||||
visual.run_until_parked();
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
|
|
||||||
let selected = cx.update(|cx| state.read(cx).selected().cloned());
|
|
||||||
assert_eq!(selected, Some(SharedString::from("feature")));
|
|
||||||
let options = cx.update(|cx| state.read(cx).options_snapshot());
|
|
||||||
assert!(options.iter().any(|(value, _)| value == "feature"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Matches when the query's characters appear in the label in order, so
|
|
||||||
/// "mn" finds "main" though the substring matcher would find nothing.
|
|
||||||
fn subsequence(query: &str, _value: &SharedString, label: &SharedString) -> bool {
|
|
||||||
let label = label.to_lowercase();
|
|
||||||
let mut letters = label.chars();
|
|
||||||
query
|
|
||||||
.to_lowercase()
|
|
||||||
.chars()
|
|
||||||
.all(|letter| letters.any(|found| found == letter))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_custom_filter_decides_which_options_survive() {
|
|
||||||
let mut host = combo_host_with(|builder, _| builder.filter(subsequence));
|
|
||||||
// "mn" is no substring of any option, so only the custom matcher finds
|
|
||||||
// "main" and "maintain"; "release" has no "n" and drops out.
|
|
||||||
type_text(&mut host, "mn");
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-option-0").is_some());
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-option-1").is_some());
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-option-2").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn replacing_the_options_reruns_the_custom_filter() {
|
|
||||||
let mut host = combo_host_with(|builder, _| builder.filter(subsequence));
|
|
||||||
type_text(&mut host, "mn");
|
|
||||||
|
|
||||||
host.cx.update(|cx| {
|
|
||||||
host.state.update(cx, |state, cx| {
|
|
||||||
state.set_options(vec![option("metro"), option("mainline")], cx)
|
|
||||||
})
|
|
||||||
});
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
// "metro" has no "n" after its "m"; "mainline" survives as the only row.
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-option-0").is_some());
|
|
||||||
assert!(host.visual.debug_bounds("test-combo-option-1").is_none());
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, SharedString,
|
|
||||||
Styled as _, TestAppContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::combobox::{Combobox, ComboboxOption, ComboboxState};
|
|
||||||
use gpui_native_core::init;
|
|
||||||
|
|
||||||
fn option(label: &'static str) -> ComboboxOption<SharedString> {
|
|
||||||
(SharedString::from(label), SharedString::from(label))
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
combobox: Entity<ComboboxState<SharedString>>,
|
|
||||||
renders: Rc<RefCell<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
*self.renders.borrow_mut() += 1;
|
|
||||||
div().size_full().p_4().child(self.combobox.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn real_keystrokes_are_not_pathologically_slow() {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
cx.update(init);
|
|
||||||
let (state, renders, window) = cx.update(|cx| {
|
|
||||||
let renders = Rc::new(RefCell::new(0));
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<ComboboxState<SharedString>>>>> =
|
|
||||||
Rc::new(RefCell::new(None));
|
|
||||||
let renders_for_host = renders.clone();
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx
|
|
||||||
.open_window(gpui::WindowOptions::default(), |window, cx| {
|
|
||||||
let state = cx.new(|cx| {
|
|
||||||
ComboboxState::new(
|
|
||||||
Combobox::new(
|
|
||||||
"bench-combo",
|
|
||||||
"Fruit",
|
|
||||||
vec![
|
|
||||||
option("main"),
|
|
||||||
option("maintain"),
|
|
||||||
option("release"),
|
|
||||||
option("banana"),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
window,
|
|
||||||
cx,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
let state_for_host = state.clone();
|
|
||||||
cell.borrow_mut().replace(state.clone());
|
|
||||||
cx.new(|_| Host {
|
|
||||||
combobox: state_for_host,
|
|
||||||
renders: renders_for_host,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
(state, renders, window)
|
|
||||||
});
|
|
||||||
let mut visual = gpui::VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
state.update(cx, |state, cx| state.focus(window, cx));
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
// The focused caret must ride executor timers, never frame requests.
|
|
||||||
let frames = visual.update(|window, cx| window.simulate_next_frame(cx));
|
|
||||||
assert_eq!(
|
|
||||||
frames, 0,
|
|
||||||
"an idle focused combobox must not request animation frames"
|
|
||||||
);
|
|
||||||
|
|
||||||
for letter in "abcdefghijklmnopqrstuvwxyz".chars() {
|
|
||||||
visual.simulate_keystrokes(&letter.to_string());
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let total_renders = *renders.borrow();
|
|
||||||
println!(
|
|
||||||
"26 real keystrokes: {total_renders} host renders ({:.2} per keystroke)",
|
|
||||||
total_renders as f32 / 26.
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
total_renders >= 26,
|
|
||||||
"every keystroke must redraw the page; got {total_renders} renders"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
total_renders <= 26 * 3,
|
|
||||||
"typing caused {total_renders} host renders; healthy filtering stays under 3 per keystroke"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,279 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, SharedString,
|
|
||||||
Styled as _, TestAppContext, VisualTestContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::command_palette::{Command, CommandPalette, CommandPaletteState};
|
|
||||||
use gpui_native_core::init;
|
|
||||||
|
|
||||||
fn commands() -> Vec<Command> {
|
|
||||||
vec![
|
|
||||||
Command::new("open-file", "Open File").hint("Cmd+O"),
|
|
||||||
Command::new("close-tab", "Close Tab"),
|
|
||||||
Command::new("save-all", "Save All").hint("Cmd+Alt+S"),
|
|
||||||
Command::new("toggle-theme", "Toggle Theme"),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The caller-owned matcher: case-insensitive substring. `reverse` flips the
|
|
||||||
/// match order, exercising that ranking belongs to the caller.
|
|
||||||
fn substring_matcher(reverse: bool) -> impl Fn(&str, &[Command]) -> Vec<Command> {
|
|
||||||
move |query, commands| {
|
|
||||||
let query = query.to_lowercase();
|
|
||||||
let matched: Vec<Command> = commands
|
|
||||||
.iter()
|
|
||||||
.filter(|command| command.label.to_lowercase().contains(&query))
|
|
||||||
.cloned()
|
|
||||||
.collect();
|
|
||||||
if reverse {
|
|
||||||
matched.into_iter().rev().collect()
|
|
||||||
} else {
|
|
||||||
matched
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
palette: Entity<CommandPaletteState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(self.palette.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Executed = Rc<RefCell<Vec<SharedString>>>;
|
|
||||||
|
|
||||||
struct PaletteHost {
|
|
||||||
cx: TestAppContext,
|
|
||||||
state: Entity<CommandPaletteState>,
|
|
||||||
executed: Executed,
|
|
||||||
visual: VisualTestContext,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn palette_host(reverse: bool) -> PaletteHost {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
// `init` registers the input bindings and then the palette's, in the
|
|
||||||
// load-bearing order.
|
|
||||||
cx.update(init);
|
|
||||||
let executed: Executed = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<CommandPaletteState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let window = cx.update(|cx| {
|
|
||||||
let executed_for_builder = executed.clone();
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
cx.open_window(gpui::WindowOptions::default(), |window, cx| {
|
|
||||||
let palette =
|
|
||||||
CommandPalette::new("test-palette", commands(), substring_matcher(reverse))
|
|
||||||
.on_execute(move |command: &Command, _, _| {
|
|
||||||
executed_for_builder.borrow_mut().push(command.id.clone())
|
|
||||||
});
|
|
||||||
let state = cx.new(|cx| CommandPaletteState::new(palette, window, cx));
|
|
||||||
cell.borrow_mut().replace(state.clone());
|
|
||||||
cx.new(|_| Host { palette: state })
|
|
||||||
})
|
|
||||||
.unwrap()
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
draw(&mut visual);
|
|
||||||
PaletteHost {
|
|
||||||
cx,
|
|
||||||
state,
|
|
||||||
executed,
|
|
||||||
visual,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(visual: &mut VisualTestContext) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn open(host: &mut PaletteHost) {
|
|
||||||
let state = host.state.clone();
|
|
||||||
host.visual.update(|window, cx| {
|
|
||||||
state.update(cx, |state, cx| state.open(window, cx));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn type_text(host: &mut PaletteHost, text: &str) {
|
|
||||||
for letter in text.chars() {
|
|
||||||
host.visual.simulate_keystrokes(&letter.to_string());
|
|
||||||
}
|
|
||||||
draw(&mut host.visual);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn open_lists_the_callers_commands() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-command-0").is_some());
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-command-3").is_some());
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-panel").is_some());
|
|
||||||
assert!(host.cx.update(|cx| host.state.read(cx).is_open()));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_runs_the_highlighted_command_and_closes() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
*host.executed.borrow(),
|
|
||||||
vec![SharedString::from("open-file")],
|
|
||||||
"the first row starts highlighted"
|
|
||||||
);
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-panel").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rows_follow_the_callers_match_order() {
|
|
||||||
let mut host = palette_host(true);
|
|
||||||
open(&mut host);
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
*host.executed.borrow(),
|
|
||||||
vec![SharedString::from("toggle-theme")],
|
|
||||||
"enter runs the first row, and the caller's matcher owns the row order"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typing_filters_through_the_caller_matcher() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
type_text(&mut host, "save");
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-command-0").is_some());
|
|
||||||
assert!(
|
|
||||||
host.visual.debug_bounds("test-palette-command-1").is_none(),
|
|
||||||
"only the matching command stays listed"
|
|
||||||
);
|
|
||||||
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
assert_eq!(
|
|
||||||
*host.executed.borrow(),
|
|
||||||
vec![SharedString::from("save-all")]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn escape_closes_without_running() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
host.visual.simulate_keystrokes("escape");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-panel").is_none());
|
|
||||||
assert!(host.executed.borrow().is_empty());
|
|
||||||
assert!(!host.cx.update(|cx| host.state.read(cx).is_open()));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrow_keys_move_the_highlight() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
host.visual.simulate_keystrokes("down");
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
assert_eq!(
|
|
||||||
*host.executed.borrow(),
|
|
||||||
vec![SharedString::from("close-tab")]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Up from the top wraps to the last row.
|
|
||||||
open(&mut host);
|
|
||||||
host.visual.simulate_keystrokes("up");
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
assert_eq!(
|
|
||||||
*host.executed.borrow(),
|
|
||||||
vec![
|
|
||||||
SharedString::from("close-tab"),
|
|
||||||
SharedString::from("toggle-theme")
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_row_runs_it() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
let row = host
|
|
||||||
.visual
|
|
||||||
.debug_bounds("test-palette-command-2")
|
|
||||||
.expect("save-all row missing");
|
|
||||||
host.visual
|
|
||||||
.simulate_click(row.center(), gpui::Modifiers::default());
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
*host.executed.borrow(),
|
|
||||||
vec![SharedString::from("save-all")]
|
|
||||||
);
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-panel").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_scrim_closes() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
draw(&mut host.visual);
|
|
||||||
host.visual.simulate_click(
|
|
||||||
gpui::point(gpui::px(10.), gpui::px(10.)),
|
|
||||||
gpui::Modifiers::default(),
|
|
||||||
);
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-panel").is_none());
|
|
||||||
assert!(host.executed.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn reopen_starts_from_a_fresh_query() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
open(&mut host);
|
|
||||||
type_text(&mut host, "zzzz");
|
|
||||||
assert!(
|
|
||||||
host.visual.debug_bounds("test-palette-command-0").is_none(),
|
|
||||||
"nothing matches nonsense"
|
|
||||||
);
|
|
||||||
|
|
||||||
host.visual.simulate_keystrokes("escape");
|
|
||||||
open(&mut host);
|
|
||||||
draw(&mut host.visual);
|
|
||||||
assert!(
|
|
||||||
host.visual.debug_bounds("test-palette-command-0").is_some(),
|
|
||||||
"a fresh open clears the query"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn set_commands_replaces_the_registry() {
|
|
||||||
let mut host = palette_host(false);
|
|
||||||
host.cx.update(|cx| {
|
|
||||||
host.state.update(cx, |state, cx| {
|
|
||||||
state.set_commands(vec![Command::new("only", "Only Command")], cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
open(&mut host);
|
|
||||||
draw(&mut host.visual);
|
|
||||||
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-command-0").is_some());
|
|
||||||
assert!(host.visual.debug_bounds("test-palette-command-1").is_none());
|
|
||||||
|
|
||||||
host.visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut host.visual);
|
|
||||||
assert_eq!(*host.executed.borrow(), vec![SharedString::from("only")]);
|
|
||||||
}
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, SharedString,
|
|
||||||
Styled as _, TestAppContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::command_palette::{Command, CommandPalette, CommandPaletteState};
|
|
||||||
use gpui_native_core::init;
|
|
||||||
|
|
||||||
fn command(label: &'static str) -> Command {
|
|
||||||
Command::new(SharedString::from(label), SharedString::from(label))
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
palette: Entity<CommandPaletteState>,
|
|
||||||
renders: Rc<RefCell<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
*self.renders.borrow_mut() += 1;
|
|
||||||
div().size_full().child(self.palette.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn typing_with_the_palette_open_is_not_pathologically_slow() {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
cx.update(init);
|
|
||||||
let (state, renders, window) = cx.update(|cx| {
|
|
||||||
let renders = Rc::new(RefCell::new(0));
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<CommandPaletteState>>>> =
|
|
||||||
Rc::new(RefCell::new(None));
|
|
||||||
let renders_for_host = renders.clone();
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx
|
|
||||||
.open_window(gpui::WindowOptions::default(), |window, cx| {
|
|
||||||
let matcher = |query: &str, commands: &[Command]| {
|
|
||||||
let query = query.to_lowercase();
|
|
||||||
commands
|
|
||||||
.iter()
|
|
||||||
.filter(|command| command.label.to_lowercase().contains(&query))
|
|
||||||
.cloned()
|
|
||||||
.collect()
|
|
||||||
};
|
|
||||||
let state = cx.new(|cx| {
|
|
||||||
CommandPaletteState::new(
|
|
||||||
CommandPalette::new(
|
|
||||||
"bench-palette",
|
|
||||||
vec![
|
|
||||||
command("Open File"),
|
|
||||||
command("Close Tab"),
|
|
||||||
command("Save All"),
|
|
||||||
command("Toggle Theme"),
|
|
||||||
],
|
|
||||||
matcher,
|
|
||||||
),
|
|
||||||
window,
|
|
||||||
cx,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
let state_for_host = state.clone();
|
|
||||||
cell.borrow_mut().replace(state.clone());
|
|
||||||
cx.new(|_| Host {
|
|
||||||
palette: state_for_host,
|
|
||||||
renders: renders_for_host,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
(state, renders, window)
|
|
||||||
});
|
|
||||||
let mut visual = gpui::VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
state.update(cx, |state, cx| state.open(window, cx));
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
// The open palette must sit idle on executor timers, never frame requests.
|
|
||||||
let frames = visual.update(|window, cx| window.simulate_next_frame(cx));
|
|
||||||
assert_eq!(
|
|
||||||
frames, 0,
|
|
||||||
"an idle open palette must not request animation frames"
|
|
||||||
);
|
|
||||||
|
|
||||||
for letter in "abcdefghijklmnopqrstuvwxyz".chars() {
|
|
||||||
visual.simulate_keystrokes(&letter.to_string());
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let total_renders = *renders.borrow();
|
|
||||||
println!(
|
|
||||||
"26 real keystrokes with the palette open: {total_renders} host renders ({:.2} per keystroke)",
|
|
||||||
total_renders as f32 / 26.
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
total_renders >= 26,
|
|
||||||
"every keystroke must redraw the page; got {total_renders} renders"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
total_renders <= 26 * 3,
|
|
||||||
"typing caused {total_renders} host renders; healthy filtering stays under 3 per keystroke"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, IntoElement, MouseButton, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::context_menu::ContextMenu;
|
|
||||||
use gpui_native_core::menu::MenuItem;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
type Actions = Rc<RefCell<Vec<&'static str>>>;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
actions: Actions,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let actions = self.actions.clone();
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
ContextMenu::new("ctx")
|
|
||||||
.target(|_, _| div().h(px(40.)).w(px(200.)).child("Row").into_any_element())
|
|
||||||
.items({
|
|
||||||
let actions = actions.clone();
|
|
||||||
move |_, _| {
|
|
||||||
vec![
|
|
||||||
MenuItem::new("Rename").handler({
|
|
||||||
let actions = actions.clone();
|
|
||||||
move |_, _| actions.borrow_mut().push("rename")
|
|
||||||
}),
|
|
||||||
MenuItem::new("Delete").handler({
|
|
||||||
let actions = actions.clone();
|
|
||||||
move |_, _| actions.borrow_mut().push("delete")
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The target row rides the `p_4` band: `16..216` horizontally, `16..56`
|
|
||||||
/// vertically, so right-clicks land near `(100, 30)`.
|
|
||||||
fn context_window() -> (TestAppContext, VisualTestContext, Actions) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let actions = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_actions = actions.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
actions: harness_actions,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, actions)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn right_click(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
|
||||||
visual.simulate_mouse_down(position, MouseButton::Right, gpui::Modifiers::default());
|
|
||||||
visual.simulate_mouse_up(position, MouseButton::Right, gpui::Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn click(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
|
||||||
visual.simulate_mouse_down(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
visual.simulate_mouse_up(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(visual: &mut VisualTestContext) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn right_click_opens_the_menu_and_items_activate() {
|
|
||||||
let (_cx, mut visual, actions) = context_window();
|
|
||||||
|
|
||||||
right_click(&mut visual, point(px(100.), px(30.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
// A pointer open arms no row, so Enter alone fires nothing.
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*actions.borrow(), Vec::<&str>::new());
|
|
||||||
|
|
||||||
// The first down arms Rename; enter fires it and closes the menu.
|
|
||||||
visual.simulate_keystrokes("down enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*actions.borrow(), vec!["rename"]);
|
|
||||||
|
|
||||||
// The menu closed with the choice, so Enter reports nothing more.
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*actions.borrow(), vec!["rename"]);
|
|
||||||
|
|
||||||
// Right-click again reopens; two downs reach the second item.
|
|
||||||
right_click(&mut visual, point(px(100.), px(30.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("down down");
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*actions.borrow(), vec!["rename", "delete"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_left_click_on_the_target_does_not_open_the_menu() {
|
|
||||||
let (_cx, mut visual, actions) = context_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(100.), px(30.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*actions.borrow(), Vec::<&str>::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_click_outside_closes_the_menu() {
|
|
||||||
let (_cx, mut visual, actions) = context_window();
|
|
||||||
|
|
||||||
right_click(&mut visual, point(px(100.), px(30.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
click(&mut visual, point(px(450.), px(300.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*actions.borrow(), Vec::<&str>::new());
|
|
||||||
|
|
||||||
// A right-click outside the target never opens it either.
|
|
||||||
right_click(&mut visual, point(px(450.), px(300.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*actions.borrow(), Vec::<&str>::new());
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
use gpui::{
|
|
||||||
Entity, InteractiveElement as _, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::button::{Button, ButtonVariant};
|
|
||||||
use gpui_native_core::input::{Input, InputState};
|
|
||||||
use gpui_native_theme::ActiveTheme as _;
|
|
||||||
use gpui_native_theme::{BORDER_WIDTH, Theme};
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
state: Entity<InputState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(
|
|
||||||
&mut self,
|
|
||||||
_window: &mut Window,
|
|
||||||
_cx: &mut gpui::Context<Self>,
|
|
||||||
) -> impl gpui::IntoElement {
|
|
||||||
div()
|
|
||||||
.flex()
|
|
||||||
.flex_col()
|
|
||||||
.gap_4()
|
|
||||||
.child(
|
|
||||||
div().debug_selector(|| "button-wrapper".to_string()).child(
|
|
||||||
Button::new("cmp-button")
|
|
||||||
.variant(ButtonVariant::Secondary)
|
|
||||||
.label("Medium"),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.debug_selector(|| "input-wrapper".to_string())
|
|
||||||
.child(Input::new("cmp-input", self.state.clone())),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The medium input and button render at the stated height plus the
|
|
||||||
/// hairline border sized outside it, so the two match wherever the platform
|
|
||||||
/// states equal values (macOS, Windows, GNOME) and stay equal to each other
|
|
||||||
/// on every platform.
|
|
||||||
#[gpui::test]
|
|
||||||
fn controls_render_at_the_stated_heights(cx: &mut TestAppContext) {
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let state = cx.update(|cx| InputState::new_with_text("", cx));
|
|
||||||
let state_for_window = state.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
state: state_for_window,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let button = visual
|
|
||||||
.debug_bounds("button-wrapper")
|
|
||||||
.expect("button rendered");
|
|
||||||
let input = visual
|
|
||||||
.debug_bounds("input-wrapper")
|
|
||||||
.expect("input rendered");
|
|
||||||
|
|
||||||
// Both bounds are snapped to device pixels, so allow half a pixel. The
|
|
||||||
// stated height is the interior; the border sizes outside it.
|
|
||||||
let input_height = cx.update(|cx| cx.theme().sizes.medium.input_height) + BORDER_WIDTH * 2.0;
|
|
||||||
let button_height = cx.update(|cx| cx.theme().sizes.medium.control_height) + BORDER_WIDTH * 2.0;
|
|
||||||
assert!((input.size.height - input_height).abs() <= gpui::px(0.5));
|
|
||||||
assert!((button.size.height - button_height).abs() <= gpui::px(0.5));
|
|
||||||
}
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, IntoElement, Modifiers, MouseButton, ParentElement as _, Render, Styled as _,
|
|
||||||
TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::button::{Button, ButtonVariant};
|
|
||||||
use gpui_native_core::dialog::Dialog;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
open: bool,
|
|
||||||
alert: bool,
|
|
||||||
changes: Rc<RefCell<Vec<bool>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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 on_change_view = view.clone();
|
|
||||||
let close_changes = self.changes.clone();
|
|
||||||
let close_view = view.clone();
|
|
||||||
div().size_full().child(
|
|
||||||
Dialog::new("test-dialog")
|
|
||||||
.open(self.open)
|
|
||||||
.alert(self.alert)
|
|
||||||
.title("Delete file?")
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
eprintln!("HARNESS on_change: {next}");
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
on_change_view.update(cx, |harness, cx| {
|
|
||||||
harness.open = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.content(|_, _| {
|
|
||||||
div()
|
|
||||||
.w(px(300.))
|
|
||||||
.h(px(80.))
|
|
||||||
.child("This cannot be undone.")
|
|
||||||
.into_any_element()
|
|
||||||
})
|
|
||||||
.actions(move |_, _| {
|
|
||||||
vec![
|
|
||||||
Button::new("dialog-cancel")
|
|
||||||
.label("Cancel")
|
|
||||||
.variant(ButtonVariant::Secondary)
|
|
||||||
.on_click({
|
|
||||||
let close_changes = close_changes.clone();
|
|
||||||
let close_view = close_view.clone();
|
|
||||||
move |_, _, cx| {
|
|
||||||
close_changes.borrow_mut().push(false);
|
|
||||||
close_view.update(cx, |harness, cx| {
|
|
||||||
harness.open = false;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.into_any_element(),
|
|
||||||
Button::new("dialog-delete")
|
|
||||||
.label("Delete")
|
|
||||||
.variant(ButtonVariant::Destructive)
|
|
||||||
.on_click({
|
|
||||||
let close_changes = close_changes.clone();
|
|
||||||
let close_view = close_view.clone();
|
|
||||||
move |_, _, cx| {
|
|
||||||
close_changes.borrow_mut().push(false);
|
|
||||||
close_view.update(cx, |harness, cx| {
|
|
||||||
harness.open = false;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.into_any_element(),
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An open 332px-wide dialog centered in the window: the panel spans
|
|
||||||
/// roughly `(794..1126, 448..640)`, so `(100, 100)` is on the scrim and
|
|
||||||
/// the window center is on the panel.
|
|
||||||
fn dialog_window(
|
|
||||||
open: bool,
|
|
||||||
alert: bool,
|
|
||||||
) -> (TestAppContext, VisualTestContext, Rc<RefCell<Vec<bool>>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
open,
|
|
||||||
alert,
|
|
||||||
changes: harness_changes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(visual: &mut VisualTestContext) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
eprintln!("DRAW");
|
|
||||||
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 escape_closes_a_modal_dialog_and_stays_closed() {
|
|
||||||
let (_cx, mut visual, changes) = dialog_window(true, false);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("escape");
|
|
||||||
assert_eq!(*changes.borrow(), vec![false]);
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
// Closed: escape on the inert remainder reports nothing further.
|
|
||||||
visual.simulate_keystrokes("escape");
|
|
||||||
assert_eq!(*changes.borrow(), vec![false]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn escape_is_inert_on_an_alert() {
|
|
||||||
let (_cx, mut visual, changes) = dialog_window(true, true);
|
|
||||||
visual.simulate_keystrokes("escape");
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn scrim_click_closes_a_modal_but_not_an_alert() {
|
|
||||||
let (_cx, mut visual, changes) = dialog_window(true, false);
|
|
||||||
click(&mut visual, point(px(100.), px(100.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![false]);
|
|
||||||
|
|
||||||
draw(&mut visual);
|
|
||||||
click(&mut visual, point(px(100.), px(100.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![false]);
|
|
||||||
|
|
||||||
// The same click on an alert's scrim answers nothing.
|
|
||||||
let (_cx, mut visual, changes) = dialog_window(true, true);
|
|
||||||
click(&mut visual, point(px(100.), px(100.)));
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_panel_does_not_dismiss() {
|
|
||||||
// The window center is where the centered panel sits.
|
|
||||||
let (_cx, mut visual, changes) = dialog_window(true, false);
|
|
||||||
click(&mut visual, point(px(960.), px(540.)));
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clicking the close button reports close like escape and the scrim; the
|
|
||||||
/// panel then unmounts the button with the rest of the dialog.
|
|
||||||
#[test]
|
|
||||||
fn the_close_button_reports_close_and_disappears() {
|
|
||||||
let (_cx, mut visual, changes) = dialog_window(true, false);
|
|
||||||
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("dialog-close")
|
|
||||||
.expect("the close button rendered");
|
|
||||||
click(&mut visual, bounds.center());
|
|
||||||
assert_eq!(*changes.borrow(), vec![false]);
|
|
||||||
|
|
||||||
draw(&mut visual);
|
|
||||||
assert!(visual.debug_bounds("dialog-close").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An alert answers only through its actions, so it renders no close button.
|
|
||||||
#[test]
|
|
||||||
fn an_alert_hides_the_close_button() {
|
|
||||||
let (_cx, mut visual, _changes) = dialog_window(true, true);
|
|
||||||
assert!(visual.debug_bounds("dialog-close").is_none());
|
|
||||||
}
|
|
||||||
@@ -1,457 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke,
|
|
||||||
Modifiers, MouseButton, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::button::ButtonVariant;
|
|
||||||
use gpui_native_core::dropdown_menu::DropdownMenu;
|
|
||||||
use gpui_native_core::menu::MenuItem;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
open: bool,
|
|
||||||
split: bool,
|
|
||||||
disabled: bool,
|
|
||||||
changes: Rc<RefCell<Vec<bool>>>,
|
|
||||||
clicks: Rc<RefCell<usize>>,
|
|
||||||
activated: Rc<RefCell<Vec<&'static str>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let view = cx.entity();
|
|
||||||
let changes = self.changes.clone();
|
|
||||||
let clicks = self.clicks.clone();
|
|
||||||
let activated = self.activated.clone();
|
|
||||||
let mut trigger = DropdownMenu::new("test-dd")
|
|
||||||
.label("Save")
|
|
||||||
.variant(ButtonVariant::Secondary)
|
|
||||||
.open(self.open)
|
|
||||||
.on_open_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.open = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.items(move |_, _| {
|
|
||||||
vec![
|
|
||||||
export_item(activated.clone(), "png"),
|
|
||||||
export_item(activated.clone(), "pdf"),
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
if self.disabled {
|
|
||||||
trigger = trigger.disabled(true);
|
|
||||||
}
|
|
||||||
if self.split {
|
|
||||||
trigger = trigger.on_click(move |_, _, _| *clicks.borrow_mut() += 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
div().size_full().child(trigger)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn export_item(activated: Rc<RefCell<Vec<&'static str>>>, format: &'static str) -> MenuItem {
|
|
||||||
MenuItem::new(format).handler(move |_, _| activated.borrow_mut().push(format))
|
|
||||||
}
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<bool>>>;
|
|
||||||
type Clicks = Rc<RefCell<usize>>;
|
|
||||||
type Activated = Rc<RefCell<Vec<&'static str>>>;
|
|
||||||
|
|
||||||
/// A maximized window whose trigger starts in the top-left corner, so the
|
|
||||||
/// main zone is under the cursor at the button's usual tap position and the
|
|
||||||
/// chevron zone is found through its debug bounds.
|
|
||||||
fn dropdown_window(
|
|
||||||
split: bool,
|
|
||||||
disabled: bool,
|
|
||||||
) -> (
|
|
||||||
TestAppContext,
|
|
||||||
VisualTestContext,
|
|
||||||
AnyWindowHandle,
|
|
||||||
Changes,
|
|
||||||
Clicks,
|
|
||||||
Activated,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let clicks = Rc::new(RefCell::new(0));
|
|
||||||
let activated = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let harness_clicks = clicks.clone();
|
|
||||||
let harness_activated = activated.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
open: false,
|
|
||||||
split,
|
|
||||||
disabled,
|
|
||||||
changes: harness_changes,
|
|
||||||
clicks: harness_clicks,
|
|
||||||
activated: harness_activated,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
(cx, visual, window, changes, clicks, activated)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(visual: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
visual
|
|
||||||
.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The painted center of an element found through its debug selector.
|
|
||||||
fn center(visual: &mut VisualTestContext, selector: &'static str) -> gpui::Point<gpui::Pixels> {
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds(selector)
|
|
||||||
.unwrap_or_else(|| panic!("{selector} should have been laid out"));
|
|
||||||
point(
|
|
||||||
bounds.origin.x + bounds.size.width / 2.,
|
|
||||||
bounds.origin.y + bounds.size.height / 2.,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Split form: clicking the main zone fires the action and the menu stays
|
|
||||||
/// closed — the button keeps its normal behavior under a child menu.
|
|
||||||
#[test]
|
|
||||||
fn action_button_fires_and_does_not_open() {
|
|
||||||
let (_cx, mut visual, window, changes, clicks, _activated) = dropdown_window(true, false);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(30.), px(14.)));
|
|
||||||
assert_eq!(*clicks.borrow(), 1, "the main zone fires on_click");
|
|
||||||
assert!(
|
|
||||||
changes.borrow().is_empty(),
|
|
||||||
"the menu must stay closed: {:?}",
|
|
||||||
changes.borrow()
|
|
||||||
);
|
|
||||||
|
|
||||||
draw(&mut visual, window);
|
|
||||||
assert!(visual.debug_bounds("menu-panel").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Split form: only the chevron zone opens the menu, and an activated row
|
|
||||||
/// closes it.
|
|
||||||
#[test]
|
|
||||||
fn chevron_opens_and_row_activation_closes() {
|
|
||||||
let (_cx, mut visual, window, changes, clicks, activated) = dropdown_window(true, false);
|
|
||||||
|
|
||||||
let chevron = center(&mut visual, "test-dd-chevron");
|
|
||||||
click(&mut visual, chevron);
|
|
||||||
assert_eq!(*changes.borrow(), vec![true]);
|
|
||||||
|
|
||||||
draw(&mut visual, window);
|
|
||||||
let row = center(&mut visual, "menu-item-0");
|
|
||||||
click(&mut visual, row);
|
|
||||||
assert_eq!(*activated.borrow(), vec!["png"]);
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false]);
|
|
||||||
assert_eq!(*clicks.borrow(), 0, "row activation never fires on_click");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Menu-only form: the whole button opens the menu on click, like a menu
|
|
||||||
/// button.
|
|
||||||
#[test]
|
|
||||||
fn plain_button_activates_the_menu() {
|
|
||||||
let (_cx, mut visual, _window, changes, clicks, _activated) = dropdown_window(false, false);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(30.), px(14.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![true]);
|
|
||||||
assert_eq!(*clicks.borrow(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Keyboard focus returns to the trigger when the menu closes, so the down
|
|
||||||
/// arrow opens it again.
|
|
||||||
#[test]
|
|
||||||
fn escape_returns_focus_to_the_trigger() {
|
|
||||||
let (_cx, mut visual, window, changes, _clicks, _activated) = dropdown_window(true, false);
|
|
||||||
|
|
||||||
let chevron = center(&mut visual, "test-dd-chevron");
|
|
||||||
click(&mut visual, chevron);
|
|
||||||
draw(&mut visual, window);
|
|
||||||
visual.simulate_keystrokes("escape");
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false]);
|
|
||||||
|
|
||||||
draw(&mut visual, window);
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![true, false, true],
|
|
||||||
"the focused trigger reopened the menu"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Enter and Space fire the split button's action while it is focused, like
|
|
||||||
/// any push button.
|
|
||||||
#[test]
|
|
||||||
fn enter_and_space_fire_the_action_while_focused() {
|
|
||||||
let (_cx, mut visual, window, changes, clicks, _activated) = dropdown_window(true, false);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(30.), px(14.)));
|
|
||||||
draw(&mut visual, window);
|
|
||||||
|
|
||||||
for key in ["enter", "space"] {
|
|
||||||
visual.simulate_event(KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
});
|
|
||||||
visual.simulate_event(KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*clicks.borrow(), 3, "the pointer click, Enter, and Space");
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Enter opens the menu while a menu-only trigger is focused.
|
|
||||||
#[test]
|
|
||||||
fn enter_opens_the_menu_only_trigger() {
|
|
||||||
let (_cx, mut visual, window, changes, _clicks, _activated) = dropdown_window(false, false);
|
|
||||||
|
|
||||||
// Opening hands focus to the panel; dismissing hands it back to the
|
|
||||||
// trigger, whose Enter then reopens the menu.
|
|
||||||
click(&mut visual, point(px(30.), px(14.)));
|
|
||||||
draw(&mut visual, window);
|
|
||||||
visual.simulate_keystrokes("escape");
|
|
||||||
draw(&mut visual, window);
|
|
||||||
|
|
||||||
{
|
|
||||||
let key = "enter";
|
|
||||||
visual.simulate_event(KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
});
|
|
||||||
visual.simulate_event(KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false, true]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Action rows sit a half-step apart so the rounded selection highlight
|
|
||||||
/// reads as its own chip instead of a continuous block.
|
|
||||||
#[test]
|
|
||||||
fn action_rows_sit_a_half_step_apart() {
|
|
||||||
let (_cx, mut visual, window, _changes, _clicks, _activated) = dropdown_window(true, false);
|
|
||||||
|
|
||||||
let chevron = center(&mut visual, "test-dd-chevron");
|
|
||||||
click(&mut visual, chevron);
|
|
||||||
draw(&mut visual, window);
|
|
||||||
|
|
||||||
let first = visual
|
|
||||||
.debug_bounds("menu-item-0")
|
|
||||||
.expect("first row should have been laid out");
|
|
||||||
let second = visual
|
|
||||||
.debug_bounds("menu-item-1")
|
|
||||||
.expect("second row should have been laid out");
|
|
||||||
let gap = second.origin.y - (first.origin.y + first.size.height);
|
|
||||||
// Painted bounds are snapped to device pixels; allow half a pixel.
|
|
||||||
assert!(
|
|
||||||
(gap - px(2.)).abs() <= px(0.5),
|
|
||||||
"rows should sit 2px apart, got {gap:?}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A pointer-opened menu arms no row, so enter cannot fire one; the first
|
|
||||||
/// down arms a row and enter fires it.
|
|
||||||
#[test]
|
|
||||||
fn pointer_opens_arm_nothing() {
|
|
||||||
let (_cx, mut visual, window, changes, _clicks, activated) = dropdown_window(true, false);
|
|
||||||
|
|
||||||
let chevron = center(&mut visual, "test-dd-chevron");
|
|
||||||
click(&mut visual, chevron);
|
|
||||||
draw(&mut visual, window);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
assert!(
|
|
||||||
activated.borrow().is_empty(),
|
|
||||||
"no row is armed on pointer open"
|
|
||||||
);
|
|
||||||
assert_eq!(*changes.borrow(), vec![true], "the menu stays open");
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("down enter");
|
|
||||||
assert_eq!(*activated.borrow(), vec!["png"]);
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A keyboard-opened menu (down arrow from the focused trigger) arms the
|
|
||||||
/// first selectable row, so enter fires it right away.
|
|
||||||
#[test]
|
|
||||||
fn keyboard_opens_arm_the_first_row() {
|
|
||||||
let (_cx, mut visual, window, changes, _clicks, activated) = dropdown_window(false, false);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(30.), px(14.)));
|
|
||||||
draw(&mut visual, window);
|
|
||||||
visual.simulate_keystrokes("escape");
|
|
||||||
draw(&mut visual, window);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
draw(&mut visual, window);
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
assert_eq!(*activated.borrow(), vec!["png"]);
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false, true, false]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A disabled trigger ignores both zones.
|
|
||||||
#[test]
|
|
||||||
fn disabled_trigger_ignores_clicks() {
|
|
||||||
let (_cx, mut visual, window, changes, clicks, _activated) = dropdown_window(true, true);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(30.), px(14.)));
|
|
||||||
let chevron = center(&mut visual, "test-dd-chevron");
|
|
||||||
click(&mut visual, chevron);
|
|
||||||
draw(&mut visual, window);
|
|
||||||
|
|
||||||
assert_eq!(*clicks.borrow(), 0);
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
assert!(visual.debug_bounds("menu-panel").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
struct PairHarness {
|
|
||||||
open_a: bool,
|
|
||||||
open_b: bool,
|
|
||||||
changes_a: Changes,
|
|
||||||
changes_b: Changes,
|
|
||||||
activated_a: Activated,
|
|
||||||
activated_b: Activated,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for PairHarness {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let (changes_a, changes_b) = (self.changes_a.clone(), self.changes_b.clone());
|
|
||||||
let (activated_a, activated_b) = (self.activated_a.clone(), self.activated_b.clone());
|
|
||||||
|
|
||||||
let menu_a = DropdownMenu::new("pair-a")
|
|
||||||
.label("Alpha")
|
|
||||||
.variant(ButtonVariant::Secondary)
|
|
||||||
.open(self.open_a)
|
|
||||||
.on_open_change({
|
|
||||||
let view = cx.entity();
|
|
||||||
let changes = changes_a.clone();
|
|
||||||
move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.open_a = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.items(move |_, _| {
|
|
||||||
vec![
|
|
||||||
MenuItem::new("png").handler({
|
|
||||||
let activated = activated_a.clone();
|
|
||||||
move |_, _| activated.borrow_mut().push("png")
|
|
||||||
}),
|
|
||||||
MenuItem::new("pdf").handler({
|
|
||||||
let activated = activated_a.clone();
|
|
||||||
move |_, _| activated.borrow_mut().push("pdf")
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
let menu_b = DropdownMenu::new("pair-b")
|
|
||||||
.label("Beta")
|
|
||||||
.variant(ButtonVariant::Secondary)
|
|
||||||
.open(self.open_b)
|
|
||||||
.on_open_change({
|
|
||||||
let view = cx.entity();
|
|
||||||
let changes = changes_b.clone();
|
|
||||||
move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.open_b = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.items(move |_, _| {
|
|
||||||
vec![
|
|
||||||
MenuItem::new("zip").handler({
|
|
||||||
let activated = activated_b.clone();
|
|
||||||
move |_, _| activated.borrow_mut().push("zip")
|
|
||||||
}),
|
|
||||||
MenuItem::new("7z").handler({
|
|
||||||
let activated = activated_b.clone();
|
|
||||||
move |_, _| activated.borrow_mut().push("7z")
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
div().size_full().flex().gap_2().child(menu_a).child(menu_b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn pair_window() -> (
|
|
||||||
VisualTestContext,
|
|
||||||
AnyWindowHandle,
|
|
||||||
Changes,
|
|
||||||
Changes,
|
|
||||||
Activated,
|
|
||||||
Activated,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes_a = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let changes_b = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let activated_a = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let activated_b = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let (changes_a_for_window, changes_b_for_window) = (changes_a.clone(), changes_b.clone());
|
|
||||||
let (activated_a_for_window, activated_b_for_window) =
|
|
||||||
(activated_a.clone(), activated_b.clone());
|
|
||||||
let window = cx.add_window(move |_, _| PairHarness {
|
|
||||||
open_a: false,
|
|
||||||
open_b: false,
|
|
||||||
changes_a: changes_a_for_window,
|
|
||||||
changes_b: changes_b_for_window,
|
|
||||||
activated_a: activated_a_for_window,
|
|
||||||
activated_b: activated_b_for_window,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(
|
|
||||||
VisualTestContext::from_window(window, &cx),
|
|
||||||
window,
|
|
||||||
changes_a,
|
|
||||||
changes_b,
|
|
||||||
activated_a,
|
|
||||||
activated_b,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// With a closed sibling dropdown on screen, a pointer-opened menu must still
|
|
||||||
/// arm rows with the keyboard: the menu's open/selection state is per
|
|
||||||
/// instance, not shared keyed state.
|
|
||||||
#[test]
|
|
||||||
fn sibling_dropdowns_keep_independent_menu_state() {
|
|
||||||
let (mut visual, window, changes_a, changes_b, activated_a, activated_b) = pair_window();
|
|
||||||
|
|
||||||
// Menu A sits at the window's top-left; menu B stays closed beside it.
|
|
||||||
click(&mut visual, point(px(30.), px(14.)));
|
|
||||||
assert_eq!(*changes_a.borrow(), vec![true]);
|
|
||||||
draw(&mut visual, window);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("down enter");
|
|
||||||
assert_eq!(*activated_a.borrow(), vec!["png"]);
|
|
||||||
assert_eq!(*changes_a.borrow(), vec![true, false]);
|
|
||||||
assert!(changes_b.borrow().is_empty(), "B must stay closed");
|
|
||||||
assert!(activated_b.borrow().is_empty());
|
|
||||||
}
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
use gpui::{
|
|
||||||
AppContext as _, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::button::{Button, ButtonVariant};
|
|
||||||
use gpui_native_core::field::{Field, FieldVariant};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct FieldHost {
|
|
||||||
variant: FieldVariant,
|
|
||||||
with_error: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for FieldHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut gpui::Context<Self>) -> impl IntoElement {
|
|
||||||
let mut field = Field::new("test-field", "Server URL")
|
|
||||||
.description("Where patches are sent.")
|
|
||||||
.variant(self.variant)
|
|
||||||
.child(
|
|
||||||
Button::new("test-field-button")
|
|
||||||
.label("Edit")
|
|
||||||
.variant(ButtonVariant::Ghost),
|
|
||||||
);
|
|
||||||
if self.with_error {
|
|
||||||
field = field.error(Some("Required"));
|
|
||||||
}
|
|
||||||
|
|
||||||
div().size_full().p_4().child(field)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn field_window(
|
|
||||||
variant: FieldVariant,
|
|
||||||
with_error: bool,
|
|
||||||
) -> (TestAppContext, gpui::AnyWindowHandle) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let window = cx.add_window(move |_, _| FieldHost {
|
|
||||||
variant,
|
|
||||||
with_error,
|
|
||||||
});
|
|
||||||
let window: gpui::AnyWindowHandle = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(cx, window)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn block_stacks_the_label_above_the_control() {
|
|
||||||
let (cx, window) = field_window(FieldVariant::Block, false);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
let label = visual
|
|
||||||
.debug_bounds("test-field-label")
|
|
||||||
.expect("label missing");
|
|
||||||
let control = visual
|
|
||||||
.debug_bounds("test-field-control")
|
|
||||||
.expect("control missing");
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
label.bottom() <= control.top(),
|
|
||||||
"label must sit above the control: {label:?} vs {control:?}"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
(label.left() - control.left()).abs() < gpui::px(1.),
|
|
||||||
"label and control must share the left edge"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn row_leads_with_the_label_and_trails_the_control() {
|
|
||||||
let (cx, window) = field_window(FieldVariant::Row, false);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
let label = visual
|
|
||||||
.debug_bounds("test-field-label")
|
|
||||||
.expect("label missing");
|
|
||||||
let control = visual
|
|
||||||
.debug_bounds("test-field-control")
|
|
||||||
.expect("control missing");
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
control.left() >= label.right(),
|
|
||||||
"the control must trail the label: {label:?} vs {control:?}"
|
|
||||||
);
|
|
||||||
let label_center = (label.top() + label.bottom()) / 2.;
|
|
||||||
let control_center = (control.top() + control.bottom()) / 2.;
|
|
||||||
assert!(
|
|
||||||
(label_center - control_center).abs() < gpui::px(1.5),
|
|
||||||
"the control must center on the title line: {label:?} vs {control:?}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_error_renders_under_the_control_only_when_set() {
|
|
||||||
let (cx, window) = field_window(FieldVariant::Block, false);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("test-field-error").is_none(),
|
|
||||||
"no error must render when none is set"
|
|
||||||
);
|
|
||||||
|
|
||||||
let (cx, window) = field_window(FieldVariant::Block, true);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
let error = visual
|
|
||||||
.debug_bounds("test-field-error")
|
|
||||||
.expect("error missing");
|
|
||||||
let control = visual
|
|
||||||
.debug_bounds("test-field-control")
|
|
||||||
.expect("control missing");
|
|
||||||
assert!(
|
|
||||||
error.top() >= control.bottom(),
|
|
||||||
"the error must sit under the control: {control:?} vs {error:?}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
use std::{cell::Cell, rc::Rc, time::Duration};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, ParentElement as _, Render, SharedString,
|
|
||||||
Styled as _, TestAppContext, VisualTestContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::common::v_flex;
|
|
||||||
use gpui_native_core::field::Field;
|
|
||||||
use gpui_native_core::init;
|
|
||||||
use gpui_native_core::input::{Input, InputState};
|
|
||||||
|
|
||||||
struct FieldHost {
|
|
||||||
url: Entity<InputState>,
|
|
||||||
renders: Rc<Cell<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for FieldHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
self.renders.set(self.renders.get() + 1);
|
|
||||||
let url_error = self
|
|
||||||
.url
|
|
||||||
.read(cx)
|
|
||||||
.value()
|
|
||||||
.is_empty()
|
|
||||||
.then_some("Required")
|
|
||||||
.map(SharedString::from);
|
|
||||||
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
v_flex().gap_3().child(
|
|
||||||
Field::new("bench-url", "Server URL")
|
|
||||||
.description("Where your patches are sent.")
|
|
||||||
.error(url_error)
|
|
||||||
.child(Input::new("bench-url-input", self.url.clone())),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A field page in a real window; the url input is focused unless
|
|
||||||
/// `focus` is false, which leaves the page truly idle.
|
|
||||||
fn field_host(
|
|
||||||
focus: bool,
|
|
||||||
) -> (
|
|
||||||
TestAppContext,
|
|
||||||
Entity<InputState>,
|
|
||||||
VisualTestContext,
|
|
||||||
Rc<Cell<usize>>,
|
|
||||||
) {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
cx.update(init);
|
|
||||||
let renders = Rc::new(Cell::new(0));
|
|
||||||
let renders_for_host = renders.clone();
|
|
||||||
let (url, window) = cx.update(|cx| {
|
|
||||||
let url = InputState::new(cx);
|
|
||||||
let url_for_host = url.clone();
|
|
||||||
let window = cx
|
|
||||||
.open_window(gpui::WindowOptions::default(), |_, cx| {
|
|
||||||
cx.new(|_| FieldHost {
|
|
||||||
url: url_for_host,
|
|
||||||
renders: renders_for_host.clone(),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(url, window)
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
if focus {
|
|
||||||
let focus = url.read(cx).focus_handle();
|
|
||||||
window.focus(&focus, cx);
|
|
||||||
}
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, url, visual, renders)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn an_idle_field_page_schedules_no_frames() {
|
|
||||||
let (_cx, _url, mut visual, _renders) = field_host(false);
|
|
||||||
let frames = visual.update(|window, cx| window.simulate_next_frame(cx));
|
|
||||||
assert_eq!(
|
|
||||||
frames, 0,
|
|
||||||
"an idle unfocused field page must not request animation frames"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The caret blink is fps-capped: it must never demand a next frame, and its
|
|
||||||
/// redraws arrive on the executor timer at the blink cadence.
|
|
||||||
#[test]
|
|
||||||
fn a_focused_field_blink_is_fps_capped_and_still_redraws() {
|
|
||||||
let (cx, _url, mut visual, renders) = field_host(true);
|
|
||||||
let renders_after_focus = renders.get();
|
|
||||||
|
|
||||||
let frames = visual.update(|window, cx| window.simulate_next_frame(cx));
|
|
||||||
assert_eq!(
|
|
||||||
frames, 0,
|
|
||||||
"the blink must be fps-capped, not per-frame scheduled"
|
|
||||||
);
|
|
||||||
|
|
||||||
// The blink redraws through the executor timer (4 fps cap).
|
|
||||||
cx.executor().advance_clock(Duration::from_millis(251));
|
|
||||||
cx.run_until_parked();
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
assert!(
|
|
||||||
renders.get() > renders_after_focus,
|
|
||||||
"the capped blink must still redraw its page"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,133 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, InputEvent as _, IntoElement, KeyDownEvent,
|
|
||||||
KeyUpEvent, Keystroke, Modifiers, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::link::Link;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
clicks: Rc<RefCell<usize>>,
|
|
||||||
disabled: bool,
|
|
||||||
with_override: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let clicks = self.clicks.clone();
|
|
||||||
div().size_full().flex().flex_col().gap_2().child({
|
|
||||||
let mut link = Link::new("test-link-href")
|
|
||||||
.label("Release notes")
|
|
||||||
.href("https://example.com/release-notes");
|
|
||||||
if self.with_override {
|
|
||||||
link = link.on_click(move |_, _, _| {
|
|
||||||
*clicks.borrow_mut() += 1;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
link.disabled(self.disabled)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window with two link rows at the top-left; row 1 spans
|
|
||||||
/// roughly y 0..21, row 2 y 29..50.
|
|
||||||
fn link_window(
|
|
||||||
with_override: bool,
|
|
||||||
disabled: bool,
|
|
||||||
) -> (TestAppContext, AnyWindowHandle, Rc<RefCell<usize>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let clicks = Rc::new(RefCell::new(0usize));
|
|
||||||
let harness_clicks = clicks.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
clicks: harness_clicks,
|
|
||||||
disabled,
|
|
||||||
with_override,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(cx, window, clicks)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(cx: &mut TestAppContext, window: AnyWindowHandle) {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_link_with_href_opens_it_through_the_platform() {
|
|
||||||
let (cx, window, clicks) = link_window(false, false);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
visual.simulate_click(point(px(20.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(
|
|
||||||
visual.opened_url().as_deref(),
|
|
||||||
Some("https://example.com/release-notes")
|
|
||||||
);
|
|
||||||
assert_eq!(*clicks.borrow(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn on_click_replaces_the_href_opening() {
|
|
||||||
let (cx, window, clicks) = link_window(true, false);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
visual.simulate_click(point(px(20.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*clicks.borrow(), 1);
|
|
||||||
assert_eq!(visual.opened_url(), None, "the handler replaces the href");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_link_does_nothing() {
|
|
||||||
let (cx, window, clicks) = link_window(false, true);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
visual.simulate_click(point(px(20.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(visual.opened_url(), None);
|
|
||||||
assert_eq!(*clicks.borrow(), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_opens_the_href_while_focused() {
|
|
||||||
let (mut cx, window, clicks) = link_window(false, false);
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
visual.simulate_click(point(px(20.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(
|
|
||||||
visual.opened_url().as_deref(),
|
|
||||||
Some("https://example.com/release-notes"),
|
|
||||||
"the pointer click itself opens"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Repaint with the link focused so its Enter/Space handlers register.
|
|
||||||
draw(&mut cx, window);
|
|
||||||
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.dispatch_event(
|
|
||||||
KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse("enter").unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
}
|
|
||||||
.to_platform_input(),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
window.dispatch_event(
|
|
||||||
KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse("enter").unwrap(),
|
|
||||||
}
|
|
||||||
.to_platform_input(),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert_eq!(*clicks.borrow(), 0, "Enter re-opens through the platform");
|
|
||||||
assert_eq!(
|
|
||||||
visual.opened_url().as_deref(),
|
|
||||||
Some("https://example.com/release-notes")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,523 +0,0 @@
|
|||||||
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<DefiniteLength>,
|
|
||||||
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();
|
|
||||||
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<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 action_item(
|
|
||||||
label: &'static str,
|
|
||||||
key: &'static str,
|
|
||||||
activated: Rc<RefCell<Vec<&'static str>>>,
|
|
||||||
) -> MenuItem {
|
|
||||||
MenuItem::new(label).handler(move |_, _| record(&activated, key))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn submenu_item(activated: Rc<RefCell<Vec<&'static str>>>) -> 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<RefCell<Vec<bool>>>,
|
|
||||||
activated: Rc<RefCell<Vec<&'static str>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for SubmenuHarness {
|
|
||||||
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-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<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,
|
|
||||||
) {
|
|
||||||
menu_window_with(open, None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn menu_window_with(
|
|
||||||
open: bool,
|
|
||||||
min_width: Option<DefiniteLength>,
|
|
||||||
) -> (
|
|
||||||
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,
|
|
||||||
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<gpui::Pixels>) {
|
|
||||||
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<gpui::Pixels>) {
|
|
||||||
visual.simulate_mouse_move(position, None, Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn center(bounds: gpui::Bounds<gpui::Pixels>) -> gpui::Point<gpui::Pixels> {
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,212 +0,0 @@
|
|||||||
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,
|
|
||||||
};
|
|
||||||
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.))
|
|
||||||
.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.))
|
|
||||||
.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:?}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,121 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke,
|
|
||||||
Modifiers, ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window,
|
|
||||||
div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::radio::{Radio, RadioGroup};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
changes: Rc<RefCell<Vec<&'static str>>>,
|
|
||||||
selected: Option<&'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();
|
|
||||||
div().size_full().flex().flex_col().child(
|
|
||||||
RadioGroup::new("test-group")
|
|
||||||
.selected(self.selected)
|
|
||||||
.on_change(move |value, _, cx| {
|
|
||||||
changes.borrow_mut().push(value);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.selected = Some(value);
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.child(Radio::new("radio-a", "a").label("Option A"))
|
|
||||||
.child(Radio::new("radio-b", "b").label("Option B"))
|
|
||||||
.child(
|
|
||||||
Radio::new("radio-off", "c")
|
|
||||||
.label("Disabled")
|
|
||||||
.disabled(true),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window whose radio rows sit at the top-left: row 1 spans
|
|
||||||
/// roughly y 0..21, row 2 y 29..50, so `(8, 10)` and `(8, 40)` hit them.
|
|
||||||
fn group_window(
|
|
||||||
selected: Option<&'static str>,
|
|
||||||
) -> (
|
|
||||||
VisualTestContext,
|
|
||||||
AnyWindowHandle,
|
|
||||||
Rc<RefCell<Vec<&'static str>>>,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
changes: harness_changes,
|
|
||||||
selected,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(VisualTestContext::from_window(window, &cx), window, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(cx: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_reports_values_through_the_controlled_loop() {
|
|
||||||
let (mut cx, _window, changes) = group_window(None);
|
|
||||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec!["a"]);
|
|
||||||
cx.simulate_click(point(px(8.), px(40.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec!["a", "b"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_selected_radio_never_unchecks_it() {
|
|
||||||
let (mut cx, _window, changes) = group_window(Some("a"));
|
|
||||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec!["a"], "re-reports the same value");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_radio_does_not_report_changes() {
|
|
||||||
let (mut cx, _window, changes) = group_window(None);
|
|
||||||
cx.simulate_click(point(px(8.), px(70.)), Modifiers::default());
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_and_space_report_the_focused_radio_value() {
|
|
||||||
let (mut cx, window, changes) = group_window(None);
|
|
||||||
cx.simulate_click(point(px(8.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec!["a"],
|
|
||||||
"the pointer click itself selects"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Repaint with the first radio focused so its Enter/Space handlers register.
|
|
||||||
draw(&mut cx, window);
|
|
||||||
|
|
||||||
for key in ["enter", "space"] {
|
|
||||||
cx.simulate_event(KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
});
|
|
||||||
cx.simulate_event(KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*changes.borrow(), vec!["a", "a", "a"]);
|
|
||||||
}
|
|
||||||
@@ -1,190 +0,0 @@
|
|||||||
use gpui::{
|
|
||||||
Context, InteractiveElement as _, IntoElement, Modifiers, ParentElement as _, Render,
|
|
||||||
ScrollDelta, ScrollEvent, ScrollHandle, SharedString, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::scroll_area::ScrollArea;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
scroll: ScrollHandle,
|
|
||||||
rows: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(
|
|
||||||
ScrollArea::new("test-scroll")
|
|
||||||
.handle(self.scroll.clone())
|
|
||||||
.h(px(200.))
|
|
||||||
.w(px(300.))
|
|
||||||
.child(div().flex().flex_col().children((0..self.rows).map(|row| {
|
|
||||||
div()
|
|
||||||
.h(px(20.))
|
|
||||||
.child(SharedString::from(format!("row {row}")))
|
|
||||||
}))),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A 300×200 scroll area over `rows` 20px rows, with the handle the test
|
|
||||||
/// can inspect directly.
|
|
||||||
fn scroll_area_window(rows: usize) -> (TestAppContext, VisualTestContext, ScrollHandle) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let handle = ScrollHandle::new();
|
|
||||||
let handle_for_view = handle.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
scroll: handle_for_view,
|
|
||||||
rows,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, handle)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn wheel_scrolling_moves_the_offset_and_stays_inside_the_content() {
|
|
||||||
let (_cx, mut visual, handle) = scroll_area_window(30);
|
|
||||||
|
|
||||||
// 30 rows × 20px = 600px of content in a 200px viewport.
|
|
||||||
visual.simulate_event(ScrollEvent {
|
|
||||||
position: point(px(100.), px(100.)),
|
|
||||||
delta: ScrollDelta::Pixels(point(px(0.), px(-60.))),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
let offset = handle.offset().y;
|
|
||||||
assert_eq!(offset, px(-60.));
|
|
||||||
|
|
||||||
// Wheeling far past the end clamps to the content.
|
|
||||||
for _ in 0..10 {
|
|
||||||
visual.simulate_event(ScrollEvent {
|
|
||||||
position: point(px(100.), px(100.)),
|
|
||||||
delta: ScrollDelta::Pixels(point(px(0.), px(-60.))),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// max_offset = 600 - 200 = 400.
|
|
||||||
assert_eq!(handle.offset().y, px(-400.));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_content_starts_unscrolled_and_horizontal_axes_scroll_x() {
|
|
||||||
let (_cx, mut visual, handle) = scroll_area_window(30);
|
|
||||||
assert_eq!(handle.offset(), gpui::point(px(0.), px(0.)));
|
|
||||||
|
|
||||||
// A vertical-only area ignores horizontal wheel deltas.
|
|
||||||
visual.simulate_event(ScrollEvent {
|
|
||||||
position: point(px(100.), px(100.)),
|
|
||||||
delta: ScrollDelta::Pixels(point(px(-60.), px(0.))),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
assert_eq!(handle.offset().x, px(0.));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The gallery's exact shell: a window-filling root, a window-filling scroll
|
|
||||||
/// area over padded flex-column content that towers past the viewport.
|
|
||||||
struct GalleryShell {
|
|
||||||
scroll: ScrollHandle,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for GalleryShell {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(
|
|
||||||
ScrollArea::new("gallery-scroll")
|
|
||||||
.handle(self.scroll.clone())
|
|
||||||
.size_full()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.p_6()
|
|
||||||
.flex()
|
|
||||||
.flex_col()
|
|
||||||
.gap_5()
|
|
||||||
.children((0..12).map(|section| {
|
|
||||||
div()
|
|
||||||
.h(px(220.))
|
|
||||||
.child(SharedString::from(format!("section {section}")))
|
|
||||||
})),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A window-filling scroll area with `p_4`, over one full-width row.
|
|
||||||
struct PaddedShell {
|
|
||||||
scroll: ScrollHandle,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for PaddedShell {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.debug_selector(|| "padded-root".to_string())
|
|
||||||
.child(
|
|
||||||
ScrollArea::new("padded-scroll")
|
|
||||||
.handle(self.scroll.clone())
|
|
||||||
.size_full()
|
|
||||||
.p_4()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.debug_selector(|| "padded-row".to_string())
|
|
||||||
.h(px(20.))
|
|
||||||
.w_full(),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn padding_insets_the_content_exactly_once() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let handle = ScrollHandle::new();
|
|
||||||
let handle_for_view = handle.clone();
|
|
||||||
let window = cx.add_window(move |_, _| PaddedShell {
|
|
||||||
scroll: handle_for_view,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let root = visual.debug_bounds("padded-root").expect("root rendered");
|
|
||||||
let row = visual.debug_bounds("padded-row").expect("row rendered");
|
|
||||||
// p_4 = 16px: the padding reaches only the wrapper, so the content is
|
|
||||||
// inset once, not once per wrapper and scroll pane.
|
|
||||||
assert_eq!(row.origin.x - root.origin.x, px(16.));
|
|
||||||
assert_eq!(root.size.width - row.size.width, px(32.));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_gallery_style_window_fill_scroll_area_scrolls_tall_flex_content() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let handle = ScrollHandle::new();
|
|
||||||
let handle_for_view = handle.clone();
|
|
||||||
let window = cx.add_window(move |_, _| GalleryShell {
|
|
||||||
scroll: handle_for_view,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 12 sections × 220px + padding towers past the window; a wheel event
|
|
||||||
// over the middle of the viewport must scroll the root area.
|
|
||||||
visual.simulate_event(ScrollEvent {
|
|
||||||
position: point(px(300.), px(300.)),
|
|
||||||
delta: ScrollDelta::Pixels(point(px(0.), px(-60.))),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
assert_eq!(handle.offset().y, px(-60.));
|
|
||||||
assert!(handle.max_offset().y > px(1000.));
|
|
||||||
}
|
|
||||||
@@ -1,369 +0,0 @@
|
|||||||
use gpui::{
|
|
||||||
Bounds, Context, IntoElement, ListAlignment, ListState, MouseButton, MouseDownEvent,
|
|
||||||
MouseMoveEvent, MouseUpEvent, ParentElement as _, Pixels, Point, Render, Styled as _,
|
|
||||||
TestAppContext, UniformListScrollHandle, VisualTestContext, Window, div, list, point, px,
|
|
||||||
uniform_list,
|
|
||||||
};
|
|
||||||
use gpui_native_core::init;
|
|
||||||
use gpui_native_core::scrollbar::{Scrollbar, ScrollbarHandle};
|
|
||||||
use gpui_native_theme::ActiveTheme as _;
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
const ROWS: usize = 50;
|
|
||||||
const ROW_HEIGHT: f32 = 24.;
|
|
||||||
const VIEWPORT_HEIGHT: f32 = 300.;
|
|
||||||
|
|
||||||
struct UniformShell {
|
|
||||||
rows: usize,
|
|
||||||
handle: UniformListScrollHandle,
|
|
||||||
renders: Rc<RefCell<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for UniformShell {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
*self.renders.borrow_mut() += 1;
|
|
||||||
div().size_full().child(
|
|
||||||
div()
|
|
||||||
.relative()
|
|
||||||
.h(px(VIEWPORT_HEIGHT))
|
|
||||||
.w(px(400.))
|
|
||||||
.child(
|
|
||||||
uniform_list("rows", self.rows, |range, _, _| {
|
|
||||||
range
|
|
||||||
.map(|ix| div().h(px(ROW_HEIGHT)).child(format!("row {ix}")))
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.track_scroll(&self.handle)
|
|
||||||
.h_full()
|
|
||||||
.w_full(),
|
|
||||||
)
|
|
||||||
.child(Scrollbar::vertical("rows", self.handle.clone())),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ListShell {
|
|
||||||
state: ListState,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for ListShell {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(
|
|
||||||
div()
|
|
||||||
.relative()
|
|
||||||
.h(px(VIEWPORT_HEIGHT))
|
|
||||||
.w(px(400.))
|
|
||||||
.child(
|
|
||||||
list(self.state.clone(), |ix, _, _| {
|
|
||||||
div()
|
|
||||||
.h(px(ROW_HEIGHT))
|
|
||||||
.child(format!("row {ix}"))
|
|
||||||
.into_any_element()
|
|
||||||
})
|
|
||||||
.h_full()
|
|
||||||
.w_full(),
|
|
||||||
)
|
|
||||||
.child(Scrollbar::vertical("rows", self.state.clone())),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn uniform_shell(
|
|
||||||
rows: usize,
|
|
||||||
) -> (
|
|
||||||
TestAppContext,
|
|
||||||
VisualTestContext,
|
|
||||||
UniformListScrollHandle,
|
|
||||||
Rc<RefCell<usize>>,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(init);
|
|
||||||
let handle = UniformListScrollHandle::new();
|
|
||||||
let handle_for_view = handle.clone();
|
|
||||||
let renders = Rc::new(RefCell::new(0));
|
|
||||||
let renders_for_view = renders.clone();
|
|
||||||
let window = cx.add_window(move |_, _| UniformShell {
|
|
||||||
rows,
|
|
||||||
handle: handle_for_view,
|
|
||||||
renders: renders_for_view,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, handle, renders)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn list_shell() -> (TestAppContext, VisualTestContext, ListState) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(init);
|
|
||||||
let state =
|
|
||||||
ListState::new(ROWS, ListAlignment::Top, px(400.)).with_uniform_item_height(px(ROW_HEIGHT));
|
|
||||||
let state_for_view = state.clone();
|
|
||||||
let window = cx.add_window(move |_, _| ListShell {
|
|
||||||
state: state_for_view,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The scrollbar geometry the tests need, straight from the handle and the
|
|
||||||
/// theme the component reads.
|
|
||||||
struct Metrics {
|
|
||||||
viewport: Bounds<Pixels>,
|
|
||||||
max_offset: Pixels,
|
|
||||||
width: Pixels,
|
|
||||||
inset: Pixels,
|
|
||||||
min_thumb: Pixels,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Metrics {
|
|
||||||
fn measure(cx: &TestAppContext, handle: &impl ScrollbarHandle) -> Self {
|
|
||||||
cx.update(|cx| {
|
|
||||||
let theme = cx.theme();
|
|
||||||
Self {
|
|
||||||
viewport: handle.viewport(),
|
|
||||||
max_offset: handle.max_offset().y,
|
|
||||||
width: theme.scrollbar.width,
|
|
||||||
inset: theme.scrollbar.inset,
|
|
||||||
min_thumb: theme.scrollbar.min_thumb,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn track_length(&self) -> Pixels {
|
|
||||||
self.viewport.size.height - self.inset * 2.0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn thumb_length(&self) -> Pixels {
|
|
||||||
let content = self.viewport.size.height + self.max_offset;
|
|
||||||
(self.viewport.size.height / content * self.track_length())
|
|
||||||
.max(self.min_thumb)
|
|
||||||
.min(self.track_length())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn thumb_center(&self) -> Point<Pixels> {
|
|
||||||
point(
|
|
||||||
self.viewport.right() - self.inset - self.width / 2.0,
|
|
||||||
self.viewport.top() + self.inset + self.thumb_length() / 2.0,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn track_near_bottom(&self) -> Point<Pixels> {
|
|
||||||
point(
|
|
||||||
self.viewport.right() - self.inset - self.width / 2.0,
|
|
||||||
self.viewport.top() + self.inset + self.track_length() - px(1.),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn full_drag_target(&self) -> Point<Pixels> {
|
|
||||||
point(
|
|
||||||
self.thumb_center().x,
|
|
||||||
self.thumb_center().y + (self.track_length() - self.thumb_length()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn press(visual: &mut VisualTestContext, position: Point<Pixels>) {
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
button: MouseButton::Left,
|
|
||||||
position,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn drag_to(visual: &mut VisualTestContext, position: Point<Pixels>) {
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position,
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn release(visual: &mut VisualTestContext, position: Point<Pixels>) {
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
button: MouseButton::Left,
|
|
||||||
position,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_the_thumb_scrolls_the_uniform_list() {
|
|
||||||
let (cx, mut visual, handle, renders) = uniform_shell(ROWS);
|
|
||||||
let metrics = Metrics::measure(&cx, &handle);
|
|
||||||
assert!(
|
|
||||||
metrics.max_offset > px(0.),
|
|
||||||
"50 24px rows overflow a 300px viewport"
|
|
||||||
);
|
|
||||||
|
|
||||||
press(&mut visual, metrics.thumb_center());
|
|
||||||
let renders_after_press = *renders.borrow();
|
|
||||||
drag_to(&mut visual, metrics.full_drag_target());
|
|
||||||
let renders_after_move = *renders.borrow();
|
|
||||||
release(&mut visual, metrics.full_drag_target());
|
|
||||||
|
|
||||||
let offset = handle.offset().y;
|
|
||||||
assert!(
|
|
||||||
(offset.abs() - metrics.max_offset).abs() < px(0.5),
|
|
||||||
"a full drag reaches the end, got {offset}"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
renders_after_move > renders_after_press,
|
|
||||||
"dragging must repaint the owning view, or the scroll never shows"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_track_jumps_to_the_position() {
|
|
||||||
let (cx, mut visual, handle, _) = uniform_shell(ROWS);
|
|
||||||
let metrics = Metrics::measure(&cx, &handle);
|
|
||||||
|
|
||||||
press(&mut visual, metrics.track_near_bottom());
|
|
||||||
release(&mut visual, metrics.track_near_bottom());
|
|
||||||
|
|
||||||
let offset = handle.offset().y;
|
|
||||||
assert!(
|
|
||||||
(offset.abs() - metrics.max_offset).abs() < px(0.5),
|
|
||||||
"a track click near the bottom jumps to the end, got {offset}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_fitted_list_ignores_the_scrollbar() {
|
|
||||||
let (cx, mut visual, handle, _) = uniform_shell(5);
|
|
||||||
let metrics = Metrics::measure(&cx, &handle);
|
|
||||||
assert_eq!(metrics.max_offset, px(0.), "5 rows fit the viewport");
|
|
||||||
|
|
||||||
press(&mut visual, metrics.track_near_bottom());
|
|
||||||
drag_to(&mut visual, metrics.full_drag_target());
|
|
||||||
release(&mut visual, metrics.full_drag_target());
|
|
||||||
|
|
||||||
assert_eq!(handle.offset().y, px(0.), "nothing scrolled");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_the_thumb_drives_a_list_state_and_reports_the_drag() {
|
|
||||||
let (cx, mut visual, state) = list_shell();
|
|
||||||
let metrics = Metrics::measure(&cx, &state);
|
|
||||||
assert!(metrics.max_offset > px(0.));
|
|
||||||
|
|
||||||
press(&mut visual, metrics.thumb_center());
|
|
||||||
assert!(
|
|
||||||
state.is_scrollbar_dragging(),
|
|
||||||
"the drag freezes the list's measured height"
|
|
||||||
);
|
|
||||||
|
|
||||||
drag_to(&mut visual, metrics.full_drag_target());
|
|
||||||
let offset = state.offset().y;
|
|
||||||
assert!(
|
|
||||||
(offset.abs() - metrics.max_offset).abs() < px(0.5),
|
|
||||||
"a full drag reaches the end, got {offset}"
|
|
||||||
);
|
|
||||||
|
|
||||||
release(&mut visual, metrics.full_drag_target());
|
|
||||||
assert!(
|
|
||||||
!state.is_scrollbar_dragging(),
|
|
||||||
"the drag ends with the mouse"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TwoShell {
|
|
||||||
left: UniformListScrollHandle,
|
|
||||||
right: UniformListScrollHandle,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for TwoShell {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.flex()
|
|
||||||
.flex_row()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.relative()
|
|
||||||
.h(px(VIEWPORT_HEIGHT))
|
|
||||||
.w(px(300.))
|
|
||||||
.child(
|
|
||||||
uniform_list("left-rows", ROWS, |range, _, _| {
|
|
||||||
range
|
|
||||||
.map(|ix| div().h(px(ROW_HEIGHT)).child(format!("row {ix}")))
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.track_scroll(&self.left)
|
|
||||||
.h_full()
|
|
||||||
.w_full(),
|
|
||||||
)
|
|
||||||
.child(Scrollbar::vertical("left", self.left.clone())),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.relative()
|
|
||||||
.h(px(VIEWPORT_HEIGHT))
|
|
||||||
.w(px(300.))
|
|
||||||
.child(
|
|
||||||
uniform_list("right-rows", ROWS, |range, _, _| {
|
|
||||||
range
|
|
||||||
.map(|ix| div().h(px(ROW_HEIGHT)).child(format!("row {ix}")))
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.track_scroll(&self.right)
|
|
||||||
.h_full()
|
|
||||||
.w_full(),
|
|
||||||
)
|
|
||||||
.child(Scrollbar::vertical("right", self.right.clone())),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn two_shell() -> (
|
|
||||||
TestAppContext,
|
|
||||||
VisualTestContext,
|
|
||||||
UniformListScrollHandle,
|
|
||||||
UniformListScrollHandle,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(init);
|
|
||||||
let (left, right) = (
|
|
||||||
UniformListScrollHandle::new(),
|
|
||||||
UniformListScrollHandle::new(),
|
|
||||||
);
|
|
||||||
let (left_for_view, right_for_view) = (left.clone(), right.clone());
|
|
||||||
let window = cx.add_window(move |_, _| TwoShell {
|
|
||||||
left: left_for_view,
|
|
||||||
right: right_for_view,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, left, right)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Dragging one scrollbar's thumb must never move a sibling scrollbar: the
|
|
||||||
/// drag state is keyed per scrollbar id, not shared across instances.
|
|
||||||
#[test]
|
|
||||||
fn sibling_scrollbars_drag_independently() {
|
|
||||||
let (cx, mut visual, left, right) = two_shell();
|
|
||||||
let metrics = Metrics::measure(&cx, &left);
|
|
||||||
assert!(metrics.max_offset > px(0.));
|
|
||||||
|
|
||||||
press(&mut visual, metrics.thumb_center());
|
|
||||||
drag_to(&mut visual, metrics.full_drag_target());
|
|
||||||
release(&mut visual, metrics.full_drag_target());
|
|
||||||
|
|
||||||
let offset = left.offset().y;
|
|
||||||
assert!(
|
|
||||||
(offset.abs() - metrics.max_offset).abs() < px(0.5),
|
|
||||||
"the left drag reaches the end, got {offset}"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
right.offset().y,
|
|
||||||
px(0.),
|
|
||||||
"the left drag leaked into the right scrollbar"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, IntoElement, MouseButton, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::segmented::Segmented;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
||||||
enum Filter {
|
|
||||||
All,
|
|
||||||
Popular,
|
|
||||||
Recent,
|
|
||||||
}
|
|
||||||
|
|
||||||
const FILTERS: [(Filter, &str); 3] = [
|
|
||||||
(Filter::All, "All"),
|
|
||||||
(Filter::Popular, "Popular"),
|
|
||||||
(Filter::Recent, "Recent"),
|
|
||||||
];
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<Filter>>>;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
filter: Filter,
|
|
||||||
changes: Changes,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
Segmented::new("filter", self.filter)
|
|
||||||
.options(FILTERS)
|
|
||||||
.on_change(move |next, _window, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.filter = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn filter_window() -> (TestAppContext, VisualTestContext, Changes) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
filter: Filter::All,
|
|
||||||
changes: harness_changes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn click(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
|
||||||
visual.simulate_mouse_down(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
visual.simulate_mouse_up(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The strip hugs its labels in the `p_4` band: the three segments span
|
|
||||||
/// roughly `18..142` horizontally and `17.6..33.9` vertically (macOS's 13 pt
|
|
||||||
/// root font), so clicks land near `32` (All), `70` (Popular), and `118`
|
|
||||||
/// (Recent).
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_segment_reports_it() {
|
|
||||||
let (_cx, mut visual, changes) = filter_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(70.), px(27.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![Filter::Popular]);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(118.), px(27.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![Filter::Popular, Filter::Recent]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn re_clicking_the_selected_segment_reports_nothing() {
|
|
||||||
let (_cx, mut visual, changes) = filter_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(70.), px(27.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![Filter::Popular]);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(70.), px(27.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![Filter::Popular]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrows_move_the_selection_when_the_strip_has_focus() {
|
|
||||||
let (_cx, mut visual, changes) = filter_window();
|
|
||||||
|
|
||||||
// Clicking a segment hands the strip the keyboard, like the list.
|
|
||||||
click(&mut visual, point(px(32.), px(27.)));
|
|
||||||
visual.simulate_keystrokes("right");
|
|
||||||
assert_eq!(*changes.borrow(), vec![Filter::Popular]);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("right");
|
|
||||||
assert_eq!(*changes.borrow(), vec![Filter::Popular, Filter::Recent]);
|
|
||||||
|
|
||||||
// The strip never wraps: right on the last segment reports nothing.
|
|
||||||
visual.simulate_keystrokes("right");
|
|
||||||
assert_eq!(*changes.borrow(), vec![Filter::Popular, Filter::Recent]);
|
|
||||||
|
|
||||||
// Home and end jump to the ends.
|
|
||||||
visual.simulate_keystrokes("home");
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![Filter::Popular, Filter::Recent, Filter::All]
|
|
||||||
);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("end");
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![Filter::Popular, Filter::Recent, Filter::All, Filter::Recent]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,286 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, Entity, IntoElement, MouseButton, ParentElement as _, Render, Styled as _,
|
|
||||||
TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::input::{Input, InputState};
|
|
||||||
use gpui_native_core::select::Select;
|
|
||||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Theme};
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
||||||
enum Strategy {
|
|
||||||
Curated,
|
|
||||||
Uncensored,
|
|
||||||
}
|
|
||||||
|
|
||||||
const STRATEGIES: [(Strategy, &str); 2] = [
|
|
||||||
(Strategy::Curated, "Curated"),
|
|
||||||
(Strategy::Uncensored, "Uncensored"),
|
|
||||||
];
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<Option<Strategy>>>>;
|
|
||||||
|
|
||||||
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, gpui::Modifiers::default());
|
|
||||||
visual.simulate_mouse_up(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
strategy: Option<Strategy>,
|
|
||||||
changes: Changes,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
Select::new("strategy")
|
|
||||||
.options(STRATEGIES)
|
|
||||||
.selected(self.strategy)
|
|
||||||
.on_change(move |next, _window, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.strategy = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// One select: trigger band is `16..46` vertically (`p_4` + the 30 px field).
|
|
||||||
fn select_window() -> (TestAppContext, VisualTestContext, Changes) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
strategy: None,
|
|
||||||
changes: harness_changes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
draw(&mut visual);
|
|
||||||
(cx, visual, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Pair {
|
|
||||||
first: Option<Strategy>,
|
|
||||||
second: Option<Strategy>,
|
|
||||||
first_changes: Changes,
|
|
||||||
second_changes: Changes,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Pair {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let view = cx.entity();
|
|
||||||
let first_changes = self.first_changes.clone();
|
|
||||||
let second_changes = self.second_changes.clone();
|
|
||||||
let first_view = view.clone();
|
|
||||||
let second_view = view.clone();
|
|
||||||
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
div()
|
|
||||||
.flex()
|
|
||||||
.flex_col()
|
|
||||||
.gap_2()
|
|
||||||
.child(
|
|
||||||
Select::new("first")
|
|
||||||
.options(STRATEGIES)
|
|
||||||
.selected(self.first)
|
|
||||||
.on_change(move |next, _window, cx| {
|
|
||||||
first_changes.borrow_mut().push(next);
|
|
||||||
first_view.update(cx, |pair, cx| {
|
|
||||||
pair.first = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
Select::new("second")
|
|
||||||
.options(STRATEGIES)
|
|
||||||
.selected(self.second)
|
|
||||||
.on_change(move |next, _window, cx| {
|
|
||||||
second_changes.borrow_mut().push(next);
|
|
||||||
second_view.update(cx, |pair, cx| {
|
|
||||||
pair.second = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Two selects: bands `16..46` and `54..84` (the 8 px gap between).
|
|
||||||
fn pair_window() -> (TestAppContext, VisualTestContext, Changes, Changes) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let first_changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let second_changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let first = first_changes.clone();
|
|
||||||
let second = second_changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Pair {
|
|
||||||
first: None,
|
|
||||||
second: None,
|
|
||||||
first_changes: first,
|
|
||||||
second_changes: second,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
draw(&mut visual);
|
|
||||||
(cx, visual, first_changes, second_changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn choosing_an_option_reports_the_value_and_closes() {
|
|
||||||
let (_cx, mut visual, changes) = select_window();
|
|
||||||
|
|
||||||
// The trigger opens the popup; with no value yet nothing is armed, so
|
|
||||||
// the first Enter does nothing and the next Down arms the first option.
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), Vec::<Option<Strategy>>::new());
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("down enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(Strategy::Curated)]);
|
|
||||||
|
|
||||||
// The popup closed with the choice, so another Enter reports nothing.
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(Strategy::Curated)]);
|
|
||||||
|
|
||||||
// The trigger reopens with the chosen value armed; the next option is
|
|
||||||
// one Down away.
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![Some(Strategy::Curated), Some(Strategy::Uncensored)]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_controlled_value_round_trips_through_the_owner() {
|
|
||||||
let (_cx, mut visual, changes) = select_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("down down");
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
// The harness stored the reported value, so the select now holds it;
|
|
||||||
// reopening arms it and Enter reports it straight back.
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![Some(Strategy::Uncensored), Some(Strategy::Uncensored)]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn two_selects_keep_their_popups_independent() {
|
|
||||||
let (_cx, mut visual, first_changes, second_changes) = pair_window();
|
|
||||||
|
|
||||||
// Opening the second select never touches the first.
|
|
||||||
click(&mut visual, point(px(100.), px(69.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("down enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
assert_eq!(*first_changes.borrow(), vec![]);
|
|
||||||
assert_eq!(*second_changes.borrow(), vec![Some(Strategy::Curated)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_click_outside_closes_the_popup() {
|
|
||||||
let (_cx, mut visual, changes) = select_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
click(&mut visual, point(px(100.), px(300.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
// The popup held keyboard focus while open; a keystroke after the outside
|
|
||||||
// click answers nothing if the popup really closed.
|
|
||||||
visual.simulate_keystrokes("enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), Vec::<Option<Strategy>>::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Clicking another control while the popup is open dismisses it and still
|
|
||||||
/// acts on the control: dismissing does not claim the event.
|
|
||||||
#[test]
|
|
||||||
fn a_click_on_another_control_dismisses_and_reaches_it() {
|
|
||||||
let (_cx, mut visual, first_changes, second_changes) = pair_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
// The second select's trigger, below the first — at x=300, past the first
|
|
||||||
// popup's 182px-wide panel: this click must close the first popup AND open
|
|
||||||
// the second.
|
|
||||||
click(&mut visual, point(px(300.), px(69.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
visual.simulate_keystrokes("down enter");
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
assert_eq!(*first_changes.borrow(), Vec::<Option<Strategy>>::new());
|
|
||||||
assert_eq!(*second_changes.borrow(), vec![Some(Strategy::Curated)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct HeightsHarness {
|
|
||||||
state: Entity<InputState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for HeightsHarness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div()
|
|
||||||
.flex()
|
|
||||||
.flex_col()
|
|
||||||
.gap_4()
|
|
||||||
.child(Select::new("heights").options(STRATEGIES))
|
|
||||||
.child(Input::new("heights-field", self.state.clone()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The trigger renders at the same height as the input field: the stated
|
|
||||||
/// metric plus the always-painted hairline border, whatever the platform.
|
|
||||||
#[test]
|
|
||||||
fn the_trigger_matches_the_input_height() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let state = cx.update(InputState::new);
|
|
||||||
let state_for_window = state.clone();
|
|
||||||
let window = cx.add_window(move |_, _| HeightsHarness {
|
|
||||||
state: state_for_window,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
let trigger = visual
|
|
||||||
.debug_bounds("select-trigger")
|
|
||||||
.expect("trigger rendered");
|
|
||||||
let field = visual.debug_bounds("input-field").expect("input rendered");
|
|
||||||
|
|
||||||
// Snapped bounds: allow half a pixel.
|
|
||||||
assert!((trigger.size.height - field.size.height).abs() <= px(0.5));
|
|
||||||
let expected = cx.update(|cx| cx.theme().sizes.medium.input_height) + BORDER_WIDTH * 2.0;
|
|
||||||
assert!((trigger.size.height - expected).abs() <= px(0.5));
|
|
||||||
}
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, IntoElement, MouseButton, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::select::Select;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
||||||
enum Level {
|
|
||||||
Low,
|
|
||||||
Mid,
|
|
||||||
High,
|
|
||||||
}
|
|
||||||
|
|
||||||
const LEVELS: [(Level, &str); 3] = [
|
|
||||||
(Level::Low, "Low"),
|
|
||||||
(Level::Mid, "Mid"),
|
|
||||||
(Level::High, "High"),
|
|
||||||
];
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<Option<Level>>>>;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
value: Option<Level>,
|
|
||||||
changes: Changes,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
Select::new("level")
|
|
||||||
.options(LEVELS)
|
|
||||||
.selected(self.value)
|
|
||||||
.on_change(move |next, _window, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.value = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn level_window() -> (TestAppContext, VisualTestContext, Changes) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
value: None,
|
|
||||||
changes: harness_changes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn click(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
|
||||||
visual.simulate_mouse_down(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
visual.simulate_mouse_up(position, MouseButton::Left, gpui::Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The three option rows must answer a pointer press wherever they were laid
|
|
||||||
/// out — a popup that clips its last item never does. Row positions come from
|
|
||||||
/// the rendered bounds, so the stated menu row metric can change freely.
|
|
||||||
#[test]
|
|
||||||
fn every_option_row_answers_the_pointer() {
|
|
||||||
let (_cx, mut visual, changes) = level_window();
|
|
||||||
|
|
||||||
// Open once to measure where the rows were actually drawn.
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
let centers: Vec<gpui::Point<gpui::Pixels>> = (0..3)
|
|
||||||
.map(|index| {
|
|
||||||
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"));
|
|
||||||
point(
|
|
||||||
row.left() + row.size.width / 2.0,
|
|
||||||
row.top() + row.size.height / 2.0,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Close again; each iteration below opens the popup fresh.
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let expected = [(Level::Low, 0), (Level::Mid, 1), (Level::High, 2)];
|
|
||||||
for (value, index) in expected {
|
|
||||||
// Choosing the previous row closed the popup; reopen it.
|
|
||||||
click(&mut visual, point(px(100.), px(31.)));
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
click(&mut visual, centers[index]);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
assert_eq!(
|
|
||||||
changes.borrow().last(),
|
|
||||||
Some(&Some(value)),
|
|
||||||
"pressing {value:?}'s row must choose it"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
use gpui::{
|
|
||||||
AppContext as _, Context, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::separator::{Separator, SeparatorOrientation};
|
|
||||||
use gpui_native_theme::{BORDER_WIDTH, Theme};
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
orientation: SeparatorOrientation,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let separator = Separator::horizontal()
|
|
||||||
.orientation(self.orientation)
|
|
||||||
.debug_selector(|| "test-separator".to_string());
|
|
||||||
if self.orientation == SeparatorOrientation::Horizontal {
|
|
||||||
div()
|
|
||||||
.w(px(200.))
|
|
||||||
.h(px(50.))
|
|
||||||
.flex()
|
|
||||||
.flex_col()
|
|
||||||
.child(separator)
|
|
||||||
} else {
|
|
||||||
div().w(px(200.)).h(px(50.)).flex().child(separator)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn separator_window(orientation: SeparatorOrientation) -> (TestAppContext, VisualTestContext) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let window = cx.add_window(move |_, _| Harness { orientation });
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
(cx, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_horizontal_rule_spans_its_container_at_hairline_height() {
|
|
||||||
let (_cx, mut visual) = separator_window(SeparatorOrientation::Horizontal);
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("test-separator")
|
|
||||||
.expect("horizontal separator rendered");
|
|
||||||
assert_eq!(bounds.size.width, px(200.));
|
|
||||||
assert_eq!(bounds.size.height, BORDER_WIDTH);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_vertical_rule_spans_its_container_at_hairline_width() {
|
|
||||||
let (_cx, mut visual) = separator_window(SeparatorOrientation::Vertical);
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("test-separator")
|
|
||||||
.expect("vertical separator rendered");
|
|
||||||
assert_eq!(bounds.size.width, BORDER_WIDTH);
|
|
||||||
assert_eq!(bounds.size.height, px(50.));
|
|
||||||
}
|
|
||||||
@@ -1,284 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, InputEvent as _, IntoElement, KeyDownEvent,
|
|
||||||
Keystroke, Modifiers, MouseButton, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::slider::Slider;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
changes: Rc<RefCell<Vec<f32>>>,
|
|
||||||
value: f32,
|
|
||||||
disabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().flex().flex_col().child(
|
|
||||||
div().w(px(200.)).child(
|
|
||||||
Slider::new("test-slider", self.value)
|
|
||||||
.min(0.0)
|
|
||||||
.max(100.0)
|
|
||||||
.step(10.0)
|
|
||||||
.disabled(self.disabled)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.value = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window holding a 200px slider over 0..=100 stepped by 10 in
|
|
||||||
/// the top-left corner: `x` maps to `round(x / 200 * 10) * 10`.
|
|
||||||
fn slider_window(disabled: bool) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<Vec<f32>>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
changes: harness_changes,
|
|
||||||
value: 0.0,
|
|
||||||
disabled,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(VisualTestContext::from_window(window, &cx), window, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(cx: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_track_sets_the_snapped_value() {
|
|
||||||
let (mut cx, _window, changes) = slider_window(false);
|
|
||||||
cx.simulate_click(point(px(100.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![50.0]);
|
|
||||||
cx.simulate_click(point(px(40.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![50.0, 20.0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_reports_values_until_release() {
|
|
||||||
let (mut cx, _window, changes) = slider_window(false);
|
|
||||||
cx.simulate_mouse_down(
|
|
||||||
point(px(40.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
cx.simulate_mouse_move(
|
|
||||||
point(px(120.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
cx.simulate_mouse_up(
|
|
||||||
point(px(120.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
assert_eq!(*changes.borrow(), vec![20.0, 60.0]);
|
|
||||||
|
|
||||||
// Moving after release must not change the value.
|
|
||||||
cx.simulate_mouse_move(
|
|
||||||
point(px(180.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
assert_eq!(*changes.borrow(), vec![20.0, 60.0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn drag_clamps_to_the_range_ends() {
|
|
||||||
let (mut cx, _window, changes) = slider_window(false);
|
|
||||||
cx.simulate_mouse_down(
|
|
||||||
point(px(100.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
// The drag captures the pointer: it keeps reporting past the track's
|
|
||||||
// right edge, clamped to the top of the range.
|
|
||||||
cx.simulate_mouse_move(
|
|
||||||
point(px(400.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
assert_eq!(*changes.borrow(), vec![50.0, 100.0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_slider_does_not_report_changes() {
|
|
||||||
let (mut cx, _window, changes) = slider_window(true);
|
|
||||||
cx.simulate_click(point(px(100.), px(10.)), Modifiers::default());
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrow_keys_step_the_value_while_focused() {
|
|
||||||
let (mut cx, window, changes) = slider_window(false);
|
|
||||||
// The click focuses the slider and sets the snapped value at that point.
|
|
||||||
cx.simulate_click(point(px(2.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![0.0]);
|
|
||||||
|
|
||||||
draw(&mut cx, window);
|
|
||||||
|
|
||||||
for key in ["right", "right", "left"] {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.dispatch_event(
|
|
||||||
KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
}
|
|
||||||
.to_platform_input(),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*changes.borrow(), vec![0.0, 10.0, 20.0, 10.0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TwoSliders {
|
|
||||||
changes_a: Rc<RefCell<Vec<f32>>>,
|
|
||||||
changes_b: Rc<RefCell<Vec<f32>>>,
|
|
||||||
value_a: f32,
|
|
||||||
value_b: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for TwoSliders {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let (changes_a, changes_b) = (self.changes_a.clone(), self.changes_b.clone());
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.child(
|
|
||||||
div().absolute().top_0().left_0().w(px(200.)).child(
|
|
||||||
Slider::new("slider-a", self.value_a)
|
|
||||||
.min(0.0)
|
|
||||||
.max(100.0)
|
|
||||||
.step(10.0)
|
|
||||||
.on_change({
|
|
||||||
let view = cx.entity();
|
|
||||||
let changes_a = changes_a.clone();
|
|
||||||
move |next, _, cx| {
|
|
||||||
changes_a.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.value_a = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
div().absolute().top(px(40.)).left_0().w(px(200.)).child(
|
|
||||||
Slider::new("slider-b", self.value_b)
|
|
||||||
.min(0.0)
|
|
||||||
.max(100.0)
|
|
||||||
.step(10.0)
|
|
||||||
.on_change({
|
|
||||||
let view = cx.entity();
|
|
||||||
move |next, _, cx| {
|
|
||||||
changes_b.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.value_b = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
fn two_slider_window() -> (
|
|
||||||
VisualTestContext,
|
|
||||||
Rc<RefCell<Vec<f32>>>,
|
|
||||||
Rc<RefCell<Vec<f32>>>,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes_a = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let changes_b = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let (changes_a_for_window, changes_b_for_window) = (changes_a.clone(), changes_b.clone());
|
|
||||||
let window = cx.add_window(move |_, _| TwoSliders {
|
|
||||||
changes_a: changes_a_for_window,
|
|
||||||
changes_b: changes_b_for_window,
|
|
||||||
value_a: 0.0,
|
|
||||||
value_b: 0.0,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(
|
|
||||||
VisualTestContext::from_window(window, &cx),
|
|
||||||
changes_a,
|
|
||||||
changes_b,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Dragging one slider must never drive a sibling slider: the drag flag is
|
|
||||||
/// per instance, not shared keyed state.
|
|
||||||
#[test]
|
|
||||||
fn two_sliders_drag_independently() {
|
|
||||||
let (mut cx, changes_a, changes_b) = two_slider_window();
|
|
||||||
|
|
||||||
// Slider A occupies y 0..16, slider B starts at y 40.
|
|
||||||
cx.simulate_mouse_down(
|
|
||||||
point(px(100.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
cx.simulate_mouse_move(
|
|
||||||
point(px(120.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
cx.simulate_mouse_up(
|
|
||||||
point(px(120.), px(10.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
assert_eq!(*changes_a.borrow(), vec![50.0, 60.0]);
|
|
||||||
assert!(changes_b.borrow().is_empty(), "A's drag leaked into B");
|
|
||||||
|
|
||||||
cx.simulate_mouse_down(
|
|
||||||
point(px(100.), px(50.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
cx.simulate_mouse_move(
|
|
||||||
point(px(40.), px(50.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
cx.simulate_mouse_up(
|
|
||||||
point(px(40.), px(50.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
assert_eq!(*changes_b.borrow(), vec![50.0, 20.0]);
|
|
||||||
assert_eq!(
|
|
||||||
*changes_a.borrow(),
|
|
||||||
vec![50.0, 60.0],
|
|
||||||
"B's drag leaked into A"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,282 +0,0 @@
|
|||||||
#![cfg(feature = "speech")]
|
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use gpui::{App, AppContext as _, Entity, SharedString, Subscription, TestAppContext};
|
|
||||||
use gpui_native_core::speech::{
|
|
||||||
AudioFormat, AudioInput, AudioSink, RecognitionSession, SpeechError, SpeechEvent,
|
|
||||||
SpeechRecognizer, SpeechSink, SpeechState, SpeechStatus,
|
|
||||||
};
|
|
||||||
|
|
||||||
type EventLog = Rc<RefCell<Vec<SpeechEvent>>>;
|
|
||||||
type Harness = (
|
|
||||||
Entity<SpeechState>,
|
|
||||||
EventLog,
|
|
||||||
Rc<RefCell<Option<SpeechSink>>>,
|
|
||||||
Rc<RefCell<usize>>,
|
|
||||||
);
|
|
||||||
|
|
||||||
struct FakeRecognizer {
|
|
||||||
available: bool,
|
|
||||||
finish_reports: bool,
|
|
||||||
sink: Rc<RefCell<Option<SpeechSink>>>,
|
|
||||||
received: Rc<RefCell<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SpeechRecognizer for FakeRecognizer {
|
|
||||||
fn is_available(&self, _cx: &App) -> bool {
|
|
||||||
self.available
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start(
|
|
||||||
&self,
|
|
||||||
sink: SpeechSink,
|
|
||||||
cx: &mut App,
|
|
||||||
) -> Result<Box<dyn RecognitionSession>, SpeechError> {
|
|
||||||
*self.sink.borrow_mut() = Some(sink.clone());
|
|
||||||
sink.ready(cx);
|
|
||||||
Ok(Box::new(FakeSession {
|
|
||||||
sink,
|
|
||||||
finish_reports: self.finish_reports,
|
|
||||||
received: self.received.clone(),
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct FakeSession {
|
|
||||||
sink: SpeechSink,
|
|
||||||
finish_reports: bool,
|
|
||||||
received: Rc<RefCell<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RecognitionSession for FakeSession {
|
|
||||||
fn push_audio(&mut self, samples: &[i16], _cx: &mut App) {
|
|
||||||
*self.received.borrow_mut() += samples.len();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn finish(&mut self, cx: &mut App) {
|
|
||||||
if self.finish_reports {
|
|
||||||
self.sink.finish(cx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Delivers one silent second, 16 kHz mono, in 100 ms chunks.
|
|
||||||
struct FakeInput;
|
|
||||||
|
|
||||||
impl AudioInput for FakeInput {
|
|
||||||
fn start(
|
|
||||||
&self,
|
|
||||||
format: AudioFormat,
|
|
||||||
sink: AudioSink,
|
|
||||||
cx: &mut App,
|
|
||||||
) -> Result<Subscription, SpeechError> {
|
|
||||||
let chunk_len = (format.sample_rate() / 10) as usize * format.channels() as usize;
|
|
||||||
let task = cx.spawn(async move |cx| {
|
|
||||||
for _ in 0..10 {
|
|
||||||
cx.background_executor()
|
|
||||||
.timer(Duration::from_millis(100))
|
|
||||||
.await;
|
|
||||||
cx.update(|cx| sink.push(vec![0; chunk_len], cx));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Ok(Subscription::new(move || drop(task)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn harness(cx: &mut TestAppContext, available: bool, finish_reports: bool) -> Harness {
|
|
||||||
let sink_slot: Rc<RefCell<Option<SpeechSink>>> = Rc::new(RefCell::new(None));
|
|
||||||
let received = Rc::new(RefCell::new(0));
|
|
||||||
let log: EventLog = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let record = log.clone();
|
|
||||||
let speech = cx.update(|cx| {
|
|
||||||
let speech = cx.new(|cx| {
|
|
||||||
SpeechState::new(cx)
|
|
||||||
.recognizer(FakeRecognizer {
|
|
||||||
available,
|
|
||||||
finish_reports,
|
|
||||||
sink: sink_slot.clone(),
|
|
||||||
received: received.clone(),
|
|
||||||
})
|
|
||||||
.input(FakeInput)
|
|
||||||
.stop_timeout(Duration::from_secs(2))
|
|
||||||
});
|
|
||||||
cx.subscribe(&speech, move |_, event, _| {
|
|
||||||
record.borrow_mut().push(event.clone())
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
speech
|
|
||||||
});
|
|
||||||
(speech, log, sink_slot, received)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn start(speech: &Entity<SpeechState>, cx: &mut TestAppContext) {
|
|
||||||
cx.update(|cx| speech.update(cx, |state, cx| state.start(cx)));
|
|
||||||
cx.run_until_parked();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_session_starts_reads_and_finalizes() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (speech, events, sink_slot, _) = harness(&mut cx, true, true);
|
|
||||||
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
cx.update(|cx| assert_eq!(speech.read(cx).status(), SpeechStatus::Recording));
|
|
||||||
assert_eq!(*events.borrow(), vec![SpeechEvent::Started]);
|
|
||||||
|
|
||||||
let sink = sink_slot.borrow().clone().expect("session opened");
|
|
||||||
cx.update(|cx| sink.hypothesis("hel", cx));
|
|
||||||
cx.run_until_parked();
|
|
||||||
cx.update(|cx| {
|
|
||||||
sink.phrase("hello ", cx);
|
|
||||||
sink.hypothesis("wor", cx);
|
|
||||||
});
|
|
||||||
cx.run_until_parked();
|
|
||||||
cx.update(|cx| {
|
|
||||||
assert_eq!(
|
|
||||||
speech.read(cx).transcript(),
|
|
||||||
SharedString::from("hello wor")
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| speech.update(cx, |state, cx| state.stop(cx)));
|
|
||||||
cx.run_until_parked();
|
|
||||||
cx.update(|cx| assert_eq!(speech.read(cx).status(), SpeechStatus::Idle));
|
|
||||||
|
|
||||||
let events = events.borrow();
|
|
||||||
assert!(
|
|
||||||
matches!(events.last(), Some(SpeechEvent::Final(text)) if text.as_ref() == "hello wor")
|
|
||||||
);
|
|
||||||
assert!(matches!(
|
|
||||||
events.get(events.len() - 2),
|
|
||||||
Some(SpeechEvent::Partial(text)) if text.as_ref() == "hello wor"
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn starting_twice_runs_a_single_session() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (speech, events, _, _) = harness(&mut cx, true, true);
|
|
||||||
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
|
|
||||||
assert_eq!(*events.borrow(), vec![SpeechEvent::Started]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn cancel_discards_the_transcript_and_ignores_late_reports() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (speech, events, sink_slot, _) = harness(&mut cx, true, true);
|
|
||||||
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
let sink = sink_slot.borrow().clone().expect("session opened");
|
|
||||||
cx.update(|cx| sink.phrase("hello", cx));
|
|
||||||
cx.run_until_parked();
|
|
||||||
|
|
||||||
cx.update(|cx| speech.update(cx, |state, cx| state.cancel(cx)));
|
|
||||||
cx.run_until_parked();
|
|
||||||
cx.update(|cx| {
|
|
||||||
let state = speech.read(cx);
|
|
||||||
assert_eq!(state.status(), SpeechStatus::Idle);
|
|
||||||
assert_eq!(state.transcript(), SharedString::from(""));
|
|
||||||
});
|
|
||||||
assert!(matches!(
|
|
||||||
events.borrow().last(),
|
|
||||||
Some(SpeechEvent::Cancelled)
|
|
||||||
));
|
|
||||||
|
|
||||||
cx.update(|cx| sink.finish(cx));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
!events
|
|
||||||
.borrow()
|
|
||||||
.iter()
|
|
||||||
.any(|event| matches!(event, SpeechEvent::Final(_)))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn starting_without_a_recognizer_fails() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let log: EventLog = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let record = log.clone();
|
|
||||||
let speech = cx.update(|cx| {
|
|
||||||
let speech = cx.new(|cx| SpeechState::new(cx).input(FakeInput));
|
|
||||||
cx.subscribe(&speech, move |_, event, _| {
|
|
||||||
record.borrow_mut().push(event.clone())
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
speech
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| assert!(!speech.read(cx).has_recognizer()));
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
|
|
||||||
cx.update(|cx| assert_eq!(speech.read(cx).status(), SpeechStatus::Idle));
|
|
||||||
assert!(matches!(events_last(&log), Some(SpeechEvent::Error(_))));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn starting_an_unavailable_recognizer_fails() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (speech, events, _, _) = harness(&mut cx, false, true);
|
|
||||||
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
cx.update(|cx| assert_eq!(speech.read(cx).status(), SpeechStatus::Idle));
|
|
||||||
assert!(matches!(
|
|
||||||
events.borrow().last(),
|
|
||||||
Some(SpeechEvent::Error(_))
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_stop_timeout_finalizes_with_the_transcript_so_far() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (speech, events, sink_slot, _) = harness(&mut cx, true, false);
|
|
||||||
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
let sink = sink_slot.borrow().clone().expect("session opened");
|
|
||||||
cx.update(|cx| sink.phrase("hello", cx));
|
|
||||||
cx.run_until_parked();
|
|
||||||
|
|
||||||
cx.update(|cx| speech.update(cx, |state, cx| state.stop(cx)));
|
|
||||||
cx.run_until_parked();
|
|
||||||
cx.update(|cx| assert_eq!(speech.read(cx).status(), SpeechStatus::Stopping));
|
|
||||||
|
|
||||||
cx.executor().advance_clock(Duration::from_secs(2));
|
|
||||||
cx.run_until_parked();
|
|
||||||
cx.update(|cx| assert_eq!(speech.read(cx).status(), SpeechStatus::Idle));
|
|
||||||
assert!(
|
|
||||||
matches!(events.borrow().last(), Some(SpeechEvent::Final(text)) if text.as_ref() == "hello")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn captured_audio_reaches_the_session_and_the_levels() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (speech, _, _, received) = harness(&mut cx, true, true);
|
|
||||||
|
|
||||||
start(&speech, &mut cx);
|
|
||||||
cx.executor().advance_clock(Duration::from_secs(1));
|
|
||||||
cx.run_until_parked();
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
let state = speech.read(cx);
|
|
||||||
assert_eq!(*received.borrow(), 16_000);
|
|
||||||
// One level per 80 ms bucket of the one-second capture.
|
|
||||||
let levels = state.levels();
|
|
||||||
assert_eq!(levels.len(), 12);
|
|
||||||
assert!(levels.iter().all(|level| *level == 0.0));
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| speech.update(cx, |state, cx| state.stop(cx)));
|
|
||||||
cx.run_until_parked();
|
|
||||||
cx.update(|cx| assert!(speech.read(cx).levels().is_empty()));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn events_last(log: &EventLog) -> Option<SpeechEvent> {
|
|
||||||
log.borrow().last().cloned()
|
|
||||||
}
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc, time::Duration};
|
|
||||||
|
|
||||||
use gpui::prelude::FluentBuilder as _;
|
|
||||||
use gpui::{
|
|
||||||
Context, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::spinner::{Spinner, SpinnerVariant};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct SpinnerHost {
|
|
||||||
variant: SpinnerVariant,
|
|
||||||
determinate: bool,
|
|
||||||
renders: Rc<RefCell<usize>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for SpinnerHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
*self.renders.borrow_mut() += 1;
|
|
||||||
let spinner = Spinner::new("test-spinner")
|
|
||||||
.variant(self.variant)
|
|
||||||
.when(self.determinate, |spinner| spinner.value(0.4));
|
|
||||||
div().size_full().child(spinner)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn host_window(
|
|
||||||
variant: SpinnerVariant,
|
|
||||||
determinate: bool,
|
|
||||||
reduce_motion: bool,
|
|
||||||
) -> (TestAppContext, Rc<RefCell<usize>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(|cx| {
|
|
||||||
Theme::init(cx);
|
|
||||||
if reduce_motion {
|
|
||||||
cx.set_reduce_motion(true);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let renders = Rc::new(RefCell::new(0));
|
|
||||||
let host_renders = renders.clone();
|
|
||||||
cx.add_window(move |_, _| SpinnerHost {
|
|
||||||
variant,
|
|
||||||
determinate,
|
|
||||||
renders: host_renders,
|
|
||||||
});
|
|
||||||
cx.run_until_parked();
|
|
||||||
(cx, renders)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn spinning_repaints_on_the_animation_clock() {
|
|
||||||
let (cx, renders) = host_window(SpinnerVariant::Ring, false, false);
|
|
||||||
assert_eq!(*renders.borrow(), 1);
|
|
||||||
|
|
||||||
// The 60fps cap drives re-renders with timers, not frame callbacks.
|
|
||||||
cx.executor().advance_clock(Duration::from_millis(100));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
*renders.borrow() > 1,
|
|
||||||
"the spinner must repaint while spinning"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn determinate_paints_one_static_frame() {
|
|
||||||
let (cx, renders) = host_window(SpinnerVariant::Ring, true, false);
|
|
||||||
assert_eq!(*renders.borrow(), 1);
|
|
||||||
|
|
||||||
cx.executor().advance_clock(Duration::from_millis(200));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(*renders.borrow(), 1, "a determinate ring holds still");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn reduce_motion_paints_one_static_frame() {
|
|
||||||
let (cx, renders) = host_window(SpinnerVariant::Ring, false, true);
|
|
||||||
assert_eq!(*renders.borrow(), 1);
|
|
||||||
|
|
||||||
cx.executor().advance_clock(Duration::from_millis(200));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(*renders.borrow(), 1, "reduced motion must freeze the spin");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_icon_variant_repaints_while_spinning() {
|
|
||||||
let (cx, renders) = host_window(SpinnerVariant::Icon, false, false);
|
|
||||||
assert_eq!(*renders.borrow(), 1);
|
|
||||||
|
|
||||||
cx.executor().advance_clock(Duration::from_millis(100));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
*renders.borrow() > 1,
|
|
||||||
"the icon must repaint while spinning"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_icon_variant_holds_still_when_determinate() {
|
|
||||||
let (cx, renders) = host_window(SpinnerVariant::Icon, true, false);
|
|
||||||
assert_eq!(*renders.borrow(), 1);
|
|
||||||
|
|
||||||
cx.executor().advance_clock(Duration::from_millis(200));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(*renders.borrow(), 1, "a held icon must not animate");
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
use gpui::{
|
|
||||||
AppContext as _, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_core::status_dot::{StatusDot, StatusDotVariant};
|
|
||||||
use gpui_native_theme::{ActiveTheme as _, Theme};
|
|
||||||
|
|
||||||
struct DotHost;
|
|
||||||
|
|
||||||
impl Render for DotHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut gpui::Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().p_4().child(
|
|
||||||
StatusDot::new("test-dot")
|
|
||||||
.variant(StatusDotVariant::Success)
|
|
||||||
.label("Synced"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dot_window() -> (TestAppContext, gpui::AnyWindowHandle) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let window = cx.add_window(|_, _| DotHost);
|
|
||||||
let window: gpui::AnyWindowHandle = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(cx, window)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A medium dot is half the theme's icon size — big enough to read a state
|
|
||||||
/// from, small enough to sit beside row text. The label must not grow it.
|
|
||||||
#[test]
|
|
||||||
fn the_dot_is_half_the_icon_size() {
|
|
||||||
let (cx, window) = dot_window();
|
|
||||||
let mut visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
let dot = visual.debug_bounds("test-dot").expect("dot missing");
|
|
||||||
|
|
||||||
let side = cx.update(|cx| cx.theme().sizes.medium.icon_size) / 2.;
|
|
||||||
assert!(
|
|
||||||
((dot.size.width - side).abs() <= gpui::px(0.5))
|
|
||||||
&& ((dot.size.height - side).abs() <= gpui::px(0.5)),
|
|
||||||
"the dot must be {side:?} wide, got {:?}",
|
|
||||||
dot.size
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,135 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke,
|
|
||||||
Modifiers, ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window,
|
|
||||||
div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::switch::Switch;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
changes: Rc<RefCell<Vec<bool>>>,
|
|
||||||
checked: bool,
|
|
||||||
disabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().flex().flex_col().child(
|
|
||||||
Switch::new("test-switch")
|
|
||||||
.label("Wi-Fi")
|
|
||||||
.checked(self.checked)
|
|
||||||
.disabled(self.disabled)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.checked = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window whose switch track sits in the top-left corner, so
|
|
||||||
/// `point(px(10.), px(10.))` lands on it.
|
|
||||||
fn switch_window(
|
|
||||||
checked: bool,
|
|
||||||
disabled: bool,
|
|
||||||
) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<Vec<bool>>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
changes: harness_changes,
|
|
||||||
checked,
|
|
||||||
disabled,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(VisualTestContext::from_window(window, &cx), window, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(cx: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_reports_the_next_value_through_the_controlled_loop() {
|
|
||||||
let (mut cx, _window, changes) = switch_window(false, false);
|
|
||||||
cx.simulate_click(point(px(10.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![true]);
|
|
||||||
|
|
||||||
// The second click must report `false`: only if the owner applied the
|
|
||||||
// first change does the switch know it is now checked.
|
|
||||||
cx.simulate_click(point(px(10.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_switch_does_not_report_changes() {
|
|
||||||
let (mut cx, _window, changes) = switch_window(false, true);
|
|
||||||
cx.simulate_click(point(px(10.), px(10.)), Modifiers::default());
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The track renders at the platform's stated switch dims, not the generic
|
|
||||||
/// control height — the stated metric is the whole chrome.
|
|
||||||
#[test]
|
|
||||||
fn track_renders_at_the_stated_switch_dims() {
|
|
||||||
let (mut cx, _window, _changes) = switch_window(false, false);
|
|
||||||
let (width, height) = cx.update(|_window, cx| {
|
|
||||||
let metrics = Theme::global(cx).sizes.medium;
|
|
||||||
(metrics.switch_width, metrics.switch_height)
|
|
||||||
});
|
|
||||||
let track = cx
|
|
||||||
.debug_bounds("test-switch-track")
|
|
||||||
.expect("the switch track should have been laid out");
|
|
||||||
assert!(
|
|
||||||
(track.size.width - width).abs() <= px(0.5),
|
|
||||||
"track is {:?} wide, stated switch width is {width:?}",
|
|
||||||
track.size.width
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
(track.size.height - height).abs() <= px(0.5),
|
|
||||||
"track is {:?} tall, stated switch height is {height:?}",
|
|
||||||
track.size.height
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_and_space_report_the_next_value_while_focused() {
|
|
||||||
let (mut cx, window, changes) = switch_window(false, false);
|
|
||||||
cx.simulate_click(point(px(10.), px(10.)), Modifiers::default());
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![true],
|
|
||||||
"the pointer click itself toggles"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Repaint with the switch focused so its Enter/Space handlers register.
|
|
||||||
draw(&mut cx, window);
|
|
||||||
|
|
||||||
for key in ["enter", "space"] {
|
|
||||||
cx.simulate_event(KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
});
|
|
||||||
cx.simulate_event(KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false, true]);
|
|
||||||
}
|
|
||||||
@@ -1,158 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use gpui::{AppContext as _, TestAppContext};
|
|
||||||
use gpui_native_core::toast::{Toast, ToastAction, Toasts};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
fn toasts_store() -> (TestAppContext, gpui::Entity<Toasts>) {
|
|
||||||
let cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let toasts = cx.update(|cx| cx.new(|_| Toasts::default()));
|
|
||||||
(cx, toasts)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn push_raises_a_toast_and_the_timer_fades_it_out() {
|
|
||||||
let (cx, toasts) = toasts_store();
|
|
||||||
|
|
||||||
let id = cx.update(|cx| toasts.update(cx, |toasts, cx| toasts.push(Toast::new("Saved"), cx)));
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 1);
|
|
||||||
|
|
||||||
// The default lifetime is five seconds; the fade-out follows it.
|
|
||||||
cx.executor().advance_clock(Toast::DURATION);
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 1, "fading out");
|
|
||||||
cx.executor()
|
|
||||||
.advance_clock(std::time::Duration::from_secs(1));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 0);
|
|
||||||
let _ = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn pushing_replaces_the_toast_on_screen() {
|
|
||||||
let (cx, toasts) = toasts_store();
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
toasts.update(cx, |toasts, cx| toasts.push(Toast::new("first"), cx));
|
|
||||||
});
|
|
||||||
cx.update(|cx| {
|
|
||||||
toasts.update(cx, |toasts, cx| toasts.push(Toast::new("second"), cx));
|
|
||||||
});
|
|
||||||
|
|
||||||
// One showing, one fading out: never two current toasts.
|
|
||||||
let (current, total) = cx.update(|cx| {
|
|
||||||
toasts.read_with(cx, |toasts, _| {
|
|
||||||
(toasts.current().map(|entry| entry.id), toasts.count())
|
|
||||||
})
|
|
||||||
});
|
|
||||||
assert_eq!(current, Some(1));
|
|
||||||
assert_eq!(total, 2);
|
|
||||||
|
|
||||||
cx.executor()
|
|
||||||
.advance_clock(std::time::Duration::from_secs(6));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn sticky_toasts_ignore_the_timer_until_dismissed() {
|
|
||||||
let (cx, toasts) = toasts_store();
|
|
||||||
|
|
||||||
let id = cx.update(|cx| {
|
|
||||||
toasts.update(cx, |toasts, cx| {
|
|
||||||
toasts.push(Toast::new("Sticky").duration(Duration::ZERO), cx)
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.executor().advance_clock(Toast::DURATION * 4);
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 1);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
toasts.update(cx, |toasts, cx| toasts.dismiss(id, cx));
|
|
||||||
});
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 1, "fading out");
|
|
||||||
cx.executor()
|
|
||||||
.advance_clock(std::time::Duration::from_secs(1));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dismiss_runs_the_handler_only_after_the_fade_completes() {
|
|
||||||
let (cx, toasts) = toasts_store();
|
|
||||||
let dismissed = Rc::new(std::cell::RefCell::new(Vec::<&'static str>::new()));
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
let first = dismissed.clone();
|
|
||||||
toasts.update(cx, |toasts, cx| {
|
|
||||||
toasts.push(
|
|
||||||
Toast::new("first").on_dismiss(move |_| record(&first, "first")),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
toasts.update(cx, |toasts, cx| toasts.dismiss(0, cx));
|
|
||||||
});
|
|
||||||
assert!(
|
|
||||||
dismissed.borrow().is_empty(),
|
|
||||||
"the handler waits out the fade"
|
|
||||||
);
|
|
||||||
cx.executor()
|
|
||||||
.advance_clock(std::time::Duration::from_secs(1));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(*dismissed.borrow(), vec!["first"]);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
toasts.update(cx, |toasts, cx| toasts.clear(cx));
|
|
||||||
});
|
|
||||||
cx.executor()
|
|
||||||
.advance_clock(std::time::Duration::from_secs(1));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert_eq!(cx.update(|cx| toasts.read(cx).count()), 0);
|
|
||||||
|
|
||||||
fn record(sink: &Rc<RefCell<Vec<&'static str>>>, what: &'static str) {
|
|
||||||
sink.borrow_mut().push(what);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Dismissing by any other path never runs the action: only a click on the
|
|
||||||
/// action itself does (the view wires the two together).
|
|
||||||
#[test]
|
|
||||||
fn dismissal_does_not_run_the_action() {
|
|
||||||
let (cx, toasts) = toasts_store();
|
|
||||||
let ran = Rc::new(std::cell::RefCell::new(false));
|
|
||||||
let dismissed = Rc::new(std::cell::RefCell::new(false));
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
let action_ran = ran.clone();
|
|
||||||
let toast_dismissed = dismissed.clone();
|
|
||||||
toasts.update(cx, |toasts, cx| {
|
|
||||||
toasts.push(
|
|
||||||
Toast::new("Draft deleted")
|
|
||||||
.action(ToastAction::new("Undo", move |_| {
|
|
||||||
*action_ran.borrow_mut() = true;
|
|
||||||
}))
|
|
||||||
.on_dismiss(move |_| *toast_dismissed.borrow_mut() = true),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
toasts.update(cx, |toasts, cx| toasts.dismiss(0, cx));
|
|
||||||
});
|
|
||||||
assert!(!*ran.borrow(), "dismiss is not the action");
|
|
||||||
cx.executor()
|
|
||||||
.advance_clock(std::time::Duration::from_secs(1));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
*dismissed.borrow(),
|
|
||||||
"on_dismiss still reports the dismissal"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,111 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AnyWindowHandle, AppContext as _, Context, IntoElement, KeyDownEvent, KeyUpEvent, Keystroke,
|
|
||||||
Modifiers, ParentElement as _, Render, Styled as _, TestAppContext, VisualTestContext, Window,
|
|
||||||
div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::toggle::Toggle;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
changes: Rc<RefCell<Vec<bool>>>,
|
|
||||||
pressed: bool,
|
|
||||||
disabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().flex().flex_col().child(
|
|
||||||
Toggle::new("test-toggle")
|
|
||||||
.label("Bold")
|
|
||||||
.pressed(self.pressed)
|
|
||||||
.disabled(self.disabled)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.pressed = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A maximized window whose toggle sits in the top-left corner, so
|
|
||||||
/// `point(px(20.), px(14.))` lands on it.
|
|
||||||
fn toggle_window(
|
|
||||||
pressed: bool,
|
|
||||||
disabled: bool,
|
|
||||||
) -> (VisualTestContext, AnyWindowHandle, Rc<RefCell<Vec<bool>>>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
changes: harness_changes,
|
|
||||||
pressed,
|
|
||||||
disabled,
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
(VisualTestContext::from_window(window, &cx), window, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(cx: &mut VisualTestContext, window: AnyWindowHandle) {
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_reports_the_next_pressed_state_through_the_controlled_loop() {
|
|
||||||
let (mut cx, _window, changes) = toggle_window(false, false);
|
|
||||||
cx.simulate_click(point(px(20.), px(14.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![true]);
|
|
||||||
|
|
||||||
// The second click must report `false`: only if the owner applied the
|
|
||||||
// first change does the toggle know it is pressed.
|
|
||||||
cx.simulate_click(point(px(20.), px(14.)), Modifiers::default());
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_toggle_does_not_report_changes() {
|
|
||||||
let (mut cx, _window, changes) = toggle_window(false, true);
|
|
||||||
cx.simulate_click(point(px(20.), px(14.)), Modifiers::default());
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn enter_and_space_report_the_next_pressed_state_while_focused() {
|
|
||||||
let (mut cx, window, changes) = toggle_window(false, false);
|
|
||||||
cx.simulate_click(point(px(20.), px(14.)), Modifiers::default());
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![true],
|
|
||||||
"the pointer click itself presses"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Repaint with the toggle focused so its Enter/Space handlers register.
|
|
||||||
draw(&mut cx, window);
|
|
||||||
|
|
||||||
for key in ["enter", "space"] {
|
|
||||||
cx.simulate_event(KeyDownEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
is_held: false,
|
|
||||||
prefer_character_input: false,
|
|
||||||
});
|
|
||||||
cx.simulate_event(KeyUpEvent {
|
|
||||||
keystroke: Keystroke::parse(key).unwrap(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_eq!(*changes.borrow(), vec![true, false, true]);
|
|
||||||
}
|
|
||||||
@@ -1,209 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, InteractiveElement as _, IntoElement, Modifiers, MouseMoveEvent, ParentElement as _,
|
|
||||||
Render, StatefulInteractiveElement as _, Styled as _, TestAppContext, VisualTestContext,
|
|
||||||
Window, WindowHandle, canvas, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_core::button::Button;
|
|
||||||
use gpui_native_core::tooltip::Tooltip;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
use gpui_native_utils::record_anchor_bounds;
|
|
||||||
|
|
||||||
/// Hover target at the top-left with a tooltip whose builder records every
|
|
||||||
/// invocation. The tooltip anchors to the target itself, so it hangs below
|
|
||||||
/// the target's bottom edge instead of at the cursor.
|
|
||||||
struct Harness {
|
|
||||||
calls: Rc<RefCell<Vec<&'static str>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let calls = self.calls.clone();
|
|
||||||
let target = div()
|
|
||||||
.id("hover-target")
|
|
||||||
.debug_selector(|| "hover-target".to_string())
|
|
||||||
.w(px(100.))
|
|
||||||
.h(px(40.))
|
|
||||||
.bg(gpui::black())
|
|
||||||
.child(
|
|
||||||
canvas(record_anchor_bounds("hover-target".into()), |_, _, _, _| {})
|
|
||||||
.absolute()
|
|
||||||
.inset_0(),
|
|
||||||
)
|
|
||||||
.tooltip(move |window, cx| {
|
|
||||||
calls.borrow_mut().push("target");
|
|
||||||
Tooltip::anchored("Target tooltip", "hover-target")(window, cx)
|
|
||||||
});
|
|
||||||
|
|
||||||
let button_calls = self.calls.clone();
|
|
||||||
let button = Button::new("hover-button")
|
|
||||||
.label("Hover me")
|
|
||||||
.tooltip(move |window, cx| {
|
|
||||||
button_calls.borrow_mut().push("button");
|
|
||||||
Tooltip::anchored("Button tooltip", "hover-button")(window, cx)
|
|
||||||
});
|
|
||||||
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.child(target)
|
|
||||||
.child(div().mt_12().child(button))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tooltip_window() -> (
|
|
||||||
TestAppContext,
|
|
||||||
VisualTestContext,
|
|
||||||
Rc<RefCell<Vec<&'static str>>>,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let calls = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_calls = calls.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
calls: harness_calls,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, calls)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hover(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position,
|
|
||||||
pressed_button: None,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/// gpui's hover delay before deploying a tooltip.
|
|
||||||
const SHOW_DELAY: std::time::Duration = std::time::Duration::from_millis(500);
|
|
||||||
|
|
||||||
fn draw(visual: &mut VisualTestContext) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
struct LowHarness;
|
|
||||||
|
|
||||||
impl Render for LowHarness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
// The target is pinned to the window's bottom edge: the space below it
|
|
||||||
// is nil, so the tooltip must flip above the parent.
|
|
||||||
div().size_full().flex().flex_col().justify_end().child(
|
|
||||||
div()
|
|
||||||
.id("low-target")
|
|
||||||
.debug_selector(|| "low-target".to_string())
|
|
||||||
.w(px(100.))
|
|
||||||
.h(px(40.))
|
|
||||||
.bg(gpui::black())
|
|
||||||
.child(
|
|
||||||
canvas(record_anchor_bounds("low-target".into()), |_, _, _, _| {})
|
|
||||||
.absolute()
|
|
||||||
.inset_0(),
|
|
||||||
)
|
|
||||||
.tooltip(Tooltip::anchored("Low tooltip", "low-target")),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn low_tooltip_window() -> (TestAppContext, VisualTestContext, WindowHandle<LowHarness>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let window = cx.add_window(move |_, _| LowHarness);
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, window)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn hovering_the_target_deploys_the_tooltip_after_the_delay() {
|
|
||||||
let (cx, mut visual, calls) = tooltip_window();
|
|
||||||
hover(&mut visual, point(px(50.), px(20.)));
|
|
||||||
assert!(
|
|
||||||
calls.borrow().is_empty(),
|
|
||||||
"builder runs only after the delay"
|
|
||||||
);
|
|
||||||
|
|
||||||
cx.executor().advance_clock(SHOW_DELAY);
|
|
||||||
visual.run_until_parked();
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*calls.borrow(), vec!["target"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn hovering_elsewhere_deploys_nothing() {
|
|
||||||
let (cx, mut visual, calls) = tooltip_window();
|
|
||||||
hover(&mut visual, point(px(800.), px(600.)));
|
|
||||||
cx.executor().advance_clock(SHOW_DELAY);
|
|
||||||
visual.run_until_parked();
|
|
||||||
draw(&mut visual);
|
|
||||||
assert!(calls.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An anchored tooltip hangs below the hovered parent's bottom edge with the
|
|
||||||
/// spacing gap: it never covers the parent, wherever the cursor is inside it.
|
|
||||||
#[test]
|
|
||||||
fn the_anchored_panel_hangs_below_the_parent_without_overlapping() {
|
|
||||||
let (cx, mut visual, _calls) = tooltip_window();
|
|
||||||
hover(&mut visual, point(px(50.), px(20.)));
|
|
||||||
cx.executor().advance_clock(SHOW_DELAY);
|
|
||||||
visual.run_until_parked();
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
let target = visual
|
|
||||||
.debug_bounds("hover-target")
|
|
||||||
.expect("target laid out");
|
|
||||||
let panel = visual
|
|
||||||
.debug_bounds("tooltip-panel")
|
|
||||||
.expect("panel rendered");
|
|
||||||
|
|
||||||
let unit = cx.update(|cx| Theme::global(cx).spacing.unit);
|
|
||||||
assert!(
|
|
||||||
(panel.top() - target.bottom() - unit).abs() <= px(0.5),
|
|
||||||
"the panel hangs {gap:?} below the target",
|
|
||||||
gap = panel.top() - target.bottom()
|
|
||||||
);
|
|
||||||
assert!((panel.left() - target.left()).abs() <= px(0.5));
|
|
||||||
assert!(
|
|
||||||
panel.top() >= target.bottom(),
|
|
||||||
"the panel must never overlap the hovered parent"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A parent pinned to the window's bottom edge leaves no room below: the
|
|
||||||
/// panel flips above the parent's top edge instead.
|
|
||||||
#[test]
|
|
||||||
fn an_anchored_tooltip_flips_above_a_low_parent() {
|
|
||||||
let (mut cx, mut visual, window) = low_tooltip_window();
|
|
||||||
|
|
||||||
let window_height = window
|
|
||||||
.update(&mut cx, |_, window, _cx| window.viewport_size().height)
|
|
||||||
.expect("window alive");
|
|
||||||
hover(&mut visual, point(px(50.), window_height - px(20.)));
|
|
||||||
cx.executor().advance_clock(SHOW_DELAY);
|
|
||||||
visual.run_until_parked();
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
let target = visual.debug_bounds("low-target").expect("target laid out");
|
|
||||||
let panel = visual
|
|
||||||
.debug_bounds("tooltip-panel")
|
|
||||||
.expect("panel rendered");
|
|
||||||
|
|
||||||
// The flip hangs the panel above the parent's top edge.
|
|
||||||
assert!(
|
|
||||||
panel.bottom() <= target.top(),
|
|
||||||
"the panel must not cover the parent: panel bottom {:?}, parent top {:?}",
|
|
||||||
panel.bottom(),
|
|
||||||
target.top()
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
panel.top() >= px(0.),
|
|
||||||
"the flipped panel must stay inside the window"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
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_data::list::{List, ListItem};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
selected: Option<usize>,
|
|
||||||
changes: Rc<RefCell<Vec<Option<usize>>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().child(
|
|
||||||
List::new("test-list")
|
|
||||||
.selected(self.selected)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.selected = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.items(|_, _| {
|
|
||||||
vec![
|
|
||||||
ListItem::new("one", "One"),
|
|
||||||
ListItem::new("two", "Two"),
|
|
||||||
ListItem::new("unavailable", "Unavailable").disabled(true),
|
|
||||||
ListItem::new("three", "Three"),
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<Option<usize>>>>;
|
|
||||||
|
|
||||||
type ListWindow = (
|
|
||||||
TestAppContext,
|
|
||||||
VisualTestContext,
|
|
||||||
WindowHandle<Harness>,
|
|
||||||
Changes,
|
|
||||||
);
|
|
||||||
|
|
||||||
/// Rows paint at the top-left with no outer padding: one row per ~34 px band
|
|
||||||
/// (16 px text line + 2×4 px padding + 2 px gap), so clicks at `(50, 16/48/80/112)`
|
|
||||||
/// land on One / Two / the disabled Unavailable / Three.
|
|
||||||
fn list_window(selected: Option<usize>) -> ListWindow {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
selected,
|
|
||||||
changes: harness_changes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, window, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
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_a_row_selects_it_and_hands_the_list_the_keyboard() {
|
|
||||||
let (_cx, mut visual, _window, changes) = list_window(None);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(16.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(0)]);
|
|
||||||
|
|
||||||
// The click focused the list, so the arrows navigate from here.
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(0), Some(1)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_disabled_row_reports_nothing() {
|
|
||||||
let (_cx, mut visual, _window, changes) = list_window(None);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(80.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), Vec::<Option<usize>>::new());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn keyboard_navigation_skips_disabled_rows_and_never_wraps() {
|
|
||||||
let (_cx, mut visual, _window, changes) = list_window(None);
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(16.)));
|
|
||||||
visual.simulate_keystrokes("down"); // over the disabled row → Two
|
|
||||||
visual.simulate_keystrokes("down"); // over the disabled row → Three
|
|
||||||
visual.simulate_keystrokes("down"); // last selectable; no wrap
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(0), Some(1), Some(3)]);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("home");
|
|
||||||
visual.simulate_keystrokes("end");
|
|
||||||
visual.simulate_keystrokes("up"); // over the disabled row → Two
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![Some(0), Some(1), Some(3), Some(0), Some(3), Some(1)]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrow_down_with_no_selection_picks_the_first_selectable_row() {
|
|
||||||
let (mut cx, mut visual, window, changes) = list_window(Some(1));
|
|
||||||
|
|
||||||
// Focus the list, then clear the selection app-side.
|
|
||||||
click(&mut visual, point(px(50.), px(48.)));
|
|
||||||
window
|
|
||||||
.update(&mut cx, |harness, _, cx| {
|
|
||||||
harness.selected = None;
|
|
||||||
cx.notify();
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
draw(&mut visual);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(1), Some(0)]);
|
|
||||||
}
|
|
||||||
@@ -1,119 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, IntoElement, Modifiers, MouseButton, ParentElement as _, Render, Styled as _,
|
|
||||||
TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_data::sidebar::{Sidebar, SidebarEntry};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<Option<usize>>>>;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
selected: Option<usize>,
|
|
||||||
changes: Changes,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().child(
|
|
||||||
Sidebar::new("test-sidebar")
|
|
||||||
.selected(self.selected)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.selected = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.entries(|_, _| {
|
|
||||||
vec![
|
|
||||||
SidebarEntry::header("Favorites"), // 0: never selectable
|
|
||||||
SidebarEntry::row("documents", "Documents"), // 1
|
|
||||||
SidebarEntry::row("locked", "Locked").disabled(true), // 2
|
|
||||||
SidebarEntry::header("Locations"), // 3: never selectable
|
|
||||||
SidebarEntry::row("trash", "Trash"), // 4
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Entries stack from the top-left: the header band (~32 px) then ~34 px row
|
|
||||||
/// bands, so clicks at `(50, 16)` hit the header, `(50, 48)` Documents,
|
|
||||||
/// `(50, 82)` Locked, `(50, 116)` the second header, `(50, 150)` Trash.
|
|
||||||
fn sidebar_window() -> (TestAppContext, VisualTestContext, Changes) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
selected: None,
|
|
||||||
changes: harness_changes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
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_a_row_selects_it_and_hands_the_sidebar_the_keyboard() {
|
|
||||||
let (_cx, mut visual, changes) = sidebar_window();
|
|
||||||
|
|
||||||
// The header band is not a row: clicking it reports nothing.
|
|
||||||
click(&mut visual, point(px(50.), px(16.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(48.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(1)]);
|
|
||||||
|
|
||||||
// The click focused the sidebar; the arrows move from here.
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(1), Some(4)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_disabled_row_reports_nothing() {
|
|
||||||
let (_cx, mut visual, changes) = sidebar_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(82.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn keyboard_navigation_skips_headers_disabled_rows_and_never_wraps() {
|
|
||||||
let (_cx, mut visual, changes) = sidebar_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(48.))); // Documents, focuses
|
|
||||||
visual.simulate_keystrokes("down"); // over Locked → Trash
|
|
||||||
visual.simulate_keystrokes("down"); // over the second header; no wrap
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(1), Some(4)]);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("up"); // over Locked → Documents
|
|
||||||
visual.simulate_keystrokes("end"); // last selectable row
|
|
||||||
visual.simulate_keystrokes("home"); // first selectable row
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![Some(1), Some(4), Some(1), Some(4), Some(1)]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,168 +0,0 @@
|
|||||||
use std::{cell::RefCell, rc::Rc};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Context, IntoElement, Modifiers, MouseButton, ParentElement as _, Render, Styled as _,
|
|
||||||
TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_data::tabs::{Tab, Tabs};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<usize>>>;
|
|
||||||
type Closes = Rc<RefCell<Vec<usize>>>;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
selected: usize,
|
|
||||||
changes: Changes,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
div().size_full().child(
|
|
||||||
Tabs::new("test-tabs")
|
|
||||||
.selected(self.selected)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |harness, cx| {
|
|
||||||
harness.selected = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.tabs(|_, _| {
|
|
||||||
vec![
|
|
||||||
Tab::new("one", "One"),
|
|
||||||
Tab::new("two", "Two"),
|
|
||||||
Tab::new("off", "Off").disabled(true),
|
|
||||||
Tab::new("end", "End"),
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Same-length labels keep the strip's geometry predictable: each tab is
|
|
||||||
/// roughly 40 px wide with a 4 px gap, so clicks at `(20/62/104/146, 12)`
|
|
||||||
/// land on One / Two / the disabled Off / End.
|
|
||||||
fn tabs_window() -> (TestAppContext, VisualTestContext, Changes) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
selected: 0,
|
|
||||||
changes: harness_changes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, changes)
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CloseHarness {
|
|
||||||
changes: Changes,
|
|
||||||
closes: Closes,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for CloseHarness {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let changes = self.changes.clone();
|
|
||||||
let closes = self.closes.clone();
|
|
||||||
let view = cx.entity();
|
|
||||||
let close_view = view.clone();
|
|
||||||
div().size_full().child(
|
|
||||||
Tabs::new("close-tabs")
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
view.update(cx, |_, cx| cx.notify());
|
|
||||||
})
|
|
||||||
.on_close(move |index, _, cx| {
|
|
||||||
closes.borrow_mut().push(index);
|
|
||||||
close_view.update(cx, |_, cx| cx.notify());
|
|
||||||
})
|
|
||||||
.tabs(|_, _| {
|
|
||||||
vec![
|
|
||||||
Tab::new("one", "One").closeable(true),
|
|
||||||
Tab::new("two", "Two"),
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn close_tabs_window() -> (TestAppContext, VisualTestContext, Changes, Closes) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let closes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let harness_closes = closes.clone();
|
|
||||||
let window = cx.add_window(move |_, _| CloseHarness {
|
|
||||||
changes: harness_changes,
|
|
||||||
closes: harness_closes,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, changes, closes)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_tab_selects_it_and_hands_the_strip_the_keyboard() {
|
|
||||||
let (_cx, mut visual, changes) = tabs_window();
|
|
||||||
|
|
||||||
// Tab 0 is already selected; selecting it again reports nothing.
|
|
||||||
click(&mut visual, point(px(20.), px(12.)));
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
|
|
||||||
// The click focused the strip, so the arrows switch from here.
|
|
||||||
visual.simulate_keystrokes("right");
|
|
||||||
assert_eq!(*changes.borrow(), vec![1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_disabled_tab_reports_nothing() {
|
|
||||||
let (_cx, mut visual, changes) = tabs_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(104.), px(12.)));
|
|
||||||
assert!(changes.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrow_keys_skip_disabled_tabs_and_never_wrap() {
|
|
||||||
let (_cx, mut visual, changes) = tabs_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(20.), px(12.))); // One: already selected, no report
|
|
||||||
visual.simulate_keystrokes("right"); // Two
|
|
||||||
visual.simulate_keystrokes("right"); // over the disabled tab → End
|
|
||||||
visual.simulate_keystrokes("right"); // last; no wrap
|
|
||||||
assert_eq!(*changes.borrow(), vec![1, 3]);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("left"); // over the disabled tab → Two
|
|
||||||
visual.simulate_keystrokes("home"); // One
|
|
||||||
visual.simulate_keystrokes("end"); // End
|
|
||||||
assert_eq!(*changes.borrow(), vec![1, 3, 1, 0, 3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_close_control_reports_a_close_without_selecting() {
|
|
||||||
let (_cx, mut visual, changes, closes) = close_tabs_window();
|
|
||||||
|
|
||||||
// Only the closeable tab ships a close control.
|
|
||||||
let selector: &'static str = Box::leak(String::from("close-tabs-close-One").into_boxed_str());
|
|
||||||
let close_bounds = visual
|
|
||||||
.debug_bounds(selector)
|
|
||||||
.expect("closeable tab renders a close control");
|
|
||||||
assert!(visual.debug_bounds("close-tabs-close-Two").is_none());
|
|
||||||
|
|
||||||
click(&mut visual, close_bounds.center());
|
|
||||||
assert_eq!(*closes.borrow(), vec![0]);
|
|
||||||
assert!(changes.borrow().is_empty(), "a close is not a selection");
|
|
||||||
}
|
|
||||||
@@ -1,181 +0,0 @@
|
|||||||
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_data::tree::{Tree, TreeItem};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
type Changes = Rc<RefCell<Vec<Option<usize>>>>;
|
|
||||||
type Disclosures = Rc<RefCell<Vec<usize>>>;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
selected: Option<usize>,
|
|
||||||
changes: Changes,
|
|
||||||
disclosures: Disclosures,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let changes = self.changes.clone();
|
|
||||||
let disclosures = self.disclosures.clone();
|
|
||||||
let view = cx.entity();
|
|
||||||
let change_view = view.clone();
|
|
||||||
div().size_full().child(
|
|
||||||
Tree::new("test-tree")
|
|
||||||
.selected(self.selected)
|
|
||||||
.on_change(move |next, _, cx| {
|
|
||||||
changes.borrow_mut().push(next);
|
|
||||||
change_view.update(cx, |harness, cx| {
|
|
||||||
harness.selected = next;
|
|
||||||
cx.notify();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.on_disclosure(move |index, _, cx| {
|
|
||||||
disclosures.borrow_mut().push(index);
|
|
||||||
view.update(cx, |_, cx| cx.notify());
|
|
||||||
})
|
|
||||||
.items(|_, _| {
|
|
||||||
vec![
|
|
||||||
TreeItem::new("root", "Root")
|
|
||||||
.depth(0)
|
|
||||||
.expandable(true)
|
|
||||||
.expanded(true),
|
|
||||||
TreeItem::new("child", "Child").depth(1),
|
|
||||||
TreeItem::new("collapsed", "Collapsed")
|
|
||||||
.depth(0)
|
|
||||||
.expandable(true),
|
|
||||||
TreeItem::new("untouchable", "Untouchable")
|
|
||||||
.depth(1)
|
|
||||||
.disabled(true),
|
|
||||||
TreeItem::new("last", "Last").depth(0),
|
|
||||||
]
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Rows paint at the top-left, one per ~34 px band (same rhythm as the
|
|
||||||
/// list), so clicks at `(50, 16/48/80/112/144)` land on Root / Child /
|
|
||||||
/// Collapsed / the disabled Untouchable / Last. Each expandable row leads
|
|
||||||
/// with a 16 px disclosure slot at its indent — a click at `(24, y)` hits
|
|
||||||
/// row 0's chevron.
|
|
||||||
fn tree_window() -> (
|
|
||||||
TestAppContext,
|
|
||||||
VisualTestContext,
|
|
||||||
WindowHandle<Harness>,
|
|
||||||
Changes,
|
|
||||||
Disclosures,
|
|
||||||
) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let changes = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let disclosures = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let harness_changes = changes.clone();
|
|
||||||
let harness_disclosures = disclosures.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
selected: None,
|
|
||||||
changes: harness_changes,
|
|
||||||
disclosures: harness_disclosures,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, window, changes, disclosures)
|
|
||||||
}
|
|
||||||
|
|
||||||
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_a_row_selects_it_and_hands_the_tree_the_keyboard() {
|
|
||||||
let (_cx, mut visual, _window, changes, disclosures) = tree_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(16.)));
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(0)]);
|
|
||||||
assert!(disclosures.borrow().is_empty());
|
|
||||||
|
|
||||||
// The click focused the tree; arrows move within the visible rows.
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(0), Some(1)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_the_disclosure_toggles_without_selecting() {
|
|
||||||
let (_cx, mut visual, _window, changes, disclosures) = tree_window();
|
|
||||||
|
|
||||||
// Row 0's chevron, not its label.
|
|
||||||
click(&mut visual, point(px(24.), px(16.)));
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*disclosures.borrow(), vec![0]);
|
|
||||||
assert!(
|
|
||||||
changes.borrow().is_empty(),
|
|
||||||
"a disclosure is not a selection"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrow_right_expands_and_left_collapses_the_selected_row() {
|
|
||||||
let (_cx, mut visual, _window, changes, disclosures) = tree_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(16.))); // Root: expanded expandable
|
|
||||||
visual.simulate_keystrokes("left"); // collapse → reports
|
|
||||||
visual.simulate_keystrokes("right"); // still collapsed per the caller → nothing yet
|
|
||||||
visual.simulate_keystrokes("down"); // Child: a leaf → arrows are inert
|
|
||||||
visual.simulate_keystrokes("right");
|
|
||||||
visual.simulate_keystrokes("left");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*disclosures.borrow(), vec![0]);
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![Some(0), Some(1)],
|
|
||||||
"disclosure keys never change the selection"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Select the collapsed expandable row and expand it.
|
|
||||||
click(&mut visual, point(px(50.), px(80.)));
|
|
||||||
visual.simulate_keystrokes("right");
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*disclosures.borrow(), vec![0, 2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn keyboard_navigation_skips_disabled_rows_and_never_wraps() {
|
|
||||||
let (_cx, mut visual, _window, changes, _disclosures) = tree_window();
|
|
||||||
|
|
||||||
click(&mut visual, point(px(50.), px(16.)));
|
|
||||||
visual.simulate_keystrokes("down"); // Two
|
|
||||||
visual.simulate_keystrokes("down"); // Collapsed — selectable, not disabled
|
|
||||||
visual.simulate_keystrokes("down"); // over the disabled row → Last
|
|
||||||
visual.simulate_keystrokes("down"); // last selectable; no wrap
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(*changes.borrow(), vec![Some(0), Some(1), Some(2), Some(4)]);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("home");
|
|
||||||
visual.simulate_keystrokes("end");
|
|
||||||
visual.simulate_keystrokes("up"); // over the disabled row
|
|
||||||
draw(&mut visual);
|
|
||||||
assert_eq!(
|
|
||||||
*changes.borrow(),
|
|
||||||
vec![
|
|
||||||
Some(0),
|
|
||||||
Some(1),
|
|
||||||
Some(2),
|
|
||||||
Some(4),
|
|
||||||
Some(0),
|
|
||||||
Some(4),
|
|
||||||
Some(2)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,265 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, Focusable as _, IntoElement, ParentElement as _, Render,
|
|
||||||
Styled as _, TestAppContext, VisualTestContext, Window, div,
|
|
||||||
};
|
|
||||||
use gpui_native_editor::code::{CodeViewState, bind_code_view_keys};
|
|
||||||
use gpui_native_editor::highlight::{HighlightedCode, Language, Role, highlight};
|
|
||||||
|
|
||||||
fn spans_with_role(code: &HighlightedCode, role: Role) -> Vec<&str> {
|
|
||||||
code.lines
|
|
||||||
.iter()
|
|
||||||
.flat_map(|line| line.spans.iter())
|
|
||||||
.filter(|span| span.role == role)
|
|
||||||
.map(|span| span.text.as_ref())
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_rust_snippet_gets_its_roles_from_the_grammar() {
|
|
||||||
let source = "// a comment\nfn main() {\n let greeting = \"hello\";\n}\n";
|
|
||||||
let code = highlight(source, Language::Rust);
|
|
||||||
|
|
||||||
assert_eq!(code.lines.len(), 5, "a trailing newline ends an empty line");
|
|
||||||
assert!(spans_with_role(&code, Role::Comment).contains(&"// a comment"));
|
|
||||||
assert!(
|
|
||||||
spans_with_role(&code, Role::Keyword)
|
|
||||||
.iter()
|
|
||||||
.any(|text| text.contains("fn"))
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
spans_with_role(&code, Role::Keyword)
|
|
||||||
.iter()
|
|
||||||
.any(|text| text.contains("let"))
|
|
||||||
);
|
|
||||||
assert!(spans_with_role(&code, Role::Function).contains(&"main"));
|
|
||||||
assert!(
|
|
||||||
spans_with_role(&code, Role::String)
|
|
||||||
.iter()
|
|
||||||
.any(|text| text.contains("hello"))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn plain_language_never_applies_grammar_roles() {
|
|
||||||
let source = "// a comment\nfn main() {\n let greeting = \"hello\";\n}\n";
|
|
||||||
let code = highlight(source, Language::Plain);
|
|
||||||
|
|
||||||
assert_eq!(code.lines.len(), 5, "a trailing newline ends an empty line");
|
|
||||||
assert!(
|
|
||||||
code.lines
|
|
||||||
.iter()
|
|
||||||
.flat_map(|line| line.spans.iter())
|
|
||||||
.all(|span| span.role == Role::Text),
|
|
||||||
"plain renders every span as text, whatever the source looks like"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_capture_spanning_newlines_styles_both_lines() {
|
|
||||||
let source = "let text = \"a\nb\";\n";
|
|
||||||
let code = highlight(source, Language::Rust);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
code.lines[0]
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.any(|span| span.role == Role::String && span.text.contains("\"a")),
|
|
||||||
"the string opens on the first line"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
code.lines[1]
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.any(|span| span.role == Role::String && span.text.contains("b\"")),
|
|
||||||
"and continues on the second"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn set_source_rehighlights_with_the_new_language() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(|cx| {
|
|
||||||
gpui_native_theme::Theme::init(cx);
|
|
||||||
bind_code_view_keys(cx);
|
|
||||||
});
|
|
||||||
let state = cx.new(|cx| CodeViewState::new("test-code", "fn main() {}\n", Language::Rust, cx));
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.set_source("def main():\n pass\n", Language::Python, cx);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
let code = cx.update(|cx| state.read(cx).code().clone());
|
|
||||||
assert!(
|
|
||||||
spans_with_role(&code, Role::Keyword)
|
|
||||||
.iter()
|
|
||||||
.any(|text| text.contains("def")),
|
|
||||||
"python's def highlights as a keyword"
|
|
||||||
);
|
|
||||||
assert!(spans_with_role(&code, Role::Function).contains(&"main"));
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
code: Entity<CodeViewState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(self.code.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn code_view_window(
|
|
||||||
line_count: usize,
|
|
||||||
) -> (TestAppContext, VisualTestContext, Entity<CodeViewState>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(|cx| {
|
|
||||||
gpui_native_theme::Theme::init(cx);
|
|
||||||
bind_code_view_keys(cx);
|
|
||||||
});
|
|
||||||
let source: String = (0..line_count)
|
|
||||||
.map(|line| format!("fn line_{line}() {{}}\n"))
|
|
||||||
.collect();
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<CodeViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |window, cx| {
|
|
||||||
let code = cx.new(|cx| CodeViewState::new("test-code", source, Language::Rust, cx));
|
|
||||||
cell.borrow_mut().replace(code.clone());
|
|
||||||
window.focus(&code.read(cx).focus_handle(cx), cx);
|
|
||||||
Host { code }
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rows_render_virtualized_and_scroll() {
|
|
||||||
let (_cx, mut visual, state) = code_view_window(80);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("test-code-row-0").is_some(),
|
|
||||||
"the first row renders"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("test-code-row-79").is_none(),
|
|
||||||
"rows past the viewport never render"
|
|
||||||
);
|
|
||||||
|
|
||||||
state.update_in(&mut visual, |state, _window, _cx| state.scroll_to_line(79));
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("test-code-row-79").is_some(),
|
|
||||||
"scrolling brings the last row in"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("test-code-row-0").is_none(),
|
|
||||||
"the first row scrolled out"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn hovering_a_line_activates_it() {
|
|
||||||
let (cx, mut visual, state) = code_view_window(80);
|
|
||||||
|
|
||||||
let row = visual
|
|
||||||
.debug_bounds("test-code-row-3")
|
|
||||||
.expect("row 3 renders in the viewport");
|
|
||||||
visual.simulate_event(gpui::MouseMoveEvent {
|
|
||||||
position: row.center(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
let active = cx.update(|cx| state.read(cx).active_line());
|
|
||||||
assert_eq!(active, Some(3), "the hovered line becomes the active line");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn selecting_then_copying_writes_the_clipboard() {
|
|
||||||
let (cx, mut visual, state) = code_view_window(10);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.select_range(
|
|
||||||
gpui_native_editor::code::SelectionPoint { row: 0, col: 0 },
|
|
||||||
gpui_native_editor::code::SelectionPoint { row: 0, col: 3 },
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("cmd-c");
|
|
||||||
|
|
||||||
let clipboard = cx.update(|cx| {
|
|
||||||
cx.read_from_clipboard()
|
|
||||||
.and_then(|item| item.text().map(|text| text.to_string()))
|
|
||||||
});
|
|
||||||
assert_eq!(
|
|
||||||
clipboard.as_deref(),
|
|
||||||
Some("fn "),
|
|
||||||
"the copy chord copies the selection"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn update_line_replaces_a_single_line_and_rehighlights() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(|cx| {
|
|
||||||
gpui_native_theme::Theme::init(cx);
|
|
||||||
});
|
|
||||||
let state = cx.new(|cx| {
|
|
||||||
CodeViewState::new(
|
|
||||||
"test-code",
|
|
||||||
"fn a() {}\nfn b() {}\nfn c() {}\n",
|
|
||||||
Language::Rust,
|
|
||||||
cx,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.update_line(1, "struct S;", cx));
|
|
||||||
});
|
|
||||||
|
|
||||||
let (source, line_count, keyword_on_line_1) = cx.update(|cx| {
|
|
||||||
let state = state.read(cx);
|
|
||||||
(
|
|
||||||
state.source().to_string(),
|
|
||||||
state.code().lines.len(),
|
|
||||||
state.code().lines[1]
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.any(|span| span.role == Role::Keyword),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(source, "fn a() {}\nstruct S;\nfn c() {}\n");
|
|
||||||
assert_eq!(line_count, 4, "the line count survives the replacement");
|
|
||||||
assert!(keyword_on_line_1, "struct highlights as a keyword");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn update_line_splices_multiline_text() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(|cx| {
|
|
||||||
gpui_native_theme::Theme::init(cx);
|
|
||||||
});
|
|
||||||
let state =
|
|
||||||
cx.new(|cx| CodeViewState::new("test-code", "fn a() {}\nfn b() {}\n", Language::Rust, cx));
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.update_line(1, "one\ntwo\nthree", cx));
|
|
||||||
});
|
|
||||||
|
|
||||||
let source = cx.update(|cx| state.read(cx).source().to_string());
|
|
||||||
assert_eq!(source, "fn a() {}\none\ntwo\nthree\n");
|
|
||||||
}
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::hint::black_box;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, Modifiers, ParentElement as _, Render,
|
|
||||||
ScrollDelta, ScrollEvent, Styled as _, TestAppContext, VisualTestContext, Window, div, point,
|
|
||||||
px,
|
|
||||||
};
|
|
||||||
use gpui_native_editor::code::{CodeViewState, bind_code_view_keys};
|
|
||||||
use gpui_native_editor::highlight::Language;
|
|
||||||
|
|
||||||
/// Deterministic pseudo-Rust with mixed tokens, roughly `lines` lines long.
|
|
||||||
fn large_source(lines: usize) -> String {
|
|
||||||
let mut source = String::with_capacity(lines * 48);
|
|
||||||
let mut index = 0usize;
|
|
||||||
while source.lines().count() < lines {
|
|
||||||
let i = index;
|
|
||||||
source.push_str(&format!(
|
|
||||||
"// Section {i}: generated for the scroll performance test.\n"
|
|
||||||
));
|
|
||||||
source.push_str(&format!("struct Section{i} {{\n"));
|
|
||||||
source.push_str(" id: u64,\n");
|
|
||||||
source.push_str(" label: String,\n");
|
|
||||||
source.push_str(" scores: Vec<f64>,\n");
|
|
||||||
source.push_str("}\n\n");
|
|
||||||
source.push_str(&format!("impl Section{i} {{\n"));
|
|
||||||
source.push_str(" /// A one-line summary of this section.\n");
|
|
||||||
source.push_str(" fn describe(&self) -> String {\n");
|
|
||||||
source.push_str(&format!(
|
|
||||||
" format!(\"section {i}: {{}} at {{:?}}\", self.label, self.scores)\n"
|
|
||||||
));
|
|
||||||
source.push_str(" }\n");
|
|
||||||
source.push_str("}\n\n");
|
|
||||||
index += 1;
|
|
||||||
}
|
|
||||||
source
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
code: Entity<CodeViewState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(self.code.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn scrolling_a_large_file_is_not_pathologically_slow() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(|cx| {
|
|
||||||
gpui_native_theme::Theme::init(cx);
|
|
||||||
bind_code_view_keys(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let source = large_source(5000);
|
|
||||||
let started = Instant::now();
|
|
||||||
let (state, highlight_elapsed) = cx.update(|cx| {
|
|
||||||
let state = CodeViewState::new("bench-code", source.clone(), Language::Rust, cx);
|
|
||||||
(state, started.elapsed())
|
|
||||||
});
|
|
||||||
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<CodeViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let code = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(code.clone());
|
|
||||||
Host { code }
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let line_count = cx.update(|cx| state.read(cx).code().lines.len());
|
|
||||||
assert!(
|
|
||||||
(5000..=5020).contains(&line_count),
|
|
||||||
"the whole file highlighted, got {line_count} lines"
|
|
||||||
);
|
|
||||||
|
|
||||||
let started = Instant::now();
|
|
||||||
for _ in 0..30 {
|
|
||||||
visual.simulate_event(ScrollEvent {
|
|
||||||
position: point(px(300.), px(300.)),
|
|
||||||
delta: ScrollDelta::Pixels(point(px(0.), px(-240.))),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let scroll_elapsed = started.elapsed();
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"highlighting {line_count} lines took {highlight_elapsed:?}; 30 wheel frames over {} lines took {scroll_elapsed:?} ({:?} per frame)",
|
|
||||||
line_count,
|
|
||||||
scroll_elapsed / 30
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
black_box(highlight_elapsed) < Duration::from_secs(10),
|
|
||||||
"highlighting took {highlight_elapsed:?}"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
black_box(scroll_elapsed) < Duration::from_secs(10),
|
|
||||||
"scrolling 30 frames took {scroll_elapsed:?}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,439 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, MouseButton, ParentElement as _, Render,
|
|
||||||
Styled as _, TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_editor::diff::{DiffRow, DiffRowKind, DiffViewState};
|
|
||||||
use gpui_native_editor::highlight::{Language, Role};
|
|
||||||
|
|
||||||
const OLD: &str = "fn a() {}\nfn b() {}\nfn c() {}\n";
|
|
||||||
const NEW: &str = "fn a() {}\nfn x() {}\nfn y() {}\nfn c() {}\n";
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rows_mark_removals_additions_and_context_with_line_numbers() {
|
|
||||||
let rows = DiffViewState::diff_rows(OLD, NEW, Language::Rust);
|
|
||||||
|
|
||||||
let kinds: Vec<DiffRowKind> = rows.iter().map(|row| row.kind).collect();
|
|
||||||
assert_eq!(
|
|
||||||
kinds,
|
|
||||||
vec![
|
|
||||||
DiffRowKind::Context,
|
|
||||||
DiffRowKind::Removed,
|
|
||||||
DiffRowKind::Added,
|
|
||||||
DiffRowKind::Added,
|
|
||||||
DiffRowKind::Context,
|
|
||||||
]
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
rows.iter().all(|row| row.kind != DiffRowKind::Hunk),
|
|
||||||
"changes within one context window need no separator"
|
|
||||||
);
|
|
||||||
|
|
||||||
let removed = &rows[1];
|
|
||||||
assert_eq!(removed.old_number, Some(2));
|
|
||||||
assert_eq!(removed.new_number, None);
|
|
||||||
let removed_text: String = removed
|
|
||||||
.content
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.map(|span| span.text.as_ref())
|
|
||||||
.collect();
|
|
||||||
assert!(removed_text.contains("fn b() {}"));
|
|
||||||
|
|
||||||
let added = &rows[2];
|
|
||||||
assert_eq!(added.old_number, None);
|
|
||||||
assert_eq!(added.new_number, Some(2));
|
|
||||||
let added_text: String = added
|
|
||||||
.content
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.map(|span| span.text.as_ref())
|
|
||||||
.collect();
|
|
||||||
assert!(added_text.contains("fn x() {}"));
|
|
||||||
|
|
||||||
let context = &rows[0];
|
|
||||||
assert_eq!(context.old_number, Some(1));
|
|
||||||
assert_eq!(context.new_number, Some(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn hunk_separators_carry_the_collapsed_line_count_and_header() {
|
|
||||||
// Ten lines with only line 6 changed: three lines of context trim the
|
|
||||||
// leading unchanged region to three hidden lines.
|
|
||||||
let mut old = String::new();
|
|
||||||
let mut new = String::new();
|
|
||||||
for line in 0..10 {
|
|
||||||
old.push_str(&format!("line {line}\n"));
|
|
||||||
if line == 6 {
|
|
||||||
new.push_str(&format!("line {line} changed\n"));
|
|
||||||
} else {
|
|
||||||
new.push_str(&format!("line {line}\n"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let rows = DiffViewState::diff_rows(&old, &new, Language::Rust);
|
|
||||||
|
|
||||||
let separators: Vec<&DiffRow> = rows
|
|
||||||
.iter()
|
|
||||||
.filter(|row| row.kind == DiffRowKind::Hunk)
|
|
||||||
.collect();
|
|
||||||
assert_eq!(separators.len(), 1, "one collapsed region above the hunk");
|
|
||||||
assert_eq!(separators[0].hidden_lines, 3);
|
|
||||||
assert_eq!(separators[0].header, "@@ -4,7 +4,7 @@");
|
|
||||||
|
|
||||||
let changed = rows
|
|
||||||
.iter()
|
|
||||||
.find(|row| row.kind == DiffRowKind::Removed)
|
|
||||||
.expect("the changed line renders as a removal");
|
|
||||||
assert_eq!(changed.old_number, Some(7));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn word_changes_tint_only_the_changed_words() {
|
|
||||||
let rows = DiffViewState::diff_rows(OLD, NEW, Language::Rust);
|
|
||||||
|
|
||||||
let removed = &rows[1];
|
|
||||||
let removed_text: String = removed
|
|
||||||
.content
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.map(|span| span.text.as_ref())
|
|
||||||
.collect();
|
|
||||||
assert_eq!(removed.changed_ranges.len(), 1, "one word moved");
|
|
||||||
let range = &removed.changed_ranges[0];
|
|
||||||
assert_eq!(&removed_text[range.clone()], "b()");
|
|
||||||
|
|
||||||
let added = &rows[2];
|
|
||||||
let added_text: String = added
|
|
||||||
.content
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.map(|span| span.text.as_ref())
|
|
||||||
.collect();
|
|
||||||
assert_eq!(added.changed_ranges.len(), 1);
|
|
||||||
let range = &added.changed_ranges[0];
|
|
||||||
assert_eq!(&added_text[range.clone()], "x()");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn an_added_line_carries_its_highlight_spans() {
|
|
||||||
let rows = DiffViewState::diff_rows("", "fn zed() {}\n", Language::Rust);
|
|
||||||
let added = rows
|
|
||||||
.iter()
|
|
||||||
.find(|row| row.kind == DiffRowKind::Added)
|
|
||||||
.expect("the new file is all additions");
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
added
|
|
||||||
.content
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.any(|span| span.role == Role::Keyword),
|
|
||||||
"fn highlights as a keyword inside the diff row"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
diff: Entity<DiffViewState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(self.diff.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn diff_view_window(
|
|
||||||
lines: usize,
|
|
||||||
changed_lines: &[usize],
|
|
||||||
) -> (TestAppContext, VisualTestContext, Entity<DiffViewState>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let mut old = String::new();
|
|
||||||
let mut new = String::new();
|
|
||||||
for line in 0..lines {
|
|
||||||
old.push_str(&format!("line {line} unchanged\n"));
|
|
||||||
if changed_lines.contains(&line) {
|
|
||||||
new.push_str(&format!("line {line} rewritten\n"));
|
|
||||||
} else {
|
|
||||||
new.push_str(&format!("line {line} unchanged\n"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let state = DiffViewState::new("test-diff", old, new, Language::Rust);
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<DiffViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let diff = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(diff.clone());
|
|
||||||
Host { diff }
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn rows_render_virtualized_and_the_view_jumps_to_the_first_change() {
|
|
||||||
let changed: Vec<usize> = (10..200).step_by(10).collect();
|
|
||||||
let (_cx, mut visual, state) = diff_view_window(200, &changed);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("diff-row-0").is_some(),
|
|
||||||
"the first row renders"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("diff-row-100").is_none(),
|
|
||||||
"rows past the viewport never render"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Scroll to the bottom of the diff first, so the jump has somewhere to
|
|
||||||
// go.
|
|
||||||
visual.simulate_event(gpui::ScrollEvent {
|
|
||||||
position: point(px(500.), px(400.)),
|
|
||||||
delta: gpui::ScrollDelta::Pixels(point(px(0.), px(-5000.))),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("diff-row-0").is_none(),
|
|
||||||
"the view scrolled to the bottom"
|
|
||||||
);
|
|
||||||
|
|
||||||
state.update_in(&mut visual, |state, _window, _cx| {
|
|
||||||
state.scroll_to_first_change()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("diff-row-4").is_some(),
|
|
||||||
"the first change scrolled into view"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("diff-row-0").is_none(),
|
|
||||||
"the hunk header above it scrolled out"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds("diff-row-100").is_none(),
|
|
||||||
"rows past the viewport still never render"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn set_versions_re_diffs() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let state = cx.new(|_| DiffViewState::new("test-diff", OLD, NEW, Language::Rust));
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.set_versions("same\n", "same\n", Language::Rust, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
let empty = cx.update(|cx| state.read(cx).rows().is_empty());
|
|
||||||
assert!(empty, "identical versions have no diff to show");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn scrolling_horizontally_moves_rows_without_vertical_drift() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let old = "fn same() {}\n".repeat(40);
|
|
||||||
let new = format!("{old}fn long() {{ {} }}\n", "text ".repeat(300));
|
|
||||||
let state = DiffViewState::new("test-diff", old, new, Language::Rust);
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<DiffViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let diff = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(diff.clone());
|
|
||||||
Host { diff }
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let before = visual
|
|
||||||
.debug_bounds("diff-row-0")
|
|
||||||
.expect("the first row renders");
|
|
||||||
|
|
||||||
visual.simulate_event(gpui::ScrollEvent {
|
|
||||||
position: before.center(),
|
|
||||||
delta: gpui::ScrollDelta::Pixels(point(px(-200.), px(0.))),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let after = visual
|
|
||||||
.debug_bounds("diff-row-0")
|
|
||||||
.expect("the first row still renders");
|
|
||||||
assert!(
|
|
||||||
(after.origin.x - before.origin.x + px(200.)).abs() <= px(2.),
|
|
||||||
"a horizontal wheel gesture scrolls the rows left"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
after.origin.y, before.origin.y,
|
|
||||||
"a horizontal gesture must not drift vertically"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn scrolling_vertically_does_not_drift_horizontally() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
// Spread changes keep the diff taller than the viewport, so there is
|
|
||||||
// something to scroll vertically; the long line forces a horizontal bar.
|
|
||||||
let mut old = String::new();
|
|
||||||
let mut body = String::new();
|
|
||||||
for line in 0..200 {
|
|
||||||
old.push_str(&format!("fn same_{line}() {{}}\n"));
|
|
||||||
if line % 10 == 0 {
|
|
||||||
body.push_str(&format!("fn changed_{line}() {{}}\n"));
|
|
||||||
} else {
|
|
||||||
body.push_str(&format!("fn same_{line}() {{}}\n"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let new = format!("{body}fn long() {{ {} }}\n", "text ".repeat(300));
|
|
||||||
let state = DiffViewState::new("test-diff", old, new, Language::Rust);
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<DiffViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let diff = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(diff.clone());
|
|
||||||
Host { diff }
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let before = visual
|
|
||||||
.debug_bounds("diff-row-0")
|
|
||||||
.expect("the first row renders");
|
|
||||||
|
|
||||||
// A pure vertical gesture...
|
|
||||||
visual.simulate_event(gpui::ScrollEvent {
|
|
||||||
position: before.center(),
|
|
||||||
delta: gpui::ScrollDelta::Pixels(point(px(0.), px(-5.))),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
// ...then a vertical one carrying minor horizontal noise, below the
|
|
||||||
// threshold where gpui would unlock the gesture's axis.
|
|
||||||
visual.simulate_event(gpui::ScrollEvent {
|
|
||||||
position: before.center(),
|
|
||||||
delta: gpui::ScrollDelta::Pixels(point(px(2.), px(-5.))),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let after = visual
|
|
||||||
.debug_bounds("diff-row-0")
|
|
||||||
.expect("the first row still renders");
|
|
||||||
assert_eq!(
|
|
||||||
after.origin.x, before.origin.x,
|
|
||||||
"vertical gestures with horizontal noise must not scroll horizontally"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
(after.origin.y - (before.origin.y - px(10.))).abs() <= px(2.),
|
|
||||||
"the vertical component scrolls the rows up"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_the_vertical_thumb_leaves_the_horizontal_offset_alone() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
// Spread changes keep the diff taller than the viewport, so the vertical
|
|
||||||
// scrollbar exists; the long line forces the horizontal one.
|
|
||||||
let mut old = String::new();
|
|
||||||
let mut body = String::new();
|
|
||||||
for line in 0..200 {
|
|
||||||
old.push_str(&format!("fn same_{line}() {{}}\n"));
|
|
||||||
if line % 10 == 0 {
|
|
||||||
body.push_str(&format!("fn changed_{line}() {{}}\n"));
|
|
||||||
} else {
|
|
||||||
body.push_str(&format!("fn same_{line}() {{}}\n"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let new = format!("{body}fn long() {{ {} }}\n", "text ".repeat(300));
|
|
||||||
let state = DiffViewState::new("test-diff", old, new, Language::Rust);
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<DiffViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let diff = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(diff.clone());
|
|
||||||
Host { diff }
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Scroll the strip left first, so horizontal drift would be observable.
|
|
||||||
let row = visual
|
|
||||||
.debug_bounds("diff-row-0")
|
|
||||||
.expect("the first row renders");
|
|
||||||
visual.simulate_event(gpui::ScrollEvent {
|
|
||||||
position: row.center(),
|
|
||||||
delta: gpui::ScrollDelta::Pixels(point(px(-100.), px(0.))),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
let before = visual
|
|
||||||
.debug_bounds("diff-row-0")
|
|
||||||
.expect("the first row still renders");
|
|
||||||
|
|
||||||
// Press the vertical thumb: right edge, within its minimum length.
|
|
||||||
let (thumb_width, inset, min_thumb) = cx.update(|cx| {
|
|
||||||
let theme = gpui_native_theme::Theme::global(cx);
|
|
||||||
(
|
|
||||||
theme.scrollbar.width,
|
|
||||||
theme.scrollbar.inset,
|
|
||||||
theme.scrollbar.min_thumb,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
let press = point(
|
|
||||||
before.origin.x + before.size.width - thumb_width / 2.,
|
|
||||||
before.origin.y + inset + min_thumb / 2.,
|
|
||||||
);
|
|
||||||
visual.simulate_event(gpui::MouseDownEvent {
|
|
||||||
position: press,
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
// Move sideways while held; only the vertical thumb is being dragged.
|
|
||||||
visual.simulate_event(gpui::MouseMoveEvent {
|
|
||||||
position: press + point(px(30.), px(0.)),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let after = visual
|
|
||||||
.debug_bounds("diff-row-0")
|
|
||||||
.expect("the first row still renders");
|
|
||||||
assert_eq!(
|
|
||||||
after.origin.x, before.origin.x,
|
|
||||||
"dragging the vertical thumb must not scroll the strip horizontally"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
after.origin.y, before.origin.y,
|
|
||||||
"a sideways-only drag must not scroll vertically"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::hint::black_box;
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, Modifiers, ParentElement as _, Render,
|
|
||||||
ScrollDelta, ScrollEvent, Styled as _, TestAppContext, VisualTestContext, Window, div, point,
|
|
||||||
px,
|
|
||||||
};
|
|
||||||
use gpui_native_editor::diff::DiffViewState;
|
|
||||||
|
|
||||||
/// Deterministic old/new pairs of roughly `lines` lines each, with changes
|
|
||||||
/// sprinkled through.
|
|
||||||
fn large_diff_pair(lines: usize) -> (String, String) {
|
|
||||||
let mut old = String::with_capacity(lines * 48);
|
|
||||||
let mut new = String::with_capacity(lines * 48);
|
|
||||||
let mut index = 0usize;
|
|
||||||
while old.lines().count() < lines {
|
|
||||||
let i = index;
|
|
||||||
let comment = format!("// Handler {i} in the generated dispatch table.\n");
|
|
||||||
old.push_str(&comment);
|
|
||||||
new.push_str(&comment);
|
|
||||||
old.push_str(&format!("fn handle_{i}(payload: &str) -> bool {{\n"));
|
|
||||||
new.push_str(&format!("fn handle_{i}(payload: &Payload) -> bool {{\n"));
|
|
||||||
old.push_str(&format!(" payload.len() < {i} + 16\n"));
|
|
||||||
new.push_str(&format!(
|
|
||||||
" payload.size() < {i} + 16 && !payload.is_empty()\n"
|
|
||||||
));
|
|
||||||
if i.is_multiple_of(5) {
|
|
||||||
old.push_str(&format!(" log::debug(\"handler {i} ran\");\n"));
|
|
||||||
}
|
|
||||||
if i.is_multiple_of(7) {
|
|
||||||
new.push_str(" metrics::count(\"handle\");\n");
|
|
||||||
}
|
|
||||||
old.push_str("}\n\n");
|
|
||||||
new.push_str("}\n\n");
|
|
||||||
index += 1;
|
|
||||||
}
|
|
||||||
(old, new)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
diff: Entity<DiffViewState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(self.diff.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn scrolling_a_large_diff_is_not_pathologically_slow() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
|
|
||||||
let (old, new) = large_diff_pair(5000);
|
|
||||||
let started = Instant::now();
|
|
||||||
let state = DiffViewState::new(
|
|
||||||
"bench-diff",
|
|
||||||
&old,
|
|
||||||
&new,
|
|
||||||
gpui_native_editor::highlight::Language::Rust,
|
|
||||||
);
|
|
||||||
let diff_elapsed = started.elapsed();
|
|
||||||
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<DiffViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let diff = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(diff.clone());
|
|
||||||
Host { diff }
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let row_count = cx.update(|cx| state.read(cx).rows().len());
|
|
||||||
assert!(row_count > 5000, "changes add rows on top of the context");
|
|
||||||
|
|
||||||
let started = Instant::now();
|
|
||||||
for _ in 0..30 {
|
|
||||||
visual.simulate_event(ScrollEvent {
|
|
||||||
position: point(px(300.), px(300.)),
|
|
||||||
delta: ScrollDelta::Pixels(point(px(0.), px(-240.))),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let scroll_elapsed = started.elapsed();
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"diffing and highlighting into {row_count} rows took {diff_elapsed:?}; 30 wheel frames took {scroll_elapsed:?} ({:?} per frame)",
|
|
||||||
scroll_elapsed / 30
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
black_box(diff_elapsed) < Duration::from_secs(10),
|
|
||||||
"diffing took {diff_elapsed:?}"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
black_box(scroll_elapsed) < Duration::from_secs(10),
|
|
||||||
"scrolling 30 frames took {scroll_elapsed:?}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,950 +0,0 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, MouseButton,
|
|
||||||
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, Render, Styled as _,
|
|
||||||
TestAppContext, VisualTestContext, Window, div, point, px, rems,
|
|
||||||
};
|
|
||||||
use gpui_native_editor::highlight::Role;
|
|
||||||
use gpui_native_editor::markdown::{
|
|
||||||
InlineItem, InlineSpan, InlineStyle, ListMarker, MarkdownBlock, MarkdownHeading,
|
|
||||||
MarkdownViewState, TextView, bind_text_view_keys,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn items_text(items: &[InlineItem]) -> String {
|
|
||||||
items
|
|
||||||
.iter()
|
|
||||||
.filter_map(|item| match item {
|
|
||||||
InlineItem::Text(span) => Some(span.text.as_ref()),
|
|
||||||
InlineItem::Image { .. } => None,
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn spans_text(spans: &[InlineSpan]) -> String {
|
|
||||||
spans
|
|
||||||
.iter()
|
|
||||||
.map(|span| span.text.as_ref())
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn paragraph_of(blocks: &[MarkdownBlock], index: usize) -> (&Vec<InlineItem>, usize, bool) {
|
|
||||||
match &blocks[index] {
|
|
||||||
MarkdownBlock::Paragraph {
|
|
||||||
items,
|
|
||||||
indent,
|
|
||||||
quote,
|
|
||||||
} => (items, *indent, *quote),
|
|
||||||
other => panic!("block {index} is {other:?}, not a paragraph"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn item_of(blocks: &[MarkdownBlock], index: usize) -> (ListMarker, String, usize, bool) {
|
|
||||||
match &blocks[index] {
|
|
||||||
MarkdownBlock::ListItem {
|
|
||||||
marker,
|
|
||||||
items,
|
|
||||||
indent,
|
|
||||||
quote,
|
|
||||||
} => (*marker, items_text(items), *indent, *quote),
|
|
||||||
other => panic!("block {index} is {other:?}, not a list item"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn inline_styles_survive_nesting_and_merge_runs() {
|
|
||||||
let blocks = parse_body("a **bold *both* b** `code` [link](https://example.com) done");
|
|
||||||
assert_eq!(blocks.len(), 1);
|
|
||||||
let (items, ..) = paragraph_of(&blocks, 0);
|
|
||||||
|
|
||||||
let styled: Vec<(String, &InlineStyle)> = items
|
|
||||||
.iter()
|
|
||||||
.filter_map(|item| match item {
|
|
||||||
InlineItem::Text(span) => Some((span.text.to_string(), &span.style)),
|
|
||||||
InlineItem::Image { .. } => None,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
assert!(styled.contains(&("bold ".to_string(), &InlineStyle::Strong)));
|
|
||||||
assert!(
|
|
||||||
styled.contains(&("both".to_string(), &InlineStyle::StrongEmphasis)),
|
|
||||||
"strong wrapping emphasis combines into one style"
|
|
||||||
);
|
|
||||||
assert!(styled.contains(&(" b".to_string(), &InlineStyle::Strong)));
|
|
||||||
assert!(styled.contains(&("code".to_string(), &InlineStyle::Code)));
|
|
||||||
assert!(styled.contains(&(
|
|
||||||
"link".to_string(),
|
|
||||||
&InlineStyle::Link {
|
|
||||||
destination: "https://example.com".into()
|
|
||||||
}
|
|
||||||
)));
|
|
||||||
assert!(styled.contains(&("a ".to_string(), &InlineStyle::Text)));
|
|
||||||
assert!(styled.contains(&(" done".to_string(), &InlineStyle::Text)));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn headings_headings_and_thematic_breaks_parse() {
|
|
||||||
let blocks = parse_body("# Title\n\ntext\n\n---\n\n###### small");
|
|
||||||
assert_eq!(blocks.len(), 4);
|
|
||||||
assert!(matches!(
|
|
||||||
&blocks[0],
|
|
||||||
MarkdownBlock::Heading {
|
|
||||||
level: MarkdownHeading::H1,
|
|
||||||
..
|
|
||||||
}
|
|
||||||
));
|
|
||||||
assert!(matches!(&blocks[2], MarkdownBlock::ThematicBreak { .. }));
|
|
||||||
assert!(matches!(
|
|
||||||
&blocks[3],
|
|
||||||
MarkdownBlock::Heading {
|
|
||||||
level: MarkdownHeading::H6,
|
|
||||||
..
|
|
||||||
}
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn lists_number_order_and_nest() {
|
|
||||||
let source = "- one\n- two\n 1. inner\n 2. more\n3. third\n";
|
|
||||||
let blocks = parse_body(source);
|
|
||||||
|
|
||||||
let markers: Vec<ListMarker> = blocks
|
|
||||||
.iter()
|
|
||||||
.filter_map(|block| match block {
|
|
||||||
MarkdownBlock::ListItem { marker, .. } => Some(*marker),
|
|
||||||
_ => None,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
assert_eq!(
|
|
||||||
markers,
|
|
||||||
vec![
|
|
||||||
ListMarker::Bullet,
|
|
||||||
ListMarker::Bullet,
|
|
||||||
ListMarker::Ordered(1),
|
|
||||||
ListMarker::Ordered(2),
|
|
||||||
ListMarker::Ordered(3),
|
|
||||||
],
|
|
||||||
"ordered numbering continues across nested lists"
|
|
||||||
);
|
|
||||||
|
|
||||||
let (_, _, outer_indent, _) = item_of(&blocks, 0);
|
|
||||||
assert_eq!(outer_indent, 0);
|
|
||||||
let (_, _, inner_indent, _) = item_of(&blocks, 2);
|
|
||||||
assert_eq!(inner_indent, 1, "nested items indent one level deeper");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn tight_items_stay_tight_but_block_content_flushes_before_them() {
|
|
||||||
let source = "- item text\n\n second paragraph\n- next\n";
|
|
||||||
let blocks = parse_body(source);
|
|
||||||
|
|
||||||
// The first paragraph of a loose item carries its marker row; the
|
|
||||||
// continuation flushes as a plain paragraph at the same indent.
|
|
||||||
let (marker, text, indent, _) = item_of(&blocks, 0);
|
|
||||||
assert_eq!(marker, ListMarker::Bullet);
|
|
||||||
assert_eq!(text, "item text");
|
|
||||||
assert_eq!(indent, 0);
|
|
||||||
|
|
||||||
let (items, indent, _) = paragraph_of(&blocks, 1);
|
|
||||||
assert_eq!(items_text(items), "second paragraph");
|
|
||||||
assert_eq!(
|
|
||||||
indent, 1,
|
|
||||||
"loose item continuations indent under the item's text"
|
|
||||||
);
|
|
||||||
|
|
||||||
let (marker, text, ..) = item_of(&blocks, 2);
|
|
||||||
assert_eq!(marker, ListMarker::Bullet);
|
|
||||||
assert_eq!(text, "next");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn tight_item_content_after_a_nested_block_stays_in_the_item() {
|
|
||||||
let blocks = parse_body(
|
|
||||||
"- doc
|
|
||||||
```rust
|
|
||||||
fn x() {}
|
|
||||||
```
|
|
||||||
after
|
|
||||||
",
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
blocks.len(),
|
|
||||||
3,
|
|
||||||
"marker row, code, and one continuation row"
|
|
||||||
);
|
|
||||||
|
|
||||||
let (marker, text, indent, _) = item_of(&blocks, 0);
|
|
||||||
assert_eq!(marker, ListMarker::Bullet);
|
|
||||||
assert_eq!(text, "doc");
|
|
||||||
assert_eq!(indent, 0);
|
|
||||||
assert!(matches!(
|
|
||||||
&blocks[1],
|
|
||||||
MarkdownBlock::Code {
|
|
||||||
indent: 1,
|
|
||||||
quote: false,
|
|
||||||
..
|
|
||||||
}
|
|
||||||
));
|
|
||||||
|
|
||||||
let (items, indent, _) = paragraph_of(&blocks, 2);
|
|
||||||
assert_eq!(items_text(items), "after");
|
|
||||||
assert_eq!(indent, 1, "the continuation stays under the item's text");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn task_markers_do_not_leak_into_following_items() {
|
|
||||||
let blocks = parse_body("- [ ] todo\n- plain\n1. [x] done\n2. next\n");
|
|
||||||
assert_eq!(item_of(&blocks, 0).0, ListMarker::Task(false));
|
|
||||||
assert_eq!(item_of(&blocks, 1).0, ListMarker::Bullet);
|
|
||||||
assert_eq!(item_of(&blocks, 2).0, ListMarker::Task(true));
|
|
||||||
assert_eq!(item_of(&blocks, 3).0, ListMarker::Ordered(2));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn quoted_text_after_an_item_flushes_the_item_first() {
|
|
||||||
let blocks = parse_body("- text\n > quoted\n");
|
|
||||||
assert_eq!(blocks.len(), 2);
|
|
||||||
assert_eq!(item_of(&blocks, 0).1, "text");
|
|
||||||
let (_, indent, quote) = paragraph_of(&blocks, 1);
|
|
||||||
assert!(quote, "the quoted paragraph carries the quote flag");
|
|
||||||
assert_eq!(indent, 1, "quote content stays under the item's text");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_rule_after_item_text_keeps_its_order() {
|
|
||||||
let blocks = parse_body("- text\n ***\n");
|
|
||||||
assert_eq!(blocks.len(), 2);
|
|
||||||
assert_eq!(item_of(&blocks, 0).1, "text");
|
|
||||||
assert!(matches!(
|
|
||||||
&blocks[1],
|
|
||||||
MarkdownBlock::ThematicBreak {
|
|
||||||
indent: 1,
|
|
||||||
quote: false
|
|
||||||
}
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn nested_code_and_tables_carry_indent_and_quote() {
|
|
||||||
let blocks =
|
|
||||||
parse_body("- fenced\n\n ```rust\n fn x() {}\n ```\n\n> | a |\n> |---|\n> | b |\n");
|
|
||||||
assert_eq!(blocks.len(), 3);
|
|
||||||
|
|
||||||
let (marker, text, indent, _) = item_of(&blocks, 0);
|
|
||||||
assert_eq!(marker, ListMarker::Bullet);
|
|
||||||
assert_eq!(text, "fenced");
|
|
||||||
assert_eq!(indent, 0);
|
|
||||||
|
|
||||||
assert!(matches!(
|
|
||||||
&blocks[1],
|
|
||||||
MarkdownBlock::Code {
|
|
||||||
indent: 1,
|
|
||||||
quote: false,
|
|
||||||
..
|
|
||||||
}
|
|
||||||
));
|
|
||||||
assert!(matches!(
|
|
||||||
&blocks[2],
|
|
||||||
MarkdownBlock::Table {
|
|
||||||
indent: 0,
|
|
||||||
quote: true,
|
|
||||||
..
|
|
||||||
}
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn quotes_mark_their_paragraphs_and_lists() {
|
|
||||||
let blocks = parse_body("> quoted\n>\n> - listed\n\nplain");
|
|
||||||
let (_, _, quote) = paragraph_of(&blocks, 0);
|
|
||||||
assert!(quote, "quoted paragraphs carry the quote flag");
|
|
||||||
assert!(item_of(&blocks, 1).3, "quoted list items carry it too");
|
|
||||||
assert!(!paragraph_of(&blocks, 2).2);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "rust")]
|
|
||||||
#[test]
|
|
||||||
fn fenced_code_highlights_known_languages_and_keeps_the_info_string() {
|
|
||||||
let blocks = parse_body("```rust\nfn main() {}\n```\n");
|
|
||||||
assert_eq!(blocks.len(), 1);
|
|
||||||
|
|
||||||
let MarkdownBlock::Code {
|
|
||||||
language, lines, ..
|
|
||||||
} = &blocks[0]
|
|
||||||
else {
|
|
||||||
panic!("the first block is code");
|
|
||||||
};
|
|
||||||
assert_eq!(language.as_deref(), Some("rust"));
|
|
||||||
assert!(
|
|
||||||
lines
|
|
||||||
.lines
|
|
||||||
.iter()
|
|
||||||
.flat_map(|line| &line.spans)
|
|
||||||
.any(|span| span.role == Role::Keyword),
|
|
||||||
"rust fences highlight through the grammar"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn fenced_code_keeps_the_info_string_and_renders_unknown_languages_plain() {
|
|
||||||
let blocks = parse_body("```text\nplain\n```");
|
|
||||||
assert_eq!(blocks.len(), 1);
|
|
||||||
|
|
||||||
let MarkdownBlock::Code {
|
|
||||||
language, lines, ..
|
|
||||||
} = &blocks[0]
|
|
||||||
else {
|
|
||||||
panic!("the block is code");
|
|
||||||
};
|
|
||||||
assert_eq!(language.as_deref(), Some("text"));
|
|
||||||
assert!(
|
|
||||||
lines
|
|
||||||
.lines
|
|
||||||
.iter()
|
|
||||||
.flat_map(|line| &line.spans)
|
|
||||||
.all(|span| span.role == Role::Text),
|
|
||||||
"unknown languages render plain"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn tables_parse_header_first_with_column_positions() {
|
|
||||||
let blocks = parse_body("| a | b |\n|---|---|\n| 1 | 2 |\n| 3 | 4 |\n");
|
|
||||||
let MarkdownBlock::Table { table, .. } = &blocks[0] else {
|
|
||||||
panic!("the block is a table");
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(table.rows.len(), 3);
|
|
||||||
assert!(table.rows[0].header);
|
|
||||||
assert!(!table.rows[1].header);
|
|
||||||
|
|
||||||
let header: Vec<String> = table.rows[0]
|
|
||||||
.cells
|
|
||||||
.iter()
|
|
||||||
.map(|cell| spans_text(&cell.spans))
|
|
||||||
.collect();
|
|
||||||
assert_eq!(header, vec!["a", "b"]);
|
|
||||||
assert_eq!(spans_text(&table.rows[2].cells[1].spans), "4");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn images_render_inline_and_standalone_and_html_renders_nothing() {
|
|
||||||
// An image alone in its paragraph becomes a standalone image block.
|
|
||||||
let blocks = parse_body("");
|
|
||||||
assert_eq!(blocks.len(), 1);
|
|
||||||
let MarkdownBlock::Image { source, alt, .. } = &blocks[0] else {
|
|
||||||
panic!("the block is an image");
|
|
||||||
};
|
|
||||||
assert_eq!(source.as_ref(), "logo.png");
|
|
||||||
assert_eq!(alt.as_ref(), "the logo");
|
|
||||||
|
|
||||||
// Two consecutive images in one paragraph become two blocks.
|
|
||||||
let blocks = parse_body("\n");
|
|
||||||
assert_eq!(blocks.len(), 2);
|
|
||||||
assert_eq!(
|
|
||||||
blocks
|
|
||||||
.iter()
|
|
||||||
.filter(|block| matches!(block, MarkdownBlock::Image { .. }))
|
|
||||||
.count(),
|
|
||||||
2
|
|
||||||
);
|
|
||||||
|
|
||||||
// An image among text renders as an inline atom; the spaces around it
|
|
||||||
// stay in their runs, and its alt text is dropped.
|
|
||||||
let blocks = parse_body("see  now\n\n<div>hidden</div>\n");
|
|
||||||
assert_eq!(blocks.len(), 1, "the html block renders no block at all");
|
|
||||||
let (items, ..) = paragraph_of(&blocks, 0);
|
|
||||||
assert_eq!(items_text(items), "see now");
|
|
||||||
assert_eq!(
|
|
||||||
items
|
|
||||||
.iter()
|
|
||||||
.filter(|item| matches!(item, InlineItem::Image { .. }))
|
|
||||||
.count(),
|
|
||||||
1
|
|
||||||
);
|
|
||||||
|
|
||||||
// Images inside headings and table cells fall back to their alt text.
|
|
||||||
let blocks = parse_body("#  head\n\n|  |\n|---|\n| x |\n");
|
|
||||||
let MarkdownBlock::Heading { spans, .. } = &blocks[0] else {
|
|
||||||
panic!("the first block is a heading");
|
|
||||||
};
|
|
||||||
assert!(
|
|
||||||
spans_text(spans).contains("alt h"),
|
|
||||||
"the heading image renders as its alt text"
|
|
||||||
);
|
|
||||||
let MarkdownBlock::Table { table, .. } = &blocks[1] else {
|
|
||||||
panic!("the second block is a table");
|
|
||||||
};
|
|
||||||
let cell_text: String = table.rows[0].cells[0]
|
|
||||||
.spans
|
|
||||||
.iter()
|
|
||||||
.map(|span| span.text.as_ref())
|
|
||||||
.collect();
|
|
||||||
assert_eq!(cell_text, "cell");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn set_source_reparses_and_blocks_reset() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let state = cx.new(|_| MarkdownViewState::new("test-md", "# old\n"));
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.set_source("- a\n- b\n", cx));
|
|
||||||
});
|
|
||||||
|
|
||||||
let (source, count) = cx.update(|cx| {
|
|
||||||
let state = state.read(cx);
|
|
||||||
(state.source().to_string(), state.blocks().len())
|
|
||||||
});
|
|
||||||
assert_eq!(source, "- a\n- b\n");
|
|
||||||
assert_eq!(count, 2, "the block list reflects the new source");
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Host {
|
|
||||||
view: Entity<MarkdownViewState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Host {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(self.view.clone())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn view_window(source: &str) -> (TestAppContext, VisualTestContext, Entity<MarkdownViewState>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let state = MarkdownViewState::new("test-md", source);
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<MarkdownViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let view = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(view.clone());
|
|
||||||
Host { view }
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The stable id string of the block at `block_index`. Leaks so the id can
|
|
||||||
/// feed `debug_bounds`, which requires `&'static str`.
|
|
||||||
fn block_id(state: &Entity<MarkdownViewState>, cx: &App, block_index: usize) -> &'static str {
|
|
||||||
Box::<str>::leak(
|
|
||||||
state
|
|
||||||
.read(cx)
|
|
||||||
.block_id(block_index)
|
|
||||||
.expect("the block has an id")
|
|
||||||
.to_string()
|
|
||||||
.into_boxed_str(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn blocks_render_virtualized_and_scroll_to_a_block() {
|
|
||||||
let mut source = String::new();
|
|
||||||
for paragraph in 0..200 {
|
|
||||||
source.push_str(&format!("paragraph {paragraph} with some body text\n\n"));
|
|
||||||
}
|
|
||||||
let (_cx, mut visual, state) = view_window(&source);
|
|
||||||
|
|
||||||
let first_id = _cx.update(|cx| block_id(&state, cx, 0));
|
|
||||||
let target_id = _cx.update(|cx| block_id(&state, cx, 150));
|
|
||||||
let last_id = _cx.update(|cx| block_id(&state, cx, 199));
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds(first_id).is_some(),
|
|
||||||
"the first block renders"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds(target_id).is_none(),
|
|
||||||
"blocks past the viewport never render"
|
|
||||||
);
|
|
||||||
|
|
||||||
state.update_in(&mut visual, |state, _window, _cx| {
|
|
||||||
state.scroll_to_block(150)
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds(target_id).is_some(),
|
|
||||||
"scrolling brings the target block in"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds(first_id).is_none(),
|
|
||||||
"the first block scrolled out"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds(last_id).is_none(),
|
|
||||||
"blocks past the target stay unrendered"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn indented_blocks_render_inside_the_frame_text_style() {
|
|
||||||
let (_cx, mut visual, state) =
|
|
||||||
view_window("- top task\n - nested task\n\n1. outer\n 1. inner\n");
|
|
||||||
|
|
||||||
let ids = _cx.update(|cx| [0, 1, 2, 3].map(|index| block_id(&state, cx, index)));
|
|
||||||
let mut height = |id: &'static str| {
|
|
||||||
visual
|
|
||||||
.debug_bounds(id)
|
|
||||||
.map(|bounds| bounds.size.height)
|
|
||||||
.expect("the block renders")
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
height(ids[0]),
|
|
||||||
height(ids[1]),
|
|
||||||
"the nested task must keep the frame's text metrics"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
height(ids[2]),
|
|
||||||
height(ids[3]),
|
|
||||||
"the nested ordered item must keep the frame's text metrics"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_link_opens_its_destination() {
|
|
||||||
let (_cx, mut visual, state) = view_window("[click me](https://example.com/welcome)");
|
|
||||||
|
|
||||||
let first_id = _cx.update(|cx| block_id(&state, cx, 0));
|
|
||||||
let block = visual
|
|
||||||
.debug_bounds(first_id)
|
|
||||||
.expect("the paragraph renders");
|
|
||||||
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position: point(block.left() + px(4.), block.center().y),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
position: point(block.left() + px(4.), block.center().y),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
visual.opened_url().as_deref(),
|
|
||||||
Some("https://example.com/welcome"),
|
|
||||||
"the link's destination opens through the platform"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parses without an app context; the view reuses this path.
|
|
||||||
fn parse_body(source: &str) -> Vec<MarkdownBlock> {
|
|
||||||
let state = MarkdownViewState::new("parse-md", source);
|
|
||||||
state.blocks().to_vec()
|
|
||||||
}
|
|
||||||
/// Mirrors the gallery's "Large file" sample: five blocks per section.
|
|
||||||
fn large_markdown(sections: usize) -> String {
|
|
||||||
let mut source = String::new();
|
|
||||||
for section in 0..sections {
|
|
||||||
source.push_str(&format!("## Section {section}\n\n"));
|
|
||||||
source.push_str(&format!(
|
|
||||||
"Handler {section} retries stale sockets and logs every outcome.\n\n"
|
|
||||||
));
|
|
||||||
source.push_str(&format!(
|
|
||||||
"- [ ] Audit handler {section}\n- See [issue {section}](https://example.com/issues/{section})\n\n"
|
|
||||||
));
|
|
||||||
source.push_str("```rust\nfn handle() -> bool { true }\n```\n\n");
|
|
||||||
}
|
|
||||||
source
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_large_document_swaps_and_renders_without_freezing() {
|
|
||||||
let source = large_markdown(1000);
|
|
||||||
let started = std::time::Instant::now();
|
|
||||||
|
|
||||||
let (_cx, mut visual, state) = view_window("# small\n");
|
|
||||||
state.update_in(&mut visual, |state, _window, cx| {
|
|
||||||
state.set_source(source, cx)
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let elapsed = std::hint::black_box(started.elapsed());
|
|
||||||
assert!(
|
|
||||||
elapsed < std::time::Duration::from_secs(30),
|
|
||||||
"swapping to a large document took {elapsed:?}; the frame must not
|
|
||||||
measure every block"
|
|
||||||
);
|
|
||||||
|
|
||||||
let count = _cx.update(|cx| state.read(cx).blocks().len());
|
|
||||||
assert_eq!(count, 5000, "five blocks per section");
|
|
||||||
|
|
||||||
let first_id = _cx.update(|cx| block_id(&state, cx, 0));
|
|
||||||
let last_id = _cx.update(|cx| block_id(&state, cx, 4999));
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds(first_id).is_some(),
|
|
||||||
"the first block renders after the swap"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
visual.debug_bounds(last_id).is_none(),
|
|
||||||
"blocks past the viewport never render"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn block_ids_survive_prepending_and_edits_around_them() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let state = cx.new(|_| MarkdownViewState::new("feed", "first\n\nsecond\n"));
|
|
||||||
|
|
||||||
let (first, second) = cx.update(|cx| {
|
|
||||||
let state = state.read(cx);
|
|
||||||
(
|
|
||||||
state.block_id(0).expect("the first block has an id"),
|
|
||||||
state.block_id(1).expect("the second block has an id"),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.set_source("new\n\nfirst\n\nsecond\n", cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
let state = state.read(cx);
|
|
||||||
assert_eq!(
|
|
||||||
state.block_id(1),
|
|
||||||
Some(first.clone()),
|
|
||||||
"prepending a block keeps the id of the old first block"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
state.block_id(2),
|
|
||||||
Some(second.clone()),
|
|
||||||
"prepending a block keeps the id of the old second block"
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Editing a block's text changes only that block's id.
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.set_source("new\n\nfirst edited\n\nsecond\n", cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
cx.update(|cx| {
|
|
||||||
let state = state.read(cx);
|
|
||||||
assert_eq!(
|
|
||||||
state.block_id(2),
|
|
||||||
Some(second),
|
|
||||||
"the untouched block keeps its id"
|
|
||||||
);
|
|
||||||
assert_ne!(
|
|
||||||
state.block_id(1),
|
|
||||||
Some(first),
|
|
||||||
"the edited block gets a new id"
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn selection_spans_pieces_and_images_contribute_no_text() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let state = cx.new(|_| {
|
|
||||||
MarkdownViewState::new(
|
|
||||||
"sel",
|
|
||||||
"alpha\n\nbravo  charlie\n\n- delta\n- echo\n",
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
let selected = cx.update(|cx| {
|
|
||||||
state.update(cx, |view, cx| {
|
|
||||||
view.select_range(0, 30, cx);
|
|
||||||
view.selected_text()
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
selected.as_deref(),
|
|
||||||
Some("alpha\nbravo \n charlie\ndelta\nec"),
|
|
||||||
"pieces join with newlines and images contribute no text"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn selection_is_empty_past_the_document_end_and_clears_on_reparse() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let state = cx.new(|_| MarkdownViewState::new("sel", "only\n"));
|
|
||||||
|
|
||||||
let (overshoot, after_reparse) = cx.update(|cx| {
|
|
||||||
state.update(cx, |view, cx| {
|
|
||||||
view.select_range(0, 10_000, cx);
|
|
||||||
let overshoot = view.selected_text();
|
|
||||||
view.set_source("other\n", cx);
|
|
||||||
(overshoot, view.selected_text())
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
overshoot.as_deref(),
|
|
||||||
Some("only"),
|
|
||||||
"ranges clamp to the document"
|
|
||||||
);
|
|
||||||
assert_eq!(after_reparse, None, "reparsing clears the selection");
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TextViewHost {
|
|
||||||
link_handler: Rc<RefCell<Vec<String>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for TextViewHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let clicks = self.link_handler.clone();
|
|
||||||
div()
|
|
||||||
.debug_selector(|| "text-view".to_string())
|
|
||||||
.size_full()
|
|
||||||
.child(
|
|
||||||
TextView::markdown(
|
|
||||||
"text-view",
|
|
||||||
"[a very long link label that fills the line](https://example.com/welcome)",
|
|
||||||
)
|
|
||||||
.on_link_click(move |url, _window, _cx| {
|
|
||||||
clicks.borrow_mut().push(url.to_string());
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn text_view_clicks_route_through_the_link_handler() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let clicks: Rc<RefCell<Vec<String>>> = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let clicks_cell = clicks.clone();
|
|
||||||
let window = cx.add_window(move |_, _| TextViewHost {
|
|
||||||
link_handler: clicks_cell,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("text-view")
|
|
||||||
.expect("the text view renders");
|
|
||||||
let position = point(bounds.left() + px(4.), bounds.top() + px(10.));
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position,
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
position,
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
clicks.borrow().as_slice(),
|
|
||||||
["https://example.com/welcome".to_string()],
|
|
||||||
"the click lands on the link and routes through the handler"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SelectionHost {
|
|
||||||
state: Entity<MarkdownViewState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for SelectionHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().child(div().w(rems(30.)).child(TextView::new(&self.state)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn selection_window(
|
|
||||||
source: &str,
|
|
||||||
) -> (TestAppContext, VisualTestContext, Entity<MarkdownViewState>) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
cx.update(bind_text_view_keys);
|
|
||||||
let state = MarkdownViewState::new("sel-view", source);
|
|
||||||
let state_cell: Rc<RefCell<Option<Entity<MarkdownViewState>>>> = Rc::new(RefCell::new(None));
|
|
||||||
let cell = state_cell.clone();
|
|
||||||
let window = cx.add_window(move |_, cx| {
|
|
||||||
let view = cx.new(|_| state);
|
|
||||||
cell.borrow_mut().replace(view.clone());
|
|
||||||
SelectionHost { state: view }
|
|
||||||
});
|
|
||||||
let state = state_cell.borrow().clone().expect("state captured");
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, visual, state)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Leaks a block id so it can feed `debug_bounds`, which needs `&'static str`.
|
|
||||||
fn leaked_block_id(
|
|
||||||
state: &Entity<MarkdownViewState>,
|
|
||||||
cx: &App,
|
|
||||||
block_index: usize,
|
|
||||||
) -> &'static str {
|
|
||||||
Box::<str>::leak(
|
|
||||||
state
|
|
||||||
.read(cx)
|
|
||||||
.block_id(block_index)
|
|
||||||
.expect("the block has an id")
|
|
||||||
.to_string()
|
|
||||||
.into_boxed_str(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn click(visual: &mut VisualTestContext, position: gpui::Point<gpui::Pixels>) {
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position,
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
position,
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_across_paragraphs_selects_and_copies() {
|
|
||||||
let (cx, mut visual, state) = selection_window("alpha bravo\n\ncharlie delta\n");
|
|
||||||
|
|
||||||
let first = cx.update(|cx| leaked_block_id(&state, cx, 0));
|
|
||||||
let second = cx.update(|cx| leaked_block_id(&state, cx, 1));
|
|
||||||
|
|
||||||
let start = {
|
|
||||||
let bounds = visual.debug_bounds(first).expect("the first block renders");
|
|
||||||
gpui::point(bounds.left() + px(4.), bounds.top() + px(8.))
|
|
||||||
};
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position: start,
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
let end = {
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds(second)
|
|
||||||
.expect("the second block renders");
|
|
||||||
gpui::point(bounds.left() + px(40.), bounds.top() + px(8.))
|
|
||||||
};
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: end,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
position: end,
|
|
||||||
button: MouseButton::Left,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
let selected = cx.update(|cx| state.read(cx).selected_text());
|
|
||||||
assert_eq!(
|
|
||||||
selected.as_deref(),
|
|
||||||
Some("alpha bravo\ncharli"),
|
|
||||||
"the drag selects from the press through the pointer"
|
|
||||||
);
|
|
||||||
|
|
||||||
visual.dispatch_action(gpui_native_editor::markdown::Copy);
|
|
||||||
let clipboard = cx.update(|cx| cx.read_from_clipboard().and_then(|item| item.text()));
|
|
||||||
assert_eq!(
|
|
||||||
clipboard.as_deref(),
|
|
||||||
selected.as_deref(),
|
|
||||||
"the copy action copies the selected text"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TaskHost {
|
|
||||||
toggles: Rc<RefCell<Vec<(usize, bool)>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for TaskHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let toggles = self.toggles.clone();
|
|
||||||
div().size_full().child(
|
|
||||||
TextView::markdown("task-view", "- [ ] first\n- [x] second\n").on_task_toggle(
|
|
||||||
move |block, checked, _window, _cx| {
|
|
||||||
toggles.borrow_mut().push((block, checked));
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_task_checkbox_toggles_it_through_the_handler() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let toggles: Rc<RefCell<Vec<(usize, bool)>>> = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let toggles_cell = toggles.clone();
|
|
||||||
let window = cx.add_window(move |_, _| TaskHost {
|
|
||||||
toggles: toggles_cell,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("task-toggle-0")
|
|
||||||
.expect("the task renders");
|
|
||||||
click(
|
|
||||||
&mut visual,
|
|
||||||
gpui::point(bounds.center().x, bounds.center().y),
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
toggles.borrow().as_slice(),
|
|
||||||
[(0, true)],
|
|
||||||
"checking the first task reports its block and the new state"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct CodeHost;
|
|
||||||
|
|
||||||
impl Render for CodeHost {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(TextView::markdown(
|
|
||||||
"code-host",
|
|
||||||
"```text\nfn main() {}\n```\n",
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_code_block_copy_button_copies_the_fenced_source() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(gpui_native_theme::Theme::init);
|
|
||||||
let window = cx.add_window(|_, _| CodeHost);
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("copy-code")
|
|
||||||
.expect("the copy button renders");
|
|
||||||
click(
|
|
||||||
&mut visual,
|
|
||||||
gpui::point(bounds.center().x, bounds.center().y),
|
|
||||||
);
|
|
||||||
|
|
||||||
let clipboard = cx.update(|cx| cx.read_from_clipboard().and_then(|item| item.text()));
|
|
||||||
assert_eq!(
|
|
||||||
clipboard.as_deref(),
|
|
||||||
Some("fn main() {}"),
|
|
||||||
"the copy button copies the fenced source"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,544 +0,0 @@
|
|||||||
#![cfg(feature = "dock")]
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, Modifiers, MouseButton,
|
|
||||||
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, Pixels, Render, Styled as _,
|
|
||||||
TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_layout::dock::{Dock, DockSidebar, DockState, PanelDescriptor, SplitDirection};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
use std::cell::Cell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
struct Panel {
|
|
||||||
tag: &'static str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Panel {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.debug_selector(move || format!("panel-{}", self.tag))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The right sidebar view a panel declares.
|
|
||||||
struct SidePanel {
|
|
||||||
tag: &'static str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for SidePanel {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.debug_selector(move || format!("side-{}", self.tag))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn descriptor(tag: &'static str) -> PanelDescriptor {
|
|
||||||
PanelDescriptor::new(tag, move |_, cx| cx.new(|_| Panel { tag }).into()).title(tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn descriptor_with_right_sidebar(tag: &'static str) -> PanelDescriptor {
|
|
||||||
PanelDescriptor::new(tag, move |_, cx| cx.new(|_| Panel { tag }).into())
|
|
||||||
.title(tag)
|
|
||||||
.right_sidebar(move |_, cx| cx.new(|_| SidePanel { tag }).into())
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
dock: Entity<DockState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(
|
|
||||||
&mut self,
|
|
||||||
_window: &mut Window,
|
|
||||||
_cx: &mut gpui::Context<Self>,
|
|
||||||
) -> impl gpui::IntoElement {
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.child(Dock::new("test-dock", self.dock.clone()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dock_window(cx: &mut TestAppContext) -> (Entity<DockState>, VisualTestContext) {
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let dock = cx.update(DockState::new);
|
|
||||||
let dock_for_window = dock.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
dock: dock_for_window,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), cx);
|
|
||||||
redraw(&mut visual);
|
|
||||||
(dock, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn redraw(visual: &mut VisualTestContext) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bounds(visual: &mut VisualTestContext, selector: &str) -> Option<gpui::Bounds<Pixels>> {
|
|
||||||
visual.debug_bounds(Box::leak(selector.to_string().into_boxed_str()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn center_of(bounds: gpui::Bounds<Pixels>) -> gpui::Point<Pixels> {
|
|
||||||
point(bounds.center().x, bounds.center().y)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn press(visual: &mut VisualTestContext, at: gpui::Point<Pixels>, click_count: usize) {
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position: at,
|
|
||||||
click_count,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
first_mouse: false,
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
position: at,
|
|
||||||
click_count,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn drag(visual: &mut VisualTestContext, from: gpui::Point<Pixels>, to: gpui::Point<Pixels>) {
|
|
||||||
visual.simulate_mouse_down(from, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: to,
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_mouse_up(to, MouseButton::Left, Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register(dock: &Entity<DockState>, tags: &[&'static str], cx: &mut TestAppContext) {
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, _cx| {
|
|
||||||
for tag in tags {
|
|
||||||
dock.register_panel(descriptor(tag));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn open(visual: &mut VisualTestContext, dock: &Entity<DockState>, tag: &'static str) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.open_panel(tag, window, cx));
|
|
||||||
});
|
|
||||||
redraw(visual);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn opening_a_panel_adds_its_tab_and_activates_it() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "panel-inbox").is_some(),
|
|
||||||
"opened panel missing"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_some(),
|
|
||||||
"tab missing"
|
|
||||||
);
|
|
||||||
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
assert!(bounds(&mut visual, "panel-explore").is_some());
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "panel-inbox").is_none(),
|
|
||||||
"the inactive view must not render"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_some(),
|
|
||||||
"the strip keeps both tabs"
|
|
||||||
);
|
|
||||||
|
|
||||||
assert!(dock.read_with(&cx, |dock, _| dock.is_open("inbox")));
|
|
||||||
assert!(dock.read_with(&cx, |dock, _| dock.is_open("explore")));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn clicking_a_tab_activates_its_panel() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
|
|
||||||
let tab = bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").expect("inbox tab missing");
|
|
||||||
press(&mut visual, center_of(tab), 1);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(bounds(&mut visual, "panel-inbox").is_some());
|
|
||||||
assert!(bounds(&mut visual, "panel-explore").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_tab_close_control_closes_its_panel() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
|
|
||||||
let close = bounds(&mut visual, "test-dock-pane-0-tabs-close-inbox").expect("close missing");
|
|
||||||
press(&mut visual, center_of(close), 1);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_none());
|
|
||||||
assert!(bounds(&mut visual, "panel-explore").is_some());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn closing_the_active_tab_activates_the_neighbor() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.close_panel("explore", cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "panel-inbox").is_some(),
|
|
||||||
"the neighbor tab should activate"
|
|
||||||
);
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-explore").is_none());
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_some());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn splitting_moves_the_tab_and_closing_collapses() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| {
|
|
||||||
dock.split_panel("explore", SplitDirection::Right, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "resize-handle").is_some(),
|
|
||||||
"the split renders a seam"
|
|
||||||
);
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0").is_some());
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-1").is_some());
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "panel-explore").is_some(),
|
|
||||||
"the moved tab activates in its new pane"
|
|
||||||
);
|
|
||||||
|
|
||||||
// Closing the new pane's only tab collapses its split.
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.close_panel("explore", cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(bounds(&mut visual, "resize-handle").is_none());
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-1").is_none());
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0").is_some());
|
|
||||||
assert!(bounds(&mut visual, "panel-inbox").is_some());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn splitting_at_a_target_pane_places_the_new_pane_there() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| {
|
|
||||||
dock.split_panel("explore", SplitDirection::Right, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
// Move inbox into a new pane left of explore's pane (pane 1), not beside
|
|
||||||
// inbox's own (pane 0, which empties and collapses).
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| {
|
|
||||||
dock.split_panel_at("inbox", "explore", SplitDirection::Left, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let moved = bounds(&mut visual, "test-dock-pane-3").expect("the new pane missing");
|
|
||||||
let target = bounds(&mut visual, "test-dock-pane-1").expect("target pane missing");
|
|
||||||
// The flex split may round the shared seam by a hair.
|
|
||||||
assert!(
|
|
||||||
moved.right() <= target.left() + px(1.),
|
|
||||||
"the new pane must sit left of the anchored pane"
|
|
||||||
);
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_the_seam_resizes_and_double_click_resets() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| {
|
|
||||||
dock.split_panel("explore", SplitDirection::Right, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let before_0 = bounds(&mut visual, "test-dock-pane-0").expect("pane 0 missing");
|
|
||||||
let before_1 = bounds(&mut visual, "test-dock-pane-1").expect("pane 1 missing");
|
|
||||||
let seam = bounds(&mut visual, "resize-handle").expect("seam missing");
|
|
||||||
|
|
||||||
drag(
|
|
||||||
&mut visual,
|
|
||||||
center_of(seam),
|
|
||||||
center_of(seam) + point(px(40.), px(0.)),
|
|
||||||
);
|
|
||||||
let grown = bounds(&mut visual, "test-dock-pane-0").expect("pane 0 missing");
|
|
||||||
let shrunk = bounds(&mut visual, "test-dock-pane-1").expect("pane 1 missing");
|
|
||||||
assert!(
|
|
||||||
grown.size.width > before_0.size.width,
|
|
||||||
"the seam leads pane 0"
|
|
||||||
);
|
|
||||||
assert!(shrunk.size.width < before_1.size.width, "pane 1 gives way");
|
|
||||||
|
|
||||||
// Double-clicking the seam restores the default ratio.
|
|
||||||
let seam = bounds(&mut visual, "resize-handle").expect("seam missing");
|
|
||||||
press(&mut visual, center_of(seam), 2);
|
|
||||||
redraw(&mut visual);
|
|
||||||
let reset_0 = bounds(&mut visual, "test-dock-pane-0").expect("pane 0 missing");
|
|
||||||
let reset_1 = bounds(&mut visual, "test-dock-pane-1").expect("pane 1 missing");
|
|
||||||
// The flex split may round an odd-pixel span by a hair.
|
|
||||||
assert!((reset_0.size.width - reset_1.size.width).abs() <= px(1.));
|
|
||||||
|
|
||||||
// A huge drag clamps instead of vanishing a side.
|
|
||||||
let seam = bounds(&mut visual, "resize-handle").expect("seam missing");
|
|
||||||
drag(
|
|
||||||
&mut visual,
|
|
||||||
center_of(seam),
|
|
||||||
center_of(seam) + point(px(2000.), px(0.)),
|
|
||||||
);
|
|
||||||
let clamped = bounds(&mut visual, "test-dock-pane-1").expect("clamped pane missing");
|
|
||||||
assert!(clamped.size.width > px(0.));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_a_tab_to_a_pane_edge_splits_it() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
|
|
||||||
// Drag explore's tab onto the right edge band of the pane's content.
|
|
||||||
let tab = bounds(&mut visual, "test-dock-pane-0-tabs-tab-explore").expect("tab missing");
|
|
||||||
let content = bounds(&mut visual, "test-dock-content-0").expect("content missing");
|
|
||||||
let drop_at = point(
|
|
||||||
content.right() - content.size.width * 0.125,
|
|
||||||
content.center().y,
|
|
||||||
);
|
|
||||||
|
|
||||||
visual.simulate_mouse_down(center_of(tab), MouseButton::Left, Modifiers::default());
|
|
||||||
// Past the drag threshold, the drag starts and the drop zones mount.
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: center_of(tab) + point(px(8.), px(2.)),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: drop_at,
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_mouse_up(drop_at, MouseButton::Left, Modifiers::default());
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "resize-handle").is_some(),
|
|
||||||
"the drop should have split the pane"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "test-dock-pane-1-tabs-tab-explore").is_some(),
|
|
||||||
"the dragged tab lives in the new pane"
|
|
||||||
);
|
|
||||||
assert!(bounds(&mut visual, "panel-explore").is_some());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn moving_a_panel_merges_panes_and_collapses_the_source() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| {
|
|
||||||
dock.split_panel("explore", SplitDirection::Right, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.move_panel("explore", "inbox", cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "resize-handle").is_none(),
|
|
||||||
"the emptied source pane collapses"
|
|
||||||
);
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_some());
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-explore").is_some());
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "panel-explore").is_some(),
|
|
||||||
"the moved tab activates in its new pane"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn close_others_keeps_one_and_close_pane_empties() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(&mut cx);
|
|
||||||
register(&dock, &["inbox", "explore", "settings"], &mut cx);
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
open(&mut visual, &dock, "settings");
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.close_others("inbox", cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_some());
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-explore").is_none());
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-settings").is_none());
|
|
||||||
assert!(bounds(&mut visual, "panel-inbox").is_some());
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.close_pane("inbox", cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_none());
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "panel-inbox").is_none(),
|
|
||||||
"the emptied pane renders nothing"
|
|
||||||
);
|
|
||||||
|
|
||||||
// The dock is reusable: opening again fills the empty root pane.
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
assert!(bounds(&mut visual, "test-dock-pane-0-tabs-tab-inbox").is_some());
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SidebarHarness {
|
|
||||||
dock: Entity<DockState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for SidebarHarness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.flex()
|
|
||||||
.flex_row()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.flex_1()
|
|
||||||
.child(Dock::new("test-dock", self.dock.clone())),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.w(px(160.))
|
|
||||||
.child(DockSidebar::new("test-dock-sidebar", self.dock.clone())),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sidebar_window(
|
|
||||||
cx: &mut TestAppContext,
|
|
||||||
) -> (Entity<DockState>, Rc<Cell<bool>>, VisualTestContext) {
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let dock = cx.update(DockState::new);
|
|
||||||
let opened = Rc::new(Cell::new(false));
|
|
||||||
let opened_for_dock = opened.clone();
|
|
||||||
let dock_for_window = dock.clone();
|
|
||||||
let window = cx.add_window(move |_, _| SidebarHarness {
|
|
||||||
dock: dock_for_window,
|
|
||||||
});
|
|
||||||
dock.update(cx, |dock, _| {
|
|
||||||
dock.on_open_right_sidebar(move |cx| {
|
|
||||||
opened_for_dock.set(true);
|
|
||||||
let _ = cx;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), cx);
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
(dock, opened, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn activating_a_panel_with_a_right_view_opens_the_sidebar() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, opened, mut visual) = sidebar_window(&mut cx);
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, _cx| {
|
|
||||||
dock.register_panel(descriptor_with_right_sidebar("overview"));
|
|
||||||
dock.register_panel(descriptor("inbox"));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
open(&mut visual, &dock, "overview");
|
|
||||||
assert!(
|
|
||||||
opened.get(),
|
|
||||||
"activation must ask the app to open the sidebar"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "side-overview").is_some(),
|
|
||||||
"the sidebar shows the panel's own view"
|
|
||||||
);
|
|
||||||
|
|
||||||
// A panel without a right view leaves the sidebar showing the last one.
|
|
||||||
open(&mut visual, &dock, "inbox");
|
|
||||||
assert!(opened.get());
|
|
||||||
assert!(bounds(&mut visual, "side-overview").is_some());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn closing_the_displayed_tab_falls_back_to_another_right_view() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, opened, mut visual) = sidebar_window(&mut cx);
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, _cx| {
|
|
||||||
dock.register_panel(descriptor_with_right_sidebar("overview"));
|
|
||||||
dock.register_panel(descriptor_with_right_sidebar("explore"));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
open(&mut visual, &dock, "overview");
|
|
||||||
open(&mut visual, &dock, "explore");
|
|
||||||
assert!(bounds(&mut visual, "side-explore").is_some());
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.close_panel("explore", cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "side-overview").is_some(),
|
|
||||||
"the sidebar falls back to a remaining panel's view"
|
|
||||||
);
|
|
||||||
let _ = opened;
|
|
||||||
}
|
|
||||||
@@ -1,158 +0,0 @@
|
|||||||
#![cfg(feature = "resizable")]
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Context, Entity, IntoElement, Modifiers, MouseButton, MouseDownEvent,
|
|
||||||
MouseMoveEvent, ParentElement as _, Pixels, Render, Styled as _, Subscription, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_layout::resizable::{Resizable, ResizablePane, ResizableState};
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
const GROUP_WIDTH: f32 = 200.0;
|
|
||||||
const GROUP_HEIGHT: f32 = 300.0;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
state: Entity<ResizableState>,
|
|
||||||
_observation: Subscription,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
div().w(px(GROUP_WIDTH)).h(px(GROUP_HEIGHT)).child(
|
|
||||||
Resizable::new("test-split", self.state.clone())
|
|
||||||
.debug_selector(|| "test-pane".to_string())
|
|
||||||
.size_full()
|
|
||||||
.pane(div())
|
|
||||||
.pane(div()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn split_window(sizes: [Pixels; 1]) -> (TestAppContext, Entity<ResizableState>, VisualTestContext) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let state = cx.update(ResizableState::new);
|
|
||||||
state.update(&mut cx, |state, cx| state.set_sizes(sizes, cx));
|
|
||||||
let window_state = state.clone();
|
|
||||||
let window = cx.add_window(move |_, cx: &mut Context<Harness>| {
|
|
||||||
let observation = cx.observe(&window_state, |_, _, cx| cx.notify());
|
|
||||||
Harness {
|
|
||||||
state: window_state,
|
|
||||||
_observation: observation,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let window = window.into();
|
|
||||||
cx.update_window(window, |_, window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
})
|
|
||||||
.unwrap();
|
|
||||||
let visual = VisualTestContext::from_window(window, &cx);
|
|
||||||
(cx, state, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn divider_center(visual: &mut VisualTestContext) -> gpui::Point<gpui::Pixels> {
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("resize-handle")
|
|
||||||
.expect("divider rendered");
|
|
||||||
point(bounds.center().x, bounds.center().y)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn drag_divider(visual: &mut VisualTestContext, delta: gpui::Pixels) {
|
|
||||||
let start = divider_center(visual);
|
|
||||||
visual.simulate_mouse_down(start, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(start.x, start.y + delta),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_mouse_up(start, MouseButton::Left, Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_drag_moves_the_pane_and_its_rendered_height() {
|
|
||||||
let (mut cx, state, mut visual) = split_window([px(100.)]);
|
|
||||||
drag_divider(&mut visual, px(60.));
|
|
||||||
|
|
||||||
state.update(&mut cx, |state, _| assert_eq!(state.sizes(), &[px(160.)]));
|
|
||||||
|
|
||||||
// The pane re-renders at the new size after the state notifies the owner.
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("test-pane-0")
|
|
||||||
.expect("first pane rendered");
|
|
||||||
assert_eq!(bounds.size.height, px(160.));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_drag_stops_where_the_remainder_keeps_its_floor() {
|
|
||||||
let (mut cx, state, mut visual) = split_window([px(100.)]);
|
|
||||||
let floor = ResizablePane::DEFAULT_MIN;
|
|
||||||
|
|
||||||
drag_divider(&mut visual, px(1000.));
|
|
||||||
|
|
||||||
state.update(&mut cx, |state, _| {
|
|
||||||
assert_eq!(state.sizes(), &[px(GROUP_HEIGHT) - floor]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_double_click_resets_the_pane_to_the_equal_split() {
|
|
||||||
let (mut cx, state, mut visual) = split_window([px(240.)]);
|
|
||||||
let start = divider_center(&mut visual);
|
|
||||||
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position: start,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
click_count: 2,
|
|
||||||
first_mouse: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
state.update(&mut cx, |state, _| {
|
|
||||||
assert_eq!(state.sizes(), &[px(GROUP_HEIGHT / 2.)]);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrow_keys_resize_the_focused_pane() {
|
|
||||||
let (mut cx, state, mut visual) = split_window([px(100.)]);
|
|
||||||
let start = divider_center(&mut visual);
|
|
||||||
|
|
||||||
// A single click focuses the handle without resetting anything.
|
|
||||||
visual.simulate_mouse_down(start, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_mouse_up(start, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
visual.simulate_keystrokes("up");
|
|
||||||
|
|
||||||
state.update(&mut cx, |state, _| assert_eq!(state.sizes(), &[px(100.)]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn an_unfocused_handle_ignores_arrow_keys() {
|
|
||||||
let (mut cx, state, mut visual) = split_window([px(100.)]);
|
|
||||||
let start = divider_center(&mut visual);
|
|
||||||
let _ = start;
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("down");
|
|
||||||
|
|
||||||
state.update(&mut cx, |state, _| assert_eq!(state.sizes(), &[px(100.)]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_panes_and_the_divider_fill_the_group_exactly() {
|
|
||||||
let (_cx, _state, mut visual) = split_window([px(100.)]);
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
let first = visual
|
|
||||||
.debug_bounds("test-pane-0")
|
|
||||||
.expect("first pane rendered");
|
|
||||||
let second = visual
|
|
||||||
.debug_bounds("test-pane-1")
|
|
||||||
.expect("remainder pane rendered");
|
|
||||||
let divider = visual
|
|
||||||
.debug_bounds("resize-handle")
|
|
||||||
.expect("divider rendered");
|
|
||||||
// The flex split may round the panes' shared edges by a hair.
|
|
||||||
assert!((first.size.height + second.size.height - px(GROUP_HEIGHT)).abs() <= px(1.));
|
|
||||||
// The divider owns no layout space: it straddles the seam between the panes.
|
|
||||||
assert!((divider.center().y - first.bottom()).abs() <= px(1.));
|
|
||||||
}
|
|
||||||
@@ -1,236 +0,0 @@
|
|||||||
#![cfg(feature = "resize_handle")]
|
|
||||||
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
use gpui::prelude::FluentBuilder as _;
|
|
||||||
use gpui::{
|
|
||||||
Axis, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _,
|
|
||||||
Pixels, Render, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_layout::resize_handle::ResizeHandle;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
type DeltaSink = Rc<RefCell<Vec<Pixels>>>;
|
|
||||||
type ResetSink = Rc<RefCell<usize>>;
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
deltas: Rc<RefCell<Vec<Pixels>>>,
|
|
||||||
resets: Rc<RefCell<usize>>,
|
|
||||||
axis: Axis,
|
|
||||||
disabled: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(
|
|
||||||
&mut self,
|
|
||||||
_window: &mut Window,
|
|
||||||
_cx: &mut gpui::Context<Self>,
|
|
||||||
) -> impl gpui::IntoElement {
|
|
||||||
let deltas = self.deltas.clone();
|
|
||||||
let resets = self.resets.clone();
|
|
||||||
let horizontal = self.axis == Axis::Horizontal;
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.flex()
|
|
||||||
.when(horizontal, |layout| layout.flex_row())
|
|
||||||
.when(!horizontal, |layout| layout.flex_col())
|
|
||||||
.child(if horizontal {
|
|
||||||
div().w(px(120.)).h_full()
|
|
||||||
} else {
|
|
||||||
div().h(px(120.)).w_full()
|
|
||||||
})
|
|
||||||
.child(
|
|
||||||
ResizeHandle::new("test-handle")
|
|
||||||
.axis(self.axis)
|
|
||||||
.disabled(self.disabled)
|
|
||||||
.on_resize(move |delta, _, _| deltas.borrow_mut().push(delta))
|
|
||||||
.on_reset(move |_, _| *resets.borrow_mut() += 1),
|
|
||||||
)
|
|
||||||
.child(if horizontal {
|
|
||||||
div().flex_1().h_full()
|
|
||||||
} else {
|
|
||||||
div().flex_1().w_full()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_window(
|
|
||||||
axis: Axis,
|
|
||||||
disabled: bool,
|
|
||||||
) -> (TestAppContext, DeltaSink, ResetSink, VisualTestContext) {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let deltas = Rc::new(RefCell::new(Vec::new()));
|
|
||||||
let resets = Rc::new(RefCell::new(0usize));
|
|
||||||
let deltas_for_window = deltas.clone();
|
|
||||||
let resets_for_window = resets.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
deltas: deltas_for_window,
|
|
||||||
resets: resets_for_window,
|
|
||||||
axis,
|
|
||||||
disabled,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), &cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
(cx, deltas, resets, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_center(visual: &mut VisualTestContext) -> gpui::Point<gpui::Pixels> {
|
|
||||||
let bounds = visual
|
|
||||||
.debug_bounds("resize-handle")
|
|
||||||
.expect("resize handle missing");
|
|
||||||
point(bounds.center().x, bounds.center().y)
|
|
||||||
}
|
|
||||||
|
|
||||||
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 dragging_reports_signed_deltas() {
|
|
||||||
let (_cx, deltas, _resets, mut visual) = handle_window(Axis::Horizontal, false);
|
|
||||||
let start = handle_center(&mut visual);
|
|
||||||
|
|
||||||
visual.simulate_mouse_down(start, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(start.x + px(30.), start.y),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(start.x + px(10.), start.y),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_mouse_up(start, MouseButton::Left, Modifiers::default());
|
|
||||||
|
|
||||||
assert_eq!(*deltas.borrow(), vec![px(30.), px(-20.)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn vertical_seams_report_y_deltas() {
|
|
||||||
let (_cx, deltas, _resets, mut visual) = handle_window(Axis::Vertical, false);
|
|
||||||
let start = handle_center(&mut visual);
|
|
||||||
|
|
||||||
visual.simulate_mouse_down(start, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(start.x, start.y + px(40.)),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_mouse_up(start, MouseButton::Left, Modifiers::default());
|
|
||||||
|
|
||||||
assert_eq!(*deltas.borrow(), vec![px(40.)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn moves_without_a_drag_report_nothing() {
|
|
||||||
let (_cx, deltas, _resets, mut visual) = handle_window(Axis::Horizontal, false);
|
|
||||||
let start = handle_center(&mut visual);
|
|
||||||
|
|
||||||
// Hovering and moving across the seam without pressing does nothing.
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(start.x + px(30.), start.y),
|
|
||||||
pressed_button: None,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
// A press-and-move that started elsewhere must not reach the handle either.
|
|
||||||
visual.simulate_mouse_down(
|
|
||||||
point(px(20.), px(20.)),
|
|
||||||
MouseButton::Left,
|
|
||||||
Modifiers::default(),
|
|
||||||
);
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(px(50.), px(20.)),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
|
|
||||||
assert!(deltas.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The button may be released outside the window, where no MouseUp is
|
|
||||||
/// delivered — the drag must end when a move reports no pressed button.
|
|
||||||
#[test]
|
|
||||||
fn releasing_outside_the_window_ends_the_drag() {
|
|
||||||
let (_cx, deltas, _resets, mut visual) = handle_window(Axis::Horizontal, false);
|
|
||||||
let start = handle_center(&mut visual);
|
|
||||||
|
|
||||||
visual.simulate_mouse_down(start, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(start.x + px(30.), start.y),
|
|
||||||
pressed_button: None,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(start.x + px(60.), start.y),
|
|
||||||
pressed_button: None,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
|
|
||||||
assert!(deltas.borrow().is_empty(), "the drag outlived its button");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn arrow_keys_move_the_seam_when_focused() {
|
|
||||||
let (_cx, deltas, _resets, mut visual) = handle_window(Axis::Horizontal, false);
|
|
||||||
let center = handle_center(&mut visual);
|
|
||||||
click(&mut visual, center);
|
|
||||||
|
|
||||||
visual.simulate_keystrokes("left");
|
|
||||||
visual.simulate_keystrokes("right");
|
|
||||||
assert_eq!(*deltas.borrow(), vec![px(-32.), px(32.)]);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn double_click_resets_and_starts_no_drag() {
|
|
||||||
let (_cx, deltas, resets, mut visual) = handle_window(Axis::Horizontal, false);
|
|
||||||
let center = handle_center(&mut visual);
|
|
||||||
|
|
||||||
for click_count in [1usize, 2] {
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position: center,
|
|
||||||
click_count,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
first_mouse: false,
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
position: center,
|
|
||||||
click_count,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
assert_eq!(*resets.borrow(), 1);
|
|
||||||
|
|
||||||
// The double click must not leave a drag behind.
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(center.x + px(20.), center.y),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
assert!(deltas.borrow().is_empty());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn disabled_handle_ignores_drag_and_keys() {
|
|
||||||
let (_cx, deltas, resets, mut visual) = handle_window(Axis::Horizontal, true);
|
|
||||||
let center = handle_center(&mut visual);
|
|
||||||
|
|
||||||
visual.simulate_mouse_down(center, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: point(center.x + px(30.), center.y),
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_mouse_up(center, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_keystrokes("left");
|
|
||||||
|
|
||||||
assert!(deltas.borrow().is_empty());
|
|
||||||
assert_eq!(*resets.borrow(), 0);
|
|
||||||
}
|
|
||||||
@@ -1,615 +0,0 @@
|
|||||||
#![cfg(feature = "shell")]
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
Entity, InteractiveElement as _, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent,
|
|
||||||
MouseUpEvent, ParentElement as _, Pixels, Render, Styled as _, TestAppContext,
|
|
||||||
VisualTestContext, Window, div, point, px,
|
|
||||||
};
|
|
||||||
use gpui_native_data::sidebar::Sidebar;
|
|
||||||
use gpui_native_layout::shell::{LayoutState, Shell, SidebarToggles};
|
|
||||||
use gpui_native_theme::{ActiveTheme as _, BORDER_WIDTH, Size, Theme};
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
state: Entity<LayoutState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(
|
|
||||||
&mut self,
|
|
||||||
_window: &mut Window,
|
|
||||||
_cx: &mut gpui::Context<Self>,
|
|
||||||
) -> impl gpui::IntoElement {
|
|
||||||
div().size_full().child(
|
|
||||||
Shell::new("test-layout", self.state.clone())
|
|
||||||
.left_sidebar(Sidebar::new("test-left"))
|
|
||||||
.center(
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.debug_selector(|| "test-center".to_string()),
|
|
||||||
)
|
|
||||||
.right_sidebar(
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.debug_selector(|| "test-right".to_string()),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn layout_window(cx: &mut TestAppContext) -> (Entity<LayoutState>, VisualTestContext) {
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let state = cx.update(LayoutState::new);
|
|
||||||
let state_for_window = state.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
state: state_for_window,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), cx);
|
|
||||||
redraw(&mut visual);
|
|
||||||
(state, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn redraw(visual: &mut VisualTestContext) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
window.draw(cx).clear(cx);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bounds(visual: &mut VisualTestContext, selector: &'static str) -> Option<gpui::Bounds<Pixels>> {
|
|
||||||
visual.debug_bounds(selector)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn center_of(bounds: gpui::Bounds<Pixels>) -> gpui::Point<Pixels> {
|
|
||||||
point(bounds.center().x, bounds.center().y)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn press(visual: &mut VisualTestContext, at: gpui::Point<Pixels>, click_count: usize) {
|
|
||||||
visual.simulate_event(MouseDownEvent {
|
|
||||||
position: at,
|
|
||||||
click_count,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
first_mouse: false,
|
|
||||||
});
|
|
||||||
visual.simulate_event(MouseUpEvent {
|
|
||||||
position: at,
|
|
||||||
click_count,
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
button: MouseButton::Left,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn drag(visual: &mut VisualTestContext, from: gpui::Point<Pixels>, to: gpui::Point<Pixels>) {
|
|
||||||
visual.simulate_mouse_down(from, MouseButton::Left, Modifiers::default());
|
|
||||||
visual.simulate_event(MouseMoveEvent {
|
|
||||||
position: to,
|
|
||||||
pressed_button: Some(MouseButton::Left),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
});
|
|
||||||
visual.simulate_mouse_up(to, MouseButton::Left, Modifiers::default());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn left_sidebar_starts_visible_and_right_hidden() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
assert!(state.read_with(&cx, |state, _| state.is_left_sidebar_visible()));
|
|
||||||
assert!(!state.read_with(&cx, |state, _| state.is_right_sidebar_visible()));
|
|
||||||
assert_eq!(
|
|
||||||
state.read_with(&cx, |state, _| state.sidebar_toggles()),
|
|
||||||
SidebarToggles::Tabbar,
|
|
||||||
"the toggles default to the dock's tab bar"
|
|
||||||
);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "sidebar-test-left").is_some(),
|
|
||||||
"left region missing"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "test-center").is_some(),
|
|
||||||
"center missing"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "test-right").is_none(),
|
|
||||||
"right region must be hidden by default"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn toggling_a_sidebar_shows_and_hides_its_region() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.set_right_sidebar_visible(true, cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "test-right").is_some(),
|
|
||||||
"right region missing"
|
|
||||||
);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.set_left_sidebar_visible(false, cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "sidebar-test-left").is_none(),
|
|
||||||
"left region lingering"
|
|
||||||
);
|
|
||||||
// The center keeps filling the shell.
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "test-center").is_some(),
|
|
||||||
"center missing"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_header_toggle_hides_its_sidebar() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
// The strip carries the toggle only under the strips placement.
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.set_sidebar_toggles(SidebarToggles::Strips, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
// The toggle sits at the trailing end of the sidebar's header strip, an
|
|
||||||
// icon-only small button: derive its center from the theme metrics.
|
|
||||||
let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default()));
|
|
||||||
let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small));
|
|
||||||
let strip_height = cx.update(|cx| cx.theme().titlebar_height);
|
|
||||||
let region = bounds(&mut visual, "sidebar-test-left").expect("left sidebar missing");
|
|
||||||
// The Sidebar fills the content area below the strip; the toggle sits at
|
|
||||||
// the strip's trailing end, one strip height above the sidebar's top.
|
|
||||||
let toggle = point(
|
|
||||||
region.right() - metrics.dense_padding_x - small.control_height / 2.0,
|
|
||||||
region.top() - strip_height / 2.0,
|
|
||||||
);
|
|
||||||
press(&mut visual, toggle, 1);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
!state.read_with(&cx, |state, _| state.is_left_sidebar_visible()),
|
|
||||||
"the toggle should hide the left sidebar"
|
|
||||||
);
|
|
||||||
assert!(bounds(&mut visual, "sidebar-test-left").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn dragging_the_seam_resizes_and_clamps() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
let before = state.read_with(&cx, |state, _| state.left_sidebar_width());
|
|
||||||
let seam = bounds(&mut visual, "resize-handle").expect("left seam missing");
|
|
||||||
|
|
||||||
drag(
|
|
||||||
&mut visual,
|
|
||||||
center_of(seam),
|
|
||||||
center_of(seam) + point(px(50.), px(0.)),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
state.read_with(&cx, |state, _| state.left_sidebar_width()),
|
|
||||||
before + px(50.)
|
|
||||||
);
|
|
||||||
|
|
||||||
// A huge drag clamps instead of collapsing the sidebar.
|
|
||||||
let seam = bounds(&mut visual, "resize-handle").expect("seam missing");
|
|
||||||
drag(
|
|
||||||
&mut visual,
|
|
||||||
center_of(seam),
|
|
||||||
center_of(seam) + point(px(2000.), px(0.)),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
state.read_with(&cx, |state, _| state.left_sidebar_width()),
|
|
||||||
px(480.)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Dragging back left shrinks it to the low clamp, no drift from the first.
|
|
||||||
let seam = bounds(&mut visual, "resize-handle").expect("seam missing");
|
|
||||||
drag(
|
|
||||||
&mut visual,
|
|
||||||
center_of(seam),
|
|
||||||
center_of(seam) - point(px(2000.), px(0.)),
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
state.read_with(&cx, |state, _| state.left_sidebar_width()),
|
|
||||||
px(160.)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_right_seam_grows_the_right_sidebar_leftwards() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
// Hide the left sidebar so the shell has exactly one seam.
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.set_left_sidebar_visible(false, cx));
|
|
||||||
state.update(cx, |state, cx| state.set_right_sidebar_visible(true, cx));
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let before = state.read_with(&cx, |state, _| state.right_sidebar_width());
|
|
||||||
let seam = bounds(&mut visual, "resize-handle").expect("right seam missing");
|
|
||||||
|
|
||||||
// Dragging left (negative) grows the right sidebar.
|
|
||||||
drag(
|
|
||||||
&mut visual,
|
|
||||||
center_of(seam),
|
|
||||||
center_of(seam) - point(px(30.), px(0.)),
|
|
||||||
);
|
|
||||||
let grown = state.read_with(&cx, |state, _| state.right_sidebar_width());
|
|
||||||
assert_eq!(grown, before + px(30.));
|
|
||||||
|
|
||||||
// The right region's width follows the state.
|
|
||||||
let region = bounds(&mut visual, "test-right").expect("right region missing");
|
|
||||||
assert_eq!(region.size.width, grown);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Toasts and alerts live on the shell's [`LayoutState`]; pushing one must
|
|
||||||
/// show up in the rendered shell with no observing or manual overlay.
|
|
||||||
#[cfg(feature = "toast")]
|
|
||||||
mod toasts {
|
|
||||||
use super::*;
|
|
||||||
use gpui_native_core::toast::{Toast, ToastAction};
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_pushed_toast_renders_in_the_shell() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
let id = cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.push_toast(
|
|
||||||
Toast::new("Draft deleted").action(ToastAction::new("Undo", |_| {
|
|
||||||
// A real app restores the draft here.
|
|
||||||
})),
|
|
||||||
cx,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
});
|
|
||||||
// No redraw, no observation: the shell repaints its own overlay.
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "toast-pill").is_some(),
|
|
||||||
"the pushed toast must appear in the shell's overlay"
|
|
||||||
);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.dismiss_toast(id, cx));
|
|
||||||
});
|
|
||||||
cx.run_until_parked();
|
|
||||||
// The fade-out keeps the pill mounted briefly.
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "toast-pill").is_some(),
|
|
||||||
"the dismissed toast must stay mounted through its fade-out"
|
|
||||||
);
|
|
||||||
cx.executor().advance_clock(Duration::from_secs(2));
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "toast-pill").is_none(),
|
|
||||||
"the dismissed toast must leave the overlay once faded"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "alert")]
|
|
||||||
mod alerts {
|
|
||||||
use super::*;
|
|
||||||
use gpui_native_core::alert::{Alert, AlertSeverity};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn a_pushed_alert_renders_in_the_shell() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.push_alert(
|
|
||||||
Alert::new("shell-alert")
|
|
||||||
.title("Backed up")
|
|
||||||
.severity(AlertSeverity::Info),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "alert-entry").is_some(),
|
|
||||||
"the pushed alert must appear in the shell's overlay"
|
|
||||||
);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| state.clear_alerts(cx));
|
|
||||||
});
|
|
||||||
cx.run_until_parked();
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "alert-entry").is_none(),
|
|
||||||
"cleared alerts must leave the overlay"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Toggles placed in the dock's tab bar need both the shell and the dock.
|
|
||||||
#[cfg(all(feature = "shell", feature = "dock"))]
|
|
||||||
mod tabbar_toggles {
|
|
||||||
use super::*;
|
|
||||||
use gpui::{AppContext as _, ScrollDelta, ScrollEvent};
|
|
||||||
use gpui_native_layout::dock::{Dock, DockState, PanelDescriptor};
|
|
||||||
use gpui_native_layout::shell::SidebarToggles;
|
|
||||||
|
|
||||||
struct DockPanel;
|
|
||||||
|
|
||||||
impl Render for DockPanel {
|
|
||||||
fn render(
|
|
||||||
&mut self,
|
|
||||||
_window: &mut Window,
|
|
||||||
_cx: &mut gpui::Context<Self>,
|
|
||||||
) -> impl gpui::IntoElement {
|
|
||||||
div().size_full()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct DockHarness {
|
|
||||||
state: Entity<LayoutState>,
|
|
||||||
dock: Entity<DockState>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for DockHarness {
|
|
||||||
fn render(
|
|
||||||
&mut self,
|
|
||||||
_window: &mut Window,
|
|
||||||
_cx: &mut gpui::Context<Self>,
|
|
||||||
) -> impl gpui::IntoElement {
|
|
||||||
div().size_full().child(
|
|
||||||
Shell::new("test-layout", self.state.clone())
|
|
||||||
.left_sidebar(Sidebar::new("test-left"))
|
|
||||||
.center(
|
|
||||||
Dock::new("test-dock", self.dock.clone())
|
|
||||||
.sidebar_toggles(self.state.clone()),
|
|
||||||
)
|
|
||||||
.right_sidebar(
|
|
||||||
div()
|
|
||||||
.size_full()
|
|
||||||
.debug_selector(|| "test-right".to_string()),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tabbar_window(
|
|
||||||
cx: &mut TestAppContext,
|
|
||||||
open: &[&str],
|
|
||||||
) -> (Entity<LayoutState>, VisualTestContext) {
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let state = cx.update(LayoutState::new);
|
|
||||||
let dock = cx.update(|cx| {
|
|
||||||
let dock = DockState::new(cx);
|
|
||||||
dock.update(cx, |dock, _| {
|
|
||||||
dock.register_panel(
|
|
||||||
PanelDescriptor::new("one", |_, cx| cx.new(|_| DockPanel).into()).title("One"),
|
|
||||||
);
|
|
||||||
for index in 0..14 {
|
|
||||||
let tag = format!("panel-{index}");
|
|
||||||
let id = tag.clone();
|
|
||||||
dock.register_panel(
|
|
||||||
PanelDescriptor::new(id, |_, cx| cx.new(|_| DockPanel).into()).title(tag),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
dock
|
|
||||||
});
|
|
||||||
let state_for_window = state.clone();
|
|
||||||
let dock_for_window = dock.clone();
|
|
||||||
let window = cx.add_window(move |_, _| DockHarness {
|
|
||||||
state: state_for_window,
|
|
||||||
dock: dock_for_window,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), cx);
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
for id in open {
|
|
||||||
dock.update(cx, |dock, cx| dock.open_panel(id, window, cx));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
(state, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn place_toggles_in_tabbar(state: &Entity<LayoutState>, cx: &mut TestAppContext) {
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.set_sidebar_toggles(SidebarToggles::Tabbar, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn tabbar_toggles_sit_at_the_bar_edges() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = tabbar_window(&mut cx, &["one"]);
|
|
||||||
place_toggles_in_tabbar(&state, &mut cx);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default()));
|
|
||||||
let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small));
|
|
||||||
let bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("tab bar missing");
|
|
||||||
let left = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing");
|
|
||||||
let right = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing");
|
|
||||||
let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll")
|
|
||||||
.expect("tab scroll container missing");
|
|
||||||
|
|
||||||
// The left toggle leads the bar after its leading pad; the right
|
|
||||||
// toggle trails it at the trailing pad. The button's outer width is
|
|
||||||
// its borders plus dense padding around the icon.
|
|
||||||
let toggle_width = BORDER_WIDTH * 2.0 + small.dense_padding_x * 2.0 + small.icon_size;
|
|
||||||
let left_center = bar.left() + metrics.dense_padding_x + toggle_width / 2.0;
|
|
||||||
let right_center = bar.right() - metrics.dense_padding_x - toggle_width / 2.0;
|
|
||||||
assert!(
|
|
||||||
(left.center().x - left_center).abs() < px(1.0),
|
|
||||||
"the left toggle must lead the tab bar"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
(right.center().x - right_center).abs() < px(1.0),
|
|
||||||
"the right toggle must trail the tab bar"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
left.center().y > bar.top() && left.center().y < bar.bottom(),
|
|
||||||
"the toggles must sit inside the tab bar"
|
|
||||||
);
|
|
||||||
let gap = scroll.left() - left.right();
|
|
||||||
assert!(
|
|
||||||
(gap - px(4.0)).abs() < px(0.5),
|
|
||||||
"the toggle and the tabs must sit one tab gap apart"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn an_overflowing_tab_bar_keeps_the_trailing_toggle_pinned() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let open = [
|
|
||||||
"one", "panel-0", "panel-1", "panel-2", "panel-3", "panel-4", "panel-5", "panel-6",
|
|
||||||
"panel-7", "panel-8", "panel-9", "panel-10", "panel-11", "panel-12", "panel-13",
|
|
||||||
];
|
|
||||||
let (state, mut visual) = tabbar_window(&mut cx, &open);
|
|
||||||
place_toggles_in_tabbar(&state, &mut cx);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let metrics = cx.update(|cx| cx.theme().sizes.resolve(Size::default()));
|
|
||||||
let small = cx.update(|cx| cx.theme().sizes.resolve(Size::Small));
|
|
||||||
let bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("tab bar missing");
|
|
||||||
let left = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing");
|
|
||||||
let right = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing");
|
|
||||||
let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll")
|
|
||||||
.expect("tab scroll container missing");
|
|
||||||
|
|
||||||
// The tabs outgrow the bar: the drag strip collapses, the scroll
|
|
||||||
// viewport spans between the toggles, and the trailing toggle stays
|
|
||||||
// pinned inside the bar instead of being pushed out of the window.
|
|
||||||
let leading_gap = scroll.left() - left.right();
|
|
||||||
assert!((leading_gap - px(4.0)).abs() < px(0.5));
|
|
||||||
let trailing_gap = right.left() - scroll.right();
|
|
||||||
assert!(
|
|
||||||
(trailing_gap - px(4.0)).abs() < px(0.5),
|
|
||||||
"the viewport must reach the trailing toggle"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
right.right() <= bar.right() + px(1.0),
|
|
||||||
"the trailing toggle must stay inside the bar"
|
|
||||||
);
|
|
||||||
let toggle_width = BORDER_WIDTH * 2.0 + small.dense_padding_x * 2.0 + small.icon_size;
|
|
||||||
let right_center = bar.right() - metrics.dense_padding_x - toggle_width / 2.0;
|
|
||||||
assert!(
|
|
||||||
(right.center().x - right_center).abs() < px(1.0),
|
|
||||||
"the trailing toggle must stay at the bar's trailing pad"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn an_overflowing_tab_bar_scrolls_with_the_wheel() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let open = [
|
|
||||||
"one", "panel-0", "panel-1", "panel-2", "panel-3", "panel-4", "panel-5", "panel-6",
|
|
||||||
"panel-7", "panel-8", "panel-9", "panel-10", "panel-11", "panel-12", "panel-13",
|
|
||||||
];
|
|
||||||
let (state, mut visual) = tabbar_window(&mut cx, &open);
|
|
||||||
place_toggles_in_tabbar(&state, &mut cx);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let scroll = bounds(&mut visual, "test-dock-pane-0-tabs-scroll")
|
|
||||||
.expect("tab scroll container missing");
|
|
||||||
let fit = bounds(&mut visual, "test-dock-pane-0-tabs-fit").expect("tab strip missing");
|
|
||||||
// The pills row is wider than the viewport, so the bar can scroll.
|
|
||||||
assert!(
|
|
||||||
fit.size.width > scroll.size.width + px(1.0),
|
|
||||||
"the tab strip must overflow the bar"
|
|
||||||
);
|
|
||||||
|
|
||||||
// A plain vertical wheel over the bar scrolls the strip toward its
|
|
||||||
// end: the bar only scrolls on the x axis, so gpui remaps the delta.
|
|
||||||
visual.simulate_event(ScrollEvent {
|
|
||||||
position: scroll.center(),
|
|
||||||
delta: ScrollDelta::Pixels(point(px(0.), px(-60.))),
|
|
||||||
modifiers: Modifiers::default(),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let fit_after =
|
|
||||||
bounds(&mut visual, "test-dock-pane-0-tabs-fit").expect("tab strip missing");
|
|
||||||
assert!(
|
|
||||||
fit_after.left() < fit.left() - px(10.0),
|
|
||||||
"the wheel must scroll the tab strip"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_tabbar_toggle_shows_and_hides_the_left_sidebar() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = tabbar_window(&mut cx, &["one"]);
|
|
||||||
place_toggles_in_tabbar(&state, &mut cx);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
let toggle = bounds(&mut visual, "sidebar-toggle-left").expect("left toggle missing");
|
|
||||||
press(&mut visual, center_of(toggle), 1);
|
|
||||||
redraw(&mut visual);
|
|
||||||
assert!(!state.read_with(&cx, |state, _| state.is_left_sidebar_visible()));
|
|
||||||
assert!(bounds(&mut visual, "sidebar-test-left").is_none());
|
|
||||||
|
|
||||||
// The toggle outlives its hidden sidebar and brings it back.
|
|
||||||
let toggle = bounds(&mut visual, "sidebar-toggle-left")
|
|
||||||
.expect("left toggle missing while the sidebar is hidden");
|
|
||||||
press(&mut visual, center_of(toggle), 1);
|
|
||||||
redraw(&mut visual);
|
|
||||||
assert!(state.read_with(&cx, |state, _| state.is_left_sidebar_visible()));
|
|
||||||
assert!(bounds(&mut visual, "sidebar-test-left").is_some());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn the_tabbar_right_toggle_opens_the_right_sidebar() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = tabbar_window(&mut cx, &["one"]);
|
|
||||||
place_toggles_in_tabbar(&state, &mut cx);
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(bounds(&mut visual, "test-right").is_none());
|
|
||||||
let toggle = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing");
|
|
||||||
press(&mut visual, center_of(toggle), 1);
|
|
||||||
redraw(&mut visual);
|
|
||||||
assert!(state.read_with(&cx, |state, _| state.is_right_sidebar_visible()));
|
|
||||||
assert!(bounds(&mut visual, "test-right").is_some());
|
|
||||||
|
|
||||||
let toggle = bounds(&mut visual, "sidebar-toggle-right").expect("right toggle missing");
|
|
||||||
press(&mut visual, center_of(toggle), 1);
|
|
||||||
redraw(&mut visual);
|
|
||||||
assert!(!state.read_with(&cx, |state, _| state.is_right_sidebar_visible()));
|
|
||||||
assert!(bounds(&mut visual, "test-right").is_none());
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn hidden_placement_renders_no_toggles() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (state, mut visual) = layout_window(&mut cx);
|
|
||||||
|
|
||||||
cx.update(|cx| {
|
|
||||||
state.update(cx, |state, cx| {
|
|
||||||
state.set_sidebar_toggles(SidebarToggles::Hidden, cx)
|
|
||||||
});
|
|
||||||
});
|
|
||||||
redraw(&mut visual);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "sidebar-toggle-left").is_none(),
|
|
||||||
"the strip must not carry a toggle when they are hidden"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "sidebar-test-left").is_some(),
|
|
||||||
"hiding the toggles must not hide the sidebar itself"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
//! The titlebar duties: traffic-light pads and window controls, asserted per
|
|
||||||
//! platform — macOS draws only pads, Windows/Linux only draw their controls.
|
|
||||||
|
|
||||||
#![cfg(feature = "dock")]
|
|
||||||
|
|
||||||
use gpui::{
|
|
||||||
AppContext as _, Entity, IntoElement, ParentElement as _, Pixels, Render, Styled as _,
|
|
||||||
TestAppContext, VisualTestContext, Window, div, px,
|
|
||||||
};
|
|
||||||
use gpui_native_layout::dock::{Dock, DockState, PanelDescriptor};
|
|
||||||
use gpui_native_layout::window_controls::TRAFFIC_LIGHT_PAD;
|
|
||||||
use gpui_native_theme::Theme;
|
|
||||||
|
|
||||||
struct Panel {
|
|
||||||
tag: &'static str,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Panel {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut gpui::Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(self.tag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn descriptor(tag: &'static str) -> PanelDescriptor {
|
|
||||||
PanelDescriptor::new(tag, move |_, cx| cx.new(|_| Panel { tag }).into()).title(tag)
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Harness {
|
|
||||||
dock: Entity<DockState>,
|
|
||||||
window_controls: bool,
|
|
||||||
traffic_light_pad: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for Harness {
|
|
||||||
fn render(&mut self, _window: &mut Window, _cx: &mut gpui::Context<Self>) -> impl IntoElement {
|
|
||||||
div().size_full().child(
|
|
||||||
Dock::new("test-dock", self.dock.clone())
|
|
||||||
.window_controls(self.window_controls)
|
|
||||||
.traffic_light_pad(self.traffic_light_pad),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn dock_window(
|
|
||||||
window_controls: bool,
|
|
||||||
traffic_light_pad: bool,
|
|
||||||
cx: &mut TestAppContext,
|
|
||||||
) -> (Entity<DockState>, VisualTestContext) {
|
|
||||||
cx.update(Theme::init);
|
|
||||||
let dock = cx.update(DockState::new);
|
|
||||||
let dock_for_window = dock.clone();
|
|
||||||
let window = cx.add_window(move |_, _| Harness {
|
|
||||||
dock: dock_for_window,
|
|
||||||
window_controls,
|
|
||||||
traffic_light_pad,
|
|
||||||
});
|
|
||||||
let mut visual = VisualTestContext::from_window(window.into(), cx);
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
(dock, visual)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn bounds(visual: &mut VisualTestContext, selector: &str) -> Option<gpui::Bounds<Pixels>> {
|
|
||||||
visual.debug_bounds(Box::leak(selector.to_string().into_boxed_str()))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn open(visual: &mut VisualTestContext, dock: &Entity<DockState>, tag: &'static str) {
|
|
||||||
visual.update(|window, cx| {
|
|
||||||
dock.update(cx, |dock, cx| dock.open_panel(tag, window, cx));
|
|
||||||
});
|
|
||||||
visual.update(|window, cx| window.draw(cx).clear(cx));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
#[test]
|
|
||||||
fn the_pill_bar_pads_for_traffic_lights_when_it_leads_the_window() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(false, false, &mut cx);
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, _cx| dock.register_panel(descriptor("overview")));
|
|
||||||
});
|
|
||||||
open(&mut visual, &dock, "overview");
|
|
||||||
|
|
||||||
let bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("pill bar missing");
|
|
||||||
let tab = bounds(&mut visual, "test-dock-pane-0-tabs-tab-overview").expect("tab missing");
|
|
||||||
let bare_inset = tab.left() - bar.left();
|
|
||||||
|
|
||||||
let (dock, mut visual) = dock_window(false, true, &mut cx);
|
|
||||||
let _ = &dock;
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, _cx| dock.register_panel(descriptor("overview")));
|
|
||||||
});
|
|
||||||
open(&mut visual, &dock, "overview");
|
|
||||||
|
|
||||||
let padded_bar = bounds(&mut visual, "test-dock-pane-0-bar").expect("padded bar missing");
|
|
||||||
let padded_tab =
|
|
||||||
bounds(&mut visual, "test-dock-pane-0-tabs-tab-overview").expect("tab missing");
|
|
||||||
let padded_inset = padded_tab.left() - padded_bar.left();
|
|
||||||
|
|
||||||
// The bare bar keeps the standard leading padding; the padded bar clears
|
|
||||||
// the traffic lights.
|
|
||||||
assert!(
|
|
||||||
bare_inset < TRAFFIC_LIGHT_PAD,
|
|
||||||
"the bare bar must not reserve the traffic-light pad (bare {bare_inset:?})"
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
padded_inset >= TRAFFIC_LIGHT_PAD - px(1.),
|
|
||||||
"the pad must clear the traffic lights (bare {bare_inset:?}, padded {padded_inset:?})"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn window_controls_render_only_when_the_flag_is_set() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(false, false, &mut cx);
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, _cx| dock.register_panel(descriptor("overview")));
|
|
||||||
});
|
|
||||||
open(&mut visual, &dock, "overview");
|
|
||||||
|
|
||||||
// macOS: the system traffic lights own the controls — nothing renders;
|
|
||||||
// the pad test above covers that mode.
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
|
||||||
assert!(
|
|
||||||
bounds(&mut visual, "window-control-close").is_none(),
|
|
||||||
"controls must not render without the flag"
|
|
||||||
);
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
let _ = (&dock, &mut visual);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "macos"))]
|
|
||||||
#[test]
|
|
||||||
fn the_flag_hosts_the_platform_controls_in_the_bar() {
|
|
||||||
let mut cx = TestAppContext::single();
|
|
||||||
let (dock, mut visual) = dock_window(true, false, &mut cx);
|
|
||||||
cx.update(|cx| {
|
|
||||||
dock.update(cx, |dock, _cx| dock.register_panel(descriptor("overview")));
|
|
||||||
});
|
|
||||||
open(&mut visual, &dock, "overview");
|
|
||||||
|
|
||||||
// The maximize control exists once; its restore flip follows the
|
|
||||||
// window's maximized state, not a separate control.
|
|
||||||
assert!(bounds(&mut visual, "window-control-close").is_some());
|
|
||||||
assert!(bounds(&mut visual, "window-control-maximize").is_some());
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user