dialog.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. noLink = false,
  118. message = '',
  119. title = '',
  120. type = 'none'
  121. } = options;
  122. const messageBoxType = messageBoxTypes.indexOf(type);
  123. if (messageBoxType === -1) throw new TypeError('Invalid message box type');
  124. if (!Array.isArray(buttons)) throw new TypeError('Buttons must be an array');
  125. if (options.normalizeAccessKeys) buttons = buttons.map(normalizeAccessKey);
  126. if (typeof title !== 'string') throw new TypeError('Title must be a string');
  127. if (typeof noLink !== 'boolean') throw new TypeError('noLink must be a boolean');
  128. if (typeof message !== 'string') throw new TypeError('Message must be a string');
  129. if (typeof detail !== 'string') throw new TypeError('Detail must be a string');
  130. if (typeof checkboxLabel !== 'string') throw new TypeError('checkboxLabel must be a string');
  131. checkboxChecked = !!checkboxChecked;
  132. if (checkboxChecked && !checkboxLabel) {
  133. throw new Error('checkboxChecked requires that checkboxLabel also be passed');
  134. }
  135. // Choose a default button to get selected when dialog is cancelled.
  136. if (cancelId == null) {
  137. // If the defaultId is set to 0, ensure the cancel button is a different index (1)
  138. cancelId = (defaultId === 0 && buttons.length > 1) ? 1 : 0;
  139. for (let i = 0; i < buttons.length; i++) {
  140. const text = buttons[i].toLowerCase();
  141. if (text === 'cancel' || text === 'no') {
  142. cancelId = i;
  143. break;
  144. }
  145. }
  146. }
  147. const settings = {
  148. window,
  149. messageBoxType,
  150. buttons,
  151. defaultId,
  152. cancelId,
  153. noLink,
  154. title,
  155. message,
  156. detail,
  157. checkboxLabel,
  158. checkboxChecked,
  159. icon
  160. };
  161. if (sync) {
  162. return binding.showMessageBoxSync(settings);
  163. } else {
  164. return binding.showMessageBox(settings);
  165. }
  166. };
  167. module.exports = {
  168. showOpenDialog: function (window, options) {
  169. return openDialog(false, window, options);
  170. },
  171. showOpenDialogSync: function (window, options) {
  172. return openDialog(true, window, options);
  173. },
  174. showSaveDialog: function (window, options) {
  175. return saveDialog(false, window, options);
  176. },
  177. showSaveDialogSync: function (window, options) {
  178. return saveDialog(true, window, options);
  179. },
  180. showMessageBox: function (window, options) {
  181. return messageBox(false, window, options);
  182. },
  183. showMessageBoxSync: function (window, options) {
  184. return messageBox(true, window, options);
  185. },
  186. showErrorBox: function (...args) {
  187. return binding.showErrorBox(...args);
  188. },
  189. showCertificateTrustDialog: function (window, options) {
  190. if (window && window.constructor !== BrowserWindow) options = window;
  191. if (options == null || typeof options !== 'object') {
  192. throw new TypeError('options must be an object');
  193. }
  194. const { certificate, message = '' } = options;
  195. if (certificate == null || typeof certificate !== 'object') {
  196. throw new TypeError('certificate must be an object');
  197. }
  198. if (typeof message !== 'string') throw new TypeError('message must be a string');
  199. return binding.showCertificateTrustDialog(window, certificate, message);
  200. }
  201. };