T63: a double press on a session's row renames it, which a user asked for after 0.1.6: Ely's ListItem::editor, added for this, draws a field in the title's place; Enter keeps the name, Escape drops it, a blur keeps it; the name stands over the program's title in the row, the header and the window's title until renamed again, and an empty one gives the title back; Ely pinned to 7f7a715
This commit is contained in:
9 files changed
+136
-17
No files matched your search
@@ -0,0 +1,77 @@
|
||||
//! A session renamed in place, as a double-click on its row asks.
|
||||
|
||||
use ely_gpui_component::forms::{InputEvent, TextInput};
|
||||
use gpui::{AppContext, Context, EntityId, Focusable, Window};
|
||||
|
||||
use super::Workspace;
|
||||
|
||||
impl Workspace {
|
||||
/// Opens a field over session `index`'s title, what it is called selected.
|
||||
pub fn rename(&mut self, index: usize, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let Some(session) = self.sessions.get(index) else {
|
||||
return;
|
||||
};
|
||||
if session.editing.is_some() {
|
||||
return;
|
||||
}
|
||||
let named = session.named().to_string();
|
||||
let id = session.terminal.entity_id();
|
||||
let input = cx.new(|cx| {
|
||||
let mut input = TextInput::new(window, cx);
|
||||
input.set_text(named.clone(), cx);
|
||||
input.select(0..named.len(), cx);
|
||||
input
|
||||
});
|
||||
let events = cx.subscribe_in(
|
||||
&input,
|
||||
window,
|
||||
move |this, _, event, window, cx| match event {
|
||||
InputEvent::Submit => this.end_rename(id, true, true, window, cx),
|
||||
InputEvent::Blur => this.end_rename(id, true, false, window, cx),
|
||||
InputEvent::Changed | InputEvent::Focus => {}
|
||||
},
|
||||
);
|
||||
window.focus(&input.focus_handle(cx), cx);
|
||||
log::debug!("app: session {} is renamed in place", index + 1);
|
||||
self.sessions[index].editing = Some((input, events));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Closes the field renaming the session `id` names, `keep`ing what it holds or not, the shown terminal `focused` again or not.
|
||||
pub fn end_rename(
|
||||
&mut self,
|
||||
id: EntityId,
|
||||
keep: bool,
|
||||
focused: bool,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let Some(session) = self
|
||||
.sessions
|
||||
.iter_mut()
|
||||
.find(|session| session.terminal.entity_id() == id)
|
||||
else {
|
||||
log::debug!("app: the session renamed is gone");
|
||||
return;
|
||||
};
|
||||
let Some((input, _)) = session.editing.take() else {
|
||||
return;
|
||||
};
|
||||
let pid = session.terminal.read(cx).pid();
|
||||
if !keep {
|
||||
log::debug!("app: session {pid} keeps its name");
|
||||
} else {
|
||||
session.name = input.read(cx).text().trim().to_string();
|
||||
if session.name.is_empty() {
|
||||
log::info!("app: session {pid} takes its program's title again");
|
||||
} else {
|
||||
log::info!("app: session {pid} is named {:?}", session.name);
|
||||
}
|
||||
}
|
||||
if focused && let Some(shown) = self.sessions.get(self.active) {
|
||||
window.focus(&shown.terminal.focus_handle(cx), cx);
|
||||
}
|
||||
self.retitle(window);
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user