Skip to content

Commit

Permalink
Fix path prefix on windows by replacing fs::canonicalize with dunce::…
Browse files Browse the repository at this point in the history
…canonicalize (fluxxcode#182)

* Replace fs::canonicalize with dunc

* Update changelog
  • Loading branch information
fluxxcode authored Oct 27, 2024
1 parent 8b1fd62 commit c1b301e
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 1 addition & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
};
Expand Down
5 changes: 1 addition & 4 deletions src/data/directory_content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
5 changes: 2 additions & 3 deletions src/data/disks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs;
use std::path::{Path, PathBuf};

/// Wrapper above the `sysinfo::Disk` struct.
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions src/data/user_directories.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs;
use std::path::{Path, PathBuf};

/// Wrapper above `directories::UserDirs`.
Expand Down Expand Up @@ -70,7 +69,7 @@ impl UserDirectories {
}

if let Some(path) = path {
return fs::canonicalize(path).ok();
return dunce::canonicalize(path).ok();
}

None
Expand Down
33 changes: 4 additions & 29 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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("<ERR>");

#[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("<ERR>");
let segment_str = segment.to_str().unwrap_or("<ERR>");

#[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;
}
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit c1b301e

Please sign in to comment.