1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 18:51:04 +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

@ -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))
);
}
});