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

Only load one CSS theme by default #103971

Closed
Closed
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
17 changes: 16 additions & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,22 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
<script defer src=\"{root_path}settings{suffix}.js\"></script>",
root_path = page.static_root_path.unwrap_or(""),
suffix = page.resource_suffix,
)
);
// Pre-load all theme CSS files, so that switching feels seamless.
//
// When loading settings.html as a popover, the equivalent HTML is
// generated in main.js.
for file in &shared.style_files {
if let Ok(theme) = file.basename() {
write!(
buf,
"<link rel=\"preload\" href=\"{root_path}{theme}{suffix}.css\" \
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of generating all this HTML and duplicating the logic, why not creating a JS function to load them and instead call this function here like:

<script>window.preloadAllCss();</script>

? That will make the maintenance easier too to only have it in one place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you would call this function in the setTimeout call too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if I did that, the system wouldn't be able to start loading the extra themes until after the JavaScript was finished loading. I want the dependency tree to be shallow. That's also why I decided against implementing this stuff in settings.js.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. We'll need to be careful if we ever update this but I think it's fine otherwise. Thanks for the extra explanation!

as=\"style\">",
root_path = page.static_root_path.unwrap_or(""),
suffix = page.resource_suffix,
);
}
}
},
&shared.style_files,
);
Expand Down
19 changes: 19 additions & 0 deletions src/librustdoc/html/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ function loadCss(cssFileName) {
document.getElementsByTagName("head")[0].appendChild(link);
}

function preLoadCss(cssFileName) {
// https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types/preload
const link = document.createElement("link");
link.href = resourcePath(cssFileName, ".css");
link.rel = "preload";
link.as = "style";
document.getElementsByTagName("head")[0].appendChild(link);
}

(function() {
const isHelpPage = window.location.pathname.endsWith("/help.html");

Expand All @@ -210,6 +219,16 @@ function loadCss(cssFileName) {
// hopefully be loaded when the JS will generate the settings content.
loadCss("settings");
loadScript(resourcePath("settings", ".js"));
// Pre-load all theme CSS files, so that switching feels seamless.
//
// When loading settings.html as a standalone page, the equivalent HTML is
// generated in context.rs.
setTimeout(() => {
const themes = getVar("themes").split(",");
for (const theme of themes) {
preLoadCss(theme);
}
}, 0);
};

window.searchState = {
Expand Down
19 changes: 2 additions & 17 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ function getSettingValue(settingName) {

const localStoredTheme = getSettingValue("theme");

const savedHref = [];

// eslint-disable-next-line no-unused-vars
function hasClass(elem, className) {
return elem && elem.classList && elem.classList.contains(className);
Expand Down Expand Up @@ -103,6 +101,7 @@ function onEach(arr, func, reversed) {
* @param {function(?)} func - The callback
* @param {boolean} [reversed] - Whether to iterate in reverse
*/
// eslint-disable-next-line no-unused-vars
function onEachLazy(lazyArray, func, reversed) {
return onEach(
Array.prototype.slice.call(lazyArray),
Expand Down Expand Up @@ -140,21 +139,7 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
return;
}

let found = false;
if (savedHref.length === 0) {
onEachLazy(document.getElementsByTagName("link"), el => {
savedHref.push(el.href);
});
}
onEach(savedHref, el => {
if (el === newHref) {
found = true;
return true;
}
});
if (found) {
styleElem.href = newHref;
}
styleElem.href = newHref;
}

// This function is called from "main.js".
Expand Down
13 changes: 3 additions & 10 deletions src/librustdoc/html/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@
<link rel="stylesheet" {# -#}
href="{{static_root_path|safe}}rustdoc{{page.resource_suffix}}.css" {# -#}
id="mainThemeStyle"> {#- -#}
{%- for theme in themes -%}
<link rel="stylesheet" {# -#}
href="{{static_root_path|safe}}{{theme}}{{page.resource_suffix}}.css" {# -#}
{%- if theme == "light" -%}
id="themeStyle"
{%- else -%}
disabled
{%- endif -%}
>
{%- endfor -%}
<link rel="stylesheet" {# -#}
href="{{static_root_path|safe}}light{{page.resource_suffix}}.css" {# -#}
id="themeStyle"> {#- -#}
<script id="default-settings" {# -#}
{% for (k, v) in layout.default_settings %}
data-{{k}}="{{v}}"
Expand Down