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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::any::Any;
use std::sync::Arc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Selector(&'static str);
#[derive(Debug, Clone)]
pub struct Command {
pub selector: Selector,
object: Option<Arc<dyn Any>>,
}
pub mod sys {
use super::Selector;
pub const QUIT_APP: Selector = Selector::new("druid-builtin.quit-app");
pub const HIDE_APPLICATION: Selector = Selector::new("druid-builtin.menu-hide-application");
pub const HIDE_OTHERS: Selector = Selector::new("druid-builtin.menu-hide-others");
pub const NEW_WINDOW: Selector = Selector::new("druid-builtin.new-window");
pub const CLOSE_WINDOW: Selector = Selector::new("druid-builtin.close-window");
pub const SHOW_CONTEXT_MENU: Selector = Selector::new("druid-builtin.show-context-menu");
pub const SET_MENU: Selector = Selector::new("druid-builtin.set-menu");
pub const SHOW_PREFERENCES: Selector = Selector::new("druid-builtin.menu-show-preferences");
pub const SHOW_ABOUT: Selector = Selector::new("druid-builtin.menu-show-about");
pub const SHOW_ALL: Selector = Selector::new("druid-builtin.menu-show-all");
pub const NEW_FILE: Selector = Selector::new("druid-builtin.menu-file-new");
pub const OPEN_FILE: Selector = Selector::new("druid-builtin.menu-file-open");
pub const SAVE_FILE: Selector = Selector::new("druid-builtin.menu-file-save");
pub const SAVE_FILE_AS: Selector = Selector::new("druid-builtin.menu-file-save-as");
pub const PRINT_SETUP: Selector = Selector::new("druid-builtin.menu-file-print-setup");
pub const PRINT: Selector = Selector::new("druid-builtin.menu-file-print");
pub const PRINT_PREVIEW: Selector = Selector::new("druid-builtin.menu-file-print");
pub const CUT: Selector = Selector::new("druid-builtin.menu-cut");
pub const COPY: Selector = Selector::new("druid-builtin.menu-copy");
pub const PASTE: Selector = Selector::new("druid-builtin.menu-paste");
pub const UNDO: Selector = Selector::new("druid-builtin.menu-undo");
pub const REDO: Selector = Selector::new("druid-builtin.menu-redo");
}
impl Selector {
pub const NOOP: Selector = Selector::new("");
pub const fn new(s: &'static str) -> Selector {
Selector(s)
}
}
impl Command {
pub fn new(selector: Selector, arg: impl Any) -> Self {
Command {
selector,
object: Some(Arc::new(arg)),
}
}
pub fn get_object<T: Any>(&self) -> Option<&T> {
match self.object.as_ref() {
None => None,
Some(obj) => obj.downcast_ref::<T>(),
}
}
}
impl From<Selector> for Command {
fn from(selector: Selector) -> Command {
Command {
selector,
object: None,
}
}
}
impl std::fmt::Display for Selector {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Selector('{}')", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_object() {
let sel = Selector::new("my-selector");
let objs = vec![0, 1, 2];
let command = Command::new(sel, objs);
assert_eq!(command.get_object(), Some(&vec![0, 1, 2]));
}
}