2014-12-12 19:28:24 +00:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2016-05-19 14:35:45 +00:00
|
|
|
const { Cu } = require("chrome");
|
2014-12-12 19:28:24 +00:00
|
|
|
const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
|
2016-02-27 12:51:10 +00:00
|
|
|
const Services = require("Services");
|
2016-02-11 12:29:47 +00:00
|
|
|
const { gDevTools } = require("devtools/client/framework/devtools");
|
2014-12-12 19:28:24 +00:00
|
|
|
|
2015-10-30 18:59:30 +00:00
|
|
|
const VARIABLES_URI = "chrome://devtools/skin/variables.css";
|
2015-10-07 15:19:44 +00:00
|
|
|
const THEME_SELECTOR_STRINGS = {
|
|
|
|
light: ":root.theme-light {",
|
|
|
|
dark: ":root.theme-dark {"
|
2016-05-17 18:25:54 +00:00
|
|
|
};
|
2014-12-12 19:28:24 +00:00
|
|
|
|
2015-10-07 15:19:44 +00:00
|
|
|
let variableFileContents;
|
2014-12-12 19:28:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string of the file found at URI
|
|
|
|
*/
|
2016-05-17 18:25:54 +00:00
|
|
|
function readURI(uri) {
|
2015-05-22 02:52:03 +00:00
|
|
|
let stream = NetUtil.newChannel({
|
|
|
|
uri: NetUtil.newURI(uri, "UTF-8"),
|
|
|
|
loadUsingSystemPrincipal: true}
|
|
|
|
).open();
|
2015-03-12 18:26:57 +00:00
|
|
|
|
2014-12-12 19:28:24 +00:00
|
|
|
let count = stream.available();
|
2016-05-19 14:35:45 +00:00
|
|
|
let data = NetUtil.readInputStreamToString(stream, count, {
|
|
|
|
charset: "UTF-8"
|
|
|
|
});
|
2014-12-12 19:28:24 +00:00
|
|
|
stream.close();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-07 15:19:44 +00:00
|
|
|
* Takes a theme name and returns the contents of its variable rule block.
|
|
|
|
* The first time this runs fetches the variables CSS file and caches it.
|
2014-12-12 19:28:24 +00:00
|
|
|
*/
|
2016-05-17 18:25:54 +00:00
|
|
|
function getThemeFile(name) {
|
2015-10-07 15:19:44 +00:00
|
|
|
if (!variableFileContents) {
|
|
|
|
variableFileContents = readURI(VARIABLES_URI);
|
2014-12-12 19:28:24 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 15:19:44 +00:00
|
|
|
// If there's no theme expected for this name, use `light` as default.
|
|
|
|
let selector = THEME_SELECTOR_STRINGS[name] ||
|
2016-05-19 14:35:45 +00:00
|
|
|
THEME_SELECTOR_STRINGS.light;
|
2015-10-07 15:19:44 +00:00
|
|
|
|
|
|
|
// This is a pretty naive way to find the contents between:
|
|
|
|
// selector {
|
|
|
|
// name: val;
|
|
|
|
// }
|
|
|
|
// There is test coverage for this feature (browser_theme.js)
|
|
|
|
// so if an } is introduced in the variables file it will catch that.
|
|
|
|
let theme = variableFileContents;
|
|
|
|
theme = theme.substring(theme.indexOf(selector));
|
|
|
|
theme = theme.substring(0, theme.indexOf("}"));
|
|
|
|
|
|
|
|
return theme;
|
2014-12-12 19:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the string value of the current theme,
|
|
|
|
* like "dark" or "light".
|
|
|
|
*/
|
2016-05-19 14:35:45 +00:00
|
|
|
const getTheme = exports.getTheme = () => {
|
|
|
|
return Services.prefs.getCharPref("devtools.theme");
|
|
|
|
};
|
2014-12-12 19:28:24 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-19 14:35:45 +00:00
|
|
|
* 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.
|
2014-12-12 19:28:24 +00:00
|
|
|
*/
|
2016-05-19 14:35:45 +00:00
|
|
|
/* eslint-disable no-unused-vars */
|
2014-12-12 19:28:24 +00:00
|
|
|
const getColor = exports.getColor = (type, theme) => {
|
|
|
|
let themeName = theme || getTheme();
|
|
|
|
let themeFile = getThemeFile(themeName);
|
2015-10-07 15:19:44 +00:00
|
|
|
let match = themeFile.match(new RegExp("--theme-" + type + ": (.*);"));
|
2014-12-12 19:28:24 +00:00
|
|
|
|
|
|
|
// Return the appropriate variable in the theme, or otherwise, null.
|
2015-10-07 15:19:44 +00:00
|
|
|
return match ? match[1] : null;
|
2014-12-12 19:28:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) => {
|
2015-05-31 11:12:39 +00:00
|
|
|
let oldTheme = getTheme();
|
|
|
|
|
2014-12-12 19:28:24 +00:00
|
|
|
Services.prefs.setCharPref("devtools.theme", newTheme);
|
|
|
|
gDevTools.emit("pref-changed", {
|
|
|
|
pref: "devtools.theme",
|
|
|
|
newValue: newTheme,
|
2015-05-31 11:12:39 +00:00
|
|
|
oldValue: oldTheme
|
2014-12-12 19:28:24 +00:00
|
|
|
});
|
|
|
|
};
|
2016-05-19 14:35:45 +00:00
|
|
|
/* eslint-enable */
|