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 5 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
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,51 @@
// 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 BaseOpenInNewPaneAction()
{
ContentPageContext.PropertyChanged += Context_PropertyChanged;
}

public virtual Task ExecuteAsync()
{
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;
}
}
}
}
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
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,31 @@
// 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 =>
HomePageContext.IsAnyItemRightClicked &&
HomePageContext.RightClickedItem is not null;

public override async Task ExecuteAsync()
{
if (await DriveHelpers.CheckEmptyDrive(HomePageContext.RightClickedItem!.Path))
return;

ContentPageContext.ShellPage!.PaneHolder?.OpenPathInNewPane(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,31 @@
// 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 =>
SidebarContext.IsItemRightClicked &&
SidebarContext.RightClickedItem is not null;

public override async Task ExecuteAsync()
{
if (await DriveHelpers.CheckEmptyDrive(HomePageContext.RightClickedItem!.Path))
return;

ContentPageContext.ShellPage!.PaneHolder?.OpenPathInNewPane(HomePageContext.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,63 @@
// 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 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()
{
foreach (ListedItem listedItem in ContentPageContext.SelectedItems)
{
await MainWindow.Instance.DispatcherQueue.EnqueueOrInvokeAsync(async () =>
{
await NavigationHelpers.AddNewTabByPathAsync(
typeof(PaneHolderPage),
(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;
}
}
}
}
yaira2 marked this conversation as resolved.
Show resolved Hide resolved
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
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class OpenInNewTabFromHomeAction : BaseOpenInNewTabAction
{
public override bool IsExecutable =>
HomePageContext.IsAnyItemRightClicked &&
HomePageContext.RightClickedItem is not null;

public override async Task ExecuteAsync()
{
if (await DriveHelpers.CheckEmptyDrive(HomePageContext.RightClickedItem!.Path))
return;

await NavigationHelpers.OpenPathInNewTab(HomePageContext.RightClickedItem!.Path ?? string.Empty, false);
}

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,31 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Actions
{
internal sealed class OpenInNewTabFromSidebarAction : BaseOpenInNewTabAction
{
public override bool IsExecutable =>
SidebarContext.IsItemRightClicked &&
SidebarContext.RightClickedItem is not null;

public override async Task ExecuteAsync()
{
if (await DriveHelpers.CheckEmptyDrive(SidebarContext.RightClickedItem!.Path))
return;

await NavigationHelpers.OpenPathInNewTab(SidebarContext.RightClickedItem!.Path ?? string.Empty, false);
}

protected override void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(ISidebarContext.IsItemRightClicked):
case nameof(ISidebarContext.RightClickedItem):
OnPropertyChanged(nameof(IsExecutable));
break;
}
}
}
}