1
Fork 0

take into account the system theme

This commit is contained in:
Guillaume Gomez 2019-05-27 15:57:44 +02:00
parent 7858dc237d
commit c076d30ce4
2 changed files with 28 additions and 3 deletions

View file

@ -54,6 +54,21 @@
box-sizing: border-box; box-sizing: border-box;
} }
/* This part handles the "default" theme being used depending on the system one. */
html {
content: "";
}
@media (prefers-color-scheme: light) {
html {
content: "light";
}
}
@media (prefers-color-scheme: dark) {
html {
content: "dark";
}
}
/* General structure and fonts */ /* General structure and fonts */
body { body {

View file

@ -86,7 +86,7 @@ function getCurrentValue(name) {
return null; return null;
} }
function switchTheme(styleElem, mainStyleElem, newTheme) { function switchTheme(styleElem, mainStyleElem, newTheme, skipStorage) {
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css"; var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
var fullNewTheme = newTheme + resourcesSuffix + ".css"; var fullNewTheme = newTheme + resourcesSuffix + ".css";
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme); var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
@ -109,8 +109,18 @@ function switchTheme(styleElem, mainStyleElem, newTheme) {
}); });
if (found === true) { if (found === true) {
styleElem.href = newHref; styleElem.href = newHref;
updateLocalStorage("rustdoc-theme", newTheme); // If this new value comes from a system setting or from the previously saved theme, no
// need to save it.
if (skipStorage !== true) {
updateLocalStorage("rustdoc-theme", newTheme);
}
} }
} }
switchTheme(currentTheme, mainTheme, getCurrentValue("rustdoc-theme") || "light"); function getSystemValue() {
return getComputedStyle(document.documentElement).getPropertyValue('content');
}
switchTheme(currentTheme, mainTheme,
getCurrentValue("rustdoc-theme") || getSystemValue() || "light",
true);