mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-07-01 10:41:03 +02:00
refactor: swap out some code for existing macros
This commit is contained in:
parent
ad97550583
commit
012762e102
5 changed files with 41 additions and 54 deletions
|
@ -1,5 +1,5 @@
|
||||||
use super::{Workspace, WorkspaceClient, WorkspaceUpdate};
|
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::data::{Workspace as HWorkspace, Workspaces};
|
||||||
use hyprland::dispatch::{Dispatch, DispatchType, WorkspaceIdentifierWithSpecial};
|
use hyprland::dispatch::{Dispatch, DispatchType, WorkspaceIdentifierWithSpecial};
|
||||||
use hyprland::event_listener::EventListenerMutable as EventListener;
|
use hyprland::event_listener::EventListenerMutable as EventListener;
|
||||||
|
@ -52,8 +52,7 @@ impl EventClient {
|
||||||
workspace.map_or_else(
|
workspace.map_or_else(
|
||||||
|| error!("Unable to locate workspace"),
|
|| error!("Unable to locate workspace"),
|
||||||
|workspace| {
|
|workspace| {
|
||||||
tx.send(WorkspaceUpdate::Add(workspace))
|
send!(tx, WorkspaceUpdate::Add(workspace));
|
||||||
.expect(ERR_CHANNEL_SEND);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -72,16 +71,17 @@ impl EventClient {
|
||||||
|
|
||||||
if let (Some(prev_workspace), Some(workspace)) = (prev_workspace, workspace) {
|
if let (Some(prev_workspace), Some(workspace)) = (prev_workspace, workspace) {
|
||||||
if prev_workspace.id != workspace.id {
|
if prev_workspace.id != workspace.id {
|
||||||
tx.send(WorkspaceUpdate::Focus {
|
send!(
|
||||||
old: prev_workspace,
|
tx,
|
||||||
new: workspace.clone(),
|
WorkspaceUpdate::Focus {
|
||||||
})
|
old: prev_workspace,
|
||||||
.expect(ERR_CHANNEL_SEND);
|
new: workspace.clone(),
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// there may be another type of update so dispatch that regardless of focus change
|
// there may be another type of update so dispatch that regardless of focus change
|
||||||
tx.send(WorkspaceUpdate::Update(workspace))
|
send!(tx, WorkspaceUpdate::Update(workspace));
|
||||||
.expect(ERR_CHANNEL_SEND);
|
|
||||||
} else {
|
} else {
|
||||||
error!("Unable to locate workspace");
|
error!("Unable to locate workspace");
|
||||||
}
|
}
|
||||||
|
@ -97,8 +97,7 @@ impl EventClient {
|
||||||
workspace.map_or_else(
|
workspace.map_or_else(
|
||||||
|| error!("Unable to locate workspace"),
|
|| error!("Unable to locate workspace"),
|
||||||
|workspace| {
|
|workspace| {
|
||||||
tx.send(WorkspaceUpdate::Remove(workspace))
|
send!(tx, WorkspaceUpdate::Remove(workspace));
|
||||||
.expect(ERR_CHANNEL_SEND);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -119,8 +118,7 @@ impl EventClient {
|
||||||
workspace.map_or_else(
|
workspace.map_or_else(
|
||||||
|| error!("Unable to locate workspace"),
|
|| error!("Unable to locate workspace"),
|
||||||
|workspace| {
|
|workspace| {
|
||||||
tx.send(WorkspaceUpdate::Move(workspace))
|
send!(tx, WorkspaceUpdate::Move(workspace));
|
||||||
.expect(ERR_CHANNEL_SEND);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
@ -140,11 +138,13 @@ impl EventClient {
|
||||||
|
|
||||||
if let (Some(prev_workspace), Some(workspace)) = (prev_workspace, workspace) {
|
if let (Some(prev_workspace), Some(workspace)) = (prev_workspace, workspace) {
|
||||||
if prev_workspace.id != workspace.id {
|
if prev_workspace.id != workspace.id {
|
||||||
tx.send(WorkspaceUpdate::Focus {
|
send!(
|
||||||
old: prev_workspace,
|
tx,
|
||||||
new: workspace,
|
WorkspaceUpdate::Focus {
|
||||||
})
|
old: prev_workspace,
|
||||||
.expect(ERR_CHANNEL_SEND);
|
new: workspace,
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error!("Unable to locate workspace");
|
error!("Unable to locate workspace");
|
||||||
|
@ -159,7 +159,7 @@ impl EventClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refresh_workspaces(workspaces: &Mutex<Vec<Workspace>>) {
|
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 active = HWorkspace::get_active().expect("Failed to get active workspace");
|
||||||
let new_workspaces = Workspaces::get()
|
let new_workspaces = Workspaces::get()
|
||||||
|
@ -173,7 +173,7 @@ impl EventClient {
|
||||||
fn get_workspace(workspaces: &Mutex<Vec<Workspace>>, id: WorkspaceType) -> Option<Workspace> {
|
fn get_workspace(workspaces: &Mutex<Vec<Workspace>>, id: WorkspaceType) -> Option<Workspace> {
|
||||||
let id_string = id_to_string(id);
|
let id_string = id_to_string(id);
|
||||||
|
|
||||||
let workspaces = workspaces.lock().expect(ERR_MUTEX_LOCK);
|
let workspaces = lock!(workspaces);
|
||||||
workspaces
|
workspaces
|
||||||
.iter()
|
.iter()
|
||||||
.find(|workspace| workspace.id == id_string)
|
.find(|workspace| workspace.id == id_string)
|
||||||
|
@ -181,7 +181,7 @@ impl EventClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_focused_workspace(workspaces: &Mutex<Vec<Workspace>>) -> Option<Workspace> {
|
fn get_focused_workspace(workspaces: &Mutex<Vec<Workspace>>) -> Option<Workspace> {
|
||||||
let workspaces = workspaces.lock().expect(ERR_MUTEX_LOCK);
|
let workspaces = lock!(workspaces);
|
||||||
workspaces
|
workspaces
|
||||||
.iter()
|
.iter()
|
||||||
.find(|workspace| workspace.focused)
|
.find(|workspace| workspace.focused)
|
||||||
|
@ -206,10 +206,9 @@ impl WorkspaceClient for EventClient {
|
||||||
let workspaces = self.workspaces.clone();
|
let workspaces = self.workspaces.clone();
|
||||||
Self::refresh_workspaces(&workspaces);
|
Self::refresh_workspaces(&workspaces);
|
||||||
|
|
||||||
let workspaces = workspaces.lock().expect(ERR_MUTEX_LOCK);
|
let workspaces = lock!(workspaces);
|
||||||
|
|
||||||
tx.send(WorkspaceUpdate::Init(workspaces.clone()))
|
send!(tx, WorkspaceUpdate::Init(workspaces.clone()));
|
||||||
.expect(ERR_CHANNEL_SEND);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rx
|
rx
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use super::{Workspace, WorkspaceClient, WorkspaceUpdate};
|
use super::{Workspace, WorkspaceClient, WorkspaceUpdate};
|
||||||
use crate::await_sync;
|
use crate::{await_sync, send};
|
||||||
use crate::error::ERR_CHANNEL_SEND;
|
|
||||||
use async_once::AsyncOnce;
|
use async_once::AsyncOnce;
|
||||||
use color_eyre::Report;
|
use color_eyre::Report;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
|
@ -75,7 +74,7 @@ impl WorkspaceClient for SwayEventClient {
|
||||||
let event =
|
let event =
|
||||||
WorkspaceUpdate::Init(workspaces.into_iter().map(Workspace::from).collect());
|
WorkspaceUpdate::Init(workspaces.into_iter().map(Workspace::from).collect());
|
||||||
|
|
||||||
tx.send(event).expect(ERR_CHANNEL_SEND);
|
send!(tx, event);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use super::{MusicClient, PlayerUpdate, Status, Track};
|
use super::{MusicClient, PlayerUpdate, Status, Track};
|
||||||
use crate::clients::music::PlayerState;
|
use crate::clients::music::PlayerState;
|
||||||
use crate::error::ERR_MUTEX_LOCK;
|
use crate::{lock, send};
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use mpris::{DBusError, Event, Metadata, PlaybackStatus, Player, PlayerFinder};
|
use mpris::{DBusError, Event, Metadata, PlaybackStatus, Player, PlayerFinder};
|
||||||
|
@ -44,7 +44,7 @@ impl Client {
|
||||||
.find_all()
|
.find_all()
|
||||||
.expect("Failed to connect to D-Bus");
|
.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 {
|
for player in players {
|
||||||
let identity = player.identity();
|
let identity = player.identity();
|
||||||
|
|
||||||
|
@ -57,8 +57,7 @@ impl Client {
|
||||||
.expect("Failed to connect to D-Bus");
|
.expect("Failed to connect to D-Bus");
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut current_player =
|
let mut current_player = lock!(current_player);
|
||||||
current_player.lock().expect(ERR_MUTEX_LOCK);
|
|
||||||
|
|
||||||
if status == PlaybackStatus::Playing || current_player.is_none() {
|
if status == PlaybackStatus::Playing || current_player.is_none() {
|
||||||
debug!("Setting active player to '{identity}'");
|
debug!("Setting active player to '{identity}'");
|
||||||
|
@ -108,22 +107,19 @@ impl Client {
|
||||||
trace!("Received player event from '{identity}': {event:?}");
|
trace!("Received player event from '{identity}': {event:?}");
|
||||||
match event {
|
match event {
|
||||||
Ok(Event::PlayerShutDown) => {
|
Ok(Event::PlayerShutDown) => {
|
||||||
current_player.lock().expect(ERR_MUTEX_LOCK).take();
|
lock!(current_player).take();
|
||||||
players.lock().expect(ERR_MUTEX_LOCK).remove(identity);
|
lock!(players).remove(identity);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Ok(Event::Playing) => {
|
Ok(Event::Playing) => {
|
||||||
current_player
|
lock!(current_player).replace(identity.to_string());
|
||||||
.lock()
|
|
||||||
.expect(ERR_MUTEX_LOCK)
|
|
||||||
.replace(identity.to_string());
|
|
||||||
|
|
||||||
if let Err(err) = Self::send_update(&player, &tx) {
|
if let Err(err) = Self::send_update(&player, &tx) {
|
||||||
error!("{err:?}");
|
error!("{err:?}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let current_player = current_player.lock().expect(ERR_MUTEX_LOCK);
|
let current_player = lock!(current_player);
|
||||||
let current_player = current_player.as_ref();
|
let current_player = current_player.as_ref();
|
||||||
if let Some(current_player) = current_player {
|
if let Some(current_player) = current_player {
|
||||||
if current_player == identity {
|
if current_player == identity {
|
||||||
|
@ -171,15 +167,13 @@ impl Client {
|
||||||
let track = Track::from(metadata);
|
let track = Track::from(metadata);
|
||||||
|
|
||||||
let player_update = PlayerUpdate::Update(Box::new(Some(track)), status);
|
let player_update = PlayerUpdate::Update(Box::new(Some(track)), status);
|
||||||
|
send!(tx, player_update);
|
||||||
tx.send(player_update)
|
|
||||||
.expect("Failed to send player update");
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_player(&self) -> Option<Player> {
|
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();
|
let player_name = player_name.as_ref();
|
||||||
|
|
||||||
player_name.and_then(|player_name| {
|
player_name.and_then(|player_name| {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::clients::music::{self, MusicClient, PlayerState, PlayerUpdate, Status, Track};
|
use crate::clients::music::{self, MusicClient, PlayerState, PlayerUpdate, Status, Track};
|
||||||
use crate::config::CommonConfig;
|
use crate::config::CommonConfig;
|
||||||
use crate::error::ERR_CHANNEL_SEND;
|
|
||||||
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
||||||
use crate::popup::Popup;
|
use crate::popup::Popup;
|
||||||
use crate::try_send;
|
use crate::{send_async, try_send};
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use dirs::{audio_dir, home_dir};
|
use dirs::{audio_dir, home_dir};
|
||||||
use glib::Continue;
|
use glib::Continue;
|
||||||
|
@ -202,14 +201,9 @@ impl Module<Button> for MusicModule {
|
||||||
display_string,
|
display_string,
|
||||||
};
|
};
|
||||||
|
|
||||||
tx.send(ModuleUpdateEvent::Update(Some(update)))
|
send_async!(tx, ModuleUpdateEvent::Update(Some(update)));
|
||||||
.await
|
|
||||||
.expect(ERR_CHANNEL_SEND);
|
|
||||||
}
|
}
|
||||||
None => tx
|
None => send_async!(tx, ModuleUpdateEvent::Update(None)),
|
||||||
.send(ModuleUpdateEvent::Update(None))
|
|
||||||
.await
|
|
||||||
.expect(ERR_CHANNEL_SEND),
|
|
||||||
},
|
},
|
||||||
PlayerUpdate::Disconnect => break,
|
PlayerUpdate::Disconnect => break,
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ use serde::Deserialize;
|
||||||
use tokio::spawn;
|
use tokio::spawn;
|
||||||
use tokio::sync::mpsc::{Receiver, Sender};
|
use tokio::sync::mpsc::{Receiver, Sender};
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
use crate::try_send;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Clone)]
|
#[derive(Debug, Deserialize, Clone)]
|
||||||
pub struct ScriptModule {
|
pub struct ScriptModule {
|
||||||
|
@ -63,8 +64,8 @@ impl Module<Label> for ScriptModule {
|
||||||
spawn(async move {
|
spawn(async move {
|
||||||
script.run(move |(out, _)| match out {
|
script.run(move |(out, _)| match out {
|
||||||
OutputStream::Stdout(stdout) => {
|
OutputStream::Stdout(stdout) => {
|
||||||
tx.try_send(ModuleUpdateEvent::Update(stdout))
|
try_send!(tx, ModuleUpdateEvent::Update(stdout));
|
||||||
.expect("Failed to send stdout"); }
|
},
|
||||||
OutputStream::Stderr(stderr) => {
|
OutputStream::Stderr(stderr) => {
|
||||||
error!("{:?}", Report::msg(stderr)
|
error!("{:?}", Report::msg(stderr)
|
||||||
.wrap_err("Watched script error:")
|
.wrap_err("Watched script error:")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue