diff --git a/lib/browser/api/dialog.js b/lib/browser/api/dialog.js index 272735fbea031..85e5b6515144b 100644 --- a/lib/browser/api/dialog.js +++ b/lib/browser/api/dialog.js @@ -138,6 +138,62 @@ const openDialog = (sync, window, options) => { return (sync) ? binding.showOpenDialogSync(settings) : binding.showOpenDialog(settings) } +const messageBox = (sync, window, options) => { + checkAppInitialized() + + if (window.constructor !== BrowserWindow) options = window + if (options == null) options = { type: 'none' } + + const { + buttons = [], + cancelId, + checkboxLabel = '', + checkboxChecked, + defaultId = -1, + detail = '', + icon = null, + message = '', + title = '', + type = 'none' + } = options + + const messageBoxType = messageBoxTypes.indexOf(type) + if (messageBoxType === -1) throw new TypeError('Invalid message box type') + if (!Array.isArray(buttons)) throw new TypeError('Buttons must be an array') + if (options.normalizeAccessKeys) buttons = buttons.map(normalizeAccessKey) + if (typeof title !== 'string') throw new TypeError('Title must be a string') + if (typeof message !== 'string') throw new TypeError('Message must be a string') + if (typeof detail !== 'string') throw new TypeError('Detail must be a string') + if (typeof checkboxLabel !== 'string') throw new TypeError('checkboxLabel must be a string') + + checkboxChecked = !!checkboxChecked + + // Choose a default button to get selected when dialog is cancelled. + if (cancelId == null) { + // If the defaultId is set to 0, ensure the cancel button is a different index (1) + cancelId = (defaultId === 0 && buttons.length > 1) ? 1 : 0 + for (let i = 0; i < buttons.length; i++) { + const text = buttons[i].toLowerCase() + if (text === 'cancel' || text === 'no') { + cancelId = i + break + } + } + } + + const flags = options.noLink ? messageBoxOptions.noLink : 0 + + if (sync) { + return binding.showMessageBoxSync(messageBoxType, buttons, + defaultId, cancelId, flags, title, message, detail, + checkboxLabel, checkboxChecked, icon, window) + } else { + return binding.showMessageBox(messageBoxType, buttons, + defaultId, cancelId, flags, title, message, detail, + checkboxLabel, checkboxChecked, icon, window) + } +} + module.exports = { showOpenDialog: function (window, options) { return openDialog(false, window, options) @@ -155,92 +211,12 @@ module.exports = { return saveDialog(true, window, options) }, - showMessageBox: function (...args) { - checkAppInitialized() - - let [window, options, callback] = parseArgs(...args) - - if (options == null) { - options = { - type: 'none' - } - } - - let { - buttons, cancelId, checkboxLabel, checkboxChecked, defaultId, detail, - icon, message, title, type - } = options - - if (type == null) { - type = 'none' - } - - const messageBoxType = messageBoxTypes.indexOf(type) - if (messageBoxType === -1) { - throw new TypeError('Invalid message box type') - } - - if (buttons == null) { - buttons = [] - } else if (!Array.isArray(buttons)) { - throw new TypeError('Buttons must be an array') - } - - if (options.normalizeAccessKeys) { - buttons = buttons.map(normalizeAccessKey) - } - - if (title == null) { - title = '' - } else if (typeof title !== 'string') { - throw new TypeError('Title must be a string') - } - - if (message == null) { - message = '' - } else if (typeof message !== 'string') { - throw new TypeError('Message must be a string') - } - - if (detail == null) { - detail = '' - } else if (typeof detail !== 'string') { - throw new TypeError('Detail must be a string') - } - - checkboxChecked = !!checkboxChecked - - if (checkboxLabel == null) { - checkboxLabel = '' - } else if (typeof checkboxLabel !== 'string') { - throw new TypeError('checkboxLabel must be a string') - } - - if (icon == null) { - icon = null - } - - if (defaultId == null) { - defaultId = -1 - } - - // Choose a default button to get selected when dialog is cancelled. - if (cancelId == null) { - // If the defaultId is set to 0, ensure the cancel button is a different index (1) - cancelId = (defaultId === 0 && buttons.length > 1) ? 1 : 0 - for (let i = 0; i < buttons.length; i++) { - const text = buttons[i].toLowerCase() - if (text === 'cancel' || text === 'no') { - cancelId = i - break - } - } - } + showMessageBox: function (window, options) { + return messageBox(false, window, options) + }, - const flags = options.noLink ? messageBoxOptions.noLink : 0 - return binding.showMessageBox(messageBoxType, buttons, defaultId, cancelId, - flags, title, message, detail, checkboxLabel, - checkboxChecked, icon, window, callback) + showMessageBoxSync: function (window, options) { + return messageBox(true, window, options) }, showErrorBox: function (...args) { @@ -269,6 +245,7 @@ module.exports = { } } +module.exports.showMessageBox = deprecate.promisify(module.exports.showMessageBox) module.exports.showOpenDialog = deprecate.promisify(module.exports.showOpenDialog) module.exports.showSaveDialog = deprecate.promisify(module.exports.showSaveDialog)