1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 18:51:04 +02:00

refactor: standardise error messages

This commit is contained in:
Jake Stanger 2022-12-11 21:31:45 +00:00
parent fd2d7e5c7a
commit 9d5049dde0
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
19 changed files with 117 additions and 110 deletions

View file

@ -1,4 +1,5 @@
use crate::config::CommonConfig;
use crate::error;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::popup::Popup;
use chrono::{DateTime, Local};
@ -48,7 +49,7 @@ impl Module<Button> for ClockModule {
let date = Local::now();
tx.send(ModuleUpdateEvent::Update(date))
.await
.expect("Failed to send date");
.expect(error::ERR_CHANNEL_SEND);
sleep(tokio::time::Duration::from_millis(500)).await;
}
});
@ -74,7 +75,7 @@ impl Module<Button> for ClockModule {
button,
orientation,
)))
.expect("Failed to toggle popup");
.expect(error::ERR_CHANNEL_SEND);
});
let format = self.format.clone();

View file

@ -1,5 +1,6 @@
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;
@ -119,8 +120,6 @@ impl Widget {
}
label
// DynamicString::new(label, &text)
}
/// Creates a `gtk::Button` from this widget
@ -150,7 +149,7 @@ impl Widget {
cmd: exec.clone(),
geometry: Popup::button_pos(button, bar_orientation),
})
.expect("Failed to send exec message");
.expect(err::ERR_CHANNEL_SEND);
});
}
@ -191,15 +190,15 @@ impl Module<gtk::Box> for CustomModule {
} else if event.cmd == "popup:toggle" {
tx.send(ModuleUpdateEvent::TogglePopup(event.geometry))
.await
.expect("Failed to send open popup event");
.expect(err::ERR_CHANNEL_SEND);
} else if event.cmd == "popup:open" {
tx.send(ModuleUpdateEvent::OpenPopup(event.geometry))
.await
.expect("Failed to send open popup event");
.expect(err::ERR_CHANNEL_SEND);
} else if event.cmd == "popup:close" {
tx.send(ModuleUpdateEvent::ClosePopup)
.await
.expect("Failed to send open popup event");
.expect(err::ERR_CHANNEL_SEND);
} 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, icon};
use crate::{await_sync, error, icon};
use color_eyre::Result;
use glib::Continue;
use gtk::prelude::*;
@ -49,11 +49,7 @@ impl Module<gtk::Box> for FocusedModule {
) -> Result<()> {
let focused = await_sync(async {
let wl = wayland::get_client().await;
let toplevels = wl
.toplevels
.read()
.expect("Failed to get read lock on toplevels")
.clone();
let toplevels = wl.toplevels.read().expect(error::ERR_READ_LOCK).clone();
toplevels.into_iter().find(|(_, (top, _))| top.active)
});
@ -81,7 +77,7 @@ impl Module<gtk::Box> for FocusedModule {
event.toplevel.app_id,
)))
.await
.expect("Failed to send focus update");
.expect(error::ERR_CHANNEL_SEND);
}
}
});

View file

@ -1,5 +1,6 @@
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;
@ -181,10 +182,10 @@ impl ItemButton {
let style_context = button.style_context();
if style_context.has_class("open") {
tx.try_send(ItemEvent::FocusItem(app_id.clone()))
.expect("Failed to send item focus event");
.expect(error::ERR_CHANNEL_SEND);
} else {
tx.try_send(ItemEvent::OpenItem(app_id.clone()))
.expect("Failed to send item open event");
.expect(error::ERR_CHANNEL_SEND);
}
});
}
@ -199,24 +200,22 @@ impl ItemButton {
let menu_state = menu_state.clone();
button.connect_enter_notify_event(move |button, _| {
let menu_state = menu_state
.read()
.expect("Failed to get read lock on item menu state");
let menu_state = menu_state.read().expect(error::ERR_READ_LOCK);
if menu_state.num_windows > 1 {
tx.try_send(ModuleUpdateEvent::Update(LauncherUpdate::Hover(
app_id.clone(),
)))
.expect("Failed to send item open popup event");
.expect(error::ERR_CHANNEL_SEND);
tx.try_send(ModuleUpdateEvent::OpenPopup(Popup::button_pos(
button,
orientation,
)))
.expect("Failed to send item open popup event");
.expect(error::ERR_CHANNEL_SEND);
} else {
tx.try_send(ModuleUpdateEvent::ClosePopup)
.expect("Failed to send item close popup event");
.expect(error::ERR_CHANNEL_SEND);
}
Inhibit(false)

View file

@ -5,6 +5,7 @@ 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 color_eyre::{Help, Report};
@ -110,12 +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("Failed to get read lock on toplevels");
let open_windows = wl.toplevels.read().expect(err::ERR_READ_LOCK);
let mut items = items.lock().expect("Failed to get lock on items");
let mut items = items.lock().expect(err::ERR_MUTEX_LOCK);
for (_, (window, _)) in open_windows.clone() {
let item = items.get_mut(&window.app_id);
@ -157,7 +155,7 @@ impl Module<gtk::Box> for LauncherModule {
let window = event.toplevel;
let app_id = window.app_id.clone();
let items = || items.lock().expect("Failed to get lock on items");
let items = || items.lock().expect(err::ERR_MUTEX_LOCK);
match event.change {
ToplevelChange::New => {
@ -218,6 +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) {
item.set_window_focused(window.id, true);
@ -269,6 +268,7 @@ impl Module<gtk::Box> for LauncherModule {
if let Err(err) = Command::new("gtk-launch")
.arg(
file.file_name()
// TODO: Don't panic for this
.expect("File segment missing from path to desktop file"),
)
.stdout(Stdio::null())
@ -286,7 +286,7 @@ impl Module<gtk::Box> for LauncherModule {
);
} else {
let wl = wayland::get_client().await;
let items = items.lock().expect("Failed to get lock on items");
let items = items.lock().expect(err::ERR_MUTEX_LOCK);
let id = match event {
ItemEvent::FocusItem(app_id) => items
@ -297,10 +297,7 @@ impl Module<gtk::Box> for LauncherModule {
};
if let Some(id) = id {
let toplevels = wl
.toplevels
.read()
.expect("Failed to get read lock on toplevels");
let toplevels = wl.toplevels.read().expect(err::ERR_READ_LOCK);
let seat = wl.seats.first().expect("Failed to get Wayland seat");
if let Some((_top, handle)) = toplevels.get(&id) {
handle.activate(seat);
@ -363,10 +360,8 @@ 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("Failed to get write lock on item menu state");
let mut menu_state =
button.menu_state.write().expect(err::ERR_WRITE_LOCK);
menu_state.num_windows += 1;
}
}
@ -387,10 +382,8 @@ 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("Failed to get write lock on item menu state");
let mut menu_state =
button.menu_state.write().expect(err::ERR_WRITE_LOCK);
menu_state.num_windows -= 1;
}
}
@ -464,7 +457,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("Failed to send window click event");
.expect(err::ERR_CHANNEL_SEND);
if let Some(win) = button.window() {
win.hide();
@ -494,7 +487,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("Failed to send window click event");
.expect(err::ERR_CHANNEL_SEND);
if let Some(win) = button.window() {
win.hide();

View file

@ -1,5 +1,6 @@
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 color_eyre::Result;
@ -229,7 +230,7 @@ impl Module<Button> for MpdModule {
button,
orientation,
)))
.expect("Failed to send MPD popup open event");
.expect(err::ERR_CHANNEL_SEND);
});
}
@ -244,7 +245,7 @@ impl Module<Button> for MpdModule {
} else {
button.hide();
tx.try_send(ModuleUpdateEvent::ClosePopup)
.expect("Failed to send close popup message");
.expect(err::ERR_CHANNEL_SEND);
}
Continue(true)
@ -324,28 +325,28 @@ impl Module<Button> for MpdModule {
btn_prev.connect_clicked(move |_| {
tx_prev
.try_send(PlayerCommand::Previous)
.expect("Failed to send prev track message");
.expect(err::ERR_CHANNEL_SEND);
});
let tx_toggle = tx.clone();
btn_play_pause.connect_clicked(move |_| {
tx_toggle
.try_send(PlayerCommand::Toggle)
.expect("Failed to send play/pause track message");
.expect(err::ERR_CHANNEL_SEND);
});
let tx_next = tx.clone();
btn_next.connect_clicked(move |_| {
tx_next
.try_send(PlayerCommand::Next)
.expect("Failed to send next track message");
.expect(err::ERR_CHANNEL_SEND);
});
let tx_vol = tx;
volume_slider.connect_change_value(move |_, _, val| {
tx_vol
.try_send(PlayerCommand::Volume(val as u8))
.expect("Failed to send volume message");
.expect(err::ERR_CHANNEL_SEND);
Inhibit(false)
});

View file

@ -1,4 +1,5 @@
use crate::config::CommonConfig;
use crate::error;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use color_eyre::Result;
use gtk::prelude::*;
@ -146,7 +147,7 @@ impl Module<gtk::Box> for SysInfoModule {
loop {
tx.send(RefreshType::Memory)
.await
.expect("Failed to send memory refresh");
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.memory())).await;
}
});
@ -159,7 +160,7 @@ impl Module<gtk::Box> for SysInfoModule {
loop {
tx.send(RefreshType::Cpu)
.await
.expect("Failed to send cpu refresh");
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.cpu())).await;
}
});
@ -172,7 +173,7 @@ impl Module<gtk::Box> for SysInfoModule {
loop {
tx.send(RefreshType::Temps)
.await
.expect("Failed to send temperature refresh");
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.temps())).await;
}
});
@ -185,7 +186,7 @@ impl Module<gtk::Box> for SysInfoModule {
loop {
tx.send(RefreshType::Disks)
.await
.expect("Failed to send disk refresh");
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.disks())).await;
}
});
@ -198,7 +199,7 @@ impl Module<gtk::Box> for SysInfoModule {
loop {
tx.send(RefreshType::Network)
.await
.expect("Failed to send network refresh");
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.networks())).await;
}
});
@ -211,7 +212,7 @@ impl Module<gtk::Box> for SysInfoModule {
loop {
tx.send(RefreshType::System)
.await
.expect("Failed to send system refresh");
.expect(error::ERR_CHANNEL_SEND);
sleep(Duration::from_secs(interval.system())).await;
}
});
@ -234,7 +235,7 @@ impl Module<gtk::Box> for SysInfoModule {
tx.send(ModuleUpdateEvent::Update(format_info.clone()))
.await
.expect("Failed to send system info map");
.expect(error::ERR_CHANNEL_SEND);
}
});

View file

@ -1,7 +1,7 @@
use crate::await_sync;
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 color_eyre::Result;
use gtk::prelude::*;
use gtk::{IconLookupFlags, IconTheme, Image, Menu, MenuBar, MenuItem, SeparatorMenuItem};
@ -75,7 +75,7 @@ fn get_menu_items(
menu_path: path.clone(),
notifier_address: id.clone(),
})
.expect("Failed to send menu item clicked event");
.expect(error::ERR_CHANNEL_SEND);
});
}

View file

@ -1,7 +1,7 @@
use crate::await_sync;
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 color_eyre::{Report, Result};
use gtk::prelude::*;
use gtk::Button;
@ -53,8 +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("Failed to send workspace click event");
tx.try_send(name.clone()).expect(error::ERR_CHANNEL_SEND);
});
}
@ -95,7 +94,7 @@ impl Module<gtk::Box> for WorkspacesModule {
};
tx.try_send(ModuleUpdateEvent::Update(WorkspaceUpdate::Init(workspaces)))
.expect("Failed to send initial workspace list");
.expect(error::ERR_CHANNEL_SEND);
// Subscribe & send events
spawn(async move {
@ -109,7 +108,7 @@ impl Module<gtk::Box> for WorkspacesModule {
while let Ok(payload) = srx.recv().await {
tx.send(ModuleUpdateEvent::Update(WorkspaceUpdate::Update(payload)))
.await
.expect("Failed to send workspace update");
.expect(error::ERR_CHANNEL_SEND);
}
});