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
Picker: pick existing files entered in the address bar
teebee1337aa846ad8a99a
diff --git a/crates/rf-app/src/address_cmd.rs b/crates/rf-app/src/address_cmd.rsindex 23b6be58..00369616 100644--- a/crates/rf-app/src/address_cmd.rs+++ b/crates/rf-app/src/address_cmd.rs@@ -67,6 +67,32 @@ impl Workspace {Location::Path(p) if !rf_win::ftp::is_ftp(p) && rf_win::network::server_root(p).is_none() && crate::archive_view::split(p).is_none() => Some(p.clone()),_ => None,};+ if self.picker.is_some() {+ if let Location::Path(p) = &loc {+ let candidate = resolve_typed_path(p, open_dir.as_deref());+ if picker_file_check_possible(p, &candidate, &text, from_completion) {+ let probe = candidate.clone();+ let work = cx.background_spawn(async move { path_is_file_in_time(&probe, Instant::now()) });+ self.focus_active(window, cx);+ cx.spawn_in(window, async move |this, cx| {+ let is_file = work.await;+ let _ = this.update_in(cx, |ws, window, cx| ws.finish_picker_address(is_file, candidate, tab, window, cx));+ })+ .detach();+ cx.notify();+ return;+ }+ let dest = if p.is_relative() && open_dir.is_some() { Location::Path(candidate) } else { loc };+ self.navigate(dest, window, cx);+ self.focus_active(window, cx);+ cx.notify();+ return;+ }+ self.navigate(loc, window, cx);+ self.focus_active(window, cx);+ cx.notify();+ return;+ }let command_possible = command_possible(matches!(loc, Location::Path(_)),from_completion,@@ -112,6 +138,34 @@ impl Workspace {}cx.notify();}++ fn finish_picker_address(&mut self, is_file: bool, candidate: PathBuf, tab: Entity<FolderView>, window: &mut Window, cx: &mut Context<Self>) {+ if is_file && self.picker.is_some() {+ crate::picker::pick_address_file(self, tab, candidate, window, cx);+ cx.notify();+ return;+ }+ let loc = Location::Path(candidate);+ if self.tabs.get(self.active).is_some_and(|t| t.view() == &tab) {+ self.navigate(loc, window, cx);+ } else {+ tab.update(cx, |v, cx| v.navigate(loc, cx));+ }+ cx.notify();+ }+}++fn has_trailing_sep(raw: &str) -> bool {+ raw.trim().trim_matches('"').ends_with(['\\', '/'])+}++fn picker_file_check_possible(typed: &Path, candidate: &Path, raw_text: &str, from_completion: bool) -> bool {+ !from_completion+ && !has_trailing_sep(raw_text)+ && !rf_win::ftp::is_ftp(typed)+ && rf_win::network::server_root(typed).is_none()+ && candidate.is_absolute()+ && candidate.parent().is_some()}/// Is this Enter allowed to look at the disk and maybe start a program? Not for a completion that was picked, nor while a picker is@@ -143,15 +197,28 @@ fn path_exists_in_time(p: &Path, began: Instant) -> bool {})}+fn path_is_file_in_time(p: &Path, began: Instant) -> bool {+ let left = LOOK_LIMIT.saturating_sub(began.elapsed()).max(Duration::from_millis(1));+ let p = p.to_path_buf();+ within(left, move || std::fs::metadata(&p).is_ok_and(|m| m.is_file())).unwrap_or_else(|| {+ log::warn!("picker address: the disk did not answer in time; treating as folder");+ false+ })+}+/// The readings of the typed path as a place, in the order they are tried, with `.` and `..` folded away. A relative name is read/// against the open folder ONLY when there is one: the folder of the process (the system folder after an autostart) is not where the/// user is looking, and a file there must not hide a program (`notepad.exe`).-fn place_readings(typed: &Path, open_dir: Option<&Path>) -> Vec<PathBuf> {+fn resolve_typed_path(typed: &Path, open_dir: Option<&Path>) -> PathBuf {let one = match open_dir.filter(|_| typed.is_relative()) {Some(dir) => dir.join(typed),None => typed.to_path_buf(),};- vec![addrcmd::normalize_lexical(&one)]+ addrcmd::normalize_lexical(&one)+}++fn place_readings(typed: &Path, open_dir: Option<&Path>) -> Vec<PathBuf> {+ vec![resolve_typed_path(typed, open_dir)]}/// Does `p` exist (a folder or a file)? A path that exists but cannot be read counts: it is a place, and the folder view says why@@ -316,4 +383,54 @@ mod tests {assert!(matches!(super::resolve_and_run(r"Z:\no\such\dir", std::path::Path::new(r"Z:\no\such\dir"), None), super::Outcome::Neither));let _ = std::fs::remove_dir_all(&root);}++ #[test]+ fn picker_address_only_checks_disk_for_plausible_file_paths() {+ use super::{picker_file_check_possible, resolve_typed_path};+ use std::path::Path;++ let open = Path::new(r"C:\Users\tb\Documents");+ let check = |raw: &str, typed: &str, open_dir: Option<&Path>, from_completion: bool| {+ let t = Path::new(typed);+ let c = resolve_typed_path(t, open_dir);+ picker_file_check_possible(t, &c, raw, from_completion)+ };++ assert!(check(r"C:\a\b.xpi", r"C:\a\b.xpi", None, false));+ assert!(check(r#""C:\a\b.zip""#, r"C:\a\b.zip", None, false));+ assert!(check("C:/a/b.txt", "C:/a/b.txt", None, false));+ assert!(check(r"\\nas\share\file.txt", r"\\nas\share\file.txt", None, false));+ assert!(check("file.xpi", "file.xpi", Some(open), false));++ assert!(!check("file.xpi", "file.xpi", None, false), "relative without open_dir");+ assert!(!check(r"C:\a\b.xpi", r"C:\a\b.xpi", None, true), "picked from completion");+ assert!(!check(r"C:\a\dir\", r"C:\a\dir\", None, false), "trailing backslash");+ assert!(!check("C:/a/dir/", "C:/a/dir/", None, false), "trailing forward slash");+ assert!(!check(r"C:\", r"C:\", None, false), "drive root");+ assert!(!check(r"\\nas", r"\\nas", None, false), "server root");+ assert!(!check(r"\\nas\share", r"\\nas\share", None, false), "share root");+ assert!(!check("ftp://host/file.txt", "ftp://host/file.txt", None, false), "FTP path");+ }++ #[test]+ fn picker_file_check_distinguishes_files_archives_dirs_and_missing_paths() {+ use std::time::Instant;+ let root = std::env::temp_dir().join(format!("rf-picker-addr-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&root);+ std::fs::create_dir_all(root.join("sub")).unwrap();+ let txt = root.join("note.txt");+ let xpi = root.join("ext.xpi");+ let zip = root.join("pack.zip");+ std::fs::write(&txt, b"hi").unwrap();+ std::fs::write(&xpi, b"PK").unwrap();+ std::fs::write(&zip, b"PK").unwrap();++ assert!(super::path_is_file_in_time(&txt, Instant::now()));+ assert!(super::path_is_file_in_time(&xpi, Instant::now()));+ assert!(super::path_is_file_in_time(&zip, Instant::now()));+ assert!(!super::path_is_file_in_time(&root.join("sub"), Instant::now()));+ assert!(!super::path_is_file_in_time(&root.join("missing.txt"), Instant::now()));++ let _ = std::fs::remove_dir_all(&root);+ }}diff --git a/crates/rf-app/src/picker.rs b/crates/rf-app/src/picker.rsindex 1e64847b..c517998c 100644--- a/crates/rf-app/src/picker.rs+++ b/crates/rf-app/src/picker.rs@@ -15,6 +15,7 @@ use gpui_kit::*;use rf_core::Location;use rustyfiles_dialog::proto::{Mode, Request, Response, first_ext, read_msg, write_msg};+use crate::folder_view::FolderView;use crate::t;use crate::theme::{self, Palette};use crate::workspace::Workspace;@@ -696,26 +697,7 @@ pub(crate) fn accept(ws: &mut Workspace, window: &mut Window, cx: &mut Context<W}}if target.exists() && s.req.overwrite_prompt {- let name = target.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();- let ws_handle = cx.entity().downgrade();- let t2 = target.clone();- crate::prompt::show(- format!("{name} {}", t!("zaten var.")),- Some(t!("Değiştirmek istiyor musunuz?").to_string()),- None,- vec![- crate::prompt::Button::new(t!("Değiştir"), crate::prompt::Kind::Primary, move |_, window, cx| {- if let Some(ws) = ws_handle.upgrade() {- let p = t2.clone();- ws.update(cx, |ws, _| finish(ws, vec![p], window));- }- Ok(())- }),- crate::prompt::Button::cancel(),- ],- window,- cx,- );+ confirm_overwrite(target, window, cx);return;}finish(ws, vec![target], window);@@ -723,6 +705,81 @@ pub(crate) fn accept(ws: &mut Workspace, window: &mut Window, cx: &mut Context<W}}+#[derive(Debug, Clone, PartialEq, Eq)]+pub(crate) enum AddressFileAction {+ Finish(PathBuf),+ ConfirmOverwrite { target: PathBuf, parent: Option<PathBuf>, name: String },+ Navigate(PathBuf),+}++pub(crate) fn address_file_action(mode: Mode, overwrite_prompt: bool, path: PathBuf) -> AddressFileAction {+ match mode {+ Mode::Open => AddressFileAction::Finish(path),+ Mode::Save if !overwrite_prompt => AddressFileAction::Finish(path),+ Mode::Save => AddressFileAction::ConfirmOverwrite {+ parent: path.parent().map(Path::to_path_buf),+ name: path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(),+ target: path,+ },+ Mode::Folder => AddressFileAction::Navigate(path.parent().map(Path::to_path_buf).unwrap_or(path)),+ }+}++pub(crate) fn pick_address_file(ws: &mut Workspace, tab: Entity<FolderView>, path: PathBuf, window: &mut Window, cx: &mut Context<Workspace>) {+ let Some(s) = ws.picker() else { return };+ match address_file_action(s.req.mode, s.req.overwrite_prompt, path) {+ AddressFileAction::Finish(target) => finish(ws, vec![target], window),+ AddressFileAction::ConfirmOverwrite { target, parent, name } => {+ if let Some(parent) = parent {+ let same_dir = tab.read(cx).current_dir().is_some_and(|d| d.as_os_str().eq_ignore_ascii_case(parent.as_os_str()));+ if same_dir {+ if !name.is_empty() {+ tab.update(cx, |v, cx| v.select_now(name.clone(), cx));+ }+ } else {+ if !name.is_empty() {+ tab.update(cx, |v, _| v.set_reselect(name.clone()));+ }+ tab.update(cx, |v, cx| v.navigate(Location::Path(parent), cx));+ }+ }+ s.touched.set(true);+ *s.last_selection.borrow_mut() = vec![target.clone()];+ s.name.update(cx, |st, cx| st.set_value(name, window, cx));+ confirm_overwrite(target, window, cx);+ }+ AddressFileAction::Navigate(dir) => {+ tab.update(cx, |v, cx| v.navigate(Location::Path(dir), cx));+ if ws.active_view() == tab {+ ws.focus_active(window, cx);+ }+ }+ }+}++fn confirm_overwrite(target: PathBuf, window: &mut Window, cx: &mut Context<Workspace>) {+ let name = target.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();+ let ws_handle = cx.entity().downgrade();+ let t2 = target.clone();+ crate::prompt::show(+ format!("{name} {}", t!("zaten var.")),+ Some(t!("Değiştirmek istiyor musunuz?").to_string()),+ None,+ vec![+ crate::prompt::Button::new(t!("Değiştir"), crate::prompt::Kind::Primary, move |_, window, cx| {+ if let Some(ws) = ws_handle.upgrade() {+ let p = t2.clone();+ ws.update(cx, |ws, _| finish(ws, vec![p], window));+ }+ Ok(())+ }),+ crate::prompt::Button::cancel(),+ ],+ window,+ cx,+ );+}+fn tell(msg: &str, window: &mut Window, cx: &mut App) {crate::prompt::show(msg.to_string(), None, None, vec![crate::prompt::Button::new(t!("Tamam"), crate::prompt::Kind::Primary, |_, _, _| Ok(()))], window, cx);}@@ -879,6 +936,30 @@ mod tests {assert_eq!(resolve(None, r"D:\x.txt"), Some(PathBuf::from(r"D:\x.txt")));assert_eq!(resolve(None, "x.txt"), None);}++ #[test]+ fn address_file_action_handles_open_save_and_folder_modes() {+ use super::{AddressFileAction, address_file_action};+ use rustyfiles_dialog::proto::Mode;++ let xpi = PathBuf::from(r"C:\Users\tb\Documents\firefox-extensions\Picket Domain Router-2.0.3.xpi");+ let zip = PathBuf::from(r"C:\Users\tb\archive.zip");+ assert_eq!(address_file_action(Mode::Open, true, xpi.clone()), AddressFileAction::Finish(xpi.clone()));+ assert_eq!(address_file_action(Mode::Open, false, zip.clone()), AddressFileAction::Finish(zip));+ assert_eq!(address_file_action(Mode::Save, false, xpi.clone()), AddressFileAction::Finish(xpi.clone()));+ assert_eq!(+ address_file_action(Mode::Save, true, xpi.clone()),+ AddressFileAction::ConfirmOverwrite {+ target: xpi.clone(),+ parent: Some(PathBuf::from(r"C:\Users\tb\Documents\firefox-extensions")),+ name: "Picket Domain Router-2.0.3.xpi".into(),+ }+ );+ assert_eq!(+ address_file_action(Mode::Folder, false, xpi),+ AddressFileAction::Navigate(PathBuf::from(r"C:\Users\tb\Documents\firefox-extensions"))+ );+ }}/// Appends a line to `%LOCALAPPDATA%/rustyfiles/dialog.log` (where the dialog DLL logs).