Only refresh module which was the click receiver

main
Tassilo Horn 3 years ago
parent 913235a51c
commit 8afb54cec9
  1. 17
      Cargo.lock
  2. 41
      swayrbar/src/bar.rs
  3. 12
      swayrbar/src/module.rs
  4. 8
      swayrbar/src/module/battery.rs
  5. 4
      swayrbar/src/module/date.rs
  6. 8
      swayrbar/src/module/pactl.rs
  7. 8
      swayrbar/src/module/sysinfo.rs
  8. 8
      swayrbar/src/module/window.rs

17
Cargo.lock generated

@ -461,9 +461,9 @@ dependencies = [
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.5.1" version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" checksum = "fd249e82c21598a9a426a4e00dd7adc1d640b22445ec8545feef801d1a74c221"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"crossbeam-deque", "crossbeam-deque",
@ -473,14 +473,13 @@ dependencies = [
[[package]] [[package]]
name = "rayon-core" name = "rayon-core"
version = "1.9.1" version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" checksum = "9f51245e1e62e1f1629cbfec37b5793bbabcaeb90f30e94d2ba03564687353e4"
dependencies = [ dependencies = [
"crossbeam-channel", "crossbeam-channel",
"crossbeam-deque", "crossbeam-deque",
"crossbeam-utils", "crossbeam-utils",
"lazy_static",
"num_cpus", "num_cpus",
] ]
@ -663,9 +662,9 @@ dependencies = [
[[package]] [[package]]
name = "sysinfo" name = "sysinfo"
version = "0.23.9" version = "0.23.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3fb8adaa82317f1e8a040281807f411803c9111303cfe129b4abb4a14b2c223" checksum = "4eea2ed6847da2e0c7289f72cb4f285f0bd704694ca067d32be811b2a45ea858"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"core-foundation-sys 0.8.3", "core-foundation-sys 0.8.3",
@ -723,9 +722,9 @@ dependencies = [
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.5.8" version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [ dependencies = [
"serde", "serde",
] ]

@ -17,7 +17,7 @@
use crate::config; use crate::config;
use crate::module; use crate::module;
use crate::module::BarModuleFn; use crate::module::{BarModuleFn, NameAndInstance};
use env_logger::Env; use env_logger::Env;
use serde_json; use serde_json;
use std::io; use std::io;
@ -36,7 +36,8 @@ pub fn start() {
let refresh_interval = config.refresh_interval; let refresh_interval = config.refresh_interval;
let mods: Arc<Vec<Box<dyn BarModuleFn>>> = Arc::new(create_modules(config)); let mods: Arc<Vec<Box<dyn BarModuleFn>>> = Arc::new(create_modules(config));
let mods_for_input = mods.clone(); 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(); let trigger_for_input = trigger.clone();
thread::spawn(move || handle_input(mods_for_input, trigger_for_input)); thread::spawn(move || handle_input(mods_for_input, trigger_for_input));
generate_status(&mods, trigger, refresh_interval); generate_status(&mods, trigger, refresh_interval);
@ -63,7 +64,7 @@ fn create_modules(config: config::Config) -> Vec<Box<dyn BarModuleFn>> {
pub fn handle_input( pub fn handle_input(
mods: Arc<Vec<Box<dyn BarModuleFn>>>, mods: Arc<Vec<Box<dyn BarModuleFn>>>,
trigger: Arc<(Mutex<()>, Condvar)>, trigger: Arc<(Mutex<NameAndInstance>, Condvar)>,
) { ) {
let mut sb = String::new(); let mut sb = String::new();
io::stdin() io::stdin()
@ -96,8 +97,11 @@ pub fn handle_input(
} }
}; };
log::debug!("Received click: {:?}", click); log::debug!("Received click: {:?}", click);
if handle_click(click, mods.clone()).is_some() { if let Some((name, instance)) = handle_click(click, mods.clone()) {
let (_, cvar) = &*trigger; 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(); cvar.notify_one();
} }
} }
@ -106,7 +110,7 @@ pub fn handle_input(
fn handle_click( fn handle_click(
click: sbt::Click, click: sbt::Click,
mods: Arc<Vec<Box<dyn BarModuleFn>>>, mods: Arc<Vec<Box<dyn BarModuleFn>>>,
) -> Option<()> { ) -> Option<NameAndInstance> {
let name = click.name?; let name = click.name?;
let instance = click.instance?; let instance = click.instance?;
let button_str = format!("{:?}", click.button); 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 // Wait a bit so that the action of the click has shown its
// effect, e.g., the window has been switched. // effect, e.g., the window has been switched.
thread::sleep(Duration::from_millis(50)); thread::sleep(Duration::from_millis(25));
return Some(()); 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( pub fn generate_status(
mods: &[Box<dyn BarModuleFn>], mods: &[Box<dyn BarModuleFn>],
trigger: Arc<(Mutex<()>, Condvar)>, trigger: Arc<(Mutex<NameAndInstance>, Condvar)>,
refresh_interval: u64, refresh_interval: u64,
) { ) {
println!("{{\"version\": 1, \"click_events\": true}}"); println!("{{\"version\": 1, \"click_events\": true}}");
@ -157,22 +162,30 @@ pub fn generate_status(
// opening [ and never the closing bracket. // opening [ and never the closing bracket.
println!("["); println!("[");
let mut name_and_instance: Option<NameAndInstance> = None;
loop { loop {
let mut blocks = vec![]; let mut blocks = vec![];
for m in mods { for m in mods {
blocks.push(m.build()); blocks.push(m.build(&name_and_instance));
} }
let json = serde_json::to_string_pretty(&blocks) let json = serde_json::to_string_pretty(&blocks)
.unwrap_or_else(|_| "".to_string()); .unwrap_or_else(|_| "".to_string());
println!("{},", json); println!("{},", json);
let (lock, cvar) = &*trigger; let (lock, cvar) = &*trigger;
let triggered = lock.lock().unwrap();
let result = cvar let result = cvar
.wait_timeout(triggered, Duration::from_millis(refresh_interval)) .wait_timeout(
lock.lock().unwrap(),
Duration::from_millis(refresh_interval),
)
.unwrap(); .unwrap();
if !result.1.timed_out() { if result.1.timed_out() {
log::debug!("Status writing thread waked up early by click event."); 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);
} }
} }
} }

@ -24,6 +24,16 @@ pub mod pactl;
pub mod sysinfo; pub mod sysinfo;
pub mod window; pub mod window;
pub type NameAndInstance = (String, String);
fn should_refresh(m: &dyn BarModuleFn, nai: &Option<NameAndInstance>) -> bool {
let cfg = m.get_config();
match nai {
None => true,
Some((n, i)) => n == &cfg.name && i == &cfg.instance,
}
}
pub trait BarModuleFn: Sync + Send { pub trait BarModuleFn: Sync + Send {
fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn> fn create(config: config::ModuleConfig) -> Box<dyn BarModuleFn>
where where
@ -44,6 +54,6 @@ pub trait BarModuleFn: Sync + Send {
None None
} }
} }
fn build(&self) -> s::Block; fn build(&self, nai: &Option<NameAndInstance>) -> s::Block;
fn subst_args<'a>(&'a self, _cmd: &'a [String]) -> Option<Vec<String>>; fn subst_args<'a>(&'a self, _cmd: &'a [String]) -> Option<Vec<String>>;
} }

@ -16,7 +16,7 @@
//! The date `swayrbar` module. //! The date `swayrbar` module.
use crate::config; use crate::config;
use crate::module::BarModuleFn; use crate::module::{should_refresh, BarModuleFn, NameAndInstance};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use battery as bat; use battery as bat;
use std::collections::HashSet; use std::collections::HashSet;
@ -128,9 +128,13 @@ impl BarModuleFn for BarModuleBattery {
&self.config &self.config
} }
fn build(&self) -> s::Block { fn build(&self, nai: &Option<NameAndInstance>) -> s::Block {
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");
if should_refresh(self, nai) {
refresh_state(&mut state); refresh_state(&mut state);
}
let text = let text =
get_text(&self.config.format, self.config.is_html_escape(), &state); get_text(&self.config.format, self.config.is_html_escape(), &state);
s::Block { s::Block {

@ -16,7 +16,7 @@
//! The date `swayrbar` module. //! The date `swayrbar` module.
use crate::module::config; use crate::module::config;
use crate::module::BarModuleFn; use crate::module::{BarModuleFn, NameAndInstance};
use swaybar_types as s; use swaybar_types as s;
const NAME: &str = "date"; const NAME: &str = "date";
@ -61,7 +61,7 @@ impl BarModuleFn for BarModuleDate {
} }
} }
fn build(&self) -> s::Block { fn build(&self, _nai: &Option<NameAndInstance>) -> s::Block {
let text = chrono_format(&self.config.format); let text = chrono_format(&self.config.format);
s::Block { s::Block {
name: Some(NAME.to_owned()), name: Some(NAME.to_owned()),

@ -16,7 +16,7 @@
//! The pactl `swayrbar` module. //! The pactl `swayrbar` module.
use crate::config; use crate::config;
use crate::module::BarModuleFn; use crate::module::{should_refresh, BarModuleFn, NameAndInstance};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
@ -142,9 +142,13 @@ impl BarModuleFn for BarModulePactl {
&self.config &self.config
} }
fn build(&self) -> s::Block { fn build(&self, nai: &Option<NameAndInstance>) -> s::Block {
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");
if should_refresh(self, nai) {
refresh_state(&mut state); refresh_state(&mut state);
}
let text = let text =
get_text(&self.config.format, self.config.is_html_escape(), &state); get_text(&self.config.format, self.config.is_html_escape(), &state);
s::Block { s::Block {

@ -16,7 +16,7 @@
//! The date `swayrbar` module. //! The date `swayrbar` module.
use crate::config; use crate::config;
use crate::module::BarModuleFn; use crate::module::{should_refresh, BarModuleFn, NameAndInstance};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
@ -145,10 +145,14 @@ impl BarModuleFn for BarModuleSysInfo {
&self.config &self.config
} }
fn build(&self) -> s::Block { fn build(&self, nai: &Option<NameAndInstance>) -> s::Block {
let mut sys = self.system.lock().expect("Could not lock state."); let mut sys = self.system.lock().expect("Could not lock state.");
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");
if should_refresh(self, nai) {
refresh_state(&mut sys, &mut state); refresh_state(&mut sys, &mut state);
}
s::Block { s::Block {
name: Some(NAME.to_owned()), name: Some(NAME.to_owned()),
instance: Some(self.config.instance.clone()), instance: Some(self.config.instance.clone()),

@ -19,7 +19,7 @@ use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use crate::config; use crate::config;
use crate::module::BarModuleFn; use crate::module::{should_refresh, BarModuleFn, NameAndInstance};
use crate::shared::fmt::subst_placeholders; use crate::shared::fmt::subst_placeholders;
use crate::shared::ipc; use crate::shared::ipc;
use crate::shared::ipc::NodeMethods; use crate::shared::ipc::NodeMethods;
@ -99,9 +99,13 @@ impl BarModuleFn for BarModuleWindow {
&self.config &self.config
} }
fn build(&self) -> s::Block { fn build(&self, nai: &Option<NameAndInstance>) -> s::Block {
let mut state = self.state.lock().expect("Could not lock state."); let mut state = self.state.lock().expect("Could not lock state.");
if should_refresh(self, nai) {
refresh_state(&mut state); refresh_state(&mut state);
}
let text = if state.pid == -1 { let text = if state.pid == -1 {
String::new() String::new()
} else { } else {

Loading…
Cancel
Save