Bug 1344815 - Show sidebar shortcut in menus r?mixedpuppy
MozReview-Commit-ID: C6q0FBtCmRK
--- a/browser/components/extensions/ext-commands.js
+++ b/browser/components/extensions/ext-commands.js
@@ -112,17 +112,17 @@ this.commands = class extends ExtensionA
* @param {Document} doc The XUL document.
* @param {string} name The name of the command.
* @param {string} shortcut The shortcut provided in the manifest.
* @see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/key
*
* @returns {Document} The newly created Key element.
*/
buildKey(doc, name, shortcut) {
- let keyElement = this.buildKeyFromShortcut(doc, shortcut);
+ let keyElement = this.buildKeyFromShortcut(doc, name, shortcut);
// We need to have the attribute "oncommand" for the "command" listener to fire,
// and it is currently ignored when set to the empty string.
keyElement.setAttribute("oncommand", "//");
/* eslint-disable mozilla/balanced-listeners */
// We remove all references to the key elements when the extension is shutdown,
// therefore the listeners for these elements will be garbage collected.
@@ -149,31 +149,36 @@ this.commands = class extends ExtensionA
return keyElement;
}
/**
* Builds a XUL Key element from the provided shortcut.
*
* @param {Document} doc The XUL document.
+ * @param {string} name The name of the shortcut.
* @param {string} shortcut The shortcut provided in the manifest.
*
* @see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/key
* @returns {Document} The newly created Key element.
*/
- buildKeyFromShortcut(doc, shortcut) {
+ buildKeyFromShortcut(doc, name, shortcut) {
let keyElement = doc.createElementNS(XUL_NS, "key");
let parts = shortcut.split("+");
// The key is always the last element.
let chromeKey = parts.pop();
// The modifiers are the remaining elements.
keyElement.setAttribute("modifiers", this.getModifiersAttribute(parts));
+ if (name == "_execute_sidebar_action") {
+ let id = `ext-key-id-${this.id}-sidebar-action`;
+ keyElement.setAttribute("id", id);
+ }
if (/^[A-Z]$/.test(chromeKey)) {
// We use the key attribute for all single digits and characters.
keyElement.setAttribute("key", chromeKey);
} else {
keyElement.setAttribute("keycode", this.getKeycodeAttribute(chromeKey));
keyElement.setAttribute("event", "keydown");
}
--- a/browser/components/extensions/ext-sidebarAction.js
+++ b/browser/components/extensions/ext-sidebarAction.js
@@ -157,16 +157,18 @@ this.sidebarAction = class extends Exten
// checkmarks in the menus.
let broadcaster = document.createElementNS(XUL_NS, "broadcaster");
broadcaster.setAttribute("id", this.id);
broadcaster.setAttribute("autoCheck", "false");
broadcaster.setAttribute("type", "checkbox");
broadcaster.setAttribute("group", "sidebar");
broadcaster.setAttribute("label", details.title);
broadcaster.setAttribute("sidebarurl", this.sidebarUrl(details.panel));
+ let id = `ext-key-id-${this.id}`;
+ broadcaster.setAttribute("key", id);
// oncommand gets attached to menuitem, so we use the observes attribute to
// get the command id we pass to SidebarUI.
broadcaster.setAttribute("oncommand", "SidebarUI.toggle(this.getAttribute('observes'))");
let header = document.getElementById("sidebar-switcher-target");
header.addEventListener("SidebarShown", this.updateHeader);
--- a/browser/components/extensions/test/browser/browser_ext_sidebarAction.js
+++ b/browser/components/extensions/test/browser/browser_ext_sidebarAction.js
@@ -1,18 +1,25 @@
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
requestLongerTimeout(2);
let extData = {
manifest: {
- "sidebar_action": {
- "default_panel": "sidebar.html",
+ commands: {
+ _execute_sidebar_action: {
+ suggested_key: {
+ default: "Ctrl+Shift+I",
+ },
+ },
+ },
+ sidebar_action: {
+ default_panel: "sidebar.html",
},
},
useAddonManager: "temporary",
files: {
"sidebar.html": `
<!DOCTYPE html>
<html>
@@ -105,16 +112,22 @@ add_task(async function sidebar_isOpen()
let extension1 = ExtensionTestUtils.loadExtension(extData);
await extension1.startup();
info("Test extension1's sidebar is opened on install");
await extension1.awaitMessage("sidebar");
await sendMessage(extension1, "isOpen", {result: true});
let sidebar1ID = SidebarUI.currentID;
+ // Test that the key is set for the extension.
+ let button = document.getElementById(`button_${makeWidgetId(extension1.id)}-sidebar-action`);
+ ok(button.hasAttribute("key"), "The menu item has a key specified");
+ let key = document.getElementById(button.getAttribute("key"));
+ ok(key, "The key attribute finds the related key element");
+
info("Load extension2");
let extension2 = ExtensionTestUtils.loadExtension(extData);
await extension2.startup();
info("Test extension2's sidebar is opened on install");
await extension2.awaitMessage("sidebar");
await sendMessage(extension1, "isOpen", {result: false});
await sendMessage(extension2, "isOpen", {result: true});