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
{
private readonly Action<bool> _handledChanged;
private readonly Action<bool>? _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<bool> handledChanged)
public GeneralKeyEventArgs(Action<bool>? handledChanged = null)
{
_handledChanged = handledChanged;
}

View File

@@ -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,
}

View File

@@ -1,3 +1,3 @@
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
{
Task HandleInputKey(Keys key, SpecialKeysStatus specialKeysStatus, Action<bool> setHandled);
Task HandleInputKey(GeneralKeyEventArgs e, SpecialKeysStatus specialKeysStatus);
}

View File

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

View File

@@ -54,15 +54,15 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler
_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);
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)

View File

@@ -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<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)
{
_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<KeyConfig> {new(key)};
var currentKeyAsList = new List<KeyConfig> {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));
}
}

View File

@@ -28,6 +28,9 @@ public static class Startup
serviceCollection.TryAddSingleton<IItemPreviewProvider, ElementPreviewProvider>();
serviceCollection.TryAddSingleton<ILifecycleService, LifecycleService>();
serviceCollection.TryAddSingleton<IModalService, ModalService>();
serviceCollection.TryAddSingleton<IDefaultModeKeyInputHandler, DefaultModeKeyInputHandler>();
serviceCollection.TryAddSingleton<IRapidTravelModeKeyInputHandler, RapidTravelModeKeyInputHandler>();
serviceCollection.TryAddSingleton<IKeyboardConfigurationService, KeyboardConfigurationService>();
return serviceCollection
.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 ObservableCollection<ITabViewModel> _tabs = new();
public IDeclarativeProperty<ViewMode> ViewMode { get; private set; }
public IDeclarativeProperty<ViewMode> ViewMode { get; }
public ReadOnlyObservableCollection<ITabViewModel> Tabs { get; private set; }
public IObservable<string?> SearchText { get; private set; }
public ReadOnlyObservableCollection<ITabViewModel> Tabs { get; }
public IObservable<string?> SearchText { get; }
public IDeclarativeProperty<ITabViewModel?> SelectedTab { get; private set; }
public DeclarativeProperty<string?> RapidTravelText { get; private set; }
public IDeclarativeProperty<string?> RapidTravelTextDebounced { get; private set; }
public IDeclarativeProperty<ITabViewModel?> SelectedTab { get; }
public DeclarativeProperty<string?> RapidTravelText { get; }
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<CommandBindingConfiguration> PossibleCommands { get; set; } = new();
[Notify] public bool NoCommandFound { get; set; }

View File

@@ -4,5 +4,5 @@ namespace FileTime.GuiApp.App.Services;
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;
public class GuiAppKeyService : IAppKeyService<Key>
public sealed class GuiAppKeyService : IAppKeyService<Key>
{
private static readonly Dictionary<Key, Keys> KeyMapping;
@@ -70,7 +70,7 @@ public class GuiAppKeyService : IAppKeyService<Key>
{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},

View File

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

View File

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

View File

@@ -21,10 +21,6 @@ public partial class GuiAppState : AppStateBase, IGuiAppState, IDisposable
ActivePanel = _activePanel.AsObservable();
}
[Notify] private bool _noCommandFound;
[Notify] private List<CommandBindingConfiguration> _possibleCommands = new();
[Notify] private ObservableCollection<RootDriveInfo> _rootDriveInfos = new();
[Notify] private IReadOnlyList<PlaceInfo> _places = new List<PlaceInfo>();
@@ -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();
}

View File

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