Skip to content

Commit bf59152

Browse files
authored
Rollup merge of #72968 - integer32llc:docs-arrow-keys, r=GuillaumeGomez
Only highlight doc search results via mouseover if mouse has moved ## What happens - Go to https://doc.rust-lang.org/stable/std/index.html - Put your mouse cursor somewhere in the middle where search results will appear and then don't move the mouse - Press 's' to focus the search box - Type a query that brings up enough search results to go under where your mouse cursor is - Press the down arrow - The search result that is one below where your mouse cursor is will be highlighted. ## What I expected When not currently using the mouse, I expect doing a search and then pressing the down arrow to always highlight the first search result immediately below the search box. ## The fix This feels a bit hacky to me; I'm open to other solutions. This introduces a global JS var that keeps track of whether the person searching has moved their mouse after doing a search or not, and only uses the mouse position to highlight search results if the person HAS moved the mouse AFTER doing a search.
2 parents e154978 + 1bc4e45 commit bf59152

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/librustdoc/html/static/main.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ function defocusSearchBar() {
100100
// 2 for "In Return Types"
101101
var currentTab = 0;
102102

103+
var mouseMovedAfterSearch = true;
104+
103105
var titleBeforeSearch = document.title;
104106

105107
function clearInputTimeout() {
@@ -162,6 +164,7 @@ function defocusSearchBar() {
162164
}
163165
addClass(main, "hidden");
164166
removeClass(search, "hidden");
167+
mouseMovedAfterSearch = false;
165168
}
166169

167170
function hideSearchResults(search) {
@@ -424,6 +427,12 @@ function defocusSearchBar() {
424427
document.addEventListener("keypress", handleShortcut);
425428
document.addEventListener("keydown", handleShortcut);
426429

430+
function resetMouseMoved(ev) {
431+
mouseMovedAfterSearch = true;
432+
}
433+
434+
document.addEventListener("mousemove", resetMouseMoved);
435+
427436
var handleSourceHighlight = (function() {
428437
var prev_line_id = 0;
429438

@@ -1353,20 +1362,22 @@ function defocusSearchBar() {
13531362
}
13541363
};
13551364
var mouseover_func = function(e) {
1356-
var el = e.target;
1357-
// to retrieve the real "owner" of the event.
1358-
while (el.tagName !== "TR") {
1359-
el = el.parentNode;
1360-
}
1361-
clearTimeout(hoverTimeout);
1362-
hoverTimeout = setTimeout(function() {
1363-
onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1364-
onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1365-
removeClass(i_e, "highlighted");
1365+
if (mouseMovedAfterSearch) {
1366+
var el = e.target;
1367+
// to retrieve the real "owner" of the event.
1368+
while (el.tagName !== "TR") {
1369+
el = el.parentNode;
1370+
}
1371+
clearTimeout(hoverTimeout);
1372+
hoverTimeout = setTimeout(function() {
1373+
onEachLazy(document.getElementsByClassName("search-results"), function(e) {
1374+
onEachLazy(e.getElementsByClassName("result"), function(i_e) {
1375+
removeClass(i_e, "highlighted");
1376+
});
13661377
});
1367-
});
1368-
addClass(el, "highlighted");
1369-
}, 20);
1378+
addClass(el, "highlighted");
1379+
}, 20);
1380+
}
13701381
};
13711382
onEachLazy(document.getElementsByClassName("search-results"), function(e) {
13721383
onEachLazy(e.getElementsByClassName("result"), function(i_e) {

0 commit comments

Comments
 (0)