refactor: move panel state into panel stack entity
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-23 04:21:01 +07:00
parent bef96c614b
commit 40c8831bf5
3 changed files with 362 additions and 346 deletions
+285 -2
View File
@@ -3,15 +3,22 @@
//! between them; the divider opens at half the row or at a set width
//! ([`PanelRow::split_at`]), and dragging it can be disabled
//! ([`PanelRow::resizable`]). A double-click restores the half split.
//!
//! The [`PanelStack`] is the stateful variant: a main tile with one tile
//! per opened detail beside it, resizing like the row's split.
use std::sync::Arc;
use gpui::{
AnyElement, AnyView, App, Axis, Div, ElementId, Entity, IntoElement, ParentElement as _,
Pixels, Render, RenderOnce, SharedString, StyleRefinement, Styled as _, Window, div,
AnyElement, AnyView, App, Axis, Context, Div, ElementId, Entity, IntoElement,
ParentElement as _, Pixels, Render, RenderOnce, SharedString, StyleRefinement, Styled as _,
WeakEntity, Window, div,
};
use crate::components::button::{Button, ButtonVariant};
use crate::components::common::{ResultLogErr as _, Sizable as _};
use crate::components::tabs::Tab;
use crate::icons::IconRole;
use crate::layout::StyledExt as _;
use crate::layout::resizable::{Resizable, ResizableState};
use crate::theme::{ActiveTheme as _, PANE_MIN_SIZE, RESIZE_HANDLE_WIDTH};
@@ -269,3 +276,279 @@ impl RenderOnce for PanelRow {
.child(row)
}
}
/// One tile of the panel stack: a main view or a detail opened beside it.
#[derive(Clone)]
pub struct SubPanel {
id: SharedString,
view: Arc<dyn PanelView>,
}
impl SubPanel {
/// Wraps a panel view under `id`.
pub fn new(id: impl Into<SharedString>, view: Arc<dyn PanelView>) -> Self {
Self {
id: id.into(),
view,
}
}
/// The tile's id.
pub fn id(&self) -> &SharedString {
&self.id
}
/// The tile's panel view.
pub fn view(&self) -> &Arc<dyn PanelView> {
&self.view
}
}
/// The window's panels: the active main tile, plus one tile per opened detail.
pub struct PanelStack {
mains: Vec<SubPanel>,
active_main: Option<SharedString>,
splits: Vec<SubPanel>,
state: Entity<ResizableState>,
fallback: AnyView,
weak_self: WeakEntity<Self>,
style: StyleRefinement,
}
impl PanelStack {
/// Creates a stack showing `fallback` while no panel is open.
pub fn new(fallback: AnyView, cx: &mut Context<Self>) -> Self {
Self {
mains: Vec::new(),
active_main: None,
splits: Vec::new(),
state: ResizableState::new(cx),
fallback,
weak_self: cx.weak_entity(),
style: StyleRefinement::default(),
}
}
/// Shows a panel as the main tile, activating its tab when already open.
pub fn open_main(
&mut self,
id: SharedString,
view: Arc<dyn PanelView>,
cx: &mut Context<Self>,
) {
if self.mains.iter().any(|panel| panel.id == id) {
if self.active_main.as_ref() != Some(&id) {
self.active_main = Some(id);
self.splits.clear();
cx.notify();
}
} else {
let panel = SubPanel::new(id, view);
self.active_main = Some(panel.id.clone());
self.mains.push(panel);
self.splits.clear();
cx.notify();
}
}
/// Opens a detail tile beside the main tile; open details keep their tile.
pub fn open_split(
&mut self,
id: SharedString,
view: Arc<dyn PanelView>,
cx: &mut Context<Self>,
) {
if self.splits.iter().any(|panel| panel.id == id) {
return;
}
self.splits.push(SubPanel::new(id, view));
cx.notify();
}
/// Activates the main tile with `id`, closing the detail tiles.
pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context<Self>) {
if self.active_main.as_ref() == Some(id) || !self.mains.iter().any(|panel| panel.id == *id)
{
return;
}
self.active_main = Some(id.clone());
self.splits.clear();
cx.notify();
}
/// Closes the main tab at `index`, activating a neighbor when it was active.
pub fn close_main_at(&mut self, index: usize, cx: &mut Context<Self>) {
if index >= self.mains.len() {
return;
}
let id = self.mains.remove(index).id;
if self.active_main.as_ref() == Some(&id) {
self.active_main = self
.mains
.get(index)
.or_else(|| index.checked_sub(1).and_then(|prev| self.mains.get(prev)))
.map(|panel| panel.id.clone());
self.splits.clear();
}
cx.notify();
}
/// Closes the detail tile with `id`.
pub fn close_split(&mut self, id: &SharedString, cx: &mut Context<Self>) {
self.remove_split(id, cx);
}
/// Closes every panel, leaving the fallback.
pub fn clear(&mut self, cx: &mut Context<Self>) {
self.mains.clear();
self.active_main = None;
self.splits.clear();
cx.notify();
}
/// Whether a main tab or a detail tile is open under `id`.
pub fn has(&self, id: &SharedString) -> bool {
self.mains.iter().any(|panel| &panel.id == id)
|| self.splits.iter().any(|panel| &panel.id == id)
}
/// The main tabs, in open order.
pub fn mains(&self) -> &[SubPanel] {
&self.mains
}
/// The id of the main tile on screen, if any.
pub fn active_main_id(&self) -> Option<&SharedString> {
self.active_main.as_ref()
}
/// Builds the titlebar tabs for the open mains.
pub fn main_tabs(&self, cx: &App) -> Vec<Tab> {
self.mains
.iter()
.map(|panel| panel.view.tab(panel.id.clone().into(), cx).closeable(true))
.collect()
}
/// The active tab's position in [`Self::mains`], or `usize::MAX` when none.
pub fn active_main_index(&self) -> usize {
self.active_main
.as_ref()
.and_then(|active| self.mains.iter().position(|panel| &panel.id == active))
.unwrap_or(usize::MAX)
}
/// Whether a main tile renders; pane 0 is the main tile when it does.
fn has_main_tile(&self) -> bool {
self.active_main.is_some()
}
/// Removes the stored size of the tile at `pane index`, keeping the rest.
fn remove_split(&mut self, id: &SharedString, cx: &mut Context<Self>) {
let Some(index) = self.splits.iter().position(|panel| panel.id == *id) else {
return;
};
self.splits.remove(index);
let base = usize::from(self.has_main_tile());
let pane_count = base + self.splits.len();
let sizes: Vec<Pixels> = self
.state
.read(cx)
.sizes()
.iter()
.enumerate()
.filter(|(ix, _)| *ix != base + index)
.map(|(_, &size)| size)
.take(pane_count.saturating_sub(1))
.collect();
self.state.update(cx, |state, cx| {
state.set_sizes(sizes, cx);
});
cx.notify();
}
}
impl gpui::Styled for PanelStack {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl Render for PanelStack {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let content = if self.mains.is_empty() && self.splits.is_empty() {
island(div().size_full().child(self.fallback.clone()), cx).into_any_element()
} else {
let mut resizable = Resizable::new("panel-stack", self.state.clone())
.axis(Axis::Horizontal)
.h_full();
if let Some(main) = self
.active_main
.as_ref()
.and_then(|active| self.mains.iter().find(|panel| &panel.id == active))
{
resizable = resizable.pane(island(
div()
.size_full()
.child(main.view.view().cached(content_style())),
cx,
));
}
for panel in &self.splits {
let id = panel.id.clone();
let weak_self = self.weak_self.clone();
resizable = resizable.pane(
div()
.relative()
.size_full()
.child(island(
div()
.size_full()
.child(panel.view.view().cached(content_style())),
cx,
))
.child(
div().absolute().top_1().right_1().child(
Button::new((ElementId::from("panel-stack-close"), id.clone()))
.icon(IconRole::Close)
.variant(ButtonVariant::Ghost)
.xsmall()
.on_click(move |_, window, cx| {
let stack = weak_self.clone();
let id = id.clone();
window.defer(cx, move |_, cx| {
stack
.update(cx, |stack, cx| {
stack.close_split(&id, cx);
})
.log_err();
});
}),
),
),
);
}
div().size_full().child(resizable).into_any_element()
};
div()
.size_full()
.min_w_0()
.p_2()
.pt_1()
.refine_style(&self.style)
.child(content)
.into_any_element()
}
}