From d66bdbb112217f3de88488d1ed7d469e34b23613 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 24 Mar 2025 08:51:24 +0800 Subject: [PATCH 1/3] Force FL window foreground when switching keyboard layout --- Flow.Launcher.Infrastructure/Win32Helper.cs | 29 ++++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index 7a3a0c36e26..a2d37a2f0b7 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -124,6 +124,16 @@ public static bool SetForegroundWindow(nint handle) return PInvoke.SetForegroundWindow(new(handle)); } + public static bool IsForegroundWindow(Window window) + { + return GetWindowHandle(window).Equals(PInvoke.GetForegroundWindow()); + } + + internal static bool IsForegroundWindow(HWND handle) + { + return handle.Equals(PInvoke.GetForegroundWindow()); + } + #endregion #region Task Switching @@ -354,10 +364,17 @@ public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious) // No installed English layout found if (enHKL == HKL.Null) return; - // Get the current foreground window - var hwnd = PInvoke.GetForegroundWindow(); + // Get the FL main window + var hwnd = GetWindowHandle(Application.Current.MainWindow, true); if (hwnd == HWND.Null) return; + // Check if the FL main window is the current foreground window + if (!IsForegroundWindow(hwnd)) + { + var result = PInvoke.SetForegroundWindow(hwnd); + if (!result) throw new Win32Exception(Marshal.GetLastWin32Error()); + } + // Get the current foreground window thread ID var threadId = PInvoke.GetWindowThreadProcessId(hwnd); if (threadId == 0) throw new Win32Exception(Marshal.GetLastWin32Error()); @@ -367,12 +384,10 @@ public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious) // the IME mode instead of switching to another layout. var currentLayout = PInvoke.GetKeyboardLayout(threadId); var currentLangId = (uint)currentLayout.Value & KeyboardLayoutLoWord; - foreach (var langTag in ImeLanguageTags) + foreach (var imeLangTag in ImeLanguageTags) { - if (GetLanguageTag(currentLangId).StartsWith(langTag, StringComparison.OrdinalIgnoreCase)) - { - return; - } + var langTag = GetLanguageTag(currentLangId); + if (langTag.StartsWith(imeLangTag, StringComparison.OrdinalIgnoreCase)) return; } // Backup current keyboard layout From db46a4adfd354c70b0b8fede7f9fe5887f97f7d0 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 30 Mar 2025 09:12:14 +0800 Subject: [PATCH 2/3] Code quality --- Flow.Launcher.Infrastructure/Win32Helper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index a2d37a2f0b7..b5b89e2d195 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -126,7 +126,7 @@ public static bool SetForegroundWindow(nint handle) public static bool IsForegroundWindow(Window window) { - return GetWindowHandle(window).Equals(PInvoke.GetForegroundWindow()); + return IsForegroundWindow(GetWindowHandle(window)); } internal static bool IsForegroundWindow(HWND handle) From bc823b3ca44ff4fb730060fe2949e3c27f4de98d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 30 Mar 2025 15:04:07 +0800 Subject: [PATCH 3/3] Fix Application.Current null exception --- Flow.Launcher.Infrastructure/Win32Helper.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index b5b89e2d195..f260b17d113 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -364,6 +364,9 @@ public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious) // No installed English layout found if (enHKL == HKL.Null) return; + // When application is exiting, the Application.Current will be null + if (Application.Current == null) return; + // Get the FL main window var hwnd = GetWindowHandle(Application.Current.MainWindow, true); if (hwnd == HWND.Null) return;