use std::rc::Rc;
use gpui::{App, SharedString, Window};
use crate::{hooks::EventHandler, icon::IconName};
#[cfg_attr(not(feature = "full"), allow(dead_code))]
#[derive(Clone)]
pub(super) enum MenuItemKind {
Action,
Checkbox(bool),
Radio(bool),
SelectOption(bool),
Submenu(Menu),
}
#[derive(Clone)]
pub struct MenuItem {
pub(super) label: SharedString,
pub(super) icon: Option<IconName>,
pub(super) shortcut: Option<SharedString>,
pub(super) disabled: bool,
pub(super) destructive: bool,
pub(super) inset: bool,
pub(super) kind: MenuItemKind,
on_select: Option<EventHandler<()>>,
}
impl MenuItem {
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
icon: None,
shortcut: None,
disabled: false,
destructive: false,
inset: false,
kind: MenuItemKind::Action,
on_select: None,
}
}
#[must_use]
pub fn icon(mut self, icon: IconName) -> Self {
self.icon = Some(icon);
self
}
#[must_use]
pub fn shortcut(mut self, shortcut: impl Into<SharedString>) -> Self {
self.shortcut = Some(shortcut.into());
self
}
#[must_use]
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
#[must_use]
pub fn destructive(mut self) -> Self {
self.destructive = true;
self
}
#[must_use]
pub fn inset(mut self) -> Self {
self.inset = true;
self
}
#[must_use]
pub fn checked(mut self, checked: bool) -> Self {
self.kind = MenuItemKind::Checkbox(checked);
self
}
#[must_use]
pub fn radio(mut self, selected: bool) -> Self {
self.kind = MenuItemKind::Radio(selected);
self
}
#[cfg_attr(not(feature = "full"), allow(dead_code))]
#[doc(hidden)]
#[must_use]
pub fn select_option(mut self, selected: bool) -> Self {
self.kind = MenuItemKind::SelectOption(selected);
self
}
#[must_use]
pub fn submenu(mut self, menu: Menu) -> Self {
self.kind = MenuItemKind::Submenu(menu);
self
}
#[must_use]
pub fn on_select(mut self, handler: impl Fn(&(), &mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(handler));
self
}
fn is_selectable(&self) -> bool {
!self.disabled
}
}
#[derive(Clone)]
pub(super) enum MenuEntry {
Item(MenuItem),
Label(SharedString),
Separator,
}
#[derive(Clone, Default)]
pub struct Menu {
pub(super) entries: Vec<MenuEntry>,
}
impl Menu {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn item(mut self, item: MenuItem) -> Self {
self.entries.push(MenuEntry::Item(item));
self
}
#[must_use]
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.entries.push(MenuEntry::Label(label.into()));
self
}
#[must_use]
pub fn separator(mut self) -> Self {
self.entries.push(MenuEntry::Separator);
self
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub(super) fn selectable_indices(&self) -> Vec<usize> {
self.entries
.iter()
.enumerate()
.filter_map(|(index, entry)| match entry {
MenuEntry::Item(item) if item.is_selectable() => Some(index),
_ => None,
})
.collect()
}
}
#[doc(hidden)]
pub type CloseMenu = Rc<dyn Fn(&mut Window, &mut App)>;
pub(super) fn choose(item: &MenuItem, close_menu: &CloseMenu, window: &mut Window, cx: &mut App) {
if let Some(handler) = item.on_select.as_ref() {
handler(&(), window, cx);
}
close_menu(window, cx);
}