Skip to content

Commit 1f71f3c

Browse files
Right click menu opt to create desktop shortcuts
Closes: #170
1 parent 3981b6a commit 1f71f3c

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ nix = "0.28"
4545
clap = { version = "4.4.8", features = ["derive"] }
4646
switcheroo-control = { git = "https://github.com/pop-os/dbus-settings-bindings" }
4747
cosmic-app-list-config = { git = "https://github.com/pop-os/cosmic-applets" }
48+
dirs = "5"
4849

4950
[profile.release]
5051
lto = "thin"

i18n/en/cosmic_app_library.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ run-on = Run on {$gpu}
1616
run-on-default = (Default)
1717
remove = Move to library home
1818
create-new = Create new folder
19+
add-desktop-shortcut = Add desktop shortcut
1920
add-group = Add group
2021
delete = Delete
2122
rename = Rename

src/app.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ use cosmic::widget::text::body;
4545
use cosmic::widget::{button, icon, search_input, text_input, tooltip, Checkbox, Column};
4646
use cosmic::{cctk::sctk, iced, Element, Theme};
4747
use cosmic_app_list_config::AppListConfig;
48-
use freedesktop_desktop_entry::PathSource;
48+
use freedesktop_desktop_entry::{DesktopEntry, PathSource};
4949
use itertools::Itertools;
5050
use log::error;
5151
use once_cell::sync::Lazy;
5252
use serde::{Deserialize, Serialize};
5353
use std::sync::Arc;
5454
use switcheroo_control::Gpu;
55+
use tokio::io::AsyncWriteExt;
5556

5657
use crate::app_group::AppLibraryConfig;
5758
use crate::fl;
@@ -284,6 +285,7 @@ enum Message {
284285
GpuUpdate(Option<Vec<Gpu>>),
285286
PinToAppTray(usize),
286287
UnPinFromAppTray(usize),
288+
DesktopShortcut(usize),
287289
AppListConfig(AppListConfig),
288290
}
289291

@@ -730,6 +732,20 @@ impl cosmic::Application for CosmicAppLibrary {
730732
.remove_pinned(&pinned_id, &app_list_helper);
731733
}
732734
}
735+
Message::DesktopShortcut(i) => {
736+
if let Some(entry) = self.entry_path_input.get(i) {
737+
let entry = Arc::clone(entry);
738+
return Command::perform(
739+
async move {
740+
if let Err(e) = desktop_shortcut(entry).await {
741+
error!("Failed copying desktop entry to desktop: {e:?}");
742+
}
743+
cosmic::app::Message::None
744+
},
745+
|x| x,
746+
);
747+
}
748+
}
733749
Message::AppListConfig(config) => {
734750
self.app_list_config = config;
735751
}
@@ -849,6 +865,13 @@ impl cosmic::Application for CosmicAppLibrary {
849865
list_column.push(menu_divider(spacing).into());
850866
list_column.push(pin_to_app_tray.into());
851867

868+
// Desktop shortcut
869+
list_column.push(
870+
menu_button(body(fl!("add-desktop-shortcut")))
871+
.on_press(Message::DesktopShortcut(*i))
872+
.into(),
873+
);
874+
852875
if self.cur_group > 0 {
853876
list_column.push(menu_divider(spacing).into());
854877
list_column.push(
@@ -1437,3 +1460,15 @@ fn menu_divider<'a>(spacing: &Spacing) -> Container<'a, Message, cosmic::Theme,
14371460
.padding([spacing.space_none, spacing.space_xxs])
14381461
.width(Length::Fill)
14391462
}
1463+
1464+
/// Copy application desktop entry to the user's desktop
1465+
async fn desktop_shortcut(entry: Arc<DesktopEntryData>) -> tokio::io::Result<u64> {
1466+
let source = entry
1467+
.path
1468+
.as_deref()
1469+
.ok_or(tokio::io::Error::other("Desktop entry doesn't have a path"))?;
1470+
let dest = dirs::desktop_dir()
1471+
.ok_or(tokio::io::Error::other("User doesn't have a desktop dir"))?
1472+
.join(format!("{}.desktop", &*entry.name));
1473+
tokio::fs::copy(source, dest).await
1474+
}

0 commit comments

Comments
 (0)