Close local tabs and detach or reattach live SSH sessions

Remember the remote daemon and session identity for hostless reattachment. Keep other tabs usable after a discarded snapshot and preserve the last live detach target when a remote shell exits. Reject remote flags for local daemon control commands.
This commit is contained in:
James Magahern
2026-11-17 16:15:28 -08:00
parent f2390ca9b4
commit 153838061e
12 changed files with 874 additions and 162 deletions
+47 -9
View File
@@ -137,7 +137,7 @@ impl Workspace {
cx.notify();
});
}
let items = commands::root_items(self.snapshot.as_deref());
let items = commands::root_items(self.snapshot.as_deref(), self.client.remote().is_some());
let owner = cx.weak_entity();
self.palette = Some(cx.new(|cx| {
CommandPalette::new(
@@ -204,6 +204,28 @@ impl Workspace {
.detach();
}
fn close_tab(&mut self, id: u64, window: &mut Window, cx: &mut Context<Self>) {
let Some(tab) = self
.snapshot
.as_ref()
.and_then(|s| s.tabs.iter().find(|tab| tab.id == id))
else {
return;
};
let message = if tab.remote.is_some() {
ClientMessage::DetachRemote { tab: id }
} else if self.client.remote().is_some() {
window.remove_window();
return;
} else {
ClientMessage::Close { id }
};
if let Err(error) = self.client.send(message) {
self.error = Some(error.to_string());
cx.notify();
}
}
fn execute(&mut self, action: CommandAction, window: &mut Window, cx: &mut Context<Self>) {
use CommandAction::*;
let pane = self.focused_pane();
@@ -253,13 +275,11 @@ impl Workspace {
Direction::Down
},
}),
ClosePane => pane_id
.map(|pane| ClientMessage::ClosePane { pane })
.or_else(|| {
let tab = self.snapshot.as_deref()?.active_tab()?;
tab.is_connecting()
.then_some(ClientMessage::ClosePendingTab { tab: tab.id })
}),
ClosePane => pane_id.map(|id| ClientMessage::Close { id }).or_else(|| {
let tab = self.snapshot.as_deref()?.active_tab()?;
tab.is_connecting()
.then_some(ClientMessage::DetachRemote { tab: tab.id })
}),
ResetPaneName => pane_id.map(|pane| ClientMessage::RenamePane { pane, name: None }),
ResetTabName => self
.snapshot
@@ -311,6 +331,17 @@ impl Workspace {
}
None
}
DetachRemote => {
let tab = self.snapshot.as_deref().and_then(Snapshot::active_tab);
if let Some(tab) = tab.filter(|tab| tab.remote.is_some()) {
Some(ClientMessage::DetachRemote { tab: tab.id })
} else {
if self.client.remote().is_some() {
window.remove_window();
}
None
}
}
Detach => {
window.remove_window();
None
@@ -455,7 +486,10 @@ impl Workspace {
view.snapshot = Some(snapshot);
if let Some(palette) = &view.palette {
if metadata_changed {
let items = commands::root_items(view.snapshot.as_deref());
let items = commands::root_items(
view.snapshot.as_deref(),
view.client.remote().is_some(),
);
palette
.update(cx, |palette, cx| palette.set_items(items, cx));
}
@@ -543,12 +577,16 @@ impl Render for Workspace {
.unwrap_or_default();
let client = self.client.clone();
let new_client = self.client.clone();
let owner = cx.weak_entity();
let header = chrome::header(
tabs,
cfg!(target_os = "macos") && !window.is_fullscreen(),
move |tab, _, _| {
let _ = client.send(ClientMessage::FocusTab { tab });
},
move |tab, window, cx| {
let _ = owner.update(cx, |view, cx| view.close_tab(tab, window, cx));
},
move |_, _| {
let _ = new_client.send(ClientMessage::NewTab { cwd: None });
},