Skip to content

Commit d501ead

Browse files
authored
Rollup merge of #92629 - jsha:theme-picker-local-only-2, r=GuillaumeGomez
Pick themes on settings page, not every page This hides the paintbrush icon on most pages by default, in preference for the settings on the settings page. When loading from a local file, and not in mobile view, continue to show the theme picker. That's because some browsers limit access to localStorage from file:/// URLs, so choosing a theme from settings.html doesn't take effect. Fixes #84539 Part of #59840 r? `@GuillaumeGomez` Demo: https://rustdoc.crud.net/jsha/theme-picker-local-only-2/std/io/trait.Read.html
2 parents 86b1581 + 04f0402 commit d501ead

File tree

7 files changed

+59
-22
lines changed

7 files changed

+59
-22
lines changed

src/librustdoc/html/render/mod.rs

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

src/librustdoc/html/static/css/rustdoc.css

+6-11
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,12 @@ details.rustdoc-toggle[open] > summary.hideme::after {
17661766
padding-top: 0px;
17671767
}
17681768

1769+
/* Space is at a premium on mobile, so remove the theme-picker icon. */
1770+
#theme-picker {
1771+
display: none;
1772+
width: 0;
1773+
}
1774+
17691775
.rustdoc {
17701776
flex-direction: column;
17711777
}
@@ -1884,12 +1890,6 @@ details.rustdoc-toggle[open] > summary.hideme::after {
18841890
height: 100%;
18851891
}
18861892

1887-
nav.sub {
1888-
width: calc(100% - 32px);
1889-
margin-left: 32px;
1890-
margin-bottom: 10px;
1891-
}
1892-
18931893
.source nav:not(.sidebar).sub {
18941894
margin-left: 32px;
18951895
}
@@ -2086,11 +2086,6 @@ details.rustdoc-toggle[open] > summary.hideme::after {
20862086
border: 0;
20872087
}
20882088

2089-
#crate-search + .search-input {
2090-
width: calc(100% + 71px);
2091-
margin-left: -36px;
2092-
}
2093-
20942089
#theme-picker, #settings-menu {
20952090
padding: 5px;
20962091
width: 31px;

src/librustdoc/html/static/js/main.js

+5
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,15 @@ function hideThemeButtonState() {
129129

130130
// Set up the theme picker list.
131131
(function () {
132+
if (!document.location.href.startsWith("file:///")) {
133+
return;
134+
}
132135
var themeChoices = getThemesElement();
133136
var themePicker = getThemePickerElement();
134137
var availableThemes = getVar("themes").split(",");
135138

139+
removeClass(themeChoices.parentElement, "hidden");
140+
136141
function switchThemeButtonState() {
137142
if (themeChoices.style.display === "block") {
138143
hideThemeButtonState();

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/librustdoc/templates/page.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
{%- endif -%}
9797
</a> {#- -#}
9898
<nav class="sub"> {#- -#}
99-
<div class="theme-picker"> {#- -#}
99+
<div class="theme-picker hidden"> {#- -#}
100100
<button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"> {#- -#}
101101
<img width="18" height="18" alt="Pick another theme!" {# -#}
102102
src="{{static_root_path|safe}}brush{{page.resource_suffix}}.svg"> {#- -#}

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)