fix(core): prevent keyboard layout watcher from stopping when receiving invalid layout

This commit is contained in:
Federico Terzi 2021-10-11 19:11:13 +02:00
parent 2c25d60e87
commit ceade8d172

View File

@ -19,7 +19,7 @@
use anyhow::Result; use anyhow::Result;
use crossbeam::channel::Sender; use crossbeam::channel::Sender;
use log::{debug, error}; use log::{debug, error, warn};
const WATCHER_INTERVAL: u64 = 1000; const WATCHER_INTERVAL: u64 = 1000;
@ -39,23 +39,21 @@ pub fn initialize_and_spawn(watcher_notify: Sender<()>) -> Result<()> {
} }
fn watcher_main(watcher_notify: &Sender<()>) { fn watcher_main(watcher_notify: &Sender<()>) {
let layout = espanso_detect::get_active_layout(); let mut layout = espanso_detect::get_active_layout();
if layout.is_none() { if layout.is_none() {
error!("unable to start keyboard layout watcher, as espanso couldn't determine active layout."); warn!("keyboard layout watcher couldn't determine active layout.")
return;
} }
let mut layout = layout.expect("missing active layout");
loop { loop {
std::thread::sleep(std::time::Duration::from_millis(WATCHER_INTERVAL)); std::thread::sleep(std::time::Duration::from_millis(WATCHER_INTERVAL));
if let Some(current_layout) = espanso_detect::get_active_layout() { let current_layout = espanso_detect::get_active_layout();
if current_layout != layout { if current_layout != layout {
debug!( debug!(
"detected keyboard layout change: from {} to {}", "detected keyboard layout change: from '{}' to '{}'",
layout, current_layout layout.as_deref().unwrap_or_default(),
current_layout.as_deref().unwrap_or_default(),
); );
if let Err(error) = watcher_notify.send(()) { if let Err(error) = watcher_notify.send(()) {
@ -64,9 +62,5 @@ fn watcher_main(watcher_notify: &Sender<()>) {
layout = current_layout; layout = current_layout;
} }
} else {
error!("keyboard layout watcher couldn't determine active layout");
break;
}
} }
} }