Skip to content
Merged
Changes from 1 commit
Commits
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
29 changes: 22 additions & 7 deletions Flow.Launcher.Infrastructure/Win32Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let call IsForegroundWindow with the handle from GetWindowHandle

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot get your idea. I already have a internal version which uses the handle from GetWindowHandle.

internal static bool IsForegroundWindow(HWND handle)
{
    return handle.Equals(PInvoke.GetForegroundWindow());
}

Copy link
Member Author

@Jack251970 Jack251970 Mar 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since HWND is an internal class, we cannot use that internal class in public function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh? You can. Just do IsForegroundWindow(GetWindowHandle(window))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you misunderstand how the modifier works. It won't restrict which method it can call inside the body, but only whether other can call it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it gives me error here:
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for my misunderstanding, now I use GetWindowHandle inside its function

}

internal static bool IsForegroundWindow(HWND handle)
{
return handle.Equals(PInvoke.GetForegroundWindow());
}

#endregion

#region Task Switching
Expand Down Expand Up @@ -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());
Expand All @@ -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
Expand Down
Loading