Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: added idle state button #72

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ impl Frontend for Wish {
Command::Position(position) => self.tooltip.update_position(position),
Command::InputText(input) => self.tooltip.set_input_text(input),
Command::PageSize(size) => self.tooltip.set_page_size(size),
// TODO: implement the pause/resume.
Command::State(_state) => {}
Command::State(state) => self.toolkit.set_idle_state(state),
Command::Predicate(predicate) => self.tooltip.add_predicate(predicate),
Command::Update => self.tooltip.update(),
Command::Clear => self.tooltip.clear(),
Expand All @@ -122,9 +121,13 @@ impl Frontend for Wish {
tx.send(Command::NoPredicate)?;
}
}
// TODO: complete the implementation
// to send GUI commands such as pause/resume.
Command::NOP => tx.send(Command::NOP)?,
Command::NOP => {
if let Some(state) = self.toolkit.new_idle_state() {
tx.send(Command::State(state))?;
} else {
tx.send(Command::NOP)?;
}
}
Command::End => {
tx.send(Command::End)?;
self.window.destroy();
Expand Down Expand Up @@ -247,6 +250,10 @@ mod tests {
);
tx1.send(Command::Update).unwrap();

// Test the idle state.
tx1.send(Command::State(true)).unwrap();
tx1.send(Command::State(false)).unwrap();

// We end the communication.
tx1.send(Command::End).unwrap();
assert_eq!(rx2.recv().unwrap(), Command::End);
Expand Down
66 changes: 45 additions & 21 deletions src/window/toolkit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::config::Config;
use super::rstk_ext::*;
use rstk::*;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

// Ratio to easily adjust the dimension of the gui
const GUI_RATIO: f64 = 0.8;
Expand All @@ -10,6 +11,9 @@ const GUI_RATIO: f64 = 0.8;
pub struct ToolKit {
themes: HashMap<&'static str, Style>,
window: Option<rstk::TkTopLevel>,
idle_state_widget: Option<rstk::TkButton>,
new_idle_state: Arc<Mutex<bool>>,
curr_idle_state: bool,
config: Config,
}

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

let style = Style {
name: "exit.toolkit.TButton",
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("TEButton", style);
self.themes.insert("TIButton", style);

let style = Style {
name: "iconify.toolkit.TButton",
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("TIButton", style);
self.themes.insert("TRButton", style);

let style = Style {
name: "toolkit.TNotebook",
Expand Down Expand Up @@ -111,31 +115,23 @@ impl ToolKit {
label.text("AFRIM Toolkit");
label.style(&self.themes["TLabel"]);
label.pack().side(PackSide::Left).layout();
// Header iconify button
let button = rstk::make_button(&frame);
button.text("x");
button.width((4.0 * GUI_RATIO) as i64);
button.style(&self.themes["TEButton"]);
button.command(rstk::end_wish);
button
.pack()
.side(PackSide::Right)
.padx((5.0 * GUI_RATIO) as u64)
.layout();
// Header exit button
// Header idle state button
let button = rstk::make_button(&frame);
button.text("-");
button.text("State");
{
let window = window.clone();
button.command(move || window.iconify());
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.width((4.0 * GUI_RATIO) as i64);
button.style(&self.themes["TIButton"]);
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()
Expand Down Expand Up @@ -288,4 +284,32 @@ impl ToolKit {
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"]);
}
}
}
Loading