using ElectronNET.API.Entities; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using System; using System.Threading.Tasks; namespace ElectronNET.API { public sealed class Dialog { private static Dialog _dialog; internal Dialog() { } internal static Dialog Instance { get { if (_dialog == null) { _dialog = new Dialog(); } return _dialog; } } /// /// Shows a message box, it will block the process until the message box is closed. /// It returns the index of the clicked button. The browserWindow argument allows /// the dialog to attach itself to a parent window, making it modal. If a callback /// is passed, the dialog will not block the process.The API call will be /// asynchronous and the result will be passed via callback(response). /// /// /// The API call will be asynchronous and the result will be passed via MessageBoxResult. public async Task ShowMessageBoxAsync(MessageBoxOptions messageBoxOptions) { return await ShowMessageBoxAsync(null, messageBoxOptions); } /// /// Shows a message box, it will block the process until the message box is closed. /// It returns the index of the clicked button. If a callback /// is passed, the dialog will not block the process. /// /// The browserWindow argument allows the dialog to attach itself to a parent window, making it modal. /// /// The API call will be asynchronous and the result will be passed via MessageBoxResult. public Task ShowMessageBoxAsync(BrowserWindow browserWindow, MessageBoxOptions messageBoxOptions) { var taskCompletionSource = new TaskCompletionSource(); BridgeConnector.Socket.On("showMessageBoxComplete", (args) => { BridgeConnector.Socket.Off("showMessageBoxComplete"); var result = ((JArray)args); taskCompletionSource.SetResult(new MessageBoxResult { Response = (int)result.First, CheckboxChecked = (bool)result.Last }); }); if (browserWindow == null) { BridgeConnector.Socket.Emit("showMessageBox", JObject.FromObject(messageBoxOptions, _jsonSerializer)); } else { BridgeConnector.Socket.Emit("showMessageBox", JObject.FromObject(browserWindow, _jsonSerializer), JObject.FromObject(messageBoxOptions, _jsonSerializer)); } return taskCompletionSource.Task; } private JsonSerializer _jsonSerializer = new JsonSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore }; } }