From ee27a6bdc31bdbc68ec6f0200f5e530c0698a832 Mon Sep 17 00:00:00 2001 From: libgenapps <33476799+libgenapps@users.noreply.github.com> Date: Mon, 1 Jan 2018 17:52:18 +0300 Subject: [PATCH] Clipboard operation retry, global exception handlers --- App.xaml.cs | 27 ++++++++++++++++++- Infrastructure/WindowManager.cs | 5 ++++ ViewModels/ErrorWindowViewModel.cs | 2 +- .../Controls/BookAttributeValueLabel.xaml.cs | 3 ++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/App.xaml.cs b/App.xaml.cs index 0c57ac0..04531d6 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,4 +1,6 @@ -using System.Windows; +using System; +using System.Threading.Tasks; +using System.Windows; using LibgenDesktop.Infrastructure; using LibgenDesktop.Models; using LibgenDesktop.ViewModels; @@ -10,6 +12,7 @@ public partial class App : Application protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); + SetupExceptionHandlers(); MainModel mainModel = new MainModel(); if (mainModel.LocalDatabaseStatus == MainModel.DatabaseStatus.OPENED) { @@ -43,5 +46,27 @@ private void ShowCreateDatabaseWindow(MainModel mainModel) Shutdown(); } } + + private void SetupExceptionHandlers() + { + AppDomain.CurrentDomain.UnhandledException += (sender, e) => ShowErrorWindow(e.ExceptionObject as Exception); + DispatcherUnhandledException += (sender, e) => + { + ShowErrorWindow(e.Exception); + e.Handled = true; + }; + TaskScheduler.UnobservedTaskException += (sender, e) => + { + ShowErrorWindow(e.Exception); + e.SetObserved(); + }; + } + + private void ShowErrorWindow(Exception exception) + { + ErrorWindowViewModel errorWindowViewModel = new ErrorWindowViewModel(exception?.ToString() ?? "(null)"); + IWindowContext errorWindowContext = WindowManager.CreateWindow(RegisteredWindows.WindowKey.ERROR_WINDOW, errorWindowViewModel); + errorWindowContext.ShowDialog(); + } } } diff --git a/Infrastructure/WindowManager.cs b/Infrastructure/WindowManager.cs index 258060a..1106475 100644 --- a/Infrastructure/WindowManager.cs +++ b/Infrastructure/WindowManager.cs @@ -166,6 +166,11 @@ public static void RemoveWindowIcon(Window window) SetWindowPos(windowHandle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); } + public static void SetClipboardText(string text) + { + Clipboard.SetDataObject(text); + } + private static void RemoveWindowStyle(Window window, int styleAttribute) { IntPtr windowHandle = new WindowInteropHelper(window).Handle; diff --git a/ViewModels/ErrorWindowViewModel.cs b/ViewModels/ErrorWindowViewModel.cs index 7caf529..93c639f 100644 --- a/ViewModels/ErrorWindowViewModel.cs +++ b/ViewModels/ErrorWindowViewModel.cs @@ -17,7 +17,7 @@ public ErrorWindowViewModel(string error) private void CopyErrorToClipboard() { - Clipboard.SetText(Error); + WindowManager.SetClipboardText(Error); } } } diff --git a/Views/Controls/BookAttributeValueLabel.xaml.cs b/Views/Controls/BookAttributeValueLabel.xaml.cs index f77ae87..e02525a 100644 --- a/Views/Controls/BookAttributeValueLabel.xaml.cs +++ b/Views/Controls/BookAttributeValueLabel.xaml.cs @@ -2,6 +2,7 @@ using System.ComponentModel; using System.Windows; using System.Windows.Controls; +using LibgenDesktop.Infrastructure; namespace LibgenDesktop.Views.Controls { @@ -16,7 +17,7 @@ public BookAttributeValueLabel() private void CopyMenuItemClick(object sender, RoutedEventArgs e) { - Clipboard.SetText(Text); + WindowManager.SetClipboardText(Text); } private void TextChanged(object sender, EventArgs e)