1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 10:41:03 +02:00

refactor: macros to reduce repeated code

This commit is contained in:
Jake Stanger 2022-12-11 22:45:52 +00:00
parent 9d5049dde0
commit 5e21cbcca6
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
18 changed files with 225 additions and 248 deletions

View file

@ -4,7 +4,7 @@ use crate::dynamic_string::DynamicString;
use crate::modules::{Module, ModuleInfo, ModuleLocation, ModuleUpdateEvent, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleLocation, ModuleUpdateEvent, WidgetContext};
use crate::popup::Popup; use crate::popup::Popup;
use crate::script::{OutputStream, Script}; use crate::script::{OutputStream, Script};
use crate::{await_sync, error as err, Config}; use crate::{await_sync, read_lock, send, write_lock, Config};
use color_eyre::Result; use color_eyre::Result;
use gtk::gdk::Monitor; use gtk::gdk::Monitor;
use gtk::prelude::*; use gtk::prelude::*;
@ -251,10 +251,7 @@ where
/// Registers the popup content with the popup. /// Registers the popup content with the popup.
fn register_popup_content(popup: &Arc<RwLock<Popup>>, id: usize, popup_content: gtk::Box) { fn register_popup_content(popup: &Arc<RwLock<Popup>>, id: usize, popup_content: gtk::Box) {
popup write_lock!(popup).register_content(id, popup_content);
.write()
.expect(err::ERR_WRITE_LOCK)
.register_content(id, popup_content);
} }
/// Sets up the bridge channel receiver /// Sets up the bridge channel receiver
@ -277,14 +274,14 @@ fn setup_receiver<TSend>(
match ev { match ev {
ModuleUpdateEvent::Update(update) => { ModuleUpdateEvent::Update(update) => {
if has_popup { if has_popup {
p_tx.send(update.clone()).expect(err::ERR_CHANNEL_SEND); send!(p_tx, update.clone());
} }
w_tx.send(update).expect(err::ERR_CHANNEL_SEND); send!(w_tx, update);
} }
ModuleUpdateEvent::TogglePopup(geometry) => { ModuleUpdateEvent::TogglePopup(geometry) => {
debug!("Toggling popup for {} [#{}]", name, id); debug!("Toggling popup for {} [#{}]", name, id);
let popup = popup.read().expect(err::ERR_READ_LOCK); let popup = read_lock!(popup);
if popup.is_visible() { if popup.is_visible() {
popup.hide(); popup.hide();
} else { } else {
@ -295,7 +292,7 @@ fn setup_receiver<TSend>(
ModuleUpdateEvent::OpenPopup(geometry) => { ModuleUpdateEvent::OpenPopup(geometry) => {
debug!("Opening popup for {} [#{}]", name, id); debug!("Opening popup for {} [#{}]", name, id);
let popup = popup.read().expect(err::ERR_READ_LOCK); let popup = read_lock!(popup);
popup.hide(); popup.hide();
popup.show_content(id); popup.show_content(id);
popup.show(geometry); popup.show(geometry);
@ -303,7 +300,7 @@ fn setup_receiver<TSend>(
ModuleUpdateEvent::ClosePopup => { ModuleUpdateEvent::ClosePopup => {
debug!("Closing popup for {} [#{}]", name, id); debug!("Closing popup for {} [#{}]", name, id);
let popup = popup.read().expect(err::ERR_READ_LOCK); let popup = read_lock!(popup);
popup.hide(); popup.hide();
} }
} }
@ -333,7 +330,7 @@ fn setup_module_common_options(container: EventBox, common: CommonConfig) {
spawn(async move { spawn(async move {
script script
.run(|(_, success)| { .run(|(_, success)| {
tx.send(success).expect(err::ERR_CHANNEL_SEND); send!(tx, success);
}) })
.await; .await;
}); });

View file

@ -1,4 +1,4 @@
use crate::error; use crate::send;
use tokio::spawn; use tokio::spawn;
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -22,7 +22,7 @@ impl<T: Send + 'static> BridgeChannel<T> {
spawn(async move { spawn(async move {
while let Some(val) = async_rx.recv().await { while let Some(val) = async_rx.recv().await {
sync_tx.send(val).expect(error::ERR_CHANNEL_SEND); send!(sync_tx, val);
} }
}); });

View file

@ -2,6 +2,7 @@ use super::toplevel::{ToplevelEvent, ToplevelInfo};
use super::toplevel_manager::listen_for_toplevels; use super::toplevel_manager::listen_for_toplevels;
use super::ToplevelChange; use super::ToplevelChange;
use super::{Env, ToplevelHandler}; use super::{Env, ToplevelHandler};
use crate::{error as err, send, write_lock};
use color_eyre::Report; use color_eyre::Report;
use indexmap::IndexMap; use indexmap::IndexMap;
use smithay_client_toolkit::environment::Environment; use smithay_client_toolkit::environment::Environment;
@ -18,7 +19,6 @@ use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::{
zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1, zwlr_foreign_toplevel_handle_v1::ZwlrForeignToplevelHandleV1,
zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1, zwlr_foreign_toplevel_manager_v1::ZwlrForeignToplevelManagerV1,
}; };
use crate::error as err;
pub struct WaylandClient { pub struct WaylandClient {
pub outputs: Vec<OutputInfo>, pub outputs: Vec<OutputInfo>,
@ -47,19 +47,16 @@ impl WaylandClient {
.expect("Failed to connect to Wayland compositor"); .expect("Failed to connect to Wayland compositor");
let outputs = Self::get_outputs(&env); let outputs = Self::get_outputs(&env);
output_tx send!(output_tx, outputs);
.send(outputs)
.expect(err::ERR_CHANNEL_SEND);
let seats = env.get_all_seats(); let seats = env.get_all_seats();
seat_tx send!(
.send( seat_tx,
seats seats
.into_iter() .into_iter()
.map(|seat| seat.detach()) .map(|seat| seat.detach())
.collect::<Vec<WlSeat>>(), .collect::<Vec<WlSeat>>()
) );
.expect(err::ERR_CHANNEL_SEND);
let _toplevel_manager = env.require_global::<ZwlrForeignToplevelManagerV1>(); let _toplevel_manager = env.require_global::<ZwlrForeignToplevelManagerV1>();
@ -67,20 +64,13 @@ impl WaylandClient {
trace!("Received toplevel event: {:?}", event); trace!("Received toplevel event: {:?}", event);
if event.change == ToplevelChange::Close { if event.change == ToplevelChange::Close {
toplevels2 write_lock!(toplevels2).remove(&event.toplevel.id);
.write()
.expect(err::ERR_WRITE_LOCK)
.remove(&event.toplevel.id);
} else { } else {
toplevels2 write_lock!(toplevels2)
.write()
.expect(err::ERR_WRITE_LOCK)
.insert(event.toplevel.id, (event.toplevel.clone(), handle)); .insert(event.toplevel.id, (event.toplevel.clone(), handle));
} }
toplevel_tx2 send!(toplevel_tx2, event);
.send(event)
.expect(err::ERR_CHANNEL_SEND);
}); });
let mut event_loop = let mut event_loop =
@ -100,9 +90,7 @@ impl WaylandClient {
} }
}); });
let outputs = output_rx let outputs = output_rx.await.expect(err::ERR_CHANNEL_RECV);
.await
.expect(err::ERR_CHANNEL_RECV);
let seats = seat_rx.await.expect(err::ERR_CHANNEL_RECV); let seats = seat_rx.await.expect(err::ERR_CHANNEL_RECV);

View file

@ -4,7 +4,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
use tracing::trace; use tracing::trace;
use wayland_client::{DispatchData, Main}; use wayland_client::{DispatchData, Main};
use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::zwlr_foreign_toplevel_handle_v1::{Event, ZwlrForeignToplevelHandleV1}; use wayland_protocols::wlr::unstable::foreign_toplevel::v1::client::zwlr_foreign_toplevel_handle_v1::{Event, ZwlrForeignToplevelHandleV1};
use crate::error; use crate::write_lock;
const STATE_ACTIVE: u32 = 2; const STATE_ACTIVE: u32 = 2;
const STATE_FULLSCREEN: u32 = 3; const STATE_FULLSCREEN: u32 = 3;
@ -143,9 +143,7 @@ impl Toplevel {
let inner = Arc::new(RwLock::new(ToplevelInfo::new())); let inner = Arc::new(RwLock::new(ToplevelInfo::new()));
handle.quick_assign(move |_handle, event, ddata| { handle.quick_assign(move |_handle, event, ddata| {
let mut inner = inner let mut inner = write_lock!(inner);
.write()
.expect(error::ERR_WRITE_LOCK);
toplevel_implem(event, &mut inner, &mut callback, ddata); toplevel_implem(event, &mut inner, &mut callback, ddata);
}); });

View file

@ -1,5 +1,5 @@
use crate::error;
use crate::script::{OutputStream, Script}; use crate::script::{OutputStream, Script};
use crate::{lock, send};
use gtk::prelude::*; use gtk::prelude::*;
use indexmap::IndexMap; use indexmap::IndexMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -66,10 +66,7 @@ impl DynamicString {
for (i, segment) in segments.into_iter().enumerate() { for (i, segment) in segments.into_iter().enumerate() {
match segment { match segment {
DynamicStringSegment::Static(str) => { DynamicStringSegment::Static(str) => {
label_parts lock!(label_parts).insert(i, str);
.lock()
.expect(error::ERR_MUTEX_LOCK)
.insert(i, str);
} }
DynamicStringSegment::Dynamic(script) => { DynamicStringSegment::Dynamic(script) => {
let tx = tx.clone(); let tx = tx.clone();
@ -79,20 +76,16 @@ impl DynamicString {
script script
.run(|(out, _)| { .run(|(out, _)| {
if let OutputStream::Stdout(out) = out { if let OutputStream::Stdout(out) = out {
let mut label_parts = let mut label_parts = lock!(label_parts);
label_parts.lock().expect(error::ERR_MUTEX_LOCK);
label_parts label_parts.insert(i, out);
// .lock()
// .expect("Failed to get lock on label parts")
.insert(i, out);
let string = label_parts let string = label_parts
.iter() .iter()
.map(|(_, part)| part.as_str()) .map(|(_, part)| part.as_str())
.collect::<String>(); .collect::<String>();
tx.send(string).expect(error::ERR_CHANNEL_SEND); send!(tx, string);
} }
}) })
.await; .await;
@ -103,14 +96,12 @@ impl DynamicString {
// initialize // initialize
{ {
let label_parts = label_parts let label_parts = lock!(label_parts)
.lock()
.expect(error::ERR_MUTEX_LOCK)
.iter() .iter()
.map(|(_, part)| part.as_str()) .map(|(_, part)| part.as_str())
.collect::<String>(); .collect::<String>();
tx.send(label_parts).expect(error::ERR_CHANNEL_SEND); send!(tx, label_parts);
} }
rx.attach(None, f); rx.attach(None, f);

89
src/macros.rs Normal file
View file

@ -0,0 +1,89 @@
/// Sends a message on an asynchronous `Sender` using `send()`
/// Panics if the message cannot be sent.
///
/// Usage:
///
/// ```rs
/// send_async!(tx, "my message");
/// ```
#[macro_export]
macro_rules! send_async {
($tx:expr, $msg:expr) => {
$tx.send($msg).await.expect($crate::error::ERR_CHANNEL_SEND)
};
}
/// Sends a message on an synchronous `Sender` using `send()`
/// Panics if the message cannot be sent.
///
/// Usage:
///
/// ```rs
/// send!(tx, "my message");
/// ```
#[macro_export]
macro_rules! send {
($tx:expr, $msg:expr) => {
$tx.send($msg).expect($crate::error::ERR_CHANNEL_SEND)
};
}
/// Sends a message on an synchronous `Sender` using `try_send()`
/// Panics if the message cannot be sent.
///
/// Usage:
///
/// ```rs
/// try_send!(tx, "my message");
/// ```
#[macro_export]
macro_rules! try_send {
($tx:expr, $msg:expr) => {
$tx.try_send($msg).expect($crate::error::ERR_CHANNEL_SEND)
};
}
/// Locks a `Mutex`.
/// Panics if the `Mutex` cannot be locked.
///
/// Usage:
///
/// ```rs
/// let mut val = lock!(my_mutex);
/// ```
#[macro_export]
macro_rules! lock {
($mutex:expr) => {
$mutex.lock().expect($crate::error::ERR_MUTEX_LOCK)
};
}
/// Gets a read lock on a `RwLock`.
/// Panics if the `RwLock` cannot be locked.
///
/// Usage:
///
/// ```rs
/// let val = read_lock!(my_rwlock);
/// ```
#[macro_export]
macro_rules! read_lock {
($rwlock:expr) => {
$rwlock.read().expect($crate::error::ERR_READ_LOCK)
};
}
/// Gets a write lock on a `RwLock`.
/// Panics if the `RwLock` cannot be locked.
///
/// Usage:
///
/// ```rs
/// let mut val = write_lock!(my_rwlock);
/// ```
#[macro_export]
macro_rules! write_lock {
($rwlock:expr) => {
$rwlock.write().expect($crate::error::ERR_WRITE_LOCK)
};
}

View file

@ -6,6 +6,7 @@ mod dynamic_string;
mod error; mod error;
mod icon; mod icon;
mod logging; mod logging;
mod macros;
mod modules; mod modules;
mod popup; mod popup;
mod script; mod script;

View file

@ -1,7 +1,7 @@
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::error;
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::{send_async, try_send};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use color_eyre::Result; use color_eyre::Result;
use glib::Continue; use glib::Continue;
@ -47,9 +47,7 @@ impl Module<Button> for ClockModule {
spawn(async move { spawn(async move {
loop { loop {
let date = Local::now(); let date = Local::now();
tx.send(ModuleUpdateEvent::Update(date)) send_async!(tx, ModuleUpdateEvent::Update(date));
.await
.expect(error::ERR_CHANNEL_SEND);
sleep(tokio::time::Duration::from_millis(500)).await; sleep(tokio::time::Duration::from_millis(500)).await;
} }
}); });
@ -69,13 +67,10 @@ impl Module<Button> for ClockModule {
let orientation = info.bar_position.get_orientation(); let orientation = info.bar_position.get_orientation();
button.connect_clicked(move |button| { button.connect_clicked(move |button| {
context try_send!(
.tx context.tx,
.try_send(ModuleUpdateEvent::TogglePopup(Popup::button_pos( ModuleUpdateEvent::TogglePopup(Popup::button_pos(button, orientation))
button, );
orientation,
)))
.expect(error::ERR_CHANNEL_SEND);
}); });
let format = self.format.clone(); let format = self.format.clone();

View file

@ -1,9 +1,9 @@
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::dynamic_string::DynamicString; use crate::dynamic_string::DynamicString;
use crate::error as err;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::popup::{ButtonGeometry, Popup}; use crate::popup::{ButtonGeometry, Popup};
use crate::script::Script; use crate::script::Script;
use crate::{send_async, try_send};
use color_eyre::{Report, Result}; use color_eyre::{Report, Result};
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{Button, Label, Orientation}; use gtk::{Button, Label, Orientation};
@ -145,11 +145,13 @@ impl Widget {
if let Some(exec) = self.on_click { if let Some(exec) = self.on_click {
button.connect_clicked(move |button| { button.connect_clicked(move |button| {
tx.try_send(ExecEvent { try_send!(
tx,
ExecEvent {
cmd: exec.clone(), cmd: exec.clone(),
geometry: Popup::button_pos(button, bar_orientation), geometry: Popup::button_pos(button, bar_orientation),
}) }
.expect(err::ERR_CHANNEL_SEND); );
}); });
} }
@ -188,17 +190,11 @@ impl Module<gtk::Box> for CustomModule {
error!("{err:?}"); error!("{err:?}");
} }
} else if event.cmd == "popup:toggle" { } else if event.cmd == "popup:toggle" {
tx.send(ModuleUpdateEvent::TogglePopup(event.geometry)) send_async!(tx, ModuleUpdateEvent::TogglePopup(event.geometry));
.await
.expect(err::ERR_CHANNEL_SEND);
} else if event.cmd == "popup:open" { } else if event.cmd == "popup:open" {
tx.send(ModuleUpdateEvent::OpenPopup(event.geometry)) send_async!(tx, ModuleUpdateEvent::OpenPopup(event.geometry));
.await
.expect(err::ERR_CHANNEL_SEND);
} else if event.cmd == "popup:close" { } else if event.cmd == "popup:close" {
tx.send(ModuleUpdateEvent::ClosePopup) send_async!(tx, ModuleUpdateEvent::ClosePopup);
.await
.expect(err::ERR_CHANNEL_SEND);
} else { } else {
error!("Received invalid command: '{}'", event.cmd); error!("Received invalid command: '{}'", event.cmd);
} }

View file

@ -1,7 +1,7 @@
use crate::clients::wayland::{self, ToplevelChange}; use crate::clients::wayland::{self, ToplevelChange};
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::{await_sync, error, icon}; use crate::{await_sync, icon, read_lock, send_async};
use color_eyre::Result; use color_eyre::Result;
use glib::Continue; use glib::Continue;
use gtk::prelude::*; use gtk::prelude::*;
@ -49,7 +49,8 @@ impl Module<gtk::Box> for FocusedModule {
) -> Result<()> { ) -> Result<()> {
let focused = await_sync(async { let focused = await_sync(async {
let wl = wayland::get_client().await; let wl = wayland::get_client().await;
let toplevels = wl.toplevels.read().expect(error::ERR_READ_LOCK).clone(); // TODO: Avoid cloning
let toplevels = read_lock!(wl.toplevels).clone();
toplevels.into_iter().find(|(_, (top, _))| top.active) toplevels.into_iter().find(|(_, (top, _))| top.active)
}); });
@ -72,12 +73,10 @@ impl Module<gtk::Box> for FocusedModule {
}; };
if update { if update {
tx.send(ModuleUpdateEvent::Update(( send_async!(
event.toplevel.title, tx,
event.toplevel.app_id, ModuleUpdateEvent::Update((event.toplevel.title, event.toplevel.app_id))
))) );
.await
.expect(error::ERR_CHANNEL_SEND);
} }
} }
}); });

View file

@ -1,10 +1,10 @@
use super::open_state::OpenState; use super::open_state::OpenState;
use crate::clients::wayland::ToplevelInfo; use crate::clients::wayland::ToplevelInfo;
use crate::error;
use crate::icon::get_icon; use crate::icon::get_icon;
use crate::modules::launcher::{ItemEvent, LauncherUpdate}; use crate::modules::launcher::{ItemEvent, LauncherUpdate};
use crate::modules::ModuleUpdateEvent; use crate::modules::ModuleUpdateEvent;
use crate::popup::Popup; use crate::popup::Popup;
use crate::{read_lock, try_send};
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{Button, IconTheme, Image, Orientation}; use gtk::{Button, IconTheme, Image, Orientation};
use indexmap::IndexMap; use indexmap::IndexMap;
@ -178,14 +178,12 @@ impl ItemButton {
let app_id = item.app_id.clone(); let app_id = item.app_id.clone();
let tx = controller_tx.clone(); let tx = controller_tx.clone();
button.connect_clicked(move |button| { button.connect_clicked(move |button| {
// lazy check :| // lazy check :| TODO: Improve this
let style_context = button.style_context(); let style_context = button.style_context();
if style_context.has_class("open") { if style_context.has_class("open") {
tx.try_send(ItemEvent::FocusItem(app_id.clone())) try_send!(tx, ItemEvent::FocusItem(app_id.clone()));
.expect(error::ERR_CHANNEL_SEND);
} else { } else {
tx.try_send(ItemEvent::OpenItem(app_id.clone())) try_send!(tx, ItemEvent::OpenItem(app_id.clone()));
.expect(error::ERR_CHANNEL_SEND);
} }
}); });
} }
@ -200,22 +198,20 @@ impl ItemButton {
let menu_state = menu_state.clone(); let menu_state = menu_state.clone();
button.connect_enter_notify_event(move |button, _| { button.connect_enter_notify_event(move |button, _| {
let menu_state = menu_state.read().expect(error::ERR_READ_LOCK); let menu_state = read_lock!(menu_state);
if menu_state.num_windows > 1 { if menu_state.num_windows > 1 {
tx.try_send(ModuleUpdateEvent::Update(LauncherUpdate::Hover( try_send!(
app_id.clone(), tx,
))) ModuleUpdateEvent::Update(LauncherUpdate::Hover(app_id.clone(),))
.expect(error::ERR_CHANNEL_SEND); );
tx.try_send(ModuleUpdateEvent::OpenPopup(Popup::button_pos( try_send!(
button, tx,
orientation, ModuleUpdateEvent::OpenPopup(Popup::button_pos(button, orientation,))
))) );
.expect(error::ERR_CHANNEL_SEND);
} else { } else {
tx.try_send(ModuleUpdateEvent::ClosePopup) try_send!(tx, ModuleUpdateEvent::ClosePopup);
.expect(error::ERR_CHANNEL_SEND);
} }
Inhibit(false) Inhibit(false)

View file

@ -5,9 +5,9 @@ use self::item::{Item, ItemButton, Window};
use self::open_state::OpenState; use self::open_state::OpenState;
use crate::clients::wayland::{self, ToplevelChange}; use crate::clients::wayland::{self, ToplevelChange};
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::error as err;
use crate::icon::find_desktop_file; use crate::icon::find_desktop_file;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::{lock, read_lock, try_send, write_lock};
use color_eyre::{Help, Report}; use color_eyre::{Help, Report};
use glib::Continue; use glib::Continue;
use gtk::prelude::*; use gtk::prelude::*;
@ -111,9 +111,9 @@ impl Module<gtk::Box> for LauncherModule {
let tx = tx.clone(); let tx = tx.clone();
spawn(async move { spawn(async move {
let wl = wayland::get_client().await; let wl = wayland::get_client().await;
let open_windows = wl.toplevels.read().expect(err::ERR_READ_LOCK); let open_windows = read_lock!(wl.toplevels);
let mut items = items.lock().expect(err::ERR_MUTEX_LOCK); let mut items = lock!(items);
for (_, (window, _)) in open_windows.clone() { for (_, (window, _)) in open_windows.clone() {
let item = items.get_mut(&window.app_id); let item = items.get_mut(&window.app_id);
@ -155,12 +155,10 @@ impl Module<gtk::Box> for LauncherModule {
let window = event.toplevel; let window = event.toplevel;
let app_id = window.app_id.clone(); let app_id = window.app_id.clone();
let items = || items.lock().expect(err::ERR_MUTEX_LOCK);
match event.change { match event.change {
ToplevelChange::New => { ToplevelChange::New => {
let new_item = { let new_item = {
let mut items = items(); let mut items = lock!(items);
let item = items.get_mut(&app_id); let item = items.get_mut(&app_id);
match item { match item {
None => { None => {
@ -187,7 +185,7 @@ impl Module<gtk::Box> for LauncherModule {
} }
ToplevelChange::Close => { ToplevelChange::Close => {
let remove_item = { let remove_item = {
let mut items = items(); let mut items = lock!(items);
let item = items.get_mut(&app_id); let item = items.get_mut(&app_id);
match item { match item {
Some(item) => { Some(item) => {
@ -218,7 +216,7 @@ impl Module<gtk::Box> for LauncherModule {
ToplevelChange::Focus(focused) => { ToplevelChange::Focus(focused) => {
// TODO: Flatten this // TODO: Flatten this
let update_title = if focused { let update_title = if focused {
if let Some(item) = items().get_mut(&app_id) { if let Some(item) = lock!(items).get_mut(&app_id) {
item.set_window_focused(window.id, true); item.set_window_focused(window.id, true);
// might be switching focus between windows of same app // might be switching focus between windows of same app
@ -243,7 +241,7 @@ impl Module<gtk::Box> for LauncherModule {
} }
} }
ToplevelChange::Title(title) => { ToplevelChange::Title(title) => {
if let Some(item) = items().get_mut(&app_id) { if let Some(item) = lock!(items).get_mut(&app_id) {
item.set_window_name(window.id, title.clone()); item.set_window_name(window.id, title.clone());
} }
@ -286,7 +284,7 @@ impl Module<gtk::Box> for LauncherModule {
); );
} else { } else {
let wl = wayland::get_client().await; let wl = wayland::get_client().await;
let items = items.lock().expect(err::ERR_MUTEX_LOCK); let items = lock!(items);
let id = match event { let id = match event {
ItemEvent::FocusItem(app_id) => items ItemEvent::FocusItem(app_id) => items
@ -297,7 +295,7 @@ impl Module<gtk::Box> for LauncherModule {
}; };
if let Some(id) = id { if let Some(id) = id {
let toplevels = wl.toplevels.read().expect(err::ERR_READ_LOCK); let toplevels = read_lock!(wl.toplevels);
let seat = wl.seats.first().expect("Failed to get Wayland seat"); let seat = wl.seats.first().expect("Failed to get Wayland seat");
if let Some((_top, handle)) = toplevels.get(&id) { if let Some((_top, handle)) = toplevels.get(&id) {
handle.activate(seat); handle.activate(seat);
@ -360,8 +358,7 @@ impl Module<gtk::Box> for LauncherModule {
if let Some(button) = buttons.get(&app_id) { if let Some(button) = buttons.get(&app_id) {
button.set_open(true); button.set_open(true);
let mut menu_state = let mut menu_state = write_lock!(button.menu_state);
button.menu_state.write().expect(err::ERR_WRITE_LOCK);
menu_state.num_windows += 1; menu_state.num_windows += 1;
} }
} }
@ -382,8 +379,7 @@ impl Module<gtk::Box> for LauncherModule {
} }
LauncherUpdate::RemoveWindow(app_id, _) => { LauncherUpdate::RemoveWindow(app_id, _) => {
if let Some(button) = buttons.get(&app_id) { if let Some(button) = buttons.get(&app_id) {
let mut menu_state = let mut menu_state = write_lock!(button.menu_state);
button.menu_state.write().expect(err::ERR_WRITE_LOCK);
menu_state.num_windows -= 1; menu_state.num_windows -= 1;
} }
} }
@ -456,8 +452,7 @@ impl Module<gtk::Box> for LauncherModule {
{ {
let tx = controller_tx.clone(); let tx = controller_tx.clone();
button.connect_clicked(move |button| { button.connect_clicked(move |button| {
tx.try_send(ItemEvent::FocusWindow(win.id)) try_send!(tx, ItemEvent::FocusWindow(win.id));
.expect(err::ERR_CHANNEL_SEND);
if let Some(win) = button.window() { if let Some(win) = button.window() {
win.hide(); win.hide();
@ -486,8 +481,7 @@ impl Module<gtk::Box> for LauncherModule {
{ {
let tx = controller_tx.clone(); let tx = controller_tx.clone();
button.connect_clicked(move |button| { button.connect_clicked(move |button| {
tx.try_send(ItemEvent::FocusWindow(win.id)) try_send!(tx, ItemEvent::FocusWindow(win.id));
.expect(err::ERR_CHANNEL_SEND);
if let Some(win) = button.window() { if let Some(win) = button.window() {
win.hide(); win.hide();

View file

@ -1,8 +1,8 @@
use crate::clients::mpd::{get_client, get_duration, get_elapsed, MpdConnectionError}; use crate::clients::mpd::{get_client, get_duration, get_elapsed, MpdConnectionError};
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::error as err;
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 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;
@ -226,11 +226,10 @@ impl Module<Button> for MpdModule {
let tx = context.tx.clone(); let tx = context.tx.clone();
button.connect_clicked(move |button| { button.connect_clicked(move |button| {
tx.try_send(ModuleUpdateEvent::TogglePopup(Popup::button_pos( try_send!(
button, tx,
orientation, ModuleUpdateEvent::TogglePopup(Popup::button_pos(button, orientation,))
))) );
.expect(err::ERR_CHANNEL_SEND);
}); });
} }
@ -244,8 +243,7 @@ impl Module<Button> for MpdModule {
button.show(); button.show();
} else { } else {
button.hide(); button.hide();
tx.try_send(ModuleUpdateEvent::ClosePopup) try_send!(tx, ModuleUpdateEvent::ClosePopup);
.expect(err::ERR_CHANNEL_SEND);
} }
Continue(true) Continue(true)
@ -323,31 +321,22 @@ impl Module<Button> for MpdModule {
let tx_prev = tx.clone(); let tx_prev = tx.clone();
btn_prev.connect_clicked(move |_| { btn_prev.connect_clicked(move |_| {
tx_prev try_send!(tx_prev, PlayerCommand::Previous);
.try_send(PlayerCommand::Previous)
.expect(err::ERR_CHANNEL_SEND);
}); });
let tx_toggle = tx.clone(); let tx_toggle = tx.clone();
btn_play_pause.connect_clicked(move |_| { btn_play_pause.connect_clicked(move |_| {
tx_toggle try_send!(tx_toggle, PlayerCommand::Toggle);
.try_send(PlayerCommand::Toggle)
.expect(err::ERR_CHANNEL_SEND);
}); });
let tx_next = tx.clone(); let tx_next = tx.clone();
btn_next.connect_clicked(move |_| { btn_next.connect_clicked(move |_| {
tx_next try_send!(tx_next, PlayerCommand::Next);
.try_send(PlayerCommand::Next)
.expect(err::ERR_CHANNEL_SEND);
}); });
let tx_vol = tx; let tx_vol = tx;
volume_slider.connect_change_value(move |_, _, val| { volume_slider.connect_change_value(move |_, _, val| {
tx_vol try_send!(tx_vol, PlayerCommand::Volume(val as u8));
.try_send(PlayerCommand::Volume(val as u8))
.expect(err::ERR_CHANNEL_SEND);
Inhibit(false) Inhibit(false)
}); });

View file

@ -1,6 +1,6 @@
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::error;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::send_async;
use color_eyre::Result; use color_eyre::Result;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::Label; use gtk::Label;
@ -140,83 +140,24 @@ impl Module<gtk::Box> for SysInfoModule {
let (refresh_tx, mut refresh_rx) = mpsc::channel(16); let (refresh_tx, mut refresh_rx) = mpsc::channel(16);
// memory refresh macro_rules! spawn_refresh {
{ ($refresh_type:expr, $func:ident) => {{
let tx = refresh_tx.clone(); let tx = refresh_tx.clone();
spawn(async move { spawn(async move {
loop { loop {
tx.send(RefreshType::Memory) send_async!(tx, $refresh_type);
.await sleep(Duration::from_secs(interval.$func())).await;
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.memory())).await;
} }
}); });
}};
} }
// cpu refresh spawn_refresh!(RefreshType::Memory, memory);
{ spawn_refresh!(RefreshType::Cpu, cpu);
let tx = refresh_tx.clone(); spawn_refresh!(RefreshType::Temps, temps);
spawn(async move { spawn_refresh!(RefreshType::Disks, disks);
loop { spawn_refresh!(RefreshType::Network, networks);
tx.send(RefreshType::Cpu) spawn_refresh!(RefreshType::System, system);
.await
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.cpu())).await;
}
});
}
// temp refresh
{
let tx = refresh_tx.clone();
spawn(async move {
loop {
tx.send(RefreshType::Temps)
.await
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.temps())).await;
}
});
}
// disk refresh
{
let tx = refresh_tx.clone();
spawn(async move {
loop {
tx.send(RefreshType::Disks)
.await
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.disks())).await;
}
});
}
// network refresh
{
let tx = refresh_tx.clone();
spawn(async move {
loop {
tx.send(RefreshType::Network)
.await
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.networks())).await;
}
});
}
// system refresh
{
let tx = refresh_tx;
spawn(async move {
loop {
tx.send(RefreshType::System)
.await
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.system())).await;
}
});
}
spawn(async move { spawn(async move {
let mut format_info = HashMap::new(); let mut format_info = HashMap::new();
@ -233,9 +174,7 @@ impl Module<gtk::Box> for SysInfoModule {
RefreshType::System => refresh_system_tokens(&mut format_info, &sys), RefreshType::System => refresh_system_tokens(&mut format_info, &sys),
}; };
tx.send(ModuleUpdateEvent::Update(format_info.clone())) send_async!(tx, ModuleUpdateEvent::Update(format_info.clone()));
.await
.expect(error::ERR_CHANNEL_SEND);
} }
}); });

View file

@ -1,7 +1,7 @@
use crate::clients::system_tray::get_tray_event_client; use crate::clients::system_tray::get_tray_event_client;
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::{await_sync, error}; use crate::{await_sync, try_send};
use color_eyre::Result; use color_eyre::Result;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::{IconLookupFlags, IconTheme, Image, Menu, MenuBar, MenuItem, SeparatorMenuItem}; use gtk::{IconLookupFlags, IconTheme, Image, Menu, MenuBar, MenuItem, SeparatorMenuItem};
@ -70,12 +70,14 @@ fn get_menu_items(
{ {
let tx = tx.clone(); let tx = tx.clone();
item.connect_activate(move |_item| { item.connect_activate(move |_item| {
tx.try_send(NotifierItemCommand::MenuItemClicked { try_send!(
tx,
NotifierItemCommand::MenuItemClicked {
submenu_id: info.id, submenu_id: info.id,
menu_path: path.clone(), menu_path: path.clone(),
notifier_address: id.clone(), notifier_address: id.clone(),
}) }
.expect(error::ERR_CHANNEL_SEND); );
}); });
} }

View file

@ -1,7 +1,7 @@
use crate::clients::sway::{get_client, get_sub_client}; use crate::clients::sway::{get_client, get_sub_client};
use crate::config::CommonConfig; use crate::config::CommonConfig;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::{await_sync, error}; use crate::{await_sync, send_async, try_send};
use color_eyre::{Report, Result}; use color_eyre::{Report, Result};
use gtk::prelude::*; use gtk::prelude::*;
use gtk::Button; use gtk::Button;
@ -53,7 +53,7 @@ fn create_button(
let tx = tx.clone(); let tx = tx.clone();
let name = name.to_string(); let name = name.to_string();
button.connect_clicked(move |_item| { button.connect_clicked(move |_item| {
tx.try_send(name.clone()).expect(error::ERR_CHANNEL_SEND); try_send!(tx, name.clone());
}); });
} }
@ -93,8 +93,10 @@ impl Module<gtk::Box> for WorkspacesModule {
} }
}; };
tx.try_send(ModuleUpdateEvent::Update(WorkspaceUpdate::Init(workspaces))) try_send!(
.expect(error::ERR_CHANNEL_SEND); tx,
ModuleUpdateEvent::Update(WorkspaceUpdate::Init(workspaces))
);
// Subscribe & send events // Subscribe & send events
spawn(async move { spawn(async move {
@ -106,9 +108,10 @@ impl Module<gtk::Box> for WorkspacesModule {
trace!("Set up Sway workspace subscription"); trace!("Set up Sway workspace subscription");
while let Ok(payload) = srx.recv().await { while let Ok(payload) = srx.recv().await {
tx.send(ModuleUpdateEvent::Update(WorkspaceUpdate::Update(payload))) send_async!(
.await tx,
.expect(error::ERR_CHANNEL_SEND); ModuleUpdateEvent::Update(WorkspaceUpdate::Update(payload))
);
} }
}); });

View file

@ -1,4 +1,4 @@
use crate::error as err; use crate::send_async;
use color_eyre::eyre::WrapErr; use color_eyre::eyre::WrapErr;
use color_eyre::{Report, Result}; use color_eyre::{Report, Result};
use serde::Deserialize; use serde::Deserialize;
@ -261,10 +261,10 @@ impl Script {
select! { select! {
_ = handle.wait() => break, _ = handle.wait() => break,
Ok(Some(line)) = stdout_lines.next_line() => { Ok(Some(line)) = stdout_lines.next_line() => {
tx.send(OutputStream::Stdout(line)).await.expect(err::ERR_CHANNEL_SEND); send_async!(tx, OutputStream::Stdout(line));
} }
Ok(Some(line)) = stderr_lines.next_line() => { Ok(Some(line)) = stderr_lines.next_line() => {
tx.send(OutputStream::Stderr(line)).await.expect(err::ERR_CHANNEL_SEND); send_async!(tx, OutputStream::Stderr(line));
} }
} }
} }

View file

@ -1,4 +1,4 @@
use crate::error as err; use crate::send;
use color_eyre::{Help, Report}; use color_eyre::{Help, Report};
use glib::Continue; use glib::Continue;
use gtk::prelude::CssProviderExt; use gtk::prelude::CssProviderExt;
@ -38,7 +38,7 @@ pub fn load_css(style_path: PathBuf) {
Ok(event) if event.kind == EventKind::Modify(ModifyKind::Data(DataChange::Any)) => { Ok(event) if event.kind == EventKind::Modify(ModifyKind::Data(DataChange::Any)) => {
debug!("{event:?}"); debug!("{event:?}");
if let Some(path) = event.paths.first() { if let Some(path) = event.paths.first() {
tx.send(path.clone()).expect(err::ERR_CHANNEL_SEND); send!(tx, path.clone());
} }
} }
Err(e) => error!("Error occurred when watching stylesheet: {:?}", e), Err(e) => error!("Error occurred when watching stylesheet: {:?}", e),