Status bar timer widget is selectable

This commit is contained in:
James Babcock committed 2026-10-14 01:35:15 -07:00
1 parent 132b90c21d
commit 9a0c843bc7
7 files changed
+68 -16

No files matched your search

+1 -1
View File
@@ -8,7 +8,7 @@
* T3 Code client: Add options (worktree, host, etc) when creating threads
* Add configuration options to the glanceboard system card and music player
* Glanceboard compass shows calibration-accuracy indicator
* Status-bar widgets (nightscout, weather) are selectable
* Status-bar widgets (nightscout, weather, timer) are selectable
### Bug fixes
+6 -1
View File
@@ -134,19 +134,24 @@ export type TextEditorHost = {
/**
* A tray widget (see Shell.setTrayIcon) whose window is a main-thread
* singleton app window, opened in the background while the switcher's
* selection is on it; undefined on hosts that can't do that.
* selection is on it; undefined on hosts that can't do that. `show`, if
* given, runs each time the selection moves onto the widget, once the
* window is open, to bring what the widget stands for on screen in it (the
* shell repaints the window after).
*/
export function inProcessTrayWidget(
ctx: AppContext,
windowId: string,
surfaceId: string,
create: (options: InProcessAppOptions) => InProcessWindow,
show?: () => void,
): TrayWidget | undefined {
const open = ctx.openInProcessAppInBackground;
if (!open) return undefined;
return {
openWindow: () => {
const { window, opened } = open(windowId, surfaceId, create);
show?.();
return { window: window.window, opened };
},
};
+4 -3
View File
@@ -1,8 +1,8 @@
import { type AppDefinition } from "../app-definition";
import { inProcessTrayWidget, type AppDefinition } from "../app-definition";
import { shell } from "../../ui/shell/shell";
import { registerTimerTools } from "../../assistant/timer-tools";
import { timerEngine, timersWakeSetting } from "./timer-engine";
import { createTimerAppWindow, startTimersTrayIcon, TIMER_SURFACE_ID, TIMER_WINDOW_ID } from "./timer-app";
import { createTimerAppWindow, showTrayTimer, startTimersTrayIcon, TIMER_SURFACE_ID, TIMER_WINDOW_ID } from "./timer-app";
/**
* Timers, stopwatch and alarms. The clock engine boots with the shell so
@@ -33,7 +33,8 @@ const timerApp: AppDefinition = {
.catch((error) => console.error(`timers ring launch failed: ${error}`));
},
});
startTimersTrayIcon();
// Selected in the switcher, the tray readout shows its timer in the app's window.
startTimersTrayIcon(inProcessTrayWidget(ctx, TIMER_WINDOW_ID, TIMER_SURFACE_ID, createTimerAppWindow, showTrayTimer));
registerTimerTools((appId) => ctx.launchApp(appId));
},
launch: (ctx) => ctx.launchInProcessApp(TIMER_WINDOW_ID, TIMER_SURFACE_ID, createTimerAppWindow),
+34 -4
View File
@@ -39,7 +39,7 @@ import {
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
import { shell } from "../../ui/shell/shell";
import { shell, type TrayWidget } from "../../ui/shell/shell";
import { WindowMenuLayer } from "../../ui/window-menu";
import { clamp } from "../../util/numeric-util";
import {
@@ -68,6 +68,7 @@ import {
formatTimeOfDay,
nearestLadderIndex,
sortAlarms,
soonestRunningTimer,
sortTimers,
stopwatchElapsedMs,
stopwatchIsRunning,
@@ -271,6 +272,19 @@ export class TimersLayer implements Layer {
if (index !== null) this.selectionKey = rows[index]!.key;
}
/**
* Put a timer on the stage, out of any picker: the tray widget's view of
* the timer it counts down. Lands the selection first, if this is before
* the first paint, so that doesn't move it again.
*/
showTimer(timer: CountdownTimer): void {
this.reconcileSelection(this.buildRows(Date.now()));
this.selectionKey = timerKey(timer);
this.mode = { kind: "list" };
this.ringButton = 0;
this.notice = "";
}
/** Called on every engine change: jump to a newly ringing item. */
onEngineChanged(): void {
const ringing = timerEngine.ringingItems();
@@ -1291,8 +1305,12 @@ function pad2(value: number): string {
// ---------------------------------------------------------------------------
// Window
/** The open Timers window's layer, for the tray widget (see showTrayTimer). */
let windowLayer: TimersLayer | null = null;
export function createTimerAppWindow(options: InProcessAppOptions): InProcessWindow {
const layer = new TimersLayer();
windowLayer = layer;
let tick: ReturnType<typeof setInterval> | null = null;
let unsubscribe: (() => void) | null = null;
const stopTick = () => {
@@ -1324,6 +1342,7 @@ export function createTimerAppWindow(options: InProcessAppOptions): InProcessWin
stopTick();
unsubscribe?.();
unsubscribe = null;
if (windowLayer === layer) windowLayer = null;
options.onClosed();
},
});
@@ -1348,9 +1367,10 @@ let trayTick: ReturnType<typeof setInterval> | null = null;
/**
* Keep a small "soonest timer" readout in the top bar while a timer runs,
* so any app can glance at it. Minute granularity until the last minute,
* so the shell isn't repainted every second for an hour.
* so the shell isn't repainted every second for an hour. `widget` makes it
* selectable in the switcher (see showTrayTimer).
*/
export function startTimersTrayIcon(): void {
export function startTimersTrayIcon(widget?: TrayWidget): void {
const sync = () => {
const remaining = soonestRunningTimerRemainingMs(timerEngine);
let text: string | null = null;
@@ -1359,7 +1379,7 @@ export function startTimersTrayIcon(): void {
}
if (text === trayText) return;
trayText = text;
shell.setTrayIcon("timer", text === null ? null : renderTrayIcon(text));
shell.setTrayIcon("timer", text === null ? null : renderTrayIcon(text), widget);
if (text !== null && trayTick === null) {
trayTick = setInterval(sync, 1000);
} else if (text === null && trayTick !== null) {
@@ -1371,6 +1391,16 @@ export function startTimersTrayIcon(): void {
sync();
}
/**
* The tray widget's view: the open Timers window with the timer the tray
* icon counts down on its stage. Called each time the switcher's selection
* moves onto the widget, once the window is open.
*/
export function showTrayTimer(): void {
const timer = soonestRunningTimer(timerEngine.state.timers, Date.now());
if (timer) windowLayer?.showTimer(timer);
}
function renderTrayIcon(text: string): GrayImage {
const font = getFont("terminus12");
const dial = 12;
+3 -7
View File
@@ -44,6 +44,7 @@ import {
parseTimersState,
pushRecentDuration,
serializeTimersState,
soonestRunningTimer,
stopwatchElapsedMs,
timerRemainingMs,
type Alarm,
@@ -685,13 +686,8 @@ function alarmNotificationText(alarm: Alarm): string {
/** Remaining time of the timer that ends soonest, for the tray icon; null when none runs. */
export function soonestRunningTimerRemainingMs(engine: TimerEngine, now = Date.now()): number | null {
let soonest: number | null = null;
for (const timer of engine.state.timers) {
if (timer.endsAtMs === null || timer.rungAtMs !== null) continue;
const remaining = timerRemainingMs(timer, now);
if (soonest === null || remaining < soonest) soonest = remaining;
}
return soonest;
const timer = soonestRunningTimer(engine.state.timers, now);
return timer ? timerRemainingMs(timer, now) : null;
}
export const timerEngine = new TimerEngine();
+10
View File
@@ -185,6 +185,16 @@ export function sortTimers(timers: readonly CountdownTimer[], nowMs: number): Co
});
}
/** The running timer that ends soonest (the one the tray icon counts down); null when none runs. */
export function soonestRunningTimer(timers: readonly CountdownTimer[], nowMs: number): CountdownTimer | null {
let soonest: CountdownTimer | null = null;
for (const timer of timers) {
if (timer.endsAtMs === null || timer.rungAtMs !== null) continue;
if (!soonest || timerRemainingMs(timer, nowMs) < timerRemainingMs(soonest, nowMs)) soonest = timer;
}
return soonest;
}
export function pushRecentDuration(recent: readonly number[], durationMs: number): number[] {
return [durationMs, ...recent.filter((value) => value !== durationMs)].slice(0, RECENT_DURATION_COUNT);
}
+10
View File
@@ -113,6 +113,16 @@ test("timers sort ringing first, then soonest running, then paused", () => {
assert.deepEqual(model.sortTimers([running, paused, rung, sooner], now).map((timer) => timer.id), [4, 2, 1, 3]);
});
test("the tray counts down the running timer that ends soonest", () => {
const now = 1_000_000;
const running = { id: 1, label: "", durationMs: 600_000, endsAtMs: now + 300_000, pausedRemainingMs: null, rungAtMs: null, createdAtMs: 1 };
const sooner = { ...running, id: 2, endsAtMs: now + 60_000 };
const paused = { ...running, id: 3, endsAtMs: null, pausedRemainingMs: 10_000 };
const rung = { ...running, id: 4, endsAtMs: now - 5000, rungAtMs: now - 5000 };
assert.equal(model.soonestRunningTimer([running, paused, rung, sooner], now)?.id, 2);
assert.equal(model.soonestRunningTimer([paused, rung], now), null);
});
test("tool arguments: times of day", () => {
assert.deepEqual(model.parseTimeOfDay("7:30"), { hour: 7, minute: 30 });
assert.deepEqual(model.parseTimeOfDay("7:30 pm"), { hour: 19, minute: 30 });