Paste from clipboard, logging

This commit is contained in:
2023-07-06 20:39:53 +02:00
parent 7afce07d21
commit 5c716d5c28
15 changed files with 156 additions and 53 deletions

View File

@@ -1,7 +1,9 @@
using FileTime.Core.Models;
namespace FileTime.App.Core.Services; namespace FileTime.App.Core.Services;
public interface ISystemClipboardService public interface ISystemClipboardService
{ {
Task CopyToClipboardAsync(string text); Task CopyToClipboardAsync(string text);
Task GetFiles(); Task<IEnumerable<FullName>> GetFiles();
} }

View File

@@ -5,8 +5,12 @@ namespace FileTime.App.Core.UserCommand;
public class PasteFilesFromClipboardCommand : IIdentifiableUserCommand public class PasteFilesFromClipboardCommand : IIdentifiableUserCommand
{ {
public const string PasteMergeCommandName = "paste_clipboard_merge"; public const string PasteMergeCommandName = "paste_clipboard_merge";
public const string PasteOverwriteCommandName = "paste_clipboard_overwrite";
public const string PasteSkipCommandName = "paste_clipboard_skip";
public static readonly PasteFilesFromClipboardCommand Merge = new(PasteMode.Merge, PasteMergeCommandName); public static readonly PasteFilesFromClipboardCommand Merge = new(PasteMode.Merge, PasteMergeCommandName);
public static readonly PasteFilesFromClipboardCommand Overwrite = new(PasteMode.Overwrite, PasteOverwriteCommandName);
public static readonly PasteFilesFromClipboardCommand Skip = new(PasteMode.Skip, PasteSkipCommandName);
public PasteMode PasteMode { get; } public PasteMode PasteMode { get; }
private PasteFilesFromClipboardCommand(PasteMode pasteMode, string commandName) private PasteFilesFromClipboardCommand(PasteMode pasteMode, string commandName)

View File

@@ -64,23 +64,38 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
AddCommandHandlers(new IUserCommandHandler[] AddCommandHandlers(new IUserCommandHandler[]
{ {
new TypeUserCommandHandler<CopyCommand>(Copy), new TypeUserCommandHandler<CopyCommand>(CopyAsync),
new TypeUserCommandHandler<DeleteCommand>(Delete), new TypeUserCommandHandler<DeleteCommand>(DeleteAsync),
new TypeUserCommandHandler<RenameCommand>(Rename), new TypeUserCommandHandler<RenameCommand>(RenameAsync),
new TypeUserCommandHandler<MarkCommand>(MarkItem), new TypeUserCommandHandler<MarkCommand>(MarkItemAsync),
new TypeUserCommandHandler<PasteCommand>(Paste), new TypeUserCommandHandler<PasteCommand>(PasteAsync),
new TypeUserCommandHandler<CreateContainer>(CreateContainer), new TypeUserCommandHandler<CreateContainer>(CreateContainerAsync),
new TypeUserCommandHandler<CreateElement>(CreateElement), new TypeUserCommandHandler<CreateElement>(CreateElementAsync),
new TypeUserCommandHandler<PasteFilesFromClipboardCommand>(PasteFilesFromClipboard), new TypeUserCommandHandler<PasteFilesFromClipboardCommand>(PasteFilesFromClipboardAsync),
}); });
} }
private async Task PasteFilesFromClipboard(PasteFilesFromClipboardCommand arg) private async Task PasteFilesFromClipboardAsync(PasteFilesFromClipboardCommand command) =>
await (command.PasteMode switch
{ {
await _systemClipboardService.GetFiles(); PasteMode.Merge => PasteFilesFromClipboardAsync(TransportMode.Merge),
PasteMode.Overwrite => PasteFilesFromClipboardAsync(TransportMode.Overwrite),
PasteMode.Skip => PasteFilesFromClipboardAsync(TransportMode.Skip),
_ => throw new ArgumentException($"Unknown {nameof(PasteMode)} value: {command.PasteMode}")
});
private async Task PasteFilesFromClipboardAsync(TransportMode mode)
{
if (_currentLocation?.FullName is not { }) return;
var files = (await _systemClipboardService.GetFiles()).ToList();
var copyCommandFactory = _serviceProvider.GetRequiredService<FileTime.Core.Command.Copy.CopyCommandFactory>();
var copyCommand = copyCommandFactory.GenerateCommand(files, mode, _currentLocation.FullName);
await AddCommandAsync(copyCommand);
} }
private async Task MarkItem() private async Task MarkItemAsync()
{ {
if (_selectedTab == null || _currentSelectedItem?.BaseItem?.FullName == null) return; if (_selectedTab == null || _currentSelectedItem?.BaseItem?.FullName == null) return;
@@ -88,7 +103,7 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
await _userCommandHandlerService.HandleCommandAsync(MoveCursorDownCommand.Instance); await _userCommandHandlerService.HandleCommandAsync(MoveCursorDownCommand.Instance);
} }
private Task Copy() private Task CopyAsync()
{ {
_clipboardService.Clear(); _clipboardService.Clear();
_clipboardService.SetCommand<FileTime.Core.Command.Copy.CopyCommandFactory>(); _clipboardService.SetCommand<FileTime.Core.Command.Copy.CopyCommandFactory>();
@@ -114,18 +129,16 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
return Task.CompletedTask; return Task.CompletedTask;
} }
private async Task Paste(PasteCommand command) private async Task PasteAsync(PasteCommand command) =>
{
await (command.PasteMode switch await (command.PasteMode switch
{ {
PasteMode.Merge => Paste(TransportMode.Merge), PasteMode.Merge => PasteAsync(TransportMode.Merge),
PasteMode.Overwrite => Paste(TransportMode.Overwrite), PasteMode.Overwrite => PasteAsync(TransportMode.Overwrite),
PasteMode.Skip => Paste(TransportMode.Skip), PasteMode.Skip => PasteAsync(TransportMode.Skip),
_ => throw new ArgumentException($"Unknown {nameof(PasteMode)} value: {command.PasteMode}") _ => throw new ArgumentException($"Unknown {nameof(PasteMode)} value: {command.PasteMode}")
}); });
}
private async Task Paste(TransportMode mode) private async Task PasteAsync(TransportMode mode)
{ {
if (_clipboardService.CommandFactoryType is null) if (_clipboardService.CommandFactoryType is null)
{ {
@@ -141,10 +154,10 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
if (command is IRequireInputCommand requireInput) await requireInput.ReadInputs(); if (command is IRequireInputCommand requireInput) await requireInput.ReadInputs();
await AddCommand(command); await AddCommandAsync(command);
} }
private async Task CreateContainer() private async Task CreateContainerAsync()
{ {
var containerNameInput = new TextInputElement("Container name"); var containerNameInput = new TextInputElement("Container name");
@@ -158,10 +171,10 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
var command = _serviceProvider var command = _serviceProvider
.GetInitableResolver(_currentLocation.FullName, newContainerName) .GetInitableResolver(_currentLocation.FullName, newContainerName)
.GetRequiredService<CreateContainerCommand>(); .GetRequiredService<CreateContainerCommand>();
await AddCommand(command); await AddCommandAsync(command);
} }
private async Task CreateElement() private async Task CreateElementAsync()
{ {
var containerNameInput = new TextInputElement("Element name"); var containerNameInput = new TextInputElement("Element name");
@@ -175,10 +188,10 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
var command = _serviceProvider var command = _serviceProvider
.GetInitableResolver(_currentLocation.FullName, newContainerName) .GetInitableResolver(_currentLocation.FullName, newContainerName)
.GetRequiredService<CreateElementCommand>(); .GetRequiredService<CreateElementCommand>();
await AddCommand(command); await AddCommandAsync(command);
} }
private async Task Rename(RenameCommand command) private async Task RenameAsync(RenameCommand command)
{ {
List<ItemToMove> itemsToMove = new(); List<ItemToMove> itemsToMove = new();
if ((_markedItems?.Collection?.Count ?? 0) > 0) if ((_markedItems?.Collection?.Count ?? 0) > 0)
@@ -331,7 +344,7 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
//TODO: check if the name changed //TODO: check if the name changed
var moveCommandFactory = _serviceProvider.GetRequiredService<MoveCommandFactory>(); var moveCommandFactory = _serviceProvider.GetRequiredService<MoveCommandFactory>();
var moveCommand = moveCommandFactory.GenerateCommand(itemsToMove); var moveCommand = moveCommandFactory.GenerateCommand(itemsToMove);
await AddCommand(moveCommand); await AddCommandAsync(moveCommand);
} }
static IEnumerable<ItemNamePart> GetItemNameParts(Regex templateRegex, string originalName, string newNameSchema) static IEnumerable<ItemNamePart> GetItemNameParts(Regex templateRegex, string originalName, string newNameSchema)
@@ -368,7 +381,7 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
} }
} }
private async Task Delete(DeleteCommand command) private async Task DeleteAsync(DeleteCommand command)
{ {
IList<FullName>? itemsToDelete = null; IList<FullName>? itemsToDelete = null;
var shouldDelete = false; var shouldDelete = false;
@@ -419,12 +432,12 @@ public class ItemManipulationUserCommandHandlerService : UserCommandHandlerServi
var deleteCommand = _serviceProvider.GetRequiredService<FileTime.Core.Command.Delete.DeleteCommand>(); var deleteCommand = _serviceProvider.GetRequiredService<FileTime.Core.Command.Delete.DeleteCommand>();
deleteCommand.HardDelete = command.IsHardDelete; deleteCommand.HardDelete = command.IsHardDelete;
deleteCommand.ItemsToDelete.AddRange(itemsToDelete!); deleteCommand.ItemsToDelete.AddRange(itemsToDelete!);
await AddCommand(deleteCommand); await AddCommandAsync(deleteCommand);
_selectedTab?.ClearMarkedItems(); _selectedTab?.ClearMarkedItems();
} }
private async Task AddCommand(ICommand command) private async Task AddCommandAsync(ICommand command)
{ {
await _commandScheduler.AddCommand(command); await _commandScheduler.AddCommand(command);
} }

View File

@@ -41,6 +41,8 @@ public class DefaultIdentifiableCommandHandlerRegister : IStartupHandler
AddUserCommand(PasteCommand.Overwrite); AddUserCommand(PasteCommand.Overwrite);
AddUserCommand(PasteCommand.Skip); AddUserCommand(PasteCommand.Skip);
AddUserCommand(PasteFilesFromClipboardCommand.Merge); AddUserCommand(PasteFilesFromClipboardCommand.Merge);
AddUserCommand(PasteFilesFromClipboardCommand.Overwrite);
AddUserCommand(PasteFilesFromClipboardCommand.Skip);
AddUserCommand(PauseCommandSchedulerCommand.Instance); AddUserCommand(PauseCommandSchedulerCommand.Instance);
AddUserCommand(RefreshCommand.Instance); AddUserCommand(RefreshCommand.Instance);
AddUserCommand(RenameCommand.Instance); AddUserCommand(RenameCommand.Instance);

View File

@@ -22,10 +22,11 @@ public class SearchContentProvider : ContentProviderBase, ISearchContentProvider
AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown, AbsolutePathType forceResolvePathType = AbsolutePathType.Unknown,
ItemInitializationSettings itemInitializationSettings = default ItemInitializationSettings itemInitializationSettings = default
) => ) =>
Task.FromResult((IItem)_searchManager.SearchTasks Task.FromResult((IItem) _searchManager.SearchTasks
.First(searchTask => searchTask.SearchContainer.NativePath == nativePath).SearchContainer); .First(searchTask => searchTask.SearchContainer.NativePath == nativePath).SearchContainer);
public override NativePath GetNativePath(FullName fullName) => new(fullName.Path); public override NativePath GetNativePath(FullName fullName) => new(fullName.Path);
public override FullName GetFullName(NativePath nativePath) => new(nativePath.Path);
public override Task<byte[]?> GetContentAsync( public override Task<byte[]?> GetContentAsync(
IElement element, IElement element,

View File

@@ -23,6 +23,7 @@ public interface IContentProvider : IContainer, IOnContainerEnter
ItemInitializationSettings itemInitializationSettings = default); ItemInitializationSettings itemInitializationSettings = default);
NativePath GetNativePath(FullName fullName); NativePath GetNativePath(FullName fullName);
FullName GetFullName(NativePath nativePath);
Task<byte[]?> GetContentAsync(IElement element, int? maxLength = null, CancellationToken cancellationToken = default); Task<byte[]?> GetContentAsync(IElement element, int? maxLength = null, CancellationToken cancellationToken = default);
bool CanHandlePath(NativePath path); bool CanHandlePath(NativePath path);

View File

@@ -15,4 +15,5 @@ public interface ITimelessContentProvider
ItemInitializationSettings itemInitializationSettings = default); ItemInitializationSettings itemInitializationSettings = default);
Task<IItem?> GetItemByNativePathAsync(NativePath nativePath, PointInTime? pointInTime = null); Task<IItem?> GetItemByNativePathAsync(NativePath nativePath, PointInTime? pointInTime = null);
FullName? GetFullNameByNativePath(NativePath nativePath);
} }

View File

@@ -88,6 +88,7 @@ public abstract class ContentProviderBase : IContentProvider
ItemInitializationSettings itemInitializationSettings = default); ItemInitializationSettings itemInitializationSettings = default);
public abstract NativePath GetNativePath(FullName fullName); public abstract NativePath GetNativePath(FullName fullName);
public abstract FullName GetFullName(NativePath nativePath);
public abstract Task<byte[]?> GetContentAsync(IElement element, public abstract Task<byte[]?> GetContentAsync(IElement element,
int? maxLength = null, int? maxLength = null,

View File

@@ -44,4 +44,16 @@ public class TimelessContentProvider : ITimelessContentProvider
return null; return null;
} }
public FullName? GetFullNameByNativePath(NativePath nativePath)
{
foreach (var contentProvider in _contentProviderRegistry.ContentProviders)
{
if(!contentProvider.CanHandlePath(nativePath)) continue;
return contentProvider.GetFullName(nativePath);
}
return null;
}
} }

View File

@@ -77,7 +77,8 @@ public static class MainConfiguration
new(PasteCommand.PasteMergeCommandName, new[] {Key.P, Key.P}), new(PasteCommand.PasteMergeCommandName, new[] {Key.P, Key.P}),
new(PasteCommand.PasteOverwriteCommandName, new[] {Key.P, Key.O}), new(PasteCommand.PasteOverwriteCommandName, new[] {Key.P, Key.O}),
new(PasteCommand.PasteSkipCommandName, new[] {Key.P, Key.S}), new(PasteCommand.PasteSkipCommandName, new[] {Key.P, Key.S}),
new(PasteFilesFromClipboardCommand.PasteMergeCommandName, new[] {Key.C, Key.X}), new(PasteFilesFromClipboardCommand.PasteMergeCommandName, new[] {new KeyConfig(Key.V, ctrl: true)}),
new(PasteFilesFromClipboardCommand.PasteOverwriteCommandName, new[] {new KeyConfig(Key.V, ctrl: true, shift: true)}),
//new CommandBindingConfiguration(ConfigCommand.PinFavorite, new[] { Key.F, Key.P }), //new CommandBindingConfiguration(ConfigCommand.PinFavorite, new[] { Key.F, Key.P }),
//new CommandBindingConfiguration(ConfigCommand.PreviousTimelineBlock, Key.H ), //new CommandBindingConfiguration(ConfigCommand.PreviousTimelineBlock, Key.H ),
//new CommandBindingConfiguration(ConfigCommand.PreviousTimelineCommand, Key.K ), //new CommandBindingConfiguration(ConfigCommand.PreviousTimelineCommand, Key.K ),

View File

@@ -0,0 +1,10 @@
using Avalonia.Controls;
namespace FileTime.GuiApp.Services;
public interface IUiAccessor
{
public TopLevel? GetTopLevel();
Task InvokeOnUIThread(Func<Task> func);
Task<T> InvokeOnUIThread<T>(Func<Task<T>> func);
}

View File

@@ -3,6 +3,9 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Avalonia; using Avalonia;
using Avalonia.ReactiveUI; using Avalonia.ReactiveUI;
using Serilog; using Serilog;
@@ -89,6 +92,9 @@ public static class Program
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
AppDomain.CurrentDomain.FirstChanceException -= OnFirstChanceException;
AppDomain.CurrentDomain.UnhandledException -= OnAppDomainUnhandledException;
TaskScheduler.UnobservedTaskException -= OnTaskSchedulerUnobservedTaskException;
try try
{ {
BuildAvaloniaApp() BuildAvaloniaApp()
@@ -110,4 +116,22 @@ public static class Program
.UsePlatformDetect() .UsePlatformDetect()
.UseReactiveUI() .UseReactiveUI()
.LogToTrace(); .LogToTrace();
private static void OnTaskSchedulerUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e)
=> HandleUnhandledException(sender, e.Exception);
private static void OnAppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
=> HandleUnhandledException(sender, e.ExceptionObject as Exception);
private static void OnFirstChanceException(object? sender, FirstChanceExceptionEventArgs e)
=> HandleUnhandledException(sender, e.Exception);
private static void HandleUnhandledException(object? sender, Exception? ex, [CallerMemberName] string caller = "")
=> Log.Fatal(
ex,
"An unhandled exception come from '{Caller}' exception handler from an object of type '{Type}' and value '{Value}': {Exception}",
caller,
sender?.GetType().ToString() ?? "null",
sender?.ToString() ?? "null",
ex);
} }

View File

@@ -1,35 +1,58 @@
using Avalonia.Controls; using System.Net;
using System.Text.Encodings.Web;
using Avalonia.Platform.Storage;
using FileTime.App.Core.Services; using FileTime.App.Core.Services;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.GuiApp.Services; namespace FileTime.GuiApp.Services;
public class SystemClipboardService : ISystemClipboardService public class SystemClipboardService : ISystemClipboardService
{ {
internal TopLevel? TopLevel { get; set; } private const string ClipboardContentFiles = "Files";
private readonly ITimelessContentProvider _timelessContentProvider;
public IUiAccessor UiAccessor { get; internal set; }
public SystemClipboardService(ITimelessContentProvider timelessContentProvider)
{
_timelessContentProvider = timelessContentProvider;
}
public async Task CopyToClipboardAsync(string text) public async Task CopyToClipboardAsync(string text)
{ {
var clipboard = TopLevel?.Clipboard; var clipboard = UiAccessor.GetTopLevel()?.Clipboard;
if (clipboard is null) { return; } if (clipboard is null)
{
return;
}
await clipboard.SetTextAsync(text); await clipboard.SetTextAsync(text);
} }
public async Task GetFiles()
public async Task<IEnumerable<FullName>> GetFiles()
{ {
var clipboard = TopLevel?.Clipboard; var clipboard = UiAccessor.GetTopLevel()?.Clipboard;
if (clipboard is null) { return; } if (clipboard is null)
{
return Enumerable.Empty<FullName>();
}
await clipboard.ClearAsync(); var formats = await UiAccessor.InvokeOnUIThread(async () => await clipboard.GetFormatsAsync());
var formats = await clipboard.GetFormatsAsync(); if (!formats.Contains(ClipboardContentFiles)) return Enumerable.Empty<FullName>();
var obj = await clipboard.GetDataAsync(ClipboardContentFiles);
if (!formats.Contains("asd")) return; if (obj is IEnumerable<IStorageItem> storageItems)
var obj = (await clipboard.GetDataAsync("PNG")); {
/*var ms = new MemoryStream(); return storageItems
Serializer.Serialize(ms, obj); .Select(i => _timelessContentProvider.GetFullNameByNativePath(new NativePath(WebUtility.UrlDecode(i.Path.AbsolutePath))))
byte[] data = ms.ToArray().Skip(4).ToArray(); .Where(i => i != null)
ms = new MemoryStream(data);*/ .OfType<FullName>();
; }
return Enumerable.Empty<FullName>();
} }
} }

View File

@@ -1,19 +1,21 @@
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Threading;
using Avalonia.VisualTree; using Avalonia.VisualTree;
using DynamicData; using DynamicData;
using FileTime.App.Core.Services; using FileTime.App.Core.Services;
using FileTime.App.Core.ViewModels; using FileTime.App.Core.ViewModels;
using FileTime.Core.Models; using FileTime.Core.Models;
using FileTime.GuiApp.Models; using FileTime.GuiApp.Models;
using FileTime.GuiApp.Services;
using FileTime.GuiApp.ViewModels; using FileTime.GuiApp.ViewModels;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace FileTime.GuiApp.Views; namespace FileTime.GuiApp.Views;
public partial class MainWindow : Window public partial class MainWindow : Window, IUiAccessor
{ {
private readonly ILogger<MainWindow>? _logger; private readonly ILogger<MainWindow>? _logger;
private readonly IModalService _modalService; private readonly IModalService _modalService;
@@ -39,7 +41,7 @@ public partial class MainWindow : Window
_logger?.LogInformation($"Starting {nameof(MainWindow)} initialization..."); _logger?.LogInformation($"Starting {nameof(MainWindow)} initialization...");
_modalService = DI.ServiceProvider.GetRequiredService<IModalService>(); _modalService = DI.ServiceProvider.GetRequiredService<IModalService>();
_modalService.OpenModals.ToCollection().Subscribe(m => _openModals = m); _modalService.OpenModals.ToCollection().Subscribe(m => _openModals = m);
DI.ServiceProvider.GetRequiredService<Services.SystemClipboardService>().TopLevel = GetTopLevel(this); DI.ServiceProvider.GetRequiredService<Services.SystemClipboardService>().UiAccessor = this;
InitializeComponent(); InitializeComponent();
ReadInputContainer.PropertyChanged += ReadInputContainerOnPropertyChanged; ReadInputContainer.PropertyChanged += ReadInputContainerOnPropertyChanged;
@@ -156,4 +158,10 @@ public partial class MainWindow : Window
_inputViewModel = null; _inputViewModel = null;
} }
} }
public TopLevel? GetTopLevel() => GetTopLevel(this);
public async Task InvokeOnUIThread(Func<Task> func) => await Dispatcher.UIThread.InvokeAsync(func);
public async Task<T> InvokeOnUIThread<T>(Func<Task<T>> func) => await Dispatcher.UIThread.InvokeAsync(func);
} }

View File

@@ -338,7 +338,7 @@ public sealed partial class LocalContentProvider : ContentProviderBase, ILocalCo
private FullName GetFullName(DirectoryInfo directoryInfo) => GetFullName(directoryInfo.FullName); private FullName GetFullName(DirectoryInfo directoryInfo) => GetFullName(directoryInfo.FullName);
private FullName GetFullName(FileInfo fileInfo) => GetFullName(fileInfo.FullName); private FullName GetFullName(FileInfo fileInfo) => GetFullName(fileInfo.FullName);
private FullName GetFullName(NativePath nativePath) => GetFullName(nativePath.Path); public override FullName GetFullName(NativePath nativePath) => GetFullName(nativePath.Path);
private FullName GetFullName(string nativePath) => private FullName GetFullName(string nativePath) =>
FullName.CreateSafe((Name + Constants.SeparatorChar + FullName.CreateSafe((Name + Constants.SeparatorChar +