1use serde::Serialize;
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
20pub struct AppKey {
21 pub action: &'static str,
23 pub key: &'static str,
25 pub label: &'static str,
27}
28
29pub const APP_KEYS: &[AppKey] = &[
31 AppKey {
32 action: "palette",
33 key: "Mod+K",
34 label: "Command palette",
35 },
36 AppKey {
37 action: "keys",
38 key: "?",
39 label: "Keyboard shortcuts",
40 },
41 AppKey {
42 action: "commit",
43 key: "Mod+Enter",
44 label: "OK: put the step into the program",
45 },
46 AppKey {
47 action: "escape",
48 key: "Escape",
49 label: "Stop what is in progress, else leave the step: a sketch kept, any other cancelled",
50 },
51 AppKey {
52 action: "cancel",
53 key: "Mod+Backspace",
54 label: "Cancel the step, whatever is in progress",
55 },
56 AppKey {
57 action: "undo",
58 key: "Mod+Z",
59 label: "Undo",
60 },
61 AppKey {
62 action: "redo",
63 key: "Mod+Shift+Z",
64 label: "Redo",
65 },
66 AppKey {
67 action: "fit",
68 key: "F",
69 label: "Fit the part in the view",
70 },
71 AppKey {
72 action: "home",
73 key: "Home",
74 label: "Reset the view",
75 },
76 AppKey {
77 action: "projection",
78 key: "O",
79 label: "Switch perspective / orthographic",
80 },
81];
82
83pub fn key_of(action: &str) -> Option<&'static str> {
85 (APP_KEYS.iter())
86 .find(|k| k.action == action)
87 .map(|k| k.key)
88}
89
90pub fn is_app_key(key: &str) -> bool {
92 APP_KEYS.iter().any(|k| k.key == key)
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 #[test]
100 fn every_key_is_of_one_action() {
101 for (i, a) in APP_KEYS.iter().enumerate() {
102 for b in &APP_KEYS[i + 1..] {
103 assert_ne!(a.key, b.key, "{} and {}", a.action, b.action);
104 assert_ne!(a.action, b.action);
105 }
106 }
107 assert_eq!(key_of("commit"), Some("Mod+Enter"));
108 assert!(is_app_key("F") && !is_app_key("E"));
109 }
110}