Skip to content

Commit d12b6ba

Browse files
authored
Feature: Added option to always switch to newly created tab (#16613)
1 parent 5a4c7c0 commit d12b6ba

19 files changed

+83
-26
lines changed

src/Files.App/Actions/Content/Tags/OpenAllTaggedActions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task ExecuteAsync(object? parameter = null)
4747
await Task.WhenAll(filePaths.Select(path => NavigationHelpers.OpenPath(path, _pageContext.ShellPage!)));
4848

4949
foreach (var path in folderPaths)
50-
await NavigationHelpers.OpenPathInNewTab(path, false);
50+
await NavigationHelpers.OpenPathInNewTab(path);
5151
}
5252

5353
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Actions/Navigation/OpenInNewTab/OpenInNewTabFromHomeAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public override async Task ExecuteAsync(object? parameter = null)
2323
if (await DriveHelpers.CheckEmptyDrive(HomePageContext.RightClickedItem!.Path))
2424
return;
2525

26-
await NavigationHelpers.OpenPathInNewTab(HomePageContext.RightClickedItem!.Path ?? string.Empty, false);
26+
await NavigationHelpers.OpenPathInNewTab(HomePageContext.RightClickedItem!.Path ?? string.Empty);
2727
}
2828

2929
protected override void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Actions/Navigation/OpenInNewTab/OpenInNewTabFromSidebarAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public override async Task ExecuteAsync(object? parameter = null)
2121
if (await DriveHelpers.CheckEmptyDrive(SidebarContext.RightClickedItem!.Path))
2222
return;
2323

24-
await NavigationHelpers.OpenPathInNewTab(SidebarContext.RightClickedItem!.Path ?? string.Empty, false);
24+
await NavigationHelpers.OpenPathInNewTab(SidebarContext.RightClickedItem!.Path ?? string.Empty);
2525
}
2626

2727
protected override void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)

src/Files.App/Data/Contracts/IGeneralSettingsService.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty
6060
/// </summary>
6161
bool AlwaysOpenDualPaneInNewTab { get; set; }
6262

63+
/// <summary>
64+
/// Gets or sets a value indicating whether or not to always switch to newly opened tab.
65+
/// </summary>
66+
bool AlwaysSwitchToNewlyOpenedTab { get; set; }
67+
6368
/// <summary>
6469
/// Gets or sets a value indicating whether or not to display the quick access widget.
6570
/// </summary>
@@ -114,7 +119,7 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty
114119
/// Gets or sets a value indicating if the favorites section should be visible.
115120
/// </summary>
116121
bool ShowPinnedSection { get; set; }
117-
122+
118123
/// <summary>
119124
/// Gets or sets a value indicating if the favorites section should be expanded.
120125
/// </summary>
@@ -244,7 +249,7 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty
244249
/// Gets or sets a value indicating whether or not to leave app running in the background.
245250
/// </summary>
246251
bool LeaveAppRunning { get; set; }
247-
252+
248253
/// <summary>
249254
/// Gets or sets a value indicating whether or not to show Files in the system tray.
250255
/// </summary>

src/Files.App/Helpers/Navigation/NavigationHelpers.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,50 @@
55
using Microsoft.UI.Xaml.Controls;
66
using Microsoft.UI.Xaml.Media;
77
using Microsoft.UI.Xaml.Media.Imaging;
8-
using Windows.Storage.Search;
9-
using Microsoft.UI.Xaml.Media;
8+
using System.IO;
109
using Windows.Storage;
10+
using Windows.Storage.Search;
1111
using Windows.System;
12-
using System.IO;
1312

1413
namespace Files.App.Helpers
1514
{
1615
public static class NavigationHelpers
1716
{
17+
private static readonly IGeneralSettingsService GeneralSettingsService = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
18+
1819
private static readonly IWindowsRecentItemsService WindowsRecentItemsService = Ioc.Default.GetRequiredService<IWindowsRecentItemsService>();
1920
private static MainPageViewModel MainPageViewModel { get; } = Ioc.Default.GetRequiredService<MainPageViewModel>();
2021
private static DrivesViewModel DrivesViewModel { get; } = Ioc.Default.GetRequiredService<DrivesViewModel>();
2122
private static INetworkService NetworkService { get; } = Ioc.Default.GetRequiredService<INetworkService>();
2223

23-
public static Task OpenPathInNewTab(string? path, bool focusNewTab)
24+
/// <summary>
25+
/// Opens the path in a new tab.
26+
/// </summary>
27+
/// <param name="path">The path to open in a new tab.</param>
28+
/// <param name="switchToNewTab">Indicates whether to switch to the new tab.</param>
29+
/// <returns></returns>
30+
public static Task OpenPathInNewTab(string? path, bool switchToNewTab)
31+
{
32+
return AddNewTabByPathAsync(typeof(ShellPanesPage), path, switchToNewTab);
33+
}
34+
35+
/// <summary>
36+
/// Opens the path in a new tab and automatically switches to the newly
37+
/// created tab if configured in settings.
38+
/// </summary>
39+
/// <param name="path">The path to open in a new tab.</param>
40+
/// <returns></returns>
41+
public static Task OpenPathInNewTab(string? path)
2442
{
25-
return AddNewTabByPathAsync(typeof(ShellPanesPage), path, focusNewTab);
43+
return AddNewTabByPathAsync(typeof(ShellPanesPage), path, GeneralSettingsService.AlwaysSwitchToNewlyOpenedTab);
2644
}
2745

2846
public static Task AddNewTabAsync()
2947
{
3048
return AddNewTabByPathAsync(typeof(ShellPanesPage), "Home", true);
3149
}
3250

33-
public static async Task AddNewTabByPathAsync(Type type, string? path, bool focusNewTab, int atIndex = -1)
51+
public static async Task AddNewTabByPathAsync(Type type, string? path, bool switchToNewTab, int atIndex = -1)
3452
{
3553
if (string.IsNullOrEmpty(path))
3654
{
@@ -63,7 +81,7 @@ public static async Task AddNewTabByPathAsync(Type type, string? path, bool focu
6381

6482
MainPageViewModel.AppInstances.Insert(index, tabItem);
6583

66-
if (focusNewTab)
84+
if (switchToNewTab)
6785
App.AppModel.TabStripSelectedIndex = index;
6886
}
6987

src/Files.App/Services/Settings/GeneralSettingsService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public bool AlwaysOpenDualPaneInNewTab
7777
set => Set(value);
7878
}
7979

80+
public bool AlwaysSwitchToNewlyOpenedTab
81+
{
82+
get => Get(false);
83+
set => Set(value);
84+
}
85+
8086
public bool ShowQuickAccessWidget
8187
{
8288
get => Get(true);

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,4 +4016,7 @@
40164016
<data name="Total" xml:space="preserve">
40174017
<value>Total</value>
40184018
</data>
4019+
<data name="AlwaysSwitchToNewlyOpenedTab" xml:space="preserve">
4020+
<value>Always switch focus to newly created tab</value>
4021+
</data>
40194022
</root>

src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ sender is not Button button ||
3838
if (await DriveHelpers.CheckEmptyDrive(path))
3939
return;
4040

41-
await NavigationHelpers.OpenPathInNewTab(path, false);
41+
await NavigationHelpers.OpenPathInNewTab(path);
4242
}
4343

4444
private void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)

src/Files.App/UserControls/Widgets/NetworkLocationsWidget.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ sender is not Button button ||
3838
if (await DriveHelpers.CheckEmptyDrive(path))
3939
return;
4040

41-
await NavigationHelpers.OpenPathInNewTab(path, false);
41+
await NavigationHelpers.OpenPathInNewTab(path);
4242
}
4343

4444
private void Button_RightTapped(object sender, RightTappedRoutedEventArgs e)

src/Files.App/UserControls/Widgets/QuickAccessWidget.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ sender is not Button button ||
2626
button.Tag.ToString() is not string path)
2727
return;
2828

29-
await NavigationHelpers.OpenPathInNewTab(path, false);
29+
await NavigationHelpers.OpenPathInNewTab(path);
3030
}
3131

3232
private async void Button_Click(object sender, RoutedEventArgs e)

0 commit comments

Comments
 (0)