Skip to content

Commit 3ab4f9a

Browse files
Remove theme.js file creation and move its code inside main.js
1 parent 8ccc89b commit 3ab4f9a

File tree

4 files changed

+74
-67
lines changed

4 files changed

+74
-67
lines changed

src/librustdoc/html/layout.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ crate fn render<T: Print, S: Print>(
8888
</button>\
8989
<div id=\"theme-choices\" role=\"menu\"></div>\
9090
</div>\
91-
<script src=\"{static_root_path}theme{suffix}.js\"></script>\
9291
<nav class=\"sub\">\
9392
<form class=\"search-form\">\
9493
<div class=\"search-container\">\

src/librustdoc/html/render/write_shared.rs

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -108,65 +108,14 @@ pub(super) fn write_shared(
108108

109109
let mut themes: Vec<&String> = themes.iter().collect();
110110
themes.sort();
111-
// To avoid theme switch latencies as much as possible, we put everything theme related
112-
// at the beginning of the html files into another js file.
113-
let theme_js = format!(
114-
r#"var themes = document.getElementById("theme-choices");
115-
var themePicker = document.getElementById("theme-picker");
116-
117-
function showThemeButtonState() {{
118-
themes.style.display = "block";
119-
themePicker.style.borderBottomRightRadius = "0";
120-
themePicker.style.borderBottomLeftRadius = "0";
121-
}}
122-
123-
function hideThemeButtonState() {{
124-
themes.style.display = "none";
125-
themePicker.style.borderBottomRightRadius = "3px";
126-
themePicker.style.borderBottomLeftRadius = "3px";
127-
}}
128-
129-
function switchThemeButtonState() {{
130-
if (themes.style.display === "block") {{
131-
hideThemeButtonState();
132-
}} else {{
133-
showThemeButtonState();
134-
}}
135-
}};
136-
137-
function handleThemeButtonsBlur(e) {{
138-
var active = document.activeElement;
139-
var related = e.relatedTarget;
140-
141-
if (active.id !== "theme-picker" &&
142-
(!active.parentNode || active.parentNode.id !== "theme-choices") &&
143-
(!related ||
144-
(related.id !== "theme-picker" &&
145-
(!related.parentNode || related.parentNode.id !== "theme-choices")))) {{
146-
hideThemeButtonState();
147-
}}
148-
}}
149-
150-
themePicker.onclick = switchThemeButtonState;
151-
themePicker.onblur = handleThemeButtonsBlur;
152-
{}.forEach(function(item) {{
153-
var but = document.createElement("button");
154-
but.textContent = item;
155-
but.onclick = function(el) {{
156-
switchTheme(currentTheme, mainTheme, item, true);
157-
useSystemTheme(false);
158-
}};
159-
but.onblur = handleThemeButtonsBlur;
160-
themes.appendChild(but);
161-
}});"#,
162-
serde_json::to_string(&themes).unwrap()
163-
);
164-
165-
write_minify(&cx.shared.fs, cx.path("theme.js"), &theme_js, options.enable_minification)?;
111+
166112
write_minify(
167113
&cx.shared.fs,
168114
cx.path("main.js"),
169-
static_files::MAIN_JS,
115+
&static_files::MAIN_JS.replace(
116+
"/* INSERT THEMES HERE */",
117+
&format!(" = {}", serde_json::to_string(&themes).unwrap()),
118+
),
170119
options.enable_minification,
171120
)?;
172121
write_minify(

src/librustdoc/html/static/main.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Local js definitions:
22
/* global addClass, getSettingValue, hasClass */
33
/* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
4-
/* global hideThemeButtonState, showThemeButtonState */
4+
/* global switchTheme, useSystemTheme */
55

66
if (!String.prototype.startsWith) {
77
String.prototype.startsWith = function(searchString, position) {
@@ -107,6 +107,65 @@ function defocusSearchBar() {
107107
getSearchInput().blur();
108108
}
109109

110+
function showThemeButtonState() {
111+
var themePicker = getThemePickerElement();
112+
var themeChoices = getThemesElement();
113+
114+
themeChoices.style.display = "block";
115+
themePicker.style.borderBottomRightRadius = "0";
116+
themePicker.style.borderBottomLeftRadius = "0";
117+
}
118+
119+
function hideThemeButtonState() {
120+
var themePicker = getThemePickerElement();
121+
var themeChoices = getThemesElement();
122+
123+
themeChoices.style.display = "none";
124+
themePicker.style.borderBottomRightRadius = "3px";
125+
themePicker.style.borderBottomLeftRadius = "3px";
126+
}
127+
128+
// Set up the theme picker list.
129+
(function () {
130+
var themeChoices = getThemesElement();
131+
var themePicker = getThemePickerElement();
132+
var availableThemes/* INSERT THEMES HERE */;
133+
134+
function switchThemeButtonState() {
135+
if (themeChoices.style.display === "block") {
136+
hideThemeButtonState();
137+
} else {
138+
showThemeButtonState();
139+
}
140+
}
141+
142+
function handleThemeButtonsBlur(e) {
143+
var active = document.activeElement;
144+
var related = e.relatedTarget;
145+
146+
if (active.id !== "theme-picker" &&
147+
(!active.parentNode || active.parentNode.id !== "theme-choices") &&
148+
(!related ||
149+
(related.id !== "theme-picker" &&
150+
(!related.parentNode || related.parentNode.id !== "theme-choices")))) {
151+
hideThemeButtonState();
152+
}
153+
}
154+
155+
themePicker.onclick = switchThemeButtonState;
156+
themePicker.onblur = handleThemeButtonsBlur;
157+
availableThemes.forEach(function(item) {
158+
var but = document.createElement("button");
159+
but.textContent = item;
160+
but.onclick = function() {
161+
switchTheme(window.currentTheme, window.mainTheme, item, true);
162+
useSystemTheme(false);
163+
};
164+
but.onblur = handleThemeButtonsBlur;
165+
themeChoices.appendChild(but);
166+
});
167+
}());
168+
110169
(function() {
111170
"use strict";
112171

src/librustdoc/html/static/storage.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/* global resourcesSuffix */
33

44
var darkThemes = ["dark", "ayu"];
5-
var currentTheme = document.getElementById("themeStyle");
6-
var mainTheme = document.getElementById("mainThemeStyle");
5+
window.currentTheme = document.getElementById("themeStyle");
6+
window.mainTheme = document.getElementById("mainThemeStyle");
77

88
var settingsDataset = (function () {
99
var settingsElement = document.getElementById("default-settings");
@@ -137,7 +137,7 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
137137
}
138138
}
139139

140-
// This function is called from "theme.js", generated in `html/render/mod.rs`.
140+
// This function is called from "main.js".
141141
// eslint-disable-next-line no-unused-vars
142142
function useSystemTheme(value) {
143143
if (value === undefined) {
@@ -161,8 +161,8 @@ var updateSystemTheme = (function() {
161161
.getPropertyValue('content');
162162

163163
switchTheme(
164-
currentTheme,
165-
mainTheme,
164+
window.currentTheme,
165+
window.mainTheme,
166166
JSON.parse(cssTheme) || "light",
167167
true
168168
);
@@ -180,10 +180,10 @@ var updateSystemTheme = (function() {
180180

181181
if (mql.matches) {
182182
// prefers a dark theme
183-
switchTheme(currentTheme, mainTheme, darkTheme, true);
183+
switchTheme(window.currentTheme, window.mainTheme, darkTheme, true);
184184
} else {
185185
// prefers a light theme, or has no preference
186-
switchTheme(currentTheme, mainTheme, lightTheme, true);
186+
switchTheme(window.currentTheme, window.mainTheme, lightTheme, true);
187187
}
188188

189189
// note: we save the theme so that it doesn't suddenly change when
@@ -212,8 +212,8 @@ if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
212212
updateSystemTheme();
213213
} else {
214214
switchTheme(
215-
currentTheme,
216-
mainTheme,
215+
window.currentTheme,
216+
window.mainTheme,
217217
getSettingValue("theme") || "light",
218218
false
219219
);

0 commit comments

Comments
 (0)