makepad_render/
menu.rs

1use crate::cx::*;
2use std::any::TypeId;
3
4impl Cx{
5    pub fn command_quit()->CommandId{uid!()}
6    pub fn command_undo()->CommandId{uid!()}
7    pub fn command_redo()->CommandId{uid!()}
8    pub fn command_cut()->CommandId{uid!()}
9    pub fn command_copy()->CommandId{uid!()}
10    pub fn command_paste()->CommandId{uid!()}
11    pub fn command_zoom_in()->CommandId{uid!()}
12    pub fn command_zoom_out()->CommandId{uid!()}
13    pub fn command_minimize()->CommandId{uid!()}
14    pub fn command_zoom()->CommandId{uid!()}
15    pub fn command_select_all()->CommandId{uid!()}
16    
17    pub fn command_default_keymap(&mut self){
18        Cx::command_quit().set_key(self, KeyCode::KeyQ);
19        Cx::command_undo().set_key(self, KeyCode::KeyZ);
20        Cx::command_redo().set_key_shift(self, KeyCode::KeyZ);
21        Cx::command_cut().set_key(self, KeyCode::KeyX);
22        Cx::command_copy().set_key(self, KeyCode::KeyC);
23        Cx::command_paste().set_key(self, KeyCode::KeyV);
24        Cx::command_select_all().set_key(self, KeyCode::KeyA);
25        Cx::command_zoom_out().set_key(self, KeyCode::Minus);
26        Cx::command_zoom_in().set_key(self, KeyCode::Equals);
27        Cx::command_minimize().set_key(self, KeyCode::KeyM);
28    }
29}
30
31
32// Command
33
34
35#[derive(PartialEq, Copy, Clone, Hash, Eq, Debug)]
36pub struct CommandId(pub TypeId);
37
38impl CommandId{
39    pub fn set_enabled(&self, cx:&mut Cx, enabled:bool)->Self{
40        let mut s = if let Some(s) = cx.command_settings.get(self){*s}else{CxCommandSetting::default()};
41        s.enabled = enabled;
42        cx.command_settings.insert(*self, s);
43        *self
44    }
45
46    pub fn set_key(&self, cx:&mut Cx, key_code:KeyCode)->Self{
47        let mut s = if let Some(s) = cx.command_settings.get(self){*s}else{CxCommandSetting::default()};
48        s.shift = false;
49        s.key_code = key_code;
50        cx.command_settings.insert(*self, s);
51        *self
52    }
53    
54    pub fn set_key_shift(&self, cx:&mut Cx, key_code:KeyCode)->Self{
55        let mut s = if let Some(s) = cx.command_settings.get(self){*s}else{CxCommandSetting::default()};
56        s.shift = true;
57        s.key_code = key_code;
58        cx.command_settings.insert(*self, s);
59        *self
60    }
61}
62
63impl Into<CommandId> for UniqueId {
64    fn into(self) -> CommandId {CommandId(self.0)}
65}
66
67
68#[derive(PartialEq, Clone)]
69pub enum Menu {
70    Main {items:Vec<Menu>},
71    Item {name: String, command:CommandId},
72    Sub {name: String, items: Vec<Menu>},
73    Line
74}
75
76impl Menu {
77    pub fn main(items: Vec<Menu>)->Menu{
78        Menu::Main{items:items}
79    }
80    
81    pub fn sub(name: &str, items: Vec<Menu>) -> Menu {
82        Menu::Sub {
83            name: name.to_string(),
84            items: items
85        }
86    }
87    
88    pub fn line() -> Menu {
89        Menu::Line
90    }
91    
92    pub fn item(name: &str, command: CommandId) -> Menu {
93        Menu::Item {
94            name: name.to_string(),
95            command: command
96        }
97    }
98}