Skip to content

Commit

Permalink
Fix: Switch focus to Files when activating window (#13588)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 authored Oct 29, 2023
1 parent afd77df commit 9372972
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Files.App/Helpers/Interop/InteropHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public static class InteropHelpers
[DllImport("kernel32.dll")]
public static extern bool SetEvent(IntPtr hEvent);

[DllImport("user32.dll", SetLastError = true)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool altTab);

[DllImport("ole32.dll")]
public static extern uint CoWaitForMultipleObjects(uint dwFlags, uint dwMilliseconds, ulong nHandles, IntPtr[] pHandles, out uint dwIndex);

Expand Down
6 changes: 6 additions & 0 deletions src/Files.App/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
}
else if (!(string.IsNullOrEmpty(launchArgs.Arguments) && MainPageViewModel.AppInstances.Count > 0))
{
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), launchArgs.Arguments);
}
else
Expand Down Expand Up @@ -173,6 +174,8 @@ public async Task InitializeApplicationAsync(object activatedEventArgs)
rootFrame.Navigate(typeof(MainPage), fileArgs.Files.First().Path, new SuppressNavigationTransitionInfo());
index = 1;
}
else
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
for (; index < fileArgs.Files.Count; index++)
{
await mainPageViewModel.AddNewTabByPathAsync(typeof(PaneHolderPage), fileArgs.Files[index].Path);
Expand Down Expand Up @@ -235,7 +238,10 @@ async Task PerformNavigationAsync(string payload, string selectItem = null)
};

if (rootFrame.Content is MainPage && MainPageViewModel.AppInstances.Any())
{
InteropHelpers.SwitchToThisWindow(WindowHandle, true);
await mainPageViewModel.AddNewTabByParamAsync(typeof(PaneHolderPage), paneNavigationArgs);
}
else
rootFrame.Navigate(typeof(MainPage), paneNavigationArgs, new SuppressNavigationTransitionInfo());
}
Expand Down

0 comments on commit 9372972

Please sign in to comment.