Add searchable command palette with nested menus and shared dispatch
This commit is contained in:
+216
-66
@@ -3,9 +3,11 @@ use crate::{
|
||||
chrome,
|
||||
client::{Client, Updates},
|
||||
colors,
|
||||
commands::{self, CommandAction},
|
||||
cursor::CursorAnimator,
|
||||
input,
|
||||
label::middle_label,
|
||||
palette::CommandPalette,
|
||||
pane_name::RenameField,
|
||||
protocol::{
|
||||
ClientMessage, PaneSnapshot, ScrollKind, SelectionCommand, SelectionKind, SelectionSide,
|
||||
@@ -33,12 +35,205 @@ pub struct Workspace {
|
||||
snapshot: Option<Arc<Snapshot>>,
|
||||
panes: HashMap<u64, Entity<TerminalPane>>,
|
||||
error: Option<String>,
|
||||
focus: FocusHandle,
|
||||
palette: Option<Entity<CommandPalette>>,
|
||||
outgoing: Vec<Entity<TerminalPane>>,
|
||||
transition: Option<Slide>,
|
||||
transition_frame_pending: bool,
|
||||
_updates: Task<()>,
|
||||
}
|
||||
impl Workspace {
|
||||
fn focused_pane(&self) -> Option<Entity<TerminalPane>> {
|
||||
let snapshot = self.snapshot.as_ref()?;
|
||||
let tab = snapshot
|
||||
.tabs
|
||||
.iter()
|
||||
.find(|tab| tab.id == snapshot.active_tab)?;
|
||||
self.panes.get(&tab.active_pane).cloned()
|
||||
}
|
||||
|
||||
fn restore_focus(&self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if let Some(pane) = self.focused_pane() {
|
||||
window.focus(&pane.read(cx).focus);
|
||||
} else {
|
||||
window.focus(&self.focus);
|
||||
}
|
||||
}
|
||||
|
||||
fn close_palette(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
self.palette = None;
|
||||
self.restore_focus(window, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn open_palette(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
if self.palette.is_some() {
|
||||
self.close_palette(window, cx);
|
||||
return;
|
||||
}
|
||||
for pane in self.panes.values() {
|
||||
pane.update(cx, |pane, cx| {
|
||||
pane.rename = None;
|
||||
pane.selecting = false;
|
||||
pane.preedit.clear();
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
let items = commands::root_items(self.snapshot.as_deref());
|
||||
let owner = cx.weak_entity();
|
||||
self.palette = Some(cx.new(|cx| {
|
||||
CommandPalette::new(
|
||||
items,
|
||||
move |action, window, cx| {
|
||||
let _ = owner.update(cx, |view, cx| {
|
||||
view.close_palette(window, cx);
|
||||
if let Some(action) = action {
|
||||
view.execute(action, window, cx);
|
||||
}
|
||||
});
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
}));
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let key = &event.keystroke;
|
||||
let command = (key.modifiers.control || key.modifiers.platform)
|
||||
&& key.modifiers.shift
|
||||
&& !key.modifiers.alt;
|
||||
if command && key.key.eq_ignore_ascii_case("p") {
|
||||
self.open_palette(window, cx);
|
||||
cx.stop_propagation();
|
||||
return;
|
||||
}
|
||||
if self.palette.is_some()
|
||||
|| self
|
||||
.focused_pane()
|
||||
.is_some_and(|pane| pane.read(cx).rename.is_some())
|
||||
{
|
||||
return;
|
||||
}
|
||||
let action = if command {
|
||||
match key.key.to_lowercase().as_str() {
|
||||
"t" => Some(CommandAction::NewTab),
|
||||
"d" => Some(CommandAction::SplitPane),
|
||||
"w" => Some(CommandAction::ClosePane),
|
||||
"r" => Some(CommandAction::RenamePane),
|
||||
"c" => Some(CommandAction::Copy),
|
||||
"v" => Some(CommandAction::Paste),
|
||||
"left" => Some(CommandAction::PreviousPane),
|
||||
"right" => Some(CommandAction::NextPane),
|
||||
_ => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(action) = action {
|
||||
self.execute(action, window, cx);
|
||||
cx.stop_propagation();
|
||||
}
|
||||
}
|
||||
|
||||
fn execute(&mut self, action: CommandAction, window: &mut Window, cx: &mut Context<Self>) {
|
||||
use CommandAction::*;
|
||||
let pane = self.focused_pane();
|
||||
let pane_id = pane.as_ref().map(|pane| pane.read(cx).data().id);
|
||||
let message = match action {
|
||||
NewTab => Some(ClientMessage::NewTab { cwd: None }),
|
||||
SplitPane => pane_id.map(|pane| ClientMessage::Split { pane, cwd: None }),
|
||||
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
|
||||
} else {
|
||||
(index + count - 1) % count
|
||||
};
|
||||
Some(ClientMessage::FocusPane {
|
||||
pane: snapshot.panes[next].id,
|
||||
})
|
||||
}),
|
||||
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));
|
||||
}
|
||||
None
|
||||
}
|
||||
Paste => {
|
||||
if let Some(pane) = pane
|
||||
&& let Some(text) = cx.read_from_clipboard().and_then(|item| item.text())
|
||||
{
|
||||
pane.update(cx, |pane, cx| {
|
||||
pane.send(
|
||||
input::paste_text(
|
||||
&text,
|
||||
pane.mode().contains(TermMode::BRACKETED_PASTE),
|
||||
),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
Detach => {
|
||||
window.remove_window();
|
||||
None
|
||||
}
|
||||
};
|
||||
if let Some(message) = message
|
||||
&& let Err(error) = self.client.send(message)
|
||||
{
|
||||
self.error = Some(error.to_string());
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -46,6 +241,8 @@ impl Workspace {
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Self {
|
||||
let focus = cx.focus_handle();
|
||||
window.focus(&focus);
|
||||
let task = cx.spawn_in(window, async move |view, cx| {
|
||||
while let Some(update) = updates.recv().await {
|
||||
if cx
|
||||
@@ -140,6 +337,15 @@ impl Workspace {
|
||||
.unwrap_or("Terminal");
|
||||
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);
|
||||
});
|
||||
} else if view.panes.is_empty() {
|
||||
window.focus(&view.focus);
|
||||
}
|
||||
}
|
||||
cx.notify();
|
||||
})
|
||||
@@ -157,6 +363,8 @@ impl Workspace {
|
||||
snapshot: None,
|
||||
panes: HashMap::new(),
|
||||
error: None,
|
||||
focus,
|
||||
palette: None,
|
||||
outgoing: Vec::new(),
|
||||
transition: None,
|
||||
transition_frame_pending: false,
|
||||
@@ -254,6 +462,9 @@ impl Render for Workspace {
|
||||
}
|
||||
body = body.child(incoming);
|
||||
let mut root = div()
|
||||
.relative()
|
||||
.track_focus(&self.focus)
|
||||
.capture_key_down(cx.listener(Self::key_down))
|
||||
.size_full()
|
||||
.flex()
|
||||
.flex_col()
|
||||
@@ -271,6 +482,9 @@ impl Render for Workspace {
|
||||
.child(error.clone()),
|
||||
);
|
||||
}
|
||||
if let Some(palette) = &self.palette {
|
||||
root = root.child(palette.clone());
|
||||
}
|
||||
root
|
||||
}
|
||||
}
|
||||
@@ -500,77 +714,13 @@ impl TerminalPane {
|
||||
header
|
||||
}
|
||||
|
||||
fn key_down(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context<Self>) {
|
||||
fn key_down(&mut self, event: &KeyDownEvent, _: &mut Window, cx: &mut Context<Self>) {
|
||||
if self.read_only || self.rename.is_some() {
|
||||
return;
|
||||
}
|
||||
let key = &event.keystroke;
|
||||
let pane = self.data().id;
|
||||
self.activity(cx);
|
||||
if key.modifiers.control
|
||||
&& key.modifiers.shift
|
||||
&& !key.modifiers.alt
|
||||
&& !key.modifiers.platform
|
||||
{
|
||||
let handled = match key.key.to_lowercase().as_str() {
|
||||
"r" => {
|
||||
self.begin_rename(window, cx);
|
||||
true
|
||||
}
|
||||
"c" => {
|
||||
self.command(ClientMessage::Copy { pane });
|
||||
true
|
||||
}
|
||||
"v" => {
|
||||
if let Some(text) = cx.read_from_clipboard().and_then(|item| item.text()) {
|
||||
self.send(
|
||||
input::paste_text(
|
||||
&text,
|
||||
self.mode().contains(TermMode::BRACKETED_PASTE),
|
||||
),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
true
|
||||
}
|
||||
"t" => {
|
||||
self.command(ClientMessage::NewTab { cwd: None });
|
||||
true
|
||||
}
|
||||
"d" => {
|
||||
self.command(ClientMessage::Split { pane, cwd: None });
|
||||
true
|
||||
}
|
||||
"w" => {
|
||||
self.command(ClientMessage::ClosePane { pane });
|
||||
true
|
||||
}
|
||||
"left" | "right" => {
|
||||
if let Some(tab) = self
|
||||
.snapshot
|
||||
.tabs
|
||||
.iter()
|
||||
.find(|t| t.id == self.snapshot.active_tab)
|
||||
&& let Some(index) = tab.panes.iter().position(|p| *p == pane)
|
||||
{
|
||||
let next = if key.key == "right" {
|
||||
(index + 1) % tab.panes.len()
|
||||
} else {
|
||||
(index + tab.panes.len() - 1) % tab.panes.len()
|
||||
};
|
||||
self.command(ClientMessage::FocusPane {
|
||||
pane: tab.panes[next],
|
||||
});
|
||||
}
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
if handled {
|
||||
cx.stop_propagation();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if key.modifiers.shift
|
||||
&& !key.modifiers.control
|
||||
&& !key.modifiers.alt
|
||||
@@ -823,7 +973,7 @@ impl Render for TerminalPane {
|
||||
let mut delay=None;
|
||||
window.with_content_mask(Some(ContentMask{bounds}),|window| {
|
||||
let origin=bounds.origin+point(px(PADDING),px(PADDING));
|
||||
let focused=window.is_window_active() && pane.active && pane.rename.is_none();
|
||||
let focused=window.is_window_active() && pane.active && pane.focus.is_focused(window) && pane.rename.is_none();
|
||||
pane.renderer.paint_with_cursor(origin,window,cx,|window| {
|
||||
if !pane.transitioning {
|
||||
delay=pane.cursor.paint(Instant::now(),focused,window);
|
||||
|
||||
Reference in New Issue
Block a user