refactor: swap out some code for existing macros

This commit is contained in:
Jake Stanger 2023-01-28 16:27:01 +00:00
parent ad97550583
commit 012762e102
No known key found for this signature in database
GPG Key ID: C51FC8F9CB0BEA61
5 changed files with 41 additions and 54 deletions

View File

@ -1,5 +1,5 @@
use super::{Workspace, WorkspaceClient, WorkspaceUpdate};
use crate::error::{ERR_CHANNEL_SEND, ERR_MUTEX_LOCK};
use crate::{lock, send};
use hyprland::data::{Workspace as HWorkspace, Workspaces};
use hyprland::dispatch::{Dispatch, DispatchType, WorkspaceIdentifierWithSpecial};
use hyprland::event_listener::EventListenerMutable as EventListener;
@ -52,8 +52,7 @@ impl EventClient {
workspace.map_or_else(
|| error!("Unable to locate workspace"),
|workspace| {
tx.send(WorkspaceUpdate::Add(workspace))
.expect(ERR_CHANNEL_SEND);
send!(tx, WorkspaceUpdate::Add(workspace));
},
);
});
@ -72,16 +71,17 @@ impl EventClient {
if let (Some(prev_workspace), Some(workspace)) = (prev_workspace, workspace) {
if prev_workspace.id != workspace.id {
tx.send(WorkspaceUpdate::Focus {
old: prev_workspace,
new: workspace.clone(),
})
.expect(ERR_CHANNEL_SEND);
send!(
tx,
WorkspaceUpdate::Focus {
old: prev_workspace,
new: workspace.clone(),
}
);
}
// there may be another type of update so dispatch that regardless of focus change
tx.send(WorkspaceUpdate::Update(workspace))
.expect(ERR_CHANNEL_SEND);
send!(tx, WorkspaceUpdate::Update(workspace));
} else {
error!("Unable to locate workspace");
}
@ -97,8 +97,7 @@ impl EventClient {
workspace.map_or_else(
|| error!("Unable to locate workspace"),
|workspace| {
tx.send(WorkspaceUpdate::Remove(workspace))
.expect(ERR_CHANNEL_SEND);
send!(tx, WorkspaceUpdate::Remove(workspace));
},
);
@ -119,8 +118,7 @@ impl EventClient {
workspace.map_or_else(
|| error!("Unable to locate workspace"),
|workspace| {
tx.send(WorkspaceUpdate::Move(workspace))
.expect(ERR_CHANNEL_SEND);
send!(tx, WorkspaceUpdate::Move(workspace));
},
);
});
@ -140,11 +138,13 @@ impl EventClient {
if let (Some(prev_workspace), Some(workspace)) = (prev_workspace, workspace) {
if prev_workspace.id != workspace.id {
tx.send(WorkspaceUpdate::Focus {
old: prev_workspace,
new: workspace,
})
.expect(ERR_CHANNEL_SEND);
send!(
tx,
WorkspaceUpdate::Focus {
old: prev_workspace,
new: workspace,
}
);
}
} else {
error!("Unable to locate workspace");
@ -159,7 +159,7 @@ impl EventClient {
}
fn refresh_workspaces(workspaces: &Mutex<Vec<Workspace>>) {
let mut workspaces = workspaces.lock().expect(ERR_MUTEX_LOCK);
let mut workspaces = lock!(workspaces);
let active = HWorkspace::get_active().expect("Failed to get active workspace");
let new_workspaces = Workspaces::get()
@ -173,7 +173,7 @@ impl EventClient {
fn get_workspace(workspaces: &Mutex<Vec<Workspace>>, id: WorkspaceType) -> Option<Workspace> {
let id_string = id_to_string(id);
let workspaces = workspaces.lock().expect(ERR_MUTEX_LOCK);
let workspaces = lock!(workspaces);
workspaces
.iter()
.find(|workspace| workspace.id == id_string)
@ -181,7 +181,7 @@ impl EventClient {
}
fn get_focused_workspace(workspaces: &Mutex<Vec<Workspace>>) -> Option<Workspace> {
let workspaces = workspaces.lock().expect(ERR_MUTEX_LOCK);
let workspaces = lock!(workspaces);
workspaces
.iter()
.find(|workspace| workspace.focused)
@ -206,10 +206,9 @@ impl WorkspaceClient for EventClient {
let workspaces = self.workspaces.clone();
Self::refresh_workspaces(&workspaces);
let workspaces = workspaces.lock().expect(ERR_MUTEX_LOCK);
let workspaces = lock!(workspaces);
tx.send(WorkspaceUpdate::Init(workspaces.clone()))
.expect(ERR_CHANNEL_SEND);
send!(tx, WorkspaceUpdate::Init(workspaces.clone()));
}
rx

View File

@ -1,6 +1,5 @@
use super::{Workspace, WorkspaceClient, WorkspaceUpdate};
use crate::await_sync;
use crate::error::ERR_CHANNEL_SEND;
use crate::{await_sync, send};
use async_once::AsyncOnce;
use color_eyre::Report;
use futures_util::StreamExt;
@ -75,7 +74,7 @@ impl WorkspaceClient for SwayEventClient {
let event =
WorkspaceUpdate::Init(workspaces.into_iter().map(Workspace::from).collect());
tx.send(event).expect(ERR_CHANNEL_SEND);
send!(tx, event);
});
}

View File

@ -1,6 +1,6 @@
use super::{MusicClient, PlayerUpdate, Status, Track};
use crate::clients::music::PlayerState;
use crate::error::ERR_MUTEX_LOCK;
use crate::{lock, send};
use color_eyre::Result;
use lazy_static::lazy_static;
use mpris::{DBusError, Event, Metadata, PlaybackStatus, Player, PlayerFinder};
@ -44,7 +44,7 @@ impl Client {
.find_all()
.expect("Failed to connect to D-Bus");
let mut players_list_val = players_list.lock().expect(ERR_MUTEX_LOCK);
let mut players_list_val = lock!(players_list);
for player in players {
let identity = player.identity();
@ -57,8 +57,7 @@ impl Client {
.expect("Failed to connect to D-Bus");
{
let mut current_player =
current_player.lock().expect(ERR_MUTEX_LOCK);
let mut current_player = lock!(current_player);
if status == PlaybackStatus::Playing || current_player.is_none() {
debug!("Setting active player to '{identity}'");
@ -108,22 +107,19 @@ impl Client {
trace!("Received player event from '{identity}': {event:?}");
match event {
Ok(Event::PlayerShutDown) => {
current_player.lock().expect(ERR_MUTEX_LOCK).take();
players.lock().expect(ERR_MUTEX_LOCK).remove(identity);
lock!(current_player).take();
lock!(players).remove(identity);
break;
}
Ok(Event::Playing) => {
current_player
.lock()
.expect(ERR_MUTEX_LOCK)
.replace(identity.to_string());
lock!(current_player).replace(identity.to_string());
if let Err(err) = Self::send_update(&player, &tx) {
error!("{err:?}");
}
}
Ok(_) => {
let current_player = current_player.lock().expect(ERR_MUTEX_LOCK);
let current_player = lock!(current_player);
let current_player = current_player.as_ref();
if let Some(current_player) = current_player {
if current_player == identity {
@ -171,15 +167,13 @@ impl Client {
let track = Track::from(metadata);
let player_update = PlayerUpdate::Update(Box::new(Some(track)), status);
tx.send(player_update)
.expect("Failed to send player update");
send!(tx, player_update);
Ok(())
}
fn get_player(&self) -> Option<Player> {
let player_name = self.current_player.lock().expect(ERR_MUTEX_LOCK);
let player_name = lock!(self.current_player);
let player_name = player_name.as_ref();
player_name.and_then(|player_name| {

View File

@ -1,9 +1,8 @@
use crate::clients::music::{self, MusicClient, PlayerState, PlayerUpdate, Status, Track};
use crate::config::CommonConfig;
use crate::error::ERR_CHANNEL_SEND;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::popup::Popup;
use crate::try_send;
use crate::{send_async, try_send};
use color_eyre::Result;
use dirs::{audio_dir, home_dir};
use glib::Continue;
@ -202,14 +201,9 @@ impl Module<Button> for MusicModule {
display_string,
};
tx.send(ModuleUpdateEvent::Update(Some(update)))
.await
.expect(ERR_CHANNEL_SEND);
send_async!(tx, ModuleUpdateEvent::Update(Some(update)));
}
None => tx
.send(ModuleUpdateEvent::Update(None))
.await
.expect(ERR_CHANNEL_SEND),
None => send_async!(tx, ModuleUpdateEvent::Update(None)),
},
PlayerUpdate::Disconnect => break,
}

View File

@ -8,6 +8,7 @@ use serde::Deserialize;
use tokio::spawn;
use tokio::sync::mpsc::{Receiver, Sender};
use tracing::error;
use crate::try_send;
#[derive(Debug, Deserialize, Clone)]
pub struct ScriptModule {
@ -63,8 +64,8 @@ impl Module<Label> for ScriptModule {
spawn(async move {
script.run(move |(out, _)| match out {
OutputStream::Stdout(stdout) => {
tx.try_send(ModuleUpdateEvent::Update(stdout))
.expect("Failed to send stdout"); }
try_send!(tx, ModuleUpdateEvent::Update(stdout));
},
OutputStream::Stderr(stderr) => {
error!("{:?}", Report::msg(stderr)
.wrap_err("Watched script error:")