Skip to content

Commit

Permalink
Rollup merge of rust-lang#61236 - GuillaumeGomez:system-theme, r=Mark…
Browse files Browse the repository at this point in the history
…-Simulacrum

take into account the system theme

Fixes rust-lang#61079.

The CSS can now take into account the system theme. I used it to generate some content on the document and from there, if no theme has already been selected, it'll look at the system level theme.

r? @QuietMisdreavus
cc @fenhl
  • Loading branch information
Centril authored Aug 21, 2019
2 parents 7b0085a + 1bd9424 commit d034cca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ themePicker.onblur = handleThemeButtonsBlur;
var but = document.createElement('button');
but.innerHTML = item;
but.onclick = function(el) {{
switchTheme(currentTheme, mainTheme, item);
switchTheme(currentTheme, mainTheme, item, true);
}};
but.onblur = handleThemeButtonsBlur;
themes.appendChild(but);
Expand Down
15 changes: 15 additions & 0 deletions src/librustdoc/html/static/rustdoc.css
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function getCurrentValue(name) {
return null;
}

function switchTheme(styleElem, mainStyleElem, newTheme) {
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
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 (saveTheme === 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",
false);

0 comments on commit d034cca

Please sign in to comment.