Skip to content

Commit

Permalink
[WFX-454] Add loading of Waterfox custom CSS with nsIStyleSheetService.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlex94 committed Sep 16, 2022
1 parent 0b8e02d commit 864a210
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 7 deletions.
1 change: 1 addition & 0 deletions waterfox/browser/app/profile/000-waterfox.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pref("xpinstall.signatures.required", false);

// ** Theme Related Options ****************************************************
// == Theme Distribution Settings ==============================================
pref("userChrome.theme.enable", true); // Original, Photon
pref("userChrome.tab.connect_to_window", true); // Original, Photon
pref("userChrome.tab.color_like_toolbar", true); // Original, Photon

Expand Down
24 changes: 24 additions & 0 deletions waterfox/browser/components/WaterfoxGlue.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetters(this, {
AddonManager: "resource://gre/modules/AddonManager.jsm",
AttributionCode: "resource:///modules/AttributionCode.jsm",
BrowserUtils: "resource:///modules/BrowserUtils.jsm",
ChromeManifest: "resource:///modules/ChromeManifest.jsm",
Overlays: "resource:///modules/Overlays.jsm",
PrefUtils: "resource:///modules/PrefUtils.jsm",
PrivateTab: "resource:///modules/PrivateTab.jsm",
StatusBar: "resource:///modules/StatusBar.jsm",
TabFeatures: "resource:///modules/TabFeatures.jsm",
Expand All @@ -30,6 +32,15 @@ const WaterfoxGlue = {
// Parse attribution data
this._setAttributionData();

// Set pref observers
this._setPrefObservers();

// Load always on Waterfox custom CSS.
// Add additional CSS here.
BrowserUtils.registerStylesheet(
"chrome://browser/skin/waterfox/general.css"
);

// Parse chrome.manifest
this.startupManifest = await this.getChromeManifest("startup");
this.privateManifest = await this.getChromeManifest("private");
Expand Down Expand Up @@ -128,6 +139,19 @@ const WaterfoxGlue = {
}
},

async _setPrefObservers() {
this.leptonListener = PrefUtils.addObserver(
"userChrome.theme.enable",
isEnabled => {
// Pref being false means we need to unload the sheet.
const userChromeSheet = "chrome://browser/skin/userChrome.css";
const userContentSheet = "chrome://browser/skin/userContent.css"
BrowserUtils.registerOrUnregisterSheet(userChromeSheet, isEnabled);
BrowserUtils.registerOrUnregisterSheet(userContentSheet, isEnabled);
}
);
},

async getChromeManifest(manifest) {
let uri;
let privateWindow = false;
Expand Down
52 changes: 48 additions & 4 deletions waterfox/browser/components/utils/BrowserUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

const EXPORTED_SYMBOLS = ["BrowserUtils"];

const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);

const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");

const { CustomizableUI } = ChromeUtils.import(
Expand All @@ -16,6 +20,13 @@ const { PanelMultiView } = ChromeUtils.import(
"resource:///modules/PanelMultiView.jsm"
);

XPCOMUtils.defineLazyServiceGetter(
this,
"styleSheetService",
"@mozilla.org/content/style-sheet-service;1",
"nsIStyleSheetService"
);

const BrowserUtils = {
// internal functions/props
get mostRecentWindow() {
Expand Down Expand Up @@ -61,15 +72,48 @@ const BrowserUtils = {
}
},

executeInAllWindows(aFunc) {
/**
* Helper function to execute a given function with some args in every open browser window.
* Window must be the functions first arg, subsequent args are passed in the same manner
* as to executeInAllWindows().
* @param func - The function to be called in each open browser window.
* @param args - The arguments to supply to the function.
* Example:
* BrowserUtils.executeInAllWindows(Urlbar.addDynamicStylesheet, "chrome://browser/skin/waterfox.css")
*/
executeInAllWindows(func, ...args) {
let windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let win = windows.getNext();
let { document } = win;
aFunc(document, win);
let window = windows.getNext();
func(window, ...args);
}
},

/**
* Helper method to register or unregister a given stylesheet depending on the bool arg passed.
* @param uri - The URI of the stylesheet to register or unregister.
* @param enabled - A boolean indicating whether to register or unregister the sheet.
*/
registerOrUnregisterSheet(uri, enabled = false) {
if (enabled) {
BrowserUtils.registerStylesheet(uri);
} else {
BrowserUtils.unregisterStylesheet(uri);
}
},

registerStylesheet(uri) {
let url = Services.io.newURI(uri);
let type = styleSheetService.USER_SHEET;
styleSheetService.loadAndRegisterSheet(url, type);
},

unregisterStylesheet(uri) {
let url = Services.io.newURI(uri);
let type = styleSheetService.USER_SHEET;
styleSheetService.unregisterSheet(url, type);
},

setStyle(aStyleSheet) {
let styleSheetService = Cc[
"@mozilla.org/content/style-sheet-service;1"
Expand Down
7 changes: 4 additions & 3 deletions waterfox/browser/themes/jar.mn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

browser.jar:
% skin browser classic/1.0 %skin/classic/browser/
skin/classic/browser/userChrome.css (lepton/leptonChrome.css)
skin/classic/browser/userContent.css (lepton/leptonContent.css)
skin/classic/browser/lepton/ (lepton/icons/*.svg)
skin/classic/browser/userChrome.css (lepton/leptonChrome.css)
skin/classic/browser/userContent.css (lepton/leptonContent.css)
skin/classic/browser/lepton/ (lepton/icons/*.svg)
* skin/classic/browser/waterfox/general.css (waterfox/general.css)
13 changes: 13 additions & 0 deletions waterfox/browser/themes/waterfox/general.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* Add any imports of other css here. */

%ifndef XP_MACOSX
/* Ensure that bottom positioned tabs are colored according to theme */
#browser-bottombox {
background-color: var(--lwt-accent-color);
}
%endif

/* Ensure that bottom positioned tabs collapse when video's are fullscreen */
:root[inDOMFullscreen] #browser-bottombox {
visibility: collapse;
}

0 comments on commit 864a210

Please sign in to comment.