Shortcuts for command palette entries
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
public interface ICommandPaletteEntryViewModel
|
||||
{
|
||||
string Identifier { get; set; }
|
||||
string Title { get; set; }
|
||||
string Identifier { get; }
|
||||
string Title { get; }
|
||||
string Shortcuts { get; }
|
||||
}
|
||||
@@ -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" />
|
||||
|
||||
@@ -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;
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -25,8 +25,9 @@
|
||||
SelectedItem="{Binding SelectedItem}">
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate x:DataType="vm:ICommandPaletteEntryViewModel">
|
||||
<Grid Margin="5">
|
||||
<Grid Margin="5" ColumnDefinitions="*, Auto">
|
||||
<TextBlock Text="{Binding Title}" />
|
||||
<TextBlock Grid.Column="1" Text="{Binding Shortcuts}" Margin="0,0,10,0" />
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
|
||||
Reference in New Issue
Block a user