Clicks work!

main
Tassilo Horn 3 years ago
parent 24f5929dd8
commit 726d9a0a9e
  1. 33
      swayrbar/src/bar.rs
  2. 11
      swayrbar/src/bar/config.rs
  3. 16
      swayrbar/src/bar/module.rs
  4. 9
      swayrbar/src/bar/module/battery.rs
  5. 21
      swayrbar/src/bar/module/date.rs
  6. 23
      swayrbar/src/bar/module/sysinfo.rs
  7. 9
      swayrbar/src/bar/module/window.rs

@ -19,6 +19,7 @@ use crate::bar::module::BarModuleFn;
use env_logger::Env; use env_logger::Env;
use serde_json; use serde_json;
use std::io; use std::io;
use std::process as p;
use std::{sync::Arc, thread}; use std::{sync::Arc, thread};
use swaybar_types as sbt; use swaybar_types as sbt;
@ -71,7 +72,7 @@ pub fn handle_input(mods: Arc<Vec<Box<dyn BarModuleFn>>>) {
} }
let click = match serde_json::from_str::<sbt::Click>( let click = match serde_json::from_str::<sbt::Click>(
buf.strip_prefix(",").unwrap_or(&buf), buf.strip_prefix(',').unwrap_or(&buf),
) { ) {
Ok(click) => click, Ok(click) => click,
Err(err) => { Err(err) => {
@ -82,6 +83,36 @@ pub fn handle_input(mods: Arc<Vec<Box<dyn BarModuleFn>>>) {
} }
}; };
log::debug!("Received click: {:?}", click); log::debug!("Received click: {:?}", click);
handle_click(click, mods.clone());
}
}
fn handle_click(
click: sbt::Click,
mods: Arc<Vec<Box<dyn BarModuleFn>>>,
) -> Option<()> {
let name = click.name?;
let instance = click.instance?;
let button_str = format!("{:?}", click.button);
for m in mods.iter() {
if let Some(on_click) = m.get_on_click_map(&name, &instance) {
if let Some(cmd) = on_click.get(&button_str) {
execute_command(cmd);
return Some(());
}
}
}
None
}
fn execute_command(cmd: &[String]) {
match p::Command::new(&cmd[0]).args(&cmd[1..]).spawn() {
Ok(_child) => (),
Err(err) => {
log::error!("Error running shell command '{}':", cmd.join(" "));
log::error!("{}", err);
}
} }
} }

@ -15,7 +15,9 @@
//! TOML configuration for swayrbar. //! TOML configuration for swayrbar.
use crate::bar::module::BarModuleFn;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Config { pub struct Config {
@ -29,10 +31,11 @@ pub struct Config {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct ModuleConfig { pub struct ModuleConfig {
pub module_type: String, pub name: String,
pub instance: String, pub instance: String,
pub format: String, pub format: String,
pub html_escape: bool, pub html_escape: bool,
pub on_click: HashMap<String, Vec<String>>,
} }
impl Default for Config { impl Default for Config {
@ -40,7 +43,11 @@ impl Default for Config {
Config { Config {
refresh_interval: 1000, refresh_interval: 1000,
modules: vec!["date/0".to_owned()], modules: vec!["date/0".to_owned()],
module_configs: vec![], module_configs: vec![
crate::bar::module::date::BarModuleDate::default_config(
"0".to_owned(),
),
],
} }
} }
} }

@ -13,6 +13,8 @@
// You should have received a copy of the GNU General Public License along with // You should have received a copy of the GNU General Public License along with
// this program. If not, see <https://www.gnu.org/licenses/>. // this program. If not, see <https://www.gnu.org/licenses/>.
use std::collections::HashMap;
use crate::bar::config; use crate::bar::config;
use swaybar_types as s; use swaybar_types as s;
@ -31,6 +33,18 @@ pub trait BarModuleFn: Sync + Send {
fn name() -> &'static str fn name() -> &'static str
where where
Self: Sized; Self: Sized;
fn matches(&self, name: &str, instance: &str) -> bool; fn get_config(&self) -> &config::ModuleConfig;
fn get_on_click_map(
&self,
name: &str,
instance: &str,
) -> Option<&HashMap<String, Vec<String>>> {
let cfg = self.get_config();
if name == cfg.name && instance == cfg.instance {
Some(&cfg.on_click)
} else {
None
}
}
fn build(&self) -> s::Block; fn build(&self) -> s::Block;
} }

@ -19,7 +19,7 @@ use crate::bar::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use crate::fmt_replace::fmt_replace; use crate::fmt_replace::fmt_replace;
use battery as bat; use battery as bat;
use std::collections::HashSet; use std::collections::{HashMap, HashSet};
use swaybar_types as s; use swaybar_types as s;
const NAME: &str = "battery"; const NAME: &str = "battery";
@ -96,10 +96,11 @@ impl BarModuleFn for BarModuleBattery {
fn default_config(instance: String) -> config::ModuleConfig { fn default_config(instance: String) -> config::ModuleConfig {
config::ModuleConfig { config::ModuleConfig {
module_type: Self::name().to_owned(), name: Self::name().to_owned(),
instance, instance,
format: "🔋 Bat: {state_of_charge:{:5.1}}%, {state}, Health: {state_of_health:{:5.1}}%".to_owned(), format: "🔋 Bat: {state_of_charge:{:5.1}}%, {state}, Health: {state_of_health:{:5.1}}%".to_owned(),
html_escape: true, html_escape: true,
on_click: HashMap::new()
} }
} }
@ -107,8 +108,8 @@ impl BarModuleFn for BarModuleBattery {
NAME NAME
} }
fn matches(&self, name: &str, instance: &str) -> bool { fn get_config(&self) -> &config::ModuleConfig {
NAME == name && self.config.instance == instance &self.config
} }
fn build(&self) -> s::Block { fn build(&self) -> s::Block {

@ -15,6 +15,8 @@
//! The date `swayrbar` module. //! The date `swayrbar` module.
use std::collections::HashMap;
use crate::bar::module::config; use crate::bar::module::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use swaybar_types as s; use swaybar_types as s;
@ -26,25 +28,30 @@ pub struct BarModuleDate {
} }
impl BarModuleFn for BarModuleDate { impl BarModuleFn for BarModuleDate {
fn create(cfg: config::ModuleConfig) -> Box<dyn BarModuleFn> {
Box::new(BarModuleDate { config: cfg })
}
fn default_config(instance: String) -> config::ModuleConfig { fn default_config(instance: String) -> config::ModuleConfig {
config::ModuleConfig { config::ModuleConfig {
module_type: "date".to_owned(), name: "date".to_owned(),
instance, instance,
format: "⏰ %F %X".to_owned(), format: "⏰ %F %X".to_owned(),
html_escape: false, html_escape: false,
// TODO: Only for testing.
on_click: HashMap::from([(
"Left".to_owned(),
vec!["foot".to_owned(), "htop".to_owned()],
)]),
} }
} }
fn create(cfg: config::ModuleConfig) -> Box<dyn BarModuleFn> {
Box::new(BarModuleDate { config: cfg })
}
fn name() -> &'static str { fn name() -> &'static str {
NAME NAME
} }
fn matches(&self, name: &str, instance: &str) -> bool { fn get_config(&self) -> &config::ModuleConfig {
NAME == name && self.config.instance == instance &self.config
} }
fn build(&self) -> s::Block { fn build(&self) -> s::Block {

@ -18,6 +18,7 @@
use crate::bar::config; use crate::bar::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use crate::fmt_replace::fmt_replace; use crate::fmt_replace::fmt_replace;
use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::Once; use std::sync::Once;
use swaybar_types as s; use swaybar_types as s;
@ -93,12 +94,22 @@ impl BarModuleFn for BarModuleSysInfo {
}) })
} }
fn default_config(instance: String) -> config::ModuleConfig {
config::ModuleConfig {
name: "sysinfo".to_owned(),
instance,
format: "💻 CPU: {cpu_usage:{:4.1}}% Mem: {mem_usage:{:4.1}}% Load: {load_avg_1:{:4.2}} / {load_avg_5:{:4.2}} / {load_avg_15:{:4.2}}".to_owned(),
html_escape: true,
on_click: HashMap::new(),
}
}
fn name() -> &'static str { fn name() -> &'static str {
NAME NAME
} }
fn matches(&self, name: &str, instance: &str) -> bool { fn get_config(&self) -> &config::ModuleConfig {
NAME == name && self.config.instance == instance &self.config
} }
fn build(&self) -> s::Block { fn build(&self) -> s::Block {
@ -135,12 +146,4 @@ impl BarModuleFn for BarModuleSysInfo {
separator_block_width: None, separator_block_width: None,
} }
} }
fn default_config(instance: String) -> config::ModuleConfig {
config::ModuleConfig {
module_type: "sysinfo".to_owned(),
instance,
format: "💻 CPU: {cpu_usage:{:4.1}}% Mem: {mem_usage:{:4.1}}% Load: {load_avg_1:{:4.2}} / {load_avg_5:{:4.2}} / {load_avg_15:{:4.2}}".to_owned(),
html_escape: true }
}
} }

@ -15,6 +15,8 @@
//! The window `swayrbar` module. //! The window `swayrbar` module.
use std::collections::HashMap;
use crate::bar::config; use crate::bar::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use crate::fmt_replace::fmt_replace; use crate::fmt_replace::fmt_replace;
@ -35,10 +37,11 @@ impl BarModuleFn for BarModuleWindow {
fn default_config(instance: String) -> config::ModuleConfig { fn default_config(instance: String) -> config::ModuleConfig {
config::ModuleConfig { config::ModuleConfig {
module_type: Self::name().to_owned(), name: Self::name().to_owned(),
instance, instance,
format: "🪟 {title} — {app_name}".to_owned(), format: "🪟 {title} — {app_name}".to_owned(),
html_escape: true, html_escape: true,
on_click: HashMap::new(),
} }
} }
@ -46,8 +49,8 @@ impl BarModuleFn for BarModuleWindow {
NAME NAME
} }
fn matches(&self, name: &str, instance: &str) -> bool { fn get_config(&self) -> &config::ModuleConfig {
NAME == name && self.config.instance == instance &self.config
} }
fn build(&self) -> s::Block { fn build(&self) -> s::Block {

Loading…
Cancel
Save