forked from Blendman974/faceclaw
Merge branch 'nightscout-missed-bolus-detector'
This commit is contained in:
commit
fedc7fed67
14 files changed
+1012
-68
No files matched your search
@@ -1176,6 +1176,23 @@ export const nightscoutAlwaysShowInTopBarSetting = new ConfigSettingBoolean({
|
||||
defaultValue: false,
|
||||
description: "Keep the Nightscout glucose graph and warnings in the top bar even when all Nightscout windows are closed.",
|
||||
});
|
||||
export const nightscoutMissedBolusAlertsSetting = new ConfigSettingBoolean({
|
||||
id: "nightscout-missed-bolus-alerts",
|
||||
label: "Missed-bolus alerts",
|
||||
storageKey: "integrations.nightscout.missedBolusAlerts",
|
||||
defaultValue: false,
|
||||
description: "Show a notification on the glasses when glucose rises like an unannounced meal with no bolus or carbs entered. Experimental.",
|
||||
});
|
||||
export const nightscoutMissedBolusCooldownSetting = new ConfigSettingString({
|
||||
id: "nightscout-missed-bolus-cooldown-minutes",
|
||||
label: "Missed-bolus alert cooldown",
|
||||
storageKey: "integrations.nightscout.missedBolusCooldownMinutes",
|
||||
defaultValue: "120",
|
||||
editorTitle: "Missed-bolus alert cooldown (min)",
|
||||
normalize: normalizeNightscoutThreshold,
|
||||
formatValue: (value) => `${value} min`,
|
||||
description: "After a missed-bolus alert, wait at least this many minutes before showing another.",
|
||||
});
|
||||
|
||||
export function loadNightscoutThresholds(): NightscoutThresholds {
|
||||
return {
|
||||
|
||||
+61
-39
@@ -190,22 +190,7 @@ export class NotificationsListLayer implements Layer {
|
||||
*/
|
||||
export class SingleNotificationLayer implements Layer {
|
||||
/** The action column; its items are rebuilt from the live notification on every paint and input. */
|
||||
private readonly detailMenu = new Menu<DetailMenuItem>({
|
||||
wrap: false,
|
||||
rowGap: DETAIL_MENU_ROW_GAP,
|
||||
highlight: { radius: 6 },
|
||||
getHeight: (item, width) => {
|
||||
const font = getDefaultSmallFont();
|
||||
return detailMenuRowLines(font, item, width).length * lineStep(font) + 8;
|
||||
},
|
||||
draw: ({ image, item, x, y, width, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const lines = detailMenuRowLines(font, item, width);
|
||||
for (let line = 0; line < lines.length; line++) {
|
||||
image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185);
|
||||
}
|
||||
},
|
||||
});
|
||||
private readonly detailMenu = createNotificationActionMenu<DetailMenuItem>((item) => item.kind === "disable-source");
|
||||
private confirmation: { source: AndroidNotification; menu: Menu<ConfirmationItem> } | null = null;
|
||||
// The last paint, which a tray-hosted layer repeats once its notification
|
||||
// is gone, until the tray swaps in a neighbour's (see closeUnavailableNotification).
|
||||
@@ -234,7 +219,7 @@ export class SingleNotificationLayer implements Layer {
|
||||
|
||||
this.detailMenu.setItems(buildDetailMenu(notification, this.options.origin));
|
||||
drawDetailContent(image, font, notification, iconForNotification(notification.key), width, height);
|
||||
this.detailMenu.paint(image, detailMenuBox(width, height), ctx.stack.isFocused());
|
||||
this.detailMenu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused());
|
||||
this.lastImage = image;
|
||||
return image;
|
||||
}
|
||||
@@ -432,20 +417,9 @@ function drawDetailContent(
|
||||
width: number,
|
||||
height: number,
|
||||
): void {
|
||||
const contentX = DETAIL_CONTENT_X;
|
||||
const menuX = width - DETAIL_MENU_WIDTH - 24;
|
||||
const contentWidth = menuX - contentX - 20;
|
||||
image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220);
|
||||
// The header row (icon + app name) and the body stack below the title's
|
||||
// line box, so a large UI font pushes them down instead of overlapping.
|
||||
const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP;
|
||||
const headerHeight = Math.max(ICON_SIZE, font.lineHeight);
|
||||
let appLineX = contentX;
|
||||
if (icon) {
|
||||
image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true });
|
||||
appLineX = contentX + ICON_SIZE + ICON_TEXT_GAP;
|
||||
}
|
||||
image.drawText(font, appLineX, centeredTextY(font, headerTop, headerHeight), `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, 150);
|
||||
const contentX = NOTIFICATION_CONTENT_X;
|
||||
const contentWidth = notificationContentWidth(width);
|
||||
const bodyTop = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`);
|
||||
|
||||
const lines: string[] = [];
|
||||
lines.push(...wrapText(font, notification.title || "(untitled)", contentWidth));
|
||||
@@ -461,7 +435,6 @@ function drawDetailContent(
|
||||
}
|
||||
|
||||
const step = lineStep(font);
|
||||
const bodyTop = headerTop + headerHeight + DETAIL_BODY_GAP;
|
||||
const maxLines = Math.max(1, ((height - bodyTop - step) / step) | 0);
|
||||
for (let index = 0; index < Math.min(lines.length, maxLines); index++) {
|
||||
const line = lines[index]!;
|
||||
@@ -472,8 +445,63 @@ function drawDetailContent(
|
||||
}
|
||||
}
|
||||
|
||||
/** Left edge of a notification detail view's content column. */
|
||||
export const NOTIFICATION_CONTENT_X = DETAIL_CONTENT_X;
|
||||
|
||||
/** Width of a notification detail view's content column, left of the action column. */
|
||||
export function notificationContentWidth(width: number): number {
|
||||
return width - DETAIL_MENU_WIDTH - 24 - NOTIFICATION_CONTENT_X - 20;
|
||||
}
|
||||
|
||||
/**
|
||||
* The top of a notification detail view: the "Notification" title, then the
|
||||
* source's icon and label. Returns the y where the body starts.
|
||||
*/
|
||||
export function drawNotificationHeader(image: GrayImage, font: UiFont, icon: GrayImage | null, sourceLabel: string): number {
|
||||
const contentX = NOTIFICATION_CONTENT_X;
|
||||
image.drawText(font, contentX, DETAIL_TITLE_Y, "Notification", 220);
|
||||
// The header row (icon + app name) and the body stack below the title's
|
||||
// line box, so a large UI font pushes them down instead of overlapping.
|
||||
const headerTop = DETAIL_TITLE_Y + font.lineHeight + DETAIL_HEADER_GAP;
|
||||
const headerHeight = Math.max(ICON_SIZE, font.lineHeight);
|
||||
let sourceX = contentX;
|
||||
if (icon) {
|
||||
image.bitBlt(icon, contentX, headerTop + ((headerHeight - ICON_SIZE) >> 1), { transparentZero: true });
|
||||
sourceX = contentX + ICON_SIZE + ICON_TEXT_GAP;
|
||||
}
|
||||
image.drawText(font, sourceX, centeredTextY(font, headerTop, headerHeight), sourceLabel, 150);
|
||||
return headerTop + headerHeight + DETAIL_BODY_GAP;
|
||||
}
|
||||
|
||||
/**
|
||||
* A notification detail view's right-hand action column. Rows truncate their
|
||||
* label to one line, except those `wraps` picks, which wrap it.
|
||||
*/
|
||||
export function createNotificationActionMenu<T extends { label: string }>(wraps: (item: T) => boolean = () => false): Menu<T> {
|
||||
const rowLines = (font: UiFont, item: T, rowWidth: number): string[] => {
|
||||
const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X;
|
||||
return wraps(item) ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)];
|
||||
};
|
||||
return new Menu<T>({
|
||||
wrap: false,
|
||||
rowGap: DETAIL_MENU_ROW_GAP,
|
||||
highlight: { radius: 6 },
|
||||
getHeight: (item, width) => {
|
||||
const font = getDefaultSmallFont();
|
||||
return rowLines(font, item, width).length * lineStep(font) + 8;
|
||||
},
|
||||
draw: ({ image, item, x, y, width, selected }) => {
|
||||
const font = getDefaultSmallFont();
|
||||
const lines = rowLines(font, item, width);
|
||||
for (let line = 0; line < lines.length; line++) {
|
||||
image.drawText(font, x + DETAIL_MENU_TEXT_X, y + DETAIL_MENU_TEXT_Y + line * lineStep(font), lines[line]!, selected ? 255 : 185);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** The action column's row boxes: right of the content, from DETAIL_MENU_TOP to the bottom margin. */
|
||||
function detailMenuBox(width: number, height: number): MenuBox {
|
||||
export function notificationActionMenuBox(width: number, height: number): MenuBox {
|
||||
return {
|
||||
x: width - DETAIL_MENU_WIDTH - 32,
|
||||
y: DETAIL_MENU_TOP,
|
||||
@@ -482,12 +510,6 @@ function detailMenuBox(width: number, height: number): MenuBox {
|
||||
};
|
||||
}
|
||||
|
||||
/** An action row's text lines: the disable-source row wraps, the rest truncate to one line. */
|
||||
function detailMenuRowLines(font: UiFont, item: DetailMenuItem, rowWidth: number): string[] {
|
||||
const textWidth = rowWidth - 2 * DETAIL_MENU_TEXT_X;
|
||||
return item.kind === "disable-source" ? wrapText(font, item.label, textWidth) : [truncateText(font, item.label, textWidth)];
|
||||
}
|
||||
|
||||
function buildDetailMenu(notification: AndroidNotification, origin: SingleNotificationLayerOrigin): DetailMenuItem[] {
|
||||
return [
|
||||
{ kind: "back", label: "Back" },
|
||||
|
||||
+47
-10
@@ -239,8 +239,11 @@ export type TrayWidget = {
|
||||
openWindow: () => { window: ShellWindow; opened: boolean } | null;
|
||||
};
|
||||
|
||||
/** A new-notification popup: the shell modal, and the notification view inside it. */
|
||||
type NotificationPopup = { modal: ShellModalLayer; content: SingleNotificationLayer };
|
||||
/**
|
||||
* A new-notification popup: the shell modal, and the notification view inside
|
||||
* it (null for an app notification, which brings its own layer).
|
||||
*/
|
||||
type NotificationPopup = { modal: ShellModalLayer; content: SingleNotificationLayer | null };
|
||||
|
||||
/**
|
||||
* The app switcher's selection while it is on a status bar item rather than
|
||||
@@ -282,6 +285,18 @@ export type AssistantActivityEvent = {
|
||||
|
||||
export type FocusKind = "sidebar" | "window";
|
||||
|
||||
/**
|
||||
* A notification an app posts itself, rather than one Android posted. It
|
||||
* opens in the same modal (and waits in the same queue) as Android
|
||||
* notifications, but is not listed in the Notifications app.
|
||||
*/
|
||||
export type AppNotification = {
|
||||
/** Unique per notification; must not collide with Android notification keys. */
|
||||
key: string;
|
||||
/** The modal's content; `close` takes the modal down. */
|
||||
createLayer: (close: () => void) => Layer;
|
||||
};
|
||||
|
||||
const noopActions: LayerActions = noopLayerActions;
|
||||
|
||||
/**
|
||||
@@ -437,9 +452,12 @@ class Shell {
|
||||
private readonly trayIcons = new Map<string, { icon: GrayImage; widget?: TrayWidget }>();
|
||||
private activeVoiceLayer: VoiceInputLayer | null = null;
|
||||
private activeKeyboardLayer: KeyboardInputLayer | null = null;
|
||||
// App-posted notifications that are open or queued, by key.
|
||||
private readonly appNotifications = new Map<string, AppNotification>();
|
||||
private readonly notificationModals = new NotificationModalQueue<NotificationPopup>({
|
||||
open: (notificationKey) => this.pushNotificationModal(notificationKey),
|
||||
isAvailable: (notificationKey) => {
|
||||
if (this.appNotifications.has(notificationKey)) return true;
|
||||
const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === notificationKey);
|
||||
return !!notification && shouldShowNotificationOnGlasses(notification.packageName);
|
||||
},
|
||||
@@ -866,25 +884,44 @@ class Shell {
|
||||
*/
|
||||
notificationRemoved(notificationKey: string): void {
|
||||
const popup = this.notificationModals.withdrawn(notificationKey);
|
||||
if (popup && !popup.content.outlivesNotification()) {
|
||||
this.closeNotificationModal(popup);
|
||||
if (popup && !popup.content?.outlivesNotification()) {
|
||||
this.closeNotificationModal(popup, notificationKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show an app's own notification the way new Android notifications are
|
||||
* shown: in the notification modal, waking the screen for it if it was off.
|
||||
*/
|
||||
postAppNotification(notification: AppNotification): void {
|
||||
this.appNotifications.set(notification.key, notification);
|
||||
const wokeScreen = this.screenOn ? false : this.wake("sidebar");
|
||||
this.openNotificationModal(notification.key, wokeScreen);
|
||||
}
|
||||
|
||||
private pushNotificationModal(notificationKey: string): NotificationPopup {
|
||||
const content = new SingleNotificationLayer(notificationKey, {
|
||||
origin: "new-notification-modal",
|
||||
onClose: () => this.closeNotificationModal(popup),
|
||||
});
|
||||
const appNotification = this.appNotifications.get(notificationKey);
|
||||
const close = () => this.closeNotificationModal(popup, notificationKey);
|
||||
const content = appNotification
|
||||
? null
|
||||
: new SingleNotificationLayer(notificationKey, { origin: "new-notification-modal", onClose: close });
|
||||
const popup: NotificationPopup = {
|
||||
content,
|
||||
modal: new ShellModalLayer(content, this.config.actions, () => this.notificationModals.removed(popup, this.screenOn)),
|
||||
modal: new ShellModalLayer(
|
||||
appNotification ? appNotification.createLayer(close) : content!,
|
||||
this.config.actions,
|
||||
() => {
|
||||
this.appNotifications.delete(notificationKey);
|
||||
this.notificationModals.removed(popup, this.screenOn);
|
||||
},
|
||||
),
|
||||
};
|
||||
this.stack.push(popup.modal);
|
||||
return popup;
|
||||
}
|
||||
|
||||
private closeNotificationModal(popup: NotificationPopup): void {
|
||||
private closeNotificationModal(popup: NotificationPopup, notificationKey: string): void {
|
||||
this.appNotifications.delete(notificationKey);
|
||||
// The queue first: it opens the next notification or puts the screen back
|
||||
// to sleep, and then ignores the removal below. remove() rather than a
|
||||
// pop, so a menu opened over the modal doesn't leave it stuck.
|
||||
|
||||
Reference in new issue
Block a user