Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

feat: replace use of `GetWindowPlacement` api with `DwmGetWindowAttribute` to get frame bounds

Lars Berger17fe065c0023
diff --git a/packages/wm/src/common/events/handle_window_moved_or_resized.rs b/packages/wm/src/common/events/handle_window_moved_or_resized.rs
index 71306285..5927270d 100644
--- a/packages/wm/src/common/events/handle_window_moved_or_resized.rs
+++ b/packages/wm/src/common/events/handle_window_moved_or_resized.rs
@@ -44,24 +44,18 @@ fn update_tiling_window(
window: TilingWindow,
state: &mut WmState,
) -> anyhow::Result<()> {
+ let parent = window.parent().context("No parent.")?;
+
// Snap window to its original position if it's the only window in the
// workspace.
- if window.parent().context("No parent.")?.is_workspace()
- && window.tiling_siblings().count() == 0
- {
+ if parent.is_workspace() && window.tiling_siblings().count() == 0 {
state.containers_to_redraw.push(window.into());
return Ok(());
}
- // Remove invisible borders from current placement to be able to compare
- // window width/height.
- let new_placement = window
- .native()
- .placement()?
- .apply_delta(&window.border_delta());
-
- let delta_width = new_placement.width() - window.width()?;
- let delta_height = new_placement.height() - window.height()?;
+ let new_position = window.native().outer_position()?;
+ let delta_width = new_position.width() - window.width()?;
+ let delta_height = new_position.height() - window.height()?;
resize_window(
window.clone().into(),
@@ -83,8 +77,8 @@ fn update_floating_window(
state: &mut WmState,
) -> anyhow::Result<()> {
// Update state with new location of the floating window.
- let new_placement = window.native().placement()?;
- window.set_floating_placement(new_placement);
+ let new_position = window.native().outer_position()?;
+ window.set_floating_placement(new_position);
// Change floating window's parent workspace if moved out of its bounds.
update_parent_workspace(window.clone().into(), state)
diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rs
index f2e1b838..094298e7 100644
--- a/packages/wm/src/common/platform/native_window.rs
+++ b/packages/wm/src/common/platform/native_window.rs
@@ -1,15 +1,16 @@
-use std::os::raw::c_void;
-use std::sync::{Arc, RwLock};
+use std::{
+ ffi::c_void,
+ sync::{Arc, RwLock},
+};
-use crate::common::Rect;
-use crate::windows::WindowState;
-use windows::Win32::Graphics::Dwm::DWMWA_BORDER_COLOR;
+use tracing::warn;
use windows::{
core::PWSTR,
Win32::{
- Foundation::{CloseHandle, BOOL, HWND, LPARAM},
+ Foundation::{CloseHandle, BOOL, HWND, LPARAM, RECT},
Graphics::Dwm::{
- DwmGetWindowAttribute, DwmSetWindowAttribute, DWMWA_CLOAKED,
+ DwmGetWindowAttribute, DwmSetWindowAttribute, DWMWA_BORDER_COLOR,
+ DWMWA_CLOAKED, DWMWA_EXTENDED_FRAME_BOUNDS,
},
System::Threading::{
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32,
@@ -22,21 +23,23 @@ use windows::{
},
WindowsAndMessaging::{
EnumWindows, GetClassNameW, GetWindow, GetWindowLongPtrW,
- GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId,
- IsIconic, IsWindowVisible, SendNotifyMessageW,
- SetForegroundWindow, SetWindowPos, ShowWindowAsync, GWL_EXSTYLE,
- GWL_STYLE, GW_OWNER, HWND_NOTOPMOST, HWND_TOPMOST,
- SWP_ASYNCWINDOWPOS, SWP_FRAMECHANGED, SWP_HIDEWINDOW,
- SWP_NOACTIVATE, SWP_NOCOPYBITS, SWP_NOSENDCHANGING,
- SWP_SHOWWINDOW, SW_MAXIMIZE, SW_MINIMIZE, SW_RESTORE,
- WINDOWPLACEMENT, WINDOW_EX_STYLE, WINDOW_STYLE, WM_CLOSE,
- WS_CAPTION, WS_CHILD, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
- WS_THICKFRAME,
+ GetWindowPlacement, GetWindowRect, GetWindowTextW,
+ GetWindowThreadProcessId, IsIconic, IsWindowVisible,
+ SendNotifyMessageW, SetForegroundWindow, SetWindowPos,
+ ShowWindowAsync, GWL_EXSTYLE, GWL_STYLE, GW_OWNER, HWND_NOTOPMOST,
+ HWND_TOPMOST, SWP_ASYNCWINDOWPOS, SWP_FRAMECHANGED,
+ SWP_HIDEWINDOW, SWP_NOACTIVATE, SWP_NOCOPYBITS,
+ SWP_NOSENDCHANGING, SWP_SHOWWINDOW, SW_MAXIMIZE, SW_MINIMIZE,
+ SW_RESTORE, WINDOWPLACEMENT, WINDOW_EX_STYLE, WINDOW_STYLE,
+ WM_CLOSE, WS_CAPTION, WS_CHILD, WS_EX_NOACTIVATE,
+ WS_EX_TOOLWINDOW, WS_THICKFRAME,
},
},
},
};
+use crate::{common::Rect, windows::WindowState};
+
pub type WindowHandle = HWND;
#[derive(Clone, Debug)]
@@ -244,16 +247,29 @@ impl NativeWindow {
Ok(())
}
- pub fn placement(&self) -> anyhow::Result<Rect> {
- let mut placement = WINDOWPLACEMENT::default();
- unsafe { GetWindowPlacement(self.handle, &mut placement) }?;
+ /// Gets the window's position, including the window's frame.
+ pub fn outer_position(&self) -> anyhow::Result<Rect> {
+ let mut rect = RECT::default();
+
+ let dwm_res = unsafe {
+ DwmGetWindowAttribute(
+ self.handle,
+ DWMWA_EXTENDED_FRAME_BOUNDS,
+ &mut rect as *mut _ as _,
+ std::mem::size_of::<RECT>() as u32,
+ )
+ };
+
+ if dwm_res.is_err() {
+ warn!("Failed to get window's frame bounds. Falling back to `GetWindowRect`.");
+ unsafe { GetWindowRect(self.handle, &mut rect as *mut _ as _) }?;
+ }
- let normal_pos = placement.rcNormalPosition;
Ok(Rect::from_ltrb(
- normal_pos.left,
- normal_pos.top,
- normal_pos.right,
- normal_pos.bottom,
+ rect.left,
+ rect.top,
+ rect.right,
+ rect.bottom,
))
}
diff --git a/packages/wm/src/containers/commands/redraw.rs b/packages/wm/src/containers/commands/redraw.rs
index 87da161a..965d6ed8 100644
--- a/packages/wm/src/containers/commands/redraw.rs
+++ b/packages/wm/src/containers/commands/redraw.rs
@@ -47,14 +47,7 @@ pub fn redraw(state: &mut WmState) -> anyhow::Result<()> {
}
}
- // Avoid adjusting the borders of non-tiling windows. Otherwise the
- // window will increase in size from its original placement.
- let rect = match window.state() {
- WindowState::Tiling => {
- window.to_rect()?.apply_delta(&window.border_delta())
- }
- _ => window.to_rect()?,
- };
+ let rect = window.to_rect()?.apply_delta(&window.border_delta());
let is_visible = match window.display_state() {
DisplayState::Showing | DisplayState::Shown => true,
diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rs
index 1458f6e5..1c856d9c 100644
--- a/packages/wm/src/monitors/monitor.rs
+++ b/packages/wm/src/monitors/monitor.rs
@@ -68,9 +68,13 @@ impl Monitor {
&self,
other: &Container,
) -> anyhow::Result<bool> {
- let other_monitor = other.monitor().context("No parent monitor.")?;
+ let dpi = self.native().dpi()?;
+ let other_dpi = other
+ .monitor()
+ .and_then(|monitor| monitor.native().dpi().ok())
+ .context("Failed to get DPI of other monitor.")?;
- Ok(self.native().dpi()? != other_monitor.native().dpi()?)
+ Ok((dpi - other_dpi).abs() < f32::EPSILON)
}
pub fn to_dto(&self) -> anyhow::Result<MonitorDto> {
diff --git a/packages/wm/src/windows/commands/manage_window.rs b/packages/wm/src/windows/commands/manage_window.rs
index 6de4fc43..a3466845 100644
--- a/packages/wm/src/windows/commands/manage_window.rs
+++ b/packages/wm/src/windows/commands/manage_window.rs
@@ -95,10 +95,10 @@ fn create_window(
== target_workspace.id()
&& !config.value.window_state_defaults.floating.centered
{
- native_window.placement()?
+ native_window.outer_position()?
} else {
native_window
- .placement()?
+ .outer_position()?
.translate_to_center(&target_workspace.to_rect()?)
};
diff --git a/packages/wm/src/windows/commands/move_window_in_direction.rs b/packages/wm/src/windows/commands/move_window_in_direction.rs
index 13556a16..f981c261 100644
--- a/packages/wm/src/windows/commands/move_window_in_direction.rs
+++ b/packages/wm/src/windows/commands/move_window_in_direction.rs
@@ -421,16 +421,14 @@ fn move_floating_window(
let monitor = window_to_move.monitor().context("No monitor.")?;
let monitor_length = match direction {
Direction::Up | Direction::Down => monitor.height()?,
- Direction::Left | Direction::Right => monitor.width()?,
+ _ => monitor.width()?,
};
- let placement = window_to_move
- .floating_placement()
- .apply_delta(&window_to_move.border_delta());
+ let position = window_to_move.native().outer_position()?;
let window_length = match direction {
- Direction::Up | Direction::Down => placement.height(),
- Direction::Left | Direction::Right => placement.width(),
+ Direction::Up | Direction::Down => position.height(),
+ _ => position.width(),
};
let length_delta = monitor_length - window_length;