Add server-owned stacked panes with vertical navigation

This commit is contained in:
2026-11-17 02:08:02 -08:00
parent 4655f8149b
commit ec97ef00b0
10 changed files with 1129 additions and 136 deletions
+167 -68
View File
@@ -10,8 +10,8 @@ use crate::{
palette::CommandPalette,
pane_name::RenameField,
protocol::{
ClientMessage, PaneSnapshot, ScrollKind, SelectionCommand, SelectionKind, SelectionSide,
Snapshot,
ClientMessage, Direction, PaneSnapshot, ScrollKind, SelectionCommand, SelectionKind,
SelectionSide, Snapshot, StackPaneInfo, VerticalDirection,
},
render::{FONT_SIZE, LINE_HEIGHT, RenderCell, Renderer},
transition::Slide,
@@ -37,7 +37,8 @@ pub struct Workspace {
error: Option<String>,
focus: FocusHandle,
palette: Option<Entity<CommandPalette>>,
outgoing: Vec<Entity<TerminalPane>>,
outgoing: HashMap<u64, Entity<TerminalPane>>,
outgoing_snapshot: Option<Arc<Snapshot>>,
transition: Option<Slide>,
transition_frame_pending: bool,
_updates: Task<()>,
@@ -130,45 +131,41 @@ impl Workspace {
let message = match action {
NewTab => Some(ClientMessage::NewTab { cwd: None }),
SplitPane => pane_id.map(|pane| ClientMessage::Split { pane, cwd: None }),
StackAbove | StackBelow => Some(ClientMessage::NewStackPane {
pane: None,
direction: if matches!(action, StackAbove) {
VerticalDirection::Up
} else {
VerticalDirection::Down
},
cwd: None,
}),
FocusAbove | FocusBelow => Some(ClientMessage::FocusDirection {
direction: if matches!(action, FocusAbove) {
Direction::Up
} else {
Direction::Down
},
}),
ClosePane => pane_id.map(|pane| ClientMessage::ClosePane { pane }),
ResetPaneName => pane_id.map(|pane| ClientMessage::RenamePane { pane, name: None }),
Copy => pane_id.map(|pane| ClientMessage::Copy { pane }),
Scroll(kind) => pane_id.map(|pane| ClientMessage::Scroll { pane, kind }),
FocusTab(tab) => Some(ClientMessage::FocusTab { tab }),
FocusPane(pane) => Some(ClientMessage::FocusPane { pane }),
PreviousTab | NextTab => self.adjacent_tab(matches!(action, NextTab)),
PreviousPane | NextPane => self.snapshot.as_ref().and_then(|snapshot| {
let index = snapshot
.panes
.iter()
.position(|pane| Some(pane.id) == pane_id)?;
let count = snapshot.panes.len();
let next = if matches!(action, NextPane) {
(index + 1) % count
PreviousTab | NextTab => Some(ClientMessage::CycleTab {
forward: matches!(action, NextTab),
}),
PreviousPane | NextPane => Some(ClientMessage::CyclePane {
forward: matches!(action, NextPane),
}),
FocusLeft | FocusRight => Some(ClientMessage::FocusDirection {
direction: if matches!(action, FocusLeft) {
Direction::Left
} else {
(index + count - 1) % count
};
Some(ClientMessage::FocusPane {
pane: snapshot.panes[next].id,
})
Direction::Right
},
}),
FocusLeft | FocusRight => {
let forward = matches!(action, FocusRight);
self.snapshot.as_ref().and_then(|snapshot| {
let index = snapshot
.panes
.iter()
.position(|pane| Some(pane.id) == pane_id)?;
let next = if forward {
index.checked_add(1)
} else {
index.checked_sub(1)
};
next.and_then(|next| snapshot.panes.get(next))
.map(|pane| ClientMessage::FocusPane { pane: pane.id })
.or_else(|| self.adjacent_tab(forward))
})
}
RenamePane => {
if let Some(pane) = pane {
pane.update(cx, |pane, cx| pane.begin_rename(window, cx));
@@ -204,22 +201,6 @@ impl Workspace {
}
}
fn adjacent_tab(&self, forward: bool) -> Option<ClientMessage> {
let snapshot = self.snapshot.as_ref()?;
let index = snapshot
.tabs
.iter()
.position(|tab| tab.id == snapshot.active_tab)?;
let count = snapshot.tabs.len();
let next = if forward {
(index + 1) % count
} else {
(index + count - 1) % count
};
Some(ClientMessage::FocusTab {
tab: snapshot.tabs[next].id,
})
}
pub fn new(
client: Client,
updates: Updates,
@@ -241,20 +222,30 @@ impl Workspace {
view.error = Some(error);
}
if let Some(snapshot) = update.snapshot {
let metadata_changed = view.snapshot.as_ref().is_none_or(|old| {
old.active_tab != snapshot.active_tab
|| old.tabs != snapshot.tabs
});
let focus_allowed = view.palette.is_none();
if let Some(direction) = view
.snapshot
.as_ref()
.and_then(|old| scene_direction(old, &snapshot))
{
view.outgoing_snapshot = view.snapshot.clone();
view.outgoing = view
.snapshot
.as_ref()
.unwrap()
.panes
.iter()
.filter_map(|data| view.panes.get(&data.id).cloned())
.filter_map(|data| {
view.panes
.get(&data.id)
.map(|pane| (data.id, pane.clone()))
})
.collect();
for pane in &view.outgoing {
for pane in view.outgoing.values() {
pane.update(cx, |pane, cx| {
pane.read_only = true;
pane.transitioning = true;
@@ -282,7 +273,7 @@ impl Workspace {
pane.active = active == Some(data.id);
pane.snapshot = snapshot.clone();
pane.index = index;
if pane.active && !was_active {
if pane.active && !was_active && focus_allowed {
if let Some(editor) = &pane.rename {
editor.read(cx).focus(window);
} else {
@@ -306,6 +297,7 @@ impl Workspace {
frame,
index,
active == Some(data.id),
focus_allowed,
window,
cx,
);
@@ -324,11 +316,11 @@ impl Workspace {
window.set_window_title(&format!("psyTerm — {title}"));
view.snapshot = Some(snapshot);
if let Some(palette) = &view.palette {
let items = commands::root_items(view.snapshot.as_deref());
palette.update(cx, |palette, cx| {
palette.set_items(items, cx);
palette.focus(window);
});
if metadata_changed {
let items = commands::root_items(view.snapshot.as_deref());
palette
.update(cx, |palette, cx| palette.set_items(items, cx));
}
} else if view.panes.is_empty() {
window.focus(&view.focus);
}
@@ -351,7 +343,8 @@ impl Workspace {
error: None,
focus,
palette: None,
outgoing: Vec::new(),
outgoing: HashMap::new(),
outgoing_snapshot: None,
transition: None,
transition_frame_pending: false,
_updates: task,
@@ -368,6 +361,7 @@ impl Render for Workspace {
{
self.transition = None;
self.outgoing.clear();
self.outgoing_snapshot = None;
for pane in self.panes.values() {
pane.update(cx, |pane, cx| {
pane.transitioning = false;
@@ -420,16 +414,12 @@ impl Render for Workspace {
.min_h_0()
.overflow_hidden()
.bg(rgb(chrome::APP_BACKGROUND));
if !self.outgoing.is_empty() {
body = body.child(scene(outgoing_offset).children(self.outgoing.iter().cloned()));
if let Some(snapshot) = &self.outgoing_snapshot {
body = body.child(pane_scene(snapshot, &self.outgoing, outgoing_offset, None));
}
let mut incoming = scene(incoming_offset);
let mut incoming = scene(incoming_offset).id("incoming-scene");
if let Some(snapshot) = &self.snapshot {
for data in &snapshot.panes {
if let Some(pane) = self.panes.get(&data.id) {
incoming = incoming.child(pane.clone());
}
}
incoming = pane_scene(snapshot, &self.panes, incoming_offset, Some(&self.client));
if snapshot.panes.is_empty() {
incoming = incoming.child(
div()
@@ -488,6 +478,106 @@ fn scene(offset: [f32; 2]) -> Div {
.bg(rgb(chrome::APP_BACKGROUND))
}
fn pane_scene(
snapshot: &Snapshot,
panes: &HashMap<u64, Entity<TerminalPane>>,
offset: [f32; 2],
client: Option<&Client>,
) -> Stateful<Div> {
// The frozen scene can contain the same collapsed headers as the live
// scene. Keep their hover, scroll, and click state in separate namespaces.
let mut content = scene(offset).id(if client.is_some() {
"incoming-scene"
} else {
"outgoing-scene"
});
if let Some(tab) = snapshot
.tabs
.iter()
.find(|tab| tab.id == snapshot.active_tab)
{
for stack in &tab.stacks {
let Some(index) = stack
.panes
.iter()
.position(|pane| pane.id == stack.active_pane)
else {
continue;
};
let mut column = div()
.flex_1()
.min_w_0()
.h_full()
.flex()
.flex_col()
.gap(px(2.))
.overflow_hidden();
if index > 0 {
column = column.child(stack_headers(
&stack.panes[..index],
format!("stack-above-{}", stack.active_pane),
client,
));
}
if let Some(pane) = panes.get(&stack.active_pane) {
column = column.child(pane.clone());
}
if index + 1 < stack.panes.len() {
column = column.child(stack_headers(
&stack.panes[index + 1..],
format!("stack-below-{}", stack.active_pane),
client,
));
}
content = content.child(column);
}
}
content
}
fn stack_headers(entries: &[StackPaneInfo], id: String, client: Option<&Client>) -> Stateful<Div> {
let mut headers = div()
.id(ElementId::Name(id.into()))
.flex()
.flex_col()
.w_full()
.flex_shrink_0()
.max_h(relative(0.25))
.overflow_y_scroll();
for entry in entries {
let pane = entry.id;
let client = client.cloned();
headers = headers.child(
div()
.id(ElementId::Name(format!("collapsed-pane-{pane}").into()))
.h(px(28.))
.flex_shrink_0()
.px(px(9.))
.border_1()
.border_color(rgb(chrome::BORDER))
.bg(rgb(chrome::TERMINAL_BACKGROUND))
.font_family(".SystemUIFont")
.text_size(px(11.))
.text_color(rgb(chrome::PANE_MUTED))
.cursor_pointer()
.hover(|style| {
style
.bg(rgb(chrome::HOVER_BACKGROUND))
.text_color(rgb(chrome::PANE_FOCUS))
})
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.on_click(move |_, _, cx| {
cx.stop_propagation();
if let Some(client) = &client {
let _ = client.send(ClientMessage::FocusPane { pane });
}
})
.child(middle_label(entry.title.clone().into())),
);
}
headers
}
fn scene_direction(old: &Snapshot, next: &Snapshot) -> Option<[f32; 2]> {
if old.panes.is_empty() || next.panes.is_empty() {
return None;
@@ -505,6 +595,13 @@ fn scene_direction(old: &Snapshot, next: &Snapshot) -> Option<[f32; 2]> {
.unwrap_or(0);
return Some([if next_index > previous { 1. } else { -1. }, 0.]);
}
if let (Some(old_tab), Some(next_tab)) = (
old.tabs.iter().find(|tab| tab.id == old.active_tab),
next.tabs.iter().find(|tab| tab.id == next.active_tab),
) && let Some(direction) = crate::transition::stack_direction(old_tab, next_tab)
{
return Some([0., direction]);
}
next.panes
.iter()
.any(|pane| !old.panes.iter().any(|old| old.id == pane.id))
@@ -542,6 +639,7 @@ impl TerminalPane {
snapshot: Arc<Snapshot>,
index: usize,
active: bool,
focus_allowed: bool,
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
@@ -554,7 +652,7 @@ impl TerminalPane {
.unwrap_or(9.)
.max(1.);
let focus = cx.focus_handle();
if active {
if active && focus_allowed {
window.focus(&focus);
}
let activation = cx.observe_window_activation(window, |pane, _, cx| {
@@ -980,6 +1078,7 @@ impl Render for TerminalPane {
div()
.flex_1()
.min_w_0()
.min_h_0()
.h_full()
.flex()
.flex_col()