Clipboard

This commit is contained in:
2022-05-07 18:58:12 +02:00
parent 60ab58659e
commit b161ded92e
16 changed files with 214 additions and 33 deletions

View File

@@ -0,0 +1,51 @@
using FileTime.Core.Command;
using FileTime.Core.Models;
namespace FileTime.App.Core.Services
{
public class ClipboardService : IClipboardService
{
private List<IAbsolutePath> _content;
public IReadOnlyList<IAbsolutePath> Content { get; private set; }
public Type? CommandType { get; private set; }
public ClipboardService()
{
_content = new List<IAbsolutePath>();
Content = _content.AsReadOnly();
}
public void AddContent(IAbsolutePath absolutePath)
{
foreach (var content in _content)
{
if (content.Equals(absolutePath)) return;
}
_content.Add(absolutePath);
}
public void RemoveContent(IAbsolutePath absolutePath)
{
for (var i = 0; i < _content.Count; i++)
{
if (_content[i].Equals(absolutePath))
{
_content.RemoveAt(i--);
}
}
}
public void Clear()
{
_content = new List<IAbsolutePath>();
Content = _content.AsReadOnly();
CommandType = null;
}
public void SetCommand<T>() where T : ITransportationCommand
{
CommandType = typeof(T);
}
}
}

View File

@@ -1,16 +1,17 @@
using System.Reactive.Linq;
using DynamicData;
using FileTime.App.Core.Command;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Models;
namespace FileTime.App.Core.Services.CommandHandler
{
public abstract class CommandHanderBase : ICommandHandler
public abstract class CommandHandlerBase : ICommandHandler
{
private readonly Dictionary<Commands, Func<Task>> _commandHandlers = new();
private readonly IAppState? _appState;
protected CommandHanderBase(IAppState? appState = null)
protected CommandHandlerBase(IAppState? appState = null)
{
_appState = appState;
}
@@ -40,9 +41,12 @@ namespace FileTime.App.Core.Services.CommandHandler
protected IDisposable SaveCurrentItems(Action<IEnumerable<IItemViewModel>> handler)
=> RunWithAppState(appState => appState.SelectedTab.Select(t => t?.CurrentItemsCollectionObservable ?? Observable.Return((IEnumerable<IItemViewModel>?)Enumerable.Empty<IItemViewModel>())).Switch().Subscribe(i => handler(i ?? Enumerable.Empty<IItemViewModel>())));
protected IDisposable SaveMarkedItems(Action<IChangeSet<IAbsolutePath>> handler)
=> RunWithAppState(appstate => appstate.SelectedTab.Select(t => t == null ? Observable.Empty<IChangeSet<IAbsolutePath>>() : t.MarkedItems).Switch().Subscribe(handler));
private IDisposable RunWithAppState(Func<IAppState, IDisposable> act)
{
if (_appState == null) throw new NullReferenceException($"AppState is nit initialized in {nameof(CommandHanderBase)}.");
if (_appState == null) throw new NullReferenceException($"AppState is nit initialized in {nameof(CommandHandlerBase)}.");
return act(_appState);
}

View File

@@ -1,24 +1,44 @@
using DynamicData;
using FileTime.App.Core.Command;
using FileTime.App.Core.Models;
using FileTime.App.Core.ViewModels;
using FileTime.Core.Command;
using FileTime.Core.Command.Copy;
using FileTime.Core.Models;
namespace FileTime.App.Core.Services.CommandHandler
{
public class ItemManipulationCommandHandler : CommandHanderBase
public class ItemManipulationCommandHandler : CommandHandlerBase
{
private ITabViewModel? _selectedTab;
private IItemViewModel? _currentSelectedItem;
private readonly ICommandHandlerService _commandHandlerService;
private readonly IClipboardService _clipboardService;
private BindedCollection<IAbsolutePath>? _markedItems;
public ItemManipulationCommandHandler(IAppState appState, ICommandHandlerService commandHandlerService) : base(appState)
public ItemManipulationCommandHandler(
IAppState appState,
ICommandHandlerService commandHandlerService,
IClipboardService clipboardService) : base(appState)
{
_commandHandlerService = commandHandlerService;
_clipboardService = clipboardService;
SaveSelectedTab(t => _selectedTab = t);
SaveSelectedTab(t =>
{
_selectedTab = t;
_markedItems?.Dispose();
_markedItems = t == null ? null : new BindedCollection<IAbsolutePath>(t.MarkedItems);
});
SaveCurrentSelectedItem(i => _currentSelectedItem = i);
AddCommandHandlers(new (Commands, Func<Task>)[]
{
(Commands.Mark, MarkItem)
(Commands.Copy, Copy),
(Commands.Mark, MarkItem),
(Commands.PasteMerge, PasteMerge),
(Commands.PasteOverwrite, PasteOverwrite),
(Commands.PasteSkip, PasteSkip),
});
}
@@ -26,8 +46,51 @@ namespace FileTime.App.Core.Services.CommandHandler
{
if (_selectedTab == null || _currentSelectedItem?.BaseItem?.FullName == null) return;
_selectedTab.ToggleMarkedItem(_currentSelectedItem.BaseItem.FullName);
_selectedTab.ToggleMarkedItem(new AbsolutePath(_currentSelectedItem.BaseItem));
await _commandHandlerService.HandleCommandAsync(Commands.MoveCursorDown);
}
private Task Copy()
{
_clipboardService.Clear();
_clipboardService.SetCommand<CopyCommand>();
if ((_markedItems?.Collection.Count ?? 0) > 0)
{
foreach (var item in _markedItems!.Collection)
{
_clipboardService.AddContent(item);
}
_selectedTab?.ClearMarkedItems();
}
else if (_currentSelectedItem?.BaseItem != null)
{
_clipboardService.AddContent(new AbsolutePath(_currentSelectedItem.BaseItem));
}
return Task.CompletedTask;
}
private async Task PasteMerge()
{
await Paste(TransportMode.Merge);
}
private async Task PasteOverwrite()
{
await Paste(TransportMode.Overwrite);
}
private async Task PasteSkip()
{
await Paste(TransportMode.Skip);
}
private Task Paste(TransportMode skip)
{
if (_clipboardService.CommandType is null) return Task.CompletedTask;
return Task.CompletedTask;
}
}
}

View File

@@ -6,7 +6,7 @@ using FileTime.Core.Models;
namespace FileTime.App.Core.Services.CommandHandler
{
public class NavigationCommandHandler : CommandHanderBase
public class NavigationCommandHandler : CommandHandlerBase
{
private ITabViewModel? _selectedTab;
private IContainer? _currentLocation;