Skip to content

Commit

Permalink
(chocolatey#904) Wrap MessageBoxes with a hidden window
Browse files Browse the repository at this point in the history
There appears to be a "feature" in WPF that prevents MessageBoxes from
displaying when there are no active windows. Many of our MessageBoxes
are to report errors, and many of them occur during startup. This
results in them not displaying as we haven't created any windows. This
works around that by creating a hidden window and closing it after the
MessageBox has been displayed.
  • Loading branch information
corbob committed Jan 16, 2022
1 parent 254be31 commit 822edf4
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Source/ChocolateyGui.Common.Windows/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using ChocolateyGui.Common.Utilities;
using ChocolateyGui.Common.ViewModels.Items;
using ChocolateyGui.Common.Windows.Startup;
using ChocolateyGui.Common.Windows.Utilities;
using ChocolateyGui.Common.Windows.ViewModels;
using LiteDB;
using Serilog;
Expand Down Expand Up @@ -135,7 +136,7 @@ protected override async void OnStartup(object sender, StartupEventArgs e)
}
catch (Exception ex)
{
MessageBox.Show(string.Format(Resources.Fatal_Startup_Error_Formatted, ex.Message));
ChocolateyMessageBox.Show(string.Format(Resources.Fatal_Startup_Error_Formatted, ex.Message));
Logger.Fatal(ex, Resources.Fatal_Startup_Error);
await OnExitAsync();
}
Expand Down Expand Up @@ -192,7 +193,7 @@ private static void CurrentDomain_UnhandledException(object sender, UnhandledExc
return;
}

MessageBox.Show(
ChocolateyMessageBox.Show(
e.ExceptionObject.ToString(),
Resources.Bootstrapper_UnhandledException,
MessageBoxButton.OK,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
<Compile Include="Controls\Dialogs\IClosableChildWindow.cs" />
<Compile Include="Services\DialogService.cs" />
<Compile Include="Services\IDialogService.cs" />
<Compile Include="Utilities\ChocolateyMessageBox.cs" />
<Compile Include="Utilities\ToolTipBehavior.cs" />
<Compile Include="Bootstrapper.cs" />
<Compile Include="Commands\CommandExecutionManager.cs" />
Expand Down
5 changes: 3 additions & 2 deletions Source/ChocolateyGui.Common.Windows/Services/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Windows.Media;
using ChocolateyGui.Common.Properties;
using ChocolateyGui.Common.Windows.Controls.Dialogs;
using ChocolateyGui.Common.Windows.Utilities;
using ChocolateyGui.Common.Windows.Views;
using ControlzEx.Theming;
using MahApps.Metro.Controls.Dialogs;
Expand Down Expand Up @@ -48,7 +49,7 @@ public async Task<MessageDialogResult> ShowMessageAsync(string title, string mes
return await ShellView.ShowMessageAsync(title, message, MessageDialogStyle.Affirmative, dialogSettings);
}

return MessageBox.Show(message, title) == MessageBoxResult.OK
return ChocolateyMessageBox.Show(message, title) == MessageBoxResult.OK
? MessageDialogResult.Affirmative
: MessageDialogResult.Negative;
}
Expand All @@ -70,7 +71,7 @@ public async Task<MessageDialogResult> ShowConfirmationMessageAsync(string title
return await ShellView.ShowMessageAsync(title, message, MessageDialogStyle.AffirmativeAndNegative, dialogSettings);
}

return MessageBox.Show(message, title, MessageBoxButton.YesNo) == MessageBoxResult.Yes
return ChocolateyMessageBox.Show(message, title, MessageBoxButton.YesNo) == MessageBoxResult.Yes
? MessageDialogResult.Affirmative
: MessageDialogResult.Negative;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="ChocolateyMessageBox.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace ChocolateyGui.Common.Windows.Utilities
{
using System.Windows;

public static class ChocolateyMessageBox
{
public static MessageBoxResult Show(string messageBoxText)
{
var dummyWindow = DummyWindow();
dummyWindow.Show();
var result = MessageBox.Show(messageBoxText);
dummyWindow.Close();
return result;
}

public static MessageBoxResult Show(string messageBoxText, string caption)
{
var dummyWindow = DummyWindow();
dummyWindow.Show();
var result = MessageBox.Show(messageBoxText, caption);
dummyWindow.Close();
return result;
}

public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
{
var dummyWindow = DummyWindow();
dummyWindow.Show();
var result = MessageBox.Show(messageBoxText, caption, button);
dummyWindow.Close();
return result;
}

public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
{
var dummyWindow = DummyWindow();
dummyWindow.Show();
var result = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options);
dummyWindow.Show();
return result;
}

private static Window DummyWindow()
{
return new Window
{
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
WindowStyle = WindowStyle.None,
Top = 0,
Left = 0,
Width = 1,
Height = 1,
ShowInTaskbar = false
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using ChocolateyGui.Common.ViewModels;
using ChocolateyGui.Common.ViewModels.Items;
using ChocolateyGui.Common.Windows.Services;
using ChocolateyGui.Common.Windows.Utilities;
using ChocolateyGui.Common.Windows.Utilities.Extensions;
using NuGet;
using Serilog;
Expand Down Expand Up @@ -402,7 +403,7 @@ protected override void OnInitialize()
catch (InvalidOperationException ex)
{
Logger.Error(ex, "Failed to initialize remote source view model.");
MessageBox.Show(
ChocolateyMessageBox.Show(
string.Format(
CultureInfo.InvariantCulture,
Resources.RemoteSourceViewModel_UnableToConnectToFeed,
Expand Down
3 changes: 2 additions & 1 deletion Source/ChocolateyGui.Common.Windows/Views/ShellView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using ChocolateyGui.Common.Services;
using ChocolateyGui.Common.Windows.Controls.Dialogs;
using ChocolateyGui.Common.Windows.Services;
using ChocolateyGui.Common.Windows.Utilities;
using MahApps.Metro.Controls.Dialogs;
using ChocolateyDialog = ChocolateyGui.Common.Windows.Controls.Dialogs.ChocolateyDialog;

Expand Down Expand Up @@ -74,7 +75,7 @@ public void CheckOperatingSystemCompatibility()
if (operatingSystemVersion.Version.Major == 10 &&
!_chocolateyConfigurationProvider.IsChocolateyExecutableBeingUsed)
{
MessageBox.Show(
ChocolateyMessageBox.Show(
"Usage of the PowerShell Version of Chocolatey (i.e. <= 0.9.8.33) has been detected. Chocolatey GUI does not support using this version of Chocolatey on Windows 10. Please update Chocolatey to the new C# Version (i.e. > 0.9.9.0) and restart Chocolatey GUI. This application will now close.",
"Incompatible Operating System Version",
MessageBoxButton.OK,
Expand Down
3 changes: 2 additions & 1 deletion Source/ChocolateyGui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using ChocolateyGui.Common.Startup;
using ChocolateyGui.Common.Windows;
using ChocolateyGui.Common.Windows.Theming;
using ChocolateyGui.Common.Windows.Utilities;

namespace ChocolateyGui
{
Expand Down Expand Up @@ -80,7 +81,7 @@ public App()
catch (Exception ex)
{
var errorMessage = string.Format("Unable to load Chocolatey GUI assembly. {0}", ex.Message);
MessageBox.Show(errorMessage);
ChocolateyMessageBox.Show(errorMessage);
throw new ApplicationException(errorMessage);
}

Expand Down

0 comments on commit 822edf4

Please sign in to comment.