Skip to content

Commit

Permalink
Fix messagebox backdrop in dark mode & add sound
Browse files Browse the repository at this point in the history
  • Loading branch information
NotYoojun committed Dec 26, 2023
1 parent 5590d56 commit 9d673fc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using iNKORE.UI.WPF.Modern.Extensions;
using System;
using System.Linq;
using System.Media;
using System.Threading.Tasks;
using System.Windows;

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

/// <summary>
/// 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.
Expand All @@ -216,8 +217,8 @@ public partial class MessageBox
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
/// <returns>A <see cref="MessageBoxResult"/> value that specifies which message box button is clicked by the user.</returns>
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult) =>
Show(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult);
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult, SystemSound sound = null) =>
Show(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult, sound);

/// <summary>
/// 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.
Expand All @@ -230,7 +231,7 @@ public partial class MessageBox
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
/// <returns>A <see cref="MessageBoxResult"/> value that specifies which message box button is clicked by the user.</returns>
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult)
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult, SystemSound? sound = null)
{
if (owner is null)
{
Expand All @@ -248,6 +249,11 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string
WindowStartupLocation = owner is null ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner
};

if (MakeSound)
{
window.SystemSoundOnLoaded = sound;
}

return window.ShowDialog();
}

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

/// <summary>
/// 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.
Expand All @@ -460,8 +466,8 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
/// <returns>An asynchronous operation showing the message box. When complete, returns a <see cref="MessageBoxResult"/>.</returns>
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult) =>
ShowAsync(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult);
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, string icon, MessageBoxResult? defaultResult, SystemSound sound = null) =>
ShowAsync(owner, messageBoxText, caption, button, new FontIconSource { Glyph = icon, FontSize = 30 }, defaultResult, sound);

/// <summary>
/// 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.
Expand All @@ -474,7 +480,7 @@ public static MessageBoxResult Show(Window owner, string messageBoxText, string
/// <param name="defaultResult">A <see cref="MessageBoxResult"/> value that specifies the default result of the message box.</param>
/// <returns>An asynchronous operation showing the message box. When complete, returns a <see cref="MessageBoxResult"/>.</returns>
/// <remarks>By default, the message box appears in front of the window that is currently active.</remarks>
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult)
public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxText, string caption, MessageBoxButton button, IconSource icon, MessageBoxResult? defaultResult, SystemSound sound = null)
{
TaskCompletionSource<MessageBoxResult> taskSource = new TaskCompletionSource<MessageBoxResult>(
#if !NET452
Expand All @@ -484,7 +490,7 @@ public static Task<MessageBoxResult> ShowAsync(Window owner, string messageBoxTe

Application.Current.Dispatcher.Invoke(() =>
{
MessageBoxResult result = Show(owner, messageBoxText, caption, button, icon, defaultResult);
MessageBoxResult result = Show(owner, messageBoxText, caption, button, icon, defaultResult, sound);
taskSource.TrySetResult(result);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -36,6 +37,8 @@ public MessageBoxResult Result

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

public static bool MakeSound { get; set; } = true;

static MessageBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageBox), new FrameworkPropertyMetadata(typeof(MessageBox)));
Expand All @@ -52,6 +55,22 @@ public MessageBox()
Loaded += On_Loaded;

SystemBackdropTypeProperty_Descriptor.AddValueChanged(this, SystemBackdropTypeProperty_ValueChanged);
ThemeManager.AddActualThemeChangedHandler(this, ThemeManager_AddActualThemeChanged);
}

private void ThemeManager_AddActualThemeChanged(object sender, RoutedEventArgs e)
{
if(WindowHelper.GetSystemBackdropType(this) != BackdropType.None)
{
if(ThemeManager.GetActualTheme(this) == ElementTheme.Dark)
{
MicaHelper.ApplyDarkMode(this);
}
else
{
MicaHelper.RemoveDarkMode(this);
}
}
}

private void SystemBackdropTypeProperty_ValueChanged(object sender, EventArgs e)
Expand All @@ -73,6 +92,23 @@ private void SystemBackdropTypeProperty_ValueChanged(object sender, EventArgs e)
}
}

#region SystemSoundOnLoaded

public static readonly DependencyProperty SystemSoundOnLoadedProperty =
DependencyProperty.Register(
nameof(SystemSoundOnLoaded),
typeof(SystemSound),
typeof(MessageBox));

public SystemSound SystemSoundOnLoaded
{
get => (SystemSound)GetValue(SystemSoundOnLoadedProperty);
set => SetValue(SystemSoundOnLoadedProperty, value);
}

#endregion


#region Caption

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

ThemeManager_AddActualThemeChanged(sender, e);
SystemBackdropTypeProperty_ValueChanged(sender, e);

SystemSoundOnLoaded?.Play();
}

private static void TryExecuteCommand(ICommand command, object parameter)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Media;
using System.Windows;

namespace iNKORE.UI.WPF.Modern.Extensions
Expand All @@ -9,13 +10,28 @@ public static string ToSymbol(this MessageBoxImage image)
{
return image switch
{
MessageBoxImage.Error => SegoeIcons.Error,
MessageBoxImage.Error => SegoeIcons.ErrorBadge,
MessageBoxImage.Information => SegoeIcons.Info,
MessageBoxImage.Warning => SegoeIcons.Warning,
MessageBoxImage.Question => SegoeIcons.Unknown,
MessageBoxImage.None => char.Parse("0x2007").ToString(),
_ => throw new NotSupportedException(),
_ => char.Parse("0x2007").ToString(),
};
}


public static SystemSound ToAlertSound(this MessageBoxImage image)
{
return image switch
{
MessageBoxImage.Error => SystemSounds.Hand,
MessageBoxImage.Information => SystemSounds.Asterisk,
MessageBoxImage.Warning => SystemSounds.Exclamation,
MessageBoxImage.Question => SystemSounds.Question,
MessageBoxImage.None => null,
_ => null,
};

}
}
}
2 changes: 1 addition & 1 deletion source/samples/WpfApp1/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ui:ThemeManager.IsThemeAware="True" ui:TitleBar.Height="40"
ui:TitleBar.ExtendViewIntoTitleBar="False" ui:WindowHelper.SystemBackdropType="Acrylic"
ui:WindowHelper.UseModernWindowStyle="True"
ui:TitleBar.IsBackButtonVisible="False" ui:ThemeManager.RequestedTheme="Light"
ui:TitleBar.IsBackButtonVisible="False"
ui:WindowHelper.CornerStyle="DoNotRound" Background="Red" Loaded="Window_Loaded">
<Canvas>
<ComboBox SelectionChanged="ComboBox_SelectionChanged" Height="35" IsEditable="True">
Expand Down
6 changes: 3 additions & 3 deletions source/samples/WpfApp1/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ private void Button_MessageBox_Click(object sender, RoutedEventArgs e)
string title = "Some title";
string message = "This is a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong test text!";

//System.Windows.MessageBox.Show(message, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
//System.Windows.MessageBox.Show("adawdawda", title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
System.Windows.MessageBox.Show(message, title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
System.Windows.MessageBox.Show("adawdawda", title, MessageBoxButton.YesNoCancel, MessageBoxImage.Error);


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


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

MessageBoxEx.DefaultBackdropType = BackdropType.Tabbed;
Expand Down

0 comments on commit 9d673fc

Please sign in to comment.