Add a distance unit option

This commit is contained in:
James Babcock committed 2026-10-26 00:45:10 -07:00
1 parent 22f869d31c
commit 1ec77a8ade
5 files changed
+82 -14

No files matched your search

+34 -10
View File
@@ -28,6 +28,7 @@ import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu";
import type { LayerContext } from "../../ui/layers";
import { truncateText, wrapText } from "../../graphics/textwrap";
import { onSettingsStoreChanged } from "../../native/settings-store";
import { formatClockTime } from "../../ui/clock-format";
import { openUrlOnPhone } from "../../native/open-url";
import {
navigateDestinationAddressDraftSetting,
@@ -35,6 +36,9 @@ import {
navigateRememberRecentSetting,
navigateDisplayLayoutSetting,
mapboxApiKeySetting,
distanceUnitSetting,
DISTANCE_UNIT_SETTING_KEY,
TIME_FORMAT_SETTING_KEY,
enumSettingMenuItem,
type ConfigSettingString,
} from "../../ui/dashboard-settings";
@@ -266,8 +270,15 @@ function post(message: WorkerAppReply): void {
// the edit screen live.
onSettingsStoreChanged((key) => {
// The token key repaints too: the phone editor writes it live during
// Edit token, and the idle page changes shape once one is set.
if (key.startsWith("navigate.") || key === MAPBOX_TOKEN_SETTING_KEY) render();
// Edit token, and the idle page changes shape once one is set. So do the
// distance unit and time format (Settings > Customization), for the
// guidance panel.
if (
key.startsWith("navigate.") ||
key === MAPBOX_TOKEN_SETTING_KEY ||
key === DISTANCE_UNIT_SETTING_KEY ||
key === TIME_FORMAT_SETTING_KEY
) render();
});
// The host queues messages until this arrives: posts to a worker whose bundle
@@ -1569,15 +1580,33 @@ function drawFooter(image: GrayImage, text: string, x = 16): void {
}
// ---------------------------------------------------------------------------
// Formatting (imperial; matches the rest of the UI)
// Formatting (distances and clock times follow Settings > Customization)
function metricDistances(): boolean {
return distanceUnitSetting.get() === "kilometers";
}
/** Under a kilometer: 10 m steps up to 100 m, then 50 m steps. */
function formatMeters(meters: number): string {
const step = meters < 100 ? 10 : 50;
return `${Math.max(0, Math.round(meters / step) * step)} m`;
}
function formatDistanceShort(meters: number): string {
if (metricDistances()) {
return meters < 950 ? formatMeters(meters) : `${(meters / 1000).toFixed(1)} km`;
}
const feet = meters * 3.28084;
if (feet < 900) return `${Math.max(0, Math.round(feet / 50) * 50)} ft`;
return `${(feet / 5280).toFixed(1)} mi`;
}
function formatDistance(meters: number): string {
if (metricDistances()) {
if (meters < 950) return formatMeters(meters);
const km = meters / 1000;
return km >= 10 ? `${Math.round(km)} km` : `${km.toFixed(1)} km`;
}
const feet = meters * 3.28084;
if (feet < 1000) return `${Math.max(0, Math.round(feet / 50) * 50)} ft`;
const miles = feet / 5280;
@@ -1597,14 +1626,9 @@ function formatDurationLong(seconds: number): string {
return `${hours}h ${minutes % 60}m`;
}
/** "14:05" / "2:05 PM", as the top-bar clock shows it. */
function formatClock(timestampMs: number): string {
const date = new Date(timestampMs);
let hours = date.getHours();
const minutes = String(date.getMinutes()).padStart(2, "0");
const meridiem = hours >= 12 ? "PM" : "AM";
hours = hours % 12;
if (hours === 0) hours = 12;
return `${hours}:${minutes} ${meridiem}`;
return formatClockTime(new Date(timestampMs));
}
// ---------------------------------------------------------------------------
+7 -1
View File
@@ -6,6 +6,7 @@ import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures";
import { type Layer, type LayerContext } from "../../ui/layers";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics";
import { distanceUnitSetting } from "../../ui/dashboard-settings";
import { renderWeatherIcon } from "./weather-icons";
import { formatTemperature, widestTemperatureSample } from "./temperature-format";
@@ -142,7 +143,7 @@ export class WeatherLayer implements Layer {
const details: string[] = [];
if (current.humidityPercent !== null) details.push(`Humidity ${Math.round(current.humidityPercent)}%`);
if (current.windSpeedMph !== null) {
details.push(`Wind ${current.windDirection ? `${current.windDirection} ` : ""}${Math.round(current.windSpeedMph)} mph`);
details.push(`Wind ${current.windDirection ? `${current.windDirection} ` : ""}${formatWindSpeed(current.windSpeedMph)}`);
}
image.drawText(small, textX, cy, details.join(" ") || "Current forecast", 170);
cy += lineStep(small);
@@ -247,6 +248,11 @@ function forecastRowHeight(font: UiFont): number {
return Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3);
}
/** A wind speed (weather data is kept in mph) in the Distance unit setting's speed unit: "12 mph" / "19 km/h". */
function formatWindSpeed(mph: number): string {
return distanceUnitSetting.get() === "kilometers" ? `${Math.round(mph * 1.609344)} km/h` : `${Math.round(mph)} mph`;
}
function formatAge(ageMs: number): string {
const minutes = Math.max(0, Math.round(ageMs / 60_000));
if (minutes < 1) return "just now";