use std::rc::Rc;
use gpui_kit::component::menu::PopupMenu;
use gpui_kit::{Action, App, Context, SharedString, Window};
use subspace_lucide::IconName;
use crate::Callback;
pub(super) type SubmenuBuilder =
Rc<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu>;
pub(crate) type Extension =
Box<dyn FnOnce(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MenuGroup {
Open,
Act,
Copy,
Chat,
View,
Remove,
Place,
}
impl MenuGroup {
pub const ORDER: [Self; 7] = [
Self::Open,
Self::Act,
Self::Copy,
Self::Chat,
Self::View,
Self::Remove,
Self::Place,
];
pub fn is_danger(self) -> bool {
self == Self::Remove
}
}
pub(super) enum Run {
Click(Callback),
Action(Box<dyn Action>),
Submenu(SubmenuBuilder),
}
pub struct MenuCommand {
pub(super) group: MenuGroup,
pub(super) label: SharedString,
pub(super) icon: IconName,
pub(super) unavailable: Option<SharedString>,
pub(super) key: Option<Box<dyn Action>>,
pub(super) run: Run,
}
impl MenuCommand {
pub fn new(
group: MenuGroup,
label: impl Into<SharedString>,
icon: IconName,
run: impl Fn(&mut Window, &mut App) + 'static,
) -> Self {
Self::with_run(group, label.into(), icon, Run::Click(Rc::new(run)))
}
pub fn action(
group: MenuGroup,
label: impl Into<SharedString>,
icon: IconName,
action: impl Action,
) -> Self {
Self::with_run(group, label.into(), icon, Run::Action(Box::new(action)))
}
pub fn submenu(
group: MenuGroup,
label: impl Into<SharedString>,
icon: IconName,
build: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> Self {
Self::with_run(group, label.into(), icon, Run::Submenu(Rc::new(build)))
}
fn with_run(group: MenuGroup, label: SharedString, icon: IconName, run: Run) -> Self {
Self {
group,
label,
icon,
unavailable: None,
key: None,
run,
}
}
pub fn key(mut self, action: impl Action) -> Self {
self.key = Some(Box::new(action));
self
}
pub fn disabled(mut self, disabled: bool, reason: impl Into<SharedString>) -> Self {
self.unavailable = disabled.then(|| reason.into());
self
}
pub fn group(&self) -> MenuGroup {
self.group
}
pub fn label(&self) -> &SharedString {
&self.label
}
pub fn reason(&self) -> Option<&SharedString> {
self.unavailable.as_ref()
}
}
#[derive(Default)]
pub struct GroupedMenu {
pub(super) commands: Vec<MenuCommand>,
pub(super) extension: Option<Extension>,
}
impl GroupedMenu {
pub fn new() -> Self {
Self::default()
}
pub fn with(mut self, command: MenuCommand) -> Self {
self.commands.push(command);
self
}
pub fn push(&mut self, command: MenuCommand) {
self.commands.push(command);
}
pub fn is_empty(&self) -> bool {
self.commands.is_empty() && self.extension.is_none()
}
pub(crate) fn extend_act(mut self, extension: Extension) -> Self {
self.extension = Some(extension);
self
}
pub fn outline(&self) -> Vec<MenuLine> {
layout(&self.commands, self.extension.is_some())
.into_iter()
.map(|slot| outline_line(&self.commands, slot))
.collect()
}
}
impl Extend<MenuCommand> for GroupedMenu {
fn extend<I: IntoIterator<Item = MenuCommand>>(&mut self, commands: I) {
self.commands.extend(commands);
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MenuLine {
Separator,
Command {
group: MenuGroup,
label: SharedString,
enabled: bool,
danger: bool,
},
Reason(SharedString),
Extension,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum Slot {
Separator,
Command(usize),
Reason(SharedString),
Extension,
}
pub(super) fn layout(commands: &[MenuCommand], extension: bool) -> Vec<Slot> {
let mut slots = Vec::new();
for group in MenuGroup::ORDER {
let extended = extension && group == MenuGroup::Act;
let members: Vec<usize> = (0..commands.len())
.filter(|index| commands[*index].group == group)
.collect();
if members.is_empty() && !extended {
continue;
}
if !slots.is_empty() {
slots.push(Slot::Separator);
}
for index in members {
slots.push(Slot::Command(index));
let reason = commands[index].unavailable.clone();
slots.extend(
reason
.filter(|why| !why.trim().is_empty())
.map(Slot::Reason),
);
}
if extended {
slots.push(Slot::Extension);
}
}
slots
}
fn outline_line(commands: &[MenuCommand], slot: Slot) -> MenuLine {
match slot {
Slot::Separator => MenuLine::Separator,
Slot::Reason(reason) => MenuLine::Reason(reason),
Slot::Extension => MenuLine::Extension,
Slot::Command(index) => {
let command = &commands[index];
let enabled = command.unavailable.is_none();
MenuLine::Command {
group: command.group,
label: command.label.clone(),
enabled,
danger: enabled && command.group.is_danger(),
}
}
}
}