Skip to content

Commit 5e01ba3

Browse files
Improve settings menu display
1 parent 17180f4 commit 5e01ba3

File tree

6 files changed

+110
-31
lines changed

6 files changed

+110
-31
lines changed

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

+33
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,15 @@ pre.rust {
14041404
border-radius: 2px;
14051405
cursor: pointer;
14061406
}
1407+
#settings-menu {
1408+
padding: 0;
1409+
}
1410+
#settings-menu > a {
1411+
padding: 5px;
1412+
width: 100%;
1413+
height: 100%;
1414+
display: block;
1415+
}
14071416

14081417
@keyframes rotating {
14091418
from {
@@ -1416,6 +1425,30 @@ pre.rust {
14161425
#settings-menu.rotate img {
14171426
animation: rotating 2s linear infinite;
14181427
}
1428+
#settings-menu #settings {
1429+
position: absolute;
1430+
right: 0;
1431+
z-index: 1;
1432+
display: block;
1433+
margin-top: 7px;
1434+
border-radius: 3px;
1435+
border: 1px solid;
1436+
}
1437+
#settings-menu #settings .setting-line {
1438+
margin: 0.6em;
1439+
}
1440+
/* This rule is to draw the little arrow connecting the settings menu to the gear icon. */
1441+
#settings-menu #settings::before {
1442+
content: '';
1443+
position: absolute;
1444+
right: 11px;
1445+
border: solid;
1446+
border-width: 1px 1px 0 0;
1447+
display: inline-block;
1448+
padding: 4px;
1449+
transform: rotate(-45deg);
1450+
top: -5px;
1451+
}
14191452

14201453
#help-button {
14211454
font-family: "Fira Sans", Arial, sans-serif;

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Original by Dempfi (https://github.com/dempfi/ayu)
55

66
/* General structure and fonts */
77

8-
body {
8+
body, #settings-menu #settings, #settings-menu #settings::before {
99
background-color: #0f1419;
1010
color: #c5c5c5;
1111
}
@@ -541,6 +541,10 @@ kbd {
541541
filter: invert(100);
542542
}
543543

544+
#settings-menu #settings, #settings-menu #settings::before {
545+
border-color: #5c6773;
546+
}
547+
544548
#copy-path {
545549
color: #fff;
546550
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
body {
1+
body, #settings-menu #settings, #settings-menu #settings::before {
22
background-color: #353535;
33
color: #ddd;
44
}
@@ -420,6 +420,10 @@ kbd {
420420
border-color: #ffb900;
421421
}
422422

423+
#settings-menu #settings, #settings-menu #settings::before {
424+
border-color: #d2d2d2;
425+
}
426+
423427
#copy-path {
424428
color: #999;
425429
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* General structure and fonts */
22

3-
body {
3+
body, #settings-menu #settings, #settings-menu #settings::before {
44
background-color: white;
55
color: black;
66
}
@@ -405,6 +405,10 @@ kbd {
405405
border-color: #717171;
406406
}
407407

408+
#settings-menu #settings, #settings-menu #settings::before {
409+
border-color: #DDDDDD;
410+
}
411+
408412
#copy-path {
409413
color: #999;
410414
}

src/librustdoc/html/static/js/settings.js

+57-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Local js definitions:
22
/* global getSettingValue, getVirtualKey, updateLocalStorage, updateSystemTheme */
3-
/* global addClass, removeClass, onEach, onEachLazy, NOT_DISPLAYED_ID */
4-
/* global MAIN_ID, getVar, getSettingsButton, switchDisplayedElement, getNotDisplayedElem */
3+
/* global addClass, removeClass, onEach, onEachLazy */
4+
/* global MAIN_ID, getVar, getSettingsButton */
55

66
"use strict";
77

@@ -206,38 +206,60 @@
206206
];
207207

208208
// Then we build the DOM.
209-
const el = document.createElement("section");
210-
el.id = "settings";
211-
let innerHTML = `
212-
<div class="main-heading">
209+
let innerHTML = "";
210+
let elementKind = "div";
211+
212+
if (isSettingsPage) {
213+
elementKind = "section";
214+
innerHTML = `<div class="main-heading">
213215
<h1 class="fqn">
214216
<span class="in-band">Rustdoc settings</span>
215217
</h1>
216-
<span class="out-of-band">`;
217-
218-
if (isSettingsPage) {
219-
innerHTML +=
220-
"<a id=\"back\" href=\"javascript:void(0)\" onclick=\"history.back();\">Back</a>";
221-
} else {
222-
innerHTML += "<a id=\"back\" href=\"javascript:void(0)\" " +
223-
"onclick=\"switchDisplayedElement(null);\">Back</a>";
218+
<span class="out-of-band">
219+
<a id="back" href="javascript:void(0)" onclick="history.back();">Back</a>
220+
</span>
221+
</div>`;
224222
}
225-
innerHTML += `</span>
226-
</div>
227-
<div class="settings">${buildSettingsPageSections(settings)}</div>`;
223+
innerHTML += `<div class="settings">${buildSettingsPageSections(settings)}</div>`;
228224

225+
const el = document.createElement(elementKind);
226+
el.id = "settings";
229227
el.innerHTML = innerHTML;
230228

231229
if (isSettingsPage) {
232230
document.getElementById(MAIN_ID).appendChild(el);
233231
} else {
234-
getNotDisplayedElem().appendChild(el);
232+
el.setAttribute("tabindex", "-1");
233+
getSettingsButton().appendChild(el);
235234
}
236235
return el;
237236
}
238237

239238
const settingsMenu = buildSettingsPage();
240239

240+
function displaySettings() {
241+
settingsMenu.style.display = "";
242+
}
243+
244+
function elemIsInParent(elem, parent) {
245+
while (elem && elem !== document.body) {
246+
if (elem === parent) {
247+
return true;
248+
}
249+
elem = elem.parentElement;
250+
}
251+
return false;
252+
}
253+
254+
function blurHandler(event) {
255+
const settingsButton = getSettingsButton();
256+
if (!elemIsInParent(document.activeElement, settingsButton) &&
257+
!elemIsInParent(event.relatedTarget, settingsButton))
258+
{
259+
window.hideSettings();
260+
}
261+
}
262+
241263
if (isSettingsPage) {
242264
// We replace the existing "onclick" callback to do nothing if clicked.
243265
getSettingsButton().onclick = function(event) {
@@ -246,25 +268,35 @@
246268
} else {
247269
// We replace the existing "onclick" callback.
248270
const settingsButton = getSettingsButton();
271+
const settingsMenu = document.getElementById("settings");
272+
window.hideSettings = function() {
273+
settingsMenu.style.display = "none";
274+
};
249275
settingsButton.onclick = function(event) {
276+
if (elemIsInParent(event.target, settingsMenu)) {
277+
return;
278+
}
250279
event.preventDefault();
251-
if (settingsMenu.parentElement.id === NOT_DISPLAYED_ID) {
252-
switchDisplayedElement(settingsMenu);
253-
} else {
280+
if (settingsMenu.style.display !== "none") {
254281
window.hideSettings();
282+
} else {
283+
displaySettings();
255284
}
256285
};
257-
window.hideSettings = function() {
258-
switchDisplayedElement(null);
259-
};
286+
settingsButton.onblur = blurHandler;
287+
settingsButton.querySelector("a").onblur = blurHandler;
288+
onEachLazy(settingsMenu.querySelectorAll("input"), el => {
289+
el.onblur = blurHandler;
290+
});
291+
settingsMenu.onblur = blurHandler;
260292
}
261293

262294
// We now wait a bit for the web browser to end re-computing the DOM...
263295
setTimeout(() => {
264296
setEvents(settingsMenu);
265297
// The setting menu is already displayed if we're on the settings page.
266298
if (!isSettingsPage) {
267-
switchDisplayedElement(settingsMenu);
299+
displaySettings();
268300
}
269301
removeClass(getSettingsButton(), "rotate");
270302
}, 0);

src/librustdoc/html/templates/page.html

+5-3
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,12 @@ <h2 class="location"></h2>
126126
placeholder="Click or press ‘S’ to search, ‘?’ for more options…" {# -#}
127127
type="search"> {#- -#}
128128
<button type="button" id="help-button" title="help">?</button> {#- -#}
129-
<a id="settings-menu" href="{{page.root_path|safe}}settings.html" title="settings"> {#- -#}
130-
<img width="22" height="22" alt="Change settings" {# -#}
129+
<div id="settings-menu" tabindex="-1">
130+
<a href="{{page.root_path|safe}}settings.html" title="settings"> {#- -#}
131+
<img width="22" height="22" alt="Change settings" {# -#}
131132
src="{{static_root_path|safe}}wheel{{page.resource_suffix}}.svg"> {#- -#}
132-
</a> {#- -#}
133+
</a> {#- -#}
134+
</div>
133135
</div> {#- -#}
134136
</form> {#- -#}
135137
</nav> {#- -#}

0 commit comments

Comments
 (0)