Move common things to AppCore from GuiApp 2
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
using DeclarativeProperty;
|
||||
using FileTime.App.Core.Configuration;
|
||||
using FileTime.App.Core.Extensions;
|
||||
using FileTime.App.Core.Models;
|
||||
using FileTime.App.Core.UserCommand;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.Core.Extensions;
|
||||
using FileTime.Core.Models;
|
||||
using FileTime.Core.Models.Extensions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FileTime.App.Core.Services;
|
||||
|
||||
public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
|
||||
{
|
||||
private readonly IAppState _appState;
|
||||
private readonly IModalService _modalService;
|
||||
private readonly IKeyboardConfigurationService _keyboardConfigurationService;
|
||||
private readonly List<KeyConfig[]> _keysToSkip = new();
|
||||
private readonly IDeclarativeProperty<IContainer?> _currentLocation;
|
||||
private readonly ILogger<DefaultModeKeyInputHandler> _logger;
|
||||
private readonly IUserCommandHandlerService _userCommandHandlerService;
|
||||
private readonly IIdentifiableUserCommandService _identifiableUserCommandService;
|
||||
private readonly BindedCollection<IModalViewModel> _openModals;
|
||||
|
||||
public DefaultModeKeyInputHandler(
|
||||
IAppState appState,
|
||||
IModalService modalService,
|
||||
IKeyboardConfigurationService keyboardConfigurationService,
|
||||
ILogger<DefaultModeKeyInputHandler> logger,
|
||||
IUserCommandHandlerService userCommandHandlerService,
|
||||
IIdentifiableUserCommandService identifiableUserCommandService)
|
||||
{
|
||||
_appState = appState;
|
||||
_identifiableUserCommandService = identifiableUserCommandService;
|
||||
_keyboardConfigurationService = keyboardConfigurationService;
|
||||
_logger = logger;
|
||||
_modalService = modalService;
|
||||
_userCommandHandlerService = userCommandHandlerService;
|
||||
|
||||
_currentLocation = _appState.SelectedTab
|
||||
.Map(t => t?.CurrentLocation)
|
||||
.Switch();
|
||||
|
||||
_openModals = modalService.OpenModals.ToBindedCollection();
|
||||
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.Up)});
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.Down)});
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.Tab)});
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.PageDown)});
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.PageUp)});
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.F4, alt: true)});
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.LWin)});
|
||||
_keysToSkip.Add(new[] {new KeyConfig(Keys.RWin)});
|
||||
}
|
||||
|
||||
public async Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action<bool> setHandled)
|
||||
{
|
||||
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));
|
||||
|
||||
if (key == Keys.Escape)
|
||||
{
|
||||
var doGeneralReset = _appState.PreviousKeys.Count > 1;
|
||||
|
||||
if ((_openModals.Collection?.Count ?? 0) > 0)
|
||||
{
|
||||
_modalService.CloseModal(_openModals.Collection!.Last());
|
||||
}
|
||||
else if (_currentLocation.Value?.GetExtension<EscHandlerContainerExtension>() is { } escHandler)
|
||||
{
|
||||
var escapeResult = await escHandler.HandleEsc();
|
||||
if (escapeResult.NavigateTo != null)
|
||||
{
|
||||
setHandled(true);
|
||||
_appState.PreviousKeys.Clear();
|
||||
if (_appState.SelectedTab.Value?.Tab is { } selectedTab)
|
||||
{
|
||||
await selectedTab.SetCurrentLocation(escapeResult.NavigateTo);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (escapeResult.Handled)
|
||||
{
|
||||
_appState.PreviousKeys.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
doGeneralReset = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (doGeneralReset)
|
||||
{
|
||||
setHandled(true);
|
||||
_appState.PreviousKeys.Clear();
|
||||
_appState.PossibleCommands = new();
|
||||
}
|
||||
}
|
||||
/*else if (key == Key.Enter
|
||||
&& _appState.MessageBoxText != null)
|
||||
{
|
||||
_appState.PreviousKeys.Clear();
|
||||
//_dialogService.ProcessMessageBox();
|
||||
setHandled(true);
|
||||
}*/
|
||||
else if (selectedCommandBinding != null)
|
||||
{
|
||||
setHandled(true);
|
||||
_appState.PreviousKeys.Clear();
|
||||
_appState.PossibleCommands = new();
|
||||
var command = _identifiableUserCommandService.GetCommand(selectedCommandBinding.Command);
|
||||
if (command is not null)
|
||||
{
|
||||
await CallCommandAsync(command);
|
||||
}
|
||||
}
|
||||
else if (_keysToSkip.Any(k => k.AreKeysEqual(_appState.PreviousKeys)))
|
||||
{
|
||||
_appState.PreviousKeys.Clear();
|
||||
_appState.PossibleCommands = new();
|
||||
return;
|
||||
}
|
||||
else if (_appState.PreviousKeys.Count == 2)
|
||||
{
|
||||
setHandled(true);
|
||||
_appState.NoCommandFound = true;
|
||||
_appState.PreviousKeys.Clear();
|
||||
_appState.PossibleCommands = new();
|
||||
}
|
||||
else
|
||||
{
|
||||
setHandled(true);
|
||||
var possibleCommands = _keyboardConfigurationService.AllShortcut.Where(c => c.Keys[0].AreEquals(keyWithModifiers)).ToList();
|
||||
|
||||
if (possibleCommands.Count == 0)
|
||||
{
|
||||
_appState.NoCommandFound = true;
|
||||
_appState.PreviousKeys.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
_appState.PossibleCommands = possibleCommands;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CallCommandAsync(IUserCommand command)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _userCommandHandlerService.HandleCommandAsync(command);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command.GetType().Name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
using FileTime.App.Core.Configuration;
|
||||
using FileTime.App.Core.Extensions;
|
||||
using FileTime.App.Core.Models;
|
||||
using FileTime.App.Core.UserCommand;
|
||||
using FileTime.App.Core.ViewModels;
|
||||
using FileTime.Core.Extensions;
|
||||
using FileTime.Core.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace FileTime.App.Core.Services;
|
||||
|
||||
public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
|
||||
{
|
||||
private const string RapidTravelFilterName = "rapid_travel_filter";
|
||||
|
||||
private readonly IAppState _appState;
|
||||
private readonly IModalService _modalService;
|
||||
private readonly IKeyboardConfigurationService _keyboardConfigurationService;
|
||||
private readonly IUserCommandHandlerService _userCommandHandlerService;
|
||||
private readonly ILogger<RapidTravelModeKeyInputHandler> _logger;
|
||||
private readonly IIdentifiableUserCommandService _identifiableUserCommandService;
|
||||
private readonly BindedCollection<IModalViewModel> _openModals;
|
||||
private ITabViewModel? _selectedTab;
|
||||
|
||||
public RapidTravelModeKeyInputHandler(
|
||||
IAppState appState,
|
||||
IModalService modalService,
|
||||
IKeyboardConfigurationService keyboardConfigurationService,
|
||||
IUserCommandHandlerService userCommandHandlerService,
|
||||
ILogger<RapidTravelModeKeyInputHandler> logger,
|
||||
IIdentifiableUserCommandService identifiableUserCommandService)
|
||||
{
|
||||
_appState = appState;
|
||||
_modalService = modalService;
|
||||
_keyboardConfigurationService = keyboardConfigurationService;
|
||||
_userCommandHandlerService = userCommandHandlerService;
|
||||
_logger = logger;
|
||||
_identifiableUserCommandService = identifiableUserCommandService;
|
||||
|
||||
_appState.SelectedTab.Subscribe(t => _selectedTab = t);
|
||||
|
||||
_openModals = modalService.OpenModals.ToBindedCollection();
|
||||
|
||||
_appState.RapidTravelTextDebounced.Subscribe((v, _) =>
|
||||
{
|
||||
if (_selectedTab?.Tab is not { } tab) return Task.CompletedTask;
|
||||
tab.RemoveItemFilter(RapidTravelFilterName);
|
||||
|
||||
if (v is null) return Task.CompletedTask;
|
||||
|
||||
tab.AddItemFilter(new ItemFilter(RapidTravelFilterName, i => i.Name.ToLower().Contains(v)));
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
public async Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action<bool> setHandled)
|
||||
{
|
||||
var keyString = key.ToString();
|
||||
|
||||
if (key == Keys.Escape)
|
||||
{
|
||||
setHandled(true);
|
||||
if ((_openModals.Collection?.Count ?? 0) > 0)
|
||||
{
|
||||
_modalService.CloseModal(_openModals.Collection!.Last());
|
||||
}
|
||||
else
|
||||
{
|
||||
await CallCommandAsync(ExitRapidTravelCommand.Instance);
|
||||
}
|
||||
}
|
||||
else if (key == Keys.Back)
|
||||
{
|
||||
if (_appState.RapidTravelText.Value!.Length > 0)
|
||||
{
|
||||
setHandled(true);
|
||||
await _appState.RapidTravelText.SetValue(
|
||||
_appState.RapidTravelText.Value![..^1]
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (keyString.Length == 1)
|
||||
{
|
||||
setHandled(true);
|
||||
await _appState.RapidTravelText.SetValue(
|
||||
_appState.RapidTravelText.Value + keyString.ToLower()
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
var currentKeyAsList = new List<KeyConfig> {new(key)};
|
||||
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(currentKeyAsList));
|
||||
if (selectedCommandBinding != null)
|
||||
{
|
||||
setHandled(true);
|
||||
await CallCommandAsync(_identifiableUserCommandService.GetCommand(selectedCommandBinding.Command));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CallCommandAsync(IUserCommand command)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _userCommandHandlerService.HandleCommandAsync(command);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command.GetType().Name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user