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

@@ -77,7 +77,8 @@ public static class MainConfiguration
new(PasteCommand.PasteMergeCommandName, new[] {Key.P, Key.P}),
new(PasteCommand.PasteOverwriteCommandName, new[] {Key.P, Key.O}),
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.PreviousTimelineBlock, Key.H ),
//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.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.ReactiveUI;
using Serilog;
@@ -23,7 +26,7 @@ public static class Program
InitRelease();
#endif
InitLogging();
Log.Logger.Information("Early app starting...");
}
@@ -71,7 +74,7 @@ public static class Program
var logFolder = Path.Combine(AppDataRoot, "logs", "bootstrap");
if (!Directory.Exists(logFolder)) Directory.CreateDirectory(logFolder);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.Enrich.FromLogContext()
@@ -89,6 +92,9 @@ public static class Program
[STAThread]
public static void Main(string[] args)
{
AppDomain.CurrentDomain.FirstChanceException -= OnFirstChanceException;
AppDomain.CurrentDomain.UnhandledException -= OnAppDomainUnhandledException;
TaskScheduler.UnobservedTaskException -= OnTaskSchedulerUnobservedTaskException;
try
{
BuildAvaloniaApp()
@@ -110,4 +116,22 @@ public static class Program
.UsePlatformDetect()
.UseReactiveUI()
.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.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.GuiApp.Services;
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)
{
var clipboard = TopLevel?.Clipboard;
var clipboard = UiAccessor.GetTopLevel()?.Clipboard;
if (clipboard is null) { return; }
if (clipboard is null)
{
return;
}
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 (obj is IEnumerable<IStorageItem> storageItems)
{
return storageItems
.Select(i => _timelessContentProvider.GetFullNameByNativePath(new NativePath(WebUtility.UrlDecode(i.Path.AbsolutePath))))
.Where(i => i != null)
.OfType<FullName>();
}
if (!formats.Contains("asd")) return;
var obj = (await clipboard.GetDataAsync("PNG"));
/*var ms = new MemoryStream();
Serializer.Serialize(ms, obj);
byte[] data = ms.ToArray().Skip(4).ToArray();
ms = new MemoryStream(data);*/
;
return Enumerable.Empty<FullName>();
}
}

View File

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