Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

The dock's Recycle Bin icon shows the bin filling and emptying (887)

denfryd0dbf1b21567
diff --git a/crates/rf-app/src/dock/bin_feed.rs b/crates/rf-app/src/dock/bin_feed.rs
new file mode 100644
index 00000000..177ff458
--- /dev/null
+++ b/crates/rf-app/src/dock/bin_feed.rs
@@ -0,0 +1,96 @@
+//! Whether the Recycle Bin is empty or full, for the dock's bin icon (issue 887). The shell draws the bin's picture for the state it is
+//! in when asked, and the dock asked once: sending something to the bin or emptying it left the dock's icon as it was until a restart.
+//!
+//! One feed for all the docks of the process: the bin's folders are watched as the desktop does (`rf_win::desktop::binwatch`), the
+//! state is asked of the shell (`SHQueryRecycleBin`) after each change, and a slow check every [`RECHECK_EVERY`] asks again and also
+//! watches a bin folder that did not exist before (a drive's first delete makes it). Islands observe [`BinState`] and ask for the bin's
+//! icon again only when the state turns. READ-ONLY: nothing in the bin is listed, opened or touched.
+
+use std::path::PathBuf;
+use std::time::Duration;
+
+use futures::StreamExt;
+use gpui_kit::*;
+use rf_win::dock::Place;
+
+use super::plan;
+
+const RECHECK_EVERY: Duration = Duration::from_secs(30);
+/// The most the shell is waited for (it is asked on the shell thread, like the dock's icons).
+const SHELL_LIMIT: Duration = Duration::from_secs(5);
+
+/// The bin's state as last asked (`None` until the first answer). Observers are told only when it changes.
+pub(super) struct BinState {
+ pub full: Option<bool>,
+ _feed: Task<()>,
+ _tick: Task<()>,
+}
+
+impl Global for BinState {}
+
+/// The key the bin's icon is cached under.
+pub(super) fn icon_key() -> String {
+ plan::icon_key(&Place::RecycleBin.icon_source())
+}
+
+pub(super) fn start(cx: &mut App) {
+ if cx.has_global::<BinState>() {
+ return;
+ }
+ let (tx, mut pokes) = futures::channel::mpsc::unbounded::<()>();
+ let slow = tx.clone();
+ let tick = cx.spawn(async move |cx| {
+ loop {
+ cx.background_executor().timer(RECHECK_EVERY).await;
+ if slow.unbounded_send(()).is_err() {
+ return;
+ }
+ }
+ });
+ let feed = cx.spawn(async move |cx| {
+ let mut watches: Vec<(PathBuf, rf_win::watch::DirWatch)> = Vec::new();
+ loop {
+ let (dirs, full) = cx
+ .background_executor()
+ .spawn(async move {
+ let full = rf_win::sta::call(SHELL_LIMIT, || rf_win::recycle::info().ok().map(|(n, _)| n > 0)).flatten();
+ (rf_win::desktop::binwatch::existing_bin_dirs(), full)
+ })
+ .await;
+ for dir in dirs {
+ if watches.iter().any(|(d, _)| *d == dir) {
+ continue;
+ }
+ let poke = tx.clone();
+ match rf_win::watch::watch_dir(&dir, Duration::from_millis(300), move || {
+ let _ = poke.unbounded_send(());
+ }) {
+ Ok(w) => watches.push((dir, w)),
+ Err(e) => log::warn!("dock: recycle bin folder not watched: {e}"),
+ }
+ }
+ // (no answer from the shell: the picture is left as it is)
+ if let Some(full) = full {
+ cx.update(|cx| {
+ if cx.try_global::<BinState>().is_some_and(|s| s.full != Some(full)) {
+ // a picture another dock made a moment ago is of the old state
+ super::icons::forget_shared(&icon_key());
+ cx.update_global::<BinState, _>(|s, _| s.full = Some(full));
+ }
+ });
+ }
+ if pokes.next().await.is_none() {
+ return;
+ }
+ // (a delete of many files is one change)
+ while pokes.try_recv().is_ok() {}
+ }
+ });
+ cx.set_global(BinState { full: None, _feed: feed, _tick: tick });
+}
+
+pub(super) fn stop(cx: &mut App) {
+ if cx.has_global::<BinState>() {
+ drop(cx.remove_global::<BinState>());
+ }
+}
diff --git a/crates/rf-app/src/dock/host.rs b/crates/rf-app/src/dock/host.rs
index 31e94c97..9c83b716 100644
--- a/crates/rf-app/src/dock/host.rs
+++ b/crates/rf-app/src/dock/host.rs
@@ -1871,6 +1871,7 @@ pub fn shutdown(cx: &mut App) {
return;
}
super::corners::stop(cx);
+ super::bin_feed::stop(cx);
let mut host = cx.remove_global::<Host>();
// the news the Settings page read about the parts goes with the dock
super::parts_status::stop();
@@ -2065,6 +2066,8 @@ pub fn run(messages: Option<UnboundedReceiver<String>>, cx: &mut App) {
sync_autohide(cx);
// the screen's bottom corners: Start and Show desktop
super::corners::start(cx);
+ // the Recycle Bin's icon follows the bin
+ super::bin_feed::start(cx);
// the thin dock (the setting's switch; nothing runs when it is off)
super::thin::start(cx);
// The other programs' tray icons (nothing at all without a tray source: see tray_source.rs). After the setup job above has been queued:
diff --git a/crates/rf-app/src/dock/icons.rs b/crates/rf-app/src/dock/icons.rs
index d88d1695..61a5247f 100644
--- a/crates/rf-app/src/dock/icons.rs
+++ b/crates/rf-app/src/dock/icons.rs
@@ -123,6 +123,22 @@ impl IconCache {
}
}
+ /// What `key` shows has changed (the Recycle Bin filled or emptied): its picture stays on show until the new one arrives, as in
+ /// [`mark_stale`](Self::mark_stale). False when it has no picture yet: the load on its way, or the next one, asks the shell anyway.
+ pub fn mark_stale_key(&mut self, key: &str) -> bool {
+ match self.map.remove(key) {
+ Some(State::Ready(img) | State::Stale(img) | State::Refreshing { img, .. } | State::RefreshFailed { img, .. }) => {
+ self.map.insert(key.to_string(), State::Stale(img));
+ true
+ }
+ Some(other) => {
+ self.map.insert(key.to_string(), other);
+ false
+ }
+ None => false,
+ }
+ }
+
/// Keeps only the entries whose key `keep` accepts (a cache that must not grow for ever lets go of what is not worth keeping).
pub fn retain(&mut self, keep: impl Fn(&str) -> bool) {
self.map.retain(|k, _| keep(k));
@@ -195,6 +211,12 @@ pub fn load_levels_shared(key: &str, srcs: &[IconSource], sizes: &[u32], shared:
(got, Some(load))
}
+/// Lets go of the loads of `key` that docks still hold for each other ([`SHARE_LINGER`]): what the icon shows has changed, and a dock
+/// that asks now must get a new picture, not the one made a moment ago.
+pub fn forget_shared(key: &str) {
+ LOADS.lock().unwrap_or_else(|p| p.into_inner()).retain(|((k, _), w)| k != key && w.strong_count() > 0);
+}
+
/// The set of pictures a [`load_levels`] gave, ready to draw. UI thread.
pub fn to_levels(bgra: Vec<rf_win::thumbs::Bgra>) -> Option<super::icon_levels::Levels> {
let sizes: Vec<u32> = bgra.iter().map(|b| b.width).collect();
@@ -236,6 +258,31 @@ mod tests {
assert_eq!(left, 0, "released loads are forgotten");
}
+ #[test]
+ fn a_changed_icon_keeps_its_picture_until_the_new_one_is_there() {
+ let mut c = IconCache::default();
+ let t = Instant::now();
+ assert!(!c.mark_stale_key("bin"), "never asked for: nothing to ask again");
+ assert!(c.start_loading("bin", t));
+ assert!(!c.mark_stale_key("bin"), "still loading: that load asks the shell anyway");
+ let img = Arc::new(RenderImage::new(vec![image::Frame::new(image::RgbaImage::new(1, 1))]));
+ c.finish("bin", Some(img), t);
+ assert!(c.start_loading("other", t));
+ assert!(c.mark_stale_key("bin"));
+ assert!(c.get("bin").is_some(), "the old picture is still drawn");
+ assert!(c.start_loading("bin", t), "and the new one is asked for");
+ assert!(!c.start_loading("other", t), "nothing else is");
+ }
+
+ #[test]
+ fn a_forgotten_shared_load_is_not_handed_to_the_next_dock() {
+ let (_, held) = load_levels_shared("test-forget-1", &[], &[44], true);
+ let held = held.expect("held");
+ forget_shared("test-forget-1");
+ let (_, next) = load_levels_shared("test-forget-1", &[], &[44], true);
+ assert!(!Arc::ptr_eq(&held, &next.expect("held")), "a new load, not the lingering one");
+ }
+
#[test]
fn an_icon_is_asked_for_once_at_a_time() {
let mut c = IconCache::default();
diff --git a/crates/rf-app/src/dock/middle.rs b/crates/rf-app/src/dock/middle.rs
index 94a8bdbc..06b33df3 100644
--- a/crates/rf-app/src/dock/middle.rs
+++ b/crates/rf-app/src/dock/middle.rs
@@ -290,6 +290,13 @@ impl MiddleIsland {
}
})
.detach();
+ // the Recycle Bin filled or emptied (issue 887): only the bin's icon is asked for again, and its old picture stays until then
+ cx.observe_global::<super::bin_feed::BinState>(|me: &mut Self, cx| {
+ if me.icons.mark_stale_key(&super::bin_feed::icon_key()) {
+ me.load_icons(cx);
+ }
+ })
+ .detach();
// until the first refresh is done the dock shows what it always has: the two places
me.items = model::merge(&[], Vec::new(), 0);
me.feel_keys = feel_keys(&me.items);
diff --git a/crates/rf-app/src/dock/mod.rs b/crates/rf-app/src/dock/mod.rs
index b5bb349f..dc57fbe6 100644
--- a/crates/rf-app/src/dock/mod.rs
+++ b/crates/rf-app/src/dock/mod.rs
@@ -40,6 +40,7 @@
pub mod actions;
mod app_menu;
mod backdrop;
+mod bin_feed;
mod cal_view;
mod cc_model;
pub mod control;