From e3284191251dba508f1a0d4f2f58e5cd49b2cb14 Mon Sep 17 00:00:00 2001 From: Brady Fomegne Date: Fri, 6 Sep 2024 10:25:33 +0100 Subject: [PATCH] chore: Migration from `rstk` to `afrish` (#80) --- Cargo.lock | 16 ++++------ Cargo.toml | 2 +- src/lib.rs | 24 +++++++-------- src/window/mod.rs | 1 - src/window/rstk_ext.rs | 67 ------------------------------------------ src/window/toolkit.rs | 35 +++++++++++----------- src/window/tooltip.rs | 15 +++++----- 7 files changed, 42 insertions(+), 118 deletions(-) delete mode 100644 src/window/rstk_ext.rs diff --git a/Cargo.lock b/Cargo.lock index feddc90..d3f3c0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,13 +57,18 @@ name = "afrim-wish" version = "0.4.0" dependencies = [ "afrim", + "afrish", "anyhow", "clap", - "rstk", "serde", "toml", ] +[[package]] +name = "afrish" +version = "0.1.0" +source = "git+https://github.com/fodydev/afrish?rev=e8c4fa2#e8c4fa2b2a0de717962d49d26fa0800000ff6c5d" + [[package]] name = "ahash" version = "0.8.11" @@ -561,15 +566,6 @@ dependencies = [ "syn", ] -[[package]] -name = "rstk" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0add4be2e35c9cfa903a4683e2a00f69004f90a76597a260e3266f36dc2d2807" -dependencies = [ - "once_cell", -] - [[package]] name = "serde" version = "1.0.199" diff --git a/Cargo.toml b/Cargo.toml index f6632ec..c3c6a05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,6 @@ rhai = ["afrim/rhai"] afrim = { version = "0.6.0", default-features = false, git = "https://github.com/pythonbrad/afrim", rev = "5f40469" } anyhow = "1.0.82" clap = "4.5.4" -rstk = "0.3.0" +afrish = { version = "0.1.0", default-features = false, git = "https://github.com/fodydev/afrish", rev = "e8c4fa2" } serde = { version = "1.0.197", features = ["serde_derive"] } toml = "0.8.12" diff --git a/src/lib.rs b/src/lib.rs index 51fa23f..a3ba33a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,18 +3,18 @@ mod window; use afrim::frontend::{Command, Frontend}; use anyhow::{anyhow, Result}; -use rstk::*; +use afrish::*; use std::sync::{ mpsc::{Receiver, Sender}, OnceLock, }; use std::thread; -use window::{rstk_ext::init_rstk_ext, toolkit::ToolKit, tooltip::ToolTip}; +use window::{toolkit::ToolKit, tooltip::ToolTip}; pub use config::Config; pub struct Wish { - window: &'static rstk::TkTopLevel, + window: &'static afrish::TkTopLevel, tooltip: ToolTip, toolkit: ToolKit, tx: Option>, @@ -22,13 +22,13 @@ pub struct Wish { } impl Wish { - fn init() -> &'static rstk::TkTopLevel { - static WISH: OnceLock = OnceLock::new(); + fn init() -> &'static afrish::TkTopLevel { + static WISH: OnceLock = OnceLock::new(); WISH.get_or_init(|| { let wish = if cfg!(debug_assertions) { - rstk::trace_with("wish").unwrap() + afrish::trace_with("wish").unwrap() } else { - rstk::start_wish().unwrap() + afrish::start_wish().unwrap() }; // The default behavior is to close the window. @@ -39,8 +39,6 @@ impl Wish { // Note that, this close button is on the title bar. wish.on_close(Self::kill); - init_rstk_ext(); - wish }) } @@ -60,7 +58,7 @@ impl Wish { } pub fn raise_error(message: &str, detail: T) { - rstk::message_box() + afrish::message_box() .parent(Self::init()) .icon(IconImage::Error) .title("Unexpected Error") @@ -71,7 +69,7 @@ impl Wish { } fn build(&mut self) { - self.tooltip.build(rstk::make_toplevel(self.window)); + self.tooltip.build(afrish::make_toplevel(self.window)); self.toolkit.build(self.window.to_owned()); } @@ -79,7 +77,7 @@ impl Wish { /// /// Note that a `process::exit` is called internally. pub fn kill() { - rstk::end_wish(); + afrish::end_wish(); } } @@ -97,7 +95,7 @@ impl Frontend for Wish { } // We shouldn't forget to listen for GUI events. - thread::spawn(rstk::mainloop); + thread::spawn(afrish::mainloop); let tx = self.tx.as_ref().unwrap(); diff --git a/src/window/mod.rs b/src/window/mod.rs index c34af22..e0e20fa 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -1,4 +1,3 @@ -pub mod rstk_ext; pub mod toolkit; pub mod tooltip; diff --git a/src/window/rstk_ext.rs b/src/window/rstk_ext.rs deleted file mode 100644 index a044fea..0000000 --- a/src/window/rstk_ext.rs +++ /dev/null @@ -1,67 +0,0 @@ -use rstk::*; - -pub fn init_rstk_ext() { - rstk::tell_wish("chan configure stdin -encoding utf-8"); -} - -#[derive(Clone, Debug, Default)] -pub struct Style { - pub name: &'static str, - pub background: String, - pub foreground: String, - pub font_size: u64, - pub font_family: String, - pub font_weight: String, -} - -impl Style { - pub fn update(&self) { - rstk::tell_wish(&format!( - "ttk::style layout {{{}}} [ttk::style layout {{{}}}];", - self.name, self.name - )); - rstk::tell_wish(&format!( - "ttk::style config {} -background {{{}}} -foreground {{{}}} -font {{{} {} {}}};", - self.name, - self.background, - self.foreground, - self.font_family, - self.font_size, - self.font_weight - )); - } -} - -pub trait TkWidgetExt { - fn style(&self, _style: &Style); -} - -impl TkWidgetExt for T { - fn style(&self, style: &Style) { - rstk::tell_wish(&format!( - "{} configure -style {{{}}}", - self.id(), - style.name - )); - } -} - -pub trait TkTopLevelExt { - fn border(&self, _value: bool); - fn topmost(&self, _value: bool); - fn position(&self, _x: u64, _y: u64); -} - -impl TkTopLevelExt for TkTopLevel { - fn border(&self, value: bool) { - rstk::tell_wish(&format!("wm overrideredirect {} {};", &self.id, !value)); - } - - fn topmost(&self, value: bool) { - rstk::tell_wish(&format!("wm attributes {} -topmost {};", &self.id, value)); - } - - fn position(&self, x: u64, y: u64) { - rstk::tell_wish(&format!("wm geometry {} +{}+{}", &self.id, x, y)); - } -} diff --git a/src/window/toolkit.rs b/src/window/toolkit.rs index 653aec1..bda88a9 100755 --- a/src/window/toolkit.rs +++ b/src/window/toolkit.rs @@ -1,6 +1,5 @@ use super::config::Config; -use super::rstk_ext::*; -use rstk::*; +use afrish::*; use std::collections::HashMap; use std::sync::{Arc, Mutex}; @@ -10,8 +9,8 @@ const GUI_RATIO: f64 = 0.8; #[derive(Clone, Default)] pub struct ToolKit { themes: HashMap<&'static str, Style>, - window: Option, - idle_state_widget: Option, + window: Option, + idle_state_widget: Option, new_idle_state: Arc>, curr_idle_state: bool, config: Config, @@ -108,15 +107,15 @@ impl ToolKit { ); // Header - let frame = rstk::make_frame(window); + let frame = afrish::make_frame(window); frame.style(&self.themes["TFrame"]); // Header label - let label = rstk::make_label(&frame); + 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 = rstk::make_button(&frame); + let button = afrish::make_button(&frame); button.text("State"); { let idle_state = Arc::clone(&self.new_idle_state); @@ -141,12 +140,12 @@ impl ToolKit { .layout(); // Separator - rstk::make_frame(window) + afrish::make_frame(window) .pack() .fill(PackFill::X) .padx((30.0 * GUI_RATIO) as u64) .layout(); - let frame = rstk::make_frame(window); + let frame = afrish::make_frame(window); frame.style(&self.themes["TFrame"]); frame .pack() @@ -155,23 +154,23 @@ impl ToolKit { .layout(); // Body - let notebook = rstk::make_notebook(window); + 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 = rstk::make_frame(window); + let frame = afrish::make_frame(window); frame.style(&self.themes["TFrame"]); $( - let subframe = rstk::make_frame(&frame); + let subframe = afrish::make_frame(&frame); subframe.style(&self.themes["TFrame"]); - let label = rstk::make_label(&subframe); + let label = afrish::make_label(&subframe); label.text($fieldname); label.style(&self.themes["TLabel"]); label.pack().side(PackSide::Left).layout(); - let button = rstk::make_button(&subframe); + let button = afrish::make_button(&subframe); button.text($fieldvalue); button.width((25.0 * GUI_RATIO) as i64); button.style(&self.themes["TButton"]); @@ -204,7 +203,7 @@ impl ToolKit { let config_version = info.version.clone(); move || { - rstk::message_box() + afrish::message_box() .parent(&window) .icon(IconImage::Information) .title("Configuration file") @@ -230,7 +229,7 @@ impl ToolKit { let window = window.clone(); move || { - rstk::message_box() + afrish::message_box() .parent(&window) .icon(IconImage::Information) .title("Keyboard shortcuts") @@ -250,7 +249,7 @@ impl ToolKit { let window = window.clone(); move || { - rstk::message_box() + afrish::message_box() .parent(&window) .icon(IconImage::Information) .title("About") @@ -283,7 +282,7 @@ impl ToolKit { .layout(); } - pub fn build(&mut self, window: rstk::TkTopLevel) { + pub fn build(&mut self, window: afrish::TkTopLevel) { self.window = Some(window); self.build_theme(); self.build_window(); diff --git a/src/window/tooltip.rs b/src/window/tooltip.rs index 140fc80..59e07ae 100644 --- a/src/window/tooltip.rs +++ b/src/window/tooltip.rs @@ -1,15 +1,14 @@ use super::config::Theme; -use super::rstk_ext::*; use afrim::frontend::Predicate; -use rstk::*; +use afrish::*; use std::collections::HashMap; #[derive(Clone, Default)] pub struct ToolTip { themes: HashMap<&'static str, Style>, - window: Option, - cursor_widget: Option, - predicates_widget: Option, + window: Option, + cursor_widget: Option, + predicates_widget: Option, predicates: Vec, current_predicate_id: usize, page_size: usize, @@ -61,20 +60,20 @@ impl ToolTip { window.deiconify(); // Cursor - let cursor_widget = rstk::make_label(window); + let cursor_widget = afrish::make_label(window); cursor_widget.text("Afrim is ready for input!"); cursor_widget.style(&self.themes["PHLabel"]); cursor_widget.pack().fill(PackFill::X).layout(); self.cursor_widget = Some(cursor_widget); // Predication - let predicates_widget = rstk::make_label(window); + let predicates_widget = afrish::make_label(window); predicates_widget.style(&self.themes["PBLabel"]); predicates_widget.pack().fill(PackFill::X).layout(); self.predicates_widget = Some(predicates_widget); } - pub fn build(&mut self, window: rstk::TkTopLevel) { + pub fn build(&mut self, window: afrish::TkTopLevel) { self.window = Some(window); self.build_theme(); self.build_window();