Driving bar after the research: four 76 px buttons (zoom ±, 2D/3D, route overview), one Follow button bottom left shown only while the map isn't following, night and exit off the bar; research on day/night and on driving buttons (Garmin aviation/marine/zūmo, ships' ECDIS/S-Mode, Android Auto) in docs/design.adoc, its follow-ups in TODO
CI / check (push) Successful in 21s
CI / deploy (push) Successful in 17s

This commit is contained in:
randy committed 2026-12-01 02:02:55 +01:00
1 parent 9b797fd48c
commit 0eb9d05cdc
5 files changed
+211 -36

No files matched your search

+36 -31
View File
@@ -1,12 +1,16 @@
import { LngLatBounds } from "/vendor/maplibre-gl/maplibre-gl.mjs";
import { requestGnssFollow, onRouteChanged } from "/js/panels.js";
import { pauseFollow, resumeFollow } from "/js/gnss.js";
import { pauseFollow, resumeFollow, isFollowing } from "/js/gnss.js";
// Driving mode: the few things needed on the road as big buttons down the
// side of the map, the planning sidebar and panels out of the way (TODO:
// tiny icons can't be hit while driving). Per device (localStorage).
// Driving mode: the few things needed on the road as big buttons, the
// planning sidebar and panels out of the way. After the research in
// docs/design.adoc ("Buttons while driving"): four buttons on the map
// (Android Auto's action strip: at most four, 76 px), the recenter button
// only while the map isn't following (as Google Maps), night automatic and
// leaving by the topbar's Drive. Per device (localStorage).
const KEY = "drivingMode";
const PITCH_3D = 60; // as map.js's 3D toggle
const CHECK_MS = 1000; // how often the recenter button looks whether the map follows
export function setupDriving(map, nightButton) {
let route = onRouteChanged((r) => (route = r));
@@ -17,47 +21,48 @@ export function setupDriving(map, nightButton) {
toggle.textContent = "Drive";
nightButton.before(toggle);
const button = (name, label, title, action) => {
const el = document.createElement("button");
el.type = "button";
el.className = `drive-bar__${name}`;
el.textContent = label;
el.title = title;
el.setAttribute("aria-label", title);
el.addEventListener("click", action);
return el;
};
const bar = document.createElement("div");
bar.className = "drive-bar";
bar.hidden = true;
const buttons = {
follow: ["Follow", "Follow the position, heading up in 3D", () => {
requestGnssFollow();
resumeFollow(map);
if (map.getPitch() === 0) map.easeTo({ pitch: PITCH_3D, duration: 300 });
}],
zoomIn: ["+", "Zoom in", () => map.zoomIn()],
zoomOut: ["−", "Zoom out", () => map.zoomOut()],
pitch: ["2D", "2D / 3D", () => map.easeTo({ pitch: map.getPitch() > 0 ? 0 : PITCH_3D, ...(map.getPitch() > 0 && { bearing: 0 }), duration: 300 })],
night: ["", "Night mode", () => nightButton.click()],
overview: ["Route", "The whole route (the follow pauses)", () => {
bar.append(
button("zoomIn", "+", "Zoom in", () => map.zoomIn()),
button("zoomOut", "−", "Zoom out", () => map.zoomOut()),
button("pitch", "3D", "2D / 3D", () => map.easeTo({ pitch: map.getPitch() > 0 ? 0 : PITCH_3D, ...(map.getPitch() > 0 && { bearing: 0 }), duration: 300 })),
button("overview", "Route", "The whole route (the follow pauses)", () => {
const coords = route?.geometry?.coordinates;
if (!coords?.length) return;
pauseFollow(map);
map.fitBounds(coords.reduce((b, c) => b.extend(c), new LngLatBounds(coords[0], coords[0])), { padding: 80, pitch: 0, bearing: 0 });
}],
exit: ["✕", "Leave driving mode", () => set(false)],
};
for (const [name, [label, title, action]] of Object.entries(buttons)) {
const button = document.createElement("button");
button.type = "button";
button.className = `drive-bar__${name}`;
button.textContent = label;
button.title = title;
button.setAttribute("aria-label", title);
button.addEventListener("click", action);
bar.appendChild(button);
}
map.getContainer().appendChild(bar);
}),
);
// the one "follow / back to the position", bottom left, only when needed
const recenter = button("recenter", "Follow", "Follow the position, heading up in 3D", () => {
requestGnssFollow();
resumeFollow(map);
if (map.getPitch() === 0) map.easeTo({ pitch: PITCH_3D, duration: 300 });
recenter.hidden = true;
});
recenter.hidden = true;
map.getContainer().append(bar, recenter);
const render = () => {
bar.querySelector(".drive-bar__pitch").textContent = map.getPitch() > 0 ? "2D" : "3D";
bar.querySelector(".drive-bar__night").textContent = nightButton.textContent;
bar.querySelector(".drive-bar__overview").disabled = !route;
recenter.hidden = bar.hidden || isFollowing(map);
};
map.on("pitchend", render);
nightButton.addEventListener("click", render);
onRouteChanged(render);
setInterval(render, CHECK_MS);
function set(on) {
document.documentElement.classList.toggle("driving", on);
+7
View File
@@ -57,6 +57,13 @@ export function pauseFollow(map) {
state.timer = setTimeout(() => (state.button.hidden = true), FREE_LOOK_MS);
}
// Is the map following a position right now (a follow step lately, not
// paused by hand)? For the driving bar's recenter button.
export function isFollowing(map) {
const state = freeLookState(map);
return Date.now() - state.followedAt <= FOLLOWING_MS && Date.now() >= state.until;
}
export function resumeFollow(map) {
const state = freeLookState(map);
state.until = 0;