Shortcuts for command palette entries

This commit is contained in:
2023-07-29 21:15:16 +02:00
parent 05301f09c8
commit a537277546
5 changed files with 54 additions and 17 deletions

View File

@@ -2,6 +2,7 @@
public interface ICommandPaletteEntryViewModel
{
string Identifier { get; set; }
string Title { get; set; }
string Identifier { get; }
string Title { get; }
string Shortcuts { get; }
}

View File

@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\GuiApp\Avalonia\FileTime.GuiApp.Abstractions\FileTime.GuiApp.Abstractions.csproj" />
<ProjectReference Include="..\FileTime.App.CommandPalette.Abstractions\FileTime.App.CommandPalette.Abstractions.csproj" />
<ProjectReference Include="..\FileTime.App.Core.Abstraction\FileTime.App.Core.Abstraction.csproj" />
<ProjectReference Include="..\FileTime.App.FuzzyPanel\FileTime.App.FuzzyPanel.csproj" />

View File

@@ -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;
}
}
public record CommandPaletteEntryViewModel(string Identifier, string Title, string Shortcuts) : ICommandPaletteEntryViewModel;

View File

@@ -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<ICommandPaletteEntryV
private readonly ICommandPaletteService _commandPaletteService;
private readonly IIdentifiableUserCommandService _identifiableUserCommandService;
private readonly IUserCommandHandlerService _userCommandHandlerService;
private readonly IKeyboardConfigurationService _keyboardConfigurationService;
private readonly ILogger<CommandPaletteViewModel> _logger;
string IModalViewModel.Name => "CommandPalette";
@@ -19,11 +23,13 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
ICommandPaletteService commandPaletteService,
IIdentifiableUserCommandService identifiableUserCommandService,
IUserCommandHandlerService userCommandHandlerService,
IKeyboardConfigurationService keyboardConfigurationService,
ILogger<CommandPaletteViewModel> logger)
{
_commandPaletteService = commandPaletteService;
_identifiableUserCommandService = identifiableUserCommandService;
_userCommandHandlerService = userCommandHandlerService;
_keyboardConfigurationService = keyboardConfigurationService;
_logger = logger;
ShowWindow = _commandPaletteService.ShowWindow;
UpdateFilteredMatchesInternal();
@@ -41,12 +47,51 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
|| c.Identifier.Contains(SearchText, StringComparison.OrdinalIgnoreCase)
)
.Select(c =>
(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<List<KeyConfig>> GetKeyConfigsForCommand(string commandIdentifier)
=> _keyboardConfigurationService
.AllShortcut
.Where(s => s.Command == commandIdentifier)
.Select(k => k.Keys)
.ToList();
public override async Task<bool> HandleKeyDown(KeyEventArgs keyEventArgs)
{
if (keyEventArgs.Handled) return false;
var handled = await base.HandleKeyDown(keyEventArgs);
if (handled)
{