From 71fade31f93e316ff50b245a220c56828030f97f Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sat, 3 Oct 2026 01:15:42 -0700 Subject: [PATCH] Notifications in the bottom bar are focusable --- ...aceclawMediaNotificationListenerService.kt | 9 +- app/g2/dashboard-controller.ts | 49 +++- app/native/notification-icons.ios.ts | 7 +- app/native/notification-icons.ts | 22 +- app/ui/notifications.ts | 30 ++- app/ui/shell/chrome-layer.ts | 83 +++++-- app/ui/shell/shell.ts | 227 +++++++++++++++++- tests/app-layout.test.cjs | 67 +++++- tests/notification-filter.test.cjs | 34 ++- 9 files changed, 476 insertions(+), 52 deletions(-) diff --git a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt index 7c40de56..c0f6f229 100644 --- a/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt +++ b/App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaNotificationListenerService.kt @@ -87,8 +87,14 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() { return false } + /** + * The tray's notification icons, iconSize*iconSize gray bytes each: one + * per notification group. When keysOut is given, each icon's + * notification key (the group member it was taken from) is appended + * to it in step, so the tray can tell which notification an icon is. + */ @JvmStatic - fun getActiveNotificationIconGrays(iconSize: Int, maxIcons: Int): ByteArray { + fun getActiveNotificationIconGrays(iconSize: Int, maxIcons: Int, keysOut: MutableList?): ByteArray { val service = activeService val size = Math.max(1, Math.min(96, iconSize)) val limit = Math.max(0, maxIcons) @@ -130,6 +136,7 @@ class FaceclawMediaNotificationListenerService : NotificationListenerService() { + " drawable=" + drawable.javaClass.simpleName + " intrinsic=" + drawable.intrinsicWidth + "x" + drawable.intrinsicHeight) appendIconGrayBytes(drawable, size, out, service, emitted, statusBarNotification.packageName) + keysOut?.add(statusBarNotification.key ?: "") if (dedupeGroupKey != null) { emittedGroupKeys.add(dedupeGroupKey) } diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index b2f7ffc1..f6f33b17 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -87,6 +87,7 @@ import { type PhoneUiButton, } from "../apps/evenhub/manager"; import { openEvenHubStoreForPackage } from "../apps/evenhub"; +import { createNotificationsAppWindow, NOTIFICATIONS_SURFACE_ID, NOTIFICATIONS_WINDOW_ID } from "../apps/notifications/notifications-app"; import { isInstalledPackagePresent } from "../apps/evenhub/updates"; import { wearerVerificationOptions } from "../apps/microphones/speakers"; import { micSession } from "../apps/microphones/mic-session"; @@ -420,6 +421,8 @@ class DashboardController { // The foreground title is mirrored on both remote-control faces. this.emit(); }, + openNotificationsWindow: () => + this.openInProcessAppInBackground(NOTIFICATIONS_WINDOW_ID, NOTIFICATIONS_SURFACE_ID, createNotificationsAppWindow), onScreenStateChanged: (on) => { // Any wake of the regular UI replaces a showing Glanceboard. if (on) this.glance.dismiss(); @@ -1956,6 +1959,11 @@ class DashboardController { const foreground = shell.foregroundWindow(); const stripShown = sidebarStripVisible(shell.getFocus(), foreground?.appId); this.appendLog(`mirror tap at ${x},${y}`); + if (!shell.hasOverlay() && shell.focusNotificationAt(x, y)) { + this.appendLog("mirror tap: notification icon"); + this.requestShellRender(); + return; + } if (!shell.hasOverlay() && stripShown && isOnSwitcherEdge(x, y, foreground?.heightMode ?? "min", foreground?.appId)) { const target = shell.windowAtSidebarPoint(x, y); if (target) { @@ -2364,6 +2372,42 @@ class DashboardController { this.requestShellRender(); return; } + const app = this.createInProcessApp(windowId, surfaceId, create); + await this.configureWindowSurface(surfaceId, false, app.window.heightMode); + shell.focusWindow(windowId); + this.requestShellRender(); + this.appendLog(`launched ${windowId}`); + } + + /** + * An in-process singleton app's window, opened in the background (neither + * foregrounded nor focused) unless it is open already: for the shell to + * bring forward itself (the Notifications window under a notification + * selected in the switcher). The bridge runs calls in order, so the + * shell may foreground it at once: its surface's visibility is settled + * when the configure call is queued, ahead of any frame. + */ + private openInProcessAppInBackground( + windowId: string, + surfaceId: string, + create: (options: InProcessAppOptions) => InProcessWindow, + ): { window: InProcessWindow; opened: boolean } { + const existing = this.inProcessApps.get(windowId); + if (existing) return { window: existing, opened: false }; + const app = this.createInProcessApp(windowId, surfaceId, create); + void this.configureWindowSurface(surfaceId, this.isForegroundWindow(windowId), app.window.heightMode).catch((error) => { + this.appendLog(`${windowId} surface configure failed: ${this.formatError(error)}`); + }); + this.appendLog(`opened ${windowId} in the background`); + return { window: app, opened: true }; + } + + /** Create an in-process singleton app's window and register it with the shell. */ + private createInProcessApp( + windowId: string, + surfaceId: string, + create: (options: InProcessAppOptions) => InProcessWindow, + ): InProcessWindow { const app = create({ actions: { ...this.sharedActions, @@ -2384,10 +2428,7 @@ class DashboardController { }); this.inProcessApps.set(windowId, app); shell.registerWindow(app.window); - await this.configureWindowSurface(surfaceId, false, app.window.heightMode); - shell.focusWindow(windowId); - this.requestShellRender(); - this.appendLog(`launched ${windowId}`); + return app; } /** Get or spawn the worker host for an app. */ diff --git a/app/native/notification-icons.ios.ts b/app/native/notification-icons.ios.ts index d94b9857..95a387ed 100644 --- a/app/native/notification-icons.ios.ts +++ b/app/native/notification-icons.ios.ts @@ -36,8 +36,11 @@ function bell(): GrayImage { return image } export function readActiveNotificationIcons(maxIcons: number, _allowStale: boolean) { - const sources = new Set(readActiveNotifications(ALL_NOTIFICATIONS).map(n => n.packageName)) - return {icons: Array.from(sources).slice(0,Math.max(0,maxIcons)).map(() => bell()),stale:false} + // One icon per source, standing for its first notification. + const sources = new Map() + for (const n of readActiveNotifications(ALL_NOTIFICATIONS)) if (!sources.has(n.packageName)) sources.set(n.packageName, n.key) + const keys = Array.from(sources.values()).slice(0,Math.max(0,maxIcons)) + return {icons: keys.map(() => bell()),keys,stale:false} } export function readNotificationIconByKey(key: string, _allowStale: boolean) { return {icon: readActiveNotifications(ALL_NOTIFICATIONS).some(n => n.key === key) ? bell() : null,stale:false} diff --git a/app/native/notification-icons.ts b/app/native/notification-icons.ts index 4ea179dc..7620ab24 100644 --- a/app/native/notification-icons.ts +++ b/app/native/notification-icons.ts @@ -4,6 +4,7 @@ import { toUint8Array } from "../util/array-util"; import { rememberNotificationSources } from "./notification-sources"; declare const com: any; +declare const java: any; const ICON_SIZE = 24; /** Ask the native listener for every active source, including those below the list's display limit. */ @@ -17,6 +18,7 @@ export const ALL_NOTIFICATIONS = 0x7fffffff; const ICON_CACHE_MS = 60_000; let cachedIcons: GrayImage[] = []; +let cachedKeys: string[] = []; let cachedAtMs = 0; const keyedIconCache = new Map(); const KEYED_ICON_CACHE_MAX = 128; @@ -33,6 +35,11 @@ export type { AndroidNotification, AndroidNotificationAction } from "./notificat export type NotificationIconsResult = { icons: GrayImage[]; + /** + * The notification each icon stands for, by key, in step with icons: the + * member of its group (which gets one icon) the icon was taken from. + */ + keys: string[]; /** True when the icons came from an expired (or empty) cache under allowStale. */ stale: boolean; }; @@ -46,23 +53,25 @@ export type NotificationIconsResult = { * on what is in the tray. */ export function readActiveNotificationIcons(maxIcons: number, allowStale: boolean): NotificationIconsResult { - if (!global.isAndroid || maxIcons <= 0) return { icons: [], stale: false }; + if (!global.isAndroid || maxIcons <= 0) return { icons: [], keys: [], stale: false }; const now = Date.now(); if (cachedAtMs > 0 && now - cachedAtMs < ICON_CACHE_MS) { logCurrent("notification icons served from cache"); - return { icons: cachedIcons.map(icon => icon.clone()), stale: false }; + return { icons: cachedIcons.map(icon => icon.clone()), keys: cachedKeys.slice(), stale: false }; } if (allowStale) { logCurrent("notification icons served stale"); - return { icons: cachedIcons.map(icon => icon.clone()), stale: true }; + return { icons: cachedIcons.map(icon => icon.clone()), keys: cachedKeys.slice(), stale: true }; } + const keyList = new java.util.ArrayList(); const bytes = spanCurrent("fetch-notification-icons", () => toUint8Array( com.faceclaw.app.FaceclawMediaNotificationListenerService.getActiveNotificationIconGrays( ICON_SIZE, maxIcons, + keyList, ), ), ); @@ -74,10 +83,15 @@ export function readActiveNotificationIcons(maxIcons: number, allowStale: boolea icon.pixels.set(bytes.subarray(index * iconByteLength, (index + 1) * iconByteLength)); icons.push(icon); } + const keys: string[] = []; + for (let index = 0; index < iconCount; index++) { + keys.push(index < keyList.size() ? String(keyList.get(index)) : ""); + } cachedIcons = icons; + cachedKeys = keys; cachedAtMs = now; - return { icons: icons.map(icon => icon.clone()), stale: false }; + return { icons: icons.map(icon => icon.clone()), keys: keys.slice(), stale: false }; } export type NotificationIconResult = { diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index 57766945..37bae622 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -71,12 +71,18 @@ type DetailMenuItem = type ConfirmationItem = { label: string; run: (ctx: LayerContext) => void }; -export type SingleNotificationLayerOrigin = "notifications-list" | "new-notification-modal"; +export type SingleNotificationLayerOrigin = "notifications-list" | "new-notification-modal" | "notification-tray"; type SingleNotificationLayerOptions = { origin: SingleNotificationLayerOrigin; - /** Close hook for the modal origin (the layer is the modal stack's base, so pop() cannot close it). */ - closeModal?: (ctx: LayerContext) => void; + /** + * Close hook for hosts that take the layer down themselves instead of + * popping it: the modal (the layer is the modal stack's base, so pop() + * cannot close it), and the app switcher's tray selection, which keeps + * the layer up until the selection moves on. `gone` says the notification + * itself went away (dismissed, or removed by one of its actions). + */ + onClose?: (ctx: LayerContext, gone: boolean) => void; }; /** @@ -199,6 +205,9 @@ export class SingleNotificationLayer implements Layer { }, }); private confirmation: { source: AndroidNotification; menu: Menu } | 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). + private lastImage: GrayImage | null = null; constructor( private readonly notificationKey: string, @@ -219,6 +228,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.lastImage = image; return image; } @@ -310,17 +320,23 @@ export class SingleNotificationLayer implements Layer { } /** Leave the detail view, whatever hosts it. */ - private close(ctx: LayerContext): void { - if (this.options.origin === "new-notification-modal") { - this.options.closeModal?.(ctx); + private close(ctx: LayerContext, gone = false): void { + if (this.options.onClose) { + this.options.onClose(ctx, gone); } else { ctx.stack.pop(); } } private closeUnavailableNotification(ctx: LayerContext, paintBelow?: PaintBelow): GrayImage { - this.close(ctx); + this.close(ctx, true); const { width, height } = ctx.stack.getBaseSize(); + // The tray replaces this layer only after the paint (swapping layers + // mid-paint would render re-entrantly); meanwhile the screen keeps what + // it showed rather than flashing the list beneath. + if (this.options.origin === "notification-tray" && this.lastImage) { + return this.lastImage.clone(); + } return paintBelow ? paintBelow() : new GrayImage(width, height, 0); } } diff --git a/app/ui/shell/chrome-layer.ts b/app/ui/shell/chrome-layer.ts index 4a4b1155..8e7efbcb 100644 --- a/app/ui/shell/chrome-layer.ts +++ b/app/ui/shell/chrome-layer.ts @@ -293,6 +293,11 @@ function statusBarPlacement(state: ShellChromeState): StatusBarPlacement | null } const NOTIFICATION_ICON_SIZE = 24; +const NOTIFICATION_ICON_STEP = NOTIFICATION_ICON_SIZE + 4; +// The box round a selected notification icon: clear of it by 2px either +// side, and by 1px above and below, all a top bar leaves above its divider. +const NOTIFICATION_BOX_MARGIN_X = 2; +const NOTIFICATION_BOX_MARGIN_Y = 1; const BORDER_VALUE = 40; export type ShellChromeWindow = { @@ -305,7 +310,13 @@ export type ShellChromeWindow = { export type ShellChromeState = { windows: ShellChromeWindow[]; + /** The selected window's index, or -1 while the selection is on a notification. */ selectedIndex: number; + /** + * The notification (by key) whose icon the switcher's selection is on + * instead of a window, if any (see Shell.moveSelection). + */ + selectedNotificationKey?: string | null; focus: "sidebar" | "window"; /** Height mode of the foreground window; decides where its top bar sits. */ foregroundHeightMode: WindowHeightMode; @@ -408,12 +419,16 @@ export class ShellChromeLayer implements Layer { // Whether the last paint's selection tab carries the window frame's left // side down, so the frame's bottom-left corner is square. private frameCornerSquare = false; + // The notification icons the last paint drew, left to right: what the + // switcher's selection can move onto, and mirror touches hit. + private notificationEntries: NotificationEntry[] = []; constructor(private readonly getState: () => ShellChromeState) {} paint(): GrayImage { const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); const state = this.getState(); + this.notificationEntries = []; const statusInRow = statusInSwitcherRow(state.foregroundAppId); const popup = switcherPosition() === "popup"; // Full-panel mode: the strip is an overlay, present only while the user @@ -434,6 +449,7 @@ export class ShellChromeLayer implements Layer { paintParts(): Plane[] { const state = this.getState(), parts: Plane[] = []; + this.notificationEntries = []; const statusInRow = statusInSwitcherRow(state.foregroundAppId); const canvas = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0); const span = topBarSpan(state); @@ -465,9 +481,10 @@ export class ShellChromeLayer implements Layer { if (statusLeft !== null) { // The status bar keeps the top bar's surface and its depth, -2, over // the row's right end below its separator line, so the clock ticking - // doesn't resend the window icons. - const right = strip.x + strip.width; - parts.push({ ...shellCrop(canvas, statusLeft, strip.y + 1, right - statusLeft, strip.height - 1, 2), depth: -2 }); + // doesn't resend the window icons. It reaches into the gap before it + // by a selection box's margin, for one round its first icon. + const right = strip.x + strip.width, left = statusLeft - NOTIFICATION_BOX_MARGIN_X; + parts.push({ ...shellCrop(canvas, left, strip.y + 1, right - left, strip.height - 1, 2), depth: -2 }); } } drawAmbientCards(canvas, span.right, parts); @@ -526,6 +543,17 @@ export class ShellChromeLayer implements Layer { return null; } + /** Keys of the notifications whose icons the last paint drew, left to right. */ + notificationKeys(): string[] { + return this.notificationEntries.map((entry) => entry.key); + } + + /** Which notification's icon is under (x, y) on screen, if any (mirror touches). */ + notificationKeyAt(x: number, y: number): string | null { + const entry = this.notificationEntries.find((e) => x >= e.x && x < e.x + e.width && y >= e.y && y < e.y + e.height); + return entry?.key ?? null; + } + /** * Draw the switcher strip or row. Given `statusImage`, a bottom row shares * its right end with the status bar, drawn there (into that image, which @@ -550,7 +578,10 @@ export class ShellChromeLayer implements Layer { const windowsRight = statusLeft === null ? strip.x + strip.width : statusLeft - ROW_STATUS_GAP; this.rowWindowsRight = windowsRight; const visibleCount = switcherLayout(count, state.foregroundHeightMode, state.foregroundAppId, windowsRight).visibleCount; - this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, state.selectedIndex, visibleCount, count); + // With the selection out on a notification, the windows stay scrolled as they were. + this.scrollRow = state.selectedIndex >= 0 + ? scrollToKeepSelectionVisible(this.scrollRow, state.selectedIndex, visibleCount, count) + : Math.min(this.scrollRow, Math.max(0, count - visibleCount)); const layout = switcherLayout(count, state.foregroundHeightMode, state.foregroundAppId, windowsRight, this.scrollRow); const { iconSize, position } = layout; const lastVisible = Math.min(count, this.scrollRow + layout.visibleCount); @@ -642,19 +673,16 @@ export class ShellChromeLayer implements Layer { // Right-aligned against the widgets, with the top bar's spacing. const slots = Math.min(windowCount, ROW_MIN_WINDOW_SLOTS); const windowsFloor = row.x + 2 * LIST_MARGIN + Math.max(0, slots * ROW_STEP - ICON_SPACING) + ROW_STATUS_GAP; - const iconsRight = trayLeft - 8, step = NOTIFICATION_ICON_SIZE + 4; + const iconsRight = trayLeft - 8, step = NOTIFICATION_ICON_STEP; const maxIcons = Math.max(0, ((iconsRight - windowsFloor + 4) / step) | 0); if (maxIcons === 0) return trayLeft; - const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData()); + const { icons, keys, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData()); if (stale) { noteStaleDataUsed(); } if (!icons.length) return trayLeft; const iconsLeft = iconsRight - icons.length * step + 4; - const iconY = top + (((TOP_BAR_HEIGHT - NOTIFICATION_ICON_SIZE) / 2) | 0); - for (let index = 0; index < icons.length; index++) { - image.drawImage(icons[index]!, iconsLeft + index * step, iconY); - } + this.drawNotificationIcons(image, state, icons, keys, iconsLeft, top + (((TOP_BAR_HEIGHT - NOTIFICATION_ICON_SIZE) / 2) | 0)); return iconsLeft; } @@ -732,16 +760,36 @@ export class ShellChromeLayer implements Layer { const trayLeft = drawTrayIcons(image, state.trayIcons, batteryLeft, barTop); const iconsX = clockX + font.measureText(clock) + 16; - const maxIcons = Math.max(0, ((trayLeft - 8 - iconsX) / (NOTIFICATION_ICON_SIZE + 4)) | 0); + const maxIcons = Math.max(0, ((trayLeft - 8 - iconsX) / NOTIFICATION_ICON_STEP) | 0); if (maxIcons > 0) { - const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData()); + const { icons, keys, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData()); if (stale) { noteStaleDataUsed(); } - const iconY = barTop + (((TOP_BAR_HEIGHT - NOTIFICATION_ICON_SIZE) / 2) | 0); - for (let index = 0; index < icons.length; index++) { - image.drawImage(icons[index]!, iconsX + index * (NOTIFICATION_ICON_SIZE + 4), iconY); - } + this.drawNotificationIcons(image, state, icons, keys, iconsX, barTop + (((TOP_BAR_HEIGHT - NOTIFICATION_ICON_SIZE) / 2) | 0)); + } + } + + /** + * Draw the notification icons left to right from `left`, noting each as a + * notification entry. The one the switcher's selection is on + * (state.selectedNotificationKey) sits in a rounded box like a sidebar + * overflow icon's: filled, with the icon inverted, while the switcher has + * focus, and outlined while its detail view in the foreground window has. + */ + private drawNotificationIcons(image: GrayImage, state: ShellChromeState, icons: GrayImage[], keys: string[], left: number, iconY: number): void { + for (let index = 0; index < icons.length; index++) { + const x = left + index * NOTIFICATION_ICON_STEP; + const key = keys[index]; + const box = { + x: x - NOTIFICATION_BOX_MARGIN_X, y: iconY - NOTIFICATION_BOX_MARGIN_Y, + width: NOTIFICATION_ICON_SIZE + 2 * NOTIFICATION_BOX_MARGIN_X, height: NOTIFICATION_ICON_SIZE + 2 * NOTIFICATION_BOX_MARGIN_Y, + }; + if (key) this.notificationEntries.push({ key, ...box }); + const selected = !!key && key === state.selectedNotificationKey; + const focused = selected && state.focus === "sidebar"; + if (selected) drawSelectionBox(image, box.x, box.y, box.width, box.height, focused); + image.drawImage(focused ? invertedIcon(icons[index]!) : icons[index]!, x, iconY); } } @@ -978,6 +1026,9 @@ function attentionDot(value: number): GrayImage { type Rect = { x: number; y: number; width: number; height: number }; +/** A notification icon as drawn: its notification's key, and its selection box. */ +type NotificationEntry = Rect & { key: string }; + /** Corner radius of the foreground window's frame. */ const FRAME_RADIUS = 8; diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index b296758a..87a6c72a 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -51,6 +51,7 @@ import { onAmbientCardsChanged } from "./ambient-cards"; import { ShellChromeLayer, sidebarContentSpan, type ShellChromeState, type ShellChromeWindow } from "./chrome-layer"; import { ShellModalLayer } from "./modal-layer"; import { ToolDebugMenuLayer } from "./tool-debug-layer"; +import type { InProcessWindow } from "./in-process-window"; import { BrightnessPickerLayer } from "./brightness-picker-layer"; import { toolRegistry } from "../../assistant/tool-registry"; import { @@ -197,6 +198,35 @@ export type ShellConfig = { onScreenStateChanged: (on: boolean) => void; /** Window registered/removed or foreground changed (persists the open-app list). */ onWindowsChanged?: () => void; + /** + * The Notifications app's window, opened in the background (neither + * foregrounded nor focused) unless it is open already, for the switcher + * to show a notification's detail view in; `opened` says this call opened + * it. Hosts that leave it out offer no notifications to select. + */ + openNotificationsWindow?: () => { window: InProcessWindow; opened: boolean } | null; +}; + +/** + * The app switcher's selection while it is on a notification's icon rather + * than a window's (bottom switcher only; see Shell.moveSelection). The + * Notifications app's window is the foreground window meanwhile, with the + * notification's detail view on top of its stack. + */ +type NotificationSelection = { + key: string; + /** Where the notification's icon sat among them, for once it has gone. */ + index: number; + host: InProcessWindow; + layer: SingleNotificationLayer; + /** + * The Notifications window was opened for the selection: it has no + * switcher icon of its own, and closes once the selection moves off the + * notifications. + */ + transient: boolean; + /** The window selected before, to go back to when the screen sleeps. */ + returnWindowId: string | null; }; /** Which surfaces need re-rendering after an input event. */ @@ -362,6 +392,8 @@ class Shell { headset: null, headsetCharging: null, ring: null, ringCharging: null, watch: null, watchCharging: null, }; private attention = new Map(); + /** Set while the switcher's selection is on a notification (see NotificationSelection). */ + private selectedNotification: NotificationSelection | null = null; // App-provided top-bar tray icons, keyed by owner id; drawn between the // notification icons and the battery indicators. private readonly trayIcons = new Map(); @@ -481,6 +513,9 @@ class Shell { removeWindow(windowId: string): void { const index = this.windows.findIndex((w) => w.windowId === windowId); if (index < 0) return; + // Closing the Notifications window under a selected notification (from + // the system menu) takes the selection with it. + if (this.selectedNotification?.host.window.windowId === windowId) this.selectedNotification = null; const wasSelected = index === this.selectedIndex; this.windows.splice(index, 1); this.attention.delete(windowId); @@ -590,8 +625,29 @@ class Shell { /** The window whose sidebar icon is at screen (x, y), for mirror touches. */ windowAtSidebarPoint(x: number, y: number): ShellWindow | null { - const index = this.chrome.windowIndexAt(x, y, this.windows.length); - return index === null ? null : this.windows[index] ?? null; + const windows = this.switcherWindows(); + const index = this.chrome.windowIndexAt(x, y, windows.length); + return index === null ? null : windows[index] ?? null; + } + + /** + * A touch from the phone's mirror on a selectable notification's icon: + * select it and go into its detail view, as a touch on a window's icon + * focuses that window. False when there is no such icon at (x, y). + */ + focusNotificationAt(x: number, y: number): boolean { + const keys = this.selectableNotificationKeys(); + const key = this.chrome.notificationKeyAt(x, y); + if (!key || !keys.includes(key)) return false; + this.selectNotification(key, keys.indexOf(key)); + const host = this.selectedNotification?.key === key ? this.selectedNotification.host.window : null; + if (!host) return false; + const alreadyFocused = this.isFocusTarget(host); + this.focus = "window"; + if (!alreadyFocused) host.onFocus?.(this.lastInput); + host.requestRender(); + this.syncInputFocus(); + return true; } /** Whether input focus currently targets this window (regardless of screen state). */ @@ -650,9 +706,13 @@ class Shell { return true; } - /** Turn the screen off, closing any shell overlays. Sidebar selection is kept. */ + /** + * Turn the screen off, closing any shell overlays. Sidebar selection is + * kept, except that one on a notification goes back to its window. + */ sleep(): void { if (!this.screenOn) return; + this.returnFromNotification(); this.cancelEscapeMenuTimer(); this.screenOn = false; this.stack.clearToBase(); @@ -670,6 +730,9 @@ class Shell { const target = this.windows[index]; const alreadyFocused = this.isFocusTarget(target); this.setSelectedIndex(index); + // Asked for by id, the Notifications window stays, whether or not it + // was opened for a selected notification. + this.endNotificationSelection(target === this.selectedNotification?.host.window); this.focus = "window"; if (!alreadyFocused) target?.onFocus?.(this.lastInput); this.syncInputFocus(); @@ -708,7 +771,7 @@ class Shell { const modal: ShellModalLayer = new ShellModalLayer( new SingleNotificationLayer(notificationKey, { origin: "new-notification-modal", - closeModal: () => this.closeNotificationModal(modal, wokeScreen), + onClose: () => this.closeNotificationModal(modal, wokeScreen), }), this.config.actions, ); @@ -1041,10 +1104,155 @@ class Shell { } } + /** + * Step the switcher's selection through the windows and then, with a + * bottom switcher, on through the notifications whose icons are showing, + * wrapping round at either end. + */ private moveSelection(delta: number): void { - if (!this.windows.length) return; - const count = this.windows.length; - this.setSelectedIndex((this.selectedIndex + delta + count) % count); + const windows = this.switcherWindows(); + const keys = this.selectableNotificationKeys(); + const count = windows.length + keys.length; + if (!count) return; + let current = this.selectedIndex; + const selected = this.selectedNotification; + if (selected) { + // Once its notification has gone, the selection stands between the + // neighbours it left. + const at = keys.indexOf(selected.key); + current = windows.length + (at >= 0 ? at : Math.min(selected.index, keys.length) - (delta > 0 ? 1 : 0)); + } + const next = (((current + delta) % count) + count) % count; + if (next < windows.length) { + this.selectSwitcherWindow(windows[next]!); + } else { + this.selectNotification(keys[next - windows.length]!, next - windows.length); + } + } + + /** + * Windows with an icon in the switcher: all of them, except a + * Notifications window opened just to show a selected notification. + */ + private switcherWindows(): ShellWindow[] { + const hidden = this.selectedNotification?.transient ? this.selectedNotification.host.window : null; + return hidden ? this.windows.filter((window) => window !== hidden) : this.windows; + } + + /** + * Keys of the notifications the switcher's selection can move onto, left + * to right: those whose icons are showing, with a bottom switcher (the + * selection reaches them past the last window). + */ + private selectableNotificationKeys(): string[] { + return this.config.openNotificationsWindow && switcherPosition() === "bottom" ? this.chrome.notificationKeys() : []; + } + + /** Move the switcher's selection onto a window, off a notification if it was on one. */ + private selectSwitcherWindow(window: ShellWindow): void { + // Foreground first, so a Notifications window closing with the + // notification selection can't hand the foreground elsewhere first. + this.setSelectedIndex(this.windows.indexOf(window)); + this.endNotificationSelection(window === this.selectedNotification?.host.window); + } + + /** + * Move the switcher's selection onto a notification: the Notifications + * window (opened in the background first, if need be) comes to the + * foreground showing its detail view. + */ + private selectNotification(key: string, index: number): void { + const selected = this.selectedNotification; + if (selected) { + selected.index = index; + if (selected.key === key) return; + selected.host.stack.popThrough(selected.layer); + selected.key = key; + selected.layer = this.notificationDetailLayer(key); + selected.host.stack.push(selected.layer); + selected.host.requestRender(); + return; + } + const returnWindowId = this.foregroundWindow()?.windowId ?? null; + const opened = this.config.openNotificationsWindow?.(); + const hostIndex = opened ? this.windows.indexOf(opened.window.window) : -1; + if (!opened || hostIndex < 0) return; + const host = opened.window; + const layer = this.notificationDetailLayer(key); + host.stack.push(layer); + this.selectedNotification = { key, index, host, layer, transient: opened.opened, returnWindowId }; + if (hostIndex === this.selectedIndex) { + host.requestRender(); + } else { + this.setSelectedIndex(hostIndex); + } + } + + /** + * Take the switcher's selection off its notification: the Notifications + * window drops the detail view (and anything opened over it), and closes + * if it was opened for the selection, unless `keepWindow` (it is the + * window being selected instead). + */ + private endNotificationSelection(keepWindow = false): void { + const selected = this.selectedNotification; + if (!selected) return; + this.selectedNotification = null; + selected.host.stack.popThrough(selected.layer); + if (selected.transient && !keepWindow) { + this.closeWindow(selected.host.window.windowId); + return; + } + selected.host.requestRender(); + // A window opened for the selection is now one with an icon of its own. + if (selected.transient) this.config.onWindowsChanged?.(); + } + + /** End a notification selection by going back to the window selected before it. */ + private returnFromNotification(): void { + const selected = this.selectedNotification; + if (!selected) return; + const back = this.windows.find((window) => window.windowId === selected.returnWindowId); + if (back) this.setSelectedIndex(this.windows.indexOf(back)); + this.endNotificationSelection(back === selected.host.window); + } + + private notificationDetailLayer(key: string): SingleNotificationLayer { + const layer: SingleNotificationLayer = new SingleNotificationLayer(key, { + origin: "notification-tray", + onClose: (_ctx, gone) => this.notificationDetailClosed(layer, gone), + }); + return layer; + } + + /** + * A selected notification's detail view asked to close. Back (or a + * double-tap) hands focus back to the switcher, its selection still on + * the notification. Once the notification is gone (dismissed, or removed + * by an action), the selection also moves on to the one that took its + * place, or back to the windows when there are none left; that waits for + * the current paint to finish, since a vanished notification closes its + * view from inside its paint. + */ + private notificationDetailClosed(layer: SingleNotificationLayer, gone: boolean): void { + if (!gone) { + if (this.selectedNotification?.layer === layer) this.yieldFocusToSidebar(); + return; + } + setTimeout(() => { + const selected = this.selectedNotification; + if (selected?.layer !== layer) return; + this.focus = "sidebar"; + const keys = this.selectableNotificationKeys().filter((key) => key !== selected.key); + const windows = this.switcherWindows(); + if (keys.length) { + const index = Math.min(selected.index, keys.length - 1); + this.selectNotification(keys[index]!, index); + } else if (windows.length) { + this.selectSwitcherWindow(windows[windows.length - 1]!); + } + this.config.requestShellRender(); + }, 0); } /** Change selection; the selected window is the foreground window. */ @@ -1573,13 +1781,14 @@ class Shell { private chromeState(): ShellChromeState { return { - windows: this.windows.map((window) => ({ + windows: this.switcherWindows().map((window) => ({ windowId: window.windowId, title: window.title, attention: Boolean(this.attention.get(window.windowId)), drawIcon: window.drawIcon, })), - selectedIndex: this.selectedIndex, + selectedIndex: this.selectedNotification ? -1 : this.selectedIndex, + selectedNotificationKey: this.selectedNotification?.key ?? null, focus: this.focus, foregroundHeightMode: this.foregroundWindow()?.heightMode ?? "min", foregroundAppId: this.foregroundWindow()?.appId, diff --git a/tests/app-layout.test.cjs b/tests/app-layout.test.cjs index 64ed7369..cc0cb72a 100644 --- a/tests/app-layout.test.cjs +++ b/tests/app-layout.test.cjs @@ -206,7 +206,8 @@ test('a bottom app switcher centres 576-wide windows and hangs one tall tab unde // The real chrome layer over the given geometry, with platform pieces stubbed: // a 4px-per-character font that paints each character as a 2x6 block, up to -// `notifications` 24px notification icons, and a phone battery at 80%. +// `notifications` 24px notification icons (keyed n0, n1, ...), and a phone +// battery at 80%. const graphics = require('../.test-build/app/graphics/image.js'); const shellScene = require('../.test-build/app/graphics/shell-scene.js'); const displayList = require('../.test-build/app/graphics/display-list.js'); @@ -224,7 +225,9 @@ function chromeLayer(g, { notifications = 0, phoneBattery = null, windows = 1, t './ambient-cards': { activeAmbientCards: () => [] }, '../../native/notification-icons': { readActiveNotificationIcons: (max) => { requested.push(max); - return { icons: Array.from({ length: Math.min(max, notifications) }, () => new graphics.GrayImage(24, 24, 200)), stale: false }; + const count = Math.min(max, notifications); + return { icons: Array.from({ length: count }, () => new graphics.GrayImage(24, 24, 200)), + keys: Array.from({ length: count }, (_, index) => `n${index}`), stale: false }; } }, '../../native/phone-battery': { readPhoneBatteryState: () => ({ battery: phoneBattery, charging: false }) }, '../../util/render-freshness': { renderPassAllowsStaleData: () => false }, @@ -366,9 +369,10 @@ test('the switcher row lays out windows, free space, notifications, widgets, bat // Right to left from the row's end (x=608), centred under its separator // line (y=366): "Thu Oct 2 9:41" (56px) flush with it; the battery block (label // 20 + gap 5 + gauge 21) 16px before it; the 30px widget 10px before that; - // then the two notification icons, 8px clear of the widget. + // then the two notification icons, 8px clear of the widget. The surface + // starts 2px before them, room for a selection box round the first. const clockX = 608 - 56, batteryLeft = clockX - 16 - 46, trayLeft = batteryLeft - 40, iconsLeft = trayLeft - 8 - 56 + 4; - assert.deepEqual([status.x, status.y, status.image.width, status.image.height], [iconsLeft, 367, 608 - iconsLeft, 35]); + assert.deepEqual([status.x, status.y, status.image.width, status.image.height], [iconsLeft - 2, 367, 608 - iconsLeft + 2, 35]); const at = (p, x, y) => p.image.withDrawsBaked().pixels[(y - p.y) * p.image.width + x - p.x]; // The clock's text runs 8 rows down the top bar's layout, which sits 5 rows into the row. assert.deepEqual([at(status, clockX, 366 + 5 + 8 + 2), at(status, trayLeft, 366 + 5 + 4), at(status, iconsLeft, 366 + 5 + 2)], [210, 180, 200]); @@ -398,7 +402,7 @@ test('many windows scroll in what the notification icons leave them', () => { const max = Math.floor((trayLeft - 8 - floor + 4) / 28); assert.equal(requested.at(-1), max); const iconsLeft = trayLeft - 8 - max * 28 + 4; - assert.equal(status.x, iconsLeft); + assert.equal(status.x, iconsLeft - 2); // Three windows fit before the status bar's gap; the rest scroll, with the // chevron at the end of the windows rather than of the row. assert.equal(chrome.windowIndexAt(32 + 10 + 2 * 40 + 4, 380, 12), 2); @@ -489,6 +493,59 @@ test('a popup switcher\'s status bar can sit at the bottom, show only in the swi assert.deepEqual([bar.y, at(bar, 300, 123)], [96, 40]); }); +test('a notification the switcher selects sits in a box, filled with its icon inverted while the switcher has focus', () => { + const g = geometry(); + g.settings.appSwitcherPositionSetting.value = 'bottom'; + g.settings.statusBarPositionSetting.value = 'bottom'; + const { chrome, state } = chromeLayer(g, { notifications: 3, windows: 2 }); + Object.assign(state, { selectedIndex: -1, selectedNotificationKey: 'n1', focus: 'sidebar' }); + let [row, status] = chrome.paintParts(); + assert.deepEqual(Array.from(chrome.notificationKeys()), ['n0', 'n1', 'n2']); + // Clock only: the icons end 8px before its 8px lead-in, 28px apart, each + // 7 rows into the row (the top bar's layout, 5 rows down). + const iconsLeft = 608 - 56 - 8 - 8 - 3 * 28 + 4, iconY = 366 + 7, n1 = iconsLeft + 28; + const at = (p, x, y) => p.image.withDrawsBaked().pixels[(y - p.y) * p.image.width + x - p.x]; + // The box: 2px either side of the icon, 1px above and below, white; the + // icon inside it inverted; its neighbours as ever. + assert.deepEqual([at(status, n1 - 2, iconY + 12), at(status, n1 + 25, iconY + 12), at(status, n1 + 12, iconY - 1)], [255, 255, 255]); + assert.equal(at(status, n1 + 12, iconY - 2), 1); + assert.deepEqual([at(status, n1 + 5, iconY + 5), at(status, iconsLeft + 5, iconY + 5)], [55, 200]); + // No window is selected: the separator runs unbroken over the windows. + assert.equal(at(row, 50, 366), 40); + // Mirror touches find the icons by their boxes, which tile the icons' run. + assert.deepEqual([chrome.notificationKeyAt(n1 + 5, 380), chrome.notificationKeyAt(n1 - 3, 380), + chrome.notificationKeyAt(iconsLeft - 3, 380), chrome.notificationKeyAt(n1 + 5, 400)], ['n1', 'n0', null, null]); + // With its detail view focused instead, the box is an outline round the plain icon. + state.focus = 'window'; + [, status] = chrome.paintParts(); + assert.deepEqual([at(status, n1 - 2, iconY + 12), at(status, n1 - 1, iconY + 12), at(status, n1 + 5, iconY + 5)], [150, 1, 200]); + // The first icon's box fits the status bar's surface. + state.selectedNotificationKey = 'n0'; + [, status] = chrome.paintParts(); + assert.equal(at(status, iconsLeft - 2, iconY + 12), 150); + // In the top bar (the status bar's default place) likewise; barTop=78. + g.settings.statusBarPositionSetting.value = 'top'; + const [bar] = chrome.paintParts(); + const barIconsLeft = 32 + 10 + 56 + 16; + assert.deepEqual(Array.from(chrome.notificationKeys()), ['n0', 'n1', 'n2']); + assert.deepEqual([at(bar, barIconsLeft - 2, 78 + 14), at(bar, barIconsLeft + 5, 78 + 7)], [150, 200]); + assert.equal(chrome.notificationKeyAt(barIconsLeft + 30, 78 + 10), 'n1'); +}); + +test('with the selection out on a notification, the windows stay scrolled as they were', () => { + const g = geometry(); + g.settings.appSwitcherPositionSetting.value = 'bottom'; + const { chrome, state } = chromeLayer(g, { notifications: 2, windows: 20 }); + state.selectedIndex = 19; + chrome.paintParts(); + const last = 32 + 10 + 2 * 40 + 4; + const before = chrome.windowIndexAt(last, 380, 20); + assert.ok(before > 2); + Object.assign(state, { selectedIndex: -1, selectedNotificationKey: 'n0' }); + chrome.paintParts(); + assert.equal(chrome.windowIndexAt(last, 380, 20), before); +}); + // Exercise the real worker message handler with platform work stubbed out. function resizeHandler(file, globals) { const source = ts.createSourceFile(file, read(file), ts.ScriptTarget.Latest, true); diff --git a/tests/notification-filter.test.cjs b/tests/notification-filter.test.cjs index ea1be2bd..4fafbe1b 100644 --- a/tests/notification-filter.test.cjs +++ b/tests/notification-filter.test.cjs @@ -98,14 +98,40 @@ function ui(fontSize = 12) { const { SingleNotificationLayer } = load('app/ui/notifications.ts', requireModule); const { NotificationFilterLayer } = load('app/ui/notification-filter.ts', requireModule); const ctx = { stack: { getBaseSize: () => ({ width: 540, height: 224 }), isFocused: () => true, pop: () => closes++ } }; - const popup = new SingleNotificationLayer('key', { origin: 'new-notification-modal', closeModal: () => closes++ }); + const popup = new SingleNotificationLayer('key', { origin: 'new-notification-modal', onClose: () => closes++ }); + const trayCloses = []; + const tray = new SingleNotificationLayer('key', { origin: 'notification-tray', onClose: (_ctx, gone) => trayCloses.push(gone) }); const filter = new NotificationFilterLayer(); - const paint = (layer) => layer.paint(ctx, () => new RecordingImage(540, 224)); + let paintedBelow = 0; + const paint = (layer) => layer.paint(ctx, () => { paintedBelow++; return new RecordingImage(540, 224); }); const input = (layer, type) => layer.handleInput({ type }, ctx); - return { prefs, popup, filter, paint, input, active: (value) => { active = value; }, - dismissals: () => dismissals, closes: () => closes }; + return { prefs, popup, tray, filter, paint, input, active: (value) => { active = value; }, + dismissals: () => dismissals, closes: () => closes, trayCloses, paintedBelow: () => paintedBelow }; } +test('a notification the switcher selected reports back and dismissal to it, and repeats its last frame once gone', async () => { + const app = ui(); + const labels = (image) => image.texts.map(({ text }) => text); + assert.ok(labels(app.paint(app.tray)).includes('Back')); + assert.ok(!labels(app.paint(app.tray)).some((text) => text.includes("Don't show"))); + await app.input(app.tray, 'double-click'); + await app.input(app.tray, 'click'); + assert.deepEqual(app.trayCloses, [false, false]); + await app.input(app.tray, 'scroll-down'); + const last = app.paint(app.tray); + await app.input(app.tray, 'click'); + assert.equal(app.dismissals(), 1); + assert.deepEqual(app.trayCloses, [false, false, true]); + // The switcher swaps in a neighbour after the paint; until then the paint + // holds what was showing rather than the list below. + const again = app.paint(app.tray); + assert.deepEqual(app.trayCloses, [false, false, true, true]); + assert.equal(app.paintedBelow(), 0); + // (Compared unbaked: the menu highlight's draws animate with the clock.) + assert.deepEqual(Buffer.from(again.pixels), Buffer.from(last.pixels)); + assert.deepEqual(again.drawList, last.drawList); +}); + test('popup dismisses before confirmation; confirming disables the source even after the notification disappears', async () => { const app = ui(); await app.input(app.popup, 'scroll-down');