Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 4a6a6de

Browse files
jbhoosreddyjasonLaster
authored andcommitted
Add accelerator support for panel (#1087)
1 parent f8063da commit 4a6a6de

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

packages/devtools-modules/src/menu/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
const { formatKeyShortcut } = require("../utils/text");
56
const EventEmitter = require("../utils/event-emitter");
67

78
function inToolbox() {
@@ -138,6 +139,9 @@ Menu.prototype._createMenuItems = function(parent) {
138139
if (item.id) {
139140
menu.id = item.id;
140141
}
142+
if (item.accelerator) {
143+
menuitem.setAttribute("acceltext", formatKeyShortcut(item.accelerator));
144+
}
141145
parent.appendChild(menu);
142146
} else if (item.type === "separator") {
143147
let menusep = doc.createElement("menuseparator");
@@ -170,7 +174,9 @@ Menu.prototype._createMenuItems = function(parent) {
170174
if (item.id) {
171175
menuitem.id = item.id;
172176
}
173-
177+
if (item.accelerator) {
178+
menuitem.setAttribute("acceltext", formatKeyShortcut(item.accelerator));
179+
}
174180
parent.appendChild(menuitem);
175181
}
176182
});

packages/devtools-modules/src/menu/menu-item.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* - role String - Define the action of the menu item; when specified the
1313
* click property will be ignored
1414
* - sublabel String
15-
* - accelerator Accelerator
1615
* - icon NativeImage
1716
* - position String - This field allows fine-grained definition of the
1817
* specific location within a given menu.
@@ -35,6 +34,8 @@
3534
* will be automatically converted to one using Menu.buildFromTemplate.
3635
* Boolean visible
3736
* If false, the menu item will be entirely hidden.
37+
* String accelerator
38+
* If specified, will be used as accelerator text for MenuItem
3839
*/
3940
function MenuItem({
4041
accesskey = null,
@@ -46,6 +47,7 @@ function MenuItem({
4647
submenu = null,
4748
type = "normal",
4849
visible = true,
50+
accelerator = "",
4951
} = { }) {
5052
this.accesskey = accesskey;
5153
this.checked = checked;
@@ -56,6 +58,7 @@ function MenuItem({
5658
this.submenu = submenu;
5759
this.type = type;
5860
this.visible = visible;
61+
this.accelerator = accelerator;
5962
}
6063

6164
module.exports = MenuItem;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
4+
5+
/**
6+
* Utils for keyboard command strings
7+
* @module utils/text
8+
*/
9+
import Services from "devtools-services";
10+
const { appinfo } = Services;
11+
12+
const isMacOS = appinfo.OS === "Darwin";
13+
14+
/**
15+
* Formats key for use in tooltips
16+
* For macOS we use the following unicode
17+
*
18+
* cmd ⌘ = \u2318
19+
* shift ⇧ – \u21E7
20+
* option (alt) ⌥ \u2325
21+
*
22+
* For Win/Lin this replaces CommandOrControl or CmdOrCtrl with Ctrl
23+
*
24+
* @memberof utils/text
25+
* @static
26+
*/
27+
export function formatKeyShortcut(shortcut) {
28+
if (isMacOS) {
29+
return shortcut
30+
.replace(/Shift\+/g, "\u21E7")
31+
.replace(/Command\+|Cmd\+/g, "\u2318")
32+
.replace(/CommandOrControl\+|CmdOrCtrl\+/g, "\u2318")
33+
.replace(/Alt\+/g, "\u2325");
34+
}
35+
return shortcut
36+
.replace(/CommandOrControl\+|CmdOrCtrl\+/g, "Ctrl")
37+
.replace(/Shift\+/g, "Shift");
38+
}

0 commit comments

Comments
 (0)