Command refactor

This commit is contained in:
2022-05-15 21:42:01 +02:00
parent 9a99aad030
commit f99f90783f
42 changed files with 544 additions and 217 deletions

View File

@@ -1,7 +1,7 @@
using System.Reactive.Linq;
using Avalonia.Input;
using FileTime.App.Core.Command;
using FileTime.App.Core.Services;
using FileTime.App.Core.UserCommand;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
using FileTime.GuiApp.Configuration;
@@ -20,18 +20,21 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
private ITabViewModel? _selectedTab;
private IContainer? _currentLocation;
private readonly ILogger<DefaultModeKeyInputHandler> _logger;
private readonly ICommandHandlerService _commandHandlerService;
private readonly IUserCommandHandlerService _userCommandHandlerService;
private readonly IIdentifiableUserCommandService _identifiableUserCommandService;
public DefaultModeKeyInputHandler(
IGuiAppState appState,
IKeyboardConfigurationService keyboardConfigurationService,
ILogger<DefaultModeKeyInputHandler> logger,
ICommandHandlerService commandHandlerService)
IUserCommandHandlerService userCommandHandlerService,
IIdentifiableUserCommandService identifiableUserCommandService)
{
_appState = appState;
_keyboardConfigurationService = keyboardConfigurationService;
_logger = logger;
_commandHandlerService = commandHandlerService;
_userCommandHandlerService = userCommandHandlerService;
_identifiableUserCommandService = identifiableUserCommandService;
_appState.SelectedTab.Subscribe(t => _selectedTab = t);
_appState.SelectedTab.Select(t => t == null ? Observable.Return<IContainer?>(null) : t.CurrentLocation!).Switch().Subscribe(l => _currentLocation = l);
@@ -51,8 +54,8 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
var keyWithModifiers = new KeyConfig(key, shift: specialKeysStatus.IsShiftPressed, alt: specialKeysStatus.IsAltPressed, ctrl: specialKeysStatus.IsCtrlPressed);
_appState.PreviousKeys.Add(keyWithModifiers);
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(_appState.PreviousKeys));
selectedCommandBinding ??= _keyboardConfigurationService.CommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(_appState.PreviousKeys));
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.Values.FirstOrDefault(c => c.Keys.AreKeysEqual(_appState.PreviousKeys));
selectedCommandBinding ??= _keyboardConfigurationService.CommandBindings.Values.FirstOrDefault(c => c.Keys.AreKeysEqual(_appState.PreviousKeys));
if (key == Key.Escape)
{
@@ -104,7 +107,7 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
setHandled(true);
_appState.PreviousKeys.Clear();
_appState.PossibleCommands = new();
await CallCommandAsync(selectedCommandBinding.Command);
await CallCommandAsync(_identifiableUserCommandService.GetCommand(selectedCommandBinding.Command));
}
else if (_keysToSkip.Any(k => k.AreKeysEqual(_appState.PreviousKeys)))
{
@@ -122,7 +125,7 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
else
{
setHandled(true);
var possibleCommands = _keyboardConfigurationService.AllShortcut.Where(c => c.Keys[0].AreEquals(keyWithModifiers)).ToList();
var possibleCommands = _keyboardConfigurationService.AllShortcut.Values.Where(c => c.Keys[0].AreEquals(keyWithModifiers)).ToList();
if (possibleCommands.Count == 0)
{
@@ -136,15 +139,15 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
}
}
private async Task CallCommandAsync(Command command)
private async Task CallCommandAsync(IUserCommand command)
{
try
{
await _commandHandlerService.HandleCommandAsync(command);
await _userCommandHandlerService.HandleCommandAsync(command);
}
catch (Exception e)
{
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command, e);
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command.GetType().Name, e);
}
}
}

View File

@@ -1,6 +1,5 @@
using Avalonia.Input;
using FileTime.App.Core.Models.Enums;
using FileTime.GuiApp.Configuration;
using FileTime.GuiApp.Models;
using FileTime.GuiApp.ViewModels;

View File

@@ -1,4 +1,4 @@
using FileTime.App.Core.Command;
using System.Collections.ObjectModel;
using FileTime.GuiApp.Configuration;
using Microsoft.Extensions.Options;
@@ -6,14 +6,14 @@ namespace FileTime.GuiApp.Services;
public class KeyboardConfigurationService : IKeyboardConfigurationService
{
public IReadOnlyList<CommandBindingConfiguration> CommandBindings { get; }
public IReadOnlyList<CommandBindingConfiguration> UniversalCommandBindings { get; }
public IReadOnlyList<CommandBindingConfiguration> AllShortcut { get; }
public IReadOnlyDictionary<string, CommandBindingConfiguration> CommandBindings { get; }
public IReadOnlyDictionary<string, CommandBindingConfiguration> UniversalCommandBindings { get; }
public IReadOnlyDictionary<string, CommandBindingConfiguration> AllShortcut { get; }
public KeyboardConfigurationService(IOptions<KeyBindingConfiguration> keyBindingConfiguration)
{
var commandBindings = new List<CommandBindingConfiguration>();
var universalCommandBindings = new List<CommandBindingConfiguration>();
var commandBindings = new Dictionary<string, CommandBindingConfiguration>();
var universalCommandBindings = new Dictionary<string, CommandBindingConfiguration>();
IEnumerable<CommandBindingConfiguration> keyBindings = keyBindingConfiguration.Value.KeyBindings;
if (keyBindingConfiguration.Value.UseDefaultBindings)
@@ -23,7 +23,7 @@ public class KeyboardConfigurationService : IKeyboardConfigurationService
foreach (var keyBinding in keyBindings)
{
if (keyBinding.Command == Command.None)
if (string.IsNullOrWhiteSpace(keyBinding.Command))
{
throw new FormatException($"No command is set in keybinding for keys '{keyBinding.KeysDisplayText}'");
}
@@ -34,21 +34,22 @@ public class KeyboardConfigurationService : IKeyboardConfigurationService
if (IsUniversal(keyBinding))
{
universalCommandBindings.Add(keyBinding);
universalCommandBindings.Add(keyBinding.Command, keyBinding);
}
else
{
commandBindings.Add(keyBinding);
commandBindings.Add(keyBinding.Command, keyBinding);
}
}
CommandBindings = commandBindings.AsReadOnly();
UniversalCommandBindings = universalCommandBindings.AsReadOnly();
AllShortcut = new List<CommandBindingConfiguration>(CommandBindings.Concat(UniversalCommandBindings)).AsReadOnly();
CommandBindings = new ReadOnlyDictionary<string, CommandBindingConfiguration>(commandBindings);
UniversalCommandBindings = new ReadOnlyDictionary<string, CommandBindingConfiguration>(universalCommandBindings);
AllShortcut = new ReadOnlyDictionary<string, CommandBindingConfiguration>(new Dictionary<string, CommandBindingConfiguration>(CommandBindings.Concat(UniversalCommandBindings)));
}
private static bool IsUniversal(CommandBindingConfiguration keyMapping)
{
return keyMapping.Command is Command.GoUp or Command.Open or Command.OpenOrRun or Command.MoveCursorUp or Command.MoveCursorDown or Command.MoveCursorUpPage or Command.MoveCursorDownPage;
return false;
//return keyMapping.Command is ConfigCommand.GoUp or ConfigCommand.Open or ConfigCommand.OpenOrRun or ConfigCommand.MoveCursorUp or ConfigCommand.MoveCursorDown or ConfigCommand.MoveCursorUpPage or ConfigCommand.MoveCursorDownPage;
}
}

View File

@@ -1,8 +1,7 @@
using Avalonia.Input;
using FileTime.App.Core.Command;
using FileTime.App.Core.Models;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.Services;
using FileTime.App.Core.UserCommand;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
using FileTime.Core.Services;
@@ -15,12 +14,14 @@ namespace FileTime.GuiApp.Services;
public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
{
private const string RAPIDTRAVELFILTERNAME = "rapid_travel_filter";
private const string RapidTravelFilterName = "rapid_travel_filter";
private readonly IAppState _appState;
private readonly IModalService _modalService;
private readonly IKeyboardConfigurationService _keyboardConfigurationService;
private readonly ICommandHandlerService _commandHandlerService;
private readonly IUserCommandHandlerService _userCommandHandlerService;
private readonly ILogger<RapidTravelModeKeyInputHandler> _logger;
private readonly IIdentifiableUserCommandService _identifiableUserCommandService;
private readonly BindedCollection<IModalViewModelBase> _openModals;
private ITabViewModel? _selectedTab;
@@ -28,15 +29,17 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
IAppState appState,
IModalService modalService,
IKeyboardConfigurationService keyboardConfigurationService,
ICommandHandlerService commandHandlerService,
ILogger<RapidTravelModeKeyInputHandler> logger)
IUserCommandHandlerService userCommandHandlerService,
ILogger<RapidTravelModeKeyInputHandler> logger,
IIdentifiableUserCommandService identifiableUserCommandService)
{
_appState = appState;
_modalService = modalService;
_keyboardConfigurationService = keyboardConfigurationService;
_commandHandlerService = commandHandlerService;
_userCommandHandlerService = userCommandHandlerService;
_logger = logger;
_identifiableUserCommandService = identifiableUserCommandService;
_appState.SelectedTab.Subscribe(t => _selectedTab = t);
_openModals = new BindedCollection<IModalViewModelBase>(modalService.OpenModals);
@@ -56,7 +59,7 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
}
else
{
await CallCommandAsync(Command.ExitRapidTravel);
await CallCommandAsync(ExitRapidTravelCommand.Instance);
}
}
else if (key == Key.Back)
@@ -77,11 +80,11 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
else
{
var currentKeyAsList = new List<KeyConfig>() {new KeyConfig(key)};
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(currentKeyAsList));
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.Values.FirstOrDefault(c => c.Keys.AreKeysEqual(currentKeyAsList));
if (selectedCommandBinding != null)
{
setHandled(true);
await CallCommandAsync(selectedCommandBinding.Command);
await CallCommandAsync(_identifiableUserCommandService.GetCommand(selectedCommandBinding.Command));
}
}
@@ -89,8 +92,8 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
{
if (_selectedTab?.Tab is not ITab tab) return;
tab.RemoveItemFilter(RAPIDTRAVELFILTERNAME);
tab.AddItemFilter(new ItemFilter(RAPIDTRAVELFILTERNAME, i => i.Name.ToLower().Contains(_appState.RapidTravelText)));
tab.RemoveItemFilter(RapidTravelFilterName);
tab.AddItemFilter(new ItemFilter(RapidTravelFilterName, i => i.Name.ToLower().Contains(_appState.RapidTravelText)));
/*var currentLocation = await _appState.SelectedTab.CurrentLocation.Container.WithoutVirtualContainer(MainPageViewModel.RAPIDTRAVEL);
var newLocation = new VirtualContainer(
currentLocation,
@@ -122,15 +125,15 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
}
}
private async Task CallCommandAsync(Command command)
private async Task CallCommandAsync(IUserCommand command)
{
try
{
await _commandHandlerService.HandleCommandAsync(command);
await _userCommandHandlerService.HandleCommandAsync(command);
}
catch (Exception e)
{
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command, e);
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command.GetType().Name, e);
}
}
}