1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-02 03:01: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,3 +1,4 @@
use color_eyre::{Help, Report};
use glib::Continue;
use gtk::prelude::CssProviderExt;
use gtk::{gdk, gio, CssProvider, StyleContext};
@ -6,40 +7,56 @@ use std::path::PathBuf;
use std::sync::mpsc;
use std::time::Duration;
use tokio::spawn;
use tracing::{error, info};
pub fn load_css(style_path: PathBuf) {
let provider = CssProvider::new();
provider
.load_from_file(&gio::File::for_path(&style_path))
.expect("Couldn't load custom style");
StyleContext::add_provider_for_screen(
&gdk::Screen::default().expect("Couldn't get default GDK screen"),
&provider,
800,
);
if let Err(err) = provider.load_from_file(&gio::File::for_path(&style_path)) {
error!("{:?}", Report::new(err)
.wrap_err("Failed to load CSS")
.suggestion("Check the CSS file for errors")
.suggestion("GTK CSS uses a subset of the full CSS spec and many properties are not available. Ensure you are not using any unsupported property.")
);
}
let screen = gdk::Screen::default().expect("Failed to get default GTK screen");
StyleContext::add_provider_for_screen(&screen, &provider, 800);
let (watcher_tx, watcher_rx) = mpsc::channel::<DebouncedEvent>();
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
spawn(async move {
let mut watcher = notify::watcher(watcher_tx, Duration::from_millis(500)).unwrap();
watcher
.watch(&style_path, RecursiveMode::NonRecursive)
.unwrap();
match notify::watcher(watcher_tx, Duration::from_millis(500)) {
Ok(mut watcher) => {
watcher
.watch(&style_path, RecursiveMode::NonRecursive)
.expect("Unexpected error when attempting to watch CSS");
loop {
if let Ok(DebouncedEvent::Write(path)) = watcher_rx.recv() {
tx.send(path).unwrap();
loop {
if let Ok(DebouncedEvent::Write(path)) = watcher_rx.recv() {
tx.send(path).expect("Failed to send style changed message");
}
}
}
Err(err) => error!(
"{:?}",
Report::new(err).wrap_err("Failed to start CSS watcher")
),
}
});
{
rx.attach(None, move |path| {
println!("Reloading CSS");
provider
.load_from_file(&gio::File::for_path(path))
.expect("Couldn't load custom style");
info!("Reloading CSS");
if let Err(err) = provider
.load_from_file(&gio::File::for_path(path)) {
error!("{:?}", Report::new(err)
.wrap_err("Failed to load CSS")
.suggestion("Check the CSS file for errors")
.suggestion("GTK CSS uses a subset of the full CSS spec and many properties are not available. Ensure you are not using any unsupported property.")
);
}
Continue(true)
});