Flow.Launcher/Flow.Launcher.Core/MessageBoxEx.xaml.cs

188 lines
6.6 KiB
C#
Raw Normal View History

2022-11-12 18:27:41 +00:00
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
2022-11-13 16:29:24 +00:00
using Flow.Launcher.Infrastructure;
2022-11-12 18:27:41 +00:00
namespace Flow.Launcher.Core
2022-11-12 18:27:41 +00:00
{
public partial class MessageBoxEx : Window
{
public MessageBoxEx()
{
InitializeComponent();
}
2022-11-13 09:29:02 +00:00
public enum MessageBoxType
{
ConfirmationWithYesNo = 0,
ConfirmationWithYesNoCancel,
2024-06-11 04:01:47 +00:00
YesNo,
2022-11-13 09:29:02 +00:00
Information,
Error,
Warning
}
2022-11-12 18:27:41 +00:00
2022-11-13 09:29:02 +00:00
public enum MessageBoxImage
2022-11-12 18:27:41 +00:00
{
2022-11-13 09:29:02 +00:00
Warning = 0,
Question,
Information,
Error,
None
2022-11-12 18:27:41 +00:00
}
2022-11-13 09:29:02 +00:00
static MessageBoxEx msgBox;
static MessageBoxResult _result = MessageBoxResult.No;
/// 1 parameter
public static MessageBoxResult Show(string messageBoxText)
2022-11-12 18:27:41 +00:00
{
2024-11-24 14:12:43 +00:00
return Show(messageBoxText, string.Empty, MessageBoxButton.OK, MessageBoxImage.None);
2022-11-12 18:27:41 +00:00
}
2022-11-13 09:29:02 +00:00
// 2 parameter
public static MessageBoxResult Show(string messageBoxText, string title)
2022-11-12 18:27:41 +00:00
{
return Show(messageBoxText, title, MessageBoxButton.OK, MessageBoxImage.None);
2022-11-13 09:29:02 +00:00
}
2022-11-12 18:27:41 +00:00
2022-11-13 09:29:02 +00:00
/// 3 parameter
public static MessageBoxResult Show(string messageBoxText, string title, MessageBoxType type)
2022-11-13 09:29:02 +00:00
{
switch (type)
{
case MessageBoxType.ConfirmationWithYesNo:
return Show(messageBoxText, title, MessageBoxButton.YesNo,
2022-11-13 09:29:02 +00:00
MessageBoxImage.Question);
2024-06-11 04:01:47 +00:00
case MessageBoxType.YesNo:
return Show(messageBoxText, title, MessageBoxButton.YesNo,
2024-06-11 04:01:47 +00:00
MessageBoxImage.Question);
2022-11-13 09:29:02 +00:00
case MessageBoxType.ConfirmationWithYesNoCancel:
return Show(messageBoxText, title, MessageBoxButton.YesNoCancel,
2022-11-13 09:29:02 +00:00
MessageBoxImage.Question);
case MessageBoxType.Information:
return Show(messageBoxText, title, MessageBoxButton.OK,
2022-11-13 09:29:02 +00:00
MessageBoxImage.Information);
case MessageBoxType.Error:
return Show(messageBoxText, title, MessageBoxButton.OK,
2022-11-13 09:29:02 +00:00
MessageBoxImage.Error);
case MessageBoxType.Warning:
return Show(messageBoxText, title, MessageBoxButton.OK,
2022-11-13 09:29:02 +00:00
MessageBoxImage.Warning);
default:
return MessageBoxResult.No;
}
}
// 4 parameter, Final Display Message.
public static MessageBoxResult Show(string messageBoxText, string title, MessageBoxButton button, MessageBoxImage image)
2022-11-13 09:29:02 +00:00
{
msgBox = new MessageBoxEx();
2024-11-24 14:12:43 +00:00
if (title == string.Empty && button == MessageBoxButton.OK && image == MessageBoxImage.None)
{
msgBox.DescOnlyTextBlock.Text = messageBoxText;
msgBox.Title = messageBoxText;
}
else
{
msgBox.TitleTextBlock.Text = title;
msgBox.DescTextBlock.Text = messageBoxText;
msgBox.Title = title;
SetImageOfMessageBox(image);
}
2022-11-13 09:29:02 +00:00
SetVisibilityOfButtons(button);
msgBox.ShowDialog();
return _result;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (sender == btnOk)
_result = MessageBoxResult.OK;
else if (sender == btnYes)
_result = MessageBoxResult.Yes;
else if (sender == btnNo)
_result = MessageBoxResult.No;
else if (sender == btnCancel)
_result = MessageBoxResult.Cancel;
else
_result = MessageBoxResult.None;
msgBox.Close();
msgBox = null;
2022-11-12 18:27:41 +00:00
}
2022-11-13 09:29:02 +00:00
private static void SetVisibilityOfButtons(MessageBoxButton button)
{
switch (button)
{
case MessageBoxButton.OK:
msgBox.btnCancel.Visibility = Visibility.Collapsed;
msgBox.btnNo.Visibility = Visibility.Collapsed;
msgBox.btnYes.Visibility = Visibility.Collapsed;
msgBox.btnOk.Focus();
break;
case MessageBoxButton.OKCancel:
msgBox.btnNo.Visibility = Visibility.Collapsed;
msgBox.btnYes.Visibility = Visibility.Collapsed;
msgBox.btnCancel.Focus();
break;
case MessageBoxButton.YesNo:
msgBox.btnOk.Visibility = Visibility.Collapsed;
msgBox.btnCancel.Visibility = Visibility.Collapsed;
msgBox.btnNo.Focus();
break;
case MessageBoxButton.YesNoCancel:
msgBox.btnOk.Visibility = Visibility.Collapsed;
msgBox.btnCancel.Focus();
break;
default:
break;
}
}
private static void SetImageOfMessageBox(MessageBoxImage image)
{
switch (image)
{
case MessageBoxImage.Warning:
msgBox.SetImage("Warning.png");
2022-11-13 16:29:24 +00:00
msgBox.Img.Visibility = Visibility.Visible;
2022-11-13 09:29:02 +00:00
break;
case MessageBoxImage.Question:
msgBox.SetImage("Question.png");
2022-11-13 16:29:24 +00:00
msgBox.Img.Visibility = Visibility.Visible;
2022-11-13 09:29:02 +00:00
break;
case MessageBoxImage.Information:
msgBox.SetImage("Information.png");
2022-11-13 16:29:24 +00:00
msgBox.Img.Visibility = Visibility.Visible;
2022-11-13 09:29:02 +00:00
break;
case MessageBoxImage.Error:
msgBox.SetImage("Error.png");
2022-11-13 16:29:24 +00:00
msgBox.Img.Visibility = Visibility.Visible;
2022-11-13 09:29:02 +00:00
break;
default:
msgBox.Img.Visibility = Visibility.Collapsed;
break;
}
}
private void SetImage(string imageName)
{
2022-11-13 16:29:24 +00:00
string uri = Constant.ProgramDirectory + "/Images/" + imageName;
2022-11-13 09:29:02 +00:00
var uriSource = new Uri(uri, UriKind.RelativeOrAbsolute);
Img.Source = new BitmapImage(uriSource);
}
2022-11-12 18:27:41 +00:00
private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
{
DialogResult = false;
Close();
}
2022-11-13 09:29:02 +00:00
private void Button_Cancel(object sender, RoutedEventArgs e)
{
msgBox.Close();
msgBox = null;
}
2022-11-12 18:27:41 +00:00
}
}