diff --git a/Cargo.lock b/Cargo.lock index 256efb3..8d3e29b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -570,9 +570,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.80" +version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f972498cf015f7c0746cac89ebe1d6ef10c293b94175a243a2d9442c163d9944" +checksum = "9b7ce2b32a1aed03c558dc61a5cd328f15aff2dbc17daad8fb8af04d2100e15c" dependencies = [ "itoa", "ryu", diff --git a/swayrbar/src/bar.rs b/swayrbar/src/bar.rs index 22af6eb..16a2561 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, NameAndInstance}; +use crate::module::{BarModuleFn, NameInstanceAndReason, RefreshReason}; use env_logger::Env; use serde_json; use std::io; @@ -30,14 +30,6 @@ use std::{sync::Arc, thread}; use swaybar_types as sbt; use swayipc as si; -#[derive(Debug)] -enum RefreshReason { - ClickEvent, - SwayEvent, -} - -type NameInstanceAndReason = (String, String, RefreshReason); - pub fn start() { env_logger::Builder::from_env(Env::default().default_filter_or("warn")) .init(); @@ -56,7 +48,7 @@ pub fn start() { let sender_for_input = sender.clone(); thread::spawn(move || handle_input(mods_for_input, sender_for_input)); - let window_mods: Vec = mods + let window_mods: Vec<(String, String)> = mods .iter() .filter(|m| m.get_config().name == "window") .map(|m| (m.get_config().name.clone(), m.get_config().instance.clone())) @@ -134,8 +126,8 @@ fn handle_input( } }; log::debug!("Received click: {:?}", click); - if let Some((name, instance)) = handle_click(click, mods.clone()) { - let event = Some((name, instance, RefreshReason::ClickEvent)); + let event = handle_click(click, mods.clone()); + if event.is_some() { send_refresh_event(&sender, event); } } @@ -156,7 +148,7 @@ fn send_refresh_event( fn handle_click( click: sbt::Click, mods: Arc>>, -) -> Option { +) -> Option { let name = click.name?; let instance = click.instance?; let button_str = format!("{:?}", click.button); @@ -176,7 +168,11 @@ fn handle_click( if cfg.name == module::window::NAME { return None; } - return Some((cfg.name.clone(), cfg.instance.clone())); + return Some(( + cfg.name.clone(), + cfg.instance.clone(), + RefreshReason::ClickEvent, + )); } } } @@ -212,7 +208,7 @@ fn sway_subscribe() -> si::Fallible { } fn handle_sway_events( - window_mods: Vec, + window_mods: Vec<(String, String)>, sender: SyncSender>, ) { let mut resets = 0; @@ -275,7 +271,7 @@ fn handle_sway_events( fn generate_status_1( mods: &[Box], - name_and_instance: &Option, + name_and_instance: &Option, ) { let mut blocks = vec![]; for m in mods { @@ -296,6 +292,6 @@ fn generate_status( println!("["); for ev in receiver.iter() { - generate_status_1(mods, &ev.map(|x| (x.0, x.1))) + generate_status_1(mods, &ev) } } diff --git a/swayrbar/src/module.rs b/swayrbar/src/module.rs index 4527504..66936f3 100644 --- a/swayrbar/src/module.rs +++ b/swayrbar/src/module.rs @@ -24,13 +24,22 @@ pub mod pactl; pub mod sysinfo; pub mod window; -pub type NameAndInstance = (String, String); +#[derive(Debug)] +pub enum RefreshReason { + ClickEvent, + SwayEvent, +} + +pub type NameInstanceAndReason = (String, String, RefreshReason); -fn should_refresh(m: &dyn BarModuleFn, nai: &Option) -> bool { +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, + Some((n, i, _)) => n == &cfg.name && i == &cfg.instance, } } @@ -54,6 +63,6 @@ pub trait BarModuleFn: Sync + Send { None } } - fn build(&self, nai: &Option) -> 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 e016dc1..ab65ccb 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::{should_refresh, BarModuleFn, NameAndInstance}; +use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason}; use crate::shared::fmt::subst_placeholders; use battery as bat; use std::collections::HashSet; @@ -128,7 +128,7 @@ impl BarModuleFn for BarModuleBattery { &self.config } - fn build(&self, nai: &Option) -> s::Block { + fn build(&self, nai: &Option) -> s::Block { let mut state = self.state.lock().expect("Could not lock state."); if should_refresh(self, nai) { diff --git a/swayrbar/src/module/date.rs b/swayrbar/src/module/date.rs index 9a7e2a6..f60f8cb 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, NameAndInstance}; +use crate::module::{BarModuleFn, NameInstanceAndReason}; use swaybar_types as s; const NAME: &str = "date"; @@ -61,7 +61,7 @@ impl BarModuleFn for BarModuleDate { } } - fn build(&self, _nai: &Option) -> 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 340929d..3352c9d 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::{should_refresh, BarModuleFn, NameAndInstance}; +use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason}; use crate::shared::fmt::subst_placeholders; use once_cell::sync::Lazy; use regex::Regex; @@ -142,7 +142,7 @@ impl BarModuleFn for BarModulePactl { &self.config } - fn build(&self, nai: &Option) -> s::Block { + fn build(&self, nai: &Option) -> s::Block { let mut state = self.state.lock().expect("Could not lock state."); if should_refresh(self, nai) { diff --git a/swayrbar/src/module/sysinfo.rs b/swayrbar/src/module/sysinfo.rs index cb5d6cc..e9aa938 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::{should_refresh, BarModuleFn, NameAndInstance}; +use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason}; use crate::shared::fmt::subst_placeholders; use std::collections::HashMap; use std::sync::Mutex; @@ -145,7 +145,7 @@ impl BarModuleFn for BarModuleSysInfo { &self.config } - fn build(&self, nai: &Option) -> 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."); diff --git a/swayrbar/src/module/window.rs b/swayrbar/src/module/window.rs index 8a2131b..4838589 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::{should_refresh, BarModuleFn, NameAndInstance}; +use crate::module::{should_refresh, BarModuleFn, NameInstanceAndReason}; use crate::shared::fmt::subst_placeholders; use crate::shared::ipc; use crate::shared::ipc::NodeMethods; @@ -112,7 +112,7 @@ impl BarModuleFn for BarModuleWindow { &self.config } - fn build(&self, nai: &Option) -> s::Block { + fn build(&self, nai: &Option) -> s::Block { let mut state = self.state.lock().expect("Could not lock state."); // In contrast to other modules, this one should only refresh its state