forked from Blendman974/faceclaw
Add Timer, Weather apps; build out UI of music app; add wakeword action setting
This commit is contained in:
1 parent
008f2277ca
commit
5c73ba2a4d
27 files changed
+2181
-312
No files matched your search
@@ -3,9 +3,11 @@ import { Application, Utils } from "@nativescript/core";
|
||||
const PERMISSION_REQUEST_CODE = 4242;
|
||||
const VOICE_PERMISSION_REQUEST_CODE = 4243;
|
||||
const CALENDAR_PERMISSION_REQUEST_CODE = 4244;
|
||||
const LOCATION_PERMISSION_REQUEST_CODE = 4245;
|
||||
const POST_NOTIFICATIONS_PERMISSION = "android.permission.POST_NOTIFICATIONS";
|
||||
const RECORD_AUDIO_PERMISSION = "android.permission.RECORD_AUDIO";
|
||||
const READ_CALENDAR_PERMISSION = "android.permission.READ_CALENDAR";
|
||||
const ACCESS_COARSE_LOCATION_PERMISSION = "android.permission.ACCESS_COARSE_LOCATION";
|
||||
|
||||
function getActivity(): androidx.appcompat.app.AppCompatActivity {
|
||||
const activity = Application.android.foregroundActivity ?? Application.android.startActivity;
|
||||
@@ -124,3 +126,26 @@ export async function ensureCalendarPermission(): Promise<boolean> {
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether approximate foreground location access has been granted. */
|
||||
export function hasLocationPermission(): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
return isPermissionGranted(ACCESS_COARSE_LOCATION_PERMISSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prompt for approximate foreground location access. Weather only needs a
|
||||
* coarse coordinate, which avoids asking for more precise access than needed.
|
||||
*/
|
||||
export async function ensureLocationPermission(): Promise<boolean> {
|
||||
if (!global.isAndroid) return false;
|
||||
try {
|
||||
await ensurePermissions(
|
||||
[ACCESS_COARSE_LOCATION_PERMISSION],
|
||||
LOCATION_PERMISSION_REQUEST_CODE,
|
||||
"Location",
|
||||
);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,11 @@ import {
|
||||
CALENDAR_SURFACE_ID,
|
||||
CALENDAR_WINDOW_ID,
|
||||
} from "../ui/shell/calendar-app";
|
||||
import {
|
||||
createWeatherAppWindow,
|
||||
WEATHER_SURFACE_ID,
|
||||
WEATHER_WINDOW_ID,
|
||||
} from "../ui/shell/weather-app";
|
||||
import {
|
||||
createDebugTestsAppWindow,
|
||||
DEBUG_TESTS_SURFACE_ID,
|
||||
@@ -229,7 +234,7 @@ class DashboardController {
|
||||
requestRender: () => shell.foregroundWindow()?.requestRender(),
|
||||
},
|
||||
apps: [
|
||||
{ appId: "stopwatch", label: "Stopwatch", icon: "timer" },
|
||||
{ appId: "timer", label: "Timer", icon: "timer" },
|
||||
{ appId: "terminal", label: "Terminal", icon: "terminal" },
|
||||
{ appId: "teleprompt", label: "Teleprompt", icon: "file-text" },
|
||||
{ appId: "music", label: "Music", icon: "music" },
|
||||
@@ -237,6 +242,7 @@ class DashboardController {
|
||||
{ appId: "transcribe", label: "Transcribe", icon: "mic" },
|
||||
{ appId: "notifications", label: "Notifications", icon: "bell" },
|
||||
{ appId: "calendar", label: "Calendar", icon: "calendar" },
|
||||
{ appId: "weather", label: "Weather", icon: "cloud-sun" },
|
||||
{ appId: "debug-tests", label: "Debug tests", icon: "flask-conical" },
|
||||
{ appId: "settings", label: "Settings", icon: "settings" },
|
||||
],
|
||||
@@ -854,8 +860,8 @@ class DashboardController {
|
||||
if (existing) return existing;
|
||||
// Worker paths must be string literals for the webpack worker loader.
|
||||
let worker: Worker;
|
||||
if (appId === "stopwatch") {
|
||||
worker = new Worker("../workers/stopwatch-app.worker");
|
||||
if (appId === "timer") {
|
||||
worker = new Worker("../workers/timer-app.worker");
|
||||
} else if (appId === "terminal") {
|
||||
worker = new Worker("../workers/terminal-app.worker");
|
||||
} else {
|
||||
@@ -898,6 +904,10 @@ class DashboardController {
|
||||
await this.launchInProcessApp(CALENDAR_WINDOW_ID, CALENDAR_SURFACE_ID, createCalendarAppWindow);
|
||||
return;
|
||||
}
|
||||
if (appId === "weather") {
|
||||
await this.launchInProcessApp(WEATHER_WINDOW_ID, WEATHER_SURFACE_ID, createWeatherAppWindow);
|
||||
return;
|
||||
}
|
||||
if (appId === "debug-tests") {
|
||||
await this.launchInProcessApp(DEBUG_TESTS_WINDOW_ID, DEBUG_TESTS_SURFACE_ID, createDebugTestsAppWindow);
|
||||
return;
|
||||
@@ -945,10 +955,17 @@ class DashboardController {
|
||||
this.appendLog("launched terminal:hub");
|
||||
return;
|
||||
}
|
||||
const serial = this.nextWindowSerial++;
|
||||
const windowId = `${appId}:${serial}`;
|
||||
host.openWindow({ windowId, title: `Stopwatch ${serial}`, iconLetter: "S", icon: "timer", focus: true });
|
||||
this.appendLog(`launched ${windowId}`);
|
||||
if (appId === "timer") {
|
||||
const existing = shell.getWindows().find((window) => window.appId === "timer");
|
||||
if (existing) {
|
||||
shell.focusWindow(existing.windowId);
|
||||
this.requestShellRender();
|
||||
return;
|
||||
}
|
||||
host.openWindow({ windowId: "timer:main", title: "Timer", iconLetter: "T", icon: "timer", focus: true });
|
||||
this.appendLog("launched timer:main");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/** Create/refresh a window surface on the compositor, if connected. */
|
||||
|
||||
@@ -34,6 +34,8 @@ export const ICON_SVGS = {
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M10.268 21a2 2 0 0 0 3.464 0"/><path d="M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326"/></svg>',
|
||||
calendar:
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/></svg>',
|
||||
"cloud-sun":
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M12 2v2"/><path d="m4.93 4.93 1.42 1.42"/><path d="M20 12h2"/><path d="m19.07 4.93-1.42 1.42"/><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>',
|
||||
"flask-conical":
|
||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M14 2v6a2 2 0 0 0 .245.96l5.51 10.08A2 2 0 0 1 18 22H6a2 2 0 0 1-1.755-2.96l5.51-10.08A2 2 0 0 0 10 8V2"/><path d="M6.453 15h11.094"/><path d="M8.5 2h7"/></svg>',
|
||||
settings:
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
export type CurrentLocation = {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
accuracyMeters: number | null;
|
||||
timestampMs: number;
|
||||
};
|
||||
|
||||
/** Resolve one current foreground location using Android's LocationManager. */
|
||||
export function getCurrentLocation(): Promise<CurrentLocation> {
|
||||
if (!global.isAndroid) {
|
||||
return Promise.reject(new Error("Weather location is only available on Android."));
|
||||
}
|
||||
const context = Utils.android.getApplicationContext();
|
||||
if (!context) {
|
||||
return Promise.reject(new Error("Android application context unavailable."));
|
||||
}
|
||||
|
||||
return new Promise<CurrentLocation>((resolve, reject) => {
|
||||
const provider = new com.faceclaw.app.FaceclawLocationProvider(context);
|
||||
let settled = false;
|
||||
const listener = new com.faceclaw.app.FaceclawLocationListener({
|
||||
onLocation: (
|
||||
latitude: number,
|
||||
longitude: number,
|
||||
accuracyMeters: number,
|
||||
timestampMs: number,
|
||||
) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
provider.setListener(null);
|
||||
resolve({
|
||||
latitude: Number(latitude),
|
||||
longitude: Number(longitude),
|
||||
accuracyMeters: Number(accuracyMeters) >= 0 ? Number(accuracyMeters) : null,
|
||||
timestampMs: Number(timestampMs),
|
||||
});
|
||||
},
|
||||
onError: (message: string) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
provider.setListener(null);
|
||||
reject(new Error(String(message)));
|
||||
},
|
||||
});
|
||||
// Retain the proxy through the Java provider until one terminal callback.
|
||||
provider.setListener(listener);
|
||||
provider.start();
|
||||
});
|
||||
}
|
||||
@@ -23,10 +23,14 @@ export type MediaControllerState = {
|
||||
available: boolean;
|
||||
accessEnabled: boolean;
|
||||
packageName: string;
|
||||
appName: string;
|
||||
playbackState: MediaPlaybackState;
|
||||
title: string;
|
||||
artist: string;
|
||||
album: string;
|
||||
positionMs: number;
|
||||
durationMs: number;
|
||||
playbackSpeed: number;
|
||||
status: string;
|
||||
canPlayPause: boolean;
|
||||
canSkipNext: boolean;
|
||||
@@ -37,10 +41,14 @@ const DEFAULT_MEDIA_STATE: MediaControllerState = {
|
||||
available: false,
|
||||
accessEnabled: false,
|
||||
packageName: "",
|
||||
appName: "",
|
||||
playbackState: "idle",
|
||||
title: "",
|
||||
artist: "",
|
||||
album: "",
|
||||
positionMs: -1,
|
||||
durationMs: -1,
|
||||
playbackSpeed: 0,
|
||||
status: "Media controller unavailable.",
|
||||
canPlayPause: false,
|
||||
canSkipNext: false,
|
||||
@@ -52,6 +60,7 @@ export class FaceclawMediaControllerBridge {
|
||||
private controller: any | null = null;
|
||||
private listenerProxy: any | null = null;
|
||||
private state: MediaControllerState = { ...DEFAULT_MEDIA_STATE };
|
||||
private stateUpdatedAtMs = Date.now();
|
||||
private started = false;
|
||||
|
||||
onStateChange(listener: (state: MediaControllerState) => void): () => void {
|
||||
@@ -61,7 +70,12 @@ export class FaceclawMediaControllerBridge {
|
||||
}
|
||||
|
||||
snapshot(): MediaControllerState {
|
||||
return { ...this.state };
|
||||
const state = { ...this.state };
|
||||
if (state.playbackState === "playing" && state.positionMs >= 0 && state.playbackSpeed !== 0) {
|
||||
state.positionMs += (Date.now() - this.stateUpdatedAtMs) * state.playbackSpeed;
|
||||
state.positionMs = Math.max(0, state.durationMs > 0 ? Math.min(state.positionMs, state.durationMs) : state.positionMs);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
async start(): Promise<void> {
|
||||
@@ -103,6 +117,21 @@ export class FaceclawMediaControllerBridge {
|
||||
this.controller?.skipToQueueItem(id);
|
||||
}
|
||||
|
||||
/** Current Android music-stream volume normalized to 0..100. */
|
||||
getMediaVolumePercent(): number {
|
||||
if (!global.isAndroid) return -1;
|
||||
this.ensureController();
|
||||
if (!this.controller) return -1;
|
||||
return Math.max(-1, Math.min(100, Math.round(Number(this.controller.getMediaVolumePercent()))));
|
||||
}
|
||||
|
||||
/** Set Android's music-stream volume from a normalized 0..100 value. */
|
||||
setMediaVolumePercent(volumePercent: number): void {
|
||||
if (!global.isAndroid) return;
|
||||
this.ensureController();
|
||||
this.controller?.setMediaVolumePercent(Math.max(0, Math.min(100, Math.round(volumePercent))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Album art for the current item, grayscale, scaled to fit maxSize; null
|
||||
* when the player provides none.
|
||||
@@ -152,9 +181,13 @@ export class FaceclawMediaControllerBridge {
|
||||
onStateChange: (
|
||||
playbackState: string,
|
||||
packageName: string,
|
||||
appName: string,
|
||||
title: string,
|
||||
artist: string,
|
||||
album: string,
|
||||
positionMs: number,
|
||||
durationMs: number,
|
||||
playbackSpeed: number,
|
||||
canPlayPause: boolean,
|
||||
canSkipNext: boolean,
|
||||
canSkipPrevious: boolean,
|
||||
@@ -165,15 +198,20 @@ export class FaceclawMediaControllerBridge {
|
||||
available: Boolean(title || artist || album || packageName) || String(playbackState) !== "idle",
|
||||
accessEnabled: Boolean(accessEnabled),
|
||||
packageName: String(packageName),
|
||||
appName: String(appName),
|
||||
playbackState: normalizePlaybackState(String(playbackState)),
|
||||
title: String(title),
|
||||
artist: String(artist),
|
||||
album: String(album),
|
||||
positionMs: Number(positionMs),
|
||||
durationMs: Number(durationMs),
|
||||
playbackSpeed: Number(playbackSpeed),
|
||||
status: String(status),
|
||||
canPlayPause: Boolean(canPlayPause),
|
||||
canSkipNext: Boolean(canSkipNext),
|
||||
canSkipPrevious: Boolean(canSkipPrevious),
|
||||
};
|
||||
this.stateUpdatedAtMs = Date.now();
|
||||
for (const listener of this.stateListeners) {
|
||||
listener(this.snapshot());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const com: any;
|
||||
|
||||
function getContext(): android.content.Context | null {
|
||||
if (!global.isAndroid) return null;
|
||||
return Utils.android.getApplicationContext() ?? null;
|
||||
}
|
||||
|
||||
/** Schedule the durable Android alarm backing one on-glasses timer. */
|
||||
export function scheduleTimerNotification(timerId: number, triggerAtMs: number, durationLabel: string): void {
|
||||
const context = getContext();
|
||||
if (!context) return;
|
||||
com.faceclaw.app.FaceclawTimerNotifications.schedule(context, timerId, triggerAtMs, durationLabel);
|
||||
}
|
||||
|
||||
/** Cancel both a pending alarm and any already-posted notification. */
|
||||
export function cancelTimerNotification(timerId: number): void {
|
||||
const context = getContext();
|
||||
if (!context) return;
|
||||
com.faceclaw.app.FaceclawTimerNotifications.cancel(context, timerId);
|
||||
}
|
||||
|
||||
/** Prompt worker-side expiry path; Java deduplicates it against the alarm. */
|
||||
export function fireTimerNotification(timerId: number, durationLabel: string): void {
|
||||
const context = getContext();
|
||||
if (!context) return;
|
||||
com.faceclaw.app.FaceclawTimerNotifications.fireNow(context, timerId, durationLabel);
|
||||
}
|
||||
@@ -0,0 +1,369 @@
|
||||
import { hasLocationPermission } from "../g2/android-permissions";
|
||||
import { getCurrentLocation, type CurrentLocation } from "./location";
|
||||
|
||||
export type WeatherPhase = "permission-required" | "locating" | "loading" | "ready" | "error";
|
||||
|
||||
export type CurrentWeather = {
|
||||
temperatureF: number | null;
|
||||
description: string;
|
||||
humidityPercent: number | null;
|
||||
windSpeedMph: number | null;
|
||||
windDirection: string;
|
||||
timestampMs: number | null;
|
||||
observed: boolean;
|
||||
};
|
||||
|
||||
export type ForecastPeriod = {
|
||||
name: string;
|
||||
startTimeMs: number;
|
||||
temperatureF: number | null;
|
||||
shortForecast: string;
|
||||
detailedForecast: string;
|
||||
precipitationPercent: number | null;
|
||||
windSpeed: string;
|
||||
windDirection: string;
|
||||
isDaytime: boolean;
|
||||
};
|
||||
|
||||
export type WeatherState = {
|
||||
phase: WeatherPhase;
|
||||
status: string;
|
||||
locationName: string;
|
||||
current: CurrentWeather | null;
|
||||
forecast: ForecastPeriod[];
|
||||
lastUpdatedMs: number | null;
|
||||
};
|
||||
|
||||
type NwsPointResponse = {
|
||||
properties?: {
|
||||
forecast?: unknown;
|
||||
forecastHourly?: unknown;
|
||||
observationStations?: unknown;
|
||||
relativeLocation?: {
|
||||
properties?: { city?: unknown; state?: unknown };
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
type NwsForecastResponse = {
|
||||
properties?: {
|
||||
periods?: NwsForecastPeriodResponse[];
|
||||
};
|
||||
};
|
||||
|
||||
type NwsForecastPeriodResponse = {
|
||||
name?: unknown;
|
||||
startTime?: unknown;
|
||||
temperature?: unknown;
|
||||
temperatureUnit?: unknown;
|
||||
shortForecast?: unknown;
|
||||
detailedForecast?: unknown;
|
||||
probabilityOfPrecipitation?: { value?: unknown };
|
||||
windSpeed?: unknown;
|
||||
windDirection?: unknown;
|
||||
isDaytime?: unknown;
|
||||
};
|
||||
|
||||
type NwsStationsResponse = {
|
||||
features?: Array<{ id?: unknown }>;
|
||||
};
|
||||
|
||||
type NwsObservationResponse = {
|
||||
properties?: {
|
||||
timestamp?: unknown;
|
||||
textDescription?: unknown;
|
||||
temperature?: NwsMeasure;
|
||||
relativeHumidity?: NwsMeasure;
|
||||
windSpeed?: NwsMeasure;
|
||||
windDirection?: NwsMeasure;
|
||||
};
|
||||
};
|
||||
|
||||
type NwsMeasure = {
|
||||
value?: unknown;
|
||||
unitCode?: unknown;
|
||||
};
|
||||
|
||||
const NWS_API_ROOT = "https://api.weather.gov";
|
||||
const NWS_HEADERS = {
|
||||
Accept: "application/geo+json",
|
||||
"User-Agent": "Faceclaw/1.0 (https://github.com/jimrandomh/faceclaw)",
|
||||
};
|
||||
const WEATHER_REFRESH_MS = 30 * 60 * 1000;
|
||||
const FETCH_TIMEOUT_MS = 20_000;
|
||||
const MAX_FORECAST_PERIODS = 14;
|
||||
|
||||
const DEFAULT_STATE: WeatherState = {
|
||||
phase: "permission-required",
|
||||
status: "Location permission is required for local weather.",
|
||||
locationName: "",
|
||||
current: null,
|
||||
forecast: [],
|
||||
lastUpdatedMs: null,
|
||||
};
|
||||
|
||||
/** Shared weather state and periodic refresh, active only while its app is open. */
|
||||
export class WeatherBridge {
|
||||
private readonly listeners = new Set<(state: WeatherState) => void>();
|
||||
private state: WeatherState = cloneState(DEFAULT_STATE);
|
||||
private refreshHandle: ReturnType<typeof setInterval> | null = null;
|
||||
private refreshInFlight: Promise<void> | null = null;
|
||||
|
||||
onStateChange(listener: (state: WeatherState) => void): () => void {
|
||||
this.listeners.add(listener);
|
||||
listener(this.snapshot());
|
||||
return () => this.listeners.delete(listener);
|
||||
}
|
||||
|
||||
snapshot(): WeatherState {
|
||||
return cloneState(this.state);
|
||||
}
|
||||
|
||||
start(): void {
|
||||
if (!this.refreshHandle) {
|
||||
this.refreshHandle = setInterval(() => void this.refreshNow(), WEATHER_REFRESH_MS);
|
||||
}
|
||||
void this.refreshNow();
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
if (this.refreshHandle) {
|
||||
clearInterval(this.refreshHandle);
|
||||
this.refreshHandle = null;
|
||||
}
|
||||
}
|
||||
|
||||
async refreshNow(): Promise<void> {
|
||||
if (this.refreshInFlight) return this.refreshInFlight;
|
||||
if (!hasLocationPermission()) {
|
||||
this.state = cloneState(DEFAULT_STATE);
|
||||
this.emit();
|
||||
return;
|
||||
}
|
||||
|
||||
this.refreshInFlight = this.refresh();
|
||||
try {
|
||||
await this.refreshInFlight;
|
||||
} finally {
|
||||
this.refreshInFlight = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async refresh(): Promise<void> {
|
||||
try {
|
||||
this.state = { ...this.state, phase: "locating", status: "Getting current location..." };
|
||||
this.emit();
|
||||
const location = await getCurrentLocation();
|
||||
|
||||
this.state = { ...this.state, phase: "loading", status: "Loading National Weather Service data..." };
|
||||
this.emit();
|
||||
const weather = await loadNwsWeather(location);
|
||||
this.state = {
|
||||
phase: "ready",
|
||||
status: "Weather updated.",
|
||||
locationName: weather.locationName,
|
||||
current: weather.current,
|
||||
forecast: weather.forecast,
|
||||
lastUpdatedMs: Date.now(),
|
||||
};
|
||||
this.emit();
|
||||
} catch (error) {
|
||||
const message = friendlyWeatherError(error);
|
||||
console.warn(`weather refresh failed: ${message}`);
|
||||
this.state = {
|
||||
...this.state,
|
||||
phase: "error",
|
||||
status: message,
|
||||
};
|
||||
this.emit();
|
||||
}
|
||||
}
|
||||
|
||||
private emit(): void {
|
||||
const snapshot = this.snapshot();
|
||||
for (const listener of this.listeners) listener(snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadNwsWeather(location: CurrentLocation): Promise<{
|
||||
locationName: string;
|
||||
current: CurrentWeather;
|
||||
forecast: ForecastPeriod[];
|
||||
}> {
|
||||
// NWS recommends no more than four decimals for its point lookup.
|
||||
const latitude = location.latitude.toFixed(4);
|
||||
const longitude = location.longitude.toFixed(4);
|
||||
const point = await fetchNwsJson<NwsPointResponse>(`${NWS_API_ROOT}/points/${latitude},${longitude}`);
|
||||
const properties = point.properties;
|
||||
const forecastUrl = stringValue(properties?.forecast);
|
||||
const hourlyUrl = stringValue(properties?.forecastHourly);
|
||||
const stationsUrl = stringValue(properties?.observationStations);
|
||||
if (!forecastUrl) throw new Error("NWS did not provide a forecast for this location.");
|
||||
|
||||
const [forecastResponse, hourlyResponse, observation] = await Promise.all([
|
||||
fetchNwsJson<NwsForecastResponse>(forecastUrl),
|
||||
hourlyUrl ? fetchNwsJson<NwsForecastResponse>(hourlyUrl).catch(() => null) : Promise.resolve(null),
|
||||
stationsUrl ? loadLatestObservation(stationsUrl).catch(() => null) : Promise.resolve(null),
|
||||
]);
|
||||
const forecast = (forecastResponse.properties?.periods ?? [])
|
||||
.map(normalizeForecastPeriod)
|
||||
.filter((period): period is ForecastPeriod => period !== null)
|
||||
.slice(0, MAX_FORECAST_PERIODS);
|
||||
if (!forecast.length) throw new Error("NWS returned an empty forecast.");
|
||||
|
||||
const hourly = (hourlyResponse?.properties?.periods ?? [])
|
||||
.map(normalizeForecastPeriod)
|
||||
.find((period): period is ForecastPeriod => period !== null);
|
||||
|
||||
return {
|
||||
locationName: pointLocationName(point, latitude, longitude),
|
||||
current: observation ? normalizeObservation(observation, hourly) : currentFromHourly(hourly, forecast[0]!),
|
||||
forecast,
|
||||
};
|
||||
}
|
||||
|
||||
async function loadLatestObservation(stationsUrl: string): Promise<NwsObservationResponse | null> {
|
||||
const stations = await fetchNwsJson<NwsStationsResponse>(stationsUrl);
|
||||
const stationUrl = stringValue(stations.features?.[0]?.id);
|
||||
if (!stationUrl) return null;
|
||||
return fetchNwsJson<NwsObservationResponse>(`${stationUrl}/observations/latest`);
|
||||
}
|
||||
|
||||
async function fetchNwsJson<T>(url: string): Promise<T> {
|
||||
const request = fetch(url, { headers: NWS_HEADERS });
|
||||
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
|
||||
const timeout = new Promise<never>((_resolve, reject) => {
|
||||
timeoutHandle = setTimeout(() => reject(new Error("Weather request timed out.")), FETCH_TIMEOUT_MS);
|
||||
});
|
||||
try {
|
||||
const response = await Promise.race([request, timeout]);
|
||||
if (!response.ok) {
|
||||
if (response.status === 404 && url.includes("/points/")) {
|
||||
throw new Error("This location is outside National Weather Service coverage.");
|
||||
}
|
||||
throw new Error(`National Weather Service request failed (HTTP ${response.status}).`);
|
||||
}
|
||||
return (await response.json()) as T;
|
||||
} finally {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle);
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeObservation(
|
||||
response: NwsObservationResponse,
|
||||
hourly: ForecastPeriod | undefined,
|
||||
): CurrentWeather {
|
||||
const properties = response.properties;
|
||||
const temperatureF = convertTemperatureToF(properties?.temperature);
|
||||
const windSpeedMph = convertSpeedToMph(properties?.windSpeed);
|
||||
const windDegrees = finiteNumber(properties?.windDirection?.value);
|
||||
return {
|
||||
temperatureF: temperatureF ?? hourly?.temperatureF ?? null,
|
||||
description: stringValue(properties?.textDescription) || hourly?.shortForecast || "Current conditions",
|
||||
humidityPercent: finiteNumber(properties?.relativeHumidity?.value),
|
||||
windSpeedMph,
|
||||
windDirection: windDegrees === null ? hourly?.windDirection ?? "" : degreesToCompass(windDegrees),
|
||||
timestampMs: timestampValue(properties?.timestamp),
|
||||
observed: true,
|
||||
};
|
||||
}
|
||||
|
||||
function currentFromHourly(hourly: ForecastPeriod | undefined, fallback: ForecastPeriod): CurrentWeather {
|
||||
const source = hourly ?? fallback;
|
||||
return {
|
||||
temperatureF: source.temperatureF,
|
||||
description: source.shortForecast || "Current forecast",
|
||||
humidityPercent: null,
|
||||
windSpeedMph: firstNumberInText(source.windSpeed),
|
||||
windDirection: source.windDirection,
|
||||
timestampMs: source.startTimeMs || null,
|
||||
observed: false,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeForecastPeriod(value: NwsForecastPeriodResponse): ForecastPeriod | null {
|
||||
if (!value || typeof value !== "object") return null;
|
||||
const name = stringValue(value.name);
|
||||
const temperature = finiteNumber(value.temperature);
|
||||
const unit = stringValue(value.temperatureUnit).toUpperCase();
|
||||
return {
|
||||
name: name || "Forecast",
|
||||
startTimeMs: timestampValue(value.startTime) ?? 0,
|
||||
temperatureF: temperature === null ? null : unit === "C" ? temperature * 9 / 5 + 32 : temperature,
|
||||
shortForecast: stringValue(value.shortForecast),
|
||||
detailedForecast: stringValue(value.detailedForecast),
|
||||
precipitationPercent: finiteNumber(value.probabilityOfPrecipitation?.value),
|
||||
windSpeed: stringValue(value.windSpeed),
|
||||
windDirection: stringValue(value.windDirection),
|
||||
isDaytime: Boolean(value.isDaytime),
|
||||
};
|
||||
}
|
||||
|
||||
function convertTemperatureToF(measure: NwsMeasure | undefined): number | null {
|
||||
const value = finiteNumber(measure?.value);
|
||||
if (value === null) return null;
|
||||
const unit = stringValue(measure?.unitCode).toLowerCase();
|
||||
if (unit.includes("degc")) return value * 9 / 5 + 32;
|
||||
return value;
|
||||
}
|
||||
|
||||
function convertSpeedToMph(measure: NwsMeasure | undefined): number | null {
|
||||
const value = finiteNumber(measure?.value);
|
||||
if (value === null) return null;
|
||||
const unit = stringValue(measure?.unitCode).toLowerCase();
|
||||
if (unit.includes("km_h")) return value * 0.621371;
|
||||
if (unit.includes("m_s")) return value * 2.23694;
|
||||
return value;
|
||||
}
|
||||
|
||||
function pointLocationName(point: NwsPointResponse, latitude: string, longitude: string): string {
|
||||
const relative = point.properties?.relativeLocation?.properties;
|
||||
const city = stringValue(relative?.city);
|
||||
const state = stringValue(relative?.state);
|
||||
return [city, state].filter(Boolean).join(", ") || `${latitude}, ${longitude}`;
|
||||
}
|
||||
|
||||
function degreesToCompass(value: number): string {
|
||||
const directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||
return directions[Math.round((((value % 360) + 360) % 360) / 45) % directions.length]!;
|
||||
}
|
||||
|
||||
function firstNumberInText(value: string): number | null {
|
||||
const match = value.match(/\d+(?:\.\d+)?/);
|
||||
return match ? Number(match[0]) : null;
|
||||
}
|
||||
|
||||
function finiteNumber(value: unknown): number | null {
|
||||
if (value === null || value === undefined || typeof value === "boolean") return null;
|
||||
if (typeof value === "string" && !value.trim()) return null;
|
||||
const number = typeof value === "number" ? value : Number(value);
|
||||
return Number.isFinite(number) ? number : null;
|
||||
}
|
||||
|
||||
function timestampValue(value: unknown): number | null {
|
||||
if (typeof value !== "string") return null;
|
||||
const timestamp = Date.parse(value);
|
||||
return Number.isFinite(timestamp) ? timestamp : null;
|
||||
}
|
||||
|
||||
function stringValue(value: unknown): string {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
function friendlyWeatherError(error: unknown): string {
|
||||
const message = (error as Error)?.message || String(error);
|
||||
if (/network request failed|failed to fetch|unable to resolve host/i.test(message)) {
|
||||
return "Couldn't reach the National Weather Service. Check the phone's connection and retry.";
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
function cloneState(state: WeatherState): WeatherState {
|
||||
return {
|
||||
...state,
|
||||
current: state.current ? { ...state.current } : null,
|
||||
forecast: state.forecast.map((period) => ({ ...period })),
|
||||
};
|
||||
}
|
||||
|
||||
export const weatherBridge = new WeatherBridge();
|
||||
@@ -20,14 +20,14 @@ export class TranscribeLayer implements Layer {
|
||||
private unsubscribeTranscript: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
|
||||
start(ctx: LayerContext): void {
|
||||
start(requestRender: () => void): void {
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
|
||||
this.onTranscript(event);
|
||||
ctx.actions.requestRender();
|
||||
requestRender();
|
||||
});
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
ctx.actions.requestRender();
|
||||
requestRender();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,202 @@
|
||||
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { type ForecastPeriod, type WeatherState } from "../../native/weather";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
|
||||
import { type DashboardInputEvent, type Layer, type LayerContext } from "../layers";
|
||||
import { drawSelectionHighlight } from "../menu";
|
||||
|
||||
const PAGE_X = 18;
|
||||
const HEADER_Y = 8;
|
||||
const CURRENT_TOP = 34;
|
||||
const CURRENT_TEXT_X = 112;
|
||||
const FORECAST_HEADER_Y = 105;
|
||||
const FORECAST_TOP = 124;
|
||||
const FORECAST_ROW_HEIGHT = 23;
|
||||
const FOOTER_HEIGHT = 18;
|
||||
const FORECAST_NAME_WIDTH = 92;
|
||||
const FORECAST_TEMP_X = 126;
|
||||
const FORECAST_PRECIP_X = 184;
|
||||
const FORECAST_SUMMARY_X = 244;
|
||||
|
||||
/** Weather's current-conditions summary and scrollable 12-hour forecast. */
|
||||
export class WeatherLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
|
||||
constructor(
|
||||
private readonly state: () => WeatherState,
|
||||
private readonly requestUpdate: () => void,
|
||||
) {}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const weather = this.state();
|
||||
image.drawText(font, PAGE_X, HEADER_Y, "Weather", 225);
|
||||
if (weather.locationName) {
|
||||
image.drawText(
|
||||
font,
|
||||
width - PAGE_X - font.measureText(truncate(font, weather.locationName, 220)),
|
||||
HEADER_Y,
|
||||
truncate(font, weather.locationName, 220),
|
||||
150,
|
||||
);
|
||||
}
|
||||
|
||||
if (weather.phase === "permission-required") {
|
||||
this.drawMessage(
|
||||
image,
|
||||
font,
|
||||
width,
|
||||
height,
|
||||
"Allow approximate location on your phone to get local weather.",
|
||||
`${GESTURE_CLICK} request ${GESTURE_DOUBLE_CLICK} back`,
|
||||
);
|
||||
return image;
|
||||
}
|
||||
if (weather.phase === "locating" || weather.phase === "loading") {
|
||||
this.drawMessage(image, font, width, height, weather.status, `${GESTURE_DOUBLE_CLICK} back`);
|
||||
return image;
|
||||
}
|
||||
if (weather.phase === "error") {
|
||||
this.drawMessage(
|
||||
image,
|
||||
font,
|
||||
width,
|
||||
height,
|
||||
weather.status,
|
||||
`${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`,
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
this.drawCurrent(image, weather, width);
|
||||
this.drawForecast(image, weather.forecast, ctx, width, height);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent): void {
|
||||
const weather = this.state();
|
||||
if (event.type === "click") {
|
||||
this.requestUpdate();
|
||||
return;
|
||||
}
|
||||
if (!weather.forecast.length || weather.phase !== "ready") return;
|
||||
if (event.type === "scroll-up") {
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
} else if (event.type === "scroll-down") {
|
||||
this.selectedIndex = Math.min(weather.forecast.length - 1, this.selectedIndex + 1);
|
||||
}
|
||||
}
|
||||
|
||||
private drawCurrent(image: GrayImage, weather: WeatherState, width: number): void {
|
||||
const current = weather.current;
|
||||
if (!current) return;
|
||||
const large = getDefaultLargeFont();
|
||||
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);
|
||||
image.drawText(
|
||||
medium,
|
||||
CURRENT_TEXT_X,
|
||||
CURRENT_TOP + 5,
|
||||
truncate(medium, current.description || "Current conditions", width - CURRENT_TEXT_X - PAGE_X),
|
||||
220,
|
||||
);
|
||||
|
||||
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`);
|
||||
}
|
||||
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 35, details.join(" ") || "Current forecast", 170);
|
||||
const source = current.observed ? "Observed" : "Forecast";
|
||||
const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : "";
|
||||
image.drawText(small, CURRENT_TEXT_X, CURRENT_TOP + 54, `${source}${age ? ` ${age}` : ""}`, 105);
|
||||
image.drawLine(PAGE_X, FORECAST_HEADER_Y - 7, width - PAGE_X, FORECAST_HEADER_Y - 7, 40);
|
||||
}
|
||||
|
||||
private drawForecast(
|
||||
image: GrayImage,
|
||||
forecast: ForecastPeriod[],
|
||||
ctx: LayerContext,
|
||||
width: number,
|
||||
height: number,
|
||||
): void {
|
||||
const font = getDefaultSmallFont();
|
||||
if (!forecast.length) {
|
||||
image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160);
|
||||
return;
|
||||
}
|
||||
this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1);
|
||||
const visibleRows = Math.max(1, Math.floor((height - FOOTER_HEIGHT - FORECAST_TOP) / FORECAST_ROW_HEIGHT));
|
||||
if (this.selectedIndex < this.scrollRow) {
|
||||
this.scrollRow = this.selectedIndex;
|
||||
} else if (this.selectedIndex >= this.scrollRow + visibleRows) {
|
||||
this.scrollRow = this.selectedIndex - visibleRows + 1;
|
||||
}
|
||||
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, forecast.length - visibleRows));
|
||||
|
||||
image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150);
|
||||
image.drawText(font, FORECAST_TEMP_X, FORECAST_HEADER_Y, "Temp", 105);
|
||||
image.drawText(font, FORECAST_PRECIP_X, FORECAST_HEADER_Y, "Rain", 105);
|
||||
image.drawText(font, width - PAGE_X - font.measureText(`${this.selectedIndex + 1}/${forecast.length}`), FORECAST_HEADER_Y, `${this.selectedIndex + 1}/${forecast.length}`, 105);
|
||||
|
||||
const last = Math.min(forecast.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < last; index++) {
|
||||
const period = forecast[index]!;
|
||||
const y = FORECAST_TOP + (index - this.scrollRow) * FORECAST_ROW_HEIGHT;
|
||||
const selected = index === this.selectedIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, PAGE_X - 7, y - 2, width - 2 * (PAGE_X - 7), FORECAST_ROW_HEIGHT - 2, ctx.stack.isFocused(), 5);
|
||||
}
|
||||
const shade = selected ? 245 : 190;
|
||||
image.drawText(font, PAGE_X, y + 2, truncate(font, period.name, FORECAST_NAME_WIDTH), shade);
|
||||
image.drawText(font, FORECAST_TEMP_X, y + 2, period.temperatureF === null ? "--" : `${Math.round(period.temperatureF)}°F`, shade);
|
||||
image.drawText(font, FORECAST_PRECIP_X, y + 2, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155);
|
||||
image.drawText(font, FORECAST_SUMMARY_X, y + 2, truncate(font, period.shortForecast, width - FORECAST_SUMMARY_X - PAGE_X), selected ? 220 : 165);
|
||||
}
|
||||
|
||||
image.drawText(
|
||||
font,
|
||||
PAGE_X,
|
||||
height - 15,
|
||||
`${GESTURE_SCROLL} forecast ${GESTURE_CLICK} refresh ${GESTURE_DOUBLE_CLICK} back`,
|
||||
105,
|
||||
);
|
||||
}
|
||||
|
||||
private drawMessage(
|
||||
image: GrayImage,
|
||||
font: BdfFont,
|
||||
width: number,
|
||||
height: number,
|
||||
message: string,
|
||||
footer: string,
|
||||
): void {
|
||||
const lines = wrapText(font, message, width - 2 * (PAGE_X + 6));
|
||||
const top = Math.max(52, Math.round((height - lines.length * 15) / 2) - 8);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
image.drawText(font, PAGE_X + 6, top + index * 15, lines[index]!, 185);
|
||||
}
|
||||
image.drawText(font, PAGE_X, height - 20, footer, 110);
|
||||
}
|
||||
}
|
||||
|
||||
function formatAge(ageMs: number): string {
|
||||
const minutes = Math.max(0, Math.round(ageMs / 60_000));
|
||||
if (minutes < 1) return "just now";
|
||||
if (minutes < 60) return `${minutes}m ago`;
|
||||
return `${Math.round(minutes / 60)}h ago`;
|
||||
}
|
||||
|
||||
function truncate(font: BdfFont, text: string, maxWidth: number): string {
|
||||
if (font.measureText(text) <= maxWidth) return text;
|
||||
let out = text;
|
||||
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) out = out.slice(0, -1);
|
||||
return `${out}...`;
|
||||
}
|
||||
@@ -24,6 +24,7 @@ export type NightscoutSettings = {
|
||||
export type BatteryDisplayMode = "icon" | "percentage";
|
||||
export type TimeFormat = "24h" | "12h";
|
||||
export type ScreenTimeoutSetting = "15s" | "30s" | "1m" | "never";
|
||||
export type WakeWordAction = "voice-input" | "off" | "turn-screen-on";
|
||||
|
||||
type ConfigSettingOptions<TValue, TId extends string> = {
|
||||
id: TId;
|
||||
@@ -243,13 +244,28 @@ const voiceProviderLabels: Record<VoiceProvider, string> = {
|
||||
|
||||
export const voiceProviderSetting = new ConfigSettingEnum<VoiceProvider>({
|
||||
id: "voice-provider",
|
||||
label: "Provider",
|
||||
label: "Transcription Provider",
|
||||
storageKey: "voice.provider",
|
||||
defaultValue: "onboard",
|
||||
values: ["onboard", "elevenlabs", "whisper"],
|
||||
formatValue: (value) => voiceProviderLabels[value] ?? value,
|
||||
});
|
||||
|
||||
const wakeWordActionLabels: Record<WakeWordAction, string> = {
|
||||
"voice-input": "Voice Input",
|
||||
off: "Off",
|
||||
"turn-screen-on": "Turn Screen On",
|
||||
};
|
||||
|
||||
export const wakeWordActionSetting = new ConfigSettingEnum<WakeWordAction>({
|
||||
id: "wake-word-action",
|
||||
label: "Wakeword Action (\"Hey Even\")",
|
||||
storageKey: "voice.wakeWordAction",
|
||||
defaultValue: "voice-input",
|
||||
values: ["voice-input", "off", "turn-screen-on"],
|
||||
formatValue: (value) => wakeWordActionLabels[value] ?? value,
|
||||
});
|
||||
|
||||
export const saveVoiceRecordingsSetting = new ConfigSettingBoolean({
|
||||
id: "save-voice-recordings",
|
||||
label: "Save voice recordings",
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
nightscoutSiteUrlSetting,
|
||||
nightscoutApiTokenSetting,
|
||||
screenTimeoutSetting,
|
||||
wakeWordActionSetting,
|
||||
} from "../dashboard-settings";
|
||||
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel";
|
||||
|
||||
@@ -49,7 +50,10 @@ function settingsSections(): SettingsSection[] {
|
||||
},
|
||||
{
|
||||
label: "Voice",
|
||||
items: [enumSettingMenuItem(voiceProviderSetting)],
|
||||
items: [
|
||||
enumSettingMenuItem(wakeWordActionSetting),
|
||||
enumSettingMenuItem(voiceProviderSetting),
|
||||
],
|
||||
},
|
||||
{
|
||||
label: "API Keys",
|
||||
@@ -118,13 +122,13 @@ function renderAbout(args: { image: import("../../graphics/image").GrayImage; x:
|
||||
}
|
||||
const textX = logo ? x + logo.width + 12 : x;
|
||||
image.drawText(font, textX, y + 8, "Faceclaw", 220);
|
||||
image.drawText(font, textX, y + 24, "Dashboard prototype", 170);
|
||||
image.drawText(font, textX, y + 24, "v0.2.0", 170);
|
||||
image.drawTextWrapped({
|
||||
font,
|
||||
x,
|
||||
y: y + 64,
|
||||
width,
|
||||
text: "By James Babcock. Distributed under the GNU General Public License, version 3. Version 0.1.0. Too much of an early janky development prototype to have proper numbered releases.",
|
||||
text: "By James Babcock. Distributed under the GNU General Public License, version 3.",
|
||||
value: 170,
|
||||
});
|
||||
}
|
||||
+2
-2
@@ -13,8 +13,8 @@ export type DashboardInputEvent =
|
||||
/**
|
||||
* The on-glasses "Hey Even" wakeword fired. Delivered on sid 0x07 by the
|
||||
* stock firmware regardless of CFW; the CFW additionally suppresses the stock
|
||||
* Even AI app so this is ours to handle. Unlike every other input event this
|
||||
* one is honoured while the screen is off.
|
||||
* Even AI app so this is ours to handle. Its configured action may be applied
|
||||
* while the screen is off, unlike ordinary input events.
|
||||
*/
|
||||
| { type: "wakeword" }
|
||||
| { type: "unknown"; kind: string; eventSource: number; eventType: number };
|
||||
|
||||
@@ -181,6 +181,11 @@ class LauncherGridLayer implements Layer {
|
||||
* new window.
|
||||
*/
|
||||
export function createLauncherWindow(options: LauncherOptions): ShellWindow {
|
||||
const sortedOptions: LauncherOptions = {
|
||||
...options,
|
||||
apps: [...options.apps].sort((a, b) => a.label.localeCompare(b.label)),
|
||||
};
|
||||
|
||||
return createInProcessWindow({
|
||||
appId: "launcher",
|
||||
windowId: LAUNCHER_WINDOW_ID,
|
||||
@@ -191,7 +196,7 @@ export function createLauncherWindow(options: LauncherOptions): ShellWindow {
|
||||
actions: options.actions,
|
||||
// Not wrapped in YieldAtRootLayer: the grid handles double-click itself to
|
||||
// back out of item selection before yielding to the sidebar.
|
||||
baseLayer: new LauncherGridLayer(options),
|
||||
baseLayer: new LauncherGridLayer(sortedOptions),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
}).window;
|
||||
|
||||
+270
-73
@@ -1,15 +1,21 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures";
|
||||
import {
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
GESTURE_SCROLL,
|
||||
GESTURE_SCROLL_DOWN,
|
||||
GESTURE_SCROLL_UP,
|
||||
} from "../gestures";
|
||||
import { drawSelectionHighlight } from "../menu";
|
||||
import {
|
||||
mediaControllerBridge,
|
||||
type MediaControllerState,
|
||||
type MediaQueueItem,
|
||||
} from "../../native/media-controller";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext, type PaintBelow } from "../layers";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
@@ -22,18 +28,25 @@ export const MUSIC_SURFACE_ID = "window:music";
|
||||
|
||||
const ART_SIZE = 96;
|
||||
const ART_X = 22;
|
||||
const ART_Y = 40;
|
||||
const ART_Y = 8;
|
||||
const META_X = ART_X + ART_SIZE + 14;
|
||||
const LIST_TOP = 150;
|
||||
const LIST_TOP = 120;
|
||||
const ROW_HEIGHT = 16;
|
||||
const LIST_X = 22;
|
||||
const ACTION_X = 22;
|
||||
const ACTION_WIDTH = 154;
|
||||
const COLUMN_DIVIDER_X = 190;
|
||||
const QUEUE_X = 204;
|
||||
const FOOTER_HEIGHT = 20;
|
||||
const PLAYLIST_ACTION_INDEX = 4;
|
||||
|
||||
type MusicRow =
|
||||
type MusicAction =
|
||||
| { kind: "play-pause"; label: string; enabled: boolean }
|
||||
| { kind: "previous"; label: string; enabled: boolean }
|
||||
| { kind: "next"; label: string; enabled: boolean }
|
||||
| { kind: "queue-item"; label: string; enabled: true; item: MediaQueueItem };
|
||||
| { kind: "volume"; label: string; enabled: boolean }
|
||||
| { kind: "playlist"; label: string; enabled: boolean };
|
||||
|
||||
type FocusColumn = "actions" | "playlist";
|
||||
|
||||
/**
|
||||
* Music controller app: metadata + album art + transport controls for the
|
||||
@@ -41,21 +54,23 @@ type MusicRow =
|
||||
* player's queue when it exposes one (scroll to a track, click to jump).
|
||||
*/
|
||||
class MusicAppLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private focusColumn: FocusColumn = "actions";
|
||||
private selectedActionIndex = 0;
|
||||
private selectedQueueIndex = 0;
|
||||
private queueScrollRow = 0;
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
|
||||
isPlaylistFocused(): boolean {
|
||||
return this.focusColumn === "playlist";
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
|
||||
image.drawText(font, 20, 8, "Music", 220);
|
||||
const status = playbackLabel(media);
|
||||
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
|
||||
|
||||
if (!media.accessEnabled) {
|
||||
const lines = wrapText(
|
||||
font,
|
||||
@@ -63,15 +78,15 @@ class MusicAppLayer implements Layer {
|
||||
width - 48,
|
||||
);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
image.drawText(font, 24, 44 + index * 14, lines[index]!, 180);
|
||||
image.drawText(font, 24, 16 + index * 14, lines[index]!, 180);
|
||||
}
|
||||
image.drawText(font, 20, height - 16, `${GESTURE_CLICK} open settings ${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
if (!media.available) {
|
||||
image.drawText(font, 24, 44, "No active media session.", 180);
|
||||
image.drawText(font, 24, 62, "Start playback in another app on the phone.", 150);
|
||||
image.drawText(font, 24, 16, "No active media session.", 180);
|
||||
image.drawText(font, 24, 34, "Start playback in another app on the phone.", 150);
|
||||
image.drawText(font, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
return image;
|
||||
}
|
||||
@@ -87,36 +102,95 @@ class MusicAppLayer implements Layer {
|
||||
if (media.album) {
|
||||
image.drawText(font, META_X, ART_Y + 52, truncate(font, media.album, metaWidth), 150);
|
||||
}
|
||||
image.drawText(font, META_X, ART_Y + 70, media.packageName, 110);
|
||||
image.drawText(font, META_X, ART_Y + 68, truncate(font, media.appName || media.packageName, metaWidth), 110);
|
||||
this.drawProgress(image, media, metaWidth);
|
||||
|
||||
const rows = this.buildRows(media);
|
||||
this.selectedIndex = clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1));
|
||||
const queue = mediaControllerBridge.getQueue();
|
||||
const actions = this.buildActions(media, queue);
|
||||
this.reconcileSelection(actions, queue);
|
||||
const listHeight = height - LIST_TOP - FOOTER_HEIGHT;
|
||||
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
|
||||
if (this.selectedIndex < this.scrollRow) {
|
||||
this.scrollRow = this.selectedIndex;
|
||||
} else if (this.selectedIndex >= this.scrollRow + visibleRows) {
|
||||
this.scrollRow = this.selectedIndex - visibleRows + 1;
|
||||
if (this.selectedQueueIndex < this.queueScrollRow) {
|
||||
this.queueScrollRow = this.selectedQueueIndex;
|
||||
} else if (this.selectedQueueIndex >= this.queueScrollRow + visibleRows) {
|
||||
this.queueScrollRow = this.selectedQueueIndex - visibleRows + 1;
|
||||
}
|
||||
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, rows.length - visibleRows));
|
||||
this.queueScrollRow = clamp(this.queueScrollRow, 0, Math.max(0, queue.length - visibleRows));
|
||||
|
||||
const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < lastVisible; index++) {
|
||||
const row = rows[index]!;
|
||||
const y = LIST_TOP + (index - this.scrollRow) * ROW_HEIGHT;
|
||||
const selected = index === this.selectedIndex;
|
||||
for (let index = 0; index < actions.length; index++) {
|
||||
const action = actions[index]!;
|
||||
const y = LIST_TOP + index * ROW_HEIGHT;
|
||||
const selected = index === this.selectedActionIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(image, LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, ctx.stack.isFocused(), 4);
|
||||
drawSelectionHighlight(
|
||||
image,
|
||||
ACTION_X - 6,
|
||||
y - 1,
|
||||
ACTION_WIDTH + 8,
|
||||
ROW_HEIGHT - 1,
|
||||
ctx.stack.isFocused() && this.focusColumn === "actions",
|
||||
4,
|
||||
);
|
||||
}
|
||||
const value = !action.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
|
||||
image.drawText(font, ACTION_X, y + 1, truncate(font, action.label, ACTION_WIDTH - 16), value);
|
||||
if (action.kind === "playlist" && action.enabled) {
|
||||
image.drawText(font, ACTION_X + ACTION_WIDTH - 16, y + 1, ">", value);
|
||||
}
|
||||
const value = !row.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
|
||||
image.drawText(font, LIST_X, y + 1, truncate(font, row.label, width - 2 * LIST_X), value);
|
||||
}
|
||||
|
||||
image.drawText(font, 20, height - 16, `${GESTURE_SCROLL} select ${GESTURE_CLICK} activate ${GESTURE_DOUBLE_CLICK} back`, 110);
|
||||
image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - FOOTER_HEIGHT - 3, 45);
|
||||
if (!queue.length) {
|
||||
image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90);
|
||||
} else {
|
||||
const queueWidth = width - QUEUE_X - 20;
|
||||
const lastVisible = Math.min(queue.length, this.queueScrollRow + visibleRows);
|
||||
for (let index = this.queueScrollRow; index < lastVisible; index++) {
|
||||
const item = queue[index]!;
|
||||
const y = LIST_TOP + (index - this.queueScrollRow) * ROW_HEIGHT;
|
||||
const selected = index === this.selectedQueueIndex;
|
||||
if (selected) {
|
||||
drawSelectionHighlight(
|
||||
image,
|
||||
QUEUE_X - 6,
|
||||
y - 1,
|
||||
queueWidth + 8,
|
||||
ROW_HEIGHT - 1,
|
||||
ctx.stack.isFocused() && this.focusColumn === "playlist",
|
||||
4,
|
||||
);
|
||||
}
|
||||
const label = `${item.active ? "> " : " "}${item.title || "(untitled)"}`;
|
||||
image.drawText(font, QUEUE_X, y + 1, truncate(font, label, queueWidth - 4), selected ? 255 : 200);
|
||||
}
|
||||
if (queue.length > visibleRows) {
|
||||
const trackHeight = visibleRows * ROW_HEIGHT - 4;
|
||||
const trackX = width - 12;
|
||||
image.fillRect(trackX, LIST_TOP, 3, trackHeight, 30);
|
||||
const thumbHeight = Math.max(8, (trackHeight * visibleRows / queue.length) | 0);
|
||||
const maxScrollRow = queue.length - visibleRows;
|
||||
const thumbY = LIST_TOP + (((trackHeight - thumbHeight) * this.queueScrollRow / maxScrollRow) | 0);
|
||||
image.fillRect(trackX, thumbY, 3, thumbHeight, 120);
|
||||
}
|
||||
}
|
||||
|
||||
const backTarget = this.focusColumn === "playlist" ? "actions" : "back";
|
||||
image.drawText(
|
||||
font,
|
||||
20,
|
||||
height - 16,
|
||||
`${GESTURE_SCROLL} select ${GESTURE_CLICK} activate ${GESTURE_DOUBLE_CLICK} ${backTarget}`,
|
||||
110,
|
||||
);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (event.type === "double-click" && this.focusColumn === "playlist") {
|
||||
this.focusColumn = "actions";
|
||||
this.selectedActionIndex = PLAYLIST_ACTION_INDEX;
|
||||
return;
|
||||
}
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
if (!media.accessEnabled) {
|
||||
if (event.type === "click") {
|
||||
@@ -126,33 +200,52 @@ class MusicAppLayer implements Layer {
|
||||
}
|
||||
return;
|
||||
}
|
||||
const rows = this.buildRows(media);
|
||||
const queue = mediaControllerBridge.getQueue();
|
||||
const actions = this.buildActions(media, queue);
|
||||
this.reconcileSelection(actions, queue);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
if (this.focusColumn === "playlist") {
|
||||
this.selectedQueueIndex = Math.max(0, this.selectedQueueIndex - 1);
|
||||
} else {
|
||||
this.selectedActionIndex = Math.max(0, this.selectedActionIndex - 1);
|
||||
}
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = Math.min(Math.max(0, rows.length - 1), this.selectedIndex + 1);
|
||||
if (this.focusColumn === "playlist") {
|
||||
this.selectedQueueIndex = Math.min(Math.max(0, queue.length - 1), this.selectedQueueIndex + 1);
|
||||
} else {
|
||||
this.selectedActionIndex = Math.min(actions.length - 1, this.selectedActionIndex + 1);
|
||||
}
|
||||
return;
|
||||
case "click": {
|
||||
const row = rows[clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1))];
|
||||
if (!row || !row.enabled) return;
|
||||
if (row.kind === "play-pause") await mediaControllerBridge.playPause();
|
||||
else if (row.kind === "previous") await mediaControllerBridge.skipPrevious();
|
||||
else if (row.kind === "next") await mediaControllerBridge.skipNext();
|
||||
else if (row.kind === "queue-item") await mediaControllerBridge.skipToQueueItem(row.item.id);
|
||||
if (this.focusColumn === "playlist") {
|
||||
const item = queue[this.selectedQueueIndex];
|
||||
if (item) await mediaControllerBridge.skipToQueueItem(item.id);
|
||||
return;
|
||||
}
|
||||
const action = actions[this.selectedActionIndex];
|
||||
if (!action || !action.enabled) return;
|
||||
if (action.kind === "play-pause") await mediaControllerBridge.playPause();
|
||||
else if (action.kind === "previous") await mediaControllerBridge.skipPrevious();
|
||||
else if (action.kind === "next") await mediaControllerBridge.skipNext();
|
||||
else if (action.kind === "volume") {
|
||||
ctx.stack.push(new VolumeModalLayer(mediaControllerBridge.getMediaVolumePercent()));
|
||||
} else if (action.kind === "playlist") {
|
||||
const activeIndex = queue.findIndex((item) => item.active);
|
||||
if (activeIndex >= 0) this.selectedQueueIndex = activeIndex;
|
||||
this.focusColumn = "playlist";
|
||||
}
|
||||
return;
|
||||
}
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private buildRows(media: MediaControllerState): MusicRow[] {
|
||||
const rows: MusicRow[] = [
|
||||
private buildActions(media: MediaControllerState, queue: MediaQueueItem[]): MusicAction[] {
|
||||
const volume = mediaControllerBridge.getMediaVolumePercent();
|
||||
return [
|
||||
{
|
||||
kind: "play-pause",
|
||||
label: media.playbackState === "playing" ? "Pause" : "Play",
|
||||
@@ -160,16 +253,33 @@ class MusicAppLayer implements Layer {
|
||||
},
|
||||
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },
|
||||
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
|
||||
{ kind: "volume", label: volume >= 0 ? `Volume (${volume})` : "Volume", enabled: volume >= 0 },
|
||||
{ kind: "playlist", label: "Playlist", enabled: queue.length > 0 },
|
||||
];
|
||||
for (const item of mediaControllerBridge.getQueue()) {
|
||||
rows.push({
|
||||
kind: "queue-item",
|
||||
label: `${item.active ? "> " : " "}${item.title || "(untitled)"}`,
|
||||
enabled: true,
|
||||
item,
|
||||
});
|
||||
}
|
||||
|
||||
private reconcileSelection(actions: MusicAction[], queue: MediaQueueItem[]): void {
|
||||
this.selectedActionIndex = clamp(this.selectedActionIndex, 0, actions.length - 1);
|
||||
this.selectedQueueIndex = clamp(this.selectedQueueIndex, 0, Math.max(0, queue.length - 1));
|
||||
if (!queue.length && this.focusColumn === "playlist") {
|
||||
this.focusColumn = "actions";
|
||||
this.selectedActionIndex = actions.length - 1;
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
private drawProgress(image: GrayImage, media: MediaControllerState, width: number): void {
|
||||
if (media.durationMs <= 0 || media.positionMs < 0) return;
|
||||
const font = getDefaultSmallFont();
|
||||
const y = ART_Y + 84;
|
||||
const elapsed = formatMediaTime(media.positionMs);
|
||||
const duration = formatMediaTime(media.durationMs);
|
||||
image.drawText(font, META_X, y, elapsed, 140);
|
||||
image.drawText(font, META_X + width - font.measureText(duration), y, duration, 140);
|
||||
|
||||
const barY = ART_Y + 100;
|
||||
image.drawRect(META_X, barY, width, 5, 55);
|
||||
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
|
||||
image.fillRect(META_X + 1, barY + 1, Math.round((width - 2) * progress), 3, 170);
|
||||
}
|
||||
|
||||
private drawArt(image: GrayImage, media: MediaControllerState): void {
|
||||
@@ -192,8 +302,95 @@ class MusicAppLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
/** Centered volume control overlay; scroll gestures adjust a 0..100 target by two. */
|
||||
class VolumeModalLayer implements Layer {
|
||||
private volume: number;
|
||||
|
||||
constructor(initialVolume: number) {
|
||||
this.volume = clamp(initialVolume, 0, 100);
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
const image = paintBelow();
|
||||
const small = getDefaultSmallFont();
|
||||
const medium = getDefaultMediumFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const boxWidth = 340;
|
||||
const boxHeight = 150;
|
||||
const x = ((width - boxWidth) / 2) | 0;
|
||||
const y = ((height - boxHeight) / 2) | 0;
|
||||
|
||||
image.fillRoundedRect(x, y, boxWidth, boxHeight, 1, 8);
|
||||
image.drawRoundedRect(x, y, boxWidth, boxHeight, 95, 8);
|
||||
image.drawText(small, x + 18, y + 14, "Media volume", 180);
|
||||
|
||||
const value = `${this.volume} / 100`;
|
||||
const valueX = x + (((boxWidth - medium.measureText(value)) / 2) | 0);
|
||||
image.drawText(medium, valueX, y + 45, value, 245);
|
||||
|
||||
const barX = x + 28;
|
||||
const barY = y + 83;
|
||||
const barWidth = boxWidth - 56;
|
||||
image.drawRect(barX, barY, barWidth, 9, 65);
|
||||
image.fillRect(barX + 1, barY + 1, Math.round((barWidth - 2) * this.volume / 100), 7, 175);
|
||||
|
||||
const hint = `${GESTURE_SCROLL_UP} +2 ${GESTURE_SCROLL_DOWN} -2 ${GESTURE_DOUBLE_CLICK} done`;
|
||||
const hintX = x + (((boxWidth - small.measureText(hint)) / 2) | 0);
|
||||
image.drawText(small, hintX, y + 116, hint, 120);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "scroll-up") {
|
||||
this.adjust(2);
|
||||
} else if (event.type === "scroll-down") {
|
||||
this.adjust(-2);
|
||||
} else if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
private adjust(delta: number): void {
|
||||
this.volume = clamp(this.volume + delta, 0, 100);
|
||||
mediaControllerBridge.setMediaVolumePercent(this.volume);
|
||||
}
|
||||
}
|
||||
|
||||
/** Let back leave the playlist column before the root wrapper yields to the shell. */
|
||||
class MusicRootLayer implements Layer {
|
||||
private readonly yieldAtRoot: YieldAtRootLayer;
|
||||
|
||||
constructor(private readonly music: MusicAppLayer) {
|
||||
this.yieldAtRoot = new YieldAtRootLayer(music);
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||
return this.yieldAtRoot.paint(ctx, paintBelow);
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
if (event.type === "double-click" && this.music.isPlaylistFocused()) {
|
||||
await this.music.handleInput(event, ctx);
|
||||
return;
|
||||
}
|
||||
await this.yieldAtRoot.handleInput(event, ctx);
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.yieldAtRoot.onRemoved();
|
||||
}
|
||||
}
|
||||
|
||||
export function createMusicAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
let progressTimer: ReturnType<typeof setInterval> | null = null;
|
||||
const musicLayer = new MusicAppLayer();
|
||||
const stopProgressTimer = () => {
|
||||
if (progressTimer !== null) {
|
||||
clearInterval(progressTimer);
|
||||
progressTimer = null;
|
||||
}
|
||||
};
|
||||
const app = createInProcessWindow({
|
||||
appId: "music",
|
||||
windowId: MUSIC_WINDOW_ID,
|
||||
@@ -202,37 +399,37 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin
|
||||
icon: "music",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(new MusicAppLayer()),
|
||||
baseLayer: new MusicRootLayer(musicLayer),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
stopProgressTimer();
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
unsubscribe = mediaControllerBridge.onStateChange(() => {
|
||||
unsubscribe = mediaControllerBridge.onStateChange((state) => {
|
||||
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
|
||||
progressTimer ??= setInterval(() => app.requestRender(), 1_000);
|
||||
} else {
|
||||
stopProgressTimer();
|
||||
}
|
||||
app.requestRender();
|
||||
});
|
||||
return app;
|
||||
}
|
||||
|
||||
function playbackLabel(media: MediaControllerState): string {
|
||||
switch (media.playbackState) {
|
||||
case "playing":
|
||||
return "Playing";
|
||||
case "paused":
|
||||
return "Paused";
|
||||
case "buffering":
|
||||
return "Buffering";
|
||||
case "stopped":
|
||||
return "Stopped";
|
||||
case "notification-access-required":
|
||||
return "Access required";
|
||||
default:
|
||||
return media.status || "Idle";
|
||||
}
|
||||
function formatMediaTime(milliseconds: number): string {
|
||||
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
return hours > 0
|
||||
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
|
||||
: `${minutes}:${String(seconds).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
function truncate(font: import("../../graphics/bdffont").BdfFont, text: string, maxWidth: number): string {
|
||||
|
||||
+20
-11
@@ -5,7 +5,12 @@ import { DashboardInputEvent, LayerActions, LayerStack } from "../layers";
|
||||
import { MenuLayer, type MenuItem } from "../menu";
|
||||
import { VoiceInputLayer } from "../apps/voice-input";
|
||||
import { SingleNotificationLayer } from "../notifications";
|
||||
import { batteryDisplayModeSetting, onAnySettingChanged, timeFormatSetting } from "../dashboard-settings";
|
||||
import {
|
||||
batteryDisplayModeSetting,
|
||||
onAnySettingChanged,
|
||||
timeFormatSetting,
|
||||
wakeWordActionSetting,
|
||||
} from "../dashboard-settings";
|
||||
import { ShellChromeLayer, type ShellChromeState, type ShellChromeWindow } from "./chrome-layer";
|
||||
import { ShellModalLayer } from "./modal-layer";
|
||||
import { SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry";
|
||||
@@ -318,22 +323,26 @@ class Shell {
|
||||
}
|
||||
|
||||
async receiveInput(event: DashboardInputEvent, frameId = 0): Promise<ShellInputOutcome> {
|
||||
this.lastInputAtMs = Date.now();
|
||||
|
||||
// The wakeword is handled before the screen-off short-circuit: "Hey Even"
|
||||
// is meant to work from a dark screen, which is the whole point of it.
|
||||
// With the CFW the stock Even AI app never launches, so the firmware does
|
||||
// not power the display for us either -- we wake it ourselves.
|
||||
// The wakeword is handled before the screen-off short-circuit so its
|
||||
// configured action can work from a dark screen. With the CFW the stock
|
||||
// Even AI app never launches, so the firmware does not power the display
|
||||
// for us either -- actions that need it wake the screen themselves.
|
||||
if (event.type === "wakeword") {
|
||||
if (!this.screenOn) {
|
||||
this.wake("sidebar");
|
||||
const action = wakeWordActionSetting.get();
|
||||
if (action === "off") {
|
||||
return { shell: false, window: false };
|
||||
}
|
||||
if (!this.activeVoiceLayer) {
|
||||
|
||||
this.lastInputAtMs = Date.now();
|
||||
const wokeScreen = !this.screenOn && this.wake("sidebar");
|
||||
if (action === "voice-input" && !this.activeVoiceLayer) {
|
||||
this.openVoiceDialog(false, true);
|
||||
}
|
||||
return { shell: true, window: false };
|
||||
return { shell: wokeScreen || action === "voice-input", window: false };
|
||||
}
|
||||
|
||||
this.lastInputAtMs = Date.now();
|
||||
|
||||
if (!this.screenOn) {
|
||||
if (event.type === "double-click") {
|
||||
this.wake("sidebar");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { imageFromAsciiArt } from "../../graphics/image";
|
||||
import { TranscribeLayer } from "../apps/transcribe";
|
||||
import type { LayerActions } from "../layers";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
@@ -49,7 +48,6 @@ export type TranscribeAppOptions = InProcessAppOptions & {
|
||||
*/
|
||||
export function createTranscribeAppWindow(options: TranscribeAppOptions): InProcessWindow {
|
||||
const layer = new TranscribeLayer();
|
||||
const actions: LayerActions = { ...options.actions };
|
||||
const app = createInProcessWindow({
|
||||
appId: "transcribe",
|
||||
windowId: TRANSCRIBE_WINDOW_ID,
|
||||
@@ -57,7 +55,7 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc
|
||||
iconLetter: "Tr",
|
||||
icon: "mic",
|
||||
closeable: true,
|
||||
actions,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(layer),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
@@ -68,10 +66,9 @@ export function createTranscribeAppWindow(options: TranscribeAppOptions): InProc
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
// start() wires the layer's transcript/status subscriptions; the LayerStack
|
||||
// exposes its context through a paint, but the layer needs it up front, so
|
||||
// build a minimal context here.
|
||||
layer.start({ stack: app.stack, actions });
|
||||
// Wire async state changes to this window's renderer, rather than the
|
||||
// controller action from which the window-specific actions were derived.
|
||||
layer.start(app.requestRender);
|
||||
options.startContinuousVoiceCapture();
|
||||
shell.setTrayIcon(TRAY_ICON_ID, MIC_ICON);
|
||||
return app;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||
import { weatherBridge } from "../../native/weather";
|
||||
import { WeatherLayer } from "../apps/weather";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "./in-process-window";
|
||||
|
||||
export const WEATHER_WINDOW_ID = "weather";
|
||||
export const WEATHER_SURFACE_ID = "window:weather";
|
||||
|
||||
/** Local current conditions and forecast from the National Weather Service. */
|
||||
export function createWeatherAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let requestingPermission = false;
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
let app: InProcessWindow;
|
||||
|
||||
const requestUpdate = () => {
|
||||
if (requestingPermission) return;
|
||||
if (hasLocationPermission()) {
|
||||
weatherBridge.start();
|
||||
return;
|
||||
}
|
||||
requestingPermission = true;
|
||||
void ensureLocationPermission().then((granted) => {
|
||||
requestingPermission = false;
|
||||
if (granted) weatherBridge.start();
|
||||
app.requestRender();
|
||||
});
|
||||
};
|
||||
|
||||
app = createInProcessWindow({
|
||||
appId: "weather",
|
||||
windowId: WEATHER_WINDOW_ID,
|
||||
title: "Weather",
|
||||
iconLetter: "W",
|
||||
icon: "cloud-sun",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(new WeatherLayer(() => weatherBridge.snapshot(), requestUpdate)),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
weatherBridge.stop();
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
unsubscribe = weatherBridge.onStateChange(() => app.requestRender());
|
||||
requestUpdate();
|
||||
return app;
|
||||
}
|
||||
@@ -1,202 +0,0 @@
|
||||
/**
|
||||
* Stopwatch app, hosted in its own worker thread. One worker serves every
|
||||
* stopwatch window; per-window state is keyed by windowId. The shell (main
|
||||
* thread) sends open/close/input/lifecycle messages over postMessage; frames
|
||||
* are painted here and submitted directly to the Java compositor from this
|
||||
* worker's own thread — no pixels cross the isolate boundary.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { getDefaultSmallFont, getFont } from "../graphics/bdffont";
|
||||
import * as frameTimings from "../native/frame-timings";
|
||||
import type { DashboardInputEvent } from "../ui/layers";
|
||||
import type { WorkerAppMessage } from "../ui/shell/worker-window";
|
||||
|
||||
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../ui/gestures";
|
||||
declare const global: any;
|
||||
declare const com: any;
|
||||
|
||||
const RENDER_INTERVAL_MS = 100;
|
||||
|
||||
const stopwatchFont = getFont("terminus32");
|
||||
const smallFont = getDefaultSmallFont();
|
||||
|
||||
type StopwatchWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
// Accumulated time survives pausing, backgrounding, and screen-off; only
|
||||
// rendering pauses.
|
||||
accumulatedMs: number;
|
||||
runningSinceMs: number | null;
|
||||
renderTimer: ReturnType<typeof setInterval> | null;
|
||||
lastSubmittedFingerprint: string;
|
||||
};
|
||||
|
||||
const windows = new Map<string, StopwatchWindow>();
|
||||
let screenOn = true;
|
||||
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "open-window":
|
||||
windows.set(message.windowId, {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
accumulatedMs: 0,
|
||||
runningSinceMs: null,
|
||||
renderTimer: null,
|
||||
lastSubmittedFingerprint: "",
|
||||
});
|
||||
break;
|
||||
case "close-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (window?.renderTimer) clearInterval(window.renderTimer);
|
||||
windows.delete(message.windowId);
|
||||
break;
|
||||
}
|
||||
case "input": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) {
|
||||
frameTimings.finishFrame(message.frameId, "discarded: unknown stopwatch window");
|
||||
break;
|
||||
}
|
||||
handleInput(window, message.event as DashboardInputEvent, message.frameId);
|
||||
break;
|
||||
}
|
||||
case "render": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (window) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "foreground": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.foreground = message.foreground;
|
||||
updateRenderTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) {
|
||||
updateRenderTimer(window);
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
function handleInput(window: StopwatchWindow, event: DashboardInputEvent, frameId: number): void {
|
||||
if (event.type === "double-click") {
|
||||
// Back out of the app root: return focus to the shell sidebar. State keeps
|
||||
// running; the frame is finished here since no repaint happens.
|
||||
frameTimings.finishFrame(frameId, "discarded: stopwatch yielded focus");
|
||||
global.postMessage({ type: "yield-focus", windowId: window.windowId });
|
||||
return;
|
||||
}
|
||||
if (event.type === "click") {
|
||||
if (window.runningSinceMs === null) {
|
||||
window.runningSinceMs = Date.now();
|
||||
} else {
|
||||
window.accumulatedMs += Date.now() - window.runningSinceMs;
|
||||
window.runningSinceMs = null;
|
||||
}
|
||||
updateRenderTimer(window);
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
frameTimings.finishFrame(frameId, "discarded: stopwatch ignored input");
|
||||
}
|
||||
|
||||
function updateRenderTimer(window: StopwatchWindow): void {
|
||||
const shouldRun = window.runningSinceMs !== null && window.foreground && screenOn;
|
||||
if (shouldRun && window.renderTimer === null) {
|
||||
window.renderTimer = setInterval(() => renderAndSubmit(window, 0), RENDER_INTERVAL_MS);
|
||||
} else if (!shouldRun && window.renderTimer !== null) {
|
||||
clearInterval(window.renderTimer);
|
||||
window.renderTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function elapsedMs(window: StopwatchWindow): number {
|
||||
const runningMs = window.runningSinceMs === null ? 0 : Date.now() - window.runningSinceMs;
|
||||
return window.accumulatedMs + runningMs;
|
||||
}
|
||||
|
||||
function paint(window: StopwatchWindow): GrayImage {
|
||||
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
const elapsed = elapsedMs(window);
|
||||
const timeLabel = formatStopwatchElapsed(elapsed);
|
||||
const stateLabel = window.runningSinceMs === null ? (elapsed > 0 ? "Paused" : "Ready") : "Running";
|
||||
|
||||
image.drawText(smallFont, 24, 16, "Stopwatch", 180);
|
||||
const timeX = Math.max(0, Math.round((window.viewportWidth - stopwatchFont.measureText(timeLabel)) / 2));
|
||||
const timeY = Math.max(0, Math.round((window.viewportHeight / 2 - stopwatchFont.lineHeight) / 2) + 20);
|
||||
image.drawText(stopwatchFont, timeX, timeY, timeLabel, 245);
|
||||
|
||||
const hintsY = window.viewportHeight - 66;
|
||||
image.drawText(smallFont, 90, hintsY, stateLabel, 150);
|
||||
image.drawText(smallFont, 90, hintsY + 18, `${GESTURE_CLICK} start / pause`, 150);
|
||||
image.drawText(smallFont, 90, hintsY + 36, `${GESTURE_DOUBLE_CLICK} back`, 150);
|
||||
return image;
|
||||
}
|
||||
|
||||
function renderAndSubmit(window: StopwatchWindow, inputFrameId: number): void {
|
||||
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
|
||||
try {
|
||||
const paintStartedAtMs = Date.now();
|
||||
const image = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => paint(window)),
|
||||
);
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const fingerprint = image.fingerprint();
|
||||
if (fingerprint === window.lastSubmittedFingerprint) {
|
||||
frameTimings.finishFrame(frameId, "discarded: stopwatch content unchanged");
|
||||
return;
|
||||
}
|
||||
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
if (!communicator) {
|
||||
frameTimings.finishFrame(frameId, "discarded: no active communicator");
|
||||
return;
|
||||
}
|
||||
const buffer = image.to8bppBuffer();
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
);
|
||||
window.lastSubmittedFingerprint = fingerprint;
|
||||
} catch (error) {
|
||||
frameTimings.finishFrame(frameId, `discarded: stopwatch render failed`);
|
||||
console.error(`stopwatch worker render failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
function pad2(value: number): string {
|
||||
return String(value).padStart(2, "0");
|
||||
}
|
||||
|
||||
function formatStopwatchElapsed(totalMs: number): string {
|
||||
const totalTenths = Math.floor(totalMs / 100);
|
||||
const tenths = totalTenths % 10;
|
||||
const totalSeconds = Math.floor(totalTenths / 10);
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
if (hours > 0) {
|
||||
return `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
||||
}
|
||||
return `${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
||||
}
|
||||
@@ -0,0 +1,541 @@
|
||||
/**
|
||||
* Timer app worker. The singleton window contains a stopwatch plus a list of
|
||||
* independently scheduled countdown timers. Android AlarmManager owns the
|
||||
* durable expiry path; worker timeouts keep the live UI prompt.
|
||||
*/
|
||||
import "@nativescript/core/globals";
|
||||
import { GrayImage } from "../graphics/image";
|
||||
import { getDefaultSmallFont, getFont } from "../graphics/bdffont";
|
||||
import * as frameTimings from "../native/frame-timings";
|
||||
import {
|
||||
cancelTimerNotification,
|
||||
fireTimerNotification,
|
||||
scheduleTimerNotification,
|
||||
} from "../native/timer-notifications";
|
||||
import type { DashboardInputEvent } from "../ui/layers";
|
||||
import type { WorkerAppMessage, WorkerAppReply } from "../ui/shell/worker-window";
|
||||
import {
|
||||
GESTURE_CLICK,
|
||||
GESTURE_DOUBLE_CLICK,
|
||||
GESTURE_SCROLL,
|
||||
} from "../ui/gestures";
|
||||
|
||||
declare const global: any;
|
||||
declare const com: any;
|
||||
|
||||
const RENDER_INTERVAL_MS = 100;
|
||||
const TIMER_ROW_HEIGHT = 34;
|
||||
const TIMER_LIST_TOP = 52;
|
||||
const FOOTER_HEIGHT = 34;
|
||||
|
||||
const largeFont = getFont("terminus32");
|
||||
const mediumFont = getFont("terminus24");
|
||||
const smallFont = getDefaultSmallFont();
|
||||
|
||||
type TimerView = "stopwatch" | "timers" | "editor";
|
||||
|
||||
type TimerWindow = {
|
||||
windowId: string;
|
||||
surfaceId: string;
|
||||
viewportWidth: number;
|
||||
viewportHeight: number;
|
||||
foreground: boolean;
|
||||
view: TimerView;
|
||||
stopwatchAction: number;
|
||||
timerSelection: number;
|
||||
editorField: number;
|
||||
editorHours: number;
|
||||
editorMinutes: number;
|
||||
editorSeconds: number;
|
||||
editorMessage: string;
|
||||
renderTimer: ReturnType<typeof setInterval> | null;
|
||||
lastSubmittedFingerprint: string;
|
||||
};
|
||||
|
||||
type CountdownTimer = {
|
||||
id: number;
|
||||
durationMs: number;
|
||||
endAtMs: number;
|
||||
expired: boolean;
|
||||
expiryTimeout: ReturnType<typeof setTimeout> | null;
|
||||
};
|
||||
|
||||
const windows = new Map<string, TimerWindow>();
|
||||
const timers: CountdownTimer[] = [];
|
||||
let screenOn = true;
|
||||
let stopwatchAccumulatedMs = 0;
|
||||
let stopwatchRunningSinceMs: number | null = null;
|
||||
let nextTimerSerial = 1;
|
||||
|
||||
function post(message: WorkerAppReply): void {
|
||||
global.postMessage(message);
|
||||
}
|
||||
|
||||
global.onmessage = (event: { data: WorkerAppMessage }) => {
|
||||
const message = event.data;
|
||||
switch (message.type) {
|
||||
case "open-window":
|
||||
windows.set(message.windowId, {
|
||||
windowId: message.windowId,
|
||||
surfaceId: message.surfaceId,
|
||||
viewportWidth: message.viewport.width,
|
||||
viewportHeight: message.viewport.height,
|
||||
foreground: false,
|
||||
view: "stopwatch",
|
||||
stopwatchAction: 0,
|
||||
timerSelection: 0,
|
||||
editorField: 0,
|
||||
editorHours: 0,
|
||||
editorMinutes: 5,
|
||||
editorSeconds: 0,
|
||||
editorMessage: "",
|
||||
renderTimer: null,
|
||||
lastSubmittedFingerprint: "",
|
||||
});
|
||||
syncExpiredTimers();
|
||||
break;
|
||||
case "close-window": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (window?.renderTimer) clearInterval(window.renderTimer);
|
||||
windows.delete(message.windowId);
|
||||
break;
|
||||
}
|
||||
case "input": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) {
|
||||
frameTimings.finishFrame(message.frameId, "discarded: unknown timer window");
|
||||
break;
|
||||
}
|
||||
handleInput(window, message.event as DashboardInputEvent, message.frameId);
|
||||
break;
|
||||
}
|
||||
case "render": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (window) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "foreground": {
|
||||
const window = windows.get(message.windowId);
|
||||
if (!window) break;
|
||||
window.foreground = message.foreground;
|
||||
updateRenderTimer(window);
|
||||
if (window.foreground) renderAndSubmit(window, 0);
|
||||
break;
|
||||
}
|
||||
case "screen":
|
||||
screenOn = message.on;
|
||||
for (const window of windows.values()) updateRenderTimer(window);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
function handleInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||
if (event.type === "double-click") {
|
||||
if (window.view === "editor") {
|
||||
window.view = "timers";
|
||||
window.editorMessage = "";
|
||||
updateRenderTimer(window);
|
||||
renderAndSubmit(window, frameId);
|
||||
} else {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer yielded focus");
|
||||
post({ type: "yield-focus", windowId: window.windowId });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.view === "stopwatch") {
|
||||
handleStopwatchInput(window, event, frameId);
|
||||
} else if (window.view === "timers") {
|
||||
handleTimersInput(window, event, frameId);
|
||||
} else {
|
||||
handleEditorInput(window, event, frameId);
|
||||
}
|
||||
}
|
||||
|
||||
function handleStopwatchInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
const delta = event.type === "scroll-down" ? 1 : -1;
|
||||
window.stopwatchAction = clamp(window.stopwatchAction + delta, 0, 2);
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
if (event.type !== "click") {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer stopwatch ignored input");
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.stopwatchAction === 0) {
|
||||
if (stopwatchRunningSinceMs === null) {
|
||||
stopwatchRunningSinceMs = Date.now();
|
||||
} else {
|
||||
stopwatchAccumulatedMs += Date.now() - stopwatchRunningSinceMs;
|
||||
stopwatchRunningSinceMs = null;
|
||||
}
|
||||
} else if (window.stopwatchAction === 1) {
|
||||
stopwatchAccumulatedMs = 0;
|
||||
stopwatchRunningSinceMs = null;
|
||||
} else {
|
||||
window.view = "timers";
|
||||
window.timerSelection = 0;
|
||||
}
|
||||
updateRenderTimer(window);
|
||||
renderAndSubmit(window, frameId);
|
||||
}
|
||||
|
||||
function handleTimersInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||
syncExpiredTimers();
|
||||
const itemCount = timers.length + 2; // Stopwatch navigation + New timer + timers.
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
const delta = event.type === "scroll-down" ? 1 : -1;
|
||||
window.timerSelection = clamp(window.timerSelection + delta, 0, itemCount - 1);
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
if (event.type !== "click") {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer list ignored input");
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.timerSelection === 0) {
|
||||
window.view = "stopwatch";
|
||||
} else if (window.timerSelection === 1) {
|
||||
openEditor(window);
|
||||
} else {
|
||||
const timer = timers[window.timerSelection - 2];
|
||||
if (timer) removeTimer(timer);
|
||||
window.timerSelection = clamp(window.timerSelection, 0, Math.max(0, timers.length + 1));
|
||||
}
|
||||
updateRenderTimer(window);
|
||||
renderAndSubmit(window, frameId);
|
||||
}
|
||||
|
||||
function handleEditorInput(window: TimerWindow, event: DashboardInputEvent, frameId: number): void {
|
||||
if (event.type === "scroll-up" || event.type === "scroll-down") {
|
||||
const delta = event.type === "scroll-up" ? 1 : -1;
|
||||
if (window.editorField === 0) {
|
||||
window.editorHours = clamp(window.editorHours + delta, 0, 23);
|
||||
} else if (window.editorField === 1) {
|
||||
window.editorMinutes = clamp(window.editorMinutes + delta, 0, 59);
|
||||
} else if (window.editorField === 2) {
|
||||
window.editorSeconds = clamp(window.editorSeconds + delta, 0, 59);
|
||||
} else {
|
||||
window.editorField = delta > 0 ? 2 : 3;
|
||||
}
|
||||
window.editorMessage = "";
|
||||
renderAndSubmit(window, frameId);
|
||||
return;
|
||||
}
|
||||
if (event.type !== "click") {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer editor ignored input");
|
||||
return;
|
||||
}
|
||||
|
||||
if (window.editorField < 3) {
|
||||
window.editorField += 1;
|
||||
} else {
|
||||
const durationMs =
|
||||
((window.editorHours * 60 + window.editorMinutes) * 60 + window.editorSeconds) * 1000;
|
||||
if (durationMs <= 0) {
|
||||
window.editorMessage = "Choose a duration";
|
||||
window.editorField = 0;
|
||||
} else {
|
||||
addTimer(durationMs);
|
||||
window.view = "timers";
|
||||
window.timerSelection = timers.length + 1;
|
||||
window.editorMessage = "";
|
||||
}
|
||||
}
|
||||
updateRenderTimer(window);
|
||||
renderAndSubmit(window, frameId);
|
||||
}
|
||||
|
||||
function openEditor(window: TimerWindow): void {
|
||||
window.view = "editor";
|
||||
window.editorField = 0;
|
||||
window.editorHours = 0;
|
||||
window.editorMinutes = 5;
|
||||
window.editorSeconds = 0;
|
||||
window.editorMessage = "";
|
||||
}
|
||||
|
||||
function addTimer(durationMs: number): void {
|
||||
// Millisecond epoch plus a small serial is unique across worker restarts and
|
||||
// still safely representable as a JavaScript/Java long bridge value.
|
||||
const id = Date.now() * 100 + (nextTimerSerial++ % 100);
|
||||
const endAtMs = Date.now() + durationMs;
|
||||
const timer: CountdownTimer = {
|
||||
id,
|
||||
durationMs,
|
||||
endAtMs,
|
||||
expired: false,
|
||||
expiryTimeout: null,
|
||||
};
|
||||
timers.push(timer);
|
||||
const durationLabel = formatDuration(durationMs);
|
||||
scheduleTimerNotification(id, endAtMs, durationLabel);
|
||||
timer.expiryTimeout = setTimeout(() => expireTimer(timer), Math.max(0, endAtMs - Date.now()));
|
||||
}
|
||||
|
||||
function expireTimer(timer: CountdownTimer, renderWindows = true): void {
|
||||
if (timer.expired) return;
|
||||
timer.expired = true;
|
||||
timer.expiryTimeout = null;
|
||||
fireTimerNotification(timer.id, formatDuration(timer.durationMs));
|
||||
for (const window of windows.values()) {
|
||||
post({ type: "set-attention", windowId: window.windowId, attention: true });
|
||||
updateRenderTimer(window);
|
||||
if (renderWindows && window.foreground && screenOn) renderAndSubmit(window, 0);
|
||||
}
|
||||
}
|
||||
|
||||
function removeTimer(timer: CountdownTimer): void {
|
||||
if (timer.expiryTimeout) clearTimeout(timer.expiryTimeout);
|
||||
cancelTimerNotification(timer.id);
|
||||
const index = timers.indexOf(timer);
|
||||
if (index >= 0) timers.splice(index, 1);
|
||||
}
|
||||
|
||||
function syncExpiredTimers(): void {
|
||||
const now = Date.now();
|
||||
for (const timer of timers) {
|
||||
// The caller is already about to paint, so avoid a nested render when a
|
||||
// sleeping/backgrounded timer is first observed as finished.
|
||||
if (!timer.expired && timer.endAtMs <= now) expireTimer(timer, false);
|
||||
}
|
||||
}
|
||||
|
||||
function updateRenderTimer(window: TimerWindow): void {
|
||||
const activeCountdowns = timers.some((timer) => !timer.expired);
|
||||
const dynamic =
|
||||
(window.view === "stopwatch" && stopwatchRunningSinceMs !== null) ||
|
||||
(window.view === "timers" && activeCountdowns);
|
||||
const shouldRun = dynamic && window.foreground && screenOn;
|
||||
if (shouldRun && window.renderTimer === null) {
|
||||
window.renderTimer = setInterval(() => {
|
||||
syncExpiredTimers();
|
||||
renderAndSubmit(window, 0);
|
||||
}, RENDER_INTERVAL_MS);
|
||||
} else if (!shouldRun && window.renderTimer !== null) {
|
||||
clearInterval(window.renderTimer);
|
||||
window.renderTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function paint(window: TimerWindow): GrayImage {
|
||||
syncExpiredTimers();
|
||||
const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0);
|
||||
if (window.view === "stopwatch") {
|
||||
paintStopwatch(image, window);
|
||||
} else if (window.view === "timers") {
|
||||
paintTimers(image, window);
|
||||
} else {
|
||||
paintEditor(image, window);
|
||||
}
|
||||
return image;
|
||||
}
|
||||
|
||||
function paintStopwatch(image: GrayImage, window: TimerWindow): void {
|
||||
drawTabs(image, "stopwatch");
|
||||
const elapsed = stopwatchElapsedMs();
|
||||
const timeLabel = formatStopwatchElapsed(elapsed);
|
||||
const timeX = Math.max(0, Math.round((window.viewportWidth - largeFont.measureText(timeLabel)) / 2));
|
||||
image.drawText(largeFont, timeX, 66, timeLabel, 245);
|
||||
|
||||
const state = stopwatchRunningSinceMs === null ? (elapsed > 0 ? "Paused" : "Ready") : "Running";
|
||||
drawCenteredText(image, smallFont, 112, state, 145);
|
||||
|
||||
const labels = [stopwatchRunningSinceMs === null ? "Start" : "Pause", "Reset", `Timers (${timers.length})`];
|
||||
drawButtonRow(image, labels, window.stopwatchAction, 154);
|
||||
drawFooter(image, `${GESTURE_SCROLL} action ${GESTURE_CLICK} select ${GESTURE_DOUBLE_CLICK} back`);
|
||||
}
|
||||
|
||||
function paintTimers(image: GrayImage, window: TimerWindow): void {
|
||||
drawTabs(image, "timers");
|
||||
const items = ["< Stopwatch", "+ New timer", ...timers.map(timerRowLabel)];
|
||||
const listBottom = image.height - FOOTER_HEIGHT;
|
||||
const visibleRows = Math.max(1, Math.floor((listBottom - TIMER_LIST_TOP) / TIMER_ROW_HEIGHT));
|
||||
const first = clamp(window.timerSelection - visibleRows + 1, 0, Math.max(0, items.length - visibleRows));
|
||||
|
||||
for (let row = 0; row < visibleRows; row++) {
|
||||
const index = first + row;
|
||||
if (index >= items.length) break;
|
||||
const y = TIMER_LIST_TOP + row * TIMER_ROW_HEIGHT;
|
||||
const selected = index === window.timerSelection;
|
||||
if (selected) {
|
||||
image.fillRoundedRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 34, 5);
|
||||
image.drawRect(18, y, image.width - 36, TIMER_ROW_HEIGHT - 4, 105);
|
||||
}
|
||||
image.drawText(index >= 2 ? mediumFont : smallFont, 32, y + (index >= 2 ? 2 : 7), items[index]!, selected ? 245 : 180);
|
||||
}
|
||||
|
||||
const selectedTimer = window.timerSelection >= 2 ? timers[window.timerSelection - 2] : null;
|
||||
const action = selectedTimer ? (selectedTimer.expired ? "dismiss" : "cancel") : "select";
|
||||
drawFooter(image, `${GESTURE_SCROLL} timer ${GESTURE_CLICK} ${action} ${GESTURE_DOUBLE_CLICK} back`);
|
||||
}
|
||||
|
||||
function paintEditor(image: GrayImage, window: TimerWindow): void {
|
||||
image.drawText(smallFont, 24, 14, "New timer", 190);
|
||||
const fields = [pad2(window.editorHours), pad2(window.editorMinutes), pad2(window.editorSeconds)];
|
||||
const labels = ["hours", "minutes", "seconds"];
|
||||
const fieldWidth = 118;
|
||||
const gap = 22;
|
||||
const totalWidth = fieldWidth * 3 + gap * 2;
|
||||
const startX = Math.round((image.width - totalWidth) / 2);
|
||||
|
||||
for (let index = 0; index < 3; index++) {
|
||||
const x = startX + index * (fieldWidth + gap);
|
||||
if (window.editorField === index) {
|
||||
image.fillRoundedRect(x, 57, fieldWidth, 68, 35, 6);
|
||||
image.drawRect(x, 57, fieldWidth, 68, 120);
|
||||
}
|
||||
drawCenteredIn(image, largeFont, x, fieldWidth, 65, fields[index]!, window.editorField === index ? 250 : 180);
|
||||
drawCenteredIn(image, smallFont, x, fieldWidth, 105, labels[index]!, 125);
|
||||
if (index < 2) image.drawText(mediumFont, x + fieldWidth + 5, 71, ":", 130);
|
||||
}
|
||||
|
||||
const startSelected = window.editorField === 3;
|
||||
const buttonWidth = 160;
|
||||
const buttonX = Math.round((image.width - buttonWidth) / 2);
|
||||
if (startSelected) {
|
||||
image.fillRoundedRect(buttonX, 151, buttonWidth, 38, 40, 6);
|
||||
image.drawRect(buttonX, 151, buttonWidth, 38, 120);
|
||||
}
|
||||
drawCenteredIn(image, smallFont, buttonX, buttonWidth, 162, "Start timer", startSelected ? 250 : 175);
|
||||
if (window.editorMessage) drawCenteredText(image, smallFont, 202, window.editorMessage, 190);
|
||||
drawFooter(image, `${GESTURE_SCROLL} adjust ${GESTURE_CLICK} next ${GESTURE_DOUBLE_CLICK} cancel`);
|
||||
}
|
||||
|
||||
function drawTabs(image: GrayImage, active: "stopwatch" | "timers"): void {
|
||||
const stopwatch = "Stopwatch";
|
||||
const timerLabel = `Timers (${timers.length})`;
|
||||
const left = 24;
|
||||
const dividerX = left + smallFont.measureText(stopwatch) + 18;
|
||||
image.drawText(smallFont, left, 15, stopwatch, active === "stopwatch" ? 245 : 105);
|
||||
image.drawLine(left, 35, dividerX - 12, 35, active === "stopwatch" ? 200 : 30);
|
||||
image.drawText(smallFont, dividerX, 15, timerLabel, active === "timers" ? 245 : 105);
|
||||
image.drawLine(dividerX, 35, dividerX + smallFont.measureText(timerLabel), 35, active === "timers" ? 200 : 30);
|
||||
}
|
||||
|
||||
function drawButtonRow(image: GrayImage, labels: string[], selected: number, y: number): void {
|
||||
const gap = 12;
|
||||
const margin = 24;
|
||||
const width = Math.floor((image.width - margin * 2 - gap * (labels.length - 1)) / labels.length);
|
||||
for (let index = 0; index < labels.length; index++) {
|
||||
const x = margin + index * (width + gap);
|
||||
if (index === selected) {
|
||||
image.fillRoundedRect(x, y, width, 40, 38, 6);
|
||||
image.drawRect(x, y, width, 40, 115);
|
||||
}
|
||||
drawCenteredIn(image, smallFont, x, width, y + 12, labels[index]!, index === selected ? 245 : 155);
|
||||
}
|
||||
}
|
||||
|
||||
function drawFooter(image: GrayImage, text: string): void {
|
||||
image.drawLine(16, image.height - FOOTER_HEIGHT, image.width - 16, image.height - FOOTER_HEIGHT, 35);
|
||||
drawCenteredText(image, smallFont, image.height - 22, text, 115);
|
||||
}
|
||||
|
||||
function drawCenteredText(image: GrayImage, font: typeof smallFont, y: number, text: string, value: number): void {
|
||||
image.drawText(font, Math.round((image.width - font.measureText(text)) / 2), y, text, value);
|
||||
}
|
||||
|
||||
function drawCenteredIn(
|
||||
image: GrayImage,
|
||||
font: typeof smallFont,
|
||||
x: number,
|
||||
width: number,
|
||||
y: number,
|
||||
text: string,
|
||||
value: number,
|
||||
): void {
|
||||
image.drawText(font, Math.round(x + (width - font.measureText(text)) / 2), y, text, value);
|
||||
}
|
||||
|
||||
function timerRowLabel(timer: CountdownTimer): string {
|
||||
const number = timers.indexOf(timer) + 1;
|
||||
if (timer.expired) return `${number}. 00:00 Finished`;
|
||||
return `${number}. ${formatCountdown(timer.endAtMs - Date.now())} Running`;
|
||||
}
|
||||
|
||||
function renderAndSubmit(window: TimerWindow, inputFrameId: number): void {
|
||||
const frameId = inputFrameId > 0 ? inputFrameId : frameTimings.startFrame(`render:${window.windowId}`);
|
||||
try {
|
||||
const paintStartedAtMs = Date.now();
|
||||
const image = frameTimings.span(frameId, "paint", () =>
|
||||
frameTimings.runWithFrame(frameId, () => paint(window)),
|
||||
);
|
||||
const paintMs = Date.now() - paintStartedAtMs;
|
||||
const fingerprint = image.fingerprint();
|
||||
if (fingerprint === window.lastSubmittedFingerprint) {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer content unchanged");
|
||||
return;
|
||||
}
|
||||
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
|
||||
if (!communicator) {
|
||||
frameTimings.finishFrame(frameId, "discarded: no active communicator");
|
||||
return;
|
||||
}
|
||||
const buffer = image.to8bppBuffer();
|
||||
communicator.submitSurfaceFrame(
|
||||
buffer.buffer,
|
||||
window.surfaceId,
|
||||
0,
|
||||
0,
|
||||
image.width,
|
||||
image.height,
|
||||
fingerprint,
|
||||
paintMs,
|
||||
frameId,
|
||||
);
|
||||
window.lastSubmittedFingerprint = fingerprint;
|
||||
} catch (error) {
|
||||
frameTimings.finishFrame(frameId, "discarded: timer render failed");
|
||||
console.error(`timer worker render failed: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
function stopwatchElapsedMs(): number {
|
||||
const runningMs = stopwatchRunningSinceMs === null ? 0 : Date.now() - stopwatchRunningSinceMs;
|
||||
return stopwatchAccumulatedMs + runningMs;
|
||||
}
|
||||
|
||||
function pad2(value: number): string {
|
||||
return String(value).padStart(2, "0");
|
||||
}
|
||||
|
||||
function formatStopwatchElapsed(totalMs: number): string {
|
||||
const totalTenths = Math.floor(totalMs / 100);
|
||||
const tenths = totalTenths % 10;
|
||||
const totalSeconds = Math.floor(totalTenths / 10);
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
return hours > 0
|
||||
? `${hours}:${pad2(minutes)}:${pad2(seconds)}.${tenths}`
|
||||
: `${pad2(minutes)}:${pad2(seconds)}.${tenths}`;
|
||||
}
|
||||
|
||||
function formatCountdown(remainingMs: number): string {
|
||||
const totalSeconds = Math.max(0, Math.ceil(remainingMs / 1000));
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
return hours > 0 ? `${hours}:${pad2(minutes)}:${pad2(seconds)}` : `${pad2(minutes)}:${pad2(seconds)}`;
|
||||
}
|
||||
|
||||
function formatDuration(durationMs: number): string {
|
||||
const totalSeconds = Math.round(durationMs / 1000);
|
||||
const seconds = totalSeconds % 60;
|
||||
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||
const minutes = totalMinutes % 60;
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
if (hours > 0) return `${hours}h ${minutes}m ${seconds}s`;
|
||||
if (minutes > 0) return `${minutes}m ${seconds}s`;
|
||||
return `${seconds}s`;
|
||||
}
|
||||
|
||||
function clamp(value: number, min: number, max: number): number {
|
||||
return Math.max(min, Math.min(max, value));
|
||||
}
|
||||
Reference in new issue
Block a user