diff --git a/TODO.adoc b/TODO.adoc index 7f2d641..f83ccfd 100644 --- a/TODO.adoc +++ b/TODO.adoc @@ -426,6 +426,9 @@ are in home-gitops `docs/canopen-nodes.adoc` ("Inšpirácia: Garmin Tread / zūm actions in `data/can.json` — pan, zoom ±, add a point (the position into the library or the trip), mark a place, switch views (navigation / overview / dashboard), mute / volume, the trip's next stop, night mode; a long press for a second action, the keypad's LEDs showing state +** [x] Drive mode keys from the keyboard (profile `key`, labels in the corners, ↑ ↓ / Enter / Space / + / Esc in a menu) +** [ ] The keypad's keys and encoder (as ↑ ↓) from the bus into the same keys; its heartbeat switches the labels to `keypad`, gone 5 s back +** [ ] The switch (auto / keyboard / keypad) and the key mapping in our diagnostics * [ ] A virtual switch panel for the PDM node (like Garmin's PowerSwitch: tiles with icon, label, state and current, groups) + rules: by speed, sun/time of day, geofence (the trip's camp), ignition and battery voltage diff --git a/docs/design.adoc b/docs/design.adoc index bc146bc..d54e71a 100644 --- a/docs/design.adoc +++ b/docs/design.adoc @@ -310,6 +310,16 @@ value | action | km}`; widgets `maneuver`, `field`, `status`, `elevation`, `compass`, `key`); once the editor exists it saves to `data/` and the file is the default. +*Keys* (artboard GridKeys): a `key` cell's `key` is the keyboard key +that clicks it (the uConsole has no touch), `keypad` the CAN keypad's; +the menus' own buttons carry theirs (1 2 3, H). In an open menu ↑ ↓ +move the row (the keypad's encoder sends the same arrows), Enter its +first button, Space its eye, + adds it, ⌫ up a folder, Esc closes. +Every bound button shows its key in the corner, from the first key +pressed on. Still to do: the keypad's keys from the bus, its heartbeat +switching the labels to the keypad by itself, and the switch (auto / +keyboard / keypad) and the mapping in our diagnostics. + *Terrain* (artboard GridTerrain): the inclinometer only off-road — on the road its cells show the elevation ahead. A 7 × 8 panel like a menu, the map 9 × 8: roll (a rear view) and pitch (a side view), each with diff --git a/public/css/style.css b/public/css/style.css index 8503ab1..fa4d8eb 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -4714,3 +4714,32 @@ html.driving .dash-card { display: none; } /* the grid's cells and its Terrain v .drive-terrain__reset { grid-area: 8 / 4 / 9 / 6; } .drive-terrain__road { grid-area: 8 / 6 / 9 / 8; } .drive-panel button.drive-terrain__level--armed { background: var(--inst-caution); color: #000; } + +/* Drive keys (canvas GridKeys): the key that does a button in its corner, the + row ↑ ↓ are on in purple with its keys (⏎ the first button, ␣ the eye, + add); + shown from the first key pressed on (html.drive-keys, driving.js) */ +html.driving.drive-keys [data-key], +html.driving.drive-keys .drive-focus { position: relative; } +html.driving.drive-keys [data-key]::after, +html.driving.drive-keys .drive-focus--main::after, +html.driving.drive-keys .drive-focus.drive-panel__eye::after, +html.driving.drive-keys .drive-focus.drive-panel__add::after { + content: attr(data-label); + position: absolute; + top: calc(4px * var(--s, 1)); + right: calc(5px * var(--s, 1)); + padding: 0 calc(4px * var(--s, 1)); + border: 1px solid currentColor; + border-radius: 4px; + color: var(--drive-muted); + font-size: calc(11px * var(--s, 1)); + font-weight: 700; + line-height: 1.3; + letter-spacing: 0; + text-transform: none; + pointer-events: none; +} +html.driving.drive-keys .drive-focus { outline: 3px solid #b45bd0; outline-offset: -3px; } +html.driving.drive-keys .drive-focus--main::after { content: "⏎"; color: #b45bd0; } +html.driving.drive-keys .drive-focus.drive-panel__eye::after { content: "␣"; color: #b45bd0; } +html.driving.drive-keys .drive-focus.drive-panel__add::after { content: "+"; color: #b45bd0; } diff --git a/public/drive/road.json b/public/drive/road.json index a1c10ff..d630f98 100644 --- a/public/drive/road.json +++ b/public/drive/road.json @@ -20,16 +20,16 @@ { "area": "5 / 14 / 6 / 17", "widget": "field", "value": "toEnd" }, { "area": "6 / 14 / 9 / 17", "widget": "elevation", "km": 10 }, { "area": "9 / 14 / 10 / 17", "widget": "compass" }, - { "area": "10 / 1 / 11 / 3", "widget": "key", "action": "trip" }, - { "area": "10 / 3 / 11 / 5", "widget": "key", "action": "points" }, - { "area": "10 / 5 / 11 / 7", "widget": "key", "action": "layers" }, - { "area": "10 / 7 / 11 / 9", "widget": "key", "action": "search" }, - { "area": "10 / 9 / 11 / 11", "widget": "key", "action": "menu" }, - { "area": "10 / 11 / 11 / 12", "widget": "key", "action": "zoomOut" }, - { "area": "10 / 12 / 11 / 13", "widget": "key", "action": "zoomIn" }, - { "area": "10 / 13 / 11 / 14", "widget": "key", "action": "pitch" }, - { "area": "10 / 14 / 11 / 15", "widget": "key", "action": "follow" }, - { "area": "10 / 15 / 11 / 16", "widget": "key", "action": "terrain" }, - { "area": "10 / 16 / 11 / 17", "widget": "key", "action": "palette" } + { "area": "10 / 1 / 11 / 3", "widget": "key", "action": "trip", "key": "F1", "keypad": "K1" }, + { "area": "10 / 3 / 11 / 5", "widget": "key", "action": "points", "key": "F2", "keypad": "K2" }, + { "area": "10 / 5 / 11 / 7", "widget": "key", "action": "layers", "key": "F3", "keypad": "K3" }, + { "area": "10 / 7 / 11 / 9", "widget": "key", "action": "search", "key": "/", "keypad": "K4" }, + { "area": "10 / 9 / 11 / 11", "widget": "key", "action": "menu", "key": "F5", "keypad": "K5" }, + { "area": "10 / 11 / 11 / 12", "widget": "key", "action": "zoomOut", "key": "-", "keypad": "E3 ⟲" }, + { "area": "10 / 12 / 11 / 13", "widget": "key", "action": "zoomIn", "key": "+", "keypad": "E3 ⟳" }, + { "area": "10 / 13 / 11 / 14", "widget": "key", "action": "pitch", "key": "3", "keypad": "K6" }, + { "area": "10 / 14 / 11 / 15", "widget": "key", "action": "follow", "key": "f", "keypad": "E3 ⏎" }, + { "area": "10 / 15 / 11 / 16", "widget": "key", "action": "terrain", "key": "t", "keypad": "K7" }, + { "area": "10 / 16 / 11 / 17", "widget": "key", "action": "palette", "key": "n", "keypad": "K8" } ] } diff --git a/public/js/drive_menus.js b/public/js/drive_menus.js index f0de0ef..4ab0635 100644 --- a/public/js/drive_menus.js +++ b/public/js/drive_menus.js @@ -30,6 +30,13 @@ export function cellButton(cls, html, title, action) { return el; } +// A hardware key for a button: pressed, it clicks it; its label in the corner (driving.js, the keys) +export function bindKey(el, key) { + el.dataset.key = key; + el.dataset.label = key === "-" ? "−" : key.length === 1 ? key.toUpperCase() : key; + return el; +} + export const emptyRow = (text) => Object.assign(document.createElement("p"), { className: "drive-panel__empty", textContent: text }); function panel(cls, label, inner) { @@ -56,7 +63,7 @@ export function pointsMenu(ctx) {
- `); + `); const input = el.querySelector("input"); const bar = el.querySelector(".drive-points__bar"); const list = el.querySelector(".drive-points__list"); @@ -189,8 +196,8 @@ export function pointsMenu(ctx) { el.querySelector(".drive-panel__note").textContent = "folders"; renderFolder(); } else { - bar.replaceChildren(...[["near", "Near the car"], ["ahead", "Ahead"], ["shown", "Shown"]].map(([id, label]) => { - const chip = cellButton("scope", label, label, () => { scope = id; render(); }); + bar.replaceChildren(...[["near", "Near the car"], ["ahead", "Ahead"], ["shown", "Shown"]].map(([id, label], i) => { + const chip = bindKey(cellButton("scope", label, label, () => { scope = id; render(); }), String(i + 1)); chip.setAttribute("aria-pressed", String(scope === id)); return chip; }), cellButton("mode", `▣Folders`, "Folders", () => { scope = "folder"; input.value = ""; render(); })); @@ -223,7 +230,7 @@ export function layersMenu(ctx) {
- `); + `); const list = el.querySelector(".drive-layers__list"); let order = []; let keeping = false; @@ -251,7 +258,7 @@ export function layersMenu(ctx) { if (el.hidden) return; const presets = loadPresets(); const current = presetOn(presets); - el.querySelector(".drive-layers__presets").replaceChildren(...PRESETS.map(([id, icon, label]) => { + el.querySelector(".drive-layers__presets").replaceChildren(...PRESETS.map(([id, icon, label], i) => { const b = cellButton("preset", `${icon}${keeping ? `Save as ${label}` : label}`, label, () => { if (keeping) { presets[id] = layers().filter((l) => l.toggle.checked).map((l) => l.name); @@ -263,7 +270,7 @@ export function layersMenu(ctx) { render(); }); b.setAttribute("aria-pressed", String(!keeping && current === id)); - return b; + return bindKey(b, String(i + 1)); }), (() => { const keep = cellButton("mode", `${keeping ? "✕" : "+"}${keeping ? "Cancel" : "Keep"}`, keeping ? "Cancel" : "Keep what is on as a preset", () => { keeping = !keeping; render(); }); diff --git a/public/js/driving.js b/public/js/driving.js index ca93d4e..4b609a5 100644 --- a/public/js/driving.js +++ b/public/js/driving.js @@ -5,7 +5,7 @@ import { formatDuration } from "/js/route.js"; import { isLoggedIn } from "/js/auth.js"; import { watchDashboard, maneuverSvg, distance } from "/js/dashboard.js"; import { formatSunInfo } from "/js/sun.js"; -import { pointsMenu, layersMenu, terrainMenu, elevationHtml, cellButton, emptyRow as empty, escapeHtml } from "/js/drive_menus.js"; +import { pointsMenu, layersMenu, terrainMenu, elevationHtml, cellButton, bindKey, emptyRow as empty, escapeHtml } from "/js/drive_menus.js"; // Driving mode: the grid of docs/design.adoc ("Layout: a grid, not floating // panels"; the Design canvas' Grid) — the page becomes cells, the map one @@ -250,6 +250,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) { } if (panel !== tripPanel && results) backToStops(); // the pins go with the search openPanel = panel; + row = 0; tripPanel.hidden = panel !== tripPanel; menuPanel.hidden = panel !== menuPanel; for (const menu of menus) { @@ -374,6 +375,7 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) { el.disabled = NOT_YET.has(cell.action) || !keyAction[cell.action]; el.addEventListener("click", () => keyAction[cell.action]?.()); if (cell.action === "zoomIn" || cell.action === "zoomOut") el.classList.add("drive-key--big"); + if (cell.key) bindKey(el, cell.key); // "keypad" too, for when the keypad is on the bus } else { el = document.createElement("div"); } @@ -546,6 +548,65 @@ export function setupDriving(map, nightButton, routePanel, gpxPanel) { map.resize(); // the grid came or went } toggle.addEventListener("click", () => set(!document.documentElement.classList.contains("driving"))); + + // Keys (canvas GridKeys): a screen key's "key" in the profile clicks it, a + // menu's [data-key] buttons too (1 2 3, H); in an open menu ↑ ↓ move the row + // (the keypad's encoder will send the same arrows), Enter its first button, + // Space its eye, + adds it, ⌫ up a folder, Esc closes. The labels in the + // corners show from the first key pressed on, so a touch-only screen stays clean. + // ponytail: keyboard only; the keypad's keys and its heartbeat switching the + // labels to "keypad" wait for the keypad on the bus (and the diagnostics' mapping) + const KEYS_SEEN = "maptool.driveKeys"; + let row = 0; + try { if (localStorage.getItem(KEYS_SEEN)) document.documentElement.classList.add("drive-keys"); } catch { /* nothing kept */ } + const norm = (k) => (k.length === 1 ? k.toLowerCase() : k); + const visible = (b) => b.offsetParent && !b.disabled; + const rowsOf = (panel) => { + const rows = new Map(); + for (const b of panel.querySelectorAll("button:not(.drive-panel__close)")) { + if (!visible(b)) continue; + const top = Math.round(b.getBoundingClientRect().top); + rows.set(top, [...(rows.get(top) || []), b]); + } + return [...rows].sort((a, b) => a[0] - b[0]).map(([, r]) => r); + }; + const markRow = (scroll) => { + for (const b of document.querySelectorAll(".drive-focus")) b.classList.remove("drive-focus", "drive-focus--main"); + if (!openPanel || !document.documentElement.classList.contains("drive-keys")) return []; + const rows = rowsOf(openPanel); + row = Math.max(0, Math.min(row, rows.length - 1)); + const current = rows[row] || []; + for (const b of current) b.classList.add("drive-focus"); + current[0]?.classList.add("drive-focus--main"); + if (scroll) current[0]?.scrollIntoView({ block: "nearest" }); + return current; + }; + let marking = 0; // the menus re-render their rows: the highlight follows, once a frame + new MutationObserver(() => { marking ||= requestAnimationFrame(() => { marking = 0; markRow(false); }); }).observe(app, { childList: true, subtree: true }); + document.addEventListener("keydown", (e) => { + if (!document.documentElement.classList.contains("driving") || e.ctrlKey || e.altKey || e.metaKey) return; + const key = norm(e.key); + const move = key === "ArrowUp" ? -1 : key === "ArrowDown" ? 1 : 0; + if (e.target.matches?.("input, textarea, select")) { + if (key === "Escape" || move) e.target.blur(); else return; // typing a search + } + if (!document.documentElement.classList.contains("drive-keys")) { + document.documentElement.classList.add("drive-keys"); + try { localStorage.setItem(KEYS_SEEN, "1"); } catch { /* private window: not kept */ } + } + const current = markRow(false); + const press = (b) => (b && visible(b) ? (b.click(), true) : false); + const bound = (scope) => [...(scope?.querySelectorAll("[data-key]") || [])].find((b) => norm(b.dataset.key) === key && visible(b)); + let done = false; + if (key === "Escape") done = !!openPanel && (open(null), true); + else if (openPanel && move) { row += move; markRow(true); done = true; } + else if (openPanel && key === "Enter") done = press(current[0]); + else if (openPanel && key === " ") done = press(current.find((b) => b.classList.contains("drive-panel__eye"))); + else if (openPanel && key === "Backspace") done = press(openPanel.querySelector(".drive-panel__up")); + if (!done && openPanel && key === "+") done = press(current.find((b) => b.classList.contains("drive-panel__add"))); + if (!done) done = press(bound(openPanel) || bound(app)); + if (done) e.preventDefault(); // F1, F5, / are the browser's otherwise + }); let saved = null; try { saved = localStorage.getItem(KEY); } catch { /* nothing kept */ } set(saved === "on");