Skip to content

Commit ca2d57e

Browse files
ellieplayswowmmstick
authored andcommitted
feat(default_apps): set default terminal in system_actions config
1 parent 8f9f287 commit ca2d57e

File tree

2 files changed

+71
-19
lines changed

2 files changed

+71
-19
lines changed

Cargo.lock

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

cosmic-settings/src/pages/system/default_apps.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ use cosmic::{
1212
widget::{self, dropdown, icon, settings},
1313
Apply, Element, Task,
1414
};
15+
use cosmic_config::{ConfigGet, ConfigSet};
16+
use cosmic_settings_config::shortcuts::action::System;
17+
use cosmic_settings_config::shortcuts::SystemActions;
1518
use cosmic_settings_page::{self as page, section, Section};
16-
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter as DesktopEntryIter};
19+
use freedesktop_desktop_entry::{
20+
default_paths, get_languages_from_env, DesktopEntry, Iter as DesktopEntryIter,
21+
};
1722
use mime_apps::App;
1823
use slotmap::SlotMap;
1924
use tokio::sync::mpsc;
@@ -81,6 +86,7 @@ pub struct AppMeta {
8186
pub struct Page {
8287
on_enter_handle: Option<cosmic::iced::task::Handle>,
8388
mime_apps: Option<CachedMimeApps>,
89+
shortcuts_config: Option<cosmic_config::Config>,
8490
}
8591

8692
impl page::AutoBind<crate::pages::Message> for Page {}
@@ -107,6 +113,10 @@ impl page::Page<crate::pages::Message> for Page {
107113
handle.abort();
108114
}
109115

116+
if self.shortcuts_config.is_none() {
117+
self.shortcuts_config = cosmic_settings_config::shortcuts::context().ok();
118+
}
119+
110120
let (task, on_enter_handle) = Task::future(async move {
111121
let mut list = mime_apps::List::default();
112122
list.load_from_paths(&mime_apps::list_paths());
@@ -239,6 +249,13 @@ impl Page {
239249
if meta.selected != Some(id) {
240250
meta.selected = Some(id);
241251
let appid = &meta.app_ids[id];
252+
253+
if category == Category::Terminal && self.shortcuts_config.is_some() {
254+
if let Some(config) = self.shortcuts_config.as_ref() {
255+
assign_default_terminal(config, appid);
256+
}
257+
}
258+
242259
for mime in mime_types {
243260
if let Ok(mime) = mime.parse() {
244261
mime_apps
@@ -368,6 +385,41 @@ fn apps() -> Section<crate::pages::Message> {
368385
})
369386
}
370387

388+
fn assign_default_terminal(config: &cosmic_config::Config, appid: &str) {
389+
let mut actions = config
390+
.get_local::<SystemActions>("system_actions")
391+
.unwrap_or_default();
392+
393+
let default_paths = default_paths();
394+
let mut resolved_path = None;
395+
396+
// loop through all FDE paths to try and find a valid .desktop file
397+
for path in default_paths {
398+
if let Ok(mut full_path) = path.canonicalize() {
399+
full_path = full_path.join([appid, ".desktop"].concat());
400+
if full_path.exists() && full_path.is_file() {
401+
resolved_path = Some(full_path);
402+
break;
403+
}
404+
}
405+
}
406+
407+
// if we find a valid .desktop file, we can grab its exec
408+
if let Some(resolved_path) = resolved_path {
409+
let desktop_entry = DesktopEntry::from_path(resolved_path, Some(&get_languages_from_env()));
410+
411+
if let Ok(desktop_entry) = desktop_entry {
412+
if let Some(exec) = desktop_entry.exec() {
413+
actions.insert(System::Terminal, String::from(exec));
414+
415+
if let Err(why) = config.set("system_actions", actions) {
416+
tracing::error!(?why, "Unable to set system_actions shortcuts config");
417+
}
418+
}
419+
}
420+
}
421+
}
422+
371423
async fn load_defaults(assocs: &BTreeMap<Arc<str>, Arc<App>>, for_mimes: &[&str]) -> AppMeta {
372424
let mut unsorted = Vec::new();
373425
let mut current_app = None;

0 commit comments

Comments
 (0)