forked from Blendman974/faceclaw
Merge pull request #68 from mwlaboratories/calendar-event-end
Calendar: include the end of each event, and list ongoing multi-day events under today
This commit is contained in:
8 files changed
+211
-30
No files matched your search
@@ -9,6 +9,7 @@ import { hasCalendarPermission } from "../../native/calendar-permissions";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { lineStep } from "../../ui/metrics";
|
||||
import type { Viewport } from "../../ui/viewport";
|
||||
import { listingDayMs, spanLabel, spansDays } from "./event-days";
|
||||
|
||||
/** How far a row's text sits inside its selection box; the title lines up with it. */
|
||||
const ROW_TEXT_INSET = 10;
|
||||
@@ -143,14 +144,20 @@ export class CalendarLayer implements Layer {
|
||||
function buildEventRows(font: UiFont, events: CalendarEvent[]): EventRow[] {
|
||||
const rows: EventRow[] = [];
|
||||
let previousDayKey = "";
|
||||
const nowMs = Date.now();
|
||||
for (const event of events) {
|
||||
const dayKey = dayKeyOf(event.startMs);
|
||||
const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(event.startMs);
|
||||
// An event still going since an earlier day is listed under today, not its first day.
|
||||
const listedMs = listingDayMs(event, nowMs);
|
||||
const dayKey = dayKeyOf(listedMs);
|
||||
const dayHeader = dayKey === previousDayKey ? null : dayHeaderLabel(listedMs);
|
||||
previousDayKey = dayKey;
|
||||
|
||||
const lines: string[] = [];
|
||||
const timeLabel = event.allDay ? "All day" : formatEventTime(event.startMs);
|
||||
lines.push(`${timeLabel} ${event.title || "(untitled)"}`);
|
||||
if (spansDays(event)) {
|
||||
lines.push(spanLabel(event));
|
||||
}
|
||||
if (event.location) {
|
||||
lines.push(event.location);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Which day an event is listed under, and how a multi-day span reads. Pure (no
|
||||
* native or UI imports) so it can be unit-tested.
|
||||
*/
|
||||
|
||||
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
|
||||
export type DayEvent = { startMs: number; endMs: number; allDay: boolean };
|
||||
|
||||
function midnight(timestampMs: number): number {
|
||||
const date = new Date(timestampMs);
|
||||
return new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
|
||||
}
|
||||
|
||||
/** The last moment the event covers: all-day ends are exclusive (the midnight after its last day). */
|
||||
function lastMomentMs(event: DayEvent): number {
|
||||
return event.allDay ? Math.max(event.startMs, event.endMs - 1) : Math.max(event.startMs, event.endMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* The day to list an event under: its start, or today for one that began on an
|
||||
* earlier day and is still going. Without this, the third day of a three-day
|
||||
* event shows under its first day and never under today.
|
||||
*/
|
||||
export function listingDayMs(event: DayEvent, nowMs: number): number {
|
||||
return Math.max(midnight(event.startMs), Math.min(midnight(nowMs), midnight(lastMomentMs(event))));
|
||||
}
|
||||
|
||||
/** Whether the event covers more than one calendar day. */
|
||||
export function spansDays(event: DayEvent): boolean {
|
||||
return midnight(lastMomentMs(event)) > midnight(event.startMs);
|
||||
}
|
||||
|
||||
/** "Mon 16 – Wed 18 Nov", or with both months when they differ ("Mon 30 Nov – Tue 1 Dec"). */
|
||||
export function spanLabel(event: DayEvent): string {
|
||||
const start = new Date(event.startMs);
|
||||
const last = new Date(lastMomentMs(event));
|
||||
const day = (date: Date) => `${WEEKDAYS[date.getDay()]} ${date.getDate()}`;
|
||||
return start.getMonth() === last.getMonth() && start.getFullYear() === last.getFullYear()
|
||||
? `${day(start)} – ${day(last)} ${MONTHS[last.getMonth()]}`
|
||||
: `${day(start)} ${MONTHS[start.getMonth()]} – ${day(last)} ${MONTHS[last.getMonth()]}`;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Date and calendar-event text for the assistant's tool results. Pure (no
|
||||
* native imports) so it can be unit-tested.
|
||||
*/
|
||||
|
||||
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
|
||||
export type FormattableEvent = {
|
||||
title: string;
|
||||
startMs: number;
|
||||
endMs: number;
|
||||
allDay: boolean;
|
||||
location?: string;
|
||||
};
|
||||
|
||||
export function formatEvent(event: FormattableEvent): string {
|
||||
const location = event.location ? ` @ ${event.location}` : "";
|
||||
return `- ${event.title || "(untitled)"} — ${formatEventSpan(event)}${location}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* When an event happens, end included: a multi-day all-day event names its last
|
||||
* day (all-day ends are exclusive, at the midnight after it), a timed event its
|
||||
* end time. Without the end, an event that started yesterday and runs through
|
||||
* tomorrow reads as yesterday's.
|
||||
*/
|
||||
export function formatEventSpan(event: Pick<FormattableEvent, "startMs" | "endMs" | "allDay">): string {
|
||||
const start = new Date(event.startMs);
|
||||
if (event.allDay) {
|
||||
const lastDay = new Date(Math.max(event.startMs, event.endMs - 1));
|
||||
return sameDay(start, lastDay)
|
||||
? `${formatDate(start)} (all day)`
|
||||
: `${formatDate(start)} – ${formatDate(lastDay)} (all day)`;
|
||||
}
|
||||
if (!(event.endMs > event.startMs)) return formatDateTime(start);
|
||||
const end = new Date(event.endMs);
|
||||
return sameDay(start, end)
|
||||
? `${formatDateTime(start)} – ${formatTime(end)}`
|
||||
: `${formatDateTime(start)} – ${formatDateTime(end)}`;
|
||||
}
|
||||
|
||||
export function formatDateTime(date: Date): string {
|
||||
return `${formatDate(date)}, ${formatTime(date)}`;
|
||||
}
|
||||
|
||||
export function formatDate(date: Date): string {
|
||||
return `${WEEKDAYS[date.getDay()]} ${MONTHS[date.getMonth()]} ${date.getDate()}`;
|
||||
}
|
||||
|
||||
export function formatTime(date: Date): string {
|
||||
let hours = date.getHours();
|
||||
const minutes = date.getMinutes().toString().padStart(2, "0");
|
||||
const meridiem = hours >= 12 ? "PM" : "AM";
|
||||
hours = hours % 12;
|
||||
if (hours === 0) hours = 12;
|
||||
return `${hours}:${minutes} ${meridiem}`;
|
||||
}
|
||||
|
||||
function sameDay(a: Date, b: Date): boolean {
|
||||
return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { readUpcomingEventsAsync, type CalendarEvent } from "../native/calendar";
|
||||
import { readUpcomingEventsAsync } from "../native/calendar";
|
||||
import { hasCalendarPermission } from "../native/calendar-permissions";
|
||||
import { mediaControllerBridge } from "../native/media-controller";
|
||||
import { spotifyService } from "../native/spotify";
|
||||
import { dismissNotification, readActiveNotifications } from "../native/notification-icons";
|
||||
import { shell } from "../ui/shell/shell";
|
||||
import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry";
|
||||
import { formatDateTime, formatEvent } from "./date-format";
|
||||
|
||||
/**
|
||||
* Registers the always-available system tools into the registry. Called once at
|
||||
@@ -12,9 +13,6 @@ import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registr
|
||||
* media, notifications); nothing here needs an app window to be open.
|
||||
*/
|
||||
|
||||
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||
|
||||
let registered = false;
|
||||
|
||||
export function registerSystemTools(registry: ToolRegistry = toolRegistry): void {
|
||||
@@ -244,35 +242,12 @@ function describeNowPlaying(): string {
|
||||
return "Nothing is currently playing.";
|
||||
}
|
||||
|
||||
function formatEvent(event: CalendarEvent): string {
|
||||
const when = event.allDay ? `${formatDate(new Date(event.startMs))} (all day)` : formatDateTime(new Date(event.startMs));
|
||||
const location = event.location ? ` @ ${event.location}` : "";
|
||||
return `- ${event.title || "(untitled)"} — ${when}${location}`;
|
||||
}
|
||||
|
||||
function formatNotification(notification: { key: string; appName: string; title: string; text: string; customText?: string[] }): string {
|
||||
const title = notification.title || notification.customText?.join(" ") || "(no title)";
|
||||
const body = notification.text ? `: ${notification.text}` : "";
|
||||
return `- [${notification.appName}] ${title}${body} (key: ${notification.key})`;
|
||||
}
|
||||
|
||||
function formatDateTime(date: Date): string {
|
||||
return `${formatDate(date)}, ${formatTime(date)}`;
|
||||
}
|
||||
|
||||
function formatDate(date: Date): string {
|
||||
return `${WEEKDAYS[date.getDay()]} ${MONTHS[date.getMonth()]} ${date.getDate()}`;
|
||||
}
|
||||
|
||||
function formatTime(date: Date): string {
|
||||
let hours = date.getHours();
|
||||
const minutes = date.getMinutes().toString().padStart(2, "0");
|
||||
const meridiem = hours >= 12 ? "PM" : "AM";
|
||||
hours = hours % 12;
|
||||
if (hours === 0) hours = 12;
|
||||
return `${hours}:${minutes} ${meridiem}`;
|
||||
}
|
||||
|
||||
function clampNumber(value: unknown, min: number, max: number, fallback: number): number {
|
||||
const num = typeof value === "number" ? value : Number(value);
|
||||
if (!Number.isFinite(num)) return fallback;
|
||||
|
||||
Reference in new issue
Block a user