| 
									
										
										
										
											2022-08-21 23:36:07 +01:00
										 |  |  | use color_eyre::{Help, Report};
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  | use glib::Continue;
 | 
					
						
							|  |  |  | use gtk::prelude::CssProviderExt;
 | 
					
						
							|  |  |  | use gtk::{gdk, gio, CssProvider, StyleContext};
 | 
					
						
							| 
									
										
										
										
											2022-09-07 22:47:47 +01:00
										 |  |  | use notify::{Event, RecursiveMode, Result, Watcher};
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  | use std::path::PathBuf;
 | 
					
						
							|  |  |  | use tokio::spawn;
 | 
					
						
							| 
									
										
										
										
											2022-08-21 23:36:07 +01:00
										 |  |  | use tracing::{error, info};
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 16:57:41 +01:00
										 |  |  | /// Attempts to load CSS file at the given path
 | 
					
						
							|  |  |  | /// and attach if to the current GTK application.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// Installs a file watcher and reloads CSS when
 | 
					
						
							|  |  |  | /// write changes are detected on the file.
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  | pub fn load_css(style_path: PathBuf) {
 | 
					
						
							|  |  |  |     let provider = CssProvider::new();
 | 
					
						
							| 
									
										
										
										
											2022-08-21 23:36:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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);
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     spawn(async move {
 | 
					
						
							| 
									
										
										
										
											2022-09-07 22:47:47 +01:00
										 |  |  |         match notify::recommended_watcher(move |res: Result<Event>| match res {
 | 
					
						
							|  |  |  |             Ok(event) => {
 | 
					
						
							|  |  |  |                 if let Some(path) = event.paths.first() {
 | 
					
						
							|  |  |  |                     tx.send(path.clone())
 | 
					
						
							|  |  |  |                         .expect("Failed to send style changed message");
 | 
					
						
							|  |  |  |                 }
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             Err(e) => error!("Error occurred when watching stylesheet: {:?}", e),
 | 
					
						
							|  |  |  |         }) {
 | 
					
						
							| 
									
										
										
										
											2022-08-21 23:36:07 +01:00
										 |  |  |             Ok(mut watcher) => {
 | 
					
						
							|  |  |  |                 watcher
 | 
					
						
							|  |  |  |                     .watch(&style_path, RecursiveMode::NonRecursive)
 | 
					
						
							|  |  |  |                     .expect("Unexpected error when attempting to watch CSS");
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-08-21 23:36:07 +01:00
										 |  |  |             Err(err) => error!(
 | 
					
						
							|  |  |  |                 "{:?}",
 | 
					
						
							|  |  |  |                 Report::new(err).wrap_err("Failed to start CSS watcher")
 | 
					
						
							|  |  |  |             ),
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  |         }
 | 
					
						
							|  |  |  |     });
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     {
 | 
					
						
							|  |  |  |         rx.attach(None, move |path| {
 | 
					
						
							| 
									
										
										
										
											2022-08-21 23:36:07 +01:00
										 |  |  |             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.")
 | 
					
						
							|  |  |  |                 );
 | 
					
						
							|  |  |  |             }
 | 
					
						
							| 
									
										
										
										
											2022-08-14 14:30:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             Continue(true)
 | 
					
						
							|  |  |  |         });
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | }
 |