|
1 | | -using System.Runtime.Versioning; |
| 1 | +using System.ComponentModel; |
| 2 | +using System.Runtime.Versioning; |
| 3 | +using TerraFX.Interop.Windows; |
2 | 4 |
|
3 | 5 | namespace AssetRipper.NativeDialogs; |
4 | 6 |
|
@@ -37,7 +39,34 @@ public static class ConfirmationDialog |
37 | 39 | [SupportedOSPlatform("windows")] |
38 | 40 | private unsafe static Task<bool?> ConfirmWindows(string message, string trueLabel, string falseLabel) |
39 | 41 | { |
40 | | - return Task.FromResult<bool?>(null); |
| 42 | + int statusCode; |
| 43 | + fixed (char* messagePtr = message) |
| 44 | + { |
| 45 | + // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw |
| 46 | + statusCode = Windows.MessageBoxW( |
| 47 | + default, // No owner window. |
| 48 | + messagePtr, // Message text. |
| 49 | + null, // Title. |
| 50 | + MB.MB_OKCANCEL | MB.MB_ICONQUESTION); // OK button and information icon. |
| 51 | + } |
| 52 | + |
| 53 | + if (statusCode == 0) |
| 54 | + { |
| 55 | + int errorCode = unchecked((int)Windows.GetLastError()); |
| 56 | + throw new Win32Exception(errorCode, "Failed to show message dialog."); |
| 57 | + } |
| 58 | + else if (statusCode is Windows.IDYES or Windows.IDOK) |
| 59 | + { |
| 60 | + return Task.FromResult<bool?>(true); |
| 61 | + } |
| 62 | + else if (statusCode is Windows.IDNO or Windows.IDCANCEL) |
| 63 | + { |
| 64 | + return Task.FromResult<bool?>(false); |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + throw new($"Unexpected status code {statusCode} from {nameof(Windows.MessageBoxW)}."); |
| 69 | + } |
41 | 70 | } |
42 | 71 |
|
43 | 72 | [SupportedOSPlatform("macos")] |
@@ -72,7 +101,20 @@ public static class ConfirmationDialog |
72 | 101 | Gtk.Application.Init(); // spins a main loop |
73 | 102 | try |
74 | 103 | { |
75 | | - result = null; |
| 104 | + using Gtk.MessageDialog md = new( |
| 105 | + null, |
| 106 | + Gtk.DialogFlags.Modal, |
| 107 | + Gtk.MessageType.Info, |
| 108 | + Gtk.ButtonsType.Ok, |
| 109 | + message |
| 110 | + ); |
| 111 | + int response = md.Run(); |
| 112 | + result = response switch |
| 113 | + { |
| 114 | + (int)Gtk.ResponseType.Ok or (int)Gtk.ResponseType.Yes => true, |
| 115 | + (int)Gtk.ResponseType.Cancel or (int)Gtk.ResponseType.No => false, |
| 116 | + _ => throw new($"Unexpected response type: {response}"), |
| 117 | + }; |
76 | 118 | } |
77 | 119 | finally |
78 | 120 | { |
|
0 commit comments