Skip to content
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

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

Merged
merged 22 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion Flow.Launcher.Infrastructure/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ GetMonitorInfo
MONITORINFOEXW

WM_ENTERSIZEMOVE
WM_EXITSIZEMOVE
WM_EXITSIZEMOVE

GetKeyboardLayout
GetWindowThreadProcessId
ActivateKeyboardLayout
GetKeyboardLayoutList
PostMessage
WM_INPUTLANGCHANGEREQUEST
INPUTLANGCHANGE_FORWARD
LOCALE_TRANSIENT_KEYBOARD1
LOCALE_TRANSIENT_KEYBOARD2
LOCALE_TRANSIENT_KEYBOARD3
LOCALE_TRANSIENT_KEYBOARD4
175 changes: 173 additions & 2 deletions Flow.Launcher.Infrastructure/Win32Helper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Flow.Launcher.Infrastructure.UserSettings;
using Microsoft.Win32;
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 +67,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 +321,172 @@ internal static HWND GetWindowHandle(Window window, bool ensure = false)
}

#endregion

#region Keyboard Layout

private const string UserProfileRegistryPath = @"Control Panel\International\User Profile";

// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-56532684c37f
private const string EnglishLanguageTag = "en";

private static readonly string[] ImeLanguageTags =
{
"zh", // Chinese
"ja", // Japanese
"ko", // Korean
};

private const uint KeyboardLayoutLoWord = 0xFFFF;

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

/// <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 foreground window
var hwnd = PInvoke.GetForegroundWindow();
if (hwnd == HWND.Null) return;

// Get the current foreground window thread ID
var threadId = PInvoke.GetWindowThreadProcessId(hwnd);
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 currentLangId = (uint)currentLayout.Value & KeyboardLayoutLoWord;
foreach (var langTag in ImeLanguageTags)
{
if (GetLanguageTag(currentLangId).StartsWith(langTag, StringComparison.OrdinalIgnoreCase))
{
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;
}

/// <summary>
/// Finds an installed English keyboard layout.
/// </summary>
/// <returns></returns>
/// <exception cref="Win32Exception"></exception>
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;
var langTag = GetLanguageTag(langId);

// Check if it's an English layout
if (langTag.StartsWith(EnglishLanguageTag, StringComparison.OrdinalIgnoreCase))
{
return hkl;
}
}

return HKL.Null;
}

/// <summary>
/// Returns the
/// <see href="https://learn.microsoft.com/globalization/locale/standard-locale-names">
/// BCP 47 language tag
/// </see>
/// of the current input language.
/// </summary>
/// <remarks>
/// Edited from: https://github.com/dotnet/winforms
/// </remarks>
private static string GetLanguageTag(uint langId)
{
// We need to convert the language identifier to a language tag, because they are deprecated and may have a
// transient value.
// https://learn.microsoft.com/globalization/locale/other-locale-names#lcid
// https://learn.microsoft.com/windows/win32/winmsg/wm-inputlangchange#remarks
//
// It turns out that the LCIDToLocaleName API, which is used inside CultureInfo, may return incorrect
// language tags for transient language identifiers. For example, it returns "nqo-GN" and "jv-Java-ID"
// instead of the "nqo" and "jv-Java" (as seen in the Get-WinUserLanguageList PowerShell cmdlet).
//
// Try to extract proper language tag from registry as a workaround approved by a Windows team.
// https://github.com/dotnet/winforms/pull/8573#issuecomment-1542600949
//
// NOTE: this logic may break in future versions of Windows since it is not documented.
if (langId is PInvoke.LOCALE_TRANSIENT_KEYBOARD1
or PInvoke.LOCALE_TRANSIENT_KEYBOARD2
or PInvoke.LOCALE_TRANSIENT_KEYBOARD3
or PInvoke.LOCALE_TRANSIENT_KEYBOARD4)
{
using var key = Registry.CurrentUser.OpenSubKey(UserProfileRegistryPath);
if (key?.GetValue("Languages") is string[] languages)
{
foreach (string language in languages)
{
using var subKey = key.OpenSubKey(language);
if (subKey?.GetValue("TransientLangId") is int transientLangId
&& transientLangId == langId)
{
return language;
}
}
}
}

return CultureInfo.GetCultureInfo((int)langId).Name;
}

#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