From 4302b23ceb979d7f5581b47b75e4f48b8991faf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Tue, 27 Feb 2024 21:17:12 +0100 Subject: [PATCH] Rapid travel filter + ESC improvements --- .../Services/DefaultModeKeyInputHandler.cs | 4 ++++ .../FileTime.App.Core/ViewModels/AppStateBase.cs | 12 ++++++------ .../Services/IKeyInputHandlerService.cs | 1 + .../Services/KeyInputHandlerService.cs | 7 +++++++ .../ViewModels/MainWindowViewModel.cs | 1 + .../FileTime.GuiApp.App/Views/MainWindow.axaml.cs | 2 +- .../ViewModels/GuiAppState.cs | 3 ++- 7 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs b/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs index 398931f..61472e9 100644 --- a/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs +++ b/src/AppCommon/FileTime.App.Core/Services/DefaultModeKeyInputHandler.cs @@ -79,6 +79,10 @@ public class DefaultModeKeyInputHandler : IDefaultModeKeyInputHandler { _modalService.CloseModal(_openModals.Last()); } + else if (_appState.RapidTravelText.Value != "") + { + await _appState.SetRapidTravelTextAsync(""); + } else if (_currentLocation.Value?.GetExtension() is { } escHandler) { var escapeResult = await escHandler.HandleEsc(); diff --git a/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs b/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs index 64778ca..fa6c644 100644 --- a/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs +++ b/src/AppCommon/FileTime.App.Core/ViewModels/AppStateBase.cs @@ -5,6 +5,7 @@ using DeclarativeProperty; using FileTime.App.Core.Configuration; using FileTime.App.Core.Models.Enums; using FileTime.Core.Models.Extensions; +using FileTime.Core.Services; using MoreLinq; using PropertyChanged.SourceGenerator; @@ -31,7 +32,7 @@ public abstract partial class AppStateBase : IAppState [Notify] public List PreviousKeys { get; } = new(); [Notify] public bool NoCommandFound { get; set; } - protected AppStateBase() + protected AppStateBase(ITabEvents tabEvents) { _rapidTravelText = new(""); RapidTravelText = _rapidTravelText.DistinctUntilChanged(); @@ -59,6 +60,8 @@ public abstract partial class AppStateBase : IAppState .Switch() .Map(c => c?.GetExtension()?.GetStatusProperty()) .Switch()!; + + tabEvents.LocationChanged += (_, _) => Task.Run(async () => await SetRapidTravelTextAsync("")).Wait(); } public void AddTab(ITabViewModel tabViewModel) @@ -79,11 +82,8 @@ public abstract partial class AppStateBase : IAppState public void SetSearchText(string? searchText) => _searchText.OnNext(searchText); - public async Task SwitchViewModeAsync(ViewMode newViewMode) - { - if (newViewMode != Models.Enums.ViewMode.RapidTravel) await SetRapidTravelTextAsync(null); - await _viewMode.SetValue(newViewMode); - } + public Task SwitchViewModeAsync(ViewMode newViewMode) + => _viewMode.SetValue(newViewMode); public async Task SetSelectedTabAsync(ITabViewModel tabToSelect) => await _selectedTab.SetValue(tabToSelect); public async Task SetRapidTravelTextAsync(string? text) => await _rapidTravelText.SetValue(text); 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 cced52c..688826a 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App.Abstractions/Services/IKeyInputHandlerService.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App.Abstractions/Services/IKeyInputHandlerService.cs @@ -5,4 +5,5 @@ namespace FileTime.GuiApp.App.Services; public interface IKeyInputHandlerService { Task ProcessKeyDown(KeyEventArgs e); + event EventHandler? UnhandledEsc; } \ No newline at end of file diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs index 53f979a..a85526a 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Services/KeyInputHandlerService.cs @@ -16,6 +16,8 @@ public class KeyInputHandlerService : IKeyInputHandlerService private readonly IAppKeyService _appKeyService; private GuiPanel _activePanel; + public event EventHandler? UnhandledEsc; + private readonly Dictionary<(GuiPanel, Key), GuiPanel> _panelMovements = new() { [(GuiPanel.FileBrowser, Key.Up)] = GuiPanel.Timeline, @@ -72,6 +74,11 @@ public class KeyInputHandlerService : IKeyInputHandlerService if (e.ToGeneralKeyEventArgs(_appKeyService, specialKeyStatus) is { } args) { await _defaultModeKeyInputHandler.HandleInputKey(args); + + if (!args.Handled && args.Key == Keys.Escape) + { + UnhandledEsc?.Invoke(this, EventArgs.Empty); + } } } else diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs index 9cbe74d..b8b5a21 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App/ViewModels/MainWindowViewModel.cs @@ -102,6 +102,7 @@ public partial class MainWindowViewModel : IMainWindowViewModel _modalService.AllModalClosed += (_, _) => FocusDefaultElement?.Invoke(); _instanceMessageHandler.ShowWindow += () => ShowWindow?.Invoke(); + _keyInputHandlerService.UnhandledEsc += (_,_) => FocusDefaultElement?.Invoke(); Task.Run(async () => { 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 6c3f887..9b35285 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.App/Views/MainWindow.axaml.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.App/Views/MainWindow.axaml.cs @@ -74,7 +74,7 @@ public partial class MainWindow : Window, IUiAccessor try { var viewModel = DI.ServiceProvider.GetRequiredService(); - viewModel.FocusDefaultElement = () => Focus(); + viewModel.FocusDefaultElement = () => Dispatcher.UIThread.Invoke(() => Focus()); viewModel.ShowWindow = Activate; ViewModel = viewModel; } diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs index 89272ad..c4acec3 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.CustomImpl/ViewModels/GuiAppState.cs @@ -2,6 +2,7 @@ using System.Collections.ObjectModel; using System.Reactive.Linq; using System.Reactive.Subjects; using FileTime.App.Core.ViewModels; +using FileTime.Core.Services; using FileTime.GuiApp.App.Models; using FileTime.GuiApp.App.ViewModels; using FileTime.Providers.Local; @@ -18,7 +19,7 @@ public partial class GuiAppState : AppStateBase, IGuiAppState, IDisposable public IObservable ActivePanel { get; } - public GuiAppState() + public GuiAppState(ITabEvents tabEvents):base(tabEvents) { ActivePanel = _activePanel.AsObservable(); }