GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: add caching for frame position, border position, and maximized/minimized status
Lars Berger406215483646
diff --git a/packages/wm/src/common/events/handle_window_location_changed.rs b/packages/wm/src/common/events/handle_window_location_changed.rsindex e237f053..b5290dc9 100644--- a/packages/wm/src/common/events/handle_window_location_changed.rs+++ b/packages/wm/src/common/events/handle_window_location_changed.rs@@ -25,18 +25,24 @@ pub fn handle_window_location_changed(// Update the window's state to be fullscreen or toggled from fullscreen.if let Some(window) = found_window {+ window.native().refresh_border_position()?;+ window.native().refresh_frame_position()?;+ window.native().refresh_is_maximized()?;+let nearest_monitor = state.nearest_monitor(&window.native()).context("Failed to get workspace of nearest monitor.")?;match window.state() {WindowState::Fullscreen(_) => {+ window.native().refresh_is_minimized()?;+// A fullscreen window that gets minimized can hit this arm, so// ignore such events and let it be handled by the handler for// `PlatformEvent::WindowMinimized` instead.if !window.native().is_fullscreen(&nearest_monitor.to_rect()?)?- && !window.native().is_maximized()- && !window.native().is_minimized()+ && !window.native().is_maximized()?+ && !window.native().is_minimized()?{info!("Window restored");toggle_window_state(@@ -48,7 +54,7 @@ pub fn handle_window_location_changed(}}_ => {- if window.native().is_maximized()+ if window.native().is_maximized()?|| window.native().is_fullscreen(&nearest_monitor.to_rect()?)?{info!("Window fullscreened");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.rsindex 135724a7..66b4ebbc 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,10 @@ pub fn handle_window_moved_or_resized(return Ok(());}+ window.native().refresh_border_position()?;+ window.native().refresh_frame_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/memo.rs b/packages/wm/src/common/memo.rsindex baf9af67..f85b4833 100644--- a/packages/wm/src/common/memo.rs+++ b/packages/wm/src/common/memo.rs@@ -2,6 +2,7 @@ use std::sync::{Arc, Mutex};/// A thread-safe type that provides lazy initialization and caching of a/// value.+#[derive(Debug)]pub struct Memo<T>whereT: Clone,diff --git a/packages/wm/src/common/platform/event_listener.rs b/packages/wm/src/common/platform/event_listener.rsindex 4515a8d9..02888392 100644--- a/packages/wm/src/common/platform/event_listener.rs+++ b/packages/wm/src/common/platform/event_listener.rs@@ -7,6 +7,7 @@ use crate::{use super::{EventWindow, NativeWindow};+#[derive(Debug)]pub enum PlatformEvent {DisplaySettingsChanged,KeybindingTriggered(KeybindingConfig),diff --git a/packages/wm/src/common/platform/native_window.rs b/packages/wm/src/common/platform/native_window.rsindex 156f611e..556bd701 100644--- a/packages/wm/src/common/platform/native_window.rs+++ b/packages/wm/src/common/platform/native_window.rs@@ -37,11 +37,16 @@ use crate::{windows::WindowState,};+#[derive(Debug)]pub struct NativeWindow {pub handle: isize,title: Memo<String>,process_name: Memo<String>,class_name: Memo<String>,+ frame_position: Memo<Rect>,+ border_position: Memo<Rect>,+ is_minimized: Memo<bool>,+ is_maximized: Memo<bool>,}impl NativeWindow {@@ -52,6 +57,10 @@ impl NativeWindow {title: Memo::new(),process_name: Memo::new(),class_name: Memo::new(),+ frame_position: Memo::new(),+ border_position: Memo::new(),+ is_minimized: Memo::new(),+ is_maximized: Memo::new(),}}@@ -73,7 +82,7 @@ impl NativeWindow {fn updated_title(&self) -> anyhow::Result<String> {let mut text: [u16; 512] = [0; 512];let length = unsafe { GetWindowTextW(HWND(self.handle), &mut text) };- Ok(String::from_utf16_lossy(&text[..length as usize]).into())+ Ok(String::from_utf16_lossy(&text[..length as usize]))}/// Gets the process name associated with the window.@@ -109,8 +118,7 @@ impl NativeWindow {CloseHandle(process_handle)?;};- let process_name = String::from_utf16(&buffer[..length as usize])?;- Ok(process_name)+ Ok(String::from_utf16_lossy(&buffer[..length as usize]))}/// Gets the class name of the window.@@ -187,14 +195,45 @@ impl NativeWindow {!is_menu_window}- pub fn is_minimized(&self) -> bool {- unsafe { IsIconic(HWND(self.handle)) }.as_bool()+ /// Whether the window is minimized.+ ///+ /// This value is lazily retrieved and cached after first retrieval.+ pub fn is_minimized(&self) -> anyhow::Result<bool> {+ self+ .is_minimized+ .get_or_init(Self::updated_is_minimized, self)+ }++ /// Updates the cached minimized status.+ pub fn refresh_is_minimized(&self) -> anyhow::Result<bool> {+ self.is_minimized.update(Self::updated_is_minimized, self)}- pub fn is_maximized(&self) -> bool {- unsafe { IsZoomed(HWND(self.handle)) }.as_bool()+ /// Whether the window is minimized.+ fn updated_is_minimized(&self) -> anyhow::Result<bool> {+ Ok(unsafe { IsIconic(HWND(self.handle)) }.as_bool())}+ /// Whether the window is maximized.+ ///+ /// This value is lazily retrieved and cached after first retrieval.+ pub fn is_maximized(&self) -> anyhow::Result<bool> {+ self+ .is_maximized+ .get_or_init(Self::updated_is_maximized, self)+ }++ /// Updates the cached maximized status.+ pub fn refresh_is_maximized(&self) -> anyhow::Result<bool> {+ self.is_maximized.update(Self::updated_is_maximized, self)+ }++ /// Whether the window is maximized.+ fn updated_is_maximized(&self) -> anyhow::Result<bool> {+ Ok(unsafe { IsZoomed(HWND(self.handle)) }.as_bool())+ }++ /// Whether the window has resize handles.pub fn is_resizable(&self) -> bool {self.has_window_style(WS_THICKFRAME)}@@ -262,7 +301,24 @@ impl NativeWindow {/// Gets the window's position, including the window's frame. Excludes/// the window's shadow borders.+ ///+ /// This value is lazily retrieved and cached after first retrieval.pub fn frame_position(&self) -> anyhow::Result<Rect> {+ self+ .frame_position+ .get_or_init(Self::updated_frame_position, self)+ }++ /// Updates the cached frame position.+ pub fn refresh_frame_position(&self) -> anyhow::Result<Rect> {+ self+ .frame_position+ .update(Self::updated_frame_position, self)+ }++ /// Gets the window's position, including the window's frame. Excludes+ /// the window's shadow borders.+ fn updated_frame_position(&self) -> anyhow::Result<Rect> {let mut rect = RECT::default();let dwm_res = unsafe {@@ -290,7 +346,24 @@ impl NativeWindow {/// Gets the window's position, including the window's frame and/// shadow borders.- fn border_position(&self) -> anyhow::Result<Rect> {+ ///+ /// This value is lazily retrieved and cached after first retrieval.+ pub fn border_position(&self) -> anyhow::Result<Rect> {+ self+ .border_position+ .get_or_init(Self::updated_border_position, self)+ }++ /// Updates the cached border position.+ pub fn refresh_border_position(&self) -> anyhow::Result<Rect> {+ self+ .border_position+ .update(Self::updated_border_position, self)+ }++ /// Gets the window's position, including the window's frame and+ /// shadow borders.+ fn updated_border_position(&self) -> anyhow::Result<Rect> {let mut rect = RECT::default();unsafe { GetWindowRect(HWND(self.handle), &mut rect as *mut _ as _) }?;@@ -367,7 +440,7 @@ impl NativeWindow {// Need to restore window if transitioning from maximized fullscreen// to non-maximized fullscreen.WindowState::Fullscreen(config) => {- if !config.maximized && self.is_maximized() {+ if !config.maximized && self.is_maximized()? {self.restore()?;}}@@ -375,7 +448,7 @@ impl NativeWindow {// from maximized to minimized works without having to restore.WindowState::Minimized => {}_ => {- if self.is_minimized() || self.is_maximized() {+ if self.is_minimized()? || self.is_maximized()? {self.restore()?;}}diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex 5460326a..fb4667d8 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -5,10 +5,10 @@#![feature(iterator_try_collect)]#![feature(once_cell_try)]-use std::{env, io, path::PathBuf, process::Child};+use std::{env, path::PathBuf};use anyhow::{Context, Error, Result};-use tokio::{process, process::Command, signal};+use tokio::{process::Command, signal};use tracing::{debug, error, info};use crate::{@@ -98,7 +98,7 @@ async fn start_wm(loop {tokio::select! {Some(event) = event_listener.event_rx.recv() => {- // debug!("Received platform event: {:?}", event);+ debug!("Received platform event: {:?}", event);if let Err(err) = wm.process_event(event, &mut config) {error!("Failed to process event: {:?}", err);