Skip to content

Commit 4df42a0

Browse files
committed
Add doc comments and additional error handling in keyboard layout switch logic
1 parent 48aff32 commit 4df42a0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Flow.Launcher.Infrastructure/Win32Helper.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,11 @@ private static unsafe HKL FindEnglishKeyboardLayout()
350350
var handles = new HKL[count];
351351
fixed (HKL* h = handles)
352352
{
353-
_ = PInvoke.GetKeyboardLayoutList(count, h);
353+
var result = PInvoke.GetKeyboardLayoutList(count, h);
354+
if (result != 0)
355+
{
356+
throw new Win32Exception(Marshal.GetLastWin32Error());
357+
}
354358
}
355359

356360
// Look for any English keyboard layout
@@ -369,6 +373,11 @@ private static unsafe HKL FindEnglishKeyboardLayout()
369373
return HKL.Null;
370374
}
371375

376+
/// <summary>
377+
/// Switches the keyboard layout to English if available.
378+
/// </summary>
379+
/// <param name="backupPrevious">If true, the current keyboard layout will be stored for later restoration.</param>
380+
/// <exception cref="Win32Exception">Thrown when there's an error getting the window thread process ID.</exception>
372381
public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious)
373382
{
374383
// Find an installed English layout
@@ -401,11 +410,17 @@ public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious)
401410
PInvoke.ActivateKeyboardLayout(enHKL, 0);
402411
}
403412

413+
/// <summary>
414+
/// Restores the previously backed-up keyboard layout.
415+
/// If it wasn't backed up or has already been restored, this method does nothing.
416+
/// </summary>
404417
public static void RestorePreviousKeyboardLayout()
405418
{
406419
if (_previousLayout == HKL.Null) return;
407420

408421
var hwnd = PInvoke.GetForegroundWindow();
422+
if (hwnd == HWND.Null) return;
423+
409424
PInvoke.PostMessage(
410425
hwnd,
411426
PInvoke.WM_INPUTLANGCHANGEREQUEST,

0 commit comments

Comments
 (0)