Skip to content

Commit e87f8cc

Browse files
Source sidebar improvements
1 parent 93520d2 commit e87f8cc

File tree

5 files changed

+86
-60
lines changed

5 files changed

+86
-60
lines changed

src/librustdoc/html/layout.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
194194
.collect::<String>(),
195195
suffix=page.resource_suffix,
196196
extra_scripts=extra_scripts.iter().map(|e| {
197-
format!("<script src=\"{root_path}{extra_script}\"></script>",
197+
format!("<script src=\"{root_path}{extra_script}{suffix}.js\"></script>",
198198
root_path=page.root_path,
199-
extra_script=e)
199+
extra_script=e,
200+
suffix=page.resource_suffix)
200201
}).collect::<String>(),
201202
)
202203
}

src/librustdoc/html/render.rs

+46-38
Original file line numberDiff line numberDiff line change
@@ -859,9 +859,11 @@ themePicker.onblur = handleThemeButtonsBlur;
859859
write_minify(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
860860
static_files::SETTINGS_JS,
861861
options.enable_minification)?;
862-
write_minify(cx.dst.join(&format!("source-script{}.js", cx.shared.resource_suffix)),
863-
include_str!("static/source-script.js"),
864-
options.enable_minification)?;
862+
if cx.shared.include_sources {
863+
write_minify(cx.dst.join(&format!("source-script{}.js", cx.shared.resource_suffix)),
864+
static_files::sidebar::SOURCE_SCRIPT,
865+
options.enable_minification)?;
866+
}
865867

866868
{
867869
let mut data = format!("var resourcesSuffix = \"{}\";\n",
@@ -990,8 +992,8 @@ themePicker.onblur = handleThemeButtonsBlur;
990992
}
991993
}
992994

993-
fn to_string(&self) -> String {
994-
let mut subs: Vec<&Hierarchy> = self.children.iter().map(|(_, v)| v).collect();
995+
fn to_json_string(&self) -> String {
996+
let mut subs: Vec<&Hierarchy> = self.children.values().collect();
995997
subs.sort_unstable_by(|a, b| a.elem.cmp(&b.elem));
996998
let mut files = self.elems.iter()
997999
.map(|s| format!("\"{}\"",
@@ -1002,46 +1004,52 @@ themePicker.onblur = handleThemeButtonsBlur;
10021004
// FIXME(imperio): we could avoid to generate "dirs" and "files" if they're empty.
10031005
format!("{{\"name\":\"{name}\",\"dirs\":[{subs}],\"files\":[{files}]}}",
10041006
name=self.elem.to_str().expect("invalid osstring conversion"),
1005-
subs=subs.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(","),
1007+
subs=subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(","),
10061008
files=files.join(","))
10071009
}
10081010
}
10091011

1010-
use std::path::Component;
1012+
if cx.shared.include_sources {
1013+
use std::path::Component;
10111014

1012-
let mut hierarchy = Hierarchy::new(OsString::new());
1013-
for source in cx.shared.local_sources.iter()
1014-
.filter_map(|p| p.0.strip_prefix(&cx.shared.src_root)
1015-
.ok()) {
1016-
let mut h = &mut hierarchy;
1017-
let mut elems = source.components()
1018-
.filter_map(|s| {
1019-
match s {
1020-
Component::Normal(s) => Some(s.to_owned()),
1021-
_ => None,
1022-
}
1023-
})
1024-
.peekable();
1025-
loop {
1026-
let cur_elem = elems.next().expect("empty file path");
1027-
if elems.peek().is_none() {
1028-
h.elems.insert(cur_elem);
1029-
break;
1030-
} else {
1031-
let e = cur_elem.clone();
1032-
h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
1033-
h = h.children.get_mut(&cur_elem).expect("not found child");
1015+
let mut hierarchy = Hierarchy::new(OsString::new());
1016+
for source in cx.shared.local_sources.iter()
1017+
.filter_map(|p| p.0.strip_prefix(&cx.shared.src_root)
1018+
.ok()) {
1019+
let mut h = &mut hierarchy;
1020+
let mut elems = source.components()
1021+
.filter_map(|s| {
1022+
match s {
1023+
Component::Normal(s) => Some(s.to_owned()),
1024+
_ => None,
1025+
}
1026+
})
1027+
.peekable();
1028+
loop {
1029+
let cur_elem = elems.next().expect("empty file path");
1030+
if elems.peek().is_none() {
1031+
h.elems.insert(cur_elem);
1032+
break;
1033+
} else {
1034+
let e = cur_elem.clone();
1035+
h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
1036+
h = h.children.get_mut(&cur_elem).expect("not found child");
1037+
}
10341038
}
10351039
}
1036-
}
10371040

1038-
let dst = cx.dst.join("source-files.js");
1039-
let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst);
1040-
all_sources.push(format!("sourcesIndex['{}'] = {};", &krate.name, hierarchy.to_string()));
1041-
all_sources.sort();
1042-
let mut w = try_err!(File::create(&dst), &dst);
1043-
try_err!(writeln!(&mut w, "var N = null;var sourcesIndex = {{}};\n{}", all_sources.join("\n")),
1044-
&dst);
1041+
let dst = cx.dst.join("source-files.js");
1042+
let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst);
1043+
all_sources.push(format!("sourcesIndex['{}'] = {};",
1044+
&krate.name,
1045+
hierarchy.to_json_string()));
1046+
all_sources.sort();
1047+
let mut w = try_err!(File::create(&dst), &dst);
1048+
try_err!(writeln!(&mut w,
1049+
"var N = null;var sourcesIndex = {{}};\n{}",
1050+
all_sources.join("\n")),
1051+
&dst);
1052+
}
10451053

10461054
// Update the search index
10471055
let dst = cx.dst.join("search-index.js");
@@ -1367,7 +1375,7 @@ impl<'a> SourceCollector<'a> {
13671375
layout::render(&mut w, &self.scx.layout,
13681376
&page, &(""), &Source(contents),
13691377
self.scx.css_file_extension.is_some(),
1370-
&self.scx.themes, &["source-files.js", "source-script.js"])?;
1378+
&self.scx.themes, &["source-files", "source-script"])?;
13711379
w.flush()?;
13721380
self.scx.local_sources.insert(p.clone(), href);
13731381
Ok(())

src/librustdoc/html/static/rustdoc.css

+19-8
Original file line numberDiff line numberDiff line change
@@ -1464,25 +1464,28 @@ kbd {
14641464
#sidebar-toggle {
14651465
position: fixed;
14661466
top: 30px;
1467-
right: 300px;
1467+
left: 300px;
14681468
z-index: 10;
14691469
padding: 3px;
1470-
border-top-left-radius: 3px;
1471-
border-bottom-left-radius: 3px;
1470+
border-top-right-radius: 3px;
1471+
border-bottom-right-radius: 3px;
14721472
cursor: pointer;
14731473
font-weight: bold;
1474-
transition: right 1s;
1474+
transition: left .5s;
14751475
font-size: 1.2em;
1476+
border: 1px solid;
1477+
border-left: 0;
14761478
}
14771479
#source-sidebar {
14781480
position: fixed;
14791481
top: 0;
14801482
bottom: 0;
1481-
right: 0;
1483+
left: 0;
14821484
width: 300px;
14831485
z-index: 1;
14841486
overflow: auto;
1485-
transition: right 1s;
1487+
transition: left .5s;
1488+
border-right: 1px solid;
14861489
}
14871490
#source-sidebar > .title {
14881491
font-size: 1.5em;
@@ -1495,6 +1498,11 @@ div.children {
14951498
padding-left: 27px;
14961499
display: none;
14971500
}
1501+
div.name {
1502+
cursor: pointer;
1503+
position: relative;
1504+
margin-left: 16px;
1505+
}
14981506
div.files > a {
14991507
display: block;
15001508
padding: 0 3px;
@@ -1507,10 +1515,13 @@ div.name.expand + .children {
15071515
}
15081516
div.name::before {
15091517
content: "\25B6";
1510-
display: inline-block;
1511-
padding-right: 4px;
1518+
padding-left: 4px;
15121519
font-size: 0.7em;
1520+
position: absolute;
1521+
left: -16px;
1522+
top: 4px;
15131523
}
15141524
div.name.expand::before {
15151525
transform: rotate(90deg);
1526+
left: -14px;
15161527
}

src/librustdoc/html/static/source-script.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
7272
children.appendChild(files);
7373
parent.appendChild(name);
7474
parent.appendChild(children);
75-
return hasFoundFile === true && search.currentFile !== null;
75+
return hasFoundFile === true && currentFile.startsWith(fullPath);
7676
}
7777

7878
function toggleSidebar() {
7979
var sidebar = document.getElementById("source-sidebar");
8080
var child = this.children[0].children[0];
81-
if (child.innerText === "<") {
82-
sidebar.style.right = "";
83-
this.style.right = "";
84-
child.innerText = ">";
81+
if (child.innerText === ">") {
82+
sidebar.style.left = "";
83+
this.style.left = "";
84+
child.innerText = "<";
8585
updateLocalStorage("rustdoc-source-sidebar-hidden", "false");
8686
} else {
87-
sidebar.style.right = "-300px";
88-
this.style.right = "0";
89-
child.innerText = "<";
87+
sidebar.style.left = "-300px";
88+
this.style.left = "0";
89+
child.innerText = ">";
9090
updateLocalStorage("rustdoc-source-sidebar-hidden", "true");
9191
}
9292
}
@@ -102,10 +102,10 @@ function createSidebarToggle() {
102102
var inner2 = document.createElement("div");
103103
inner2.style.marginTop = "-2px";
104104
if (getCurrentValue("rustdoc-source-sidebar-hidden") === "true") {
105-
inner2.innerText = "<";
106-
sidebarToggle.style.right = "0";
107-
} else {
108105
inner2.innerText = ">";
106+
sidebarToggle.style.left = "0";
107+
} else {
108+
inner2.innerText = "<";
109109
}
110110

111111
inner1.appendChild(inner2);
@@ -125,7 +125,7 @@ function createSourceSidebar() {
125125
var sidebar = document.createElement("div");
126126
sidebar.id = "source-sidebar";
127127
if (getCurrentValue("rustdoc-source-sidebar-hidden") === "true") {
128-
sidebar.style.right = "-300px";
128+
sidebar.style.left = "-300px";
129129
}
130130

131131
var currentFile = getCurrentFilePath();

src/librustdoc/html/static_files.rs

+6
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,9 @@ pub mod source_code_pro {
109109
/// The file `SourceCodePro-LICENSE.txt`, the license text of the Source Code Pro font.
110110
pub static LICENSE: &'static [u8] = include_bytes!("static/SourceCodePro-LICENSE.txt");
111111
}
112+
113+
/// Files related to the sidebar in rustdoc sources.
114+
pub mod sidebar {
115+
/// File script to handle sidebar.
116+
pub static SOURCE_SCRIPT: &'static str = include_str!("static/source-script.js");
117+
}

0 commit comments

Comments
 (0)