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

feat: logging support and proper error handling

This commit is contained in:
Jake Stanger 2022-08-21 23:36:07 +01:00
parent 917838c98c
commit ab8f7ecfc8
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
28 changed files with 1056 additions and 388 deletions

View file

@ -1,13 +1,14 @@
use crate::icon;
use crate::modules::{Module, ModuleInfo};
use crate::sway::node::get_open_windows;
use crate::sway::WindowEvent;
use crate::sway::{SwayClient, WindowEvent};
use color_eyre::Result;
use glib::Continue;
use gtk::prelude::*;
use gtk::{IconTheme, Image, Label, Orientation};
use ksway::{Client, IpcEvent};
use ksway::IpcEvent;
use serde::Deserialize;
use tokio::task::spawn_blocking;
use tracing::error;
#[derive(Debug, Deserialize, Clone)]
pub struct FocusedModule {
@ -26,7 +27,7 @@ const fn default_icon_size() -> i32 {
}
impl Module<gtk::Box> for FocusedModule {
fn into_widget(self, _info: &ModuleInfo) -> gtk::Box {
fn into_widget(self, _info: &ModuleInfo) -> Result<gtk::Box> {
let icon_theme = IconTheme::new();
if let Some(theme) = self.icon_theme {
@ -41,34 +42,42 @@ impl Module<gtk::Box> for FocusedModule {
container.add(&icon);
container.add(&label);
let mut sway = Client::connect().unwrap();
let mut sway = SwayClient::connect()?;
let srx = sway.subscribe(vec![IpcEvent::Window]).unwrap();
let srx = sway.subscribe(vec![IpcEvent::Window])?;
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
let focused = get_open_windows(&mut sway)
let focused = sway
.get_open_windows()?
.into_iter()
.find(|node| node.focused);
if let Some(focused) = focused {
tx.send(focused).unwrap();
tx.send(focused)?;
}
spawn_blocking(move || loop {
while let Ok((_, payload)) = srx.try_recv() {
let payload: WindowEvent = serde_json::from_slice(&payload).unwrap();
match serde_json::from_slice::<WindowEvent>(&payload) {
Ok(payload) => {
let update = match payload.change.as_str() {
"focus" => true,
"title" => payload.container.focused,
_ => false,
};
let update = match payload.change.as_str() {
"focus" => true,
"title" => payload.container.focused,
_ => false,
};
if update {
tx.send(payload.container).unwrap();
if update {
tx.send(payload.container)
.expect("Failed to sendf focus update");
}
}
Err(err) => error!("{:?}", err),
}
}
sway.poll().unwrap();
if let Err(err) = sway.poll() {
error!("{:?}", err);
}
});
{
@ -89,6 +98,6 @@ impl Module<gtk::Box> for FocusedModule {
});
}
container
Ok(container)
}
}