Skip to content
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

Code Quality: Use RichCommands in home and sidebar #15329

Merged
merged 25 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Files.App/Actions/IAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ HotKey MediaHotKey
bool IsExecutable
=> true;

/// <summary>
/// Returns whether the action is accessible in any context.
/// </summary>
bool IsAccessibleGlobally
=> true;

/// <summary>
/// Executes the action asynchronously.
/// </summary>
Expand Down
53 changes: 0 additions & 53 deletions src/Files.App/Actions/Navigation/OpenDirectoryInNewPaneAction.cs

This file was deleted.

67 changes: 0 additions & 67 deletions src/Files.App/Actions/Navigation/OpenDirectoryInNewTabAction.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal abstract class BaseOpenInNewPaneAction : ObservableObject, IAction
{
protected IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
protected IContentPageContext ContentPageContext { get; } = Ioc.Default.GetRequiredService<IContentPageContext>();
protected IHomePageContext HomePageContext { get; } = Ioc.Default.GetRequiredService<IHomePageContext>();
protected ISidebarContext SidebarContext { get; } = Ioc.Default.GetRequiredService<ISidebarContext>();

public string Label
=> "OpenInNewPane".GetLocalizedResource();

public string Description
=> "OpenDirectoryInNewPaneDescription".GetLocalizedResource();

public virtual bool IsExecutable =>
ContentPageContext.SelectedItem is not null &&
ContentPageContext.SelectedItem.IsFolder &&
UserSettingsService.GeneralSettingsService.ShowOpenInNewPane;

public virtual bool IsAccessibleGlobally
=> true;

public BaseOpenInNewPaneAction()
{
ContentPageContext.PropertyChanged += Context_PropertyChanged;
}

public virtual Task ExecuteAsync(object? parameter = null)
{
NavigationHelpers.OpenInSecondaryPane(
ContentPageContext.ShellPage,
ContentPageContext.ShellPage.SlimContentPage.SelectedItems.FirstOrDefault());

return Task.CompletedTask;
}

protected virtual void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.ShellPage):
case nameof(IContentPageContext.PageType):
case nameof(IContentPageContext.HasSelection):
case nameof(IContentPageContext.SelectedItems):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class OpenInNewPaneAction : BaseOpenInNewPaneAction
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class OpenInNewPaneFromHomeAction : BaseOpenInNewPaneAction
{
public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewPane &&
HomePageContext.IsAnyItemRightClicked &&
HomePageContext.RightClickedItem is not null &&
(HomePageContext.RightClickedItem is WidgetFileTagCardItem fileTagItem
? fileTagItem.IsFolder
: true);

public override bool IsAccessibleGlobally
=> false;

public override async Task ExecuteAsync(object? parameter = null)
{
if (HomePageContext.RightClickedItem is null)
return;

if (await DriveHelpers.CheckEmptyDrive(HomePageContext.RightClickedItem!.Path))
return;

ContentPageContext.ShellPage!.PaneHolder?.OpenSecondaryPane(HomePageContext.RightClickedItem!.Path ?? string.Empty);
}

protected override void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IHomePageContext.IsAnyItemRightClicked):
case nameof(IHomePageContext.RightClickedItem):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class OpenInNewPaneFromSidebarAction : BaseOpenInNewPaneAction
{
public override bool IsExecutable =>
UserSettingsService.GeneralSettingsService.ShowOpenInNewPane &&
SidebarContext.IsItemRightClicked &&
SidebarContext.RightClickedItem is not null &&
SidebarContext.RightClickedItem.MenuOptions.IsLocationItem;

public override bool IsAccessibleGlobally
=> false;

public override async Task ExecuteAsync(object? parameter = null)
{
if (SidebarContext.RightClickedItem is null)
return;

if (await DriveHelpers.CheckEmptyDrive(SidebarContext.RightClickedItem!.Path))
return;

ContentPageContext.ShellPage!.PaneHolder?.OpenSecondaryPane(SidebarContext.RightClickedItem!.Path ?? string.Empty);
}

protected override void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(ISidebarContext.IsItemRightClicked):
case nameof(ISidebarContext.RightClickedItem):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal abstract class BaseOpenInNewTabAction : ObservableObject, IAction
{
protected IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
protected IContentPageContext ContentPageContext { get; } = Ioc.Default.GetRequiredService<IContentPageContext>();
protected IHomePageContext HomePageContext { get; } = Ioc.Default.GetRequiredService<IHomePageContext>();
protected ISidebarContext SidebarContext { get; } = Ioc.Default.GetRequiredService<ISidebarContext>();

public string Label
=> "OpenInNewTab".GetLocalizedResource();

public string Description
=> "OpenDirectoryInNewTabDescription".GetLocalizedResource();

public RichGlyph Glyph
=> new(opacityStyle: "ColorIconOpenInNewTab");

public virtual bool IsAccessibleGlobally
=> true;

public virtual bool IsExecutable =>
ContentPageContext.ShellPage is not null &&
ContentPageContext.ShellPage.SlimContentPage is not null &&
ContentPageContext.SelectedItems.Count is not 0 &&
ContentPageContext.SelectedItems.Count <= 5 &&
ContentPageContext.SelectedItems.Count(x => x.IsFolder) == ContentPageContext.SelectedItems.Count &&
UserSettingsService.GeneralSettingsService.ShowOpenInNewTab;

public BaseOpenInNewTabAction()
{
ContentPageContext.PropertyChanged += Context_PropertyChanged;
}

public virtual async Task ExecuteAsync(object? parameter = null)
{
foreach (ListedItem listedItem in ContentPageContext.SelectedItems)
{
await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
await NavigationHelpers.AddNewTabByPathAsync(
typeof(ShellPanesPage),
(listedItem as ShortcutItem)?.TargetPath ?? listedItem.ItemPath,
false);
},
Microsoft.UI.Dispatching.DispatcherQueuePriority.Low);
}
}

protected virtual void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IContentPageContext.ShellPage):
case nameof(IContentPageContext.PageType):
case nameof(IContentPageContext.HasSelection):
case nameof(IContentPageContext.SelectedItems):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class OpenInNewTabAction : BaseOpenInNewTabAction
{
}
}