Add manual tab names with selected-pane fallback
This commit is contained in:
+86
-1
@@ -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()
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user