From 9626a9943d493206f9e3383a524d65a4de509459 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Wed, 23 Oct 2024 02:16:56 -0400 Subject: [PATCH] Right click menu to create desktop shortcuts Closes: #170 --- Cargo.lock | 1 + Cargo.toml | 1 + i18n/en/cosmic_app_library.ftl | 1 + src/app.rs | 36 +++++++++++++++++++++++++++++++++- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index a4ff002..6227aef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1099,6 +1099,7 @@ dependencies = [ "clap", "cosmic-app-list-config", "current_locale", + "dirs 5.0.1", "freedesktop-desktop-entry", "freedesktop-icons", "futures", diff --git a/Cargo.toml b/Cargo.toml index c57c935..b035488 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ nix = "0.28" clap = { version = "4.4.8", features = ["derive"] } switcheroo-control = { git = "https://github.com/pop-os/dbus-settings-bindings" } cosmic-app-list-config = { git = "https://github.com/pop-os/cosmic-applets" } +dirs = "5" [profile.release] lto = "thin" diff --git a/i18n/en/cosmic_app_library.ftl b/i18n/en/cosmic_app_library.ftl index 41344da..f49b931 100644 --- a/i18n/en/cosmic_app_library.ftl +++ b/i18n/en/cosmic_app_library.ftl @@ -16,6 +16,7 @@ run-on = Run on {$gpu} run-on-default = (Default) remove = Move to library home create-new = Create new folder +add-desktop-shortcut = Add desktop shortcut add-group = Add group delete = Delete rename = Rename diff --git a/src/app.rs b/src/app.rs index 3a5de35..4dbe415 100644 --- a/src/app.rs +++ b/src/app.rs @@ -312,6 +312,7 @@ enum Message { GpuUpdate(Option>), PinToAppTray(usize), UnPinFromAppTray(usize), + DesktopShortcut(usize), AppListConfig(AppListConfig), } @@ -760,6 +761,20 @@ impl cosmic::Application for CosmicAppLibrary { .remove_pinned(&pinned_id, &app_list_helper); } } + Message::DesktopShortcut(i) => { + if let Some(entry) = self.entry_path_input.get(i) { + let entry = Arc::clone(entry); + return Task::perform( + async move { + if let Err(e) = desktop_shortcut(entry).await { + error!("Failed copying desktop entry to desktop: {e:?}"); + } + cosmic::app::Message::None + }, + |x| x, + ); + } + } Message::AppListConfig(config) => { self.app_list_config = config; } @@ -847,6 +862,14 @@ impl cosmic::Application for CosmicAppLibrary { } } + // Desktop shortcut + list_column.push(divider::horizontal::light().into()); + list_column.push( + menu_button(body(fl!("add-desktop-shortcut"))) + .on_press(Message::DesktopShortcut(*i)) + .into(), + ); + // add to pinned let svg_accent = Rc::new(|theme: &cosmic::Theme| { let color = theme.cosmic().accent_color().into(); @@ -870,7 +893,6 @@ impl cosmic::Application for CosmicAppLibrary { } else { Message::PinToAppTray(*i) }); - list_column.push(divider::horizontal::light().into()); list_column.push(pin_to_app_tray.into()); if self.cur_group > 0 { @@ -1448,3 +1470,15 @@ impl cosmic::Application for CosmicAppLibrary { (self_, Task::none()) } } + +/// Copy application desktop entry to the user's desktop +async fn desktop_shortcut(entry: Arc) -> tokio::io::Result { + let source = entry + .path + .as_deref() + .ok_or(tokio::io::Error::other("Desktop entry doesn't have a path"))?; + let dest = dirs::desktop_dir() + .ok_or(tokio::io::Error::other("User doesn't have a desktop dir"))? + .join(format!("{}.desktop", &*entry.name)); + tokio::fs::copy(source, dest).await +}