GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: "ghost" window cleanup (#1220)
Gilbert Sanchezf3322721c271
diff --git a/packages/wm-platform/src/native_window.rs b/packages/wm-platform/src/native_window.rsindex 20de41bd..464eb8ed 100644--- a/packages/wm-platform/src/native_window.rs+++ b/packages/wm-platform/src/native_window.rs@@ -24,7 +24,7 @@ use windows::{WindowsAndMessaging::{EnumWindows, GetClassNameW, GetLayeredWindowAttributes, GetWindow,GetWindowLongPtrW, GetWindowRect, GetWindowTextW,- GetWindowThreadProcessId, IsIconic, IsWindowVisible, IsZoomed,+ GetWindowThreadProcessId, IsIconic, IsWindow, IsWindowVisible, IsZoomed,SendNotifyMessageW, SetForegroundWindow,SetLayeredWindowAttributes, SetWindowLongPtrW, SetWindowPlacement,SetWindowPos, ShowWindowAsync, GWL_EXSTYLE, GWL_STYLE, GW_OWNER,@@ -180,6 +180,13 @@ impl NativeWindow {Ok(class_name)}+ /// Whether the window is still valid.+ ///+ /// Returns `true` if the underlying window handle refers to an existing window.+ pub fn is_valid(&self) -> bool {+ unsafe { IsWindow(HWND(self.handle)) }.as_bool()+ }+/// Whether the window is actually visible.pub fn is_visible(&self) -> anyhow::Result<bool> {let is_visible =diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex b5c81768..36216ff3 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -9,10 +9,10 @@#![warn(clippy::all, clippy::pedantic)]#![feature(iterator_try_collect)]-use std::{env, path::PathBuf};+use std::{env, path::PathBuf, time::Duration};use anyhow::{Context, Error};-use tokio::{process::Command, signal};+use tokio::{process::Command, signal, time};use tracing::{debug, error, info, warn, Level};use tracing_subscriber::{fmt::{self, writer::MakeWriterExt},@@ -23,7 +23,7 @@ use wm_platform::Platform;use crate::{ipc_server::IpcServer, sys_tray::SystemTray, user_config::UserConfig,- wm::WindowManager,+ wm::WindowManager, wm_state::WINDOW_CLEANUP_INTERVAL_SECS,};mod commands;@@ -95,6 +95,9 @@ async fn start_wm(let startup_commands = config.value.general.startup_commands.clone();wm.process_commands(&startup_commands, None, &mut config)?;+ // Create an interval for cleaning up invalid/ghost windows.+ let mut cleanup_interval = time::interval(Duration::from_secs(WINDOW_CLEANUP_INTERVAL_SECS));+loop {let res = tokio::select! {Some(()) = tray.exit_rx.recv() => {@@ -109,6 +112,15 @@ async fn start_wm(info!("Received SIGINT signal.");break;},+ _ = cleanup_interval.tick() => {+ // Clean up invalid/ghost windows periodically.+ if !wm.state.is_paused {+ if let Err(err) = wm.state.cleanup_invalid_windows() {+ warn!("Failed to clean up invalid windows: {}", err);+ }+ }+ Ok(())+ },Some(event) = event_listener.event_rx.recv() => {debug!("Received platform event: {:?}", event);wm.process_event(event, &mut config)diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex 58126b1e..3d46d48a 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -9,6 +9,7 @@ use wm_common::{};use wm_platform::{NativeMonitor, NativeWindow, Platform};+use crate::commands::window::unmanage_window;use crate::{commands::{container::set_focused_descendant,@@ -25,6 +26,10 @@ use crate::{user_config::UserConfig,};+/// Interval in seconds for cleaning up invalid/ghost windows.+/// Set to 5 seconds to balance between responsiveness and performance.+pub const WINDOW_CLEANUP_INTERVAL_SECS: u64 = 5;+pub struct WmState {/// Root node of the container tree. Monitors are the children of the/// root node, followed by workspaces, then split containers/windows.@@ -579,6 +584,26 @@ impl WmState {}).cloned()}++ /// Cleans up invalid window handles that may have become stale.+ ///+ /// This addresses the "ghost window" issue where applications terminate+ /// without properly sending window destroy events, leaving invalid+ /// window references in GlazeWM's state.+ pub fn cleanup_invalid_windows(&mut self) -> anyhow::Result<()> {+ let invalid_windows = self+ .windows()+ .into_iter()+ .filter(|window| !window.native().is_valid());++ // Remove each invalid window from the window tree+ for window in invalid_windows {+ tracing::debug!("Removing invalid window: {}", window);+ unmanage_window(window, self)?;+ }++ Ok(())+ }}impl Drop for WmState {