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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::cell::Cell;
use std::rc::Rc;
use gdk::{EventKey, ModifierType, keyval_to_unicode};
use gdk::enums::key::{Escape, Tab, ISO_Left_Tab};
use gtk::{Inhibit, LabelExt};
use mg_settings::{self, EnumFromStr, EnumMetaData, SettingCompletion, SpecialCommand};
use mg_settings::key::Key::{self, Char};
use app::{
Mg,
Mode,
Msg,
BLOCKING_INPUT_MODE,
COMMAND_MODE,
INPUT_MODE,
};
use app::ShortcutCommand::{Complete, Incomplete};
use key_converter::gdk_key_to_key;
pub fn shortcut_to_string(keys: &[Key], show_count: bool) -> String {
if show_count {
let strings: Vec<_> = keys.iter().map(ToString::to_string).collect();
strings.join("")
}
else {
String::new()
}
}
impl<COMM, SETT> Mg<COMM, SETT>
where COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + mg_settings::settings::Settings + SettingCompletion + 'static,
{
pub fn add_to_shortcut(&mut self, key: Key) {
self.model.current_shortcut.push(key);
self.update_shortcut_label();
}
pub fn clear_shortcut(&mut self) {
self.model.current_shortcut.clear();
self.update_shortcut_label();
}
pub fn handle_input_shortcut(&mut self, key: &EventKey) -> bool {
if let Some(key) = gdk_key_to_key(key) {
if self.model.shortcuts.contains_key(&key) {
let answer = &self.model.shortcuts[&key].clone();
self.model.shortcut_pressed = true;
self.set_dialog_answer(answer);
return true;
}
}
false
}
pub fn inhibit_handle_shortcut(current_mode: &Rc<Cell<Mode>>, key: &EventKey) -> Inhibit {
let keyval = key.get_keyval();
let alt_pressed = key.get_state().contains(ModifierType::MOD1_MASK);
let control_pressed = key.get_state().contains(ModifierType::CONTROL_MASK);
let shift_pressed = key.get_state().contains(ModifierType::SHIFT_MASK);
let current_mode = current_mode.get();
let is_char = keyval_to_unicode(keyval).is_some();
let should_inhibit =
current_mode == Mode::Normal || keyval == Escape ||
((current_mode == Mode::Command || current_mode == Mode::Input || current_mode == Mode::BlockingInput) &&
(alt_pressed || control_pressed || (!is_char && shift_pressed) || keyval == Tab ||
keyval == ISO_Left_Tab));
Inhibit(should_inhibit)
}
pub fn handle_shortcut(&mut self, key: &EventKey) -> Option<Msg<COMM, SETT>> {
let keyval = key.get_keyval();
let alt_pressed = key.get_state().contains(ModifierType::MOD1_MASK);
let control_pressed = key.get_state().contains(ModifierType::CONTROL_MASK);
let shift_pressed = key.get_state().contains(ModifierType::SHIFT_MASK);
if !self.model.entry_shown || alt_pressed || control_pressed || shift_pressed || keyval == Tab ||
keyval == ISO_Left_Tab
{
if let Some(key) = gdk_key_to_key(key) {
self.add_to_shortcut(key);
let action = {
let mut current_mode = self.model.mode_string.clone();
if current_mode == INPUT_MODE || current_mode == BLOCKING_INPUT_MODE {
current_mode = COMMAND_MODE.to_string();
}
self.model.mappings.get(¤t_mode.as_ref())
.and_then(|mappings| mappings.get(self.shortcut_without_prefix()).cloned())
};
if let Some(action) = action {
let prefix = self.shortcut_prefix();
if !self.model.entry_shown {
self.reset();
}
self.clear_shortcut();
match self.action_to_command(&action) {
Complete(command) => {
return self.handle_command(Some(command), false, prefix);
},
Incomplete(command) => {
self.input_command(command);
self.show_completion();
},
}
}
else if self.no_possible_shortcut() {
let current_mode = self.model.current_mode.get();
if current_mode != Mode::Input && !self.model.entry_shown {
self.reset();
}
self.clear_shortcut();
}
}
}
None
}
fn no_possible_shortcut(&self) -> bool {
if let Some(mappings) = self.model.mappings.get(&self.model.mode_string.as_ref()) {
let shortcut = self.shortcut_without_prefix();
for key in mappings.keys() {
if key.starts_with(shortcut) {
return false;
}
}
}
true
}
fn shortcut_prefix(&self) -> Option<u32> {
let mut digits = self.model.current_shortcut.iter()
.take_while(is_digit)
.peekable();
if digits.peek().is_none() {
None
}
else {
let num = digits
.fold(0, |num, key| {
if let Char(c) = *key {
if let Some(digit) = c.to_digit(10) {
return num * 10 + digit;
}
}
num
});
Some(num)
}
}
fn shortcut_without_prefix(&self) -> &[Key] {
let first = self.model.current_shortcut.first().cloned().unwrap_or(Char('0'));
let start =
if first == Char('0') {
0
}
else {
self.model.current_shortcut.iter()
.position(is_not_digit)
.unwrap_or_else(|| self.model.current_shortcut.len())
};
&self.model.current_shortcut[start..]
}
fn update_shortcut_label(&self) {
self.shortcut.widget().set_text(&shortcut_to_string(&self.model.current_shortcut, self.model.show_count));
}
}
fn is_digit(key: &&Key) -> bool {
!is_not_digit(key)
}
fn is_not_digit(key: &Key) -> bool {
if let Char(c) = *key {
!c.to_digit(10).is_some()
}
else {
true
}
}