1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +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::popup::Popup;
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 gtk::gdk::Monitor;
use gtk::prelude::*;
@ -251,10 +251,7 @@ where
/// Registers the popup content with the popup.
fn register_popup_content(popup: &Arc<RwLock<Popup>>, id: usize, popup_content: gtk::Box) {
popup
.write()
.expect(err::ERR_WRITE_LOCK)
.register_content(id, popup_content);
write_lock!(popup).register_content(id, popup_content);
}
/// Sets up the bridge channel receiver
@ -277,14 +274,14 @@ fn setup_receiver<TSend>(
match ev {
ModuleUpdateEvent::Update(update) => {
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) => {
debug!("Toggling popup for {} [#{}]", name, id);
let popup = popup.read().expect(err::ERR_READ_LOCK);
let popup = read_lock!(popup);
if popup.is_visible() {
popup.hide();
} else {
@ -295,7 +292,7 @@ fn setup_receiver<TSend>(
ModuleUpdateEvent::OpenPopup(geometry) => {
debug!("Opening popup for {} [#{}]", name, id);
let popup = popup.read().expect(err::ERR_READ_LOCK);
let popup = read_lock!(popup);
popup.hide();
popup.show_content(id);
popup.show(geometry);
@ -303,7 +300,7 @@ fn setup_receiver<TSend>(
ModuleUpdateEvent::ClosePopup => {
debug!("Closing popup for {} [#{}]", name, id);
let popup = popup.read().expect(err::ERR_READ_LOCK);
let popup = read_lock!(popup);
popup.hide();
}
}
@ -333,7 +330,7 @@ fn setup_module_common_options(container: EventBox, common: CommonConfig) {
spawn(async move {
script
.run(|(_, success)| {
tx.send(success).expect(err::ERR_CHANNEL_SEND);
send!(tx, success);
})
.await;
});

View file

@ -1,4 +1,4 @@
use crate::error;
use crate::send;
use tokio::spawn;
use tokio::sync::mpsc;
@ -22,7 +22,7 @@ impl<T: Send + 'static> BridgeChannel<T> {
spawn(async move {
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::ToplevelChange;
use super::{Env, ToplevelHandler};
use crate::{error as err, send, write_lock};
use color_eyre::Report;
use indexmap::IndexMap;
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_manager_v1::ZwlrForeignToplevelManagerV1,
};
use crate::error as err;
pub struct WaylandClient {
pub outputs: Vec<OutputInfo>,
@ -47,19 +47,16 @@ impl WaylandClient {
.expect("Failed to connect to Wayland compositor");
let outputs = Self::get_outputs(&env);
output_tx
.send(outputs)
.expect(err::ERR_CHANNEL_SEND);
send!(output_tx, outputs);
let seats = env.get_all_seats();
seat_tx
.send(
seats
.into_iter()
.map(|seat| seat.detach())
.collect::<Vec<WlSeat>>(),
)
.expect(err::ERR_CHANNEL_SEND);
send!(
seat_tx,
seats
.into_iter()
.map(|seat| seat.detach())
.collect::<Vec<WlSeat>>()
);
let _toplevel_manager = env.require_global::<ZwlrForeignToplevelManagerV1>();
@ -67,20 +64,13 @@ impl WaylandClient {
trace!("Received toplevel event: {:?}", event);
if event.change == ToplevelChange::Close {
toplevels2
.write()
.expect(err::ERR_WRITE_LOCK)
.remove(&event.toplevel.id);
write_lock!(toplevels2).remove(&event.toplevel.id);
} else {
toplevels2
.write()
.expect(err::ERR_WRITE_LOCK)
write_lock!(toplevels2)
.insert(event.toplevel.id, (event.toplevel.clone(), handle));
}
toplevel_tx2
.send(event)
.expect(err::ERR_CHANNEL_SEND);
send!(toplevel_tx2, event);
});
let mut event_loop =
@ -100,9 +90,7 @@ impl WaylandClient {
}
});
let outputs = output_rx
.await
.expect(err::ERR_CHANNEL_RECV);
let outputs = output_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 wayland_client::{DispatchData, Main};
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_FULLSCREEN: u32 = 3;
@ -143,9 +143,7 @@ impl Toplevel {
let inner = Arc::new(RwLock::new(ToplevelInfo::new()));
handle.quick_assign(move |_handle, event, ddata| {
let mut inner = inner
.write()
.expect(error::ERR_WRITE_LOCK);
let mut inner = write_lock!(inner);
toplevel_implem(event, &mut inner, &mut callback, ddata);
});

View file

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

View file

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

View file

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

View file

@ -1,7 +1,7 @@
use crate::clients::wayland::{self, ToplevelChange};
use crate::config::CommonConfig;
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 glib::Continue;
use gtk::prelude::*;
@ -49,7 +49,8 @@ impl Module<gtk::Box> for FocusedModule {
) -> Result<()> {
let focused = await_sync(async {
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)
});
@ -72,12 +73,10 @@ impl Module<gtk::Box> for FocusedModule {
};
if update {
tx.send(ModuleUpdateEvent::Update((
event.toplevel.title,
event.toplevel.app_id,
)))
.await
.expect(error::ERR_CHANNEL_SEND);
send_async!(
tx,
ModuleUpdateEvent::Update((event.toplevel.title, event.toplevel.app_id))
);
}
}
});

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,7 +1,7 @@
use crate::clients::sway::{get_client, get_sub_client};
use crate::config::CommonConfig;
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 gtk::prelude::*;
use gtk::Button;
@ -53,7 +53,7 @@ fn create_button(
let tx = tx.clone();
let name = name.to_string();
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)))
.expect(error::ERR_CHANNEL_SEND);
try_send!(
tx,
ModuleUpdateEvent::Update(WorkspaceUpdate::Init(workspaces))
);
// Subscribe & send events
spawn(async move {
@ -106,9 +108,10 @@ impl Module<gtk::Box> for WorkspacesModule {
trace!("Set up Sway workspace subscription");
while let Ok(payload) = srx.recv().await {
tx.send(ModuleUpdateEvent::Update(WorkspaceUpdate::Update(payload)))
.await
.expect(error::ERR_CHANNEL_SEND);
send_async!(
tx,
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::{Report, Result};
use serde::Deserialize;
@ -261,10 +261,10 @@ impl Script {
select! {
_ = handle.wait() => break,
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() => {
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 glib::Continue;
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)) => {
debug!("{event:?}");
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),