Skip to content

Commit 7aa4a20

Browse files
committed
rustdoc: merge doctest tooltip with notable traits tooltip
Fixes https://discord.com/channels/442252698964721669/443150878111694848/1066420140167680000 Fixes #91100
1 parent c62665e commit 7aa4a20

File tree

12 files changed

+186
-236
lines changed

12 files changed

+186
-236
lines changed

src/librustdoc/html/highlight.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,19 @@ fn write_header(out: &mut Buffer, class: &str, extra_content: Option<Buffer>, to
9696
);
9797

9898
if tooltip != Tooltip::None {
99+
let edition_code;
99100
write!(
100101
out,
101-
"<div class='tooltip'{}>ⓘ</div>",
102-
if let Tooltip::Edition(edition_info) = tooltip {
103-
format!(" data-edition=\"{}\"", edition_info)
104-
} else {
105-
String::new()
102+
"<a href=\"#\" class=\"tooltip\" title=\"{}\">ⓘ</a>",
103+
match tooltip {
104+
Tooltip::Ignore => "This example is not tested",
105+
Tooltip::CompileFail => "This example deliberately fails to compile",
106+
Tooltip::ShouldPanic => "This example panics",
107+
Tooltip::Edition(edition) => {
108+
edition_code = format!("This example runs with edition {edition}");
109+
&edition_code
110+
}
111+
Tooltip::None => unreachable!(),
106112
},
107113
);
108114
}

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ pub(crate) fn notable_traits_button(ty: &clean::Type, cx: &mut Context<'_>) -> O
13131313
if has_notable_trait {
13141314
cx.types_with_notable_traits.insert(ty.clone());
13151315
Some(format!(
1316-
" <a href=\"#\" class=\"notable-traits\" data-ty=\"{ty}\">ⓘ</a>",
1316+
" <a href=\"#\" class=\"tooltip\" data-notable-ty=\"{ty}\">ⓘ</a>",
13171317
ty = Escape(&format!("{:#}", ty.print(cx))),
13181318
))
13191319
} else {

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

+9-45
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ h2.small-section-header > .anchor {
697697
.main-heading a:hover,
698698
.example-wrap > pre.rust a:hover,
699699
.all-items a:hover,
700-
.docblock a:not(.test-arrow):not(.scrape-help):hover,
701-
.docblock-short a:not(.test-arrow):not(.scrape-help):hover,
700+
.docblock a:not(.test-arrow):not(.scrape-help):not(.tooltip):hover,
701+
.docblock-short a:not(.test-arrow):not(.scrape-help):not(.tooltip):hover,
702702
.item-info a {
703703
text-decoration: underline;
704704
}
@@ -1093,44 +1093,8 @@ pre.rust .doccomment {
10931093
display: block;
10941094
left: -25px;
10951095
top: 5px;
1096-
}
1097-
1098-
.example-wrap .tooltip:hover::after {
1099-
padding: 5px 3px 3px 3px;
1100-
border-radius: 6px;
1101-
margin-left: 5px;
1102-
font-size: 1rem;
1103-
border: 1px solid var(--border-color);
1104-
position: absolute;
1105-
width: max-content;
1106-
top: -2px;
1107-
z-index: 1;
1108-
background-color: var(--tooltip-background-color);
1109-
color: var(--tooltip-color);
1110-
}
1111-
1112-
.example-wrap .tooltip:hover::before {
1113-
content: " ";
1114-
position: absolute;
1115-
top: 50%;
1116-
left: 16px;
1117-
margin-top: -5px;
1118-
z-index: 1;
1119-
border: 5px solid transparent;
1120-
border-right-color: var(--tooltip-background-color);
1121-
}
1122-
1123-
.example-wrap.ignore .tooltip:hover::after {
1124-
content: "This example is not tested";
1125-
}
1126-
.example-wrap.compile_fail .tooltip:hover::after {
1127-
content: "This example deliberately fails to compile";
1128-
}
1129-
.example-wrap.should_panic .tooltip:hover::after {
1130-
content: "This example panics";
1131-
}
1132-
.example-wrap.edition .tooltip:hover::after {
1133-
content: "This code runs with edition " attr(data-edition);
1096+
margin: 0;
1097+
line-height: 1;
11341098
}
11351099

11361100
.example-wrap.compile_fail .tooltip,
@@ -1196,7 +1160,7 @@ a.test-arrow:hover {
11961160
border-right: 3px solid var(--target-border-color);
11971161
}
11981162

1199-
.notable-traits {
1163+
.code-header a.tooltip {
12001164
color: inherit;
12011165
margin-right: 15px;
12021166
position: relative;
@@ -1205,7 +1169,7 @@ a.test-arrow:hover {
12051169
/* placeholder thunk so that the mouse can easily travel from "(i)" to popover
12061170
the resulting "hover tunnel" is a stepped triangle, approximating
12071171
https://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown */
1208-
.notable-traits:hover::after {
1172+
a.tooltip:hover::after {
12091173
position: absolute;
12101174
top: calc(100% - 10px);
12111175
left: -15px;
@@ -1214,19 +1178,19 @@ a.test-arrow:hover {
12141178
content: "\00a0";
12151179
}
12161180

1217-
.notable .content {
1181+
.popover.tooltip .content {
12181182
margin: 0.25em 0.5em;
12191183
}
12201184

1221-
.notable .content pre, .notable .content code {
1185+
.popover.tooltip .content pre, .popover.tooltip .content code {
12221186
background: transparent;
12231187
margin: 0;
12241188
padding: 0;
12251189
font-size: 1.25rem;
12261190
white-space: pre-wrap;
12271191
}
12281192

1229-
.notable .content > h3:first-child {
1193+
.popover.tooltip .content > h3:first-child {
12301194
margin: 0 0 5px 0;
12311195
}
12321196

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

-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ Original by Dempfi (https://github.com/dempfi/ayu)
7070
--test-arrow-hover-background-color: rgba(57, 175, 215, 0.368);
7171
--target-background-color: rgba(255, 236, 164, 0.06);
7272
--target-border-color: rgba(255, 180, 76, 0.85);
73-
--tooltip-background-color: #314559;
74-
--tooltip-color: #c5c5c5;
7573
--kbd-color: #c5c5c5;
7674
--kbd-background: #314559;
7775
--kbd-box-shadow-color: #5c6773;

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

-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
--test-arrow-hover-background-color: #4e8bca;
6666
--target-background-color: #494a3d;
6767
--target-border-color: #bb7410;
68-
--tooltip-background-color: #000;
69-
--tooltip-color: #fff;
7068
--kbd-color: #000;
7169
--kbd-background: #fafbfc;
7270
--kbd-box-shadow-color: #c6cbd1;

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

-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
--test-arrow-hover-background-color: #4e8bca;
6666
--target-background-color: #fdffd3;
6767
--target-border-color: #ad7c37;
68-
--tooltip-background-color: #000;
69-
--tooltip-color: #fff;
7068
--kbd-color: #000;
7169
--kbd-background: #fafbfc;
7270
--kbd-box-shadow-color: #c6cbd1;

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

+60-52
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ function loadCss(cssUrl) {
379379
}
380380
ev.preventDefault();
381381
searchState.defocus();
382-
window.hideAllModals(true); // true = reset focus for notable traits
382+
window.hideAllModals(true); // true = reset focus for tooltips
383383
}
384384

385385
function handleShortcut(ev) {
@@ -789,17 +789,17 @@ function loadCss(cssUrl) {
789789
// we need to switch away from mobile mode and make the main content area scrollable.
790790
hideSidebar();
791791
}
792-
if (window.CURRENT_NOTABLE_ELEMENT) {
793-
// As a workaround to the behavior of `contains: layout` used in doc togglers, the
794-
// notable traits popup is positioned using javascript.
792+
if (window.CURRENT_TOOLTIP_ELEMENT) {
793+
// As a workaround to the behavior of `contains: layout` used in doc togglers,
794+
// tooltip popovers are positioned using javascript.
795795
//
796796
// This means when the window is resized, we need to redo the layout.
797-
const base = window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE;
798-
const force_visible = base.NOTABLE_FORCE_VISIBLE;
799-
hideNotable(false);
797+
const base = window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE;
798+
const force_visible = base.TOOLTIP_FORCE_VISIBLE;
799+
hideTooltip(false);
800800
if (force_visible) {
801-
showNotable(base);
802-
base.NOTABLE_FORCE_VISIBLE = true;
801+
showTooltip(base);
802+
base.TOOLTIP_FORCE_VISIBLE = true;
803803
}
804804
}
805805
});
@@ -827,27 +827,35 @@ function loadCss(cssUrl) {
827827
});
828828
});
829829

830-
function showNotable(e) {
831-
if (!window.NOTABLE_TRAITS) {
830+
function showTooltip(e) {
831+
const notable_ty = e.getAttribute("data-notable-ty");
832+
if (!window.NOTABLE_TRAITS && notable_ty) {
832833
const data = document.getElementById("notable-traits-data");
833834
if (data) {
834835
window.NOTABLE_TRAITS = JSON.parse(data.innerText);
835836
} else {
836-
throw new Error("showNotable() called on page without any notable traits!");
837+
throw new Error("showTooltip() called with notable without any notable traits!");
837838
}
838839
}
839-
if (window.CURRENT_NOTABLE_ELEMENT && window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE === e) {
840+
if (window.CURRENT_TOOLTIP_ELEMENT && window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE === e) {
840841
// Make this function idempotent.
841842
return;
842843
}
843844
window.hideAllModals(false);
844-
const ty = e.getAttribute("data-ty");
845845
const wrapper = document.createElement("div");
846-
wrapper.innerHTML = "<div class=\"content\">" + window.NOTABLE_TRAITS[ty] + "</div>";
847-
wrapper.className = "notable popover";
846+
if (notable_ty) {
847+
wrapper.innerHTML = "<div class=\"content\">" +
848+
window.NOTABLE_TRAITS[notable_ty] + "</div>";
849+
} else if (e.getAttribute("title") !== undefined) {
850+
const titleContent = document.createElement("div");
851+
titleContent.className = "content";
852+
titleContent.appendChild(document.createTextNode(e.getAttribute("title")));
853+
wrapper.appendChild(titleContent);
854+
}
855+
wrapper.className = "tooltip popover";
848856
const focusCatcher = document.createElement("div");
849857
focusCatcher.setAttribute("tabindex", "0");
850-
focusCatcher.onfocus = hideNotable;
858+
focusCatcher.onfocus = hideTooltip;
851859
wrapper.appendChild(focusCatcher);
852860
const pos = e.getBoundingClientRect();
853861
// 5px overlap so that the mouse can easily travel from place to place
@@ -869,62 +877,62 @@ function loadCss(cssUrl) {
869877
);
870878
}
871879
wrapper.style.visibility = "";
872-
window.CURRENT_NOTABLE_ELEMENT = wrapper;
873-
window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE = e;
880+
window.CURRENT_TOOLTIP_ELEMENT = wrapper;
881+
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE = e;
874882
wrapper.onpointerleave = function(ev) {
875883
// If this is a synthetic touch event, ignore it. A click event will be along shortly.
876884
if (ev.pointerType !== "mouse") {
877885
return;
878886
}
879-
if (!e.NOTABLE_FORCE_VISIBLE && !elemIsInParent(event.relatedTarget, e)) {
880-
hideNotable(true);
887+
if (!e.TOOLTIP_FORCE_VISIBLE && !elemIsInParent(event.relatedTarget, e)) {
888+
hideTooltip(true);
881889
}
882890
};
883891
}
884892

885-
function notableBlurHandler(event) {
886-
if (window.CURRENT_NOTABLE_ELEMENT &&
887-
!elemIsInParent(document.activeElement, window.CURRENT_NOTABLE_ELEMENT) &&
888-
!elemIsInParent(event.relatedTarget, window.CURRENT_NOTABLE_ELEMENT) &&
889-
!elemIsInParent(document.activeElement, window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE) &&
890-
!elemIsInParent(event.relatedTarget, window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE)
893+
function tooltipBlurHandler(event) {
894+
if (window.CURRENT_TOOLTIP_ELEMENT &&
895+
!elemIsInParent(document.activeElement, window.CURRENT_TOOLTIP_ELEMENT) &&
896+
!elemIsInParent(event.relatedTarget, window.CURRENT_TOOLTIP_ELEMENT) &&
897+
!elemIsInParent(document.activeElement, window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE) &&
898+
!elemIsInParent(event.relatedTarget, window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE)
891899
) {
892900
// Work around a difference in the focus behaviour between Firefox, Chrome, and Safari.
893-
// When I click the button on an already-opened notable trait popover, Safari
901+
// When I click the button on an already-opened tooltip popover, Safari
894902
// hides the popover and then immediately shows it again, while everyone else hides it
895903
// and it stays hidden.
896904
//
897905
// To work around this, make sure the click finishes being dispatched before
898-
// hiding the popover. Since `hideNotable()` is idempotent, this makes Safari behave
906+
// hiding the popover. Since `hideTooltip()` is idempotent, this makes Safari behave
899907
// consistently with the other two.
900-
setTimeout(() => hideNotable(false), 0);
908+
setTimeout(() => hideTooltip(false), 0);
901909
}
902910
}
903911

904-
function hideNotable(focus) {
905-
if (window.CURRENT_NOTABLE_ELEMENT) {
906-
if (window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.NOTABLE_FORCE_VISIBLE) {
912+
function hideTooltip(focus) {
913+
if (window.CURRENT_TOOLTIP_ELEMENT) {
914+
if (window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE) {
907915
if (focus) {
908-
window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.focus();
916+
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.focus();
909917
}
910-
window.CURRENT_NOTABLE_ELEMENT.NOTABLE_BASE.NOTABLE_FORCE_VISIBLE = false;
918+
window.CURRENT_TOOLTIP_ELEMENT.TOOLTIP_BASE.TOOLTIP_FORCE_VISIBLE = false;
911919
}
912920
const body = document.getElementsByTagName("body")[0];
913-
body.removeChild(window.CURRENT_NOTABLE_ELEMENT);
914-
window.CURRENT_NOTABLE_ELEMENT = null;
921+
body.removeChild(window.CURRENT_TOOLTIP_ELEMENT);
922+
window.CURRENT_TOOLTIP_ELEMENT = null;
915923
}
916924
}
917925

918-
onEachLazy(document.getElementsByClassName("notable-traits"), e => {
926+
onEachLazy(document.getElementsByClassName("tooltip"), e => {
919927
e.onclick = function() {
920-
this.NOTABLE_FORCE_VISIBLE = this.NOTABLE_FORCE_VISIBLE ? false : true;
921-
if (window.CURRENT_NOTABLE_ELEMENT && !this.NOTABLE_FORCE_VISIBLE) {
922-
hideNotable(true);
928+
this.TOOLTIP_FORCE_VISIBLE = this.TOOLTIP_FORCE_VISIBLE ? false : true;
929+
if (window.CURRENT_TOOLTIP_ELEMENT && !this.TOOLTIP_FORCE_VISIBLE) {
930+
hideTooltip(true);
923931
} else {
924-
showNotable(this);
925-
window.CURRENT_NOTABLE_ELEMENT.setAttribute("tabindex", "0");
926-
window.CURRENT_NOTABLE_ELEMENT.focus();
927-
window.CURRENT_NOTABLE_ELEMENT.onblur = notableBlurHandler;
932+
showTooltip(this);
933+
window.CURRENT_TOOLTIP_ELEMENT.setAttribute("tabindex", "0");
934+
window.CURRENT_TOOLTIP_ELEMENT.focus();
935+
window.CURRENT_TOOLTIP_ELEMENT.onblur = tooltipBlurHandler;
928936
}
929937
return false;
930938
};
@@ -933,16 +941,16 @@ function loadCss(cssUrl) {
933941
if (ev.pointerType !== "mouse") {
934942
return;
935943
}
936-
showNotable(this);
944+
showTooltip(this);
937945
};
938946
e.onpointerleave = function(ev) {
939947
// If this is a synthetic touch event, ignore it. A click event will be along shortly.
940948
if (ev.pointerType !== "mouse") {
941949
return;
942950
}
943-
if (!this.NOTABLE_FORCE_VISIBLE &&
944-
!elemIsInParent(ev.relatedTarget, window.CURRENT_NOTABLE_ELEMENT)) {
945-
hideNotable(true);
951+
if (!this.TOOLTIP_FORCE_VISIBLE &&
952+
!elemIsInParent(ev.relatedTarget, window.CURRENT_TOOLTIP_ELEMENT)) {
953+
hideTooltip(true);
946954
}
947955
};
948956
});
@@ -1044,14 +1052,14 @@ function loadCss(cssUrl) {
10441052
}
10451053

10461054
/**
1047-
* Hide popover menus, notable trait tooltips, and the sidebar (if applicable).
1055+
* Hide popover menus, clickable tooltips, and the sidebar (if applicable).
10481056
*
1049-
* Pass "true" to reset focus for notable traits.
1057+
* Pass "true" to reset focus for tooltip popovers.
10501058
*/
10511059
window.hideAllModals = function(switchFocus) {
10521060
hideSidebar();
10531061
window.hidePopoverMenus();
1054-
hideNotable(switchFocus);
1062+
hideTooltip(switchFocus);
10551063
};
10561064

10571065
/**

0 commit comments

Comments
 (0)