use egui::Key;
use super::model::NSMenu;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MacKeyboardShortcut {
pub key: Key,
pub command: bool,
pub shift: bool,
pub alt: bool,
}
impl MacKeyboardShortcut {
pub const fn new(key: Key) -> Self {
Self {
key,
command: false,
shift: false,
alt: false,
}
}
pub const fn cmd(key: Key) -> Self {
Self {
key,
command: true,
shift: false,
alt: false,
}
}
pub const fn cmd_shift(key: Key) -> Self {
Self {
key,
command: true,
shift: true,
alt: false,
}
}
pub const fn cmd_alt(key: Key) -> Self {
Self {
key,
command: true,
shift: false,
alt: true,
}
}
pub fn matches(&self, input: &egui::InputState) -> bool {
let input_cmd = input.modifiers.command || input.modifiers.ctrl || input.modifiers.mac_cmd;
let input_shift = input.modifiers.shift;
let input_alt = input.modifiers.alt;
if input_cmd == self.command
&& input_shift == self.shift
&& input_alt == self.alt
&& input.key_pressed(self.key)
{
return true;
}
input.events.iter().any(|ev| {
if let egui::Event::Key {
key,
pressed: true,
modifiers,
..
} = ev
{
if *key == self.key {
let ev_cmd = modifiers.command || modifiers.ctrl || modifiers.mac_cmd;
return ev_cmd == self.command
&& modifiers.shift == self.shift
&& modifiers.alt == self.alt;
}
}
false
})
}
pub fn format_text(&self) -> String {
let mut s = String::new();
if self.command {
s.push_str("Ctrl+");
}
if self.alt {
s.push_str("Alt+");
}
if self.shift {
s.push_str("Shift+");
}
match self.key {
Key::Num0 => s.push('0'),
Key::Num1 => s.push('1'),
Key::Num2 => s.push('2'),
Key::Num3 => s.push('3'),
Key::Num4 => s.push('4'),
Key::Num5 => s.push('5'),
Key::Num6 => s.push('6'),
Key::Num7 => s.push('7'),
Key::Num8 => s.push('8'),
Key::Num9 => s.push('9'),
Key::Plus => s.push('+'),
Key::Minus => s.push('-'),
_ => s.push_str(&format!("{:?}", self.key)),
}
s
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum MacMenuItemKind {
Action {
id: String,
shortcut: Option<MacKeyboardShortcut>,
},
Submenu {
menu: NSMenu,
},
Separator,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NSMenuItem {
pub title: String,
pub kind: MacMenuItemKind,
pub enabled: bool,
pub checked: bool,
}
impl NSMenuItem {
pub fn action(title: impl Into<String>, id: impl Into<String>) -> Self {
Self {
title: title.into(),
kind: MacMenuItemKind::Action {
id: id.into(),
shortcut: None,
},
enabled: true,
checked: false,
}
}
pub fn action_with_shortcut(
title: impl Into<String>,
id: impl Into<String>,
shortcut: MacKeyboardShortcut,
) -> Self {
Self {
title: title.into(),
kind: MacMenuItemKind::Action {
id: id.into(),
shortcut: Some(shortcut),
},
enabled: true,
checked: false,
}
}
pub fn submenu(title: impl Into<String>, menu: NSMenu) -> Self {
Self {
title: title.into(),
kind: MacMenuItemKind::Submenu { menu },
enabled: true,
checked: false,
}
}
pub fn separator() -> Self {
Self {
title: String::new(),
kind: MacMenuItemKind::Separator,
enabled: false,
checked: false,
}
}
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}
pub fn checked(mut self, checked: bool) -> Self {
self.checked = checked;
self
}
}