feat: promisify dialog.showOpenDialog() (#16973)
* feat: promisify dialog.showOpenDialog() * address feedback from review * address feedback from review
This commit is contained in:
parent
7936237677
commit
e05985145b
12 changed files with 235 additions and 154 deletions
|
@ -1,6 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const { app, BrowserWindow } = require('electron')
|
||||
const { app, BrowserWindow, deprecate } = require('electron')
|
||||
const binding = process.atomBinding('dialog')
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
|
||||
|
@ -70,70 +70,54 @@ const checkAppInitialized = function () {
|
|||
}
|
||||
}
|
||||
|
||||
const openDialog = (sync, window, options) => {
|
||||
checkAppInitialized()
|
||||
|
||||
if (window.constructor !== BrowserWindow) options = window
|
||||
if (options == null) {
|
||||
options = {
|
||||
title: 'Open',
|
||||
properties: ['openFile']
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
buttonLabel = '',
|
||||
defaultPath = '',
|
||||
filters = [],
|
||||
properties = ['openFile'],
|
||||
title = '',
|
||||
message = '',
|
||||
securityScopedBookmarks = false
|
||||
} = options
|
||||
|
||||
if (!Array.isArray(properties)) throw new TypeError('Properties must be an array')
|
||||
|
||||
let dialogProperties = 0
|
||||
for (const prop in fileDialogProperties) {
|
||||
if (properties.includes(prop)) {
|
||||
dialogProperties |= fileDialogProperties[prop]
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof title !== 'string') throw new TypeError('Title must be a string')
|
||||
if (typeof buttonLabel !== 'string') throw new TypeError('Button label must be a string')
|
||||
if (typeof defaultPath !== 'string') throw new TypeError('Default path must be a string')
|
||||
if (typeof message !== 'string') throw new TypeError('Message must be a string')
|
||||
|
||||
const settings = { title, buttonLabel, defaultPath, filters, message, securityScopedBookmarks, window }
|
||||
settings.properties = dialogProperties
|
||||
|
||||
return (sync) ? binding.showOpenDialogSync(settings) : binding.showOpenDialog(settings)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
showOpenDialog: function (...args) {
|
||||
checkAppInitialized()
|
||||
|
||||
let [window, options, callback] = parseArgs(...args)
|
||||
|
||||
if (options == null) {
|
||||
options = {
|
||||
title: 'Open',
|
||||
properties: ['openFile']
|
||||
}
|
||||
}
|
||||
|
||||
let { buttonLabel, defaultPath, filters, properties, title, message, securityScopedBookmarks = false } = options
|
||||
|
||||
if (properties == null) {
|
||||
properties = ['openFile']
|
||||
} else if (!Array.isArray(properties)) {
|
||||
throw new TypeError('Properties must be an array')
|
||||
}
|
||||
|
||||
let dialogProperties = 0
|
||||
for (const prop in fileDialogProperties) {
|
||||
if (properties.includes(prop)) {
|
||||
dialogProperties |= fileDialogProperties[prop]
|
||||
}
|
||||
}
|
||||
|
||||
if (title == null) {
|
||||
title = ''
|
||||
} else if (typeof title !== 'string') {
|
||||
throw new TypeError('Title must be a string')
|
||||
}
|
||||
|
||||
if (buttonLabel == null) {
|
||||
buttonLabel = ''
|
||||
} else if (typeof buttonLabel !== 'string') {
|
||||
throw new TypeError('Button label must be a string')
|
||||
}
|
||||
|
||||
if (defaultPath == null) {
|
||||
defaultPath = ''
|
||||
} else if (typeof defaultPath !== 'string') {
|
||||
throw new TypeError('Default path must be a string')
|
||||
}
|
||||
|
||||
if (filters == null) {
|
||||
filters = []
|
||||
}
|
||||
|
||||
if (message == null) {
|
||||
message = ''
|
||||
} else if (typeof message !== 'string') {
|
||||
throw new TypeError('Message must be a string')
|
||||
}
|
||||
|
||||
const wrappedCallback = typeof callback === 'function' ? function (success, result, bookmarkData) {
|
||||
return success ? callback(result, bookmarkData) : callback()
|
||||
} : null
|
||||
const settings = { title, buttonLabel, defaultPath, filters, message, securityScopedBookmarks, window }
|
||||
settings.properties = dialogProperties
|
||||
return binding.showOpenDialog(settings, wrappedCallback)
|
||||
showOpenDialog: function (window, options) {
|
||||
return openDialog(false, window, options)
|
||||
},
|
||||
showOpenDialogSync: function (window, options) {
|
||||
return openDialog(true, window, options)
|
||||
},
|
||||
|
||||
showSaveDialog: function (...args) {
|
||||
checkAppInitialized()
|
||||
|
||||
|
@ -306,6 +290,8 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports.showOpenDialog = deprecate.promisify(module.exports.showOpenDialog)
|
||||
|
||||
// Mark standard asynchronous functions.
|
||||
v8Util.setHiddenValue(module.exports.showMessageBox, 'asynchronous', true)
|
||||
v8Util.setHiddenValue(module.exports.showOpenDialog, 'asynchronous', true)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue