Bug 1382667 - Implement an onBeforeCommand function for CustomizableUI that will run before the command is executed. r?gijs
MozReview-Commit-ID: DGR3mo01vXP
--- 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.