From 40cb643a3212d1154a5f84dc27c83739589986da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Fri, 21 Jul 2023 23:31:33 +0200 Subject: [PATCH] RunOrOpenCommand --- .../UserCommand/RunOrOpenCommand.cs | 13 +++ .../NavigationUserCommandHandlerService.cs | 25 +++++- ...faultIdentifiableCommandHandlerRegister.cs | 1 + .../ViewModels/TabViewModel.cs | 90 +------------------ src/Core/FileTime.Core.Services/Tab.cs | 83 +---------------- .../Configuration/MainConfiguration.cs | 16 ++-- 6 files changed, 49 insertions(+), 179 deletions(-) create mode 100644 src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/RunOrOpenCommand.cs diff --git a/src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/RunOrOpenCommand.cs b/src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/RunOrOpenCommand.cs new file mode 100644 index 0000000..1e865dc --- /dev/null +++ b/src/AppCommon/FileTime.App.Core.Abstraction/UserCommand/RunOrOpenCommand.cs @@ -0,0 +1,13 @@ +namespace FileTime.App.Core.UserCommand; + +public class RunOrOpenCommand : IIdentifiableUserCommand +{ + public const string CommandName = "run_or_open"; + public static RunOrOpenCommand Instance { get; } = new(); + + private RunOrOpenCommand() + { + } + + public string UserCommandID => CommandName; +} \ No newline at end of file diff --git a/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/NavigationUserCommandHandlerService.cs b/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/NavigationUserCommandHandlerService.cs index 602dee3..53d729b 100644 --- a/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/NavigationUserCommandHandlerService.cs +++ b/src/AppCommon/FileTime.App.Core/Services/UserCommandHandler/NavigationUserCommandHandlerService.cs @@ -1,4 +1,5 @@ using System.Collections.ObjectModel; +using System.Diagnostics; using DeclarativeProperty; using FileTime.App.CommandPalette.Services; using FileTime.App.Core.Extensions; @@ -78,11 +79,31 @@ public class NavigationUserCommandHandlerService : UserCommandHandlerServiceBase new TypeUserCommandHandler(OpenCommandPalette), new TypeUserCommandHandler(OpenContainer), new TypeUserCommandHandler(OpenSelected), + new TypeUserCommandHandler(RunOrOpen), new TypeUserCommandHandler(Refresh), new TypeUserCommandHandler(SwitchToTab), }); } + private async Task RunOrOpen() + { + if (_currentSelectedItem?.Value is IContainerViewModel) + { + await OpenSelected(); + } + else if (_currentSelectedItem?.Value is IElementViewModel + {Element: {NativePath: not null, Provider: ILocalContentProvider} localFile} + ) + { + Process.Start(new ProcessStartInfo(localFile.NativePath!.Path) {UseShellExecute = true}); + + if (_viewMode == ViewMode.RapidTravel) + { + await ExitRapidTravel(); + } + } + } + private Task OpenCommandPalette() { _commandPaletteService.OpenCommandPalette(); @@ -232,10 +253,10 @@ public class NavigationUserCommandHandlerService : UserCommandHandlerServiceBase var relevantItems = _currentSelectedItem?.Value is null ? items.Reverse().ToList() : items.TakeWhile(i => !i.EqualsTo(_currentSelectedItem?.Value)).Reverse().ToList(); - + var fallBackItems = relevantItems.Take(PageSize).Reverse(); var preferredItems = relevantItems.Skip(PageSize); - + return preferredItems.Concat(fallBackItems).FirstOrDefault(); }); diff --git a/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs b/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs index 3016211..f7fd7c3 100644 --- a/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs +++ b/src/AppCommon/FileTime.App.Core/StartupServices/DefaultIdentifiableCommandHandlerRegister.cs @@ -47,6 +47,7 @@ public class DefaultIdentifiableCommandHandlerRegister : IStartupHandler AddUserCommand(PauseCommandSchedulerCommand.Instance); AddUserCommand(RefreshCommand.Instance); AddUserCommand(RenameCommand.Instance); + AddUserCommand(RunOrOpenCommand.Instance); AddUserCommand(StartCommandSchedulerCommand.Instance); AddUserCommand(IdentifiableSearchCommand.SearchByNameContains); AddUserCommand(SwitchToTabCommand.SwitchToLastTab); diff --git a/src/AppCommon/FileTime.App.Core/ViewModels/TabViewModel.cs b/src/AppCommon/FileTime.App.Core/ViewModels/TabViewModel.cs index 466b026f..56146ad 100644 --- a/src/AppCommon/FileTime.App.Core/ViewModels/TabViewModel.cs +++ b/src/AppCommon/FileTime.App.Core/ViewModels/TabViewModel.cs @@ -1,5 +1,4 @@ using System.Collections.ObjectModel; -using System.ComponentModel; using System.Reactive.Linq; using DeclarativeProperty; using DynamicData; @@ -87,22 +86,6 @@ public partial class TabViewModel : ITabViewModel () => CurrentItems.Subscribe(c => UpdateConsumer(c, ref _currentItemsConsumer)) ); - /*CurrentSelectedItem = - Observable.CombineLatest( - CurrentItems, - tab.CurrentSelectedItem, - (currentItems, currentSelectedItemPath) => - currentItems == null - ? Observable.Return((IItemViewModel?)null) - : currentItems - .ToCollection() - .Select(items => - items.FirstOrDefault(i => i.BaseItem?.FullName?.Path == currentSelectedItemPath?.Path.Path)) - ) - .Switch() - .Publish(null) - .RefCount();*/ - CurrentSelectedItem = DeclarativePropertyHelpers.CombineLatest( tab.CurrentSelectedItem, CurrentItems.Watch, IItemViewModel>(), @@ -118,17 +101,6 @@ public partial class TabViewModel : ITabViewModel ); CurrentSelectedItemAsContainer = CurrentSelectedItem.Map(i => i as IContainerViewModel); - //CurrentSelectedItem = tab.CurrentSelectedItem.Map((item, _) => Task.FromResult(CurrentItems.Value?.FirstOrDefault(i => i.BaseItem?.FullName?.Path == item?.Path.Path))); - - /*CurrentSelectedItem = - Observable.CombineLatest( - CurrentItems, - tab.CurrentSelectedItem, - (currentItems, currentSelectedItemPath) => - CurrentItemsCollection?.Collection?.FirstOrDefault(i => i.BaseItem?.FullName?.Path == currentSelectedItemPath?.Path.Path) - ) - .Publish(null) - .RefCount();*/ SelectedsChildren = CurrentSelectedItem .Debounce(TimeSpan.FromMilliseconds(200), resetTimer: true) @@ -167,62 +139,6 @@ public partial class TabViewModel : ITabViewModel ); tab.CurrentLocation.Subscribe(_ => _markedItems.Clear()).AddToDisposables(_disposables); - - /*IObservable>?> InitSelectedsChildren() - { - var currentSelectedItemThrottled = - CurrentSelectedItem.Throttle(TimeSpan.FromMilliseconds(250)).Publish(null).RefCount(); - return Observable.Merge( - currentSelectedItemThrottled - .WhereNotNull() - .OfType() - .Where(c => c?.Container is not null) - .Select(c => c.Container!.Items) - .Select(i => - i - ?.TransformAsync(MapItemAsync) - .Transform(i => MapItemToViewModel(i, ItemViewModelType.SelectedChild)) - .Sort(SortItems()) - ), - currentSelectedItemThrottled - .Where(c => c is null or not IContainerViewModel) - .Select(_ => (IObservable>?) null) - ) - /*.ObserveOn(_rxSchedulerService.GetWorkerScheduler()) - .SubscribeOn(_rxSchedulerService.GetUIScheduler())#1# - .Publish(null) - .RefCount(); - } - - IObservable>?> InitParentsChildren() - { - var parentThrottled = CurrentLocation - .Select(l => l?.Parent) - .DistinctUntilChanged() - .Publish(null) - .RefCount(); - - return Observable.Merge( - parentThrottled - .Where(p => p is not null) - .Select(p => Observable.FromAsync(async () => (IContainer) await p!.ResolveAsync())) - .Switch() - .Select(p => p.Items) - .Select(items => - items - ?.TransformAsync(MapItemAsync) - .Transform(i => MapItemToViewModel(i, ItemViewModelType.Parent)) - .Sort(SortItems()) - ), - parentThrottled - .Where(p => p is null) - .Select(_ => (IObservable>?) null) - ) - /*.ObserveOn(_rxSchedulerService.GetWorkerScheduler()) - .SubscribeOn(_rxSchedulerService.GetUIScheduler())#1# - .Publish(null) - .RefCount(); - }*/ } @@ -309,10 +225,8 @@ public partial class TabViewModel : ITabViewModel } } - public void ClearMarkedItems() - { - _markedItems.Clear(); - } + public void ClearMarkedItems() + => _markedItems.Clear(); ~TabViewModel() => Dispose(false); diff --git a/src/Core/FileTime.Core.Services/Tab.cs b/src/Core/FileTime.Core.Services/Tab.cs index 3d37c38..8f528eb 100644 --- a/src/Core/FileTime.Core.Services/Tab.cs +++ b/src/Core/FileTime.Core.Services/Tab.cs @@ -1,6 +1,4 @@ using System.Collections.ObjectModel; -using System.Collections.Specialized; -using System.Reactive.Disposables; using System.Reactive.Subjects; using DeclarativeProperty; using DynamicData; @@ -10,7 +8,6 @@ using FileTime.Core.Helper; using FileTime.Core.Models; using FileTime.Core.Timeline; using ObservableComputations; -using static System.DeferTools; namespace FileTime.Core.Services; @@ -57,19 +54,6 @@ public class Tab : ITab return Task.CompletedTask; }); - /*CurrentLocation = _currentLocation - .DistinctUntilChanged() - .Merge(_currentLocationForced) - .Do(_ => - { - if (_currentSelectedItemCached is not null) - { - LastDeepestSelectedPath = FullName.CreateSafe(PathHelper.GetLongerPath(LastDeepestSelectedPath?.Path, _currentSelectedItemCached.Path.Path)); - } - }) - .Publish(null) - .RefCount();*/ - CurrentItems = CurrentLocation.Map((container, _) => { var items = container is null @@ -77,49 +61,9 @@ public class Tab : ITab : container.Items.Selecting(i => MapItem(i)); return Task.FromResult(items); } - ) /*.Watch, IItem>()*/; - /*using var _ = Defer(() => - CurrentItems.Subscribe(c => UpdateConsumer(c, ref _currentItemsConsumer)) - );*/ + ); - /*CurrentItems.RegisterTrigger( - (sender, items) => - { - if (items is null) - return null; - - items.CollectionChanged += Handler; - - return Disposable.Create(() => items.CollectionChanged -= Handler); - - void Handler(object? o, NotifyCollectionChangedEventArgs e) - { - var t = Task.Run(async () => await sender.ReFireAsync()); - t.Wait(); - } - });*/ - - /*CurrentItems = - Observable.Merge( - Observable.CombineLatest( - CurrentLocation - .Where(c => c is not null) - .Select(c => c!.ItemsCollection) - .Select(items => items.TransformAsync(MapItem)), - _itemFilters.Connect().StartWithEmpty().ToCollection(), - (items, filters) => - //Note: Dont user Sort before where, as DynamicData cant handle - //sort in (so that's if they are before) filters - items - .Where(i => filters.All(f => f.Filter(i))) - .Sort(SortItems()) - ), - CurrentLocation - .Where(c => c is null) - .Select(_ => (IObservable>?) null) - ) - .Publish(null) - .RefCount();*/ + CurrentSelectedItem = DeclarativePropertyHelpers.CombineLatest( CurrentItems.Watch, IItem>(), _currentRequestItem.DistinctUntilChanged(), @@ -137,29 +81,6 @@ public class Tab : ITab _refreshSmoothnessCalculator.RecalculateSmoothness(); }); - - /*CurrentSelectedItem = - Observable.CombineLatest( - CurrentItems - .Select(c => - c == null - ? Observable.Return?>(null) - : c.ToCollection() - ) - .Switch(), - _currentSelectedItem, - (items, selected) => - { - if (selected != null && (items?.Any(i => i.FullName == selected.Path) ?? true)) return selected; - if (items == null || items.Count == 0) return null; - - return GetSelectedItemByItems(items); - } - ) - .DistinctUntilChanged() - .Publish(null) - .RefCount();*/ - CurrentSelectedItem.Subscribe(async (s, _) => { _currentSelectedItemCached = s; diff --git a/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs b/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs index 654c56e..5e0761c 100644 --- a/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs +++ b/src/GuiApp/Avalonia/FileTime.GuiApp.Abstractions/Configuration/MainConfiguration.cs @@ -66,12 +66,18 @@ public static class MainConfiguration new(GoToPathCommand.CommandName, new[] {Key.G, Key.P}), new(GoToProviderCommand.CommandName, new[] {Key.G, Key.T}), new(GoToRootCommand.CommandName, new[] {Key.G, Key.R}), + new(GoUpCommand.CommandName, Key.Left), new(DeleteCommand.HardDeleteCommandName, new[] {new KeyConfig(Key.D, shift: true), new KeyConfig(Key.D, shift: true)}), new(MarkCommand.CommandName, Key.Space), new(MoveCursorToLastCommand.CommandName, new KeyConfig(Key.G, shift: true)), new(MoveCursorToFirstCommand.CommandName, new[] {Key.G, Key.G}), + new(MoveCursorUpCommand.CommandName, Key.Up), + new(MoveCursorDownCommand.CommandName, Key.Down), + new(MoveCursorUpPageCommand.CommandName, Key.PageUp), + new(MoveCursorDownPageCommand.CommandName, Key.PageDown), //new CommandBindingConfiguration(ConfigCommand.NextTimelineBlock, Key.L ), //new CommandBindingConfiguration(ConfigCommand.NextTimelineCommand, Key.J ), + new(OpenSelectedCommand.CommandName, Key.Right), new(OpenCommandPaletteCommand.CommandName, new[] {Key.F1}), new(OpenCommandPaletteCommand.CommandName, new[] {new KeyConfig(Key.P, ctrl: true, shift: true)}), new(OpenInDefaultFileExplorerCommand.CommandName, new[] {Key.O, Key.E}), @@ -85,7 +91,8 @@ public static class MainConfiguration //new CommandBindingConfiguration(ConfigCommand.PreviousTimelineCommand, Key.K ), new(RefreshCommand.CommandName, Key.R), new(RenameCommand.CommandName, Key.F2), - new(RenameCommand.CommandName, new[] { Key.C, Key.W }), + new(RenameCommand.CommandName, new[] {Key.C, Key.W}), + new(RunOrOpenCommand.CommandName, Key.Enter), //new CommandBindingConfiguration(ConfigCommand.RunCommand, new KeyConfig(Key.D4, shift: true)), //new CommandBindingConfiguration(ConfigCommand.ScanContainerSize, new[] { Key.C, Key.S }), //new CommandBindingConfiguration(ConfigCommand.ShowAllShortcut, Key.F1), @@ -104,13 +111,6 @@ public static class MainConfiguration //new CommandBindingConfiguration(ConfigCommand.TimelineRefresh, new[] { Key.T, Key.R }), new(StartCommandSchedulerCommand.CommandName, new[] {Key.T, Key.S}), //new CommandBindingConfiguration(ConfigCommand.ToggleAdvancedIcons, new[] { Key.Z, Key.I }), - new(GoUpCommand.CommandName, Key.Left), - new(OpenSelectedCommand.CommandName, Key.Right), - //new CommandBindingConfiguration(ConfigCommand.OpenOrRun, Key.Enter), - new(MoveCursorUpCommand.CommandName, Key.Up), - new(MoveCursorDownCommand.CommandName, Key.Down), - new(MoveCursorUpPageCommand.CommandName, Key.PageUp), - new(MoveCursorDownPageCommand.CommandName, Key.PageDown), }; private static void PopulateDefaultEditorPrograms(Dictionary configuration)