Skip to content

Commit

Permalink
remove lazy widget. Cache desktop entries
Browse files Browse the repository at this point in the history
I prefer the older way but..

Still doesn't work, but there is a popup
  • Loading branch information
wiiznokes committed Sep 4, 2024
1 parent b4d17c0 commit dd19f7f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
18 changes: 17 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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";
Expand All @@ -48,6 +51,7 @@ pub struct AppState {
pub focused: usize,
pub qr_code: Option<Result<qr_code::State, ()>>,
last_quit: Option<(i64, PopupKind)>,
pub desktop_entries: Vec<DesktopEntry<'static>>,
}

impl AppState {
Expand Down Expand Up @@ -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,
Expand All @@ -218,6 +230,7 @@ impl cosmic::Application for AppState {
qr_code: None,
config,
last_quit: None,
desktop_entries,
};

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -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()
}
Expand Down
33 changes: 14 additions & 19 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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::<Vec<_>>();
.collect::<Vec<_>>()

Column::with_children(res)
});
};

context_menu(
btn,
Expand All @@ -382,7 +377,7 @@ impl AppState {
),
menu::Tree::with_children(
text("Open with").width(Length::Fill),
vec![menu::Tree::new(open_with)],
open_with,
),
]),
)
Expand Down

0 comments on commit dd19f7f

Please sign in to comment.