Skip to content

Commit 977a242

Browse files
committed
add watcher for shortcuts changes for COSMIC, fixes #21
1 parent 974ee52 commit 977a242

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shortcuts-overlay"
3-
version = "0.1.3"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "Apache-2.0"
66
description = "A keyboard shortcuts overlay for the COSMIC DE and other Wayland compositors"

src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,19 @@ fn main() -> Result<()> {
6161

6262
// watcher code
6363
let state_clone = Arc::clone(&state);
64-
let handle = std::thread::spawn(|| {
64+
let watch_config_handle = std::thread::spawn(|| {
6565
watcher::watch_overlay_config(state_clone).unwrap();
6666
});
67+
let state_clone_keybindings = Arc::clone(&state);
68+
let watch_shortcuts_handle = std::thread::spawn(|| {
69+
watcher::watch_shortcuts(state_clone_keybindings, xdg_desktop).unwrap();
70+
});
71+
6772
// start
6873
overlay::start(state)?;
6974

70-
handle.join().unwrap();
75+
watch_config_handle.join().unwrap();
76+
watch_shortcuts_handle.join().unwrap();
7177

7278
Ok(())
7379
}

src/watcher.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use crate::keybinding_reader::load_cosmic_shortcuts;
12
use crate::state::State;
3+
use crate::util::XDGDesktop;
24
use notify::{Event, RecursiveMode, Result, Watcher};
35
use std::sync::Arc;
46
use std::{path::Path, sync::mpsc};
57

68
const OVERLAY_CONFIG_FILE: &str = "/usr/share/shortcuts-overlay/overlay-config.toml";
9+
const COSMIC_SHORTCUTS_DIR: &str = ".config/cosmic/com.system76.CosmicSettings.Shortcuts/";
710

811
pub(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

Comments
 (0)