Add manual tab names with selected-pane fallback

This commit is contained in:
2026-11-17 08:56:02 -08:00
parent d8630c2d52
commit 6b2bd8c064
8 changed files with 281 additions and 20 deletions
+18 -10
View File
@@ -7,10 +7,11 @@
use std::rc::Rc;
use gpui::{
App, Div, ElementId, MouseButton, SharedString, Stateful, Window, div, prelude::*, px, rgb,
App, Div, ElementId, Entity, MouseButton, SharedString, Stateful, Window, div, prelude::*, px,
rgb,
};
use crate::label::middle_label;
use crate::{label::middle_label, pane_name::RenameField};
pub const APP_BACKGROUND: u32 = 0x06060f;
pub const PANEL_BACKGROUND: u32 = 0x080817;
@@ -34,6 +35,7 @@ pub struct ChromeTab {
pub id: u64,
pub label: SharedString,
pub active: bool,
pub editor: Option<Entity<RenameField>>,
}
/// An app-owned titlebar. Callbacks issue server commands; no tab state lives here.
@@ -58,6 +60,18 @@ pub fn header(
let on_tab = on_tab.clone();
let active = tab.active;
let id = tab.id;
let label = div()
.flex_1()
.min_w_0()
.h_full()
.flex()
.items_center()
.overflow_hidden();
let label = if let Some(editor) = tab.editor {
label.child(editor)
} else {
label.child(middle_label(tab.label))
};
tab_strip = tab_strip.child(
div()
.id(ElementId::Name(format!("tab-{id}").into()))
@@ -93,14 +107,7 @@ pub fn header(
.h(px(12.))
.bg(rgb(if active { ACCENT } else { BORDER })),
)
.child(
div()
.flex_1()
.min_w_0()
.h_full()
.overflow_hidden()
.child(middle_label(tab.label)),
),
.child(label),
);
}
@@ -189,6 +196,7 @@ mod tests {
id: 1,
label: "Shell".into(),
active: true,
editor: None,
}],
|_, _, _| {},
|_, _| {},
+20
View File
@@ -15,6 +15,8 @@ pub enum CommandAction {
ClosePane,
RenamePane,
ResetPaneName,
RenameTab,
ResetTabName,
Copy,
Paste,
PreviousTab,
@@ -131,6 +133,22 @@ pub fn root_items(snapshot: Option<&Snapshot>) -> Vec<PaletteItem> {
ResetPaneName,
)
.available(pane),
PaletteItem::action(
"rename-tab",
"Rename tab",
"name title label workspace",
None,
RenameTab,
)
.available(tabs),
PaletteItem::action(
"reset-tab-name",
"Use automatic tab name",
"reset rename title inherit selected pane",
None,
ResetTabName,
)
.available(tabs),
PaletteItem::action(
"previous-tab",
"Previous tab",
@@ -338,6 +356,8 @@ mod tests {
assert!(!find("close-pane").enabled);
assert!(!find("switch-tab").enabled);
assert!(find("reset-pane-name").shortcut.is_none());
assert!(!find("rename-tab").enabled);
assert!(!find("reset-tab-name").enabled);
let unique: std::collections::HashSet<_> = items.iter().map(|item| &item.id).collect();
assert_eq!(unique.len(), items.len());
}
+1 -1
View File
@@ -1,4 +1,4 @@
//! Single-line pane-name editing. UTF-8 offsets stay internal; platform input
//! Single-line pane/tab-name editing. UTF-8 offsets stay internal; platform input
//! methods use UTF-16 ranges. Editing never sends bytes to the terminal process.
use std::ops::Range;
+8 -1
View File
@@ -10,7 +10,7 @@ use anyhow::{Context, Result, bail};
use bincode::Options;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
pub const VERSION: u32 = 3;
pub const VERSION: u32 = 4;
pub const MAX_MESSAGE_BYTES: usize = 16 * 1024 * 1024;
pub type PaneId = u64;
pub type TabId = u64;
@@ -96,6 +96,11 @@ pub enum ClientMessage {
CyclePane {
forward: bool,
},
/// A blank/absent name returns to the selected pane's effective name.
RenameTab {
tab: TabId,
name: Option<String>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@@ -173,7 +178,9 @@ pub struct Snapshot {
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TabInfo {
pub id: TabId,
/// Effective display name: manual override, then the selected pane's name.
pub title: String,
pub manual_title: Option<String>,
/// All pane IDs, flattened in column/stack order.
pub panes: Vec<PaneId>,
pub active_pane: PaneId,
+134 -3
View File
@@ -647,6 +647,15 @@ impl Server {
self.refresh_directories();
self.dirty = true;
}
ClientMessage::RenameTab { tab, name } => {
self.tabs
.iter_mut()
.find(|entry| entry.id == tab)
.context("unknown tab")?
.manual_title = name.and_then(clean_title);
self.refresh_directories();
self.dirty = true;
}
ClientMessage::Hello { .. } | ClientMessage::Shutdown => unreachable!(),
}
Ok(())
@@ -881,6 +890,7 @@ impl Server {
self.tabs.push(TabInfo {
id,
title: "Shell".into(),
manual_title: None,
panes: vec![pane],
active_pane: pane,
stacks: vec![single_pane_stack(pane)],
@@ -939,9 +949,11 @@ impl Server {
fn snapshot(&mut self) -> Snapshot {
self.revision += 1;
for tab in &mut self.tabs {
if let Some(pane) = self.panes.get(&tab.active_pane) {
tab.title = pane.title();
}
tab.title = tab
.manual_title
.clone()
.or_else(|| self.panes.get(&tab.active_pane).map(Pane::title))
.unwrap_or_else(|| "Shell".into());
for stack in &mut tab.stacks {
for entry in &mut stack.panes {
if let Some(pane) = self.panes.get(&entry.id) {
@@ -1942,6 +1954,125 @@ mod tests {
assert!(automatic.panes[0].manual_title.is_none());
}
#[test]
fn manual_tab_names_override_selected_panes_and_survive_clients_and_exit() {
let server = TestServer::start();
let mut client = server.client();
send(&mut client, ClientMessage::Attach);
let initial = snapshot(&mut client, |s| s.panes.len() == 1);
let tab = initial.active_tab;
let first = initial.panes[0].id;
assert!(active_tab(&initial).manual_title.is_none());
assert_eq!(active_tab(&initial).title, initial.panes[0].title);
input(&mut client, first, "printf '\\033]2;Codex\\007'\n");
snapshot(&mut client, |s| active_tab(s).title == "Codex");
send(
&mut client,
ClientMessage::NewStackPane {
pane: None,
direction: VerticalDirection::Down,
cwd: None,
},
);
let stacked = snapshot(&mut client, |s| active_tab(s).panes.len() == 2);
let second = active_tab(&stacked).active_pane;
send(
&mut client,
ClientMessage::RenamePane {
pane: second,
name: Some("Build".into()),
},
);
snapshot(&mut client, |s| active_tab(s).title == "Build");
send(
&mut client,
ClientMessage::RenameTab {
tab,
name: Some(" Pro\nject\t\u{7} ".into()),
},
);
let named = snapshot(&mut client, |s| active_tab(s).title == "Project");
assert_eq!(active_tab(&named).manual_title.as_deref(), Some("Project"));
assert_eq!(named.panes[0].title, "Build");
send(&mut client, ClientMessage::FocusPane { pane: first });
input(&mut client, first, "printf '\\033]2;Editor\\007'\n");
let changed = snapshot(&mut client, |s| s.panes[0].title == "Editor");
assert_eq!(active_tab(&changed).title, "Project");
send(&mut client, ClientMessage::FocusPane { pane: second });
input(&mut client, second, "exit\n");
let exited = snapshot(&mut client, |s| active_tab(s).panes.len() == 1);
assert_eq!(active_tab(&exited).active_pane, first);
assert_eq!(active_tab(&exited).title, "Project");
send(&mut client, ClientMessage::NewTab { cwd: None });
let another = snapshot(&mut client, |s| s.tabs.len() == 2);
let other_tab = another.active_tab;
assert!(active_tab(&another).manual_title.is_none());
assert_eq!(active_tab(&another).title, another.panes[0].title);
// An independent client can rename an inactive tab without stealing
// focus. All attached views receive the server's name immediately.
let mut other = server.client();
send(
&mut other,
ClientMessage::RenameTab {
tab,
name: Some("Server name".into()),
},
);
let renamed = snapshot(&mut client, |s| {
s.tabs
.iter()
.any(|entry| entry.id == tab && entry.title == "Server name")
});
assert_eq!(renamed.active_tab, other_tab);
send(&mut client, ClientMessage::Detach);
drop(client);
let mut client = server.client();
send(&mut client, ClientMessage::Attach);
let reattached = snapshot(&mut client, |s| s.tabs.len() == 2);
let saved = reattached
.tabs
.iter()
.find(|entry| entry.id == tab)
.unwrap();
assert_eq!(saved.title, "Server name");
assert_eq!(saved.manual_title.as_deref(), Some("Server name"));
send(&mut client, ClientMessage::FocusTab { tab });
send(
&mut client,
ClientMessage::RenameTab {
tab,
name: Some(" \n\t ".into()),
},
);
let automatic = snapshot(&mut client, |s| active_tab(s).title == "Editor");
assert!(active_tab(&automatic).manual_title.is_none());
input(&mut client, first, "printf '\\033]2;Changed\\007'\n");
snapshot(&mut client, |s| active_tab(s).title == "Changed");
send(
&mut client,
ClientMessage::RenamePane {
pane: first,
name: Some("Pane override".into()),
},
);
snapshot(&mut client, |s| active_tab(s).title == "Pane override");
send(
&mut client,
ClientMessage::RenameTab {
tab,
name: Some("Pinned again".into()),
},
);
snapshot(&mut client, |s| active_tab(s).title == "Pinned again");
send(&mut client, ClientMessage::RenameTab { tab, name: None });
let automatic = snapshot(&mut client, |s| active_tab(s).title == "Pane override");
assert!(active_tab(&automatic).manual_title.is_none());
}
#[test]
fn displayed_titles_are_bounded_single_line_text() {
assert_eq!(
+86 -1
View File
@@ -29,6 +29,12 @@ use std::{
};
const PADDING: f32 = 6.;
struct TabRename {
tab: u64,
editor: Entity<RenameField>,
_blur: Subscription,
}
pub struct Workspace {
client: Client,
connection: SharedString,
@@ -37,6 +43,7 @@ pub struct Workspace {
error: Option<String>,
focus: FocusHandle,
palette: Option<Entity<CommandPalette>>,
tab_rename: Option<TabRename>,
outgoing: HashMap<u64, Entity<TerminalPane>>,
outgoing_snapshot: Option<Arc<Snapshot>>,
transition: Option<Slide>,
@@ -44,6 +51,54 @@ pub struct Workspace {
_updates: Task<()>,
}
impl Workspace {
fn begin_tab_rename(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let Some(tab) = self.snapshot.as_ref().and_then(|snapshot| {
snapshot
.tabs
.iter()
.find(|tab| tab.id == snapshot.active_tab)
}) else {
return;
};
let id = tab.id;
let initial = tab.title.clone();
let owner = cx.weak_entity();
let editor = cx.new(|cx| {
RenameField::new(
initial,
move |name, window, cx| {
let _ = owner.update(cx, |view, cx| {
view.tab_rename = None;
if let Some(name) = name
&& let Err(error) = view.client.send(ClientMessage::RenameTab {
tab: id,
name: Some(name),
})
{
view.error = Some(error.to_string());
}
view.restore_focus(window, cx);
cx.notify();
});
},
window,
cx,
)
});
let focus = editor.read(cx).focus_handle(cx);
let blur = cx.on_blur(&focus, window, |view, _, cx| {
// Clicking elsewhere cancels the edit and keeps the clicked focus.
view.tab_rename = None;
cx.notify();
});
self.tab_rename = Some(TabRename {
tab: id,
editor,
_blur: blur,
});
cx.notify();
}
fn focused_pane(&self) -> Option<Entity<TerminalPane>> {
let snapshot = self.snapshot.as_ref()?;
let tab = snapshot
@@ -72,6 +127,7 @@ impl Workspace {
self.close_palette(window, cx);
return;
}
self.tab_rename = None;
for pane in self.panes.values() {
pane.update(cx, |pane, cx| {
pane.rename = None;
@@ -111,6 +167,7 @@ impl Workspace {
return;
}
if self.palette.is_some()
|| self.tab_rename.is_some()
|| self
.focused_pane()
.is_some_and(|pane| pane.read(cx).rename.is_some())
@@ -149,6 +206,17 @@ impl Workspace {
}),
ClosePane => pane_id.map(|pane| ClientMessage::ClosePane { pane }),
ResetPaneName => pane_id.map(|pane| ClientMessage::RenamePane { pane, name: None }),
ResetTabName => self
.snapshot
.as_ref()
.map(|snapshot| ClientMessage::RenameTab {
tab: snapshot.active_tab,
name: None,
}),
RenameTab => {
self.begin_tab_rename(window, cx);
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 }),
@@ -226,7 +294,16 @@ impl Workspace {
old.active_tab != snapshot.active_tab
|| old.tabs != snapshot.tabs
});
let focus_allowed = view.palette.is_none();
let cancelled_tab_rename =
view.tab_rename.as_ref().is_some_and(|rename| {
snapshot.active_tab != rename.tab
|| !snapshot.tabs.iter().any(|tab| tab.id == rename.tab)
});
if cancelled_tab_rename {
view.tab_rename = None;
}
let focus_allowed =
view.palette.is_none() && view.tab_rename.is_none();
if let Some(direction) = view
.snapshot
.as_ref()
@@ -321,6 +398,8 @@ impl Workspace {
palette
.update(cx, |palette, cx| palette.set_items(items, cx));
}
} else if cancelled_tab_rename {
view.restore_focus(window, cx);
} else if view.panes.is_empty() {
window.focus(&view.focus);
}
@@ -343,6 +422,7 @@ impl Workspace {
error: None,
focus,
palette: None,
tab_rename: None,
outgoing: HashMap::new(),
outgoing_snapshot: None,
transition: None,
@@ -386,6 +466,11 @@ impl Render for Workspace {
id: tab.id,
label: tab.title.clone().into(),
active: tab.id == s.active_tab,
editor: self
.tab_rename
.as_ref()
.filter(|rename| rename.tab == tab.id)
.map(|rename| rename.editor.clone()),
})
.collect()
})
+1
View File
@@ -116,6 +116,7 @@ mod tests {
crate::protocol::TabInfo {
id: 1,
title: String::new(),
manual_title: None,
panes: ids.to_vec(),
active_pane: active,
stacks: vec![crate::protocol::StackInfo {