Weather app on glanceboard system card and status bar, and has an image

This commit is contained in:
James Babcock committed 2026-10-04 03:08:37 -07:00
1 parent 61ede9b2a0
commit 6db913af48
15 files changed
+762 -33

No files matched your search

+2
View File
@@ -1,10 +1,12 @@
import { type AppDefinition } from "../app-definition";
import { createWeatherAppWindow, WEATHER_SURFACE_ID, WEATHER_WINDOW_ID } from "./weather-app";
import { startWeatherIndicators } from "./weather-indicators";
const weatherApp: AppDefinition = {
appId: "weather",
title: "Weather",
icon: "cloud-sun",
boot: () => startWeatherIndicators(),
launch: (ctx) => ctx.launchInProcessApp(WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow),
};
+23
View File
@@ -1,6 +1,9 @@
import { ensureLocationPermission, hasLocationPermission } from "../../native/location-permissions";
import { weatherBridge } from "../../native/weather";
import { toggleSettingMenuItem } from "../../ui/dashboard-settings";
import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
import { WeatherLayer } from "./weather";
import { weatherShowInStatusBarSetting, weatherShowOnSystemCardSetting } from "./weather-settings";
import {
createInProcessWindow,
YieldAtRootLayer,
@@ -32,6 +35,14 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
}).catch((error) => { requestingPermission = false; console.warn(`Weather permission: ${error}`); });
};
// An indicator switched on without location access asks for it here, since
// its background refreshes have no screen of their own to prompt from.
const indicatorSettingOptions = {
onChange: (_ctx: unknown, enabled: boolean) => {
if (enabled && !hasLocationPermission()) requestUpdate();
},
};
const app = createInProcessWindow({
appId: "weather",
windowId: WEATHER_WINDOW_ID,
@@ -47,6 +58,18 @@ export function createWeatherAppWindow(options: InProcessAppOptions): InProcessW
requestUpdate();
},
},
{
label: "Settings",
onSelect: (ctx) => {
// Pop the menu first so closing the settings modal lands back on
// the forecast, not this menu.
ctx.stack.pop();
openSettingsSubMenu(ctx, "Weather settings", [
toggleSettingMenuItem(weatherShowInStatusBarSetting, indicatorSettingOptions),
toggleSettingMenuItem(weatherShowOnSystemCardSetting, indicatorSettingOptions),
]);
},
},
],
actions: options.actions,
baseLayer: new YieldAtRootLayer(new WeatherLayer(() => weatherBridge.snapshot(), requestUpdate)),
+119
View File
@@ -0,0 +1,119 @@
import { renderSvgIcon } from "../../graphics/icons";
import { type GrayImage, type UiFont } from "../../graphics/image";
import { type WeatherCondition, type WeatherState } from "../../native/weather";
// Lucide weather icons (ISC licensed), kept verbatim so they can be diffed
// against upstream.
const WEATHER_SVGS = {
sun:
'<svg viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="4"/><path d="M12 2v2"/><path d="M12 20v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="m17.66 17.66 1.41 1.41"/><path d="M2 12h2"/><path d="M20 12h2"/><path d="m6.34 17.66-1.41 1.41"/><path d="m19.07 4.93-1.41 1.41"/></svg>',
moon:
'<svg viewBox="0 0 24 24" fill="none"><path d="M20.985 12.486a9 9 0 1 1-9.473-9.472c.405-.022.617.46.402.803a6 6 0 0 0 8.268 8.268c.344-.215.825-.004.803.401"/></svg>',
cloud:
'<svg viewBox="0 0 24 24" fill="none"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"/></svg>',
"cloud-sun":
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 2v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="M20 12h2"/><path d="m19.07 4.93-1.41 1.41"/><path d="M15.947 12.65a4 4 0 0 0-5.925-4.128"/><path d="M13 22H7a5 5 0 1 1 4.9-6H13a3 3 0 0 1 0 6Z"/></svg>',
"cloud-moon":
'<svg viewBox="0 0 24 24" fill="none"><path d="M13 16a3 3 0 0 1 0 6H7a5 5 0 1 1 4.9-6z"/><path d="M18.376 14.512a6 6 0 0 0 3.461-4.127c.148-.625-.659-.97-1.248-.714a4 4 0 0 1-5.259-5.26c.255-.589-.09-1.395-.716-1.248a6 6 0 0 0-4.594 5.36"/></svg>',
"cloud-fog":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M16 17H7"/><path d="M17 21H9"/></svg>',
wind:
'<svg viewBox="0 0 24 24" fill="none"><path d="M12.8 19.6A2 2 0 1 0 14 16H2"/><path d="M17.5 8a2.5 2.5 0 1 1 2 4H2"/><path d="M9.8 4.4A2 2 0 1 1 11 8H2"/></svg>',
"cloud-drizzle":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M8 19v1"/><path d="M8 14v1"/><path d="M16 19v1"/><path d="M16 14v1"/><path d="M12 21v1"/><path d="M12 16v1"/></svg>',
"cloud-sun-rain":
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 2v2"/><path d="m4.93 4.93 1.41 1.41"/><path d="M20 12h2"/><path d="m19.07 4.93-1.41 1.41"/><path d="M15.947 12.65a4 4 0 0 0-5.925-4.128"/><path d="M3 20a5 5 0 1 1 8.9-4H13a3 3 0 0 1 2 5.24"/><path d="M11 20v2"/><path d="M7 19v2"/></svg>',
"cloud-moon-rain":
'<svg viewBox="0 0 24 24" fill="none"><path d="M11 20v2"/><path d="M18.376 14.512a6 6 0 0 0 3.461-4.127c.148-.625-.659-.97-1.248-.714a4 4 0 0 1-5.259-5.26c.255-.589-.09-1.395-.716-1.248a6 6 0 0 0-4.594 5.36"/><path d="M3 20a5 5 0 1 1 8.9-4H13a3 3 0 0 1 2 5.24"/><path d="M7 19v2"/></svg>',
"cloud-rain":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M16 14v6"/><path d="M8 14v6"/><path d="M12 16v6"/></svg>',
"cloud-lightning":
'<svg viewBox="0 0 24 24" fill="none"><path d="M6 16.326A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 .5 8.973"/><path d="m13 12-3 5h4l-3 5"/></svg>',
"cloud-snow":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M8 15h.01"/><path d="M8 19h.01"/><path d="M12 17h.01"/><path d="M12 21h.01"/><path d="M16 15h.01"/><path d="M16 19h.01"/></svg>',
"cloud-hail":
'<svg viewBox="0 0 24 24" fill="none"><path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"/><path d="M16 14v2"/><path d="M8 14v2"/><path d="M16 20h.01"/><path d="M8 20h.01"/><path d="M12 16v2"/><path d="M12 22h.01"/></svg>',
} as const;
type WeatherIconName = keyof typeof WEATHER_SVGS;
/** Status bar and system card hide weather whose last refresh is older than this. */
const INDICATOR_STALE_MS = 3 * 60 * 60 * 1000;
/** Gap between a reading's icon and its temperature. */
const READING_ICON_GAP = 3;
function weatherIconName(condition: WeatherCondition, isDaytime: boolean): WeatherIconName {
switch (condition) {
case "clear":
return isDaytime ? "sun" : "moon";
case "partly-cloudy":
return isDaytime ? "cloud-sun" : "cloud-moon";
case "cloudy":
return "cloud";
case "fog":
return "cloud-fog";
case "wind":
return "wind";
case "drizzle":
return "cloud-drizzle";
case "showers":
return isDaytime ? "cloud-sun-rain" : "cloud-moon-rain";
case "rain":
return "cloud-rain";
case "thunderstorm":
return "cloud-lightning";
case "snow":
return "cloud-snow";
case "sleet":
return "cloud-hail";
}
}
/** A size×size icon for the condition (sun or moon variants by time of day), rendered once and cached. */
export function renderWeatherIcon(condition: WeatherCondition, isDaytime: boolean, size: number): GrayImage | null {
const name = weatherIconName(condition, isDaytime);
return renderSvgIcon(`weather:${name}`, WEATHER_SVGS[name], size);
}
/** Current conditions as the status bar and system card show them. */
export type WeatherReading = {
condition: WeatherCondition | null;
isDaytime: boolean;
/** Rounded, unit-less ("72°"), to stay compact. */
temperature: string;
};
/** The current reading, or null when there is none or it has gone stale. */
export function weatherReading(state: WeatherState, nowMs: number): WeatherReading | null {
const current = state.current;
if (!current || state.lastUpdatedMs === null || nowMs - state.lastUpdatedMs > INDICATOR_STALE_MS) return null;
return {
condition: current.condition,
isDaytime: current.isDaytime,
temperature: current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°`,
};
}
/** Width drawWeatherReading takes for this reading. */
export function measureWeatherReading(reading: WeatherReading, font: UiFont, iconSize: number): number {
return (reading.condition ? iconSize + READING_ICON_GAP : 0) + font.measureText(reading.temperature);
}
/** The condition icon then the temperature, each centred vertically in the `height` rows from `top`. */
export function drawWeatherReading(
image: GrayImage,
reading: WeatherReading,
font: UiFont,
iconSize: number,
x: number,
top: number,
height: number,
shade: number,
): void {
if (reading.condition) {
const icon = renderWeatherIcon(reading.condition, reading.isDaytime, iconSize);
if (icon) image.drawImage(icon, x, top + (((height - icon.height) / 2) | 0));
x += iconSize + READING_ICON_GAP;
}
image.drawText(font, x, top + Math.max(0, ((height - font.lineHeight) / 2) | 0), reading.temperature, shade);
}
+46
View File
@@ -0,0 +1,46 @@
import { GrayImage, type UiFont } from "../../graphics/image";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { weatherBridge } from "../../native/weather";
import { onAnySettingChanged } from "../../ui/dashboard-settings";
import { shell } from "../../ui/shell/shell";
import { drawWeatherReading, measureWeatherReading, weatherReading, type WeatherReading } from "./weather-icons";
import { weatherShowInStatusBarSetting, weatherShowOnSystemCardSetting } from "./weather-settings";
const TRAY_ICON_ID = "weather";
const TRAY_HEIGHT = 24;
const TRAY_ICON_SIZE = 20;
let started = false;
/** What the tray currently shows, to skip rebuilding an unchanged icon. */
let trayShown: { key: string; font: UiFont } | null = null;
/**
* Shell-lifetime subscription: keeps the hourly background refresh running
* while the status-bar or system-card indicator is enabled, and the tray
* icon in sync. The system card reads the bridge itself while it shows.
*/
export function startWeatherIndicators(): void {
if (started) return;
started = true;
weatherBridge.onStateChange(syncWeatherIndicators);
onAnySettingChanged(syncWeatherIndicators);
}
function syncWeatherIndicators(): void {
const inStatusBar = weatherShowInStatusBarSetting.get();
weatherBridge.setBackgroundRefresh(inStatusBar || weatherShowOnSystemCardSetting.get());
const reading = inStatusBar ? weatherReading(weatherBridge.snapshot(), Date.now()) : null;
const font = getDefaultSmallFont();
const key = reading ? `${reading.condition}|${reading.isDaytime}|${reading.temperature}` : null;
if (key === (trayShown?.key ?? null) && (!trayShown || trayShown.font === font)) return;
trayShown = key === null ? null : { key, font };
shell.setTrayIcon(TRAY_ICON_ID, reading ? buildWeatherTrayIcon(reading, font) : null);
}
/** Condition icon and temperature, baked to a raster so the tray keeps one stable image. */
function buildWeatherTrayIcon(reading: WeatherReading, font: UiFont): GrayImage {
const image = new GrayImage(Math.ceil(measureWeatherReading(reading, font, TRAY_ICON_SIZE)), TRAY_HEIGHT, 0);
drawWeatherReading(image, reading, font, TRAY_ICON_SIZE, 0, 0, TRAY_HEIGHT, 220);
return image.withDrawsBaked();
}
+24
View File
@@ -0,0 +1,24 @@
import { ConfigSettingBoolean } from "../../ui/dashboard-settings";
/**
* Where current conditions show outside the Weather app. While either is on
* and the glasses are connected, the weather bridge refreshes hourly (see
* weather-indicators.ts).
*/
export const weatherShowInStatusBarSetting = new ConfigSettingBoolean({
id: "weather-show-in-status-bar",
label: "Show in status bar",
storageKey: "weather.showInStatusBar",
defaultValue: false,
description:
"Show the current conditions and temperature in the top bar. Weather for your current location is fetched once an hour while the glasses are connected.",
});
export const weatherShowOnSystemCardSetting = new ConfigSettingBoolean({
id: "weather-show-on-system-card",
label: "Show on system card",
storageKey: "weather.showOnSystemCard",
defaultValue: false,
description:
"Show the current conditions and temperature on the Glanceboard's system card. Weather for your current location is fetched once an hour while the glasses are connected.",
});
+30 -7
View File
@@ -6,11 +6,18 @@ 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 { renderWeatherIcon } from "./weather-icons";
const PAGE_X = 18;
const HEADER_Y = 8;
const CURRENT_TOP = 34;
/** Minimum x of the description column right of the temperature. */
const CURRENT_TEXT_X = 112;
/** The current-conditions icon, left of the temperature. */
const CURRENT_ICON_SIZE = 40;
const CURRENT_ICON_GAP = 10;
/** Gap between the temperature and the description column. */
const CURRENT_TEXT_GAP = 16;
const FORECAST_HEADER_Y = 105;
const FORECAST_TOP = 124;
/** Forecast rows hold a 20px icon; grow with the font past that. */
@@ -70,7 +77,8 @@ export class WeatherLayer implements Layer {
);
return image;
}
if (weather.phase === "locating" || weather.phase === "loading") {
// A refresh with earlier data in hand keeps showing that data.
if ((weather.phase === "locating" || weather.phase === "loading") && !weather.current) {
this.drawMessage(image, font, width, height, weather.status);
return image;
}
@@ -94,7 +102,7 @@ export class WeatherLayer implements Layer {
}
return;
}
if (!weather.forecast.length || weather.phase !== "ready") return;
if (!weather.forecast.length || !showsForecast(weather)) return;
if (event.type === "scroll-up" || event.type === "scroll-down") {
this.forecastMenu.setItems(weather.forecast);
void this.forecastMenu.handleInput(event);
@@ -108,16 +116,24 @@ export class WeatherLayer implements Layer {
const medium = getDefaultMediumFont();
const small = getDefaultSmallFont();
const temperature = current.temperatureF === null ? "--°" : `${Math.round(current.temperatureF)}°F`;
image.drawText(large, PAGE_X, CURRENT_TOP + 8, temperature, 245);
const temperatureY = CURRENT_TOP + 8;
let temperatureX = PAGE_X;
if (current.condition) {
const icon = renderWeatherIcon(current.condition, current.isDaytime, CURRENT_ICON_SIZE);
if (icon) image.drawImage(icon, PAGE_X, temperatureY + (((large.lineHeight - icon.height) / 2) | 0));
temperatureX += CURRENT_ICON_SIZE + CURRENT_ICON_GAP;
}
image.drawText(large, temperatureX, temperatureY, temperature, 245);
const textX = Math.max(CURRENT_TEXT_X, temperatureX + Math.ceil(large.measureText(temperature)) + CURRENT_TEXT_GAP);
// Description / details / source stack compactly from just below the
// header line, so at large font sizes they stay clear of the forecast
// table instead of running into it.
let cy = 30;
image.drawText(
medium,
CURRENT_TEXT_X,
textX,
cy,
truncateText(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
truncateText(medium, current.description || "Current conditions", width - textX - PAGE_X),
220,
);
cy += medium.lineHeight + 2;
@@ -127,11 +143,12 @@ export class WeatherLayer implements Layer {
if (current.windSpeedMph !== null) {
details.push(`Wind ${current.windDirection ? `${current.windDirection} ` : ""}${Math.round(current.windSpeedMph)} mph`);
}
image.drawText(small, CURRENT_TEXT_X, cy, details.join(" ") || "Current forecast", 170);
image.drawText(small, textX, cy, details.join(" ") || "Current forecast", 170);
cy += lineStep(small);
const source = current.observed ? "Observed" : "Forecast";
const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : "";
image.drawText(small, CURRENT_TEXT_X, cy, `${source}${age ? ` ${age}` : ""}`, 105);
const updating = weather.phase === "locating" || weather.phase === "loading" ? " · updating" : "";
image.drawText(small, textX, cy, `${source}${age ? ` ${age}` : ""}${updating}`, 105);
image.drawLine(PAGE_X, FORECAST_HEADER_Y - 7, width - PAGE_X, FORECAST_HEADER_Y - 7, 40);
}
@@ -216,6 +233,12 @@ export class WeatherLayer implements Layer {
}
}
/** Whether paint shows the forecast table (ready, or refreshing over earlier data). */
function showsForecast(weather: WeatherState): boolean {
if (weather.phase === "ready") return true;
return (weather.phase === "locating" || weather.phase === "loading") && weather.current !== null;
}
/** Forecast row pitch, including the gap between selection boxes. */
function forecastRowHeight(font: UiFont): number {
return Math.max(FORECAST_MIN_ROW_HEIGHT, tightRowHeight(font) + 3);