From 007a4cf95c1972dccf154b1cea605a2f4a0361f1 Mon Sep 17 00:00:00 2001 From: Joshua Megnauth <48846352+joshuamegnauth54@users.noreply.github.com> Date: Sun, 3 Aug 2025 08:37:20 -0400 Subject: [PATCH] Fix trash watcher regression (#1122) Recursively watching the trash directories can be slow, but not watching at least the "files" folder (e.g. .Trash-1000/files) leads to missed events. I also simplified the events parsing. --- src/app.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/app.rs b/src/app.rs index f9b996c..b405eea 100644 --- a/src/app.rs +++ b/src/app.rs @@ -5957,21 +5957,20 @@ impl Application for App { ), Subscription::run_with_id( TypeId::of::(), - stream::channel(25, |mut output| async move { + stream::channel(1, |mut output| async move { let watcher_res = new_debouncer( time::Duration::from_millis(250), Some(time::Duration::from_millis(250)), move |event_res: notify_debouncer_full::DebounceEventResult| match event_res { - Ok(mut events) => { - events.retain(|event| { - matches!( - event.kind, - notify::EventKind::Create(_) | notify::EventKind::Remove(_) - ) - }); + Ok(events) => { + // Rescan on any event. We don't need to evaluate each event + // because as long as the trash changed in any way we need to + // rescan. + let should_rescan = + events.iter().any(|event| !event.kind.is_access()); - if !events.is_empty() { + if should_rescan { if let Err(e) = futures::executor::block_on(async { output.send(Message::RescanTrash).await }) { @@ -5994,7 +5993,14 @@ impl Application for App { ))] match (watcher_res, trash::os_limited::trash_folders()) { (Ok(mut watcher), Ok(trash_bins)) => { - for path in trash_bins { + // Watch the "bins" themselves as well as the files folder where + // trashed items are placed. This allows us to avoid recursively + // watching the trash which is slow but also properly get events. + let trash_paths = trash_bins + .into_iter() + .map(|path| [path.join("files"), path]) + .flatten(); + for path in trash_paths { if let Err(e) = watcher .watcher() .watch(&path, notify::RecursiveMode::NonRecursive)