Better isolation of window change event handling code
This commit is contained in:
@@ -16,7 +16,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use sm_session_notifier::SMSessionNotifier;
|
use sm_session_notifier::SMSessionNotifier;
|
||||||
use window_change::{win_event_hook_loop, WIN_CHANGE_CALLBACK};
|
use window_change::{await_win_change_events};
|
||||||
use windows::{
|
use windows::{
|
||||||
core::Interface,
|
core::Interface,
|
||||||
Win32::{
|
Win32::{
|
||||||
@@ -131,9 +131,7 @@ fn main() {
|
|||||||
.boot_devices()
|
.boot_devices()
|
||||||
.expect("failed to get initial audio devices and sessions");
|
.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)
|
muter.lock().unwrap().notify_window_changed(&s)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
win_event_hook_loop();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use windows::Win32::{
|
|||||||
|
|
||||||
use crate::pid_to_exe::pid_to_exe_path;
|
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(
|
unsafe extern "system" fn win_event_proc(
|
||||||
_hook: HWINEVENTHOOK,
|
_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 {
|
unsafe {
|
||||||
SetWinEventHook(
|
SetWinEventHook(
|
||||||
EVENT_SYSTEM_FOREGROUND,
|
EVENT_SYSTEM_FOREGROUND,
|
||||||
|
|||||||
Reference in New Issue
Block a user