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
use std::cell::Cell;
use std::char;
use std::rc::Rc;
use gdk::EventKey;
use gdk::enums::key::Escape;
use gtk::Inhibit;
use mg_settings::{
self,
EnumFromStr,
EnumMetaData,
SettingCompletion,
SpecialCommand,
};
use app::{Mg, Mode};
use app::ActivationType::Current;
use app::Msg::{self, EnterNormalModeAndReset};
impl<COMM, SETT> Mg<COMM, SETT>
where COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + mg_settings::settings::Settings + SettingCompletion + 'static,
{
#[allow(non_upper_case_globals)]
fn command_key_press(&mut self, key: &EventKey) -> Option<Msg<COMM, SETT>> {
match key.get_keyval() {
Escape => Some(EnterNormalModeAndReset),
_ => self.handle_shortcut(key),
}
}
fn command_key_release(&mut self, _key: &EventKey) -> Option<Msg<COMM, SETT>> {
if !self.is_normal_command() && COMM::is_incremental(self.model.current_command_mode) {
let command = self.model.status_bar_command.clone();
let msg = self.handle_special_command(Current, &command);
return msg;
}
None
}
#[allow(non_upper_case_globals)]
fn inhibit_command_key_press(current_mode: &Rc<Cell<Mode>>, key: &EventKey) -> Inhibit {
match key.get_keyval() {
Escape => Inhibit(false),
_ => Self::inhibit_handle_shortcut(current_mode, key),
}
}
#[allow(non_upper_case_globals)]
fn inhibit_input_key_press(current_mode: &Rc<Cell<Mode>>, key: &EventKey) -> Inhibit {
match key.get_keyval() {
Escape => Inhibit(false),
_ => Self::inhibit_handle_shortcut(current_mode, key),
}
}
pub fn inhibit_key_press(current_mode: &Rc<Cell<Mode>>, key: &EventKey) -> Inhibit {
match current_mode.get() {
Mode::Normal => Self::inhibit_normal_key_press(current_mode, key),
Mode::Command => Self::inhibit_command_key_press(current_mode, key),
Mode::BlockingInput | Mode::Input => Self::inhibit_input_key_press(current_mode, key),
_ => Self::inhibit_handle_shortcut(current_mode, key)
}
}
#[allow(non_upper_case_globals)]
fn input_key_press(&mut self, key: &EventKey) -> Option<Msg<COMM, SETT>> {
match key.get_keyval() {
Escape => {
if let Some(callback) = self.model.input_callback.take() {
callback(None, self.model.shortcut_pressed);
}
Some(EnterNormalModeAndReset)
},
keyval => {
if self.handle_input_shortcut(key) {
return None;
}
else if let Some(character) = char::from_u32(keyval) {
if self.model.choices.contains(&character) {
self.set_dialog_answer(&character.to_string());
return None;
}
}
self.handle_shortcut(key)
},
}
}
pub fn key_press(&mut self, key: &EventKey) {
let msg =
match self.model.current_mode.get() {
Mode::Normal => self.normal_key_press(key),
Mode::Command => self.command_key_press(key),
Mode::BlockingInput | Mode::Input => self.input_key_press(key),
_ => self.handle_shortcut(key)
};
if let Some(msg) = msg {
self.model.relm.stream().emit(msg);
}
}
pub fn key_release(&mut self, key: &EventKey) {
let msg =
match self.model.current_mode.get() {
Mode::Command => self.command_key_release(key),
_ => None,
};
if let Some(msg) = msg {
self.model.relm.stream().emit(msg);
}
}
}