Skip to content

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

fix: explorer restart fix due to COM interface becoming stale (#1273)

George Sladkovsky3eb840fcf176
diff --git a/packages/wm-common/src/parsed_config.rs b/packages/wm-common/src/parsed_config.rs
index 129785dd..0bd55df5 100644
--- a/packages/wm-common/src/parsed_config.rs
+++ b/packages/wm-common/src/parsed_config.rs
@@ -341,13 +341,13 @@ impl MatchType {
match self {
MatchType::Equals { equals } => value == equals,
MatchType::Includes { includes } => value.contains(includes),
- MatchType::Regex { regex } => regex::Regex::new(regex)
- .map(|re| re.is_match(value))
- .unwrap_or(false),
+ MatchType::Regex { regex } => {
+ regex::Regex::new(regex).is_ok_and(|re| re.is_match(value))
+ }
MatchType::NotEquals { not_equals } => value != not_equals,
- MatchType::NotRegex { not_regex } => regex::Regex::new(not_regex)
- .map(|re| !re.is_match(value))
- .unwrap_or(false),
+ MatchType::NotRegex { not_regex } => {
+ regex::Regex::new(not_regex).is_ok_and(|re| !re.is_match(value))
+ }
}
}
}
diff --git a/packages/wm-platform/src/com.rs b/packages/wm-platform/src/com.rs
index 666395dd..9fb6de34 100644
--- a/packages/wm-platform/src/com.rs
+++ b/packages/wm-platform/src/com.rs
@@ -1,3 +1,5 @@
+use std::cell::RefCell;
+
use anyhow::Context;
use windows::{
core::{ComInterface, IUnknown, IUnknown_Vtbl, GUID, HRESULT},
@@ -19,7 +21,9 @@ thread_local! {
/// Manages per-thread COM initialization. COM must be initialized on each
/// thread that uses it, so we store this in thread-local storage to handle
/// the setup and cleanup automatically.
- pub static COM_INIT: ComInit = ComInit::new();
+ ///
+ /// Wrapped in `RefCell` to allow mutation via `COM_INIT.borrow_mut()`.
+ pub static COM_INIT: RefCell<ComInit> = RefCell::new(ComInit::new());
}
pub struct ComInit {
@@ -86,6 +90,45 @@ impl ComInit {
.as_ref()
.context("Unable to create `ITaskbarList2` instance.")
}
+
+ /// Refreshes cached COM interfaces.
+ ///
+ /// Called automatically by `with_retry` when COM operations fail due to
+ /// stale interface pointers (e.g., after Explorer restarts).
+ pub fn refresh(&mut self) {
+ // Re-create the service provider.
+ self.service_provider = unsafe {
+ CoCreateInstance(&CLSID_IMMERSIVE_SHELL, None, CLSCTX_ALL)
+ }
+ .ok();
+
+ // Re-create the application view collection.
+ self.application_view_collection = self
+ .service_provider
+ .as_ref()
+ .and_then(|provider: &IServiceProvider| unsafe {
+ provider.QueryService(&IApplicationViewCollection::IID).ok()
+ });
+
+ // Re-create the taskbar list.
+ self.taskbar_list =
+ unsafe { CoCreateInstance(&TaskbarList, None, CLSCTX_SERVER) }.ok();
+ }
+
+ /// Executes a COM operation, refreshing interfaces on failure and
+ /// retrying once. Use this for operations that may fail due to stale
+ /// COM interfaces.
+ pub fn with_retry<T, F>(&mut self, op: F) -> anyhow::Result<T>
+ where
+ F: Fn(&Self) -> anyhow::Result<T>,
+ {
+ if let Ok(result) = op(self) {
+ Ok(result)
+ } else {
+ self.refresh();
+ op(self)
+ }
+ }
}
impl Default for ComInit {
diff --git a/packages/wm-platform/src/native_window.rs b/packages/wm-platform/src/native_window.rs
index 6befa0b7..20de41bd 100644
--- a/packages/wm-platform/src/native_window.rs
+++ b/packages/wm-platform/src/native_window.rs
@@ -674,22 +674,24 @@ impl NativeWindow {
}
pub fn set_cloaked(&self, cloaked: bool) -> anyhow::Result<()> {
- COM_INIT.with(|com_init| -> anyhow::Result<()> {
- let view_collection = com_init.application_view_collection()?;
+ COM_INIT.with(|com_init| {
+ com_init.borrow_mut().with_retry(|c| {
+ let view_collection = c.application_view_collection()?;
- let mut view = None;
- unsafe {
- view_collection.get_view_for_hwnd(self.handle, &raw mut view)
- }
- .ok()?;
+ let mut view = None;
+ unsafe {
+ view_collection.get_view_for_hwnd(self.handle, &raw mut view)
+ }
+ .ok()?;
- let view = view
- .context("Unable to get application view by window handle.")?;
+ let view = view
+ .context("Unable to get application view by window handle.")?;
- // Ref: https://github.com/Ciantic/AltTabAccessor/issues/1#issuecomment-1426877843
- unsafe { view.set_cloak(1, if cloaked { 2 } else { 0 }) }
- .ok()
- .context("Failed to cloak window.")
+ // Ref: https://github.com/Ciantic/AltTabAccessor/issues/1#issuecomment-1426877843
+ unsafe { view.set_cloak(1, if cloaked { 2 } else { 0 }) }
+ .ok()
+ .context("Failed to cloak window.")
+ })
})
}
@@ -702,16 +704,18 @@ impl NativeWindow {
&self,
visible: bool,
) -> anyhow::Result<()> {
- COM_INIT.with(|com_init| -> anyhow::Result<()> {
- let taskbar_list = com_init.taskbar_list()?;
+ COM_INIT.with(|com_init| {
+ com_init.borrow_mut().with_retry(|c| {
+ let taskbar_list = c.taskbar_list()?;
- if visible {
- unsafe { taskbar_list.AddTab(HWND(self.handle))? };
- } else {
- unsafe { taskbar_list.DeleteTab(HWND(self.handle))? };
- }
+ if visible {
+ unsafe { taskbar_list.AddTab(HWND(self.handle))? };
+ } else {
+ unsafe { taskbar_list.DeleteTab(HWND(self.handle))? };
+ }
- Ok(())
+ Ok(())
+ })
})
}
@@ -829,14 +833,16 @@ impl NativeWindow {
/// Causes the native Windows taskbar to be moved to the bottom of the
/// z-order when this window is active.
pub fn mark_fullscreen(&self, fullscreen: bool) -> anyhow::Result<()> {
- COM_INIT.with(|com_init| -> anyhow::Result<()> {
- let taskbar_list = com_init.taskbar_list()?;
+ COM_INIT.with(|com_init| {
+ com_init.borrow_mut().with_retry(|c| {
+ let taskbar_list = c.taskbar_list()?;
- unsafe {
- taskbar_list.MarkFullscreenWindow(HWND(self.handle), fullscreen)
- }?;
+ unsafe {
+ taskbar_list.MarkFullscreenWindow(HWND(self.handle), fullscreen)
+ }?;
- Ok(())
+ Ok(())
+ })
})
}
diff --git a/packages/wm/src/wm_state.rs b/packages/wm/src/wm_state.rs
index 89c064b8..58126b1e 100644
--- a/packages/wm/src/wm_state.rs
+++ b/packages/wm/src/wm_state.rs
@@ -562,8 +562,7 @@ impl WmState {
.filter(|descendant| {
descendant
.to_rect()
- .map(|rect| rect.contains_point(point))
- .unwrap_or(false)
+ .is_ok_and(|rect| rect.contains_point(point))
})
.collect()
}
@@ -576,8 +575,7 @@ impl WmState {
.find(|monitor| {
monitor
.to_rect()
- .map(|rect| rect.contains_point(point))
- .unwrap_or(false)
+ .is_ok_and(|rect| rect.contains_point(point))
})
.cloned()
}