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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::collections::HashMap;
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use gtk;
use mg_settings::{EnumFromStr, EnumMetaData, SettingCompletion, SpecialCommand};
use mg_settings::key::Key;
use mg_settings::settings;
use relm::{
ContainerComponent,
EventStream,
Relm,
Update,
Widget,
};
use app::{Mg, BLOCKING_INPUT_MODE, INPUT_MODE};
use app::color::color_blue;
use app::Msg::{
BlockingCustomDialog,
BlockingInput,
BlockingQuestion,
BlockingYesNoQuestion,
EnterNormalModeAndReset,
Input,
Question,
ResetInput,
YesNoQuestion,
};
use app::status_bar::Msg::{Identifier, ShowIdentifier};
use completion::NO_COMPLETER_IDENT;
use completion::completion_view::Msg::SetOriginalInput;
use self::DialogResult::{Answer, Shortcut};
pub trait Responder {
fn respond(&self, answer: DialogResult);
}
pub struct BlockingInputDialog {
tx: SyncSender<Option<String>>,
}
impl BlockingInputDialog {
pub fn new() -> (Self, Receiver<Option<String>>) {
let (tx, rx) = sync_channel(1);
(BlockingInputDialog {
tx,
}, rx)
}
}
impl Responder for BlockingInputDialog {
fn respond(&self, answer: DialogResult) {
match answer {
Answer(answer) => {
self.tx.send(answer).expect("Sending answer shouldn't fail")
},
_ => unimplemented!(),
}
gtk::main_quit();
}
}
pub struct InputDialog<WIDGET: Widget> {
callback: Box<Fn(Option<String>) -> WIDGET::Msg>,
stream: EventStream<WIDGET::Msg>,
}
impl<WIDGET: Widget> InputDialog<WIDGET> {
pub fn new<F>(relm: &Relm<WIDGET>, callback: F) -> Self
where F: Fn(Option<String>) -> WIDGET::Msg + 'static,
{
InputDialog {
callback: Box::new(callback),
stream: relm.stream().clone(),
}
}
}
impl<WIDGET: Widget> Responder for InputDialog<WIDGET> {
fn respond(&self, answer: DialogResult) {
match answer {
Answer(answer) => self.stream.emit((self.callback)(answer)),
_ => unimplemented!(),
}
}
}
pub struct YesNoInputDialog<WIDGET: Widget> {
callback: Box<Fn(bool) -> WIDGET::Msg>,
stream: EventStream<WIDGET::Msg>,
}
impl<WIDGET: Widget> YesNoInputDialog<WIDGET> {
pub fn new<F>(relm: &Relm<WIDGET>, callback: F) -> Self
where F: Fn(bool) -> WIDGET::Msg + 'static,
{
YesNoInputDialog {
callback: Box::new(callback),
stream: relm.stream().clone(),
}
}
}
impl<WIDGET: Widget> Responder for YesNoInputDialog<WIDGET> {
fn respond(&self, answer: DialogResult) {
match answer {
Answer(answer) => self.stream.emit((self.callback)(answer == Some("y".to_string()))),
_ => unimplemented!(),
}
}
}
pub struct DialogBuilder {
blocking: bool,
choices: Vec<char>,
completer: Option<String>,
default_answer: String,
message: String,
responder: Option<Box<Responder>>,
shortcuts: HashMap<Key, String>,
}
impl DialogBuilder {
#[allow(unknown_lints, new_without_default_derive)]
pub fn new() -> Self {
DialogBuilder {
blocking: false,
choices: vec![],
completer: None,
default_answer: String::new(),
message: String::new(),
responder: None,
shortcuts: HashMap::new(),
}
}
pub fn blocking(mut self, blocking: bool) -> Self {
self.blocking = blocking;
self
}
pub fn choices(mut self, choices: Vec<char>) -> Self {
self.choices = choices;
self
}
pub fn completer(mut self, completer: &str) -> Self {
self.completer = Some(completer.to_string());
self
}
pub fn default_answer(mut self, answer: String) -> Self {
self.default_answer = answer;
self
}
pub fn message(mut self, message: String) -> Self {
self.message = message;
self
}
pub fn responder(mut self, responder: Box<Responder>) -> Self {
self.responder = Some(responder);
self
}
pub fn shortcut(mut self, shortcut: Key, value: &str) -> Self {
self.shortcuts.insert(shortcut, value.to_string());
self
}
}
pub enum DialogResult {
Answer(Option<String>),
Shortcut(String),
}
impl<COMM, SETT> Mg<COMM, SETT>
where COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
{
pub fn blocking_custom_dialog(&mut self, responder: Box<Responder>, builder: DialogBuilder) {
let builder = builder
.blocking(true)
.responder(responder);
self.show_dialog_without_shortcuts(builder);
}
pub fn blocking_input(&mut self, responder: Box<Responder>, message: String, default_answer: String) {
let builder = DialogBuilder::new()
.blocking(true)
.default_answer(default_answer)
.message(message)
.responder(responder);
self.show_dialog_without_shortcuts(builder);
}
pub fn blocking_question(&mut self, responder: Box<Responder>, message: String, choices: Vec<char>) {
let builder = DialogBuilder::new()
.blocking(true)
.choices(choices)
.message(message)
.responder(responder);
self.show_dialog_without_shortcuts(builder);
}
pub fn blocking_yes_no_question(&mut self, responder: Box<Responder>, message: String) {
let builder = DialogBuilder::new()
.blocking(true)
.choices(vec!['y', 'n'])
.message(message)
.responder(responder);
self.show_dialog_without_shortcuts(builder);
}
pub fn input(&mut self, responder: Box<Responder>, message: String, default_answer: String) {
let builder = DialogBuilder::new()
.default_answer(default_answer)
.message(message)
.responder(responder);
self.show_dialog(builder);
}
pub fn question(&mut self, responder: Box<Responder>, message: String, choices: &[char]) {
let builder = DialogBuilder::new()
.responder(responder)
.message(message)
.choices(choices.to_vec());
self.show_dialog(builder);
}
pub fn set_dialog_answer(&mut self, answer: &str) {
let mut should_reset = false;
if let Some(callback) = self.model.input_callback.take() {
callback(Some(answer.to_string()), self.model.shortcut_pressed);
self.model.choices.clear();
should_reset = true;
}
if should_reset {
self.model.relm.stream().emit(EnterNormalModeAndReset);
}
}
pub fn show_dialog(&mut self, mut dialog_builder: DialogBuilder) {
self.model.shortcut_pressed = false;
self.model.shortcuts.clear();
for (key, value) in dialog_builder.shortcuts {
self.model.shortcuts.insert(key, value);
}
let choices = dialog_builder.choices.clone();
if !choices.is_empty() {
self.model.choices.clear();
self.model.choices.append(&mut dialog_builder.choices);
let choices: Vec<_> = choices.iter().map(|c| c.to_string()).collect();
let choices = choices.join("/");
self.status_bar.emit(Identifier(format!("{} ({}) ", dialog_builder.message, choices)));
self.status_bar.emit(ShowIdentifier);
}
else {
self.status_bar.emit(Identifier(format!("{} ", dialog_builder.message)));
self.show_entry();
self.set_input(&dialog_builder.default_answer);
}
if let Some(completer) = dialog_builder.completer {
self.set_completer(&completer);
self.completion_view.emit(SetOriginalInput(dialog_builder.default_answer));
self.show_completion();
}
else {
self.set_completer(NO_COMPLETER_IDENT);
}
self.model.answer = None;
if dialog_builder.blocking {
self.set_mode(BLOCKING_INPUT_MODE);
}
else {
self.set_mode(INPUT_MODE);
}
if let Some(responder) = dialog_builder.responder {
self.model.input_callback = Some(Box::new(move |answer, shortcut_pressed| {
let answer =
if shortcut_pressed {
if let Some(answer) = answer.clone() {
Shortcut(answer)
}
else {
Answer(None)
}
}
else {
Answer(answer.clone())
};
responder.respond(answer);
}));
}
color_blue(self.status_bar.widget());
}
pub fn show_dialog_without_shortcuts(&mut self, dialog_builder: DialogBuilder) {
self.show_dialog(dialog_builder);
}
pub fn yes_no_question(&mut self, responder: Box<Responder>, message: String) {
let builder = DialogBuilder::new()
.choices(vec!['y', 'n'])
.message(message)
.responder(responder);
self.show_dialog(builder);
}
}
pub fn blocking_dialog<COMM, SETT>(mg: &EventStream<<Mg<COMM, SETT> as Update>::Msg>, builder: DialogBuilder)
-> Option<String>
where COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
{
let (blocking_input_dialog, rx) = BlockingInputDialog::new();
let responder = Box::new(blocking_input_dialog);
mg.emit(BlockingCustomDialog(responder, builder));
gtk::main();
mg.emit(ResetInput);
rx.try_recv().unwrap_or(None)
}
pub fn blocking_input<COMM, SETT>(mg: &EventStream<<Mg<COMM, SETT> as Update>::Msg>, msg: String,
default_answer: String) -> Option<String>
where COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
{
let (blocking_input_dialog, rx) = BlockingInputDialog::new();
let responder = Box::new(blocking_input_dialog);
mg.emit(BlockingInput(responder, msg, default_answer));
gtk::main();
mg.emit(ResetInput);
rx.try_recv().unwrap_or(None)
}
pub fn blocking_question<COMM, SETT>(mg: &EventStream<<Mg<COMM, SETT> as Update>::Msg>, msg: String,
choices: &[char]) -> Option<String>
where COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
{
let (blocking_input_dialog, rx) = BlockingInputDialog::new();
let responder = Box::new(blocking_input_dialog);
mg.emit(BlockingQuestion(responder, msg, choices.to_vec()));
gtk::main();
mg.emit(ResetInput);
rx.try_recv().unwrap_or(None)
}
pub fn blocking_yes_no_question<COMM, SETT>(mg: &EventStream<<Mg<COMM, SETT> as Update>::Msg>, msg: String)
-> bool
where COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
{
let (blocking_input_dialog, rx) = BlockingInputDialog::new();
let responder = Box::new(blocking_input_dialog);
mg.emit(BlockingYesNoQuestion(responder, msg));
gtk::main();
mg.emit(ResetInput);
rx.try_recv() == Ok(Some("y".to_string()))
}
pub fn input<CALLBACK, COMM, SETT, WIDGET>(mg: &ContainerComponent<Mg<COMM, SETT>>, relm: &Relm<WIDGET>, msg: String,
default_answer: String, callback: CALLBACK)
where CALLBACK: Fn(Option<String>) -> WIDGET::Msg + 'static,
COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
WIDGET: Widget + 'static,
{
let responder = Box::new(InputDialog::new(relm, callback));
mg.emit(Input(responder, msg, default_answer));
}
pub fn question<CALLBACK, COMM, SETT, WIDGET>(mg: &ContainerComponent<Mg<COMM, SETT>>, relm: &Relm<WIDGET>, msg: String,
choices: &'static [char], callback: CALLBACK)
where CALLBACK: Fn(Option<String>) -> WIDGET::Msg + 'static,
COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
WIDGET: Widget + 'static,
{
let responder = Box::new(InputDialog::new(relm, callback));
mg.emit(Question(responder, msg, choices));
}
pub fn yes_no_question<CALLBACK, COMM, SETT, WIDGET>(mg: &ContainerComponent<Mg<COMM, SETT>>, relm: &Relm<WIDGET>,
msg: String, callback: CALLBACK)
where CALLBACK: Fn(bool) -> WIDGET::Msg + 'static,
COMM: Clone + EnumFromStr + EnumMetaData + SpecialCommand + 'static,
SETT: Default + EnumMetaData + settings::Settings + SettingCompletion + 'static,
WIDGET: Widget + 'static,
{
let responder = Box::new(YesNoInputDialog::new(relm, callback));
mg.emit(YesNoQuestion(responder, msg));
}