From 591915e0ed86071b8a977401ac3345bb02584a98 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sat, 9 Apr 2022 17:41:43 +0200 Subject: [PATCH] Improve window module --- swayrbar/src/module/window.rs | 48 +++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/swayrbar/src/module/window.rs b/swayrbar/src/module/window.rs index 2da3c39..72264ae 100644 --- a/swayrbar/src/module/window.rs +++ b/swayrbar/src/module/window.rs @@ -27,16 +27,34 @@ use swaybar_types as s; const NAME: &str = "window"; +struct State { + name: String, + app_name: String, + pid: i32, +} + pub struct BarModuleWindow { config: config::ModuleConfig, - pid: Mutex, + state: Mutex, +} + +fn subst_placeholders(s: &str, state: &State, html_escape: bool) -> String { + format_placeholders!(s, html_escape, { + "title" | "name" => state.name.clone(), + "app_name" => state.app_name.clone(), + "pid" => state.pid, + }) } impl BarModuleFn for BarModuleWindow { fn create(config: config::ModuleConfig) -> Box { Box::new(BarModuleWindow { config, - pid: Mutex::new(0), + state: Mutex::new(State { + name: String::new(), + app_name: String::new(), + pid: -1, + }), }) } @@ -73,15 +91,16 @@ impl BarModuleFn for BarModuleWindow { .find(|n| n.focused && n.get_type() == ipc::Type::Window); let text = match focused_win { Some(win) => { - let mut pid = self.pid.lock().expect("Couldn't lock pid!"); - *pid = win.pid.unwrap_or(-1); - format_placeholders!( + let mut state = + self.state.lock().expect("Couldn't lock state!"); + state.name = win.get_name().to_owned(); + state.app_name = win.get_app_name().to_owned(); + state.pid = win.pid.unwrap_or(-1); + subst_placeholders( &self.config.format, - self.config.is_html_escape(), { - "title" | "name" => win.get_name(), - "app_name" => win.get_app_name(), - "pid" => *pid, - }) + &*state, + self.config.is_html_escape(), + ) } None => String::new(), }; @@ -119,14 +138,11 @@ impl BarModuleFn for BarModuleWindow { } } - fn subst_args<'a>(&'a self, cmd: &'a [String]) -> Option> { + fn subst_args<'b>(&'b self, cmd: &'b [String]) -> Option> { + let state = self.state.lock().expect("Could not lock state."); let cmd = cmd .iter() - .map(|arg| { - format_placeholders!(arg, false, { - "pid" => *self.pid.lock().expect("Could not lock pid."), - }) - }) + .map(|arg| subst_placeholders(arg, &*state, false)) .collect(); Some(cmd) }