From db4b0dd63b3aa259a2a80123c722c97d471cc140 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Tue, 1 Oct 2024 08:29:01 -0700 Subject: [PATCH] fix(windows): conditional compilation for 32-bit This commit ensures that komorebi will compile when targeting 32-bit architectures, namely `stable-i686-pc-windows-msvc`. Thanks to @kennykerr for pointing out that Get/SetWindowLongPtrA/W calls don't actually exist on 32-bit builds of Windows and are aliased instead to Get/SetWindowLongA/W which take i32 args instead of isize args: https://github.com/microsoft/windows-rs/issues/3304 --- komorebi/src/window.rs | 13 +++++++++- komorebi/src/windows_api.rs | 50 ++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index d6b15dfc..97acd326 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -356,15 +356,26 @@ impl Window { WindowsApi::set_window_accent(self.hwnd, None) } - #[allow(dead_code)] + #[cfg(target_pointer_width = "64")] pub fn update_style(self, style: &WindowStyle) -> Result<()> { WindowsApi::update_style(self.hwnd, isize::try_from(style.bits())?) } + #[cfg(target_pointer_width = "32")] + pub fn update_style(self, style: &WindowStyle) -> Result<()> { + WindowsApi::update_style(self.hwnd, i32::try_from(style.bits())?) + } + + #[cfg(target_pointer_width = "64")] pub fn update_ex_style(self, style: &ExtendedWindowStyle) -> Result<()> { WindowsApi::update_ex_style(self.hwnd, isize::try_from(style.bits())?) } + #[cfg(target_pointer_width = "32")] + pub fn update_ex_style(self, style: &ExtendedWindowStyle) -> Result<()> { + WindowsApi::update_ex_style(self.hwnd, i32::try_from(style.bits())?) + } + pub fn style(self) -> Result { let bits = u32::try_from(WindowsApi::gwl_style(self.hwnd)?)?; Ok(WindowStyle::from_bits_truncate(bits)) diff --git a/komorebi/src/windows_api.rs b/komorebi/src/windows_api.rs index 5021c54b..aaacb245 100644 --- a/komorebi/src/windows_api.rs +++ b/komorebi/src/windows_api.rs @@ -703,7 +703,7 @@ impl WindowsApi { } } - #[allow(dead_code)] + #[cfg(target_pointer_width = "64")] fn set_window_long_ptr_w( hwnd: HWND, index: WINDOW_LONG_PTR_INDEX, @@ -715,14 +715,39 @@ impl WindowsApi { .map(|_| {}) } + #[cfg(target_pointer_width = "32")] + fn set_window_long_ptr_w( + hwnd: HWND, + index: WINDOW_LONG_PTR_INDEX, + new_value: i32, + ) -> Result<()> { + Result::from(WindowsResult::from(unsafe { + SetWindowLongPtrW(hwnd, index, new_value) + })) + .map(|_| {}) + } + + #[cfg(target_pointer_width = "64")] pub fn gwl_style(hwnd: isize) -> Result { Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE) } + #[cfg(target_pointer_width = "32")] + pub fn gwl_style(hwnd: isize) -> Result { + Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE) + } + + #[cfg(target_pointer_width = "64")] pub fn gwl_ex_style(hwnd: isize) -> Result { Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_EXSTYLE) } + #[cfg(target_pointer_width = "32")] + pub fn gwl_ex_style(hwnd: isize) -> Result { + Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_EXSTYLE) + } + + #[cfg(target_pointer_width = "64")] fn window_long_ptr_w(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX) -> Result { // Can return 0, which does not always mean that an error has occurred // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongptrw @@ -731,16 +756,35 @@ impl WindowsApi { })) } - #[allow(dead_code)] + #[cfg(target_pointer_width = "32")] + fn window_long_ptr_w(hwnd: HWND, index: WINDOW_LONG_PTR_INDEX) -> Result { + // Can return 0, which does not always mean that an error has occurred + // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowlongptrw + Result::from(WindowsResult::from(unsafe { + GetWindowLongPtrW(hwnd, index) + })) + } + + #[cfg(target_pointer_width = "64")] pub fn update_style(hwnd: isize, new_value: isize) -> Result<()> { Self::set_window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE, new_value) } - #[allow(dead_code)] + #[cfg(target_pointer_width = "32")] + pub fn update_style(hwnd: isize, new_value: i32) -> Result<()> { + Self::set_window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE, new_value) + } + + #[cfg(target_pointer_width = "64")] pub fn update_ex_style(hwnd: isize, new_value: isize) -> Result<()> { Self::set_window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_EXSTYLE, new_value) } + #[cfg(target_pointer_width = "32")] + pub fn update_ex_style(hwnd: isize, new_value: i32) -> Result<()> { + Self::set_window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_EXSTYLE, new_value) + } + pub fn window_text_w(hwnd: isize) -> Result { let mut text: [u16; 512] = [0; 512]; match WindowsResult::from(unsafe { GetWindowTextW(HWND(as_ptr!(hwnd)), &mut text) }) {