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 public interface ICommandPaletteEntryViewModel
{ {
string Identifier { get; set; } string Identifier { get; }
string Title { get; set; } string Title { get; }
string Shortcuts { get; }
} }

View File

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

View File

@@ -2,15 +2,4 @@
namespace FileTime.App.CommandPalette.ViewModels; namespace FileTime.App.CommandPalette.ViewModels;
[ViewModel] public record CommandPaletteEntryViewModel(string Identifier, string Title, string Shortcuts) : ICommandPaletteEntryViewModel;
public partial class CommandPaletteEntryViewModel : ICommandPaletteEntryViewModel
{
[Property] private string _identifier;
[Property] private string _title;
public CommandPaletteEntryViewModel(string identifier, string title)
{
_identifier = identifier;
_title = title;
}
}

View File

@@ -1,8 +1,11 @@
using Avalonia.Input; using System.Text;
using Avalonia.Input;
using FileTime.App.CommandPalette.Services; using FileTime.App.CommandPalette.Services;
using FileTime.App.Core.Services; using FileTime.App.Core.Services;
using FileTime.App.Core.ViewModels; using FileTime.App.Core.ViewModels;
using FileTime.App.FuzzyPanel; using FileTime.App.FuzzyPanel;
using FileTime.GuiApp.Configuration;
using FileTime.GuiApp.Services;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace FileTime.App.CommandPalette.ViewModels; namespace FileTime.App.CommandPalette.ViewModels;
@@ -12,6 +15,7 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
private readonly ICommandPaletteService _commandPaletteService; private readonly ICommandPaletteService _commandPaletteService;
private readonly IIdentifiableUserCommandService _identifiableUserCommandService; private readonly IIdentifiableUserCommandService _identifiableUserCommandService;
private readonly IUserCommandHandlerService _userCommandHandlerService; private readonly IUserCommandHandlerService _userCommandHandlerService;
private readonly IKeyboardConfigurationService _keyboardConfigurationService;
private readonly ILogger<CommandPaletteViewModel> _logger; private readonly ILogger<CommandPaletteViewModel> _logger;
string IModalViewModel.Name => "CommandPalette"; string IModalViewModel.Name => "CommandPalette";
@@ -19,11 +23,13 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
ICommandPaletteService commandPaletteService, ICommandPaletteService commandPaletteService,
IIdentifiableUserCommandService identifiableUserCommandService, IIdentifiableUserCommandService identifiableUserCommandService,
IUserCommandHandlerService userCommandHandlerService, IUserCommandHandlerService userCommandHandlerService,
IKeyboardConfigurationService keyboardConfigurationService,
ILogger<CommandPaletteViewModel> logger) ILogger<CommandPaletteViewModel> logger)
{ {
_commandPaletteService = commandPaletteService; _commandPaletteService = commandPaletteService;
_identifiableUserCommandService = identifiableUserCommandService; _identifiableUserCommandService = identifiableUserCommandService;
_userCommandHandlerService = userCommandHandlerService; _userCommandHandlerService = userCommandHandlerService;
_keyboardConfigurationService = keyboardConfigurationService;
_logger = logger; _logger = logger;
ShowWindow = _commandPaletteService.ShowWindow; ShowWindow = _commandPaletteService.ShowWindow;
UpdateFilteredMatchesInternal(); UpdateFilteredMatchesInternal();
@@ -41,12 +47,51 @@ public class CommandPaletteViewModel : FuzzyPanelViewModel<ICommandPaletteEntryV
|| c.Identifier.Contains(SearchText, StringComparison.OrdinalIgnoreCase) || c.Identifier.Contains(SearchText, StringComparison.OrdinalIgnoreCase)
) )
.Select(c => .Select(c =>
(ICommandPaletteEntryViewModel) new CommandPaletteEntryViewModel(c.Identifier, c.Title) (ICommandPaletteEntryViewModel) new CommandPaletteEntryViewModel(c.Identifier, c.Title, GetKeyConfigsString(c.Identifier))
) )
.ToList(); .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) public override async Task<bool> HandleKeyDown(KeyEventArgs keyEventArgs)
{ {
if (keyEventArgs.Handled) return false;
var handled = await base.HandleKeyDown(keyEventArgs); var handled = await base.HandleKeyDown(keyEventArgs);
if (handled) if (handled)
{ {

View File

@@ -25,8 +25,9 @@
SelectedItem="{Binding SelectedItem}"> SelectedItem="{Binding SelectedItem}">
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate x:DataType="vm:ICommandPaletteEntryViewModel"> <DataTemplate x:DataType="vm:ICommandPaletteEntryViewModel">
<Grid Margin="5"> <Grid Margin="5" ColumnDefinitions="*, Auto">
<TextBlock Text="{Binding Title}" /> <TextBlock Text="{Binding Title}" />
<TextBlock Grid.Column="1" Text="{Binding Shortcuts}" Margin="0,0,10,0" />
</Grid> </Grid>
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>