300 lines
7.8 KiB
Rust
300 lines
7.8 KiB
Rust
//! Command metadata shared by the palette and workspace dispatch.
|
|||
|
|
//!
|
||
|
|
//! Add an action and a registry entry here to expose a command. A shortcut is
|
||
|
|
//! optional; menus use the same item model and may be nested to any depth.
|
||
|
|
use crate::protocol::{ScrollKind, Snapshot};
|
||
|
|
|
||
|
|
#[derive(Clone, Debug)]
|
||
|
|
pub enum CommandAction {
|
||
|
|
NewTab,
|
||
|
|
SplitPane,
|
||
|
|
ClosePane,
|
||
|
|
RenamePane,
|
||
|
|
ResetPaneName,
|
||
|
|
Copy,
|
||
|
|
Paste,
|
||
|
|
PreviousTab,
|
||
|
|
NextTab,
|
||
|
|
PreviousPane,
|
||
|
|
NextPane,
|
||
|
|
FocusLeft,
|
||
|
|
FocusRight,
|
||
|
|
FocusTab(u64),
|
||
|
|
FocusPane(u64),
|
||
|
|
Scroll(ScrollKind),
|
||
|
|
Detach,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Clone, Debug)]
|
||
|
|
pub enum PaletteItemKind {
|
||
|
|
Action(CommandAction),
|
||
|
|
Menu(Vec<PaletteItem>),
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Clone, Debug)]
|
||
|
|
pub struct PaletteItem {
|
||
|
|
/// Stable across snapshots, so open submenus survive background updates.
|
||
|
|
pub id: String,
|
||
|
|
pub title: String,
|
||
|
|
pub keywords: String,
|
||
|
|
pub shortcut: Option<&'static str>,
|
||
|
|
pub enabled: bool,
|
||
|
|
pub kind: PaletteItemKind,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl PaletteItem {
|
||
|
|
pub fn action(
|
||
|
|
id: impl Into<String>,
|
||
|
|
title: impl Into<String>,
|
||
|
|
keywords: impl Into<String>,
|
||
|
|
shortcut: Option<&'static str>,
|
||
|
|
action: CommandAction,
|
||
|
|
) -> Self {
|
||
|
|
Self {
|
||
|
|
id: id.into(),
|
||
|
|
title: title.into(),
|
||
|
|
keywords: keywords.into(),
|
||
|
|
shortcut,
|
||
|
|
enabled: true,
|
||
|
|
kind: PaletteItemKind::Action(action),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn menu(
|
||
|
|
id: impl Into<String>,
|
||
|
|
title: impl Into<String>,
|
||
|
|
keywords: impl Into<String>,
|
||
|
|
items: Vec<Self>,
|
||
|
|
) -> Self {
|
||
|
|
Self {
|
||
|
|
id: id.into(),
|
||
|
|
title: title.into(),
|
||
|
|
keywords: keywords.into(),
|
||
|
|
shortcut: None,
|
||
|
|
enabled: !items.is_empty(),
|
||
|
|
kind: PaletteItemKind::Menu(items),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn available(mut self, enabled: bool) -> Self {
|
||
|
|
self.enabled = enabled;
|
||
|
|
self
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// The complete command list. UI state affects availability, never discovery.
|
||
|
|
pub fn root_items(snapshot: Option<&Snapshot>) -> Vec<PaletteItem> {
|
||
|
|
use CommandAction::*;
|
||
|
|
let pane = snapshot.is_some_and(|s| !s.panes.is_empty());
|
||
|
|
let tabs = snapshot.is_some_and(|s| !s.tabs.is_empty());
|
||
|
|
let mut items = vec![
|
||
|
|
PaletteItem::action(
|
||
|
|
"new-tab",
|
||
|
|
"New tab",
|
||
|
|
"create shell terminal",
|
||
|
|
Some("Ctrl+Shift+T"),
|
||
|
|
NewTab,
|
||
|
|
),
|
||
|
|
PaletteItem::action(
|
||
|
|
"split-pane",
|
||
|
|
"Split pane right",
|
||
|
|
"new create horizontal shell",
|
||
|
|
Some("Ctrl+Shift+D"),
|
||
|
|
SplitPane,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"close-pane",
|
||
|
|
"Close pane",
|
||
|
|
"exit shell terminate",
|
||
|
|
Some("Ctrl+Shift+W"),
|
||
|
|
ClosePane,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"rename-pane",
|
||
|
|
"Rename pane",
|
||
|
|
"name title label",
|
||
|
|
Some("Ctrl+Shift+R"),
|
||
|
|
RenamePane,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"reset-pane-name",
|
||
|
|
"Use automatic pane name",
|
||
|
|
"reset rename title cwd directory",
|
||
|
|
None,
|
||
|
|
ResetPaneName,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"previous-tab",
|
||
|
|
"Previous tab",
|
||
|
|
"switch navigate left",
|
||
|
|
None,
|
||
|
|
PreviousTab,
|
||
|
|
)
|
||
|
|
.available(tabs),
|
||
|
|
PaletteItem::action(
|
||
|
|
"next-tab",
|
||
|
|
"Next tab",
|
||
|
|
"switch navigate right",
|
||
|
|
None,
|
||
|
|
NextTab,
|
||
|
|
)
|
||
|
|
.available(tabs),
|
||
|
|
PaletteItem::action(
|
||
|
|
"previous-pane",
|
||
|
|
"Previous pane",
|
||
|
|
"switch focus cycle",
|
||
|
|
Some("Ctrl+Shift+Left"),
|
||
|
|
PreviousPane,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"next-pane",
|
||
|
|
"Next pane",
|
||
|
|
"switch focus cycle",
|
||
|
|
Some("Ctrl+Shift+Right"),
|
||
|
|
NextPane,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"focus-left",
|
||
|
|
"Focus pane or tab left",
|
||
|
|
"navigate previous horizontal",
|
||
|
|
None,
|
||
|
|
FocusLeft,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"focus-right",
|
||
|
|
"Focus pane or tab right",
|
||
|
|
"navigate next horizontal",
|
||
|
|
None,
|
||
|
|
FocusRight,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"copy",
|
||
|
|
"Copy selection",
|
||
|
|
"clipboard text",
|
||
|
|
Some("Ctrl+Shift+C"),
|
||
|
|
Copy,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"paste",
|
||
|
|
"Paste",
|
||
|
|
"clipboard text",
|
||
|
|
Some("Ctrl+Shift+V"),
|
||
|
|
Paste,
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"history-page-up",
|
||
|
|
"Scroll history up one page",
|
||
|
|
"scrollback previous page",
|
||
|
|
Some("Shift+PageUp"),
|
||
|
|
Scroll(ScrollKind::PageUp),
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"history-page-down",
|
||
|
|
"Scroll history down one page",
|
||
|
|
"scrollback next page",
|
||
|
|
Some("Shift+PageDown"),
|
||
|
|
Scroll(ScrollKind::PageDown),
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"history-top",
|
||
|
|
"Go to oldest history",
|
||
|
|
"scrollback top beginning",
|
||
|
|
Some("Shift+Home"),
|
||
|
|
Scroll(ScrollKind::Top),
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"history-bottom",
|
||
|
|
"Return to live terminal",
|
||
|
|
"scrollback bottom latest end",
|
||
|
|
Some("Shift+End"),
|
||
|
|
Scroll(ScrollKind::Bottom),
|
||
|
|
)
|
||
|
|
.available(pane),
|
||
|
|
PaletteItem::action(
|
||
|
|
"detach",
|
||
|
|
"Detach window",
|
||
|
|
"close window keep sessions running",
|
||
|
|
None,
|
||
|
|
Detach,
|
||
|
|
),
|
||
|
|
];
|
||
|
|
let tab_items = snapshot
|
||
|
|
.into_iter()
|
||
|
|
.flat_map(|snapshot| snapshot.tabs.iter())
|
||
|
|
.enumerate()
|
||
|
|
.map(|(index, tab)| {
|
||
|
|
PaletteItem::action(
|
||
|
|
format!("tab-{}", tab.id),
|
||
|
|
format!("{} {}", index + 1, tab.title),
|
||
|
|
"switch focus tab",
|
||
|
|
None,
|
||
|
|
FocusTab(tab.id),
|
||
|
|
)
|
||
|
|
})
|
||
|
|
.collect();
|
||
|
|
let pane_items = snapshot
|
||
|
|
.into_iter()
|
||
|
|
.flat_map(|snapshot| snapshot.panes.iter())
|
||
|
|
.enumerate()
|
||
|
|
.map(|(index, pane)| {
|
||
|
|
PaletteItem::action(
|
||
|
|
format!("pane-{}", pane.id),
|
||
|
|
format!("{} {}", index + 1, pane.title),
|
||
|
|
"switch focus pane",
|
||
|
|
None,
|
||
|
|
FocusPane(pane.id),
|
||
|
|
)
|
||
|
|
})
|
||
|
|
.collect();
|
||
|
|
items.insert(
|
||
|
|
5,
|
||
|
|
PaletteItem::menu(
|
||
|
|
"switch-tab",
|
||
|
|
"Switch to tab…",
|
||
|
|
"navigate focus tabs",
|
||
|
|
tab_items,
|
||
|
|
),
|
||
|
|
);
|
||
|
|
items.insert(
|
||
|
|
6,
|
||
|
|
PaletteItem::menu(
|
||
|
|
"focus-pane",
|
||
|
|
"Focus pane…",
|
||
|
|
"navigate switch panes",
|
||
|
|
pane_items,
|
||
|
|
),
|
||
|
|
);
|
||
|
|
items
|
||
|
|
}
|
||
|
|
|
||
|
|
#[cfg(test)]
|
||
|
|
mod tests {
|
||
|
|
use super::*;
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn empty_workspace_keeps_commands_discoverable_without_unsafe_actions() {
|
||
|
|
let items = root_items(None);
|
||
|
|
let find = |id: &str| items.iter().find(|item| item.id == id).unwrap();
|
||
|
|
assert!(find("new-tab").enabled);
|
||
|
|
assert!(find("detach").enabled);
|
||
|
|
assert!(!find("close-pane").enabled);
|
||
|
|
assert!(!find("switch-tab").enabled);
|
||
|
|
assert!(find("reset-pane-name").shortcut.is_none());
|
||
|
|
let unique: std::collections::HashSet<_> = items.iter().map(|item| &item.id).collect();
|
||
|
|
assert_eq!(unique.len(), items.len());
|
||
|
|
}
|
||
|
|
}
|