Skip to content

Commit 6f1c26f

Browse files
authored
Fix: Fixed issue where window wasn't always brought to foreground (#14917)
1 parent e8a5c10 commit 6f1c26f

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

src/Files.App/Helpers/Interop/InteropHelpers.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ public static class InteropHelpers
2929
[DllImport("kernel32.dll")]
3030
public static extern bool SetEvent(IntPtr hEvent);
3131

32-
[DllImport("user32.dll", SetLastError = true)]
33-
public static extern void SwitchToThisWindow(IntPtr hWnd, bool altTab);
34-
3532
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
3633
public static extern int GetDpiForWindow(IntPtr hwnd);
3734

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2023 Files Community
2+
// Licensed under the MIT License. See the LICENSE.
3+
4+
using Windows.Win32;
5+
using Windows.Win32.UI.WindowsAndMessaging;
6+
7+
namespace Files.App.Helpers
8+
{
9+
public static partial class Win32Helper
10+
{
11+
/// <summary>
12+
/// Brings the app window to foreground.
13+
/// </summary>
14+
/// <remarks>
15+
/// For more information, visit
16+
/// <br/>
17+
/// - <a href="https://stackoverflow.com/questions/1544179/what-are-the-differences-between-bringwindowtotop-setforegroundwindow-setwindo" />
18+
/// <br/>
19+
/// - <a href="https://stackoverflow.com/questions/916259/win32-bring-a-window-to-top" />
20+
/// </remarks>
21+
/// <param name="hWnd">The window handle to bring.</param>
22+
public static unsafe void BringToForegroundEx(Windows.Win32.Foundation.HWND hWnd)
23+
{
24+
var hCurWnd = PInvoke.GetForegroundWindow();
25+
var dwMyID = PInvoke.GetCurrentThreadId();
26+
var dwCurID = PInvoke.GetWindowThreadProcessId(hCurWnd);
27+
28+
PInvoke.AttachThreadInput(dwCurID, dwMyID, true);
29+
30+
PInvoke.SetWindowPos(hWnd, (Windows.Win32.Foundation.HWND)(-1), 0, 0, 0, 0, SET_WINDOW_POS_FLAGS.SWP_NOSIZE | SET_WINDOW_POS_FLAGS.SWP_NOMOVE);
31+
PInvoke.SetWindowPos(hWnd, (Windows.Win32.Foundation.HWND)(-2), 0, 0, 0, 0, SET_WINDOW_POS_FLAGS.SWP_SHOWWINDOW | SET_WINDOW_POS_FLAGS.SWP_NOSIZE | SET_WINDOW_POS_FLAGS.SWP_NOMOVE);
32+
PInvoke.SetForegroundWindow(hWnd);
33+
PInvoke.SetFocus(hWnd);
34+
PInvoke.SetActiveWindow(hWnd);
35+
PInvoke.AttachThreadInput(dwCurID, dwMyID, false);
36+
}
37+
}
38+
}

src/Files.App/MainWindow.xaml.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
9393
}
9494
else if (!(string.IsNullOrEmpty(launchArgs.Arguments) && MainPageViewModel.AppInstances.Count > 0))
9595
{
96-
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
96+
// Bring to foreground (#14730)
97+
Win32Helper.BringToForegroundEx(new(WindowHandle));
98+
9799
await NavigationHelpers.AddNewTabByPathAsync(typeof(PaneHolderPage), launchArgs.Arguments, true);
98100
}
99101
else
@@ -106,6 +108,12 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
106108
if (eventArgs.Uri.AbsoluteUri == "files-uwp:")
107109
{
108110
rootFrame.Navigate(typeof(MainPage), null, new SuppressNavigationTransitionInfo());
111+
112+
if (MainPageViewModel.AppInstances.Count > 0)
113+
{
114+
// Bring to foreground (#14730)
115+
Win32Helper.BringToForegroundEx(new(WindowHandle));
116+
}
109117
}
110118
else
111119
{
@@ -171,7 +179,11 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
171179
index = 1;
172180
}
173181
else
174-
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
182+
{
183+
// Bring to foreground (#14730)
184+
Win32Helper.BringToForegroundEx(new(WindowHandle));
185+
}
186+
175187
for (; index < fileArgs.Files.Count; index++)
176188
{
177189
await NavigationHelpers.AddNewTabByPathAsync(typeof(PaneHolderPage), fileArgs.Files[index].Path, true);
@@ -245,7 +257,9 @@ async Task PerformNavigationAsync(string payload, string selectItem = null)
245257

246258
if (rootFrame.Content is MainPage && MainPageViewModel.AppInstances.Any())
247259
{
248-
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
260+
// Bring to foreground (#14730)
261+
Win32Helper.BringToForegroundEx(new(WindowHandle));
262+
249263
await NavigationHelpers.AddNewTabByParamAsync(typeof(PaneHolderPage), paneNavigationArgs);
250264
}
251265
else

src/Files.App/NativeMethods.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ LPARAM
3131
WM_LBUTTONUP
3232
WM_RBUTTONUP
3333
WM_DESTROY
34+
SetForegroundWindow
35+
GetForegroundWindow
36+
GetCurrentThreadId
37+
GetWindowThreadProcessId
38+
AttachThreadInput
39+
SetWindowPos
40+
SetFocus
41+
SetActiveWindow

0 commit comments

Comments
 (0)