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
#![feature(type_ascription, vec_remove_item)]
#![allow(dead_code)]
extern crate orbclient;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
pub mod shortcut;
mod utils;
mod tests;
use utils::{load};
use std::fmt::Debug;
use std::cmp::{PartialEq, Eq};
use orbclient::{Window, EventOption};
use shortcut::{CommonId, Shortcut, ShortcutComparator};
impl ShortcutComparator {
pub fn new() -> ShortcutComparator {
ShortcutComparator {
pressed: Vec::new(),
supported: None,
min_shortcut_len: 2
}
}
pub fn with_custom(custom: Vec<Shortcut>, min_shortcut_len: u8) -> ShortcutComparator {
let mut sc = ShortcutComparator {
pressed: Vec::new(),
supported: Some(custom),
min_shortcut_len: min_shortcut_len,
};
sc.sort();
sc
}
pub fn from_json(json: &str) -> ShortcutComparator {
let mut sc = ShortcutComparator {
pressed: Vec::new(),
supported: None,
min_shortcut_len: 2,
};
sc
}
pub fn enable(&mut self, shortcuts: Vec<Shortcut>) {
match self.supported.as_mut() {
None => {},
Some(ssc) => {
for shortcut in shortcuts {
ssc.push(shortcut);
}
}
}
}
pub fn update(&mut self, key_event: orbclient::event::KeyEvent) -> Option<&CommonId> {
let mut m = None;
if key_event.pressed {
self.pressed.push(key_event.scancode);
self.pressed.sort();
self.pressed.dedup();
m = self.compare();
} else if !key_event.pressed {
self.pressed.remove_item(&key_event.scancode);
}
m
}
fn sort(&mut self) {
match self.supported.as_mut() {
None => panic!("Wasn't able to assign shortcuts."),
Some(scv) => {println!("{:?}", scv); }
};
}
fn compare(&mut self) -> Option<&CommonId> {
let mut pressed_match = None;
match self.supported.as_mut() {
None => {},
Some(shortcuts) => {
if self.pressed.len() >= self.min_shortcut_len as usize {
for ssc in shortcuts {
if ssc.keys.len() == self.pressed.len() {
if ssc.keys == self.pressed {
pressed_match = Some(&ssc.id);
}
}
}
}
}
}
pressed_match
}
}