From 62db431338636dd0adafca15d636c08f68941984 Mon Sep 17 00:00:00 2001 From: Fabian-Lars Date: Tue, 4 Apr 2023 01:30:52 +0200 Subject: [PATCH] fix(windows): Use SetForegroundWindow before focus hack (#719) --- .changes/fix-windows-focus.md | 5 +++++ src/platform_impl/windows/window.rs | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 .changes/fix-windows-focus.md diff --git a/.changes/fix-windows-focus.md b/.changes/fix-windows-focus.md new file mode 100644 index 000000000..4ed71b3ff --- /dev/null +++ b/.changes/fix-windows-focus.md @@ -0,0 +1,5 @@ +--- +'tao': 'patch' +--- + +Fix set_focus not working on Windows in some situations like interactive notifications. diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index f18ce43b8..7cc6b3c52 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -1266,10 +1266,13 @@ unsafe fn taskbar_mark_fullscreen(handle: HWND, fullscreen: bool) { } unsafe fn force_window_active(handle: HWND) { - // In some situation, calling SetForegroundWindow could not bring up the window, - // This is a little hack which can "steal" the foreground window permission + // Try to focus the window without the hack first. + SetForegroundWindow(handle); + + // In some situations, calling SetForegroundWindow could not bring up the window, + // This is a little hack which can "steal" the foreground window permission. // We only call this function in the window creation, so it should be fine. - // See : https://stackoverflow.com/questions/10740346/setforegroundwindow-only-working-while-visual-studio-is-open + // See: https://stackoverflow.com/questions/10740346/setforegroundwindow-only-working-while-visual-studio-is-open let alt_sc = MapVirtualKeyW(u32::from(VK_MENU.0), MAPVK_VK_TO_VSC); let mut inputs: [INPUT; 2] = mem::zeroed();