Skip to content

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

feat: get size of border delta via `GetWindowRect`

Lars Berger4a79fa2b9057
diff --git a/packages/wm/src/common/events/handle_window_location_changed.rs b/packages/wm/src/common/events/handle_window_location_changed.rs
index d4a0ab51..f27bd448 100644
--- a/packages/wm/src/common/events/handle_window_location_changed.rs
+++ b/packages/wm/src/common/events/handle_window_location_changed.rs
@@ -56,7 +56,7 @@ pub fn handle_window_location_changed(
)?;
} else if matches!(window.state(), WindowState::Floating(_)) {
// Update state with the new location of the floating window.
- let new_position = window.native().outer_position()?;
+ let new_position = window.native().frame_position()?;
window.set_floating_placement(new_position);
let workspace = window.workspace().context("No workspace.")?;
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 241b9569..a35cabda 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
@@ -34,7 +34,7 @@ pub fn handle_window_moved_or_resized(
return Ok(());
}
- let new_position = window.native().outer_position()?;
+ let new_position = window.native().frame_position()?;
let delta_width = new_position.width() - window.width()?;
let delta_height = new_position.height() - window.height()?;
diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rs
index 094298e7..e51f9a26 100644
--- a/packages/wm/src/common/platform/native_window.rs
+++ b/packages/wm/src/common/platform/native_window.rs
@@ -38,7 +38,10 @@ use windows::{
},
};
-use crate::{common::Rect, windows::WindowState};
+use crate::{
+ common::{LengthValue, Rect, RectDelta},
+ windows::WindowState,
+};
pub type WindowHandle = HWND;
@@ -247,8 +250,9 @@ impl NativeWindow {
Ok(())
}
- /// Gets the window's position, including the window's frame.
- pub fn outer_position(&self) -> anyhow::Result<Rect> {
+ /// Gets the window's position, including the window's frame. Excludes
+ /// the window's shadow borders.
+ pub fn frame_position(&self) -> anyhow::Result<Rect> {
let mut rect = RECT::default();
let dwm_res = unsafe {
@@ -260,10 +264,26 @@ impl NativeWindow {
)
};
- 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 _) }?;
+ match dwm_res {
+ Ok(_) => Ok(Rect::from_ltrb(
+ rect.left,
+ rect.top,
+ rect.right,
+ rect.bottom,
+ )),
+ _ => {
+ warn!("Failed to get window's frame position. Falling back to border position.");
+ self.border_position()
+ }
}
+ }
+
+ /// Gets the window's position, including the window's frame and
+ /// shadow borders.
+ fn border_position(&self) -> anyhow::Result<Rect> {
+ let mut rect = RECT::default();
+
+ unsafe { GetWindowRect(self.handle, &mut rect as *mut _ as _) }?;
Ok(Rect::from_ltrb(
rect.left,
@@ -273,6 +293,20 @@ impl NativeWindow {
))
}
+ /// Gets the delta between the window's frame and the window's border.
+ /// This represents the size of a window's shadow borders.
+ pub fn border_delta(&self) -> anyhow::Result<RectDelta> {
+ let border_pos = self.border_position()?;
+ let frame_pos = self.frame_position()?;
+
+ Ok(RectDelta::new(
+ LengthValue::new_px((frame_pos.left - border_pos.left) as f32),
+ LengthValue::new_px((frame_pos.top - border_pos.top) as f32),
+ LengthValue::new_px((border_pos.right - frame_pos.right) as f32),
+ LengthValue::new_px((border_pos.bottom - frame_pos.bottom) as f32),
+ ))
+ }
+
fn has_window_style(&self, style: WINDOW_STYLE) -> bool {
unsafe {
let current_style = GetWindowLongPtrW(self.handle, GWL_STYLE);
diff --git a/packages/wm/src/windows/commands/manage_window.rs b/packages/wm/src/windows/commands/manage_window.rs
index a3466845..76d2aea0 100644
--- a/packages/wm/src/windows/commands/manage_window.rs
+++ b/packages/wm/src/windows/commands/manage_window.rs
@@ -2,7 +2,7 @@ use anyhow::Context;
use tracing::info;
use crate::{
- common::platform::NativeWindow,
+ common::{platform::NativeWindow, LengthValue, RectDelta},
containers::{
commands::{attach_container, set_focused_descendant},
traits::{CommonGetters, PositionGetters},
@@ -91,26 +91,28 @@ fn create_window(
// Calculate where window should be placed when floating is enabled. Use
// the original width/height of the window and optionally position it in
// the center of the workspace.
- let floating_placement = if nearest_workspace.id()
- == target_workspace.id()
- && !config.value.window_state_defaults.floating.centered
- {
- native_window.outer_position()?
- } else {
- native_window
- .outer_position()?
- .translate_to_center(&target_workspace.to_rect()?)
- };
+ let prefer_center = config.value.window_state_defaults.floating.centered;
+ let floating_placement =
+ if nearest_workspace.id() != target_workspace.id() || prefer_center {
+ native_window
+ .frame_position()?
+ .translate_to_center(&target_workspace.to_rect()?)
+ } else {
+ native_window.frame_position()?
+ };
let window_state = window_state_to_create(&native_window, config);
+ let border_delta = native_window.border_delta()?;
+ let inner_gap = config.value.gaps.inner_gap.clone();
let window_container: WindowContainer = match window_state {
WindowState::Tiling => TilingWindow::new(
None,
native_window,
None,
+ border_delta,
floating_placement,
- config.value.gaps.inner_gap.clone(),
+ inner_gap,
)
.into(),
_ => NonTilingWindow::new(
@@ -118,6 +120,7 @@ fn create_window(
native_window,
window_state,
None,
+ border_delta,
None,
floating_placement,
)
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 d86b57a1..6aa3b737 100644
--- a/packages/wm/src/windows/commands/move_window_in_direction.rs
+++ b/packages/wm/src/windows/commands/move_window_in_direction.rs
@@ -420,7 +420,7 @@ fn move_floating_window(
// monitor in the given direction.
let monitor = window_to_move.monitor().context("No monitor.")?;
let monitor_rect = monitor.native().working_rect()?.clone();
- let window_pos = window_to_move.native().outer_position()?;
+ let window_pos = window_to_move.native().frame_position()?;
let (monitor_length, window_length) = match direction {
Direction::Up | Direction::Down => {
diff --git a/packages/wm/src/windows/non_tiling_window.rs b/packages/wm/src/windows/non_tiling_window.rs
index 203c20d3..7092514f 100644
--- a/packages/wm/src/windows/non_tiling_window.rs
+++ b/packages/wm/src/windows/non_tiling_window.rs
@@ -45,6 +45,7 @@ impl NonTilingWindow {
native: NativeWindow,
state: WindowState,
prev_state: Option<WindowState>,
+ border_delta: RectDelta,
insertion_target: Option<(Container, usize)>,
floating_placement: Rect,
) -> Self {
@@ -58,13 +59,7 @@ impl NonTilingWindow {
prev_state,
insertion_target,
display_state: DisplayState::Shown,
- // TODO: Pass border delta via the constructor.
- border_delta: RectDelta::new(
- LengthValue::new_px(7.),
- LengthValue::new_px(0.),
- LengthValue::new_px(7.),
- LengthValue::new_px(7.),
- ),
+ border_delta,
has_pending_dpi_adjustment: false,
floating_placement,
};
@@ -85,6 +80,7 @@ impl NonTilingWindow {
Some(self.id()),
self.native(),
Some(self.state()),
+ self.border_delta(),
self.floating_placement(),
inner_gap,
)
diff --git a/packages/wm/src/windows/tiling_window.rs b/packages/wm/src/windows/tiling_window.rs
index a31ed0a4..ae24594b 100644
--- a/packages/wm/src/windows/tiling_window.rs
+++ b/packages/wm/src/windows/tiling_window.rs
@@ -51,6 +51,7 @@ impl TilingWindow {
id: Option<Uuid>,
native: NativeWindow,
prev_state: Option<WindowState>,
+ border_delta: RectDelta,
floating_placement: Rect,
inner_gap: LengthValue,
) -> Self {
@@ -64,13 +65,7 @@ impl TilingWindow {
state: WindowState::Tiling,
prev_state,
display_state: DisplayState::Shown,
- // TODO: Pass border delta via the constructor.
- border_delta: RectDelta::new(
- LengthValue::new_px(7.),
- LengthValue::new_px(0.),
- LengthValue::new_px(7.),
- LengthValue::new_px(7.),
- ),
+ border_delta,
has_pending_dpi_adjustment: false,
floating_placement,
inner_gap,
@@ -89,6 +84,7 @@ impl TilingWindow {
self.native(),
state,
Some(WindowState::Tiling),
+ self.border_delta(),
None,
self.floating_placement(),
)