From b25cc1c8bfbef480b72e2bd1c3e126c79cac050e Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 21 Oct 2022 14:10:12 -0700 Subject: [PATCH] fix(wm): reset foreground access when calls fail --- komorebi/src/window.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index a7e3f066..aaeaac99 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -266,7 +266,8 @@ impl Window { // Raise Window to foreground let mut foregrounded = false; - let mut max_attempts = 5; + let mut tried_resetting_foreground_access = false; + let mut max_attempts = 10; while !foregrounded && max_attempts > 0 { match WindowsApi::set_foreground_window(self.hwnd()) { Ok(_) => { @@ -278,6 +279,13 @@ impl Window { "could not set as foreground window, but continuing execution of focus(): {}", error ); + + // If this still doesn't work then maybe try https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-locksetforegroundwindow + if !tried_resetting_foreground_access { + let process_id = WindowsApi::current_process_id(); + WindowsApi::allow_set_foreground_window(process_id)?; + tried_resetting_foreground_access = true; + } } }; }