Add Avalonia MessageBox

This commit is contained in:
Michael Bucari-Tovo 2022-07-14 00:50:50 -06:00
parent 31d6fc8197
commit 7a8e910697
20 changed files with 570 additions and 35 deletions

View file

@ -0,0 +1,77 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using LibationWinForms.AvaloniaUI.Views.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibationWinForms.AvaloniaUI.ViewModels.Dialogs
{
public class MessageBoxViewModel
{
private string _message;
public string Message { get { return _message; } set { _message = value; } }
public string Caption { get; } = "Message Box";
private MessageBoxButtons _button;
private MessageBoxIcon _icon;
private MessageBoxDefaultButton _defaultButton;
public MessageBoxButtons Buttons => _button;
public bool IsAsterisk => _icon == MessageBoxIcon.Asterisk;
public bool IsError => _icon == MessageBoxIcon.Error;
public bool IsQuestion => _icon == MessageBoxIcon.Question;
public bool IsExclamation => _icon == MessageBoxIcon.Exclamation;
public bool HasButton3 => !string.IsNullOrEmpty(Button3Text);
public bool HasButton2 => !string.IsNullOrEmpty(Button2Text);
public int WindowHeight { get;private set; }
public int WindowWidth { get;private set; }
public string Button1Text => _button switch
{
MessageBoxButtons.OK => "OK",
MessageBoxButtons.OKCancel => "OK",
MessageBoxButtons.AbortRetryIgnore => "Abort",
MessageBoxButtons.YesNoCancel => "Yes",
MessageBoxButtons.YesNo => "Yes",
MessageBoxButtons.RetryCancel => "Retry",
MessageBoxButtons.CancelTryContinue => "Cancel",
_ => string.Empty,
};
public string Button2Text => _button switch
{
MessageBoxButtons.OKCancel => "Cancel",
MessageBoxButtons.AbortRetryIgnore => "Retry",
MessageBoxButtons.YesNoCancel => "No",
MessageBoxButtons.YesNo => "No",
MessageBoxButtons.RetryCancel => "Cancel",
MessageBoxButtons.CancelTryContinue => "Try",
_ => string.Empty,
};
public string Button3Text => _button switch
{
MessageBoxButtons.AbortRetryIgnore => "Ignore",
MessageBoxButtons.YesNoCancel => "Cancel",
MessageBoxButtons.CancelTryContinue => "Continue",
_ => string.Empty,
};
public MessageBoxViewModel() { }
public MessageBoxViewModel(string message, string caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultBtn)
{
Message = message;
Caption = caption;
_button = buttons;
_icon = icon;
_defaultButton = defaultBtn;
}
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LibationWinForms.AvaloniaUI.ViewModels
{
public class MainWindowViewModel
{
}
}

View file

@ -9,6 +9,7 @@ using DataLayer;
using Dinah.Core;
using FileLiberator;
using LibationFileManager;
using LibationWinForms.AvaloniaUI.Views.Dialogs;
using ReactiveUI;
namespace LibationWinForms.AvaloniaUI.ViewModels
@ -152,7 +153,7 @@ namespace LibationWinForms.AvaloniaUI.ViewModels
finally
{
if (Result == ProcessBookResult.None)
Result = showRetry(LibraryBook);
Result = await showRetry(LibraryBook);
Status = Result switch
{
@ -313,15 +314,15 @@ namespace LibationWinForms.AvaloniaUI.ViewModels
#region Failure Handler
private ProcessBookResult showRetry(LibraryBook libraryBook)
private async Task<ProcessBookResult> showRetry(LibraryBook libraryBook)
{
Logger.Error("ERROR. All books have not been processed. Most recent book: processing failed");
System.Windows.Forms.DialogResult? dialogResult = Configuration.Instance.BadBook switch
DialogResult? dialogResult = Configuration.Instance.BadBook switch
{
Configuration.BadBookAction.Abort => System.Windows.Forms.DialogResult.Abort,
Configuration.BadBookAction.Retry => System.Windows.Forms.DialogResult.Retry,
Configuration.BadBookAction.Ignore => System.Windows.Forms.DialogResult.Ignore,
Configuration.BadBookAction.Abort => DialogResult.Abort,
Configuration.BadBookAction.Retry => DialogResult.Retry,
Configuration.BadBookAction.Ignore => DialogResult.Ignore,
Configuration.BadBookAction.Ask => null,
_ => null
};
@ -346,9 +347,9 @@ $@" Title: {libraryBook.Book.Title}
}
// if null then ask user
dialogResult ??= System.Windows.Forms.MessageBox.Show(string.Format(SkipDialogText + "\r\n\r\nSee Settings to avoid this box in the future.", details), "Skip importing this book?", SkipDialogButtons, System.Windows.Forms.MessageBoxIcon.Question, SkipDialogDefaultButton);
dialogResult ??= await MessageBox.Show(string.Format(SkipDialogText + "\r\n\r\nSee Settings to avoid this box in the future.", details), "Skip importing this book?", SkipDialogButtons, MessageBoxIcon.Question, SkipDialogDefaultButton);
if (dialogResult == System.Windows.Forms.DialogResult.Abort)
if (dialogResult == DialogResult.Abort)
return ProcessBookResult.FailedAbort;
if (dialogResult == SkipResult)
@ -373,9 +374,9 @@ An error occurred while trying to process this book.
- IGNORE: Permanently ignore this book. Continue processing books. (Will not try this book again later.)
".Trim();
private System.Windows.Forms.MessageBoxButtons SkipDialogButtons => System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore;
private System.Windows.Forms.MessageBoxDefaultButton SkipDialogDefaultButton => System.Windows.Forms.MessageBoxDefaultButton.Button1;
private System.Windows.Forms.DialogResult SkipResult => System.Windows.Forms.DialogResult.Ignore;
private MessageBoxButtons SkipDialogButtons => MessageBoxButtons.AbortRetryIgnore;
private MessageBoxDefaultButton SkipDialogDefaultButton => MessageBoxDefaultButton.Button1;
private DialogResult SkipResult => DialogResult.Ignore;
}
#endregion