Bug 870011 - Allow user to restore customized toolbars and panels to their default states without a restart. r=mconley.
authorMike de Boer <mdeboer@mozilla.com>
Fri, 24 May 2013 17:43:39 +0200
changeset 155337 bab65fae9ff62deb4d9093ea66a6f06cb7dec4aa
parent 155336 a77f006151883bc92d22d27eff3e67ea0cf0d00a
child 155338 4153a68faf4f9949f0523ec584074551c9e16305
push id25666
push userjwein@mozilla.com
push dateMon, 18 Nov 2013 15:56:58 +0000
treeherdermozilla-central@f2adb62d07eb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley
bugs870011
milestone24.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 870011 - Allow user to restore customized toolbars and panels to their default states without a restart. r=mconley.
browser/components/customizableui/src/CustomizableUI.jsm
browser/components/customizableui/src/CustomizeMode.jsm
--- a/browser/components/customizableui/src/CustomizableUI.jsm
+++ b/browser/components/customizableui/src/CustomizableUI.jsm
@@ -1415,16 +1415,34 @@ let CustomizableUIInternal = {
     }
 
     throw new Error("Could not find any window nodes for area " + aArea);
   },
 
   reset: function() {
     Services.prefs.clearUserPref(kPrefCustomizationState);
     LOG("State reset");
+
+    // Reset placements to make restoring default placements possible.
+    gPlacements = new Map();
+    // Clear the saved state to ensure that defaults will be used.
+    gSavedState = null;
+    // Restore the state for each area to its defaults
+    for (let [area, defaultPlacements] of gDefaultPlacements) {
+      this.restoreStateForArea(area);
+    }
+
+    // Rebuild each registered area (across windows) to reflect the state that
+    // was reset above.
+    for (let [areaId, areaNodes] of gBuildAreas) {
+      let placements = gPlacements.get(areaId);
+      for (let areaNode of areaNodes) {
+        this.buildArea(areaId, placements, areaNode);
+      }
+    }
   },
 
   _addParentFlex: function(aElement) {
     // If necessary, add flex to accomodate new child.
     if (aElement.hasAttribute("flex")) {
       let parent = aElement.parentNode;
       let parentFlex = parent.hasAttribute("flex") ? parseInt(parent.getAttribute("flex"), 10) : 0;
       let elementFlex = parseInt(aElement.getAttribute("flex"), 10);
--- a/browser/components/customizableui/src/CustomizeMode.jsm
+++ b/browser/components/customizableui/src/CustomizeMode.jsm
@@ -119,33 +119,17 @@ CustomizeMode.prototype = {
     // while customizing.
     let panelHolder = document.getElementById("customization-panelHolder");
     panelHolder.appendChild(window.PanelUI.mainView);
 
     let self = this;
     deck.addEventListener("transitionend", function customizeTransitionEnd() {
       deck.removeEventListener("transitionend", customizeTransitionEnd);
 
-      // Add drag-and-drop event handlers to all of the customizable areas.
-      self.areas = [];
-      for (let area of CustomizableUI.areas) {
-        let target = CustomizableUI.getCustomizeTargetForArea(area, window);
-        target.addEventListener("dragstart", self);
-        target.addEventListener("dragover", self);
-        target.addEventListener("dragexit", self);
-        target.addEventListener("drop", self);
-        target.addEventListener("dragend", self);
-        for (let child of target.children) {
-          if (self.isCustomizableItem(child)) {
-            self.wrapToolbarItem(child, getPlaceForItem(child));
-          }
-        }
-        self.areas.push(target);
-      }
-
+      self._wrapToolbarItems();
       self.populatePalette();
       self.dispatchToolboxEvent("customizationready");
     });
 
     this.visiblePalette.addEventListener("dragstart", this);
     this.visiblePalette.addEventListener("dragover", this);
     this.visiblePalette.addEventListener("dragexit", this);
     this.visiblePalette.addEventListener("drop", this);
@@ -190,28 +174,17 @@ CustomizeMode.prototype = {
     let tabViewDeck = document.getElementById("tab-view-deck");
     tabViewDeck.addEventListener("transitionend", function onTransitionEnd(evt) {
       if (evt.propertyName != "padding-top")
         return;
       tabViewDeck.removeEventListener("transitionend", onTransitionEnd);
       documentElement.removeAttribute("customize-exiting");
     });
 
-    for (let target of this.areas) {
-      for (let toolbarItem of target.children) {
-        if (this.isWrappedToolbarItem(toolbarItem)) {
-          this.unwrapToolbarItem(toolbarItem);
-        }
-      }
-      target.removeEventListener("dragstart", this);
-      target.removeEventListener("dragover", this);
-      target.removeEventListener("dragexit", this);
-      target.removeEventListener("drop", this);
-      target.removeEventListener("dragend", this);
-    }
+    this._unwrapToolbarItems();
 
     if (this._changed) {
       // XXXmconley: At first, it seems strange to also persist the old way with
       //             currentset - but this might actually be useful for switching
       //             to old builds. We might want to keep this around for a little
       //             bit.
       this.persistCurrentSets();
     }
@@ -441,30 +414,83 @@ CustomizeMode.prototype = {
     }
 
     if (aWrapper.parentNode) {
       aWrapper.parentNode.replaceChild(toolbarItem, aWrapper);
     }
     return toolbarItem;
   },
 
+  _wrapToolbarItems: function() {
+    let window = this.window;
+    // Add drag-and-drop event handlers to all of the customizable areas.
+    this.areas = [];
+    for (let area of CustomizableUI.areas) {
+      let target = CustomizableUI.getCustomizeTargetForArea(area, window);
+      target.addEventListener("dragstart", this);
+      target.addEventListener("dragover", this);
+      target.addEventListener("dragexit", this);
+      target.addEventListener("drop", this);
+      target.addEventListener("dragend", this);
+      for (let child of target.children) {
+        if (this.isCustomizableItem(child)) {
+          this.wrapToolbarItem(child, getPlaceForItem(child));
+        }
+      }
+      this.areas.push(target);
+    }
+  },
+
+  _unwrapToolbarItems: function() {
+    for (let target of this.areas) {
+      for (let toolbarItem of target.children) {
+        if (this.isWrappedToolbarItem(toolbarItem)) {
+          this.unwrapToolbarItem(toolbarItem);
+        }
+      }
+      target.removeEventListener("dragstart", this);
+      target.removeEventListener("dragover", this);
+      target.removeEventListener("dragexit", this);
+      target.removeEventListener("drop", this);
+      target.removeEventListener("dragend", this);
+    }
+  },
+
   persistCurrentSets: function()  {
     let document = this.document;
     let toolbars = document.querySelectorAll("toolbar[customizable='true']");
     for (let toolbar of toolbars) {
       let set = toolbar.currentSet;
       toolbar.setAttribute("currentset", set);
       LOG("Setting currentset of " + toolbar.id + " as " + set);
       // Persist the currentset attribute directly on hardcoded toolbars.
       document.persist(toolbar.id, "currentset");
     }
   },
 
   reset: function() {
+    this.depopulatePalette();
+    this._unwrapToolbarItems();
+
     CustomizableUI.reset();
+
+    this._wrapToolbarItems();
+    this.populatePalette();
+
+    let document = this.document;
+    let toolbars = document.querySelectorAll("toolbar[customizable='true']");
+    for (let toolbar of toolbars) {
+      let set = toolbar.currentSet;
+      toolbar.removeAttribute("currentset");
+      LOG("[RESET] Removing currentset of " + toolbar.id);
+      // Persist the currentset attribute directly on hardcoded toolbars.
+      document.persist(toolbar.id, "currentset");
+    }
+
+    this.resetButton.hidden = CustomizableUI.inDefaultState;
   },
 
   onWidgetMoved: function(aWidgetId, aArea, aOldPosition, aNewPosition) {
     this._onUIChange();
   },
 
   onWidgetAdded: function(aWidgetId, aArea, aPosition) {
     this._onUIChange();