From e15386ad8f6aa7da84e23527302014b262f698c6 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sun, 10 Apr 2022 10:06:51 +0200 Subject: [PATCH] Improve window module --- swayrbar/src/module/window.rs | 44 +++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/swayrbar/src/module/window.rs b/swayrbar/src/module/window.rs index 9f8fb54..4b2d81e 100644 --- a/swayrbar/src/module/window.rs +++ b/swayrbar/src/module/window.rs @@ -38,6 +38,21 @@ pub struct BarModuleWindow { state: Mutex, } +fn refresh_state(state: &mut State) { + let root = ipc::get_root_node(false); + let focused_win = root + .iter() + .find(|n| n.focused && n.get_type() == ipc::Type::Window); + match focused_win { + Some(win) => { + state.name = win.get_name().to_owned(); + state.app_name = win.get_app_name().to_owned(); + state.pid = win.pid.unwrap_or(-1); + } + None => state.pid = -1, + }; +} + fn subst_placeholders(s: &str, html_escape: bool, state: &State) -> String { format_placeholders!(s, html_escape, { "title" | "name" => state.name.clone(), @@ -85,25 +100,18 @@ impl BarModuleFn for BarModuleWindow { } fn build(&self) -> s::Block { - let root = ipc::get_root_node(false); - let focused_win = root - .iter() - .find(|n| n.focused && n.get_type() == ipc::Type::Window); - let text = match focused_win { - Some(win) => { - 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(), - &*state, - ) - } - None => String::new(), + let mut state = self.state.lock().expect("Could not lock state."); + refresh_state(&mut state); + let text = if state.pid == -1 { + String::new() + } else { + subst_placeholders( + &self.config.format, + self.config.is_html_escape(), + &state, + ) }; + s::Block { name: Some(NAME.to_owned()), instance: Some(self.config.instance.clone()),