From 8afb54cec9326eb56b09847c7a6048bdadaebdba Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 15 Apr 2022 21:18:35 +0200 Subject: [PATCH] Only refresh module which was the click receiver --- Cargo.lock | 17 +++++++------- swayrbar/src/bar.rs | 41 ++++++++++++++++++++++------------ swayrbar/src/module.rs | 12 +++++++++- swayrbar/src/module/battery.rs | 10 ++++++--- swayrbar/src/module/date.rs | 4 ++-- swayrbar/src/module/pactl.rs | 10 ++++++--- swayrbar/src/module/sysinfo.rs | 10 ++++++--- swayrbar/src/module/window.rs | 10 ++++++--- 8 files changed, 76 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7364c2c..18a0dcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -461,9 +461,9 @@ dependencies = [ [[package]] name = "rayon" -version = "1.5.1" +version = "1.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" +checksum = "fd249e82c21598a9a426a4e00dd7adc1d640b22445ec8545feef801d1a74c221" dependencies = [ "autocfg", "crossbeam-deque", @@ -473,14 +473,13 @@ dependencies = [ [[package]] name = "rayon-core" -version = "1.9.1" +version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" +checksum = "9f51245e1e62e1f1629cbfec37b5793bbabcaeb90f30e94d2ba03564687353e4" dependencies = [ "crossbeam-channel", "crossbeam-deque", "crossbeam-utils", - "lazy_static", "num_cpus", ] @@ -663,9 +662,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.23.9" +version = "0.23.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3fb8adaa82317f1e8a040281807f411803c9111303cfe129b4abb4a14b2c223" +checksum = "4eea2ed6847da2e0c7289f72cb4f285f0bd704694ca067d32be811b2a45ea858" dependencies = [ "cfg-if", "core-foundation-sys 0.8.3", @@ -723,9 +722,9 @@ dependencies = [ [[package]] name = "toml" -version = "0.5.8" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" dependencies = [ "serde", ] diff --git a/swayrbar/src/bar.rs b/swayrbar/src/bar.rs index f8f6b0e..fccd077 100644 --- a/swayrbar/src/bar.rs +++ b/swayrbar/src/bar.rs @@ -17,7 +17,7 @@ use crate::config; use crate::module; -use crate::module::BarModuleFn; +use crate::module::{BarModuleFn, NameAndInstance}; use env_logger::Env; use serde_json; use std::io; @@ -36,7 +36,8 @@ pub fn start() { let refresh_interval = config.refresh_interval; let mods: Arc>> = Arc::new(create_modules(config)); let mods_for_input = mods.clone(); - let trigger = Arc::new((Mutex::new(()), Condvar::new())); + let trigger = + Arc::new((Mutex::new((String::new(), String::new())), Condvar::new())); let trigger_for_input = trigger.clone(); thread::spawn(move || handle_input(mods_for_input, trigger_for_input)); generate_status(&mods, trigger, refresh_interval); @@ -63,7 +64,7 @@ fn create_modules(config: config::Config) -> Vec> { pub fn handle_input( mods: Arc>>, - trigger: Arc<(Mutex<()>, Condvar)>, + trigger: Arc<(Mutex, Condvar)>, ) { let mut sb = String::new(); io::stdin() @@ -96,8 +97,11 @@ pub fn handle_input( } }; log::debug!("Received click: {:?}", click); - if handle_click(click, mods.clone()).is_some() { - let (_, cvar) = &*trigger; + if let Some((name, instance)) = handle_click(click, mods.clone()) { + let (mtx, cvar) = &*trigger; + let mut name_and_instance = mtx.lock().unwrap(); + name_and_instance.0 = name; + name_and_instance.1 = instance; cvar.notify_one(); } } @@ -106,7 +110,7 @@ pub fn handle_input( fn handle_click( click: sbt::Click, mods: Arc>>, -) -> Option<()> { +) -> Option { let name = click.name?; let instance = click.instance?; let button_str = format!("{:?}", click.button); @@ -119,8 +123,9 @@ fn handle_click( } // Wait a bit so that the action of the click has shown its // effect, e.g., the window has been switched. - thread::sleep(Duration::from_millis(50)); - return Some(()); + thread::sleep(Duration::from_millis(25)); + let cfg = m.get_config(); + return Some((cfg.name.clone(), cfg.instance.clone())); } } } @@ -149,7 +154,7 @@ fn execute_command(cmd: &[String]) { pub fn generate_status( mods: &[Box], - trigger: Arc<(Mutex<()>, Condvar)>, + trigger: Arc<(Mutex, Condvar)>, refresh_interval: u64, ) { println!("{{\"version\": 1, \"click_events\": true}}"); @@ -157,22 +162,30 @@ pub fn generate_status( // opening [ and never the closing bracket. println!("["); + let mut name_and_instance: Option = None; + loop { let mut blocks = vec![]; for m in mods { - blocks.push(m.build()); + blocks.push(m.build(&name_and_instance)); } let json = serde_json::to_string_pretty(&blocks) .unwrap_or_else(|_| "".to_string()); println!("{},", json); let (lock, cvar) = &*trigger; - let triggered = lock.lock().unwrap(); let result = cvar - .wait_timeout(triggered, Duration::from_millis(refresh_interval)) + .wait_timeout( + lock.lock().unwrap(), + Duration::from_millis(refresh_interval), + ) .unwrap(); - if !result.1.timed_out() { - log::debug!("Status writing thread waked up early by click event."); + if result.1.timed_out() { + name_and_instance = None; + } else { + name_and_instance = Some((*result.0).clone()); + log::debug!("Status writing thread waked up early by click event for {}/{}.", + &result.0.0, & result.0.1); } } } diff --git a/swayrbar/src/module.rs b/swayrbar/src/module.rs index 26d6a01..4527504 100644 --- a/swayrbar/src/module.rs +++ b/swayrbar/src/module.rs @@ -24,6 +24,16 @@ pub mod pactl; pub mod sysinfo; pub mod window; +pub type NameAndInstance = (String, String); + +fn should_refresh(m: &dyn BarModuleFn, nai: &Option) -> bool { + let cfg = m.get_config(); + match nai { + None => true, + Some((n, i)) => n == &cfg.name && i == &cfg.instance, + } +} + pub trait BarModuleFn: Sync + Send { fn create(config: config::ModuleConfig) -> Box where @@ -44,6 +54,6 @@ pub trait BarModuleFn: Sync + Send { None } } - fn build(&self) -> s::Block; + fn build(&self, nai: &Option) -> s::Block; fn subst_args<'a>(&'a self, _cmd: &'a [String]) -> Option>; } diff --git a/swayrbar/src/module/battery.rs b/swayrbar/src/module/battery.rs index 9dc3b4e..e016dc1 100644 --- a/swayrbar/src/module/battery.rs +++ b/swayrbar/src/module/battery.rs @@ -16,7 +16,7 @@ //! The date `swayrbar` module. use crate::config; -use crate::module::BarModuleFn; +use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::shared::fmt::subst_placeholders; use battery as bat; use std::collections::HashSet; @@ -128,9 +128,13 @@ impl BarModuleFn for BarModuleBattery { &self.config } - fn build(&self) -> s::Block { + fn build(&self, nai: &Option) -> s::Block { let mut state = self.state.lock().expect("Could not lock state."); - refresh_state(&mut state); + + if should_refresh(self, nai) { + refresh_state(&mut state); + } + let text = get_text(&self.config.format, self.config.is_html_escape(), &state); s::Block { diff --git a/swayrbar/src/module/date.rs b/swayrbar/src/module/date.rs index ca6ed91..9a7e2a6 100644 --- a/swayrbar/src/module/date.rs +++ b/swayrbar/src/module/date.rs @@ -16,7 +16,7 @@ //! The date `swayrbar` module. use crate::module::config; -use crate::module::BarModuleFn; +use crate::module::{BarModuleFn, NameAndInstance}; use swaybar_types as s; const NAME: &str = "date"; @@ -61,7 +61,7 @@ impl BarModuleFn for BarModuleDate { } } - fn build(&self) -> s::Block { + fn build(&self, _nai: &Option) -> s::Block { let text = chrono_format(&self.config.format); s::Block { name: Some(NAME.to_owned()), diff --git a/swayrbar/src/module/pactl.rs b/swayrbar/src/module/pactl.rs index fb7294c..340929d 100644 --- a/swayrbar/src/module/pactl.rs +++ b/swayrbar/src/module/pactl.rs @@ -16,7 +16,7 @@ //! The pactl `swayrbar` module. use crate::config; -use crate::module::BarModuleFn; +use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::shared::fmt::subst_placeholders; use once_cell::sync::Lazy; use regex::Regex; @@ -142,9 +142,13 @@ impl BarModuleFn for BarModulePactl { &self.config } - fn build(&self) -> s::Block { + fn build(&self, nai: &Option) -> s::Block { let mut state = self.state.lock().expect("Could not lock state."); - refresh_state(&mut state); + + if should_refresh(self, nai) { + refresh_state(&mut state); + } + let text = get_text(&self.config.format, self.config.is_html_escape(), &state); s::Block { diff --git a/swayrbar/src/module/sysinfo.rs b/swayrbar/src/module/sysinfo.rs index 255be0c..cb5d6cc 100644 --- a/swayrbar/src/module/sysinfo.rs +++ b/swayrbar/src/module/sysinfo.rs @@ -16,7 +16,7 @@ //! The date `swayrbar` module. use crate::config; -use crate::module::BarModuleFn; +use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::shared::fmt::subst_placeholders; use std::collections::HashMap; use std::sync::Mutex; @@ -145,10 +145,14 @@ impl BarModuleFn for BarModuleSysInfo { &self.config } - fn build(&self) -> s::Block { + fn build(&self, nai: &Option) -> s::Block { let mut sys = self.system.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state."); - refresh_state(&mut sys, &mut state); + + if should_refresh(self, nai) { + refresh_state(&mut sys, &mut state); + } + s::Block { name: Some(NAME.to_owned()), instance: Some(self.config.instance.clone()), diff --git a/swayrbar/src/module/window.rs b/swayrbar/src/module/window.rs index c13d19f..c3a381b 100644 --- a/swayrbar/src/module/window.rs +++ b/swayrbar/src/module/window.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; use std::sync::Mutex; use crate::config; -use crate::module::BarModuleFn; +use crate::module::{should_refresh, BarModuleFn, NameAndInstance}; use crate::shared::fmt::subst_placeholders; use crate::shared::ipc; use crate::shared::ipc::NodeMethods; @@ -99,9 +99,13 @@ impl BarModuleFn for BarModuleWindow { &self.config } - fn build(&self) -> s::Block { + fn build(&self, nai: &Option) -> s::Block { let mut state = self.state.lock().expect("Could not lock state."); - refresh_state(&mut state); + + if should_refresh(self, nai) { + refresh_state(&mut state); + } + let text = if state.pid == -1 { String::new() } else {