Skip to content

RichCommands: Switch tabs command #11916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Files.App/Actions/Navigation/CloseSelectedTabAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.System;

namespace Files.App.Actions
{
internal class CloseSelectedTabAction : ObservableObject, IAction
{
private readonly IMultitaskingContext context = Ioc.Default.GetRequiredService<IMultitaskingContext>();

public string Label { get; } = "CloseTab".GetLocalizedResource();

public string Description { get; } = "TODO: Need to be described.";

public HotKey HotKey { get; } = new(VirtualKey.W, VirtualKeyModifiers.Control);

public HotKey SecondHotKey { get; } = new(VirtualKey.F4, VirtualKeyModifiers.Control);

public RichGlyph Glyph { get; } = new();

public bool IsExecutable =>
context.Control is not null &&
context.TabCount > 0 &&
context.CurrentTabItem is not null;

public CloseSelectedTabAction()
{
context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
context.Control!.CloseTab(context.CurrentTabItem);

return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IMultitaskingContext.CurrentTabItem):
case nameof(IMultitaskingContext.Control):
case nameof(IMultitaskingContext.TabCount):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
41 changes: 41 additions & 0 deletions src/Files.App/Actions/Navigation/NextTabAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.System;

namespace Files.App.Actions
{
internal class NextTabAction : ObservableObject, IAction
{
private readonly IMultitaskingContext multitaskingContext = Ioc.Default.GetRequiredService<IMultitaskingContext>();

public string Label { get; } = "NextTab".GetLocalizedResource();

public string Description { get; } = "TODO: Need to be described.";

public bool IsExecutable => multitaskingContext.TabCount > 1;

public HotKey HotKey { get; } = new(VirtualKey.Tab, VirtualKeyModifiers.Control);

public NextTabAction()
{
multitaskingContext.PropertyChanged += MultitaskingContext_PropertyChanged;
}

public Task ExecuteAsync()
{
App.AppModel.TabStripSelectedIndex = (App.AppModel.TabStripSelectedIndex + 1) % multitaskingContext.TabCount;
return Task.CompletedTask;
}

private void MultitaskingContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IMultitaskingContext.TabCount))
OnPropertyChanged(nameof(IsExecutable));
}
}
}
45 changes: 45 additions & 0 deletions src/Files.App/Actions/Navigation/PreviousTabAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Commands;
using Files.App.Contexts;
using Files.App.Extensions;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.System;

namespace Files.App.Actions
{
internal class PreviousTabAction : ObservableObject, IAction
{
private readonly IMultitaskingContext multitaskingContext = Ioc.Default.GetRequiredService<IMultitaskingContext>();

public string Label { get; } = "PreviousTab".GetLocalizedResource();

public string Description { get; } = "TODO: Need to be described.";

public bool IsExecutable => multitaskingContext.TabCount > 1;

public HotKey HotKey { get; } = new(VirtualKey.Tab, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);

public PreviousTabAction()
{
multitaskingContext.PropertyChanged += MultitaskingContext_PropertyChanged;
}

public Task ExecuteAsync()
{
if (App.AppModel.TabStripSelectedIndex is 0)
App.AppModel.TabStripSelectedIndex = multitaskingContext.TabCount - 1;
else
App.AppModel.TabStripSelectedIndex--;

return Task.CompletedTask;
}

private void MultitaskingContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(IMultitaskingContext.TabCount))
OnPropertyChanged(nameof(IsExecutable));
}
}
}
3 changes: 3 additions & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,8 @@ public enum CommandCodes
CloseOtherTabsCurrent,
CloseOtherTabsSelected,
ReopenClosedTab,
PreviousTab,
NextTab,
CloseSelectedTab,
}
}
6 changes: 6 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ internal class CommandManager : ICommandManager
public IRichCommand CloseOtherTabsCurrent => commands[CommandCodes.CloseOtherTabsCurrent];
public IRichCommand CloseOtherTabsSelected => commands[CommandCodes.CloseOtherTabsSelected];
public IRichCommand ReopenClosedTab => commands[CommandCodes.ReopenClosedTab];
public IRichCommand PreviousTab => commands[CommandCodes.PreviousTab];
public IRichCommand NextTab => commands[CommandCodes.NextTab];
public IRichCommand CloseSelectedTab => commands[CommandCodes.CloseSelectedTab];

public CommandManager()
{
Expand Down Expand Up @@ -260,6 +263,9 @@ public CommandManager()
[CommandCodes.CloseOtherTabsCurrent] = new CloseOtherTabsCurrentAction(),
[CommandCodes.CloseOtherTabsSelected] = new CloseOtherTabsSelectedAction(),
[CommandCodes.ReopenClosedTab] = new ReopenClosedTabAction(),
[CommandCodes.PreviousTab] = new PreviousTabAction(),
[CommandCodes.NextTab] = new NextTabAction(),
[CommandCodes.CloseSelectedTab] = new CloseSelectedTabAction(),
};

[DebuggerDisplay("Command None")]
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand CloseOtherTabsCurrent { get; }
IRichCommand CloseOtherTabsSelected { get; }
IRichCommand ReopenClosedTab { get; }
IRichCommand PreviousTab { get; }
IRichCommand NextTab { get; }
IRichCommand CloseSelectedTab { get; }
}
}
9 changes: 9 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2860,4 +2860,13 @@
<data name="Untagged" xml:space="preserve">
<value>Untagged</value>
</data>
<data name="PreviousTab" xml:space="preserve">
<value>Moves to the previous tab</value>
</data>
<data name="NextTab" xml:space="preserve">
<value>Moves to the next tab</value>
</data>
<data name="CloseTab" xml:space="preserve">
<value>Closes current tab</value>
</data>
</root>
33 changes: 0 additions & 33 deletions src/Files.App/ViewModels/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public TabItem? SelectedTabItem

public ICommand NavigateToNumberedTabKeyboardAcceleratorCommand { get; private set; }
public IAsyncRelayCommand OpenNewWindowAcceleratorCommand { get; private set; }
public ICommand CloseSelectedTabKeyboardAcceleratorCommand { get; private set; }

public MainPageViewModel(
IUserSettingsService userSettings,
Expand All @@ -57,7 +56,6 @@ public MainPageViewModel(
// Create commands
NavigateToNumberedTabKeyboardAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(NavigateToNumberedTabKeyboardAccelerator);
OpenNewWindowAcceleratorCommand = new AsyncRelayCommand<KeyboardAcceleratorInvokedEventArgs>(OpenNewWindowAccelerator);
CloseSelectedTabKeyboardAcceleratorCommand = new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(CloseSelectedTabKeyboardAccelerator);
}

private void NavigateToNumberedTabKeyboardAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
Expand Down Expand Up @@ -101,26 +99,6 @@ private void NavigateToNumberedTabKeyboardAccelerator(KeyboardAcceleratorInvoked
// Select the last tab
indexToSelect = AppInstances.Count - 1;
break;

case VirtualKey.Tab:
bool shift = e.KeyboardAccelerator.Modifiers.HasFlag(VirtualKeyModifiers.Shift);

if (!shift) // ctrl + tab, select next tab
{
if ((App.AppModel.TabStripSelectedIndex + 1) < AppInstances.Count)
indexToSelect = App.AppModel.TabStripSelectedIndex + 1;
else
indexToSelect = 0;
}
else // ctrl + shift + tab, select previous tab
{
if ((App.AppModel.TabStripSelectedIndex - 1) >= 0)
indexToSelect = App.AppModel.TabStripSelectedIndex - 1;
else
indexToSelect = AppInstances.Count - 1;
}

break;
}

// Only select the tab if it is in the list
Expand All @@ -136,17 +114,6 @@ private async Task OpenNewWindowAccelerator(KeyboardAcceleratorInvokedEventArgs?
e!.Handled = true;
}

private void CloseSelectedTabKeyboardAccelerator(KeyboardAcceleratorInvokedEventArgs? e)
{
var index = App.AppModel.TabStripSelectedIndex >= AppInstances.Count
? AppInstances.Count - 1
: App.AppModel.TabStripSelectedIndex;

var tabItem = AppInstances[index];
MultitaskingControl?.CloseTab(tabItem);
e!.Handled = true;
}

public static async Task AddNewTabByPathAsync(Type type, string? path, int atIndex = -1)
{
if (string.IsNullOrEmpty(path))
Expand Down
28 changes: 0 additions & 28 deletions src/Files.App/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,41 +96,13 @@
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
<KeyboardAccelerator Key="Tab" Modifiers="Control,Shift">
<i:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="Invoked">
<icore:InvokeCommandAction Command="{x:Bind ViewModel.NavigateToNumberedTabKeyboardAcceleratorCommand}" />
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
<KeyboardAccelerator Key="Tab" Modifiers="Control">
<i:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="Invoked">
<icore:InvokeCommandAction Command="{x:Bind ViewModel.NavigateToNumberedTabKeyboardAcceleratorCommand}" />
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
<KeyboardAccelerator Key="N" Modifiers="Control">
<i:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="Invoked">
<icore:InvokeCommandAction Command="{x:Bind ViewModel.OpenNewWindowAcceleratorCommand}" />
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
<KeyboardAccelerator Key="F4" Modifiers="Control">
<i:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="Invoked">
<icore:InvokeCommandAction Command="{x:Bind ViewModel.CloseSelectedTabKeyboardAcceleratorCommand}" />
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
<KeyboardAccelerator Key="W" Modifiers="Control">
<i:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="Invoked">
<icore:InvokeCommandAction Command="{x:Bind ViewModel.CloseSelectedTabKeyboardAcceleratorCommand}" />
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
</Page.KeyboardAccelerators>

<Border>
Expand Down