|
1 | 1 | using System.ComponentModel; |
| 2 | +using System.Diagnostics; |
2 | 3 | using System.Diagnostics.CodeAnalysis; |
3 | 4 | using System.Runtime.Versioning; |
4 | 5 | using TerraFX.Interop.Windows; |
@@ -111,51 +112,88 @@ public Options(string message, Type type = Type.OkCancel) |
111 | 112 | } |
112 | 113 |
|
113 | 114 | [SupportedOSPlatform("linux")] |
114 | | - private static Task<bool?> ConfirmLinux(Options options) |
| 115 | + private static async Task<bool?> ConfirmLinux(Options options) |
115 | 116 | { |
116 | | - if (Gtk.Global.IsSupported) |
| 117 | + string escapedMessage = ProcessExecutor.EscapeString(options.Message); |
| 118 | + if (await LinuxHelper.HasZenity()) |
117 | 119 | { |
118 | | - return ConfirmLinuxGtk(options); |
| 120 | + Process process = new() |
| 121 | + { |
| 122 | + StartInfo = new() |
| 123 | + { |
| 124 | + FileName = "zenity", |
| 125 | + RedirectStandardOutput = true, |
| 126 | + RedirectStandardError = true, |
| 127 | + }, |
| 128 | + }; |
| 129 | + |
| 130 | + process.StartInfo.ArgumentList.Add("--question"); |
| 131 | + process.StartInfo.ArgumentList.Add("--text"); |
| 132 | + process.StartInfo.ArgumentList.Add(escapedMessage); |
| 133 | + if (options.Type != Type.YesNo) |
| 134 | + { |
| 135 | + string escapedTrueLabel = ProcessExecutor.EscapeString(options.TrueLabel); |
| 136 | + string escapedFalseLabel = ProcessExecutor.EscapeString(options.FalseLabel); |
| 137 | + |
| 138 | + process.StartInfo.ArgumentList.Add("--ok-label"); |
| 139 | + process.StartInfo.ArgumentList.Add(escapedTrueLabel); |
| 140 | + process.StartInfo.ArgumentList.Add("--cancel-label"); |
| 141 | + process.StartInfo.ArgumentList.Add(escapedFalseLabel); |
| 142 | + } |
| 143 | + |
| 144 | + process.BeginOutputReadLine(); |
| 145 | + process.BeginErrorReadLine(); |
| 146 | + |
| 147 | + if (!process.Start()) |
| 148 | + { |
| 149 | + return null; // Failed to start the process |
| 150 | + } |
| 151 | + |
| 152 | + await process.WaitForExitAsync(); |
| 153 | + |
| 154 | + return process.ExitCode switch |
| 155 | + { |
| 156 | + 0 => true, // User clicked OK or Yes |
| 157 | + 1 => false, // User clicked Cancel or No |
| 158 | + _ => null, // An error occurred |
| 159 | + }; |
119 | 160 | } |
120 | | - else |
| 161 | + else if (await LinuxHelper.HasKDialog()) |
121 | 162 | { |
122 | | - // Fallback |
123 | | - return Task.FromResult<bool?>(null); |
124 | | - } |
125 | | - } |
| 163 | + Process process = new() |
| 164 | + { |
| 165 | + StartInfo = new() |
| 166 | + { |
| 167 | + FileName = "kdialog", |
| 168 | + RedirectStandardOutput = true, |
| 169 | + RedirectStandardError = true, |
| 170 | + }, |
| 171 | + }; |
126 | 172 |
|
127 | | - [SupportedOSPlatform("linux")] |
128 | | - private static async Task<bool?> ConfirmLinuxGtk(Options options) |
129 | | - { |
130 | | - bool? result; |
131 | | - while (!GtkHelper.TryInitialize()) |
132 | | - { |
133 | | - await GtkHelper.Delay(); // Wait for the GTK initialization to complete |
134 | | - } |
| 173 | + process.StartInfo.ArgumentList.Add(options.Type is Type.YesNo ? "--yesno" : "--okcancel"); |
| 174 | + process.StartInfo.ArgumentList.Add(escapedMessage); |
135 | 175 |
|
136 | | - try |
137 | | - { |
138 | | - using Gtk.MessageDialog md = new( |
139 | | - null, |
140 | | - Gtk.DialogFlags.Modal, |
141 | | - Gtk.MessageType.Info, |
142 | | - options.Type == Type.OkCancel ? Gtk.ButtonsType.OkCancel : Gtk.ButtonsType.YesNo, |
143 | | - options.Message |
144 | | - ); |
| 176 | + process.BeginOutputReadLine(); |
| 177 | + process.BeginErrorReadLine(); |
| 178 | + |
| 179 | + if (!process.Start()) |
| 180 | + { |
| 181 | + return null; // Failed to start the process |
| 182 | + } |
| 183 | + |
| 184 | + await process.WaitForExitAsync(); |
145 | 185 |
|
146 | | - int response = md.Run(); |
147 | | - result = response switch |
| 186 | + return process.ExitCode switch |
148 | 187 | { |
149 | | - (int)Gtk.ResponseType.Ok or (int)Gtk.ResponseType.Yes => true, |
150 | | - (int)Gtk.ResponseType.Cancel or (int)Gtk.ResponseType.No => false, |
151 | | - _ => throw new($"Unexpected response type: {response}"), |
| 188 | + 0 => true, // User clicked OK or Yes |
| 189 | + 1 => false, // User clicked Cancel or No |
| 190 | + _ => null, // An error occurred |
152 | 191 | }; |
153 | 192 | } |
154 | | - finally |
| 193 | + else |
155 | 194 | { |
156 | | - GtkHelper.Shutdown(); // Ensure GTK is properly shut down after use |
| 195 | + // Fallback |
| 196 | + return null; |
157 | 197 | } |
158 | | - |
159 | | - return result; |
160 | 198 | } |
161 | 199 | } |
0 commit comments