33using System . Windows . Input ;
44using System . Windows . Media . Imaging ;
55using Flow . Launcher . Infrastructure ;
6+ using Flow . Launcher . Infrastructure . Logger ;
67
78namespace Flow . Launcher . Core
89{
910 public partial class MessageBoxEx : Window
1011 {
1112 private static MessageBoxEx msgBox ;
1213 private static MessageBoxResult _result = MessageBoxResult . None ;
13- private static MessageBoxButton _button ;
14+
15+ private readonly MessageBoxButton _button ;
1416
1517 private MessageBoxEx ( MessageBoxButton button )
1618 {
@@ -45,26 +47,40 @@ public static MessageBoxResult Show(string messageBoxText, string caption, Messa
4547 // 5 parameter, Final Display Message.
4648 public static MessageBoxResult Show ( string messageBoxText , string caption , MessageBoxButton button , MessageBoxImage icon , MessageBoxResult defaultResult )
4749 {
48- msgBox = new MessageBoxEx ( button ) ;
49- if ( caption == string . Empty && button == MessageBoxButton . OK && icon == MessageBoxImage . None )
50+ if ( ! Application . Current . Dispatcher . CheckAccess ( ) )
5051 {
51- msgBox . Title = messageBoxText ;
52- msgBox . DescOnlyTextBlock . Visibility = Visibility . Visible ;
53- msgBox . DescOnlyTextBlock . Text = messageBoxText ;
52+ return Application . Current . Dispatcher . Invoke ( ( ) => Show ( messageBoxText , caption , button , icon , defaultResult ) ) ;
5453 }
55- else
54+
55+ try
56+ {
57+ msgBox = new MessageBoxEx ( button ) ;
58+ if ( caption == string . Empty && button == MessageBoxButton . OK && icon == MessageBoxImage . None )
59+ {
60+ msgBox . Title = messageBoxText ;
61+ msgBox . DescOnlyTextBlock . Visibility = Visibility . Visible ;
62+ msgBox . DescOnlyTextBlock . Text = messageBoxText ;
63+ }
64+ else
65+ {
66+ msgBox . Title = caption ;
67+ msgBox . TitleTextBlock . Text = caption ;
68+ msgBox . DescTextBlock . Text = messageBoxText ;
69+ SetImageOfMessageBox ( icon ) ;
70+ }
71+ SetButtonVisibilityFocusAndResult ( button , defaultResult ) ;
72+ msgBox . ShowDialog ( ) ;
73+ return _result ;
74+ }
75+ catch ( Exception e )
5676 {
57- msgBox . Title = caption ;
58- msgBox . TitleTextBlock . Text = caption ;
59- msgBox . DescTextBlock . Text = messageBoxText ;
60- SetImageOfMessageBox ( icon ) ;
77+ Log . Error ( $ "|MessageBoxEx.Show|An error occurred: { e . Message } ") ;
78+ msgBox = null ;
79+ return MessageBoxResult . None ;
6180 }
62- SetVisibilityOfButtons ( button , defaultResult ) ;
63- msgBox . ShowDialog ( ) ;
64- return _result ;
6581 }
6682
67- private static void SetVisibilityOfButtons ( MessageBoxButton button , MessageBoxResult defaultResult )
83+ private static void SetButtonVisibilityFocusAndResult ( MessageBoxButton button , MessageBoxResult defaultResult )
6884 {
6985 switch ( button )
7086 {
@@ -73,31 +89,53 @@ private static void SetVisibilityOfButtons(MessageBoxButton button, MessageBoxRe
7389 msgBox . btnNo . Visibility = Visibility . Collapsed ;
7490 msgBox . btnYes . Visibility = Visibility . Collapsed ;
7591 msgBox . btnOk . Focus ( ) ;
92+ _result = MessageBoxResult . OK ;
7693 break ;
7794 case MessageBoxButton . OKCancel :
7895 msgBox . btnNo . Visibility = Visibility . Collapsed ;
7996 msgBox . btnYes . Visibility = Visibility . Collapsed ;
8097 if ( defaultResult == MessageBoxResult . Cancel )
98+ {
8199 msgBox . btnCancel . Focus ( ) ;
100+ _result = MessageBoxResult . Cancel ;
101+ }
82102 else
103+ {
83104 msgBox . btnOk . Focus ( ) ;
105+ _result = MessageBoxResult . OK ;
106+ }
84107 break ;
85108 case MessageBoxButton . YesNo :
86109 msgBox . btnOk . Visibility = Visibility . Collapsed ;
87110 msgBox . btnCancel . Visibility = Visibility . Collapsed ;
88111 if ( defaultResult == MessageBoxResult . No )
112+ {
89113 msgBox . btnNo . Focus ( ) ;
114+ _result = MessageBoxResult . No ;
115+ }
90116 else
117+ {
91118 msgBox . btnYes . Focus ( ) ;
119+ _result = MessageBoxResult . Yes ;
120+ }
92121 break ;
93122 case MessageBoxButton . YesNoCancel :
94123 msgBox . btnOk . Visibility = Visibility . Collapsed ;
95124 if ( defaultResult == MessageBoxResult . No )
125+ {
96126 msgBox . btnNo . Focus ( ) ;
127+ _result = MessageBoxResult . No ;
128+ }
97129 else if ( defaultResult == MessageBoxResult . Cancel )
130+ {
98131 msgBox . btnCancel . Focus ( ) ;
132+ _result = MessageBoxResult . Cancel ;
133+ }
99134 else
135+ {
100136 msgBox . btnYes . Focus ( ) ;
137+ _result = MessageBoxResult . Yes ;
138+ }
101139 break ;
102140 default :
103141 break ;
0 commit comments