use crate::{
command::Command,
menus::{ActionItem, MenuItem, SeparatorItem, SubMenuItem},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Menu {
pub title: String,
pub items: Vec<super::MenuItem>,
pub enabled: bool,
pub hotkey: Option<char>,
pub focused_item: Option<usize>,
}
impl Menu {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
items: Vec::new(),
enabled: true,
hotkey: None,
focused_item: None,
}
}
pub fn with_hotkey(title: impl Into<String>, hotkey: char) -> Self {
Self {
title: title.into(),
items: Vec::new(),
enabled: true,
hotkey: Some(hotkey),
focused_item: None,
}
}
pub fn with_items<S: Into<String>>(
title: S,
hotkey: Option<char>,
items: Vec<MenuItem>,
) -> Self {
Self {
title: title.into(),
items,
enabled: true,
hotkey,
focused_item: None,
}
}
pub fn hotkey(mut self, hotkey: char) -> Self {
self.hotkey = Some(hotkey);
self
}
pub fn item(mut self, item: MenuItem) -> Self {
self.items.push(item);
self
}
pub fn items(mut self, items: Vec<MenuItem>) -> Self {
self.items.extend(items);
self
}
pub fn add_action<S: Into<String>, C: Into<Command>>(
&mut self,
label: S,
command: C,
hotkey: Option<char>,
) -> &mut Self {
let action = ActionItem::new(label, command);
let action = if let Some(hk) = hotkey {
action.hotkey(hk)
} else {
action
};
self.items.push(MenuItem::Action(action));
self
}
pub fn add_separator(&mut self) -> &mut Self {
self.items.push(MenuItem::Separator(SeparatorItem::new()));
self
}
pub fn add_submenu<S: Into<String>>(&mut self, label: S, hotkey: Option<char>) -> &mut Self {
self.items.push(MenuItem::SubMenu(SubMenuItem::with_items(
label,
hotkey,
vec![],
)));
self
}
pub fn add_item(&mut self, item: MenuItem) {
self.items.push(item);
}
}