import { LngLatBounds } from "/vendor/maplibre-gl/maplibre-gl.mjs"; import { requestGnssFollow, onRouteChanged } from "/js/panels.js"; import { pauseFollow, resumeFollow, isFollowing } from "/js/gnss.js"; // 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)); const toggle = document.createElement("button"); toggle.type = "button"; toggle.className = "topbar__drive"; toggle.title = "Driving mode: big buttons, no planning panels"; 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; 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 }); }), ); // 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__overview").disabled = !route; recenter.hidden = bar.hidden || isFollowing(map); }; map.on("pitchend", render); onRouteChanged(render); setInterval(render, CHECK_MS); function set(on) { document.documentElement.classList.toggle("driving", on); bar.hidden = !on; toggle.classList.toggle("active", on); try { localStorage.setItem(KEY, on ? "on" : "off"); } catch { /* private window: not kept */ } render(); map.resize(); // the sidebar came or went } toggle.addEventListener("click", () => set(bar.hidden)); let saved = null; try { saved = localStorage.getItem(KEY); } catch { /* nothing kept */ } set(saved === "on"); }