Skip to content

Commit

Permalink
take into account the system theme
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 21, 2019
1 parent 7858dc2 commit c076d30
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/librustdoc/html/static/rustdoc.css
Expand Up @@ -54,6 +54,21 @@
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 */

body {
Expand Down
16 changes: 13 additions & 3 deletions src/librustdoc/html/static/storage.js
Expand Up @@ -86,7 +86,7 @@ function getCurrentValue(name) {
return null;
}

function switchTheme(styleElem, mainStyleElem, newTheme) {
function switchTheme(styleElem, mainStyleElem, newTheme, skipStorage) {
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
var fullNewTheme = newTheme + resourcesSuffix + ".css";
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
Expand All @@ -109,8 +109,18 @@ function switchTheme(styleElem, mainStyleElem, newTheme) {
});
if (found === true) {
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);

0 comments on commit c076d30

Please sign in to comment.