Skip to content

Commit

Permalink
Merge pull request #44 from JeremyAnsel/message_box
Browse files Browse the repository at this point in the history
Implement ApplicationCommands.Copy in MessageBox
  • Loading branch information
NotYoojun authored Feb 25, 2024
2 parents de728b5 + 37b58f8 commit fe050d0
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -16,6 +18,7 @@
using System.Windows.Interop;
using System.Windows.Media;
using static iNKORE.UI.WPF.Modern.Controls.LocalizedDialogCommands;
using System.Diagnostics;

namespace iNKORE.UI.WPF.Modern.Controls
{
Expand Down Expand Up @@ -48,6 +51,8 @@ static MessageBox()

public MessageBox()
{
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, new ExecutedRoutedEventHandler(ExecuteCopy)));

SetValue(TemplateSettingsPropertyKey, new MessageBoxTemplateSettings());
var handler = new RoutedEventHandler((sender, e) => ApplyDarkMode());
ThemeManager.AddActualThemeChangedHandler(this, handler);
Expand All @@ -58,6 +63,76 @@ public MessageBox()
ThemeManager.AddActualThemeChangedHandler(this, ThemeManager_AddActualThemeChanged);
}

private void ExecuteCopy(object sender, ExecutedRoutedEventArgs e)
{
const string longlines = "---------------------------";
StringBuilder sb = new();
sb.Append(longlines);
sb.AppendLine();
sb.Append(Caption);
sb.AppendLine();
sb.Append(longlines);
sb.AppendLine();
sb.Append(Content);
sb.AppendLine();
sb.Append(longlines);
sb.AppendLine();
//switch (MessageBoxButtons)
//{
// case MessageBoxButton.OK:
// sb.Append(OKButtonText);
// break;
// case MessageBoxButton.OKCancel:
// sb.Append(OKButtonText + " " + CancelButtonText);
// break;
// case MessageBoxButton.YesNo:
// sb.Append(YesButtonText + " " + NoButtonText);
// break;
// case MessageBoxButton.YesNoCancel:
// sb.Append(YesButtonText + " " + NoButtonText + " " + CancelButtonText);
// break;
//}

bool isFirstButtonLoaded = true;
var buttons = new Button[]
{
OKButton,
YesButton,
NoButton,
CancelButton,
};

foreach(var button in buttons)
{
if(button.Visibility == Visibility.Visible)
{
if (!isFirstButtonLoaded)
{
sb.Append(" ");
}

sb.Append(button.Content.ToString());
isFirstButtonLoaded = false;
}
}

sb.AppendLine();
sb.Append(longlines);

try
{
new UIPermission(UIPermissionClipboard.AllClipboard).Demand();
Clipboard.SetText(sb.ToString());
}
catch (SecurityException)
{
if (Debugger.IsAttached)
{
throw;
}
}
}

private void ThemeManager_AddActualThemeChanged(object sender, RoutedEventArgs e)
{
if(WindowHelper.GetSystemBackdropType(this) != BackdropType.None)
Expand Down

0 comments on commit fe050d0

Please sign in to comment.