-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
RichCommand: Base and commands ShowFileExtensions/ShowHiddenItems #11327
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Files.App.Actions | ||
| { | ||
| public interface IAction | ||
| { | ||
| string Label { get; } | ||
|
|
||
| bool IsExecutable => true; | ||
|
|
||
| Task ExecuteAsync(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| namespace Files.App.Actions | ||
| { | ||
| public interface IToggleAction : IAction | ||
| { | ||
| bool IsOn { get; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using CommunityToolkit.Mvvm.ComponentModel; | ||
| using CommunityToolkit.Mvvm.DependencyInjection; | ||
| using Files.App.Extensions; | ||
| using Files.Backend.Services.Settings; | ||
| using System.ComponentModel; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Files.App.Actions | ||
| { | ||
| internal class ShowFileExtensionsAction : ObservableObject, IToggleAction | ||
| { | ||
| private readonly IFoldersSettingsService settings = Ioc.Default.GetRequiredService<IFoldersSettingsService>(); | ||
|
|
||
| public string Label => "ShowFileExtensions".GetLocalizedResource(); | ||
|
|
||
| public bool IsOn => settings.ShowFileExtensions; | ||
|
|
||
| public ShowFileExtensionsAction() => settings.PropertyChanged += Settings_PropertyChanged; | ||
|
|
||
| public Task ExecuteAsync() | ||
| { | ||
| settings.ShowFileExtensions = !settings.ShowFileExtensions; | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private void Settings_PropertyChanged(object? _, PropertyChangedEventArgs e) | ||
| { | ||
| if (e.PropertyName is nameof(IFoldersSettingsService.ShowFileExtensions)) | ||
| OnPropertyChanged(nameof(IsOn)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using CommunityToolkit.Mvvm.ComponentModel; | ||
| using CommunityToolkit.Mvvm.DependencyInjection; | ||
| using Files.App.Extensions; | ||
| using Files.Backend.Services.Settings; | ||
| using System.ComponentModel; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Files.App.Actions | ||
| { | ||
| internal class ShowHiddenItemsAction : ObservableObject, IToggleAction | ||
| { | ||
| private readonly IFoldersSettingsService settings = Ioc.Default.GetRequiredService<IFoldersSettingsService>(); | ||
|
|
||
| public string Label => "ShowHiddenItems".GetLocalizedResource(); | ||
|
|
||
| public bool IsOn => settings.ShowHiddenItems; | ||
|
|
||
| public ShowHiddenItemsAction() => settings.PropertyChanged += Settings_PropertyChanged; | ||
|
|
||
| public Task ExecuteAsync() | ||
| { | ||
| settings.ShowHiddenItems = !settings.ShowHiddenItems; | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private void Settings_PropertyChanged(object? _, PropertyChangedEventArgs e) | ||
| { | ||
| if (e.PropertyName is nameof(IFoldersSettingsService.ShowHiddenItems)) | ||
| OnPropertyChanged(nameof(IsOn)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace Files.App.Commands | ||
| { | ||
| public enum CommandCodes | ||
| { | ||
| None, | ||
|
|
||
| // show | ||
| ShowHiddenItems, | ||
| ShowFileExtensions, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using Microsoft.UI.Xaml.Input; | ||
| using System.ComponentModel; | ||
| using System.Threading.Tasks; | ||
| using System.Windows.Input; | ||
|
|
||
| namespace Files.App.Commands | ||
| { | ||
| public interface IRichCommand : ICommand, INotifyPropertyChanging, INotifyPropertyChanged | ||
| { | ||
| CommandCodes Code { get; } | ||
|
|
||
| string Label { get; } | ||
|
|
||
| bool IsToggle { get; } | ||
| bool IsOn { get; set; } | ||
| bool IsExecutable { get; } | ||
|
|
||
| Task ExecuteAsync(); | ||
|
|
||
| void ExecuteTapped(object sender, TappedRoutedEventArgs e); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| using CommunityToolkit.Mvvm.ComponentModel; | ||
| using CommunityToolkit.Mvvm.Input; | ||
| using Files.App.Actions; | ||
| using Microsoft.UI.Xaml.Input; | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Diagnostics; | ||
| using System.Threading.Tasks; | ||
| using System.Windows.Input; | ||
|
|
||
| namespace Files.App.Commands | ||
| { | ||
| internal class CommandManager : ICommandManager | ||
| { | ||
| private readonly IDictionary<CommandCodes, IRichCommand> commands = new Dictionary<CommandCodes, IRichCommand> | ||
| { | ||
| [CommandCodes.None] = new NoneCommand(), | ||
| }; | ||
|
|
||
| public IRichCommand this[CommandCodes code] => GetCommand(code); | ||
|
|
||
| public IRichCommand None => GetCommand(CommandCodes.None); | ||
| public IRichCommand ShowHiddenItems => GetCommand(CommandCodes.ShowHiddenItems); | ||
| public IRichCommand ShowFileExtensions => GetCommand(CommandCodes.ShowFileExtensions); | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| public IEnumerator<IRichCommand> GetEnumerator() => commands.Values.GetEnumerator(); | ||
|
|
||
| private IRichCommand GetCommand(CommandCodes code) | ||
| { | ||
| if (commands.TryGetValue(code, out IRichCommand? command)) | ||
| return command; | ||
|
|
||
| var action = GetAction(code); | ||
| var newCommand = new ActionCommand(code, action); | ||
| commands.Add(code, newCommand); | ||
|
|
||
| return newCommand; | ||
| } | ||
|
|
||
| private static IAction GetAction(CommandCodes code) => code switch | ||
| { | ||
| CommandCodes.ShowHiddenItems => new ShowHiddenItemsAction(), | ||
| CommandCodes.ShowFileExtensions => new ShowFileExtensionsAction(), | ||
| _ => throw new ArgumentOutOfRangeException(nameof(code)), | ||
| }; | ||
|
|
||
| [DebuggerDisplay("Command None")] | ||
| private class NoneCommand : IRichCommand | ||
| { | ||
| public event EventHandler? CanExecuteChanged { add {} remove {} } | ||
| public event PropertyChangingEventHandler? PropertyChanging { add { } remove { } } | ||
| public event PropertyChangedEventHandler? PropertyChanged { add { } remove { } } | ||
|
|
||
| public CommandCodes Code => CommandCodes.None; | ||
|
|
||
| public string Label => string.Empty; | ||
|
|
||
| public bool IsToggle => false; | ||
| public bool IsOn { get => false; set {} } | ||
| public bool IsExecutable => false; | ||
|
|
||
| public bool CanExecute(object? _) => false; | ||
| public void Execute(object? _) {} | ||
| public Task ExecuteAsync() => Task.CompletedTask; | ||
| public void ExecuteTapped(object _, TappedRoutedEventArgs e) {} | ||
| } | ||
|
|
||
| [DebuggerDisplay("Command {Code}")] | ||
| private class ActionCommand : ObservableObject, IRichCommand | ||
| { | ||
| public event EventHandler? CanExecuteChanged; | ||
|
|
||
| private readonly IAction action; | ||
| private readonly ICommand command; | ||
|
|
||
| public CommandCodes Code { get; } | ||
|
|
||
| public string Label => action.Label; | ||
|
|
||
| public bool IsToggle => action is IToggleAction; | ||
|
|
||
| public bool IsOn | ||
| { | ||
| get => action is IToggleAction toggleAction && toggleAction.IsOn; | ||
| set | ||
| { | ||
| if (action is IToggleAction toggleAction && toggleAction.IsOn != value) | ||
| command.Execute(null); | ||
| } | ||
| } | ||
|
|
||
| public bool IsExecutable => action.IsExecutable; | ||
|
|
||
| public ActionCommand(CommandCodes code, IAction action) | ||
| { | ||
| Code = code; | ||
| this.action = action; | ||
|
|
||
| command = new AsyncRelayCommand(ExecuteAsync, () => action.IsExecutable); | ||
|
|
||
| if (action is INotifyPropertyChanging notifyPropertyChanging) | ||
| notifyPropertyChanging.PropertyChanging += Action_PropertyChanging; | ||
| if (action is INotifyPropertyChanged notifyPropertyChanged) | ||
| notifyPropertyChanged.PropertyChanged += Action_PropertyChanged; | ||
| } | ||
|
|
||
| public bool CanExecute(object? parameter) => command.CanExecute(parameter); | ||
| public void Execute(object? parameter) => command.Execute(parameter); | ||
|
|
||
| public Task ExecuteAsync() | ||
| { | ||
| if (IsExecutable) | ||
| return action.ExecuteAsync(); | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public async void ExecuteTapped(object _, TappedRoutedEventArgs e) => await action.ExecuteAsync(); | ||
|
|
||
| private void Action_PropertyChanging(object? _, PropertyChangingEventArgs e) | ||
| { | ||
| switch (e.PropertyName) | ||
| { | ||
| case nameof(IAction.Label): | ||
| OnPropertyChanging(nameof(Label)); | ||
| break; | ||
| case nameof(IToggleAction.IsOn) when IsToggle: | ||
| OnPropertyChanging(nameof(IsOn)); | ||
| break; | ||
| case nameof(IAction.IsExecutable): | ||
| OnPropertyChanging(nameof(IsExecutable)); | ||
| break; | ||
| } | ||
| } | ||
| private void Action_PropertyChanged(object? sender, PropertyChangedEventArgs e) | ||
| { | ||
| switch (e.PropertyName) | ||
| { | ||
| case nameof(IAction.Label): | ||
| OnPropertyChanged(nameof(Label)); | ||
| break; | ||
| case nameof(IToggleAction.IsOn) when IsToggle: | ||
| OnPropertyChanged(nameof(IsOn)); | ||
| break; | ||
| case nameof(IAction.IsExecutable): | ||
| OnPropertyChanged(nameof(IsExecutable)); | ||
| CanExecuteChanged?.Invoke(this, EventArgs.Empty); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Files.App.Commands | ||
| { | ||
| public interface ICommandManager : IEnumerable<IRichCommand> | ||
| { | ||
| IRichCommand this[CommandCodes code] { get; } | ||
|
|
||
| IRichCommand None { get; } | ||
|
|
||
| IRichCommand ShowHiddenItems { get; } | ||
| IRichCommand ShowFileExtensions { get; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.