electron/atom/browser/api/lib/dialog.coffee

126 lines
3.7 KiB
CoffeeScript
Raw Normal View History

binding = process.atomBinding 'dialog'
v8Util = process.atomBinding 'v8_util'
app = require 'app'
BrowserWindow = require 'browser-window'
fileDialogProperties =
2014-08-06 05:47:44 +00:00
openFile: 1 << 0
openDirectory: 1 << 1
multiSelections: 1 << 2
createDirectory: 1 << 3
messageBoxTypes = ['none', 'info', 'warning', 'error', 'question']
2015-07-23 09:20:43 +00:00
messageBoxOptions =
noLink: 1 << 0
parseArgs = (window, options, callback) ->
unless window is null or window?.constructor is BrowserWindow
# Shift.
callback = options
options = window
window = null
if not callback? and typeof options is 'function'
# Shift.
callback = options
options = null
[window, options, callback]
checkAppInitialized = ->
throw new Error('dialog module can only be used after app is ready') unless app.isReady()
module.exports =
showOpenDialog: (args...) ->
checkAppInitialized()
[window, options, callback] = parseArgs args...
2013-09-23 08:51:00 +00:00
options ?= title: 'Open', properties: ['openFile']
options.properties ?= ['openFile']
throw new TypeError('Properties need to be array') unless Array.isArray options.properties
properties = 0
for prop, value of fileDialogProperties
properties |= value if prop in options.properties
2013-09-23 08:51:00 +00:00
options.title ?= ''
options.defaultPath ?= ''
options.filters ?= []
wrappedCallback =
if typeof callback is 'function'
(success, result) -> callback(if success then result)
else
null
2013-09-23 08:51:00 +00:00
binding.showOpenDialog String(options.title),
String(options.defaultPath),
options.filters
properties,
window,
wrappedCallback
2014-09-07 22:14:43 +00:00
showSaveDialog: (args...) ->
checkAppInitialized()
[window, options, callback] = parseArgs args...
2013-09-23 08:51:00 +00:00
options ?= title: 'Save'
options.title ?= ''
options.defaultPath ?= ''
2014-08-06 13:51:36 +00:00
options.filters ?= []
wrappedCallback =
if typeof callback is 'function'
(success, result) -> callback(if success then result)
else
null
binding.showSaveDialog String(options.title),
String(options.defaultPath),
options.filters
window,
wrappedCallback
2013-05-10 13:19:13 +00:00
2014-09-07 22:14:43 +00:00
showMessageBox: (args...) ->
checkAppInitialized()
[window, options, callback] = parseArgs args...
2013-06-07 07:59:12 +00:00
2013-09-23 08:51:00 +00:00
options ?= type: 'none'
options.type ?= 'none'
messageBoxType = messageBoxTypes.indexOf options.type
throw new TypeError('Invalid message box type') unless messageBoxType > -1
2013-05-10 13:19:13 +00:00
throw new TypeError('Buttons need to be array') unless Array.isArray options.buttons
2013-09-23 08:51:00 +00:00
options.title ?= ''
options.message ?= ''
options.detail ?= ''
2015-01-05 23:08:42 +00:00
options.icon ?= null
2015-07-07 10:33:11 +00:00
# Choose a default button to get selected when dialog is cancelled.
2015-07-07 10:33:11 +00:00
unless options.cancelId?
options.cancelId = 0
for text, i in options.buttons
if text.toLowerCase() in ['cancel', 'no']
options.cancelId = i
break
2013-05-10 13:19:13 +00:00
2015-07-23 09:20:43 +00:00
flags = if options.noLink then messageBoxOptions.noLink else 0
binding.showMessageBox messageBoxType,
2013-06-07 07:59:12 +00:00
options.buttons,
2015-07-07 10:26:50 +00:00
options.cancelId,
2015-07-23 09:20:43 +00:00
flags,
options.title,
options.message,
options.detail,
2015-01-05 23:08:42 +00:00
options.icon,
window,
callback
2014-11-05 08:04:39 +00:00
showErrorBox: (args...) ->
binding.showErrorBox args...
# Mark standard asynchronous functions.
2015-07-08 08:23:43 +00:00
for api in ['showMessageBox', 'showOpenDialog', 'showSaveDialog']
v8Util.setHiddenValue module.exports[api], 'asynchronous', true