Files
gpui-native-kit/crates/layout/tests/panel.rs
T
2026-11-15 12:50:57 +07:00

154 lines
4.7 KiB
Rust

#![cfg(feature = "panel")]
//! The panel row: the main island fills the window; splitting makes two
//! half-width islands with a drag divider that resizes them.
use gpui::{
AppContext as _, Context, Entity, InteractiveElement as _, IntoElement, Modifiers, MouseButton,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _, Render, SharedString,
Styled as _, TestAppContext, VisualTestContext, Window, div, point, px, size,
};
use gpui_native_layout::panel::{Panel, PanelRow};
use gpui_native_theme::Theme;
const WINDOW_WIDTH: f32 = 1024.0;
const WINDOW_HEIGHT: f32 = 768.0;
/// A full-size marker the tests measure through its debug selector.
struct TagView {
selector: SharedString,
}
impl TagView {
fn new(selector: &'static str) -> Self {
Self {
selector: SharedString::from(selector),
}
}
}
impl Render for TagView {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
let selector = self.selector.clone();
div()
.debug_selector(move || selector.to_string())
.size_full()
}
}
impl Panel for TagView {}
struct PanelHarness {
split: bool,
main: Entity<TagView>,
split_view: Entity<TagView>,
}
impl Render for PanelHarness {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
let panel = PanelRow::new("panel-harness").main(self.main.clone());
let panel = if self.split {
panel.split(self.split_view.clone())
} else {
panel
};
div().size_full().child(panel)
}
}
fn panel_window(cx: &mut TestAppContext, split: bool) -> VisualTestContext {
let (main, split_view) = cx.update(|cx| {
Theme::init(cx);
let main = cx.new(|_| TagView::new("panel-test-main"));
let split = cx.new(|_| TagView::new("panel-test-split"));
(main, split)
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
PanelHarness {
split,
main,
split_view,
}
});
let cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
cx
}
#[gpui::test]
fn main_fills_the_row_without_a_split(cx: &mut TestAppContext) {
let mut cx = panel_window(cx, false);
let main = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out");
// The row's padding and the island's inset are all main gives up.
assert!(main.size.width >= px(WINDOW_WIDTH - 40.0));
assert!(main.size.height >= px(WINDOW_HEIGHT - 40.0));
assert!(main.origin.x >= px(0.0));
}
#[gpui::test]
fn split_makes_two_half_width_panels(cx: &mut TestAppContext) {
let mut cx = panel_window(cx, true);
let main = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out");
let split = cx
.debug_bounds("panel-test-split")
.expect("split panel laid out");
// Both islands share the row at half width, main first, and neither
// leaves the viewport. The divider's width lands on the remainder pane.
assert!(
(main.size.width - split.size.width).abs() <= px(10.0),
"main={:?} split={:?}",
main.size.width,
split.size.width
);
assert!(main.size.width < px(WINDOW_WIDTH / 2.0));
assert!(main.right() <= split.left());
assert!(split.right() <= px(WINDOW_WIDTH));
assert!(main.size.height >= px(WINDOW_HEIGHT - 40.0));
}
#[gpui::test]
fn dragging_the_divider_resizes_the_panels(cx: &mut TestAppContext) {
let mut cx = panel_window(cx, true);
let before = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out");
// Press the divider between the islands and drag it 100px to the right.
let seam = point(before.right() + px(13.0), px(400.0));
cx.simulate_event(MouseDownEvent {
position: seam,
button: MouseButton::Left,
modifiers: Modifiers::default(),
click_count: 1,
first_mouse: false,
});
cx.simulate_event(MouseMoveEvent {
position: point(seam.x + px(100.0), seam.y),
pressed_button: Some(MouseButton::Left),
modifiers: Modifiers::default(),
});
cx.simulate_event(MouseUpEvent {
position: point(seam.x + px(100.0), seam.y),
button: MouseButton::Left,
modifiers: Modifiers::default(),
click_count: 1,
});
cx.run_until_parked();
// The main panel grew by the drag; the split panel gave up the space.
let after = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out after the drag");
assert_eq!(after.size.width, before.size.width + px(100.0));
}