GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat: skip watcher cleanup if wm exitted successfully
Lars Bergerc448b0fde209
diff --git a/packages/watcher/src/main.rs b/packages/watcher/src/main.rsindex af72f193..83f15b9f 100644--- a/packages/watcher/src/main.rs+++ b/packages/watcher/src/main.rs@@ -1,10 +1,10 @@-use anyhow::Context;+use anyhow::{bail, Context};use tracing::info;use wm::{cleanup::run_cleanup, common::platform::NativeWindow,containers::ContainerDto, ipc_client::IpcClient,- ipc_server::ClientResponseData, wm_event::WmEvent,+ ipc_server::ClientResponseData, windows::WindowDto, wm_event::WmEvent,};#[tokio::main]@@ -13,13 +13,75 @@ async fn main() -> anyhow::Result<()> {let mut client = IpcClient::connect().await?;+ // Get handles to windows that are already open on watcher launch.+ let mut managed_handles = query_initial_windows(&mut client)+ .await?+ .into_iter()+ .map(|window| window.handle)+ .collect::<Vec<_>>();++ // Update window handles on window manage/unmanage events.+ let subscribe_res =+ watch_managed_handles(&mut client, &mut managed_handles).await;++ match subscribe_res {+ Ok(_) => info!("WM exited successfully. Skipping watcher cleanup."),+ Err(_) => {+ info!("WM exitted unexpectedly. Running watcher cleanup.");++ let managed_windows = managed_handles+ .into_iter()+ .map(|handle| NativeWindow::new(handle))+ .collect::<Vec<_>>();++ run_cleanup(managed_windows);+ }+ }++ Ok(())+}++async fn query_initial_windows(+ client: &mut IpcClient,+) -> anyhow::Result<Vec<WindowDto>> {+ let query_message = "query windows";++ client+ .send(query_message)+ .await+ .context("Failed to send window query command.")?;++ client+ .client_response(&query_message)+ .await+ .and_then(|response| match response.data {+ Some(ClientResponseData::Windows(data)) => Some(data),+ _ => None,+ })+ .map(|data| {+ data+ .windows+ .into_iter()+ .filter_map(|container| match container {+ ContainerDto::Window(window) => Some(window),+ _ => None,+ })+ .collect::<Vec<_>>()+ })+ .context("Invalid data in windows query response.")+}++async fn watch_managed_handles(+ client: &mut IpcClient,+ handles: &mut Vec<isize>,+) -> anyhow::Result<()> {let subscription_message =- "subscribe -e window_managed window_unmanaged";+ "subscribe -e window_managed window_unmanaged application_exiting";client.send(&subscription_message).await- .context("Failed to send command to IPC server.")?;+ .context("Failed to send subscribe command to IPC server.")?;let subscription_id = client.client_response(&subscription_message)@@ -32,8 +94,6 @@ async fn main() -> anyhow::Result<()> {}).context("No subscription ID in watcher event subscription.")?;- let mut managed_windows = Vec::new();-loop {let event_data = client.event_subscription(&subscription_id)@@ -44,24 +104,22 @@ async fn main() -> anyhow::Result<()> {Some(WmEvent::WindowManaged { managed_window }) => {if let ContainerDto::Window(window) = managed_window {info!("Watcher added handle: {}.", window.handle);- managed_windows.push(NativeWindow::new(window.handle));+ handles.push(window.handle);}}Some(WmEvent::WindowUnmanaged {unmanaged_handle, ..}) => {info!("Watcher removed handle: {}.", unmanaged_handle);- managed_windows.retain(|window| window.handle != unmanaged_handle);+ handles.retain(|&handle| handle != unmanaged_handle);+ }+ Some(WmEvent::ApplicationExiting) => {+ return Ok(());}Some(_) => unreachable!(),None => {- info!("Watcher event subscription ended. Running cleanup.");- break;+ bail!("IPC connection closed unexpectedly.")}}}-- run_cleanup(managed_windows);-- Ok(())}diff --git a/packages/wm/src/main.rs b/packages/wm/src/main.rsindex 0c82fe35..bd3548cb 100644--- a/packages/wm/src/main.rs+++ b/packages/wm/src/main.rs@@ -84,7 +84,7 @@ async fn start_wm(.with(// std output layer for devfmt::Layer::new()- .with_writer(std::io::stdout.with_max_level(verbosity.level()))+ .with_writer(std::io::stdout.with_max_level(verbosity.level())),).with(// file output layer for errors@@ -113,10 +113,10 @@ async fn start_wm(// Add application icon to system tray.let mut tray = SystemTray::new(&config.path)?;- let mut ipc_server = IpcServer::start().await?;-let mut wm = WindowManager::new(&mut config)?;+ let mut ipc_server = IpcServer::start().await?;+// Start listening for platform events after populating initial state.let mut event_listener = Platform::start_event_listener(&config)?;diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rsindex e07b7cad..2c9edf28 100644--- a/packages/wm/src/wm_state.rs+++ b/packages/wm/src/wm_state.rs@@ -6,7 +6,7 @@ use tracing::warn;use uuid::Uuid;use crate::{- cleanup,+ cleanup::run_cleanup,common::{commands::platform_sync,platform::{NativeMonitor, NativeWindow, Platform},@@ -473,6 +473,6 @@ impl Drop for WmState {.map(|window| window.native().clone()).collect::<Vec<_>>();- cleanup::run_cleanup(managed_windows);+ run_cleanup(managed_windows);}}