refactor: move panel state into panel stack
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run

This commit is contained in:
2026-11-24 08:39:17 +07:00
parent bef96c614b
commit bac641002d
11 changed files with 734 additions and 556 deletions
+294 -47
View File
@@ -1,14 +1,16 @@
//! The panel row: the main island fills the window; splitting makes two
//! The panel stack: the main island fills the window; splitting makes two
//! half-width islands with a drag divider that resizes them.
use std::sync::Arc;
use gpui::{
AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _,
Pixels, Render, SharedString, Styled as _, TestAppContext, VisualTestContext, Window, div,
point, px, size,
Render, SharedString, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px,
size,
};
use ui::common::v_flex;
use ui::layout::panel::{Panel, PanelRow, PanelView};
use ui::layout::panel::{Panel, PanelSlot, PanelStack, PanelView};
use ui::tabs::Tabs;
use ui::theme::Theme;
@@ -44,61 +46,74 @@ impl Panel for TagView {
}
struct PanelHarness {
split: bool,
split_width: Option<Pixels>,
resizable: bool,
main: Entity<TagView>,
split_view: Entity<TagView>,
stack: Entity<PanelStack>,
}
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())
.resizable(self.resizable);
let panel = match self.split_width {
Some(width) => panel.split_at(self.split_view.clone(), width),
None if self.split => panel.split(self.split_view.clone()),
None => panel,
};
div().size_full().child(panel)
div().size_full().child(self.stack.clone())
}
}
fn panel_window(cx: &mut TestAppContext, split: bool) -> VisualTestContext {
split_window(cx, split, None, true)
/// A marker view.
fn tag(cx: &mut App, selector: &'static str) -> Entity<TagView> {
cx.new(|_| TagView::new(selector))
}
fn split_window(
/// Opens the test window over a stack seeded by `seed`.
fn stack_window(
cx: &mut TestAppContext,
split: bool,
split_width: Option<Pixels>,
resizable: bool,
) -> VisualTestContext {
let (main, split_view) = cx.update(|cx| {
seed: impl FnOnce(&mut PanelStack, &mut Context<PanelStack>),
) -> (VisualTestContext, Entity<PanelStack>) {
let stack = 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 stack = cx.new(|cx| PanelStack::new("panel-harness", cx));
stack.update(cx, seed);
stack
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
PanelHarness {
split,
split_width,
resizable,
main,
split_view,
stack: stack.clone(),
}
});
let cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
cx
(cx, stack)
}
/// Seeds the main stack with one panel.
fn main_only(stack: &mut PanelStack, cx: &mut Context<PanelStack>) {
stack.insert(
PanelSlot::Main,
"main",
Arc::new(tag(cx, "panel-test-main")),
cx,
);
}
/// Seeds the main stack and the split stack with one panel each.
fn main_and_split(stack: &mut PanelStack, cx: &mut Context<PanelStack>) {
main_only(stack, cx);
stack.insert(
PanelSlot::Split,
"split",
Arc::new(tag(cx, "panel-test-split")),
cx,
);
}
/// Seeds the empty state: a 300px pane with another beside it.
fn empty_state(stack: &mut PanelStack, cx: &mut Context<PanelStack>) {
stack.set_empty(
tag(cx, "panel-test-main"),
Some((tag(cx, "panel-test-split").into(), px(300.))),
cx,
);
}
#[gpui::test]
fn main_fills_the_row_without_a_split(cx: &mut TestAppContext) {
let mut cx = panel_window(cx, false);
let (mut cx, _stack) = stack_window(cx, main_only);
let main = cx
.debug_bounds("panel-test-main")
@@ -112,7 +127,7 @@ fn main_fills_the_row_without_a_split(cx: &mut TestAppContext) {
#[gpui::test]
fn split_makes_two_half_width_panels(cx: &mut TestAppContext) {
let mut cx = panel_window(cx, true);
let (mut cx, _stack) = stack_window(cx, main_and_split);
let main = cx
.debug_bounds("panel-test-main")
@@ -135,10 +150,10 @@ fn split_makes_two_half_width_panels(cx: &mut TestAppContext) {
assert!(main.size.height >= px(WINDOW_HEIGHT - 40.0));
}
/// A split opened at a width lays the main pane out at exactly that width.
/// An empty state's split opens its main pane at the given width.
#[gpui::test]
fn split_at_opens_the_main_pane_at_the_width(cx: &mut TestAppContext) {
let mut cx = split_window(cx, true, Some(px(300.0)), true);
fn an_empty_state_split_opens_its_main_pane_at_the_width(cx: &mut TestAppContext) {
let (mut cx, _stack) = stack_window(cx, empty_state);
let main = cx
.debug_bounds("panel-test-main")
@@ -147,9 +162,8 @@ fn split_at_opens_the_main_pane_at_the_width(cx: &mut TestAppContext) {
.debug_bounds("panel-test-split")
.expect("split panel laid out");
// The marker fills the island's content box, which extends under the
// overlay border to the full pane width.
assert_eq!(main.size.width, px(300.0));
// The marker fills the island's content box, inside its 1px border.
assert!((main.size.width - px(300.0)).abs() <= px(2.0));
assert!(main.right() <= split.left());
assert!(split.size.width > px(300.0));
assert!(split.right() <= px(WINDOW_WIDTH));
@@ -158,12 +172,15 @@ fn split_at_opens_the_main_pane_at_the_width(cx: &mut TestAppContext) {
/// With resizing disabled there is no divider and drags over the seam do nothing.
#[gpui::test]
fn a_static_split_ignores_drags(cx: &mut TestAppContext) {
let mut cx = split_window(cx, true, Some(px(300.0)), false);
let (mut cx, _stack) = stack_window(cx, |stack, cx| {
empty_state(stack, cx);
stack.set_resizable(false, cx);
});
let before = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out");
assert_eq!(before.size.width, px(300.0));
assert!((before.size.width - px(300.0)).abs() <= px(2.0));
assert!(
cx.debug_bounds("resize-handle").is_none(),
"a static split has no divider"
@@ -197,7 +214,7 @@ fn a_static_split_ignores_drags(cx: &mut TestAppContext) {
#[gpui::test]
fn dragging_the_divider_resizes_the_panels(cx: &mut TestAppContext) {
let mut cx = panel_window(cx, true);
let (mut cx, _stack) = stack_window(cx, main_and_split);
let before = cx
.debug_bounds("panel-test-main")
@@ -333,3 +350,233 @@ fn panel_tab_uses_title_prefix_and_loading(cx: &mut TestAppContext) {
"loading adds the spinner to the tab"
);
}
/// The split pane exists only while the split stack holds a panel.
#[gpui::test]
fn the_split_pane_follows_the_split_stack(cx: &mut TestAppContext) {
let (mut cx, stack) = stack_window(cx, main_only);
assert!(
cx.debug_bounds("panel-test-split").is_none(),
"no split pane without a split panel"
);
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.insert(
PanelSlot::Split,
"split",
Arc::new(tag(cx, "panel-test-split")),
cx,
)
})
});
cx.run_until_parked();
assert!(
cx.debug_bounds("panel-test-split").is_some(),
"a split panel opens the split pane"
);
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.close(PanelSlot::Split, &SharedString::from("split"), cx)
})
});
cx.run_until_parked();
assert!(
cx.debug_bounds("panel-test-split").is_none(),
"closing the last split panel closes the pane"
);
}
/// A main panel replaces the empty state.
#[gpui::test]
fn a_main_panel_replaces_the_empty_state(cx: &mut TestAppContext) {
let (mut cx, stack) = stack_window(cx, |stack, cx| {
stack.set_empty(
tag(cx, "panel-test-empty"),
Some((tag(cx, "panel-test-side").into(), px(300.))),
cx,
);
});
assert!(cx.debug_bounds("panel-test-empty").is_some());
assert!(cx.debug_bounds("panel-test-side").is_some());
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.insert(
PanelSlot::Main,
"main",
Arc::new(tag(cx, "panel-test-main")),
cx,
)
})
});
cx.run_until_parked();
assert!(
cx.debug_bounds("panel-test-empty").is_none(),
"the empty state leaves when a main panel opens"
);
assert!(
cx.debug_bounds("panel-test-side").is_none(),
"its split pane leaves with it"
);
assert!(cx.debug_bounds("panel-test-main").is_some());
}
/// A split that leaves the screen and comes back starts at half again.
#[gpui::test]
fn a_reopened_split_starts_at_half_again(cx: &mut TestAppContext) {
let (mut cx, stack) = stack_window(cx, main_and_split);
let start = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out")
.size
.width;
let handle = cx.debug_bounds("resize-handle").expect("divider laid out");
let seam = point(handle.center().x, 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();
let dragged = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out after the drag")
.size
.width;
assert_eq!(dragged, start + px(100.0));
let split = SharedString::from("split");
cx.update(|_, cx| stack.update(cx, |stack, cx| stack.close(PanelSlot::Split, &split, cx)));
cx.run_until_parked();
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.insert(
PanelSlot::Split,
split.clone(),
Arc::new(tag(cx, "panel-test-split")),
cx,
)
})
});
cx.run_until_parked();
let reopened = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out after reopening")
.size
.width;
assert_eq!(
reopened, start,
"the divider position is forgotten while no split is on screen"
);
}
/// The titlebar strip above the stack.
struct TabsHarness {
stack: Entity<PanelStack>,
}
impl Render for TabsHarness {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
v_flex()
.size_full()
.children(PanelStack::main_tabs(self.stack.clone(), cx))
.child(self.stack.clone())
}
}
/// The titlebar strip lists the main stack; its tabs switch and close panels.
#[gpui::test]
fn main_tabs_switch_and_close_the_main_stack(cx: &mut TestAppContext) {
let stack = cx.update(|cx| {
Theme::init(cx);
cx.new(|cx| PanelStack::new("panel-harness", cx))
});
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
TabsHarness {
stack: stack.clone(),
}
});
let mut cx = VisualTestContext::from_window(window.into(), cx);
cx.run_until_parked();
assert!(
cx.debug_bounds("panel-harness-main-tabs-tab-panel-test-first")
.is_none(),
"no strip while the main stack is empty"
);
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.insert(
PanelSlot::Main,
"first",
Arc::new(tag(cx, "panel-test-first")),
cx,
);
stack.insert(
PanelSlot::Main,
"second",
Arc::new(tag(cx, "panel-test-second")),
cx,
);
})
});
cx.run_until_parked();
assert!(
cx.debug_bounds("panel-test-second").is_some(),
"the newest panel opens shown"
);
let first = cx
.debug_bounds("panel-harness-main-tabs-tab-panel-test-first")
.expect("the strip lists the first panel");
cx.simulate_click(first.center(), Modifiers::none());
cx.run_until_parked();
assert!(
cx.debug_bounds("panel-test-first").is_some(),
"clicking a tab shows its panel"
);
assert!(cx.debug_bounds("panel-test-second").is_none());
// The close control is laid out only while its tab is hovered.
let second = cx
.debug_bounds("panel-harness-main-tabs-tab-panel-test-second")
.expect("the strip lists the second panel");
cx.simulate_event(MouseMoveEvent {
position: second.center(),
pressed_button: None,
modifiers: Modifiers::default(),
});
cx.run_until_parked();
let close = cx
.debug_bounds("panel-harness-main-tabs-close-panel-test-second")
.expect("tabs close");
cx.simulate_click(close.center(), Modifiers::none());
cx.run_until_parked();
assert!(
cx.debug_bounds("panel-harness-main-tabs-tab-panel-test-second")
.is_none(),
"closing a tab closes its panel"
);
assert!(
cx.debug_bounds("panel-test-first").is_some(),
"the shown panel stays"
);
}