Skip to content

Commit e9edbf1

Browse files
Put the notable popup in the current page DOM
1 parent 09102d5 commit e9edbf1

File tree

9 files changed

+65
-92
lines changed

9 files changed

+65
-92
lines changed

src/librustdoc/html/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1460,6 +1460,7 @@ fn init_id_map() -> FxHashMap<String, usize> {
14601460
map.insert("sidebar-vars".to_owned(), 1);
14611461
map.insert("copy-path".to_owned(), 1);
14621462
map.insert("TOC".to_owned(), 1);
1463+
map.insert("notable-traits".to_owned(), 1);
14631464
// This is the list of IDs used by rustdoc sections (but still generated by
14641465
// rustdoc).
14651466
map.insert("fields".to_owned(), 1);

src/librustdoc/html/render/context.rs

-30
Original file line numberDiff line numberDiff line change
@@ -372,34 +372,6 @@ impl<'tcx> Context<'tcx> {
372372
anchor = anchor
373373
))
374374
}
375-
376-
fn generate_notable_trait_index(&self, crate_name: &str) -> Result<(), Error> {
377-
let notable_traits = self.shared.notable_traits.borrow();
378-
if !notable_traits.is_empty() {
379-
// This is crate specific.
380-
let notable_traits_file = self.dst.join(crate_name).join("notable-traits.js");
381-
let out = "window.NOTABLE_TRAITS = [".to_owned();
382-
383-
// We need to put them back into a vec to sort them by their index.
384-
let mut notables = notable_traits.iter().collect::<Vec<_>>();
385-
notables.sort_by(|(_, pos1), (_, pos2)| pos1.cmp(pos2));
386-
387-
let mut out = notables.into_iter().fold(out, |mut acc, ((for_, content), pos)| {
388-
if *pos > 0 {
389-
acc.push(',');
390-
}
391-
acc.push_str(&format!(
392-
"[\"{}\",\"{}\"]",
393-
for_.replace("\"", "\\\""),
394-
content.replace("\\", "\\\\").replace("\"", "\\\""),
395-
));
396-
acc
397-
});
398-
out.push_str("];");
399-
self.shared.fs.write(notable_traits_file, out)?;
400-
}
401-
Ok(())
402-
}
403375
}
404376

405377
/// Generates the documentation for `crate` into the directory `dst`
@@ -586,8 +558,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
586558
let final_file = self.dst.join(crate_name).join("all.html");
587559
let settings_file = self.dst.join("settings.html");
588560

589-
self.generate_notable_trait_index(crate_name)?;
590-
591561
let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
592562
if !root_path.ends_with('/') {
593563
root_path.push('/');

src/librustdoc/html/render/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1265,8 +1265,8 @@ fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
12651265
&mut out_for,
12661266
"{}",
12671267
impl_.for_.print(cx),
1268-
// "<div class=\"notable\">Notable traits for {}</div>\
1269-
// <code class=\"content\">",
1268+
// "<div class=\"notable\">Notable traits for {}</div>\
1269+
// <code class=\"content\">",
12701270
);
12711271
}
12721272

src/librustdoc/html/render/print_item.rs

+17
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ pub(super) fn print_item(
171171
unreachable!();
172172
}
173173
}
174+
// We now write down all the notable traits DOM in one place that will be used by JS after.
175+
let mut notable_traits = cx.shared.notable_traits.borrow_mut();
176+
if !notable_traits.is_empty() {
177+
let mut notables = notable_traits.drain().collect::<Vec<_>>();
178+
notables.sort_by(|(_, pos1), (_, pos2)| pos1.cmp(pos2));
179+
buf.write_str("<div id=\"notable-traits\" class=\"code-header\">");
180+
for notable in notables {
181+
buf.write_str(&format!(
182+
"<div class=\"notable-traits-tooltiptext\">\
183+
<div class=\"notable\">Notable traits for {}</div>\
184+
<code class=\"content\">{}</code>\
185+
</div>",
186+
notable.0.0, notable.0.1
187+
));
188+
}
189+
buf.write_str("</div>");
190+
}
174191
}
175192

176193
/// For large structs, enums, unions, etc, determine whether to hide their fields

src/librustdoc/html/static/css/rustdoc.css

+8-4
Original file line numberDiff line numberDiff line change
@@ -1268,14 +1268,13 @@ h3.variant {
12681268
cursor: pointer;
12691269
}
12701270

1271-
#notable-traits-tooltiptext {
1271+
.notable-traits-tooltiptext {
12721272
padding: 5px 3px 3px 3px;
12731273
border-radius: 6px;
12741274
margin-left: 5px;
12751275
z-index: 10;
12761276
font-size: 16px;
12771277
cursor: default;
1278-
position: absolute;
12791278
border: 1px solid;
12801279
}
12811280

@@ -1290,17 +1289,22 @@ h3.variant {
12901289
margin: 0;
12911290
}
12921291

1293-
#notable-traits-tooltiptext .notable {
1292+
.notable-traits-tooltiptext .notable {
12941293
margin: 0;
12951294
margin-bottom: 13px;
12961295
font-size: 19px;
12971296
font-weight: 600;
12981297
}
1298+
#notable-traits .notable-traits-tooltiptext {
1299+
display: none;
1300+
position: absolute;
1301+
}
12991302

1300-
#notable-traits-tooltiptext code.content{
1303+
.notable-traits-tooltiptext code.content{
13011304
margin: 0;
13021305
padding: 0;
13031306
font-size: 20px;
1307+
font-weight: 600;
13041308
}
13051309

13061310
/* Example code has the "Run" button that needs to be positioned relative to the pre */

src/librustdoc/html/static/css/themes/ayu.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,12 @@ pre.ignore:hover, .information:hover + pre.ignore {
415415
border-color: transparent #314559 transparent transparent;
416416
}
417417

418-
#notable-traits-tooltiptext {
418+
.notable-traits-tooltiptext {
419419
background-color: #314559;
420420
border-color: #5c6773;
421421
}
422422

423-
#notable-traits-tooltiptext .notable {
423+
.notable-traits-tooltiptext .notable {
424424
border-bottom-color: #5c6773;
425425
}
426426

src/librustdoc/html/static/css/themes/dark.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -363,12 +363,12 @@ pre.ignore:hover, .information:hover + pre.ignore {
363363
border-color: transparent black transparent transparent;
364364
}
365365

366-
#notable-traits-tooltiptext {
366+
.notable-traits-tooltiptext {
367367
background-color: #111;
368368
border-color: #777;
369369
}
370370

371-
#notable-traits-tooltiptext .notable {
371+
.notable-traits-tooltiptext .notable {
372372
border-bottom-color: #d2d2d2;
373373
}
374374

src/librustdoc/html/static/css/themes/light.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,12 @@ pre.ignore:hover, .information:hover + pre.ignore {
349349
border-color: transparent black transparent transparent;
350350
}
351351

352-
#notable-traits-tooltiptext {
352+
.notable-traits-tooltiptext {
353353
background-color: #eee;
354354
border-color: #999;
355355
}
356356

357-
#notable-traits-tooltiptext .notable {
357+
.notable-traits-tooltiptext .notable {
358358
border-bottom-color: #DDDDDD;
359359
}
360360

src/librustdoc/html/static/js/main.js

+31-50
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,6 @@ function hideThemeButtonState() {
171171
(function() {
172172
"use strict";
173173

174-
function loadScript(url, callback) {
175-
var script = document.createElement('script');
176-
script.src = url;
177-
script.onload = callback;
178-
document.head.append(script);
179-
}
180-
181174
window.searchState = {
182175
loadingText: "Loading search results...",
183176
input: document.getElementsByClassName("search-input")[0],
@@ -262,6 +255,12 @@ function hideThemeButtonState() {
262255
return;
263256
}
264257

258+
function loadScript(url) {
259+
var script = document.createElement('script');
260+
script.src = url;
261+
document.head.append(script);
262+
}
263+
265264
var searchLoaded = false;
266265
function loadSearch() {
267266
if (!searchLoaded) {
@@ -1009,51 +1008,33 @@ function hideThemeButtonState() {
10091008
//
10101009
// Handling the notable traits popup.
10111010
//
1012-
function getNotablePopup(callback) {
1013-
var elemId = "notable-traits-tooltiptext";
1014-
var elem = document.getElementById(elemId);
1015-
1016-
if (!elem) {
1017-
// If the element cannot be found, it means it hasn't been created yet and that the
1018-
// notable traits index wasn't loaded either.
1019-
var script = getVar("root-path") + window.currentCrate + "/notable-traits.js";
1020-
loadScript(script, function() {
1021-
elem = document.createElement("code");
1022-
elem.id = elemId;
1023-
elem.classList = "docblock";
1024-
main.appendChild(elem);
1025-
callback(elem);
1026-
});
1011+
function showNotableTraitPopup(elem) {
1012+
if (elem === window.currentNotableElem) {
1013+
window.currentNotablePopup.style.display = "";
1014+
window.currentNotableElem = null;
10271015
return;
1016+
} else if (window.currentNotablePopup) {
1017+
window.currentNotablePopup.style.display = "";
10281018
}
1029-
callback(elem);
1030-
}
1031-
function showNotableTraitPopup(elem) {
1032-
getNotablePopup(function(popup) {
1033-
if (elem === window.currentNotableElem) {
1034-
popup.style.display = "none";
1035-
window.currentNotableElem = null;
1036-
return;
1037-
}
1038-
var elemRect = elem.getBoundingClientRect();
1039-
var containerRect = main.getBoundingClientRect();
1040-
1041-
var index = elem.getAttribute("data-index");
1042-
var notableTrait = window.NOTABLE_TRAITS[parseInt(index)];
1043-
1044-
popup.innerHTML = "<div class=\"notable\">Notable traits for " + notableTrait[0] + "</div><code class=\"content\">" + notableTrait[1] + "</code>";
1045-
popup.style.top = (elemRect.top - containerRect.top) + "px";
1046-
// In here, if the "i" is too much on the right, better put the popup on its left.
1047-
if (elem.offsetLeft > main.offsetWidth / 2) {
1048-
popup.style.left = "";
1049-
popup.style.right = (main.offsetWidth - elem.offsetLeft + 2) + "px";
1050-
} else {
1051-
popup.style.right = "";
1052-
popup.style.left = (elem.offsetLeft + 12) + "px";
1053-
}
1054-
popup.style.display = "";
1055-
window.currentNotableElem = elem;
1056-
});
1019+
var elemRect = elem.getBoundingClientRect();
1020+
var containerRect = main.getBoundingClientRect();
1021+
1022+
var index = elem.getAttribute("data-index");
1023+
var notableTraitContainer = document.getElementById("notable-traits");
1024+
var notable = notableTraitContainer.children[parseInt(index)];
1025+
1026+
notable.style.top = (elemRect.top - containerRect.top) + "px";
1027+
// In here, if the "i" is too much on the right, better put the popup on its left.
1028+
if (elem.offsetLeft > main.offsetWidth / 2) {
1029+
notable.style.left = "";
1030+
notable.style.right = (main.offsetWidth - elem.offsetLeft + 2) + "px";
1031+
} else {
1032+
notable.style.right = "";
1033+
notable.style.left = (elem.offsetLeft + 12) + "px";
1034+
}
1035+
notable.style.display = "block";
1036+
window.currentNotableElem = elem;
1037+
window.currentNotablePopup = notable;
10571038
}
10581039

10591040
onEachLazy(document.getElementsByClassName("notable-traits-tooltip"), function(e) {

0 commit comments

Comments
 (0)