Add Timer, Weather apps; build out UI of music app; add wakeword action setting

This commit is contained in:
James Babcock committed 2026-07-20 02:04:21 -07:00
1 parent 008f2277ca
commit 5c73ba2a4d
27 files changed
+2181 -312

No files matched your search

+3 -3
View File
@@ -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();
});
}
+202
View File
@@ -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}...`;
}
+17 -1
View File
@@ -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",
+7 -3
View File
@@ -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
View File
@@ -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 };
+6 -1
View File
@@ -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
View File
@@ -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
View File
@@ -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");
+4 -7
View File
@@ -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;
+56
View File
@@ -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;
}