Files
roytam1 9af117c445 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1138649 - Update remaining callsites to use newChannel2 in toolkit/components (r=gijs,billm) (fa57770530)
- Bug 1167053 - Convert NetUtil.newChannel2 callsites to use new API - update newChannel2 (r=sicking,paolo) (3049b3a9c0)
- Bug 1138808 r=vchang (34450de32b)
- Bug 1166580 - Add tests for mozHasPendingMessage. r=fabrice (09e245c692)
- Bug 1195942 - System message registration fails for substituted permissions r=gwagner (b622602e2b)
- Bug 1196988 - Remove THA support. r=gwagner (4b9100c298)
- Bug 1181555 - Add 'experimental-webcomponents' permission. r=fabrice (204680f324)
- Bug 1139953 - Pass a dummy channel into the cookie service so that nsICookiePermission can compute private browsing mode correctly. r=jdm (13aafbd766)
- Bug 1186920 - Fix b2g applications that use cookies. r=gwagner (1f8525ac26)
2022-03-30 15:02:42 +08:00

95 lines
2.8 KiB
JavaScript

/* 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";
/**
* Colors for themes taken from:
* https://developer.mozilla.org/en-US/docs/Tools/DevToolsColors
*/
const { Ci, Cu } = require("chrome");
const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
loader.lazyRequireGetter(this, "Services");
loader.lazyImporter(this, "gDevTools", "resource://gre/modules/devtools/gDevTools.jsm");
const themeURIs = {
light: "chrome://global/skin/devtools/light-theme.css",
dark: "chrome://global/skin/devtools/dark-theme.css"
}
const cachedThemes = {};
/**
* Returns a string of the file found at URI
*/
function readURI (uri) {
let stream = NetUtil.newChannel({
uri: NetUtil.newURI(uri, "UTF-8"),
loadUsingSystemPrincipal: true}
).open();
let count = stream.available();
let data = NetUtil.readInputStreamToString(stream, count, { charset: "UTF-8" });
stream.close();
return data;
}
/**
* Takes a theme name and either returns it from the cache,
* or fetches the theme CSS file and caches it.
*/
function getThemeFile (name) {
// Use the cached theme, or generate it
let themeFile = cachedThemes[name] || readURI(themeURIs[name]).match(/--theme-.*: .*;/g).join("\n");
// Cache if not already cached
if (!cachedThemes[name]) {
cachedThemes[name] = themeFile;
}
return themeFile;
}
/**
* Returns the string value of the current theme,
* like "dark" or "light".
*/
const getTheme = exports.getTheme = () => Services.prefs.getCharPref("devtools.theme");
/**
* Returns a color indicated by `type` (like "toolbar-background", or "highlight-red"),
* with the ability to specify a theme, or use whatever the current theme is
* if left unset. If theme not found, falls back to "light" theme. Returns null
* if the type cannot be found for the theme given.
*/
const getColor = exports.getColor = (type, theme) => {
let themeName = theme || getTheme();
// If there's no theme URIs for this theme, use `light` as default.
if (!themeURIs[themeName]) {
themeName = "light";
}
let themeFile = getThemeFile(themeName);
let match;
// Return the appropriate variable in the theme, or otherwise, null.
return (match = themeFile.match(new RegExp("--theme-" + type + ": (.*);"))) ? match[1] : null;
};
/**
* Mimics selecting the theme selector in the toolbox;
* sets the preference and emits an event on gDevTools to trigger
* the themeing.
*/
const setTheme = exports.setTheme = (newTheme) => {
Services.prefs.setCharPref("devtools.theme", newTheme);
gDevTools.emit("pref-changed", {
pref: "devtools.theme",
newValue: newTheme,
oldValue: getTheme()
});
};