From 73568922d5878d000ab6df3e9f413e77c768bec0 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 30 Jul 2021 21:05:00 -0700 Subject: [PATCH] fix(workspaces): let set_foreground_window fail When hiding/restoring windows as part of workspace switching, calls to SetForegroundWindow fail, and if they fail, other hidden windows get lost forever in hidden mode when the error is returned up the call chain using the ? operator. This commit separates out the focus() calls from the loops restoring windows when switching workspaces, and also ensures that calls to SetForegroundWindow will log an error, but ultimately continue to the end of the focus() function call. --- komorebi/src/window.rs | 10 +++++++++- komorebi/src/workspace.rs | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index abb61689..f29c7a9b 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -124,7 +124,15 @@ impl Window { WindowsApi::attach_thread_input(current_thread_id, window_thread_id, true)?; // Raise Window to foreground - WindowsApi::set_foreground_window(self.hwnd())?; + match WindowsApi::set_foreground_window(self.hwnd()) { + Ok(_) => {} + Err(error) => { + tracing::error!( + "could not set as foreground window, but continuing execution of focus(): {}", + error + ); + } + }; // Center cursor in Window WindowsApi::center_cursor_in_rect(&WindowsApi::window_rect(self.hwnd())?)?; diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 062f4c78..49172a98 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -58,16 +58,22 @@ impl Workspace { pub fn restore(&mut self) -> Result<()> { let idx = self.focused_container_idx(); + let mut to_focus = None; for (i, container) in self.containers_mut().iter_mut().enumerate() { if let Some(window) = container.visible_window_mut() { window.restore(); if idx == i { - window.focus()?; + to_focus = Option::from(window); } } } + // Do this here to make sure that an error doesn't stop the restoration of other windows + if let Some(window) = to_focus { + window.focus()?; + } + Ok(()) }