Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove theme.js file #82732

Merged
merged 3 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ crate fn render<T: Print, S: Print>(
</button>\
<div id=\"theme-choices\" role=\"menu\"></div>\
</div>\
<script src=\"{static_root_path}theme{suffix}.js\"></script>\
<nav class=\"sub\">\
<form class=\"search-form\">\
<div class=\"search-container\">\
Expand Down
61 changes: 5 additions & 56 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,65 +108,14 @@ pub(super) fn write_shared(

let mut themes: Vec<&String> = themes.iter().collect();
themes.sort();
// To avoid theme switch latencies as much as possible, we put everything theme related
// at the beginning of the html files into another js file.
let theme_js = format!(
r#"var themes = document.getElementById("theme-choices");
var themePicker = document.getElementById("theme-picker");

function showThemeButtonState() {{
themes.style.display = "block";
themePicker.style.borderBottomRightRadius = "0";
themePicker.style.borderBottomLeftRadius = "0";
}}

function hideThemeButtonState() {{
themes.style.display = "none";
themePicker.style.borderBottomRightRadius = "3px";
themePicker.style.borderBottomLeftRadius = "3px";
}}

function switchThemeButtonState() {{
if (themes.style.display === "block") {{
hideThemeButtonState();
}} else {{
showThemeButtonState();
}}
}};

function handleThemeButtonsBlur(e) {{
var active = document.activeElement;
var related = e.relatedTarget;

if (active.id !== "theme-picker" &&
(!active.parentNode || active.parentNode.id !== "theme-choices") &&
(!related ||
(related.id !== "theme-picker" &&
(!related.parentNode || related.parentNode.id !== "theme-choices")))) {{
hideThemeButtonState();
}}
}}

themePicker.onclick = switchThemeButtonState;
themePicker.onblur = handleThemeButtonsBlur;
{}.forEach(function(item) {{
var but = document.createElement("button");
but.textContent = item;
but.onclick = function(el) {{
switchTheme(currentTheme, mainTheme, item, true);
useSystemTheme(false);
}};
but.onblur = handleThemeButtonsBlur;
themes.appendChild(but);
}});"#,
serde_json::to_string(&themes).unwrap()
);

write_minify(&cx.shared.fs, cx.path("theme.js"), &theme_js, options.enable_minification)?;

write_minify(
&cx.shared.fs,
cx.path("main.js"),
static_files::MAIN_JS,
&static_files::MAIN_JS.replace(
"/* INSERT THEMES HERE */",
&format!(" = {}", serde_json::to_string(&themes).unwrap()),
),
options.enable_minification,
)?;
write_minify(
Expand Down
78 changes: 70 additions & 8 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// ignore-tidy-filelength
// Local js definitions:
/* global addClass, getSettingValue, hasClass */
/* global onEach, onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
/* global hideThemeButtonState, showThemeButtonState */
/* global switchTheme, useSystemTheme */

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
Expand Down Expand Up @@ -84,12 +85,15 @@ function getSearchElement() {
return document.getElementById("search");
}

var THEME_PICKER_ELEMENT_ID = "theme-picker";
var THEMES_ELEMENT_ID = "theme-choices";

function getThemesElement() {
return document.getElementById("theme-choices");
return document.getElementById(THEMES_ELEMENT_ID);
}

function getThemePickerElement() {
return document.getElementById("theme-picker");
return document.getElementById(THEME_PICKER_ELEMENT_ID);
}

// Returns the current URL without any query parameter or hash.
Expand All @@ -107,6 +111,65 @@ function defocusSearchBar() {
getSearchInput().blur();
}

function showThemeButtonState() {
var themePicker = getThemePickerElement();
var themeChoices = getThemesElement();

themeChoices.style.display = "block";
themePicker.style.borderBottomRightRadius = "0";
themePicker.style.borderBottomLeftRadius = "0";
}

function hideThemeButtonState() {
var themePicker = getThemePickerElement();
var themeChoices = getThemesElement();

themeChoices.style.display = "none";
themePicker.style.borderBottomRightRadius = "3px";
themePicker.style.borderBottomLeftRadius = "3px";
}

// Set up the theme picker list.
(function () {
var themeChoices = getThemesElement();
var themePicker = getThemePickerElement();
var availableThemes/* INSERT THEMES HERE */;

function switchThemeButtonState() {
if (themeChoices.style.display === "block") {
hideThemeButtonState();
} else {
showThemeButtonState();
}
}

function handleThemeButtonsBlur(e) {
var active = document.activeElement;
var related = e.relatedTarget;

if (active.id !== THEME_PICKER_ELEMENT_ID &&
(!active.parentNode || active.parentNode.id !== THEMES_ELEMENT_ID) &&
(!related ||
(related.id !== THEME_PICKER_ELEMENT_ID &&
(!related.parentNode || related.parentNode.id !== THEMES_ELEMENT_ID)))) {
hideThemeButtonState();
}
}

themePicker.onclick = switchThemeButtonState;
themePicker.onblur = handleThemeButtonsBlur;
availableThemes.forEach(function(item) {
var but = document.createElement("button");
but.textContent = item;
but.onclick = function() {
switchTheme(window.currentTheme, window.mainTheme, item, true);
useSystemTheme(false);
};
but.onblur = handleThemeButtonsBlur;
themeChoices.appendChild(but);
});
}());

(function() {
"use strict";

Expand Down Expand Up @@ -453,8 +516,7 @@ function defocusSearchBar() {
break;

default:
var themePicker = getThemePickerElement();
if (themePicker.parentNode.contains(ev.target)) {
if (getThemePickerElement().parentNode.contains(ev.target)) {
handleThemeKeyDown(ev);
}
}
Expand All @@ -467,7 +529,7 @@ function defocusSearchBar() {
switch (getVirtualKey(ev)) {
case "ArrowUp":
ev.preventDefault();
if (active.previousElementSibling && ev.target.id !== "theme-picker") {
if (active.previousElementSibling && ev.target.id !== THEME_PICKER_ELEMENT_ID) {
active.previousElementSibling.focus();
} else {
showThemeButtonState();
Expand All @@ -476,7 +538,7 @@ function defocusSearchBar() {
break;
case "ArrowDown":
ev.preventDefault();
if (active.nextElementSibling && ev.target.id !== "theme-picker") {
if (active.nextElementSibling && ev.target.id !== THEME_PICKER_ELEMENT_ID) {
active.nextElementSibling.focus();
} else {
showThemeButtonState();
Expand All @@ -486,7 +548,7 @@ function defocusSearchBar() {
case "Enter":
case "Return":
case "Space":
if (ev.target.id === "theme-picker" && themes.style.display === "none") {
if (ev.target.id === THEME_PICKER_ELEMENT_ID && themes.style.display === "none") {
ev.preventDefault();
showThemeButtonState();
themes.firstElementChild.focus();
Expand Down
18 changes: 9 additions & 9 deletions src/librustdoc/html/static/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/* global resourcesSuffix */

var darkThemes = ["dark", "ayu"];
var currentTheme = document.getElementById("themeStyle");
var mainTheme = document.getElementById("mainThemeStyle");
window.currentTheme = document.getElementById("themeStyle");
window.mainTheme = document.getElementById("mainThemeStyle");

var settingsDataset = (function () {
var settingsElement = document.getElementById("default-settings");
Expand Down Expand Up @@ -137,7 +137,7 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
}
}

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

switchTheme(
currentTheme,
mainTheme,
window.currentTheme,
window.mainTheme,
JSON.parse(cssTheme) || "light",
true
);
Expand All @@ -180,10 +180,10 @@ var updateSystemTheme = (function() {

if (mql.matches) {
// prefers a dark theme
switchTheme(currentTheme, mainTheme, darkTheme, true);
switchTheme(window.currentTheme, window.mainTheme, darkTheme, true);
} else {
// prefers a light theme, or has no preference
switchTheme(currentTheme, mainTheme, lightTheme, true);
switchTheme(window.currentTheme, window.mainTheme, lightTheme, true);
}

// note: we save the theme so that it doesn't suddenly change when
Expand Down Expand Up @@ -212,8 +212,8 @@ if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
updateSystemTheme();
} else {
switchTheme(
currentTheme,
mainTheme,
window.currentTheme,
window.mainTheme,
getSettingValue("theme") || "light",
false
);
Expand Down