fix(wm): ensure window msg threads are stopped

This commit ensures that message handling threads for windows that are
created by komorebi are exited properly when the windows are destroyed.

For some reason, passing the HWND returned by CreateWindowExW to
GetMessageW continues returning TRUE even after the window has been
destroyed.

If HWND::NULL (via the Default trait) is passed to GetMessageW, which
retrieves messages for any window belonging to the current thread (our
threads here only own a single window), GetMessageW returns FALSE as
expected after the window is destroyed.

re #862
This commit is contained in:
LGUG2Z
2024-06-19 14:13:53 -07:00
parent 8bf4ab9f15
commit 83cc7bf7c0
4 changed files with 39 additions and 16 deletions

View File

@@ -98,13 +98,19 @@ impl Border {
let hwnd = WindowsApi::create_border_window(PCWSTR(name.as_ptr()), h_module)?;
hwnd_sender.send(hwnd)?;
let mut message = MSG::default();
unsafe {
while GetMessageW(&mut message, HWND(hwnd), 0, 0).into() {
TranslateMessage(&message);
DispatchMessageW(&message);
std::thread::sleep(Duration::from_millis(10));
let mut msg: MSG = MSG::default();
loop {
unsafe {
if !GetMessageW(&mut msg, HWND::default(), 0, 0).as_bool() {
tracing::debug!("border window event processing thread shutdown");
break;
};
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
std::thread::sleep(Duration::from_millis(10))
}
Ok(())

View File

@@ -1,4 +1,5 @@
use std::sync::mpsc;
use std::time::Duration;
use windows::core::PCWSTR;
use windows::Win32::Foundation::HWND;
@@ -68,13 +69,19 @@ impl Hidden {
let hwnd = WindowsApi::create_hidden_window(PCWSTR(name.as_ptr()), h_module)?;
hwnd_sender.send(hwnd)?;
let mut message = MSG::default();
let mut msg: MSG = MSG::default();
unsafe {
while GetMessageW(&mut message, HWND(hwnd), 0, 0).into() {
TranslateMessage(&message);
DispatchMessageW(&message);
loop {
unsafe {
if !GetMessageW(&mut msg, HWND::default(), 0, 0).as_bool() {
tracing::debug!("hidden window event processing thread shutdown");
break;
};
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
std::thread::sleep(Duration::from_millis(10))
}
Ok(())

View File

@@ -120,11 +120,17 @@ impl Stackbar {
SetLayeredWindowAttributes(hwnd, COLORREF(0), 0, LWA_COLORKEY)?;
hwnd_sender.send(hwnd)?;
let mut msg = MSG::default();
while GetMessageW(&mut msg, hwnd, 0, 0).into() {
let mut msg: MSG = MSG::default();
loop {
if !GetMessageW(&mut msg, HWND::default(), 0, 0).as_bool() {
tracing::debug!("stackbar window event processing thread shutdown");
break;
};
TranslateMessage(&msg);
DispatchMessageW(&msg);
std::thread::sleep(Duration::from_millis(10));
std::thread::sleep(Duration::from_millis(10))
}
}

View File

@@ -1,4 +1,5 @@
use std::sync::OnceLock;
use std::time::Duration;
use crossbeam_channel::Receiver;
use crossbeam_channel::Sender;
@@ -34,16 +35,19 @@ pub fn start() {
)
};
let mut msg: MSG = MSG::default();
loop {
let mut msg: MSG = MSG::default();
unsafe {
if !GetMessageW(&mut msg, HWND(0), 0, 0).as_bool() {
tracing::info!("windows event processing shutdown");
tracing::debug!("windows event processing thread shutdown");
break;
};
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
std::thread::sleep(Duration::from_millis(10))
}
})
});