Skip to content

Commit 798cfa4

Browse files
committed
Update
1 parent f86eb68 commit 798cfa4

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

src/Files.App/Helpers/Application/AppLifecycleHelper.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
using Windows.Storage;
1919
using Windows.System;
2020
using Windows.UI.Notifications;
21-
using Windows.Win32;
22-
using Windows.Win32.UI.WindowsAndMessaging;
2321
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
2422

2523
namespace Files.App.Helpers
@@ -236,33 +234,6 @@ public static void SaveSessionTabs()
236234
.ToList();
237235
}
238236

239-
/// <summary>
240-
/// Brings the app window to foreground.
241-
/// </summary>
242-
/// <remarks>
243-
/// For more information, visit
244-
/// <br/>
245-
/// - <a href="https://stackoverflow.com/questions/1544179/what-are-the-differences-between-bringwindowtotop-setforegroundwindow-setwindo" />
246-
/// <br/>
247-
/// - <a href="https://stackoverflow.com/questions/916259/win32-bring-a-window-to-top" />
248-
/// </remarks>
249-
/// <param name="hWnd">The window handle to bring.</param>
250-
public static unsafe void BringToForegroundEx(Windows.Win32.Foundation.HWND hWnd)
251-
{
252-
var hCurWnd = PInvoke.GetForegroundWindow();
253-
var dwMyID = PInvoke.GetCurrentThreadId();
254-
var dwCurID = PInvoke.GetWindowThreadProcessId(hCurWnd);
255-
256-
PInvoke.AttachThreadInput(dwCurID, dwMyID, true);
257-
258-
PInvoke.SetWindowPos(hWnd, (Windows.Win32.Foundation.HWND)(-1), 0, 0, 0, 0, SET_WINDOW_POS_FLAGS.SWP_NOSIZE | SET_WINDOW_POS_FLAGS.SWP_NOMOVE);
259-
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);
260-
PInvoke.SetForegroundWindow(hWnd);
261-
PInvoke.SetFocus(hWnd);
262-
PInvoke.SetActiveWindow(hWnd);
263-
PInvoke.AttachThreadInput(dwCurID, dwMyID, false);
264-
}
265-
266237
/// <summary>
267238
/// Shows exception on the Debug Output and sends Toast Notification to the Windows Notification Center.
268239
/// </summary>
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 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
9494
else if (!(string.IsNullOrEmpty(launchArgs.Arguments) && MainPageViewModel.AppInstances.Count > 0))
9595
{
9696
// Bring to foreground (#14730)
97-
AppLifecycleHelper.BringToForegroundEx(new(WindowHandle));
97+
Win32Helper.BringToForegroundEx(new(WindowHandle));
9898

9999
await NavigationHelpers.AddNewTabByPathAsync(typeof(PaneHolderPage), launchArgs.Arguments, true);
100100
}
@@ -112,7 +112,7 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
112112
if (MainPageViewModel.AppInstances.Count > 0)
113113
{
114114
// Bring to foreground (#14730)
115-
AppLifecycleHelper.BringToForegroundEx(new(WindowHandle));
115+
Win32Helper.BringToForegroundEx(new(WindowHandle));
116116
}
117117
}
118118
else
@@ -181,7 +181,7 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
181181
else
182182
{
183183
// Bring to foreground (#14730)
184-
AppLifecycleHelper.BringToForegroundEx(new(WindowHandle));
184+
Win32Helper.BringToForegroundEx(new(WindowHandle));
185185
}
186186

187187
for (; index < fileArgs.Files.Count; index++)
@@ -258,7 +258,7 @@ async Task PerformNavigationAsync(string payload, string selectItem = null)
258258
if (rootFrame.Content is MainPage && MainPageViewModel.AppInstances.Any())
259259
{
260260
// Bring to foreground (#14730)
261-
AppLifecycleHelper.BringToForegroundEx(new(WindowHandle));
261+
Win32Helper.BringToForegroundEx(new(WindowHandle));
262262

263263
await NavigationHelpers.AddNewTabByParamAsync(typeof(PaneHolderPage), paneNavigationArgs);
264264
}

0 commit comments

Comments
 (0)