Voice input suspends the screen-off timer; add menu to Nightscout; fix defocused highlight style in Terminal menu

This commit is contained in:
James Babcock committed 2026-07-21 09:40:43 -07:00
1 parent 026a8ed439
commit 3452945322
5 files changed
+87 -30

No files matched your search

+42 -5
View File
@@ -2,8 +2,15 @@ import { BdfFont, getDefaultLargeFont, getDefaultSmallFont } from "../../graphic
import { GrayImage, imageFromAsciiArt } from "../../graphics/image"; import { GrayImage, imageFromAsciiArt } from "../../graphics/image";
import { DashboardInputEvent, Layer, LayerContext } from "../layers"; import { DashboardInputEvent, Layer, LayerContext } from "../layers";
import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge"; import { nightscoutBridge, type NightscoutState } from "../../native/nightscout-bridge";
import { isNightscoutSettingsConfigured } from "../dashboard-settings"; import {
isNightscoutSettingsConfigured,
nightscoutApiTokenSetting,
nightscoutSiteUrlSetting,
textSettingMenuItem,
} from "../dashboard-settings";
import { formatAgeShortFromTimestamp, formatTimestamp } from "~/util/date-util"; import { formatAgeShortFromTimestamp, formatTimestamp } from "~/util/date-util";
import { MenuLayer } from "../menu";
import { openSettingsSubMenu } from "../dashboard/settings-panel";
import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../gestures"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../gestures";
const nightscoutLargeFont = getDefaultLargeFont(); const nightscoutLargeFont = getDefaultLargeFont();
@@ -299,14 +306,15 @@ export class NightscoutLayer implements Layer {
if (!isNightscoutSettingsConfigured() || nightscout.configurationMissing) { if (!isNightscoutSettingsConfigured() || nightscout.configurationMissing) {
image.drawText(font, 22, 44, "Nightscout needs configuration.", 180); image.drawText(font, 22, 44, "Nightscout needs configuration.", 180);
image.drawText(font, 22, 62, "Use Settings > Integrations > Nightscout.", 140); image.drawText(font, 22, 62, `Set the site URL and API token in ${GESTURE_CLICK} menu > Settings.`, 140);
image.drawText(font, 22, footerY, `${GESTURE_CLICK} menu ${GESTURE_DOUBLE_CLICK} back`, 110);
return image; return image;
} }
if (!nightscout.available || !nightscout.latest) { if (!nightscout.available || !nightscout.latest) {
image.drawText(font, 22, 44, "No Nightscout data available.", 180); image.drawText(font, 22, 44, "No Nightscout data available.", 180);
image.drawText(font, 22, 58, truncateLine(nightscout.status, 60), 140); image.drawText(font, 22, 58, truncateLine(nightscout.status, 60), 140);
image.drawText(font, 22, footerY, `${GESTURE_CLICK} refresh ${GESTURE_DOUBLE_CLICK} back`, 110); image.drawText(font, 22, footerY, `${GESTURE_CLICK} menu ${GESTURE_DOUBLE_CLICK} back`, 110);
return image; return image;
} }
@@ -340,14 +348,14 @@ export class NightscoutLayer implements Layer {
const graphHeight = Math.max(40, height - 128 - 48); const graphHeight = Math.max(40, height - 128 - 48);
drawNightscoutGraph(image, { x: 22, y: 128, width: width - 44, height: graphHeight }, nightscout, nowMs, font); drawNightscoutGraph(image, { x: 22, y: 128, width: width - 44, height: graphHeight }, nightscout, nowMs, font);
image.drawText(font, 22, height - 42, "2-hour glucose history with basal / carbs / boluses", 130); image.drawText(font, 22, height - 42, "2-hour glucose history with basal / carbs / boluses", 130);
image.drawText(font, 22, footerY, `${GESTURE_CLICK} refresh ${GESTURE_DOUBLE_CLICK} back`, 110); image.drawText(font, 22, footerY, `${GESTURE_CLICK} menu ${GESTURE_DOUBLE_CLICK} back`, 110);
return image; return image;
} }
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> { async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
switch (event.type) { switch (event.type) {
case "click": case "click":
await nightscoutBridge.refreshNow(); ctx.stack.push(createNightscoutMenu());
return; return;
case "double-click": case "double-click":
ctx.stack.pop(); ctx.stack.pop();
@@ -358,6 +366,35 @@ export class NightscoutLayer implements Layer {
} }
} }
/** The app menu (opened with a click); MenuLayer self-closes on double-click. */
function createNightscoutMenu(): MenuLayer {
return new MenuLayer(
"Nightscout",
[
{
label: "Refresh",
onSelect: async (ctx) => {
ctx.stack.pop();
await nightscoutBridge.refreshNow();
},
},
{
label: "Settings",
onSelect: (ctx) => {
// Pop the menu first so closing the settings modal lands back on
// the glucose view, not this menu.
ctx.stack.pop();
openSettingsSubMenu(ctx, "Nightscout settings", [
textSettingMenuItem(nightscoutSiteUrlSetting),
textSettingMenuItem(nightscoutApiTokenSetting),
]);
},
},
],
{ x: 8, y: 8, width: 200, minHeight: 0 },
);
}
function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean { function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS; return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
} }
+1 -17
View File
@@ -18,12 +18,10 @@ import {
toggleSettingMenuItem, toggleSettingMenuItem,
uiFontSetting, uiFontSetting,
voiceProviderSetting, voiceProviderSetting,
nightscoutSiteUrlSetting,
nightscoutApiTokenSetting,
screenTimeoutSetting, screenTimeoutSetting,
wakeWordActionSetting, wakeWordActionSetting,
} from "../dashboard-settings"; } from "../dashboard-settings";
import { openSettingsSubMenu, SettingsPanelLayer, type SettingsSection } from "./settings-panel"; import { SettingsPanelLayer, type SettingsSection } from "./settings-panel";
/** The Settings app's master-detail panel (sections on the left, contents on the right). */ /** The Settings app's master-detail panel (sections on the left, contents on the right). */
export function createSettingsPanelLayer(): SettingsPanelLayer { export function createSettingsPanelLayer(): SettingsPanelLayer {
@@ -63,20 +61,6 @@ function settingsSections(): SettingsSection[] {
textSettingMenuItem(anthropicApiKeySetting), textSettingMenuItem(anthropicApiKeySetting),
], ],
}, },
{
label: "Integrations",
items: [
{
label: "Nightscout",
onSelect: (ctx) => {
openSettingsSubMenu(ctx, "Nightscout", [
textSettingMenuItem(nightscoutSiteUrlSetting),
textSettingMenuItem(nightscoutApiTokenSetting),
]);
},
},
],
},
{ {
label: "Terminal", label: "Terminal",
items: [ items: [
+9
View File
@@ -273,6 +273,13 @@ class Shell {
applyScreenTimeout(nowMs = Date.now()): boolean { applyScreenTimeout(nowMs = Date.now()): boolean {
const timeoutMs = this.config.getScreenTimeoutMs(); const timeoutMs = this.config.getScreenTimeoutMs();
if (timeoutMs === null || !this.screenOn) return false; if (timeoutMs === null || !this.screenOn) return false;
// An open voice dialog suspends the timeout: a long dictation or refine
// has no button presses, but the screen must stay on for it. Sliding
// lastInputAtMs forward also restarts the full timeout when it closes.
if (this.activeVoiceLayer) {
this.lastInputAtMs = nowMs;
return false;
}
if (nowMs - this.lastInputAtMs < timeoutMs) return false; if (nowMs - this.lastInputAtMs < timeoutMs) return false;
this.sleep(); this.sleep();
return true; return true;
@@ -451,6 +458,8 @@ class Shell {
() => { () => {
if (this.activeVoiceLayer === layer) { if (this.activeVoiceLayer === layer) {
this.activeVoiceLayer = null; this.activeVoiceLayer = null;
// The idle countdown restarts in full once voice input ends.
this.noteUserActivity();
} }
}, },
(text) => this.sendTextToForegroundWindow(text), (text) => this.sendTextToForegroundWindow(text),
+17 -6
View File
@@ -12,10 +12,10 @@ import { shell, type ShellWindow } from "./shell";
export type WorkerAppMessage = export type WorkerAppMessage =
| { type: "open-window"; windowId: string; surfaceId: string; viewport: { width: number; height: number } } | { type: "open-window"; windowId: string; surfaceId: string; viewport: { width: number; height: number } }
| { type: "close-window"; windowId: string } | { type: "close-window"; windowId: string }
| { type: "input"; windowId: string; event: unknown; frameId: number } | { type: "input"; windowId: string; event: unknown; frameId: number; focused: boolean }
| { type: "text-input"; windowId: string; text: string } | { type: "text-input"; windowId: string; text: string }
| { type: "render"; windowId: string } | { type: "render"; windowId: string; focused: boolean }
| { type: "foreground"; windowId: string; foreground: boolean } | { type: "foreground"; windowId: string; foreground: boolean; focused: boolean }
| { type: "screen"; on: boolean }; | { type: "screen"; on: boolean };
export type WorkerAppReply = export type WorkerAppReply =
@@ -153,10 +153,16 @@ export class WorkerAppHost {
}, },
drawIcon: windowIcon(spec.icon, spec.iconLetter), drawIcon: windowIcon(spec.icon, spec.iconLetter),
handleInput: (event, frameId) => { handleInput: (event, frameId) => {
this.post({ type: "input", windowId: spec.windowId, event, frameId }); this.post({
type: "input",
windowId: spec.windowId,
event,
frameId,
focused: shell.isWindowFocused(spec.windowId),
});
}, },
requestRender: () => { requestRender: () => {
this.post({ type: "render", windowId: spec.windowId }); this.post({ type: "render", windowId: spec.windowId, focused: shell.isWindowFocused(spec.windowId) });
}, },
receiveTextInput: (text) => { receiveTextInput: (text) => {
this.post({ type: "text-input", windowId: spec.windowId, text }); this.post({ type: "text-input", windowId: spec.windowId, text });
@@ -166,7 +172,12 @@ export class WorkerAppHost {
if (foreground) { if (foreground) {
shell.setWindowAttention(spec.windowId, false); shell.setWindowAttention(spec.windowId, false);
} }
this.post({ type: "foreground", windowId: spec.windowId, foreground }); this.post({
type: "foreground",
windowId: spec.windowId,
foreground,
focused: shell.isWindowFocused(spec.windowId),
});
}, },
setScreenOn: (on) => { setScreenOn: (on) => {
// Screen state is per-app, but sending per-window keeps the protocol // Screen state is per-app, but sending per-window keeps the protocol
+18 -2
View File
@@ -50,6 +50,12 @@ type BaseWindow = {
windowId: string; windowId: string;
surfaceId: string; surfaceId: string;
foreground: boolean; foreground: boolean;
/**
* Whether this window is the shell's input target (vs. the sidebar having
* focus). Pushed by the shell on every input/render/foreground message;
* every focus transition triggers one of those, so it never goes stale.
*/
focused: boolean;
renderScheduled: boolean; renderScheduled: boolean;
lastSubmittedFingerprint: string; lastSubmittedFingerprint: string;
}; };
@@ -119,6 +125,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
frameTimings.finishFrame(message.frameId, "discarded: unknown terminal window"); frameTimings.finishFrame(message.frameId, "discarded: unknown terminal window");
break; break;
} }
window.focused = message.focused;
handleInput(window, message.event as DashboardInputEvent, message.frameId); handleInput(window, message.event as DashboardInputEvent, message.frameId);
break; break;
} }
@@ -134,13 +141,16 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
} }
case "render": { case "render": {
const window = windows.get(message.windowId); const window = windows.get(message.windowId);
if (window) renderAndSubmit(window, 0); if (!window) break;
window.focused = message.focused;
renderAndSubmit(window, 0);
break; break;
} }
case "foreground": { case "foreground": {
const window = windows.get(message.windowId); const window = windows.get(message.windowId);
if (!window) break; if (!window) break;
window.foreground = message.foreground; window.foreground = message.foreground;
window.focused = message.focused;
if (window.foreground) renderAndSubmit(window, 0); if (window.foreground) renderAndSubmit(window, 0);
break; break;
} }
@@ -182,6 +192,7 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb
windowId, windowId,
surfaceId, surfaceId,
foreground: false, foreground: false,
focused: false,
renderScheduled: false, renderScheduled: false,
lastSubmittedFingerprint: "", lastSubmittedFingerprint: "",
selectedIndex: 0, selectedIndex: 0,
@@ -270,6 +281,7 @@ function createViewWindow(windowId: string, surfaceId: string, socket: string, l
windowId, windowId,
surfaceId, surfaceId,
foreground: false, foreground: false,
focused: false,
renderScheduled: false, renderScheduled: false,
lastSubmittedFingerprint: "", lastSubmittedFingerprint: "",
socket, socket,
@@ -504,7 +516,11 @@ function paintHub(window: HubWindow): GrayImage {
if (y + HUB_ROW_HEIGHT > viewportHeight - 30) break; if (y + HUB_ROW_HEIGHT > viewportHeight - 30) break;
const selected = index === window.selectedIndex; const selected = index === window.selectedIndex;
if (selected) { if (selected) {
image.fillRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 15); // Match the shell convention: fill only when this window has focus, so
// an outline-only selection signals the sidebar owns input.
if (window.focused) {
image.fillRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 15);
}
image.drawRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 45); image.drawRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 45);
} }
image.drawText(terminalFont, 32, y + 2, items[index]!.label, selected ? 255 : 200); image.drawText(terminalFont, 32, y + 2, items[index]!.label, selected ? 255 : 200);