Skip to content

RichCommand: Layout Sort Group #11678

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 16 commits into from
Mar 16, 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
221 changes: 221 additions & 0 deletions src/Files.App/Actions/Display/GroupAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using Files.App.Contexts;
using Files.App.Extensions;
using Files.Shared.Enums;
using System.ComponentModel;
using System.Threading.Tasks;

namespace Files.App.Actions
{
internal class GroupByNoneAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.None;

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

internal class GroupByNameAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.Name;

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

internal class GroupByDateModifiedAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.DateModified;

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

internal class GroupByDateCreatedAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.DateCreated;

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

internal class GroupBySizeAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.Size;

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

internal class GroupByTypeAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.FileType;

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

internal class GroupBySyncStatusAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.SyncStatus;

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

protected override bool GetIsExecutable(ContentPageTypes pageType) => pageType is ContentPageTypes.CloudDrive;
}

internal class GroupByTagAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.FileTag;

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

internal class GroupByOriginalFolderAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.OriginalFolder;

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

protected override bool GetIsExecutable(ContentPageTypes pageType) => pageType is ContentPageTypes.CloudDrive;
}

internal class GroupByDateDeletedAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.DateDeleted;

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

protected override bool GetIsExecutable(ContentPageTypes pageType) => pageType is ContentPageTypes.RecycleBin;
}

internal class GroupByFolderPathAction : GroupByAction
{
protected override GroupOption GroupOption { get; } = GroupOption.FolderPath;

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

protected override bool GetIsExecutable(ContentPageTypes pageType) => pageType is ContentPageTypes.Library;
}

internal abstract class GroupByAction : ObservableObject, IToggleAction
{
protected IContentPageContext ContentContext { get; } = Ioc.Default.GetRequiredService<IContentPageContext>();
protected IDisplayPageContext DisplayContext { get; } = Ioc.Default.GetRequiredService<IDisplayPageContext>();

protected abstract GroupOption GroupOption { get; }

public abstract string Label { get; }

private bool isOn;
public bool IsOn => isOn;

private bool isExecutable = false;
public bool IsExecutable => isExecutable;

public GroupByAction()
{
isOn = DisplayContext.GroupOption == GroupOption;
isExecutable = GetIsExecutable(ContentContext.PageType);

ContentContext.PropertyChanged += ContentContext_PropertyChanged;
DisplayContext.PropertyChanged += DisplayContext_PropertyChanged;
}

public Task ExecuteAsync()
{
DisplayContext.GroupOption = GroupOption;
return Task.CompletedTask;
}

protected virtual bool GetIsExecutable(ContentPageTypes pageType) => true;

private void ContentContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IContentPageContext.PageType))
SetProperty(ref isExecutable, GetIsExecutable(ContentContext.PageType), nameof(IsExecutable));
}

private void DisplayContext_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IDisplayPageContext.GroupOption))
SetProperty(ref isOn, DisplayContext.GroupOption == GroupOption, nameof(IsOn));
}
}

internal class GroupAscendingAction : ObservableObject, IToggleAction
{
private IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

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

public bool IsOn => context.GroupDirection is SortDirection.Ascending;
public bool IsExecutable => context.GroupOption is not GroupOption.None;

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

public Task ExecuteAsync()
{
context.GroupDirection = SortDirection.Ascending;
return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IDisplayPageContext.GroupOption):
OnPropertyChanged(nameof(IsExecutable));
break;
case nameof(IDisplayPageContext.GroupDirection):
OnPropertyChanged(nameof(IsOn));
break;
}
}
}

internal class GroupDescendingAction : ObservableObject, IToggleAction
{
private IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

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

public bool IsOn => context.GroupDirection is SortDirection.Descending;
public bool IsExecutable => context.GroupOption is not GroupOption.None;

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

public Task ExecuteAsync()
{
context.GroupDirection = SortDirection.Descending;
return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(IDisplayPageContext.GroupOption):
OnPropertyChanged(nameof(IsExecutable));
break;
case nameof(IDisplayPageContext.GroupDirection):
OnPropertyChanged(nameof(IsOn));
break;
}
}
}

internal class ToggleGroupDirectionAction : IAction
{
private IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

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

public Task ExecuteAsync()
{
context.GroupDirection = context.SortDirection is SortDirection.Descending ? SortDirection.Ascending : SortDirection.Descending;
return Task.CompletedTask;
}
}
}
161 changes: 161 additions & 0 deletions src/Files.App/Actions/Display/LayoutAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
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 LayoutDetailsAction : ToggleLayoutAction
{
protected override LayoutTypes LayoutType => LayoutTypes.Details;

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

public override RichGlyph Glyph { get; } = new("\uE179");
public override HotKey HotKey { get; } = new(VirtualKey.Number1, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);
}

internal class LayoutTilesAction : ToggleLayoutAction
{
protected override LayoutTypes LayoutType => LayoutTypes.Tiles;

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

public override RichGlyph Glyph { get; } = new("\uE15C");
public override HotKey HotKey { get; } = new(VirtualKey.Number2, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);
}

internal class LayoutGridSmallAction : ToggleLayoutAction
{
protected override LayoutTypes LayoutType => LayoutTypes.GridSmall;

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

public override RichGlyph Glyph { get; } = new("\uE80A");
public override HotKey HotKey { get; } = new(VirtualKey.Number3, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);
}

internal class LayoutGridMediumAction : ToggleLayoutAction
{
protected override LayoutTypes LayoutType => LayoutTypes.GridMedium;

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

public override RichGlyph Glyph { get; } = new("\uF0E2");
public override HotKey HotKey { get; } = new(VirtualKey.Number4, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);
}

internal class LayoutGridLargeAction : ToggleLayoutAction
{
protected override LayoutTypes LayoutType => LayoutTypes.GridLarge;

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

public override RichGlyph Glyph { get; } = new("\uE739");
public override HotKey HotKey { get; } = new(VirtualKey.Number5, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);
}

internal class LayoutColumnsAction : ToggleLayoutAction
{
protected override LayoutTypes LayoutType => LayoutTypes.Columns;

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

public override RichGlyph Glyph { get; } = new(opacityStyle: "ColorIconColumnsLayout");
public override HotKey HotKey { get; } = new(VirtualKey.Number6, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);
}

internal class LayoutAdaptiveAction : ToggleLayoutAction
{
protected override LayoutTypes LayoutType => LayoutTypes.Adaptive;

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

public override bool IsExecutable => Context.IsLayoutAdaptiveEnabled;

public override RichGlyph Glyph { get; } = new("\uF576");
public override HotKey HotKey { get; } = new(VirtualKey.Number7, VirtualKeyModifiers.Control | VirtualKeyModifiers.Shift);

protected override void OnContextChanged(string propertyName)
{
if (propertyName is nameof(IDisplayPageContext.IsLayoutAdaptiveEnabled))
OnPropertyChanged(nameof(IsExecutable));
}
}

internal abstract class ToggleLayoutAction : ObservableObject, IToggleAction
{
protected IDisplayPageContext Context { get; } = Ioc.Default.GetRequiredService<IDisplayPageContext>();

protected abstract LayoutTypes LayoutType { get; }

public abstract string Label { get; }

public abstract RichGlyph Glyph { get; }
public abstract HotKey HotKey { get; }

private bool isOn;
public bool IsOn => isOn;

public virtual bool IsExecutable => true;

public ToggleLayoutAction()
{
isOn = Context.LayoutType == LayoutType;
Context.PropertyChanged += Context_PropertyChanged;
}

public Task ExecuteAsync()
{
Context.LayoutType = LayoutType;
return Task.CompletedTask;
}

private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName is nameof(IDisplayPageContext.LayoutType))
SetProperty(ref isOn, Context.LayoutType == LayoutType, nameof(IsOn));

if (e.PropertyName is not null)
OnContextChanged(e.PropertyName);
}

protected virtual void OnContextChanged(string propertyName) { }
}

internal class LayoutDecreaseSizeAction : IAction
{
private readonly IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

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

public HotKey HotKey { get; } = new(VirtualKey.Subtract, VirtualKeyModifiers.Control);
public HotKey MediaHotKey { get; } = new((VirtualKey)189, VirtualKeyModifiers.Control);

public Task ExecuteAsync()
{
context.DecreaseLayoutSize();
return Task.CompletedTask;
}
}

internal class LayoutIncreaseSizeAction : IAction
{
private readonly IDisplayPageContext context = Ioc.Default.GetRequiredService<IDisplayPageContext>();

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

public HotKey HotKey { get; } = new(VirtualKey.Add, VirtualKeyModifiers.Control);
public HotKey MediaHotKey { get; } = new((VirtualKey)187, VirtualKeyModifiers.Control);

public Task ExecuteAsync()
{
context.IncreaseLayoutSize();
return Task.CompletedTask;
}
}
}
Loading