Merge pull request #336 from JakeStanger/fix/vim-style-watcher

Fix style hot reloading when editing with Vim
This commit is contained in:
Jake Stanger 2023-10-17 20:38:14 +01:00 committed by GitHub
commit 8fc516a85d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 6 deletions

View File

@ -180,7 +180,7 @@ fn parse_desktop_file(path: &Path) -> Option<DesktopFile> {
.for_each(|(key, value)| { .for_each(|(key, value)| {
desktop_file desktop_file
.entry(key.to_string()) .entry(key.to_string())
.or_insert_with(Vec::new) .or_default()
.push(value.to_string()); .push(value.to_string());
}); });

View File

@ -4,7 +4,7 @@ use glib::Continue;
use gtk::ffi::GTK_STYLE_PROVIDER_PRIORITY_USER; use gtk::ffi::GTK_STYLE_PROVIDER_PRIORITY_USER;
use gtk::prelude::CssProviderExt; use gtk::prelude::CssProviderExt;
use gtk::{gdk, gio, CssProvider, StyleContext}; use gtk::{gdk, gio, CssProvider, StyleContext};
use notify::event::{DataChange, ModifyKind}; use notify::event::ModifyKind;
use notify::{recommended_watcher, Event, EventKind, RecursiveMode, Result, Watcher}; use notify::{recommended_watcher, Event, EventKind, RecursiveMode, Result, Watcher};
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Duration; use std::time::Duration;
@ -39,11 +39,17 @@ pub fn load_css(style_path: PathBuf) {
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT); let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);
spawn(async move { spawn(async move {
let style_path2 = style_path.clone();
let mut watcher = recommended_watcher(move |res: Result<Event>| match res { let mut watcher = recommended_watcher(move |res: Result<Event>| match res {
Ok(event) if event.kind == EventKind::Modify(ModifyKind::Data(DataChange::Any)) => { Ok(event) if matches!(event.kind, EventKind::Modify(ModifyKind::Data(_))) => {
debug!("{event:?}"); debug!("{event:?}");
if let Some(path) = event.paths.first() { if event
send!(tx, path.clone()); .paths
.first()
.map(|p| p == &style_path2)
.unwrap_or_default()
{
send!(tx, style_path2.clone());
} }
} }
Err(e) => error!("Error occurred when watching stylesheet: {:?}", e), Err(e) => error!("Error occurred when watching stylesheet: {:?}", e),
@ -51,8 +57,10 @@ pub fn load_css(style_path: PathBuf) {
}) })
.expect("Failed to create CSS file watcher"); .expect("Failed to create CSS file watcher");
let dir_path = style_path.parent().expect("to exist");
watcher watcher
.watch(&style_path, RecursiveMode::NonRecursive) .watch(dir_path, RecursiveMode::NonRecursive)
.expect("Failed to start CSS file watcher"); .expect("Failed to start CSS file watcher");
debug!("Installed CSS file watcher on '{}'", style_path.display()); debug!("Installed CSS file watcher on '{}'", style_path.display());