From 704050da2334c465784954d81c8990c4bc7a92c5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 8 Nov 2020 14:49:29 +0100 Subject: [PATCH 1/3] Make keyboard interactions in the settings menu more pleasant --- src/librustdoc/html/static/main.js | 45 +++++++++++++------------- src/librustdoc/html/static/settings.js | 25 +++++++++++--- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 10342679cf474..4d75195fb81d4 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -40,6 +40,29 @@ if (!DOMTokenList.prototype.remove) { }; } + +// Gets the human-readable string for the virtual-key code of the +// given KeyboardEvent, ev. +// +// This function is meant as a polyfill for KeyboardEvent#key, +// since it is not supported in Trident. We also test for +// KeyboardEvent#keyCode because the handleShortcut handler is +// also registered for the keydown event, because Blink doesn't fire +// keypress on hitting the Escape key. +// +// So I guess you could say things are getting pretty interoperable. +function getVirtualKey(ev) { + if ("key" in ev && typeof ev.key != "undefined") { + return ev.key; + } + + var c = ev.charCode || ev.keyCode; + if (c == 27) { + return "Escape"; + } + return String.fromCharCode(c); +} + function getSearchInput() { return document.getElementsByClassName("search-input")[0]; } @@ -323,28 +346,6 @@ function defocusSearchBar() { } } - // Gets the human-readable string for the virtual-key code of the - // given KeyboardEvent, ev. - // - // This function is meant as a polyfill for KeyboardEvent#key, - // since it is not supported in Trident. We also test for - // KeyboardEvent#keyCode because the handleShortcut handler is - // also registered for the keydown event, because Blink doesn't fire - // keypress on hitting the Escape key. - // - // So I guess you could say things are getting pretty interoperable. - function getVirtualKey(ev) { - if ("key" in ev && typeof ev.key != "undefined") { - return ev.key; - } - - var c = ev.charCode || ev.keyCode; - if (c == 27) { - return "Escape"; - } - return String.fromCharCode(c); - } - function getHelpElement() { buildHelperPopup(); return document.getElementById("help"); diff --git a/src/librustdoc/html/static/settings.js b/src/librustdoc/html/static/settings.js index da3378ccf0dd0..daed6e731343e 100644 --- a/src/librustdoc/html/static/settings.js +++ b/src/librustdoc/html/static/settings.js @@ -1,5 +1,5 @@ // Local js definitions: -/* global getCurrentValue, updateLocalStorage, updateSystemTheme */ +/* global getCurrentValue, getVirtualKey, updateLocalStorage, updateSystemTheme */ (function () { function changeSetting(settingName, value) { @@ -14,10 +14,25 @@ } } + function handleKey(ev) { + // Don't interfere with browser shortcuts + if (ev.ctrlKey || ev.altKey || ev.metaKey) { + return; + } + switch (getVirtualKey(ev)) { + case "Enter": + case "Return": + case "Space": + ev.target.checked = !ev.target.checked; + ev.preventDefault(); + break; + } + } + function setEvents() { var elems = { - toggles: document.getElementsByClassName("slider"), - selects: document.getElementsByClassName("select-wrapper") + toggles: Array.prototype.slice.call(document.getElementsByClassName("slider")), + selects: Array.prototype.slice.call(document.getElementsByClassName("select-wrapper")), }; var i; @@ -32,6 +47,8 @@ toggle.onchange = function() { changeSetting(this.id, this.checked); }; + toggle.onkeyup = handleKey; + toggle.onkeyrelease = handleKey; } } @@ -50,5 +67,5 @@ } } - setEvents(); + window.addEventListener("DOMContentLoaded", setEvents); })(); From 589588ce7b526d974357f4d83fedcf5309e4a3e5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 8 Nov 2020 17:10:18 +0100 Subject: [PATCH 2/3] Improve settings.js code by using 'onEachLazy' directly instead of looping through DOM iterators --- src/librustdoc/html/static/settings.js | 55 +++++++++++--------------- 1 file changed, 22 insertions(+), 33 deletions(-) diff --git a/src/librustdoc/html/static/settings.js b/src/librustdoc/html/static/settings.js index daed6e731343e..bc14420232c4d 100644 --- a/src/librustdoc/html/static/settings.js +++ b/src/librustdoc/html/static/settings.js @@ -30,41 +30,30 @@ } function setEvents() { - var elems = { - toggles: Array.prototype.slice.call(document.getElementsByClassName("slider")), - selects: Array.prototype.slice.call(document.getElementsByClassName("select-wrapper")), - }; - var i; - - if (elems.toggles && elems.toggles.length > 0) { - for (i = 0; i < elems.toggles.length; ++i) { - var toggle = elems.toggles[i].previousElementSibling; - var settingId = toggle.id; - var settingValue = getSettingValue(settingId); - if (settingValue !== null) { - toggle.checked = settingValue === "true"; - } - toggle.onchange = function() { - changeSetting(this.id, this.checked); - }; - toggle.onkeyup = handleKey; - toggle.onkeyrelease = handleKey; + onEachLazy(document.getElementsByClassName("slider"), function(elem) { + var toggle = elem.previousElementSibling; + var settingId = toggle.id; + var settingValue = getSettingValue(settingId); + if (settingValue !== null) { + toggle.checked = settingValue === "true"; } - } - - if (elems.selects && elems.selects.length > 0) { - for (i = 0; i < elems.selects.length; ++i) { - var select = elems.selects[i].getElementsByTagName("select")[0]; - var settingId = select.id; - var settingValue = getSettingValue(settingId); - if (settingValue !== null) { - select.value = settingValue; - } - select.onchange = function() { - changeSetting(this.id, this.value); - }; + toggle.onchange = function() { + changeSetting(this.id, this.checked); + }; + toggle.onkeyup = handleKey; + toggle.onkeyrelease = handleKey; + }); + onEachLazy(document.getElementsByClassName("select-wrapper"), function(elem) { + var select = elem.getElementsByTagName("select")[0]; + var settingId = select.id; + var settingValue = getSettingValue(settingId); + if (settingValue !== null) { + select.value = settingValue; } - } + select.onchange = function() { + changeSetting(this.id, this.value); + }; + }); } window.addEventListener("DOMContentLoaded", setEvents); From 14ecee769318615b6497957d225c8869d07f3e97 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 8 Nov 2020 17:12:03 +0100 Subject: [PATCH 3/3] Improve comment for getVirtualKey function --- src/librustdoc/html/static/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 4d75195fb81d4..f1ba28c69d0e2 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -45,7 +45,7 @@ if (!DOMTokenList.prototype.remove) { // given KeyboardEvent, ev. // // This function is meant as a polyfill for KeyboardEvent#key, -// since it is not supported in Trident. We also test for +// since it is not supported in IE 11 or Chrome for Android. We also test for // KeyboardEvent#keyCode because the handleShortcut handler is // also registered for the keydown event, because Blink doesn't fire // keypress on hitting the Escape key.