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-02-27 12:51:10 +00:00
|
|
|
const Services = require("Services");
|
2014-12-12 19:28:24 +00:00
|
|
|
|
2016-07-28 18:11:42 +00:00
|
|
|
const variableFileContents = require("raw!devtools/client/themes/variables.css");
|
|
|
|
|
2015-10-07 15:19:44 +00:00
|
|
|
const THEME_SELECTOR_STRINGS = {
|
|
|
|
light: ":root.theme-light {",
|
2016-10-06 20:43:57 +00:00
|
|
|
dark: ":root.theme-dark {",
|
|
|
|
firebug: ":root.theme-firebug {"
|
2016-05-17 18:25:54 +00:00
|
|
|
};
|
2014-12-12 19:28:24 +00:00
|
|
|
|
|
|
|
/**
|
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 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
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-11-21 15:47:10 +00:00
|
|
|
* Set the theme preference.
|
2014-12-12 19:28:24 +00:00
|
|
|
*/
|
|
|
|
const setTheme = exports.setTheme = (newTheme) => {
|
|
|
|
Services.prefs.setCharPref("devtools.theme", newTheme);
|
|
|
|
};
|
2016-05-19 14:35:45 +00:00
|
|
|
/* eslint-enable */
|