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.

2.5 KB
Raw Blame
//! The screen's bottom corners (issue 313): bottom left does what the Start button does, bottom right shows the desktop.
//!
//! The windows and the rules are `rf_win::dock::corners`; here they are kept up to date and their clicks are carried out. The dock's
//! changes call [`refresh`]; a slow watch calls it too, for the hide of Windows' taskbar taking force, which nothing announces.

use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use futures::StreamExt;
use gpui_kit::*;
use rf_win::dock::corners::Corner;

use super::host::{self, MonId};

const WATCH_EVERY: Duration = Duration::from_secs(2);

/// False from [`stop`] on: an update still waiting in the message loop must not make the windows again.
static ON: AtomicBool = AtomicBool::new(false);

/// The two tasks live as long as this.
struct Watch {
    _clicks: Task<()>,
    _slow: Task<()>,
}

impl Global for Watch {}

pub(super) fn start(cx: &mut App) {
    let (tx, mut clicks) = futures::channel::mpsc::unbounded::<(u32, Corner)>();
    // (called from a window procedure: it only passes the news on)
    rf_win::dock::corners::on_click(move |mon, corner| {
        let _ = tx.unbounded_send((mon, corner));
    });
    let heard = cx.spawn(async move |cx| {
        while let Some((mon, corner)) = clicks.next().await {
            cx.update(|cx| act(MonId(mon), corner, cx));
        }
    });
    let slow = cx.spawn(async move |cx| {
        loop {
            cx.background_executor().timer(WATCH_EVERY).await;
            cx.update(refresh);
        }
    });
    ON.store(true, Ordering::Relaxed);
    cx.set_global(Watch { _clicks: heard, _slow: slow });
    refresh(cx);
}

/// Makes the corner windows what the docks want now. Safe before [`start`] and after [`stop`].
pub(super) fn refresh(cx: &mut App) {
    if !cx.has_global::<Watch>() {
        return;
    }
    let want = host::corner_monitors(cx);
    // (window work from the message loop, not from inside this update)
    host::on_message_loop(cx, move || {
        if ON.load(Ordering::Relaxed) {
            rf_win::dock::corners::sync(&want);
        }
    });
}

fn act(mon: MonId, corner: Corner, cx: &mut App) {
    match corner {
        Corner::Start => {
            host::bring_up(mon, cx);
            super::left::press_start(mon, cx);
        }
        Corner::Desktop => crate::shell::keylayer::toggle_show_desktop(),
    }
}

pub(super) fn stop(cx: &mut App) {
    ON.store(false, Ordering::Relaxed);
    if cx.has_global::<Watch>() {
        drop(cx.remove_global::<Watch>());
    }
    rf_win::dock::corners::clear();
}