|
| 1 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 2 | +using CommunityToolkit.Mvvm.Input; |
| 3 | +using Files.App.Actions; |
| 4 | +using Microsoft.UI.Xaml.Input; |
| 5 | +using System; |
| 6 | +using System.Collections; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.ComponentModel; |
| 9 | +using System.Diagnostics; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Windows.Input; |
| 12 | + |
| 13 | +namespace Files.App.Commands |
| 14 | +{ |
| 15 | + internal class CommandManager : ICommandManager |
| 16 | + { |
| 17 | + private readonly IDictionary<CommandCodes, IRichCommand> commands = new Dictionary<CommandCodes, IRichCommand> |
| 18 | + { |
| 19 | + [CommandCodes.None] = new NoneCommand(), |
| 20 | + }; |
| 21 | + |
| 22 | + public IRichCommand this[CommandCodes code] => GetCommand(code); |
| 23 | + |
| 24 | + public IRichCommand None => GetCommand(CommandCodes.None); |
| 25 | + public IRichCommand ShowHiddenItems => GetCommand(CommandCodes.ShowHiddenItems); |
| 26 | + public IRichCommand ShowFileExtensions => GetCommand(CommandCodes.ShowFileExtensions); |
| 27 | + |
| 28 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 29 | + public IEnumerator<IRichCommand> GetEnumerator() => commands.Values.GetEnumerator(); |
| 30 | + |
| 31 | + private IRichCommand GetCommand(CommandCodes code) |
| 32 | + { |
| 33 | + if (commands.TryGetValue(code, out IRichCommand? command)) |
| 34 | + return command; |
| 35 | + |
| 36 | + var action = GetAction(code); |
| 37 | + var newCommand = new ActionCommand(code, action); |
| 38 | + commands.Add(code, newCommand); |
| 39 | + |
| 40 | + return newCommand; |
| 41 | + } |
| 42 | + |
| 43 | + private static IAction GetAction(CommandCodes code) => code switch |
| 44 | + { |
| 45 | + CommandCodes.ShowHiddenItems => new ShowHiddenItemsAction(), |
| 46 | + CommandCodes.ShowFileExtensions => new ShowFileExtensionsAction(), |
| 47 | + _ => throw new ArgumentOutOfRangeException(nameof(code)), |
| 48 | + }; |
| 49 | + |
| 50 | + [DebuggerDisplay("Command None")] |
| 51 | + private class NoneCommand : IRichCommand |
| 52 | + { |
| 53 | + public event EventHandler? CanExecuteChanged { add {} remove {} } |
| 54 | + public event PropertyChangingEventHandler? PropertyChanging { add { } remove { } } |
| 55 | + public event PropertyChangedEventHandler? PropertyChanged { add { } remove { } } |
| 56 | + |
| 57 | + public CommandCodes Code => CommandCodes.None; |
| 58 | + |
| 59 | + public string Label => string.Empty; |
| 60 | + |
| 61 | + public bool IsToggle => false; |
| 62 | + public bool IsOn { get => false; set {} } |
| 63 | + public bool IsExecutable => false; |
| 64 | + |
| 65 | + public bool CanExecute(object? _) => false; |
| 66 | + public void Execute(object? _) {} |
| 67 | + public Task ExecuteAsync() => Task.CompletedTask; |
| 68 | + public void ExecuteTapped(object _, TappedRoutedEventArgs e) {} |
| 69 | + } |
| 70 | + |
| 71 | + [DebuggerDisplay("Command {Code}")] |
| 72 | + private class ActionCommand : ObservableObject, IRichCommand |
| 73 | + { |
| 74 | + public event EventHandler? CanExecuteChanged; |
| 75 | + |
| 76 | + private readonly IAction action; |
| 77 | + private readonly ICommand command; |
| 78 | + |
| 79 | + public CommandCodes Code { get; } |
| 80 | + |
| 81 | + public string Label => action.Label; |
| 82 | + |
| 83 | + public bool IsToggle => action is IToggleAction; |
| 84 | + |
| 85 | + public bool IsOn |
| 86 | + { |
| 87 | + get => action is IToggleAction toggleAction && toggleAction.IsOn; |
| 88 | + set |
| 89 | + { |
| 90 | + if (action is IToggleAction toggleAction && toggleAction.IsOn != value) |
| 91 | + command.Execute(null); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public bool IsExecutable => action.IsExecutable; |
| 96 | + |
| 97 | + public ActionCommand(CommandCodes code, IAction action) |
| 98 | + { |
| 99 | + Code = code; |
| 100 | + this.action = action; |
| 101 | + |
| 102 | + command = new AsyncRelayCommand(ExecuteAsync, () => action.IsExecutable); |
| 103 | + |
| 104 | + if (action is INotifyPropertyChanging notifyPropertyChanging) |
| 105 | + notifyPropertyChanging.PropertyChanging += Action_PropertyChanging; |
| 106 | + if (action is INotifyPropertyChanged notifyPropertyChanged) |
| 107 | + notifyPropertyChanged.PropertyChanged += Action_PropertyChanged; |
| 108 | + } |
| 109 | + |
| 110 | + public bool CanExecute(object? parameter) => command.CanExecute(parameter); |
| 111 | + public void Execute(object? parameter) => command.Execute(parameter); |
| 112 | + |
| 113 | + public Task ExecuteAsync() |
| 114 | + { |
| 115 | + if (IsExecutable) |
| 116 | + return action.ExecuteAsync(); |
| 117 | + return Task.CompletedTask; |
| 118 | + } |
| 119 | + |
| 120 | + public async void ExecuteTapped(object _, TappedRoutedEventArgs e) => await action.ExecuteAsync(); |
| 121 | + |
| 122 | + private void Action_PropertyChanging(object? _, PropertyChangingEventArgs e) |
| 123 | + { |
| 124 | + switch (e.PropertyName) |
| 125 | + { |
| 126 | + case nameof(IAction.Label): |
| 127 | + OnPropertyChanging(nameof(Label)); |
| 128 | + break; |
| 129 | + case nameof(IToggleAction.IsOn) when IsToggle: |
| 130 | + OnPropertyChanging(nameof(IsOn)); |
| 131 | + break; |
| 132 | + case nameof(IAction.IsExecutable): |
| 133 | + OnPropertyChanging(nameof(IsExecutable)); |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + private void Action_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 138 | + { |
| 139 | + switch (e.PropertyName) |
| 140 | + { |
| 141 | + case nameof(IAction.Label): |
| 142 | + OnPropertyChanged(nameof(Label)); |
| 143 | + break; |
| 144 | + case nameof(IToggleAction.IsOn) when IsToggle: |
| 145 | + OnPropertyChanged(nameof(IsOn)); |
| 146 | + break; |
| 147 | + case nameof(IAction.IsExecutable): |
| 148 | + OnPropertyChanged(nameof(IsExecutable)); |
| 149 | + CanExecuteChanged?.Invoke(this, EventArgs.Empty); |
| 150 | + break; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments