dialog.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict'
  2. const { app, BrowserWindow, deprecate } = require('electron')
  3. const binding = process.electronBinding('dialog')
  4. const v8Util = process.electronBinding('v8_util')
  5. const fileDialogProperties = {
  6. openFile: 1 << 0,
  7. openDirectory: 1 << 1,
  8. multiSelections: 1 << 2,
  9. createDirectory: 1 << 3,
  10. showHiddenFiles: 1 << 4,
  11. promptToCreate: 1 << 5,
  12. noResolveAliases: 1 << 6,
  13. treatPackageAsDirectory: 1 << 7
  14. }
  15. const normalizeAccessKey = (text) => {
  16. if (typeof text !== 'string') return text
  17. // macOS does not have access keys so remove single ampersands
  18. // and replace double ampersands with a single ampersand
  19. if (process.platform === 'darwin') {
  20. return text.replace(/&(&?)/g, '$1')
  21. }
  22. // Linux uses a single underscore as an access key prefix so escape
  23. // existing single underscores with a second underscore, replace double
  24. // ampersands with a single ampersand, and replace a single ampersand with
  25. // a single underscore
  26. if (process.platform === 'linux') {
  27. return text.replace(/_/g, '__').replace(/&(.?)/g, (match, after) => {
  28. if (after === '&') return after
  29. return `_${after}`
  30. })
  31. }
  32. return text
  33. }
  34. const checkAppInitialized = function () {
  35. if (!app.isReady()) {
  36. throw new Error('dialog module can only be used after app is ready')
  37. }
  38. }
  39. const saveDialog = (sync, window, options) => {
  40. checkAppInitialized()
  41. if (window && window.constructor !== BrowserWindow) {
  42. options = window
  43. window = null
  44. }
  45. if (options == null) options = { title: 'Save' }
  46. const {
  47. buttonLabel = '',
  48. defaultPath = '',
  49. filters = [],
  50. title = '',
  51. message = '',
  52. securityScopedBookmarks = false,
  53. nameFieldLabel = '',
  54. showsTagField = true
  55. } = options
  56. if (typeof title !== 'string') throw new TypeError('Title must be a string')
  57. if (typeof buttonLabel !== 'string') throw new TypeError('Button label must be a string')
  58. if (typeof defaultPath !== 'string') throw new TypeError('Default path must be a string')
  59. if (typeof message !== 'string') throw new TypeError('Message must be a string')
  60. if (typeof nameFieldLabel !== 'string') throw new TypeError('Name field label must be a string')
  61. const settings = { buttonLabel, defaultPath, filters, title, message, securityScopedBookmarks, nameFieldLabel, showsTagField, window }
  62. return (sync) ? binding.showSaveDialogSync(settings) : binding.showSaveDialog(settings)
  63. }
  64. const openDialog = (sync, window, options) => {
  65. checkAppInitialized()
  66. if (window && window.constructor !== BrowserWindow) {
  67. options = window
  68. window = null
  69. }
  70. if (options == null) {
  71. options = {
  72. title: 'Open',
  73. properties: ['openFile']
  74. }
  75. }
  76. const {
  77. buttonLabel = '',
  78. defaultPath = '',
  79. filters = [],
  80. properties = ['openFile'],
  81. title = '',
  82. message = '',
  83. securityScopedBookmarks = false
  84. } = options
  85. if (!Array.isArray(properties)) throw new TypeError('Properties must be an array')
  86. let dialogProperties = 0
  87. for (const prop in fileDialogProperties) {
  88. if (properties.includes(prop)) {
  89. dialogProperties |= fileDialogProperties[prop]
  90. }
  91. }
  92. if (typeof title !== 'string') throw new TypeError('Title must be a string')
  93. if (typeof buttonLabel !== 'string') throw new TypeError('Button label must be a string')
  94. if (typeof defaultPath !== 'string') throw new TypeError('Default path must be a string')
  95. if (typeof message !== 'string') throw new TypeError('Message must be a string')
  96. const settings = { title, buttonLabel, defaultPath, filters, message, securityScopedBookmarks, window }
  97. settings.properties = dialogProperties
  98. return (sync) ? binding.showOpenDialogSync(settings) : binding.showOpenDialog(settings)
  99. }
  100. const messageBox = (sync, window, options) => {
  101. checkAppInitialized()
  102. if (window && window.constructor !== BrowserWindow) {
  103. options = window
  104. window = null
  105. }
  106. if (options == null) options = { type: 'none' }
  107. const messageBoxTypes = ['none', 'info', 'warning', 'error', 'question']
  108. const messageBoxOptions = { noLink: 1 << 0 }
  109. let {
  110. buttons = [],
  111. cancelId,
  112. checkboxLabel = '',
  113. checkboxChecked,
  114. defaultId = -1,
  115. detail = '',
  116. icon = null,
  117. message = '',
  118. title = '',
  119. type = 'none'
  120. } = options
  121. const messageBoxType = messageBoxTypes.indexOf(type)
  122. if (messageBoxType === -1) throw new TypeError('Invalid message box type')
  123. if (!Array.isArray(buttons)) throw new TypeError('Buttons must be an array')
  124. if (options.normalizeAccessKeys) buttons = buttons.map(normalizeAccessKey)
  125. if (typeof title !== 'string') throw new TypeError('Title must be a string')
  126. if (typeof message !== 'string') throw new TypeError('Message must be a string')
  127. if (typeof detail !== 'string') throw new TypeError('Detail must be a string')
  128. if (typeof checkboxLabel !== 'string') throw new TypeError('checkboxLabel must be a string')
  129. checkboxChecked = !!checkboxChecked
  130. // Choose a default button to get selected when dialog is cancelled.
  131. if (cancelId == null) {
  132. // If the defaultId is set to 0, ensure the cancel button is a different index (1)
  133. cancelId = (defaultId === 0 && buttons.length > 1) ? 1 : 0
  134. for (let i = 0; i < buttons.length; i++) {
  135. const text = buttons[i].toLowerCase()
  136. if (text === 'cancel' || text === 'no') {
  137. cancelId = i
  138. break
  139. }
  140. }
  141. }
  142. const flags = options.noLink ? messageBoxOptions.noLink : 0
  143. if (sync) {
  144. return binding.showMessageBoxSync(messageBoxType, buttons,
  145. defaultId, cancelId, flags, title, message, detail,
  146. checkboxLabel, checkboxChecked, icon, window)
  147. } else {
  148. return binding.showMessageBox(messageBoxType, buttons,
  149. defaultId, cancelId, flags, title, message, detail,
  150. checkboxLabel, checkboxChecked, icon, window)
  151. }
  152. }
  153. module.exports = {
  154. showOpenDialog: function (window, options) {
  155. return openDialog(false, window, options)
  156. },
  157. showOpenDialogSync: function (window, options) {
  158. return openDialog(true, window, options)
  159. },
  160. showSaveDialog: function (window, options) {
  161. return saveDialog(false, window, options)
  162. },
  163. showSaveDialogSync: function (window, options) {
  164. return saveDialog(true, window, options)
  165. },
  166. showMessageBox: function (window, options) {
  167. return messageBox(false, window, options)
  168. },
  169. showMessageBoxSync: function (window, options) {
  170. return messageBox(true, window, options)
  171. },
  172. showErrorBox: function (...args) {
  173. return binding.showErrorBox(...args)
  174. },
  175. showCertificateTrustDialog: function (window, options) {
  176. if (window && window.constructor !== BrowserWindow) options = window
  177. if (options == null || typeof options !== 'object') {
  178. throw new TypeError('options must be an object')
  179. }
  180. const { certificate, message = '' } = options
  181. if (certificate == null || typeof certificate !== 'object') {
  182. throw new TypeError('certificate must be an object')
  183. }
  184. if (typeof message !== 'string') throw new TypeError('message must be a string')
  185. return binding.showCertificateTrustDialog(window, certificate, message)
  186. }
  187. }
  188. module.exports.showMessageBox = deprecate.promisifyMultiArg(module.exports.showMessageBox, ({ response, checkboxChecked }) => [response, checkboxChecked])
  189. module.exports.showOpenDialog = deprecate.promisifyMultiArg(module.exports.showOpenDialog, ({ filePaths, bookmarks }) => [filePaths, bookmarks])
  190. module.exports.showSaveDialog = deprecate.promisifyMultiArg(module.exports.showSaveDialog, ({ filePath, bookmarks }) => [filePath, bookmarks])
  191. module.exports.showCertificateTrustDialog = deprecate.promisify(module.exports.showCertificateTrustDialog)