Move common things to AppCore from GuiApp 3

This commit is contained in:
2023-08-07 14:54:04 +02:00
parent 8f5caf5c57
commit 2107d4f92a
16 changed files with 70 additions and 64 deletions

View File

@@ -2,7 +2,7 @@
public class GeneralKeyEventArgs public class GeneralKeyEventArgs
{ {
private readonly Action<bool> _handledChanged; private readonly Action<bool>? _handledChanged;
private bool _handled; private bool _handled;
public required Keys Key { get; init; } public required Keys Key { get; init; }
@@ -14,12 +14,12 @@ public class GeneralKeyEventArgs
if (_handled != value) if (_handled != value)
{ {
_handled = value; _handled = value;
_handledChanged(value); _handledChanged?.Invoke(value);
} }
} }
} }
public GeneralKeyEventArgs(Action<bool> handledChanged) public GeneralKeyEventArgs(Action<bool>? handledChanged = null)
{ {
_handledChanged = handledChanged; _handledChanged = handledChanged;
} }

View File

@@ -1,4 +1,6 @@
namespace FileTime.App.Core.Models; using System.ComponentModel;
namespace FileTime.App.Core.Models;
public enum Keys public enum Keys
{ {
@@ -46,7 +48,7 @@ public enum Keys
Right, Right,
Enter, Enter,
Escape, Escape,
Back, Backspace,
Space, Space,
PageUp, PageUp,
PageDown, PageDown,
@@ -55,14 +57,24 @@ public enum Keys
Tab, Tab,
LWin, LWin,
RWin, RWin,
[Description("0")]
Num0, Num0,
[Description("1")]
Num1, Num1,
[Description("2")]
Num2, Num2,
[Description("3")]
Num3, Num3,
[Description("4")]
Num4, Num4,
[Description("5")]
Num5, Num5,
[Description("6")]
Num6, Num6,
[Description("7")]
Num7, Num7,
[Description("8")]
Num8, Num8,
[Description("9")]
Num9, Num9,
} }

View File

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

View File

@@ -4,5 +4,5 @@ namespace FileTime.App.Core.Services;
public interface IKeyInputHandler public interface IKeyInputHandler
{ {
Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action<bool> setHandled); Task HandleInputKey(GeneralKeyEventArgs e, SpecialKeysStatus specialKeysStatus);
} }

View File

@@ -11,6 +11,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
<PackageReference Include="morelinq" Version="3.4.2" /> <PackageReference Include="morelinq" Version="3.4.2" />

View File

@@ -54,15 +54,15 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
_keysToSkip.Add(new[] {new KeyConfig(Keys.RWin)}); _keysToSkip.Add(new[] {new KeyConfig(Keys.RWin)});
} }
public async Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action<bool> 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); _appState.PreviousKeys.Add(keyWithModifiers);
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(_appState.PreviousKeys)); var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(_appState.PreviousKeys));
selectedCommandBinding ??= _keyboardConfigurationService.CommandBindings.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; var doGeneralReset = _appState.PreviousKeys.Count > 1;
@@ -75,7 +75,7 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
var escapeResult = await escHandler.HandleEsc(); var escapeResult = await escHandler.HandleEsc();
if (escapeResult.NavigateTo != null) if (escapeResult.NavigateTo != null)
{ {
setHandled(true); args.Handled = true;
_appState.PreviousKeys.Clear(); _appState.PreviousKeys.Clear();
if (_appState.SelectedTab.Value?.Tab is { } selectedTab) if (_appState.SelectedTab.Value?.Tab is { } selectedTab)
{ {
@@ -97,7 +97,7 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
if (doGeneralReset) if (doGeneralReset)
{ {
setHandled(true); args.Handled = true;
_appState.PreviousKeys.Clear(); _appState.PreviousKeys.Clear();
_appState.PossibleCommands = new(); _appState.PossibleCommands = new();
} }
@@ -107,11 +107,11 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
{ {
_appState.PreviousKeys.Clear(); _appState.PreviousKeys.Clear();
//_dialogService.ProcessMessageBox(); //_dialogService.ProcessMessageBox();
setHandled(true); args.Handled = true;
}*/ }*/
else if (selectedCommandBinding != null) else if (selectedCommandBinding != null)
{ {
setHandled(true); args.Handled = true;
_appState.PreviousKeys.Clear(); _appState.PreviousKeys.Clear();
_appState.PossibleCommands = new(); _appState.PossibleCommands = new();
var command = _identifiableUserCommandService.GetCommand(selectedCommandBinding.Command); var command = _identifiableUserCommandService.GetCommand(selectedCommandBinding.Command);
@@ -128,14 +128,14 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
} }
else if (_appState.PreviousKeys.Count == 2) else if (_appState.PreviousKeys.Count == 2)
{ {
setHandled(true); args.Handled = true;
_appState.NoCommandFound = true; _appState.NoCommandFound = true;
_appState.PreviousKeys.Clear(); _appState.PreviousKeys.Clear();
_appState.PossibleCommands = new(); _appState.PossibleCommands = new();
} }
else else
{ {
setHandled(true); args.Handled = true;
var possibleCommands = _keyboardConfigurationService.AllShortcut.Where(c => c.Keys[0].AreEquals(keyWithModifiers)).ToList(); var possibleCommands = _keyboardConfigurationService.AllShortcut.Where(c => c.Keys[0].AreEquals(keyWithModifiers)).ToList();
if (possibleCommands.Count == 0) if (possibleCommands.Count == 0)

View File

@@ -5,6 +5,7 @@ using FileTime.App.Core.UserCommand;
using FileTime.App.Core.ViewModels; using FileTime.App.Core.ViewModels;
using FileTime.Core.Extensions; using FileTime.Core.Extensions;
using FileTime.Core.Models; using FileTime.Core.Models;
using Humanizer;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace FileTime.App.Core.Services; namespace FileTime.App.Core.Services;
@@ -53,13 +54,13 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
}); });
} }
public async Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action<bool> 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) if ((_openModals.Collection?.Count ?? 0) > 0)
{ {
_modalService.CloseModal(_openModals.Collection!.Last()); _modalService.CloseModal(_openModals.Collection!.Last());
@@ -69,11 +70,11 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
await CallCommandAsync(ExitRapidTravelCommand.Instance); await CallCommandAsync(ExitRapidTravelCommand.Instance);
} }
} }
else if (key == Keys.Back) else if (args.Key == Keys.Backspace)
{ {
if (_appState.RapidTravelText.Value!.Length > 0) if (_appState.RapidTravelText.Value!.Length > 0)
{ {
setHandled(true); args.Handled = true;
await _appState.RapidTravelText.SetValue( await _appState.RapidTravelText.SetValue(
_appState.RapidTravelText.Value![..^1] _appState.RapidTravelText.Value![..^1]
); );
@@ -81,18 +82,18 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
} }
else if (keyString.Length == 1) else if (keyString.Length == 1)
{ {
setHandled(true); args.Handled = true;
await _appState.RapidTravelText.SetValue( await _appState.RapidTravelText.SetValue(
_appState.RapidTravelText.Value + keyString.ToLower() _appState.RapidTravelText.Value + keyString.ToLower()
); );
} }
else else
{ {
var currentKeyAsList = new List<KeyConfig> {new(key)}; var currentKeyAsList = new List<KeyConfig> {new(args.Key)};
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(currentKeyAsList)); var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(currentKeyAsList));
if (selectedCommandBinding != null) if (selectedCommandBinding != null)
{ {
setHandled(true); args.Handled = true;
await CallCommandAsync(_identifiableUserCommandService.GetCommand(selectedCommandBinding.Command)); await CallCommandAsync(_identifiableUserCommandService.GetCommand(selectedCommandBinding.Command));
} }
} }

View File

@@ -28,6 +28,9 @@ public static class Startup
serviceCollection.TryAddSingleton<IItemPreviewProvider, ElementPreviewProvider>(); serviceCollection.TryAddSingleton<IItemPreviewProvider, ElementPreviewProvider>();
serviceCollection.TryAddSingleton<ILifecycleService, LifecycleService>(); serviceCollection.TryAddSingleton<ILifecycleService, LifecycleService>();
serviceCollection.TryAddSingleton<IModalService, ModalService>(); serviceCollection.TryAddSingleton<IModalService, ModalService>();
serviceCollection.TryAddSingleton<IDefaultModeKeyInputHandler, DefaultModeKeyInputHandler>();
serviceCollection.TryAddSingleton<IRapidTravelModeKeyInputHandler, RapidTravelModeKeyInputHandler>();
serviceCollection.TryAddSingleton<IKeyboardConfigurationService, KeyboardConfigurationService>();
return serviceCollection return serviceCollection
.AddCommandHandlers() .AddCommandHandlers()

View File

@@ -19,16 +19,16 @@ public abstract partial class AppStateBase : IAppState
private readonly DeclarativeProperty<ViewMode> _viewMode = new(Models.Enums.ViewMode.Default); private readonly DeclarativeProperty<ViewMode> _viewMode = new(Models.Enums.ViewMode.Default);
private readonly ObservableCollection<ITabViewModel> _tabs = new(); private readonly ObservableCollection<ITabViewModel> _tabs = new();
public IDeclarativeProperty<ViewMode> ViewMode { get; private set; } public IDeclarativeProperty<ViewMode> ViewMode { get; }
public ReadOnlyObservableCollection<ITabViewModel> Tabs { get; private set; } public ReadOnlyObservableCollection<ITabViewModel> Tabs { get; }
public IObservable<string?> SearchText { get; private set; } public IObservable<string?> SearchText { get; }
public IDeclarativeProperty<ITabViewModel?> SelectedTab { get; private set; } public IDeclarativeProperty<ITabViewModel?> SelectedTab { get; }
public DeclarativeProperty<string?> RapidTravelText { get; private set; } public DeclarativeProperty<string?> RapidTravelText { get; }
public IDeclarativeProperty<string?> RapidTravelTextDebounced { get; private set; } public IDeclarativeProperty<string?> RapidTravelTextDebounced { get; }
public IDeclarativeProperty<string?> ContainerStatus { get; private set; } public IDeclarativeProperty<string?> ContainerStatus { get; }
[Notify] public List<KeyConfig> PreviousKeys { get; } = new(); [Notify] public List<KeyConfig> PreviousKeys { get; } = new();
[Notify] public List<CommandBindingConfiguration> PossibleCommands { get; set; } = new(); [Notify] public List<CommandBindingConfiguration> PossibleCommands { get; set; } = new();
[Notify] public bool NoCommandFound { get; set; } [Notify] public bool NoCommandFound { get; set; }

View File

@@ -4,5 +4,5 @@ namespace FileTime.GuiApp.App.Services;
public interface IKeyInputHandlerService public interface IKeyInputHandlerService
{ {
Task ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action<bool> setHandled); Task ProcessKeyDown(KeyEventArgs e);
} }

View File

@@ -5,7 +5,7 @@ using FileTime.App.Core.Services;
namespace FileTime.GuiApp.App.Services; namespace FileTime.GuiApp.App.Services;
public class GuiAppKeyService : IAppKeyService<Key> public sealed class GuiAppKeyService : IAppKeyService<Key>
{ {
private static readonly Dictionary<Key, Keys> KeyMapping; private static readonly Dictionary<Key, Keys> KeyMapping;
@@ -70,7 +70,7 @@ public class GuiAppKeyService : IAppKeyService<Key>
{Key.Right, Keys.Right}, {Key.Right, Keys.Right},
{Key.Enter, Keys.Enter}, {Key.Enter, Keys.Enter},
{Key.Escape, Keys.Escape}, {Key.Escape, Keys.Escape},
{Key.Back, Keys.Back}, {Key.Back, Keys.Backspace},
{Key.Space, Keys.Space}, {Key.Space, Keys.Space},
{Key.PageUp, Keys.PageUp}, {Key.PageUp, Keys.PageUp},
{Key.PageDown, Keys.PageDown}, {Key.PageDown, Keys.PageDown},

View File

@@ -2,6 +2,7 @@ using Avalonia.Input;
using FileTime.App.Core.Models; using FileTime.App.Core.Models;
using FileTime.App.Core.Models.Enums; using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.Services; using FileTime.App.Core.Services;
using FileTime.GuiApp.App.Extensions;
using FileTime.GuiApp.App.Models; using FileTime.GuiApp.App.Models;
using FileTime.GuiApp.App.ViewModels; using FileTime.GuiApp.App.ViewModels;
@@ -13,7 +14,6 @@ public class KeyInputHandlerService : IKeyInputHandlerService
private readonly IDefaultModeKeyInputHandler _defaultModeKeyInputHandler; private readonly IDefaultModeKeyInputHandler _defaultModeKeyInputHandler;
private readonly IRapidTravelModeKeyInputHandler _rapidTravelModeKeyInputHandler; private readonly IRapidTravelModeKeyInputHandler _rapidTravelModeKeyInputHandler;
private readonly IAppKeyService<Key> _appKeyService; private readonly IAppKeyService<Key> _appKeyService;
private ViewMode _viewMode;
private GuiPanel _activePanel; private GuiPanel _activePanel;
private readonly Dictionary<(GuiPanel, Key), GuiPanel> _panelMovements = new() private readonly Dictionary<(GuiPanel, Key), GuiPanel> _panelMovements = new()
@@ -33,13 +33,12 @@ public class KeyInputHandlerService : IKeyInputHandlerService
_rapidTravelModeKeyInputHandler = rapidTravelModeKeyInputHandler; _rapidTravelModeKeyInputHandler = rapidTravelModeKeyInputHandler;
_appKeyService = appKeyService; _appKeyService = appKeyService;
appState.ViewMode.Subscribe(v => _viewMode = v);
appState.ActivePanel.Subscribe(p => _activePanel = p); appState.ActivePanel.Subscribe(p => _activePanel = p);
} }
public async Task ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action<bool> setHandled) public async Task ProcessKeyDown(KeyEventArgs e)
{ {
if (key is Key.LeftAlt if (e.Key is Key.LeftAlt
or Key.RightAlt or Key.RightAlt
or Key.LeftShift or Key.LeftShift
or Key.RightShift or Key.RightShift
@@ -51,16 +50,16 @@ public class KeyInputHandlerService : IKeyInputHandlerService
//_appState.NoCommandFound = false; //_appState.NoCommandFound = false;
var isAltPressed = (keyModifiers & KeyModifiers.Alt) == KeyModifiers.Alt; var isAltPressed = (e.KeyModifiers & KeyModifiers.Alt) == KeyModifiers.Alt;
var isShiftPressed = (keyModifiers & KeyModifiers.Shift) == KeyModifiers.Shift; var isShiftPressed = (e.KeyModifiers & KeyModifiers.Shift) == KeyModifiers.Shift;
var isCtrlPressed = (keyModifiers & KeyModifiers.Control) == KeyModifiers.Control; var isCtrlPressed = (e.KeyModifiers & KeyModifiers.Control) == KeyModifiers.Control;
if (isCtrlPressed if (isCtrlPressed
&& key is Key.Left or Key.Right or Key.Up or Key.Down && e.Key is Key.Left or Key.Right or Key.Up or Key.Down
&& _panelMovements.TryGetValue((_activePanel, key), out var newPanel)) && _panelMovements.TryGetValue((_activePanel, e.Key), out var newPanel))
{ {
_appState.SetActivePanel(newPanel); _appState.SetActivePanel(newPanel);
setHandled(true); e.Handled = true;
return; return;
} }
@@ -68,18 +67,18 @@ public class KeyInputHandlerService : IKeyInputHandlerService
if (_activePanel == GuiPanel.FileBrowser) 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 else
{ {
if (_appKeyService.MapKey(key) is { } mappedKey) if (e.ToGeneralKeyEventArgs(_appKeyService) is { } args)
{ {
await _rapidTravelModeKeyInputHandler.HandleInputKey(mappedKey, specialKeyStatus, setHandled); await _rapidTravelModeKeyInputHandler.HandleInputKey(args, specialKeyStatus);
} }
} }
} }

View File

@@ -73,8 +73,8 @@ public partial class MainWindowViewModel : IMainWindowViewModel
Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait(); Task.Run(async () => await _lifecycleService.InitStartupHandlersAsync()).Wait();
} }
public void ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action<bool> setHandled) public void ProcessKeyDown(KeyEventArgs e)
=> _keyInputHandlerService.ProcessKeyDown(key, keyModifiers, setHandled); => _keyInputHandlerService.ProcessKeyDown(e);
public async Task OpenContainerByFullName(FullName fullName) public async Task OpenContainerByFullName(FullName fullName)
{ {

View File

@@ -100,7 +100,7 @@ public partial class MainWindow : Window, IUiAccessor
private void OnKeyDown(object sender, KeyEventArgs e) private void OnKeyDown(object sender, KeyEventArgs e)
{ {
if ((_openModals?.Count ?? 0) > 0) return; 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) private void HeaderPointerPressed(object sender, PointerPressedEventArgs e)

View File

@@ -21,10 +21,6 @@ public partial class GuiAppState : AppStateBase, IGuiAppState, IDisposable
ActivePanel = _activePanel.AsObservable(); ActivePanel = _activePanel.AsObservable();
} }
[Notify] private bool _noCommandFound;
[Notify] private List<CommandBindingConfiguration> _possibleCommands = new();
[Notify] private ObservableCollection<RootDriveInfo> _rootDriveInfos = new(); [Notify] private ObservableCollection<RootDriveInfo> _rootDriveInfos = new();
[Notify] private IReadOnlyList<PlaceInfo> _places = new List<PlaceInfo>(); [Notify] private IReadOnlyList<PlaceInfo> _places = new List<PlaceInfo>();
@@ -35,8 +31,5 @@ public partial class GuiAppState : AppStateBase, IGuiAppState, IDisposable
public void SetActivePanel(GuiPanel newPanel) public void SetActivePanel(GuiPanel newPanel)
=> _activePanel.OnNext(newPanel); => _activePanel.OnNext(newPanel);
public void Dispose() public void Dispose() => _activePanel.Dispose();
{
_activePanel.Dispose();
}
} }

View File

@@ -53,9 +53,6 @@ public static class Startup
{ {
serviceCollection.TryAddSingleton<IRxSchedulerService, AvaloniaRxSchedulerService>(); serviceCollection.TryAddSingleton<IRxSchedulerService, AvaloniaRxSchedulerService>();
serviceCollection.TryAddSingleton<IKeyInputHandlerService, KeyInputHandlerService>(); serviceCollection.TryAddSingleton<IKeyInputHandlerService, KeyInputHandlerService>();
serviceCollection.TryAddSingleton<IDefaultModeKeyInputHandler, DefaultModeKeyInputHandler>();
serviceCollection.TryAddSingleton<IKeyboardConfigurationService, KeyboardConfigurationService>();
serviceCollection.TryAddSingleton<IRapidTravelModeKeyInputHandler, RapidTravelModeKeyInputHandler>();
serviceCollection.TryAddSingleton<IIconProvider, MaterialIconProvider>(); serviceCollection.TryAddSingleton<IIconProvider, MaterialIconProvider>();
serviceCollection.TryAddSingleton<IDialogService, DialogService>(); serviceCollection.TryAddSingleton<IDialogService, DialogService>();
serviceCollection.TryAddSingleton<SystemClipboardService>(); serviceCollection.TryAddSingleton<SystemClipboardService>();