/
githubmirror
/
Files
Обзор
Документация
Войти
/
githubmirror
/
Files
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Files.App/Data/Commands/ActionCommand.cs
243 строки
6 KB
yair100
Code Quality: Performance optimizations (#18801)
06 авг 2026, 17:33
Не верифицирован
06 авг 2026, 17:33
7dc84dd
Код
Авторство
О чём код?
// 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 Sentry; namespace Files.App.Data.Commands { [DebuggerDisplay("Command {Code}")] internal sealed partial class ActionCommand : ObservableObject, IRichCommand { private IActionsSettingsService ActionsSettingsService { get; } = Ioc.Default.GetRequiredService<IActionsSettingsService>(); public event EventHandler? CanExecuteChanged; public IAction Action { get; } /// <inheritdoc/> public CommandCodes Code { get; } /// <inheritdoc/> public string Label => Action.Label; /// <inheritdoc/> public string ExtendedLabel => Action.ExtendedLabel; /// <inheritdoc/> public string LabelWithHotKey => HotKeyText is null ? Label : $"{Label} ({HotKeyText})"; /// <inheritdoc/> public string AutomationName => Label; /// <inheritdoc/> public string Description => Action.Description; /// <inheritdoc/> public RichGlyph Glyph => Action.Glyph; private object? icon; /// <inheritdoc/> public object? Icon => icon ??= Action.Glyph.ToIcon(); private FontIcon? fontIcon; /// <inheritdoc/> public FontIcon? FontIcon => fontIcon ??= Action.Glyph.ToFontIcon(); private Style? themedIconStyle; /// <inheritdoc/> public Style? ThemedIconStyle => themedIconStyle ??= Action.Glyph.ToThemedIconStyle(); private bool isCustomHotKeys = false; /// <inheritdoc/> public bool IsCustomHotKeys { get => isCustomHotKeys; set => SetProperty(ref isCustomHotKeys, value); } /// <inheritdoc/> public string? HotKeyText { get { string text = HotKeys.LocalizedLabel; if (string.IsNullOrEmpty(text)) return null; return text; } } /// <inheritdoc/> public string AccessKey => Action.AccessKey; private HotKeyCollection hotKeys; /// <inheritdoc/> public HotKeyCollection HotKeys { get => hotKeys; set { if (SetProperty(ref hotKeys, value)) { OnPropertyChanged(nameof(HotKeyText)); OnPropertyChanged(nameof(LabelWithHotKey)); IsCustomHotKeys = true; } } } /// <inheritdoc/> public HotKeyCollection DefaultHotKeys { get; } /// <inheritdoc/> public bool IsToggle => Action is IToggleAction; /// <inheritdoc/> public bool IsOn { get => Action is IToggleAction toggleAction && toggleAction.IsOn; set { if (Action is IToggleAction toggleAction && toggleAction.IsOn != value) Execute(null); } } /// <inheritdoc/> public bool IsExecutable => Action.IsExecutable; /// <inheritdoc/> public bool IsAccessibleGlobally => Action.IsAccessibleGlobally; /// <inheritdoc/> public string AutomationId => Action.AutomationId; public ActionCommand(CommandCodes code, IAction action) { Code = code; Action = action; hotKeys = GetDefaultKeyBindings(action); DefaultHotKeys = GetDefaultKeyBindings(action); if (action is INotifyPropertyChanging notifyPropertyChanging) notifyPropertyChanging.PropertyChanging += Action_PropertyChanging; if (action is INotifyPropertyChanged notifyPropertyChanged) notifyPropertyChanged.PropertyChanged += Action_PropertyChanged; } private static HotKeyCollection GetDefaultKeyBindings(IAction action) { return new(action.HotKey, action.SecondHotKey, action.ThirdHotKey, action.MediaHotKey); } /// <inheritdoc/> public bool CanExecute(object? parameter) { return Action.IsExecutable; } /// <inheritdoc/> public async void Execute(object? parameter) { await ExecuteAsync(parameter); } /// <inheritdoc/> public Task ExecuteAsync(object? parameter = null) { if (IsExecutable) { SentrySdk.Metrics.EmitCounter("actions", 1, [new KeyValuePair<string, object>("command", Code.ToString())]); return Action.ExecuteAsync(parameter); } return Task.CompletedTask; } /// <inheritdoc/> public async void ExecuteTapped(object sender, TappedRoutedEventArgs e) { await ExecuteAsync(); } internal void OverwriteKeyBindings(HotKeyCollection hotKeys) { HotKeys = hotKeys; } internal void RestoreKeyBindings() { hotKeys = DefaultHotKeys; IsCustomHotKeys = false; } 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; } } } }