Move common things to AppCore from GuiApp 2

This commit is contained in:
2023-08-07 13:09:58 +02:00
parent 936c3896b9
commit 8f5caf5c57
20 changed files with 97 additions and 73 deletions

View File

@@ -55,4 +55,14 @@ public enum Keys
Tab,
LWin,
RWin,
Num0,
Num1,
Num2,
Num3,
Num4,
Num5,
Num6,
Num7,
Num8,
Num9,
}

View File

@@ -0,0 +1,3 @@
namespace FileTime.App.Core.Models;
public record SpecialKeysStatus(bool IsAltPressed, bool IsShiftPressed, bool IsCtrlPressed);

View File

@@ -0,0 +1,3 @@
namespace FileTime.App.Core.Services;
public interface IDefaultModeKeyInputHandler : IKeyInputHandler { }

View File

@@ -0,0 +1,8 @@
using FileTime.App.Core.Models;
namespace FileTime.App.Core.Services;
public interface IKeyInputHandler
{
Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action<bool> setHandled);
}

View File

@@ -0,0 +1,3 @@
namespace FileTime.App.Core.Services;
public interface IRapidTravelModeKeyInputHandler : IKeyInputHandler { }

View File

@@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using DeclarativeProperty;
using FileTime.App.Core.Configuration;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.ViewModels.Timeline;
@@ -13,8 +14,10 @@ public interface IAppState
IDeclarativeProperty<ViewMode> ViewMode { get; }
DeclarativeProperty<string?> RapidTravelText { get; }
IDeclarativeProperty<string?> RapidTravelTextDebounced { get; }
ITimelineViewModel TimelineViewModel { get; }
IDeclarativeProperty<string?> ContainerStatus { get; }
List<KeyConfig> PreviousKeys { get; }
List<CommandBindingConfiguration> PossibleCommands { get; set; }
bool NoCommandFound { get; set; }
void AddTab(ITabViewModel tabViewModel);
void RemoveTab(ITabViewModel tabViewModel);

View File

@@ -16,6 +16,10 @@
<PackageReference Include="morelinq" Version="3.4.2" />
<PackageReference Include="MvvmGen" Version="1.2.1" />
<PackageReference Include="ObservableComputations" Version="2.3.0" />
<PackageReference Include="PropertyChanged.SourceGenerator" Version="1.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Interactive.Async" Version="6.0.1" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -2,16 +2,16 @@ using System.Collections.ObjectModel;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using DeclarativeProperty;
using FileTime.App.Core.Configuration;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.ViewModels.Timeline;
using FileTime.Core.Models.Extensions;
using MvvmGen;
using MoreLinq;
using PropertyChanged.SourceGenerator;
namespace FileTime.App.Core.ViewModels;
[ViewModel]
[Inject(typeof(ITimelineViewModel), "TimelineViewModel", PropertyAccessModifier = AccessModifier.Public)]
public abstract partial class AppStateBase : IAppState
{
private readonly BehaviorSubject<string?> _searchText = new(null);
@@ -29,8 +29,11 @@ public abstract partial class AppStateBase : IAppState
public IDeclarativeProperty<string?> RapidTravelTextDebounced { get; private set; }
public IDeclarativeProperty<string?> ContainerStatus { get; private set; }
[Notify] public List<KeyConfig> PreviousKeys { get; } = new();
[Notify] public List<CommandBindingConfiguration> PossibleCommands { get; set; } = new();
[Notify] public bool NoCommandFound { get; set; }
partial void OnInitialize()
protected AppStateBase()
{
RapidTravelText = new("");
RapidTravelTextDebounced = RapidTravelText