Bug 1529366 - Implement batch-updating of Touch Bar inputs. r=mikedeboer,spohl
authorHarry Twyford <harry.a.twyford@gmail.com>
Mon, 18 Mar 2019 12:14:11 +0000
changeset 464868 e49c905f4cf68877d022c8457913253e1cb3acb1
parent 464867 4556fb6dc667ef735fcb15f418ca76041775cd20
child 464869 66f76326e8d33aaf91c3422376abc72f953997d6
push id35727
push userdvarga@mozilla.com
push dateTue, 19 Mar 2019 09:48:59 +0000
treeherdermozilla-central@70baa37ae1eb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmikedeboer, spohl
bugs1529366
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1529366 - Implement batch-updating of Touch Bar inputs. r=mikedeboer,spohl Changes updateTouchBarInput to a batch-updating updateTouchBarInputs. Also adds a check for a cached localized title to getTouchBarInput(). Differential Revision: https://phabricator.services.mozilla.com/D20956
browser/components/touchbar/MacTouchBar.js
widget/cocoa/nsTouchBarUpdater.mm
widget/nsITouchBarUpdater.idl
--- a/browser/components/touchbar/MacTouchBar.js
+++ b/browser/components/touchbar/MacTouchBar.js
@@ -206,90 +206,107 @@ class TouchBarHelper {
   }
 
   getTouchBarInput(inputName) {
     // inputName might be undefined if an input's context() returns undefined.
     if (!inputName || !kBuiltInInputs.hasOwnProperty(inputName)) {
       return null;
     }
 
+    // context() may specify that one named input "point" to another.
     if (typeof kBuiltInInputs[inputName].context == "function") {
       inputName = kBuiltInInputs[inputName].context();
     }
 
     if (!inputName || !kBuiltInInputs.hasOwnProperty(inputName)) {
       return null;
     }
 
     let inputData = kBuiltInInputs[inputName];
 
     let item = new TouchBarInput(inputData);
 
+    // Skip localization if there is already a cached localized title.
+    if (kBuiltInInputs[inputName].hasOwnProperty("localTitle")) {
+      return item;
+    }
+
     // Async l10n fills in the localized input labels after the initial load.
     this._l10n.formatValue(item.key).then((result) => {
       item.title = result;
       kBuiltInInputs[inputName].localTitle = result; // Cache result.
       // Checking this.window since this callback can fire after all windows are closed.
       if (this.window) {
+        let wrapperArray = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
+        wrapperArray.appendElement(item);
         let baseWindow = this.window.docShell.treeOwner.QueryInterface(Ci.nsIBaseWindow);
         let updater = Cc["@mozilla.org/widget/touchbarupdater;1"]
                         .getService(Ci.nsITouchBarUpdater);
-        updater.updateTouchBarInput(baseWindow, item);
+        updater.updateTouchBarInputs(baseWindow, wrapperArray);
       }
     });
 
     return item;
   }
 
   /**
    * Fetches a specific Touch Bar Input by name and updates it on the Touch Bar.
    * @param {string} inputName
    *        A key to a value in the kBuiltInInputs object in this file.
+   * @param {...*} [otherInputs] (optional)
+   *        Additional keys to values in the kBuiltInInputs object in this file.
    */
-  _updateTouchBarInput(inputName) {
+  _updateTouchBarInputs(inputName, ...otherInputs) {
     let input = this.getTouchBarInput(inputName);
     if (!input || !this.window) {
       return;
     }
+    let inputs = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
+    inputs.appendElement(input);
+    for (let otherInputName of otherInputs) {
+      input = this.getTouchBarInput(otherInputName);
+      if (!input) {
+        continue;
+      }
+      inputs.appendElement(input);
+    }
 
     let baseWindow = this.window.docShell.treeOwner.QueryInterface(Ci.nsIBaseWindow);
     let updater = Cc["@mozilla.org/widget/touchbarupdater;1"]
                     .getService(Ci.nsITouchBarUpdater);
-    updater.updateTouchBarInput(baseWindow, input);
+    updater.updateTouchBarInputs(baseWindow, inputs);
   }
 
   observe(subject, topic, data) {
     switch (topic) {
       case "touchbar-location-change":
         this.activeUrl = data;
         // ReaderView button is disabled on every location change since
         // Reader View must determine if the new page can be Reader Viewed.
         kBuiltInInputs.ReaderView.disabled = !data.startsWith("about:reader");
-        this._updateTouchBarInput("ReaderView");
         kBuiltInInputs.Back.disabled = !this.window.gBrowser.canGoBack;
-        this._updateTouchBarInput("Back");
         kBuiltInInputs.Forward.disabled = !this.window.gBrowser.canGoForward;
-        this._updateTouchBarInput("Forward");
+        this._updateTouchBarInputs("ReaderView", "Back", "Forward");
         break;
       case "bookmark-icon-updated":
         data == "starred" ?
           kBuiltInInputs.AddBookmark.image = "bookmark-filled.pdf"
           : kBuiltInInputs.AddBookmark.image = "bookmark.pdf";
-        this._updateTouchBarInput("AddBookmark");
+        this._updateTouchBarInputs("AddBookmark");
         break;
       case "reader-mode-available":
         kBuiltInInputs.ReaderView.disabled = false;
-        this._updateTouchBarInput("ReaderView");
+        this._updateTouchBarInputs("ReaderView");
         break;
       case "intl:app-locales-changed":
         // On locale change, refresh all inputs after loading new localTitle.
         for (let inputName of this._storedLayout) {
           delete kBuiltInInputs[inputName].localTitle;
-          this._updateTouchBarInput(inputName);
         }
+        this._updateTouchBarInputs(...this._storedLayout);
         break;
       case "quit-application":
         this.destructor();
         break;
     }
   }
 }
 
--- a/widget/cocoa/nsTouchBarUpdater.mm
+++ b/widget/cocoa/nsTouchBarUpdater.mm
@@ -4,38 +4,48 @@
 
 #import <Cocoa/Cocoa.h>
 
 #include "nsTouchBar.h"
 #include "nsITouchBarInput.h"
 #include "nsTouchBarUpdater.h"
 
 #include "nsCocoaWindow.h"
+#include "nsIArray.h"
 #include "nsIBaseWindow.h"
 #include "nsIWidget.h"
 
 #if !defined(MAC_OS_X_VERSION_10_12_2) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_12_2
 @interface BaseWindow (NSTouchBarProvider)
 @property(strong) NSTouchBar* touchBar;
 @end
 #endif
 
 NS_IMPL_ISUPPORTS(nsTouchBarUpdater, nsITouchBarUpdater);
 
 NS_IMETHODIMP
-nsTouchBarUpdater::UpdateTouchBarInput(nsIBaseWindow* aWindow, nsITouchBarInput* aInput) {
+nsTouchBarUpdater::UpdateTouchBarInputs(nsIBaseWindow* aWindow, nsIArray* aInputs) {
   nsCOMPtr<nsIWidget> widget = nullptr;
   aWindow->GetMainWidget(getter_AddRefs(widget));
   if (!widget) {
     return NS_ERROR_FAILURE;
   }
   BaseWindow* cocoaWin = (BaseWindow*)widget->GetNativeData(NS_NATIVE_WINDOW);
   if (!cocoaWin) {
     return NS_ERROR_FAILURE;
   }
 
   if ([cocoaWin respondsToSelector:@selector(touchBar)]) {
-    TouchBarInput* convertedInput = [[TouchBarInput alloc] initWithXPCOM:aInput];
-    [(nsTouchBar*)cocoaWin.touchBar updateItem:convertedInput];
+    uint32_t itemCount = 0;
+    aInputs->GetLength(&itemCount);
+    for (uint32_t i = 0; i < itemCount; ++i) {
+      nsCOMPtr<nsITouchBarInput> input = do_QueryElementAt(aInputs, i);
+      if (!input) {
+        continue;
+      }
+
+      TouchBarInput* convertedInput = [[TouchBarInput alloc] initWithXPCOM:input];
+      [(nsTouchBar*)cocoaWin.touchBar updateItem:convertedInput];
+    }
   }
 
   return NS_OK;
 }
--- a/widget/nsITouchBarUpdater.idl
+++ b/widget/nsITouchBarUpdater.idl
@@ -1,20 +1,19 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+#include "nsIArray.idl"
 #include "nsIBaseWindow.idl"
 #include "nsISupports.idl"
-#include "nsITouchBarInput.idl"
 
 /**
  * Front-to-backend communication to keep Touch Bar updated
  */
 [scriptable, uuid(38f396e2-93c9-4a77-aaf7-2d50b9962186)]
 interface nsITouchBarUpdater : nsISupports
 {
   /**
-   * Updates a Touch Bar input in the specified window with the properties in
-   * aInput.
+   * Updates an array of nsITouchBarInputs in the specified window.
    */
-  void updateTouchBarInput(in nsIBaseWindow aWindow, in nsITouchBarInput aInput);
+  void updateTouchBarInputs(in nsIBaseWindow aWindow, in nsIArray aInputs);
 };