Bug 1412028 - expose executeKey from devtools-startup;r=ochameau draft
authorJulian Descottes <jdescottes@mozilla.com>
Fri, 27 Oct 2017 16:35:25 +0200
changeset 687708 b8768451b78cdb8aa941a858697deb6685dd8403
parent 687707 d1afec30aa16fe8ab9fce2fa45049cad15b25ddd
child 687709 978337dad9c0235a1ee040ec650ac1ee38d04566
child 687710 c0627e37a1acdc474cda1e6abacb765bbb189738
push id86575
push userjdescottes@mozilla.com
push dateFri, 27 Oct 2017 16:19:51 +0000
reviewersochameau
bugs1412028
milestone58.0a1
Bug 1412028 - expose executeKey from devtools-startup;r=ochameau In order to execute the initial user action after going through the onboarding flow, devtools-startup should expose a method easy to call from the outside to trigger a devtools "command". For now this is limited to devtools keyshortcuts. MozReview-Commit-ID: 4QFAQCUu7IK
devtools/shim/devtools-startup.js
--- a/devtools/shim/devtools-startup.js
+++ b/devtools/shim/devtools-startup.js
@@ -439,37 +439,49 @@ DevToolsStartup.prototype = {
   },
 
   hookKeyShortcuts(window) {
     let doc = window.document;
     let keyset = doc.createElement("keyset");
     keyset.setAttribute("id", "devtoolsKeyset");
 
     for (let key of KeyShortcuts) {
-      let xulKey = this.createKey(doc, key, () => this.onKey(window, key));
+      let keyId = key.toolId || key.id;
+      let xulKey = this.createKey(doc, key, () => this.executeKey(keyId, window));
       keyset.appendChild(xulKey);
     }
 
     // Appending a <key> element is not always enough. The <keyset> needs
     // to be detached and reattached to make sure the <key> is taken into
     // account (see bug 832984).
     let mainKeyset = doc.getElementById("mainKeyset");
     mainKeyset.parentNode.insertBefore(keyset, mainKeyset);
   },
 
-  onKey(window, key) {
+  /**
+   * Execute the command associated with a DevTools key shortcut, for the provided
+   * window.
+   *
+   * @param {String} keyId
+   *        id or toolId for the DevTools key shortcut to execute (see KeyShortcuts).
+   * @param {Window} window
+   *        the window where the command should be executed (command will most likely
+   *        apply to the selected tab).
+   */
+  executeKey(keyId, window) {
     // Record the timing at which this event started in order to compute later in
     // gDevTools.showToolbox, the complete time it takes to open the toolbox.
     // i.e. especially take `initDevTools` into account.
 
     let startTime = window.performance.now();
     let require = this.initDevTools("KeyShortcut");
     if (require) {
       // require might be null if initDevTools was called while DevTools are disabled.
       let { gDevToolsBrowser } = require("devtools/client/framework/devtools-browser");
+      let key = KeyShortcuts.find(k => keyId === (k.toolId || k.id));
       gDevToolsBrowser.onKeyShortcut(window, key, startTime);
     }
   },
 
   // Create a <xul:key> DOM Element
   createKey(doc, { id, toolId, shortcut, modifiers: mod }, oncommand) {
     let k = doc.createElement("key");
     k.id = "key_" + (id || toolId);