Skip to content

Commit 3767a61

Browse files
committed
Try to open with mime apps before trying xdg-open
1 parent c3a189a commit 3767a61

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

src/app.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -528,15 +528,15 @@ pub struct App {
528528

529529
impl App {
530530
fn open_file(&mut self, path: &PathBuf) {
531-
let mut found_desktop_exec = false;
532531
let mime = mime_icon::mime_for_path(path);
533532
if mime == "application/x-desktop" {
533+
// Try opening desktop application
534534
match freedesktop_entry_parser::parse_entry(path) {
535535
Ok(entry) => match entry.section("Desktop Entry").attr("Exec") {
536536
Some(exec) => match mime_app::exec_to_command(exec, None) {
537537
Some(mut command) => match spawn_detached(&mut command) {
538538
Ok(()) => {
539-
found_desktop_exec = true;
539+
return;
540540
}
541541
Err(err) => {
542542
log::warn!("failed to execute {:?}: {}", path, err);
@@ -555,6 +555,7 @@ impl App {
555555
}
556556
}
557557
} else if mime == "application/x-executable" || mime == "application/vnd.appimage" {
558+
// Try opening executable
558559
let mut command = std::process::Command::new(path);
559560
match spawn_detached(&mut command) {
560561
Ok(()) => {}
@@ -571,23 +572,44 @@ impl App {
571572
}
572573
},
573574
}
574-
found_desktop_exec = true;
575+
return;
575576
}
576-
if !found_desktop_exec {
577-
match open::that_detached(path) {
577+
578+
// Try mime apps, which should be faster than xdg-open
579+
for app in mime_app::mime_apps(&mime) {
580+
let Some(mut command) = app.command(Some(path.clone().into())) else {
581+
continue;
582+
};
583+
match spawn_detached(&mut command) {
578584
Ok(()) => {
579585
let _ = recently_used_xbel::update_recently_used(
580586
path,
581587
App::APP_ID.to_string(),
582588
"cosmic-files".to_string(),
583589
None,
584590
);
591+
return;
585592
}
586593
Err(err) => {
587-
log::warn!("failed to open {:?}: {}", path, err);
594+
log::warn!("failed to open {:?} with {:?}: {}", path, app.id, err);
588595
}
589596
}
590597
}
598+
599+
// Fall back to using open crate
600+
match open::that_detached(path) {
601+
Ok(()) => {
602+
let _ = recently_used_xbel::update_recently_used(
603+
path,
604+
App::APP_ID.to_string(),
605+
"cosmic-files".to_string(),
606+
None,
607+
);
608+
}
609+
Err(err) => {
610+
log::warn!("failed to open {:?}: {}", path, err);
611+
}
612+
}
591613
}
592614

593615
fn open_tab_entity(

0 commit comments

Comments
 (0)