use std::rc::Rc;
use gpui::{
AnyElement, App, ElementId, Hsla, InteractiveElement, IntoElement, ParentElement, SharedString,
Styled, Window, div,
};
use gpui_base::TestSupportExt;
use gpui_material_theme::{CornerShape, TypeRole, TypographyEmphasis};
use crate::menu_appearance::{MenuItemStyle, MenuSurfaceStyle, Paint};
use crate::{Icon, IconName, SemanticTone};
pub(super) type Action = Rc<dyn Fn(&mut Window, &mut App)>;
pub(super) type Change = Rc<dyn Fn(bool, &mut Window, &mut App)>;
pub(super) type ItemContent = Rc<dyn Fn() -> AnyElement>;
pub(super) type ComposedTrigger = Box<dyn FnOnce(bool, Action) -> AnyElement>;
pub(super) type LazyItems = Box<dyn FnOnce(&mut Window, &mut App) -> Vec<MenuItem>>;
pub(super) enum MenuItems {
Eager(Vec<MenuItem>),
Lazy(LazyItems),
}
impl MenuItems {
pub(super) fn resolve(self, window: &mut Window, cx: &mut App) -> Vec<MenuItem> {
match self {
Self::Eager(items) => items,
Self::Lazy(build) => build(window, cx),
}
}
}
pub struct MenuItem {
pub(super) id: ElementId,
pub(super) label: SharedString,
pub(super) disabled: bool,
pub(super) kind: Kind,
pub(super) slots: ItemSlots,
pub(super) on_checked_change: Option<Change>,
pub(super) tone: SemanticTone,
pub(super) style: MenuItemStyle,
}
pub(super) enum Kind {
Action(Option<Action>),
Check(bool, Option<Action>),
Select(bool, Option<Action>),
Heading,
Divider,
Section {
rows: Vec<MenuItem>,
style: MenuSurfaceStyle,
inactive_shape: Option<CornerShape>,
},
Submenu(Vec<MenuItem>),
}
#[derive(Clone, Default)]
pub(super) struct ItemSlots {
pub(super) content: Option<ItemContent>,
pub(super) leading: Option<ItemContent>,
pub(super) supporting: Option<ItemContent>,
pub(super) trailing: Option<ItemContent>,
pub(super) shortcut: Option<SharedString>,
pub(super) selection_icon: Option<ItemContent>,
}
impl ItemSlots {
pub(super) fn label(&self, label: SharedString) -> AnyElement {
self.content
.as_ref()
.map_or_else(|| label.into_any_element(), |content| content())
}
pub(super) fn children(
&self,
label: SharedString,
marked: bool,
submenu: bool,
paint: Paint<'_>,
(supporting_color, shortcut_color): (Hsla, Hsla),
active: bool,
) -> Vec<AnyElement> {
let motion = paint.motion;
let icon_size = motion.px(18.);
let leading = if marked {
Some(self.selection_icon.as_ref().map_or_else(
|| {
Icon::new(IconName::Check)
.size(icon_size)
.into_any_element()
},
|icon| icon(),
))
} else {
self.leading.as_ref().map(|icon| icon())
};
let supporting_scale = paint.theme.typography.text_scale();
let body = div()
.flex_1()
.min_w_0()
.overflow_hidden()
.child(div().truncate().child(self.label(label)))
.children(self.supporting.as_ref().map(|supporting| {
div()
.text_size(motion.px(12. * supporting_scale))
.line_height(motion.px(16. * supporting_scale))
.text_color(supporting_color)
.child(supporting())
}));
let mut children = vec![
div()
.min_w(icon_size)
.flex_none()
.children(leading)
.into_any_element(),
body.into_any_element(),
];
if let Some(trailing) = self.trailing.as_ref() {
children.push(trailing());
} else if let Some(shortcut) = self.shortcut.as_ref().filter(|text| !text.is_empty()) {
let label_small = paint
.theme
.typography
.scale_style(TypeRole::LabelSmall.style(TypographyEmphasis::Baseline));
let hint = div()
.flex_none()
.text_size(motion.px(label_small.size))
.line_height(motion.px(label_small.line_height))
.text_color(shortcut_color)
.child(shortcut.clone());
children.push(if active {
hint.id("shortcut").test_support().into_any_element()
} else {
hint.into_any_element()
});
}
children.extend(submenu.then(|| {
Icon::new(IconName::ChevronRight)
.size(icon_size)
.mirror_in_rtl(true)
.into_any_element()
}));
children
}
}
impl MenuItem {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
disabled: false,
kind: Kind::Action(None),
slots: ItemSlots::default(),
on_checked_change: None,
tone: SemanticTone::Default,
style: MenuItemStyle::default(),
}
}
pub fn group(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
kind: Kind::Heading,
..Self::new(id, title)
}
}
pub fn divider(id: impl Into<ElementId>) -> Self {
Self {
kind: Kind::Divider,
..Self::new(id, "")
}
}
pub fn section(
id: impl Into<ElementId>,
name: impl Into<SharedString>,
rows: impl IntoIterator<Item = MenuItem>,
) -> Self {
Self {
kind: Kind::Section {
rows: rows.into_iter().collect(),
style: MenuSurfaceStyle::default(),
inactive_shape: None,
},
..Self::new(id, name)
}
}
pub fn group_appearance(mut self, appearance: MenuSurfaceStyle) -> Self {
if let Kind::Section { style, .. } = &mut self.kind {
*style = appearance;
}
self
}
pub fn inactive_shape(mut self, shape: CornerShape) -> Self {
if let Kind::Section { inactive_shape, .. } = &mut self.kind {
*inactive_shape = Some(shape);
}
self
}
pub fn appearance(mut self, style: MenuItemStyle) -> Self {
self.style = style;
self
}
pub fn tone(mut self, tone: SemanticTone) -> Self {
self.tone = tone;
self
}
pub fn destructive(self) -> Self {
self.tone(SemanticTone::Danger)
}
pub fn shortcut(mut self, text: impl Into<SharedString>) -> Self {
self.slots.shortcut = Some(text.into());
self
}
pub fn submenu(mut self, items: impl IntoIterator<Item = MenuItem>) -> Self {
self.kind = Kind::Submenu(items.into_iter().collect());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn checked(mut self, checked: bool) -> Self {
self.kind = Kind::Check(checked, self.action());
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.kind = Kind::Select(selected, self.action());
self
}
pub fn content<E: IntoElement>(mut self, build: impl Fn() -> E + 'static) -> Self {
self.slots.content = Some(Rc::new(move || build().into_any_element()));
self
}
pub fn leading<E: IntoElement>(mut self, build: impl Fn() -> E + 'static) -> Self {
self.slots.leading = Some(Rc::new(move || build().into_any_element()));
self
}
pub fn supporting<E: IntoElement>(mut self, build: impl Fn() -> E + 'static) -> Self {
self.slots.supporting = Some(Rc::new(move || build().into_any_element()));
self
}
pub fn trailing<E: IntoElement>(mut self, build: impl Fn() -> E + 'static) -> Self {
self.slots.trailing = Some(Rc::new(move || build().into_any_element()));
self
}
pub fn selection_icon<E: IntoElement>(mut self, build: impl Fn() -> E + 'static) -> Self {
self.slots.selection_icon = Some(Rc::new(move || build().into_any_element()));
self
}
pub fn on_checked_change(
mut self,
callback: impl Fn(bool, &mut Window, &mut App) + 'static,
) -> Self {
self.on_checked_change = Some(Rc::new(callback));
self
}
fn action(&self) -> Option<Action> {
match &self.kind {
Kind::Action(action) | Kind::Check(_, action) | Kind::Select(_, action) => {
action.clone()
}
_ => None,
}
}
pub(super) fn activation(&self) -> Option<Action> {
if let Kind::Check(checked, _) = self.kind
&& let Some(callback) = &self.on_checked_change
{
let callback = callback.clone();
return Some(Rc::new(move |window, cx| callback(!checked, window, cx)));
}
self.action()
}
pub fn on_activate(mut self, action: impl Fn(&mut Window, &mut App) + 'static) -> Self {
match &mut self.kind {
Kind::Action(handler) | Kind::Check(_, handler) | Kind::Select(_, handler) => {
*handler = Some(Rc::new(action));
}
_ => {}
}
self
}
}