Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect default terminal in cosmic_settings_config (keybind SUPER+T) when changed #975

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions Cargo.lock

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

54 changes: 53 additions & 1 deletion cosmic-settings/src/pages/system/default_apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {}
Expand All @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down