From 91c532d9b10720412439a6c5107b376873cbb181 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 21 Oct 2022 11:54:28 -0700 Subject: [PATCH] fix(wm): listen to settingchange + devicechange This commit adds listeners on two more events, WM_SETTINGCHANGE and WM_DEVICECHANGE, in the hope of more reliably catching monitor attachments and detachments based on info in an SO answer. fix #267 --- komorebi/Cargo.toml | 3 ++- komorebi/src/windows_callbacks.rs | 33 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index 5e49a070..3578ca52 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -56,7 +56,8 @@ features = [ "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_Shell", "Win32_UI_Shell_Common", - "Win32_UI_WindowsAndMessaging" + "Win32_UI_WindowsAndMessaging", + "Win32_System_SystemServices" ] [features] diff --git a/komorebi/src/windows_callbacks.rs b/komorebi/src/windows_callbacks.rs index 9803077a..38e88421 100644 --- a/komorebi/src/windows_callbacks.rs +++ b/komorebi/src/windows_callbacks.rs @@ -18,12 +18,17 @@ use windows::Win32::Graphics::Gdi::HDC; use windows::Win32::Graphics::Gdi::HMONITOR; use windows::Win32::Graphics::Gdi::PAINTSTRUCT; use windows::Win32::Graphics::Gdi::PS_SOLID; +use windows::Win32::System::SystemServices::DBT_DEVNODES_CHANGED; use windows::Win32::UI::Accessibility::HWINEVENTHOOK; use windows::Win32::UI::WindowsAndMessaging::DefWindowProcW; use windows::Win32::UI::WindowsAndMessaging::PostQuitMessage; +use windows::Win32::UI::WindowsAndMessaging::SPI_ICONVERTICALSPACING; +use windows::Win32::UI::WindowsAndMessaging::SPI_SETWORKAREA; use windows::Win32::UI::WindowsAndMessaging::WM_DESTROY; +use windows::Win32::UI::WindowsAndMessaging::WM_DEVICECHANGE; use windows::Win32::UI::WindowsAndMessaging::WM_DISPLAYCHANGE; use windows::Win32::UI::WindowsAndMessaging::WM_PAINT; +use windows::Win32::UI::WindowsAndMessaging::WM_SETTINGCHANGE; use crate::container::Container; use crate::monitor::Monitor; @@ -178,6 +183,34 @@ pub extern "system" fn hidden_window( LRESULT(0) } + // Added based on this https://stackoverflow.com/a/33762334 + WM_SETTINGCHANGE => { + #[allow(clippy::cast_possible_truncation)] + if wparam.0 as u32 == SPI_SETWORKAREA.0 + || wparam.0 as u32 == SPI_ICONVERTICALSPACING.0 + { + let event_type = WindowManagerEvent::DisplayChange(Window { hwnd: window.0 }); + WINEVENT_CALLBACK_CHANNEL + .lock() + .0 + .send(event_type) + .expect("could not send message on WINEVENT_CALLBACK_CHANNEL"); + } + LRESULT(0) + } + // Added based on this https://stackoverflow.com/a/33762334 + WM_DEVICECHANGE => { + #[allow(clippy::cast_possible_truncation)] + if wparam.0 as u32 == DBT_DEVNODES_CHANGED { + let event_type = WindowManagerEvent::DisplayChange(Window { hwnd: window.0 }); + WINEVENT_CALLBACK_CHANNEL + .lock() + .0 + .send(event_type) + .expect("could not send message on WINEVENT_CALLBACK_CHANNEL"); + } + LRESULT(0) + } _ => DefWindowProcW(window, message, wparam, lparam), } }