Skip to content

Commit

Permalink
Rich Commands: Toggle sidebar (#11847)
Browse files Browse the repository at this point in the history
Co-authored-by: Yair <[email protected]>
  • Loading branch information
ferrariofilippo and yaira2 authored Mar 30, 2023
1 parent 443a71e commit bb138fd
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 23 deletions.
41 changes: 41 additions & 0 deletions src/Files.App/Actions/Show/ToggleSidebarAction.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.Extensions;
using Files.App.ViewModels;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.System;

namespace Files.App.Actions
{
internal class ToggleSidebarAction : ObservableObject, IToggleAction
{
private readonly SidebarViewModel viewModel = Ioc.Default.GetRequiredService<SidebarViewModel>();

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

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

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

public bool IsOn => viewModel.IsSidebarOpen;

public ToggleSidebarAction()
{
viewModel.PropertyChanged += ViewModel_PropertyChanged;
}

public Task ExecuteAsync()
{
viewModel.IsSidebarOpen = !IsOn;
return Task.CompletedTask;
}

private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(SidebarViewModel.IsSidebarOpen))
OnPropertyChanged(nameof(IsOn));
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs e)
.AddSingleton<IJumpListService, JumpListService>()
.AddSingleton<MainPageViewModel>()
.AddSingleton<PreviewPaneViewModel>()
.AddSingleton<SidebarViewModel>()
.AddSingleton<SettingsViewModel>()
.AddSingleton<OngoingTasksViewModel>()
.AddSingleton<AppearanceViewModel>()
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Commands/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum CommandCodes
ToggleShowHiddenItems,
ToggleShowFileExtensions,
TogglePreviewPane,
ToggleSidebar,

// File System
CopyItem,
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal class CommandManager : ICommandManager
public IRichCommand ToggleShowHiddenItems => commands[CommandCodes.ToggleShowHiddenItems];
public IRichCommand ToggleShowFileExtensions => commands[CommandCodes.ToggleShowFileExtensions];
public IRichCommand TogglePreviewPane => commands[CommandCodes.TogglePreviewPane];
public IRichCommand ToggleSidebar => commands[CommandCodes.ToggleSidebar];
public IRichCommand SelectAll => commands[CommandCodes.SelectAll];
public IRichCommand InvertSelection => commands[CommandCodes.InvertSelection];
public IRichCommand ClearSelection => commands[CommandCodes.ClearSelection];
Expand Down Expand Up @@ -164,6 +165,7 @@ public CommandManager()
[CommandCodes.ToggleShowHiddenItems] = new ToggleShowHiddenItemsAction(),
[CommandCodes.ToggleShowFileExtensions] = new ToggleShowFileExtensionsAction(),
[CommandCodes.TogglePreviewPane] = new TogglePreviewPaneAction(),
[CommandCodes.ToggleSidebar] = new ToggleSidebarAction(),
[CommandCodes.SelectAll] = new SelectAllAction(),
[CommandCodes.InvertSelection] = new InvertSelectionAction(),
[CommandCodes.ClearSelection] = new ClearSelectionAction(),
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand ToggleShowHiddenItems { get; }
IRichCommand ToggleShowFileExtensions { get; }
IRichCommand TogglePreviewPane { get; }
IRichCommand ToggleSidebar { get; }

IRichCommand CopyItem { get; }
IRichCommand CopyPath { get; }
Expand Down
3 changes: 3 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,9 @@
<data name="ExitCompactOverlayDescription" xml:space="preserve">
<value>Exit compact overlay</value>
</data>
<data name="ToggleSidebar" xml:space="preserve">
<value>Toggle the sidebar</value>
</data>
<data name="Key.Menu" xml:space="preserve">
<value>Menu</value>
<comment>Key name for hotkeys in menus. Use abbreviation if possible.</comment>
Expand Down
10 changes: 0 additions & 10 deletions src/Files.App/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
<KeyboardAccelerator
Key="S"
x:Name="ToggleSidebarVisibilityKeyboardAcceleretor"
Modifiers="Control,Menu">
<i:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="Invoked">
<icore:InvokeCommandAction Command="{x:Bind ToggleSidebarCollapsedStateCommand}" />
</icore:EventTriggerBehavior>
</i:Interaction.Behaviors>
</KeyboardAccelerator>
</Page.KeyboardAccelerators>

<Border>
Expand Down
15 changes: 2 additions & 13 deletions src/Files.App/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.WinUI.Helpers;
using CommunityToolkit.WinUI.UI;
using CommunityToolkit.WinUI.UI.Controls;
Expand All @@ -25,7 +24,6 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Input;
using UWPToWinAppSDKUpgradeHelpers;
using Windows.ApplicationModel;
using Windows.Services.Store;
Expand All @@ -45,6 +43,8 @@ public sealed partial class MainPage : Page, INotifyPropertyChanged
public ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();
public IWindowContext WindowContext { get; } = Ioc.Default.GetRequiredService<IWindowContext>();

public SidebarViewModel SidebarAdaptiveViewModel = Ioc.Default.GetRequiredService<SidebarViewModel>();

public AppModel AppModel => App.AppModel;

public MainPageViewModel ViewModel
Expand All @@ -53,20 +53,15 @@ public MainPageViewModel ViewModel
set => DataContext = value;
}


/// <summary>
/// True if the user is currently resizing the preview pane
/// </summary>
private bool draggingPreviewPane;

private bool keyReleased = true;

public SidebarViewModel SidebarAdaptiveViewModel = new SidebarViewModel();

public readonly OngoingTasksViewModel OngoingTasksViewModel;

private ICommand ToggleSidebarCollapsedStateCommand => new RelayCommand<KeyboardAcceleratorInvokedEventArgs>(x => ToggleSidebarCollapsedState(x));

public MainPage()
{
InitializeComponent();
Expand Down Expand Up @@ -406,12 +401,6 @@ private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
}
}

private void ToggleSidebarCollapsedState(KeyboardAcceleratorInvokedEventArgs? e)
{
SidebarAdaptiveViewModel.IsSidebarOpen = !SidebarAdaptiveViewModel.IsSidebarOpen;
e!.Handled = true;
}

private void SidebarControl_Loaded(object sender, RoutedEventArgs e)
{
SidebarAdaptiveViewModel.UpdateTabControlMargin(); // Set the correct tab margin on startup
Expand Down

0 comments on commit bb138fd

Please sign in to comment.