Added MessageBoxAlertAdmin

This commit is contained in:
Michael Bucari-Tovo 2022-07-16 22:04:00 -06:00
parent 82d8d954ef
commit 1578be2520
9 changed files with 210 additions and 15 deletions

View File

@ -102,7 +102,7 @@ namespace LibationWinForms.AvaloniaUI
if (Design.IsDesignMode) if (Design.IsDesignMode)
return; return;
#if WINDOWS7_0 #if WINDOWS7_0_OR_GREATER
var handle = form.PlatformImpl.Handle.Handle; var handle = form.PlatformImpl.Handle.Handle;
var currentStyle = GetWindowLong(handle, GWL_STYLE); var currentStyle = GetWindowLong(handle, GWL_STYLE);
@ -110,7 +110,7 @@ namespace LibationWinForms.AvaloniaUI
#endif #endif
} }
#if WINDOWS7_0 #if WINDOWS7_0_OR_GREATER
const long WS_MINIMIZEBOX = 0x00020000L; const long WS_MINIMIZEBOX = 0x00020000L;
const long WS_MAXIMIZEBOX = 0x10000L; const long WS_MAXIMIZEBOX = 0x10000L;
const int GWL_STYLE = -16; const int GWL_STYLE = -16;

View File

@ -224,6 +224,31 @@ namespace LibationWinForms.AvaloniaUI
defaultButton); defaultButton);
} }
/// <summary>
/// Logs error. Displays a message box dialog with specified text and caption.
/// </summary>
/// <param name="synchronizeInvoke">Form calling this method.</param>
/// <param name="text">The text to display in the message box.</param>
/// <param name="caption">The text to display in the title bar of the message box.</param>
/// <param name="exception">Exception to log.</param>
public static async Task ShowAdminAlert(Window owner, string text, string caption, Exception exception)
{
// for development and debugging, show me what broke!
if (System.Diagnostics.Debugger.IsAttached)
throw exception;
try
{
Serilog.Log.Logger.Error(exception, "Alert admin error: {@DebugText}", new { text, caption });
}
catch { }
var form = new MessageBoxAlertAdminDialog(text, caption, exception);
await DisplayWindow(form, owner);
}
private static async Task<DialogResult> ShowCore(Window owner, string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton) private static async Task<DialogResult> ShowCore(Window owner, string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
{ {
if (Avalonia.Threading.Dispatcher.UIThread.CheckAccess()) if (Avalonia.Threading.Dispatcher.UIThread.CheckAccess())
@ -264,11 +289,15 @@ namespace LibationWinForms.AvaloniaUI
dialog.Height = dialog.MinHeight; dialog.Height = dialog.MinHeight;
dialog.Width = dialog.MinWidth; dialog.Width = dialog.MinWidth;
return await DisplayWindow(dialog, owner);
}
private static async Task<DialogResult> DisplayWindow(Window toDisplay, Window owner)
{
if (owner is null) if (owner is null)
{ {
if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (Application.Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
return await dialog.ShowDialog<DialogResult>(desktop.MainWindow); return await toDisplay.ShowDialog<DialogResult>(desktop.MainWindow);
} }
else else
{ {
@ -282,7 +311,7 @@ namespace LibationWinForms.AvaloniaUI
}; };
window.Show(); window.Show();
var result = await dialog.ShowDialog<DialogResult>(window); var result = await toDisplay.ShowDialog<DialogResult>(window);
window.Close(); window.Close();
return result; return result;
} }
@ -290,7 +319,7 @@ namespace LibationWinForms.AvaloniaUI
} }
else else
{ {
return await dialog.ShowDialog<DialogResult>(owner); return await toDisplay.ShowDialog<DialogResult>(owner);
} }
} }

View File

@ -316,7 +316,7 @@ namespace LibationWinForms.AvaloniaUI.ViewModels
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBoxLib.ShowAdminAlert( await MessageBox.ShowAdminAlert(
null, null,
"Error scanning library. You may still manually select books to remove from Libation's library.", "Error scanning library. You may still manually select books to remove from Libation's library.",
"Error scanning library", "Error scanning library",

View File

@ -101,8 +101,8 @@ namespace LibationWinForms.AvaloniaUI.Views.Dialogs
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBoxLib.ShowAdminAlert( await MessageBox.ShowAdminAlert(
null, this,
$"An error occurred while importing an account from:\r\n{filePath[0]}\r\n\r\nIs the file encrypted?", $"An error occurred while importing an account from:\r\n{filePath[0]}\r\n\r\nIs the file encrypted?",
"Error Importing Account", "Error Importing Account",
ex); ex);
@ -149,7 +149,7 @@ namespace LibationWinForms.AvaloniaUI.Views.Dialogs
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBoxLib.ShowAdminAlert(null, "Error attempting to save accounts", "Error saving accounts", ex); await MessageBox.ShowAdminAlert(this, "Error attempting to save accounts", "Error saving accounts", ex);
} }
} }
@ -259,8 +259,8 @@ namespace LibationWinForms.AvaloniaUI.Views.Dialogs
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBoxLib.ShowAdminAlert( await MessageBox.ShowAdminAlert(
null, this,
$"An error occurred while exporting account:\r\n{account.AccountName}", $"An error occurred while exporting account:\r\n{account.AccountName}",
"Error Exporting Account", "Error Exporting Account",
ex); ex);

View File

@ -0,0 +1,81 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450"
MinWidth="600" MinHeight="450"
MaxWidth="600" MaxHeight="450"
x:Class="LibationWinForms.AvaloniaUI.Views.Dialogs.MessageBoxAlertAdminDialog"
Title="MessageBoxAlertAdminDialog"
WindowStartupLocation="CenterOwner"
Icon="/AvaloniaUI/Assets/libation.ico">
<Grid RowDefinitions="Auto,*,Auto,Auto">
<Grid
Grid.Column="0"
Margin="10,10,10,0"
ColumnDefinitions="Auto,*">
<Image Grid.Column="0" Width="64" Height="64" Source="/AvaloniaUI/Assets/MBIcons/error.png" />
<TextBlock
Grid.Column="1"
Margin="10"
TextWrapping="Wrap"
Text="{Binding ErrorDescription}" />
</Grid>
<TextBox
Grid.Row="1"
Margin="10,10,10,0"
IsReadOnly="True"
Text="{Binding ExceptionMessage}" />
<Grid
Grid.Row="2"
Margin="10,10,10,0"
ColumnDefinitions="Auto,*">
<TextBlock
Grid.Column="0"
Text="If you'd like to report this error to an advinistrator:&#xa;&#xa;Step 1: Go to Libation's &quot;issues&quot; page on github&#xa;Step 2: Find your log files&#xa;Setp 3: Click &quot;New issue&quot; button&#xa;Step 4: Drag/drop your log files" />
<StackPanel
Margin="50,0,0,0"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Orientation="Vertical">
<StackPanel.Styles>
<Style Selector="TextBlock">
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="TextDecorations" Value="Underline"/>
</Style>
</StackPanel.Styles>
<TextBlock
Margin="10"
Tapped="GoToGithub_Tapped"
Text="Click to go to github" />
<TextBlock
Margin="10"
Tapped="GoToLogs_Tapped"
Text="Click to open log files folder" />
</StackPanel>
</Grid>
<Button
Grid.Row="3"
Height="30"
HorizontalAlignment="Center"
Margin="10,10,10,10"
Padding="30,3,30,3"
Name="OkButton"
Content="Ok"
Click="OkButton_Clicked" />
</Grid>
</Window>

View File

@ -0,0 +1,74 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Dinah.Core;
using FileManager;
using System;
namespace LibationWinForms.AvaloniaUI.Views.Dialogs
{
public partial class MessageBoxAlertAdminDialog : DialogWindow
{
public string ErrorDescription { get; set; } = "[Error message]\n[Error message]\n[Error message]";
public string ExceptionMessage { get; set; } = "EXCEPTION MESSAGE!";
public MessageBoxAlertAdminDialog()
{
InitializeComponent();
ControlToFocusOnShow = this.FindControl<Button>(nameof(OkButton));
if (Design.IsDesignMode)
DataContext = this;
}
public MessageBoxAlertAdminDialog(string text, string caption, Exception exception) : this()
{
ErrorDescription = text;
this.Title = caption;
ExceptionMessage = $"{exception.Message}\r\n\r\n{exception.StackTrace}";
DataContext = this;
}
private async void GoToGithub_Tapped(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
var url = "https://github.com/rmcrackan/Libation/issues";
try
{
Go.To.Url(url);
}
catch
{
await MessageBox.Show($"Error opening url\r\n{url}", "Error opening url", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private async void GoToLogs_Tapped(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
LongPath dir = "";
try
{
dir = LibationFileManager.Configuration.Instance.LibationFiles;
}
catch { }
try
{
Go.To.Folder(dir.ShortPathName);
}
catch
{
await MessageBox.Show($"Error opening folder\r\n{dir}", "Error opening folder", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
public void OkButton_Clicked(object sender, Avalonia.Interactivity.RoutedEventArgs e)
{
SaveAndClose();
}
}
}

View File

@ -41,7 +41,7 @@ namespace LibationWinForms.AvaloniaUI.Views
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBoxLib.ShowAdminAlert(null, "Error attempting to export your library.", "Error exporting", ex); await MessageBox.ShowAdminAlert(this, "Error attempting to export your library.", "Error exporting", ex);
} }
} }
} }

View File

@ -71,8 +71,8 @@ namespace LibationWinForms.AvaloniaUI.Views
} }
catch (Exception ex) catch (Exception ex)
{ {
MessageBoxLib.ShowAdminAlert( await MessageBox.ShowAdminAlert(
null, this,
"Error importing library. Please try again. If this still happens after 2 or 3 tries, stop and contact administrator", "Error importing library. Please try again. If this still happens after 2 or 3 tries, stop and contact administrator",
"Error importing library", "Error importing library",
ex); ex);

View File

@ -26,6 +26,9 @@ namespace LibationWinForms
if (config is null) return; if (config is null) return;
var bmp = System.Drawing.SystemIcons.Error.ToBitmap();
/* /*
Results below compare startup times when parallelizing startup tasks vs when Results below compare startup times when parallelizing startup tasks vs when
running everything sequentially, from the entry point until after the call to running everything sequentially, from the entry point until after the call to
@ -174,7 +177,9 @@ namespace LibationWinForms
AppScaffolding.LibationScaffolding.RunPostMigrationScaffolding(config); AppScaffolding.LibationScaffolding.RunPostMigrationScaffolding(config);
// global exception handling (ShowAdminAlert) attempts to use logging. only call it after logging has been init'd // global exception handling (ShowAdminAlert) attempts to use logging. only call it after logging has been init'd
#if WINDOWS7_0_OR_GREATER
postLoggingGlobalExceptionHandling(); postLoggingGlobalExceptionHandling();
#endif
return true; return true;
} }
@ -190,6 +195,7 @@ namespace LibationWinForms
{ {
var title = "Fatal error, pre-logging"; var title = "Fatal error, pre-logging";
var body = "An unrecoverable error occurred. Since this error happened before logging could be initialized, this error can not be written to the log file."; var body = "An unrecoverable error occurred. Since this error happened before logging could be initialized, this error can not be written to the log file.";
#if WINDOWS7_0_OR_GREATER
try try
{ {
MessageBoxLib.ShowAdminAlert(null, body, title, ex); MessageBoxLib.ShowAdminAlert(null, body, title, ex);
@ -198,6 +204,7 @@ namespace LibationWinForms
{ {
MessageBox.Show($"{body}\r\n\r\n{ex.Message}\r\n\r\n{ex.StackTrace}", title, MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show($"{body}\r\n\r\n{ex.Message}\r\n\r\n{ex.StackTrace}", title, MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
#endif
} }
private static void RunInstaller(Configuration config) private static void RunInstaller(Configuration config)
@ -300,13 +307,17 @@ namespace LibationWinForms
} }
catch (Exception ex) catch (Exception ex)
{ {
#if WINDOWS7_0_OR_GREATER
MessageBoxLib.ShowAdminAlert(null, "Error checking for update", "Error checking for update", ex); MessageBoxLib.ShowAdminAlert(null, "Error checking for update", "Error checking for update", ex);
#endif
return; return;
} }
if (upgradeProperties.ZipUrl is null) if (upgradeProperties.ZipUrl is null)
{ {
#if WINDOWS7_0_OR_GREATER
MessageBox.Show(upgradeProperties.HtmlUrl, "New version available"); MessageBox.Show(upgradeProperties.HtmlUrl, "New version available");
#endif
return; return;
} }