Skip to content

Commit

Permalink
Right click menu opt to create desktop shortcuts
Browse files Browse the repository at this point in the history
Closes: #170
  • Loading branch information
joshuamegnauth54 committed Oct 23, 2024
1 parent 3981b6a commit 1f71f3c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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"
Expand Down
1 change: 1 addition & 0 deletions i18n/en/cosmic_app_library.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 36 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ use cosmic::widget::text::body;
use cosmic::widget::{button, icon, search_input, text_input, tooltip, Checkbox, Column};
use cosmic::{cctk::sctk, iced, Element, Theme};
use cosmic_app_list_config::AppListConfig;
use freedesktop_desktop_entry::PathSource;
use freedesktop_desktop_entry::{DesktopEntry, PathSource};
use itertools::Itertools;
use log::error;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use switcheroo_control::Gpu;
use tokio::io::AsyncWriteExt;

use crate::app_group::AppLibraryConfig;
use crate::fl;
Expand Down Expand Up @@ -284,6 +285,7 @@ enum Message {
GpuUpdate(Option<Vec<Gpu>>),
PinToAppTray(usize),
UnPinFromAppTray(usize),
DesktopShortcut(usize),
AppListConfig(AppListConfig),
}

Expand Down Expand Up @@ -730,6 +732,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 Command::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;
}
Expand Down Expand Up @@ -849,6 +865,13 @@ impl cosmic::Application for CosmicAppLibrary {
list_column.push(menu_divider(spacing).into());
list_column.push(pin_to_app_tray.into());

// Desktop shortcut
list_column.push(
menu_button(body(fl!("add-desktop-shortcut")))
.on_press(Message::DesktopShortcut(*i))
.into(),
);

if self.cur_group > 0 {
list_column.push(menu_divider(spacing).into());
list_column.push(
Expand Down Expand Up @@ -1437,3 +1460,15 @@ fn menu_divider<'a>(spacing: &Spacing) -> Container<'a, Message, cosmic::Theme,
.padding([spacing.space_none, spacing.space_xxs])
.width(Length::Fill)
}

/// Copy application desktop entry to the user's desktop
async fn desktop_shortcut(entry: Arc<DesktopEntryData>) -> tokio::io::Result<u64> {
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
}

0 comments on commit 1f71f3c

Please sign in to comment.