From eb533e04b931c2a0020dd7d0d202b8f161d33bec Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 29 Dec 2016 12:05:58 -0800 Subject: [PATCH 1/3] Add option to normalize dialog access keys --- lib/browser/api/dialog.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/browser/api/dialog.js b/lib/browser/api/dialog.js index e57eb41b83c..ae41632e3b3 100644 --- a/lib/browser/api/dialog.js +++ b/lib/browser/api/dialog.js @@ -38,6 +38,29 @@ const parseArgs = function (window, options, callback, ...args) { return [window, options, callback] } +const normalizeAccessKey = (text) => { + if (typeof text !== 'string') return text + + // macOS does not have access keys so remove single ampersands + // and replace double ampersands with a single ampersand + if (process.platform === 'darwin') { + return text.replace(/&(&?)/g, '$1') + } + + // Linux uses a single underscore as an access key prefix so escape + // existing single underscores with a second underscore, replace double + // ampersands with a single ampersand, and replace a single ampersand with + // a single underscore + if (process.platform === 'linux') { + return text.replace(/_/g, '__').replace(/&(.?)/g, (match, after) => { + if (after === '&') return after + return `_${after}` + }) + } + + return text +} + const checkAppInitialized = function () { if (!app.isReady()) { throw new Error('dialog module can only be used after app is ready') @@ -154,7 +177,7 @@ module.exports = { } } - let {buttons, cancelId, defaultId, detail, icon, message, title, type} = options + let {buttons, cancelId, defaultId, detail, icon, message, title, type, normalizeAccessKeys} = options if (type == null) { type = 'none' @@ -171,6 +194,10 @@ module.exports = { throw new TypeError('Buttons must be an array') } + if (normalizeAccessKeys) { + buttons = buttons.map(normalizeAccessKey) + } + if (title == null) { title = '' } else if (typeof title !== 'string') { From d200cf2e7705eb503d76a90b29947bde75553b4e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 29 Dec 2016 13:18:50 -0800 Subject: [PATCH 2/3] Document normalizeAccessKeys option --- docs/api/dialog.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/api/dialog.md b/docs/api/dialog.md index 8ee8582c971..18f1fc6b071 100644 --- a/docs/api/dialog.md +++ b/docs/api/dialog.md @@ -35,6 +35,14 @@ The `dialog` module has the following methods: * `properties` String[] (optional) - Contains which features the dialog should use, can contain `openFile`, `openDirectory`, `multiSelections`, `createDirectory` and `showHiddenFiles`. + * `normalizeAccessKeys` Boolean (optional) - Normalize the keyboard access keys + across platforms. Default is `false`. Enabling this assumes `&` is used in + the button labels for the placement of the keyboard shortcut access key + and labels will be converted so they work correctly on each platform, `&` + characters are removed on macOS, converted to `_` on Linux, and left + untouched on Windows. For example, a button label of `Vie&w` will be + converted to `Vie_w` on Linux and `View` on macOS and can be selected + via `Alt-W` on Windows and Linux. * `callback` Function (optional) * `filePaths` String[] - An array of file paths chosen by the user From 5f862effaa2c055da7c3a82c76a7afc3a6635491 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 29 Dec 2016 13:21:43 -0800 Subject: [PATCH 3/3] Access directly on options object --- lib/browser/api/dialog.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/browser/api/dialog.js b/lib/browser/api/dialog.js index ae41632e3b3..9e1091cf964 100644 --- a/lib/browser/api/dialog.js +++ b/lib/browser/api/dialog.js @@ -177,7 +177,7 @@ module.exports = { } } - let {buttons, cancelId, defaultId, detail, icon, message, title, type, normalizeAccessKeys} = options + let {buttons, cancelId, defaultId, detail, icon, message, title, type} = options if (type == null) { type = 'none' @@ -194,7 +194,7 @@ module.exports = { throw new TypeError('Buttons must be an array') } - if (normalizeAccessKeys) { + if (options.normalizeAccessKeys) { buttons = buttons.map(normalizeAccessKey) }