Skip to content

Implement auto-switching to English when the option is enabled #3366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
81a4632
Implement auto-switching to English when the option is enabled
Yusyuriv Mar 21, 2025
aa3ad10
When looking for English keyboard layout, use pre-defined IDs instead…
Yusyuriv Mar 22, 2025
023ab45
Use PInvoke instead of DllImport & Several adjustments
Jack251970 Mar 22, 2025
465108a
Fix keyboard layout fetch issue
Jack251970 Mar 22, 2025
67be335
Rename methods to make their purpose more obvious; slight code style …
Yusyuriv Mar 22, 2025
c39079b
Revert accidental change
Yusyuriv Mar 22, 2025
4146f4d
Use focus events to trigger
Jack251970 Mar 22, 2025
f83e8ed
Revert "Use focus events to trigger"
Yusyuriv Mar 22, 2025
6ad4b23
Don't switch to English when IME can be disabled instead
Yusyuriv Mar 22, 2025
ca04823
Remove generic language code
Yusyuriv Mar 22, 2025
747f958
Fix keyboard restore issue when window is deactivated
Jack251970 Mar 23, 2025
cd28c09
Fix the issue with not being able to switch back to the original keyb…
Yusyuriv Mar 23, 2025
bf011f1
Revert "Fix keyboard restore issue when window is deactivated"
Yusyuriv Mar 23, 2025
382d0c2
Don't broadcast language change
Yusyuriv Mar 23, 2025
48aff32
Clarify why not switch keyboard layout for languages that have IME mode
Yusyuriv Mar 23, 2025
4df42a0
Add doc comments and additional error handling in keyboard layout swi…
Yusyuriv Mar 23, 2025
1bf5733
Fix incorrect error handling logic in keyboard layout change
Yusyuriv Mar 23, 2025
5be88dd
Remove blank line
Jack251970 Mar 23, 2025
c63debe
Add foreground window check
Jack251970 Mar 23, 2025
4fc7f70
Adjust formats
Jack251970 Mar 23, 2025
d827d0a
Use language tag instead of language id
Jack251970 Mar 23, 2025
4f2a951
Small code style changes in keyboard change logic
Yusyuriv Mar 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Flow.Launcher.Infrastructure/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ GetMonitorInfo
MONITORINFOEXW

WM_ENTERSIZEMOVE
WM_EXITSIZEMOVE
WM_EXITSIZEMOVE

GetKeyboardLayout
GetWindowThreadProcessId
ActivateKeyboardLayout
GetKeyboardLayoutList

PostMessage
WM_INPUTLANGCHANGEREQUEST
INPUTLANGCHANGE_FORWARD
116 changes: 115 additions & 1 deletion Flow.Launcher.Infrastructure/Win32Helper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Dwm;
using Windows.Win32.UI.Input.KeyboardAndMouse;
using Windows.Win32.UI.WindowsAndMessaging;
using Flow.Launcher.Infrastructure.UserSettings;
using Point = System.Windows.Point;

namespace Flow.Launcher.Infrastructure
{
Expand Down Expand Up @@ -63,7 +66,7 @@ public static unsafe bool DWMSetDarkModeForWindow(Window window, bool useDarkMod
}

/// <summary>
///
///
/// </summary>
/// <param name="window"></param>
/// <param name="cornerType">DoNotRound, Round, RoundSmall, Default</param>
Expand Down Expand Up @@ -317,5 +320,116 @@ internal static HWND GetWindowHandle(Window window, bool ensure = false)
}

#endregion

#region Keyboard Layout

// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-56532684c37f
private static readonly uint[] EnglishLanguageIds =
{
0x0009, 0x0409, 0x0809, 0x0C09, 0x1009, 0x1409, 0x1809, 0x1C09, 0x2009, 0x2409, 0x2809, 0x2C09, 0x3009,
0x3409, 0x3C09, 0x4009, 0x4409, 0x4809, 0x4C09,
};

private static readonly uint[] ImeLanguageIds =
{
0x0004, 0x7804, 0x0804, 0x1004, 0x7C04, 0x0C04, 0x1404, 0x0404, 0x0011, 0x0411, 0x0012, 0x0412,
};

private const uint KeyboardLayoutLoWord = 0xFFFF;

// Store the previous keyboard layout
private static HKL _previousLayout;

private static unsafe HKL FindEnglishKeyboardLayout()
{
// Get the number of keyboard layouts
int count = PInvoke.GetKeyboardLayoutList(0, null);
if (count <= 0) return HKL.Null;

// Get all keyboard layouts
var handles = new HKL[count];
fixed (HKL* h = handles)
{
var result = PInvoke.GetKeyboardLayoutList(count, h);
if (result == 0)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}

// Look for any English keyboard layout
foreach (var hkl in handles)
{
// The lower word contains the language identifier
var langId = (uint)hkl.Value & KeyboardLayoutLoWord;

// Check if it's an English layout
if (EnglishLanguageIds.Contains(langId))
{
return hkl;
}
}

return HKL.Null;
}

/// <summary>
/// Switches the keyboard layout to English if available.
/// </summary>
/// <param name="backupPrevious">If true, the current keyboard layout will be stored for later restoration.</param>
/// <exception cref="Win32Exception">Thrown when there's an error getting the window thread process ID.</exception>
public static unsafe void SwitchToEnglishKeyboardLayout(bool backupPrevious)
{
// Find an installed English layout
var enHKL = FindEnglishKeyboardLayout();

// No installed English layout found
if (enHKL == HKL.Null) return;

// Get the current window thread ID
var threadId = PInvoke.GetWindowThreadProcessId(PInvoke.GetForegroundWindow());
if (threadId == 0) throw new Win32Exception(Marshal.GetLastWin32Error());

// If the current layout has an IME mode, disable it without switching to another layout.
// This is needed because for languages with IME mode, Flow Launcher just temporarily disables
// the IME mode instead of switching to another layout.
var currentLayout = PInvoke.GetKeyboardLayout(threadId);
var currentLayoutCode = (uint)currentLayout.Value & KeyboardLayoutLoWord;
if (ImeLanguageIds.Contains(currentLayoutCode))
{
return;
}

// Backup current keyboard layout
if (backupPrevious)
{
_previousLayout = currentLayout;
}

// Switch to English layout
PInvoke.ActivateKeyboardLayout(enHKL, 0);
}

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

var hwnd = PInvoke.GetForegroundWindow();
if (hwnd == HWND.Null) return;

PInvoke.PostMessage(
hwnd,
PInvoke.WM_INPUTLANGCHANGEREQUEST,
PInvoke.INPUTLANGCHANGE_FORWARD,
_previousLayout.Value
);
_previousLayout = HKL.Null;
}

#endregion
}
}
12 changes: 11 additions & 1 deletion Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,11 @@ public void Show()
MainWindowOpacity = 1;
MainWindowVisibilityStatus = true;
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true });

if (StartWithEnglishMode)
{
Win32Helper.SwitchToEnglishKeyboardLayout(true);
}
});
}

Expand Down Expand Up @@ -1441,7 +1446,12 @@ public async void Hide()
// 📌 Apply DWM Cloak (Completely hide the window)
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
}


if (StartWithEnglishMode)
{
Win32Helper.RestorePreviousKeyboardLayout();
}

await Task.Delay(50);

// Update WPF properties
Expand Down
Loading