2018-12-04 13:59:27 -08:00
|
|
|
// From rust:
|
|
|
|
/* global resourcesSuffix */
|
|
|
|
|
2020-10-15 18:03:25 +02:00
|
|
|
var darkThemes = ["dark", "ayu"];
|
2018-01-22 23:44:08 +01:00
|
|
|
var currentTheme = document.getElementById("themeStyle");
|
|
|
|
var mainTheme = document.getElementById("mainThemeStyle");
|
2020-10-15 18:03:25 +02:00
|
|
|
var localStoredTheme = getCurrentValue("rustdoc-theme");
|
2018-01-22 23:44:08 +01:00
|
|
|
|
2018-02-20 20:11:58 +01:00
|
|
|
var savedHref = [];
|
|
|
|
|
2018-11-06 01:40:12 +01:00
|
|
|
function hasClass(elem, className) {
|
2018-12-02 01:22:44 +01:00
|
|
|
return elem && elem.classList && elem.classList.contains(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function addClass(elem, className) {
|
2018-11-16 16:31:07 +01:00
|
|
|
if (!elem || !elem.classList) {
|
|
|
|
return;
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
2018-11-16 16:31:07 +01:00
|
|
|
elem.classList.add(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeClass(elem, className) {
|
2018-11-16 16:31:07 +01:00
|
|
|
if (!elem || !elem.classList) {
|
|
|
|
return;
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
2018-11-16 16:31:07 +01:00
|
|
|
elem.classList.remove(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 23:31:21 +02:00
|
|
|
function onEach(arr, func, reversed) {
|
2018-02-20 20:11:58 +01:00
|
|
|
if (arr && arr.length > 0 && func) {
|
2018-11-16 16:31:07 +01:00
|
|
|
var length = arr.length;
|
2020-05-17 15:34:59 +02:00
|
|
|
var i;
|
2018-09-12 23:31:21 +02:00
|
|
|
if (reversed !== true) {
|
2020-05-17 15:34:59 +02:00
|
|
|
for (i = 0; i < length; ++i) {
|
2018-09-12 23:31:21 +02:00
|
|
|
if (func(arr[i]) === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-05-17 15:34:59 +02:00
|
|
|
for (i = length - 1; i >= 0; --i) {
|
2018-09-12 23:31:21 +02:00
|
|
|
if (func(arr[i]) === true) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 16:16:53 +02:00
|
|
|
return false;
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-02 01:22:44 +01:00
|
|
|
function onEachLazy(lazyArray, func, reversed) {
|
|
|
|
return onEach(
|
|
|
|
Array.prototype.slice.call(lazyArray),
|
|
|
|
func,
|
|
|
|
reversed);
|
|
|
|
}
|
|
|
|
|
2020-05-17 15:34:59 +02:00
|
|
|
function hasOwnProperty(obj, property) {
|
|
|
|
return Object.prototype.hasOwnProperty.call(obj, property);
|
|
|
|
}
|
|
|
|
|
2018-10-15 20:38:51 -07:00
|
|
|
function usableLocalStorage() {
|
|
|
|
// Check if the browser supports localStorage at all:
|
2019-07-24 17:50:48 +02:00
|
|
|
if (typeof Storage === "undefined") {
|
2018-10-15 20:38:51 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Check if we can access it; this access will fail if the browser
|
|
|
|
// preferences deny access to localStorage, e.g., to prevent storage of
|
|
|
|
// "cookies" (or cookie-likes, as is the case here).
|
|
|
|
try {
|
2019-06-02 16:57:26 +02:00
|
|
|
return window.localStorage !== null && window.localStorage !== undefined;
|
2018-10-15 20:38:51 -07:00
|
|
|
} catch(err) {
|
|
|
|
// Storage is supported, but browser preferences deny access to it.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:15:27 -07:00
|
|
|
function updateLocalStorage(name, value) {
|
|
|
|
if (usableLocalStorage()) {
|
|
|
|
localStorage[name] = value;
|
|
|
|
} else {
|
|
|
|
// No Web Storage support so we do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentValue(name) {
|
|
|
|
if (usableLocalStorage() && localStorage[name] !== undefined) {
|
|
|
|
return localStorage[name];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:49:01 +02:00
|
|
|
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
|
2018-02-24 19:14:36 +01:00
|
|
|
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
|
|
|
|
var fullNewTheme = newTheme + resourcesSuffix + ".css";
|
|
|
|
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
|
2018-02-20 20:11:58 +01:00
|
|
|
|
2018-03-08 00:11:17 +01:00
|
|
|
if (styleElem.href === newHref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var found = false;
|
2018-02-20 20:11:58 +01:00
|
|
|
if (savedHref.length === 0) {
|
2018-12-16 14:44:44 +01:00
|
|
|
onEachLazy(document.getElementsByTagName("link"), function(el) {
|
2018-02-20 20:11:58 +01:00
|
|
|
savedHref.push(el.href);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onEach(savedHref, function(el) {
|
|
|
|
if (el === newHref) {
|
|
|
|
found = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (found === true) {
|
|
|
|
styleElem.href = newHref;
|
2020-10-11 17:52:47 +02:00
|
|
|
// If this new value comes from a system setting or from the previously
|
|
|
|
// saved theme, no need to save it.
|
2019-08-21 12:49:01 +02:00
|
|
|
if (saveTheme === true) {
|
2019-05-27 15:57:44 +02:00
|
|
|
updateLocalStorage("rustdoc-theme", newTheme);
|
|
|
|
}
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
2018-01-22 23:44:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 02:53:37 +02:00
|
|
|
function useSystemTheme(value) {
|
|
|
|
if (value === undefined) {
|
|
|
|
value = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLocalStorage("rustdoc-use-system-theme", value);
|
|
|
|
|
|
|
|
// update the toggle if we're on the settings page
|
|
|
|
var toggle = document.getElementById("use-system-theme");
|
|
|
|
if (toggle && toggle instanceof HTMLInputElement) {
|
|
|
|
toggle.checked = value;
|
|
|
|
}
|
2019-05-27 15:57:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-11 02:53:37 +02:00
|
|
|
var updateSystemTheme = (function() {
|
|
|
|
if (!window.matchMedia) {
|
|
|
|
// fallback to the CSS computed value
|
|
|
|
return function() {
|
|
|
|
let cssTheme = getComputedStyle(document.documentElement)
|
|
|
|
.getPropertyValue('content');
|
|
|
|
|
|
|
|
switchTheme(
|
|
|
|
currentTheme,
|
|
|
|
mainTheme,
|
|
|
|
JSON.parse(cssTheme) || light,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// only listen to (prefers-color-scheme: dark) because light is the default
|
|
|
|
var mql = window.matchMedia("(prefers-color-scheme: dark)");
|
|
|
|
|
|
|
|
function handlePreferenceChange(mql) {
|
|
|
|
// maybe the user has disabled the setting in the meantime!
|
|
|
|
if (getCurrentValue("rustdoc-use-system-theme") !== "false") {
|
|
|
|
var lightTheme = getCurrentValue("rustdoc-preferred-light-theme") || "light";
|
|
|
|
var darkTheme = getCurrentValue("rustdoc-preferred-dark-theme") || "dark";
|
|
|
|
|
|
|
|
if (mql.matches) {
|
|
|
|
// prefers a dark theme
|
|
|
|
switchTheme(currentTheme, mainTheme, darkTheme, true);
|
|
|
|
} else {
|
|
|
|
// prefers a light theme, or has no preference
|
|
|
|
switchTheme(currentTheme, mainTheme, lightTheme, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: we save the theme so that it doesn't suddenly change when
|
|
|
|
// the user disables "use-system-theme" and reloads the page or
|
|
|
|
// navigates to another page
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mql.addListener(handlePreferenceChange);
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
handlePreferenceChange(mql);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (getCurrentValue("rustdoc-use-system-theme") !== "false" && window.matchMedia) {
|
2020-10-15 18:03:25 +02:00
|
|
|
// update the preferred dark theme if the user is already using a dark theme
|
|
|
|
// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
|
|
|
|
if (getCurrentValue("rustdoc-use-system-theme") === null
|
|
|
|
&& getCurrentValue("rustdoc-preferred-dark-theme") === null
|
|
|
|
&& darkThemes.indexOf(localStoredTheme) >= 0) {
|
|
|
|
updateLocalStorage("rustdoc-preferred-dark-theme", localStoredTheme);
|
|
|
|
}
|
|
|
|
|
2020-10-11 02:53:37 +02:00
|
|
|
// call the function to initialize the theme at least once!
|
|
|
|
updateSystemTheme();
|
|
|
|
} else {
|
2020-10-11 17:52:47 +02:00
|
|
|
switchTheme(
|
|
|
|
currentTheme,
|
|
|
|
mainTheme,
|
|
|
|
getCurrentValue("rustdoc-theme") || "light",
|
|
|
|
false
|
|
|
|
);
|
2020-10-11 02:53:37 +02:00
|
|
|
}
|