Bug 1382667 - Implement an onBeforeCommand function for CustomizableUI that will run before the command is executed. r?gijs draft
authorJared Wein <jwein@mozilla.com>
Thu, 20 Jul 2017 09:35:50 -0400
changeset 612708 8221bad3e8204d4a51137504711dd1163838f8ad
parent 612707 91cd595d1e15fdc4c04e0e6c496605c0c7227436
child 612709 b4a15299733145a8bc1d01bd2cb4fe0010d65803
push id69580
push userbmo:jaws@mozilla.com
push dateFri, 21 Jul 2017 00:47:44 +0000
reviewersgijs
bugs1382667
milestone56.0a1
Bug 1382667 - Implement an onBeforeCommand function for CustomizableUI that will run before the command is executed. r?gijs MozReview-Commit-ID: DGR3mo01vXP
browser/components/customizableui/CustomizableUI.jsm
--- a/browser/components/customizableui/CustomizableUI.jsm
+++ b/browser/components/customizableui/CustomizableUI.jsm
@@ -1516,16 +1516,24 @@ var CustomizableUIInternal = {
     }
 
     aTargetNode.setAttribute("shortcut", ShortcutUtils.prettifyShortcut(shortcut));
   },
 
   handleWidgetCommand(aWidget, aNode, aEvent) {
     log.debug("handleWidgetCommand");
 
+    if (aWidget.onBeforeCommand) {
+      try {
+        aWidget.onBeforeCommand.call(null, aEvent);
+      } catch (e) {
+        log.error(e);
+      }
+    }
+
     if (aWidget.type == "button") {
       if (aWidget.onCommand) {
         try {
           aWidget.onCommand.call(null, aEvent);
         } catch (e) {
           log.error(e);
         }
       } else {
@@ -2409,16 +2417,20 @@ var CustomizableUIInternal = {
       widget._introducedInVersion = aData.introducedInVersion || 0;
     }
 
     this.wrapWidgetEventHandler("onBeforeCreated", widget);
     this.wrapWidgetEventHandler("onClick", widget);
     this.wrapWidgetEventHandler("onCreated", widget);
     this.wrapWidgetEventHandler("onDestroyed", widget);
 
+    if (typeof aData.onBeforeCommand == "function") {
+      widget.onBeforeCommand = aData.onBeforeCommand;
+    }
+
     if (widget.type == "button") {
       widget.onCommand = typeof aData.onCommand == "function" ?
                            aData.onCommand :
                            null;
     } else if (widget.type == "view") {
       if (typeof aData.viewId != "string") {
         log.error("Expected a string for widget " + widget.id + " viewId, but got "
                   + aData.viewId);
@@ -3286,16 +3298,22 @@ this.CustomizableUI = {
    * - onCreated(aNode): Attached to all widgets; a function that will be invoked
    *                  whenever the widget has a DOM node constructed, passing the
    *                  constructed node as an argument.
    * - onDestroyed(aDoc): Attached to all non-custom widgets; a function that
    *                  will be invoked after the widget has a DOM node destroyed,
    *                  passing the document from which it was removed. This is
    *                  useful especially for 'view' type widgets that need to
    *                  cleanup after views that were constructed on the fly.
+   * - onBeforeCommand(aEvt): A function that will be invoked when the user
+   *                          activates the button but before the command
+   *                          is evaluated. Useful if code needs to run to
+   *                          change the button's icon in preparation to the
+   *                          pending command action. Called for both type=button
+   *                          and type=view.
    * - onCommand(aEvt): Only useful for button widgets; a function that will be
    *                    invoked when the user activates the button.
    * - onClick(aEvt): Attached to all widgets; a function that will be invoked
    *                  when the user clicks the widget.
    * - onViewShowing(aEvt): Only useful for views; a function that will be
    *                  invoked when a user shows your view. If any event
    *                  handler calls aEvt.preventDefault(), the view will
    *                  not be shown.