11mod common;
2+
23pub use common:: { Model , ShortcutBinding , ShortcutMessage , ShortcutModel } ;
34
45pub mod custom;
@@ -16,11 +17,14 @@ use cosmic_settings_config::shortcuts::action::{
1617 Direction , FocusDirection , Orientation , ResizeDirection ,
1718} ;
1819use cosmic_settings_config:: shortcuts:: { self , Action , Shortcuts } ;
20+ use cosmic_settings_config:: Binding ;
1921use cosmic_settings_page:: Section ;
2022use cosmic_settings_page:: { self as page, section} ;
23+ use itertools:: Itertools ;
2124use shortcuts:: action:: System as SystemAction ;
2225use slab:: Slab ;
2326use slotmap:: { DefaultKey , Key , SecondaryMap , SlotMap } ;
27+ use std:: io;
2428
2529pub struct Page {
2630 modified : Modified ,
@@ -49,11 +53,11 @@ struct SubPages {
4953 window_tiling : page:: Entity ,
5054}
5155
52- #[ derive( Default ) ]
5356struct Search {
5457 input : String ,
5558 actions : SlotMap < DefaultKey , Action > ,
5659 localized : SecondaryMap < DefaultKey , String > ,
60+ config : cosmic_config:: Config ,
5761 shortcuts : Shortcuts ,
5862 defaults : Shortcuts ,
5963}
@@ -248,7 +252,6 @@ impl Page {
248252 self . search_model . on_clear ( ) ;
249253 return ;
250254 }
251-
252255 if self . search . actions . is_empty ( ) {
253256 self . search . cache_localized_actions ( ) ;
254257 }
@@ -280,16 +283,52 @@ impl page::AutoBind<crate::pages::Message> for Page {
280283 }
281284}
282285
286+ impl Default for Search {
287+ fn default ( ) -> Self {
288+ Self {
289+ input : String :: default ( ) ,
290+ defaults : Shortcuts :: default ( ) ,
291+ config : shortcuts:: context ( ) . unwrap ( ) ,
292+ localized : SecondaryMap :: default ( ) ,
293+ actions : SlotMap :: new ( ) ,
294+ shortcuts : Shortcuts :: default ( ) ,
295+ }
296+ }
297+ }
298+
283299impl Search {
284300 fn cache_localized_actions ( & mut self ) {
285301 self . actions . clear ( ) ;
286302 self . localized . clear ( ) ;
287-
288- for action in all_actions ( ) {
303+ let custom_actions = self . retrieve_custom_actions ( ) ;
304+ for action in all_system_actions ( ) {
289305 let localized = localize_action ( action) ;
290306 let id = self . actions . insert ( action. clone ( ) ) ;
291307 self . localized . insert ( id, localized) ;
292308 }
309+ for ( binding, action) in custom_actions {
310+ let localized = localize_custom_action ( & action, & binding) ;
311+ let id = self . actions . insert ( action. clone ( ) ) ;
312+ self . localized . insert ( id, localized) ;
313+ }
314+ }
315+
316+ fn retrieve_custom_actions ( & self ) -> Vec < ( Binding , Action ) > {
317+ let custom_shortcusts = match self . config . get :: < Shortcuts > ( "custom" ) {
318+ Ok ( shortcuts) => shortcuts,
319+ Err ( cosmic_config:: Error :: GetKey ( _, why) ) if why. kind ( ) == io:: ErrorKind :: NotFound => {
320+ Shortcuts :: default ( )
321+ }
322+ Err ( why) => {
323+ tracing:: error!( ?why, "unable to get the current shortcuts config" ) ;
324+ Shortcuts :: default ( )
325+ }
326+ } ;
327+ custom_shortcusts
328+ . 0
329+ . into_iter ( )
330+ . unique_by ( |( _, action) | localize_action ( action) )
331+ . collect :: < Vec < ( Binding , Action ) > > ( )
293332 }
294333
295334 fn shortcut_models ( & mut self ) -> Slab < ShortcutModel > {
@@ -418,7 +457,7 @@ fn action_category(action: &Action) -> Option<Category> {
418457 } )
419458}
420459
421- fn all_actions ( ) -> & ' static [ Action ] {
460+ fn all_system_actions ( ) -> & ' static [ Action ] {
422461 & [
423462 Action :: Close ,
424463 Action :: Debug ,
@@ -625,3 +664,11 @@ fn localize_action(action: &Action) -> String {
625664 Action :: Spawn ( command) => command. clone ( ) ,
626665 }
627666}
667+
668+ fn localize_custom_action ( action : & Action , binding : & Binding ) -> String {
669+ if let Some ( description) = & binding. description {
670+ description. to_string ( )
671+ } else {
672+ localize_action ( & action)
673+ }
674+ }
0 commit comments