Skip to content

Commit 5f93159

Browse files
Fasten up theme loading
1 parent 3c52acd commit 5f93159

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

Diff for: src/librustdoc/html/layout.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ r##"<!DOCTYPE html>
4848
4949
<link rel="stylesheet" type="text/css" href="{root_path}normalize.css">
5050
<link rel="stylesheet" type="text/css" href="{root_path}rustdoc.css" id="mainThemeStyle">
51-
<link rel="stylesheet" type="text/css" href="{root_path}main.css" id="themeStyle">
51+
<link rel="stylesheet" type="text/css" href="" id="themeStyle">
52+
<script src="{root_path}storage.js"></script>
5253
{css_extension}
5354
5455
{favicon}
@@ -70,10 +71,10 @@ r##"<!DOCTYPE html>
7071
{sidebar}
7172
</nav>
7273
73-
<div id="theme-picker">
74+
<button id="theme-picker">
7475
<img src="{root_path}brush.svg" width="18" alt="Pick another theme!">
7576
<div id="theme-choices"></div>
76-
</div>
77+
</button>
7778
<script src="{root_path}theme.js"></script>
7879
<nav class="sub">
7980
<form class="search-form js-only">

Diff for: src/librustdoc/html/render.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,6 @@ themePicker.onclick = function() {{
917917
themePicker.style.borderBottomLeftRadius = "0";
918918
}}
919919
}};
920-
var currentTheme = document.getElementById("themeStyle");
921-
var mainTheme = document.getElementById("mainThemeStyle");
922920
[{}].forEach(function(item) {{
923921
var div = document.createElement('div');
924922
div.innerHTML = item;
@@ -927,32 +925,13 @@ var mainTheme = document.getElementById("mainThemeStyle");
927925
}};
928926
themes.appendChild(div);
929927
}});
930-
931-
function updateLocalStorage(theme) {{
932-
if (typeof(Storage) !== "undefined") {{
933-
localStorage.theme = theme;
934-
}} else {{
935-
// No Web Storage support so we do nothing
936-
}}
937-
}}
938-
function switchTheme(styleElem, mainStyleElem, newTheme) {{
939-
styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
940-
updateLocalStorage(newTheme);
941-
}}
942-
function getCurrentTheme() {{
943-
if (typeof(Storage) !== "undefined" && localStorage.theme !== undefined) {{
944-
return localStorage.theme;
945-
}}
946-
return "main";
947-
}}
948-
949-
switchTheme(currentTheme, mainTheme, getCurrentTheme());
950928
"#, themes.iter()
951929
.map(|s| format!("\"{}\"", s))
952930
.collect::<Vec<String>>()
953931
.join(",")).as_bytes())?;
954932

955933
write(cx.dst.join("main.js"), include_bytes!("static/main.js"))?;
934+
write(cx.dst.join("storage.js"), include_bytes!("static/storage.js"))?;
956935

957936
if let Some(ref css) = cx.shared.css_file_extension {
958937
let out = cx.dst.join("theme.css");

Diff for: src/librustdoc/html/static/storage.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*!
2+
* Copyright 2018 The Rust Project Developers. See the COPYRIGHT
3+
* file at the top-level directory of this distribution and at
4+
* http://rust-lang.org/COPYRIGHT.
5+
*
6+
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
* option. This file may not be copied, modified, or distributed
10+
* except according to those terms.
11+
*/
12+
13+
var currentTheme = document.getElementById("themeStyle");
14+
var mainTheme = document.getElementById("mainThemeStyle");
15+
16+
function updateLocalStorage(name, value) {
17+
if (typeof(Storage) !== "undefined") {
18+
localStorage[name] = value;
19+
} else {
20+
// No Web Storage support so we do nothing
21+
}
22+
}
23+
24+
function getCurrentValue(name) {
25+
if (typeof(Storage) !== "undefined" && localStorage[name] !== undefined) {
26+
return localStorage[name];
27+
}
28+
return null;
29+
}
30+
31+
function switchTheme(styleElem, mainStyleElem, newTheme) {
32+
styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
33+
updateLocalStorage('theme', newTheme);
34+
/*var elem = document.getElementsByTagName('body')[0];
35+
if (elem) {
36+
updateLocalStorage('background', getComputedStyle(elem)['background-color']);
37+
}*/
38+
}
39+
40+
/*var elem = document.getElementsByTagName('body')[0];
41+
if (elem) {
42+
var value =
43+
}*/
44+
switchTheme(currentTheme, mainTheme, getCurrentValue('theme') || 'main');

0 commit comments

Comments
 (0)