using ElectronNET.API.Entities; using System; using System.Runtime.Versioning; using System.Text.Json; using System.Threading.Tasks; namespace ElectronNET.API { /// /// Display native system dialogs for opening and saving files, alerting, etc. /// public sealed class Dialog { private static Dialog _dialog; private static object _syncRoot = new object(); internal Dialog() { } internal static Dialog Instance { get { if (_dialog == null) { lock (_syncRoot) { if (_dialog == null) { _dialog = new Dialog(); } } } return _dialog; } } /// /// Note: On Windows and Linux an open dialog can not be both a file selector /// and a directory selector, so if you set properties to ['openFile', 'openDirectory'] /// on these platforms, a directory selector will be shown. /// /// The browserWindow argument allows the dialog to attach itself to a parent window, making it modal. /// /// An array of file paths chosen by the user public Task ShowOpenDialogAsync(BrowserWindow browserWindow, OpenDialogOptions options) { var tcs = new TaskCompletionSource(); var guid = Guid.NewGuid().ToString(); BridgeConnector.Socket.Once("showOpenDialogComplete" + guid, tcs.SetResult); BridgeConnector.Socket.Emit("showOpenDialog", browserWindow, options, guid); return tcs.Task; } /// /// Dialog for save files. /// /// The browserWindow argument allows the dialog to attach itself to a parent window, making it modal. /// /// Returns String, the path of the file chosen by the user, if a callback is provided it returns an empty string. public Task ShowSaveDialogAsync(BrowserWindow browserWindow, SaveDialogOptions options) { var tcs = new TaskCompletionSource(); var guid = Guid.NewGuid().ToString(); BridgeConnector.Socket.Once("showSaveDialogComplete" + guid, tcs.SetResult); BridgeConnector.Socket.Emit("showSaveDialog", browserWindow, options, guid); return tcs.Task; } /// /// 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(string message) { return await this.ShowMessageBoxAsync(null, new MessageBoxOptions(message)).ConfigureAwait(false); } /// /// 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 this.ShowMessageBoxAsync(null, messageBoxOptions).ConfigureAwait(false); } /// /// 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 async Task ShowMessageBoxAsync(BrowserWindow browserWindow, string message) { return await this.ShowMessageBoxAsync(browserWindow, new MessageBoxOptions(message)).ConfigureAwait(false); } /// /// 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 tcs = new TaskCompletionSource(); var guid = Guid.NewGuid().ToString(); BridgeConnector.Socket.Once("showMessageBoxComplete" + guid, (args) => { // args is [response:int, checkboxChecked:boolean] var arr = args.EnumerateArray(); var e = arr.GetEnumerator(); e.MoveNext(); var response = e.Current.GetInt32(); e.MoveNext(); var checkbox = e.Current.GetBoolean(); tcs.SetResult(new MessageBoxResult { Response = response, CheckboxChecked = checkbox }); }); if (browserWindow == null) { BridgeConnector.Socket.Emit("showMessageBox", messageBoxOptions, guid); } else { BridgeConnector.Socket.Emit("showMessageBox", browserWindow, messageBoxOptions, guid); } return tcs.Task; } /// /// Displays a modal dialog that shows an error message. /// /// This API can be called safely before the ready event the app module emits, /// it is usually used to report errors in early stage of startup.If called /// before the app readyevent on Linux, the message will be emitted to stderr, /// and no GUI dialog will appear. /// /// The title to display in the error box. /// The text content to display in the error box. public void ShowErrorBox(string title, string content) { BridgeConnector.Socket.Emit("showErrorBox", title, content); } /// /// On macOS, this displays a modal dialog that shows a message and certificate information, /// and gives the user the option of trusting/importing the certificate. If you provide a /// browserWindow argument the dialog will be attached to the parent window, making it modal. /// /// /// [SupportedOSPlatform("macOS")] [SupportedOSPlatform("Windows")] public Task ShowCertificateTrustDialogAsync(CertificateTrustDialogOptions options) { return ShowCertificateTrustDialogAsync(null, options); } /// /// On macOS, this displays a modal dialog that shows a message and certificate information, /// and gives the user the option of trusting/importing the certificate. If you provide a /// browserWindow argument the dialog will be attached to the parent window, making it modal. /// /// /// /// [SupportedOSPlatform("macOS")] [SupportedOSPlatform("Windows")] public Task ShowCertificateTrustDialogAsync(BrowserWindow browserWindow, CertificateTrustDialogOptions options) { var tcs = new TaskCompletionSource(); string guid = Guid.NewGuid().ToString(); BridgeConnector.Socket.Once("showCertificateTrustDialogComplete" + guid, () => tcs.SetResult()); BridgeConnector.Socket.Emit("showCertificateTrustDialog", browserWindow, options, guid); return tcs.Task; } } }