@@ -12,8 +12,13 @@ use cosmic::{
12
12
widget:: { self , dropdown, icon, settings} ,
13
13
Apply , Element , Task ,
14
14
} ;
15
+ use cosmic_config:: { ConfigGet , ConfigSet } ;
16
+ use cosmic_settings_config:: shortcuts:: action:: System ;
17
+ use cosmic_settings_config:: shortcuts:: SystemActions ;
15
18
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
+ } ;
17
22
use mime_apps:: App ;
18
23
use slotmap:: SlotMap ;
19
24
use tokio:: sync:: mpsc;
@@ -81,6 +86,7 @@ pub struct AppMeta {
81
86
pub struct Page {
82
87
on_enter_handle : Option < cosmic:: iced:: task:: Handle > ,
83
88
mime_apps : Option < CachedMimeApps > ,
89
+ shortcuts_config : Option < cosmic_config:: Config > ,
84
90
}
85
91
86
92
impl page:: AutoBind < crate :: pages:: Message > for Page { }
@@ -107,6 +113,10 @@ impl page::Page<crate::pages::Message> for Page {
107
113
handle. abort ( ) ;
108
114
}
109
115
116
+ if self . shortcuts_config . is_none ( ) {
117
+ self . shortcuts_config = cosmic_settings_config:: shortcuts:: context ( ) . ok ( ) ;
118
+ }
119
+
110
120
let ( task, on_enter_handle) = Task :: future ( async move {
111
121
let mut list = mime_apps:: List :: default ( ) ;
112
122
list. load_from_paths ( & mime_apps:: list_paths ( ) ) ;
@@ -239,6 +249,13 @@ impl Page {
239
249
if meta. selected != Some ( id) {
240
250
meta. selected = Some ( id) ;
241
251
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
+
242
259
for mime in mime_types {
243
260
if let Ok ( mime) = mime. parse ( ) {
244
261
mime_apps
@@ -368,6 +385,41 @@ fn apps() -> Section<crate::pages::Message> {
368
385
} )
369
386
}
370
387
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
+
371
423
async fn load_defaults ( assocs : & BTreeMap < Arc < str > , Arc < App > > , for_mimes : & [ & str ] ) -> AppMeta {
372
424
let mut unsorted = Vec :: new ( ) ;
373
425
let mut current_app = None ;
0 commit comments