Skip to content

Commit c6408e0

Browse files
committed
Adding functionality to set default terminal in cosmic_settings_config when changed
1 parent 442589f commit c6408e0

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

Diff for: cosmic-settings/src/pages/system/default_apps.rs

+53-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ 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::{default_paths, get_languages_from_env, DesktopEntry, Iter as DesktopEntryIter};
1720
use mime_apps::App;
1821
use slotmap::SlotMap;
1922
use tokio::sync::mpsc;
@@ -81,6 +84,7 @@ pub struct AppMeta {
8184
pub struct Page {
8285
on_enter_handle: Option<cosmic::iced::task::Handle>,
8386
mime_apps: Option<CachedMimeApps>,
87+
shortcuts_config: Option<cosmic_config::Config>
8488
}
8589

8690
impl page::AutoBind<crate::pages::Message> for Page {}
@@ -107,6 +111,10 @@ impl page::Page<crate::pages::Message> for Page {
107111
handle.abort();
108112
}
109113

114+
if self.shortcuts_config.is_none() {
115+
self.shortcuts_config = cosmic_settings_config::shortcuts::context().ok();
116+
}
117+
110118
let (task, on_enter_handle) = Task::future(async move {
111119
let mut list = mime_apps::List::default();
112120
list.load_from_paths(&mime_apps::list_paths());
@@ -239,6 +247,50 @@ impl Page {
239247
if meta.selected != Some(id) {
240248
meta.selected = Some(id);
241249
let appid = &meta.app_ids[id];
250+
251+
// if we're changing terminal, we want to update the COSMIC system actions shortcut
252+
// for terminal
253+
if category == Category::Terminal && self.shortcuts_config.is_some() {
254+
let shortcuts = self.shortcuts_config.as_ref().unwrap();
255+
let system_actions = shortcuts.get::<SystemActions>("system_actions");
256+
match system_actions {
257+
Err(e) => {
258+
// we don't have system_actions set?
259+
println!("{}", e);
260+
}
261+
Ok(mut actions) => {
262+
// get the current terminal option, which should default to cosmic-term
263+
let current_terminal = actions.get(&System::Terminal);
264+
if current_terminal.is_some() {
265+
let default_paths = default_paths();
266+
let mut resolved_path = None;
267+
268+
// loop through all FDE paths to try and find a valid .desktop file
269+
for path in default_paths {
270+
let full_path = path.join([appid, ".desktop"].concat());
271+
if full_path.exists() && full_path.is_file() {
272+
resolved_path = Some(full_path);
273+
}
274+
}
275+
276+
// if we find a valid .desktop file, we can grab its exec
277+
if resolved_path.is_some() {
278+
let desktop_entry = DesktopEntry::from_path(resolved_path.unwrap(), Some(&get_languages_from_env()));
279+
if desktop_entry.is_ok() {
280+
// grab the exec, add it to system_actions and commit
281+
let exec = String::from(desktop_entry.unwrap().exec().unwrap());
282+
actions.insert(System::Terminal, exec);
283+
284+
if let Err(e) = shortcuts.set("system_actions", actions) {
285+
println!("{}", e);
286+
}
287+
}
288+
}
289+
}
290+
}
291+
}
292+
}
293+
242294
for mime in mime_types {
243295
if let Ok(mime) = mime.parse() {
244296
mime_apps

0 commit comments

Comments
 (0)