Skip to content

Commit 9372972

Browse files
authored
Fix: Switch focus to Files when activating window (#13588)
1 parent afd77df commit 9372972

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

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

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

31+
[DllImport("user32.dll", SetLastError = true)]
32+
public static extern void SwitchToThisWindow(IntPtr hWnd, bool altTab);
33+
3134
[DllImport("ole32.dll")]
3235
public static extern uint CoWaitForMultipleObjects(uint dwFlags, uint dwMilliseconds, ulong nHandles, IntPtr[] pHandles, out uint dwIndex);
3336

src/Files.App/MainWindow.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
9797
}
9898
else if (!(string.IsNullOrEmpty(launchArgs.Arguments) && MainPageViewModel.AppInstances.Count > 0))
9999
{
100+
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
100101
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), launchArgs.Arguments);
101102
}
102103
else
@@ -173,6 +174,8 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
173174
rootFrame.Navigate(typeof(MainPage), fileArgs.Files.First().Path, new SuppressNavigationTransitionInfo());
174175
index = 1;
175176
}
177+
else
178+
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
176179
for (; index < fileArgs.Files.Count; index++)
177180
{
178181
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), fileArgs.Files[index].Path);
@@ -235,7 +238,10 @@ async Task PerformNavigationAsync(string payload, string selectItem = null)
235238
};
236239

237240
if (rootFrame.Content is MainPage && MainPageViewModel.AppInstances.Any())
241+
{
242+
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
238243
await mainPageViewModel.AddNewTabByParamAsync(typeof(PaneHolderPage), paneNavigationArgs);
244+
}
239245
else
240246
rootFrame.Navigate(typeof(MainPage), paneNavigationArgs, new SuppressNavigationTransitionInfo());
241247
}

0 commit comments

Comments
 (0)