Skip to content

Commit

Permalink
Clipboard operation retry, global exception handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
libgenapps committed Jan 1, 2018
1 parent 2970cbe commit ee27a6b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
27 changes: 26 additions & 1 deletion App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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();
}
}
}
5 changes: 5 additions & 0 deletions Infrastructure/WindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ViewModels/ErrorWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ErrorWindowViewModel(string error)

private void CopyErrorToClipboard()
{
Clipboard.SetText(Error);
WindowManager.SetClipboardText(Error);
}
}
}
3 changes: 2 additions & 1 deletion Views/Controls/BookAttributeValueLabel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using LibgenDesktop.Infrastructure;

namespace LibgenDesktop.Views.Controls
{
Expand All @@ -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)
Expand Down

0 comments on commit ee27a6b

Please sign in to comment.