Bug 1053434 - Listen for lightweight theme changes and call relevant listeners for devedition theme;r=Gijs

This commit is contained in:
Brian Grinstead 2014-10-30 15:52:18 -07:00
parent ae2d218e50
commit 730d24de95
2 changed files with 71 additions and 5 deletions

View File

@ -14,10 +14,11 @@ let DevEdition = {
styleSheetLocation: "chrome://browser/skin/devedition.css",
styleSheet: null,
defaultThemeID: "{972ce4c6-7e08-4474-a285-3208198ce6fd}",
init: function () {
this._updateDevtoolsThemeAttribute();
this._updateStyleSheet();
this._updateStyleSheetFromPrefs();
// Listen for changes to all prefs except for complete themes.
// No need for this since changing a complete theme requires a
@ -25,14 +26,27 @@ let DevEdition = {
Services.prefs.addObserver(this._lwThemePrefName, this, false);
Services.prefs.addObserver(this._prefName, this, false);
Services.prefs.addObserver(this._devtoolsThemePrefName, this, false);
Services.obs.addObserver(this, "lightweight-theme-styling-update", false);
},
observe: function (subject, topic, data) {
if (topic == "lightweight-theme-styling-update") {
let newTheme = JSON.parse(data);
if (!newTheme || newTheme.id === this.defaultThemeID) {
// A lightweight theme has been unapplied, so just re-read prefs.
this._updateStyleSheetFromPrefs();
} else {
// A lightweight theme has been applied, but the pref may not be
// set yet if this happened from customize menu or addons page.
this._toggleStyleSheet(false);
}
}
if (topic == "nsPref:changed") {
if (data == this._devtoolsThemePrefName) {
this._updateDevtoolsThemeAttribute();
} else {
this._updateStyleSheet();
this._updateStyleSheetFromPrefs();
}
}
},
@ -42,11 +56,12 @@ let DevEdition = {
// to change colors based on the selected devtools theme.
document.documentElement.setAttribute("devtoolstheme",
Services.prefs.getCharPref(this._devtoolsThemePrefName));
if (this.styleSheet) {
ToolbarIconColor.inferFromText();
}
},
_updateStyleSheet: function() {
// Only try to apply the dev edition theme if it is preffered
// on and there are no other themes applied.
_updateStyleSheetFromPrefs: function() {
let lightweightThemeSelected = false;
try {
lightweightThemeSelected = Services.prefs.getBoolPref(this._lwThemePrefName);
@ -60,18 +75,24 @@ let DevEdition = {
let deveditionThemeEnabled = Services.prefs.getBoolPref(this._prefName) &&
!lightweightThemeSelected && defaultThemeSelected;
this._toggleStyleSheet(deveditionThemeEnabled);
},
_toggleStyleSheet: function(deveditionThemeEnabled) {
if (deveditionThemeEnabled && !this.styleSheet) {
let styleSheetAttr = `href="${this.styleSheetLocation}" type="text/css"`;
let styleSheet = this.styleSheet = document.createProcessingInstruction(
'xml-stylesheet', styleSheetAttr);
this.styleSheet.addEventListener("load", function onLoad() {
styleSheet.removeEventListener("load", onLoad);
gBrowser.tabContainer._positionPinnedTabs();
ToolbarIconColor.inferFromText();
});
document.insertBefore(this.styleSheet, document.documentElement);
} else if (!deveditionThemeEnabled && this.styleSheet) {
this.styleSheet.remove();
this.styleSheet = null;
gBrowser.tabContainer._positionPinnedTabs();
ToolbarIconColor.inferFromText();
}
},
@ -80,6 +101,7 @@ let DevEdition = {
Services.prefs.removeObserver(this._lwThemePrefName, this);
Services.prefs.removeObserver(this._prefName, this);
Services.prefs.removeObserver(this._devtoolsThemePrefName, this);
Services.obs.removeObserver(this, "lightweight-theme-styling-update", false);
this.styleSheet = null;
}
};

View File

@ -65,5 +65,49 @@ function startTests() {
is (document.documentElement.getAttribute("devtoolstheme"), "light",
"The documentElement has an attribute based on devtools theme.");
testLightweightThemePreview();
finish();
}
function dummyLightweightTheme(id) {
return {
id: id,
name: id,
headerURL: "http://lwttest.invalid/a.png",
footerURL: "http://lwttest.invalid/b.png",
textcolor: "red",
accentcolor: "blue"
};
}
function testLightweightThemePreview() {
let {LightweightThemeManager} = Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", {});
info ("Turning the pref on, then previewing lightweight themes");
Services.prefs.setBoolPref(PREF_DEVEDITION_THEME, true);
ok (DevEdition.styleSheet, "The devedition stylesheet is enabled.");
LightweightThemeManager.previewTheme(dummyLightweightTheme("preview0"));
ok (!DevEdition.styleSheet, "The devedition stylesheet is not enabled after a lightweight theme preview.");
LightweightThemeManager.previewTheme(dummyLightweightTheme("preview1"));
ok (!DevEdition.styleSheet, "The devedition stylesheet is not enabled after a second lightweight theme preview.");
LightweightThemeManager.resetPreview();
ok (DevEdition.styleSheet, "The devedition stylesheet is enabled again after resetting the preview.");
info ("Turning the pref on, then previewing a theme, turning it off and resetting the preview");
Services.prefs.setBoolPref(PREF_DEVEDITION_THEME, true);
ok (DevEdition.styleSheet, "The devedition stylesheet is enabled.");
LightweightThemeManager.previewTheme(dummyLightweightTheme("preview2"));
ok (!DevEdition.styleSheet, "The devedition stylesheet is not enabled after a lightweight theme preview.");
Services.prefs.setBoolPref(PREF_DEVEDITION_THEME, false);
ok (!DevEdition.styleSheet, "The devedition stylesheet is not enabled after pref is turned off.");
LightweightThemeManager.resetPreview();
ok (!DevEdition.styleSheet, "The devedition stylesheet is still disabled after resetting the preview.");
info ("Turning the pref on, then previewing the default theme, turning it off and resetting the preview");
Services.prefs.setBoolPref(PREF_DEVEDITION_THEME, true);
ok (DevEdition.styleSheet, "The devedition stylesheet is enabled.");
LightweightThemeManager.previewTheme(dummyLightweightTheme("{972ce4c6-7e08-4474-a285-3208198ce6fd}"));
ok (DevEdition.styleSheet, "The devedition stylesheet is still enabled after the default theme is applied.");
LightweightThemeManager.resetPreview();
ok (DevEdition.styleSheet, "The devedition stylesheet is still enabled after resetting the preview.");
}