Bug 1061747 - Makes the "Restore Defaults" button reset the browser theme. Also adds test to check whether this button resets to the default theme successfully.r?jaws
MozReview-Commit-ID: 4rk5avn74i4
--- a/browser/components/customizableui/CustomizableUI.jsm
+++ b/browser/components/customizableui/CustomizableUI.jsm
@@ -22,16 +22,18 @@ XPCOMUtils.defineLazyModuleGetter(this,
XPCOMUtils.defineLazyGetter(this, "gWidgetsBundle", function() {
const kUrl = "chrome://browser/locale/customizableui/customizableWidgets.properties";
return Services.strings.createBundle(kUrl);
});
XPCOMUtils.defineLazyModuleGetter(this, "ShortcutUtils",
"resource://gre/modules/ShortcutUtils.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "gELS",
"@mozilla.org/eventlistenerservice;1", "nsIEventListenerService");
+XPCOMUtils.defineLazyModuleGetter(this, "LightweightThemeManager",
+ "resource://gre/modules/LightweightThemeManager.jsm");
const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const kSpecialWidgetPfx = "customizableui-special-";
const kPrefCustomizationState = "browser.uiCustomization.state";
const kPrefCustomizationAutoAdd = "browser.uiCustomization.autoAdd";
const kPrefCustomizationDebug = "browser.uiCustomization.debug";
@@ -146,16 +148,17 @@ var gBuildWindows = new Map();
var gNewElementCount = 0;
var gGroupWrapperCache = new Map();
var gSingleWrapperCache = new WeakMap();
var gListeners = new Set();
var gUIStateBeforeReset = {
uiCustomizationState: null,
drawInTitlebar: null,
+ currentTheme: null,
gUIStateBeforeReset: null,
};
XPCOMUtils.defineLazyGetter(this, "log", () => {
let scope = {};
Cu.import("resource://gre/modules/Console.jsm", scope);
let debug;
try {
@@ -2523,22 +2526,24 @@ var CustomizableUIInternal = {
gResetting = false;
},
_resetUIState: function() {
try {
gUIStateBeforeReset.drawInTitlebar = Services.prefs.getBoolPref(kPrefDrawInTitlebar);
gUIStateBeforeReset.uiCustomizationState = Services.prefs.getCharPref(kPrefCustomizationState);
+ gUIStateBeforeReset.currentTheme = LightweightThemeManager.currentTheme;
} catch(e) { }
this._resetExtraToolbars();
Services.prefs.clearUserPref(kPrefCustomizationState);
Services.prefs.clearUserPref(kPrefDrawInTitlebar);
+ LightweightThemeManager.currentTheme = null;
log.debug("State reset");
// Reset placements to make restoring default placements possible.
gPlacements = new Map();
gDirtyAreaCache = new Set();
gSeenWidgets = new Set();
// Clear the saved state to ensure that defaults will be used.
gSavedState = null;
@@ -2597,23 +2602,25 @@ var CustomizableUIInternal = {
if (gUIStateBeforeReset.uiCustomizationState == null ||
gUIStateBeforeReset.drawInTitlebar == null) {
return;
}
gUndoResetting = true;
let uiCustomizationState = gUIStateBeforeReset.uiCustomizationState;
let drawInTitlebar = gUIStateBeforeReset.drawInTitlebar;
+ let currentTheme = gUIStateBeforeReset.currentTheme;
// Need to clear the previous state before setting the prefs
// because pref observers may check if there is a previous UI state.
this._clearPreviousUIState();
Services.prefs.setCharPref(kPrefCustomizationState, uiCustomizationState);
Services.prefs.setBoolPref(kPrefDrawInTitlebar, drawInTitlebar);
+ LightweightThemeManager.currentTheme = currentTheme;
this.loadSavedState();
// If the user just customizes toolbar/titlebar visibility, gSavedState will be null
// and we don't need to do anything else here:
if (gSavedState) {
for (let areaId of Object.keys(gSavedState.placements)) {
let placements = gSavedState.placements[areaId];
gPlacements.set(areaId, placements);
}
@@ -2782,16 +2789,21 @@ var CustomizableUIInternal = {
}
}
if (Services.prefs.prefHasUserValue(kPrefDrawInTitlebar)) {
log.debug(kPrefDrawInTitlebar + " pref is non-default");
return false;
}
+ if(LightweightThemeManager.currentTheme) {
+ log.debug(LightweightThemeManager.currentTheme + " theme is non-default");
+ return false;
+ }
+
return true;
},
setToolbarVisibility: function(aToolbarId, aIsVisible) {
// We only persist the attribute the first time.
let isFirstChangedToolbar = true;
for (let window of CustomizableUI.windows) {
let toolbar = window.document.getElementById(aToolbarId);
@@ -3486,17 +3498,18 @@ this.CustomizableUI = {
/**
* Can the last Restore Defaults operation be undone.
*
* @return A boolean stating whether an undo of the
* Restore Defaults can be performed.
*/
get canUndoReset() {
return gUIStateBeforeReset.uiCustomizationState != null ||
- gUIStateBeforeReset.drawInTitlebar != null;
+ gUIStateBeforeReset.drawInTitlebar != null ||
+ gUIStateBeforeReset.currentTheme != null;
},
/**
* Get the placement of a widget. This is by far the best way to obtain
* information about what the state of your widget is. The internals of
* this call are cheap (no DOM necessary) and you will know where the user
* has put your widget.
*
--- a/browser/components/customizableui/CustomizeMode.jsm
+++ b/browser/components/customizableui/CustomizeMode.jsm
@@ -1170,16 +1170,18 @@ CustomizeMode.prototype = {
btn.disabled = true;
return Task.spawn(function*() {
this._removePanelCustomizationPlaceholders();
yield this.depopulatePalette();
yield this._unwrapToolbarItems();
CustomizableUI.reset();
+ this.swatchForTheme(this.document);
+
yield this._wrapToolbarItems();
this.populatePalette();
this.persistCurrentSets(true);
this._updateResetButton();
this._updateUndoResetButton();
this._updateEmptyPaletteNotice();
@@ -1196,16 +1198,18 @@ CustomizeMode.prototype = {
return Task.spawn(function*() {
this._removePanelCustomizationPlaceholders();
yield this.depopulatePalette();
yield this._unwrapToolbarItems();
CustomizableUI.undoReset();
+ this.swatchForTheme(this.document);
+
yield this._wrapToolbarItems();
this.populatePalette();
this.persistCurrentSets(true);
this._updateResetButton();
this._updateUndoResetButton();
this._updateEmptyPaletteNotice();
@@ -1453,16 +1457,19 @@ CustomizeMode.prototype = {
this.swatchForTheme(doc);
for (let element of [footer, recommendedLabel]) {
while (element.previousSibling &&
element.previousSibling.localName == "toolbarbutton") {
element.previousSibling.remove();
}
}
aEvent.target.removeAttribute("height");
+
+ if(LightweightThemeManager.currentTheme)
+ this._onUIChange();
},
_onUIChange: function() {
this._changed = true;
if (!this.resetting) {
this._updateResetButton();
this._updateUndoResetButton();
this._updateEmptyPaletteNotice();
--- a/browser/components/customizableui/test/browser_1007336_lwthemes_in_customize_mode.js
+++ b/browser/components/customizableui/test/browser_1007336_lwthemes_in_customize_mode.js
@@ -47,16 +47,32 @@ add_task(function* () {
"The second theme in the 'My Themes' section should be the newly installed theme: " +
"Installed theme id: " + installedThemeId + "; First theme ID: " + firstLWThemeId);
is(header.nextSibling.nextSibling.nextSibling, recommendedHeader,
"There should be two themes in the 'My Themes' section");
let defaultTheme = header.nextSibling;
defaultTheme.doCommand();
is(Services.prefs.getCharPref("lightweightThemes.selectedThemeID"), "", "No lwtheme should be selected");
+
+ // ensure current theme isn't set to "Default"
+ popupShownPromise = popupShown(popup);
+ EventUtils.synthesizeMouseAtCenter(themesButton, {});
+ info("Clicked on themes button a second time");
+ yield popupShownPromise;
+
+ firstLWTheme = recommendedHeader.nextSibling;
+ themeChangedPromise = promiseObserverNotified("lightweight-theme-changed");
+ firstLWTheme.doCommand();
+ info("Clicked on first theme again");
+ yield themeChangedPromise;
+
+ // check that "Restore Defaults" button resets theme
+ yield gCustomizeMode.reset();
+ is(LightweightThemeManager.currentTheme, null, "Current theme reset to default");
});
add_task(function* asyncCleanup() {
yield endCustomizing();
Services.prefs.clearUserPref("lightweightThemes.usedThemes");
Services.prefs.clearUserPref("lightweightThemes.recommendedThemes");
});
--- a/browser/components/customizableui/test/browser_970511_undo_restore_default.js
+++ b/browser/components/customizableui/test/browser_970511_undo_restore_default.js
@@ -1,33 +1,52 @@
/* 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/. */
"use strict";
requestLongerTimeout(2);
-// Restoring default should show an "undo" option which undoes the restoring operation.
+// Restoring default should reset theme and show an "undo" option which undoes the restoring operation.
add_task(function*() {
let homeButtonId = "home-button";
CustomizableUI.removeWidgetFromArea(homeButtonId);
yield startCustomizing();
ok(!CustomizableUI.inDefaultState, "Not in default state to begin with");
is(CustomizableUI.getPlacementOfWidget(homeButtonId), null, "Home button is in palette");
let undoResetButton = document.getElementById("customization-undo-reset-button");
is(undoResetButton.hidden, true, "The undo button is hidden before reset");
+ let themesButton = document.getElementById("customization-lwtheme-button");
+ let popup = document.getElementById("customization-lwtheme-menu");
+ let popupShownPromise = popupShown(popup);
+ EventUtils.synthesizeMouseAtCenter(themesButton, {});
+ info("Clicked on themes button");
+ yield popupShownPromise;
+
+ let recommendedHeader = document.getElementById("customization-lwtheme-menu-recommended");
+ let firstLWTheme = recommendedHeader.nextSibling;
+ let firstLWThemeId = firstLWTheme.theme.id;
+ let themeChangedPromise = promiseObserverNotified("lightweight-theme-changed");
+ firstLWTheme.doCommand();
+ info("Clicked on first theme");
+ yield themeChangedPromise;
+
+ is(LightweightThemeManager.currentTheme.id, firstLWThemeId, "Theme changed to first option");
+
yield gCustomizeMode.reset();
ok(CustomizableUI.inDefaultState, "In default state after reset");
is(undoResetButton.hidden, false, "The undo button is visible after reset");
+ is(LightweightThemeManager.currentTheme, null, "Theme reset to default");
yield gCustomizeMode.undoReset()
+ is(LightweightThemeManager.currentTheme.id, firstLWThemeId, "Theme has been reset from default to original choice");
ok(!CustomizableUI.inDefaultState, "Not in default state after undo-reset");
is(undoResetButton.hidden, true, "The undo button is hidden after clicking on the undo button");
is(CustomizableUI.getPlacementOfWidget(homeButtonId), null, "Home button is in palette");
yield gCustomizeMode.reset();
});
// Performing an action after a reset will hide the reset button.