chore(example): Support special filenames in open-dialog example

This commit is contained in:
Titouan Real 2024-08-26 02:15:06 +00:00 committed by GitHub
parent b15ae66ac8
commit 23bb4a122f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -114,17 +114,20 @@ impl cosmic::Application for App {
return cosmic::command::future(async move { return cosmic::command::future(async move {
// Check if its a valid local file path. // Check if its a valid local file path.
let path = match url.scheme() { let path = match url.scheme() {
"file" => url.path(), "file" => url.to_file_path().unwrap(),
other => { other => {
return Message::Error(format!("{url} has unknown scheme: {other}")); return Message::Error(format!("{url} has unknown scheme: {other}"));
} }
}; };
// Open the file by its path. // Open the file by its path.
let mut file = match tokio::fs::File::open(path).await { let mut file = match tokio::fs::File::open(&path).await {
Ok(file) => file, Ok(file) => file,
Err(why) => { Err(why) => {
return Message::Error(format!("failed to open {path}: {why}")); return Message::Error(format!(
"failed to open {}: {why}",
path.display()
));
} }
}; };
@ -132,7 +135,7 @@ impl cosmic::Application for App {
contents.clear(); contents.clear();
if let Err(why) = file.read_to_string(&mut contents).await { if let Err(why) = file.read_to_string(&mut contents).await {
return Message::Error(format!("failed to read {path}: {why}")); return Message::Error(format!("failed to read {}: {why}", path.display()));
} }
contents.shrink_to_fit(); contents.shrink_to_fit();