diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index d08a7bde71c73..93e4af1d620f1 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -101,6 +101,8 @@ r##"
Move up in search results
Move down in search results
+
+
Switch tab
Go to active search result
+
diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index bccad6c89dc4f..015611db3ffdf 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -39,6 +39,13 @@ "associatedconstant", "union"]; + // On the search screen, so you remain on the last tab you opened. + // + // 0 for "Types/modules" + // 1 for "As parameters" + // 2 for "As return value" + var currentTab = 0; + function hasClass(elem, className) { if (elem && className && elem.className) { var elemClass = elem.className; @@ -694,41 +701,56 @@ }); var search_input = document.getElementsByClassName('search-input')[0]; - search_input.onkeydown = null; search_input.onkeydown = function(e) { - var actives = []; + // "actives" references the currently highlighted item in each search tab. + // Each array in "actives" represents a tab. + var actives = [[], [], []]; + // "current" is used to know which tab we're looking into. + var current = 0; onEach(document.getElementsByClassName('search-results'), function(e) { - onEach(document.getElementsByClassName('highlighted'), function(e) { - actives.push(e); + onEach(e.getElementsByClassName('highlighted'), function(e) { + actives[current].push(e); }); + current += 1; }); if (e.which === 38) { // up - if (!actives.length || !actives[0].previousElementSibling) { + if (!actives[currentTab].length || + !actives[currentTab][0].previousElementSibling) { return; } - addClass(actives[0].previousElementSibling, 'highlighted'); - removeClass(actives[0], 'highlighted'); + addClass(actives[currentTab][0].previousElementSibling, 'highlighted'); + removeClass(actives[currentTab][0], 'highlighted'); } else if (e.which === 40) { // down - if (!actives.length) { + if (!actives[currentTab].length) { var results = document.getElementsByClassName('search-results'); if (results.length > 0) { - var res = results[0].getElementsByClassName('result'); + var res = results[currentTab].getElementsByClassName('result'); if (res.length > 0) { addClass(res[0], 'highlighted'); } } - } else if (actives[0].nextElementSibling) { - addClass(actives[0].nextElementSibling, 'highlighted'); - removeClass(actives[0], 'highlighted'); + } else if (actives[currentTab][0].nextElementSibling) { + addClass(actives[currentTab][0].nextElementSibling, 'highlighted'); + removeClass(actives[currentTab][0], 'highlighted'); } } else if (e.which === 13) { // return - if (actives.length) { - document.location.href = actives[0].getElementsByTagName('a')[0].href; + if (actives[currentTab].length) { + document.location.href = + actives[currentTab][0].getElementsByTagName('a')[0].href; + } + } else if (e.which === 9) { // tab + if (e.shiftKey) { + printTab(currentTab > 0 ? currentTab - 1 : 2); + } else { + printTab(currentTab > 1 ? 0 : currentTab + 1); } - } else if (actives.length > 0) { - removeClass(actives[0], 'highlighted'); + e.preventDefault(); + } else if (e.which === 16) { // shift + // Does nothing, it's just to avoid losing "focus" on the highlighted element. + } else if (actives[currentTab].length > 0) { + removeClass(actives[currentTab][0], 'highlighted'); } }; } @@ -747,7 +769,7 @@ var output = ''; if (array.length > 0) { - output = ``; + output = '
'; var shown = []; array.forEach(function(item) { @@ -801,7 +823,7 @@ }); output += '
'; } else { - output = `
No results :(
` + + output = '
No results :(
' + 'Try on DuckDuckGo?
'; @@ -809,6 +831,13 @@ return output; } + function makeTabHeader(tabNb, text) { + if (currentTab === tabNb) { + return '
' + text + '
'; + } + return '
' + text + '
'; + } + function showResults(results) { var output, query = getQuery(); @@ -816,9 +845,10 @@ output = '

Results for ' + escape(query.query) + (query.type ? ' (type: ' + escape(query.type) + ')' : '') + '

' + '
' + - '
Types/modules
' + - '
As parameters
' + - '
As return value
'; + makeTabHeader(0, "Types/modules") + + makeTabHeader(1, "As parameters") + + makeTabHeader(2, "As return value") + + '
'; output += addTab(results['others'], query); output += addTab(results['in_args'], query, false); @@ -1394,6 +1424,9 @@ // In the search display, allows to switch between tabs. function printTab(nb) { + if (nb === 0 || nb === 1 || nb === 2) { + currentTab = nb; + } var nb_copy = nb; onEach(document.getElementById('titles').childNodes, function(elem) { if (nb_copy === 0) { diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 9e2798f1cf71a..467fab888ab11 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -552,7 +552,7 @@ body.blur > :not(#help) { flex: 0 0 auto; box-shadow: 0 0 6px rgba(0,0,0,.2); width: 550px; - height: 330px; + height: 354px; border: 1px solid; } #help dt {