refactor: replace signed out view with explore banner
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 09:46:11 +07:00
parent bac641002d
commit 3f9e4e1c12
8 changed files with 548 additions and 237 deletions
+139 -55
View File
@@ -2,9 +2,10 @@
//! background. It owns every panel it shows: a main stack, tabbed in the
//! titlebar through [`PanelStack::main_tabs`], and a split stack that opens a
//! second pane beside the main panel while it holds a panel, at half the
//! width, with a drag divider between the panes; a double-click restores half.
//! While no main panel is open the stack shows the empty state its owner set
//! ([`PanelStack::set_empty`]).
//! width or at a set width on either side of it
//! ([`PanelStack::insert_split_at`]), with a drag divider between the panes; a
//! double-click restores half. While no main panel is open the stack shows the
//! empty state its owner set ([`PanelStack::set_empty`]).
use std::sync::Arc;
@@ -128,6 +129,15 @@ pub enum PanelSlot {
Split,
}
/// The side of the main panel a split pane opens on.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SplitSide {
/// Before the main panel.
Leading,
/// After the main panel.
Trailing,
}
/// An ordered stack of panels with one of them shown.
#[derive(Default)]
struct Stack {
@@ -216,6 +226,12 @@ pub struct PanelStack {
split: Stack,
empty: Option<EmptyContent>,
resizable: bool,
/// The width the on-screen split pane opened at; `None` splits the row in half.
split_width: Option<Pixels>,
/// The side of the main panel the on-screen split pane opened on.
split_side: SplitSide,
/// The main pane width the divider was last seeded at, to tell a drag from a window resize.
opened_at: Option<Pixels>,
/// The divider position, kept only while a split is on screen.
sizes: Entity<ResizableState>,
}
@@ -251,6 +267,9 @@ impl PanelStack {
split: Stack::default(),
empty: None,
resizable: true,
split_width: None,
split_side: SplitSide::Trailing,
opened_at: None,
sizes: ResizableState::new(cx),
}
}
@@ -311,6 +330,34 @@ impl PanelStack {
self.insert(slot, id, view, cx);
}
/// Whether the panel under `id` is open in `slot`.
pub fn is_open(&self, slot: PanelSlot, id: &SharedString) -> bool {
self.stack(slot).position(id).is_some()
}
/// Whether the panel under `id` is the one shown in `slot`.
pub fn is_active(&self, slot: PanelSlot, id: &SharedString) -> bool {
self.stack(slot).active.as_ref() == Some(id)
}
/// Shows `view` in the split stack under `id`; a split pane not on screen yet opens at `width` on `side`.
pub fn insert_split_at<T>(
&mut self,
id: T,
width: Pixels,
side: SplitSide,
view: Arc<dyn PanelView>,
cx: &mut Context<Self>,
) where
T: Into<SharedString>,
{
if !self.is_split_shown() {
self.split_width = Some(width);
self.split_side = side;
}
self.insert(PanelSlot::Split, id, view, cx);
}
/// Closes the panel under `id`.
pub fn close(&mut self, slot: PanelSlot, id: &SharedString, cx: &mut Context<Self>) {
if let Some(index) = self.stack(slot).position(id) {
@@ -431,12 +478,19 @@ impl PanelStack {
.is_some_and(|empty| empty.split.is_some())
}
/// Drops the divider position while no split is on screen, so the next one opens at its start width.
/// Drops the divider position, width, and side while no split is on screen, so the next split opens at its start width.
fn forget_divider(&mut self, cx: &mut Context<Self>) {
if self.is_split_shown() || self.sizes.read(cx).sizes().is_empty() {
if self.is_split_shown() {
return;
}
self.sizes.update(cx, |state, cx| state.set_sizes([], cx));
self.split_width = None;
self.split_side = SplitSide::Trailing;
self.opened_at = None;
if !self.sizes.read(cx).sizes().is_empty() {
self.sizes.update(cx, |state, cx| state.set_sizes([], cx));
}
}
/// The split pane's content: the split stack's tabs over its shown panel.
@@ -489,63 +543,93 @@ impl PanelStack {
impl Render for PanelStack {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let gutter = RESIZE_HANDLE_WIDTH;
let extent = window.viewport_size().width - gutter * 2.0;
let cached = |view: AnyView| view.cached(content_style()).into_any_element();
// The main pane's content, the split pane's when one is on screen, and the width the main pane opens at.
let (main, split, width): (AnyElement, Option<AnyElement>, Option<Pixels>) =
match self.main.active() {
Some(panel) => (
// The main pane's content, the split pane's when one is on screen, and how the first pane opens.
let (main, split, width, leading) = match self.main.active() {
Some(panel) => {
let leading = self.split_side == SplitSide::Leading;
// The pane opened at a width is the sized one; the other fills what is left.
let width = if leading {
self.split_width
} else {
self.split_width
.map(|split_width| extent - gutter - split_width)
};
(
cached(panel.view()),
(!self.split.is_empty()).then(|| self.split_pane(cx)),
None,
),
None => match &self.empty {
Some(empty) => {
let (split, width) = match &empty.split {
Some((view, width)) => (Some(cached(view.clone())), Some(*width)),
None => (None, None),
};
(cached(empty.main.clone()), split, width)
width,
leading,
)
}
None => match &self.empty {
Some(empty) => {
let (split, width) = match &empty.split {
Some((view, width)) => (Some(cached(view.clone())), Some(*width)),
None => (None, None),
};
(cached(empty.main.clone()), split, width, false)
}
None => (div().into_any_element(), None, None, false),
},
};
div().flex_1().min_w_0().h_full().p_2().pt_1().map(|this| {
match split {
Some(split) => {
let main = island(div().size_full().child(main), cx);
let split = island(div().size_full().child(split), cx);
let (first, last) = if leading {
(split, main)
} else {
(main, split)
};
let first_width = width.unwrap_or((extent - gutter) / 2.0).max(PANE_MIN_SIZE);
if !self.resizable {
this.child(
div()
.w_full()
.h_full()
.flex()
.flex_row()
.child(div().flex_none().w(first_width).h_full().child(first))
.child(div().flex_none().w(gutter))
.child(div().flex_1().min_w_0().h_full().child(last)),
)
} else {
// A split opened at a width keeps it across window resizes until the divider is dragged.
let current = self.sizes.read(cx).sizes().first().copied();
let keeps_width = self.split_width.is_some() && current == self.opened_at;
if current.is_none() || (keeps_width && self.opened_at != Some(first_width))
{
self.sizes.update(cx, |state, cx| {
state.set_pane_width(0, first_width, cx);
});
self.opened_at = Some(first_width);
}
this.child(
div().w_full().h_full().child(
Resizable::new((self.id.clone(), "split"), self.sizes.clone())
.axis(Axis::Horizontal)
.pane(first)
.pane(last)
.h_full(),
),
)
}
None => (div().into_any_element(), None, None),
},
};
let main = island(div().size_full().child(main), cx);
let row = match split {
Some(split) => {
let split = island(div().size_full().child(split), cx);
let extent = window.viewport_size().width - gutter * 2.0;
let main_width = width.unwrap_or((extent - gutter) / 2.0).max(PANE_MIN_SIZE);
if !self.resizable {
}
None => this.child(
div()
.w_full()
.h_full()
.flex()
.flex_row()
.child(div().flex_none().w(main_width).h_full().child(main))
.child(div().flex_none().w(gutter))
.child(div().flex_1().min_w_0().h_full().child(split))
} else {
if self.sizes.read(cx).sizes().is_empty() {
self.sizes
.update(cx, |state, cx| state.set_pane_width(0, main_width, cx));
}
div().w_full().h_full().child(
Resizable::new((self.id.clone(), "split"), self.sizes.clone())
.axis(Axis::Horizontal)
.pane(main)
.pane(split)
.h_full(),
)
}
.child(island(div().size_full().child(main), cx)),
),
}
None => div().w_full().h_full().child(main),
};
div().flex_1().min_w_0().h_full().p_2().pt_1().child(row)
})
}
}
+5 -28
View File
@@ -111,9 +111,11 @@ impl ResizableState {
pub fn set_pane_width(&mut self, ix: usize, width: Pixels, cx: &mut Context<Self>) {
// Persisted layouts can carry negative sizes.
let width = width.max(PANE_MIN_SIZE);
while self.sizes.len() <= ix {
self.sizes.push(width);
}
if self.sizes[ix] != width {
self.sizes[ix] = width;
cx.notify();
@@ -305,7 +307,9 @@ impl RenderOnce for Resizable {
Axis::Vertical => bounds.size.height,
Axis::Horizontal => bounds.size.width,
};
state.update(cx, |state, cx| state.set_measured(extent, cx));
state.update(cx, |state, cx| {
state.set_measured(extent, cx);
});
}
},
|_, _, _, _| {},
@@ -316,30 +320,3 @@ impl RenderOnce for Resizable {
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use gpui::TestAppContext;
#[gpui::test]
fn resize_never_stores_sub_floor_size(cx: &mut TestAppContext) {
let state = cx.new(|_| ResizableState::default());
state.update(cx, |state, cx| {
// The measured extent is smaller than the other pane's floor.
state.set_measured(px(100.), cx);
state.resize(0, px(500.), &[px(48.), px(150.)], cx);
assert_eq!(state.sizes[0], px(48.));
});
}
#[gpui::test]
fn set_pane_width_clamps_negative_values(cx: &mut TestAppContext) {
let state = cx.new(|_| ResizableState::default());
state.update(cx, |state, cx| {
// Persisted layouts can carry negative sizes.
state.set_pane_width(0, px(-20.), cx);
assert_eq!(state.pane_size(0, 1), PANE_MIN_SIZE);
});
}
}
+241 -1
View File
@@ -10,7 +10,7 @@ use gpui::{
size,
};
use ui::common::v_flex;
use ui::layout::panel::{Panel, PanelSlot, PanelStack, PanelView};
use ui::layout::panel::{Panel, PanelSlot, PanelStack, PanelView, SplitSide};
use ui::tabs::Tabs;
use ui::theme::Theme;
@@ -580,3 +580,243 @@ fn main_tabs_switch_and_close_the_main_stack(cx: &mut TestAppContext) {
"the shown panel stays"
);
}
/// A split inserted at a width opens with it.
#[gpui::test]
fn a_split_inserted_at_a_width_opens_there(cx: &mut TestAppContext) {
let (mut cx, stack) = stack_window(cx, main_only);
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.insert_split_at(
"side",
px(300.),
SplitSide::Trailing,
Arc::new(tag(cx, "panel-test-split")),
cx,
)
})
});
cx.run_until_parked();
let split = cx
.debug_bounds("panel-test-split")
.expect("split panel laid out");
assert!((split.size.width - px(300.0)).abs() <= px(2.0));
cx.update(|_, cx| {
let stack = stack.read(cx);
let side = SharedString::from("side");
assert!(stack.is_open(PanelSlot::Split, &side));
assert!(stack.is_active(PanelSlot::Split, &side));
});
}
/// A split already on screen keeps its divider and side when another opens at a width.
#[gpui::test]
fn a_width_and_side_apply_to_a_split_new_to_the_screen_only(cx: &mut TestAppContext) {
let (mut cx, stack) = stack_window(cx, main_and_split);
let before = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out");
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.insert_split_at(
"side",
px(300.),
SplitSide::Leading,
Arc::new(tag(cx, "panel-test-side")),
cx,
)
})
});
cx.run_until_parked();
let after = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out");
assert_eq!(after, before);
cx.update(|_, cx| {
let stack = stack.read(cx);
let split = SharedString::from("split");
assert!(stack.is_open(PanelSlot::Split, &split));
assert!(!stack.is_active(PanelSlot::Split, &split));
});
let side = cx
.debug_bounds("panel-test-side")
.expect("the new split panel laid out");
assert!(
side.left() >= after.right(),
"it still opens after the main panel"
);
}
/// A split opened at a width keeps it across window resizes until the divider is dragged.
#[gpui::test]
fn a_width_split_follows_resizes_until_dragged(cx: &mut TestAppContext) {
let (mut cx, _stack) = stack_window(cx, |stack, cx| {
main_only(stack, cx);
stack.insert_split_at(
"side",
px(300.),
SplitSide::Trailing,
Arc::new(tag(cx, "panel-test-split")),
cx,
);
});
cx.simulate_resize(size(px(WINDOW_WIDTH + 400.0), px(WINDOW_HEIGHT)));
cx.run_until_parked();
let split = cx
.debug_bounds("panel-test-split")
.expect("split panel laid out after the resize");
assert!(
(split.size.width - px(300.0)).abs() <= px(2.0),
"the split keeps its width: {:?}",
split.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;
cx.simulate_resize(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)));
cx.run_until_parked();
let resized = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out after the resize")
.size
.width;
assert_eq!(resized, dragged, "a dragged divider keeps its pane's width");
}
/// A leading split opens before the main panel and keeps its width, dragged or not, across window resizes.
#[gpui::test]
fn a_leading_split_opens_before_the_main_panel_at_its_width(cx: &mut TestAppContext) {
let (mut cx, _stack) = stack_window(cx, |stack, cx| {
main_only(stack, cx);
stack.insert_split_at(
"side",
px(300.),
SplitSide::Leading,
Arc::new(tag(cx, "panel-test-split")),
cx,
);
});
let split = cx
.debug_bounds("panel-test-split")
.expect("split panel laid out");
let main = cx
.debug_bounds("panel-test-main")
.expect("main panel laid out");
assert!(split.right() <= main.left(), "the split comes first");
assert!((split.size.width - px(300.0)).abs() <= px(2.0));
cx.simulate_resize(size(px(WINDOW_WIDTH + 400.0), px(WINDOW_HEIGHT)));
cx.run_until_parked();
let split = cx
.debug_bounds("panel-test-split")
.expect("split panel laid out after the resize");
assert!(
(split.size.width - px(300.0)).abs() <= px(2.0),
"the split keeps its width: {:?}",
split.size.width
);
// Drag the divider 100px right, then put the window back: the split keeps the dragged 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();
cx.simulate_resize(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)));
cx.run_until_parked();
let split = cx
.debug_bounds("panel-test-split")
.expect("split panel laid out after the resize");
assert!(
(split.size.width - px(400.0)).abs() <= px(2.0),
"the split keeps its dragged width: {:?}",
split.size.width
);
}
/// Once no split is on screen, the next one opens after the main panel again.
#[gpui::test]
fn a_split_after_a_leading_one_opens_after_the_main_panel(cx: &mut TestAppContext) {
let (mut cx, stack) = stack_window(cx, |stack, cx| {
main_only(stack, cx);
stack.insert_split_at(
"side",
px(300.),
SplitSide::Leading,
Arc::new(tag(cx, "panel-test-side")),
cx,
);
});
let side = SharedString::from("side");
cx.update(|_, cx| {
stack.update(cx, |stack, cx| {
stack.close(PanelSlot::Split, &side, cx);
stack.insert(
PanelSlot::Split,
"split",
Arc::new(tag(cx, "panel-test-split")),
cx,
);
})
});
cx.run_until_parked();
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");
assert!(
main.right() <= split.left(),
"the split opens after the main panel again"
);
}