1+ use crate :: keybinding_reader:: load_cosmic_shortcuts;
12use crate :: state:: State ;
3+ use crate :: util:: XDGDesktop ;
24use notify:: { Event , RecursiveMode , Result , Watcher } ;
35use std:: sync:: Arc ;
46use std:: { path:: Path , sync:: mpsc} ;
57
68const OVERLAY_CONFIG_FILE : & str = "/usr/share/shortcuts-overlay/overlay-config.toml" ;
9+ const COSMIC_SHORTCUTS_DIR : & str = ".config/cosmic/com.system76.CosmicSettings.Shortcuts/" ;
710
811pub ( crate ) fn watch_overlay_config ( state : Arc < State > ) -> Result < ( ) > {
912 let ( tx, rx) = mpsc:: channel :: < Result < Event > > ( ) ;
@@ -28,3 +31,33 @@ pub(crate) fn watch_overlay_config(state: Arc<State>) -> Result<()> {
2831
2932 Ok ( ( ) )
3033}
34+
35+ pub ( crate ) fn watch_shortcuts ( state : Arc < State > , desktop : XDGDesktop ) -> Result < ( ) > {
36+ let ( tx, rx) = mpsc:: channel :: < Result < Event > > ( ) ;
37+
38+ let mut watcher = notify:: recommended_watcher ( tx) ?;
39+
40+ let path = match desktop {
41+ XDGDesktop :: COSMIC => dirs:: home_dir ( ) . unwrap ( ) . join ( COSMIC_SHORTCUTS_DIR ) ,
42+ XDGDesktop :: NIRI => unimplemented ! ( ) ,
43+ } ;
44+
45+ // Add a path to be watched. All files and directories at that path and
46+ // below will be monitored for changes.
47+ watcher. watch ( & path, RecursiveMode :: Recursive ) ?;
48+ // Block forever, printing out events as they come in
49+ for res in rx {
50+ match res {
51+ Ok ( event) => {
52+ log:: debug!( "event: {:?}" , event) ;
53+ if event. kind . is_modify ( ) {
54+ let shortcuts = load_cosmic_shortcuts ( ) . unwrap_or_default ( ) ;
55+ state. _set_keybindings ( shortcuts) ;
56+ }
57+ }
58+ Err ( e) => log:: error!( "watch error: {:?}" , e) ,
59+ }
60+ }
61+
62+ Ok ( ( ) )
63+ }
0 commit comments