From a537277546ba0a46bab65e297284f22826a1bd82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Sat, 29 Jul 2023 21:15:16 +0200 Subject: [PATCH] Shortcuts for command palette entries --- .../ICommandPaletteEntryViewModel.cs | 5 +- .../FileTime.App.CommandPalette.csproj | 1 + .../CommandPaletteEntryViewModel.cs | 13 +---- .../ViewModels/CommandPaletteViewModel.cs | 49 ++++++++++++++++++- .../Views/CommandPalette.axaml | 3 +- 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/AppCommon/FileTime.App.CommandPalette.Abstractions/ViewModels/ICommandPaletteEntryViewModel.cs b/src/AppCommon/FileTime.App.CommandPalette.Abstractions/ViewModels/ICommandPaletteEntryViewModel.cs index eebf2fd..3db2a13 100644 --- a/src/AppCommon/FileTime.App.CommandPalette.Abstractions/ViewModels/ICommandPaletteEntryViewModel.cs +++ b/src/AppCommon/FileTime.App.CommandPalette.Abstractions/ViewModels/ICommandPaletteEntryViewModel.cs @@ -2,6 +2,7 @@ public interface ICommandPaletteEntryViewModel { - string Identifier { get; set; } - string Title { get; set; } + string Identifier { get; } + string Title { get; } + string Shortcuts { get; } } \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.CommandPalette/FileTime.App.CommandPalette.csproj b/src/AppCommon/FileTime.App.CommandPalette/FileTime.App.CommandPalette.csproj index f439ade..fe4b568 100644 --- a/src/AppCommon/FileTime.App.CommandPalette/FileTime.App.CommandPalette.csproj +++ b/src/AppCommon/FileTime.App.CommandPalette/FileTime.App.CommandPalette.csproj @@ -7,6 +7,7 @@ + diff --git a/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteEntryViewModel.cs b/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteEntryViewModel.cs index 14c0e1b..2479549 100644 --- a/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteEntryViewModel.cs +++ b/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteEntryViewModel.cs @@ -2,15 +2,4 @@ namespace FileTime.App.CommandPalette.ViewModels; -[ViewModel] -public partial class CommandPaletteEntryViewModel : ICommandPaletteEntryViewModel -{ - [Property] private string _identifier; - [Property] private string _title; - - public CommandPaletteEntryViewModel(string identifier, string title) - { - _identifier = identifier; - _title = title; - } -} \ No newline at end of file +public record CommandPaletteEntryViewModel(string Identifier, string Title, string Shortcuts) : ICommandPaletteEntryViewModel; \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteViewModel.cs b/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteViewModel.cs index 66ff7ef..bd04bdb 100644 --- a/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteViewModel.cs +++ b/src/AppCommon/FileTime.App.CommandPalette/ViewModels/CommandPaletteViewModel.cs @@ -1,8 +1,11 @@ -using Avalonia.Input; +using System.Text; +using Avalonia.Input; using FileTime.App.CommandPalette.Services; using FileTime.App.Core.Services; using FileTime.App.Core.ViewModels; using FileTime.App.FuzzyPanel; +using FileTime.GuiApp.Configuration; +using FileTime.GuiApp.Services; using Microsoft.Extensions.Logging; namespace FileTime.App.CommandPalette.ViewModels; @@ -12,6 +15,7 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel _logger; string IModalViewModel.Name => "CommandPalette"; @@ -19,11 +23,13 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel logger) { _commandPaletteService = commandPaletteService; _identifiableUserCommandService = identifiableUserCommandService; _userCommandHandlerService = userCommandHandlerService; + _keyboardConfigurationService = keyboardConfigurationService; _logger = logger; ShowWindow = _commandPaletteService.ShowWindow; UpdateFilteredMatchesInternal(); @@ -41,12 +47,51 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel - (ICommandPaletteEntryViewModel) new CommandPaletteEntryViewModel(c.Identifier, c.Title) + (ICommandPaletteEntryViewModel) new CommandPaletteEntryViewModel(c.Identifier, c.Title, GetKeyConfigsString(c.Identifier)) ) .ToList(); + private string GetKeyConfigsString(string commandIdentifier) + { + var keyConfigs = GetKeyConfigsForCommand(commandIdentifier); + if (keyConfigs.Count == 0) return string.Empty; + + return string.Join( + " ; ", + keyConfigs + .Select(ks => + string.Join( + ", ", + ks.Select(FormatKeyConfig) + ) + ) + ); + } + + private string FormatKeyConfig(KeyConfig keyConfig) + { + var stringBuilder = new StringBuilder(); + + if (keyConfig.Ctrl) stringBuilder.Append("Ctrl + "); + if (keyConfig.Shift) stringBuilder.Append("Shift + "); + if (keyConfig.Alt) stringBuilder.Append("Alt + "); + + stringBuilder.Append(keyConfig.Key.ToString()); + + return stringBuilder.ToString(); + } + + private List> GetKeyConfigsForCommand(string commandIdentifier) + => _keyboardConfigurationService + .AllShortcut + .Where(s => s.Command == commandIdentifier) + .Select(k => k.Keys) + .ToList(); + public override async Task HandleKeyDown(KeyEventArgs keyEventArgs) { + if (keyEventArgs.Handled) return false; + var handled = await base.HandleKeyDown(keyEventArgs); if (handled) { diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp/Views/CommandPalette.axaml b/src/GuiApp/Avalonia/FileTime.GuiApp/Views/CommandPalette.axaml index bd28874..37cad10 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp/Views/CommandPalette.axaml +++ b/src/GuiApp/Avalonia/FileTime.GuiApp/Views/CommandPalette.axaml @@ -25,8 +25,9 @@ SelectedItem="{Binding SelectedItem}"> - + +