Skip to content

Commit

Permalink
Ensure the behavior is the same for all plateform when the path does …
Browse files Browse the repository at this point in the history
…not exist.
  • Loading branch information
IohannRabeson committed Nov 25, 2024
1 parent 2f95642 commit 336751a
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub struct Icon {
/// }
/// ```
pub fn get_file_icon(path: impl AsRef<Path>) -> Option<Icon> {
// For consistency: on MacOS if the path does not exist None is returned
// but on Windows a default icon is returned.
if !path.as_ref().exists() {
return None;
}

implementation::get_file_icon(path)
}

Expand All @@ -47,7 +53,6 @@ mod implementation {
let path = path.as_ref().canonicalize().ok()?;
let file_path = NSString::from_str(path.to_str()?);
let color_space_name = NSString::from_str("NSDeviceRGBColorSpace");

let shared_workspace = unsafe { NSWorkspace::sharedWorkspace() };
let image = unsafe { shared_workspace.iconForFile(&file_path) };
let image_size = unsafe { image.size() };
Expand Down Expand Up @@ -91,7 +96,7 @@ mod implementation {
bitmap_representation.bitmapData(),
bitmap_representation.bytesPerPlane() as usize,
)
.to_vec()
.to_vec()
};

Some(Icon {
Expand Down Expand Up @@ -125,12 +130,6 @@ mod implementation {
},
};
let path = path.as_ref();

// For consistency: on MacOS if the path does not exist None is returned.
if !path.exists() {
return None;
}

let file_path = HSTRING::from(path);
let info = &mut SHFILEINFOW::default();

Expand Down Expand Up @@ -291,4 +290,4 @@ mod tests {
fn test_not_existing_file() {
assert!(get_file_icon("NOT EXISTING").is_none());
}
}
}

0 comments on commit 336751a

Please sign in to comment.