diff --git a/CHANGELOG.md b/CHANGELOG.md index da661966..59880ecc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### 🐛 Bug Fixes - Fixed heading `Places` not being able to be updated with `FileDialogLabels` [#180](https://github.com/fluxxcode/egui-file-dialog/pull/180) +- Fix display errors with path prefix on Windows [#182](https://github.com/fluxxcode/egui-file-dialog/pull/182) ### 🔧 Changes - Use path edit as file to save [#160](https://github.com/fluxxcode/egui-file-dialog/pull/160) diff --git a/Cargo.toml b/Cargo.toml index 9655f21e..24eca5eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,9 @@ sysinfo = { version = "0.32", default-features = false, features = ["disk"] } # Used for persistent storage serde = { version = "1", features = ["derive"], optional = true } +# Used to canonicalize paths +dunce = "1.0.5" + [features] default = ["serde", "default_fonts"] serde = ["dep:serde"] diff --git a/src/config/mod.rs b/src/config/mod.rs index bc093a2e..b4d1efff 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -4,7 +4,6 @@ pub use labels::FileDialogLabels; mod keybindings; pub use keybindings::{FileDialogKeyBindings, KeyBinding}; -use std::fs; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -447,7 +446,7 @@ impl QuickAccess { let path = path.into(); let canonicalized_path = if self.canonicalize_paths { - fs::canonicalize(&path).unwrap_or(path) + dunce::canonicalize(&path).unwrap_or(path) } else { path }; diff --git a/src/data/directory_content.rs b/src/data/directory_content.rs index 07b4cfe9..e49090a7 100644 --- a/src/data/directory_content.rs +++ b/src/data/directory_content.rs @@ -384,10 +384,7 @@ fn load_directory( fn is_path_hidden(item: &DirectoryEntry) -> bool { use std::os::windows::fs::MetadataExt; - match fs::metadata(item.as_path()) { - Ok(metadata) => metadata.file_attributes() & 0x2 > 0, - Err(_) => false, - } + fs::metadata(item.as_path()).map_or(false, |metadata| metadata.file_attributes() & 0x2 > 0) } #[cfg(not(windows))] diff --git a/src/data/disks.rs b/src/data/disks.rs index 1966c295..c5769ab5 100644 --- a/src/data/disks.rs +++ b/src/data/disks.rs @@ -1,4 +1,3 @@ -use std::fs; use std::path::{Path, PathBuf}; /// Wrapper above the `sysinfo::Disk` struct. @@ -39,7 +38,7 @@ impl Disk { /// Returns the input path in case of an error. fn canonicalize(path: &Path, canonicalize: bool) -> PathBuf { if canonicalize { - fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) + dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) } else { path.to_path_buf() } @@ -91,7 +90,7 @@ fn gen_display_name(disk: &sysinfo::Disk) -> String { return mount_point; } - name.push_str(format!(" ({})", mount_point).as_str()); + name.push_str(format!(" ({mount_point})").as_str()); name } diff --git a/src/data/user_directories.rs b/src/data/user_directories.rs index d5c03fd2..496d838f 100644 --- a/src/data/user_directories.rs +++ b/src/data/user_directories.rs @@ -1,4 +1,3 @@ -use std::fs; use std::path::{Path, PathBuf}; /// Wrapper above `directories::UserDirs`. @@ -70,7 +69,7 @@ impl UserDirectories { } if let Some(path) = path { - return fs::canonicalize(path).ok(); + return dunce::canonicalize(path).ok(); } None diff --git a/src/file_dialog.rs b/src/file_dialog.rs index c7772361..e6cc9ed9 100644 --- a/src/file_dialog.rs +++ b/src/file_dialog.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; +use std::io; use std::path::{Path, PathBuf}; -use std::{fs, io}; use egui::text::{CCursor, CCursorRange}; @@ -1399,41 +1399,16 @@ impl FileDialog { let mut path = PathBuf::new(); if let Some(data) = self.current_directory() { - #[cfg(windows)] - let mut drive_letter = String::from("\\"); - for (i, segment) in data.iter().enumerate() { path.push(segment); - #[cfg(windows)] - let mut file_name = segment.to_str().unwrap_or(""); - - #[cfg(windows)] - { - // Skip the path namespace prefix generated by - // fs::canonicalize() on Windows - if i == 0 { - drive_letter = file_name.replace(r"\\?\", ""); - continue; - } - - // Replace the root segment with the disk letter - if i == 1 && segment == "\\" { - file_name = drive_letter.as_str(); - } else if i != 0 { - ui.label(self.config.directory_separator.as_str()); - } - } - - #[cfg(not(windows))] - let file_name = segment.to_str().unwrap_or(""); + let segment_str = segment.to_str().unwrap_or(""); - #[cfg(not(windows))] if i != 0 { ui.label(self.config.directory_separator.as_str()); } - if ui.button(file_name).clicked() { + if ui.button(segment_str).clicked() { self.load_directory(path.as_path()); return; } @@ -2625,7 +2600,7 @@ impl FileDialog { /// Returns the input path if an error occurs or canonicalization is disabled. fn canonicalize_path(&self, path: &Path) -> PathBuf { if self.config.canonicalize_paths { - fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) + dunce::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()) } else { path.to_path_buf() }