feat: promisify dialog.showMessageBox() (#17298)
* feat: promisify dialog.showMessageBox() * address feedback from review
This commit is contained in:
parent
ea6a926494
commit
8991c0056e
7 changed files with 234 additions and 191 deletions
|
@ -73,7 +73,7 @@ const checkAppInitialized = function () {
|
|||
const saveDialog = (sync, window, options) => {
|
||||
checkAppInitialized()
|
||||
|
||||
if (window.constructor !== BrowserWindow) options = window
|
||||
if (window && window.constructor !== BrowserWindow) options = window
|
||||
if (options == null) options = { title: 'Save' }
|
||||
|
||||
const {
|
||||
|
@ -100,7 +100,7 @@ const saveDialog = (sync, window, options) => {
|
|||
const openDialog = (sync, window, options) => {
|
||||
checkAppInitialized()
|
||||
|
||||
if (window.constructor !== BrowserWindow) options = window
|
||||
if (window && window.constructor !== BrowserWindow) options = window
|
||||
if (options == null) {
|
||||
options = {
|
||||
title: 'Open',
|
||||
|
@ -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 && window.constructor !== BrowserWindow) options = window
|
||||
if (options == null) options = { type: 'none' }
|
||||
|
||||
let {
|
||||
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()
|
||||
showMessageBox: function (window, options) {
|
||||
return messageBox(false, window, options)
|
||||
},
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue