Skip to content

Commit 9d673fc

Browse files
committed
Fix messagebox backdrop in dark mode & add sound
1 parent 5590d56 commit 9d673fc

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

source/iNKORE.UI.WPF.Modern.Controls/Controls/MessageBox/MessageBox.Helper.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using iNKORE.UI.WPF.Modern.Extensions;
44
using System;
55
using System.Linq;
6+
using System.Media;
67
using System.Threading.Tasks;
78
using System.Windows;
89

@@ -203,7 +204,7 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string
203204
/// <returns>A <see cref="MessageBoxResult"/> value that specifies which message box button is clicked by the user.</returns>
204205
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
205206
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult? defaultResult) =>
206-
Show(owner, messageBoxText, caption, button, icon.ToSymbol(), defaultResult);
207+
Show(owner, messageBoxText, caption, button, icon.ToSymbol(), defaultResult, icon.ToAlertSound());
207208

208209
/// <summary>
209210
/// Displays a message box in front of the specified window. The message box displays a message, title bar caption, button, and icon; and accepts a default message box result and returns a result.
@@ -216,8 +217,8 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string
216217
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
217218
/// <returns>A <see cref="MessageBoxResult"/> value that specifies which message box button is clicked by the user.</returns>
218219
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
219-
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult) =>
220-
Show(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult);
220+
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult, SystemSound sound = null) =>
221+
Show(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult, sound);
221222

222223
/// <summary>
223224
/// Displays a message box in front of the specified window. The message box displays a message, title bar caption, button, and icon; and accepts a default message box result and returns a result.
@@ -230,7 +231,7 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string
230231
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
231232
/// <returns>A <see cref="MessageBoxResult"/> value that specifies which message box button is clicked by the user.</returns>
232233
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
233-
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult)
234+
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult, SystemSound? sound = null)
234235
{
235236
if (owner is null)
236237
{
@@ -248,6 +249,11 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string
248249
WindowStartupLocation = owner is null ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner
249250
};
250251

252+
if (MakeSound)
253+
{
254+
window.SystemSoundOnLoaded = sound;
255+
}
256+
251257
return window.ShowDialog();
252258
}
253259

@@ -447,7 +453,7 @@ public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxTe
447453
/// <returns>An asynchronous operation showing the message box. When complete, returns a <see cref="MessageBoxResult"/>.</returns>
448454
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
449455
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult? defaultResult) =>
450-
ShowAsync(owner, messageBoxText, caption, button, icon.ToSymbol(), defaultResult);
456+
ShowAsync(owner, messageBoxText, caption, button, icon.ToSymbol(), defaultResult, icon.ToAlertSound());
451457

452458
/// <summary>
453459
/// Begins an asynchronous operation to displays a message box in front of the specified window. The message box displays a message, title bar caption, button, and icon; and accepts a default message box result and returns a result.
@@ -460,8 +466,8 @@ public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxTe
460466
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
461467
/// <returns>An asynchronous operation showing the message box. When complete, returns a <see cref="MessageBoxResult"/>.</returns>
462468
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
463-
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult) =>
464-
ShowAsync(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult);
469+
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult, SystemSound sound = null) =>
470+
ShowAsync(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult, sound);
465471

466472
/// <summary>
467473
/// Begins an asynchronous operation to displays a message box in front of the specified window. The message box displays a message, title bar caption, button, and icon; and accepts a default message box result and returns a result.
@@ -474,7 +480,7 @@ public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxTe
474480
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
475481
/// <returns>An asynchronous operation showing the message box. When complete, returns a <see cref="MessageBoxResult"/>.</returns>
476482
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
477-
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult)
483+
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult, SystemSound sound = null)
478484
{
479485
TaskCompletionSource<MessageBoxResult> taskSource = new TaskCompletionSource<MessageBoxResult>(
480486
#if !NET452
@@ -484,7 +490,7 @@ public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxTe
484490

485491
Application.Current.Dispatcher.Invoke(() =>
486492
{
487-
MessageBoxResult result = Show(owner, messageBoxText, caption, button, icon, defaultResult);
493+
MessageBoxResult result = Show(owner, messageBoxText, caption, button, icon, defaultResult, sound);
488494
taskSource.TrySetResult(result);
489495
});
490496

source/iNKORE.UI.WPF.Modern.Controls/Controls/MessageBox/MessageBox.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.ComponentModel;
88
using System.Linq;
9+
using System.Media;
910
using System.Runtime.InteropServices;
1011
using System.Text;
1112
using System.Threading.Tasks;
@@ -36,6 +37,8 @@ public MessageBoxResult Result
3637

3738
public static BackdropType DefaultBackdropType { get; set; } = BackdropType.None;
3839

40+
public static bool MakeSound { get; set; } = true;
41+
3942
static MessageBox()
4043
{
4144
DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBox), new FrameworkPropertyMetadata(typeof(MessageBox)));
@@ -52,6 +55,22 @@ public MessageBox()
5255
Loaded += On_Loaded;
5356

5457
SystemBackdropTypeProperty_Descriptor.AddValueChanged(this, SystemBackdropTypeProperty_ValueChanged);
58+
ThemeManager.AddActualThemeChangedHandler(this, ThemeManager_AddActualThemeChanged);
59+
}
60+
61+
private void ThemeManager_AddActualThemeChanged(object sender, RoutedEventArgs e)
62+
{
63+
if(WindowHelper.GetSystemBackdropType(this) != BackdropType.None)
64+
{
65+
if(ThemeManager.GetActualTheme(this) == ElementTheme.Dark)
66+
{
67+
MicaHelper.ApplyDarkMode(this);
68+
}
69+
else
70+
{
71+
MicaHelper.RemoveDarkMode(this);
72+
}
73+
}
5574
}
5675

5776
private void SystemBackdropTypeProperty_ValueChanged(object sender, EventArgs e)
@@ -73,6 +92,23 @@ private void SystemBackdropTypeProperty_ValueChanged(object sender, EventArgs e)
7392
}
7493
}
7594

95+
#region SystemSoundOnLoaded
96+
97+
public static readonly DependencyProperty SystemSoundOnLoadedProperty =
98+
DependencyProperty.Register(
99+
nameof(SystemSoundOnLoaded),
100+
typeof(SystemSound),
101+
typeof(MessageBox));
102+
103+
public SystemSound SystemSoundOnLoaded
104+
{
105+
get => (SystemSound)GetValue(SystemSoundOnLoadedProperty);
106+
set => SetValue(SystemSoundOnLoadedProperty, value);
107+
}
108+
109+
#endregion
110+
111+
76112
#region Caption
77113

78114
public static readonly DependencyProperty CaptionProperty =
@@ -787,7 +823,10 @@ private void On_Loaded(object sender, RoutedEventArgs e)
787823
WindowHelper.SetSystemBackdropType(this, DefaultBackdropType);
788824
}
789825

826+
ThemeManager_AddActualThemeChanged(sender, e);
790827
SystemBackdropTypeProperty_ValueChanged(sender, e);
828+
829+
SystemSoundOnLoaded?.Play();
791830
}
792831

793832
private static void TryExecuteCommand(ICommand command, object parameter)

source/iNKORE.UI.WPF.Modern.Controls/Controls/MessageBox/MessageBoxImageExtensions.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Media;
23
using System.Windows;
34

45
namespace iNKORE.UI.WPF.Modern.Extensions
@@ -9,13 +10,28 @@ public static string ToSymbol(this MessageBoxImage image)
910
{
1011
return image switch
1112
{
12-
MessageBoxImage.Error => SegoeIcons.Error,
13+
MessageBoxImage.Error => SegoeIcons.ErrorBadge,
1314
MessageBoxImage.Information => SegoeIcons.Info,
1415
MessageBoxImage.Warning => SegoeIcons.Warning,
1516
MessageBoxImage.Question => SegoeIcons.Unknown,
1617
MessageBoxImage.None => char.Parse("0x2007").ToString(),
17-
_ => throw new NotSupportedException(),
18+
_ => char.Parse("0x2007").ToString(),
1819
};
1920
}
21+
22+
23+
public static SystemSound ToAlertSound(this MessageBoxImage image)
24+
{
25+
return image switch
26+
{
27+
MessageBoxImage.Error => SystemSounds.Hand,
28+
MessageBoxImage.Information => SystemSounds.Asterisk,
29+
MessageBoxImage.Warning => SystemSounds.Exclamation,
30+
MessageBoxImage.Question => SystemSounds.Question,
31+
MessageBoxImage.None => null,
32+
_ => null,
33+
};
34+
35+
}
2036
}
2137
}

source/samples/WpfApp1/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
ui:ThemeManager.IsThemeAware="True" ui:TitleBar.Height="40"
1111
ui:TitleBar.ExtendViewIntoTitleBar="False" ui:WindowHelper.SystemBackdropType="Acrylic"
1212
ui:WindowHelper.UseModernWindowStyle="True"
13-
ui:TitleBar.IsBackButtonVisible="False" ui:ThemeManager.RequestedTheme="Light"
13+
ui:TitleBar.IsBackButtonVisible="False"
1414
ui:WindowHelper.CornerStyle="DoNotRound" Background="Red" Loaded="Window_Loaded">
1515
<Canvas>
1616
<ComboBox SelectionChanged="ComboBox_SelectionChanged" Height="35" IsEditable="True">

source/samples/WpfApp1/MainWindow.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ private void Button_MessageBox_Click(object sender, RoutedEventArgs e)
8181
string title = "Some title";
8282
string message = "This is a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong test text!";
8383

84-
//System.Windows.MessageBox.Show(message, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
85-
//System.Windows.MessageBox.Show("adawdawda", title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
84+
System.Windows.MessageBox.Show(message, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
85+
System.Windows.MessageBox.Show("adawdawda", title, MessageBoxButton.YesNoCancel, MessageBoxImage.Error);
8686

8787

8888
MessageBoxEx.DefaultBackdropType = BackdropType.None;
@@ -99,7 +99,7 @@ private void Button_MessageBox_Click(object sender, RoutedEventArgs e)
9999
MessageBoxEx.DefaultBackdropType = BackdropType.Mica;
100100

101101

102-
MessageBoxEx.Show("redadwada", null, MessageBoxButton.OK, SegoeIcons.Airplane, MessageBoxResult.OK);
102+
MessageBoxEx.Show("redadwada", null, MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
103103
MessageBoxEx.ShowAsync(message, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question).GetAwaiter().GetResult();
104104

105105
MessageBoxEx.DefaultBackdropType = BackdropType.Tabbed;

0 commit comments

Comments
 (0)