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

204 lines
7.5 KiB
C#
Raw Normal View History

2022-11-12 18:27:41 +00:00
using System;
2024-11-27 14:16:52 +00:00
using System.IO;
2024-11-27 14:27:30 +00:00
using System.Threading.Tasks;
2022-11-12 18:27:41 +00:00
using System.Windows;
using System.Windows.Input;
2022-11-13 16:29:24 +00:00
using Flow.Launcher.Infrastructure;
2024-11-27 14:16:52 +00:00
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger;
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
{
private static MessageBoxEx msgBox;
private static MessageBoxResult _result = MessageBoxResult.None;
private readonly MessageBoxButton _button;
private MessageBoxEx(MessageBoxButton button)
2022-11-12 18:27:41 +00:00
{
_button = button;
2022-11-12 18:27:41 +00:00
InitializeComponent();
}
public static MessageBoxResult Show(string messageBoxText)
=> Show(messageBoxText, string.Empty, MessageBoxButton.OK, MessageBoxImage.None, MessageBoxResult.OK);
public static MessageBoxResult Show(
string messageBoxText,
string caption = "",
MessageBoxButton button = MessageBoxButton.OK,
MessageBoxImage icon = MessageBoxImage.None,
MessageBoxResult defaultResult = MessageBoxResult.OK)
2022-11-13 09:29:02 +00:00
{
if (!Application.Current.Dispatcher.CheckAccess())
{
return Application.Current.Dispatcher.Invoke(() => Show(messageBoxText, caption, button, icon, defaultResult));
}
try
2024-11-24 14:12:43 +00:00
{
msgBox = new MessageBoxEx(button);
if (caption == string.Empty && button == MessageBoxButton.OK && icon == MessageBoxImage.None)
{
msgBox.Title = messageBoxText;
msgBox.DescOnlyTextBlock.Visibility = Visibility.Visible;
msgBox.DescOnlyTextBlock.Text = messageBoxText;
}
else
{
msgBox.Title = caption;
msgBox.TitleTextBlock.Text = caption;
msgBox.DescTextBlock.Text = messageBoxText;
2024-11-27 14:27:30 +00:00
_ = SetImageOfMessageBoxAsync(icon);
}
SetButtonVisibilityFocusAndResult(button, defaultResult);
msgBox.ShowDialog();
return _result;
2024-11-24 14:12:43 +00:00
}
catch (Exception e)
2024-11-24 14:12:43 +00:00
{
Log.Error($"|MessageBoxEx.Show|An error occurred: {e.Message}");
msgBox = null;
return MessageBoxResult.None;
2024-11-24 14:12:43 +00:00
}
2022-11-13 09:29:02 +00:00
}
2022-11-12 18:27:41 +00:00
private static void SetButtonVisibilityFocusAndResult(MessageBoxButton button, MessageBoxResult defaultResult)
2022-11-13 09:29:02 +00:00
{
switch (button)
{
case MessageBoxButton.OK:
msgBox.btnCancel.Visibility = Visibility.Collapsed;
msgBox.btnNo.Visibility = Visibility.Collapsed;
msgBox.btnYes.Visibility = Visibility.Collapsed;
msgBox.btnOk.Focus();
_result = MessageBoxResult.OK;
2022-11-13 09:29:02 +00:00
break;
case MessageBoxButton.OKCancel:
msgBox.btnNo.Visibility = Visibility.Collapsed;
msgBox.btnYes.Visibility = Visibility.Collapsed;
if (defaultResult == MessageBoxResult.Cancel)
{
msgBox.btnCancel.Focus();
_result = MessageBoxResult.Cancel;
}
else
{
msgBox.btnOk.Focus();
_result = MessageBoxResult.OK;
}
2022-11-13 09:29:02 +00:00
break;
case MessageBoxButton.YesNo:
msgBox.btnOk.Visibility = Visibility.Collapsed;
msgBox.btnCancel.Visibility = Visibility.Collapsed;
if (defaultResult == MessageBoxResult.No)
{
msgBox.btnNo.Focus();
_result = MessageBoxResult.No;
}
else
{
msgBox.btnYes.Focus();
_result = MessageBoxResult.Yes;
}
2022-11-13 09:29:02 +00:00
break;
case MessageBoxButton.YesNoCancel:
msgBox.btnOk.Visibility = Visibility.Collapsed;
if (defaultResult == MessageBoxResult.No)
{
msgBox.btnNo.Focus();
_result = MessageBoxResult.No;
}
else if (defaultResult == MessageBoxResult.Cancel)
{
msgBox.btnCancel.Focus();
_result = MessageBoxResult.Cancel;
}
else
{
msgBox.btnYes.Focus();
_result = MessageBoxResult.Yes;
}
2022-11-13 09:29:02 +00:00
break;
default:
break;
}
}
2024-11-27 14:27:30 +00:00
private static async Task SetImageOfMessageBoxAsync(MessageBoxImage icon)
2022-11-13 09:29:02 +00:00
{
switch (icon)
2022-11-13 09:29:02 +00:00
{
case MessageBoxImage.Exclamation:
2024-11-27 14:27:30 +00:00
await msgBox.SetImageAsync("Exclamation.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:
2024-11-27 14:27:30 +00:00
await msgBox.SetImageAsync("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:
2024-11-27 14:27:30 +00:00
await msgBox.SetImageAsync("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:
2024-11-27 14:27:30 +00:00
await msgBox.SetImageAsync("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;
}
}
2024-11-27 14:27:30 +00:00
private async Task SetImageAsync(string imageName)
2022-11-13 09:29:02 +00:00
{
2024-11-27 14:16:52 +00:00
var imagePath = Path.Combine(Constant.ProgramDirectory, "Images", imageName);
var imageSource = await ImageLoader.LoadAsync(imagePath);
Img.Source = imageSource;
2022-11-13 09:29:02 +00:00
}
private void KeyEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
2022-11-12 18:27:41 +00:00
{
if (_button == MessageBoxButton.YesNo)
return;
else if (_button == MessageBoxButton.OK)
_result = MessageBoxResult.OK;
else
_result = MessageBoxResult.Cancel;
2022-11-12 18:27:41 +00:00
DialogResult = false;
Close();
}
2022-11-13 09:29:02 +00:00
private void Button_Click(object sender, RoutedEventArgs e)
2022-11-13 09:29:02 +00:00
{
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;
2022-11-13 09:29:02 +00:00
msgBox.Close();
msgBox = null;
}
private void Button_Cancel(object sender, RoutedEventArgs e)
{
if (_button == MessageBoxButton.YesNo)
return;
else if (_button == MessageBoxButton.OK)
_result = MessageBoxResult.OK;
else
_result = MessageBoxResult.Cancel;
msgBox.Close();
msgBox = null;
}
2022-11-12 18:27:41 +00:00
}
}