@@ -5,10 +5,13 @@ pub use action::Action;
55
66pub mod modifier;
77
8+ use action:: Direction ;
89pub use modifier:: { Modifier , Modifiers , ModifiersDef } ;
910
1011mod binding;
12+ mod gesture;
1113pub use binding:: Binding ;
14+ pub use gesture:: Gesture ;
1215
1316pub mod sym;
1417
@@ -49,6 +52,31 @@ pub fn shortcuts(context: &cosmic_config::Config) -> Shortcuts {
4952 shortcuts
5053}
5154
55+ /// Get the current system gesture configuration
56+ ///
57+ /// Merges user-defined custom gestures to the system default config
58+ pub fn gestures ( context : & cosmic_config:: Config ) -> Gestures {
59+ // Load gestures defined by the system.
60+ let mut gestures = context
61+ . get :: < Gestures > ( "default_gestures" )
62+ . unwrap_or_else ( |why| {
63+ tracing:: error!( "shortcuts defaults config error: {why:?}" ) ;
64+ Gestures :: default ( )
65+ } ) ;
66+
67+ // Load custom gestures defined by the user.
68+ let custom_gestures = context
69+ . get :: < Gestures > ( "custom_gestures" )
70+ . unwrap_or_else ( |why| {
71+ tracing:: error!( "shortcuts custom config error: {why:?}" ) ;
72+ Gestures :: default ( )
73+ } ) ;
74+
75+ // Combine while overriding system gestures.
76+ gestures. 0 . extend ( custom_gestures. 0 ) ;
77+ gestures
78+ }
79+
5280/// Get a map of system actions and their configured commands
5381pub fn system_actions ( context : & cosmic_config:: Config ) -> SystemActions {
5482 let mut config = SystemActions :: default ( ) ;
@@ -80,6 +108,8 @@ pub fn system_actions(context: &cosmic_config::Config) -> SystemActions {
80108pub struct Config {
81109 pub defaults : Shortcuts ,
82110 pub custom : Shortcuts ,
111+ pub default_gestures : Gestures ,
112+ pub custom_gestures : Gestures ,
83113 pub system_actions : SystemActions ,
84114}
85115
@@ -97,6 +127,18 @@ impl Config {
97127 . shortcut_for_action ( action)
98128 . or_else ( || self . defaults . shortcut_for_action ( action) )
99129 }
130+
131+ pub fn gestures ( & self ) -> impl Iterator < Item = ( & Gesture , & Action ) > {
132+ self . custom_gestures
133+ . iter ( )
134+ . chain ( self . default_gestures . iter ( ) )
135+ }
136+
137+ pub fn gesture_for_action ( & self , action : & Action ) -> Option < String > {
138+ self . custom_gestures
139+ . gesture_for_action ( action)
140+ . or_else ( || self . default_gestures . gesture_for_action ( action) )
141+ }
100142}
101143
102144/// A map of defined key [Binding]s and their triggerable [Action]s
@@ -261,3 +303,49 @@ impl<'de> Deserialize<'de> for SystemActionsImpl {
261303 deserializer. deserialize_map ( SystemActionsMapVisitor )
262304 }
263305}
306+
307+ /// A map of defined [Gesture]s and their triggerable [Action]s
308+ #[ derive( Clone , Debug , Default , PartialEq , Deserialize , Serialize ) ]
309+ #[ serde( transparent) ]
310+ pub struct Gestures ( pub HashMap < Gesture , Action > ) ;
311+
312+ impl Gestures {
313+ pub fn insert_default_gesture (
314+ & mut self ,
315+ fingers : u32 ,
316+ direction : Direction ,
317+ action : Action ,
318+ ) {
319+ if !self . 0 . values ( ) . any ( |a| a == & action) {
320+ let pattern = Gesture {
321+ description : None ,
322+ fingers,
323+ direction,
324+ } ;
325+ if !self . 0 . contains_key ( & pattern) {
326+ self . 0 . insert ( pattern, action. clone ( ) ) ;
327+ }
328+ }
329+ }
330+
331+ pub fn iter ( & self ) -> impl Iterator < Item = ( & Gesture , & Action ) > {
332+ self . 0 . iter ( )
333+ }
334+
335+ pub fn iter_mut ( & mut self ) -> impl Iterator < Item = ( & Gesture , & mut Action ) > {
336+ self . 0 . iter_mut ( )
337+ }
338+
339+ pub fn gesture_for_action ( & self , action : & Action ) -> Option < String > {
340+ self . gestures ( action)
341+ . next ( ) // take the first one
342+ . map ( |gesture| gesture. to_string ( ) )
343+ }
344+
345+ pub fn gestures < ' a > ( & ' a self , action : & ' a Action ) -> impl Iterator < Item = & ' a Gesture > {
346+ self . 0
347+ . iter ( )
348+ . filter ( move |( _, a) | * a == action)
349+ . map ( |( b, _) | b)
350+ }
351+ }
0 commit comments