Better isolation of window change event handling code

This commit is contained in:
Your Name
2023-02-20 20:39:02 -05:00
parent f7fde32897
commit 671814fc81
2 changed files with 5 additions and 6 deletions

View File

@@ -16,7 +16,7 @@ use std::{
};
use sm_session_notifier::SMSessionNotifier;
use window_change::{win_event_hook_loop, WIN_CHANGE_CALLBACK};
use window_change::{await_win_change_events};
use windows::{
core::Interface,
Win32::{
@@ -131,9 +131,7 @@ fn main() {
.boot_devices()
.expect("failed to get initial audio devices and sessions");
*WIN_CHANGE_CALLBACK.lock().unwrap() = Some(Box::new(move |s| {
await_win_change_events(Box::new(move |s| {
muter.lock().unwrap().notify_window_changed(&s)
}));
win_event_hook_loop();
}

View File

@@ -14,7 +14,7 @@ use windows::Win32::{
use crate::pid_to_exe::pid_to_exe_path;
pub static WIN_CHANGE_CALLBACK: Mutex<Option<Box<dyn Fn(&str)+Send>>> = Mutex::new(None);
static WIN_CHANGE_CALLBACK: Mutex<Option<Box<dyn Fn(&str)+Send>>> = Mutex::new(None);
unsafe extern "system" fn win_event_proc(
_hook: HWINEVENTHOOK,
@@ -48,7 +48,8 @@ unsafe extern "system" fn win_event_proc(
}
}
pub fn win_event_hook_loop() {
pub fn await_win_change_events(callback: Box<dyn Fn(&str) + Send>) {
*WIN_CHANGE_CALLBACK.lock().unwrap() = Some(callback);
unsafe {
SetWinEventHook(
EVENT_SYSTEM_FOREGROUND,