Skip to content

Commit 04f0402

Browse files
committed
Add support for "always theme" in setting
1 parent c4b994f commit 04f0402

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

src/librustdoc/html/render/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<S
421421
"Theme preferences",
422422
vec![
423423
Setting::from(("use-system-theme", "Use system theme", true)),
424+
Setting::Select {
425+
js_data_name: "theme",
426+
description: "Theme",
427+
default_value: "light",
428+
options: theme_names.clone(),
429+
},
424430
Setting::Select {
425431
js_data_name: "preferred-dark-theme",
426432
description: "Preferred dark theme",

src/librustdoc/html/static/js/settings.js

+28
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// Local js definitions:
22
/* global getSettingValue, getVirtualKey, onEachLazy, updateLocalStorage, updateSystemTheme */
3+
/* global addClass, removeClass */
34

45
(function () {
56
function changeSetting(settingName, value) {
67
updateLocalStorage("rustdoc-" + settingName, value);
78

89
switch (settingName) {
10+
case "theme":
911
case "preferred-dark-theme":
1012
case "preferred-light-theme":
1113
case "use-system-theme":
1214
updateSystemTheme();
15+
updateLightAndDark();
1316
break;
1417
}
1518
}
@@ -29,7 +32,32 @@
2932
}
3033
}
3134

35+
function showLightAndDark() {
36+
addClass(document.getElementById("theme").parentElement.parentElement, "hidden");
37+
removeClass(document.getElementById("preferred-light-theme").parentElement.parentElement,
38+
"hidden");
39+
removeClass(document.getElementById("preferred-dark-theme").parentElement.parentElement,
40+
"hidden");
41+
}
42+
43+
function hideLightAndDark() {
44+
addClass(document.getElementById("preferred-light-theme").parentElement.parentElement,
45+
"hidden");
46+
addClass(document.getElementById("preferred-dark-theme").parentElement.parentElement,
47+
"hidden");
48+
removeClass(document.getElementById("theme").parentElement.parentElement, "hidden");
49+
}
50+
51+
function updateLightAndDark() {
52+
if (getSettingValue("use-system-theme") !== "false") {
53+
showLightAndDark();
54+
} else {
55+
hideLightAndDark();
56+
}
57+
}
58+
3259
function setEvents() {
60+
updateLightAndDark();
3361
onEachLazy(document.getElementsByClassName("slider"), function(elem) {
3462
var toggle = elem.previousElementSibling;
3563
var settingId = toggle.id;

src/librustdoc/html/static/js/storage.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,25 @@ var updateSystemTheme = (function() {
187187
var mql = window.matchMedia("(prefers-color-scheme: dark)");
188188

189189
function handlePreferenceChange(mql) {
190+
let use = function(theme) {
191+
switchTheme(window.currentTheme, window.mainTheme, theme, true);
192+
};
190193
// maybe the user has disabled the setting in the meantime!
191194
if (getSettingValue("use-system-theme") !== "false") {
192195
var lightTheme = getSettingValue("preferred-light-theme") || "light";
193196
var darkTheme = getSettingValue("preferred-dark-theme") || "dark";
194197

195198
if (mql.matches) {
196-
// prefers a dark theme
197-
switchTheme(window.currentTheme, window.mainTheme, darkTheme, true);
199+
use(darkTheme);
198200
} else {
199201
// prefers a light theme, or has no preference
200-
switchTheme(window.currentTheme, window.mainTheme, lightTheme, true);
202+
use(lightTheme);
201203
}
202-
203204
// note: we save the theme so that it doesn't suddenly change when
204205
// the user disables "use-system-theme" and reloads the page or
205206
// navigates to another page
207+
} else {
208+
use(getSettingValue("theme"));
206209
}
207210
}
208211

src/test/rustdoc-gui/toggle-docs-mobile.goml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
goto: file://|DOC_PATH|/test_docs/struct.Foo.html
22
size: (433, 600)
33
assert-attribute: (".top-doc", {"open": ""})
4-
click: (4, 240) // This is the position of the top doc comment toggle
4+
click: (4, 260) // This is the position of the top doc comment toggle
55
assert-attribute-false: (".top-doc", {"open": ""})
6-
click: (4, 240)
6+
click: (4, 260)
77
assert-attribute: (".top-doc", {"open": ""})
88
// To ensure that the toggle isn't over the text, we check that the toggle isn't clicked.
9-
click: (3, 240)
9+
click: (3, 260)
1010
assert-attribute: (".top-doc", {"open": ""})
1111

1212
// Assert the position of the toggle on the top doc block.
@@ -22,10 +22,10 @@ assert-position: (
2222
// Now we do the same but with a little bigger width
2323
size: (600, 600)
2424
assert-attribute: (".top-doc", {"open": ""})
25-
click: (4, 240) // New Y position since all search elements are back on one line.
25+
click: (4, 260) // New Y position since all search elements are back on one line.
2626
assert-attribute-false: (".top-doc", {"open": ""})
27-
click: (4, 240)
27+
click: (4, 260)
2828
assert-attribute: (".top-doc", {"open": ""})
2929
// To ensure that the toggle isn't over the text, we check that the toggle isn't clicked.
30-
click: (3, 240)
30+
click: (3, 260)
3131
assert-attribute: (".top-doc", {"open": ""})

0 commit comments

Comments
 (0)