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.
Rust archive-managerdesktop-appdual-paneexplorer-alternativefile-browserfile-explorerfile-managerfluent-designgpu-acceleratedgpuimacosmicaproductivityquick-lookrustrust-guitabswindowswindows-11windows-explorer
//! Shared sidebar/home data: drives and pinned folders, loaded off the UI thread.
use std::path::PathBuf;
use gpui_kit::assets::IconName as L;
use gpui_kit::*;
use rf_win::{Drive, DriveKind, KnownFolder};
/// The pictures a folder of the Drives section can have instead of the folder's (issue 408): key, picture, name.
pub const DRIVE_PLACE_ICONS: [(&str, gpui_kit::assets::IconName, &str); 11] = {
use gpui_kit::assets::IconName as L;
[
("folder", L::Folder, "Klasör"),
("drive", L::HardDrive, "Disk"),
("server", L::Server, "Sunucu"),
("cloud", L::Cloud, "Bulut"),
("usb", L::Usb, "USB"),
("music", L::Music, "Müzik"),
("image", L::Image, "Resimler"),
("camera", L::Camera, "Kamera"),
("code", L::Code, "Kod"),
("home", L::House, "Ev"),
("star", L::Star, "Yıldız"),
]
};
/// The picture chosen for a folder of the Drives section, when one is.
pub fn drive_place_icon(path: &std::path::Path, cx: &gpui_kit::App) -> Option<gpui_kit::assets::IconName> {
let key = cx.global::<crate::settings::Settings>().drive_place_icons.iter().find(|(p, _)| p == path).map(|(_, k)| k.clone())?;
DRIVE_PLACE_ICONS.iter().find(|(k, _, _)| *k == key).map(|(_, icon, _)| *icon)
}
/// Gives a folder of the Drives section the picture `key` ("folder": its own again).
pub fn set_drive_place_icon(path: std::path::PathBuf, key: &'static str, cx: &mut gpui_kit::App) {
crate::settings::update(cx, move |s| {
s.drive_place_icons.retain(|(p, _)| *p != path);
if key != "folder" {
s.drive_place_icons.push((path, key.to_string()));
}
});
}
/// Drive display name with the generic fallback labels translated ("Yerel Disk (C:)").
/// The names pinned folders are shown with: a name two or more of them share gets its parent folder after it ("work (Clients)",
/// "work (Projects)"), so that each says where it is (a Discord report: two pins named "work" could not be told apart).
pub fn pin_labels(pins: &[(String, &std::path::Path)]) -> Vec<String> {
pins.iter()
.map(|(title, path)| {
let shared = pins.iter().filter(|(t, _)| t == title).count() > 1;
match path.parent().and_then(|p| p.file_name()).filter(|_| shared) {
Some(parent) => format!("{title} ({})", parent.to_string_lossy()),
None => title.clone(),
}
})
.collect()
}
#[cfg(test)]
mod pin_label_tests {
use std::path::Path;
#[test]
fn only_names_shared_by_two_pins_get_their_parent() {
let pins = [("work".to_string(), Path::new(r"C:\Clients\work")), ("work".to_string(), Path::new(r"D:\Projects\work")), ("Music".to_string(), Path::new(r"C:\Users\a\Music"))];
assert_eq!(super::pin_labels(&pins), ["work (Clients)", "work (Projects)", "Music"]);
// a drive's root has no parent to name
let roots = [("D:".to_string(), Path::new(r"D:\")), ("D:".to_string(), Path::new(r"D:\"))];
assert_eq!(super::pin_labels(&roots), ["D:", "D:"]);
}
}
pub fn drive_name(d: &Drive) -> String {
if d.label.is_empty() {
let kind = match d.kind {
DriveKind::Removable => crate::t!("USB Sürücü"),
DriveKind::Network => crate::t!("Ağ Sürücüsü"),
DriveKind::Optical => crate::t!("DVD Sürücü"),
_ => crate::t!("Yerel Disk"),
};
format!("{kind} ({}:)", d.letter)
} else {
d.display_name()
}
}
/// Freedesktop icon names for a drive, tried in order. Drives are locations, not folders.
pub fn drive_icon_names(kind: DriveKind) -> &'static [&'static str] {
match kind {
DriveKind::Removable => &["drive-removable-media", "drive-harddisk-usb", "drive-harddisk"],
DriveKind::Network => &["network-server", "folder-remote"],
DriveKind::Optical => &["drive-optical", "drive-cdrom"],
DriveKind::Fixed | DriveKind::RamDisk | DriveKind::Unknown => &["drive-harddisk", "drive-harddisk-system"],
}
}
#[derive(Clone)]
pub struct Pinned {
pub title: String,
pub path: PathBuf,
pub icon: L,
}
#[derive(Clone)]
pub struct Recent {
pub name: String,
pub target: PathBuf,
pub used_ms: i64,
pub is_dir: bool,
}
/// Adds `path` to the sidebar's Drives section, or takes it out when it is there (issue 408).
pub fn toggle_drive_place(path: PathBuf, cx: &mut gpui_kit::App) {
crate::settings::update(cx, |s| {
if let Some(i) = s.drive_places.iter().position(|p| *p == path) {
s.drive_places.remove(i);
} else {
s.drive_places.push(path);
}
});
}
/// How many recent items Home lists (14 until 1.0.2; the user: longer).
const RECENT_SHOWN: usize = 30;
/// The recent list of Home: Windows' own recent items, the files that came into Downloads lately, and what rustyfiles copied onto this
/// PC (issue 421: "show also files you've recently downloaded or copied to the PC, like many Android file managers do"), newest first.
fn load_recent(limit: usize) -> Vec<Recent> {
let mut all = windows_recent(limit);
if RECENT_ARRIVALS.load(std::sync::atomic::Ordering::Relaxed) {
all.extend(recent_downloads(limit));
all.extend(arrivals());
}
merge_recent(all, limit)
}
/// Settings › "Downloaded and copied files in Recent" (issue 563: a folder of pictures copied lately filled the list although none
/// was opened). Off: the list is Windows' own recent items only. Set from the settings (`apply_side_effects`).
pub static RECENT_ARRIVALS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(true);
/// Newest first, each place once (the newest of its times), at most `limit`. Pure.
fn merge_recent(mut all: Vec<Recent>, limit: usize) -> Vec<Recent> {
all.sort_by(|a, b| b.used_ms.cmp(&a.used_ms));
let mut out: Vec<Recent> = Vec::with_capacity(limit);
for r in all {
if !out.iter().any(|o| o.target == r.target) {
out.push(r);
}
if out.len() >= limit {
break;
}
}
out
}
/// How far back a download or an arrival still counts as recent.
const ARRIVAL_DAYS: i64 = 14;
fn ms_of(t: std::time::SystemTime) -> i64 {
t.duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0)
}
/// The files that came into Downloads in the last [`ARRIVAL_DAYS`] days (by when they were made there), newest first. A download
/// still being written (.crdownload, .part, .tmp) is left out.
fn recent_downloads(limit: usize) -> Vec<Recent> {
let Some(dir) = rf_win::known::KnownFolder::Downloads.path() else { return Vec::new() };
let since = ms_of(std::time::SystemTime::now()) - ARRIVAL_DAYS * 86_400_000;
let mut out = arrived_in(&dir, since);
out.sort_by(|a, b| b.used_ms.cmp(&a.used_ms));
out.truncate(limit);
out
}
/// How many entries of one folder are looked at, at most (a Downloads folder of thousands of files must not slow Home down).
const DOWNLOADS_LOOKED_AT: usize = 2000;
/// The files made in `dir` since `since` (ms), and in its folders ONE level down whose contents changed since then.
///
/// WHY THE FOLDERS (issue 421, on 1.0.3: "I was sending the files from phone to a folder in C:\Users\User\Downloads\Nectarlink"):
/// many programs put what they receive in a folder of their own inside Downloads (a phone transfer app, Telegram Desktop, a browser
/// set to ask). A folder whose contents changed has a new modification time, so only those are opened, and only one level down.
fn arrived_in(dir: &std::path::Path, since: i64) -> Vec<Recent> {
let mut out = Vec::new();
let Ok(rd) = std::fs::read_dir(dir) else { return out };
let mut folders = Vec::new();
for e in rd.flatten().take(DOWNLOADS_LOOKED_AT) {
let Ok(meta) = e.metadata() else { continue };
if meta.is_dir() {
// (a link or a junction is not followed: it can lead anywhere)
if !meta.file_type().is_symlink() && meta.modified().is_ok_and(|t| ms_of(t) >= since) {
folders.push(e.path());
}
continue;
}
if let Some(r) = arrived_file(&e, &meta, since) {
out.push(r);
}
}
for folder in folders {
let Ok(rd) = std::fs::read_dir(&folder) else { continue };
for e in rd.flatten().take(DOWNLOADS_LOOKED_AT) {
let Ok(meta) = e.metadata() else { continue };
if !meta.is_dir()
&& let Some(r) = arrived_file(&e, &meta, since)
{
out.push(r);
}
}
}
out
}
/// A file of Downloads as a recent item, when it was made there since `since` and is not a download still being written.
fn arrived_file(e: &std::fs::DirEntry, meta: &std::fs::Metadata, since: i64) -> Option<Recent> {
let name = e.file_name().to_string_lossy().into_owned();
let ext = name.rsplit_once('.').map(|(_, x)| x.to_ascii_lowercase()).unwrap_or_default();
if matches!(ext.as_str(), "crdownload" | "part" | "tmp" | "partial" | "download") || name.eq_ignore_ascii_case("desktop.ini") {
return None;
}
let at = ms_of(meta.created().or_else(|_| meta.modified()).ok()?);
(at >= since).then(|| Recent { name, target: e.path(), used_ms: at, is_dir: false })
}
#[cfg(test)]
mod arrived_tests {
#[test]
fn files_in_downloads_and_in_its_folders_one_level_down_count_and_deeper_ones_do_not() {
let root = std::env::temp_dir().join(format!("rf-arrived-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(root.join("Nectarlink").join("deeper")).unwrap();
std::fs::write(root.join("a.pdf"), b"x").unwrap();
std::fs::write(root.join("b.crdownload"), b"x").unwrap();
std::fs::write(root.join("Nectarlink").join("photo.jpg"), b"x").unwrap();
std::fs::write(root.join("Nectarlink").join("deeper").join("far.jpg"), b"x").unwrap();
let mut names: Vec<String> = super::arrived_in(&root, 0).into_iter().map(|r| r.name).collect();
names.sort();
assert_eq!(names, ["a.pdf", "photo.jpg"], "a download being written and a file two levels down are not counted");
// nothing made since a moment in the future
assert!(super::arrived_in(&root, i64::MAX).is_empty());
let _ = std::fs::remove_dir_all(&root);
}
}
/// What rustyfiles copied onto this PC lately: the items that landed at the top of a copy's destination (`note_arrivals`), kept in
/// `arrivals.json` of the settings folder, at most [`ARRIVALS_KEPT`].
static ARRIVALS: std::sync::Mutex<Option<Vec<(PathBuf, i64)>>> = std::sync::Mutex::new(None);
const ARRIVALS_KEPT: usize = 60;
fn arrivals_file() -> Option<PathBuf> {
crate::settings::app_dir().map(|d| d.join("arrivals.json"))
}
fn with_arrivals<T>(f: impl FnOnce(&mut Vec<(PathBuf, i64)>) -> T) -> T {
let mut g = ARRIVALS.lock().unwrap_or_else(|e| e.into_inner());
let list = g.get_or_insert_with(|| {
arrivals_file().and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str::<Vec<(PathBuf, i64)>>(&t).ok()).unwrap_or_default()
});
f(list)
}
/// Notes items a copy brought onto this PC (from another drive, a phone, a share), for Home's recent list. Called by the copy when it
/// has finished; the list is written at once (it is a few lines).
pub fn note_arrivals(items: &[PathBuf]) {
if items.is_empty() {
return;
}
let now = ms_of(std::time::SystemTime::now());
let text = with_arrivals(|list| {
list.retain(|(p, _)| !items.contains(p));
list.extend(items.iter().map(|p| (p.clone(), now)));
list.sort_by(|a, b| b.1.cmp(&a.1));
list.truncate(ARRIVALS_KEPT);
serde_json::to_string(list).ok()
});
if let (Some(t), Some(p)) = (text, arrivals_file()) {
let _ = std::fs::write(p, t);
}
}
/// The noted arrivals still on the disk and still recent.
fn arrivals() -> Vec<Recent> {
let since = ms_of(std::time::SystemTime::now()) - ARRIVAL_DAYS * 86_400_000;
let list = with_arrivals(|l| l.clone());
list.into_iter()
.filter(|(_, at)| *at >= since)
.filter_map(|(p, at)| {
let meta = std::fs::metadata(&p).ok()?;
Some(Recent { name: p.file_name()?.to_string_lossy().into_owned(), target: p, used_ms: at, is_dir: meta.is_dir() })
})
.collect()
}
/// Windows' own "recent items" (%APPDATA%\Microsoft\Windows\Recent\*.lnk), newest first.
fn windows_recent(limit: usize) -> Vec<Recent> {
let Some(dir) = dirs::data_dir().map(|d| d.join("Microsoft").join("Windows").join("Recent")) else {
return Vec::new();
};
let Ok(rd) = std::fs::read_dir(&dir) else { return Vec::new() };
let mut links: Vec<(std::time::SystemTime, PathBuf)> = rd
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|x| x.eq_ignore_ascii_case("lnk")))
.filter_map(|e| Some((e.metadata().ok()?.modified().ok()?, e.path())))
.collect();
links.sort_by(|a, b| b.0.cmp(&a.0));
let mut out: Vec<Recent> = Vec::new();
for (t, link) in links.into_iter().take(limit * 3) {
let Ok(info) = rf_win::shortcut::resolve(&link) else { continue };
if info.target.as_os_str().is_empty() || out.iter().any(|r| r.target == info.target) {
continue;
}
let Ok(meta) = std::fs::metadata(&info.target) else { continue };
out.push(Recent {
// a drive's root has no file name (a terminal or a program opened on "D:\" puts it here): it is shown by its path, not
// as an empty row (issue 623)
name: info.target.file_name().map_or_else(|| info.target.display().to_string(), |n| n.to_string_lossy().into_owned()),
target: info.target,
used_ms: t.duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0),
is_dir: meta.is_dir(),
});
if out.len() >= limit {
break;
}
}
out
}
/// The two lists show the same files at the same times: nothing to draw again.
fn same_recent(a: &[Recent], b: &[Recent]) -> bool {
a.len() == b.len() && a.iter().zip(b).all(|(x, y)| x.target == y.target && x.used_ms == y.used_ms && x.is_dir == y.is_dir)
}
/// Whether two paths name the same folder, whatever the letters' case and a trailing separator. Pure.
fn same_folder(a: &std::path::Path, b: &std::path::Path) -> bool {
let plain = |p: &std::path::Path| p.to_string_lossy().trim_end_matches(['\\', '/']).to_lowercase();
plain(a) == plain(b)
}
/// Cloud folders (OneDrive, Dropbox, iCloud, Google Drive) and WSL distributions.
fn load_cloud_and_wsl() -> Vec<Pinned> {
let mut out = Vec::new();
let mut add = |title: String, path: PathBuf, icon: L| {
// (one folder written two ways is one row: issue 987, Nextcloud registers each account twice, once with a trailing
// separator; both rows were listed and both lit up when one was open)
if !path.is_dir() {
return;
}
match out.iter_mut().find(|p: &&mut Pinned| same_folder(&p.path, &path)) {
// (the longer name says more: "Nextcloud - cloud.example.com - User" over a bare "Nextcloud")
Some(known) if title.chars().count() > known.title.chars().count() => known.title = title,
Some(_) => {}
None => out.push(Pinned { title, path, icon }),
}
};
for (var, title) in [("OneDriveConsumer", "OneDrive"), ("OneDriveCommercial", "OneDrive (İş)"), ("OneDrive", "OneDrive")] {
if let Some(v) = std::env::var_os(var) {
add(title.into(), PathBuf::from(v), L::Cloud);
}
}
if let Some(home) = dirs::home_dir() {
add("iCloud Drive".into(), home.join("iCloudDrive"), L::Cloud);
add("Dropbox".into(), home.join("Dropbox"), L::Cloud);
}
if let Some(local) = dirs::data_local_dir() {
// Dropbox records its real location in info.json
if let Ok(t) = std::fs::read_to_string(local.join("Dropbox").join("info.json")) {
if let Some(i) = t.find("\"path\": \"") {
let rest = &t[i + 9..];
if let Some(j) = rest.find('"') {
add("Dropbox".into(), PathBuf::from(rest[..j].replace("\\\\", "\\")), L::Cloud);
}
}
}
}
for letter in ['G', 'H'] {
add("Google Drive".into(), PathBuf::from(format!("{letter}:\\My Drive")), L::Cloud);
}
// every other sync root Windows knows (issue 141: Proton Drive, pCloud, Nextcloud, Box, MEGA ...), as Explorer lists them; one
// that is already in the list above (OneDrive) stays as it is
for r in rf_win::syncroots::list() {
add(r.name, r.path, L::Cloud);
}
// and the folders a client pins to Explorer's navigation pane itself (MEGA's sync folders)
for r in rf_win::syncroots::pinned_folders() {
add(r.name, r.path, L::Cloud);
}
// WSL distributions registered for this user
if let Ok(lxss) = windows_registry::CURRENT_USER.open(r"Software\Microsoft\Windows\CurrentVersion\Lxss") {
if let Ok(keys) = lxss.keys() {
for k in keys {
if let Ok(d) = lxss.open(&k) {
if let Ok(name) = d.get_string("DistributionName") {
let path = PathBuf::from(format!(r"\\wsl.localhost\{name}"));
out.push(Pinned { title: format!("WSL: {name}"), path, icon: L::SquareTerminal });
}
}
}
}
}
out
}
pub struct Places {
pub drives: Vec<Drive>,
pub cloud: Vec<Pinned>,
pub pinned: Vec<Pinned>,
pub recent: Vec<Recent>,
/// Windows writes no recent items (switched off in its settings or by a policy): Home says so instead of an empty list.
pub recent_off: bool,
pub libraries: Vec<rf_win::libraries::Library>,
/// Network locations ("Add a network location" shortcuts): (name, target).
pub network: Vec<(String, std::path::PathBuf)>,
/// Devices under This PC that are not drives (MTP phones, cameras): browsed as shell
/// folders.
pub devices: Vec<rf_win::shellfolder::ShellChild>,
/// Whether the Windows Recycle Bin contains items; `None` until the shell has answered.
pub recycle_full: Option<bool>,
recycle_icon_hovered: bool,
pub loaded: bool,
/// The recent items and the cloud / WSL entries are in too (they come a little after `loaded`: see `refresh`).
pub extras_loaded: bool,
_task: Option<Task<()>>,
_recycle_task: Option<Task<()>>,
_recent_task: Option<Task<()>>,
/// When the reading that gave `recent` began: an older reading that arrives later does not replace it.
recent_read: Option<std::time::Instant>,
/// The watch on Windows' Recent folder (see `watch_recent`).
_recent_watch: Option<rf_win::watch::DirWatch>,
}
impl Places {
pub fn new(cx: &mut Context<Self>) -> Self {
let mut this =
Self {
drives: Vec::new(),
cloud: Vec::new(),
pinned: Vec::new(),
recent: Vec::new(),
recent_off: false,
libraries: Vec::new(),
network: Vec::new(),
devices: Vec::new(),
recycle_full: None,
recycle_icon_hovered: false,
loaded: false,
extras_loaded: false,
_task: None,
_recycle_task: None,
_recent_task: None,
recent_read: None,
_recent_watch: None,
};
this.refresh(cx);
this.watch_recent(cx);
// the drives the user hides (Settings > Files and folders, a drive's own menu) leave and come back at once
let mut hidden = rf_win::drives::hidden();
// and the pins, when they change anywhere (a pinned folder deleted or renamed: `pins_follow`)
let mut pins = cx.global::<crate::settings::Settings>().pinned.clone();
let mut recycle_theme = cx.global::<crate::settings::Settings>().windows_file_explorer_theme;
cx.observe_global::<crate::settings::Settings>(move |p, cx| {
let now = rf_win::drives::hidden();
let settings = cx.global::<crate::settings::Settings>();
let now_pins = settings.pinned.clone();
let now_recycle_theme = settings.windows_file_explorer_theme;
if recycle_theme && !now_recycle_theme {
p.set_recycle_icon_hovered(false, cx);
}
recycle_theme = now_recycle_theme;
if now != hidden || now_pins != pins {
hidden = now;
pins = now_pins;
p.refresh(cx);
}
})
.detach();
// Notice drives appearing/disappearing (USB, network mappings) without any shell hooks.
cx.spawn(async move |this, cx| {
let mut mask = rf_win::drives::drive_mask();
loop {
cx.background_executor().timer(std::time::Duration::from_secs(3)).await;
let now = rf_win::drives::drive_mask();
if now != mask {
mask = now;
if this.update(cx, |p, cx| p.refresh(cx)).is_err() {
break;
}
}
}
})
.detach();
// Phones come and go without a drive letter: ask the shell now and then. Off the UI
// thread, and the list is only replaced when it changed. Not while no window is in front
// of the user (minimised, or another app active): enumerating This PC loads shell
// extensions and can touch the drives; the look happens again when one comes back.
let mut misses: std::collections::HashMap<String, u8> = std::collections::HashMap::new();
cx.spawn(async move |this, cx| loop {
if !rf_win::window::app_in_front() {
cx.background_executor().timer(std::time::Duration::from_secs(5)).await;
if this.upgrade().is_none() {
break;
}
continue;
}
let found = cx.background_spawn(async move { rf_win::shellfolder::devices() }).await;
let alive = this
.update(cx, |p, cx| {
let found = steady_devices(&p.devices, found, &mut misses, DEVICE_MISSES);
if p.devices != found {
p.devices = found;
cx.notify();
}
})
.is_ok();
if !alive {
break;
}
cx.background_executor().timer(std::time::Duration::from_secs(5)).await;
})
.detach();
this
}
pub fn set_recycle_icon_hovered(&mut self, hovered: bool, cx: &mut Context<Self>) {
if self.recycle_icon_hovered == hovered {
return;
}
self.recycle_icon_hovered = hovered;
if !hovered {
self._recycle_task = None;
return;
}
self._recycle_task = Some(cx.spawn(async move |this, cx| {
let mut error_logged = false;
loop {
match cx.background_spawn(async { rf_win::recycle::info() }).await {
Ok((item_count, _)) => {
error_logged = false;
if this
.update(cx, |places, cx| {
let full = item_count > 0;
if places.recycle_full != Some(full) {
places.recycle_full = Some(full);
cx.notify();
}
})
.is_err()
{
break;
}
}
Err(error) if !error_logged => {
log::warn!("could not read Recycle Bin state for its icon: {error:#}");
error_logged = true;
}
Err(_) => {}
}
cx.background_executor().timer(std::time::Duration::from_secs(3)).await;
}
}));
}
/// Keeps the recent items in step with Windows: its Recent folder is watched, and the list is read again a moment after a
/// shortcut is written there. The list was read at the start and on F5 only, and the app stays open for days: Home showed the
/// files of the morning in the evening (issue 341: "a gap of up to 10 hours between entries").
fn watch_recent(&mut self, cx: &mut Context<Self>) {
let Some(dir) = rf_win::dock::startrecent::recent_dir() else { return };
let (tx, mut rx) = futures::channel::mpsc::unbounded::<()>();
// (a file opened in a program writes its shortcut in two or three steps: one reading after them)
match rf_win::watch::watch_dir(&dir, std::time::Duration::from_millis(1500), move || {
let _ = tx.unbounded_send(());
}) {
Ok(w) => self._recent_watch = Some(w),
Err(e) => {
log::warn!("recent items: the folder cannot be watched: {e:#}");
return;
}
}
self._recent_task = Some(cx.spawn(async move |this, cx| {
use futures::StreamExt;
while rx.next().await.is_some() {
let began = std::time::Instant::now();
let read = cx.background_spawn(async move {
let recent = load_recent(RECENT_SHOWN);
rf_win::localized::warm(recent.iter().filter(|r| r.is_dir).flat_map(|r| crate::nav_touch::label_paths(&r.target)));
(recent, rf_win::dock::startrecent::recent_tracking_on())
});
let (recent, tracking) = read.await;
// (several changes that came during the reading are one more reading, not one each)
while rx.try_recv().is_ok() {}
let alive = this.update(cx, |p, cx| {
p.recent_read = Some(began);
if !same_recent(&p.recent, &recent) || p.recent_off == tracking {
p.recent = recent;
p.recent_off = !tracking;
cx.notify();
}
});
if alive.is_err() {
break;
}
}
}));
}
pub fn refresh(&mut self, cx: &mut Context<Self>) {
let user_pins = cx.try_global::<crate::settings::Settings>().and_then(|s| s.pinned.clone());
// Two pieces of work, so that the sidebar does not wait for the slow one. The recent items ask the disk about the place each
// one names (a stat each), and so do the cloud entries (OneDrive, Dropbox, Google Drive...); a place that has to wake up first
// (an offline share, a sleeping disk, a WSL folder opened lately) answers after seconds. The WSL distributions are read from
// the registry and cost nothing. The pinned folders and the drives need none of that.
// The lists are not complete until the second piece is in, so the jump list waits for it again after a refresh.
self.extras_loaded = false;
let work = cx.background_spawn(async move {
let known = rf_win::known_folders();
let pinned = match user_pins {
None => known
.into_iter()
.map(|(k, path)| Pinned { title: k.title().to_string(), path, icon: known_icon(k) })
.collect::<Vec<_>>(),
Some(paths) => paths
.into_iter()
.map(|path| match known.iter().find(|(_, p)| *p == path) {
Some((k, _)) => Pinned { title: k.title().to_string(), path, icon: known_icon(*k) },
None => Pinned {
title: rf_core::history::display_name(&path),
path,
icon: L::Folder,
},
})
.collect(),
};
// (less the drives the user hides and the ones Explorer hides)
let drives = rf_win::drives::visible_drives();
let libraries = rf_win::libraries::list();
let network = rf_win::network::network_shortcuts();
(pinned, drives, libraries, network)
});
let began = std::time::Instant::now();
let extras = cx.background_spawn(async move {
let recent = load_recent(RECENT_SHOWN);
// the localized names the jump list and the Home view show for these folders are read here, on this thread: the
// drawing only looks in the cache (see rf_win::localized::label_if_known)
rf_win::localized::warm(recent.iter().filter(|r| r.is_dir).flat_map(|r| crate::nav_touch::label_paths(&r.target)));
(recent, rf_win::dock::startrecent::recent_tracking_on(), load_cloud_and_wsl())
});
self._task = Some(cx.spawn(async move |this, cx| {
let (pinned, drives, libraries, network) = work.await;
// the names of the pinned folders and of the folders above them (the breadcrumb of a click on a pin), read off the UI
// thread and not in the piece the sidebar waits for
let pin_paths: Vec<PathBuf> = pinned.iter().flat_map(|p| crate::nav_touch::label_paths(&p.path)).collect();
let pin_names = cx.background_spawn(async move { rf_win::localized::warm(pin_paths) });
let _ = this.update(cx, |p, cx| {
// A drive that is gone from the system (unplugged, ejected elsewhere) takes the panes on it Home, as our own eject does
// (issue 764: Explorer closes such tabs; going Home keeps the window's tabs as they were). A drive only hidden from the
// list is still there and is left alone.
let gone: Vec<char> = p
.drives
.iter()
.map(|d| d.letter)
.filter(|l| !drives.iter().any(|d| d.letter == *l) && !rf_win::drives::letter_present(*l))
.collect();
for letter in gone {
log::info!("places: {letter}: is gone; panes on it go Home");
crate::workspace::release_drive(letter, cx);
}
p.pinned = pinned;
p.drives = drives;
p.libraries = libraries;
p.network = network;
p.loaded = true;
cx.notify();
});
let (recent, tracking, cloud) = extras.await;
let _ = this.update(cx, |p, cx| {
// (the cloud entries can take seconds: the watch may have read the recent items again meanwhile)
if p.recent_read.is_none_or(|at| at <= began) {
p.recent = recent;
p.recent_off = !tracking;
p.recent_read = Some(began);
}
p.cloud = cloud;
p.extras_loaded = true;
cx.notify();
});
if pin_names.await > 0 {
// a pinned folder has a localized name: the views that show it are drawn again
let _ = this.update(cx, |_, cx| cx.notify());
}
}));
}
}
pub fn known_icon(k: KnownFolder) -> L {
match k {
KnownFolder::Desktop => L::Monitor,
KnownFolder::Downloads => L::Download,
KnownFolder::Documents => L::FileText,
KnownFolder::Pictures => L::Image,
KnownFolder::Music => L::Music,
KnownFolder::Videos => L::Video,
KnownFolder::Profile => L::House,
}
}
pub fn drive_icon(d: &Drive) -> L {
match d.kind {
DriveKind::Removable => L::Usb,
DriveKind::Network => L::Network,
DriveKind::Optical => L::Disc3,
_ => L::HardDrive,
}
}
/// The user's pins after `from` was deleted (`to` None) or moved or renamed to `to`: a pin on `from` or inside it goes, or follows it.
/// `None` when no pin is touched.
///
/// WHY (a deleted folder stayed in the sidebar): the pins are a list in the settings, and deleting or renaming the folder did not touch it.
pub fn pins_after(pins: &[std::path::PathBuf], from: &std::path::Path, to: Option<&std::path::Path>) -> Option<Vec<std::path::PathBuf>> {
let touched = |p: &std::path::PathBuf| p.starts_with(from);
if !pins.iter().any(touched) {
return None;
}
Some(
pins.iter()
.filter_map(|p| match (p.strip_prefix(from), to) {
(Ok(_), None) => None,
(Ok(rest), Some(to)) => Some(if rest.as_os_str().is_empty() { to.to_path_buf() } else { to.join(rest) }),
(Err(_), _) => Some(p.clone()),
})
.collect(),
)
}
/// Applies [`pins_after`] to the user's own pin list (the default pins, when the user never changed them, are the known folders and
/// stay as they are).
pub fn pins_follow(cx: &mut App, from: &std::path::Path, to: Option<&std::path::Path>) {
let Some(list) = cx.global::<crate::settings::Settings>().pinned.clone() else { return };
if let Some(next) = pins_after(&list, from, to) {
crate::settings::update(cx, move |s| s.pinned = Some(next));
}
}
#[cfg(test)]
mod cloud_row_tests {
use super::same_folder;
use std::path::Path;
/// Issue 987: Nextcloud writes one folder with and without a trailing separator; it is one folder.
#[test]
fn one_folder_written_two_ways_is_one_folder() {
assert!(same_folder(Path::new(r"C:\Users\me\NextcloudB\"), Path::new(r"c:\users\me\NextcloudB")));
assert!(!same_folder(Path::new(r"C:\Users\me\NextcloudA"), Path::new(r"C:\Users\me\NextcloudB")));
}
}
#[cfg(test)]
mod recent_merge_tests {
use super::{Recent, merge_recent};
use std::path::PathBuf;
fn r(p: &str, at: i64) -> Recent {
Recent { name: p.into(), target: PathBuf::from(p), used_ms: at, is_dir: false }
}
/// Issue 421: downloads and copies join Windows' recent items: newest first, each place once, at most the limit.
#[test]
fn the_sources_are_merged_newest_first_once_each() {
let all = vec![r(r"C:\a.txt", 10), r(r"C:\dl\b.zip", 30), r(r"C:\a.txt", 50), r(r"D:\c.jpg", 20)];
let m = merge_recent(all, 2);
let names: Vec<&str> = m.iter().map(|x| x.target.to_str().unwrap()).collect();
assert_eq!(names, [r"C:\a.txt", r"C:\dl\b.zip"]);
assert_eq!(m[0].used_ms, 50, "a place's newest time");
}
}
#[cfg(test)]
mod pins_follow_tests {
use super::pins_after;
use std::path::{Path, PathBuf};
#[test]
fn a_pin_goes_with_its_folder_or_follows_it() {
let pins = vec![PathBuf::from(r"C:\a\Work"), PathBuf::from(r"C:\a\Work\inner"), PathBuf::from(r"D:\Photos")];
assert_eq!(pins_after(&pins, Path::new(r"C:\a\Work"), None), Some(vec![PathBuf::from(r"D:\Photos")]), "deleted: it and what was inside go");
assert_eq!(
pins_after(&pins, Path::new(r"C:\a\Work"), Some(Path::new(r"C:\a\Job"))),
Some(vec![PathBuf::from(r"C:\a\Job"), PathBuf::from(r"C:\a\Job\inner"), PathBuf::from(r"D:\Photos")])
);
assert_eq!(pins_after(&pins, Path::new(r"C:\a\Wor"), None), None, "a name that only starts the same is another folder");
assert_eq!(pins_after(&pins, Path::new(r"E:\x"), None), None);
}
}
/// How many looks in a row (5 s apart) a device may be missing from This PC before it leaves the sidebar.
const DEVICE_MISSES: u8 = 4;
/// The devices to show after a look at This PC that found `found`: a device that is missing keeps its place until it has been missing
/// `limit` looks in a row (`misses` counts them). Issue 217: a receiver on the Wi-Fi (Denon AVR-X1800H), a DSL box, are media devices
/// the shell finds on the network on one look and not on the next, and they came and went in the sidebar every few seconds; Explorer
/// keeps them. A new device is shown at once.
fn steady_devices(
shown: &[rf_win::shellfolder::ShellChild],
found: Vec<rf_win::shellfolder::ShellChild>,
misses: &mut std::collections::HashMap<String, u8>,
limit: u8,
) -> Vec<rf_win::shellfolder::ShellChild> {
let mut out = found;
for d in &out {
misses.remove(&d.parsing);
}
for d in shown {
if out.iter().any(|f| f.parsing == d.parsing) {
continue;
}
let n = misses.entry(d.parsing.clone()).or_insert(0);
*n += 1;
if *n < limit {
out.push(d.clone());
} else {
misses.remove(&d.parsing);
}
}
out
}
#[cfg(test)]
mod steady_device_tests {
use super::steady_devices;
use rf_win::shellfolder::ShellChild;
fn dev(name: &str) -> ShellChild {
ShellChild { name: name.into(), parsing: format!("::{{x}}\\{name}"), is_folder: true, fs_path: None, size: None, modified_ms: None }
}
#[test]
fn a_device_missing_from_a_look_or_two_stays_and_one_gone_for_good_leaves() {
let mut misses = Default::default();
let mut shown = steady_devices(&[], vec![dev("phone"), dev("receiver")], &mut misses, 3);
assert_eq!(shown.len(), 2);
// the receiver is not found twice: still shown
for _ in 0..2 {
shown = steady_devices(&shown, vec![dev("phone")], &mut misses, 3);
assert_eq!(shown.len(), 2);
}
// found again: its count starts over
shown = steady_devices(&shown, vec![dev("phone"), dev("receiver")], &mut misses, 3);
for _ in 0..2 {
shown = steady_devices(&shown, vec![dev("phone")], &mut misses, 3);
}
assert_eq!(shown.len(), 2);
// the third look in a row without it: gone
shown = steady_devices(&shown, vec![dev("phone")], &mut misses, 3);
assert_eq!(shown.iter().map(|d| d.name.as_str()).collect::<Vec<_>>(), ["phone"]);
}
}