1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::cx::*;
use std::any::TypeId;

impl Cx{
    pub fn command_quit()->CommandId{uid!()}
    pub fn command_undo()->CommandId{uid!()}
    pub fn command_redo()->CommandId{uid!()}
    pub fn command_cut()->CommandId{uid!()}
    pub fn command_copy()->CommandId{uid!()}
    pub fn command_paste()->CommandId{uid!()}
    pub fn command_zoom_in()->CommandId{uid!()}
    pub fn command_zoom_out()->CommandId{uid!()}
    pub fn command_minimize()->CommandId{uid!()}
    pub fn command_zoom()->CommandId{uid!()}
    pub fn command_select_all()->CommandId{uid!()}
    
    pub fn command_default_keymap(&mut self){
        Cx::command_quit().set_key(self, KeyCode::KeyQ);
        Cx::command_undo().set_key(self, KeyCode::KeyZ);
        Cx::command_redo().set_key_shift(self, KeyCode::KeyZ);
        Cx::command_cut().set_key(self, KeyCode::KeyX);
        Cx::command_copy().set_key(self, KeyCode::KeyC);
        Cx::command_paste().set_key(self, KeyCode::KeyV);
        Cx::command_select_all().set_key(self, KeyCode::KeyA);
        Cx::command_zoom_out().set_key(self, KeyCode::Minus);
        Cx::command_zoom_in().set_key(self, KeyCode::Equals);
        Cx::command_minimize().set_key(self, KeyCode::KeyM);
    }
}


// Command


#[derive(PartialEq, Copy, Clone, Hash, Eq, Debug)]
pub struct CommandId(pub TypeId);

impl CommandId{
    pub fn set_enabled(&self, cx:&mut Cx, enabled:bool)->Self{
        let mut s = if let Some(s) = cx.command_settings.get(self){*s}else{CxCommandSetting::default()};
        s.enabled = enabled;
        cx.command_settings.insert(*self, s);
        *self
    }

    pub fn set_key(&self, cx:&mut Cx, key_code:KeyCode)->Self{
        let mut s = if let Some(s) = cx.command_settings.get(self){*s}else{CxCommandSetting::default()};
        s.shift = false;
        s.key_code = key_code;
        cx.command_settings.insert(*self, s);
        *self
    }
    
    pub fn set_key_shift(&self, cx:&mut Cx, key_code:KeyCode)->Self{
        let mut s = if let Some(s) = cx.command_settings.get(self){*s}else{CxCommandSetting::default()};
        s.shift = true;
        s.key_code = key_code;
        cx.command_settings.insert(*self, s);
        *self
    }
}

impl Into<CommandId> for UniqueId {
    fn into(self) -> CommandId {CommandId(self.0)}
}


#[derive(PartialEq, Clone)]
pub enum Menu {
    Main {items:Vec<Menu>},
    Item {name: String, command:CommandId},
    Sub {name: String, items: Vec<Menu>},
    Line
}

impl Menu {
    pub fn main(items: Vec<Menu>)->Menu{
        Menu::Main{items:items}
    }
    
    pub fn sub(name: &str, items: Vec<Menu>) -> Menu {
        Menu::Sub {
            name: name.to_string(),
            items: items
        }
    }
    
    pub fn line() -> Menu {
        Menu::Line
    }
    
    pub fn item(name: &str, command: CommandId) -> Menu {
        Menu::Item {
            name: name.to_string(),
            command: command
        }
    }
}