Skip to content

Commit

Permalink
fix(windows): conditional compilation for 32-bit
Browse files Browse the repository at this point in the history
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:
microsoft/windows-rs#3304
  • Loading branch information
LGUG2Z committed Oct 2, 2024
1 parent 7b563aa commit db4b0dd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
13 changes: 12 additions & 1 deletion komorebi/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WindowStyle> {
let bits = u32::try_from(WindowsApi::gwl_style(self.hwnd)?)?;
Ok(WindowStyle::from_bits_truncate(bits))
Expand Down
50 changes: 47 additions & 3 deletions komorebi/src/windows_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<isize> {
Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE)
}

#[cfg(target_pointer_width = "32")]
pub fn gwl_style(hwnd: isize) -> Result<i32> {
Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_STYLE)
}

#[cfg(target_pointer_width = "64")]
pub fn gwl_ex_style(hwnd: isize) -> Result<isize> {
Self::window_long_ptr_w(HWND(as_ptr!(hwnd)), GWL_EXSTYLE)
}

#[cfg(target_pointer_width = "32")]
pub fn gwl_ex_style(hwnd: isize) -> Result<i32> {
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<isize> {
// 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
Expand All @@ -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<i32> {
// 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<String> {
let mut text: [u16; 512] = [0; 512];
match WindowsResult::from(unsafe { GetWindowTextW(HWND(as_ptr!(hwnd)), &mut text) }) {
Expand Down

0 comments on commit db4b0dd

Please sign in to comment.