-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolkit.rs
executable file
·318 lines (285 loc) · 10.6 KB
/
toolkit.rs
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
use super::config::Config;
use afrish::*;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
// Ratio to easily adjust the dimension of the gui
const GUI_RATIO: f64 = 0.8;
#[derive(Clone, Default)]
pub struct ToolKit {
themes: HashMap<&'static str, Style>,
window: Option<afrish::TkTopLevel>,
idle_state_widget: Option<afrish::TkButton>,
new_idle_state: Arc<Mutex<bool>>,
curr_idle_state: bool,
config: Config,
}
impl ToolKit {
pub fn new(config: Config) -> Self {
Self {
config,
..Default::default()
}
}
fn build_theme(&mut self) {
let font_family = "Charis-SIL";
let style = Style {
name: "toolkit.TFrame",
background: "#1e1e1e".to_owned(),
..Default::default()
};
self.themes.insert("TFrame", style);
let style = Style {
name: "label.toolkit.TLabel",
background: "#1e1e1e".to_owned(),
foreground: "#ffffff".to_owned(),
font_size: (12.0 * GUI_RATIO) as u64,
font_family: font_family.to_string(),
..Default::default()
};
self.themes.insert("TLabel", style);
let style = Style {
name: "button.toolkit.TButton",
background: "#ffffff".to_owned(),
foreground: "#1e1e1e".to_owned(),
font_size: (12.0 * GUI_RATIO) as u64,
font_family: font_family.to_string(),
..Default::default()
};
self.themes.insert("TButton", style);
let style = Style {
name: "idle.toolkit.TButton",
background: "#e03131".to_owned(),
foreground: "#1e1e1e".to_owned(),
font_size: (12.0 * GUI_RATIO) as u64,
font_family: font_family.to_string(),
font_weight: "bold".to_owned(),
};
self.themes.insert("TIButton", style);
let style = Style {
name: "running.toolkit.TButton",
background: "#1971c2".to_owned(),
foreground: "#1e1e1e".to_owned(),
font_size: (12.0 * GUI_RATIO) as u64,
font_family: font_family.to_string(),
font_weight: "bold".to_owned(),
};
self.themes.insert("TRButton", style);
let style = Style {
name: "toolkit.TNotebook",
background: "#1e1e1e".to_owned(),
..Default::default()
};
self.themes.insert("TNotebook", style);
let style = Style {
name: "toolkit.TNotebook.Tab",
background: "#ffffff".to_owned(),
foreground: "#1e1e1e".to_owned(),
font_size: (12.0 * GUI_RATIO) as u64,
font_family: font_family.to_string(),
..Default::default()
};
self.themes.insert("notebook", style);
self.themes.iter().for_each(|(_, style)| style.update());
}
fn build_window(&mut self) {
let window = self.window.as_ref().unwrap();
window.title("Afrim Wish");
window.resizable(false, false);
window.background("#1e1e1e");
window.geometry(
(480.0 * GUI_RATIO) as u64,
(250.0 * GUI_RATIO) as u64,
-1,
-1,
);
// Header
let frame = afrish::make_frame(window);
frame.style(&self.themes["TFrame"]);
// Header label
let label = afrish::make_label(&frame);
label.text("AFRIM Toolkit");
label.style(&self.themes["TLabel"]);
label.pack().side(PackSide::Left).layout();
// Header idle state button
let button = afrish::make_button(&frame);
button.text("State");
{
let idle_state = Arc::clone(&self.new_idle_state);
button.command(move || {
let mut idle_state = idle_state.lock().unwrap();
*idle_state = !*idle_state;
});
}
button
.pack()
.side(PackSide::Right)
.padx((5.0 * GUI_RATIO) as u64)
.layout();
self.idle_state_widget = Some(button);
// We build the header
frame
.pack()
.fill(PackFill::X)
.padx((20.0 * GUI_RATIO) as u64)
.pady((15.0 * GUI_RATIO) as u64)
.layout();
// Separator
afrish::make_frame(window)
.pack()
.fill(PackFill::X)
.padx((30.0 * GUI_RATIO) as u64)
.layout();
let frame = afrish::make_frame(window);
frame.style(&self.themes["TFrame"]);
frame
.pack()
.fill(PackFill::X)
.pady((10.0 * GUI_RATIO) as u64)
.layout();
// Body
let notebook = afrish::make_notebook(window);
notebook.style(&self.themes["TNotebook"]);
// Page builder
macro_rules! make_page {
( $tabname: expr, $($fieldname: expr => $fieldvalue: expr => $see_more: stmt)*) => {
let frame = afrish::make_frame(window);
frame.style(&self.themes["TFrame"]);
$(
let subframe = afrish::make_frame(&frame);
subframe.style(&self.themes["TFrame"]);
let label = afrish::make_label(&subframe);
label.text($fieldname);
label.style(&self.themes["TLabel"]);
label.pack().side(PackSide::Left).layout();
let button = afrish::make_button(&subframe);
button.text($fieldvalue);
button.width((25.0 * GUI_RATIO) as i64);
button.style(&self.themes["TButton"]);
let cmd = {$see_more};
button.command(cmd);
button.pack().side(PackSide::Right).layout();
// We build the field
subframe
.pack()
.fill(PackFill::X)
.pady((2.0 * GUI_RATIO) as u64)
.layout();
)*
notebook.add(&frame, $tabname)
};
}
// Details page
let info = self.config.info.to_owned().unwrap_or_default();
let core = self.config.core.to_owned().unwrap_or_default();
make_page!(
"Details",
"IME:" => &info.name => {
let window = window.clone();
let config_name = info.name.to_owned();
let config_authors = info.authors.join(", ");
let config_description = info.description.clone();
let config_website = info.website.clone();
let config_version = info.version.clone();
move || {
afrish::message_box()
.parent(&window)
.icon(IconImage::Information)
.title("Configuration file")
.message(&config_name)
.detail(&format!(
"{}\n{}\n{}\n{}",
&config_version,
&config_description,
&config_website,
&config_authors,
))
.show();
}
}
"Auto Commit:" => &core.auto_commit.to_string() => || ()
"Buffer Size:" => &core.buffer_size.to_string() => || ()
);
// Help page
make_page!(
"Help",
"Keyboard shortcuts:" => "open" => {
let window = window.clone();
move || {
afrish::message_box()
.parent(&window)
.icon(IconImage::Information)
.title("Keyboard shortcuts")
.message("Keyboard shortcuts")
.detail("\
Command -> Shortcuts\n\n\
Pause / Resume -> CtrlLeft + CtrlRight\n\n\
Clear -> Escape / Space\n\n\
Select Next Predicate -> Ctrl + ShiftLeft\n\n\
Select Previous Predicate -> Ctrl + ShiftRight\n\n\
Commit Selected Predicate -> Ctrl + Space\
")
.show();
}
}
"About Afrim Wish:" => "open" => {
let window = window.clone();
move || {
afrish::message_box()
.parent(&window)
.icon(IconImage::Information)
.title("About")
.message(env!("CARGO_PKG_NAME"))
.detail(&format!(
"\
version: {}\n\n\
by {}\n\n\
{}\n\n\
{}\n\n\
This program comes with absolutely no warranty.\n\
See the {} license for more details.\
",
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_AUTHORS"),
env!("CARGO_PKG_DESCRIPTION"),
env!("CARGO_PKG_REPOSITORY"),
env!("CARGO_PKG_LICENSE")
))
.show();
}
}
);
// We build the notebook
notebook
.pack()
.fill(PackFill::X)
.padx((20.0 * GUI_RATIO) as u64)
.layout();
}
pub fn build(&mut self, window: afrish::TkTopLevel) {
self.window = Some(window);
self.build_theme();
self.build_window();
}
pub fn new_idle_state(&mut self) -> Option<bool> {
let curr_idle_state = self.curr_idle_state;
let new_idle_state = *self.new_idle_state.lock().unwrap();
let toggle = curr_idle_state != new_idle_state;
if toggle {
self.curr_idle_state = new_idle_state;
return Some(self.curr_idle_state);
}
None
}
pub fn set_idle_state(&mut self, state: bool) {
self.curr_idle_state = state;
*self.new_idle_state.lock().unwrap() = state;
let idle_state_widget = self.idle_state_widget.as_ref().unwrap();
if state {
idle_state_widget.text("IDLE");
idle_state_widget.style(&self.themes["TIButton"]);
} else {
idle_state_widget.text("Running");
idle_state_widget.style(&self.themes["TRButton"]);
}
}
}