Skip to content

Commit 2281ca5

Browse files
authored
patch: added idle state button (#72)
1 parent eefd855 commit 2281ca5

File tree

2 files changed

+57
-26
lines changed

2 files changed

+57
-26
lines changed

src/lib.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ impl Frontend for Wish {
108108
Command::Position(position) => self.tooltip.update_position(position),
109109
Command::InputText(input) => self.tooltip.set_input_text(input),
110110
Command::PageSize(size) => self.tooltip.set_page_size(size),
111-
// TODO: implement the pause/resume.
112-
Command::State(_state) => {}
111+
Command::State(state) => self.toolkit.set_idle_state(state),
113112
Command::Predicate(predicate) => self.tooltip.add_predicate(predicate),
114113
Command::Update => self.tooltip.update(),
115114
Command::Clear => self.tooltip.clear(),
@@ -122,9 +121,13 @@ impl Frontend for Wish {
122121
tx.send(Command::NoPredicate)?;
123122
}
124123
}
125-
// TODO: complete the implementation
126-
// to send GUI commands such as pause/resume.
127-
Command::NOP => tx.send(Command::NOP)?,
124+
Command::NOP => {
125+
if let Some(state) = self.toolkit.new_idle_state() {
126+
tx.send(Command::State(state))?;
127+
} else {
128+
tx.send(Command::NOP)?;
129+
}
130+
}
128131
Command::End => {
129132
tx.send(Command::End)?;
130133
self.window.destroy();
@@ -247,6 +250,10 @@ mod tests {
247250
);
248251
tx1.send(Command::Update).unwrap();
249252

253+
// Test the idle state.
254+
tx1.send(Command::State(true)).unwrap();
255+
tx1.send(Command::State(false)).unwrap();
256+
250257
// We end the communication.
251258
tx1.send(Command::End).unwrap();
252259
assert_eq!(rx2.recv().unwrap(), Command::End);

src/window/toolkit.rs

+45-21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::config::Config;
22
use super::rstk_ext::*;
33
use rstk::*;
44
use std::collections::HashMap;
5+
use std::sync::{Arc, Mutex};
56

67
// Ratio to easily adjust the dimension of the gui
78
const GUI_RATIO: f64 = 0.8;
@@ -10,6 +11,9 @@ const GUI_RATIO: f64 = 0.8;
1011
pub struct ToolKit {
1112
themes: HashMap<&'static str, Style>,
1213
window: Option<rstk::TkTopLevel>,
14+
idle_state_widget: Option<rstk::TkButton>,
15+
new_idle_state: Arc<Mutex<bool>>,
16+
curr_idle_state: bool,
1317
config: Config,
1418
}
1519

@@ -52,24 +56,24 @@ impl ToolKit {
5256
self.themes.insert("TButton", style);
5357

5458
let style = Style {
55-
name: "exit.toolkit.TButton",
59+
name: "idle.toolkit.TButton",
5660
background: "#e03131".to_owned(),
5761
foreground: "#1e1e1e".to_owned(),
5862
font_size: (12.0 * GUI_RATIO) as u64,
5963
font_family: font_family.to_string(),
6064
font_weight: "bold".to_owned(),
6165
};
62-
self.themes.insert("TEButton", style);
66+
self.themes.insert("TIButton", style);
6367

6468
let style = Style {
65-
name: "iconify.toolkit.TButton",
69+
name: "running.toolkit.TButton",
6670
background: "#1971c2".to_owned(),
6771
foreground: "#1e1e1e".to_owned(),
6872
font_size: (12.0 * GUI_RATIO) as u64,
6973
font_family: font_family.to_string(),
7074
font_weight: "bold".to_owned(),
7175
};
72-
self.themes.insert("TIButton", style);
76+
self.themes.insert("TRButton", style);
7377

7478
let style = Style {
7579
name: "toolkit.TNotebook",
@@ -111,31 +115,23 @@ impl ToolKit {
111115
label.text("AFRIM Toolkit");
112116
label.style(&self.themes["TLabel"]);
113117
label.pack().side(PackSide::Left).layout();
114-
// Header iconify button
115-
let button = rstk::make_button(&frame);
116-
button.text("x");
117-
button.width((4.0 * GUI_RATIO) as i64);
118-
button.style(&self.themes["TEButton"]);
119-
button.command(rstk::end_wish);
120-
button
121-
.pack()
122-
.side(PackSide::Right)
123-
.padx((5.0 * GUI_RATIO) as u64)
124-
.layout();
125-
// Header exit button
118+
// Header idle state button
126119
let button = rstk::make_button(&frame);
127-
button.text("-");
120+
button.text("State");
128121
{
129-
let window = window.clone();
130-
button.command(move || window.iconify());
122+
let idle_state = Arc::clone(&self.new_idle_state);
123+
button.command(move || {
124+
let mut idle_state = idle_state.lock().unwrap();
125+
126+
*idle_state = !*idle_state;
127+
});
131128
}
132-
button.width((4.0 * GUI_RATIO) as i64);
133-
button.style(&self.themes["TIButton"]);
134129
button
135130
.pack()
136131
.side(PackSide::Right)
137132
.padx((5.0 * GUI_RATIO) as u64)
138133
.layout();
134+
self.idle_state_widget = Some(button);
139135
// We build the header
140136
frame
141137
.pack()
@@ -288,4 +284,32 @@ impl ToolKit {
288284
self.build_theme();
289285
self.build_window();
290286
}
287+
288+
pub fn new_idle_state(&mut self) -> Option<bool> {
289+
let curr_idle_state = self.curr_idle_state;
290+
let new_idle_state = *self.new_idle_state.lock().unwrap();
291+
let toggle = curr_idle_state != new_idle_state;
292+
293+
if toggle {
294+
self.curr_idle_state = new_idle_state;
295+
296+
return Some(self.curr_idle_state);
297+
}
298+
299+
None
300+
}
301+
302+
pub fn set_idle_state(&mut self, state: bool) {
303+
self.curr_idle_state = state;
304+
*self.new_idle_state.lock().unwrap() = state;
305+
let idle_state_widget = self.idle_state_widget.as_ref().unwrap();
306+
307+
if state {
308+
idle_state_widget.text("IDLE");
309+
idle_state_widget.style(&self.themes["TIButton"]);
310+
} else {
311+
idle_state_widget.text("Running");
312+
idle_state_widget.style(&self.themes["TRButton"]);
313+
}
314+
}
291315
}

0 commit comments

Comments
 (0)