Notifications in the bottom bar are focusable

This commit is contained in:
James Babcock committed 2026-10-03 01:15:42 -07:00
1 parent 96e155ebb0
commit 71fade31f9
9 files changed
+476 -52

No files matched your search

+23 -7
View File
@@ -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<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).
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);
}
}
+67 -16
View File
@@ -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;
+218 -9
View File
@@ -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<string, boolean>();
/** 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<string, GrayImage>();
@@ -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,