|
| 1 | +// Copyright (c) 2024 Files Community |
| 2 | +// Licensed under the MIT License. See the LICENSE. |
| 3 | + |
| 4 | +using Files.App.Actions; |
| 5 | +using Microsoft.AppCenter.Analytics; |
| 6 | +using Microsoft.UI.Xaml; |
| 7 | +using Microsoft.UI.Xaml.Controls; |
| 8 | +using Microsoft.UI.Xaml.Input; |
| 9 | + |
| 10 | +namespace Files.App.Data.Commands |
| 11 | +{ |
| 12 | + [DebuggerDisplay("Command {Code}")] |
| 13 | + internal sealed class ActionCommand : ObservableObject, IRichCommand |
| 14 | + { |
| 15 | + private IActionsSettingsService ActionsSettingsService { get; } = Ioc.Default.GetRequiredService<IActionsSettingsService>(); |
| 16 | + |
| 17 | + public event EventHandler? CanExecuteChanged; |
| 18 | + |
| 19 | + public IAction Action { get; } |
| 20 | + |
| 21 | + public CommandCodes Code { get; } |
| 22 | + |
| 23 | + public string Label |
| 24 | + => Action.Label; |
| 25 | + |
| 26 | + public string LabelWithHotKey |
| 27 | + => HotKeyText is null ? Label : $"{Label} ({HotKeyText})"; |
| 28 | + |
| 29 | + public string AutomationName |
| 30 | + => Label; |
| 31 | + |
| 32 | + public string Description |
| 33 | + => Action.Description; |
| 34 | + |
| 35 | + public RichGlyph Glyph |
| 36 | + => Action.Glyph; |
| 37 | + |
| 38 | + public object? Icon { get; } |
| 39 | + |
| 40 | + public FontIcon? FontIcon { get; } |
| 41 | + |
| 42 | + public Style? OpacityStyle { get; } |
| 43 | + |
| 44 | + private bool isCustomHotKeys = false; |
| 45 | + public bool IsCustomHotKeys |
| 46 | + { |
| 47 | + get => isCustomHotKeys; |
| 48 | + set => SetProperty(ref isCustomHotKeys, value); |
| 49 | + } |
| 50 | + |
| 51 | + public string? HotKeyText |
| 52 | + { |
| 53 | + get |
| 54 | + { |
| 55 | + string text = HotKeys.LocalizedLabel; |
| 56 | + if (string.IsNullOrEmpty(text)) |
| 57 | + return null; |
| 58 | + return text; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private HotKeyCollection hotKeys; |
| 63 | + public HotKeyCollection HotKeys |
| 64 | + { |
| 65 | + get => hotKeys; |
| 66 | + set |
| 67 | + { |
| 68 | + if (SetProperty(ref hotKeys, value)) |
| 69 | + { |
| 70 | + OnPropertyChanged(nameof(HotKeyText)); |
| 71 | + OnPropertyChanged(nameof(LabelWithHotKey)); |
| 72 | + IsCustomHotKeys = true; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public HotKeyCollection DefaultHotKeys { get; } |
| 78 | + |
| 79 | + public bool IsToggle |
| 80 | + => Action is IToggleAction; |
| 81 | + |
| 82 | + public bool IsOn |
| 83 | + { |
| 84 | + get => Action is IToggleAction toggleAction && toggleAction.IsOn; |
| 85 | + set |
| 86 | + { |
| 87 | + if (Action is IToggleAction toggleAction && toggleAction.IsOn != value) |
| 88 | + Execute(null); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public bool IsExecutable |
| 93 | + => Action.IsExecutable; |
| 94 | + |
| 95 | + public ActionCommand(CommandManager manager, CommandCodes code, IAction action) |
| 96 | + { |
| 97 | + Code = code; |
| 98 | + Action = action; |
| 99 | + Icon = action.Glyph.ToIcon(); |
| 100 | + FontIcon = action.Glyph.ToFontIcon(); |
| 101 | + OpacityStyle = action.Glyph.ToOpacityStyle(); |
| 102 | + hotKeys = CommandManager.GetDefaultKeyBindings(action); |
| 103 | + DefaultHotKeys = CommandManager.GetDefaultKeyBindings(action); |
| 104 | + |
| 105 | + if (action is INotifyPropertyChanging notifyPropertyChanging) |
| 106 | + notifyPropertyChanging.PropertyChanging += Action_PropertyChanging; |
| 107 | + if (action is INotifyPropertyChanged notifyPropertyChanged) |
| 108 | + notifyPropertyChanged.PropertyChanged += Action_PropertyChanged; |
| 109 | + } |
| 110 | + |
| 111 | + public bool CanExecute(object? parameter) |
| 112 | + { |
| 113 | + return Action.IsExecutable; |
| 114 | + } |
| 115 | + |
| 116 | + public async void Execute(object? parameter) |
| 117 | + { |
| 118 | + await ExecuteAsync(); |
| 119 | + } |
| 120 | + |
| 121 | + public Task ExecuteAsync() |
| 122 | + { |
| 123 | + if (IsExecutable) |
| 124 | + { |
| 125 | + Analytics.TrackEvent($"Triggered {Code} action"); |
| 126 | + return Action.ExecuteAsync(); |
| 127 | + } |
| 128 | + |
| 129 | + return Task.CompletedTask; |
| 130 | + } |
| 131 | + |
| 132 | + public async void ExecuteTapped(object sender, TappedRoutedEventArgs e) |
| 133 | + { |
| 134 | + await ExecuteAsync(); |
| 135 | + } |
| 136 | + |
| 137 | + internal void OverwriteKeyBindings(HotKeyCollection hotKeys) |
| 138 | + { |
| 139 | + HotKeys = hotKeys; |
| 140 | + } |
| 141 | + |
| 142 | + internal void RestoreKeyBindings() |
| 143 | + { |
| 144 | + hotKeys = DefaultHotKeys; |
| 145 | + IsCustomHotKeys = false; |
| 146 | + } |
| 147 | + |
| 148 | + private void Action_PropertyChanging(object? sender, PropertyChangingEventArgs e) |
| 149 | + { |
| 150 | + switch (e.PropertyName) |
| 151 | + { |
| 152 | + case nameof(IAction.Label): |
| 153 | + OnPropertyChanging(nameof(Label)); |
| 154 | + OnPropertyChanging(nameof(LabelWithHotKey)); |
| 155 | + OnPropertyChanging(nameof(AutomationName)); |
| 156 | + break; |
| 157 | + case nameof(IToggleAction.IsOn) when IsToggle: |
| 158 | + OnPropertyChanging(nameof(IsOn)); |
| 159 | + break; |
| 160 | + case nameof(IAction.IsExecutable): |
| 161 | + OnPropertyChanging(nameof(IsExecutable)); |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private void Action_PropertyChanged(object? sender, PropertyChangedEventArgs e) |
| 167 | + { |
| 168 | + switch (e.PropertyName) |
| 169 | + { |
| 170 | + case nameof(IAction.Label): |
| 171 | + OnPropertyChanged(nameof(Label)); |
| 172 | + OnPropertyChanged(nameof(LabelWithHotKey)); |
| 173 | + OnPropertyChanged(nameof(AutomationName)); |
| 174 | + break; |
| 175 | + case nameof(IToggleAction.IsOn) when IsToggle: |
| 176 | + OnPropertyChanged(nameof(IsOn)); |
| 177 | + break; |
| 178 | + case nameof(IAction.IsExecutable): |
| 179 | + OnPropertyChanged(nameof(IsExecutable)); |
| 180 | + CanExecuteChanged?.Invoke(this, EventArgs.Empty); |
| 181 | + break; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments