11using System . ComponentModel ;
2+ using System . Diagnostics . CodeAnalysis ;
23using System . Runtime . Versioning ;
34using TerraFX . Interop . Windows ;
45
56namespace AssetRipper . NativeDialogs ;
67
78public 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
0 commit comments