feat: promisify dialog.showSaveDialog() (#17054)
* feat: promisify dialog.showSaveDialog() * address some feedback from review * filename => filePath * fix last filename => filePath
This commit is contained in:
		
					parent
					
						
							
								92c9dbc179
							
						
					
				
			
			
				commit
				
					
						6cb7b8d3a4
					
				
			
		
					 11 changed files with 166 additions and 135 deletions
				
			
		| 
						 | 
				
			
			@ -70,6 +70,33 @@ const checkAppInitialized = function () {
 | 
			
		|||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const saveDialog = (sync, window, options) => {
 | 
			
		||||
  checkAppInitialized()
 | 
			
		||||
 | 
			
		||||
  if (window.constructor !== BrowserWindow) options = window
 | 
			
		||||
  if (options == null) options = { title: 'Save' }
 | 
			
		||||
 | 
			
		||||
  const {
 | 
			
		||||
    buttonLabel = '',
 | 
			
		||||
    defaultPath = '',
 | 
			
		||||
    filters = [],
 | 
			
		||||
    title = '',
 | 
			
		||||
    message = '',
 | 
			
		||||
    securityScopedBookmarks = false,
 | 
			
		||||
    nameFieldLabel = '',
 | 
			
		||||
    showsTagField = true
 | 
			
		||||
  } = options
 | 
			
		||||
 | 
			
		||||
  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')
 | 
			
		||||
  if (typeof nameFieldLabel !== 'string') throw new TypeError('Name field label must be a string')
 | 
			
		||||
 | 
			
		||||
  const settings = { buttonLabel, defaultPath, filters, title, message, securityScopedBookmarks, nameFieldLabel, showsTagField, window }
 | 
			
		||||
  return (sync) ? binding.showSaveDialogSync(settings) : binding.showSaveDialog(settings)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const openDialog = (sync, window, options) => {
 | 
			
		||||
  checkAppInitialized()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -115,65 +142,17 @@ module.exports = {
 | 
			
		|||
  showOpenDialog: function (window, options) {
 | 
			
		||||
    return openDialog(false, window, options)
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  showOpenDialogSync: function (window, options) {
 | 
			
		||||
    return openDialog(true, window, options)
 | 
			
		||||
  },
 | 
			
		||||
  showSaveDialog: function (...args) {
 | 
			
		||||
    checkAppInitialized()
 | 
			
		||||
 | 
			
		||||
    let [window, options, callback] = parseArgs(...args)
 | 
			
		||||
  showSaveDialog: function (window, options) {
 | 
			
		||||
    return saveDialog(false, window, options)
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
    if (options == null) {
 | 
			
		||||
      options = {
 | 
			
		||||
        title: 'Save'
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let { buttonLabel, defaultPath, filters, title, message, securityScopedBookmarks = false, nameFieldLabel, showsTagField } = options
 | 
			
		||||
 | 
			
		||||
    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')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (nameFieldLabel == null) {
 | 
			
		||||
      nameFieldLabel = ''
 | 
			
		||||
    } else if (typeof nameFieldLabel !== 'string') {
 | 
			
		||||
      throw new TypeError('Name field label must be a string')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (showsTagField == null) {
 | 
			
		||||
      showsTagField = true
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const wrappedCallback = typeof callback === 'function' ? function (success, result, bookmarkData) {
 | 
			
		||||
      return success ? callback(result, bookmarkData) : callback()
 | 
			
		||||
    } : null
 | 
			
		||||
    const settings = { title, buttonLabel, defaultPath, filters, message, securityScopedBookmarks, nameFieldLabel, showsTagField, window }
 | 
			
		||||
    return binding.showSaveDialog(settings, wrappedCallback)
 | 
			
		||||
  showSaveDialogSync: function (window, options) {
 | 
			
		||||
    return saveDialog(true, window, options)
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  showMessageBox: function (...args) {
 | 
			
		||||
| 
						 | 
				
			
			@ -291,6 +270,7 @@ module.exports = {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
module.exports.showOpenDialog = deprecate.promisify(module.exports.showOpenDialog)
 | 
			
		||||
module.exports.showSaveDialog = deprecate.promisify(module.exports.showSaveDialog)
 | 
			
		||||
 | 
			
		||||
// Mark standard asynchronous functions.
 | 
			
		||||
v8Util.setHiddenValue(module.exports.showMessageBox, 'asynchronous', true)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue