Skip to content

Commit 43dca24

Browse files
authored
Rollup merge of #104366 - GuillaumeGomez:simplify-settings-theme-choice, r=notriddle
Simplify settings theme choice I removed the storage changes from #98765 and only kept the UI changes. You can test it [here](https://rustdoc.crud.net/imperio/simplify-settings-theme-choice/foo/index.html). Discussion about this still in progress [on zulip](https://rust-lang.zulipchat.com/#narrow/stream/266220-rustdoc/topic/Last.20part.20of.20settings.20simplification). r? ````@notriddle````
2 parents 1521795 + dacf9b8 commit 43dca24

File tree

3 files changed

+79
-69
lines changed

3 files changed

+79
-69
lines changed

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

+36-24
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
const isSettingsPage = window.location.pathname.endsWith("/settings.html");
1010

1111
function changeSetting(settingName, value) {
12+
if (settingName === "theme") {
13+
const useSystem = value === "system preference" ? "true" : "false";
14+
updateLocalStorage("use-system-theme", useSystem);
15+
}
1216
updateLocalStorage(settingName, value);
1317

1418
switch (settingName) {
1519
case "theme":
1620
case "preferred-dark-theme":
1721
case "preferred-light-theme":
18-
case "use-system-theme":
1922
updateSystemTheme();
2023
updateLightAndDark();
2124
break;
@@ -45,19 +48,18 @@
4548
}
4649

4750
function showLightAndDark() {
48-
addClass(document.getElementById("theme").parentElement, "hidden");
4951
removeClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
5052
removeClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
5153
}
5254

5355
function hideLightAndDark() {
5456
addClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
5557
addClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
56-
removeClass(document.getElementById("theme").parentElement, "hidden");
5758
}
5859

5960
function updateLightAndDark() {
60-
if (getSettingValue("use-system-theme") !== "false") {
61+
const useSystem = getSettingValue("use-system-theme");
62+
if (useSystem === "true" || (useSystem === null && getSettingValue("theme") === null)) {
6163
showLightAndDark();
6264
} else {
6365
hideLightAndDark();
@@ -91,7 +93,18 @@
9193
});
9294
onEachLazy(settingsElement.querySelectorAll("input[type=\"radio\"]"), elem => {
9395
const settingId = elem.name;
94-
const settingValue = getSettingValue(settingId);
96+
let settingValue = getSettingValue(settingId);
97+
if (settingId === "theme") {
98+
const useSystem = getSettingValue("use-system-theme");
99+
if (useSystem === "true" || settingValue === null) {
100+
if (useSystem !== "false") {
101+
settingValue = "system preference";
102+
} else {
103+
// This is the default theme.
104+
settingValue = "light";
105+
}
106+
}
107+
}
95108
if (settingValue !== null && settingValue !== "null") {
96109
elem.checked = settingValue === elem.value;
97110
}
@@ -120,26 +133,30 @@
120133

121134
if (setting["options"] !== undefined) {
122135
// This is a select setting.
123-
output += `<div class="radio-line" id="${js_data_name}">\
124-
<span class="setting-name">${setting_name}</span>\
125-
<div class="choices">`;
136+
output += `\
137+
<div class="radio-line" id="${js_data_name}">
138+
<span class="setting-name">${setting_name}</span>
139+
<div class="choices">`;
126140
onEach(setting["options"], option => {
127141
const checked = option === setting["default"] ? " checked" : "";
142+
const full = `${js_data_name}-${option.replace(/ /g,"-")}`;
128143

129-
output += `<label for="${js_data_name}-${option}" class="choice">\
130-
<input type="radio" name="${js_data_name}" \
131-
id="${js_data_name}-${option}" value="${option}"${checked}>\
132-
<span>${option}</span>\
133-
</label>`;
144+
output += `\
145+
<label for="${full}" class="choice">
146+
<input type="radio" name="${js_data_name}"
147+
id="${full}" value="${option}"${checked}>
148+
<span>${option}</span>
149+
</label>`;
134150
});
135151
output += "</div></div>";
136152
} else {
137153
// This is a toggle.
138154
const checked = setting["default"] === true ? " checked" : "";
139-
output += `<label class="toggle">\
140-
<input type="checkbox" id="${js_data_name}"${checked}>\
141-
<span class="label">${setting_name}</span>\
142-
</label>`;
155+
output += `\
156+
<label class="toggle">\
157+
<input type="checkbox" id="${js_data_name}"${checked}>\
158+
<span class="label">${setting_name}</span>\
159+
</label>`;
143160
}
144161
output += "</div>";
145162
}
@@ -156,16 +173,11 @@
156173
theme_names.push("light", "dark", "ayu");
157174

158175
const settings = [
159-
{
160-
"name": "Use system theme",
161-
"js_name": "use-system-theme",
162-
"default": true,
163-
},
164176
{
165177
"name": "Theme",
166178
"js_name": "theme",
167-
"default": "light",
168-
"options": theme_names,
179+
"default": "system preference",
180+
"options": theme_names.concat("system preference"),
169181
},
170182
{
171183
"name": "Preferred light theme",

src/test/rustdoc-gui/settings.goml

+2-39
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ click: "#settings-menu"
3737
wait-for: "#settings"
3838

3939
// We check that the "Use system theme" is disabled.
40-
assert-property: ("#use-system-theme", {"checked": "false"})
41-
assert: "//*[@class='setting-line']//span[text()='Use system theme']"
40+
assert-property: ("#theme-system-preference", {"checked": "false"})
4241
// Meaning that only the "theme" menu is showing up.
4342
assert: ".setting-line:not(.hidden) #theme"
4443
assert: ".setting-line.hidden #preferred-dark-theme"
@@ -115,13 +114,6 @@ assert-css: (
115114
"border-color": "rgb(221, 221, 221)",
116115
},
117116
)
118-
assert-css: (
119-
"#use-system-theme",
120-
{
121-
"background-color": "rgba(0, 0, 0, 0)",
122-
"border-color": "rgb(221, 221, 221)",
123-
}
124-
)
125117
// Let's start with the hover for toggles.
126118
move-cursor-to: "#auto-hide-large-items"
127119
assert-css: (
@@ -131,14 +123,6 @@ assert-css: (
131123
"border-color": "rgb(33, 150, 243)",
132124
},
133125
)
134-
move-cursor-to: "#use-system-theme"
135-
assert-css: (
136-
"#use-system-theme",
137-
{
138-
"background-color": "rgba(0, 0, 0, 0)",
139-
"border-color": "rgb(33, 150, 243)",
140-
}
141-
)
142126
move-cursor-to: "#settings-menu > a"
143127
// Let's now check with the focus for toggles.
144128
focus: "#auto-hide-large-items"
@@ -150,15 +134,6 @@ assert-css: (
150134
"box-shadow": "rgb(33, 150, 243) 0px 0px 1px 1px",
151135
},
152136
)
153-
focus: "#use-system-theme"
154-
assert-css: (
155-
"#use-system-theme",
156-
{
157-
"background-color": "rgba(0, 0, 0, 0)",
158-
"border-color": "rgb(221, 221, 221)",
159-
"box-shadow": "rgb(33, 150, 243) 0px 0px 1px 1px",
160-
},
161-
)
162137
// Now we check we both focus and hover for toggles.
163138
move-cursor-to: "#auto-hide-large-items"
164139
focus: "#auto-hide-large-items"
@@ -170,24 +145,12 @@ assert-css: (
170145
"box-shadow": "rgb(33, 150, 243) 0px 0px 1px 1px",
171146
},
172147
)
173-
move-cursor-to: "#use-system-theme"
174-
focus: "#use-system-theme"
175-
assert-css: (
176-
"#use-system-theme",
177-
{
178-
"background-color": "rgba(0, 0, 0, 0)",
179-
"border-color": "rgb(33, 150, 243)",
180-
"box-shadow": "rgb(33, 150, 243) 0px 0px 1px 1px",
181-
},
182-
)
183148

184149
// We now switch the display.
185-
click: "#use-system-theme"
150+
click: "#theme-system-preference"
186151
// Wait for the hidden element to show up.
187152
wait-for: ".setting-line:not(.hidden) #preferred-dark-theme"
188153
assert: ".setting-line:not(.hidden) #preferred-light-theme"
189-
// Check that the theme picking is hidden.
190-
assert: ".setting-line.hidden #theme"
191154

192155
// We check their text as well.
193156
assert-text: ("#preferred-dark-theme .setting-name", "Preferred dark theme")

src/test/rustdoc-gui/theme-change.goml

+41-6
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,66 @@
22
goto: "file://" + |DOC_PATH| + "/test_docs/index.html"
33
local-storage: {"rustdoc-use-system-theme": "false", "rustdoc-theme": "dark"}
44
reload:
5+
6+
store-value: (background_light, "rgb(255, 255, 255)")
7+
store-value: (background_dark, "rgb(53, 53, 53)")
8+
store-value: (background_ayu, "rgb(15, 20, 25)")
9+
510
click: "#settings-menu"
611
wait-for: "#theme-ayu"
712
click: "#theme-ayu"
813
// should be the ayu theme so let's check the color.
9-
wait-for-css: ("body", { "background-color": "rgb(15, 20, 25)" })
14+
wait-for-css: ("body", { "background-color": |background_ayu| })
1015
assert-local-storage: { "rustdoc-theme": "ayu" }
1116
click: "#theme-light"
1217
// should be the light theme so let's check the color.
13-
wait-for-css: ("body", { "background-color": "rgb(255, 255, 255)" })
18+
wait-for-css: ("body", { "background-color": |background_light| })
1419
assert-local-storage: { "rustdoc-theme": "light" }
1520
click: "#theme-dark"
1621
// Should be the dark theme so let's check the color.
17-
wait-for-css: ("body", { "background-color": "rgb(53, 53, 53)" })
22+
wait-for-css: ("body", { "background-color": |background_dark| })
1823
assert-local-storage: { "rustdoc-theme": "dark" }
1924

25+
local-storage: {
26+
"rustdoc-preferred-light-theme": "light",
27+
"rustdoc-preferred-dark-theme": "light",
28+
}
2029
goto: "file://" + |DOC_PATH| + "/settings.html"
30+
2131
wait-for: "#settings"
2232
click: "#theme-light"
23-
wait-for-css: ("body", { "background-color": "rgb(255, 255, 255)" })
33+
wait-for-css: ("body", { "background-color": |background_light| })
2434
assert-local-storage: { "rustdoc-theme": "light" }
2535

2636
click: "#theme-dark"
27-
wait-for-css: ("body", { "background-color": "rgb(53, 53, 53)" })
37+
wait-for-css: ("body", { "background-color": |background_dark| })
2838
assert-local-storage: { "rustdoc-theme": "dark" }
2939

3040
click: "#theme-ayu"
31-
wait-for-css: ("body", { "background-color": "rgb(15, 20, 25)" })
41+
wait-for-css: ("body", { "background-color": |background_ayu| })
3242
assert-local-storage: { "rustdoc-theme": "ayu" }
43+
44+
assert-local-storage-false: { "rustdoc-use-system-theme": "true" }
45+
click: "#theme-system-preference"
46+
wait-for: ".setting-line:not(.hidden) #preferred-light-theme"
47+
assert-local-storage: { "rustdoc-use-system-theme": "true" }
48+
// We click on both preferred light and dark themes to be sure that there is a change.
49+
click: "#preferred-light-theme-dark"
50+
click: "#preferred-dark-theme-dark"
51+
wait-for-css: ("body", { "background-color": |background_dark| })
52+
53+
reload:
54+
// Ensure that the "preferred themes" are still displayed.
55+
wait-for: ".setting-line:not(.hidden) #preferred-light-theme"
56+
click: "#theme-light"
57+
wait-for-css: ("body", { "background-color": |background_light| })
58+
assert-local-storage: { "rustdoc-theme": "light" }
59+
// Ensure it's now hidden again
60+
wait-for: ".setting-line.hidden #preferred-light-theme"
61+
// And ensure the theme was rightly set.
62+
wait-for-css: ("body", { "background-color": |background_light| })
63+
assert-local-storage: { "rustdoc-theme": "light" }
64+
65+
reload:
66+
wait-for: "#settings"
67+
assert: ".setting-line.hidden #preferred-light-theme"

0 commit comments

Comments
 (0)