/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Data/Commands/ModifiableCommand.cs
186 строк
5 KB
yair100
Code Quality: Add Category, ExtendedLabel, AutomationId and AccessKey to actions (#18308)
27 мар 2026, 00:40
Не верифицирован
27 мар 2026, 00:40
6ef1f17
Код
Авторство
О чём код?
// Copyright (c) Files Community // Licensed under the MIT License. using Files.App.Actions; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; using System.Collections.Immutable; namespace Files.App.Data.Commands { [DebuggerDisplay("Command {Code} (Modifiable)")] internal sealed partial class ModifiableCommand : ObservableObject, IRichCommand { public event EventHandler? CanExecuteChanged; private IRichCommand BaseCommand; private ImmutableDictionary<KeyModifiers, IRichCommand> ModifiedCommands; /// <inheritdoc/> public CommandCodes Code => BaseCommand.Code; /// <inheritdoc/> public string Label => BaseCommand.Label; /// <inheritdoc/> public string ExtendedLabel => BaseCommand.ExtendedLabel; /// <inheritdoc/> public string LabelWithHotKey => BaseCommand.LabelWithHotKey; /// <inheritdoc/> public string AutomationName => BaseCommand.AutomationName; /// <inheritdoc/> public string Description => BaseCommand.Description; /// <inheritdoc/> public RichGlyph Glyph => BaseCommand.Glyph; /// <inheritdoc/> public object? Icon => BaseCommand.Icon; /// <inheritdoc/> public FontIcon? FontIcon => BaseCommand.FontIcon; /// <inheritdoc/> public Style? ThemedIconStyle => BaseCommand.ThemedIconStyle; /// <inheritdoc/> public bool IsCustomHotKeys => BaseCommand.IsCustomHotKeys; /// <inheritdoc/> public string? HotKeyText => BaseCommand.HotKeyText; /// <inheritdoc/> public string AccessKey => BaseCommand.AccessKey; /// <inheritdoc/> public HotKeyCollection HotKeys { get => BaseCommand.HotKeys; set => BaseCommand.HotKeys = value; } /// <inheritdoc/> public HotKeyCollection DefaultHotKeys { get; } /// <inheritdoc/> public bool IsToggle => BaseCommand.IsToggle; /// <inheritdoc/> public bool IsOn { get => BaseCommand.IsOn; set => BaseCommand.IsOn = value; } /// <inheritdoc/> public bool IsExecutable => BaseCommand.IsExecutable; /// <inheritdoc/> public bool IsAccessibleGlobally => BaseCommand.IsAccessibleGlobally; /// <inheritdoc/> public string AutomationId => BaseCommand.AutomationId; public ModifiableCommand(IRichCommand baseCommand, Dictionary<KeyModifiers, IRichCommand> modifiedCommands) { BaseCommand = baseCommand; ModifiedCommands = modifiedCommands.ToImmutableDictionary(); DefaultHotKeys = new(BaseCommand.HotKeys); if (baseCommand is ActionCommand actionCommand) { if (actionCommand.Action is INotifyPropertyChanging notifyPropertyChanging) notifyPropertyChanging.PropertyChanging += Action_PropertyChanging; if (actionCommand.Action is INotifyPropertyChanged notifyPropertyChanged) notifyPropertyChanged.PropertyChanged += Action_PropertyChanged; } } /// <inheritdoc/> public bool CanExecute(object? parameter) { return BaseCommand.CanExecute(parameter); } /// <inheritdoc/> public async void Execute(object? parameter) { await ExecuteAsync(parameter); } /// <inheritdoc/> public Task ExecuteAsync(object? parameter = null) { if (ModifiedCommands.TryGetValue(HotKeyHelpers.GetCurrentKeyModifiers(), out var modifiedCommand) && modifiedCommand.IsExecutable) return modifiedCommand.ExecuteAsync(parameter); else return BaseCommand.ExecuteAsync(parameter); } /// <inheritdoc/> public async void ExecuteTapped(object sender, TappedRoutedEventArgs e) { await ExecuteAsync(); } private void Action_PropertyChanging(object? sender, PropertyChangingEventArgs e) { switch (e.PropertyName) { case nameof(IAction.Label): OnPropertyChanging(nameof(Label)); OnPropertyChanging(nameof(LabelWithHotKey)); OnPropertyChanging(nameof(AutomationName)); OnPropertyChanging(nameof(ExtendedLabel)); break; case nameof(IAction.ExtendedLabel): OnPropertyChanging(nameof(ExtendedLabel)); break; case nameof(IAction.AccessKey): OnPropertyChanging(nameof(AccessKey)); 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)); OnPropertyChanged(nameof(LabelWithHotKey)); OnPropertyChanged(nameof(AutomationName)); OnPropertyChanged(nameof(ExtendedLabel)); break; case nameof(IAction.ExtendedLabel): OnPropertyChanged(nameof(ExtendedLabel)); break; case nameof(IAction.AccessKey): OnPropertyChanged(nameof(AccessKey)); break; case nameof(IToggleAction.IsOn) when IsToggle: OnPropertyChanged(nameof(IsOn)); break; case nameof(IAction.IsExecutable): OnPropertyChanged(nameof(IsExecutable)); CanExecuteChanged?.Invoke(this, EventArgs.Empty); break; } } } }