import { GrayImage } from "../../graphics/image"; import { DrawOp } from "../../graphics/display-list"; import { flattenPlanes } from "../../graphics/plane"; import { InputEvent } from "../gestures"; import { Layer, LayerActions, LayerContext, LayerStack, PaintBelow } from "../layers"; import { appViewportRect, appViewportSize, SHELL_OPAQUE_BLACK } from "./geometry"; // The modal box covers most of the min-height app viewport. Retained vector // clears optionally hide the app outside its opaque rect. Size and position are // computed per paint: the display-mode setting changes the viewport size at // runtime, and the vertical position setting moves the band. const MODAL_MARGIN = 14; const MODAL_PADDING = 4; /** Content (inner layer stack) size of the modal box. */ function modalInterior(): { width: number; height: number } { const viewport = appViewportSize("min"); return { width: viewport.width - 2 * MODAL_MARGIN - 2 * MODAL_PADDING, height: Math.min(viewport.height - 2 * MODAL_MARGIN, Math.floor(65530 / Math.ceil((viewport.width - 2 * MODAL_MARGIN) / 2))) - 2 * MODAL_PADDING, }; } /** Screen rect of the modal box, aligned to the min-height window band. */ export function modalRect(): { x: number; y: number; width: number; height: number } { const viewport = appViewportRect("min"); return { x: viewport.x + MODAL_MARGIN, y: viewport.y + MODAL_MARGIN, width: viewport.width - 2 * MODAL_MARGIN, height: Math.min(viewport.height - 2 * MODAL_MARGIN, Math.floor(65530 / Math.ceil((viewport.width - 2 * MODAL_MARGIN) / 2))), }; } /** * A shell overlay hosting an inner layer stack in a bordered box over the * app viewport (used for new-notification popups). Normally the shell's lower * layers stay visible. For a wake popup, only the box is opaque (keeping its * image within 64 KiB), and a resource-free retained list clears the outside * margins over separate app surfaces. The inner stack paints at the modal's interior * size; inner value-0 pixels read as black, not transparent. */ export class ShellModalLayer implements Layer { private readonly stack: LayerStack; constructor( baseLayer: Layer, actions: LayerActions, private readonly hooks: { /** Arrival-time screen state: mask the background only for a wake popup. */ hideBackground?: boolean; /** The modal left the shell stack, however it went. */ removed?: () => void; /** Input reached the modal (before its inner stack handles it). */ input?: (event: InputEvent) => void; } = {}, ) { this.stack = new LayerStack(baseLayer, actions, modalInterior()); } onRemoved(): void { this.hooks.removed?.(); } paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { // Wake popups omit lower chrome, but ordinary popups keep the usual overlay. if (!this.hooks.hideBackground) paintBelow(); // Leave the outer margins transparent so the // scene encoder crops to the resource-sized modal, not the whole display. // Native dim=0 still preserves a 1/0 halftone; clipped CLEAR calls below // instead produce literal black without changing shared dimming semantics. const { width, height } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height); // Track a display-mode switch while the modal is up, so the inner stack // paints at the same size the box below is drawn with. const interior = modalInterior(); this.stack.setBaseSize(interior); // Flattening bakes the inner stack's planes (glyphs included) so the blit // below transplants the finished modal content into this layer's plane. // The top plane's retained presentations (a menu's selection highlight // and scroll strip) stay unbaked: they are display lists the glasses // animate, and baking one freezes it on its first frame. Lower planes' // are baked so the top plane's raster still covers them. const planes = this.stack.paint(); const top = planes[planes.length - 1]!; const inner = flattenPlanes(planes.map((plane) => plane === top ? { ...plane, image: plane.image.withDrawsBaked(false) } : plane), interior); const rect = modalRect(); const innerX = rect.x + MODAL_PADDING, innerY = rect.y + MODAL_PADDING; image.fillRect(rect.x, rect.y, rect.width, rect.height, SHELL_OPAQUE_BLACK); image.drawRect(rect.x, rect.y, rect.width, rect.height, 110); image.bitBlt(inner, innerX, innerY, { transparentZero: true }); top.image.copyPresentationsInto(image, innerX + top.x, innerY + top.y); if (!this.hooks.hideBackground) return image; // ShellScene replays retained calls after the opaque box. Clear only the // complement of that box, keeping the inner menu's presentation order. // These are vector clips, not tiled image resources; no full-screen image // is uploaded. Clips move with the scene's stereo depth just like the box. const margins = [ { x: 0, y: 0, width, height: rect.y }, { x: 0, y: rect.y + rect.height, width, height: height - rect.y - rect.height }, { x: 0, y: rect.y, width: rect.x, height: rect.height }, { x: rect.x + rect.width, y: rect.y, width: width - rect.x - rect.width, height: rect.height }, ]; image.drawDisplayList({ resources: [], calls: margins .filter(clip => clip.width > 0 && clip.height > 0) .map(clip => ({ op: DrawOp.CLEAR, color: 0, clip })) }, 0, 0, width, height); return image; } async handleInput(event: InputEvent, _ctx: LayerContext): Promise { this.hooks.input?.(event); await this.stack.handleInput(event); } }