diff --git a/src/app.rs b/src/app.rs index 1cdfa60..98ecec2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -20,6 +20,7 @@ use cosmic::iced_widget::{qr_code, Column}; use cosmic::widget::{button, icon, text, text_input, MouseArea, Space}; use cosmic::{Element, Theme}; +use freedesktop_desktop_entry::DesktopEntry; use futures::executor::block_on; use crate::config::{Config, CONFIG_VERSION, PRIVATE_MODE}; @@ -33,6 +34,8 @@ use cosmic::cosmic_config; use std::sync::atomic::{self, AtomicBool}; use std::thread; +use freedesktop_desktop_entry as fde; + pub const QUALIFIER: &str = "io.github"; pub const ORG: &str = "wiiznokes"; pub const APP: &str = "cosmic-ext-applet-clipboard-manager"; @@ -48,6 +51,7 @@ pub struct AppState { pub focused: usize, pub qr_code: Option>, last_quit: Option<(i64, PopupKind)>, + pub desktop_entries: Vec>, } impl AppState { @@ -208,6 +212,14 @@ impl cosmic::Application for AppState { let db = block_on(async { db::Db::new(&config).await.unwrap() }); + let mut desktop_entries = Vec::new(); + + for path in fde::Iter::new(fde::default_paths()) { + if let Ok(e) = DesktopEntry::from_path(path, Some(&[] as &[&str])) { + desktop_entries.push(e); + } + } + let window = AppState { core, config_handler: flags.config_handler, @@ -218,6 +230,7 @@ impl cosmic::Application for AppState { qr_code: None, config, last_quit: None, + desktop_entries, }; #[cfg(debug_assertions)] @@ -388,7 +401,10 @@ impl cosmic::Application for AppState { } }, AppMsg::Open(_) => todo!(), - AppMsg::OpenWith { entry, desktop_entry } => todo!(), + AppMsg::OpenWith { + entry, + desktop_entry, + } => todo!(), } Command::none() } diff --git a/src/view.rs b/src/view.rs index b9b9d51..4b873db 100644 --- a/src/view.rs +++ b/src/view.rs @@ -329,9 +329,9 @@ impl AppState { btn.width(Length::Fill).into() }; - let open_with = Lazy::new(entry, |entry| { - println!("lazy"); - + let open_with = { + // println!("lazy"); + let mut mimes = vec![entry.mime.as_ref()]; if let Ok(Content::Text(content)) = entry.get_content() { @@ -340,31 +340,26 @@ impl AppState { } } - let res = fde::DesktopEntry::from_paths::<&str>( - fde::Iter::new(fde::default_paths()), - Some(&[]), - ) - .filter_map(|e| { - e.ok().and_then(|e| { + self + .desktop_entries + .iter() + .filter_map(|e| { e.mime_type() .unwrap_or_default() .iter() .any(|e| mimes.contains(e)) - .then_some( + .then_some(menu::Tree::new( button(text(e.appid.clone())) .on_press(AppMsg::OpenWith { entry: (*entry).clone(), - desktop_entry: e, + desktop_entry: e.clone(), }) - .width(Length::Fill) - .into(), - ) + .width(Length::Fill), + )) }) - }) - .collect::>(); + .collect::>() - Column::with_children(res) - }); + }; context_menu( btn, @@ -382,7 +377,7 @@ impl AppState { ), menu::Tree::with_children( text("Open with").width(Length::Fill), - vec![menu::Tree::new(open_with)], + open_with, ), ]), )