From 4df42a0f638ea0dc2a4be6999606cc1ea1141e63 Mon Sep 17 00:00:00 2001 From: Yusyuriv Date: Sun, 23 Mar 2025 18:40:41 +0600 Subject: [PATCH] Add doc comments and additional error handling in keyboard layout switch logic --- Flow.Launcher.Infrastructure/Win32Helper.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Win32Helper.cs b/Flow.Launcher.Infrastructure/Win32Helper.cs index f874c7006..7cebedfdc 100644 --- a/Flow.Launcher.Infrastructure/Win32Helper.cs +++ b/Flow.Launcher.Infrastructure/Win32Helper.cs @@ -350,7 +350,11 @@ namespace Flow.Launcher.Infrastructure var handles = new HKL[count]; fixed (HKL* h = handles) { - _ = PInvoke.GetKeyboardLayoutList(count, h); + var result = PInvoke.GetKeyboardLayoutList(count, h); + if (result != 0) + { + throw new Win32Exception(Marshal.GetLastWin32Error()); + } } // Look for any English keyboard layout @@ -369,6 +373,11 @@ namespace Flow.Launcher.Infrastructure return HKL.Null; } + /// + /// Switches the keyboard layout to English if available. + /// + /// If true, the current keyboard layout will be stored for later restoration. + /// Thrown when there's an error getting the window thread process ID. public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious) { // Find an installed English layout @@ -401,11 +410,17 @@ namespace Flow.Launcher.Infrastructure PInvoke.ActivateKeyboardLayout(enHKL, 0); } + /// + /// Restores the previously backed-up keyboard layout. + /// If it wasn't backed up or has already been restored, this method does nothing. + /// public static void RestorePreviousKeyboardLayout() { if (_previousLayout == HKL.Null) return; var hwnd = PInvoke.GetForegroundWindow(); + if (hwnd == HWND.Null) return; + PInvoke.PostMessage( hwnd, PInvoke.WM_INPUTLANGCHANGEREQUEST,