feat(default_apps): set default terminal in system_actions config

This commit is contained in:
ellieplayswow 2025-02-14 10:44:59 +00:00 committed by Michael Murphy
parent 8f9f287db0
commit 777cbae425
2 changed files with 71 additions and 19 deletions

View file

@ -12,8 +12,13 @@ use cosmic::{
widget::{self, dropdown, icon, settings},
Apply, Element, Task,
};
use cosmic_config::{ConfigGet, ConfigSet};
use cosmic_settings_config::shortcuts::action::System;
use cosmic_settings_config::shortcuts::SystemActions;
use cosmic_settings_page::{self as page, section, Section};
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter as DesktopEntryIter};
use freedesktop_desktop_entry::{
default_paths, get_languages_from_env, DesktopEntry, Iter as DesktopEntryIter,
};
use mime_apps::App;
use slotmap::SlotMap;
use tokio::sync::mpsc;
@ -81,6 +86,7 @@ pub struct AppMeta {
pub struct Page {
on_enter_handle: Option<cosmic::iced::task::Handle>,
mime_apps: Option<CachedMimeApps>,
shortcuts_config: Option<cosmic_config::Config>,
}
impl page::AutoBind<crate::pages::Message> for Page {}
@ -107,6 +113,10 @@ impl page::Page<crate::pages::Message> for Page {
handle.abort();
}
if self.shortcuts_config.is_none() {
self.shortcuts_config = cosmic_settings_config::shortcuts::context().ok();
}
let (task, on_enter_handle) = Task::future(async move {
let mut list = mime_apps::List::default();
list.load_from_paths(&mime_apps::list_paths());
@ -239,6 +249,13 @@ impl Page {
if meta.selected != Some(id) {
meta.selected = Some(id);
let appid = &meta.app_ids[id];
if category == Category::Terminal && self.shortcuts_config.is_some() {
if let Some(config) = self.shortcuts_config.as_ref() {
assign_default_terminal(config, appid);
}
}
for mime in mime_types {
if let Ok(mime) = mime.parse() {
mime_apps
@ -368,6 +385,41 @@ fn apps() -> Section<crate::pages::Message> {
})
}
fn assign_default_terminal(config: &cosmic_config::Config, appid: &str) {
let mut actions = config
.get_local::<SystemActions>("system_actions")
.unwrap_or_default();
let default_paths = default_paths();
let mut resolved_path = None;
// loop through all FDE paths to try and find a valid .desktop file
for path in default_paths {
if let Ok(mut full_path) = path.canonicalize() {
full_path = full_path.join([appid, ".desktop"].concat());
if full_path.exists() && full_path.is_file() {
resolved_path = Some(full_path);
break;
}
}
}
// if we find a valid .desktop file, we can grab its exec
if let Some(resolved_path) = resolved_path {
let desktop_entry = DesktopEntry::from_path(resolved_path, Some(&get_languages_from_env()));
if let Ok(desktop_entry) = desktop_entry {
if let Some(exec) = desktop_entry.exec() {
actions.insert(System::Terminal, String::from(exec));
if let Err(why) = config.set("system_actions", actions) {
tracing::error!(?why, "Unable to set system_actions shortcuts config");
}
}
}
}
}
async fn load_defaults(assocs: &BTreeMap<Arc<str>, Arc<App>>, for_mimes: &[&str]) -> AppMeta {
let mut unsorted = Vec::new();
let mut current_app = None;