From 226ee73aa48ffa9767e54ea84271ec1f3805d1d9 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sun, 12 May 2024 13:41:56 -0700 Subject: [PATCH] fix(borders): reap untracked hwnds in destroy_all This commit ensures that even border hwnds that may now be untracked as a result of events such as monitor changes will now be reaped in the destroy_all_borders function. --- komorebi/src/border_manager/border.rs | 19 +++++++++++++++++++ komorebi/src/border_manager/mod.rs | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/komorebi/src/border_manager/border.rs b/komorebi/src/border_manager/border.rs index 37d12b77..49448fc5 100644 --- a/komorebi/src/border_manager/border.rs +++ b/komorebi/src/border_manager/border.rs @@ -19,6 +19,7 @@ use std::sync::atomic::Ordering; use std::sync::mpsc; use std::time::Duration; use windows::core::PCWSTR; +use windows::Win32::Foundation::BOOL; use windows::Win32::Foundation::COLORREF; use windows::Win32::Foundation::HWND; use windows::Win32::Foundation::LPARAM; @@ -47,10 +48,28 @@ use windows::Win32::UI::WindowsAndMessaging::WM_DESTROY; use windows::Win32::UI::WindowsAndMessaging::WM_PAINT; use windows::Win32::UI::WindowsAndMessaging::WNDCLASSW; +pub extern "system" fn border_hwnds(hwnd: HWND, lparam: LPARAM) -> BOOL { + let hwnds = unsafe { &mut *(lparam.0 as *mut Vec) }; + + if let Ok(class) = WindowsApi::real_window_class_w(hwnd) { + if class.starts_with("komoborder") { + hwnds.push(hwnd.0); + } + } + + true.into() +} + pub struct Border { pub hwnd: isize, } +impl From for Border { + fn from(value: isize) -> Self { + Self { hwnd: value } + } +} + impl Border { pub const fn hwnd(&self) -> HWND { HWND(self.hwnd) diff --git a/komorebi/src/border_manager/mod.rs b/komorebi/src/border_manager/mod.rs index ca508adf..1cb0978b 100644 --- a/komorebi/src/border_manager/mod.rs +++ b/komorebi/src/border_manager/mod.rs @@ -22,6 +22,7 @@ use crate::Rect; use crate::Rgb; use crate::WindowManager; use crate::WindowsApi; +use border::border_hwnds; use border::Border; use komorebi_core::WindowKind; @@ -77,6 +78,17 @@ pub fn destroy_all_borders() -> color_eyre::Result<()> { BORDERS_MONITORS.lock().clear(); FOCUS_STATE.lock().clear(); + let mut remaining_hwnds = vec![]; + + WindowsApi::enum_windows( + Some(border_hwnds), + &mut remaining_hwnds as *mut Vec as isize, + )?; + + for hwnd in remaining_hwnds { + Border::from(hwnd).destroy()?; + } + Ok(()) }