From 2110b185d205ab43e988c36292c2070175cba6b3 Mon Sep 17 00:00:00 2001 From: "waffle.lord" Date: Sun, 9 Jun 2024 16:14:57 -0400 Subject: [PATCH] add copy to clipboard button --- .idea/.idea.SPTInstaller/.idea/avalonia.xml | 1 + SPTInstaller/App.axaml.cs | 12 +++--- SPTInstaller/SPTInstaller.csproj | 4 +- SPTInstaller/ViewModels/MessageViewModel.cs | 45 +++++++++++++++++++++ SPTInstaller/Views/MessageView.axaml | 8 +++- 5 files changed, 59 insertions(+), 11 deletions(-) diff --git a/.idea/.idea.SPTInstaller/.idea/avalonia.xml b/.idea/.idea.SPTInstaller/.idea/avalonia.xml index 6f7ffd1..c49db03 100644 --- a/.idea/.idea.SPTInstaller/.idea/avalonia.xml +++ b/.idea/.idea.SPTInstaller/.idea/avalonia.xml @@ -20,6 +20,7 @@ + diff --git a/SPTInstaller/App.axaml.cs b/SPTInstaller/App.axaml.cs index 038fcd6..18582ce 100644 --- a/SPTInstaller/App.axaml.cs +++ b/SPTInstaller/App.axaml.cs @@ -12,7 +12,7 @@ namespace SPTInstaller; public partial class App : Application { - private readonly string _logPath = Path.Join(Environment.CurrentDirectory, "spt-installer_.log"); + public static string LogPath = Path.Join(Environment.CurrentDirectory, "spt-installer.log"); public override void Initialize() { @@ -21,9 +21,8 @@ public partial class App : Application Log.Logger = new LoggerConfiguration() .MinimumLevel.Information() .WriteTo - .File(path: _logPath, - restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information, - rollingInterval: RollingInterval.Day) + .File(path: LogPath, + restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information) .CreateLogger(); RxApp.DefaultExceptionHandler = Observer.Create((exception) => @@ -42,9 +41,8 @@ public partial class App : Application Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo - .File(path: _logPath, - restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug, - rollingInterval: RollingInterval.Day) + .File(path: LogPath, + restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug) .CreateLogger(); System.Diagnostics.Trace.Listeners.Add(new SerilogTraceListener.SerilogTraceListener()); diff --git a/SPTInstaller/SPTInstaller.csproj b/SPTInstaller/SPTInstaller.csproj index cc02ac7..960f8e6 100644 --- a/SPTInstaller/SPTInstaller.csproj +++ b/SPTInstaller/SPTInstaller.csproj @@ -10,8 +10,8 @@ icon.ico Assets\spt_installer.ico Debug;Release;TEST - 2.70 - 2.70 + 2.71 + 2.71 SPT diff --git a/SPTInstaller/ViewModels/MessageViewModel.cs b/SPTInstaller/ViewModels/MessageViewModel.cs index fd6947f..dfb686c 100644 --- a/SPTInstaller/ViewModels/MessageViewModel.cs +++ b/SPTInstaller/ViewModels/MessageViewModel.cs @@ -6,6 +6,9 @@ using SPTInstaller.Helpers; using SPTInstaller.Interfaces; using System.Threading.Tasks; using System.Windows.Input; +using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Input; +using Avalonia.Platform.Storage; namespace SPTInstaller.ViewModels; @@ -43,6 +46,47 @@ public class MessageViewModel : ViewModelBase set => this.RaiseAndSetIfChanged(ref _cacheInfoText, value); } + private string _clipCommandText; + + public string ClipCommandText + { + get => _clipCommandText; + set => this.RaiseAndSetIfChanged(ref _clipCommandText, value); + } + + public ICommand CopyLogFileToClipboard => ReactiveCommand.CreateFromTask(async () => + { + if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) + { + try + { + if (desktop.MainWindow?.Clipboard == null) + { + ClipCommandText = "Could not get clipboard :("; + return; + } + + var dataObject = new DataObject(); + var logFile = await desktop.MainWindow.StorageProvider.TryGetFileFromPathAsync(App.LogPath); + + if (logFile == null) + { + ClipCommandText = "Could not get log file :("; + return; + } + + dataObject.Set(DataFormats.Files, new[] {logFile}); + + await desktop.MainWindow.Clipboard.SetDataObjectAsync(dataObject); + ClipCommandText = "Copied!"; + } + catch (Exception ex) + { + ClipCommandText = ex.Message; + } + } + }); + private StatusSpinner.SpinnerState _cacheCheckState; public StatusSpinner.SpinnerState CacheCheckState @@ -64,6 +108,7 @@ public class MessageViewModel : ViewModelBase { ShowCloseButton = showCloseButton; Message = result.Message; + ClipCommandText = "Copy installer log to clipboard"; Task.Run(() => { diff --git a/SPTInstaller/Views/MessageView.axaml b/SPTInstaller/Views/MessageView.axaml index 0ee61f5..3ad9149 100644 --- a/SPTInstaller/Views/MessageView.axaml +++ b/SPTInstaller/Views/MessageView.axaml @@ -15,7 +15,7 @@ -