From 2107d4f92ac77d628882de3f27c77e289c9a621e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Mon, 7 Aug 2023 14:54:04 +0200 Subject: [PATCH] Move common things to AppCore from GuiApp 3 --- .../Models/GeneralKeyEventArgs.cs | 6 ++-- .../Models/Keys.cs | 16 ++++++++-- .../Models/SpecialKeysStatus.cs | 2 +- .../Services/IKeyInputHandler.cs | 2 +- .../FileTime.App.Core.csproj | 1 + .../Services/DefaultModeKeyInputHandler.cs | 18 ++++++------ .../RapidTravelModeKeyInputHandler.cs | 19 ++++++------ src/AppCommon/FileTime.App.Core/Startup.cs | 3 ++ .../ViewModels/AppStateBase.cs | 14 ++++----- .../Services/IKeyInputHandlerService.cs | 2 +- .../Services/GuiAppKeyService.cs | 4 +-- .../Services/KeyInputHandlerService.cs | 29 +++++++++---------- .../ViewModels/MainWindowViewModel.cs | 4 +-- .../Views/MainWindow.axaml.cs | 2 +- .../ViewModels/GuiAppState.cs | 9 +----- .../Avalonia/FileTime.GuiApp/Startup.cs | 3 -- 16 files changed, 70 insertions(+), 64 deletions(-) diff --git a/src/AppCommon/FileTime.App.Core.Abstraction/Models/GeneralKeyEventArgs.cs b/src/AppCommon/FileTime.App.Core.Abstraction/Models/GeneralKeyEventArgs.cs index ee83f00..4bc2a76 100644 --- a/src/AppCommon/FileTime.App.Core.Abstraction/Models/GeneralKeyEventArgs.cs +++ b/src/AppCommon/FileTime.App.Core.Abstraction/Models/GeneralKeyEventArgs.cs @@ -2,7 +2,7 @@ public class GeneralKeyEventArgs { - private readonly Action _handledChanged; + private readonly Action? _handledChanged; private bool _handled; public required Keys Key { get; init; } @@ -14,12 +14,12 @@ public class GeneralKeyEventArgs if (_handled != value) { _handled = value; - _handledChanged(value); + _handledChanged?.Invoke(value); } } } - public GeneralKeyEventArgs(Action handledChanged) + public GeneralKeyEventArgs(Action? handledChanged = null) { _handledChanged = handledChanged; } diff --git a/src/AppCommon/FileTime.App.Core.Abstraction/Models/Keys.cs b/src/AppCommon/FileTime.App.Core.Abstraction/Models/Keys.cs index eaddcde..78dbffd 100644 --- a/src/AppCommon/FileTime.App.Core.Abstraction/Models/Keys.cs +++ b/src/AppCommon/FileTime.App.Core.Abstraction/Models/Keys.cs @@ -1,4 +1,6 @@ -namespace FileTime.App.Core.Models; +using System.ComponentModel; + +namespace FileTime.App.Core.Models; public enum Keys { @@ -46,7 +48,7 @@ public enum Keys Right, Enter, Escape, - Back, + Backspace, Space, PageUp, PageDown, @@ -55,14 +57,24 @@ public enum Keys Tab, LWin, RWin, + [Description("0")] Num0, + [Description("1")] Num1, + [Description("2")] Num2, + [Description("3")] Num3, + [Description("4")] Num4, + [Description("5")] Num5, + [Description("6")] Num6, + [Description("7")] Num7, + [Description("8")] Num8, + [Description("9")] Num9, } \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.Core.Abstraction/Models/SpecialKeysStatus.cs b/src/AppCommon/FileTime.App.Core.Abstraction/Models/SpecialKeysStatus.cs index 4a42fd1..99be174 100644 --- a/src/AppCommon/FileTime.App.Core.Abstraction/Models/SpecialKeysStatus.cs +++ b/src/AppCommon/FileTime.App.Core.Abstraction/Models/SpecialKeysStatus.cs @@ -1,3 +1,3 @@ namespace FileTime.App.Core.Models; -public record SpecialKeysStatus(bool IsAltPressed, bool IsShiftPressed, bool IsCtrlPressed); \ No newline at end of file +public record struct SpecialKeysStatus(bool IsAltPressed, bool IsShiftPressed, bool IsCtrlPressed); \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.Core.Abstraction/Services/IKeyInputHandler.cs b/src/AppCommon/FileTime.App.Core.Abstraction/Services/IKeyInputHandler.cs index 768af6e..602ba68 100644 --- a/src/AppCommon/FileTime.App.Core.Abstraction/Services/IKeyInputHandler.cs +++ b/src/AppCommon/FileTime.App.Core.Abstraction/Services/IKeyInputHandler.cs @@ -4,5 +4,5 @@ namespace FileTime.App.Core.Services; public interface IKeyInputHandler { - Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action setHandled); + Task HandleInputKey(GeneralKeyEventArgs e, SpecialKeysStatus specialKeysStatus); } \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.Core/FileTime.App.Core.csproj b/src/AppCommon/FileTime.App.Core/FileTime.App.Core.csproj index 8d2cf9a..7b79224 100644 --- a/src/AppCommon/FileTime.App.Core/FileTime.App.Core.csproj +++ b/src/AppCommon/FileTime.App.Core/FileTime.App.Core.csproj @@ -11,6 +11,7 @@ + diff --git a/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs b/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs index 8d7956b..666bf78 100644 --- a/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs +++ b/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs @@ -54,15 +54,15 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler _keysToSkip.Add(new[] {new KeyConfig(Keys.RWin)}); } - public async Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action setHandled) + public async Task HandleInputKey(GeneralKeyEventArgs args, SpecialKeysStatus specialKeysStatus) { - var keyWithModifiers = new KeyConfig(key, shift: specialKeysStatus.IsShiftPressed, alt: specialKeysStatus.IsAltPressed, ctrl: specialKeysStatus.IsCtrlPressed); + var keyWithModifiers = new KeyConfig(args.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) + if (args.Key == Keys.Escape) { var doGeneralReset = _appState.PreviousKeys.Count > 1; @@ -75,7 +75,7 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler var escapeResult = await escHandler.HandleEsc(); if (escapeResult.NavigateTo != null) { - setHandled(true); + args.Handled = true; _appState.PreviousKeys.Clear(); if (_appState.SelectedTab.Value?.Tab is { } selectedTab) { @@ -97,7 +97,7 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler if (doGeneralReset) { - setHandled(true); + args.Handled = true; _appState.PreviousKeys.Clear(); _appState.PossibleCommands = new(); } @@ -107,11 +107,11 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler { _appState.PreviousKeys.Clear(); //_dialogService.ProcessMessageBox(); - setHandled(true); + args.Handled = true; }*/ else if (selectedCommandBinding != null) { - setHandled(true); + args.Handled = true; _appState.PreviousKeys.Clear(); _appState.PossibleCommands = new(); var command = _identifiableUserCommandService.GetCommand(selectedCommandBinding.Command); @@ -128,14 +128,14 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler } else if (_appState.PreviousKeys.Count == 2) { - setHandled(true); + args.Handled = true; _appState.NoCommandFound = true; _appState.PreviousKeys.Clear(); _appState.PossibleCommands = new(); } else { - setHandled(true); + args.Handled = true; var possibleCommands = _keyboardConfigurationService.AllShortcut.Where(c => c.Keys[0].AreEquals(keyWithModifiers)).ToList(); if (possibleCommands.Count == 0) diff --git a/src/AppCommon/FileTime.App.Core/Services/RapidTravelModeKeyInputHandler.cs b/src/AppCommon/FileTime.App.Core/Services/RapidTravelModeKeyInputHandler.cs index 074641d..7c2e7d9 100644 --- a/src/AppCommon/FileTime.App.Core/Services/RapidTravelModeKeyInputHandler.cs +++ b/src/AppCommon/FileTime.App.Core/Services/RapidTravelModeKeyInputHandler.cs @@ -5,6 +5,7 @@ using FileTime.App.Core.UserCommand; using FileTime.App.Core.ViewModels; using FileTime.Core.Extensions; using FileTime.Core.Models; +using Humanizer; using Microsoft.Extensions.Logging; namespace FileTime.App.Core.Services; @@ -53,13 +54,13 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler }); } - public async Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action setHandled) + public async Task HandleInputKey(GeneralKeyEventArgs args, SpecialKeysStatus specialKeysStatus) { - var keyString = key.ToString(); + var keyString = args.Key.Humanize(); - if (key == Keys.Escape) + if (args.Key == Keys.Escape) { - setHandled(true); + args.Handled = true; if ((_openModals.Collection?.Count ?? 0) > 0) { _modalService.CloseModal(_openModals.Collection!.Last()); @@ -69,11 +70,11 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler await CallCommandAsync(ExitRapidTravelCommand.Instance); } } - else if (key == Keys.Back) + else if (args.Key == Keys.Backspace) { if (_appState.RapidTravelText.Value!.Length > 0) { - setHandled(true); + args.Handled = true; await _appState.RapidTravelText.SetValue( _appState.RapidTravelText.Value![..^1] ); @@ -81,18 +82,18 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler } else if (keyString.Length == 1) { - setHandled(true); + args.Handled = true; await _appState.RapidTravelText.SetValue( _appState.RapidTravelText.Value + keyString.ToLower() ); } else { - var currentKeyAsList = new List {new(key)}; + var currentKeyAsList = new List {new(args.Key)}; var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(currentKeyAsList)); if (selectedCommandBinding != null) { - setHandled(true); + args.Handled = true; await CallCommandAsync(_identifiableUserCommandService.GetCommand(selectedCommandBinding.Command)); } } diff --git a/src/AppCommon/FileTime.App.Core/Startup.cs b/src/AppCommon/FileTime.App.Core/Startup.cs index a53d051..285169f 100644 --- a/src/AppCommon/FileTime.App.Core/Startup.cs +++ b/src/AppCommon/FileTime.App.Core/Startup.cs @@ -28,6 +28,9 @@ public static class Startup serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(); + serviceCollection.TryAddSingleton(); + serviceCollection.TryAddSingleton(); + serviceCollection.TryAddSingleton(); return serviceCollection .AddCommandHandlers() diff --git a/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs b/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs index 94420a3..7581236 100644 --- a/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs +++ b/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs @@ -19,16 +19,16 @@ public abstract partial class AppStateBase : IAppState private readonly DeclarativeProperty _viewMode = new(Models.Enums.ViewMode.Default); private readonly ObservableCollection _tabs = new(); - public IDeclarativeProperty ViewMode { get; private set; } + public IDeclarativeProperty ViewMode { get; } - public ReadOnlyObservableCollection Tabs { get; private set; } - public IObservable SearchText { get; private set; } + public ReadOnlyObservableCollection Tabs { get; } + public IObservable SearchText { get; } - public IDeclarativeProperty SelectedTab { get; private set; } - public DeclarativeProperty RapidTravelText { get; private set; } - public IDeclarativeProperty RapidTravelTextDebounced { get; private set; } + public IDeclarativeProperty SelectedTab { get; } + public DeclarativeProperty RapidTravelText { get; } + public IDeclarativeProperty RapidTravelTextDebounced { get; } - public IDeclarativeProperty ContainerStatus { get; private set; } + public IDeclarativeProperty ContainerStatus { get; } [Notify] public List PreviousKeys { get; } = new(); [Notify] public List PossibleCommands { get; set; } = new(); [Notify] public bool NoCommandFound { get; set; } diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.App.Abstractions/Services/IKeyInputHandlerService.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.App.Abstractions/Services/IKeyInputHandlerService.cs index e12ccf2..cced52c 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App.Abstractions/Services/IKeyInputHandlerService.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App.Abstractions/Services/IKeyInputHandlerService.cs @@ -4,5 +4,5 @@ namespace FileTime.GuiApp.App.Services; public interface IKeyInputHandlerService { - Task ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action setHandled); + Task ProcessKeyDown(KeyEventArgs e); } \ No newline at end of file diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/GuiAppKeyService.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/GuiAppKeyService.cs index a79d8f1..e6c850d 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/GuiAppKeyService.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/GuiAppKeyService.cs @@ -5,7 +5,7 @@ using FileTime.App.Core.Services; namespace FileTime.GuiApp.App.Services; -public class GuiAppKeyService : IAppKeyService +public sealed class GuiAppKeyService : IAppKeyService { private static readonly Dictionary KeyMapping; @@ -70,7 +70,7 @@ public class GuiAppKeyService : IAppKeyService {Key.Right, Keys.Right}, {Key.Enter, Keys.Enter}, {Key.Escape, Keys.Escape}, - {Key.Back, Keys.Back}, + {Key.Back, Keys.Backspace}, {Key.Space, Keys.Space}, {Key.PageUp, Keys.PageUp}, {Key.PageDown, Keys.PageDown}, diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs index f460049..5747f9f 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs @@ -2,6 +2,7 @@ using Avalonia.Input; using FileTime.App.Core.Models; using FileTime.App.Core.Models.Enums; using FileTime.App.Core.Services; +using FileTime.GuiApp.App.Extensions; using FileTime.GuiApp.App.Models; using FileTime.GuiApp.App.ViewModels; @@ -13,7 +14,6 @@ public class KeyInputHandlerService : IKeyInputHandlerService private readonly IDefaultModeKeyInputHandler _defaultModeKeyInputHandler; private readonly IRapidTravelModeKeyInputHandler _rapidTravelModeKeyInputHandler; private readonly IAppKeyService _appKeyService; - private ViewMode _viewMode; private GuiPanel _activePanel; private readonly Dictionary<(GuiPanel, Key), GuiPanel> _panelMovements = new() @@ -33,13 +33,12 @@ public class KeyInputHandlerService : IKeyInputHandlerService _rapidTravelModeKeyInputHandler = rapidTravelModeKeyInputHandler; _appKeyService = appKeyService; - appState.ViewMode.Subscribe(v => _viewMode = v); appState.ActivePanel.Subscribe(p => _activePanel = p); } - public async Task ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action setHandled) + public async Task ProcessKeyDown(KeyEventArgs e) { - if (key is Key.LeftAlt + if (e.Key is Key.LeftAlt or Key.RightAlt or Key.LeftShift or Key.RightShift @@ -51,16 +50,16 @@ public class KeyInputHandlerService : IKeyInputHandlerService //_appState.NoCommandFound = false; - var isAltPressed = (keyModifiers & KeyModifiers.Alt) == KeyModifiers.Alt; - var isShiftPressed = (keyModifiers & KeyModifiers.Shift) == KeyModifiers.Shift; - var isCtrlPressed = (keyModifiers & KeyModifiers.Control) == KeyModifiers.Control; + var isAltPressed = (e.KeyModifiers & KeyModifiers.Alt) == KeyModifiers.Alt; + var isShiftPressed = (e.KeyModifiers & KeyModifiers.Shift) == KeyModifiers.Shift; + var isCtrlPressed = (e.KeyModifiers & KeyModifiers.Control) == KeyModifiers.Control; if (isCtrlPressed - && key is Key.Left or Key.Right or Key.Up or Key.Down - && _panelMovements.TryGetValue((_activePanel, key), out var newPanel)) + && e.Key is Key.Left or Key.Right or Key.Up or Key.Down + && _panelMovements.TryGetValue((_activePanel, e.Key), out var newPanel)) { _appState.SetActivePanel(newPanel); - setHandled(true); + e.Handled = true; return; } @@ -68,18 +67,18 @@ public class KeyInputHandlerService : IKeyInputHandlerService if (_activePanel == GuiPanel.FileBrowser) { - if (_viewMode == ViewMode.Default) + if (_appState.ViewMode.Value == ViewMode.Default) { - if (_appKeyService.MapKey(key) is { } mappedKey) + if (e.ToGeneralKeyEventArgs(_appKeyService) is { } args) { - await _defaultModeKeyInputHandler.HandleInputKey(mappedKey, specialKeyStatus, setHandled); + await _defaultModeKeyInputHandler.HandleInputKey(args, specialKeyStatus); } } else { - if (_appKeyService.MapKey(key) is { } mappedKey) + if (e.ToGeneralKeyEventArgs(_appKeyService) is { } args) { - await _rapidTravelModeKeyInputHandler.HandleInputKey(mappedKey, specialKeyStatus, setHandled); + await _rapidTravelModeKeyInputHandler.HandleInputKey(args, specialKeyStatus); } } } diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs index c9bfed5..487d7ff 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs @@ -73,8 +73,8 @@ public partial class MainWindowViewModel : IMainWindowViewModel Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait(); } - public void ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action setHandled) - => _keyInputHandlerService.ProcessKeyDown(key, keyModifiers, setHandled); + public void ProcessKeyDown(KeyEventArgs e) + => _keyInputHandlerService.ProcessKeyDown(e); public async Task OpenContainerByFullName(FullName fullName) { diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Views/MainWindow.axaml.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Views/MainWindow.axaml.cs index 0b597f2..dae406c 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Views/MainWindow.axaml.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Views/MainWindow.axaml.cs @@ -100,7 +100,7 @@ public partial class MainWindow : Window, IUiAccessor private void OnKeyDown(object sender, KeyEventArgs e) { if ((_openModals?.Count ?? 0) > 0) return; - ViewModel?.ProcessKeyDown(e.Key, e.KeyModifiers, h => e.Handled = h); + ViewModel?.ProcessKeyDown(e); } private void HeaderPointerPressed(object sender, PointerPressedEventArgs e) diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs index 103c595..eeb042a 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs @@ -21,10 +21,6 @@ public partial class GuiAppState : AppStateBase, IGuiAppState, IDisposable ActivePanel = _activePanel.AsObservable(); } - [Notify] private bool _noCommandFound; - - [Notify] private List _possibleCommands = new(); - [Notify] private ObservableCollection _rootDriveInfos = new(); [Notify] private IReadOnlyList _places = new List(); @@ -35,8 +31,5 @@ public partial class GuiAppState : AppStateBase, IGuiAppState, IDisposable public void SetActivePanel(GuiPanel newPanel) => _activePanel.OnNext(newPanel); - public void Dispose() - { - _activePanel.Dispose(); - } + public void Dispose() => _activePanel.Dispose(); } diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp/Startup.cs b/src/GuiApp/Avalonia/FileTime.GuiApp/Startup.cs index e684ff8..8fb9da2 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp/Startup.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp/Startup.cs @@ -53,9 +53,6 @@ public static class Startup { serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(); - serviceCollection.TryAddSingleton(); - serviceCollection.TryAddSingleton(); - serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton(); serviceCollection.TryAddSingleton();