WIP: RapidTravel, ModalService

This commit is contained in:
2022-05-10 21:36:39 +02:00
parent 5b9d0667cc
commit 0ac9209676
19 changed files with 247 additions and 88 deletions

View File

@@ -11,6 +11,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MvvmGen" Version="1.1.5" />
<PackageReference Include="PropertyChanged.SourceGenerator" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>

View File

@@ -1,12 +1,17 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using DynamicData;
using PropertyChanged.SourceGenerator;
namespace FileTime.App.Core.Models;
public class BindedCollection<T> : IDisposable
public partial class BindedCollection<T> : IDisposable, INotifyPropertyChanged
{
private readonly IDisposable _disposable;
public ReadOnlyObservableCollection<T> Collection { get; }
private IDisposable? _innerDisposable;
[Notify] private ReadOnlyObservableCollection<T>? _collection;
public BindedCollection(IObservable<IChangeSet<T>> dynamicList)
{
_disposable = dynamicList
@@ -14,12 +19,34 @@ public class BindedCollection<T> : IDisposable
.DisposeMany()
.Subscribe();
_collection = collection;
}
public BindedCollection(IObservable<IObservable<IChangeSet<T>>?> dynamicListSource)
{
_disposable = dynamicListSource.Subscribe(dynamicList =>
{
_innerDisposable?.Dispose();
if (dynamicList is not null)
{
_innerDisposable = dynamicList
.Bind(out var collection)
.DisposeMany()
.Subscribe();
Collection = collection;
}
else
{
Collection = null;
}
});
}
public void Dispose()
{
_disposable.Dispose();
_innerDisposable?.Dispose();
GC.SuppressFinalize(this);
}
}

View File

@@ -0,0 +1,12 @@
using DynamicData;
using FileTime.App.Core.ViewModels;
namespace FileTime.App.Core.Services;
public interface IModalService
{
IObservable<IChangeSet<IModalViewModelBase>> OpenModals { get; }
void OpenModal(IModalViewModelBase modalToOpen);
void CloseModal(IModalViewModelBase modalToClose);
}

View File

@@ -8,9 +8,11 @@ public interface IAppState
ObservableCollection<ITabViewModel> Tabs { get; }
IObservable<ITabViewModel?> SelectedTab { get; }
IObservable<string?> SearchText { get; }
ViewMode ViewMode { get; }
IObservable<ViewMode> ViewMode { get; }
string RapidTravelText { get; set; }
void AddTab(ITabViewModel tabViewModel);
void RemoveTab(ITabViewModel tabViewModel);
void SetSearchText(string? searchText);
void SwitchViewMode(ViewMode newViewMode);
}

View File

@@ -0,0 +1,6 @@
namespace FileTime.App.Core.ViewModels;
public interface IModalViewModelBase
{
string Name { get; }
}

View File

@@ -1,4 +1,4 @@
using DynamicData;
using System.Reactive.Linq;
using FileTime.App.Core.Command;
using FileTime.App.Core.Models;
using FileTime.App.Core.ViewModels;
@@ -14,7 +14,7 @@ public class ItemManipulationCommandHandler : CommandHandlerBase
private IItemViewModel? _currentSelectedItem;
private readonly ICommandHandlerService _commandHandlerService;
private readonly IClipboardService _clipboardService;
private BindedCollection<IAbsolutePath>? _markedItems;
private readonly BindedCollection<IAbsolutePath>? _markedItems;
public ItemManipulationCommandHandler(
IAppState appState,
@@ -24,14 +24,11 @@ public class ItemManipulationCommandHandler : CommandHandlerBase
_commandHandlerService = commandHandlerService;
_clipboardService = clipboardService;
SaveSelectedTab(t =>
{
_selectedTab = t;
_markedItems?.Dispose();
_markedItems = t == null ? null : new BindedCollection<IAbsolutePath>(t.MarkedItems);
});
SaveSelectedTab(t => _selectedTab = t);
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
_markedItems = new BindedCollection<IAbsolutePath>(appState.SelectedTab.Select(t => t?.MarkedItems));
AddCommandHandlers(new (Commands, Func<Task>)[]
{
(Commands.Copy, Copy),
@@ -55,9 +52,9 @@ public class ItemManipulationCommandHandler : CommandHandlerBase
_clipboardService.Clear();
_clipboardService.SetCommand<CopyCommand>();
if ((_markedItems?.Collection.Count ?? 0) > 0)
if ((_markedItems?.Collection?.Count ?? 0) > 0)
{
foreach (var item in _markedItems!.Collection)
foreach (var item in _markedItems!.Collection!)
{
_clipboardService.AddContent(item);
}

View File

@@ -1,6 +1,7 @@
using System.Reactive.Linq;
using FileTime.App.Core.Command;
using FileTime.App.Core.Extensions;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
@@ -8,6 +9,7 @@ namespace FileTime.App.Core.Services.CommandHandler;
public class NavigationCommandHandler : CommandHandlerBase
{
private readonly IAppState _appState;
private ITabViewModel? _selectedTab;
private IContainer? _currentLocation;
private IItemViewModel? _currentSelectedItem;
@@ -15,6 +17,8 @@ public class NavigationCommandHandler : CommandHandlerBase
public NavigationCommandHandler(IAppState appState) : base(appState)
{
_appState = appState;
SaveSelectedTab(t => _selectedTab = t);
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
SaveCurrentLocation(l => _currentLocation = l);
@@ -22,6 +26,7 @@ public class NavigationCommandHandler : CommandHandlerBase
AddCommandHandlers(new (Commands, Func<Task>)[]
{
(Commands.EnterRapidTravel, EnterRapidTravel),
(Commands.GoUp, GoUp),
(Commands.MoveCursorDown, MoveCursorDown),
(Commands.MoveCursorUp, MoveCursorUp),
@@ -64,4 +69,10 @@ public class NavigationCommandHandler : CommandHandlerBase
_selectedTab.Tab?.SetSelectedItem(newSelectedItem.ToAbsolutePath());
}
private Task EnterRapidTravel()
{
_appState.SwitchViewMode(ViewMode.RapidTravel);
return Task.CompletedTask;
}
}

View File

@@ -13,17 +13,21 @@ public abstract partial class AppStateBase : IAppState
private readonly BehaviorSubject<string?> _searchText = new(null);
private readonly BehaviorSubject<ITabViewModel?> _selectedTab = new(null);
private readonly BehaviorSubject<IEnumerable<ITabViewModel>> _tabs = new(Enumerable.Empty<ITabViewModel>());
private readonly BehaviorSubject<ViewMode> _viewMode = new(Models.Enums.ViewMode.Default);
[Property]
private ViewMode _viewMode;
public IObservable<ViewMode> ViewMode { get; private set; }
public ObservableCollection<ITabViewModel> Tabs { get; } = new();
public IObservable<string?> SearchText { get; private set; }
public IObservable<ITabViewModel?> SelectedTab { get; private set; }
[Property] private string _rapidTravelText = "";
partial void OnInitialize()
{
ViewMode = _viewMode.AsObservable();
Tabs.CollectionChanged += (_, _) => _tabs.OnNext(Tabs);
SearchText = _searchText.AsObservable();
SelectedTab = Observable.CombineLatest(_tabs, _selectedTab, GetSelectedTab);
@@ -43,6 +47,11 @@ public abstract partial class AppStateBase : IAppState
public void SetSearchText(string? searchText) => _searchText.OnNext(searchText);
public void SwitchViewMode(ViewMode newViewMode)
{
_viewMode.OnNext(newViewMode);
}
public void SetSelectedTab(ITabViewModel tabToSelect) => _selectedTab.OnNext(tabToSelect);
private ITabViewModel? GetSelectedTab(IEnumerable<ITabViewModel> tabs, ITabViewModel? expectedSelectedTab)

View File

@@ -6,7 +6,6 @@ using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.Services;
using FileTime.Core.Models;
using FileTime.Core.Services;
using FileTime.Tools.Extensions;
using Microsoft.Extensions.DependencyInjection;
using MvvmGen;
@@ -21,7 +20,7 @@ public partial class TabViewModel : ITabViewModel, IDisposable
private readonly IRxSchedulerService _rxSchedulerService;
private readonly SourceList<IAbsolutePath> _markedItems = new();
private readonly List<IDisposable> _disposables = new();
private bool disposed;
private bool _disposed;
public ITab? Tab { get; private set; }
public int TabNumber { get; private set; }
@@ -96,23 +95,9 @@ public partial class TabViewModel : ITabViewModel, IDisposable
SelectedsChildrenCollectionObservable = InitAsd(SelectedsChildren);
ParentsChildrenCollectionObservable = InitAsd(ParentsChildren);
CurrentItems.Subscribe(children =>
{
CurrentItemsCollection?.Dispose();
CurrentItemsCollection = children.MapNull(c => new BindedCollection<IItemViewModel>(c!));
});
ParentsChildren.Subscribe(children =>
{
ParentsChildrenCollection?.Dispose();
ParentsChildrenCollection = children.MapNull(c => new BindedCollection<IItemViewModel>(c!));
});
SelectedsChildren.Subscribe(children =>
{
SelectedsChildrenCollection?.Dispose();
SelectedsChildrenCollection = children.MapNull(c => new BindedCollection<IItemViewModel>(c!));
});
CurrentItemsCollection = new(CurrentItems);
ParentsChildrenCollection = new(ParentsChildren);
SelectedsChildrenCollection = new(SelectedsChildren);
tab.CurrentLocation.Subscribe((_) => _markedItems.Clear());
@@ -131,7 +116,7 @@ public partial class TabViewModel : ITabViewModel, IDisposable
i?.TransformAsync(MapItem)
.Transform(i => MapItemToViewModel(i, ItemViewModelType.SelectedChild))),
currentSelectedItemThrottled
.Where(c => c is null || c is not IContainerViewModel)
.Where(c => c is null or not IContainerViewModel)
.Select(_ => (IObservable<IChangeSet<IItemViewModel>>?) null)
)
.ObserveOn(_rxSchedulerService.GetWorkerScheduler())
@@ -251,7 +236,7 @@ public partial class TabViewModel : ITabViewModel, IDisposable
private void Dispose(bool disposing)
{
if (!disposed && disposing)
if (!_disposed && disposing)
{
foreach (var disposable in _disposables)
{
@@ -265,6 +250,6 @@ public partial class TabViewModel : ITabViewModel, IDisposable
}
}
disposed = true;
_disposed = true;
}
}

View File

@@ -0,0 +1,6 @@
namespace FileTime.Core.Models;
public record ItemFilter(
string Name,
Func<IItem, bool> Filter
);

View File

@@ -1,6 +0,0 @@
namespace FileTime.Core.Models;
public record ItemsTransformator(
string Name,
Func<IEnumerable<IItem>, Task<IEnumerable<IItem>>> Transformator
);

View File

@@ -11,8 +11,8 @@ public interface ITab : IInitable<IContainer>
IObservable<IObservable<IChangeSet<IItem>>?> CurrentItems { get; }
void SetCurrentLocation(IContainer newLocation);
void AddSelectedItemsTransformator(ItemsTransformator transformator);
void RemoveSelectedItemsTransformator(ItemsTransformator transformator);
void RemoveSelectedItemsTransformatorByName(string name);
void AddItemFilter(ItemFilter filter);
void RemoveItemFilter(ItemFilter filter);
void RemoveItemFilter(string name);
void SetSelectedItem(IAbsolutePath newSelectedItem);
}

View File

@@ -1,6 +1,7 @@
using System.Reactive.Linq;
using System.Reactive.Subjects;
using DynamicData;
using DynamicData.Alias;
using FileTime.Core.Models;
namespace FileTime.Core.Services;
@@ -9,7 +10,7 @@ public class Tab : ITab
{
private readonly BehaviorSubject<IContainer?> _currentLocation = new(null);
private readonly BehaviorSubject<IAbsolutePath?> _currentSelectedItem = new(null);
private readonly List<ItemsTransformator> _transformators = new();
private readonly SourceList<ItemFilter> _itemFilters = new();
private IAbsolutePath? _currentSelectedItemCached;
public IObservable<IContainer?> CurrentLocation { get; }
@@ -21,16 +22,19 @@ public class Tab : ITab
CurrentLocation = _currentLocation.DistinctUntilChanged().Publish(null).RefCount();
CurrentItems =
Observable.Merge(
Observable.CombineLatest(
CurrentLocation
.Where(c => c is not null)
.Select(c => c!.Items)
.Switch()
.Select(items => items?.TransformAsync(MapItem)),
_itemFilters.Connect().StartWithEmpty().ToCollection(),
(items, filters) => items?.Where(i => filters.All(f => f.Filter(i)))),
CurrentLocation
.Where(c => c is null)
.Select(_ => (IObservable<IChangeSet<IItem>>?)null)
.Select(_ => (IObservable<IChangeSet<IItem>>?) null)
)
.Publish((IObservable<IChangeSet<IItem>>?)null)
.Publish((IObservable<IChangeSet<IItem>>?) null)
.RefCount();
CurrentSelectedItem =
@@ -79,9 +83,14 @@ public class Tab : ITab
public void SetSelectedItem(IAbsolutePath newSelectedItem) => _currentSelectedItem.OnNext(newSelectedItem);
public void AddSelectedItemsTransformator(ItemsTransformator transformator) => _transformators.Add(transformator);
public void RemoveSelectedItemsTransformator(ItemsTransformator transformator) => _transformators.Remove(transformator);
public void RemoveSelectedItemsTransformatorByName(string name) => _transformators.RemoveAll(t => t.Name == name);
public void AddItemFilter(ItemFilter filter) => _itemFilters.Add(filter);
public void RemoveItemFilter(ItemFilter filter) => _itemFilters.Remove(filter);
public void RemoveItemFilter(string name)
{
var itemsToRemove = _itemFilters.Items.Where(t => t.Name == name).ToList();
_itemFilters.RemoveMany(itemsToRemove);
}
public async Task OpenSelected()
{

View File

@@ -30,7 +30,8 @@ public static class Startup
.AddSingleton<IKeyInputHandlerService, KeyInputHandlerService>()
.AddSingleton<IDefaultModeKeyInputHandler, DefaultModeKeyInputHandler>()
.AddSingleton<IKeyboardConfigurationService, KeyboardConfigurationService>()
.AddSingleton<IRapidTravelModeKeyInputHandler, RapidTravelModeKeyInputHandler>();
.AddSingleton<IRapidTravelModeKeyInputHandler, RapidTravelModeKeyInputHandler>()
.AddSingleton<IModalService, ModalService>();
}
internal static IServiceCollection RegisterLogging(this IServiceCollection serviceCollection)

View File

@@ -11,6 +11,7 @@ public class KeyInputHandlerService : IKeyInputHandlerService
private readonly IGuiAppState _appState;
private readonly IDefaultModeKeyInputHandler _defaultModeKeyInputHandler;
private readonly IRapidTravelModeKeyInputHandler _rapidTravelModeKeyInputHandler;
private ViewMode _viewMode;
public KeyInputHandlerService(
IGuiAppState appState,
@@ -21,16 +22,18 @@ public class KeyInputHandlerService : IKeyInputHandlerService
_appState = appState;
_defaultModeKeyInputHandler = defaultModeKeyInputHandler;
_rapidTravelModeKeyInputHandler = rapidTravelModeKeyInputHandler;
appState.ViewMode.Subscribe(v => _viewMode = v);
}
public async Task ProcessKeyDown(Key key, KeyModifiers keyModifiers, Action<bool> setHandled)
{
if (key == Key.LeftAlt
|| key == Key.RightAlt
|| key == Key.LeftShift
|| key == Key.RightShift
|| key == Key.LeftCtrl
|| key == Key.RightCtrl)
if (key is Key.LeftAlt
or Key.RightAlt
or Key.LeftShift
or Key.RightShift
or Key.LeftCtrl
or Key.RightCtrl)
{
return;
}
@@ -43,7 +46,7 @@ public class KeyInputHandlerService : IKeyInputHandlerService
var specialKeyStatus = new SpecialKeysStatus(isAltPressed, isShiftPressed, isCtrlPressed);
if (_appState.ViewMode == ViewMode.Default)
if (_viewMode == ViewMode.Default)
{
await _defaultModeKeyInputHandler.HandleInputKey(key, specialKeyStatus, setHandled);
}

View File

@@ -49,12 +49,6 @@ public class KeyboardConfigurationService : IKeyboardConfigurationService
private static bool IsUniversal(CommandBindingConfiguration keyMapping)
{
return keyMapping.Command == Commands.GoUp
|| keyMapping.Command == Commands.Open
|| keyMapping.Command == Commands.OpenOrRun
|| keyMapping.Command == Commands.MoveCursorUp
|| keyMapping.Command == Commands.MoveCursorDown
|| keyMapping.Command == Commands.MoveCursorUpPage
|| keyMapping.Command == Commands.MoveCursorDownPage;
return keyMapping.Command is Commands.GoUp or Commands.Open or Commands.OpenOrRun or Commands.MoveCursorUp or Commands.MoveCursorDown or Commands.MoveCursorUpPage or Commands.MoveCursorDownPage;
}
}

View File

@@ -0,0 +1,20 @@
using DynamicData;
using FileTime.App.Core.Services;
using FileTime.App.Core.ViewModels;
namespace FileTime.GuiApp.Services;
public class ModalService : IModalService
{
private readonly SourceList<IModalViewModelBase> _openModals = new();
public IObservable<IChangeSet<IModalViewModelBase>> OpenModals { get; }
public ModalService()
{
OpenModals = _openModals.Connect();
}
public void OpenModal(IModalViewModelBase modalToOpen) => _openModals.Add(modalToOpen);
public void CloseModal(IModalViewModelBase modalToClose) => _openModals.Remove(modalToClose);
}

View File

@@ -1,29 +1,62 @@
using Avalonia.Input;
using FileTime.App.Core.Command;
using FileTime.App.Core.Models;
using FileTime.App.Core.Models.Enums;
using FileTime.App.Core.Services;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
using FileTime.Core.Services;
using FileTime.GuiApp.Configuration;
using FileTime.GuiApp.Extensions;
using FileTime.GuiApp.Models;
using Microsoft.Extensions.Logging;
namespace FileTime.GuiApp.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 ICommandHandlerService _commandHandlerService;
private readonly ILogger<RapidTravelModeKeyInputHandler> _logger;
private readonly BindedCollection<IModalViewModelBase> _openModals;
private ITabViewModel? _selectedTab;
public RapidTravelModeKeyInputHandler(
IAppState appState,
IModalService modalService,
IKeyboardConfigurationService keyboardConfigurationService,
ICommandHandlerService commandHandlerService,
ILogger<RapidTravelModeKeyInputHandler> logger)
{
_appState = appState;
_modalService = modalService;
_keyboardConfigurationService = keyboardConfigurationService;
_commandHandlerService = commandHandlerService;
_logger = logger;
_appState.SelectedTab.Subscribe(t => _selectedTab = t);
_openModals = new BindedCollection<IModalViewModelBase>(modalService.OpenModals);
}
public async Task HandleInputKey(Key key, SpecialKeysStatus specialKeysStatus, Action<bool> setHandled)
{
/*var keyString = key.ToString();
var keyString = key.ToString();
var updateRapidTravelFilter = false;
if (key == Key.Escape)
{
setHandled(true);
if (_appState.IsAllShortcutVisible)
if ((_openModals.Collection?.Count ?? 0) > 0)
{
_appState.IsAllShortcutVisible = false;
}
else if (_appState.MessageBoxText != null)
{
_appState.MessageBoxText = null;
_modalService.CloseModal(_openModals.Collection!.Last());
}
else
{
await _appState.ExitRapidTravelMode();
_appState.SwitchViewMode(ViewMode.Default);
}
}
else if (key == Key.Back)
@@ -43,8 +76,8 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
}
else
{
var currentKeyAsList = new List<KeyConfig>() { new KeyConfig(key) };
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => AreKeysEqual(c.Keys, currentKeyAsList));
var currentKeyAsList = new List<KeyConfig>() {new KeyConfig(key)};
var selectedCommandBinding = _keyboardConfigurationService.UniversalCommandBindings.FirstOrDefault(c => c.Keys.AreKeysEqual(currentKeyAsList));
if (selectedCommandBinding != null)
{
setHandled(true);
@@ -54,7 +87,11 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
if (updateRapidTravelFilter)
{
var currentLocation = await _appState.SelectedTab.CurrentLocation.Container.WithoutVirtualContainer(MainPageViewModel.RAPIDTRAVEL);
if (_selectedTab?.Tab is not ITab tab) return;
tab.RemoveItemFilter(RAPIDTRAVELFILTERNAME);
tab.AddItemFilter(new ItemFilter(RAPIDTRAVELFILTERNAME, i => i.Name.ToLower().Contains(_appState.RapidTravelText)));
/*var currentLocation = await _appState.SelectedTab.CurrentLocation.Container.WithoutVirtualContainer(MainPageViewModel.RAPIDTRAVEL);
var newLocation = new VirtualContainer(
currentLocation,
new List<Func<IEnumerable<IContainer>, IEnumerable<IContainer>>>()
@@ -81,7 +118,19 @@ public class RapidTravelModeKeyInputHandler : IRapidTravelModeKeyInputHandler
else if (!currentLocationItems.Select(i => i.Item.Name).Any(n => n == selectedItemName))
{
await _appState.SelectedTab.MoveCursorToFirst();
}
}*/
}
}
private async Task CallCommandAsync(Commands command)
{
try
{
await _commandHandlerService.HandleCommandAsync(command);
}
catch (Exception e)
{
_logger.LogError(e, "Unknown error while running command. {Command} {Error}", command, e);
}
}
}

View File

@@ -236,6 +236,35 @@
</Grid>
<Grid Grid.Row="3">
<Grid
IsVisible="{Binding AppState.ViewMode^, Converter={StaticResource EqualityConverter},ConverterParameter=RapidTravel}">
<Grid.RowDefinitions>
<RowDefinition Height="1" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Rectangle
Height="1"
Margin="10,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Fill="{DynamicResource ContentSeparatorBrush}" />
<StackPanel
Grid.Row="1"
Margin="30,10,10,10"
Orientation="Horizontal">
<TextBlock
Margin="0,0,30,0"
Text="Rapid travel mode" />
<TextBlock Text="Filter " />
<TextBlock Text="{Binding AppState.RapidTravelText}" />
</StackPanel>
</Grid>
<Grid
IsVisible="{Binding AppState.PossibleCommands.Count, Converter={StaticResource NotEqualsConverter}, ConverterParameter=0}">
<Grid.RowDefinitions>