diff --git a/src/backend/audio.rs b/src/backend/audio.rs index b7939e3..167aa66 100644 --- a/src/backend/audio.rs +++ b/src/backend/audio.rs @@ -475,7 +475,7 @@ impl BackendThread> for MediaBackend { let instance = instance.clone(); move || { while let Ok(sender) = registration_rx.recv() { - if let Ok(_) = sender.send(MediaMessage::Init(tx.clone())) { + if sender.send(MediaMessage::Init(tx.clone())).is_ok() { let mut instance_guard = instance.lock().unwrap(); if let Some(name) = &instance_guard.active_player && let Some(status) = instance_guard.players.get(name) @@ -517,28 +517,28 @@ impl BackendThread> for MediaBackend { }; while let Ok(msg) = rx.recv() { let mut instance_guard = instance.lock().unwrap(); - if let Some(name) = &instance_guard.active_player { - if let Some(status) = instance_guard.players.get(name) { - match msg { - MediaMessage::PlayerPlay => { - if let PlayerStatus::Paused(_) = status { - instance_guard.active_player_play(&connection).await; - } + if let Some(name) = &instance_guard.active_player + && let Some(status) = instance_guard.players.get(name) + { + match msg { + MediaMessage::PlayerPlay => { + if let PlayerStatus::Paused(_) = status { + instance_guard.active_player_play(&connection).await; } - MediaMessage::PlayerPause => { - if let PlayerStatus::Playing(_) = status { - instance_guard.active_player_pause(&connection).await; - } - } - MediaMessage::PlayerStop => { - if let PlayerStatus::Playing(_) | PlayerStatus::Paused(_) = - status - { - instance_guard.active_player_stop(&connection).await; - } - } - _ => {} } + MediaMessage::PlayerPause => { + if let PlayerStatus::Playing(_) = status { + instance_guard.active_player_pause(&connection).await; + } + } + MediaMessage::PlayerStop => { + if let PlayerStatus::Playing(_) | PlayerStatus::Paused(_) = + status + { + instance_guard.active_player_stop(&connection).await; + } + } + _ => {} } } } @@ -575,7 +575,7 @@ impl MediaBackend { .collect(); for player in players { - Self::spawn_new_player_watcher(&connection, &instance, &player); + Self::spawn_new_player_watcher(connection, &instance, &player); } Ok(()) @@ -680,11 +680,11 @@ impl MediaBackend { continue; } - if let Some(value) = args.changed_properties().get("PlaybackStatus") { - if let Ok(status) = ::try_from(value.clone()) { - debug!(&LOGGER, "Player ({name}) status has changed to: '{status}'"); - Self::insert_or_update_player(&instance, &props, name.into(), status).await; - } + if let Some(value) = args.changed_properties().get("PlaybackStatus") + && let Ok(status) = ::try_from(value.clone()) + { + debug!(&LOGGER, "Player ({name}) status has changed to: '{status}'"); + Self::insert_or_update_player(&instance, &props, name.into(), status).await; } } debug!(&LOGGER, "(2) Removing player: {name}"); @@ -699,7 +699,7 @@ impl MediaBackend { player_name: String, player_status: String, ) { - match Self::get_player_status_from_string(&props, player_status).await { + match Self::get_player_status_from_string(props, player_status).await { Ok(status) => { let mut instance_guard = instance.lock().unwrap(); instance_guard.players.insert(player_name.clone(), status); @@ -757,57 +757,51 @@ impl MediaBackend { } async fn active_player_play(&self, connection: &zbus::Connection) { - if let Some(player_name) = &self.active_player { - if let Ok(proxy) = zbus::Proxy::new( - &connection, + if let Some(player_name) = &self.active_player + && let Ok(proxy) = zbus::Proxy::new( + connection, player_name.as_str(), MPRIS_PATH, MPRIS_PLAYER_INTERFACE, ) .await - { - if let Err(e) = proxy.call_method("Play", &()).await { - warn!( - &LOGGER, - "Error starting play for player: {player_name}, {e}" - ); - } - } + && let Err(e) = proxy.call_method("Play", &()).await + { + warn!( + &LOGGER, + "Error starting play for player: {player_name}, {e}" + ); } } async fn active_player_pause(&mut self, connection: &zbus::Connection) { - if let Some(player_name) = &self.active_player { - if let Ok(proxy) = zbus::Proxy::new( - &connection, + if let Some(player_name) = &self.active_player + && let Ok(proxy) = zbus::Proxy::new( + connection, player_name.as_str(), MPRIS_PATH, MPRIS_PLAYER_INTERFACE, ) .await - { - if let Err(e) = proxy.call_method("Pause", &()).await { - warn!(&LOGGER, "Error pausing player: {player_name}, {e}"); - } - } + && let Err(e) = proxy.call_method("Pause", &()).await + { + warn!(&LOGGER, "Error pausing player: {player_name}, {e}"); self.select_active_player(self.active_player.clone()); } } async fn active_player_stop(&mut self, connection: &zbus::Connection) { - if let Some(player_name) = &self.active_player { - if let Ok(proxy) = zbus::Proxy::new( - &connection, + if let Some(player_name) = &self.active_player + && let Ok(proxy) = zbus::Proxy::new( + connection, player_name.as_str(), MPRIS_PATH, MPRIS_PLAYER_INTERFACE, ) .await - { - if let Err(e) = proxy.call_method("Stop", &()).await { - warn!(&LOGGER, "Error stopping player: {player_name}, {e}"); - } - } + && let Err(e) = proxy.call_method("Stop", &()).await + { + warn!(&LOGGER, "Error stopping player: {player_name}, {e}"); } self.select_active_player(None); } diff --git a/src/backend/commands.rs b/src/backend/commands.rs index 57d62f9..acea831 100644 --- a/src/backend/commands.rs +++ b/src/backend/commands.rs @@ -6,6 +6,7 @@ use jmb_logger_rs::*; use std::io::Read; use std::net::Shutdown; use std::os::unix::net::UnixListener; +use std::str::FromStr; use std::sync::{Arc, Mutex}; pub struct ControlSocket { @@ -34,11 +35,11 @@ impl SimpleBackendThread for ControlSocket { } }); std::thread::spawn(move || { - if std::fs::metadata(SOCKET_PATH).is_ok() { - if let Err(e) = std::fs::remove_file(SOCKET_PATH) { - error!(&LOGGER, "Error deleting old socket file: {e}"); - return; - } + if std::fs::metadata(SOCKET_PATH).is_ok() + && let Err(e) = std::fs::remove_file(SOCKET_PATH) + { + error!(&LOGGER, "Error deleting old socket file: {e}"); + return; } let socket = UnixListener::bind(SOCKET_PATH); let Ok(socket) = socket else { @@ -58,7 +59,7 @@ impl SimpleBackendThread for ControlSocket { let _ = stream.shutdown(Shutdown::Both); continue; } - if let Some(command) = ControlCommand::from_str(&read) { + if let Ok(command) = ControlCommand::from_str(&read) { instance.lock().unwrap().publish(command); } } diff --git a/src/backend/gpu_utilization.rs b/src/backend/gpu_utilization.rs index 144776e..77868d3 100644 --- a/src/backend/gpu_utilization.rs +++ b/src/backend/gpu_utilization.rs @@ -97,11 +97,13 @@ impl I915Engine { let pmu_type = read_number(&format!("{base}/type"))? as u32; let config = read_config(&format!("{base}/events/{name}"))?; - let mut attr = PerfEventAttr::default(); - attr.type_ = pmu_type; - attr.size = size_of::() as u32; - attr.config = config; - attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING; + let attr = PerfEventAttr { + type_: pmu_type, + size: size_of::() as u32, + config, + read_format: PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING, + ..Default::default() + }; // pid = -1 and cpu = -1: system-wide event, if permitted. let fd = unsafe { @@ -181,7 +183,7 @@ impl I915UtilizationMonitor { for (engine, sample) in &mut self.engines { let new_sample = engine.read().unwrap(); if let Some(last) = sample { - values.push(new_sample.calculate_utilization(&last) as u8); + values.push(new_sample.calculate_utilization(last) as u8); } let _ = sample.insert(new_sample); } diff --git a/src/backend/window_manager_hypr.rs b/src/backend/window_manager_hypr.rs index 6e0c461..bb73860 100644 --- a/src/backend/window_manager_hypr.rs +++ b/src/backend/window_manager_hypr.rs @@ -204,7 +204,7 @@ impl HyprlandComms { .output() } - pub fn cursor_monitor() -> Option { + pub fn _cursor_monitor() -> Option { if let Ok(pos) = CursorPosition::get() && let Ok(monitors) = Monitors::get() { diff --git a/src/config.rs b/src/config.rs index ad3a4e7..d36a13c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ use serde::Deserialize; +use std::default::Default; use std::fs; #[derive(Deserialize, Debug, Clone, Default)] @@ -10,8 +11,7 @@ impl Config { pub fn load() -> Config { fs::read_to_string("jmb-bar.conf") .ok() - .map(|s| toml::from_str::(&s).ok()) - .flatten() + .and_then(|s| toml::from_str::(&s).ok()) .unwrap_or_default() } } @@ -33,7 +33,8 @@ impl Default for IdleConfig { dim_timeout: 60, lock_timeout: 300, sleep_timeout: 320, - ..Default::default() + lock_command: String::default(), + sleep_command: String::default(), } } } diff --git a/src/ctl.rs b/src/ctl.rs index ec12cc0..0d213a5 100644 --- a/src/ctl.rs +++ b/src/ctl.rs @@ -1,7 +1,9 @@ use jmb_logger_rs::*; use std::env; +use std::fmt::{Display, Formatter}; use std::io::Write; use std::os::unix::net::UnixStream; +use std::str::FromStr; const CTL_LOGGER: Logger = Logger::create_with_custom_format(Level::DEBUG, String::new()); pub const SOCKET_PATH: &str = "/run/user/1000/jmb-bar.sock"; @@ -11,17 +13,21 @@ pub enum ControlCommand { OpenRunMenu, } -impl ControlCommand { - pub fn from_str(s: &str) -> Option { - match s { - "menu" => Some(ControlCommand::OpenRunMenu), - _ => None, +impl Display for ControlCommand { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ControlCommand::OpenRunMenu => f.write_str("menu"), } } +} - pub fn to_string(self) -> String { - match self { - ControlCommand::OpenRunMenu => String::from("menu"), +impl FromStr for ControlCommand { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "menu" => Ok(ControlCommand::OpenRunMenu), + other => Err(format!("Unknown command: {other}")), } } } @@ -43,7 +49,7 @@ fn main() { let args: Vec = env::args().collect(); match args.get(1) { - Some(str) if let Some(cmd) = ControlCommand::from_str(str) => stream + Some(str) if let Ok(cmd) = ControlCommand::from_str(str) => stream .write(cmd.to_string().as_bytes()) .expect("Error sending message"), _ => 0usize, diff --git a/src/ui/run_menu.rs b/src/ui/run_menu.rs index a2e6535..5e98e91 100644 --- a/src/ui/run_menu.rs +++ b/src/ui/run_menu.rs @@ -75,8 +75,6 @@ impl RunMenu { window.set_monitor(monitor.as_ref()); if let Some(monitor) = monitor { let geometry = monitor.geometry(); - let (_, natural_height, _, _) = - window.child().unwrap().measure(Orientation::Vertical, -1); let top_margin = (geometry.height()) / 2; window.set_margin(Edge::Top, top_margin - 100); } else { @@ -100,14 +98,12 @@ impl RunMenu { } if e.text_length() > 0 { let text = e.text(); - if text.starts_with("=") { - if let Ok(eval) = meval::eval_str(&text[1..]) { - let label = gtk::Label::new(Some(&format!("{eval}"))); - label.add_css_class("exec-button"); - label.set_halign(Align::Start); - label.set_hexpand(true); - content_box.append(&label); - } + if let Some(eval) = text.strip_prefix("=") { + let label = gtk::Label::new(Some(eval)); + label.add_css_class("exec-button"); + label.set_halign(Align::Start); + label.set_hexpand(true); + content_box.append(&label); } else { instance_clone.list_matching(e, &content_box, &mut matcher_rc.borrow_mut()); instance_clone.window.set_default_size(500, -1); @@ -164,8 +160,8 @@ impl RunMenu { self.window.present(); } - fn create_entry(&self, content_box: >k::Box, entry: &String) -> gtk::Button { - let entry_clone = entry.clone(); + fn create_entry(&self, content_box: >k::Box, entry: &str) -> gtk::Button { + let entry_clone = entry.to_owned(); let button = gtk::Button::new(); button.set_halign(Align::Fill); button.set_hexpand(true); @@ -198,14 +194,14 @@ impl RunMenu { content_box: >k::Box, matcher: &mut Matcher, ) -> bool { - let keys: Vec = self.entries.borrow().keys().map(|i| i.clone()).collect(); + let keys: Vec = self.entries.borrow().keys().cloned().collect(); let mut first = true; let mut added = false; for (entry, _) in Pattern::parse(&entry.text(), CaseMatching::Ignore, Normalization::Smart) .match_list(keys, matcher) .iter() { - let button = self.create_entry(content_box, &entry); + let button = self.create_entry(content_box, entry); if first { button.add_css_class("suggested"); first = false;