Skip to content

Commit 63132ce

Browse files
committed
Add scaffolding for confirmation and messaging
1 parent dbe43ba commit 63132ce

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

AssetRipper.NativeDialogs.Example/Arguments.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ internal sealed partial class Arguments
2323
[Description("Show the save file dialog.")]
2424
public bool SaveFile { get; set; }
2525

26+
[CommandLineArgument]
27+
[Description("Show the confirmation dialog.")]
28+
public bool Confirmation { get; set; }
29+
2630
[CommandLineArgument]
2731
[Description("Show the message dialog.")]
2832
public bool Message { get; set; }

AssetRipper.NativeDialogs.Example/Program.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,26 @@ static async Task Main(string[] args)
6161
string? file = await SaveFileDialog.SaveFileAsync();
6262
Print(file);
6363
}
64+
else if (arguments.Confirmation)
65+
{
66+
bool? result = await ConfirmationDialog.ConfirmAsync("Are you sure you want to proceed?", "Yes", "No");
67+
switch (result)
68+
{
69+
case true:
70+
Console.WriteLine("Confirmed.");
71+
break;
72+
case false:
73+
Console.WriteLine("Cancelled.");
74+
break;
75+
case null:
76+
Console.WriteLine("No response.");
77+
break;
78+
}
79+
}
6480
else if (arguments.Message)
6581
{
66-
throw new NotImplementedException("Message dialog is not implemented in this example.");
82+
await MessageDialog.MessageAsync("This is a message dialog.", "OK");
83+
Console.WriteLine("Message dialog displayed.");
6784
}
6885
else
6986
{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.Runtime.Versioning;
2+
3+
namespace AssetRipper.NativeDialogs;
4+
5+
public static class ConfirmationDialog
6+
{
7+
public static bool Supported =>
8+
OperatingSystem.IsWindows() ||
9+
OperatingSystem.IsMacOS() ||
10+
(OperatingSystem.IsLinux() && Gtk.Global.IsSupported);
11+
12+
public static Task<bool?> ConfirmAsync(string message, string trueLabel, string falseLabel)
13+
{
14+
if (OperatingSystem.IsWindows())
15+
{
16+
return ConfirmAsyncWindows();
17+
}
18+
else if (OperatingSystem.IsMacOS())
19+
{
20+
return ConfirmAsyncMacOS();
21+
}
22+
else if (OperatingSystem.IsLinux())
23+
{
24+
return ConfirmAsyncLinux();
25+
}
26+
else
27+
{
28+
return Task.FromResult<bool?>(false);
29+
}
30+
}
31+
32+
[SupportedOSPlatform("windows")]
33+
private unsafe static Task<bool?> ConfirmAsyncWindows()
34+
{
35+
return Task.FromResult<bool?>(null);
36+
}
37+
38+
[SupportedOSPlatform("macos")]
39+
private static Task<bool?> ConfirmAsyncMacOS()
40+
{
41+
return Task.FromResult<bool?>(null);
42+
}
43+
44+
[SupportedOSPlatform("linux")]
45+
private static Task<bool?> ConfirmAsyncLinux()
46+
{
47+
if (Gtk.Global.IsSupported)
48+
{
49+
bool? result;
50+
Gtk.Application.Init(); // spins a main loop
51+
try
52+
{
53+
result = null;
54+
}
55+
finally
56+
{
57+
Gtk.Application.Quit(); // stops the main loop
58+
}
59+
60+
return Task.FromResult(result);
61+
}
62+
else
63+
{
64+
// Fallback
65+
return Task.FromResult<bool?>(null);
66+
}
67+
}
68+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System.Runtime.Versioning;
2+
3+
namespace AssetRipper.NativeDialogs;
4+
5+
public static class MessageDialog
6+
{
7+
public static bool Supported =>
8+
OperatingSystem.IsWindows() ||
9+
OperatingSystem.IsMacOS() ||
10+
(OperatingSystem.IsLinux() && Gtk.Global.IsSupported);
11+
12+
public static Task MessageAsync(string message, string label)
13+
{
14+
if (OperatingSystem.IsWindows())
15+
{
16+
return MessageAsyncWindows();
17+
}
18+
else if (OperatingSystem.IsMacOS())
19+
{
20+
return MessageAsyncMacOS();
21+
}
22+
else if (OperatingSystem.IsLinux())
23+
{
24+
return MessageAsyncLinux();
25+
}
26+
else
27+
{
28+
return Task.CompletedTask;
29+
}
30+
}
31+
32+
[SupportedOSPlatform("windows")]
33+
private unsafe static Task MessageAsyncWindows()
34+
{
35+
return Task.CompletedTask;
36+
}
37+
38+
[SupportedOSPlatform("macos")]
39+
private static Task MessageAsyncMacOS()
40+
{
41+
return Task.CompletedTask;
42+
}
43+
44+
[SupportedOSPlatform("linux")]
45+
private static Task MessageAsyncLinux()
46+
{
47+
if (Gtk.Global.IsSupported)
48+
{
49+
Gtk.Application.Init(); // spins a main loop
50+
try
51+
{
52+
}
53+
finally
54+
{
55+
Gtk.Application.Quit(); // stops the main loop
56+
}
57+
58+
return Task.CompletedTask;
59+
}
60+
else
61+
{
62+
// Fallback
63+
return Task.CompletedTask;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)