From 577fa0a97f657c28446e90bc1edad3834e80072e Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 7 Oct 2022 06:57:35 -0700 Subject: [PATCH] fix(wm): retry foreground calls on focus change For the past few weeks since upgrading the windows-rs crate I've seen sporadic failures when calling SetForegroundWindow which require a full restart to get the wm working as expected again. Adding in a retry loop here seems to help when the issue comes up for me on Windows 11. --- komorebi/src/window.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index 5ca7310d..d45db1f0 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -257,15 +257,20 @@ impl Window { }; // Raise Window to foreground - match WindowsApi::set_foreground_window(self.hwnd()) { - Ok(_) => {} - Err(error) => { - tracing::error!( - "could not set as foreground window, but continuing execution of focus(): {}", - error - ); - } - }; + let mut foregrounded = false; + while !foregrounded { + match WindowsApi::set_foreground_window(self.hwnd()) { + Ok(_) => { + foregrounded = true; + } + Err(error) => { + tracing::error!( + "could not set as foreground window, but continuing execution of focus(): {}", + error + ); + } + }; + } // Center cursor in Window if mouse_follows_focus {