Skip to content

Commit 763abe2

Browse files
committed
Implement options for messages and confirmations
1 parent 314eb23 commit 763abe2

File tree

3 files changed

+71
-30
lines changed

3 files changed

+71
-30
lines changed

AssetRipper.NativeDialogs.Example/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static async Task Main(string[] args)
6969
}
7070
else if (arguments.Confirmation)
7171
{
72-
bool? result = await ConfirmationDialog.Confirm("Are you sure you want to proceed?", "Yes", "No");
72+
bool? result = await ConfirmationDialog.Confirm(new("Are you sure you want to proceed?", ConfirmationDialog.Type.YesNo));
7373
switch (result)
7474
{
7575
case true:

AssetRipper.NativeDialogs/ConfirmationDialog.cs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
using System.ComponentModel;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Runtime.Versioning;
34
using TerraFX.Interop.Windows;
45

56
namespace AssetRipper.NativeDialogs;
67

78
public static class ConfirmationDialog
89
{
9-
public static Task<bool?> Confirm(string message, string trueLabel, string falseLabel)
10+
public enum Type
1011
{
11-
ArgumentException.ThrowIfNullOrEmpty(message);
12-
ArgumentException.ThrowIfNullOrEmpty(trueLabel);
13-
ArgumentException.ThrowIfNullOrEmpty(falseLabel);
14-
ArgumentOutOfRangeException.ThrowIfEqual(trueLabel, falseLabel);
12+
OkCancel,
13+
YesNo,
14+
}
15+
16+
public readonly struct Options
17+
{
18+
public required string Message { get; init; }
19+
public Type Type { get; init; } = Type.OkCancel;
20+
internal string TrueLabel => Type == Type.OkCancel ? "OK" : "Yes";
21+
internal string FalseLabel => Type == Type.OkCancel ? "Cancel" : "No";
22+
23+
public Options()
24+
{
25+
}
26+
27+
[SetsRequiredMembers]
28+
public Options(string message, Type type = Type.OkCancel)
29+
{
30+
Message = message;
31+
Type = type;
32+
}
33+
}
34+
35+
public static Task<bool?> Confirm(Options options)
36+
{
37+
ArgumentException.ThrowIfNullOrEmpty(options.Message);
1538

1639
if (OperatingSystem.IsWindows())
1740
{
18-
return ConfirmWindows(message, trueLabel, falseLabel);
41+
return ConfirmWindows(options);
1942
}
2043
else if (OperatingSystem.IsMacOS())
2144
{
22-
return ConfirmMacOS(message, trueLabel, falseLabel);
45+
return ConfirmMacOS(options);
2346
}
2447
else if (OperatingSystem.IsLinux())
2548
{
26-
return ConfirmLinux(message, trueLabel, falseLabel);
49+
return ConfirmLinux(options);
2750
}
2851
else
2952
{
@@ -32,17 +55,17 @@ public static class ConfirmationDialog
3255
}
3356

3457
[SupportedOSPlatform("windows")]
35-
private unsafe static Task<bool?> ConfirmWindows(string message, string trueLabel, string falseLabel)
58+
private unsafe static Task<bool?> ConfirmWindows(Options options)
3659
{
3760
int statusCode;
38-
fixed (char* messagePtr = message)
61+
fixed (char* messagePtr = options.Message)
3962
{
4063
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw
4164
statusCode = Windows.MessageBoxW(
4265
default, // No owner window.
4366
messagePtr, // Message text.
4467
null, // Title.
45-
MB.MB_OKCANCEL | MB.MB_ICONQUESTION); // OK button and information icon.
68+
(options.Type == Type.OkCancel ? (uint)MB.MB_OKCANCEL : MB.MB_YESNO) | MB.MB_ICONQUESTION); // OK button and information icon.
4669
}
4770

4871
if (statusCode == 0)
@@ -65,19 +88,19 @@ public static class ConfirmationDialog
6588
}
6689

6790
[SupportedOSPlatform("macos")]
68-
private static async Task<bool?> ConfirmMacOS(string message, string trueLabel, string falseLabel)
91+
private static async Task<bool?> ConfirmMacOS(Options options)
6992
{
70-
string escapedMessage = ProcessExecutor.EscapeString(message);
71-
string escapedTrueLabel = ProcessExecutor.EscapeString(trueLabel);
72-
string escapedFalseLabel = ProcessExecutor.EscapeString(falseLabel);
93+
string escapedMessage = ProcessExecutor.EscapeString(options.Message);
94+
string escapedTrueLabel = ProcessExecutor.EscapeString(options.TrueLabel);
95+
string escapedFalseLabel = ProcessExecutor.EscapeString(options.FalseLabel);
7396
string? result = await ProcessExecutor.TryRun("osascript",
7497
"-e", $"display dialog \"{escapedMessage}\" buttons {{\"{escapedFalseLabel}\", \"{escapedTrueLabel}\"}} default button \"{escapedTrueLabel}\"",
7598
"-e", "button returned of result");
76-
if (result == trueLabel)
99+
if (result == options.TrueLabel)
77100
{
78101
return true;
79102
}
80-
else if (result == falseLabel)
103+
else if (result == options.FalseLabel)
81104
{
82105
return false;
83106
}
@@ -88,7 +111,7 @@ public static class ConfirmationDialog
88111
}
89112

90113
[SupportedOSPlatform("linux")]
91-
private static Task<bool?> ConfirmLinux(string message, string trueLabel, string falseLabel)
114+
private static Task<bool?> ConfirmLinux(Options options)
92115
{
93116
if (Gtk.Global.IsSupported)
94117
{
@@ -101,7 +124,7 @@ public static class ConfirmationDialog
101124
Gtk.DialogFlags.Modal,
102125
Gtk.MessageType.Info,
103126
Gtk.ButtonsType.Ok,
104-
message
127+
options.Message
105128
);
106129
int response = md.Run();
107130
result = response switch

AssetRipper.NativeDialogs/MessageDialog.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
using System.ComponentModel;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.Runtime.Versioning;
34
using TerraFX.Interop.Windows;
45

56
namespace AssetRipper.NativeDialogs;
67

78
public static class MessageDialog
89
{
9-
public static Task Message(string message)
10+
public readonly struct Options
11+
{
12+
public required string Message { get; init; }
13+
14+
public Options()
15+
{
16+
}
17+
18+
[SetsRequiredMembers]
19+
public Options(string message)
20+
{
21+
Message = message;
22+
}
23+
}
24+
25+
public static Task Message(string message) => Message(new Options(message));
26+
27+
public static Task Message(Options options)
1028
{
1129
if (OperatingSystem.IsWindows())
1230
{
13-
return MessageWindows(message);
31+
return MessageWindows(options);
1432
}
1533
else if (OperatingSystem.IsMacOS())
1634
{
17-
return MessageMacOS(message);
35+
return MessageMacOS(options);
1836
}
1937
else if (OperatingSystem.IsLinux())
2038
{
21-
return MessageLinux(message);
39+
return MessageLinux(options);
2240
}
2341
else
2442
{
@@ -27,10 +45,10 @@ public static Task Message(string message)
2745
}
2846

2947
[SupportedOSPlatform("windows")]
30-
private unsafe static Task MessageWindows(string message)
48+
private unsafe static Task MessageWindows(Options options)
3149
{
3250
int statusCode;
33-
fixed (char* messagePtr = message)
51+
fixed (char* messagePtr = options.Message)
3452
{
3553
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw
3654
statusCode = Windows.MessageBoxW(
@@ -54,14 +72,14 @@ private unsafe static Task MessageWindows(string message)
5472
}
5573

5674
[SupportedOSPlatform("macos")]
57-
private static Task MessageMacOS(string message)
75+
private static Task MessageMacOS(Options options)
5876
{
59-
string escapedMessage = ProcessExecutor.EscapeString(message);
77+
string escapedMessage = ProcessExecutor.EscapeString(options.Message);
6078
return ProcessExecutor.TryRun("osascript", "-e", $"display dialog \"{escapedMessage}\" buttons {{\"OK\"}}");
6179
}
6280

6381
[SupportedOSPlatform("linux")]
64-
private static Task MessageLinux(string message)
82+
private static Task MessageLinux(Options options)
6583
{
6684
if (Gtk.Global.IsSupported)
6785
{
@@ -73,7 +91,7 @@ private static Task MessageLinux(string message)
7391
Gtk.DialogFlags.Modal,
7492
Gtk.MessageType.Info,
7593
Gtk.ButtonsType.Ok,
76-
message
94+
options.Message
7795
);
7896
md.Run();
7997
}

0 commit comments

Comments
 (0)