refactor: move mime app logic to mime_app module
This commit is contained in:
parent
64998b37a1
commit
3cd0d0c00a
2 changed files with 53 additions and 57 deletions
59
src/app.rs
59
src/app.rs
|
|
@ -67,7 +67,7 @@ use crate::config::{
|
|||
use crate::dialog::{Dialog, DialogKind, DialogMessage, DialogResult, DialogSettings};
|
||||
use crate::key_bind::key_binds;
|
||||
use crate::localize::LANGUAGE_SORTER;
|
||||
use crate::mime_app::{self, MimeApp, MimeAppCache};
|
||||
use crate::mime_app::{self, MimeApp, MimeAppCache, MimeAppMatch};
|
||||
use crate::mounter::{
|
||||
MOUNTERS, MounterAuth, MounterItem, MounterItems, MounterKey, MounterMessage,
|
||||
};
|
||||
|
|
@ -656,13 +656,6 @@ impl DialogPages {
|
|||
|
||||
pub struct FavoriteIndex(usize);
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
enum MimeAppMatch {
|
||||
Exact,
|
||||
Related,
|
||||
Other,
|
||||
}
|
||||
|
||||
pub struct MounterData(MounterKey, MounterItem);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
|
|
@ -2274,52 +2267,6 @@ impl App {
|
|||
.into()
|
||||
}
|
||||
|
||||
fn get_apps_for_mime(&self, mime_type: &Mime) -> Vec<(&Arc<MimeApp>, MimeAppMatch)> {
|
||||
let mut results = Vec::new();
|
||||
|
||||
let mut dedupe = FxHashSet::default();
|
||||
|
||||
// start with exact matches
|
||||
results.extend(
|
||||
self.mime_app_cache
|
||||
.get(mime_type)
|
||||
.iter()
|
||||
.filter(|&mime_app| dedupe.insert(&mime_app.id))
|
||||
.map(|mime_app| (mime_app, MimeAppMatch::Exact)),
|
||||
);
|
||||
|
||||
// grab matches based off of subclass / parent mime type
|
||||
if let Some(parent_types) = mime_icon::parent_mime_types(mime_type) {
|
||||
for parent_type in parent_types {
|
||||
results.extend(
|
||||
self.mime_app_cache
|
||||
.get(&parent_type)
|
||||
.iter()
|
||||
.filter(|&mime_app| dedupe.insert(&mime_app.id))
|
||||
.map(|mime_app| (mime_app, MimeAppMatch::Related)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Add other apps
|
||||
results.extend({
|
||||
let mut apps = self
|
||||
.mime_app_cache
|
||||
.apps()
|
||||
.iter()
|
||||
.filter(|mime_app| !mime_app.no_display())
|
||||
.filter(|&mime_app| dedupe.insert(&mime_app.id))
|
||||
.map(|mime_app| (mime_app, MimeAppMatch::Other))
|
||||
.collect::<Vec<_>>();
|
||||
apps.sort_by(|(a, _), (b, _)| {
|
||||
crate::localize::LANGUAGE_SORTER.compare(&a.name, &b.name)
|
||||
});
|
||||
apps
|
||||
});
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
// Update favorites based on renaming or moving dirs.
|
||||
fn update_favorites(&mut self, path_changes: &[(impl AsRef<Path>, impl AsRef<Path>)]) -> bool {
|
||||
let mut favorites_changed = false;
|
||||
|
|
@ -3233,7 +3180,7 @@ impl Application for App {
|
|||
selected,
|
||||
..
|
||||
} => {
|
||||
let available_apps = self.get_apps_for_mime(&mime);
|
||||
let available_apps = self.mime_app_cache.get_apps_for_mime(&mime);
|
||||
|
||||
if let Some((app, _)) = available_apps.get(selected) {
|
||||
if let Some(mut command) =
|
||||
|
|
@ -5971,7 +5918,7 @@ impl Application for App {
|
|||
};
|
||||
|
||||
let mut column = widget::list_column();
|
||||
let available_apps = self.get_apps_for_mime(mime);
|
||||
let available_apps = self.mime_app_cache.get_apps_for_mime(mime);
|
||||
let item_height = 32.0;
|
||||
let mut displayed_default = false;
|
||||
let mut last_kind = MimeAppMatch::Exact;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use cosmic::widget;
|
|||
pub use mime_guess::Mime;
|
||||
#[cfg(feature = "desktop")]
|
||||
use notify_debouncer_full::notify;
|
||||
use rustc_hash::FxHashMap;
|
||||
use rustc_hash::{FxHashMap, FxHashSet};
|
||||
use std::ffi::OsStr;
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
|
@ -176,6 +176,13 @@ pub fn exec_to_command(
|
|||
Some(commands)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum MimeAppMatch {
|
||||
Exact,
|
||||
Related,
|
||||
Other,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct MimeApp {
|
||||
pub id: String,
|
||||
|
|
@ -245,6 +252,48 @@ impl MimeAppCache {
|
|||
mime_app_cache
|
||||
}
|
||||
|
||||
pub fn get_apps_for_mime(&self, mime_type: &Mime) -> Vec<(&Arc<MimeApp>, MimeAppMatch)> {
|
||||
let mut results = Vec::new();
|
||||
|
||||
let mut dedupe = FxHashSet::default();
|
||||
|
||||
// start with exact matches
|
||||
results.extend(
|
||||
self.get(mime_type)
|
||||
.iter()
|
||||
.filter(|&mime_app| dedupe.insert(&mime_app.id))
|
||||
.map(|mime_app| (mime_app, MimeAppMatch::Exact)),
|
||||
);
|
||||
|
||||
// grab matches based off of subclass / parent mime type
|
||||
if let Some(parent_types) = crate::mime_icon::parent_mime_types(mime_type) {
|
||||
for parent_type in parent_types {
|
||||
results.extend(
|
||||
self.get(&parent_type)
|
||||
.iter()
|
||||
.filter(|&mime_app| dedupe.insert(&mime_app.id))
|
||||
.map(|mime_app| (mime_app, MimeAppMatch::Related)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
results.extend({
|
||||
let mut apps = self
|
||||
.apps()
|
||||
.iter()
|
||||
.filter(|mime_app| !mime_app.no_display())
|
||||
.filter(|&mime_app| dedupe.insert(&mime_app.id))
|
||||
.map(|mime_app| (mime_app, MimeAppMatch::Other))
|
||||
.collect::<Vec<_>>();
|
||||
apps.sort_by(|(a, _), (b, _)| {
|
||||
crate::localize::LANGUAGE_SORTER.compare(&a.name, &b.name)
|
||||
});
|
||||
apps
|
||||
});
|
||||
|
||||
results
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "desktop"))]
|
||||
pub fn reload(&mut self) {}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue