use std::sync::Arc;
use crate::prelude::*;
#[cfg_attr(feature = "hot-reload", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HotKey {
pub key: Key,
pub mods: Modifiers,
}
impl HotKey {
pub fn new(mods: Modifiers, key: Key) -> Self {
Self { key, mods }
}
pub fn primary(key: Key) -> Self {
#[cfg(target_os = "macos")]
let mods = Modifiers::META;
#[cfg(not(target_os = "macos"))]
let mods = Modifiers::CONTROL;
Self::new(mods, key)
}
pub fn primary_shift(key: Key) -> Self {
#[cfg(target_os = "macos")]
let mods = Modifiers::META | Modifiers::SHIFT;
#[cfg(not(target_os = "macos"))]
let mods = Modifiers::CONTROL | Modifiers::SHIFT;
Self::new(mods, key)
}
}
#[cfg_attr(all(feature = "hot-reload", debug_assertions), derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StandardAction {
Copy,
Cut,
Paste,
SelectAll,
}
#[cfg_attr(all(feature = "hot-reload", debug_assertions), derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub enum MenuItem {
Action {
title: LocalizedString,
command: CommandId,
shortcut: Option<HotKey>,
enabled: bool,
selected: bool,
},
Submenu { title: LocalizedString, menu: MenuDesc, enabled: bool },
Standard(StandardAction),
Separator,
}
#[cfg_attr(all(feature = "hot-reload", debug_assertions), derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Default)]
pub struct MenuDesc {
pub items: Arc<Vec<MenuItem>>,
}
impl MenuDesc {
pub fn new() -> Self {
Self { items: Arc::new(Vec::new()) }
}
pub fn from_items(items: Vec<MenuItem>) -> Self {
Self { items: Arc::new(items) }
}
pub fn add_item(mut self, item: MenuItem) -> Self {
Arc::make_mut(&mut self.items).push(item);
self
}
pub fn add_separator(mut self) -> Self {
Arc::make_mut(&mut self.items).push(MenuItem::Separator);
self
}
}