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.

3.9 KB
Raw Blame
//! 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)) {
                        log::info!("dock: the recycle bin is {} now", if full { "full" } else { "empty" });
                        // 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>());
    }
}