From 5d094f601f25b7d3ae5c25cd02a55a0ecdee053a Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Sat, 27 Aug 2022 14:56:45 -0700 Subject: [PATCH] fix(wm): enforce valid hwnd check for border fns This commit ensures that the active window border has non-zero HWND before attempting to either hide it or set the border position. This is required as the border is only initialized when a komorebic command is received, meaning that the default value of 0 will never change if a user decides to use komorebi without the active window border. Most notably this commit fixes an issue where users who did not have the active window border enabled would not be able to move away from an empty workspace using a komorebic command. fix #217 --- komorebi/src/border.rs | 50 ++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/komorebi/src/border.rs b/komorebi/src/border.rs index b190fa39b..5940fb784 100644 --- a/komorebi/src/border.rs +++ b/komorebi/src/border.rs @@ -87,7 +87,11 @@ impl Border { } pub fn hide(self) -> Result<()> { - WindowsApi::hide_border_window(self.hwnd()) + if self.hwnd == 0 { + Ok(()) + } else { + WindowsApi::hide_border_window(self.hwnd()) + } } pub fn set_position( @@ -96,29 +100,33 @@ impl Border { invisible_borders: &Rect, activate: bool, ) -> Result<()> { - let mut should_expand_border = false; - - let mut rect = WindowsApi::window_rect(window.hwnd())?; - rect.top -= invisible_borders.bottom; - rect.bottom += invisible_borders.bottom; - - let border_overflows = BORDER_OVERFLOW_IDENTIFIERS.lock(); - if border_overflows.contains(&window.title()?) - || border_overflows.contains(&window.exe()?) - || border_overflows.contains(&window.class()?) - { - should_expand_border = true; - } + if self.hwnd == 0 { + Ok(()) + } else { + let mut should_expand_border = false; - if should_expand_border { - rect.left -= invisible_borders.left; - rect.top -= invisible_borders.top; - rect.right += invisible_borders.right; + let mut rect = WindowsApi::window_rect(window.hwnd())?; + rect.top -= invisible_borders.bottom; rect.bottom += invisible_borders.bottom; - } - *BORDER_RECT.lock() = rect; + let border_overflows = BORDER_OVERFLOW_IDENTIFIERS.lock(); + if border_overflows.contains(&window.title()?) + || border_overflows.contains(&window.exe()?) + || border_overflows.contains(&window.class()?) + { + should_expand_border = true; + } + + if should_expand_border { + rect.left -= invisible_borders.left; + rect.top -= invisible_borders.top; + rect.right += invisible_borders.right; + rect.bottom += invisible_borders.bottom; + } - WindowsApi::position_border_window(self.hwnd(), &rect, activate) + *BORDER_RECT.lock() = rect; + + WindowsApi::position_border_window(self.hwnd(), &rect, activate) + } } }