Skip to content

Commit d034cca

Browse files
authored
Rollup merge of #61236 - GuillaumeGomez:system-theme, r=Mark-Simulacrum
take into account the system theme Fixes #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
2 parents 7b0085a + 1bd9424 commit d034cca

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/librustdoc/html/render.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ themePicker.onblur = handleThemeButtonsBlur;
914914
var but = document.createElement('button');
915915
but.innerHTML = item;
916916
but.onclick = function(el) {{
917-
switchTheme(currentTheme, mainTheme, item);
917+
switchTheme(currentTheme, mainTheme, item, true);
918918
}};
919919
but.onblur = handleThemeButtonsBlur;
920920
themes.appendChild(but);

src/librustdoc/html/static/rustdoc.css

+15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@
5454
box-sizing: border-box;
5555
}
5656

57+
/* This part handles the "default" theme being used depending on the system one. */
58+
html {
59+
content: "";
60+
}
61+
@media (prefers-color-scheme: light) {
62+
html {
63+
content: "light";
64+
}
65+
}
66+
@media (prefers-color-scheme: dark) {
67+
html {
68+
content: "dark";
69+
}
70+
}
71+
5772
/* General structure and fonts */
5873

5974
body {

src/librustdoc/html/static/storage.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function getCurrentValue(name) {
8686
return null;
8787
}
8888

89-
function switchTheme(styleElem, mainStyleElem, newTheme) {
89+
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
9090
var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
9191
var fullNewTheme = newTheme + resourcesSuffix + ".css";
9292
var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
@@ -109,8 +109,18 @@ function switchTheme(styleElem, mainStyleElem, newTheme) {
109109
});
110110
if (found === true) {
111111
styleElem.href = newHref;
112-
updateLocalStorage("rustdoc-theme", newTheme);
112+
// If this new value comes from a system setting or from the previously saved theme, no
113+
// need to save it.
114+
if (saveTheme === true) {
115+
updateLocalStorage("rustdoc-theme", newTheme);
116+
}
113117
}
114118
}
115119

116-
switchTheme(currentTheme, mainTheme, getCurrentValue("rustdoc-theme") || "light");
120+
function getSystemValue() {
121+
return getComputedStyle(document.documentElement).getPropertyValue('content');
122+
}
123+
124+
switchTheme(currentTheme, mainTheme,
125+
getCurrentValue("rustdoc-theme") || getSystemValue() || "light",
126+
false);

0 commit comments

Comments
 (0)