diff --git a/app/apps/ai-chat/ai-chat-app.ts b/app/apps/ai-chat/ai-chat-app.ts index 2c897fd6..95e9ac02 100644 --- a/app/apps/ai-chat/ai-chat-app.ts +++ b/app/apps/ai-chat/ai-chat-app.ts @@ -13,6 +13,10 @@ import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } import { shell } from "../../ui/shell/shell"; import { VoiceDraft } from "./voice-draft"; +/** The transcript's scrollbar, hugging the window's right padding, and the gap the text keeps from it. */ +const SCROLLBAR_WIDTH = 2; +const SCROLLBAR_GAP = 11; + class AiChatLayer implements Layer { readonly acceptsDirectional = true; private firstLine: number | null = null; // null follows the streaming tail @@ -38,24 +42,30 @@ class AiChatLayer implements Layer { } paint(ctx: LayerContext): GrayImage { const size = ctx.stack.getBaseSize(); + const { padding } = size; const image = new GrayImage(size.width, size.height); const font = getDefaultSmallFont(); const step = font.lineHeight + 3; - const left = 12; - const width = size.width - 28; + // The title, and the footer under the transcript, stay put inside the + // padding suggested for the window's edges; the transcript scrolls + // between them. Text runs from the left padding to the gutter kept for + // the scrollbar. + const left = padding.left; + const scrollbarX = size.width - padding.right - SCROLLBAR_WIDTH; + const width = Math.max(0, scrollbarX - SCROLLBAR_GAP - left); const record = this.conversations.current(); if (this.selectedId !== record.id) { this.selectedId = record.id; this.firstLine = null; } const transcript = record.session?.transcript ?? record.history?.transcript ?? []; const title = this.conversations.title(); - image.drawText(font, left, 6, truncateText(font, title, width), 180); + image.drawText(font, left, padding.top, truncateText(font, title, width), 180); const label = assistantBackendSetting.get() === "external" ? "External agent" : `${assistantModelLabel(record.model)} · ${record.reasoning === "default" ? "default reasoning" : record.reasoning}`; const footer = wrapText(font, `${label} ${gestureHints([ [GESTURE_CLICK, "menu"], [GESTURE_DOUBLE_CLICK, "back"], [GESTURE_LONG_PRESS, "hold to talk"], ])}`, width); - const footerY = size.height - step * footer.length; + const footerY = size.height - padding.bottom - font.lineHeight - step * (footer.length - 1); const footerTop = footerY - step * 3 - 12; - const historyTop = step + 16; + const historyTop = padding.top + step + 10; const visible = Math.max(1, Math.floor((footerTop - historyTop - 8) / step)); const lines: { text: string; color: number }[] = []; for (const entry of transcript) { @@ -82,7 +92,7 @@ class AiChatLayer implements Layer { if (this.maxFirstLine > 0) { const track = visible * step; const thumb = Math.max(8, Math.floor(track * visible / lines.length)); - image.fillRect(size.width - 5, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), 2, thumb, 110); + image.fillRect(scrollbarX, historyTop + Math.floor((track - thumb) * first / this.maxFirstLine), SCROLLBAR_WIDTH, thumb, 110); } image.fillRect(left, footerTop, width, 1, 65); const state = this.draft.active ? this.draft.status : record.session?.status || this.draft.status; diff --git a/app/apps/blocks/blocks-app.worker.ts b/app/apps/blocks/blocks-app.worker.ts index 7ab33372..3580bee9 100644 --- a/app/apps/blocks/blocks-app.worker.ts +++ b/app/apps/blocks/blocks-app.worker.ts @@ -25,6 +25,7 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; import { type MenuItem } from "../../ui/menu"; import { WindowMenu } from "../../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; +import type { Viewport } from "../../ui/viewport"; import { directionalFallback, GESTURE_CLICK, @@ -155,6 +156,13 @@ type BlocksWindow = { title: string; viewportWidth: number; viewportHeight: number; + /** + * The viewport as the shell last sent it: besides the size above, what each + * edge touches and the padding suggested there. The window menu's layers + * lay out by it; the game's own layout is fixed art that keeps its place + * (see LAYOUT_HEIGHT). + */ + viewport: Viewport; foreground: boolean; /** Whether this window is the shell's input target (pushed with each message). */ focused: boolean; @@ -207,6 +215,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { title: message.title, viewportWidth: message.viewport.width, viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, @@ -295,7 +304,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { if (!window) break; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; - window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight }); + window.viewport = message.viewport; + window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; if (window.foreground) renderAndSubmit(window, 0); break; @@ -357,7 +367,7 @@ function windowMenu(window: BlocksWindow): WindowMenu { title: () => window.title, items: () => (window.phase === "playing" ? [] : windowMenuItems(window)), claimsLongPress: () => window.phase === "playing", - size: { width: window.viewportWidth, height: window.viewportHeight }, + size: window.viewport, paintBase: () => paintContent(window), isFocused: () => window.focused, }); diff --git a/app/apps/calculator/calculator-graph.ts b/app/apps/calculator/calculator-graph.ts index 62c73362..f0a14e6d 100644 --- a/app/apps/calculator/calculator-graph.ts +++ b/app/apps/calculator/calculator-graph.ts @@ -35,25 +35,27 @@ export function drawGraph( const clampX = (x: number): number => Math.max(rect.x, Math.min(rect.x + rect.width - 1, x)); const clampY = (y: number): number => Math.max(rect.y, Math.min(rect.y + rect.height - 1, y)); - // Tick grid first so the curve and axes draw over it. + // Tick grid first so the curve and axes draw over it. A tick or axis at + // the viewport's far limit lands on the frame's last column or row, not + // past it: the caller may have run the rect to the window's edge. for (const tick of graph.xAxis.ticks) { if (tick < viewport.xMin || tick > viewport.xMax) continue; - const x = Math.round(px(tick)); + const x = Math.round(clampX(px(tick))); image.drawLine(x, rect.y, x, rect.y + rect.height - 1, GRID_VALUE); } for (const tick of graph.yAxis.ticks) { if (tick < viewport.yMin || tick > viewport.yMax) continue; - const y = Math.round(py(tick)); + const y = Math.round(clampY(py(tick))); image.drawLine(rect.x, y, rect.x + rect.width - 1, y, GRID_VALUE); } // Axes. if (viewport.yMin <= 0 && viewport.yMax >= 0) { - const y = Math.round(py(0)); + const y = Math.round(clampY(py(0))); image.drawLine(rect.x, y, rect.x + rect.width - 1, y, AXIS_VALUE); } if (viewport.xMin <= 0 && viewport.xMax >= 0) { - const x = Math.round(px(0)); + const x = Math.round(clampX(px(0))); image.drawLine(x, rect.y, x, rect.y + rect.height - 1, AXIS_VALUE); } diff --git a/app/apps/calculator/calculator.ts b/app/apps/calculator/calculator.ts index 6c7b3263..b6da988a 100644 --- a/app/apps/calculator/calculator.ts +++ b/app/apps/calculator/calculator.ts @@ -62,6 +62,24 @@ export const calculatorDegreesSetting = new ConfigSettingBoolean({ const CONTINUOUS_CLIENT = "calculator"; const DICTATION_HOLDER = "calculator-dictation"; +// --------------------------------------------------------------------------- +// Layout + +/** + * The column the view is laid out in: the window's width inside the padding + * suggested for its side edges (viewport.ts). Tabs, text, the plot and the + * footer all start at its left; centred text is centred in it. + */ +type Column = { x: number; width: number }; + +/** Below the tabs' text top: their underline, and where the body starts. */ +const TAB_UNDERLINE_OFFSET = 20; +const BODY_TOP_OFFSET = 31; +/** Indent, on both sides, of wrapped body text (a failure's reason, a step's detail, an answer). */ +const BODY_INDENT = 8; +/** The same for the prompt of an empty view, which keeps well clear of the sides. */ +const PROMPT_INDENT = 24; + // --------------------------------------------------------------------------- // Layer @@ -380,14 +398,17 @@ export class CalculatorLayer implements Layer { // Paint paint(ctx: LayerContext): GrayImage { - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const small = getDefaultSmallFont(); + const column: Column = { x: padding.left, width: Math.max(0, width - padding.left - padding.right) }; - this.paintTabs(image, small, width); + // The tabs sit at the top edge's padding and the footer at the bottom's; + // the body is what lies between. + this.paintTabs(image, small, column, padding.top); - const footerY = height - small.lineHeight - 6; - const bodyTop = 46; + const footerY = height - padding.bottom - small.lineHeight; + const bodyTop = padding.top + BODY_TOP_OFFSET; let bodyBottom = footerY - 4; // A standing offer or a running deep pass takes the rows just above the @@ -395,35 +416,36 @@ export class CalculatorLayer implements Layer { const offer = this.coordinator.pendingLongRun; const progress = this.coordinator.longRunProgress; if (offer || progress) { - bodyBottom = this.paintLongRunPanel(image, small, width, bodyBottom, offer, progress); + bodyBottom = this.paintLongRunPanel(image, small, column, bodyBottom, offer, progress); } if (this.working) { - this.drawCentered(image, small, Math.round((bodyTop + bodyBottom) / 2) - 8, "Working…", 180); + this.drawCentered(image, small, column, Math.round((bodyTop + bodyBottom) / 2) - 8, "Working…", 180); } else if (this.lastFailure !== null) { - this.paintFailure(image, small, width, bodyTop, bodyBottom); + this.paintFailure(image, small, column, bodyTop, bodyBottom); } else if (this.mode === "explain" && this.coordinator.stepPosition !== null) { - this.paintExplain(image, width, bodyTop, bodyBottom); + this.paintExplain(image, column, bodyTop, bodyBottom); } else if (this.mode === "graph" && this.coordinator.lastGraph) { - this.paintGraph(image, small, width, bodyTop, bodyBottom); + this.paintGraph(image, small, column, bodyTop, bodyBottom); } else if (this.lastAnswer) { - this.paintAnswer(image, width, bodyTop, bodyBottom); + this.paintAnswer(image, column, bodyTop, bodyBottom); } else { - this.paintEmpty(image, small, width, bodyTop, bodyBottom); + this.paintEmpty(image, small, column, bodyTop, bodyBottom); } - image.drawText(small, 24, footerY, this.footerHints(), 110); + image.drawText(small, column.x, footerY, this.footerHints(), 110); return image; } - /** The three modes as tabs, plus the deg/listening status chips. */ - private paintTabs(image: GrayImage, font: UiFont, width: number): void { - let x = 24; + /** The three modes as tabs from `y` down, plus the deg/listening status chips. */ + private paintTabs(image: GrayImage, font: UiFont, column: Column, y: number): void { + let x = column.x; + const underlineY = y + TAB_UNDERLINE_OFFSET; for (const mode of CALCULATOR_MODES) { const label = modeLabel(mode); const active = mode === this.mode; - image.drawText(font, x, 15, label, active ? 245 : 105); - image.drawLine(x, 35, x + font.measureText(label), 35, active ? 200 : 30); + image.drawText(font, x, y, label, active ? 245 : 105); + image.drawLine(x, underlineY, x + font.measureText(label), underlineY, active ? 200 : 30); x += font.measureText(label) + 18; } @@ -433,28 +455,28 @@ export class CalculatorLayer implements Layer { else if (this.continuousActive && this.foreground) chips.push("mic on"); if (chips.length > 0) { const text = chips.join(" · "); - image.drawText(font, width - 24 - font.measureText(text), 15, text, this.dictating ? 220 : 110); + image.drawText(font, column.x + column.width - font.measureText(text), y, text, this.dictating ? 220 : 110); } } private paintFailure( image: GrayImage, font: UiFont, - width: number, + column: Column, bodyTop: number, bodyBottom: number, ): void { - this.paintHeardLine(image, font, width, bodyTop); + this.paintHeardLine(image, font, column, bodyTop); const top = this.lastInput ? bodyTop + lineStep(font) + 6 : bodyTop + 8; - const lines = wrapText(font, this.lastFailure ?? "", width - 64); + const lines = wrapText(font, this.lastFailure ?? "", column.width - 2 * BODY_INDENT); const step = lineStep(font); const maxLines = Math.max(1, Math.floor((bodyBottom - top) / step)); for (let index = 0; index < Math.min(lines.length, maxLines); index++) { - image.drawText(font, 32, top + index * step, lines[index]!, 200); + image.drawText(font, column.x + BODY_INDENT, top + index * step, lines[index]!, 200); } } - private paintExplain(image: GrayImage, width: number, bodyTop: number, bodyBottom: number): void { + private paintExplain(image: GrayImage, column: Column, bodyTop: number, bodyBottom: number): void { const small = getDefaultSmallFont(); const medium = getDefaultMediumFont(); const position = this.coordinator.stepPosition; @@ -463,63 +485,65 @@ export class CalculatorLayer implements Layer { if (!position || !step) return; let y = bodyTop + 2; - image.drawText(small, 24, y, `Step ${position.index + 1} of ${position.total}`, 130); + image.drawText(small, column.x, y, `Step ${position.index + 1} of ${position.total}`, 130); y += lineStep(small) + 2; - image.drawText(medium, 24, y, truncateText(medium, step.title, width - 48), 245); + image.drawText(medium, column.x, y, truncateText(medium, step.title, column.width), 245); y += lineStep(medium) + 2; - image.drawText(medium, 24, y, truncateText(medium, plain(step.expression), width - 48), 220); + image.drawText(medium, column.x, y, truncateText(medium, plain(step.expression), column.width), 220); y += lineStep(medium) + 6; - const detailLines = wrapText(small, step.detail, width - 64); + const detailLines = wrapText(small, step.detail, column.width - 2 * BODY_INDENT); const stepPitch = lineStep(small); const maxLines = Math.max(0, Math.floor((bodyBottom - y) / stepPitch)); for (let index = 0; index < Math.min(detailLines.length, maxLines); index++) { - image.drawText(small, 32, y + index * stepPitch, detailLines[index]!, 150); + image.drawText(small, column.x + BODY_INDENT, y + index * stepPitch, detailLines[index]!, 150); } } private paintGraph( image: GrayImage, font: UiFont, - width: number, + column: Column, bodyTop: number, bodyBottom: number, ): void { const graph = this.coordinator.lastGraph; if (!graph) return; const headline = this.lastAnswer.split("\n")[0] ?? ""; - image.drawText(font, 24, bodyTop, truncateText(font, headline, width - 48), 200); + image.drawText(font, column.x, bodyTop, truncateText(font, headline, column.width), 200); + // The plot's frame spans the column, so it meets the side padding itself. const plotTop = bodyTop + lineStep(font) + 4; drawGraph(image, graph, { - x: 18, + x: column.x, y: plotTop, - width: width - 36, + width: column.width, height: Math.max(2, bodyBottom - plotTop), }); } - private paintAnswer(image: GrayImage, width: number, bodyTop: number, bodyBottom: number): void { + private paintAnswer(image: GrayImage, column: Column, bodyTop: number, bodyBottom: number): void { const small = getDefaultSmallFont(); - this.paintHeardLine(image, small, width, bodyTop); + this.paintHeardLine(image, small, column, bodyTop); const top = this.lastInput ? bodyTop + lineStep(small) + 4 : bodyTop; // A short single-line answer earns the large font; anything longer drops // to the medium font and wraps. const lines = this.lastAnswer.split("\n"); const large = getDefaultLargeFont(); - if (lines.length === 1 && large.measureText(lines[0]!) <= width - 64) { + const maxWidth = column.width - 2 * BODY_INDENT; + if (lines.length === 1 && large.measureText(lines[0]!) <= maxWidth) { const y = Math.round((top + bodyBottom - large.lineHeight) / 2); - this.drawCentered(image, large, y, lines[0]!, 245); + this.drawCentered(image, large, column, y, lines[0]!, 245); return; } const medium = getDefaultMediumFont(); - const wrapped = lines.flatMap((line) => wrapText(medium, line, width - 64)); + const wrapped = lines.flatMap((line) => wrapText(medium, line, maxWidth)); const step = lineStep(medium); const maxLines = Math.max(1, Math.floor((bodyBottom - top) / step)); const shown = wrapped.slice(0, maxLines); let y = Math.max(top, Math.round((top + bodyBottom - shown.length * step) / 2)); for (const line of shown) { - this.drawCentered(image, medium, y, line, 235); + this.drawCentered(image, medium, column, y, line, 235); y += step; } } @@ -527,7 +551,7 @@ export class CalculatorLayer implements Layer { private paintEmpty( image: GrayImage, font: UiFont, - width: number, + column: Column, bodyTop: number, bodyBottom: number, ): void { @@ -536,31 +560,32 @@ export class CalculatorLayer implements Layer { : this.listening === "continuous" ? `Say a problem, then "${modeLabel(this.mode).toLowerCase()} it"` : "Tap to say a problem — or use Voice input to type one"; - const lines = wrapText(font, prompt, width - 96); + const lines = wrapText(font, prompt, column.width - 2 * PROMPT_INDENT); const step = lineStep(font); let y = Math.round((bodyTop + bodyBottom - lines.length * step) / 2); for (const line of lines) { - this.drawCentered(image, font, y, line, 150); + this.drawCentered(image, font, column, y, line, 150); y += step; } } - private paintHeardLine(image: GrayImage, font: UiFont, width: number, y: number): void { + private paintHeardLine(image: GrayImage, font: UiFont, column: Column, y: number): void { if (!this.lastInput) return; const label = this.textInput ? "Entered" : "Heard"; - image.drawText(font, 24, y, label, 110); - const x = 24 + font.measureText(label) + 10; - image.drawText(font, x, y, truncateText(font, this.lastInput, width - x - 24), 160); + image.drawText(font, column.x, y, label, 110); + const x = column.x + font.measureText(label) + 10; + image.drawText(font, x, y, truncateText(font, this.lastInput, column.x + column.width - x), 160); } /** * The standing offer, or the live progress of an accepted run, in the two - * rows above the footer. Returns the new body bottom. + * rows above the footer, under a rule across the column. Returns the new + * body bottom. */ private paintLongRunPanel( image: GrayImage, font: UiFont, - width: number, + column: Column, bodyBottom: number, offer: LongRunOffer | null, progress: LongRunProgress | null, @@ -572,9 +597,9 @@ export class CalculatorLayer implements Layer { ? offerGlassesPrompt(offer).split("\n") : []; const panelTop = bodyBottom - lines.length * step - 6; - image.drawLine(16, panelTop, width - 16, panelTop, 40); + if (column.width > 0) image.drawLine(column.x, panelTop, column.x + column.width - 1, panelTop, 40); for (let index = 0; index < lines.length; index++) { - image.drawText(font, 24, panelTop + 4 + index * step, truncateText(font, lines[index]!, width - 48), 190); + image.drawText(font, column.x, panelTop + 4 + index * step, truncateText(font, lines[index]!, column.width), 190); } return panelTop - 2; } @@ -604,8 +629,8 @@ export class CalculatorLayer implements Layer { ]); } - private drawCentered(image: GrayImage, font: UiFont, y: number, text: string, value: number): void { - const x = Math.max(0, Math.round((image.width - font.measureText(text)) / 2)); + private drawCentered(image: GrayImage, font: UiFont, column: Column, y: number, text: string, value: number): void { + const x = column.x + Math.max(0, Math.round((column.width - font.measureText(text)) / 2)); image.drawText(font, x, y, text, value); } } diff --git a/app/apps/calendar/calendar.ts b/app/apps/calendar/calendar.ts index 4272cd97..5d112ab4 100644 --- a/app/apps/calendar/calendar.ts +++ b/app/apps/calendar/calendar.ts @@ -8,14 +8,25 @@ import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures"; 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"; -// Title position, shared with the other list apps (terminal, notifications). -const TITLE_X = 18; -const TITLE_Y = 10; -const LIST_TOP = 38; -const ROW_X = 16; +/** How far a row's text sits inside its selection box; the title lines up with it. */ +const ROW_TEXT_INSET = 10; +/** + * The title's drop below the window's top padding (the position shared with + * the other list apps: terminal, notifications), and the distance from its + * top to the first row. + */ +const TITLE_OFFSET_Y = 2; +const TITLE_BAND = 28; const ROW_GAP = 4; const MAX_EVENTS = 50; +/** + * The message shown in place of the list (no permission, nothing to show): + * its indent from the title, and its drop below the window's top padding. + */ +const MESSAGE_INDENT = 6; +const MESSAGE_OFFSET_Y = 64; /** Height of the day header band above a row that starts a new day. */ function dayHeaderHeight(font: UiFont): number { @@ -46,20 +57,25 @@ export class CalendarLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const viewport = ctx.stack.getBaseSize(); + const { width, height, padding } = viewport; const image = new GrayImage(width, height, 0); + // Rows' selection boxes sit at the padding suggested for the window's + // side edges; the title lines up with their text. + const titleX = padding.left + ROW_TEXT_INSET; + const titleY = padding.top + TITLE_OFFSET_Y; if (!hasCalendarPermission()) { - image.drawText(font, TITLE_X, TITLE_Y, "Calendar", 220); - this.paintPermissionPrompt(image, font, width, height); + image.drawText(font, titleX, titleY, "Calendar", 220); + this.paintPermissionPrompt(image, font, viewport); return image; } const events = readUpcomingEvents(MAX_EVENTS); if (!events.length) { - image.drawText(font, TITLE_X, TITLE_Y, "Calendar", 220); + image.drawText(font, titleX, titleY, "Calendar", 220); const state = getCalendarReadState(); - image.drawText(font, 24, 72, state === "loading" ? "Loading calendar..." + image.drawText(font, titleX + MESSAGE_INDENT, padding.top + MESSAGE_OFFSET_Y, state === "loading" ? "Loading calendar..." : state === "error" ? "Calendar unavailable. Try again shortly." : "No upcoming events.", 190); return image; } @@ -67,22 +83,26 @@ export class CalendarLayer implements Layer { const rows = buildEventRows(font, events); this.selectedIndex = clamp(this.selectedIndex, 0, rows.length - 1); - const listBottom = height; - const scrollY = scrollForSelected(rows, this.selectedIndex, listBottom - LIST_TOP); + // The agenda scrolls, so it runs to the window's top and bottom edges + // and rows scrolled past an end are cut off right at the edge. The + // padding suggested for those edges is inside the scrolled content + // instead: above the title and below the last row. + const listTop = titleY + TITLE_BAND; + const scrollY = scrollForSelected(rows, this.selectedIndex, height - listTop, padding.bottom); // The title scrolls away with the list; row text is deferred glyphs // (composited above raster fills), so a fixed title would show through // rows scrolled over it. - if (TITLE_Y - scrollY + font.lineHeight > 0) { - image.drawText(font, TITLE_X, TITLE_Y - scrollY, "Calendar", 220); + if (titleY - scrollY + font.lineHeight > 0) { + image.drawText(font, titleX, titleY - scrollY, "Calendar", 220); } - let cursorY = LIST_TOP - scrollY; - for (let index = 0; index < rows.length; index++) { + const rowWidth = width - padding.left - padding.right; + let cursorY = listTop - scrollY; + for (let index = 0; index < rows.length && cursorY < height; index++) { const row = rows[index]!; - if (cursorY + row.height >= LIST_TOP && cursorY <= listBottom) { - drawEventRow(image, font, row, ROW_X, cursorY, width, index === this.selectedIndex); + if (cursorY + row.height > 0) { + drawEventRow(image, font, row, padding.left, cursorY, rowWidth, index === this.selectedIndex); } cursorY += row.height + ROW_GAP; - if (cursorY > listBottom + 80) break; } return image; } @@ -102,13 +122,21 @@ export class CalendarLayer implements Layer { } } - private paintPermissionPrompt(image: GrayImage, font: UiFont, width: number, height: number): void { + private paintPermissionPrompt(image: GrayImage, font: UiFont, viewport: Viewport): void { + const { width, height, padding } = viewport; const message = "Grant calendar permission on your phone to see your events on the glasses."; - const lines = wrapText(font, message, width - 48); - for (let index = 0; index < lines.length; index++) { - image.drawText(font, 24, 72 + index * lineStep(font), lines[index]!, 190); + // Indented under the title, and as far short of the right padding. + const indent = ROW_TEXT_INSET + MESSAGE_INDENT; + const x = padding.left + indent; + const lines = wrapText(font, message, width - padding.left - padding.right - 2 * indent); + // The hint rests on the bottom padding; the message keeps to the lines that fit above it. + const hintY = height - padding.bottom - font.lineHeight; + const top = padding.top + MESSAGE_OFFSET_Y; + const maxLines = Math.max(1, Math.floor((hintY - top) / lineStep(font))); + for (let index = 0; index < Math.min(lines.length, maxLines); index++) { + image.drawText(font, x, top + index * lineStep(font), lines[index]!, 190); } - image.drawText(font, 24, height - 36, `${GESTURE_CLICK} request`, 110); + image.drawText(font, x, hintY, `${GESTURE_CLICK} request`, 110); } } @@ -137,6 +165,7 @@ function buildEventRows(font: UiFont, events: CalendarEvent[]): EventRow[] { return rows; } +/** Draw a row whose selection box spans `width` from `x`; its day header and text sit ROW_TEXT_INSET inside that. */ function drawEventRow( image: GrayImage, font: UiFont, @@ -146,30 +175,38 @@ function drawEventRow( width: number, selected: boolean, ): void { + const textX = x + ROW_TEXT_INSET; let cursorY = y; if (row.dayHeader) { - image.drawText(font, x, cursorY + 2, row.dayHeader, 150); + image.drawText(font, textX, cursorY + 2, row.dayHeader, 150); cursorY += dayHeaderHeight(font); } const bodyHeight = row.lines.length * lineStep(font) + 4; if (selected) { - image.fillRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 15, 6); - image.drawRoundedRect(x - 6, cursorY, width - 2 * (x - 6), bodyHeight, 90, 6); + image.fillRoundedRect(x, cursorY, width, bodyHeight, 15, 6); + image.drawRoundedRect(x, cursorY, width, bodyHeight, 90, 6); } - const maxTextWidth = width - 2 * x; + const maxTextWidth = width - 2 * ROW_TEXT_INSET; for (let index = 0; index < row.lines.length; index++) { const value = index === 0 ? (selected ? 235 : 205) : 160; - image.drawText(font, x, cursorY + 3 + index * lineStep(font), truncateText(font, row.lines[index]!, maxTextWidth), value); + image.drawText(font, textX, cursorY + 3 + index * lineStep(font), truncateText(font, row.lines[index]!, maxTextWidth), value); } } -function scrollForSelected(rows: EventRow[], selectedIndex: number, viewportHeight: number): number { +/** + * The scroll offset that centers the selected row in `viewportHeight`, the + * room below the title's resting place, within the ends of what scrolls + * there: the rows, then `padBottom` after the last of them. So the first + * row rests with the title and the padding above it in view, and the last + * with the padding below it. + */ +function scrollForSelected(rows: EventRow[], selectedIndex: number, viewportHeight: number, padBottom: number): number { let selectedTop = 0; for (let index = 0; index < selectedIndex; index++) { selectedTop += rows[index]!.height + ROW_GAP; } const selectedBottom = selectedTop + rows[selectedIndex]!.height; - const contentHeight = rows.reduce((sum, row) => sum + row.height + ROW_GAP, 0); + const contentHeight = rows.reduce((sum, row) => sum + row.height + ROW_GAP, -ROW_GAP) + padBottom; const maxScroll = Math.max(0, contentHeight - viewportHeight); const centered = selectedTop - Math.max(0, (viewportHeight - (selectedBottom - selectedTop)) / 2); return clamp(centered | 0, 0, maxScroll); diff --git a/app/apps/compass/calibration-layer.ts b/app/apps/compass/calibration-layer.ts index 772fccef..f9af9b36 100644 --- a/app/apps/compass/calibration-layer.ts +++ b/app/apps/compass/calibration-layer.ts @@ -25,10 +25,6 @@ const INSTRUCTIONS_BY_FRAME: Record = { magnetic: "magnetic compass heading (as read by another compass), then adjust the calibration offset below", }; -/** Margins around the instruction paragraph. */ -const TEXT_PADDING_X = 16; -const TEXT_PADDING_TOP = 10; - export class CompassCalibrationLayer implements Layer { constructor( /** Current uncorrected magnetometer heading, or null before the first reading. */ @@ -36,7 +32,7 @@ export class CompassCalibrationLayer implements Layer { ) {} paint(ctx: LayerContext): GrayImage { - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const small = getDefaultSmallFont(); const medium = getDefaultMediumFont(); @@ -47,14 +43,17 @@ export class CompassCalibrationLayer implements Layer { // Before the first reading there is no frame to name yet; the true-mode // wording is the default and the readout will correct it once data arrives. const frame = resolved?.frame ?? "true"; - const lines = wrapText(small, INSTRUCTIONS_PREAMBLE + INSTRUCTIONS_BY_FRAME[frame], width - TEXT_PADDING_X * 2); + // The instructions fill the width inside the padding suggested for the + // window's edges, from the top's down. + const lines = wrapText(small, INSTRUCTIONS_PREAMBLE + INSTRUCTIONS_BY_FRAME[frame], Math.max(0, width - padding.left - padding.right)); for (let i = 0; i < lines.length; i++) { - image.drawText(small, TEXT_PADDING_X, TEXT_PADDING_TOP + i * smallStep, lines[i]!, 190); + image.drawText(small, padding.left, padding.top + i * smallStep, lines[i]!, 190); } - const textBottom = TEXT_PADDING_TOP + lines.length * smallStep; + const textBottom = padding.top + lines.length * smallStep; - // Readouts along the bottom: dim label over a bright value, in thirds. - const valueTop = height - medium.lineHeight; + // Readouts along the bottom, above its padding: dim label over a bright + // value, in equal columns. + const valueTop = height - padding.bottom - medium.lineHeight; const labelTop = valueTop - small.lineHeight; const columns: Array<[string, string]> = [ ["Raw", raw === null ? "--°" : `${Math.round(raw)}°`], diff --git a/app/apps/compass/compass-app.ts b/app/apps/compass/compass-app.ts index 8c01ff0f..714515ce 100644 --- a/app/apps/compass/compass-app.ts +++ b/app/apps/compass/compass-app.ts @@ -41,18 +41,9 @@ export const COMPASS_SURFACE_ID = "window:compass"; const RECONCILE_INTERVAL_MS = 400; /** Vertical breathing room between the readout, the status line and the rose. */ const STACK_GAP = 8; -/** Padding above the readout and below the rose's near edge. */ -const TOP_PAD = 2; -const BOTTOM_PAD = 4; -/** Clearance between the rose's widest point and the viewport edge. */ -const EDGE_PAD = 8; -/** Inset of the top-right magnetometer calibration readout from the window edges. */ -const ACCURACY_TOP_PAD = TOP_PAD + 4; -const ACCURACY_RIGHT_PAD = EDGE_PAD + 4; /** Cap on the rose's radius, so a tall window doesn't get a comical one. */ const MAX_ROSE_RADIUS = 98; - class CompassLayer implements Layer { private rawHeading: number | null = null; private diagnostics: CompassDiagnostics | null = null; @@ -161,35 +152,41 @@ class CompassLayer implements Layer { } paint(ctx: LayerContext): GrayImage { - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const small = getDefaultSmallFont(); const large = getDefaultLargeFont(); const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees; const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`; - const statusLines = wrapText(small, this.statusText(), width - STACK_GAP * 2); + // Text and the rose keep inside the padding suggested for the window's edges. + const textWidth = Math.max(0, width - padding.left - padding.right); + const statusLines = wrapText(small, this.statusText(), textWidth); const smallStep = lineStep(small); - const headingFont = large.measureText(headingText) <= width - EDGE_PAD * 2 ? large : small; + const headingFont = large.measureText(headingText) <= textWidth ? large : small; const readoutWidth = headingFont.measureText(headingText); const readoutHeight = headingFont.lineHeight; // One column centred on the display's true centre, so the rose sits where // the wearer is looking rather than 32px beside it (with a side strip). const cx = screenCenterInViewportX(); + // A line of that column: centred there, short of leaving the padded width. + const lineX = (lineWidth: number) => + Math.round(Math.max(padding.left, Math.min(width - padding.right - lineWidth, cx - lineWidth / 2))); // The rose is sized and placed first — it hangs off the bottom edge — and - // the readout then floats in whatever room is left above it. + // the readout then floats in whatever room is left above it. Its far + // edges land on the last row and column inside the padding. const textHeight = readoutHeight + 4 + statusLines.length * smallStep; const { cy, radius, ringTop } = layoutCompassRose({ width, height, cx, - topClearance: TOP_PAD + textHeight + STACK_GAP, - bottomPad: BOTTOM_PAD, - edgePad: EDGE_PAD, + topClearance: padding.top + textHeight + STACK_GAP, + bottomPad: padding.bottom + 1, + edgePad: { left: padding.left, right: padding.right + 1 }, maxRadius: MAX_ROSE_RADIUS, }); - let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2)); + let y = Math.max(padding.top, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2)); const fade = heading === null ? 0.45 : 1; const clipY = y + textHeight + 6; const backgroundKey = [width, height, cx, cy, radius, clipY, fade].join(","); @@ -201,16 +198,16 @@ class CompassLayer implements Layer { } const image = this.background.image.clone(); drawCompassRose(image, cx, cy, radius, heading); - const readoutX = Math.round(Math.max(EDGE_PAD, Math.min(width - EDGE_PAD - readoutWidth, cx - readoutWidth / 2))); - image.drawText(headingFont, readoutX, y, headingText, heading === null ? 150 : 255); + image.drawText(headingFont, lineX(readoutWidth), y, headingText, heading === null ? 150 : 255); y += readoutHeight + 4; for (const line of statusLines) { - image.drawText(small, Math.round(cx - small.measureText(line) / 2), y, line, 125); + image.drawText(small, lineX(small.measureText(line)), y, line, 125); y += smallStep; } if (glassesHaveCompass()) { + // The magnetometer calibration readout sits in the window's top-right corner. const accuracyText = magneticAccuracyText(this.diagnostics); - image.drawText(small, width - ACCURACY_RIGHT_PAD - small.measureText(accuracyText), ACCURACY_TOP_PAD, accuracyText, 175); + image.drawText(small, width - padding.right - small.measureText(accuracyText), padding.top, accuracyText, 175); } return image; diff --git a/app/apps/compass/compass-rose.ts b/app/apps/compass/compass-rose.ts index f32be49f..0e266530 100644 --- a/app/apps/compass/compass-rose.ts +++ b/app/apps/compass/compass-rose.ts @@ -78,17 +78,19 @@ export function layoutCompassRose(options: { /** Lowest y already used above the rose (readout text, its gap). */ topClearance: number; bottomPad: number; - edgePad: number; + /** Clearance between the rose's widest point and the box's sides: one figure for both, or each side's. */ + edgePad: number | { left: number; right: number }; maxRadius: number; minRadius?: number; }): { cx: number; cy: number; radius: number; ringTop: number } { const { width, height, cx } = options; + const sidePad = typeof options.edgePad === "number" ? { left: options.edgePad, right: options.edgePad } : options.edgePad; const radius = Math.max( options.minRadius ?? 24, Math.min( options.maxRadius, Math.floor((height - options.bottomPad - options.topClearance - TICK_HEIGHT) / (RING_HEIGHT_PER_RADIUS + DISC_DEPTH)), - Math.floor((Math.min(cx, width - cx) - options.edgePad) / RING_HALF_WIDTH_PER_RADIUS), + Math.floor(Math.min(cx - sidePad.left, width - cx - sidePad.right) / RING_HALF_WIDTH_PER_RADIUS), ), ); // The wall hangs below the rim, so the near rim sits a wall's depth up diff --git a/app/apps/developer/accelerometer-demo.ts b/app/apps/developer/accelerometer-demo.ts index bdc88c0a..2e39c14d 100644 --- a/app/apps/developer/accelerometer-demo.ts +++ b/app/apps/developer/accelerometer-demo.ts @@ -13,6 +13,12 @@ import { shell } from "../../ui/shell/shell"; const IMU_REPORT_PACE = 200; // How often to reconcile the IMU on/off state with window visibility. const RECONCILE_INTERVAL_MS = 400; +/** The readout under the title sits this far in from the title's left edge. */ +const BODY_INDENT = 2; +/** An axis row, from the readout's left edge: its value, then its bar, which stops this short of the right edge's padding. */ +const VALUE_OFFSET_X = 22; +const BAR_OFFSET_X = 96; +const BAR_RIGHT_INSET = 4; /** * Accelerometer demo: turns the IMU stream on while this page is visible (the @@ -77,12 +83,17 @@ export class AccelerometerDemoLayer implements Layer { paint(ctx: LayerContext): GrayImage { const small = getDefaultSmallFont(); const medium = getDefaultMediumFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); + // The title line sits at the padding suggested for the top, left and + // right edges; everything under it is placed from there. + const top = padding.top; + const right = width - padding.right; + const bodyX = padding.left + BODY_INDENT; - image.drawText(small, 20, 8, "Accelerometer", 220); + image.drawText(small, padding.left, top, "Accelerometer", 220); const status = this.enabled ? "IMU on" : "IMU off"; - image.drawText(small, width - 20 - small.measureText(status), 8, status, 150); + image.drawText(small, right - small.measureText(status), top, status, 150); if (this.reading) { const axes: Array<[string, number]> = [ @@ -90,19 +101,19 @@ export class AccelerometerDemoLayer implements Layer { ["Y", this.reading.y], ["Z", this.reading.z], ]; - const barX = 118; - const barW = width - barX - 24; + const barX = bodyX + BAR_OFFSET_X; + const barW = Math.max(0, right - BAR_RIGHT_INSET - barX); const scale = Math.max(this.peak, 1e-6); - let y = 40; + let y = top + 32; for (const [label, value] of axes) { - image.drawText(medium, 22, y, label, 210); - image.drawText(medium, 44, y, value.toFixed(3), 235); + image.drawText(medium, bodyX, y, label, 210); + image.drawText(medium, bodyX + VALUE_OFFSET_X, y, value.toFixed(3), 235); drawCenteredBar(image, barX, y + 2, barW, 12, clamp(value / scale, -1, 1)); y += 30; } image.drawText( small, - 22, + bodyX, y + 6, `source: ${imuSourceLabel(this.reading.source)} samples: ${this.sampleCount}`, 130, @@ -111,9 +122,9 @@ export class AccelerometerDemoLayer implements Layer { const message = this.enabled ? "Waiting for IMU data…" : "Paused (page not visible)."; - image.drawText(small, 22, 44, message, 190); - image.drawText(small, 22, 66, "The firmware may not report the", 120); - image.drawText(small, 22, 82, "accelerometer at all — experimental.", 120); + image.drawText(small, bodyX, top + 36, message, 190); + image.drawText(small, bodyX, top + 58, "The firmware may not report the", 120); + image.drawText(small, bodyX, top + 74, "accelerometer at all — experimental.", 120); } return image; diff --git a/app/apps/developer/bandwidth-benchmark.ts b/app/apps/developer/bandwidth-benchmark.ts index 73310aa6..3237ab6e 100644 --- a/app/apps/developer/bandwidth-benchmark.ts +++ b/app/apps/developer/bandwidth-benchmark.ts @@ -27,9 +27,12 @@ const POLL_INTERVAL_MS = 500; // timeout drain + slack). const POLL_DEADLINE_MS = DURATION_MS + 15_000; -const HEADER_HEIGHT = 30; -const LIST_X = 20; -const VALUE_X = 150; +/** The title band: the rows and the run's text start this far below the top edge's padding. */ +const HEADER_HEIGHT = 22; +/** Horizontal inset of a row's text from its highlight box; the title and the text under the rows line up with it. */ +const ROW_TEXT_INSET = 6; +/** A row's value column, from its label's left edge. */ +const VALUE_OFFSET_X = 130; type Phase = "config" | "arming" | "running" | "done"; type ConfigRow = "size" | "window" | "link" | "start"; @@ -126,36 +129,43 @@ export class BandwidthBenchmarkLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); + // The rows' highlight boxes span the window inside the padding suggested + // for its side edges, and the title sits at the top edge's; the page's + // text shares the rows' text column. + const textX = padding.left + ROW_TEXT_INSET; + const bodyTop = padding.top + HEADER_HEIGHT; - image.drawText(font, 20, 8, "BLE bandwidth", 220); + image.drawText(font, textX, padding.top, "BLE bandwidth", 220); if (this.phase === "arming" || this.phase === "running") { - image.drawText(font, LIST_X, HEADER_HEIGHT + 8, "Running: streaming no-op packets for 15 s...", 200); + image.drawText(font, textX, bodyTop + 8, "Running: streaming no-op packets for 15 s...", 200); image.drawText( font, - LIST_X, - HEADER_HEIGHT + 30, + textX, + bodyTop + 30, `${MESSAGE_SIZES[this.sizeIndex]} B, window ${WINDOW_SIZES[this.windowIndex]}` + (LINK_REQUESTS_SUPPORTED ? `, ${LINK_MODES[this.linkModeIndex]}` : ""), 150, ); - image.drawText(font, LIST_X, HEADER_HEIGHT + 52, "Display updates pause until the run finishes.", 110); + image.drawText(font, textX, bodyTop + 52, "Display updates pause until the run finishes.", 110); return image; } + // The rows all fit their box, so it is a fixed block under the title + // rather than a scroller that would run to the window's bottom edge. const rowH = tightRowHeight(font) + 4; const rowCount = this.menu.items.length; this.menu.paint( image, - { x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: rowCount * rowH }, + { x: padding.left, y: bodyTop - 1, width: Math.max(0, width - padding.left - padding.right), height: rowCount * rowH }, ctx.stack.isFocused(), ); - let y = HEADER_HEIGHT + rowCount * rowH + 10; + let y = bodyTop + rowCount * rowH + 10; if (this.error) { - image.drawText(font, LIST_X, y, this.error, 180); + image.drawText(font, textX, y, this.error, 180); } else if (this.result) { const r = this.result; const seconds = r.elapsedMs / 1000; @@ -171,7 +181,9 @@ export class BandwidthBenchmarkLayer implements Layer { lines.push(`Link request: ${LINK_MODES[r.linkMode] ?? "Current"}${phy}`); } for (const line of lines) { - image.drawText(font, LIST_X, y, line, 190); + // A window too short for every line (the RayNeo iO's) shows the first ones. + if (y + font.lineHeight > height) break; + image.drawText(font, textX, y, line, 190); y += 20; } } @@ -187,10 +199,9 @@ export class BandwidthBenchmarkLayer implements Layer { : item === "link" ? ["Link request", LINK_MODES[this.linkModeIndex]!] : [this.phase === "done" ? "Run again" : "Start", ""]; const textY = centeredTextY(font, y, height); - // Text sits 6px in from the row box (LIST_X), values at VALUE_X. - image.drawText(font, x + 6, textY, label, selected ? 255 : 200); + image.drawText(font, x + ROW_TEXT_INSET, textY, label, selected ? 255 : 200); if (value) { - image.drawText(font, x + 6 + VALUE_X - LIST_X, textY, value, selected ? 235 : 160); + image.drawText(font, x + ROW_TEXT_INSET + VALUE_OFFSET_X, textY, value, selected ? 235 : 160); } } diff --git a/app/apps/developer/buzzer-demo.ts b/app/apps/developer/buzzer-demo.ts index 258ace7c..e10ce694 100644 --- a/app/apps/developer/buzzer-demo.ts +++ b/app/apps/developer/buzzer-demo.ts @@ -13,9 +13,12 @@ import { type SoundEffect, } from "../../ui/sound-effects"; -const HEADER_HEIGHT = 30; -const LIST_X = 20; -const FOOTER_HEIGHT = 0; +/** The title band: the list starts this far below the top edge's padding. */ +const HEADER_HEIGHT = 22; +/** Horizontal inset of a row's text from its highlight box; the title lines up with it. */ +const ROW_TEXT_INSET = 6; +/** The status text ends this far short of the rows' right edge. */ +const STATUS_INSET = 10; function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -39,19 +42,26 @@ export class BuzzerDemoLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - image.drawText(font, 20, 8, "Buzzer demo", 220); + image.drawText(font, padding.left + ROW_TEXT_INSET, padding.top, "Buzzer demo", 220); const status = this.playing ? `Playing: ${this.playing}` : `${GESTURE_CLICK} play`; - image.drawText(font, width - 24 - font.measureText(status), 8, status, 140); + image.drawText(font, width - padding.right - STATUS_INSET - font.measureText(status), padding.top, status, 140); - const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT; - this.menu.paint( - image, - { x: LIST_X - 6, y: HEADER_HEIGHT - 1, width: width - 2 * LIST_X + 12, height: listHeight }, - ctx.stack.isFocused(), - ); + // The title line stays put, so the list starts right under it with no + // padding of its own there. It scrolls, so it runs to the window's + // bottom edge with the padding suggested for that edge inside it, after + // the last row. + const listTop = padding.top + HEADER_HEIGHT - 1; + if (height > listTop) { + this.menu.paint( + image, + { x: padding.left, y: listTop, width: Math.max(0, width - padding.left - padding.right), height: height - listTop, + padTop: 0, padBottom: padding.bottom }, + ctx.stack.isFocused(), + ); + } return image; } @@ -104,6 +114,6 @@ export class BuzzerDemoLayer implements Layer { function drawEffectRow({ image, item: effect, x, y, height, selected }: MenuDrawArgs): void { const font = getDefaultSmallFont(); const textY = centeredTextY(font, y, height); - image.drawText(font, x + 6, textY, effect.name, selected ? 255 : 200); - image.drawText(font, x + 6 + 110, textY, effect.desc, selected ? 180 : 120); + image.drawText(font, x + ROW_TEXT_INSET, textY, effect.name, selected ? 255 : 200); + image.drawText(font, x + ROW_TEXT_INSET + 110, textY, effect.desc, selected ? 180 : 120); } diff --git a/app/apps/developer/developer-app.ts b/app/apps/developer/developer-app.ts index a7faf8cd..276990b3 100644 --- a/app/apps/developer/developer-app.ts +++ b/app/apps/developer/developer-app.ts @@ -12,7 +12,6 @@ import { RingStatusLayer } from "./ring-status"; import { LoadAppFromQrLayer, LoadAppFromUrlLayer } from "./load-app"; import { unicodeTestMenu } from "./unicode-test"; import { type AppContext } from "../app-definition"; -import { appViewportSize } from "../../ui/shell/geometry"; import { createInProcessWindow, YieldAtRootLayer, @@ -23,17 +22,9 @@ import { export const DEVELOPER_WINDOW_ID = "developer"; export const DEVELOPER_SURFACE_ID = "window:developer"; -const MENU_LAYOUT = { - x: 8, - y: 8, - width: 272, - showBorder: false, - minHeight: 0, - maxHeight: appViewportSize("min").height - 16, - // These menus are pages, not popups: "Debug tests" replaces the root menu - // visually instead of letting the taller root show through beneath it. - opaque: true, -}; +// These menus are pages, not popups: "Debug tests" replaces the root menu +// visually instead of letting the taller root show through beneath it. +const MENU_LAYOUT = { page: true, width: 248 } as const; type OpenPage = (ctx: Parameters[0]) => void; diff --git a/app/apps/developer/input-events.ts b/app/apps/developer/input-events.ts index a779d5a9..e3da7375 100644 --- a/app/apps/developer/input-events.ts +++ b/app/apps/developer/input-events.ts @@ -6,6 +6,14 @@ import { addInputListener, InputEventLog, inputTimestamp } from "../../ui/input- import type { Layer, LayerContext } from "../../ui/layers"; import { drawListScrollbar, type MenuItem } from "../../ui/menu"; +/** Room the log's rows keep free on the right for the scrollbar, and the bar's own width. */ +const SCROLLBAR_GUTTER = 6; +const SCROLLBAR_WIDTH = 3; +/** A row's second (raw report) line is indented this far, and stops as far short of the row's end. */ +const DETAIL_INDENT = 12; +/** Gap between the log and the hint line under it. */ +const FOOTER_GAP = 6; + /** Gestures are samples here; back/clear/pause live in the tap-then-hold menu. */ export class InputEventsLayer implements Layer { readonly acceptsDirectional = true; @@ -48,30 +56,41 @@ export class InputEventsLayer implements Layer { } paint(ctx: LayerContext): GrayImage { - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const font = getDefaultSmallFont(); - image.drawText(font, 12, 8, "Input events", 230); + // The title and column-heading lines stay put at the top and the hint + // line at the bottom, each inside the padding suggested for its edge. + // The log scrolls between them, so it needs no padding of its own. + const left = padding.left; + const top = padding.top; + const right = width - padding.right; + const footerY = height - padding.bottom - font.lineHeight; + const listTop = top + 42; + const listBottom = footerY - FOOTER_GAP; + // The rows stop short of the scrollbar, which keeps inside the padding. + const rowsRight = right - SCROLLBAR_GUTTER; + image.drawText(font, left, top, "Input events", 230); const status = `${this.log.paused ? "Paused" : "Live"} ${this.log.count} events`; - image.drawText(font, width - 12 - font.measureText(status), 8, status, 180); - image.drawText(font, 12, 28, this.phoneTime ? "Phone receive time | gap ms | source | event" : "Time (R=ring tick) | gap | source | event", 140); + image.drawText(font, right - font.measureText(status), top, status, 180); + image.drawText(font, left, top + 20, this.phoneTime ? "Phone receive time | gap ms | source | event" : "Time (R=ring tick) | gap | source | event", 140); const lineHeight = Math.max(18, font.lineHeight + 2); const rowHeight = lineHeight * 2 + 4; - this.visibleRows = Math.max(1, Math.floor((height - 76) / rowHeight)); + this.visibleRows = Math.max(1, Math.floor((listBottom - listTop) / rowHeight)); this.scrollRow = Math.min(this.scrollRow, Math.max(0, this.log.entries.length - this.visibleRows)); - const timeX = 12; + const timeX = left; const gapX = timeX + font.measureText("00:00:00.000") + 12; const sourceX = gapX + font.measureText("+99999") + 12; const eventX = sourceX + font.measureText("right-arm") + 12; if (!this.log.entries.length) { - image.drawText(font, 12, 62, "Touch, tap, double-tap, hold, or swipe the ring.", 210); - image.drawText(font, 12, 82, "Press arrives before an interpreted gesture.", 150); + image.drawText(font, left, listTop + 12, "Touch, tap, double-tap, hold, or swipe the ring.", 210); + image.drawText(font, left, listTop + 32, "Press arrives before an interpreted gesture.", 150); } for (const [index, entry] of this.log.entries.slice(this.scrollRow, this.scrollRow + this.visibleRows).entries()) { const { event } = entry; const ringTime = !this.phoneTime && event.ringInput; const gapMs = ringTime ? entry.ringGapTicks : entry.gapMs; - const y = 50 + index * rowHeight; + const y = listTop + index * rowHeight; const gap = gapMs === null ? "-" : `${gapMs >= 0 ? "+" : ""}${gapMs}`; const source = "source" in event ? event.source ?? "-" : "-"; const label = (entry.filtered ? "*" : "") + (event.type === "unknown" @@ -80,18 +99,20 @@ export class InputEventsLayer implements Layer { image.drawText(font, timeX, y, ringTime ? `R${ringTime.tick}` : inputTimestamp(event.timestampMs), 170); image.drawText(font, gapX, y, truncateText(font, gap, sourceX - gapX - 4), 160); image.drawText(font, sourceX, y, source, 180); - image.drawText(font, eventX, y, truncateText(font, label, width - eventX - 12), event.type === "ring-press" ? 255 : 200); + image.drawText(font, eventX, y, truncateText(font, label, rowsRight - eventX), event.type === "ring-press" ? 255 : 200); if (event.ringInput) { const { type, aux, speed } = event.ringInput; // These are the original report bytes, not physical distance/speed units. const details = `Raw ring: type ${type} AUX ${aux} speed ${speed}`; - image.drawText(font, timeX + 12, y + lineHeight, - truncateText(font, details, width - timeX - 36), 170); + image.drawText(font, timeX + DETAIL_INDENT, y + lineHeight, + truncateText(font, details, rowsRight - timeX - 2 * DETAIL_INDENT), 170); } } - if (this.log.paused) drawListScrollbar(image, width - 5, 50, height - 78, - this.scrollRow, this.visibleRows, this.log.entries.length); - image.drawText(font, 12, height - 20, + if (this.log.paused && listBottom > listTop) { + drawListScrollbar(image, right - SCROLLBAR_WIDTH, listTop, listBottom - listTop, + this.scrollRow, this.visibleRows, this.log.entries.length); + } + image.drawText(font, left, footerY, this.log.paused ? "Scroll: history Tap-hold: controls" : "Unfiltered (*=dedup) Tap-hold: controls", 140); return image; } diff --git a/app/apps/developer/light-sensor-demo.ts b/app/apps/developer/light-sensor-demo.ts index 8f713f8d..9182b20c 100644 --- a/app/apps/developer/light-sensor-demo.ts +++ b/app/apps/developer/light-sensor-demo.ts @@ -22,6 +22,10 @@ const RECONCILE_INTERVAL_MS = 400; // Length of the on-screen trace. const HISTORY_MS = 30_000; const MAX_SAMPLES = Math.ceil(HISTORY_MS / 100) + 2; +/** The readout under the title sits this far in from the title's left edge. */ +const BODY_INDENT = 2; +/** Gap between the trace's box and the footer line under it. */ +const CHART_FOOTER_GAP = 18; type Sample = { atMs: number; value: number; peak: number }; @@ -102,39 +106,47 @@ export class LightSensorDemoLayer implements Layer { paint(ctx: LayerContext): GrayImage { const small = getDefaultSmallFont(); const medium = getDefaultMediumFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); + // The title line, the trace's box and the footer line sit at the padding + // suggested for the edges they reach; the rest is placed from them. + const left = padding.left; + const top = padding.top; + const right = width - padding.right; + const bodyX = left + BODY_INDENT; + const footerY = height - padding.bottom - small.lineHeight; - image.drawText(small, 20, 8, "Light sensor", 220); + image.drawText(small, left, top, "Light sensor", 220); const status = this.enabled ? "polling" : "paused"; - image.drawText(small, width - 20 - small.measureText(status), 8, status, 150); + image.drawText(small, right - small.measureText(status), top, status, 150); const report = this.report; if (!report) { const message = this.enabled ? "Waiting for a report…" : "Paused (page not visible)."; - image.drawText(small, 22, 44, message, 190); - image.drawText(small, 22, 66, "Needs Faceclaw's custom firmware.", 120); - image.drawText(small, 20, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(small, bodyX, top + 36, message, 190); + image.drawText(small, bodyX, top + 58, "Needs Faceclaw's custom firmware.", 120); + image.drawText(small, left, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } // Headline reading. The stock driver's unit is roughly a tenth of a lux. const lux = (report.alsValue / 10).toFixed(1); - image.drawText(medium, 22, 30, `${lux} lux`, 245); + image.drawText(medium, bodyX, top + 22, `${lux} lux`, 245); const peakText = `peak ${(report.peak / 10).toFixed(1)}`; - image.drawText(small, 22 + medium.measureText(`${lux} lux`) + 14, 36, peakText, 170); + image.drawText(small, bodyX + medium.measureText(`${lux} lux`) + 14, top + 28, peakText, 170); const flags: string[] = []; if (!report.opened) flags.push("sensor closed"); if (report.passive) flags.push("passive"); if (report.autoBrightness) flags.push("auto on"); if (report.passive && !report.readOk) flags.push("read failed"); - image.drawText(small, 22, 58, flags.join(" ") || "stock polling", 150); + image.drawText(small, bodyX, top + 50, flags.join(" ") || "stock polling", 150); + const statsY = top + 68; image.drawText( small, - 22, - 76, + bodyX, + statsY, `brightness ${report.brightness} stock target ${report.stockTarget} gear ${report.gear} scale ${(report.scaleQ10 / 1024).toFixed(2)}`, 170, ); @@ -142,16 +154,17 @@ export class LightSensorDemoLayer implements Layer { // 30 s trace of the reading (bright) and the driver's 5-sample peak (dim). // The axis label sits between the stats line and the chart, so the chart // starts a full text line lower than the stats. - const chartTop = 76 + small.lineHeight * 2 + 4; - const bounds = { x: 20, y: chartTop, width: width - 40, height: height - chartTop - 34 }; + const chartTop = statsY + small.lineHeight * 2 + 4; + const bounds = { x: left, y: chartTop, width: right - left, height: footerY - CHART_FOOTER_GAP - chartTop }; const maxValue = Math.max(...this.samples.map((sample) => Math.max(sample.value, sample.peak)), 10); const scale = roundUpScale(maxValue * 1.08); image.drawText(small, bounds.x, bounds.y - small.lineHeight - 2, `0–${(scale / 10).toFixed(0)} lux, last 30 s`, 120); - drawTrace(image, this.samples, Date.now(), bounds, scale); + // A window too short for the trace (a large font on the RayNeo iO) goes without. + if (bounds.width > 1 && bounds.height > 1) drawTrace(image, this.samples, Date.now(), bounds, scale); - image.drawText(small, 20, height - 16, `reports: ${this.reportCount}`, 110); + image.drawText(small, left, footerY, `reports: ${this.reportCount}`, 110); const back = `${GESTURE_DOUBLE_CLICK} back`; - image.drawText(small, width - 20 - small.measureText(back), height - 16, back, 110); + image.drawText(small, right - small.measureText(back), footerY, back, 110); return image; } diff --git a/app/apps/developer/load-app.ts b/app/apps/developer/load-app.ts index ba6c9555..15532e3f 100644 --- a/app/apps/developer/load-app.ts +++ b/app/apps/developer/load-app.ts @@ -9,8 +9,6 @@ import { lineStep } from "../../ui/metrics"; import { developerAppUrlSetting } from "../../ui/dashboard-settings"; import { cancelQrScan, isQrScannerAvailable, scanQrCode } from "../../native/qr-scan"; -const MARGIN = 20; - /** * "Load app from URL": the glasses side of the phone text editor. Entering the * page opens the editor on developerAppUrlSetting, the typed URL echoes here @@ -44,27 +42,30 @@ export class LoadAppFromUrlLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + // The page's text fills the window inside the padding suggested for its + // edges: the title at the top, the hint line at the bottom. + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const step = lineStep(font); - const textWidth = width - 2 * MARGIN; + const left = padding.left; + const textWidth = Math.max(0, width - padding.left - padding.right); - image.drawText(font, MARGIN, 8, "Load app from URL", 220); - let y = 8 + step + 6; - image.drawText(font, MARGIN, y, "Type the app's URL in the phone app", 150); + image.drawText(font, left, padding.top, "Load app from URL", 220); + let y = padding.top + step + 6; + image.drawText(font, left, y, "Type the app's URL in the phone app", 150); y += step; - image.drawText(font, MARGIN, y, "(or use voice input from the menu).", 150); + image.drawText(font, left, y, "(or use voice input from the menu).", 150); y += step + 8; const url = developerAppUrlSetting.get(); for (const line of wrapText(font, url || "(empty)", textWidth).slice(0, 3)) { - image.drawText(font, MARGIN, y, line, url ? 230 : 110); + image.drawText(font, left, y, line, url ? 230 : 110); y += step; } if (this.error) { y += 4; for (const line of wrapText(font, this.error, textWidth).slice(0, 2)) { - image.drawText(font, MARGIN, y, line, 150); + image.drawText(font, left, y, line, 150); y += step; } } @@ -72,7 +73,7 @@ export class LoadAppFromUrlLayer implements Layer { const footer = this.launching ? "Loading..." : `${GESTURE_CLICK} load ${GESTURE_DOUBLE_CLICK} cancel`; - image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110); + image.drawText(font, left, height - padding.bottom - font.lineHeight, footer, 110); return image; } @@ -145,19 +146,22 @@ export class LoadAppFromQrLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + // Laid out like the URL page: text inside the padding suggested for the + // window's edges, the hint line at the bottom. + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const step = lineStep(font); + const left = padding.left; - image.drawText(font, MARGIN, 8, "Load app from QR code", 220); - let y = 8 + step + 6; - for (const line of wrapText(font, this.status, width - 2 * MARGIN).slice(0, 5)) { - image.drawText(font, MARGIN, y, line, 170); + image.drawText(font, left, padding.top, "Load app from QR code", 220); + let y = padding.top + step + 6; + for (const line of wrapText(font, this.status, Math.max(0, width - padding.left - padding.right)).slice(0, 5)) { + image.drawText(font, left, y, line, 170); y += step; } const footer = this.busy ? `${GESTURE_DOUBLE_CLICK} back` : `${GESTURE_CLICK} scan again ${GESTURE_DOUBLE_CLICK} back`; - image.drawText(font, MARGIN, height - font.lineHeight - 12, footer, 110); + image.drawText(font, left, height - padding.bottom - font.lineHeight, footer, 110); return image; } diff --git a/app/apps/developer/microphones-monitor.ts b/app/apps/developer/microphones-monitor.ts index 513bc8db..5c29d11d 100644 --- a/app/apps/developer/microphones-monitor.ts +++ b/app/apps/developer/microphones-monitor.ts @@ -37,6 +37,8 @@ const STOCK_COUNTER_OFFSET = 204; // Counter gaps at or beyond this are late/duplicate copies, not losses. const STOCK_LATE_GAP = 128; const LEVEL_FLOOR_DB = -90; +/** Gap between the two temples' columns. */ +const COLUMN_GAP = 16; // wallMs (JS receipt time) windows the rates; arrivalMs (the session core's // elapsed-realtime stamp, taken on the BLE thread) measures gaps without the @@ -229,39 +231,45 @@ export class MicrophonesMonitorLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); const line = font.lineHeight; - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const nowMs = Date.now(); + // The title line, the two columns and the footer line span the window + // inside the padding suggested for its edges. + const left = padding.left; + const titleY = padding.top; + const right = width - padding.right; + const footerY = height - padding.bottom - line; - image.drawText(font, 16, 5, "Microphones (passive)", 230); + image.drawText(font, left, titleY, "Microphones (passive)", 230); const capture = isFaceclawAudioCaptureActive() ? "Faceclaw capture: on" : "Faceclaw capture: off"; - image.drawText(font, width - 16 - font.measureText(capture), 5, capture, 170); + image.drawText(font, right - font.measureText(capture), titleY, capture, 170); if (!this.communicator) { - image.drawText(font, 16, 5 + line * 2, "Glasses not connected.", 190); - image.drawText(font, 16, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(font, left, titleY + line * 2, "Glasses not connected.", 190); + image.drawText(font, left, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } - const gutter = 16; - const columnWidth = Math.floor((width - gutter * 3) / 2); + const columnWidth = Math.max(0, Math.floor((right - left - COLUMN_GAP) / 2)); + const rightColumnX = left + columnWidth + COLUMN_GAP; const stripHeight = 22; - const top = 5 + line + 6; - const bottom = height - 16 - stripHeight - 8; - this.paintArm(image, "L", "Left", gutter, top, columnWidth, bottom, nowMs); - this.paintArm(image, "R", "Right", gutter * 2 + columnWidth, top, columnWidth, bottom, nowMs); + const top = titleY + line + 6; + const bottom = footerY - stripHeight - 8; + this.paintArm(image, "L", "Left", left, top, columnWidth, bottom, nowMs); + this.paintArm(image, "R", "Right", rightColumnX, top, columnWidth, bottom, nowMs); - const stripY = height - 16 - stripHeight - 4; + const stripY = footerY - stripHeight - 4; const stripMax = Math.max(1, ...this.stats.L.buckets, ...this.stats.R.buckets); - drawStrip(image, this.stats.L.buckets, gutter, stripY, columnWidth, stripHeight, stripMax); - drawStrip(image, this.stats.R.buckets, gutter * 2 + columnWidth, stripY, columnWidth, stripHeight, stripMax); + drawStrip(image, this.stats.L.buckets, left, stripY, columnWidth, stripHeight, stripMax); + drawStrip(image, this.stats.R.buckets, rightColumnX, stripY, columnWidth, stripHeight, stripMax); const footer = this.unknownArmPackets > 0 ? `last ${HISTORY_MS / 1000} s unattributed packets ${this.unknownArmPackets}` : `packets per ${BUCKET_MS} ms, last ${HISTORY_MS / 1000} s`; - image.drawText(font, gutter, height - 16, footer, 110); + image.drawText(font, left, footerY, footer, 110); const back = `${GESTURE_DOUBLE_CLICK} back tap reset`; - image.drawText(font, width - gutter - font.measureText(back), height - 16, back, 110); + image.drawText(font, right - font.measureText(back), footerY, back, 110); return image; } diff --git a/app/apps/developer/resource-usage.ts b/app/apps/developer/resource-usage.ts index 70f0c8f8..525880ad 100644 --- a/app/apps/developer/resource-usage.ts +++ b/app/apps/developer/resource-usage.ts @@ -16,6 +16,14 @@ const HISTORY_MS = 60_000; const MAX_SAMPLES = Math.ceil(HISTORY_MS / SAMPLE_INTERVAL_MS) + 2; /** Line brightness per memory series, headline first. */ const MEMORY_SHADES = [240, 155, 80]; +/** The charts' heights where the window has room for them. */ +const MEMORY_CHART_HEIGHT = 70; +const CPU_CHART_HEIGHT = 60; +/** Between the charts: a gap, then the CPU chart's label line. */ +const CHART_GAP = 10; +const CHART_LABEL_HEIGHT = 16; +/** Least gap between the CPU chart and the footer line. */ +const CHART_FOOTER_GAP = 4; type ChartSample = ResourceSample & { cpuPercent: number }; @@ -56,20 +64,26 @@ export class ResourceUsageLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const latest = this.samples[this.samples.length - 1]; + // The title line, the charts and the footer line span the window inside + // the padding suggested for its edges; the rest is placed from the top. + const left = padding.left; + const top = padding.top; + const right = width - padding.right; + const footerY = height - padding.bottom - font.lineHeight; - image.drawText(font, 16, 5, "Resource usage — last minute", 230); + image.drawText(font, left, top, "Resource usage — last minute", 230); if (!latest) { - image.drawText(font, 16, 30, this.sampleError || "Waiting for first sample…", 170); - image.drawText(font, 16, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(font, left, top + 25, this.sampleError || "Waiting for first sample…", 170); + image.drawText(font, left, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } const cpuText = `CPU ${formatPercent(latest.cpuPercent)} ${latest.threads} threads`; - image.drawText(font, width - 16 - font.measureText(cpuText), 5, cpuText, 200); - image.drawText(font, 16, 21, this.sampleError || latest.status, this.sampleError ? 170 : 140); + image.drawText(font, right - font.measureText(cpuText), top, cpuText, 200); + image.drawText(font, left, top + 16, this.sampleError || latest.status, this.sampleError ? 170 : 140); const memorySeries = MEMORY_SERIES.map((label, index) => ({ label, @@ -78,34 +92,44 @@ export class ResourceUsageLayer implements Layer { })); const memoryPeak = Math.max(...this.samples.flatMap((sample) => sample.memoryMb), 64); const memoryScale = roundUp(memoryPeak * 1.08, 64); + const legendY = top + 43; const legendLeft = drawLegend( image, font, - width, - 48, + right, + legendY, memorySeries.map((series) => [`${series.label} ${formatMb(series.value(latest))}`, series.shade] as const), ); const scaleLabel = `Memory 0–${formatMb(memoryScale)}`; const shortScaleLabel = `0–${formatMb(memoryScale)}`; image.drawText( font, - 16, - 48, - 16 + font.measureText(scaleLabel) + 8 <= legendLeft ? scaleLabel : shortScaleLabel, + left, + legendY, + left + font.measureText(scaleLabel) + 8 <= legendLeft ? scaleLabel : shortScaleLabel, 140, ); - drawChart(image, this.samples, latest.atMs, { x: 16, y: 64, width: width - 32, height: 70 }, memoryScale, memorySeries); + // The charts shrink together where the window is too short for both + // above the footer line (the RayNeo iO's). + const chartsTop = legendY + CHART_LABEL_HEIGHT; + const room = footerY - CHART_FOOTER_GAP - chartsTop - CHART_GAP - CHART_LABEL_HEIGHT; + const fit = clamp(room / (MEMORY_CHART_HEIGHT + CPU_CHART_HEIGHT), 0, 1); + const memoryHeight = Math.floor(MEMORY_CHART_HEIGHT * fit); + const chartWidth = Math.max(0, right - left); + drawChart(image, this.samples, latest.atMs, { x: left, y: chartsTop, width: chartWidth, height: memoryHeight }, memoryScale, memorySeries); const cpuPeak = Math.max(...this.samples.map((sample) => sample.cpuPercent), 100); const cpuScale = roundUp(cpuPeak * 1.08, 25); - image.drawText(font, 16, 144, `CPU 0–${Math.round(cpuScale)}% (one core)`, 140); - drawChart(image, this.samples, latest.atMs, { x: 16, y: 160, width: width - 32, height: 60 }, cpuScale, [ - { value: (sample) => sample.cpuPercent, shade: 220 }, - ]); + const cpuLabelY = chartsTop + memoryHeight + CHART_GAP; + image.drawText(font, left, cpuLabelY, `CPU 0–${Math.round(cpuScale)}% (one core)`, 140); + drawChart(image, this.samples, latest.atMs, + { x: left, y: cpuLabelY + CHART_LABEL_HEIGHT, width: chartWidth, height: Math.floor(CPU_CHART_HEIGHT * fit) }, cpuScale, [ + { value: (sample) => sample.cpuPercent, shade: 220 }, + ]); - image.drawText(font, 16, height - 16, `-60s`, 80); + image.drawText(font, left, footerY, `-60s`, 80); const nowLabel = `now`; - image.drawText(font, width - 16 - font.measureText(nowLabel), height - 16, nowLabel, 110); + image.drawText(font, right - font.measureText(nowLabel), footerY, nowLabel, 110); return image; } @@ -148,6 +172,7 @@ function drawChart( scale: number, series: readonly Series[], ): void { + if (bounds.width < 2 || bounds.height < 2) return; image.drawRect(bounds.x, bounds.y, bounds.width, bounds.height, 45); const midY = bounds.y + Math.round((bounds.height - 1) / 2); image.drawLine(bounds.x + 1, midY, bounds.x + bounds.width - 2, midY, 24); @@ -167,16 +192,16 @@ function drawChart( } } -/** Right-aligned legend of line swatches and labels; returns its left edge. */ +/** Legend of line swatches and labels, right-aligned to end at `right`; returns its left edge. */ function drawLegend( image: GrayImage, font: ReturnType, - width: number, + right: number, y: number, entries: ReadonlyArray, ): number { const totalWidth = entries.reduce((sum, [label]) => sum + font.measureText(label) + 14, 0); - const left = width - 16 - totalWidth; + const left = right - totalWidth; let x = left; for (const [label, shade] of entries) { image.drawLine(x, y + 6, x + 8, y + 6, shade); diff --git a/app/apps/developer/ring-status.ts b/app/apps/developer/ring-status.ts index a680a3dc..de86f81a 100644 --- a/app/apps/developer/ring-status.ts +++ b/app/apps/developer/ring-status.ts @@ -18,8 +18,10 @@ import { } from "../../native/ring-status"; const POLL_INTERVAL_MS = 1_000; -const LEFT_X = 16; -const VALUE_X = 130; +/** The title sits this far in from the left edge of the rows and buttons under it. */ +const TITLE_INDENT = 2; +/** A row's value column, from its label's left edge. */ +const VALUE_OFFSET_X = 114; const BUTTON_GAP = 8; type Row = { label: string; value: string; dim?: boolean }; @@ -102,26 +104,31 @@ export class RingStatusLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const lineStep = font.lineHeight + 2; const buttonHeight = font.lineHeight + 8; - const buttonTop = height - buttonHeight - 6; + // The rows and the button strip span the window inside the padding + // suggested for its side edges; the title sits at the top edge's and the + // buttons' outlines at the bottom edge's. + const left = padding.left; + const buttonTop = height - padding.bottom - buttonHeight; - image.drawText(font, 18, 10, "Ring status", 230); - let y = 10 + lineStep + 4; + image.drawText(font, left + TITLE_INDENT, padding.top, "Ring status", 230); + let y = padding.top + lineStep + 4; const rows = this.rows(); if (this.notice) rows.push({ label: "", value: this.notice }); for (const row of rows) { if (y + font.lineHeight > buttonTop - 4) break; - image.drawText(font, LEFT_X, y, row.label, row.dim ? 100 : 150); - image.drawText(font, VALUE_X, y, row.value, row.dim ? 120 : 220); + image.drawText(font, left, y, row.label, row.dim ? 100 : 150); + image.drawText(font, left + VALUE_OFFSET_X, y, row.value, row.dim ? 120 : 220); y += lineStep; } - const buttonWidth = Math.floor((width - 2 * LEFT_X - BUTTON_GAP * (BUTTONS.length - 1)) / BUTTONS.length); + const buttonWidth = Math.max(0, + Math.floor((width - padding.left - padding.right - BUTTON_GAP * (BUTTONS.length - 1)) / BUTTONS.length)); BUTTONS.forEach((button, index) => { - const x = LEFT_X + index * (buttonWidth + BUTTON_GAP); + const x = left + index * (buttonWidth + BUTTON_GAP); const selected = index === this.selected; if (selected) image.fillRoundedRect(x, buttonTop, buttonWidth, buttonHeight, 40, 4); image.drawRoundedRect(x, buttonTop, buttonWidth, buttonHeight, selected ? 200 : 70, 4); diff --git a/app/apps/developer/unicode-test.ts b/app/apps/developer/unicode-test.ts index 200c86d7..1330bfc1 100644 --- a/app/apps/developer/unicode-test.ts +++ b/app/apps/developer/unicode-test.ts @@ -3,7 +3,7 @@ import { fontSelectionLabel, getDefaultSmallFont, getUiFontSelection } from "../ import { GrayImage, type UiFont } from "../../graphics/image"; import { type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; -import { MenuLayer, type MenuLayout } from "../../ui/menu"; +import { MenuLayer, type MenuPageLayout } from "../../ui/menu"; /** * Unicode test: sample texts chosen to exercise text-rendering features that @@ -175,9 +175,6 @@ export const UNICODE_SAMPLES: readonly UnicodeSample[] = [ }, ]; -const MARGIN_X = 18; -const TITLE_Y = 8; -const FOOTER_MARGIN = 20; const BODY_GAP = 6; /** @@ -196,32 +193,36 @@ export class UnicodeSampleLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + // The title and note stay put at the top and the footer line at the + // bottom, inside the padding suggested for the window's edges; the text + // pages between them, so it needs no padding of its own. + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - const textWidth = width - 2 * MARGIN_X; + const left = padding.left; + const textWidth = Math.max(0, width - padding.left - padding.right); - image.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.sample.label, textWidth), 220); - const noteY = TITLE_Y + font.lineHeight; + image.drawText(font, left, padding.top, truncateText(font, this.sample.label, textWidth), 220); + const noteY = padding.top + font.lineHeight; const noteLines = wrapText(font, this.sample.exercises, textWidth); for (let i = 0; i < noteLines.length; i++) { - image.drawText(font, MARGIN_X, noteY + i * font.lineHeight, noteLines[i]!, 120); + image.drawText(font, left, noteY + i * font.lineHeight, noteLines[i]!, 120); } const bodyY = noteY + noteLines.length * font.lineHeight + BODY_GAP; - const footerY = height - FOOTER_MARGIN; + const footerY = height - padding.bottom - font.lineHeight; this.bodyLineCount = Math.max(1, Math.floor((footerY - bodyY) / font.lineHeight)); const lines = this.getLines(font, textWidth); this.clampScroll(lines.length); const visible = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount); for (let i = 0; i < visible.length; i++) { - image.drawText(font, MARGIN_X, bodyY + i * font.lineHeight, visible[i]!, 235); + image.drawText(font, left, bodyY + i * font.lineHeight, visible[i]!, 235); } const page = Math.floor(this.firstLine / this.pageStep()) + 1; - image.drawText(font, MARGIN_X, footerY, `Page ${page}/${this.pageCount(lines.length)}`, 110); + image.drawText(font, left, footerY, `Page ${page}/${this.pageCount(lines.length)}`, 110); const fontLabel = fontSelectionLabel(getUiFontSelection()); - image.drawText(font, width - MARGIN_X - font.measureText(fontLabel), footerY, fontLabel, 110); + image.drawText(font, width - padding.right - font.measureText(fontLabel), footerY, fontLabel, 110); return image; } @@ -271,7 +272,7 @@ export class UnicodeSampleLayer implements Layer { } /** The language list; selecting a row opens its sample. */ -export function unicodeTestMenu(layout: MenuLayout): MenuLayer { +export function unicodeTestMenu(layout: MenuPageLayout): MenuLayer { return new MenuLayer( "Unicode test", UNICODE_SAMPLES.map((sample) => ({ diff --git a/app/apps/evenhub/store-detail-layer.ts b/app/apps/evenhub/store-detail-layer.ts index ba179d86..34b86704 100644 --- a/app/apps/evenhub/store-detail-layer.ts +++ b/app/apps/evenhub/store-detail-layer.ts @@ -16,7 +16,11 @@ import { cleanError, installStoreApp } from "./store-install"; import { describeUpdate } from "./updates"; import { lineStep, listRowHeight } from "../../ui/metrics"; -const X = 18; +/** How far an action row's highlight box overhangs the column of text it belongs to, on each side. */ +const ACTION_BOX_OVERHANG = 5; +/** The side-by-side layout: the action menu's label column, and the gap between the details and its box. */ +const SIDE_MENU_WIDTH = 150; +const SIDE_MENU_GAP = 9; type DetailAction = "about" | "whats-new" | "primary"; const ACTIONS: readonly DetailAction[] = ["about", "whats-new", "primary"]; @@ -60,10 +64,15 @@ export class EvenHubStoreDetailLayer implements Layer { paint(ctx: LayerContext): GrayImage { if (!this.detailPromise) void this.ensureDetail(ctx); const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const step = lineStep(font); const actionRowH = listRowHeight(font) + 4; + // The action rows' highlight boxes are the outermost thing drawn toward + // the edges they reach, so they sit at the padding suggested for those + // edges; the details keep to the text column inside the boxes' overhang. + const textX = padding.left + ACTION_BOX_OVERHANG; + const fullTextWidth = Math.max(0, width - padding.left - padding.right - 2 * ACTION_BOX_OVERHANG); const installed = getInstalledEvenHubApp(this.app.packageId); this.primaryLabel = this.working ? "Installing..." : this.primaryActionLabel(installed); @@ -109,26 +118,22 @@ export class EvenHubStoreDetailLayer implements Layer { // Stacked layout (details full-width, actions at the bottom) when it // fits; otherwise the actions become a narrow top-right menu and the // details flow down a left column beside it. - const stackedActionTop = height - 6 - ACTIONS.length * actionRowH; - const stacked = 8 + linesHeight(buildLines(width - X * 2)) + 6 <= stackedActionTop; - const menuW = 150; - const menuX = width - menuW - 12; - const textWidth = stacked ? width - X * 2 : menuX - X - 14; + const stackedActionTop = height - padding.bottom - ACTIONS.length * actionRowH; + const stacked = padding.top + linesHeight(buildLines(fullTextWidth)) + 6 <= stackedActionTop; + const sideBoxWidth = SIDE_MENU_WIDTH + 2 * ACTION_BOX_OVERHANG; + const sideBoxX = width - padding.right - sideBoxWidth; + const textWidth = stacked ? fullTextWidth : Math.max(0, sideBoxX - SIDE_MENU_GAP - textX); - let y = 8; + let y = padding.top; for (const line of buildLines(textWidth)) { - image.drawText(font, X, y, line.text, line.value); + image.drawText(font, textX, y, line.text, line.value); y += step + line.gapAfter; } - const actionX = stacked ? X : menuX; - const actionW = stacked ? textWidth : menuW; - const actionTop = stacked ? stackedActionTop : 8; - this.actions.paint( - image, - { x: actionX - 5, y: actionTop, width: actionW + 10, height: ACTIONS.length * actionRowH - 1 }, - ctx.stack.isFocused(), - ); + const actionBox = stacked + ? { x: padding.left, y: stackedActionTop, width: fullTextWidth + 2 * ACTION_BOX_OVERHANG } + : { x: sideBoxX, y: padding.top, width: sideBoxWidth }; + this.actions.paint(image, { ...actionBox, height: ACTIONS.length * actionRowH - 1 }, ctx.stack.isFocused()); return image; } diff --git a/app/apps/evenhub/store-layer.ts b/app/apps/evenhub/store-layer.ts index 8df5be91..994f7979 100644 --- a/app/apps/evenhub/store-layer.ts +++ b/app/apps/evenhub/store-layer.ts @@ -14,15 +14,37 @@ import { EvenHubStoreDetailLayer, type EvenHubStoreDetailOptions } from "./store import { cleanError, installStoreApp } from "./store-install"; import { checkForEvenHubUpdates, describeUpdate, type EvenHubAppUpdate } from "./updates"; import { lineStep } from "../../ui/metrics"; +import type { Viewport } from "../../ui/viewport"; import { isEvenHubStoreAvailable } from "./glasses-compat"; -const LIST_X = 18; +/** Horizontal inset of a row's text from its highlight box; the header's text lines up with it. */ +const ROW_TEXT_INSET = 6; /** Horizontal gap between tab labels. */ const TAB_GAP = 18; +/** How far a focused tab's highlight box reaches past its label, sideways and above/below. */ +const TAB_HIGHLIGHT_PAD_X = 6; +const TAB_HIGHLIGHT_PAD_Y = 3; +/** Room kept right of the rows for the scrollbar, and the bar's own width. */ +const SCROLLBAR_GUTTER = 6; +const SCROLLBAR_WIDTH = 3; -/** Header: title + tabs line, then the status/subtitle line, then a small gap. */ -function headerHeight(font: UiFont): number { - return 8 + lineStep(font) + font.lineHeight + 6; +/** + * Where the header sits in a viewport. Its text shares a column with the + * rows' text, whose highlight boxes start at the left edge's padding. The + * outermost thing it draws toward the top is a focused tab's highlight box, + * so that box sits at the top edge's padding and the title line follows + * from it. `bottom` is where the header ends: title + tabs line, then the + * status/subtitle line, then a small gap. + */ +function headerLayout(viewport: Viewport, font: UiFont): { textX: number; textWidth: number; titleY: number; bottom: number } { + const { width, padding } = viewport; + const titleY = padding.top + TAB_HIGHLIGHT_PAD_Y; + return { + textX: padding.left + ROW_TEXT_INSET, + textWidth: Math.max(0, width - padding.left - padding.right - 2 * ROW_TEXT_INSET), + titleY, + bottom: titleY + lineStep(font) + font.lineHeight + 6, + }; } /** Row pitch of the two-line app list (title + detail), including the 2px gap between highlights. */ @@ -119,8 +141,8 @@ function emptyTab(): TabState { draw: ({ image, item, x, y, width }) => { const font = getDefaultSmallFont(); const { title, detail, titleValue } = rowText(item); - const textX = x + 6; - const textW = width - 12; + const textX = x + ROW_TEXT_INSET; + const textW = width - 2 * ROW_TEXT_INSET; image.drawText(font, textX, y + 2, truncateText(font, title, textW), titleValue); image.drawText(font, textX, y + 2 + lineStep(font), truncateText(font, detail, textW), 115); }, @@ -198,83 +220,93 @@ export class EvenHubStoreLayer implements Layer { } const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const viewport = ctx.stack.getBaseSize(); + const { width, height, padding } = viewport; const image = new GrayImage(width, height, 0); - const headerH = headerHeight(font); - image.drawText(font, LIST_X, 8, "EvenHub", 220); + const { textX, textWidth, titleY, bottom: headerBottom } = headerLayout(viewport, font); + image.drawText(font, textX, titleY, "EvenHub", 220); if (this.showingLogin) { - image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, this.loginStatus, width - LIST_X * 2), 125); + image.drawText(font, textX, titleY + lineStep(font), truncateText(font, this.loginStatus, textWidth), 125); const message = this.loginBusy ? "Signing in..." : "Enter your Even account email and password in the phone app."; - for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) { - image.drawText(font, LIST_X, headerH + 16 + index * lineStep(font), line, 190); + for (const [index, line] of wrapText(font, message, textWidth).entries()) { + image.drawText(font, textX, headerBottom + 16 + index * lineStep(font), line, 190); } - image.drawText(font, LIST_X, height - font.lineHeight - 4, `${GESTURE_CLICK} log in`, 105); + image.drawText(font, textX, height - padding.bottom - font.lineHeight, `${GESTURE_CLICK} log in`, 105); return image; } - this.paintTabs(image, font, ctx); + this.paintTabs(image, font, ctx, textX, titleY); const tab = this.tabs[this.activeTab]; const subtitle = tab.status || this.subtitle(this.activeTab, tab); - image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, subtitle, width - LIST_X * 2), 125); + image.drawText(font, textX, titleY + lineStep(font), truncateText(font, subtitle, textWidth), 125); if (tab.rows.length === 0) { const message = tab.loading ? "Loading..." : this.emptyMessage(this.activeTab, tab); - for (const [index, line] of wrapText(font, message, width - LIST_X * 2).slice(0, 3).entries()) { - image.drawText(font, LIST_X, headerH + 22 + index * lineStep(font), line, 190); + for (const [index, line] of wrapText(font, message, textWidth).slice(0, 3).entries()) { + image.drawText(font, textX, headerBottom + 22 + index * lineStep(font), line, 190); } return image; } + if (height <= headerBottom) return image; - const rowH = storeRowHeight(font); - const visibleRows = Math.max(1, Math.floor((height - headerH - 4) / rowH)); + // The header stays put, so the list starts right under it with no + // padding of its own there. It scrolls, so it runs to the window's + // bottom edge, the padding suggested for that edge inside it: the last + // row keeps clear of a border there, and a row cut off for scrolling + // meets the edge rather than a blank strip. The scrollbar keeps inside + // the padding, in a gutter the rows leave free. const listFocused = ctx.stack.isFocused() && this.focus === "list"; const menu = tabMenu(tab); + const rowsWidth = Math.max(0, width - padding.left - padding.right - SCROLLBAR_GUTTER); menu.paint( image, - { x: LIST_X - 6, y: headerH, width: width - LIST_X * 2 + 12, height: visibleRows * rowH - 2 }, + { x: padding.left, y: headerBottom, width: rowsWidth, height: height - headerBottom, padTop: 0, padBottom: padding.bottom }, listFocused, ); - menu.drawScrollbar(image, width - 5, headerH, visibleRows * rowH - 3); + menu.drawScrollbar(image, width - padding.right - SCROLLBAR_WIDTH, headerBottom, + Math.max(0, height - padding.bottom - headerBottom)); return image; } /** The notice that replaces the store when the paired glasses can't use it. */ private paintUnavailable(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); - const image = new GrayImage(width, height, 0); - image.drawText(font, LIST_X, 8, "EvenHub", 220); + const viewport = ctx.stack.getBaseSize(); + const image = new GrayImage(viewport.width, viewport.height, 0); + const { textX, textWidth, titleY, bottom: headerBottom } = headerLayout(viewport, font); + image.drawText(font, textX, titleY, "EvenHub", 220); if (this.unavailablePackageName) { const status = `${this.unavailablePackageName}'s package is missing.`; - image.drawText(font, LIST_X, 8 + lineStep(font), truncateText(font, status, width - LIST_X * 2), 125); + image.drawText(font, textX, titleY + lineStep(font), truncateText(font, status, textWidth), 125); } const message = "Using the Even Realities app store requires Even Realities glasses."; - for (const [index, line] of wrapText(font, message, width - LIST_X * 2).entries()) { - image.drawText(font, LIST_X, headerHeight(font) + 16 + index * lineStep(font), line, 190); + for (const [index, line] of wrapText(font, message, textWidth).entries()) { + image.drawText(font, textX, headerBottom + 16 + index * lineStep(font), line, 190); } return image; } - /** Tab labels to the right of the title; the active one is bright and underlined. */ - private paintTabs(image: GrayImage, font: UiFont, ctx: LayerContext): void { + /** Tab labels to the right of the title (at titleX, titleY); the active one is bright and underlined. */ + private paintTabs(image: GrayImage, font: UiFont, ctx: LayerContext, titleX: number, titleY: number): void { const tabsFocused = ctx.stack.isFocused() && this.focus === "tabs"; - let x = LIST_X + font.measureText("EvenHub") + 28; + let x = titleX + font.measureText("EvenHub") + 28; for (const tab of TABS) { const label = this.tabLabel(tab.id); const w = font.measureText(label); const active = tab.id === this.activeTab; if (active) { if (tabsFocused) { - drawSelectionHighlight(image, x - 6, 5, w + 12, font.lineHeight + 6, true, 4); + drawSelectionHighlight(image, x - TAB_HIGHLIGHT_PAD_X, titleY - TAB_HIGHLIGHT_PAD_Y, + w + 2 * TAB_HIGHLIGHT_PAD_X, font.lineHeight + 2 * TAB_HIGHLIGHT_PAD_Y, true, 4); } else { - image.fillRect(x, 8 + font.lineHeight + 1, w, 1, 150); + image.fillRect(x, titleY + font.lineHeight + 1, w, 1, 150); } } - image.drawText(font, x, 8, label, active ? 235 : 120); + image.drawText(font, x, titleY, label, active ? 235 : 120); x += w + TAB_GAP; } } diff --git a/app/apps/files/file-browser.ts b/app/apps/files/file-browser.ts index a9470f4d..4657d06c 100644 --- a/app/apps/files/file-browser.ts +++ b/app/apps/files/file-browser.ts @@ -19,13 +19,21 @@ import { import { directionalFallback, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; -const LIST_X = 20; /** List view: the selection box extends this far left and right of the row text. */ const LIST_HIGHLIGHT_PAD = 6; +/** Header text: how far in from the rows' sides, which lines it up with list-row text. */ +const HEADER_INSET_X = LIST_HIGHLIGHT_PAD; +/** Between the header's title and the path after it. */ +const HEADER_PATH_GAP = 14; +/** Under the header's line, before the rows. */ +const HEADER_GAP = 2; -/** Header: one line holding both the title and the current path. */ -function headerHeight(font: UiFont): number { - return 10 + font.lineHeight; +/** + * Bottom of the header: one line holding both the title and the current + * path, below the padding suggested for the window's top edge. + */ +function headerBottom(font: UiFont, paddingTop: number): number { + return paddingTop + font.lineHeight + HEADER_GAP; } export type FilesViewMode = "icons" | "list"; @@ -147,35 +155,37 @@ export class FileBrowserLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); + // The rows (grid bands, list selection boxes) span the window inside the + // padding suggested for its sides. + const rowsX = padding.left; + const rowsWidth = Math.max(0, width - padding.left - padding.right); - image.drawText(font, 20, 8, "Files", 220); + const titleX = rowsX + HEADER_INSET_X; + image.drawText(font, titleX, padding.top, "Files", 220); const pathLabel = this.location === null ? "Places" : this.location; - const pathX = 20 + font.measureText("Files") + 14; - image.drawText(font, pathX, 8, truncateLeft(font, pathLabel, width - pathX - 20), 130); + const pathX = titleX + font.measureText("Files") + HEADER_PATH_GAP; + image.drawText(font, pathX, padding.top, truncateLeft(font, pathLabel, rowsX + rowsWidth - HEADER_INSET_X - pathX), 130); - if (filesViewModeSetting.get() === "list") { - this.paintList(image, font, ctx); + // The header stays put and the rows scroll under it, as far as the + // window's bottom edge: the padding suggested for that edge goes inside + // the scrolled content, so the last row keeps clear of a border there + // and a row cut off for scrolling meets it rather than a blank strip. + const listView = filesViewModeSetting.get() === "list"; + const headerEnd = headerBottom(font, padding.top); + // List row boxes start one pixel above the header's bottom edge. + const top = listView ? headerEnd - 1 : headerEnd; + const box = { x: rowsX, y: top, width: rowsWidth, height: Math.max(0, height - top), padTop: 0, padBottom: padding.bottom }; + if (listView) { + this.listMenu.setItems(this.flatRows()); + this.listMenu.paint(image, box, ctx.stack.isFocused()); } else { - const headerH = headerHeight(font); - this.grid.paint(image, { x: 0, y: headerH, width, height: height - headerH - 6 }, ctx.stack.isFocused()); + this.grid.paint(image, box, ctx.stack.isFocused()); } return image; } - private paintList(image: GrayImage, font: UiFont, ctx: LayerContext): void { - const { width, height } = ctx.stack.getBaseSize(); - const headerH = headerHeight(font); - this.listMenu.setItems(this.flatRows()); - // Row boxes start one pixel above the header's bottom edge. - this.listMenu.paint( - image, - { x: LIST_X - LIST_HIGHLIGHT_PAD, y: headerH - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: height - headerH - 4 }, - ctx.stack.isFocused(), - ); - } - private drawListRow({ image, item, x, y, width, height, selected }: MenuDrawArgs): void { const font = getDefaultSmallFont(); const label = truncateText(font, item.label, width - 2 * LIST_HIGHLIGHT_PAD); diff --git a/app/apps/files/font-previewer.ts b/app/apps/files/font-previewer.ts index a486497a..6af93ee9 100644 --- a/app/apps/files/font-previewer.ts +++ b/app/apps/files/font-previewer.ts @@ -5,10 +5,8 @@ import { canLoadFontFile, fontFileDisplayName, fontFileMetrics, renderFontFileTe import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, gestureHints, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; -const MARGIN_X = 18; -const TITLE_Y = 8; -const BODY_TOP = 26; -const FOOTER_MARGIN = 20; +/** From the title's top to the first sample's. */ +const TITLE_BAND = 18; const SAMPLE_GAP = 4; const SIZES = [8,9,10,11, 12, 13, 14, 16, 18, 20, 24, 28, 32, 40, 48] as const; @@ -48,36 +46,41 @@ export class FontPreviewerLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const out = new GrayImage(width, height, 0); const size = SIZES[this.sizeIndex]!; const gamma = GAMMAS[this.gammaIndex]!; + // Everything keeps inside the padding suggested for the window's edges: + // the title line at the top, the samples across the width, the hint + // line at the bottom. + const left = padding.left; + const bodyWidth = Math.max(0, width - padding.left - padding.right); + const bodyTop = padding.top + TITLE_BAND; const status = `${size}px γ ${gamma.toFixed(1)}`; const statusWidth = font.measureText(status); - const title = truncateText(font, this.getDisplayName(), width - 2 * MARGIN_X - statusWidth - 12); - out.drawText(font, MARGIN_X, TITLE_Y, title, 220); - out.drawText(font, width - MARGIN_X - statusWidth, TITLE_Y, status, 150); + const title = truncateText(font, this.getDisplayName(), bodyWidth - statusWidth - 12); + out.drawText(font, left, padding.top, title, 220); + out.drawText(font, left + bodyWidth - statusWidth, padding.top, status, 150); if (this.loadable === null) { this.loadable = canLoadFontFile(this.path); } - const footerY = height - FOOTER_MARGIN; + const footerY = height - padding.bottom - font.lineHeight; if (!this.loadable) { - out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load font)", 160); - out.drawText(font, MARGIN_X, footerY, gestureHints([[GESTURE_DOUBLE_CLICK, "back"]]), 110); + out.drawText(font, left, bodyTop + 16, "(could not load font)", 160); + out.drawText(font, left, footerY, gestureHints([[GESTURE_DOUBLE_CLICK, "back"]]), 110); return out; } - const bodyWidth = width - 2 * MARGIN_X; - let y = BODY_TOP; + let y = bodyTop; for (const [id, text] of [ ["pangram", PANGRAM], ["charset", CHARSET], ] as const) { const line = this.getCached(id, () => renderFontFileText(this.path, text, size, gamma)); if (!line) continue; - out.bitBlt(line, MARGIN_X, y, { width: bodyWidth }); + out.bitBlt(line, left, y, { width: bodyWidth }); y += line.height + SAMPLE_GAP; } @@ -97,13 +100,13 @@ export class FontPreviewerLayer implements Layer { }), ); if (paragraph) { - out.bitBlt(paragraph, MARGIN_X, y + SAMPLE_GAP); + out.bitBlt(paragraph, left, y + SAMPLE_GAP); } } out.drawText( font, - MARGIN_X, + left, footerY, gestureHints([ [GESTURE_SCROLL, "size"], diff --git a/app/apps/files/image-viewer.ts b/app/apps/files/image-viewer.ts index a6bf3456..f500026f 100644 --- a/app/apps/files/image-viewer.ts +++ b/app/apps/files/image-viewer.ts @@ -5,9 +5,9 @@ import { loadImageFileAsGray, PHOTO_TONE } from "../../native/image-files"; import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; -const MARGIN_X = 18; -const TITLE_Y = 8; -const BODY_TOP = 24; +/** From the title's top to the image area's. */ +const TITLE_BAND = 16; +/** How far the image area keeps from the window's sides and bottom. */ const BODY_MARGIN = 2; /** @@ -19,7 +19,8 @@ const BODY_MARGIN = 2; */ export class ImageViewerLayer implements Layer { private image: GrayImage | null = null; - private loadAttempted = false; + /** The image area ("WxH") the file was last decoded to fit; null before the first load. */ + private fittedTo: string | null = null; constructor( private readonly path: string, @@ -28,24 +29,31 @@ export class ImageViewerLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const out = new GrayImage(width, height, 0); - out.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X), 220); + // The title line keeps inside the padding suggested for the window's + // edges; the image fills the rest of the window whatever its edges touch. + out.drawText(font, padding.left, padding.top, truncateText(font, this.title, width - padding.left - padding.right), 220); - const bodyHeight = height - BODY_TOP - BODY_MARGIN; - if (!this.loadAttempted) { - this.loadAttempted = true; - this.image = loadImageFileAsGray(this.path, width - 2 * BODY_MARGIN, bodyHeight, PHOTO_TONE); + const bodyTop = padding.top + TITLE_BAND; + const bodyWidth = width - 2 * BODY_MARGIN; + const bodyHeight = height - bodyTop - BODY_MARGIN; + // The area moves with the window's size and its top padding, so the + // file is decoded again whenever it changes rather than once. + const fit = `${bodyWidth}x${bodyHeight}`; + if (this.fittedTo !== fit) { + this.fittedTo = fit; + this.image = loadImageFileAsGray(this.path, bodyWidth, bodyHeight, PHOTO_TONE); } if (!this.image) { - out.drawText(font, MARGIN_X, BODY_TOP + 16, "(could not load image)", 160); - out.drawText(font, MARGIN_X, height - 16, `${GESTURE_DOUBLE_CLICK} back`, 110); + out.drawText(font, padding.left, bodyTop + 16, "(could not load image)", 160); + out.drawText(font, padding.left, height - padding.bottom - font.lineHeight, `${GESTURE_DOUBLE_CLICK} back`, 110); return out; } out.bitBlt( this.image, Math.round((width - this.image.width) / 2), - BODY_TOP + Math.round((bodyHeight - this.image.height) / 2), + bodyTop + Math.round((bodyHeight - this.image.height) / 2), ); return out; } diff --git a/app/apps/files/text-viewer.ts b/app/apps/files/text-viewer.ts index 323cd138..906a87d9 100644 --- a/app/apps/files/text-viewer.ts +++ b/app/apps/files/text-viewer.ts @@ -5,11 +5,9 @@ import { GrayImage, type UiFont } from "../../graphics/image"; import { type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; -const MARGIN_X = 18; -const TITLE_Y = 16; -const BODY_X = 18; -const BODY_Y = 44; -const FOOTER_MARGIN = 36; +/** The title: how far right of the body's column, and from its top to the first body line's. */ +const TITLE_INSET_X = 4; +const TITLE_BAND = 28; /** Line step for a bitmap face (the 12px font plus leading, as before). */ const BDF_LINE_STEP = 14; @@ -24,8 +22,11 @@ function lineStepOf(font: UiFont): number { } /** - * Paged text viewer for a document. Sized to its hosting stack (a Files - * document window, or pushed over the file browser for view-in-place). + * Paged text viewer for a document: a title line, the page's lines, and a + * page-number line, all inside the padding suggested for the window's edges + * (the title and page number stay put, so the lines page between them). + * Sized to its hosting stack (a Files document window, or pushed over the + * file browser for view-in-place). */ export class TextViewerLayer implements Layer { private lines: string[] | null = null; @@ -42,20 +43,23 @@ export class TextViewerLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getViewerFont(); const lineStep = lineStepOf(font); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - const footerY = height - FOOTER_MARGIN; - this.bodyLineCount = Math.max(1, Math.floor((footerY - BODY_Y) / lineStep)); - image.drawText(font, MARGIN_X + 4, TITLE_Y, truncateText(font, this.title, width - 2 * MARGIN_X - 8), 220); + const bodyX = padding.left; + const bodyWidth = Math.max(0, width - padding.left - padding.right); + const bodyY = padding.top + TITLE_BAND; + const footerY = height - padding.bottom - font.lineHeight; + this.bodyLineCount = Math.max(1, Math.floor((footerY - bodyY) / lineStep)); + image.drawText(font, bodyX + TITLE_INSET_X, padding.top, truncateText(font, this.title, bodyWidth - 2 * TITLE_INSET_X), 220); - const lines = this.getLines(font, width); + const lines = this.getLines(font, bodyWidth); const visibleLines = lines.slice(this.firstLine, this.firstLine + this.bodyLineCount); for (let index = 0; index < visibleLines.length; index++) { - image.drawText(font, BODY_X, BODY_Y + index * lineStep, visibleLines[index]!, 230); + image.drawText(font, bodyX, bodyY + index * lineStep, visibleLines[index]!, 230); } const currentPage = Math.floor(this.firstLine / this.pageStep()) + 1; - image.drawText(font, BODY_X, footerY, `Page ${currentPage}/${this.totalPageCount(lines.length)}`, 110); + image.drawText(font, bodyX, footerY, `Page ${currentPage}/${this.totalPageCount(lines.length)}`, 110); return image; } @@ -89,14 +93,15 @@ export class TextViewerLayer implements Layer { this.firstLine = Math.max(0, Math.min(maxFirstLine, this.firstLine + delta)); } - private getLines(font: UiFont, width: number): string[] { - if (this.lines === null || this.wrappedForWidth !== width || this.wrappedWithFont !== font.fingerprintId) { + /** The document wrapped to the body column's width; cached per width and font. */ + private getLines(font: UiFont, bodyWidth: number): string[] { + if (this.lines === null || this.wrappedForWidth !== bodyWidth || this.wrappedWithFont !== font.fingerprintId) { const normalized = this.documentText.replace(/\t/g, " ").replace(/\r/g, ""); - this.lines = wrapText(font, normalized, width - BODY_X - 12, { + this.lines = wrapText(font, normalized, bodyWidth, { preserveLeadingWhitespace: true, breakLongWords: true, }); - this.wrappedForWidth = width; + this.wrappedForWidth = bodyWidth; this.wrappedWithFont = font.fingerprintId; } return this.lines; diff --git a/app/apps/files/video-window.ts b/app/apps/files/video-window.ts index 2310e117..7b1d8d09 100644 --- a/app/apps/files/video-window.ts +++ b/app/apps/files/video-window.ts @@ -53,9 +53,11 @@ export const videoMonochromeSetting = new ConfigSettingBoolean({ /** How far one ring scroll seeks. */ const SEEK_STEP_MS = 10_000; -const MARGIN_X = 18; -const TITLE_Y = 10; +/** The settings panel's rows: at most this wide, their text this far inside their selection box. */ const PANEL_WIDTH = 340; +const PANEL_ROW_TEXT_INSET = 10; +/** The panel's title: how far below the window's top padding. */ +const TITLE_OFFSET_Y = 2; /** * One opened video: its probe result, the native player, and the window @@ -202,7 +204,9 @@ type PanelRow = { kind: "size" } | { kind: "audio" } | { kind: "monochrome" } | /** * The pre-play settings panel (the window's root): picture size, audio and * monochrome, all remembered for the next video, then Play (or Resume plus - * Play from start once a position is kept). + * Play from start once a position is kept). The rows' selection boxes start + * at the padding suggested for the window's left edge, and the title and + * the lines under it line up with the rows' text. */ class VideoSettingsLayer implements Layer { private readonly menu = new Menu({ @@ -224,11 +228,16 @@ class VideoSettingsLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - image.drawText(font, MARGIN_X, TITLE_Y, truncateText(font, this.session.title, width - 2 * MARGIN_X), 220); + const rowsX = padding.left; + const available = Math.max(0, width - padding.left - padding.right); + const textX = rowsX + PANEL_ROW_TEXT_INSET; + const textWidth = available - 2 * PANEL_ROW_TEXT_INSET; + const titleY = padding.top + TITLE_OFFSET_Y; + image.drawText(font, textX, titleY, truncateText(font, this.session.title, textWidth), 220); const step = lineStep(font); - let y = TITLE_Y + step; + let y = titleY + step; const info = this.session.info; const errors: string[] = []; @@ -236,12 +245,12 @@ class VideoSettingsLayer implements Layer { if (this.session.error) errors.push(`Playback stopped: ${this.session.error}`); if (info) { const facts = [`${info.width}x${info.height}`, formatMediaTime(info.durationMs), info.hasAudio ? "sound" : "no sound"]; - image.drawText(font, MARGIN_X, y, facts.join(" · "), 130); + image.drawText(font, textX, y, facts.join(" · "), 130); y += step; } for (const error of errors) { - for (const line of wrapText(font, error, width - 2 * MARGIN_X, { breakLongWords: true }).slice(0, 3)) { - image.drawText(font, MARGIN_X, y, line, 200); + for (const line of wrapText(font, error, textWidth, { breakLongWords: true }).slice(0, 3)) { + image.drawText(font, textX, y, line, 200); y += step; } } @@ -253,9 +262,10 @@ class VideoSettingsLayer implements Layer { // Land on Resume/Play by default: the settings usually stay as they were. const playIndex = rows.findIndex((row) => row.kind === "play"); this.menu.setItems(rows, index >= 0 ? index : playIndex >= 0 ? playIndex : undefined); - const panelWidth = Math.min(PANEL_WIDTH, width - 2 * (MARGIN_X - 6)); - this.menu.paint(image, { x: MARGIN_X - 6, y, width: panelWidth, height: Math.max(0, height - y - 4) }, - ctx.stack.isFocused()); + // The rows scroll under the fixed lines above, as far as the window's + // bottom edge, with the padding suggested for that edge inside the list. + this.menu.paint(image, { x: rowsX, y, width: Math.min(PANEL_WIDTH, available), height: Math.max(0, height - y), + padTop: 0, padBottom: padding.bottom }, ctx.stack.isFocused()); return image; } @@ -294,8 +304,8 @@ class VideoSettingsLayer implements Layer { private drawRow({ image, item, x, y, width, selected }: MenuDrawArgs): void { const font = getDefaultSmallFont(); - const textX = x + 10; - const textWidth = width - 20; + const textX = x + PANEL_ROW_TEXT_INSET; + const textWidth = width - 2 * PANEL_ROW_TEXT_INSET; switch (item.kind) { case "size": { const size = videoSizeSetting.get(); diff --git a/app/apps/flappy/flappy-app.worker.ts b/app/apps/flappy/flappy-app.worker.ts index 27fae1c7..5f95d777 100644 --- a/app/apps/flappy/flappy-app.worker.ts +++ b/app/apps/flappy/flappy-app.worker.ts @@ -41,6 +41,7 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; import { type MenuItem } from "../../ui/menu"; import { WindowMenu } from "../../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; +import type { Viewport } from "../../ui/viewport"; import { directionalFallback, GESTURE_CLICK, @@ -95,6 +96,12 @@ const RENDER_TICK_MS = 30; /** Physics steps per wing frame while flying. */ const WING_STEPS_PER_FRAME = 6; +// --- HUD (pixels below the top line's y) -------------------------------------- +/** The title: a shorter font than the score's, centered on the same line. */ +const HUD_TITLE_DROP = 4; +/** The best score, under the title. */ +const HUD_BEST_DROP = 34; + const SFX_FLAP: Step[] = [{ freq: 880, duty: 40, ms: 20 }, { freq: 1175, duty: 40, ms: 25 }]; const SFX_SCORE: Step[] = [{ freq: 1568, duty: 40, ms: 40 }, { freq: 2093, duty: 40, ms: 60 }]; const SFX_HIT: Step[] = [{ freq: 220, duty: 55, ms: 80 }, { freq: 165, duty: 55, ms: 120 }]; @@ -135,6 +142,13 @@ type FlappyWindow = { title: string; viewportWidth: number; viewportHeight: number; + /** + * The viewport as the shell last sent it: besides the size above, what each + * edge touches and the padding suggested there. The playfield fills the + * window whatever its edges touch; only the HUD's top line keeps the + * padding (see paintHud). + */ + viewport: Viewport; foreground: boolean; /** Whether this window is the shell's input target (pushed with each message). */ focused: boolean; @@ -198,6 +212,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { title: message.title, viewportWidth: message.viewport.width, viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, @@ -225,7 +240,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { if (!window) break; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; - window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight }); + window.viewport = message.viewport; + window.menu?.resize(message.viewport); if (window.foreground) renderAndSubmit(window, 0); break; } @@ -372,7 +388,7 @@ function windowMenu(window: FlappyWindow): WindowMenu { post, title: () => window.title, items: () => windowMenuItems(window), - size: { width: window.viewportWidth, height: window.viewportHeight }, + size: window.viewport, paintBase: () => paintContent(window), isFocused: () => window.focused, }); @@ -712,14 +728,18 @@ function birdWingFrame(window: FlappyWindow): number { function paintHud(image: GrayImage, window: FlappyWindow): void { const width = window.viewportWidth; + // The top line (the score, or the title before the first flap) is the + // only thing here placed from a window edge: it keeps the padding + // suggested for the top, and the best score hangs under it. + const top = window.viewport.padding.top; if (window.phase !== "ready") { - drawCenteredIn(image, largeFont, 0, width, 10, String(window.score), 245); + drawCenteredIn(image, largeFont, 0, width, top, String(window.score), 245); } switch (window.phase) { case "ready": - drawCenteredIn(image, mediumFont, 0, width, 14, "FLAPPY", 245); + drawCenteredIn(image, mediumFont, 0, width, top + HUD_TITLE_DROP, "FLAPPY", 245); if (window.highScore > 0) { - drawCenteredIn(image, smallFont, 0, width, 44, `best ${window.highScore}`, 150); + drawCenteredIn(image, smallFont, 0, width, top + HUD_BEST_DROP, `best ${window.highScore}`, 150); } drawCenteredIn(image, smallFont, 0, width, groundTop(window) - 26, "Touch ring / swipe to flap", 150); break; diff --git a/app/apps/freecell/freecell-app.worker.ts b/app/apps/freecell/freecell-app.worker.ts index bd611a44..1bdee5a8 100644 --- a/app/apps/freecell/freecell-app.worker.ts +++ b/app/apps/freecell/freecell-app.worker.ts @@ -33,6 +33,7 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; import { WindowMenu } from "../../ui/window-menu"; import type { MenuItem } from "../../ui/menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; +import type { Viewport } from "../../ui/viewport"; import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures"; declare const global: any; @@ -61,8 +62,16 @@ function isRed(card: number): boolean { const CARD_W = 60; const CARD_H = 36; -const TOP_Y = 4; -const CASCADE_Y = 48; +/** How far outside the card or cascade it marks the cursor's outline is drawn. */ +const CURSOR_OUTSET = 3; +/** Gap between the top row (free cells and foundations) and the cascades. */ +const ROW_GAP = 8; +/** The most of a buried cascade card left showing: enough for its label. */ +const MAX_STRIP = 16; +/** Least column pitch that keeps the cursor's outline round one column off the next column's cards. */ +const MIN_PITCH = CARD_W + CURSOR_OUTSET; +/** Height of the longest cascade of a fresh deal (7 cards) fanned at MAX_STRIP. */ +const DEALT_CASCADE_H = 6 * MAX_STRIP + CARD_H; /** * Cursor locations, in scroll order: free cells 0-3, foundations 0-3 (the @@ -106,6 +115,12 @@ type FreecellWindow = { title: string; viewportWidth: number; viewportHeight: number; + /** + * The viewport as the shell last sent it: besides the size above, what each + * edge touches and the padding suggested there, which the board is laid + * out inside (see layoutFor). + */ + viewport: Viewport; foreground: boolean; /** Whether this window is the shell's input target (pushed with each message). */ focused: boolean; @@ -155,6 +170,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { title: message.title, viewportWidth: message.viewport.width, viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, @@ -210,7 +226,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { if (!window) break; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; - window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight }); + window.viewport = message.viewport; + window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; if (window.foreground) renderAndSubmit(window, 0); break; @@ -286,7 +303,7 @@ function windowMenu(window: FreecellWindow): WindowMenu { post, title: () => window.title, items: () => windowMenuItems(window), - size: { width: window.viewportWidth, height: window.viewportHeight }, + size: window.viewport, paintBase: () => paintContent(window), isFocused: () => window.focused, }); @@ -706,21 +723,67 @@ function checkWin(window: FreecellWindow): void { // Painting type Layout = { + /** Distance between the left edges of neighboring columns. */ pitch: number; + /** Left edge of the first column's cards. */ marginX: number; + /** Top of the free cells and foundations. */ + topY: number; + /** Top of the cascades. */ + cascadeY: number; /** Vertical pixels per buried cascade card (its visible label strip). */ strip: (cascadeLength: number) => number; }; +/** + * The eight columns across a window of the given width, keeping the given + * padding at its left and right edges: spread as far apart as a whole-pixel + * pitch allows, with what is left over split between the two sides. + */ +function columnLayout(width: number, padLeft: number, padRight: number): Pick { + const left = padLeft + CURSOR_OUTSET; + const slack = width - padRight - CURSOR_OUTSET - left - CARD_W; + const pitch = Math.floor(slack / 7); + return { pitch, marginX: left + Math.floor((slack - pitch * 7) / 2) }; +} + +/** + * The two rows down a window of the given height, keeping the given padding + * at its top and bottom edges, and the height that leaves a cascade to fan + * out in. + */ +function rowLayout( + height: number, + padTop: number, + padBottom: number, +): Pick & { cascadeH: number } { + const topY = padTop + CURSOR_OUTSET; + const cascadeY = topY + CARD_H + ROW_GAP; + return { topY, cascadeY, cascadeH: height - padBottom - CURSOR_OUTSET - cascadeY }; +} + +/** + * Lay the board out in the window. The cursor's outline is the outermost + * thing drawn toward every edge (CURSOR_OUTSET outside whatever it marks), so + * it is what sits at the padding suggested for the edge; the columns spread + * across the width inside it, and the cascades fan down the height. A + * window too small for that (the RayNeo iO's) is short of room for the cards + * as it is, so along the axis where they would crowd, they take the + * padding's room too. + */ function layoutFor(window: FreecellWindow): Layout { - const pitch = Math.floor((window.viewportWidth - 12) / 8); - const marginX = Math.floor((window.viewportWidth - (pitch * 8 - (pitch - CARD_W))) / 2); - const available = window.viewportHeight - CASCADE_Y - 2; + const { padding } = window.viewport; + let columns = columnLayout(window.viewportWidth, padding.left, padding.right); + if (columns.pitch < MIN_PITCH) columns = columnLayout(window.viewportWidth, 0, 0); + let rows = rowLayout(window.viewportHeight, padding.top, padding.bottom); + if (rows.cascadeH < DEALT_CASCADE_H) rows = rowLayout(window.viewportHeight, 0, 0); + const { topY, cascadeY, cascadeH } = rows; return { - pitch, - marginX, + ...columns, + topY, + cascadeY, strip: (cascadeLength: number) => - cascadeLength <= 1 ? 0 : Math.min(16, Math.floor((available - CARD_H) / (cascadeLength - 1))), + cascadeLength <= 1 ? 0 : Math.min(MAX_STRIP, Math.floor((cascadeH - CARD_H) / (cascadeLength - 1))), }; } @@ -743,25 +806,26 @@ function paintContent(window: FreecellWindow): GrayImage { const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0); const layout = layoutFor(window); const colX = (i: number) => layout.marginX + i * layout.pitch; + const { topY } = layout; // Top row: free cells then foundations, with a divider between the groups. for (let i = 0; i < 4; i++) { const card = window.cells[i]!; if (card === null) { - image.drawRoundedRect(colX(i), TOP_Y, CARD_W, CARD_H, 70, 4); + image.drawRoundedRect(colX(i), topY, CARD_W, CARD_H, 70, 4); } else { - paintCard(image, colX(i), TOP_Y, card, window.selected === LOC_CELL0 + i, true); + paintCard(image, colX(i), topY, card, window.selected === LOC_CELL0 + i, true); } } - image.drawLine(colX(4) - 4, TOP_Y + 4, colX(4) - 4, TOP_Y + CARD_H - 4, 70); + image.drawLine(colX(4) - 4, topY + 4, colX(4) - 4, topY + CARD_H - 4, 70); for (let i = 0; i < 4; i++) { const x = colX(4 + i); const card = window.foundations[i]!; if (card === null) { - image.drawRoundedRect(x, TOP_Y, CARD_W, CARD_H, 70, 4); - image.drawText(labelFont, x + Math.round((CARD_W - labelFont.measureText("A")) / 2), TOP_Y + 10, "A", 70); + image.drawRoundedRect(x, topY, CARD_W, CARD_H, 70, 4); + image.drawText(labelFont, x + Math.round((CARD_W - labelFont.measureText("A")) / 2), topY + 10, "A", 70); } else { - paintCard(image, x, TOP_Y, card, window.selected === LOC_FOUNDATION0 + i, true); + paintCard(image, x, topY, card, window.selected === LOC_FOUNDATION0 + i, true); } } @@ -777,7 +841,7 @@ function paintCascade(image: GrayImage, window: FreecellWindow, layout: Layout, const x = layout.marginX + index * layout.pitch; const cascade = window.cascades[index]!; if (cascade.length === 0) { - image.drawRoundedRect(x, CASCADE_Y, CARD_W, CARD_H, 45, 4); + image.drawRoundedRect(x, layout.cascadeY, CARD_W, CARD_H, 45, 4); return; } const strip = layout.strip(cascade.length); @@ -788,7 +852,7 @@ function paintCascade(image: GrayImage, window: FreecellWindow, layout: Layout, highlightFrom = cascade.length - run; } for (let i = 0; i < cascade.length; i++) { - paintCard(image, x, CASCADE_Y + i * strip, cascade[i]!, i >= highlightFrom, i === cascade.length - 1); + paintCard(image, x, layout.cascadeY + i * strip, cascade[i]!, i >= highlightFrom, i === cascade.length - 1); } } @@ -831,17 +895,18 @@ function paintCursor(image: GrayImage, window: FreecellWindow, layout: Layout): let height: number; if (window.cursor < LOC_CASCADE0) { x = layout.marginX + (window.cursor % 8) * layout.pitch; - y = TOP_Y; + y = layout.topY; height = CARD_H; } else { const index = window.cursor - LOC_CASCADE0; const cascade = window.cascades[index]!; x = layout.marginX + index * layout.pitch; - y = CASCADE_Y; + y = layout.cascadeY; height = cascade.length === 0 ? CARD_H : (cascade.length - 1) * layout.strip(cascade.length) + CARD_H; } - image.drawRect(x - 3, y - 3, CARD_W + 6, height + 6, 255); - image.drawRect(x - 2, y - 2, CARD_W + 4, height + 4, 255); + for (const outset of [CURSOR_OUTSET, CURSOR_OUTSET - 1]) { + image.drawRect(x - outset, y - outset, CARD_W + 2 * outset, height + 2 * outset, 255); + } } function paintWinOverlay(image: GrayImage, window: FreecellWindow): void { diff --git a/app/apps/glanceboard/glanceboard-app.ts b/app/apps/glanceboard/glanceboard-app.ts index 8afaf474..9151d416 100644 --- a/app/apps/glanceboard/glanceboard-app.ts +++ b/app/apps/glanceboard/glanceboard-app.ts @@ -10,6 +10,7 @@ import { Menu } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; import { shell } from "../../ui/shell/shell"; +import type { Viewport } from "../../ui/viewport"; import { GlanceBoard } from "./board"; import { glanceFont } from "./glance-font"; import { @@ -33,17 +34,20 @@ import { findGlanceWidget, glanceWidgetSpans } from "./widgets"; export const GLANCEBOARD_WINDOW_ID = "glanceboard"; export const GLANCEBOARD_SURFACE_ID = "window:glanceboard"; -const TITLE_X = 18; -const TITLE_Y = 10; -const PARAGRAPH_X = 24; -/** The entry page splits at the middle: menu left, slot preview right. */ +/** The entry page's title: its drop below the top padding. It lines up with the menu rows' text. */ +const TITLE_OFFSET_Y = 2; +/** Indent of the intro paragraph under the title, and how far short of the middle it wraps. */ +const PARAGRAPH_INDENT = 6; +const PARAGRAPH_RIGHT_GAP = 12; +/** The entry page splits at the middle: menu left, slot preview right, this far from the split. */ const PREVIEW_MARGIN = 8; +/** The preview's outline, drawn just outside the scaled board. */ +const PREVIEW_OUTLINE = 1; const PREVIEW_MIN_LINE_VALUE = 20; const PREVIEW_LINE_VALUE = 100; -/** The entry menu's row boxes: left inset, gap below the paragraph, and inset from the page's bottom. */ -const HOME_MENU_X = 20; +/** The entry menu's row boxes: gap below the paragraph, and how far short of the middle they end. */ const HOME_MENU_TOP_GAP = 12; -const HOME_MENU_BOTTOM_MARGIN = 8; +const HOME_MENU_RIGHT_GAP = 20; /** Horizontal inset of row text (and custom row content) from its selection box. */ const HOME_ROW_TEXT_X = 10; // A throwaway menu to satisfy MenuItem.onSelect's second parameter; the @@ -56,9 +60,7 @@ const INTRO = "long-press-and-hold. It will close automatically."; /** Full-page list layout shared by the sub-screens. */ -function pageMenuLayout(width: number) { - return { x: 8, y: 8, width: width - 16, opaque: true, showBorder: false } as const; -} +const PAGE_MENU_LAYOUT = { page: true } as const; /** * The Glanceboard app's entry page. Left half: title, a short description @@ -67,6 +69,10 @@ function pageMenuLayout(width: number) { * name of the widget in each. "Select Contents" moves focus into that * preview, where scroll picks a slot and a tap opens the picker for it; * a double-tap returns focus to the menu. + * + * The menu's row boxes and the preview's outline sit at the padding + * suggested for the window's edges (viewport.ts). The menu scrolls, so it + * runs to the bottom edge itself, with that edge's padding after its last row. */ class GlanceboardHomeLayer implements Layer { private readonly menu: Menu; @@ -125,17 +131,23 @@ class GlanceboardHomeLayer implements Layer { }); } - /** Where the scaled board sits: the right half, board aspect, vertically centred. */ - private previewRect(width: number, height: number): { x: number; y: number; width: number; height: number } { - const half = (width / 2) | 0; - const availableWidth = width - half - 2 * PREVIEW_MARGIN; + /** + * Where the scaled board sits: board aspect, centred in the right half's + * room inside the window's padding, which its outline may reach. + */ + private previewRect(viewport: Viewport): { x: number; y: number; width: number; height: number } { + const { width, height, padding } = viewport; + const left = ((width / 2) | 0) + PREVIEW_MARGIN; + const top = padding.top + PREVIEW_OUTLINE; + const availableWidth = Math.max(0, width - padding.right - PREVIEW_OUTLINE - left); + const availableHeight = Math.max(0, height - padding.bottom - PREVIEW_OUTLINE - top); const layout = glanceLayout(); - const scale = Math.min(availableWidth / layout.width, (height - 2 * PREVIEW_MARGIN) / layout.height); + const scale = Math.min(availableWidth / layout.width, availableHeight / layout.height); const previewWidth = Math.round(layout.width * scale); const previewHeight = Math.round(layout.height * scale); return { - x: half + PREVIEW_MARGIN + (((availableWidth - previewWidth) / 2) | 0), - y: ((height - previewHeight) / 2) | 0, + x: left + (((availableWidth - previewWidth) / 2) | 0), + y: top + (((availableHeight - previewHeight) / 2) | 0), width: previewWidth, height: previewHeight, }; @@ -143,32 +155,37 @@ class GlanceboardHomeLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const viewport = ctx.stack.getBaseSize(); + const { width, height, padding } = viewport; const image = new GrayImage(width, height, 0); const half = (width / 2) | 0; - image.drawText(font, TITLE_X, TITLE_Y, "Glanceboard", 220); + const menuX = padding.left; + const titleX = menuX + HOME_ROW_TEXT_X; + const titleY = padding.top + TITLE_OFFSET_Y; + image.drawText(font, titleX, titleY, "Glanceboard", 220); const step = lineStep(font); - let y = TITLE_Y + step + 6; - for (const line of wrapText(font, INTRO, half - PARAGRAPH_X - 12)) { - image.drawText(font, PARAGRAPH_X, y, line, 160); + const paragraphX = titleX + PARAGRAPH_INDENT; + let y = titleY + step + 6; + for (const line of wrapText(font, INTRO, half - PARAGRAPH_RIGHT_GAP - paragraphX)) { + image.drawText(font, paragraphX, y, line, 160); y += step; } // The menu sits below the paragraph in the left half. While the preview // has focus the menu keeps its outline-only selection. const menuTop = y + HOME_MENU_TOP_GAP; - const menuWidth = half - 2 * HOME_MENU_X; - const menuHeight = Math.max(0, height - menuTop - HOME_MENU_BOTTOM_MARGIN); + const menuWidth = Math.max(0, half - HOME_MENU_RIGHT_GAP - menuX); + const menuHeight = Math.max(0, height - menuTop); this.paintCtx = ctx; try { - this.menu.paint(image, { x: HOME_MENU_X, y: menuTop, width: menuWidth, height: menuHeight }, + this.menu.paint(image, { x: menuX, y: menuTop, width: menuWidth, height: menuHeight, padBottom: padding.bottom }, ctx.stack.isFocused() && this.focus === "menu"); } finally { this.paintCtx = null; } - this.menu.drawScrollbar(image, HOME_MENU_X + menuWidth + 5, menuTop, menuHeight - 4); + this.menu.drawScrollbar(image, menuX + menuWidth + 5, menuTop, Math.max(0, menuHeight - padding.bottom - 4)); this.selectedSlot = Math.min(this.selectedSlot, glanceLayout().slots.length - 1); - const preview = this.previewRect(width, height); + const preview = this.previewRect(viewport); drawSlotPreview(image, preview, { selectedSlot: this.selectedSlot, selectionFocused: ctx.stack.isFocused() && this.focus === "preview", @@ -238,7 +255,7 @@ function drawSlotPreview( height: Math.round(r.height * sy), }); - image.drawRect(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2, 72); + image.drawRect(rect.x - PREVIEW_OUTLINE, rect.y - PREVIEW_OUTLINE, rect.width + 2 * PREVIEW_OUTLINE, rect.height + 2 * PREVIEW_OUTLINE, 72); const selected = scaled(layout.slots[options.selectedSlot]!.rect); drawSelectionHighlight(image, selected.x + 1, selected.y + 1, selected.width - 2, selected.height - 2, options.selectionFocused, 4); @@ -321,7 +338,7 @@ function openGlanceSettings(ctx: LayerContext): void { enumSettingMenuItem(glanceDepthSetting), ...widgetSettingsMenuItems(), ]; - const layer = new MenuLayer("Glanceboard settings", items(), pageMenuLayout(ctx.stack.getBaseSize().width)); + const layer = new MenuLayer("Glanceboard settings", items(), PAGE_MENU_LAYOUT); ctx.stack.push(layer); } @@ -341,7 +358,7 @@ function widgetSettingsMenuItems(): MenuItem[] { return { label: widget.label, description: `${widget.label} has no settings.`, disabled: true, onSelect: () => {} }; } return submenuItem(widget.label, (ctx) => { - ctx.stack.push(new MenuLayer(`${widget.label} settings`, settingsMenuItems(), pageMenuLayout(ctx.stack.getBaseSize().width))); + ctx.stack.push(new MenuLayer(`${widget.label} settings`, settingsMenuItems(), PAGE_MENU_LAYOUT)); }, { description: `Settings for the ${widget.label} widget.` }); }); } diff --git a/app/apps/health/health-app.ts b/app/apps/health/health-app.ts index cfea5e21..f8c076f9 100644 --- a/app/apps/health/health-app.ts +++ b/app/apps/health/health-app.ts @@ -123,11 +123,12 @@ class HealthLayer implements Layer { } paint(ctx: LayerContext): GrayImage { - const { width, height } = ctx.stack.getBaseSize(); + // The pages lay out inside the padding suggested for the window's edges. + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); drawGlancePage( image, - { width, height }, + { width, height, padding }, { small: getDefaultSmallFont(), large: getDefaultLargeFont() }, GLANCE_PAGES[this.pageIndex] ?? GLANCE_PAGES[0]!, { diff --git a/app/apps/launcher/launcher-app.ts b/app/apps/launcher/launcher-app.ts index d7cf2ccf..ad592ff6 100644 --- a/app/apps/launcher/launcher-app.ts +++ b/app/apps/launcher/launcher-app.ts @@ -4,7 +4,7 @@ import { GrayImage } from "../../graphics/image"; import { type Plane } from "../../graphics/plane"; import { renderIcon, type IconName } from "../../graphics/icons"; import { InputEvent } from "../../ui/gestures"; -import { IconGrid, ICON_GRID_BAND_INSET_X, ICON_GRID_HIGHLIGHT_INSET_Y, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid"; +import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid"; import { Layer, LayerActions, LayerContext } from "../../ui/layers"; import { MenuLayer, type MenuItem } from "../../ui/menu"; import { WINDOW_MENU_LAYOUT } from "../../ui/window-menu"; @@ -43,12 +43,9 @@ export type LauncherOptions = { export const LAUNCHER_WINDOW_ID = "launcher"; export const LAUNCHER_SURFACE_ID = "window:launcher"; -const GRID_TOP = 6; -/** - * The root grid's top: its first row's highlight band then sits as far - * below the window's top edge as it is from the side edges. - */ -const ROOT_GRID_TOP = ICON_GRID_BAND_INSET_X - ICON_GRID_HIGHLIGHT_INSET_Y; +/** A folder's name, above its grid: how far in from the grid's side, and the gap under it. */ +const FOLDER_NAME_INSET_X = 4; +const FOLDER_NAME_GAP = 6; /** One cell of the grid: an app, or a folder holding some of the apps. */ type LauncherGridEntry = @@ -260,16 +257,25 @@ class LauncherGridLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - // The folder-name header band scales with the font. - const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : ROOT_GRID_TOP; + const gridX = padding.left; + const gridWidth = width - padding.left - padding.right; + // The grid scrolls, so it runs to the window's top and bottom edges, the + // padding suggested for them inside it: its end rows keep clear of a + // border there, and a row cut off for scrolling meets the border rather + // than a blank strip. A folder's name stays put above its grid instead, + // in a band that scales with the font. + let gridTop = 0; + let padTop = padding.top; if (this.currentFolder !== null) { - image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160); + const nameX = gridX + FOLDER_NAME_INSET_X; + image.drawText(font, nameX, padding.top, truncateText(font, this.currentFolder, gridWidth - 2 * FOLDER_NAME_INSET_X), 160); + gridTop = padding.top + font.lineHeight + FOLDER_NAME_GAP; + padTop = 0; } - // The grid runs to the window's bottom edge, so a row cut off there for - // scrolling meets the window's border rather than a blank strip. - this.grid.paint(image, { x: 0, y: gridTop, width, height: height - gridTop }, ctx.stack.isFocused()); + this.grid.paint(image, { x: gridX, y: gridTop, width: gridWidth, height: height - gridTop, padTop, padBottom: padding.bottom }, + ctx.stack.isFocused()); return image; } diff --git a/app/apps/microphones/captions-layer.ts b/app/apps/microphones/captions-layer.ts index 01d87f54..e4a2e8e2 100644 --- a/app/apps/microphones/captions-layer.ts +++ b/app/apps/microphones/captions-layer.ts @@ -5,11 +5,19 @@ import { GESTURE_DOUBLE_CLICK, GESTURE_SCROLL, type InputEvent } from "../../ui/ import { Layer, type LayerContext } from "../../ui/layers"; import { micSession, type CaptionLine, type MicSessionState } from "./mic-session"; +/** From the title's top to the first caption row. */ +const BODY_TOP_OFFSET = 20; +/** Indent of the caption rows under the title. */ +const ROW_INDENT = 4; + /** * On-glasses captions: each line is prefixed with the speaker's name (their * id until tagged); foreign-language lines show the translation directly * beneath the recognized original. Scroll reviews recent lines; the view * snaps back to live on new speech. + * + * The title and the hint line stay put, at the padding suggested for the + * window's top and bottom edges; the caption rows scroll between the two. */ export class CaptionsLayer implements Layer { private state: MicSessionState = micSession.getState(); @@ -33,19 +41,21 @@ export class CaptionsLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const state = this.state; + const left = padding.left; + const rowX = left + ROW_INDENT; + const bodyTop = padding.top + BODY_TOP_OFFSET; - image.drawText(font, 12, 4, "Captions", 220); + image.drawText(font, left, padding.top, "Captions", 220); if (!state.captionsActive) { - image.drawText(font, 12, 26, state.captionsNote, 130); + image.drawText(font, left, bodyTop + 2, state.captionsNote, 130); } - const rows = this.buildRows(font, width - 36, state.captionLines); + const rows = this.buildRows(font, Math.max(0, width - padding.right - rowX), state.captionLines); const step = lineStep(font) + 1; - const bodyTop = 24; - const footerY = height - font.lineHeight - 4; + const footerY = height - padding.bottom - font.lineHeight; const visibleRows = Math.max(1, Math.floor((footerY - bodyTop) / step)); const maxScrollback = Math.max(0, rows.length - visibleRows); this.scrollback = Math.min(this.scrollback, maxScrollback); @@ -54,16 +64,16 @@ export class CaptionsLayer implements Layer { const row = rows[index]!; const y = bodyTop + (index - firstRow) * step; if (row.prefix) { - image.drawText(font, 16, y, row.prefix, row.prefixValue); - image.drawText(font, 16 + font.measureText(row.prefix), y, row.text, row.value); + image.drawText(font, rowX, y, row.prefix, row.prefixValue); + image.drawText(font, rowX + font.measureText(row.prefix), y, row.text, row.value); } else { - image.drawText(font, 16 + row.indent, y, row.text, row.value); + image.drawText(font, rowX + row.indent, y, row.text, row.value); } } image.drawText( font, - 12, + left, footerY, `${GESTURE_SCROLL} history ${GESTURE_DOUBLE_CLICK} back` + (this.scrollback > 0 ? ` (${this.scrollback} back)` : ""), diff --git a/app/apps/microphones/levels-layer.ts b/app/apps/microphones/levels-layer.ts index 5f37c974..c35caca8 100644 --- a/app/apps/microphones/levels-layer.ts +++ b/app/apps/microphones/levels-layer.ts @@ -4,6 +4,12 @@ import { GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures"; import { Layer, type LayerContext } from "../../ui/layers"; import { micSession, type MicSessionState } from "./mic-session"; +/** From the title's top to the status line, and to the first meter row. */ +const STATUS_OFFSET_Y = 18; +const METERS_OFFSET_Y = 42; +/** Width of the mic-name column left of the meters. */ +const LABEL_COLUMN_WIDTH = 118; + /** * Live level meters, one per active microphone (a single mixed channel on * stock firmware; up to four mics with the mic-control firmware), plus the @@ -22,24 +28,27 @@ export class LevelsLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const state = this.state; + // Text starts at the padding suggested for the window's left edge, and + // the meters run to the right edge's. + const left = padding.left; - image.drawText(font, 12, 6, "Microphone Levels", 220); - image.drawText(font, 12, 24, state.statusText, 110); + image.drawText(font, left, padding.top, "Microphone Levels", 220); + image.drawText(font, left, padding.top + STATUS_OFFSET_Y, state.statusText, 110); - const barX = 130; - const barWidth = width - barX - 40; + const barX = left + LABEL_COLUMN_WIDTH; + const barWidth = Math.max(0, width - padding.right - barX); const rowHeight = 26; - let y = 48; + let y = padding.top + METERS_OFFSET_Y; const labels = state.levelLabels.length ? state.levelLabels : ["Glasses mic"]; labels.forEach((label, index) => { const level = state.levels[index] ?? 0; const enabled = state.levelsEnabled[index] ?? true; - image.drawText(font, 12, y + 3, enabled ? label : `${label} (off)`, enabled ? 190 : 90); + image.drawText(font, left, y + 3, enabled ? label : `${label} (off)`, enabled ? 190 : 90); image.drawRect(barX, y, barWidth, 16, enabled ? 70 : 40); - const fill = Math.round((barWidth - 4) * level); + const fill = Math.round(Math.max(0, barWidth - 4) * level); if (fill > 0) { // A disabled mic still meters (dimly) so its signal can be checked // before switching it back on. @@ -55,15 +64,15 @@ export class LevelsLayer implements Layer { y += 6; if (state.doaDeviceDeg !== null) { - image.drawText(font, 12, y, `Direction of arrival: ${Math.round(state.doaDeviceDeg)}°`, 160); + image.drawText(font, left, y, `Direction of arrival: ${Math.round(state.doaDeviceDeg)}°`, 160); y += 18; } - image.drawText(font, 12, y, `Signal-strength ratio: ${state.ssr}`, 130); + image.drawText(font, left, y, `Signal-strength ratio: ${state.ssr}`, 130); y += 18; const ancText = state.ancOn && state.ancEngine ? `on — ${state.ancEngine}` : "off"; - image.drawText(font, 12, y, `Noise cancellation: ${ancText}`, 130); + image.drawText(font, left, y, `Noise cancellation: ${ancText}`, 130); - image.drawText(font, 12, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(font, left, height - padding.bottom - font.lineHeight, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } diff --git a/app/apps/microphones/microphones-app.ts b/app/apps/microphones/microphones-app.ts index a520da1d..8accbefb 100644 --- a/app/apps/microphones/microphones-app.ts +++ b/app/apps/microphones/microphones-app.ts @@ -1,6 +1,5 @@ import { imageFromAsciiArt } from "../../graphics/image"; import { getDefaultSmallFont } from "../../graphics/ui-fonts"; -import { appViewportSize } from "../../ui/shell/geometry"; import { MenuLayer, TextPageLayer, @@ -62,15 +61,7 @@ const TRAY_ICON = imageFromAsciiArt( { "#": 220 }, ); -const MENU_LAYOUT = { - x: 8, - y: 8, - width: 292, - showBorder: false, - minHeight: 0, - maxHeight: appViewportSize("min").height - 16, - opaque: true, -}; +const MENU_LAYOUT = { page: true, width: 268 } as const; /** Persist a config change; the running session applies it live. */ function updateMicConfig(mutate: (config: MicConfig) => MicConfig): void { diff --git a/app/apps/microphones/radar-layer.ts b/app/apps/microphones/radar-layer.ts index ff986c51..34527ae2 100644 --- a/app/apps/microphones/radar-layer.ts +++ b/app/apps/microphones/radar-layer.ts @@ -5,6 +5,16 @@ import { Layer, type LayerContext } from "../../ui/layers"; import { micSession, type MicSessionState } from "./mic-session"; import { dotBrightness } from "./radar-dots"; +/** Room above the plot's ring for its "front" label. */ +const FRONT_LABEL_BAND = 14; +/** Gap between the ring and the hint line under it. */ +const RING_FOOTER_GAP = 10; +/** The least the ring keeps clear of the window's sides, which leaves the readout panel its corner. */ +const RING_SIDE_CLEARANCE = 90; +/** The readout panel: the least its column is wide, and its drop below the title's top. */ +const PANEL_MIN_WIDTH = 104; +const PANEL_OFFSET_Y = 20; + /** * The Sonic Radar: a top-down view around the wearer's head. The shaded * wedge is the listening beam (scroll rotates it, click locks it onto the @@ -14,6 +24,10 @@ import { dotBrightness } from "./radar-dots"; * scene is world-anchored (an N tick marks north and dots stay put as the * head turns); dots glow full-bright while their speaker is talking and fade * with the time since they last spoke. + * + * The plot is centred on the window, between the title row and the hint + * line; those and the readout panel keep to the padding suggested for the + * window's edges. */ export class RadarLayer implements Layer { private state: MicSessionState = micSession.getState(); @@ -28,17 +42,20 @@ export class RadarLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const state = this.state; + const footerY = height - padding.bottom - font.lineHeight; + const ringTop = padding.top + FRONT_LABEL_BAND; + const ringBottom = footerY - RING_FOOTER_GAP; const centerX = (width / 2) | 0; - const centerY = ((height - 24) / 2 + 6) | 0; - const radius = Math.min(centerX - 90, ((height - 40) / 2) | 0); + const centerY = ((ringTop + ringBottom) / 2) | 0; + const radius = Math.max(0, Math.min(centerX - RING_SIDE_CLEARANCE, ((ringBottom - ringTop) / 2) | 0)); - image.drawText(font, 12, 6, "Sonic Radar", 220); + image.drawText(font, padding.left, padding.top, "Sonic Radar", 220); const modeText = state.mode === "extended" ? "4-mic array" : "firmware DoA"; - image.drawText(font, width - font.measureText(modeText) - 12, 6, modeText, 110); + image.drawText(font, width - padding.right - font.measureText(modeText), padding.top, modeText, 110); // Beam wedge (device frame; the head is the reference for display). const beamDeviceDeg = this.deviceBeamDeg(state); @@ -70,7 +87,7 @@ export class RadarLayer implements Layer { // Radar rings. drawCircle(image, centerX, centerY, radius, 60); drawCircle(image, centerX, centerY, (radius / 2) | 0, 30); - image.drawText(font, centerX - (font.measureText("front") / 2) | 0, centerY - radius - 14, "front", 90); + image.drawText(font, centerX - (font.measureText("front") / 2) | 0, centerY - radius - FRONT_LABEL_BAND, "front", 90); // Compass north tick: anchors the world frame visually as the head turns. if (state.headingDeg !== null) { @@ -127,37 +144,31 @@ export class RadarLayer implements Layer { } const label = dot.name; const labelValue = Math.max(90, value - 50); - const labelX = dotX + 8 + font.measureText(label) > width ? dotX - font.measureText(label) - 8 : dotX + 8; + const labelX = dotX + 8 + font.measureText(label) > width - padding.right ? dotX - font.measureText(label) - 8 : dotX + 8; image.drawText(font, labelX, dotY - 6, label, labelValue); } - // Side panel: beam + level readouts. - const panelX = width - 116; - let panelY = 26; - image.drawText( - font, - panelX, - panelY, - state.headingDeg !== null ? `Heading ${Math.round(state.headingDeg)}°` : "No compass", - state.headingDeg !== null ? 150 : 90, - ); - panelY += 16; - image.drawText(font, panelX, panelY, `Beam ${Math.round(state.beamWorldDeg)}°`, 170); - panelY += 16; - image.drawText(font, panelX, panelY, `Width ${Math.round(state.beamWidthDeg)}°`, 130); - panelY += 16; - image.drawText(font, panelX, panelY, state.beamLocked ? "Locked on talker" : "Manual aim", 130); - panelY += 16; - if (state.doaDeviceDeg !== null) { - image.drawText(font, panelX, panelY, `Heard ${Math.round(state.doaDeviceDeg)}°`, 200); + // Side panel: beam + level readouts, in a column that ends at the right + // edge's padding and is wide enough for its longest line. + const panelLines: Array<[text: string, value: number]> = [ + state.headingDeg !== null ? [`Heading ${Math.round(state.headingDeg)}°`, 150] : ["No compass", 90], + [`Beam ${Math.round(state.beamWorldDeg)}°`, 170], + [`Width ${Math.round(state.beamWidthDeg)}°`, 130], + [state.beamLocked ? "Locked on talker" : "Manual aim", 130], + ]; + if (state.doaDeviceDeg !== null) panelLines.push([`Heard ${Math.round(state.doaDeviceDeg)}°`, 200]); + panelLines.push([state.beamFilterOn ? "Filter: beam only" : "Filter: off", 130]); + const panelWidth = Math.max(PANEL_MIN_WIDTH, ...panelLines.map(([text]) => Math.ceil(font.measureText(text)))); + const panelX = width - padding.right - panelWidth; + let panelY = padding.top + PANEL_OFFSET_Y; + for (const [text, value] of panelLines) { + image.drawText(font, panelX, panelY, text, value); panelY += 16; } - image.drawText(font, panelX, panelY, state.beamFilterOn ? "Filter: beam only" : "Filter: off", 130); - const footerY = height - font.lineHeight - 4; image.drawText( font, - 12, + padding.left, footerY, `${GESTURE_SCROLL} aim ${GESTURE_CLICK} lock ${GESTURE_DOUBLE_CLICK} back`, 110, diff --git a/app/apps/minesweeper/minesweeper-app.worker.ts b/app/apps/minesweeper/minesweeper-app.worker.ts index 3cd49ee9..739b71a1 100644 --- a/app/apps/minesweeper/minesweeper-app.worker.ts +++ b/app/apps/minesweeper/minesweeper-app.worker.ts @@ -33,6 +33,7 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; import { type MenuItem } from "../../ui/menu"; import { WindowMenu } from "../../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; +import type { Viewport } from "../../ui/viewport"; import { directionalFallback, GESTURE_CLICK, @@ -53,9 +54,10 @@ const smallFont = getDefaultSmallFont(); const COLS = 12; const ROWS = 9; const CELL = 26; -const BOARD_X = 14; -const BOARD_Y = 14; -const PANEL_X = BOARD_X + COLS * CELL + 28; +/** How far outside its cells the board's frame is drawn. */ +const BOARD_FRAME = 2; +/** Gap between the board's cells and the panel to their right. */ +const PANEL_GAP = 28; const DIFFICULTIES = [ { name: "Easy", mines: 14 }, @@ -107,6 +109,12 @@ type MinesweeperWindow = { title: string; viewportWidth: number; viewportHeight: number; + /** + * The viewport as the shell last sent it: besides the size above, what each + * edge touches and the padding suggested there, which places the board + * (see boardOrigin). + */ + viewport: Viewport; foreground: boolean; /** Whether this window is the shell's input target (pushed with each message). */ focused: boolean; @@ -166,6 +174,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { title: message.title, viewportWidth: message.viewport.width, viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, @@ -255,7 +264,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { if (!window) break; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; - window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight }); + window.viewport = message.viewport; + window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; if (window.foreground) renderAndSubmit(window, 0); break; @@ -352,7 +362,7 @@ function windowMenu(window: MinesweeperWindow): WindowMenu { title: () => window.title, items: () => windowMenuItems(window), claimsLongPress: () => window.phase === "playing", - size: { width: window.viewportWidth, height: window.viewportHeight }, + size: window.viewport, paintBase: () => paintContent(window), isFocused: () => window.focused, }); @@ -666,46 +676,67 @@ function paint(window: MinesweeperWindow): Plane[] { return windowMenu(window).paint(); } +/** Top-left corner of the board's cells, in viewport coordinates. */ +type BoardOrigin = { x: number; y: number }; + +/** + * Where the board goes: its frame, the outermost thing drawn toward the + * window's top and left edges, sits at the padding suggested for them. The + * board is a fixed size and the panel hangs off its right side, so the whole + * layout moves with it and nothing is placed from the other two edges. + */ +function boardOrigin(window: MinesweeperWindow): BoardOrigin { + const { padding } = window.viewport; + return { x: padding.left + BOARD_FRAME, y: padding.top + BOARD_FRAME }; +} + function paintContent(window: MinesweeperWindow): GrayImage { const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0); - image.drawRect(BOARD_X - 2, BOARD_Y - 2, COLS * CELL + 4, ROWS * CELL + 4, 120); + const board = boardOrigin(window); + image.drawRect( + board.x - BOARD_FRAME, + board.y - BOARD_FRAME, + COLS * CELL + 2 * BOARD_FRAME, + ROWS * CELL + 2 * BOARD_FRAME, + 120, + ); if (window.phase === "paused") { // Hide the board so pausing can't be used to study it off the clock. - drawCenteredIn(image, mediumFont, BOARD_X, COLS * CELL, BOARD_Y + 80, "PAUSED", 230); - drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 120, `${GESTURE_CLICK} resume`, 150); - drawCenteredIn(image, smallFont, BOARD_X, COLS * CELL, BOARD_Y + 140, `${GESTURE_DOUBLE_CLICK} leave`, 150); + drawCenteredIn(image, mediumFont, board.x, COLS * CELL, board.y + 80, "PAUSED", 230); + drawCenteredIn(image, smallFont, board.x, COLS * CELL, board.y + 120, `${GESTURE_CLICK} resume`, 150); + drawCenteredIn(image, smallFont, board.x, COLS * CELL, board.y + 140, `${GESTURE_DOUBLE_CLICK} leave`, 150); } else { - paintBoard(image, window); + paintBoard(image, window, board); } - paintPanel(image, window); + paintPanel(image, window, board); return image; } -function paintBoard(image: GrayImage, window: MinesweeperWindow): void { +function paintBoard(image: GrayImage, window: MinesweeperWindow, board: BoardOrigin): void { for (let y = 0; y < ROWS; y++) { for (let x = 0; x < COLS; x++) { - paintCell(image, window, x, y); + paintCell(image, window, board, x, y); } } // Cursor: a double bright outline so it stays visible over any cell. In // row-select it spans the whole selected row; in column-select it marks the // cell (games over show neither). if (window.phase !== "playing") return; - const cx = BOARD_X + window.cursorX * CELL; - const cy = BOARD_Y + window.cursorY * CELL; + const cx = board.x + window.cursorX * CELL; + const cy = board.y + window.cursorY * CELL; if (window.selectMode === "row") { - image.drawRect(BOARD_X - 1, cy - 1, COLS * CELL + 1, CELL + 1, 255); - image.drawRect(BOARD_X, cy, COLS * CELL - 1, CELL - 1, 255); + image.drawRect(board.x - 1, cy - 1, COLS * CELL + 1, CELL + 1, 255); + image.drawRect(board.x, cy, COLS * CELL - 1, CELL - 1, 255); } else { image.drawRect(cx - 1, cy - 1, CELL + 1, CELL + 1, 255); image.drawRect(cx, cy, CELL - 1, CELL - 1, 255); } } -function paintCell(image: GrayImage, window: MinesweeperWindow, x: number, y: number): void { +function paintCell(image: GrayImage, window: MinesweeperWindow, board: BoardOrigin, x: number, y: number): void { const index = y * COLS + x; - const cx = BOARD_X + x * CELL; - const cy = BOARD_Y + y * CELL; + const cx = board.x + x * CELL; + const cy = board.y + y * CELL; const state = window.cellState[index]!; const isMine = window.mines[index] === 1; const gameOver = window.phase === "lost"; @@ -757,21 +788,23 @@ function drawMine(image: GrayImage, cx: number, cy: number, shade: number): void image.drawLine(centerX - 7, centerY + 7, centerX + 7, centerY - 7, shade); } -function paintPanel(image: GrayImage, window: MinesweeperWindow): void { - drawStat(image, PANEL_X, BOARD_Y, "Mines", String(mineCount(window) - window.flagCount)); - drawStat(image, PANEL_X, BOARD_Y + 62, "Time", formatElapsed(elapsedMs(window))); - image.drawText(smallFont, PANEL_X, BOARD_Y + 126, DIFFICULTIES[window.difficultyIndex]!.name, 140); +/** The stats and gesture hints beside the board, laid out down from its top. */ +function paintPanel(image: GrayImage, window: MinesweeperWindow, board: BoardOrigin): void { + const x = board.x + COLS * CELL + PANEL_GAP; + drawStat(image, x, board.y, "Mines", String(mineCount(window) - window.flagCount)); + drawStat(image, x, board.y + 62, "Time", formatElapsed(elapsedMs(window))); + image.drawText(smallFont, x, board.y + 126, DIFFICULTIES[window.difficultyIndex]!.name, 140); if (window.phase === "won" || window.phase === "lost") { - image.drawText(mediumFont, PANEL_X, BOARD_Y + 152, window.phase === "won" ? "CLEARED!" : "BOOM!", 250); - image.drawText(smallFont, PANEL_X, BOARD_Y + 178, `${GESTURE_CLICK} new game`, 150); - image.drawText(smallFont, PANEL_X, BOARD_Y + 198, `${GESTURE_LONG_PRESS} menu`, 150); + image.drawText(mediumFont, x, board.y + 152, window.phase === "won" ? "CLEARED!" : "BOOM!", 250); + image.drawText(smallFont, x, board.y + 178, `${GESTURE_CLICK} new game`, 150); + image.drawText(smallFont, x, board.y + 198, `${GESTURE_LONG_PRESS} menu`, 150); } else if (window.selectMode === "row") { - image.drawText(smallFont, PANEL_X, 200, `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick`, 115); - image.drawText(smallFont, PANEL_X, 222, `${GESTURE_DOUBLE_CLICK} pause`, 115); + image.drawText(smallFont, x, board.y + 186, `${GESTURE_SCROLL} row ${GESTURE_CLICK} pick`, 115); + image.drawText(smallFont, x, board.y + 208, `${GESTURE_DOUBLE_CLICK} pause`, 115); } else { - image.drawText(smallFont, PANEL_X, 200, `${GESTURE_SCROLL} cell ${GESTURE_CLICK} dig`, 115); - image.drawText(smallFont, PANEL_X, 222, `${GESTURE_LONG_PRESS} flag ${GESTURE_DOUBLE_CLICK} rows`, 115); + image.drawText(smallFont, x, board.y + 186, `${GESTURE_SCROLL} cell ${GESTURE_CLICK} dig`, 115); + image.drawText(smallFont, x, board.y + 208, `${GESTURE_LONG_PRESS} flag ${GESTURE_DOUBLE_CLICK} rows`, 115); } } diff --git a/app/apps/music/media-browse.ts b/app/apps/music/media-browse.ts index 3876c67e..da949966 100644 --- a/app/apps/music/media-browse.ts +++ b/app/apps/music/media-browse.ts @@ -11,13 +11,24 @@ import { } from "../../native/media-browser"; import { Layer, type LayerContext } from "../../ui/layers"; -/** Header: title line + path line (34px at the 12px bitmap default). */ +/** Header: title line + path line (26px at the 12px bitmap default), below the window's top padding. */ function headerHeight(font: UiFont): number { - return 8 + lineStep(font) + font.lineHeight; + return lineStep(font) + font.lineHeight; } -const LIST_X = 20; -/** The selection box extends this far left and right of the row text. */ +/** + * The selection box extends this far left and right of the row text. The + * boxes start at the window's left padding, so the title, path and hint + * line, which line up with the row text, start this far in from it. + */ const LIST_HIGHLIGHT_PAD = 6; +/** Status messages (connecting, errors, empty) are indented this far from the title, and sit this far under the header. */ +const MESSAGE_INDENT = 4; +const MESSAGE_GAP = 8; +/** Clear rows between the list and the hint line under it. */ +const FOOTER_GAP = 7; +/** Width of the bar Menu.drawScrollbar draws, and the room kept for it right of the rows. */ +const SCROLLBAR_WIDTH = 3; +const SCROLLBAR_GUTTER = 6; export type MediaBrowseOptions = { app: MediaBrowserApp; @@ -59,55 +70,68 @@ export class MediaBrowseLayer implements Layer { this.startConnect(ctx); } const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); + // The list's selection boxes span the window's side padding, less the + // scrollbar's gutter. The header and the hint line are fixed, inside the + // top and bottom padding, with the list scrolling between them. + const listX = padding.left; + const textX = listX + LIST_HIGHLIGHT_PAD; + const textWidth = width - padding.left - padding.right - 2 * LIST_HIGHLIGHT_PAD; + const headerBottom = padding.top + headerHeight(font); + const messageX = textX + MESSAGE_INDENT; + const messageY = headerBottom + MESSAGE_GAP; + const footerY = height - padding.bottom - font.lineHeight; - image.drawText(font, 20, 8, `Library: ${this.options.app.appName}`, 220); + image.drawText(font, textX, padding.top, `Library: ${this.options.app.appName}`, 220); if (this.phase === "connecting" || this.phase === "idle") { - image.drawText(font, 24, headerHeight(font) + 8, `Starting ${this.options.app.appName}...`, 150); - image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(font, messageX, messageY, `Starting ${this.options.app.appName}...`, 150); + image.drawText(font, textX, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } if (this.phase === "failed") { - image.drawText(font, 24, headerHeight(font) + 8, "Could not connect:", 180); - image.drawText(font, 24, headerHeight(font) + 8 + lineStep(font), truncateRight(font, this.connectError, width - 48), 140); - image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(font, messageX, messageY, "Could not connect:", 180); + image.drawText(font, messageX, messageY + lineStep(font), truncateRight(font, this.connectError, textWidth - 2 * MESSAGE_INDENT), 140); + image.drawText(font, textX, footerY, `${GESTURE_CLICK} retry ${GESTURE_DOUBLE_CLICK} back`, 110); return image; } const level = this.currentLevel(); const path = this.levels.map((entry) => entry.title).join(" / "); - image.drawText(font, 20, 8 + lineStep(font), truncateLeft(font, path, width - 40), 130); + image.drawText(font, textX, padding.top + lineStep(font), truncateLeft(font, path, textWidth), 130); if (!level || level.items === null) { - image.drawText(font, 24, headerHeight(font) + 8, level?.error ? level.error : "Loading...", level?.error ? 140 : 150); - image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} up / back`, 110); + image.drawText(font, messageX, messageY, level?.error ? level.error : "Loading...", level?.error ? 140 : 150); + image.drawText(font, textX, footerY, `${GESTURE_DOUBLE_CLICK} up / back`, 110); return image; } const items = level.items; if (!items.length) { - image.drawText(font, 24, headerHeight(font) + 8, "(empty)", 120); - image.drawText(font, 20, height - font.lineHeight - 4, `${GESTURE_DOUBLE_CLICK} up / back`, 110); + image.drawText(font, messageX, messageY, "(empty)", 120); + image.drawText(font, textX, footerY, `${GESTURE_DOUBLE_CLICK} up / back`, 110); return image; } - const listTop = headerHeight(font); - const listHeight = height - listTop - font.lineHeight - 10; - const visibleRows = Math.max(1, (listHeight / tightRowHeight(font)) | 0); // Row boxes start one pixel above the header's bottom edge. - level.menu.paint( - image, - { x: LIST_X - LIST_HIGHLIGHT_PAD, y: listTop - 1, width: width - 2 * LIST_X + 2 * LIST_HIGHLIGHT_PAD, height: listHeight }, - ctx.stack.isFocused(), - ); - level.menu.drawScrollbar(image, width - 12, listTop, visibleRows * tightRowHeight(font) - 4); + const listTop = headerBottom - 1; + const listBottom = footerY - FOOTER_GAP; + if (listBottom > listTop) { + level.menu.paint( + image, + { x: listX, y: listTop, width: width - padding.left - padding.right - SCROLLBAR_GUTTER, height: listBottom - listTop }, + ctx.stack.isFocused(), + ); + // The bar hugs the window's right padding, a little short of the list's ends. + const trackHeight = listBottom - listTop - 4; + if (trackHeight > 0) level.menu.drawScrollbar(image, width - padding.right - SCROLLBAR_WIDTH, headerBottom, trackHeight); + } image.drawText( font, - 20, - height - 16, + textX, + footerY, `${GESTURE_SCROLL} select ${GESTURE_CLICK} open/play ${GESTURE_DOUBLE_CLICK} up / back`, 110, ); diff --git a/app/apps/music/music-app.ts b/app/apps/music/music-app.ts index 4a32c2db..117178e1 100644 --- a/app/apps/music/music-app.ts +++ b/app/apps/music/music-app.ts @@ -14,26 +14,41 @@ import { MusicSettingsLayer } from "./music-settings"; import { spotifyMenuItem } from "./spotify-menu"; import { Layer, type LayerContext, type PaintBelow } from "../../ui/layers"; import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; +import type { Viewport } from "../../ui/viewport"; export const MUSIC_WINDOW_ID = "music"; export const MUSIC_SURFACE_ID = "window:music"; const ART_SIZE = 112; -const ART_X = 8; // left margin matches the top margin -const ART_Y = 8; -const META_X = ART_X + ART_SIZE + 14; -const PROGRESS_BAR_Y = ART_Y + 116; +/** + * The outline hugging the album art. It is the outermost thing the + * now-playing page draws toward the window's top and left edges, so it sits + * at the padding suggested for them and the art just inside it. + */ +const ART_FRAME = 1; +/** Gap between the art and the metadata column. */ +const META_GAP = 14; +/** The metadata's first line, the progress bar and the lists, down from the art's top. */ +const META_OFFSET_Y = 2; +const PROGRESS_BAR_OFFSET_Y = 116; +const LIST_OFFSET_Y = 128; /** Clear rows between the time labels' baseline (digits have no descenders) and the bar. */ const PROGRESS_TIME_GAP = 3; -const LIST_TOP = 136; -/** Gap between the menus' last row band and the viewport bottom. */ -const LIST_BOTTOM_MARGIN = 4; -const ACTION_X = 22; const ACTION_WIDTH = 154; -const COLUMN_DIVIDER_X = 190; -const QUEUE_X = 204; /** Horizontal inset of row text from its selection box. */ const LIST_TEXT_INSET = 6; +/** The actions column's selection boxes, which start at the window's left padding. */ +const ACTION_BOX_WIDTH = ACTION_WIDTH + 2 * LIST_TEXT_INSET - 4; +/** The column divider and the queue's selection boxes, right of the actions column's left edge. */ +const COLUMN_DIVIDER_OFFSET_X = 174; +const QUEUE_OFFSET_X = 182; +/** Width of the bar Menu.drawScrollbar draws, and the room kept for it right of a list's rows. */ +const SCROLLBAR_WIDTH = 3; +const SCROLLBAR_GUTTER = 6; +/** How far the setup prompt's text is indented from its hint line. */ +const PROMPT_INDENT = 4; +/** Gap between the no-session page's status line, its prompt and its list. */ +const SECTION_GAP = 6; const PLAYLIST_ACTION_INDEX = 1; /** Minimum room for the dim artist after a queue row's title; less and it is left off. */ const QUEUE_SUBTITLE_MIN_WIDTH = 40; @@ -102,107 +117,111 @@ class MusicAppLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const viewport = ctx.stack.getBaseSize(); + const { width, height, padding } = viewport; const image = new GrayImage(width, height, 0); const media = mediaControllerBridge.snapshot(); if (!media.accessEnabled) { - const lines = wrapText(font, mediaControllerBridge.accessPrompt, width - 48); + // The hint line sits at the window's left and bottom padding; the + // prompt is indented from it, as far from the right padding as the left. + const lines = wrapText(font, mediaControllerBridge.accessPrompt, width - padding.left - padding.right - 2 * PROMPT_INDENT); for (let index = 0; index < lines.length; index++) { - image.drawText(font, 24, 16 + index * lineStep(font), lines[index]!, 180); + image.drawText(font, padding.left + PROMPT_INDENT, padding.top + index * lineStep(font), lines[index]!, 180); } - image.drawText(font, 20, height - 16, `${GESTURE_CLICK} ${mediaControllerBridge.accessAction}`, 110); + image.drawText(font, padding.left, height - padding.bottom - font.lineHeight, `${GESTURE_CLICK} ${mediaControllerBridge.accessAction}`, 110); return image; } + // Lists' selection boxes start at the window's left padding and stop + // short of the scrollbar, which hugs the right padding. + const listX = padding.left; + const rowsRight = width - padding.right - SCROLLBAR_GUTTER; + const focused = ctx.stack.isFocused(); + if (!media.available) { const apps = mediaBrowserBridge.listBrowsableApps(!this.libraryAppsFresh); this.libraryAppsFresh = true; this.libraryMenu.setItems(apps); - image.drawText(font, 24, 16, global.isIOS ? media.status || "Nothing is playing." : "No active media session.", 180); + // The text lines up with the picker's row text. + const textX = listX + LIST_TEXT_INSET; + const textWidth = rowsRight - listX - 2 * LIST_TEXT_INSET; + image.drawText(font, textX, padding.top, global.isIOS ? media.status || "Nothing is playing." : "No active media session.", 180); const prompt = global.isIOS ? "Browse your library, or start playback on the phone." : apps.length ? "Browse a music app's library, or start playback on the phone." : "Start playback in another app on the phone."; - let y = 22 + lineStep(font); - for (const line of wrapText(font, prompt, width - 48)) { - image.drawText(font, 24, y, line, 150); + let y = padding.top + lineStep(font) + SECTION_GAP; + for (const line of wrapText(font, prompt, textWidth)) { + image.drawText(font, textX, y, line, 150); y += lineStep(font); } // Why the last command failed (e.g. playing from the Spotify library with no device open). - for (const line of media.notice ? wrapText(font, media.notice, width - 48) : []) { - image.drawText(font, 24, y, line, 200); + for (const line of media.notice ? wrapText(font, media.notice, textWidth) : []) { + image.drawText(font, textX, y, line, 200); y += lineStep(font); } if (apps.length) { - const listTop = y + 6; - const listHeight = height - listTop - LIST_BOTTOM_MARGIN; - this.libraryMenu.paint( - image, - { x: 24 - LIST_TEXT_INSET, y: listTop - 1, width: width - 48 + 2 * LIST_TEXT_INSET - 4, height: listHeight }, - ctx.stack.isFocused(), - ); - this.libraryMenu.drawScrollbar(image, width - 12, listTop, listHeight - 4); + paintListToBottomEdge(image, this.libraryMenu, { x: listX, top: y + SECTION_GAP, width: rowsRight - listX }, viewport, focused, true); } return image; } this.libraryAppsFresh = false; - this.drawArt(image, media); + const artX = padding.left + ART_FRAME; + const artY = padding.top + ART_FRAME; + this.drawArt(image, media, artX, artY); - const metaWidth = width - META_X - 24; + // The metadata column and the progress bar end at the window's right padding. + const metaX = artX + ART_SIZE + META_GAP; + const metaWidth = width - padding.right - metaX; const metaStep = lineStep(font) + 1; - const metaTop = ART_Y + 2; + const metaTop = artY + META_OFFSET_Y; + const progressBarY = artY + PROGRESS_BAR_OFFSET_Y; // Line budget above the progress-bar time line: the layout is five slots // (title, second title line, artist, album, player app); when the font is // too large for all five, the title gets one truncated line instead of // wrapping to two, and the rest shift up a slot. - const maxLines = Math.floor((progressTimeY(font) - metaTop - 4 - font.lineHeight) / metaStep) + 1; + const maxLines = Math.floor((progressTimeY(font, progressBarY) - metaTop - 4 - font.lineHeight) / metaStep) + 1; const titleSlots = maxLines >= 5 ? 2 : 1; const title = media.title || "Unknown title"; const titleLines = titleSlots === 2 ? wrapText(font, title, metaWidth).slice(0, 2) : [truncateText(font, title, metaWidth)]; for (let index = 0; index < titleLines.length; index++) { - image.drawText(font, META_X, metaTop + index * metaStep, titleLines[index]!, 230); + image.drawText(font, metaX, metaTop + index * metaStep, titleLines[index]!, 230); } - image.drawText(font, META_X, metaTop + 4 + titleSlots * metaStep, media.artist || "Unknown artist", 180); + image.drawText(font, metaX, metaTop + 4 + titleSlots * metaStep, media.artist || "Unknown artist", 180); if (media.album) { - image.drawText(font, META_X, metaTop + 4 + (titleSlots + 1) * metaStep, truncateText(font, media.album, metaWidth), 150); + image.drawText(font, metaX, metaTop + 4 + (titleSlots + 1) * metaStep, truncateText(font, media.album, metaWidth), 150); } // The player line, or for a few seconds why a command failed. image.drawText( font, - META_X, + metaX, metaTop + 4 + (titleSlots + 2) * metaStep, truncateText(font, media.notice || media.appName || media.packageName, metaWidth), media.notice ? 200 : 110, ); - this.drawProgress(image, media, metaWidth); + this.drawProgress(image, media, metaX, progressBarY, metaWidth); const queue = mediaControllerBridge.getQueue(); const actions = this.buildActions(media, queue); this.reconcileSelection(actions, queue); - const listHeight = height - LIST_TOP - LIST_BOTTOM_MARGIN; - const focused = ctx.stack.isFocused(); - // Row boxes start one pixel above LIST_TOP, level with the column divider. - this.actionsMenu.paint( - image, - { x: ACTION_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: ACTION_WIDTH + 2 * LIST_TEXT_INSET - 4, height: listHeight }, - focused && this.focusColumn === "actions", - ); + const listTop = artY + LIST_OFFSET_Y; + paintListToBottomEdge(image, this.actionsMenu, { x: listX, top: listTop, width: ACTION_BOX_WIDTH }, viewport, + focused && this.focusColumn === "actions"); - image.drawLine(COLUMN_DIVIDER_X, LIST_TOP - 3, COLUMN_DIVIDER_X, height - LIST_BOTTOM_MARGIN - 3, 45); + // The divider is fixed, so it stops at the window's bottom padding. + const dividerX = listX + COLUMN_DIVIDER_OFFSET_X; + const dividerBottom = height - padding.bottom - 1; + if (dividerBottom > listTop) image.drawLine(dividerX, listTop - 3, dividerX, dividerBottom, 45); + const queueX = listX + QUEUE_OFFSET_X; if (!queue.length) { - image.drawText(font, QUEUE_X, LIST_TOP + 1, "Playlist unavailable", 90); + image.drawText(font, queueX + LIST_TEXT_INSET, listTop + 1, "Playlist unavailable", 90); } else { - const queueWidth = width - QUEUE_X - 20; - this.queueMenu.paint( - image, - { x: QUEUE_X - LIST_TEXT_INSET, y: LIST_TOP - 1, width: queueWidth + 2 * LIST_TEXT_INSET - 4, height: listHeight }, - focused && this.focusColumn === "playlist", - ); - this.queueMenu.drawScrollbar(image, width - 12, LIST_TOP, listHeight - 4); + paintListToBottomEdge(image, this.queueMenu, { x: queueX, top: listTop, width: rowsRight - queueX }, viewport, + focused && this.focusColumn === "playlist", true); } return image; @@ -386,22 +405,23 @@ class MusicAppLayer implements Layer { } } - private drawProgress(image: GrayImage, media: MediaControllerState, width: number): void { + /** The elapsed/duration labels and, under them, the bar at (x, barY), `width` wide. */ + private drawProgress(image: GrayImage, media: MediaControllerState, x: number, barY: number, width: number): void { if (media.durationMs <= 0 || media.positionMs < 0) return; const font = getDefaultSmallFont(); const elapsed = formatMediaTime(media.positionMs); const duration = formatMediaTime(media.durationMs); - const timeY = progressTimeY(font); - image.drawText(font, META_X, timeY, elapsed, 140); - image.drawText(font, META_X + width - font.measureText(duration), timeY, duration, 140); + const timeY = progressTimeY(font, barY); + image.drawText(font, x, timeY, elapsed, 140); + image.drawText(font, x + width - font.measureText(duration), timeY, duration, 140); - const barY = PROGRESS_BAR_Y; - image.drawRect(META_X, barY, width, 5, 55); + image.drawRect(x, barY, width, 5, 55); const progress = clamp(media.positionMs / media.durationMs, 0, 1); - image.fillRect(META_X + 1, barY + 1, Math.round((width - 2) * progress), 3, 170); + image.fillRect(x + 1, barY + 1, Math.round((width - 2) * progress), 3, 170); } - private drawArt(image: GrayImage, media: MediaControllerState): void { + /** The album art, or a placeholder, in the ART_SIZE box at (artX, artY), with its outline around it. */ + private drawArt(image: GrayImage, media: MediaControllerState, artX: number, artY: number): void { // Keyed on artVersion, not the strings: players (e.g. Spotify) publish // metadata before the art bitmap is downloaded, or with the previous // track's bitmap after a quick double skip, then re-emit the same @@ -412,17 +432,18 @@ class MusicAppLayer implements Layer { } if (this.art) { // Center within the art box. - const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0); - const dy = ART_Y + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0); + const dx = artX + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0); + const dy = artY + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0); image.bitBlt(this.art, dx, dy); - image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60); + image.drawRect(dx - ART_FRAME, dy - ART_FRAME, this.art.width + 2 * ART_FRAME, this.art.height + 2 * ART_FRAME, 60); } else { - image.drawRect(ART_X, ART_Y, ART_SIZE, ART_SIZE, 60); + // The placeholder's outline is where a full-size cover's would be. + image.drawRect(artX - ART_FRAME, artY - ART_FRAME, ART_SIZE + 2 * ART_FRAME, ART_SIZE + 2 * ART_FRAME, 60); const font = getDefaultSmallFont(); image.drawText( font, - ART_X + Math.round((ART_SIZE - font.measureText("no art")) / 2), - ART_Y + Math.round((ART_SIZE - font.lineHeight) / 2), + artX + Math.round((ART_SIZE - font.measureText("no art")) / 2), + artY + Math.round((ART_SIZE - font.lineHeight) / 2), "no art", 90, ); @@ -567,12 +588,37 @@ export function createMusicAppWindow(options: InProcessAppOptions): InProcessWin } /** - * Line top of the elapsed/duration labels: their baseline sits a fixed gap - * above the progress bar, so the labels hug the bar whatever the font's - * height (a taller face grows upward into the metadata's line budget). + * Paint a list that scrolls under fixed content: from `top` down to the + * window's bottom edge, where a row cut off for scrolling meets the edge, + * with that edge's padding inside the list after its last row. Selection + * boxes start one pixel above `top`. The scrollbar, when asked for, stays + * inside the window's right and bottom padding. */ -function progressTimeY(font: UiFont): number { - return PROGRESS_BAR_Y - PROGRESS_TIME_GAP - font.ascent; +function paintListToBottomEdge( + image: GrayImage, + menu: Menu, + rows: { x: number; top: number; width: number }, + viewport: Viewport, + focused: boolean, + scrollbar = false, +): void { + const { width, height, padding } = viewport; + const boxTop = rows.top - 1; + if (boxTop >= height || rows.width <= 0) return; + menu.paint(image, { x: rows.x, y: boxTop, width: rows.width, height: height - boxTop, padBottom: padding.bottom }, focused); + const trackHeight = height - padding.bottom - rows.top; + if (scrollbar && trackHeight > 0) { + menu.drawScrollbar(image, width - padding.right - SCROLLBAR_WIDTH, rows.top, trackHeight); + } +} + +/** + * Line top of the elapsed/duration labels: their baseline sits a fixed gap + * above the progress bar (at barY), so the labels hug the bar whatever the + * font's height (a taller face grows upward into the metadata's line budget). + */ +function progressTimeY(font: UiFont, barY: number): number { + return barY - PROGRESS_TIME_GAP - font.ascent; } function formatMediaTime(milliseconds: number): string { diff --git a/app/apps/music/music-settings.ts b/app/apps/music/music-settings.ts index c4c45207..e04c8f48 100644 --- a/app/apps/music/music-settings.ts +++ b/app/apps/music/music-settings.ts @@ -10,7 +10,6 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "../../ui/menu"; /** Discovered apps stay visible even when ignored or no longer publishing a session. */ export class MusicSettingsLayer implements Layer { private menu: MenuLayer | null = null; - private menuWidth = 0; /** The package behind each current row, parallel to the menu's items. */ private packages: string[] = []; @@ -20,7 +19,7 @@ export class MusicSettingsLayer implements Layer { } /** Refresh the rows from the remembered apps, keeping the selection on the same package. */ - private currentMenu(ctx: LayerContext): MenuLayer { + private currentMenu(): MenuLayer { const apps = readMediaApps().sort((a, b) => a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName)); const items: MenuItem[] = apps.map((app) => ({ @@ -39,14 +38,8 @@ export class MusicSettingsLayer implements Layer { const index = apps.findIndex((app) => app.packageName === selectedPackage); this.packages = apps.map((app) => app.packageName); - const width = ctx.stack.getBaseSize().width; - if (!this.menu || width !== this.menuWidth) { - this.menuWidth = width; - this.menu = new MenuLayer("Music settings", items, { - x: 8, y: 8, width: width - 16, - opaque: true, showBorder: false, - footer: "On = enabled Off = ignored", - }).selectItem(Math.max(0, index)); + if (!this.menu) { + this.menu = new MenuLayer("Music settings", items, { page: true, footer: "On = enabled Off = ignored" }).selectItem(Math.max(0, index)); } else { this.menu.setItems(items, index >= 0 ? index : undefined); } @@ -54,10 +47,10 @@ export class MusicSettingsLayer implements Layer { } paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { - return this.currentMenu(ctx).paint(ctx, paintBelow); + return this.currentMenu().paint(ctx, paintBelow); } async handleInput(event: InputEvent, ctx: LayerContext): Promise { - await this.currentMenu(ctx).handleInput(event, ctx); + await this.currentMenu().handleInput(event, ctx); } } diff --git a/app/apps/navigate/navigate-app.worker.ts b/app/apps/navigate/navigate-app.worker.ts index 142cef5e..9bd37f2d 100644 --- a/app/apps/navigate/navigate-app.worker.ts +++ b/app/apps/navigate/navigate-app.worker.ts @@ -59,6 +59,7 @@ import { type SavedDestination, } from "./destinations"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; +import { sameViewport, type Viewport } from "../../ui/viewport"; import type { ToolSpec, ToolResult } from "../../assistant/tool-registry"; import { GESTURE_CLICK, @@ -95,7 +96,11 @@ import { drawManeuverGlyph } from "./maneuver-icons"; declare const global: any; const MAP_SIZE = 260; +/** The guidance panel's region beside the map pane, and how far in its content starts. */ const PANEL_X = MAP_SIZE + 10; +const PANEL_INSET = 8; +/** Clear space between the footer hint line and what ends above it (the idle list, the map-mode map). */ +const FOOTER_GAP = 8; /** Map refetch policy knobs (design doc: never per-fix, only when stale). */ const BEARING_BUCKET_DEG = 15; const MOVE_REFRESH_FRACTION = 0.15; @@ -132,6 +137,13 @@ type NavWindow = { title: string; viewportWidth: number; viewportHeight: number; + /** + * The viewport as the shell last sent it: besides the size above, what each + * edge touches and the padding suggested there. The maps fill their panes + * whatever the edges touch; text, the idle list and the footer hints keep + * the padding. + */ + viewport: Viewport; foreground: boolean; focused: boolean; menu: WindowMenu | null; @@ -306,6 +318,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { title: message.title, viewportWidth: message.viewport.width, viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, @@ -313,19 +326,29 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { }; post({ type: "set-tools", windowId: message.windowId, tools: NAVIGATE_TOOLS }); break; - case "resize-window": + case "resize-window": { if (!window || window.windowId !== message.windowId) break; - if (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height) break; + // The layout follows the padding as well as the size, so a change to + // what the edges touch alone is laid out and painted again too. + if (sameViewport(window.viewport, message.viewport)) break; + const mapSize = phase === "map" ? mapDimensions() : null; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; + window.viewport = message.viewport; window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; - if (phase === "map") { - resetMapState(); - maybeRefreshMap(); + // The map is fetched at the size it is shown at: only a change to that + // needs a new one. + if (mapSize) { + const resized = mapDimensions(); + if (resized.width !== mapSize.width || resized.height !== mapSize.height) { + resetMapState(); + maybeRefreshMap(); + } } render(); break; + } case "close-window": if (editing) { // Window closed mid-edit: shut the phone editor down. @@ -826,9 +849,13 @@ function maybeRefreshMap(): void { }); } +/** + * The map's size: the pane beside the guidance panel, or in map mode the + * whole window down to the footer hint. + */ function mapDimensions(): { width: number; height: number } { return phase === "map" && window - ? { width: window.viewportWidth, height: Math.max(1, window.viewportHeight - 30) } + ? { width: window.viewportWidth, height: Math.max(1, footerY(window.viewport) - FOOTER_GAP) } : { width: MAP_SIZE, height: MAP_SIZE }; } @@ -1212,10 +1239,11 @@ function idleEntries(): IdleEntry[] { return entries; } -/** Horizontal inset of the idle list's selection boxes. */ -const IDLE_LIST_X = 20; /** Pixels between consecutive idle rows' selection boxes. */ const IDLE_ROW_GAP = 2; +/** The idle list's scrollbar, and the gap its rows leave before it. */ +const IDLE_SCROLLBAR_WIDTH = 3; +const IDLE_SCROLLBAR_GAP = 4; function idleList(): Menu { idleMenu ??= new Menu({ @@ -1260,7 +1288,7 @@ function windowMenu(win: NavWindow): WindowMenu { post, title: () => win.title, items: () => windowMenuItems(), - size: { width: win.viewportWidth, height: win.viewportHeight }, + size: win.viewport, paintBase: () => paintContent(win), isFocused: () => win.focused, }); @@ -1342,12 +1370,28 @@ function paint(win: NavWindow): Plane[] { return windowMenu(win).paint(); } +/** + * Page text (a title, hints, status messages) starts this far inside the + * padding suggested for the window's sides, which is where the idle list's + * selection boxes and the footer hints start. + */ +const TEXT_INSET = 4; +/** How far below a page's title (at the top padding) its text starts: the idle page's hint, the edit screen's message. */ +const IDLE_HINT_DROP = 36; +const EDIT_MESSAGE_DROP = 48; + +/** Where a page's text starts and how wide it may run. */ +function pageText(viewport: Viewport): { x: number; width: number } { + const { width, padding } = viewport; + return { x: padding.left + TEXT_INSET, width: width - padding.left - padding.right - 2 * TEXT_INSET }; +} + function paintContent(win: NavWindow): GrayImage { const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); if (editing) { - paintEdit(image, editing); + paintEdit(image, win, editing); } else if (phase === "map") { - paintMap(image); + paintMap(image, win); } else if (phase === "idle" || phase === "acquiring" || phase === "routing") { paintIdle(image, win); } else { @@ -1357,27 +1401,31 @@ function paintContent(win: NavWindow): GrayImage { } /** The "type on the phone" screen for a destination name/address edit. */ -function paintEdit(image: GrayImage, edit: DestinationEdit): void { +function paintEdit(image: GrayImage, win: NavWindow, edit: DestinationEdit): void { + const { padding } = win.viewport; + const text = pageText(win.viewport); const step = smallFont.lineHeight + 2; - image.drawText(mediumFont, 24, 16, edit.title, 245); - const messageY = 64; - const lines = wrapText(smallFont, "Type the value in the phone app, or use voice input.", image.width - 48); + image.drawText(mediumFont, text.x, padding.top, edit.title, 245); + const messageY = padding.top + EDIT_MESSAGE_DROP; + const lines = wrapText(smallFont, "Type the value in the phone app, or use voice input.", text.width); for (let index = 0; index < lines.length; index++) { - image.drawText(smallFont, 24, messageY + index * step, lines[index]!, 190); + image.drawText(smallFont, text.x, messageY + index * step, lines[index]!, 190); } const value = edit.setting.get(); image.drawText( smallFont, - 24, + text.x, messageY + (lines.length + 1) * step, - truncateText(smallFont, value || "(empty)", image.width - 48), + truncateText(smallFont, value || "(empty)", text.width), value ? 225 : 130, ); - drawFooter(image, `${GESTURE_CLICK} done ${GESTURE_DOUBLE_CLICK} cancel`); + drawFooter(image, win.viewport, `${GESTURE_CLICK} done ${GESTURE_DOUBLE_CLICK} cancel`); } function paintIdle(image: GrayImage, win: NavWindow): void { - image.drawText(mediumFont, 24, 16, "Navigate", 245); + const { padding } = win.viewport; + const text = pageText(win.viewport); + image.drawText(mediumFont, text.x, padding.top, "Navigate", 245); const busy = phase !== "idle"; const entries = busy ? [] : idleEntries(); const configured = isMapboxConfigured(); @@ -1386,16 +1434,17 @@ function paintIdle(image: GrayImage, win: NavWindow): void { : entries.length ? "View the map or pick a destination below. Use Voice input (system menu, long-press) to say a destination." : "Ask the voice assistant to navigate somewhere, or pick Voice input from the system menu (long-press) to say a destination. Save Home, Work and other places from the app menu."; - const hintLines = wrapText(smallFont, hint, image.width - 48); + const hintLines = wrapText(smallFont, hint, text.width); const hintStep = smallFont.lineHeight + 2; + const hintY = padding.top + IDLE_HINT_DROP; for (let index = 0; index < hintLines.length; index++) { - image.drawText(smallFont, 24, 52 + index * hintStep, hintLines[index]!, 170); + image.drawText(smallFont, text.x, hintY + index * hintStep, hintLines[index]!, 170); } - let y = 52 + hintLines.length * hintStep + 6; + let y = hintY + hintLines.length * hintStep + 6; if (statusMessage) { - const statusLines = wrapText(smallFont, statusMessage, image.width - 48); + const statusLines = wrapText(smallFont, statusMessage, text.width); for (let index = 0; index < statusLines.length; index++) { - image.drawText(smallFont, 24, y + index * hintStep, statusLines[index]!, busy ? 220 : 190); + image.drawText(smallFont, text.x, y + index * hintStep, statusLines[index]!, busy ? 220 : 190); } y += statusLines.length * hintStep + 6; } @@ -1403,23 +1452,34 @@ function paintIdle(image: GrayImage, win: NavWindow): void { paintIdleList(image, win, entries, y); drawFooter( image, + win.viewport, `${GESTURE_SCROLL} select ${GESTURE_CLICK} ${configured ? "go" : "choose"} ${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`, ); } else { - drawFooter(image, `${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`); + drawFooter(image, win.viewport, `${GESTURE_SHORT_THEN_LONG_PRESS} app menu ${GESTURE_DOUBLE_CLICK} back`); } } -/** Map action, saved destinations, then recent places, one selectable row each. */ +/** + * Map action, saved destinations, then recent places, one selectable row + * each. The list scrolls between the page's text above it and the footer + * hint, both of which stay put, so it needs no padding of its own at its + * top and bottom; its selection boxes span the window between the padding + * suggested for the sides. + */ function paintIdleList(image: GrayImage, win: NavWindow, entries: IdleEntry[], top: number): void { + const { width, padding } = win.viewport; const rowHeight = listRowHeight(smallFont); - const listBottom = image.height - 30; + const listBottom = footerY(win.viewport) - FOOTER_GAP; const visibleRows = Math.max(1, Math.floor((listBottom - top) / rowHeight)); - const rowWidth = image.width - 40 - (entries.length > visibleRows ? 10 : 0); + const listHeight = visibleRows * rowHeight - IDLE_ROW_GAP; + // The scrollbar hugs the right padding; the rows give way to it only when there is one. + const right = width - padding.right; + const scrollbar = entries.length > visibleRows ? IDLE_SCROLLBAR_GAP + IDLE_SCROLLBAR_WIDTH : 0; const list = idleList(); list.setItems(entries); - list.paint(image, { x: IDLE_LIST_X, y: top, width: rowWidth, height: visibleRows * rowHeight - IDLE_ROW_GAP }, win.focused); - list.drawScrollbar(image, IDLE_LIST_X + rowWidth + 4, top, visibleRows * rowHeight - 2); + list.paint(image, { x: padding.left, y: top, width: right - scrollbar - padding.left, height: listHeight }, win.focused); + list.drawScrollbar(image, right - IDLE_SCROLLBAR_WIDTH, top, listHeight); } /** Two columns: the entry's name, then its detail (action hint, address or place). */ @@ -1444,6 +1504,22 @@ function drawIdleRow({ image, item: entry, x, y, width, selected }: MenuDrawArgs image.drawText(smallFont, detailX, textY, truncateText(smallFont, detail, x + width - 6 - detailX), selected ? 170 : 130); } +// The guidance panel's head, measured from the maneuver glyph's top-left +// corner (the panel's content x, at the window's top padding). +const GLYPH_SIZE = 56; +/** Text beside the glyph: the distance to the maneuver, or "Arrived". */ +const GLYPH_CAPTION_X = 68; +const DISTANCE_Y = 10; +const ARRIVED_Y = 12; +/** A roundabout's exit number, inside its glyph. */ +const EXIT_NUMBER_X = 22; +const EXIT_NUMBER_Y = 32; +/** The instruction (or the destination's name) under the glyph. */ +const INSTRUCTION_Y = 70; +/** Clear space under the panel's status line (above the footer hint) and under its ETA line (above the status). */ +const PANEL_STATUS_GAP = 10; +const PANEL_ETA_GAP = 6; + function paintNavigating(image: GrayImage, win: NavWindow): void { // Map pane (left). if (mapImage) { @@ -1460,72 +1536,87 @@ function paintNavigating(image: GrayImage, win: NavWindow): void { image.drawText(smallFont, 24, MAP_SIZE / 2 - 8, text, 120); } - // Guidance panel (right) — its own region so text deltas never overlap the map's. - const panelWidth = win.viewportWidth - PANEL_X; + // Guidance panel (right) — its own region so text deltas never overlap the + // map's. Its left side is placed from the map pane; its top, right and + // bottom keep the padding suggested for the window's edges. + const { padding } = win.viewport; + const x = PANEL_X + PANEL_INSET; + const top = padding.top; + const textWidth = win.viewportWidth - padding.right - x; if (phase === "arrived") { - drawManeuverGlyph(image, PANEL_X + 8, 14, 56, "arrive", "", 245); - image.drawText(mediumFont, PANEL_X + 76, 26, "Arrived", 245); + drawManeuverGlyph(image, x, top, GLYPH_SIZE, "arrive", "", 245); + image.drawText(mediumFont, x + GLYPH_CAPTION_X, top + ARRIVED_Y, "Arrived", 245); image.drawTextWrapped({ font: smallFont, - x: PANEL_X + 8, - y: 84, - width: panelWidth - 16, + x, + y: top + INSTRUCTION_Y, + width: textWidth, text: destinationName, value: 200, }); - drawFooter(image, `${GESTURE_CLICK} done ${GESTURE_DOUBLE_CLICK} back`); + drawFooter(image, win.viewport, `${GESTURE_CLICK} done ${GESTURE_DOUBLE_CLICK} back`); return; } + // The status and ETA lines stack above the footer hint. + const statusY = footerY(win.viewport) - smallFont.lineHeight - PANEL_STATUS_GAP; const step = follower && progress ? follower.route.steps[progress.nextStepIndex] : null; if (step && progress) { - drawManeuverGlyph(image, PANEL_X + 8, 14, 56, step.maneuverType, step.maneuverModifier, 245); + drawManeuverGlyph(image, x, top, GLYPH_SIZE, step.maneuverType, step.maneuverModifier, 245); if (step.exit !== null && (step.maneuverType === "roundabout" || step.maneuverType === "rotary")) { - image.drawText(smallFont, PANEL_X + 30, 46, String(step.exit), 245); + image.drawText(smallFont, x + EXIT_NUMBER_X, top + EXIT_NUMBER_Y, String(step.exit), 245); } - image.drawText(largeFont, PANEL_X + 76, 24, formatDistanceShort(progress.metersToNextManeuver), 250); + image.drawText(largeFont, x + GLYPH_CAPTION_X, top + DISTANCE_Y, formatDistanceShort(progress.metersToNextManeuver), 250); image.drawTextWrapped({ font: mediumFont, - x: PANEL_X + 8, - y: 84, - width: panelWidth - 16, + x, + y: top + INSTRUCTION_Y, + width: textWidth, text: step.instruction || step.name || step.maneuverType, value: 220, }); const eta = `${formatDistance(progress.remainingMeters)} · ${formatDurationShort(progress.remainingSec)} · ${formatClock( Date.now() + progress.remainingSec * 1000, )}`; - image.drawText(smallFont, PANEL_X + 8, win.viewportHeight - 62, eta, 170); + image.drawText(smallFont, x, statusY - smallFont.lineHeight - PANEL_ETA_GAP, eta, 170); } else { image.drawTextWrapped({ font: mediumFont, - x: PANEL_X + 8, - y: 24, - width: panelWidth - 16, + x, + y: top, + width: textWidth, text: `Waiting for GPS...`, value: 200, }); } if (statusMessage) { - image.drawText(smallFont, PANEL_X + 8, win.viewportHeight - 44, statusMessage, 200); + image.drawText(smallFont, x, statusY, truncateText(smallFont, statusMessage, textWidth), 200); } const modeHint = mapMode === "follow" ? "overview" : "follow"; - drawFooter(image, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} ${modeHint} ${GESTURE_DOUBLE_CLICK} back`, PANEL_X + 8); + drawFooter(image, win.viewport, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} ${modeHint} ${GESTURE_DOUBLE_CLICK} back`, x); } -function paintMap(image: GrayImage): void { +/** Clear space under the text of the map-mode status banner. */ +const STATUS_BANNER_GAP = 4; + +function paintMap(image: GrayImage, win: NavWindow): void { + const { padding } = win.viewport; const { width, height } = mapDimensions(); if (mapImage) { image.bitBlt(mapImage, 0, 0); drawChevron(image, width / 2, height / 2, headHeadingDeg ?? 0); } else { - image.drawText(smallFont, 24, height / 2 - 8, mapLastError ? "Map unavailable; retrying..." : "Loading map...", 170); + const text = mapLastError ? "Map unavailable; retrying..." : "Loading map..."; + image.drawText(smallFont, pageText(win.viewport).x, height / 2 - 8, text, 170); } if (statusMessage) { - image.fillRect(0, 0, width, smallFont.lineHeight + 8, 0); - image.drawText(smallFont, 16, 4, truncateText(smallFont, statusMessage, width - 32), 200); + // A banner across the map's top: the strip runs edge to edge, and its + // text keeps the padding suggested for the edges it lies along. + image.fillRect(0, 0, width, padding.top + smallFont.lineHeight + STATUS_BANNER_GAP, 0); + image.drawText(smallFont, padding.left, padding.top, + truncateText(smallFont, statusMessage, width - padding.left - padding.right), 200); } - drawFooter(image, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} destinations ${GESTURE_DOUBLE_CLICK} back`); + drawFooter(image, win.viewport, `${GESTURE_SCROLL} zoom ${GESTURE_CLICK} destinations ${GESTURE_DOUBLE_CLICK} back`); } /** @@ -1575,8 +1666,15 @@ function fillTriangle(image: GrayImage, x0: number, y0: number, x1: number, y1: } } -function drawFooter(image: GrayImage, text: string, x = 16): void { - image.drawText(smallFont, x, image.height - 22, text, 115); +/** Top of the footer hint line, which rests on the padding suggested for the window's bottom edge. */ +function footerY(viewport: Viewport): number { + return viewport.height - viewport.padding.bottom - smallFont.lineHeight; +} + +/** Draw the footer hint line from `x` (default: the left padding), cut short at the right padding. */ +function drawFooter(image: GrayImage, viewport: Viewport, text: string, x = viewport.padding.left): void { + const maxWidth = viewport.width - viewport.padding.right - x; + image.drawText(smallFont, x, footerY(viewport), truncateText(smallFont, text, maxWidth), 115); } // --------------------------------------------------------------------------- diff --git a/app/apps/nightscout/missed-bolus-notification.ts b/app/apps/nightscout/missed-bolus-notification.ts index 3b09ecf0..660f771f 100644 --- a/app/apps/nightscout/missed-bolus-notification.ts +++ b/app/apps/nightscout/missed-bolus-notification.ts @@ -8,11 +8,9 @@ import { type InputEvent } from "../../ui/gestures"; import { type Layer, type LayerContext } from "../../ui/layers"; import { lineStep } from "../../ui/metrics"; import { - NOTIFICATION_CONTENT_X, createNotificationActionMenu, drawNotificationHeader, - notificationActionMenuBox, - notificationContentWidth, + notificationDetailLayout, } from "../../ui/notifications"; import { type GlucoseRise } from "./missed-bolus-detector"; import { @@ -25,7 +23,6 @@ import { const ICON_SIZE = 24; const GRAPH_GAP = 6; -const GRAPH_BOTTOM_MARGIN = 12; const MIN_GRAPH_HEIGHT = 32; type ActionItem = { label: string; run: () => void }; @@ -56,13 +53,15 @@ export class MissedBolusNotificationLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); const step = lineStep(font); - const { width, height } = ctx.stack.getBaseSize(); - const image = new GrayImage(width, height, 0); - const contentX = NOTIFICATION_CONTENT_X; - const contentWidth = notificationContentWidth(width); + const viewport = ctx.stack.getBaseSize(); + const image = new GrayImage(viewport.width, viewport.height, 0); + // The same columns as a notification's detail view, inside the padding + // suggested for the edges of whatever hosts this (the shell's modal). + const layout = notificationDetailLayout(viewport); + const { contentX, contentWidth } = layout; const nowMs = Date.now(); - let y = drawNotificationHeader(image, font, renderIcon("nightscout", ICON_SIZE), `Nightscout ${formatRelativeTime(this.postedAtMs)}`); + let y = drawNotificationHeader(image, font, renderIcon("nightscout", ICON_SIZE), `Nightscout ${formatRelativeTime(this.postedAtMs)}`, layout); image.drawText(font, contentX, y, "Missed bolus?", 230); y += step; const minutes = Math.round((this.rise.latestMs - this.rise.onsetMs) / 60_000); @@ -90,12 +89,12 @@ export class MissedBolusNotificationLayer implements Layer { y += step; } const graphTop = y + GRAPH_GAP; - const graphHeight = height - GRAPH_BOTTOM_MARGIN - graphTop; + const graphHeight = layout.bottom - graphTop; if (graphHeight >= MIN_GRAPH_HEIGHT) { drawNightscoutGraph(image, { x: contentX, y: graphTop, width: contentWidth, height: graphHeight }, nightscout, nowMs, font); } - this.menu.paint(image, notificationActionMenuBox(width, height), ctx.stack.isFocused()); + this.menu.paint(image, layout.menuBox, ctx.stack.isFocused()); return image; } diff --git a/app/apps/nightscout/nightscout.ts b/app/apps/nightscout/nightscout.ts index 8266a31d..5ebcc902 100644 --- a/app/apps/nightscout/nightscout.ts +++ b/app/apps/nightscout/nightscout.ts @@ -30,6 +30,19 @@ export const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000; const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000; const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000; +/** + * The app page's fixed parts, placed from the title at the window's top and + * left padding: a status message and the graph, down from the title's top, + * and the status lines' column, right of the readout's left edge. + */ +const PAGE_MESSAGE_OFFSET_Y = 28; +const PAGE_GRAPH_OFFSET_Y = 112; +const PAGE_STATUS_OFFSET_X = 148; +/** Gap between the page's graph and the caption under it. */ +const PAGE_CAPTION_GAP = 6; +/** The shortest graph the page draws above its caption; with less room the graph takes the caption's. */ +const PAGE_MIN_GRAPH_HEIGHT = 40; + /** Draws the trend indicator; returns its width. */ export function drawDirectionIndicator( image: GrayImage, @@ -298,36 +311,44 @@ function drawNightscoutReferenceLine( export class NightscoutLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - // Sized to the hosting stack (the Nightscout app viewport). - const { width, height } = ctx.stack.getBaseSize(); + // Sized to the hosting stack (the Nightscout app viewport). Nothing here + // scrolls, so the page keeps inside the padding suggested for each of + // the viewport's edges: the title, the readout and the graph start at + // the left padding, and the graph and its caption end at the bottom's. + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const nightscout = nightscoutBridge.snapshot(); const nowMs = Date.now(); const step = lineStep(font); - image.drawText(font, 22, 16, "Nightscout", 220); + const left = padding.left; + const top = padding.top; + const right = width - padding.right; + const bottom = height - padding.bottom; + image.drawText(font, left, top, "Nightscout", 220); + const messageY = top + PAGE_MESSAGE_OFFSET_Y; if (!isNightscoutSettingsConfigured() || nightscout.configurationMissing) { - image.drawText(font, 22, 44, "Nightscout needs configuration.", 180); - image.drawText(font, 22, 44 + step, "Long-press for the menu, then pick Settings to set", 140); - image.drawText(font, 22, 44 + 2 * step, "the site URL and API token.", 140); + image.drawText(font, left, messageY, "Nightscout needs configuration.", 180); + image.drawText(font, left, messageY + step, "Long-press for the menu, then pick Settings to set", 140); + image.drawText(font, left, messageY + 2 * step, "the site URL and API token.", 140); return image; } if (!nightscout.available || !nightscout.latest) { - image.drawText(font, 22, 44, "No Nightscout data available.", 180); - image.drawText(font, 22, 44 + step, truncateLine(nightscout.status, 60), 140); + image.drawText(font, left, messageY, "No Nightscout data available.", 180); + image.drawText(font, left, messageY + step, truncateLine(nightscout.status, 60), 140); return image; } // Two-column header above the graph: title + current glucose on the // left, the status lines bottom-aligned on the right. - const graphTop = 128; + const graphTop = top + PAGE_GRAPH_OFFSET_Y; const latest = nightscout.latest; const glucoseText = `${latest.sgv}`; - const glucoseX = 22; + const glucoseX = left; const glucoseWidth = nightscoutLargeFont.measureText(glucoseText); // Glucose value vertically centered between the title and the graph. - const titleBottom = 16 + font.lineHeight; + const titleBottom = top + font.lineHeight; const glucoseY = Math.round((titleBottom + graphTop - nightscoutLargeFont.lineHeight) / 2); image.drawText(nightscoutLargeFont, glucoseX, glucoseY, glucoseText, 230); if (isNightscoutPointStale(latest, nowMs)) { @@ -335,8 +356,8 @@ export class NightscoutLayer implements Layer { } image.drawText(font, glucoseX + glucoseWidth + 8, glucoseY + nightscoutLargeFont.lineHeight - font.lineHeight, nightscout.units, 140); - const statusX = 170; - const statusWidth = width - 22 - statusX; + const statusX = left + PAGE_STATUS_OFFSET_X; + const statusWidth = right - statusX; const alerts = evaluateNightscoutAlerts(nightscout, loadNightscoutThresholds(), nowMs); const statusLines: { text: string; shade: number; direction?: string; fields?: { text: string; warning?: boolean }[] }[] = [ { text: `Delta ${formatDelta(nightscout.delta)} Trend `, shade: 180, direction: nightscout.direction }, @@ -382,10 +403,14 @@ export class NightscoutLayer implements Layer { statusY += step; } - const captionY = height - font.lineHeight - 6; - const graphHeight = Math.max(40, captionY - 6 - graphTop); - drawNightscoutGraph(image, { x: 22, y: graphTop, width: width - 44, height: graphHeight }, nightscout, nowMs, font); - image.drawText(font, 22, captionY, "2-hour glucose history with basal / carbs / boluses", 130); + // The graph spans the padded width, down to the caption on the last + // line above the bottom padding. A window too short for both gives the + // caption's room to the graph. + const captionY = bottom - font.lineHeight; + const captioned = captionY - PAGE_CAPTION_GAP - graphTop >= PAGE_MIN_GRAPH_HEIGHT; + const graphBottom = captioned ? captionY - PAGE_CAPTION_GAP : bottom; + drawNightscoutGraph(image, { x: left, y: graphTop, width: right - left, height: graphBottom - graphTop }, nightscout, nowMs, font); + if (captioned) image.drawText(font, left, captionY, "2-hour glucose history with basal / carbs / boluses", 130); return image; } diff --git a/app/apps/onboarding/data-collection-page.ts b/app/apps/onboarding/data-collection-page.ts index 3cce2c28..2dbbc352 100644 --- a/app/apps/onboarding/data-collection-page.ts +++ b/app/apps/onboarding/data-collection-page.ts @@ -4,11 +4,11 @@ import { dataCollectionSetting, dataCollectionSummary, type DataCollectionLevel import type { InputEvent } from "../../ui/gestures"; import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics"; +import type { Edges } from "../../ui/viewport"; import { onboardingFont } from "./onboarding-fonts"; -import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; +import { drawPageHeading, PAGE_TEXT_INSET, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; const CONTENT_GAP = 10; -const BOTTOM_MARGIN = 8; const LABEL_GAP = 24; /** None / Minimal / Full, each with a one-line summary; a tap saves the selected level. */ @@ -29,9 +29,10 @@ export class DataCollectionPage implements OnboardingPage { this.menu.select(dataCollectionSetting.values.indexOf(dataCollectionSetting.get())); } - paint(image: GrayImage, focused: boolean): void { + paint(image: GrayImage, focused: boolean, padding: Edges): void { const top = drawPageHeading( image, + padding, "Faceclaw Statistics", (global.isIOS ? "TestFlight reports are separate. " : "Google ML Kit diagnostics are separate. ") + "Choose Faceclaw statistics below. Details and controls: Settings > Privacy.", @@ -41,9 +42,14 @@ export class DataCollectionPage implements OnboardingPage { this.labelColumnWidth = Math.max(...dataCollectionSetting.values.map((level) => body.measureText(dataCollectionSetting.displayValue(level)))) + LABEL_GAP; + // The list scrolls, so it runs to the window's bottom edge, with that + // edge's padding after its last row. this.menu.paint( image, - { x: PAGE_X - 10, y: top, width: image.width - 2 * (PAGE_X - 10), height: image.height - top - BOTTOM_MARGIN }, + { + x: padding.left, y: top, width: Math.max(0, image.width - padding.left - padding.right), height: Math.max(0, image.height - top), + padBottom: padding.bottom, + }, focused, ); } @@ -68,9 +74,9 @@ export class DataCollectionPage implements OnboardingPage { private drawRow({ image, item: level, x, y, width, height, selected }: MenuDrawArgs): void { const body = onboardingFont("body"); const detail = onboardingFont("detail"); - image.drawText(body, x + 10, y + LIST_ROW_TEXT_INSET, dataCollectionSetting.displayValue(level), selected ? 255 : 200); - const summaryX = x + 10 + this.labelColumnWidth; - const summary = truncateText(detail, dataCollectionSummary(level), x + width - 10 - summaryX); + image.drawText(body, x + PAGE_TEXT_INSET, y + LIST_ROW_TEXT_INSET, dataCollectionSetting.displayValue(level), selected ? 255 : 200); + const summaryX = x + PAGE_TEXT_INSET + this.labelColumnWidth; + const summary = truncateText(detail, dataCollectionSummary(level), x + width - PAGE_TEXT_INSET - summaryX); image.drawText(detail, summaryX, y + Math.round((height - detail.lineHeight) / 2), summary, selected ? 210 : 150); } } diff --git a/app/apps/onboarding/depth-page.ts b/app/apps/onboarding/depth-page.ts index ad3b1c1d..29625240 100644 --- a/app/apps/onboarding/depth-page.ts +++ b/app/apps/onboarding/depth-page.ts @@ -3,12 +3,12 @@ import { appSwitcherPositionSetting, uiDepthSetting, type UiDepth } from "../../ import type { InputEvent } from "../../ui/gestures"; import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics"; +import type { Edges } from "../../ui/viewport"; import { layoutHasDepth } from "./layout-page"; import { onboardingFont } from "./onboarding-fonts"; -import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; +import { drawPageHeading, PAGE_TEXT_INSET, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; const CONTENT_GAP = 10; -const BOTTOM_MARGIN = 8; const COLUMN_WIDTH = 220; /** @@ -35,15 +35,21 @@ export class DepthPage implements OnboardingPage { this.menu.select(uiDepthSetting.values.indexOf(uiDepthSetting.get())); } - paint(image: GrayImage, focused: boolean): void { + paint(image: GrayImage, focused: boolean, padding: Edges): void { const top = drawPageHeading( image, + padding, "Depth", "Swipe to move the display nearer or farther away, then tap to continue. Change later in Settings > Customization.", ) + CONTENT_GAP; + // The list scrolls, so it runs to the window's bottom edge, with that + // edge's padding after its last row. this.menu.paint( image, - { x: PAGE_X - 10, y: top, width: COLUMN_WIDTH, height: image.height - top - BOTTOM_MARGIN }, + { + x: padding.left, y: top, width: Math.min(COLUMN_WIDTH, Math.max(0, image.width - padding.left - padding.right)), + height: Math.max(0, image.height - top), padBottom: padding.bottom, + }, focused, ); } @@ -66,6 +72,6 @@ export class DepthPage implements OnboardingPage { } private drawRow({ image, item: depth, x, y, selected }: MenuDrawArgs): void { - image.drawText(onboardingFont("body"), x + 10, y + LIST_ROW_TEXT_INSET, uiDepthSetting.displayValue(depth), selected ? 255 : 190); + image.drawText(onboardingFont("body"), x + PAGE_TEXT_INSET, y + LIST_ROW_TEXT_INSET, uiDepthSetting.displayValue(depth), selected ? 255 : 190); } } diff --git a/app/apps/onboarding/display-mode-page.ts b/app/apps/onboarding/display-mode-page.ts index 5f449478..464433fb 100644 --- a/app/apps/onboarding/display-mode-page.ts +++ b/app/apps/onboarding/display-mode-page.ts @@ -4,11 +4,11 @@ import { displayLayoutSetting, type DisplayLayout } from "../../ui/dashboard-set import type { InputEvent } from "../../ui/gestures"; import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics"; +import type { Edges } from "../../ui/viewport"; import { onboardingFont } from "./onboarding-fonts"; -import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; +import { drawPageHeading, PAGE_TEXT_INSET, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; const CONTENT_GAP = 10; -const BOTTOM_MARGIN = 8; const COLUMN_GAP = 16; const DESCRIPTIONS = { @@ -41,24 +41,29 @@ export class DisplayModePage implements OnboardingPage { this.menu.setItems(values, index >= 0 ? index : values.indexOf("tall")); } - paint(image: GrayImage, focused: boolean): void { + paint(image: GrayImage, focused: boolean, padding: Edges): void { const top = drawPageHeading( image, + padding, "Display mode", "Swipe to choose how much of the display windows use; it changes as you go. Tap to continue.", ) + CONTENT_GAP; const body = onboardingFont("body"); // TTF widths are fractional; the menu's row images need whole pixels. - const columnWidth = Math.ceil(20 + Math.max(...this.menu.items.map((value) => body.measureText(displayLayoutSetting.displayValue(value))))); - const menuBox = { x: PAGE_X - 10, y: top, width: columnWidth, height: image.height - top - BOTTOM_MARGIN }; + const columnWidth = Math.ceil(2 * PAGE_TEXT_INSET + + Math.max(...this.menu.items.map((value) => body.measureText(displayLayoutSetting.displayValue(value))))); + // The list scrolls, so it runs to the window's bottom edge, with that + // edge's padding after its last row. + const menuBox = { x: padding.left, y: top, width: columnWidth, height: Math.max(0, image.height - top), padBottom: padding.bottom }; this.menu.paint(image, menuBox, focused); const layout = this.menu.selectedItem; if (layout === null) return; + // The description stays put beside the list, inside the right and bottom padding. const detail = onboardingFont("detail"); const left = menuBox.x + columnWidth + COLUMN_GAP; let y = top + LIST_ROW_TEXT_INSET; - for (const line of wrapText(detail, description(layout), image.width - PAGE_X - left)) { - if (y + detail.lineHeight > image.height - BOTTOM_MARGIN) break; + for (const line of wrapText(detail, description(layout), image.width - padding.right - PAGE_TEXT_INSET - left)) { + if (y + detail.lineHeight > image.height - padding.bottom) break; image.drawText(detail, left, y, line, 170); y += detail.lineHeight; } @@ -86,7 +91,7 @@ export class DisplayModePage implements OnboardingPage { } private drawRow({ image, item: layout, x, y, selected }: MenuDrawArgs): void { - image.drawText(onboardingFont("body"), x + 10, y + LIST_ROW_TEXT_INSET, displayLayoutSetting.displayValue(layout), selected ? 255 : 190); + image.drawText(onboardingFont("body"), x + PAGE_TEXT_INSET, y + LIST_ROW_TEXT_INSET, displayLayoutSetting.displayValue(layout), selected ? 255 : 190); } } diff --git a/app/apps/onboarding/layout-page.ts b/app/apps/onboarding/layout-page.ts index 50411469..59cbbac0 100644 --- a/app/apps/onboarding/layout-page.ts +++ b/app/apps/onboarding/layout-page.ts @@ -3,12 +3,12 @@ import { appSwitcherPositionSetting, glassesTypeSetting, type AppSwitcherPositio import type { InputEvent } from "../../ui/gestures"; import { MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "../../ui/menu-core"; import { screenWidth } from "../../ui/shell/geometry"; +import type { Edges } from "../../ui/viewport"; import { AREA_OUTLINE, Diagram, diagramBandHeight, drawLayout, layoutAreaHeight } from "./layout-diagram"; import { onboardingFont } from "./onboarding-fonts"; -import { drawPageHeading, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; +import { centredX, drawPageHeading, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; const CONTENT_GAP = 10; -const BOTTOM_MARGIN = 8; /** Diagram width as a fraction of the display's: the most a diagram is drawn at. */ const DIAGRAM_SCALE = 0.4; /** Padding inside a cell's selection box, round the diagram and its caption. */ @@ -37,35 +37,39 @@ export function layoutHasDepth(position: AppSwitcherPosition): boolean { * layout, Bottom and Popup on top, the recommended one selected at first; * swipes move through them in reading order, a tap saves the selected one. * The page is immersive, so the diagrams get 40% of the display's width - * each. Choosing a layout reflows open windows (the dashboard controller - * watches the setting). + * each. The grid is centred in the window, its cells' selection boxes + * keeping inside the padding of the window's edges. Choosing a layout + * reflows open windows (the dashboard controller watches the setting). */ export class LayoutPage implements OnboardingPage { readonly immersive = true; /** Kept across visits, so going back returns to the last choice. */ private selected = LAYOUT_ORDER.indexOf(recommendedLayout()); - paint(image: GrayImage, focused: boolean): void { + paint(image: GrayImage, focused: boolean, padding: Edges): void { const top = drawPageHeading( image, + padding, "Layout", "Swipe to choose where the app switcher goes, then tap to continue.", ) + CONTENT_GAP; const title = onboardingFont("body"); const subtitle = onboardingFont("detail"); const captionHeight = CAPTION_GAP + title.lineHeight + subtitle.lineHeight; - const available = image.height - top - BOTTOM_MARGIN; + const available = image.height - top - padding.bottom; + const availableWidth = image.width - padding.left - padding.right; + // The most a diagram can take for two rows and two columns of cells to + // fit, in whole pixels, so rounding its size can't push the grid past + // the window's padding. + const maxDiagramHeight = Math.floor((available - ROW_GAP) / 2 - 2 * CELL_PADDING - captionHeight); + const maxDiagramWidth = Math.floor((availableWidth - COLUMN_GAP) / 2 - 2 * CELL_PADDING); // Short of DIAGRAM_SCALE only where two rows of cells wouldn't fit. - const scale = Math.min( - DIAGRAM_SCALE, - ((available - ROW_GAP) / 2 - 2 * CELL_PADDING - captionHeight) / layoutAreaHeight(), - ((image.width - COLUMN_GAP) / 2 - 2 * CELL_PADDING) / screenWidth(), - ); + const scale = Math.max(0, Math.min(DIAGRAM_SCALE, maxDiagramHeight / layoutAreaHeight(), maxDiagramWidth / screenWidth())); const diagramWidth = Math.round(screenWidth() * scale); const diagramHeight = Math.round(layoutAreaHeight() * scale); const cellWidth = diagramWidth + 2 * CELL_PADDING; const cellHeight = diagramHeight + 2 * CELL_PADDING + captionHeight; - const left = Math.round((image.width - 2 * cellWidth - COLUMN_GAP) / 2); + const left = centredX(image, padding, 2 * cellWidth + COLUMN_GAP); const gridTop = top + Math.max(0, Math.round((available - 2 * cellHeight - ROW_GAP) / 2)); LAYOUT_ORDER.forEach((position, index) => { const x = left + (index % 2) * (cellWidth + COLUMN_GAP); diff --git a/app/apps/onboarding/onboarding-app.ts b/app/apps/onboarding/onboarding-app.ts index fb799dc8..4a6e713f 100644 --- a/app/apps/onboarding/onboarding-app.ts +++ b/app/apps/onboarding/onboarding-app.ts @@ -84,9 +84,9 @@ export class OnboardingLayer implements Layer { } paint(ctx: LayerContext): GrayImage { - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - this.pages[this.index]!.paint(image, ctx.stack.isFocused()); + this.pages[this.index]!.paint(image, ctx.stack.isFocused(), padding); return image; } diff --git a/app/apps/onboarding/onboarding-page.ts b/app/apps/onboarding/onboarding-page.ts index 5081854d..460b3f19 100644 --- a/app/apps/onboarding/onboarding-page.ts +++ b/app/apps/onboarding/onboarding-page.ts @@ -1,6 +1,7 @@ import type { GrayImage } from "../../graphics/image"; import { wrapText } from "../../graphics/textwrap"; import type { InputEvent } from "../../ui/gestures"; +import type { Edges } from "../../ui/viewport"; import { onboardingFont } from "./onboarding-fonts"; /** Page-to-flow navigation, handed to a page's input handler. */ @@ -25,13 +26,31 @@ export interface OnboardingPage { applies?(): boolean; /** The page became current, going forward or back. */ enter?(): void; - paint(image: GrayImage, focused: boolean): void; + /** + * Paint the page over the whole image, the window's viewport. `padding` + * is the padding suggested for each of its edges (viewport.ts): none on an + * immersive page, whose edges are the screen's. + */ + paint(image: GrayImage, focused: boolean, padding: Edges): void; handleInput(event: InputEvent, nav: OnboardingNav): void | Promise; } -/** Left margin and title position shared by the pages (the UI-wide (18, 10) title spot). */ -export const PAGE_X = 18; -export const TITLE_Y = 10; +/** + * How far a page's text (its title and explanation, a list row's label) + * sits inside the side padding. A list's selection boxes start at the + * padding itself, so the title lines up with the rows' text. + */ +export const PAGE_TEXT_INSET = 10; +/** The title's drop below the top padding. */ +const TITLE_OFFSET_Y = 2; + +/** + * The x that centres something `width` wide in the window, short of + * entering either side's padding. + */ +export function centredX(image: GrayImage, padding: Edges, width: number): number { + return Math.max(padding.left, Math.min(image.width - padding.right - width, Math.round((image.width - width) / 2))); +} /** * Draw a page's title and its wrapped explanation (at most `maxLines` @@ -39,17 +58,21 @@ export const TITLE_Y = 10; */ export function drawPageHeading( image: GrayImage, + padding: Edges, title: string, explanation: string, maxLines = 2, ): number { const titleFont = onboardingFont("title"); const detailFont = onboardingFont("detail"); - image.drawText(titleFont, PAGE_X, TITLE_Y, title, 230); - let y = TITLE_Y + titleFont.lineHeight + 2; - const lines = wrapText(detailFont, explanation, image.width - 2 * PAGE_X).slice(0, maxLines); + const x = padding.left + PAGE_TEXT_INSET; + const textWidth = Math.max(0, image.width - padding.left - padding.right - 2 * PAGE_TEXT_INSET); + const titleY = padding.top + TITLE_OFFSET_Y; + image.drawText(titleFont, x, titleY, title, 230); + let y = titleY + titleFont.lineHeight + 2; + const lines = wrapText(detailFont, explanation, textWidth).slice(0, maxLines); for (const line of lines) { - image.drawText(detailFont, PAGE_X, y, line, 160); + image.drawText(detailFont, x, y, line, 160); y += detailFont.lineHeight; } return y; diff --git a/app/apps/onboarding/text-size-page.ts b/app/apps/onboarding/text-size-page.ts index 485691b7..69ff4f2b 100644 --- a/app/apps/onboarding/text-size-page.ts +++ b/app/apps/onboarding/text-size-page.ts @@ -14,11 +14,11 @@ import { FONT_SIZE_CHOICES } from "../../ui/font-picker"; import type { InputEvent } from "../../ui/gestures"; import { Menu, type MenuBox, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, listRowHeight } from "../../ui/metrics"; +import type { Edges } from "../../ui/viewport"; import { onboardingFont } from "./onboarding-fonts"; -import { drawPageHeading, PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; +import { drawPageHeading, PAGE_TEXT_INSET, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; const CONTENT_GAP = 10; -const BOTTOM_MARGIN = 8; const COLUMN_GAP = 12; const PREVIEW_PADDING = 10; const ROW_GAP = 2; @@ -75,39 +75,40 @@ export class TextSizePage implements OnboardingPage { this.face = { kind: "fixed", label: selection.kind === "bitmap" ? fontSelectionLabel(selection) : "Terminus" }; } - paint(image: GrayImage, focused: boolean): void { + paint(image: GrayImage, focused: boolean, padding: Edges): void { + // The preview's frame keeps inside the padding of the window's edges. + const right = image.width - padding.right; + const bottom = image.height - padding.bottom; if (this.face.kind === "fixed") { const top = drawPageHeading( image, + padding, "Text size", `Your UI font, ${this.face.label}, comes in one size. To choose a size, pick a different font in ` + "Settings > Customization. Tap to continue.", 3, ) + CONTENT_GAP; - drawPreview(image, getDefaultSmallFont(), { - x: PAGE_X - 10, - y: top, - width: image.width - 2 * (PAGE_X - 10), - height: image.height - top - BOTTOM_MARGIN, - }); + drawPreview(image, getDefaultSmallFont(), { x: padding.left, y: top, width: right - padding.left, height: bottom - top }); return; } const top = drawPageHeading( image, + padding, "Text size", "Swipe to choose a size, then tap to continue. The typeface and weight can be changed in Settings.", ) + CONTENT_GAP; - const height = image.height - top - BOTTOM_MARGIN; const body = onboardingFont("body"); // TTF widths are fractional; the menu's row images need whole pixels. - const columnWidth = Math.ceil(20 + Math.max(...this.face.sizes.map((size) => body.measureText(sizeLabel(size)))) + + const columnWidth = Math.ceil(2 * PAGE_TEXT_INSET + Math.max(...this.face.sizes.map((size) => body.measureText(sizeLabel(size)))) + NOTE_GAP + onboardingFont("detail").measureText(RECOMMENDED_NOTE)); - const menuBox = { x: PAGE_X - 10, y: top, width: columnWidth, height }; + // The list scrolls, so it runs to the window's bottom edge, with that + // edge's padding after its last row. + const menuBox = { x: padding.left, y: top, width: columnWidth, height: Math.max(0, image.height - top), padBottom: padding.bottom }; this.sizeMenu.paint(image, menuBox, focused); const previewX = menuBox.x + menuBox.width + COLUMN_GAP; const size = this.sizeMenu.selectedItem; const font = (size !== null ? TtfFont.load(this.face.path, size) : null) ?? getDefaultSmallFont(); - drawPreview(image, font, { x: previewX, y: top, width: image.width - (PAGE_X - 10) - previewX, height }); + drawPreview(image, font, { x: previewX, y: top, width: right - previewX, height: bottom - top }); } handleInput(event: InputEvent, nav: OnboardingNav): void { @@ -136,11 +137,11 @@ export class TextSizePage implements OnboardingPage { private drawSizeRow({ image, item: size, x, y, height, selected }: MenuDrawArgs): void { const body = onboardingFont("body"); const label = sizeLabel(size); - image.drawText(body, x + 10, y + LIST_ROW_TEXT_INSET, label, selected ? 255 : 190); + image.drawText(body, x + PAGE_TEXT_INSET, y + LIST_ROW_TEXT_INSET, label, selected ? 255 : 190); if (size !== defaultUiFontSize()) return; const detail = onboardingFont("detail"); const noteY = y + Math.round((height - detail.lineHeight) / 2); - image.drawText(detail, x + 10 + body.measureText(label) + NOTE_GAP, noteY, RECOMMENDED_NOTE, selected ? 210 : 150); + image.drawText(detail, x + PAGE_TEXT_INSET + body.measureText(label) + NOTE_GAP, noteY, RECOMMENDED_NOTE, selected ? 210 : 150); } } diff --git a/app/apps/onboarding/welcome-page.ts b/app/apps/onboarding/welcome-page.ts index 4d9c62b4..bfec82dd 100644 --- a/app/apps/onboarding/welcome-page.ts +++ b/app/apps/onboarding/welcome-page.ts @@ -2,31 +2,35 @@ import type { GrayImage, UiFont } from "../../graphics/image"; import { getFullLogo } from "../../graphics/logo"; import { wrapText } from "../../graphics/textwrap"; import type { InputEvent } from "../../ui/gestures"; +import type { Edges } from "../../ui/viewport"; import { FACECLAW_VERSION } from "../../version"; import { onboardingFont } from "./onboarding-fonts"; -import { PAGE_X, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; +import { centredX, type OnboardingNav, type OnboardingPage } from "./onboarding-page"; const LOGO_MAX_HEIGHT = 180; -const LOGO_MARGIN = 12; const LOGO_TEXT_GAP = 28; const PROMPT_GAP = 22; const PROMPT = "Tap the ring or touchpad to continue"; type TextLine = { font: UiFont; text: string; value: number; spaceBefore: number }; -/** The splash: logo beside the name, version and how to go on, centred as one group. */ +/** + * The splash: logo beside the name, version and how to go on, centred in + * the window as one group, which keeps inside the padding of its edges. + */ export class WelcomePage implements OnboardingPage { readonly immersive = true; - paint(image: GrayImage): void { + paint(image: GrayImage, _focused: boolean, padding: Edges): void { const { width, height } = image; - const logo = getFullLogo(Math.min(LOGO_MAX_HEIGHT, height - 2 * LOGO_MARGIN)); + const availableWidth = Math.max(0, width - padding.left - padding.right); + const logo = getFullLogo(Math.min(LOGO_MAX_HEIGHT, height - padding.top - padding.bottom)); const logoWidth = logo ? logo.width + LOGO_TEXT_GAP : 0; const body = onboardingFont("body"); const lines: TextLine[] = [ { font: onboardingFont("hero"), text: "Faceclaw", value: 240, spaceBefore: 0 }, { font: body, text: `Version ${FACECLAW_VERSION}`, value: 150, spaceBefore: 0 }, - ...wrapText(body, PROMPT, width - 2 * PAGE_X - logoWidth).map((text, index) => ({ + ...wrapText(body, PROMPT, availableWidth - logoWidth).map((text, index) => ({ font: body, text, value: 215, @@ -35,7 +39,7 @@ export class WelcomePage implements OnboardingPage { ]; const textWidth = Math.max(...lines.map((line) => line.font.measureText(line.text))); const textHeight = lines.reduce((total, line) => total + line.spaceBefore + line.font.lineHeight, 0); - const left = Math.max(PAGE_X, Math.round((width - logoWidth - textWidth) / 2)); + const left = centredX(image, padding, logoWidth + textWidth); if (logo) { image.bitBlt(logo, left, Math.round((height - logo.height) / 2), { transparentZero: true }); } diff --git a/app/apps/pinball/pinball-app.worker.ts b/app/apps/pinball/pinball-app.worker.ts index 8ff5a1bb..3a844d17 100644 --- a/app/apps/pinball/pinball-app.worker.ts +++ b/app/apps/pinball/pinball-app.worker.ts @@ -37,6 +37,7 @@ import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting"; import { type MenuItem } from "../../ui/menu"; import { WindowMenu } from "../../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; +import type { Viewport } from "../../ui/viewport"; import { directionalFallback, GESTURE_CLICK, @@ -250,6 +251,13 @@ type PinballWindow = { title: string; viewportWidth: number; viewportHeight: number; + /** + * The viewport as the shell last sent it: besides the size above, what each + * edge touches and the padding suggested there. The window menu's layers + * lay out by it; the table and its panel are fixed art, drawn at the + * coordinates the physics runs in (see LAYOUT_HEIGHT). + */ + viewport: Viewport; foreground: boolean; /** Whether this window is the shell's input target (pushed with each message). */ focused: boolean; @@ -314,6 +322,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { title: message.title, viewportWidth: message.viewport.width, viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, @@ -416,7 +425,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { if (!window) break; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; - window.menu?.resize({ width: window.viewportWidth, height: window.viewportHeight }); + window.viewport = message.viewport; + window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; if (window.foreground) renderAndSubmit(window, 0); break; @@ -507,7 +517,7 @@ function windowMenu(window: PinballWindow): WindowMenu { post, title: () => window.title, items: () => (window.phase === "playing" ? [] : windowMenuItems(window)), - size: { width: window.viewportWidth, height: window.viewportHeight }, + size: window.viewport, paintBase: () => paintContent(window), isFocused: () => window.focused, }); diff --git a/app/apps/roam/roam-app.worker.ts b/app/apps/roam/roam-app.worker.ts index a06be8bc..b348fba6 100644 --- a/app/apps/roam/roam-app.worker.ts +++ b/app/apps/roam/roam-app.worker.ts @@ -22,6 +22,7 @@ import type { MenuItem } from "../../ui/menu"; import { WindowMenu } from "../../ui/window-menu"; import { DatePickerLayer } from "../../ui/date-picker"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; +import type { Viewport } from "../../ui/viewport"; import type { ToolResult, ToolSpec } from "../../assistant/tool-registry"; import { GESTURE_CLICK, type InputEvent } from "../../ui/gestures"; import { @@ -29,7 +30,7 @@ import { roamGraphNameSetting, type ConfigSettingString, } from "../../ui/dashboard-settings"; -import { DocumentView } from "../../ui/document/document-view"; +import { DocumentView, type DocumentViewSize } from "../../ui/document/document-view"; import { docNodePageLink, type DocNode } from "../../ui/document/document-model"; import { createBlock, @@ -56,9 +57,18 @@ import { declare const global: any; -const HEADER_HEIGHT = 30; -const BOTTOM_MARGIN = 6; -const DOC_MARGIN = 6; +/** + * The fixed header, measured down from the padding suggested for the + * window's top edge: the rule under its title line, and its bottom, where + * the page's body starts. + */ +const HEADER_RULE_Y = 20; +const HEADER_HEIGHT = 24; +/** The header's title starts this far right of the rule's left end. */ +const TITLE_INSET = 2; +/** Message text (the body messages, the setting-edit screen): its indent from the padding at each side, and its drop below the header. */ +const MESSAGE_INSET = 12; +const MESSAGE_DROP = 16; /** Reload the shown page when it is foregrounded and older than this. */ const STALE_AFTER_MS = 60_000; @@ -70,6 +80,12 @@ type RoamWindow = { title: string; viewportWidth: number; viewportHeight: number; + /** + * The viewport as the shell last sent it: besides the size above, what each + * edge touches and the padding suggested there, which the header and the + * document are placed by (see documentBox). + */ + viewport: Viewport; foreground: boolean; focused: boolean; menu: WindowMenu | null; @@ -181,13 +197,11 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { title: message.title, viewportWidth: message.viewport.width, viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, - view: new DocumentView( - message.viewport.width - DOC_MARGIN * 2, - message.viewport.height - HEADER_HEIGHT - BOTTOM_MARGIN, - ), + view: new DocumentView(documentBox(message.viewport)), lastSubmittedFingerprint: "", }; post({ type: "set-tools", windowId: message.windowId, tools: ROAM_TOOLS }); @@ -258,7 +272,8 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { if (!window || window.windowId !== message.windowId) break; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; - window.view.resize(message.viewport.width - DOC_MARGIN * 2, message.viewport.height - HEADER_HEIGHT - BOTTOM_MARGIN); + window.viewport = message.viewport; + window.view.resize(documentBox(message.viewport)); window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; render(); @@ -357,7 +372,7 @@ function windowMenu(win: RoamWindow): WindowMenu { post, title: () => win.title, items: () => menuItems(), - size: { width: win.viewportWidth, height: win.viewportHeight }, + size: win.viewport, paintBase: () => paintContent(win), isFocused: () => win.focused, }); @@ -631,65 +646,97 @@ function paint(win: RoamWindow): Plane[] { ); } +/** Bottom of the fixed header, where the page's body starts. */ +function headerBottom(viewport: Viewport): number { + return viewport.padding.top + HEADER_HEIGHT; +} + +/** + * The document's region: from the header down to the window's bottom edge, + * between the padding suggested for its sides. The document scrolls, so the + * bottom edge's padding goes inside it, after its last line, and lines + * scrolled past use the room right up to the edge. + */ +function documentBox(viewport: Viewport): DocumentViewSize & { x: number; y: number } { + const { width, height, padding } = viewport; + const y = headerBottom(viewport); + return { + x: padding.left, + y, + width: Math.max(0, width - padding.left - padding.right), + height: Math.max(0, height - y), + padBottom: padding.bottom, + }; +} + +/** Where message text starts and how wide it may run. */ +function messageText(viewport: Viewport): { x: number; width: number } { + const { width, padding } = viewport; + return { x: padding.left + MESSAGE_INSET, width: width - padding.left - padding.right - 2 * MESSAGE_INSET }; +} + +/** + * The header's title, at the padding suggested for the window's top edge, + * and the rule under it, which spans the window between the side padding. + */ +function drawHeader(image: GrayImage, viewport: Viewport, title: string): void { + const { width, padding } = viewport; + image.drawText(smallFont, padding.left + TITLE_INSET, padding.top, title, 225); + const ruleY = padding.top + HEADER_RULE_Y; + image.drawLine(padding.left, ruleY, width - padding.right - 1, ruleY, 40); +} + /** The "type on the phone" screen shown while a Roam setting is being edited. */ function paintSettingEdit(win: RoamWindow, setting: ConfigSettingString): GrayImage { + const { height, padding } = win.viewport; const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); const step = smallFont.lineHeight + 2; - image.drawText(smallFont, DOC_MARGIN + 2, 6, setting.glassesEditTitle, 225); - image.drawLine(DOC_MARGIN, HEADER_HEIGHT - 4, win.viewportWidth - DOC_MARGIN, HEADER_HEIGHT - 4, 40); - const messageY = HEADER_HEIGHT + 16; - const lines = wrapText(smallFont, "Type the value in the phone app, or use voice input.", win.viewportWidth - DOC_MARGIN * 2 - 24); + drawHeader(image, win.viewport, setting.glassesEditTitle); + const text = messageText(win.viewport); + const messageY = headerBottom(win.viewport) + MESSAGE_DROP; + const lines = wrapText(smallFont, "Type the value in the phone app, or use voice input.", text.width); for (let index = 0; index < lines.length; index++) { - image.drawText(smallFont, DOC_MARGIN + 12, messageY + index * step, lines[index]!, 190); + image.drawText(smallFont, text.x, messageY + index * step, lines[index]!, 190); } // The live value, unmasked so the user can check what they typed. const value = setting.get(); const valueY = messageY + (lines.length + 1) * step; - image.drawText( - smallFont, - DOC_MARGIN + 12, - valueY, - truncateText(smallFont, value || "(empty)", win.viewportWidth - DOC_MARGIN * 2 - 24), - value ? 225 : 130, - ); - image.drawText(smallFont, DOC_MARGIN + 12, win.viewportHeight - smallFont.lineHeight - 12, `${GESTURE_CLICK} done`, 110); + image.drawText(smallFont, text.x, valueY, truncateText(smallFont, value || "(empty)", text.width), value ? 225 : 130); + // The hint line rests on the padding suggested for the window's bottom edge. + image.drawText(smallFont, text.x, height - padding.bottom - smallFont.lineHeight, `${GESTURE_CLICK} done`, 110); return image; } function paintContent(win: RoamWindow): GrayImage { + const { width, padding } = win.viewport; const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); const title = currentRef?.title ?? "Roam"; const status = loading ? "Loading..." : pageMissing ? "(new page)" : ""; const statusWidth = status ? smallFont.measureText(status) + 12 : 0; - image.drawText( - smallFont, - DOC_MARGIN + 2, - 6, - truncateText(smallFont, title, win.viewportWidth - DOC_MARGIN * 2 - statusWidth - 4), - 225, - ); + const titleWidth = width - padding.left - padding.right - statusWidth - 2 * TITLE_INSET; + drawHeader(image, win.viewport, truncateText(smallFont, title, titleWidth)); if (status) { - image.drawText(smallFont, win.viewportWidth - DOC_MARGIN - smallFont.measureText(status), 6, status, 130); + image.drawText(smallFont, width - padding.right - smallFont.measureText(status), padding.top, status, 130); } - image.drawLine(DOC_MARGIN, HEADER_HEIGHT - 4, win.viewportWidth - DOC_MARGIN, HEADER_HEIGHT - 4, 40); if (!isRoamConfigured()) { - drawBodyMessage(image, win, "Set the Roam graph name and API token: tap, then hold, for the menu; pick Set graph name and Set API token."); + drawBodyMessage(image, win.viewport, "Set the Roam graph name and API token: tap, then hold, for the menu; pick Set graph name and Set API token."); } else if (errorMessage) { - drawBodyMessage(image, win, `${errorMessage}\n\n${GESTURE_CLICK} retry`); + drawBodyMessage(image, win.viewport, `${errorMessage}\n\n${GESTURE_CLICK} retry`); } else if (currentPage && currentPage.children.length === 0 && !loading) { - drawBodyMessage(image, win, "Nothing here yet. Use voice input to add a todo."); + drawBodyMessage(image, win.viewport, "Nothing here yet. Use voice input to add a todo."); } else { - win.view.paint(image, DOC_MARGIN, HEADER_HEIGHT, win.focused); + const box = documentBox(win.viewport); + win.view.paint(image, box.x, box.y, win.focused); } return image; } -function drawBodyMessage(image: GrayImage, win: RoamWindow, message: string): void { - const lines = wrapText(smallFont, message, win.viewportWidth - DOC_MARGIN * 2 - 24); - let y = HEADER_HEIGHT + 16; - for (const line of lines) { - image.drawText(smallFont, DOC_MARGIN + 12, y, line, 190); +function drawBodyMessage(image: GrayImage, viewport: Viewport, message: string): void { + const text = messageText(viewport); + let y = headerBottom(viewport) + MESSAGE_DROP; + for (const line of wrapText(smallFont, message, text.width)) { + image.drawText(smallFont, text.x, y, line, 190); y += smallFont.lineHeight + 2; } } diff --git a/app/apps/t3code/t3code-app.worker.ts b/app/apps/t3code/t3code-app.worker.ts index ec2697fc..a0f998d3 100644 --- a/app/apps/t3code/t3code-app.worker.ts +++ b/app/apps/t3code/t3code-app.worker.ts @@ -43,6 +43,7 @@ import { drawSubmenuIndicator, submenuItem, type MenuItem } from "../../ui/menu" import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu"; +import type { Viewport } from "../../ui/viewport"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; import type { ToolResult, ToolSpec } from "../../assistant/tool-registry"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, type InputEvent } from "../../ui/gestures"; @@ -131,12 +132,27 @@ const VISIT_THROTTLE_MS = 10_000; /** How long a transient status ("Sent", an error) stays in a footer. */ const NOTICE_MS = 6_000; const SETTLED_SHOWN = 25; -const LIST_LEFT = 12; -const LIST_RIGHT = 20; -const TITLE_X = 18; -const TITLE_Y = 10; -const BODY_LEFT = 14; -const BODY_RIGHT = 18; +// Every screen lays out inside the padding suggested for its window's edges +// (viewport.ts); these are offsets from it. +/** + * A list screen's title: how far right of the rows' selection boxes, which + * start at the left padding (so it lines up with their text), and how far + * below the top padding. + */ +const LIST_TITLE_INSET_X = 6; +const LIST_TITLE_OFFSET_Y = 2; +/** Between a title and a status that shares its row. */ +const TITLE_STATUS_GAP = 16; +/** Below a title's line: the gap before the list's first text line, and how far above that line its selection box starts. */ +const LIST_TITLE_GAP = 8; +const LIST_LEAD = 2; +/** A list's scrollbar (Menu.drawScrollbar's bar), hugging the right padding, and the gap the rows keep from it. */ +const LIST_SCROLLBAR_WIDTH = 3; +const LIST_SCROLLBAR_GAP = 10; +/** How far a list's scrollbar track stops short of the rows' span at each end. */ +const LIST_SCROLLBAR_INSET = 2; +/** Between a list and lines of text pinned under it. */ +const LIST_FOOTER_GAP = 6; function font(): UiFont { return getDefaultSmallFont(); @@ -155,8 +171,8 @@ type Screen = type AppWindow = { windowId: string; surfaceId: string; - viewportWidth: number; - viewportHeight: number; + /** The window's size, what its edges touch and their padding, as of the last open or resize message. */ + viewport: Viewport; foreground: boolean; focused: boolean; menu: WindowMenu | null; @@ -365,8 +381,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { window = { windowId: message.windowId, surfaceId: message.surfaceId, - viewportWidth: message.viewport.width, - viewportHeight: message.viewport.height, + viewport: message.viewport, foreground: false, focused: false, menu: null, @@ -379,8 +394,9 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { break; case "resize-window": if (!window || window.windowId !== message.windowId) break; - window.viewportWidth = message.viewport.width; - window.viewportHeight = message.viewport.height; + // A new size, or the same size with different edges (so padding): + // either way every screen lays out again. + window.viewport = message.viewport; window.menu?.resize(message.viewport); window.lastSubmittedFingerprint = ""; render(); @@ -760,7 +776,7 @@ function windowMenu(win: AppWindow): WindowMenu { post, title: () => (screen.kind === "thread" ? threadTitle() : APP_TITLE), items: () => menuItems(), - size: { width: win.viewportWidth, height: win.viewportHeight }, + size: win.viewport, paintBase: () => paintContent(win), isFocused: () => win.focused, }); @@ -1886,49 +1902,110 @@ function listStatusLine(): string { return glanceSummary({ needsYou: needs, working }); } +/** + * Paint a list screen's title, and a dim status beside it, inside the + * window's top padding. Returns the title's x (other text on the screen + * lines up with it) and the y of the first text line below it. + */ +function paintListTitle(image: GrayImage, viewport: Viewport, title: string, status: string): { x: number; listTop: number } { + const f = font(); + const { width, padding } = viewport; + const x = padding.left + LIST_TITLE_INSET_X; + const y = padding.top + LIST_TITLE_OFFSET_Y; + image.drawText(f, x, y, title, 220); + if (status) { + const statusX = x + f.measureText(title) + TITLE_STATUS_GAP; + image.drawText(f, statusX, y, truncateText(f, status, width - padding.right - statusX), 150); + } + return { x, listTop: y + lineStep(f) + LIST_TITLE_GAP }; +} + +/** Where a list's scrollbar starts: it hugs the window's right padding. */ +function listScrollbarX(viewport: Viewport): number { + return viewport.width - viewport.padding.right - LIST_SCROLLBAR_WIDTH; +} + +/** Width of a list's rows, from the window's left padding to the scrollbar's gutter. */ +function listRowsWidth(viewport: Viewport): number { + return Math.max(0, listScrollbarX(viewport) - LIST_SCROLLBAR_GAP - viewport.padding.left); +} + +/** + * Paint the list from just above the text line at `listTop` down to the + * window's bottom edge, or to above the text pinned under it at `footerTop`. + * The list scrolls, so at the window's edge it takes that edge's padding + * inside it, after its last row: the row rests clear of a border there, and + * rows scrolled past the end are cut off at the edge itself. + */ +function paintListRows(win: AppWindow, image: GrayImage, menu: Menu, listTop: number, footerTop?: number): void { + const { height, padding } = win.viewport; + const toEdge = footerTop === undefined; + const boxTop = listTop - LIST_LEAD; + const boxBottom = toEdge ? height : footerTop - LIST_FOOTER_GAP; + // Where the last row ends once scrolled to it. + const rowsBottom = toEdge ? height - padding.bottom : boxBottom; + menu.paint( + image, + { x: padding.left, y: boxTop, width: listRowsWidth(win.viewport), height: Math.max(0, boxBottom - boxTop), padBottom: toEdge ? padding.bottom : 0 }, + win.focused, + ); + const trackTop = boxTop + LIST_SCROLLBAR_INSET; + menu.drawScrollbar(image, listScrollbarX(win.viewport), trackTop, Math.max(0, rowsBottom - LIST_SCROLLBAR_INSET - trackTop)); +} + function paintList(win: AppWindow, mode: "list" | "environments"): GrayImage { - const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); + const { width, height, padding } = win.viewport; + const image = new GrayImage(width, height, 0); const f = font(); const step = lineStep(f); const title = mode === "environments" ? "T3 Code - Environments" : APP_TITLE; - image.drawText(f, TITLE_X, TITLE_Y, title, 220); const status = mode === "list" ? listStatusLine() : notice?.text ?? ""; - if (status) { - const statusX = TITLE_X + f.measureText(title) + 16; - image.drawText(f, statusX, TITLE_Y, truncateText(f, status, win.viewportWidth - statusX - 14), 150); - } - let listTop = TITLE_Y + step + 8; + const header = paintListTitle(image, win.viewport, title, status); + let listTop = header.listTop; if (mode === "list" && clients.size === 0) { const help = wrapText( f, "Run `t3 pair` on the computer running T3 Code (serve it on your network with `t3 serve --host 0.0.0.0`, or use Tailscale), then pick Pair below and paste the link into the phone app.", - win.viewportWidth - TITLE_X * 2, + width - padding.right - header.x, ); - help.forEach((line, index) => image.drawText(f, TITLE_X, listTop + index * step, line, 160)); + help.forEach((line, index) => image.drawText(f, header.x, listTop + index * step, line, 160)); listTop += help.length * step + 8; } const items = mode === "environments" ? environmentItems() : listItems(); const menu = list(); menu.setItems(items, selectionIndex(items)); listSelectionKey = menu.selectedItem?.key ?? listSelectionKey; - const listHeight = win.viewportHeight - 6 - listTop; - menu.paint(image, { x: LIST_LEFT, y: listTop - 2, width: win.viewportWidth - LIST_LEFT - LIST_RIGHT, height: listHeight }, win.focused); - menu.drawScrollbar(image, win.viewportWidth - 10, listTop, listHeight - 4); + paintListRows(win, image, menu, listTop); return image; } -const THREAD_HEADER_HEIGHT = 28; +/** The thread screen's header, below its title's top: the rule under the title, and the transcript's first line. */ +const THREAD_RULE_OFFSET = 18; +const THREAD_BODY_OFFSET = 26; +/** How far the thread screen's text sits inside the ends of its header rule. */ +const THREAD_TEXT_INSET = 2; +/** The transcript's scrollbar, hugging the window's right padding, and the gap the header rule keeps from it. */ +const THREAD_SCROLLBAR_WIDTH = 2; +const THREAD_SCROLLBAR_GAP = 8; +/** Between the transcript and the footer under it. */ +const THREAD_FOOTER_GAP = 4; +/** The transcript's top: its header stays put above it, inside the window's top padding. */ function threadBodyTop(): number { - return THREAD_HEADER_HEIGHT + 4; + return (window?.viewport.padding.top ?? 0) + THREAD_BODY_OFFSET; } -/** Body height above the footer (hint line, plus a plan line when there is one). */ +/** + * Body height above the footer (hint line, plus a plan line when there is + * one), whose last line rests on the window's bottom padding. + */ function threadBodyHeight(): number { if (!window) return 0; - const step = lineStep(font()); + const f = font(); + const { height, padding } = window.viewport; const footerLines = threadView?.handle.model.todoList() && currentShellThread() && threadStatus(currentShellThread()!) === "working" ? 2 : 1; - return window.viewportHeight - threadBodyTop() - footerLines * step - 8; + const footerTop = height - padding.bottom - f.lineHeight - (footerLines - 1) * lineStep(f); + return footerTop - THREAD_FOOTER_GAP - threadBodyTop(); } function threadFooterHint(): { text: string; value: number } { @@ -1948,18 +2025,28 @@ function threadFooterHint(): { text: string; value: number } { } function paintThread(win: AppWindow): GrayImage { - const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); + const { width: windowWidth, height, padding } = win.viewport; + const image = new GrayImage(windowWidth, height, 0); const view = threadView; const f = font(); const step = lineStep(f); - const width = win.viewportWidth - BODY_LEFT - BODY_RIGHT; + // The header rule runs from the window's left padding to the gutter kept + // for the scrollbar, which hugs the right padding; text sits just inside + // the rule's ends. + const scrollbarX = windowWidth - padding.right - THREAD_SCROLLBAR_WIDTH; + const ruleLeft = padding.left; + const ruleRight = scrollbarX - THREAD_SCROLLBAR_GAP; + const left = ruleLeft + THREAD_TEXT_INSET; + const right = ruleRight - THREAD_TEXT_INSET; + const width = Math.max(0, right - left); if (!view) return image; const shell = currentShellThread(); const status = shell ? threadStatusLabel(threadStatus(shell)) : ""; const statusWidth = status ? f.measureText(status) + 12 : 0; - image.drawText(f, BODY_LEFT, 6, truncateText(f, threadTitle(), width - statusWidth), 225); - if (status) image.drawText(f, win.viewportWidth - BODY_RIGHT - f.measureText(status), 6, status, 150); - image.drawLine(BODY_LEFT - 2, THREAD_HEADER_HEIGHT - 4, win.viewportWidth - BODY_RIGHT + 2, THREAD_HEADER_HEIGHT - 4, 40); + const titleY = padding.top; + image.drawText(f, left, titleY, truncateText(f, threadTitle(), width - statusWidth), 225); + if (status) image.drawText(f, right - f.measureText(status), titleY, status, 150); + image.drawLine(ruleLeft, titleY + THREAD_RULE_OFFSET, ruleRight, titleY + THREAD_RULE_OFFSET, 40); const model = view.handle.model; const bodyTop = threadBodyTop(); @@ -1967,9 +2054,9 @@ function paintThread(win: AppWindow): GrayImage { const visible = Math.max(1, Math.floor(bodyHeight / step)); if (model.sequence === null) { const client = clients.get(view.envId); - image.drawText(f, BODY_LEFT, bodyTop, client?.ready ? "Loading…" : client?.status || "Not connected", 150); + image.drawText(f, left, bodyTop, client?.ready ? "Loading…" : client?.status || "Not connected", 150); } else if (model.deleted) { - image.drawText(f, BODY_LEFT, bodyTop, "This thread was deleted.", 150); + image.drawText(f, left, bodyTop, "This thread was deleted.", 150); } else { const lines = view.layout.layout(timelineEntries(model), f, width); if (model.hasMoreHistory) lines.unshift({ text: "(earlier messages on the computer)", value: 90, indent: 0 }, { text: "", value: 0, indent: 0 }); @@ -1978,21 +2065,22 @@ function paintThread(win: AppWindow): GrayImage { const first = view.firstLine ?? view.maxFirstLine; for (let row = 0; row < visible; row++) { const line = lines[first + row]; - if (line?.text) image.drawText(f, BODY_LEFT + line.indent, bodyTop + row * step, line.text, line.value); + if (line?.text) image.drawText(f, left + line.indent, bodyTop + row * step, line.text, line.value); } if (view.maxFirstLine > 0) { const track = visible * step; const thumb = Math.max(8, Math.floor((track * visible) / lines.length)); - image.fillRect(win.viewportWidth - 8, bodyTop + Math.floor(((track - thumb) * first) / view.maxFirstLine), 2, thumb, 110); + image.fillRect(scrollbarX, bodyTop + Math.floor(((track - thumb) * first) / view.maxFirstLine), THREAD_SCROLLBAR_WIDTH, thumb, 110); } } - // Footer: the agent's plan progress while it works, then the hint/status line. - let footerY = win.viewportHeight - step - 4; + // Footer: the agent's plan progress while it works, then the hint/status + // line, resting on the window's bottom padding. + let footerY = height - padding.bottom - f.lineHeight; const hint = threadFooterHint(); if (hint.text) { const text = truncateText(f, hint.text, width); - image.drawText(f, win.viewportWidth - BODY_RIGHT - f.measureText(text), footerY, text, hint.value); + image.drawText(f, right - f.measureText(text), footerY, text, hint.value); } const todo = model.todoList(); if (todo && shell && threadStatus(shell) === "working") { @@ -2001,28 +2089,38 @@ function paintThread(win: AppWindow): GrayImage { const done = steps.filter((entry) => entry.status === "completed").length; const current = steps.find((entry) => entry.status === "running") ?? steps.find((entry) => entry.status === "pending"); const text = `Plan ${done}/${steps.length}${current ? ` · ${plainMarkdown(current.text)}` : ""}`; - image.drawText(f, BODY_LEFT, footerY, truncateText(f, text, width), 160); + image.drawText(f, left, footerY, truncateText(f, text, width), 160); } return image; } +/** + * A screen of text: a title, paragraphs wrapped to the window, and a hint + * line. With no rows to line up with, the text sits right at the window's + * padding on every side. + */ function paintMessageScreen(win: AppWindow, title: string, paragraphs: Array<{ text: string; value: number }>, footer: string): GrayImage { - const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); + const { width: windowWidth, height, padding } = win.viewport; + const image = new GrayImage(windowWidth, height, 0); const f = font(); const step = lineStep(f); - const width = win.viewportWidth - TITLE_X * 2; - image.drawText(f, TITLE_X, TITLE_Y, title, 220); - let y = TITLE_Y + step + 10; + const x = padding.left; + const width = windowWidth - padding.left - padding.right; + image.drawText(f, x, padding.top, title, 220); + let y = padding.top + step + 10; + const footerY = height - padding.bottom - f.lineHeight; + // Paragraph lines stop above the hint line, or at the bottom padding when there is none. + const bodyBottom = footer ? footerY : height - padding.bottom; for (const paragraph of paragraphs) { if (!paragraph.text) continue; for (const line of wrapText(f, paragraph.text, width)) { - if (y > win.viewportHeight - 2 * step) break; - image.drawText(f, TITLE_X, y, line, paragraph.value); + if (y + f.lineHeight > bodyBottom) break; + image.drawText(f, x, y, line, paragraph.value); y += step; } y += 6; } - if (footer) image.drawText(f, TITLE_X, win.viewportHeight - step - 6, footer, 110); + if (footer) image.drawText(f, x, footerY, footer, 110); return image; } @@ -2050,32 +2148,26 @@ function paintPair(win: AppWindow): GrayImage { function paintCompose(win: AppWindow): GrayImage { const target = composeTarget(); if (!target) return paintMessageScreen(win, "New thread", [{ text: "That project is no longer available.", value: 170 }], ""); - const image = new GrayImage(win.viewportWidth, win.viewportHeight, 0); + const { width, height, padding } = win.viewport; + const image = new GrayImage(width, height, 0); const f = font(); const step = lineStep(f); - const title = "New thread"; - image.drawText(f, TITLE_X, TITLE_Y, title, 220); - const status = busyText || notice?.text || ""; - if (status) { - const statusX = TITLE_X + f.measureText(title) + 16; - image.drawText(f, statusX, TITLE_Y, truncateText(f, status, win.viewportWidth - statusX - 14), 150); - } - // Below the list: an error, or why nothing can start here. + const header = paintListTitle(image, win.viewport, "New thread", busyText || notice?.text || ""); + // Below the list: an error, or why nothing can start here. Its lines stay + // put, the last resting on the window's bottom padding, and the list ends + // above them. const resolved = composeResolved(target); const problem = screenError || (resolved.model ? "" : (resolved.blocker ?? "")); - const problemLines = problem ? wrapText(f, problem, win.viewportWidth - TITLE_X * 2).slice(0, 3) : []; - const listTop = TITLE_Y + step + 8; - const listWidth = win.viewportWidth - LIST_LEFT - LIST_RIGHT; - const listHeight = win.viewportHeight - 6 - listTop - (problemLines.length ? problemLines.length * step + 6 : 0); + const problemLines = problem ? wrapText(f, problem, width - padding.right - header.x).slice(0, 3) : []; + const problemTop = height - padding.bottom - f.lineHeight - (problemLines.length - 1) * step; const items = composeItems(); const widest = Math.max(0, ...items.filter((item) => item.kind === "field").map((item) => f.measureText(item.label))); - composeValueColumn = Math.min(widest + 16, Math.floor(listWidth * 0.4)); + composeValueColumn = Math.min(widest + 16, Math.floor(listRowsWidth(win.viewport) * 0.4)); const menu = list(); menu.setItems(items, selectionIndex(items)); composeSelectionKey = menu.selectedItem?.key ?? composeSelectionKey; - menu.paint(image, { x: LIST_LEFT, y: listTop - 2, width: listWidth, height: listHeight }, win.focused); - menu.drawScrollbar(image, win.viewportWidth - 10, listTop, listHeight - 4); - problemLines.forEach((line, index) => image.drawText(f, TITLE_X, listTop + listHeight + 4 + index * step, line, 230)); + paintListRows(win, image, menu, header.listTop, problemLines.length ? problemTop : undefined); + problemLines.forEach((line, index) => image.drawText(f, header.x, problemTop + index * step, line, 230)); return image; } diff --git a/app/apps/teleprompter/reader.ts b/app/apps/teleprompter/reader.ts index a3c39205..3c20da16 100644 --- a/app/apps/teleprompter/reader.ts +++ b/app/apps/teleprompter/reader.ts @@ -9,8 +9,6 @@ import { lineStep } from "../../ui/metrics"; import { clamp } from "../../util/numeric-util"; import { ScriptTracker, tokenizeScript, type TokenizedScript } from "./script-tracker"; -const MARGIN_X = 18; -const BODY_TOP = 6; /** Gap between the last body line and the footer. */ const FOOTER_GAP = 4; /** Lines per ring scroll tick. */ @@ -41,6 +39,11 @@ export type ReaderHooks = { * and re-anchor the tracker to what is now at the anchor row, so scrolling * doubles as "I'm here now". Click pauses/resumes tracking; double-click * goes back to the file list. + * + * The script's column and the footer line keep inside the padding suggested + * for the window's edges. The lines scroll, so they run from the window's + * top edge itself: only the script's first line keeps that edge's padding + * above it. */ export class TeleprompterReaderLayer implements Layer { private readonly script: TokenizedScript; @@ -116,13 +119,18 @@ export class TeleprompterReaderLayer implements Layer { const font = getDefaultMediumFont(); const smallFont = getDefaultSmallFont(); const step = lineStep(font); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); + const bodyX = padding.left; + const bodyWidth = Math.max(0, width - padding.left - padding.right); - const footerY = height - smallFont.lineHeight - 4; - this.visibleRows = Math.max(1, Math.floor((footerY - FOOTER_GAP - BODY_TOP) / step)); + const footerY = height - padding.bottom - smallFont.lineHeight; + const bodyBottom = footerY - FOOTER_GAP; + // Rows that fit wherever the script is scrolled to, which is what + // scrolling and the anchor row go by. + this.visibleRows = Math.max(1, Math.floor((bodyBottom - padding.top) / step)); this.anchorRow = Math.floor(this.visibleRows / 3); - const lines = this.layout(font, width - 2 * MARGIN_X); + const lines = this.layout(font, bodyWidth); if (this.autoScrollPending) { this.autoScrollPending = false; @@ -133,20 +141,24 @@ export class TeleprompterReaderLayer implements Layer { const position = this.tracker.position; const nextToken = position < this.script.words.length ? this.script.wordTokens[position]! : this.script.tokens.length; - const lastLine = Math.min(lines.length, this.firstLine + this.visibleRows); + // Scrolled past the script's start, the lines begin at the window's top + // edge and the top padding's room may hold one more of them. + const bodyTop = this.firstLine === 0 ? padding.top : 0; + const shownRows = Math.max(1, Math.floor((bodyBottom - bodyTop) / step)); + const lastLine = Math.min(lines.length, this.firstLine + shownRows); for (let lineIndex = this.firstLine; lineIndex < lastLine; lineIndex++) { - const y = BODY_TOP + (lineIndex - this.firstLine) * step; + const y = bodyTop + (lineIndex - this.firstLine) * step; for (const span of lines[lineIndex]!.spans) { const isNext = span.tokenIndex === nextToken; const value = isNext ? NEXT_VALUE : span.tokenIndex < nextToken ? PAST_VALUE : UPCOMING_VALUE; - image.drawText(font, MARGIN_X + span.x, y, span.text, value); + image.drawText(font, bodyX + span.x, y, span.text, value); if (isNext) { - image.fillRect(MARGIN_X + span.x, y + font.lineHeight - 2, span.width, 2, NEXT_VALUE); + image.fillRect(bodyX + span.x, y + font.lineHeight - 2, span.width, 2, NEXT_VALUE); } } } - this.paintFooter(image, smallFont, width, footerY); + this.paintFooter(image, smallFont, bodyX, bodyWidth, footerY); return image; } @@ -169,15 +181,16 @@ export class TeleprompterReaderLayer implements Layer { } } - private paintFooter(image: GrayImage, font: UiFont, width: number, y: number): void { + /** The footer line across [x, x + width): the script's name at its left, status and progress at its right. */ + private paintFooter(image: GrayImage, font: UiFont, x: number, width: number, y: number): void { const total = this.script.words.length; const percent = total === 0 ? 0 : Math.round((100 * this.tracker.position) / total); const right = `${this.tracking ? this.status || "Listening" : "Paused"} ${percent}%`; const rightWidth = font.measureText(right); - image.drawText(font, width - MARGIN_X - rightWidth, y, right, 110); - const titleMax = width - 2 * MARGIN_X - rightWidth - 12; + image.drawText(font, x + width - rightWidth, y, right, 110); + const titleMax = width - rightWidth - 12; if (titleMax > 20) { - image.drawText(font, MARGIN_X, y, truncateText(font, this.title, titleMax), 110); + image.drawText(font, x, y, truncateText(font, this.title, titleMax), 110); } } diff --git a/app/apps/teleprompter/teleprompter-app.ts b/app/apps/teleprompter/teleprompter-app.ts index c615297b..ee9f8150 100644 --- a/app/apps/teleprompter/teleprompter-app.ts +++ b/app/apps/teleprompter/teleprompter-app.ts @@ -4,7 +4,6 @@ import { readTextFile, type DirectoryEntry } from "../../native/file-access"; import { LIST_ROW_TEXT_INSET } from "../../ui/metrics"; import { MenuLayer, type MenuItem } from "../../ui/menu"; import { type Layer, type LayerContext } from "../../ui/layers"; -import { appViewportSize } from "../../ui/shell/geometry"; import { createInProcessWindow, YieldAtRootLayer, @@ -28,15 +27,7 @@ export const TELEPROMPTER_SURFACE_ID = "window:teleprompter"; const SCRIPT_FILE = /\.(txt|md|markdown|text|log)$/i; -const HOME_LAYOUT = { - x: 8, - y: 8, - width: 272, - showBorder: false, - minHeight: 0, - maxHeight: appViewportSize("min").height - 16, - opaque: true, -}; +const HOME_LAYOUT = { page: true, width: 248 } as const; export type TeleprompterAppOptions = InProcessAppOptions & { /** Hold continuous mic capture while a script is being tracked. */ diff --git a/app/apps/terminal/hub-header.ts b/app/apps/terminal/hub-header.ts index c3fbbe7a..b3eefd3f 100644 --- a/app/apps/terminal/hub-header.ts +++ b/app/apps/terminal/hub-header.ts @@ -1,14 +1,41 @@ import { wrapText, type WrapFont } from '../../graphics/textwrap'; +import type { Edges } from '../../ui/viewport'; -/** Left edge of the hub's title line (and of a wrapped status block under it). */ -export const HUB_TITLE_X = 12; +/** + * Where the hub's title line (and a wrapped status block under it) starts, + * right of the window's left padding: the list's selection boxes start at + * the padding and unindented row text sits this far inside its box, so the + * title lines up with it. + */ +export const HUB_TITLE_INSET_X = 6; +/** The title line's top, below the window's top padding. */ +const HUB_TITLE_OFFSET_Y = 2; +/** Between the title and a status that shares its row. */ +const HUB_STATUS_GAP = 16; +/** Between the header's last line and the list's first text line. */ +const HUB_LIST_GAP = 6; -/** Keep short status on the title row; give long errors their full width. */ -export function layoutHubHeader(font: WrapFont, title: string, status: string, width: number, step: number) { - const inlineX = HUB_TITLE_X + font.measureText(title) + 16; - const inline = !/[\r\n]/.test(status) && font.measureText(status) <= Math.max(0, width - inlineX - 12); - const lines = inline ? [status] : wrapText(font, status, Math.max(1, width - HUB_TITLE_X - 12)); - const x = inline ? inlineX : HUB_TITLE_X; - const y = inline ? 10 : 10 + step; - return { x, y, lines, listTop: inline ? 16 + step : 16 + step * (1 + lines.length) }; +/** + * Lay the hub's header out in a window `width` wide with the suggested + * `padding` (viewport.ts): the title inside the top and left padding, the + * status ending at the right padding. Keep short status on the title row; + * give long errors their full width. + */ +export function layoutHubHeader( + font: WrapFont, + title: string, + status: string, + viewport: { width: number; padding: Edges }, + step: number, +) { + const { width, padding } = viewport; + const titleX = padding.left + HUB_TITLE_INSET_X; + const titleY = padding.top + HUB_TITLE_OFFSET_Y; + const right = width - padding.right; + const inlineX = titleX + font.measureText(title) + HUB_STATUS_GAP; + const inline = !/[\r\n]/.test(status) && font.measureText(status) <= Math.max(0, right - inlineX); + const lines = inline ? [status] : wrapText(font, status, Math.max(1, right - titleX)); + const x = inline ? inlineX : titleX; + const y = inline ? titleY : titleY + step; + return { titleX, titleY, x, y, lines, listTop: titleY + HUB_LIST_GAP + step * (inline ? 1 : 1 + lines.length) }; } diff --git a/app/apps/terminal/terminal-app.worker.ts b/app/apps/terminal/terminal-app.worker.ts index 0f32c5d3..622c3aea 100644 --- a/app/apps/terminal/terminal-app.worker.ts +++ b/app/apps/terminal/terminal-app.worker.ts @@ -45,7 +45,7 @@ import { GrayImage, type UiFont } from "../../graphics/image"; import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane"; import { prepareFrameDraws } from "../../graphics/glyph-wire"; import { getDefaultSmallFont, getTerminalFontConfig } from "../../graphics/ui-fonts"; -import { HUB_TITLE_X, layoutHubHeader } from "./hub-header"; +import { HUB_TITLE_INSET_X, layoutHubHeader } from "./hub-header"; import { TERMINAL_ICON_GLYPHS, type IconActivity } from "../../graphics/icons"; import { drawSessionRow, @@ -76,6 +76,7 @@ import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { centeredTextY, lineStep, listRowHeight, textInkHeight } from "../../ui/metrics"; import { WindowMenu, WindowMenuLayer } from "../../ui/window-menu"; import { appViewportSize, type WindowHeightMode } from "../../ui/shell/geometry"; +import type { EdgeKind, Edges, Viewport } from "../../ui/viewport"; import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-window"; import type { ToolResult, ToolSpec } from "../../assistant/tool-registry"; @@ -123,6 +124,13 @@ type BaseWindow = { /** Content viewport from the shell's open-window message; grid = viewport / cell. */ viewportWidth: number; viewportHeight: number; + /** + * What the viewport's edges touch and the padding suggested for them + * (viewport.ts), as of the last open or resize message. The hub's pages lay + * out inside the padding; a session's grid fills the window regardless. + */ + edges: Edges; + padding: Edges; gridCols: number; gridRows: number; foreground: boolean; @@ -216,8 +224,6 @@ type PendingView = { options: G2MirrorClientOptions; }; -type Viewport = { width: number; height: number }; - const windows = new Map(); const pendingViews = new Map(); // Windows waiting on the shell's answer to a set-height-mode, by window id: @@ -364,11 +370,22 @@ global.onmessage = (event: { data: WorkerAppMessage }) => { break; } const window = windows.get(message.windowId); - if (!window || (window.viewportWidth === message.viewport.width && window.viewportHeight === message.viewport.height)) break; + if (!window) break; + const resized = window.viewportWidth !== message.viewport.width || window.viewportHeight !== message.viewport.height; window.viewportWidth = message.viewport.width; window.viewportHeight = message.viewport.height; + window.edges = message.viewport.edges; + window.padding = message.viewport.padding; window.menu?.resize(message.viewport); - window.lastSubmittedFingerprint = ""; + // A resize at the same size says what the window's edges touch has + // changed. The hub's pages move with their padding; a session's grid + // does not, so it keeps its connection, and only a menu page open over + // it (which lays out in the padding) has anything to repaint. + if (window.kind === "view" && !resized) { + if (window.menu?.isOpen()) scheduleRender(window); + break; + } + if (resized) window.lastSubmittedFingerprint = ""; if (window.kind === "view") { window.gridCols = Math.floor(window.viewportWidth / window.cellWidth); window.gridRows = Math.floor(window.viewportHeight / window.cellHeight); @@ -531,7 +548,7 @@ function cancelPendingReconnects(): void { } } -function openWindow(windowId: string, surfaceId: string, title: string, viewport: { width: number; height: number }): void { +function openWindow(windowId: string, surfaceId: string, title: string, viewport: Viewport): void { const pendingView = pendingViews.get(windowId); if (pendingView) { pendingViews.delete(windowId); @@ -548,6 +565,8 @@ function openWindow(windowId: string, surfaceId: string, title: string, viewport title, viewportWidth: viewport.width, viewportHeight: viewport.height, + edges: viewport.edges, + padding: viewport.padding, gridCols: Math.floor(viewport.width / hubCell.cellWidth), gridRows: Math.floor(viewport.height / hubCell.cellHeight), foreground: false, @@ -914,7 +933,7 @@ function createViewWindow( windowId: string, surfaceId: string, title: string, - viewport: { width: number; height: number }, + viewport: Viewport, view: PendingView, ): ViewWindow { const { connectionId, socket, label, glyph } = view; @@ -933,6 +952,8 @@ function createViewWindow( title, viewportWidth: viewport.width, viewportHeight: viewport.height, + edges: viewport.edges, + padding: viewport.padding, gridCols, gridRows, font: cell.font, @@ -1067,7 +1088,7 @@ function windowMenu(window: TerminalWindow): WindowMenu { post, title: () => window.title, items: () => windowMenuItems(window), - size: { width: window.viewportWidth, height: window.viewportHeight }, + size: { width: window.viewportWidth, height: window.viewportHeight, edges: window.edges, padding: window.padding }, paintBase: () => paintContent(window), isFocused: () => window.focused, }); @@ -1308,12 +1329,20 @@ type HubItem = { /** Vertical offset of a row's text line from the top of its selection box. */ const HUB_ROW_TEXT_INSET = 4; -/** Left edge of the list's selection boxes. */ -const HUB_LIST_LEFT = 12; -/** Right inset of the selection boxes, clear of the scrollbar. */ -const HUB_LIST_RIGHT = 20; -/** Left padding of unindented (heading and top-level) row text inside its selection box. */ -const HUB_TOP_LEVEL_TEXT_X = 6; +/** How far the first selection box starts above the list's first text line. */ +const HUB_LIST_LEAD = 2; +/** The list's scrollbar (Menu.drawScrollbar's bar), and the gap the selection boxes keep from it. */ +const HUB_SCROLLBAR_WIDTH = 3; +const HUB_SCROLLBAR_GAP = 10; +/** How far the scrollbar's track stops short of the rows' span at each end. */ +const HUB_SCROLLBAR_INSET = 2; +/** + * Left padding of unindented (heading and top-level) row text inside its + * selection box; the hub's title starts in the same column. + */ +const HUB_TOP_LEVEL_TEXT_X = HUB_TITLE_INSET_X; +/** A page of text's body, indented under its title. */ +const PAGE_BODY_INDENT = 6; /** The hub window's list, created on first use so openWindow stays free of paint dependencies. */ function hubMenu(window: HubWindow): Menu { @@ -1725,6 +1754,8 @@ function returnToHub(view: ViewWindow, then?: () => void): void { if (activeViewId === view.windowId) activeViewId = null; hub.viewportWidth = viewport.width; hub.viewportHeight = viewport.height; + hub.edges = viewport.edges; + hub.padding = viewport.padding; hub.menu?.resize(viewport); const items = hubItems(hub); const row = items.findIndex( @@ -1833,54 +1864,71 @@ function paintHub(window: HubWindow): GrayImage { if (window.mode === "add") { return paintAddConnection(window); } - const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0); + const { viewportWidth: width, viewportHeight: height, padding } = window; + const image = new GrayImage(width, height, 0); const font = chromeFont(); const step = lineStep(font); - // No border box: the shell chrome (top bar + sidebar) already frames the app. - // Long statuses get a wrapped block and move the list below it. + // The header stays put inside the window's top padding, its title in line + // with the rows' text. Long statuses get a wrapped block and move the list + // below it. const title = window.mode === "connections" ? "Terminal - Connections" : "Terminal"; - image.drawText(font, HUB_TITLE_X, 10, title, 220); - const header = layoutHubHeader(font, title, hubStatusLine(window), window.viewportWidth, step); + const header = layoutHubHeader(font, title, hubStatusLine(window), { width, padding }, step); + image.drawText(font, header.titleX, header.titleY, title, 220); header.lines.forEach((line, index) => image.drawText(font, header.x, header.y + index * step, line, 170)); let listTop = header.listTop; if (window.mode === "sessions" && controls.size === 0) { - image.drawText(font, HUB_TITLE_X + 6, listTop, "Add a g2mirror:// connection to get started, see:", 150); - image.drawText(font, HUB_TITLE_X + 6, listTop + step, "https://github.com/jimrandomh/g2mirror", 190); + image.drawText(font, header.titleX, listTop, "Add a g2mirror:// connection to get started, see:", 150); + image.drawText(font, header.titleX, listTop + step, "https://github.com/jimrandomh/g2mirror", 190); listTop += 2 * step + 6; } const list = hubMenu(window); list.setItems(hubItems(window)); - // Selection boxes start above the text line; the menu fills the selected - // box only while this window has focus (an outline alone means the sidebar - // owns input), matching the shell convention. - const listHeight = window.viewportHeight - 6 - listTop; + // Selection boxes start at the left padding, above the text line; the menu + // fills the selected box only while this window has focus (an outline alone + // means the sidebar owns input), matching the shell convention. The list + // scrolls, so under the header it runs to the window's bottom edge with + // that edge's padding inside it, after the last row: rows scrolled past the + // end are cut off at the edge itself. The scrollbar hugs the right padding. + const boxTop = listTop - HUB_LIST_LEAD; + const scrollbarX = width - padding.right - HUB_SCROLLBAR_WIDTH; list.paint( image, - { x: HUB_LIST_LEFT, y: listTop - 2, width: window.viewportWidth - HUB_LIST_LEFT - HUB_LIST_RIGHT, height: listHeight }, + { + x: padding.left, + y: boxTop, + width: Math.max(0, scrollbarX - HUB_SCROLLBAR_GAP - padding.left), + height: Math.max(0, height - boxTop), + padBottom: padding.bottom, + }, window.focused, ); - list.drawScrollbar(image, window.viewportWidth - 10, listTop, listHeight - 4); + const trackTop = boxTop + HUB_SCROLLBAR_INSET; + list.drawScrollbar(image, scrollbarX, trackTop, Math.max(0, height - padding.bottom - HUB_SCROLLBAR_INSET - trackTop)); return image; } function paintAddConnection(window: HubWindow): GrayImage { - const image = new GrayImage(window.viewportWidth, window.viewportHeight, 0); + const { viewportWidth: width, viewportHeight: height, padding } = window; + const image = new GrayImage(width, height, 0); const font = chromeFont(); const step = lineStep(font); - image.drawText(font, 18, 10, "Add connection", 220); - const instructionsY = 20 + step; - image.drawText(font, 24, instructionsY, "Type the g2mirror://token@host string in the", 170); - image.drawText(font, 24, instructionsY + step, "phone app (or use voice input from the menu).", 170); + // A page of text: its title sits at the window's top and left padding, the + // body indented under it, and the hint line on the bottom padding. + image.drawText(font, padding.left, padding.top, "Add connection", 220); + const bodyX = padding.left + PAGE_BODY_INDENT; + const instructionsY = padding.top + 10 + step; + image.drawText(font, bodyX, instructionsY, "Type the g2mirror://token@host string in the", 170); + image.drawText(font, bodyX, instructionsY + step, "phone app (or use voice input from the menu).", 170); const draftY = instructionsY + 3 * step; const draft = terminalNewConnectionSetting.get(); - image.drawText(font, 24, draftY, truncateLabel(draft || "(empty)", 52), 220); + image.drawText(font, bodyX, draftY, truncateLabel(draft || "(empty)", 52), 220); if (window.addError) { - image.drawText(font, 24, draftY + step + 6, window.addError, 150); + image.drawText(font, bodyX, draftY + step + 6, window.addError, 150); } - image.drawText(font, 24, window.viewportHeight - font.lineHeight - 12, `click save ${GESTURE_DOUBLE_CLICK} cancel`, 110); + image.drawText(font, bodyX, height - padding.bottom - font.lineHeight, `click save ${GESTURE_DOUBLE_CLICK} cancel`, 110); return image; } diff --git a/app/apps/timer/timer-app.ts b/app/apps/timer/timer-app.ts index bd9f0293..94729fa5 100644 --- a/app/apps/timer/timer-app.ts +++ b/app/apps/timer/timer-app.ts @@ -40,6 +40,7 @@ import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics"; import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window"; import { shell, type TrayWidget } from "../../ui/shell/shell"; +import type { Viewport } from "../../ui/viewport"; import { WindowMenuLayer } from "../../ui/window-menu"; import { clamp } from "../../util/numeric-util"; import { @@ -85,16 +86,27 @@ import { export const TIMER_WINDOW_ID = "timer"; export const TIMER_SURFACE_ID = "window:timer"; -/** Width of the list column, as a share of the viewport, and its bounds. */ +/** + * Width of the list column (its rows and the scrollbar's gutter), as a share + * of the viewport's width inside its side padding, and its bounds. + */ const LIST_SHARE = 0.4; const LIST_MIN_WIDTH = 190; const LIST_MAX_WIDTH = 240; -const LIST_TOP = 6; /** Pixels between consecutive list rows' selection boxes. */ const LIST_ROW_GAP = 1; -const STAGE_X = 10; +/** + * Room the list column keeps right of its rows for the scrollbar, which + * hugs the column's right edge: the gap before it, and its own 3 columns. + */ +const LIST_SCROLLBAR_GAP = 5; +const LIST_SCROLLBAR_GUTTER = LIST_SCROLLBAR_GAP + 3; +/** Gap between the list's divider and its rows. */ +const LIST_DIVIDER_GAP = 7; /** Gap between the stage's right edge and the list's divider. */ -const STAGE_LIST_GAP = 14; +const STAGE_LIST_GAP = 18; +/** How far the stage's small-font content is indented from its title, and ends short of the stage's right edge. */ +const STAGE_BODY_INSET = 2; /** Brightness factor for the list while a picker owns the stage. */ const LIST_DIM = 0.4; /** Big-digit sizes tried largest first until the text fits the stage. */ @@ -198,8 +210,8 @@ export class TimersLayer implements Layer { isSelectable: (row) => row.kind !== "header", draw: (args) => this.drawListRow(args), }); - /** Per-paint inputs to drawListRow. */ - private listPaint = { dim: 1, now: 0 }; + /** Per-paint inputs to drawListRow, and the padding the last paint put above the list's first row. */ + private listPaint = { dim: 1, now: 0, padTop: 0 }; private mode: Mode = { kind: "list" }; /** Which of the ringing stage's two buttons is highlighted. */ private ringButton = 0; @@ -247,7 +259,9 @@ export class TimersLayer implements Layer { let index = rows.findIndex((row) => row.key === this.selectionKey); if (index < 0) { // The row went away (a dismissed timer, a deleted alarm): stay in place. - const firstVisibleRow = Math.floor(this.list.scrollTop / listRowHeight(smallFont())); + // Rows start below the list's top padding, which scrolls with them. + const rowsScrolled = Math.max(0, this.list.scrollTop - this.listPaint.padTop) / listRowHeight(smallFont()); + const firstVisibleRow = Math.floor(rowsScrolled); index = clamp(firstVisibleRow, 0, rows.length - 1); while (index < rows.length && rows[index]!.kind === "header") index++; if (index >= rows.length) index = rows.findIndex((row) => row.kind !== "header"); @@ -579,9 +593,7 @@ export class TimersLayer implements Layer { /** A mirror touch: on the list, select the row under it. */ hitTest(x: number, y: number, ctx: LayerContext): boolean { if (this.mode.kind !== "list") return false; - const { width } = ctx.stack.getBaseSize(); - const listX = width - listWidthFor(width); - if (x < listX) return false; + if (x < columnsFor(ctx.stack.getBaseSize()).listX) return false; const rows = this.buildRows(Date.now()); this.syncList(rows); const index = this.list.indexAt(y); @@ -874,18 +886,17 @@ export class TimersLayer implements Layer { paint(ctx: LayerContext): GrayImage { timerEngine.check(); const now = Date.now(); - const { width, height } = ctx.stack.getBaseSize(); + const viewport = ctx.stack.getBaseSize(); + const { width, height, padding } = viewport; const image = new GrayImage(width, height, 0); const rows = this.buildRows(now); const selectedIndex = this.reconcileSelection(rows); - const listWidth = listWidthFor(width); - const listX = width - listWidth; + const { dividerX, stage } = columnsFor(viewport); const picking = this.mode.kind !== "list"; - this.paintList(image, rows, selectedIndex, listX, listWidth, height, ctx.stack.isFocused(), picking ? LIST_DIM : 1, now); - image.drawLine(listX - 7, 8, listX - 7, height - 8, picking ? 20 : 40); + this.paintList(image, rows, selectedIndex, viewport, ctx.stack.isFocused(), picking ? LIST_DIM : 1, now); + image.drawLine(dividerX, padding.top, dividerX, height - padding.bottom, picking ? 20 : 40); - const stage = { x: STAGE_X, y: 0, width: listX - 7 - STAGE_LIST_GAP - STAGE_X, height }; switch (this.mode.kind) { case "dial": this.paintDial(image, stage, this.mode, now); @@ -906,21 +917,23 @@ export class TimersLayer implements Layer { image: GrayImage, rows: Row[], selectedIndex: number, - listX: number, - listWidth: number, - height: number, + viewport: Viewport, focused: boolean, dim: number, now: number, ): void { - const rowHeight = listRowHeight(smallFont()); - const visible = Math.max(1, Math.floor((height - 2 * LIST_TOP) / rowHeight)); - this.listPaint = { dim, now }; + const { height, padding } = viewport; + const { listX, rowsWidth, scrollbarX } = columnsFor(viewport); + this.listPaint = { dim, now, padTop: padding.top }; // While a picker owns the stage the list has no live selection, so no // highlight; drawListRow still brightens the selected row by key. this.list.setItems(rows, dim === 1 ? selectedIndex : null); - this.list.paint(image, { x: listX, y: LIST_TOP, width: listWidth - 12, height: visible * rowHeight - LIST_ROW_GAP }, focused); - this.list.drawScrollbar(image, listX + listWidth - 7, LIST_TOP, visible * rowHeight - 4); + // The list scrolls, so it runs to the window's top and bottom edges, + // with the padding suggested for them inside it (before the first row, + // after the last): rows scrolled past an end are cut off at the edge. + // The scrollbar stays put, so it keeps inside that padding. + this.list.paint(image, { x: listX, y: 0, width: rowsWidth, height, padTop: padding.top, padBottom: padding.bottom }, focused); + this.list.drawScrollbar(image, scrollbarX, padding.top, Math.max(0, height - padding.top - padding.bottom)); } private drawListRow({ image, item: row, x, y, width, height }: MenuDrawArgs): void { @@ -1137,7 +1150,13 @@ export class TimersLayer implements Layer { // --------------------------------------------------------------------------- // Stage layout helpers -type StageRect = { x: number; y: number; width: number; height: number }; +/** + * The stage's box: its title's left edge and top, and the bottom of its + * hints. `centerX` is what its centred content (the big digits, the picker + * fields) centres on: the middle of the stage's column, from the window's + * left edge to the divider, whatever padding the box keeps inside that. + */ +type StageRect = { x: number; y: number; width: number; height: number; centerX: number }; /** * Lays the stage out top to bottom: a title line, the big digits, then @@ -1149,13 +1168,28 @@ class StageBlock { private readonly hintsTop: number; constructor(private readonly rect: StageRect) { - this.cursorY = rect.y + 12; - this.hintsTop = rect.y + rect.height - 10 - lineStep(smallFont()); + this.cursorY = rect.y; + this.hintsTop = rect.y + rect.height - smallFont().lineHeight; + } + + /** The stage's small-font content: its left edge and width. */ + private get bodyX(): number { + return this.rect.x + STAGE_BODY_INSET; + } + + private get bodyWidth(): number { + return this.rect.width - 2 * STAGE_BODY_INSET; + } + + /** Where content `width` wide sits to be centred on the stage's column, kept inside the stage. */ + private centeredX(width: number): number { + const { x, width: stageWidth, centerX } = this.rect; + return clamp(Math.floor(centerX - width / 2), x, Math.max(x, x + stageWidth - width)); } title(image: GrayImage, text: string, value: number): void { const font = mediumFont(); - image.drawText(font, this.rect.x + 4, this.cursorY, truncateText(font, text, this.rect.width - 8), value); + image.drawText(font, this.rect.x, this.cursorY, truncateText(font, text, this.rect.width), value); this.cursorY += font.lineHeight + 10; } @@ -1163,9 +1197,9 @@ class StageBlock { digits(image: GrayImage, text: string, value: number, suffix = ""): void { const suffixFont = mediumFont(); const suffixWidth = suffix ? suffixFont.measureText(suffix) + 10 : 0; - const font = fittingDigitFont(text, this.rect.width - 8 - suffixWidth); + const font = fittingDigitFont(text, this.rect.width - suffixWidth); const textWidth = font.measureText(text); - const x = this.rect.x + Math.max(0, Math.floor((this.rect.width - textWidth - suffixWidth) / 2)); + const x = this.centeredX(textWidth + suffixWidth); image.drawText(font, x, this.cursorY, text, value); if (suffix) { image.drawText(suffixFont, x + textWidth + 10, this.cursorY + font.ascent - suffixFont.ascent, suffix, Math.round(value * 0.8)); @@ -1174,8 +1208,8 @@ class StageBlock { } progress(image: GrayImage, fraction: number, value: number): void { - const x = this.rect.x + 6; - const width = this.rect.width - 12; + const x = this.bodyX; + const width = this.bodyWidth; image.fillRoundedRect(x, this.cursorY, width, 6, 40, 3); const filled = Math.round(width * clamp(fraction, 0, 1)); if (filled > 0) image.fillRoundedRect(x, this.cursorY, Math.max(6, filled), 6, value, 3); @@ -1185,7 +1219,7 @@ class StageBlock { line(image: GrayImage, text: string, value: number): void { if (!text) return; const font = smallFont(); - image.drawText(font, this.rect.x + 6, this.cursorY, truncateText(font, text, this.rect.width - 12), value); + image.drawText(font, this.bodyX, this.cursorY, truncateText(font, text, this.bodyWidth), value); this.cursorY += lineStep(font); } @@ -1197,8 +1231,8 @@ class StageBlock { /** A three-column row (lap table). */ columns(image: GrayImage, font: UiFont, cells: string[], value: number): void { if (this.cursorY + font.lineHeight > this.hintsTop - 4) return; - const x = this.rect.x + 6; - const width = this.rect.width - 12; + const x = this.bodyX; + const width = this.bodyWidth; const columnWidth = Math.floor(width / cells.length); for (let index = 0; index < cells.length; index++) { const text = cells[index]!; @@ -1215,8 +1249,8 @@ class StageBlock { this.line(image, "Once", Math.round(value * 0.7)); return; } - const step = Math.min(26, Math.floor((this.rect.width - 12) / 7)); - const x = this.rect.x + 6; + const step = Math.min(26, Math.floor(this.bodyWidth / 7)); + const x = this.bodyX; for (let index = 0; index < 7; index++) { const on = (mask & DAY_BITS[index]!) !== 0; const letter = DAY_LETTERS[index]!; @@ -1236,7 +1270,7 @@ class StageBlock { const widths = values.map((value) => font.measureText(value) + 2 * padX); const sepWidths = separators.map((separator) => (separator ? sepFont.measureText(separator) + 8 : 6)); const total = widths.reduce((sum, width) => sum + width, 0) + sepWidths.reduce((sum, width) => sum + width, 0); - let x = this.rect.x + Math.max(0, Math.floor((this.rect.width - total) / 2)); + let x = this.centeredX(total); const boxHeight = font.lineHeight + 4; for (let index = 0; index < values.length; index++) { const width = widths[index]!; @@ -1267,10 +1301,10 @@ class StageBlock { const font = smallFont(); const gap = 12; const height = font.lineHeight + 14; - const width = Math.floor((this.rect.width - 12 - gap * (labels.length - 1)) / labels.length); + const width = Math.floor((this.bodyWidth - gap * (labels.length - 1)) / labels.length); const y = Math.max(this.cursorY + 4, this.hintsTop - 10 - height); for (let index = 0; index < labels.length; index++) { - const x = this.rect.x + 6 + index * (width + gap); + const x = this.bodyX + index * (width + gap); const active = index === selected; if (active) { image.fillRoundedRect(x, y, width, height, focused ? 40 : 15, 8); @@ -1285,12 +1319,35 @@ class StageBlock { } hints(image: GrayImage, pairs: Array<[string, string]>): void { - image.drawText(smallFont(), this.rect.x + 6, this.hintsTop, gestureHints(pairs), 110); + image.drawText(smallFont(), this.bodyX, this.hintsTop, gestureHints(pairs), 110); } } -function listWidthFor(viewportWidth: number): number { - return clamp(Math.round(viewportWidth * LIST_SHARE), LIST_MIN_WIDTH, LIST_MAX_WIDTH); +/** + * The screen's columns in a viewport. Fixed content keeps the padding + * suggested for the window's edges: the stage's box sits inside it on the + * left, top and bottom, and the list column (rows, then the scrollbar's + * gutter) ends at it on the right. The divider stands between the two. + */ +function columnsFor(viewport: Viewport): { listX: number; rowsWidth: number; scrollbarX: number; dividerX: number; stage: StageRect } { + const { width, height, padding } = viewport; + const listRight = width - padding.right; + const listWidth = clamp(Math.round((listRight - padding.left) * LIST_SHARE), LIST_MIN_WIDTH, LIST_MAX_WIDTH); + const listX = listRight - listWidth; + const dividerX = listX - LIST_DIVIDER_GAP; + return { + listX, + rowsWidth: listWidth - LIST_SCROLLBAR_GUTTER, + scrollbarX: listRight - LIST_SCROLLBAR_GUTTER + LIST_SCROLLBAR_GAP, + dividerX, + stage: { + x: padding.left, + y: padding.top, + width: Math.max(0, dividerX - STAGE_LIST_GAP - padding.left), + height: Math.max(0, height - padding.top - padding.bottom), + centerX: dividerX / 2, + }, + }; } function drawPauseMark(image: GrayImage, x: number, y: number, value: number): void { diff --git a/app/apps/transcribe/transcribe.ts b/app/apps/transcribe/transcribe.ts index 2fd35a6c..36df07a2 100644 --- a/app/apps/transcribe/transcribe.ts +++ b/app/apps/transcribe/transcribe.ts @@ -6,7 +6,7 @@ import { writeTextToDownloads } from "../../native/file-access"; import { voiceControlBridge } from "../../native/voice-control"; import { Layer, type LayerContext } from "../../ui/layers"; import { type InputEvent } from "../../ui/gestures"; -import { TranscriptModel, TranscriptScroll, wrapTranscribeText } from "./transcript-model"; +import { TranscriptModel, TranscriptScroll, transcriptLayout, wrapTranscribeText } from "./transcript-model"; /** * Live transcription view. Continuous mic capture is owned by the app wrapper @@ -47,26 +47,28 @@ export class TranscribeLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); - const image = new GrayImage(width, height, 0); - const text = this.transcript.text || (this.listening ? "Listening..." : this.detail); - const wrapped = wrapTranscribeText((line) => font.measureText(line), text, width - 64); + const viewport = ctx.stack.getBaseSize(); + const image = new GrayImage(viewport.width, viewport.height, 0); - const bodyTop = paintListeningHeader(image, font, "Transcribe", this.saveNotice || this.status, this.listening ? this.meter : null); + const bodyTop = paintListeningHeader(image, font, "Transcribe", this.saveNotice || this.status, this.listening ? this.meter : null, + viewport.padding); const step = lineStep(font) + 2; - const bodyLines = Math.max(1, Math.floor((height - 6 - bodyTop - font.lineHeight) / step) + 1); - const firstLine = this.scroll.layout(wrapped.length, bodyLines); - for (let index = firstLine; index < Math.min(wrapped.length, firstLine + bodyLines); index++) { + const layout = transcriptLayout(viewport, bodyTop, font.lineHeight, step); + const text = this.transcript.text || (this.listening ? "Listening..." : this.detail); + const wrapped = wrapTranscribeText((line) => font.measureText(line), text, layout.textWidth); + + const firstLine = this.scroll.layout(wrapped.length, layout.pageLines); + for (let index = firstLine; index < Math.min(wrapped.length, firstLine + layout.drawnLines); index++) { const y = bodyTop + (index - firstLine) * step; - image.drawText(font, 32, y, wrapped[index]!, 230); + image.drawText(font, layout.textX, y, wrapped[index]!, 230); } - if (wrapped.length > bodyLines) { - const trackHeight = height - bodyTop - 6; - const thumbHeight = Math.max(8, Math.round(trackHeight * bodyLines / wrapped.length)); - const thumbY = bodyTop + Math.round((trackHeight - thumbHeight) * firstLine / (wrapped.length - bodyLines)); - image.fillRect(width - 14, bodyTop, 2, trackHeight, 45); - image.fillRect(width - 14, thumbY, 2, thumbHeight, 140); + if (wrapped.length > layout.pageLines) { + const track = layout.scrollbar; + const thumbHeight = Math.max(8, Math.round(track.height * layout.pageLines / wrapped.length)); + const thumbY = track.y + Math.round((track.height - thumbHeight) * firstLine / (wrapped.length - layout.pageLines)); + image.fillRect(track.x, track.y, track.width, track.height, 45); + image.fillRect(track.x, thumbY, track.width, thumbHeight, 140); } return image; } diff --git a/app/apps/transcribe/transcript-model.ts b/app/apps/transcribe/transcript-model.ts index 8b0e811a..af950a70 100644 --- a/app/apps/transcribe/transcript-model.ts +++ b/app/apps/transcribe/transcript-model.ts @@ -1,3 +1,5 @@ +import type { Viewport } from "../../ui/viewport"; + type TranscriptEvent = { text: string; isFinal: boolean; @@ -54,6 +56,50 @@ export class TranscriptScroll { } } +/** The scrollbar, and the room the transcript's lines leave for it at their right. */ +const SCROLLBAR_WIDTH = 2; +const SCROLLBAR_GUTTER = 8; + +export type TranscriptLayout = { + /** The text column: its left, and the width its lines wrap to. */ + textX: number; + textWidth: number; + /** Lines the view scrolls by: as many as fit above the bottom padding. */ + pageLines: number; + /** Lines to draw from the first one shown: as many as fit above the window's bottom edge. */ + drawnLines: number; + /** The scrollbar's track, for a transcript longer than a page. */ + scrollbar: { x: number; y: number; width: number; height: number }; +}; + +/** + * Where a listening view (Transcribe, Translate) puts its transcript, under + * a fixed header that ends at `top`. The text column starts at the padding + * suggested for the window's left edge (viewport.ts), in line with the + * header's title, and stops short of the right edge's by the scrollbar's + * gutter; the scrollbar stands in that, against the padding. The lines + * scroll, so they run to the window's bottom edge: the view scrolls by the + * lines that fit above that edge's padding, which rests the transcript's + * last line clear of a border there, and while more lines follow it draws + * as many as fit above the edge itself. + */ +export function transcriptLayout(viewport: Viewport, top: number, lineHeight: number, step: number): TranscriptLayout { + const { width, height, padding } = viewport; + const linesAbove = (bottom: number) => Math.max(1, Math.floor((bottom - top - lineHeight) / step) + 1); + return { + textX: padding.left, + textWidth: Math.max(0, width - padding.left - padding.right - SCROLLBAR_GUTTER), + pageLines: linesAbove(height - padding.bottom), + drawnLines: linesAbove(height), + scrollbar: { + x: width - padding.right - SCROLLBAR_WIDTH, + y: top, + width: SCROLLBAR_WIDTH, + height: Math.max(0, height - padding.bottom - top), + }, + }; +} + export function wrapTranscribeText(measure: (text: string) => number, text: string, maxWidth: number): string[] { const lines: string[] = []; for (const paragraph of text.split("\n")) { diff --git a/app/apps/translate/translate.ts b/app/apps/translate/translate.ts index e747be2d..cb826b31 100644 --- a/app/apps/translate/translate.ts +++ b/app/apps/translate/translate.ts @@ -7,7 +7,7 @@ import { voiceControlBridge } from "../../native/voice-control"; import { Layer, type LayerContext } from "../../ui/layers"; import { type InputEvent } from "../../ui/gestures"; import { type LlmStreamHandle } from "../../assistant/llm-protocol"; -import { TranscriptScroll, wrapTranscribeText } from "../transcribe/transcript-model"; +import { TranscriptScroll, transcriptLayout, wrapTranscribeText } from "../transcribe/transcript-model"; import { TranslationModel, type TranslationJob, type TranslationLine } from "./translation-model"; import { resolveTranslationModel, streamTranslation } from "./translate-llm"; import { resolveTranslateLanguage, spokenLanguageName, translateShowOriginalSetting } from "./translate-settings"; @@ -72,14 +72,16 @@ export class TranslateLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); - const image = new GrayImage(width, height, 0); + const viewport = ctx.stack.getBaseSize(); + const image = new GrayImage(viewport.width, viewport.height, 0); const measure = (line: string) => font.measureText(line); - const maxWidth = width - 64; const status = this.problem ? "Setup needed" : this.translationError || this.status; const meter = this.listening && !this.problem ? this.meter : null; - const bodyTop = paintListeningHeader(image, font, "Translate", this.saveNotice || status, meter); + const bodyTop = paintListeningHeader(image, font, "Translate", this.saveNotice || status, meter, viewport.padding); + const step = lineStep(font) + 2; + const layout = transcriptLayout(viewport, bodyTop, font.lineHeight, step); + const maxWidth = layout.textWidth; let lines: TranslationLine[]; if (this.problem) { @@ -92,20 +94,18 @@ export class TranslateLayer implements Layer { } } - const step = lineStep(font) + 2; - const bodyLines = Math.max(1, Math.floor((height - 6 - bodyTop - font.lineHeight) / step) + 1); - const firstLine = this.scroll.layout(lines.length, bodyLines); - for (let index = firstLine; index < Math.min(lines.length, firstLine + bodyLines); index++) { + const firstLine = this.scroll.layout(lines.length, layout.pageLines); + for (let index = firstLine; index < Math.min(lines.length, firstLine + layout.drawnLines); index++) { const y = bodyTop + (index - firstLine) * step; - image.drawText(font, 32, y, lines[index]!.text, lines[index]!.gray); + image.drawText(font, layout.textX, y, lines[index]!.text, lines[index]!.gray); } - if (lines.length > bodyLines) { - const trackHeight = height - bodyTop - 6; - const thumbHeight = Math.max(8, Math.round(trackHeight * bodyLines / lines.length)); - const thumbY = bodyTop + Math.round((trackHeight - thumbHeight) * firstLine / (lines.length - bodyLines)); - image.fillRect(width - 14, bodyTop, 2, trackHeight, 45); - image.fillRect(width - 14, thumbY, 2, thumbHeight, 140); + if (lines.length > layout.pageLines) { + const track = layout.scrollbar; + const thumbHeight = Math.max(8, Math.round(track.height * layout.pageLines / lines.length)); + const thumbY = track.y + Math.round((track.height - thumbHeight) * firstLine / (lines.length - layout.pageLines)); + image.fillRect(track.x, track.y, track.width, track.height, 45); + image.fillRect(track.x, thumbY, track.width, thumbHeight, 140); } return image; } diff --git a/app/apps/weather/weather.ts b/app/apps/weather/weather.ts index 156bcb55..78ed84ce 100644 --- a/app/apps/weather/weather.ts +++ b/app/apps/weather/weather.ts @@ -7,29 +7,75 @@ import { type Layer, type LayerContext } from "../../ui/layers"; import { Menu, type MenuDrawArgs } from "../../ui/menu-core"; import { centeredTextY, lineStep, tightRowHeight } from "../../ui/metrics"; import { distanceUnitSetting } from "../../ui/dashboard-settings"; +import type { Viewport } from "../../ui/viewport"; import { renderWeatherIcon } from "./weather-icons"; import { formatTemperature, widestTemperatureSample } from "./temperature-format"; -const PAGE_X = 18; -const HEADER_Y = 8; -const CURRENT_TOP = 34; -/** Minimum x of the description column right of the temperature. */ -const CURRENT_TEXT_X = 112; +/** + * How far the page's text (title, current conditions, forecast names) starts + * right of the forecast rows' selection boxes, which span the padding + * suggested for the window's side edges. + */ +const PAGE_TEXT_INSET = 7; +/** + * The page's fixed parts, down from the title line at the window's top + * padding: the temperature, the description column's first line, the rule + * above the forecast, the forecast's column headings, and its first row's text. + */ +const TEMPERATURE_OFFSET_Y = 34; +const CURRENT_TEXT_OFFSET_Y = 22; +const FORECAST_RULE_OFFSET_Y = 90; +const FORECAST_HEADER_OFFSET_Y = 97; +const FORECAST_OFFSET_Y = 116; +/** Minimum distance of the description column from the page text's left edge. */ +const CURRENT_TEXT_MIN_OFFSET_X = 94; /** The current-conditions icon, left of the temperature. */ const CURRENT_ICON_SIZE = 40; const CURRENT_ICON_GAP = 10; /** Gap between the temperature and the description column. */ const CURRENT_TEXT_GAP = 16; -const FORECAST_HEADER_Y = 105; -const FORECAST_TOP = 124; /** Forecast rows hold a 20px icon; grow with the font past that. */ const FORECAST_MIN_ROW_HEIGHT = 23; /** Gap between forecast columns. */ const FORECAST_COL_GAP = 14; -/** Left edge of the forecast rows' selection boxes. */ -const FORECAST_BOX_X = PAGE_X - 7; /** Pixels between consecutive forecast rows' selection boxes. */ const FORECAST_ROW_GAP = 2; +/** A full-page message: its indent from the page text, and the least it sits below the title line. */ +const MESSAGE_INDENT = 6; +const MESSAGE_MIN_OFFSET_Y = 44; + +/** + * Where the page's parts go in a viewport. The forecast rows' selection + * boxes are the outermost thing toward the window's sides, so they sit at + * the padding suggested there, and the page's text is placed from them. + */ +type PageLayout = { + width: number; + height: number; + /** The forecast rows' selection boxes. */ + boxX: number; + boxWidth: number; + /** The span of the page's text: the title starts at textX, the location name ends at textRight. */ + textX: number; + textRight: number; + /** The title line's top, at the window's top padding. */ + top: number; + /** The window's bottom padding, kept under a footer line and inside the forecast list. */ + padBottom: number; +}; + +function pageLayout({ width, height, padding }: Viewport): PageLayout { + return { + width, + height, + boxX: padding.left, + boxWidth: width - padding.left - padding.right, + textX: padding.left + PAGE_TEXT_INSET, + textRight: width - padding.right - PAGE_TEXT_INSET, + top: padding.top, + padBottom: padding.bottom, + }; +} /** Forecast column x offsets from the row box's left edge. */ type ForecastColumns = { name: number; temp: number; precip: number; summary: number }; @@ -54,26 +100,20 @@ export class WeatherLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); - const image = new GrayImage(width, height, 0); + const page = pageLayout(ctx.stack.getBaseSize()); + const image = new GrayImage(page.width, page.height, 0); const weather = this.state(); - image.drawText(font, PAGE_X, HEADER_Y, "Weather", 225); + image.drawText(font, page.textX, page.top, "Weather", 225); if (weather.locationName) { - image.drawText( - font, - width - PAGE_X - font.measureText(truncateText(font, weather.locationName, 220)), - HEADER_Y, - truncateText(font, weather.locationName, 220), - 150, - ); + const locationName = truncateText(font, weather.locationName, 220); + image.drawText(font, page.textRight - font.measureText(locationName), page.top, locationName, 150); } if (weather.phase === "permission-required") { this.drawMessage( image, font, - width, - height, + page, "Allow approximate location on your phone to get local weather.", `${GESTURE_CLICK} request`, ); @@ -81,16 +121,16 @@ export class WeatherLayer implements Layer { } // A refresh with earlier data in hand keeps showing that data. if ((weather.phase === "locating" || weather.phase === "loading") && !weather.current) { - this.drawMessage(image, font, width, height, weather.status); + this.drawMessage(image, font, page, weather.status); return image; } if (weather.phase === "error") { - this.drawMessage(image, font, width, height, weather.status, `${GESTURE_CLICK} retry`); + this.drawMessage(image, font, page, weather.status, `${GESTURE_CLICK} retry`); return image; } - this.drawCurrent(image, weather, width); - this.drawForecast(image, weather.forecast, ctx, width, height); + this.drawCurrent(image, weather, page); + this.drawForecast(image, weather.forecast, ctx, page); return image; } @@ -111,31 +151,34 @@ export class WeatherLayer implements Layer { } } - private drawCurrent(image: GrayImage, weather: WeatherState, width: number): void { + private drawCurrent(image: GrayImage, weather: WeatherState, page: PageLayout): void { const current = weather.current; if (!current) return; const large = getDefaultLargeFont(); const medium = getDefaultMediumFont(); const small = getDefaultSmallFont(); const temperature = current.temperatureF === null ? "--°" : formatTemperature(current.temperatureF); - const temperatureY = CURRENT_TOP + 8; - let temperatureX = PAGE_X; + const temperatureY = page.top + TEMPERATURE_OFFSET_Y; + let temperatureX = page.textX; if (current.condition) { const icon = renderWeatherIcon(current.condition, current.isDaytime, CURRENT_ICON_SIZE); - if (icon) image.drawImage(icon, PAGE_X, temperatureY + (((large.lineHeight - icon.height) / 2) | 0)); + if (icon) image.drawImage(icon, page.textX, temperatureY + (((large.lineHeight - icon.height) / 2) | 0)); temperatureX += CURRENT_ICON_SIZE + CURRENT_ICON_GAP; } image.drawText(large, temperatureX, temperatureY, temperature, 245); - const textX = Math.max(CURRENT_TEXT_X, temperatureX + Math.ceil(large.measureText(temperature)) + CURRENT_TEXT_GAP); + const textX = Math.max( + page.textX + CURRENT_TEXT_MIN_OFFSET_X, + temperatureX + Math.ceil(large.measureText(temperature)) + CURRENT_TEXT_GAP, + ); // Description / details / source stack compactly from just below the // header line, so at large font sizes they stay clear of the forecast // table instead of running into it. - let cy = 30; + let cy = page.top + CURRENT_TEXT_OFFSET_Y; image.drawText( medium, textX, cy, - truncateText(medium, current.description || "Current conditions", width - textX - PAGE_X), + truncateText(medium, current.description || "Current conditions", page.textRight - textX), 220, ); cy += medium.lineHeight + 2; @@ -153,29 +196,22 @@ export class WeatherLayer implements Layer { const age = current.timestampMs ? formatAge(Date.now() - current.timestampMs) : ""; const updating = weather.phase === "locating" || weather.phase === "loading" ? " · updating" : ""; image.drawText(small, textX, cy, `${source}${age ? ` ${age}` : ""}${updating}`, 105); - image.drawLine(PAGE_X, FORECAST_HEADER_Y - 7, width - PAGE_X, FORECAST_HEADER_Y - 7, 40); + const ruleY = page.top + FORECAST_RULE_OFFSET_Y; + image.drawLine(page.textX, ruleY, page.textRight - 1, ruleY, 40); } private drawForecast( image: GrayImage, forecast: ForecastPeriod[], ctx: LayerContext, - width: number, - height: number, + page: PageLayout, ): void { const font = getDefaultSmallFont(); + const forecastTop = page.top + FORECAST_OFFSET_Y; if (!forecast.length) { - image.drawText(font, PAGE_X, FORECAST_TOP, "No upcoming forecast periods.", 160); + image.drawText(font, page.textX, forecastTop, "No upcoming forecast periods.", 160); return; } - const rowH = forecastRowHeight(font); - // The last row only needs its text line (not a full row pitch of - // clearance below), which usually fits one more row before the bottom. - // The menu only draws rows whose whole selection box fits, so the box - // holds that many rows, cut at the image's bottom edge. - const visibleRows = Math.max(1, 1 + Math.floor((height - FORECAST_TOP - (font.lineHeight + 4)) / rowH)); - const boxTop = FORECAST_TOP - 2; - const boxHeight = Math.min(visibleRows * rowH - FORECAST_ROW_GAP, height - boxTop); // Columns are sized to the widest period name so names ("Wednesday // Night") never truncate; the summary column absorbs what's left. @@ -183,24 +219,30 @@ export class WeatherLayer implements Layer { font.measureText("Upcoming"), ...forecast.map((period) => font.measureText(period.name)), ); - const tempX = PAGE_X + nameWidth + FORECAST_COL_GAP; + const tempX = page.textX + nameWidth + FORECAST_COL_GAP; const precipX = tempX + Math.max(font.measureText("Temp"), font.measureText(widestTemperatureSample())) + FORECAST_COL_GAP; const summaryX = precipX + Math.max(font.measureText("Rain"), font.measureText("100%")) + FORECAST_COL_GAP; - image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150); - image.drawText(font, tempX, FORECAST_HEADER_Y, "Temp", 105); - image.drawText(font, precipX, FORECAST_HEADER_Y, "Rain", 105); + const headerY = page.top + FORECAST_HEADER_OFFSET_Y; + image.drawText(font, page.textX, headerY, "Upcoming", 150); + image.drawText(font, tempX, headerY, "Temp", 105); + image.drawText(font, precipX, headerY, "Rain", 105); this.columns = { - name: PAGE_X - FORECAST_BOX_X, - temp: tempX - FORECAST_BOX_X, - precip: precipX - FORECAST_BOX_X, - summary: summaryX - FORECAST_BOX_X, + name: page.textX - page.boxX, + temp: tempX - page.boxX, + precip: precipX - page.boxX, + summary: summaryX - page.boxX, }; this.forecastMenu.setItems(forecast); + // The rows scroll under the fixed column headings, down to the window's + // bottom edge, where a row cut off for scrolling meets the edge; that + // edge's padding goes inside the list, after its last row. + const boxTop = forecastTop - 2; + if (boxTop >= page.height) return; this.forecastMenu.paint( image, - { x: FORECAST_BOX_X, y: boxTop, width: width - 2 * FORECAST_BOX_X, height: boxHeight }, + { x: page.boxX, y: boxTop, width: page.boxWidth, height: page.height - boxTop, padBottom: page.padBottom }, ctx.stack.isFocused(), ); } @@ -213,7 +255,7 @@ export class WeatherLayer implements Layer { image.drawText(font, x + columns.name, textY, period.name, shade); image.drawText(font, x + columns.temp, textY, period.temperatureF === null ? "--" : formatTemperature(period.temperatureF), shade); image.drawText(font, x + columns.precip, textY, period.precipitationPercent === null ? "--" : `${Math.round(period.precipitationPercent)}%`, selected ? 220 : 155); - // The summary stops PAGE_X short of the viewport's right edge, as the name starts PAGE_X in from the left. + // The summary stops as far short of the selection box's right edge as the name starts in from its left. const summaryWidth = width - columns.summary - columns.name; image.drawText(font, x + columns.summary, textY, truncateText(font, period.shortForecast, summaryWidth), selected ? 220 : 165); } @@ -221,18 +263,18 @@ export class WeatherLayer implements Layer { private drawMessage( image: GrayImage, font: UiFont, - width: number, - height: number, + page: PageLayout, message: string, footer?: string, ): void { - const lines = wrapText(font, message, width - 2 * (PAGE_X + 6)); - const top = Math.max(52, Math.round((height - lines.length * 15) / 2) - 8); + const lines = wrapText(font, message, page.textRight - page.textX - 2 * MESSAGE_INDENT); + // Centered in the window, but never up into the title line. + const top = Math.max(page.top + MESSAGE_MIN_OFFSET_Y, Math.round((page.height - lines.length * 15) / 2) - 8); for (let index = 0; index < lines.length; index++) { - image.drawText(font, PAGE_X + 6, top + index * 15, lines[index]!, 185); + image.drawText(font, page.textX + MESSAGE_INDENT, top + index * 15, lines[index]!, 185); } if (footer) { - image.drawText(font, PAGE_X, height - 20, footer, 110); + image.drawText(font, page.textX, page.height - page.padBottom - font.lineHeight, footer, 110); } } } diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 083bc7cc..e6513690 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -74,7 +74,8 @@ import { ALL_APPS } from "../apps/all-apps"; import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition"; import { type InProcessAppOptions, type InProcessWindow } from "../ui/shell/in-process-window"; import { loadPersistedOpenApps, savePersistedOpenApps } from "../ui/shell/open-apps-persistence"; -import { appViewportRect, isOnSwitcherEdge, screenHeight, screenWidth, sidebarStripVisible, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry"; +import { appViewportRect, isOnSwitcherEdge, screenHeight, screenWidth, sidebarStripVisible, windowEdges, type ScreenRect, type WindowHeightMode } from "../ui/shell/geometry"; +import { sameEdges } from "../ui/viewport"; import { type ContinuousCaptureOptions, type LayerActions, type TextSettingsEditToggle, type TranscriptionChoice } from "../ui/layers"; import { type KeyboardInputSession } from "../ui/shell/keyboard-input"; import { appSwitcherPositionSetting, glassesModelName, glassesTypeSetting, statusBarPositionSetting, statusBarVisibilitySetting, windowBorderSetting, assistantAllowProactiveSetting, assistantBackendSetting, assistantBridgeHostSetting, assistantBridgePortSetting, assistantBridgeTokenSetting, getBrightnessPreferences, displayLayoutSetting, navigateDisplayLayoutSetting, terminalDisplayLayoutSetting, elevenLabsApiKeySetting, getStringSettingById, openAiApiKeySetting, nightscoutApiTokenSetting, firmwareDebugFlagsSetting, whisperServerApiKeySetting, whisperServerLanguageSetting, whisperServerModelSetting, whisperServerUrlSetting, lockScreenEnabledSetting, nightscoutSiteUrlSetting, onAnySettingChanged, previewColorSetting, effectiveRingConnectionMode, type RingConnectionMode, saveVoiceRecordingsSetting, sonioxApiKeySetting, screenTimeoutSetting, screenTimeoutSettingToMs, shouldPopUpNewNotification, suspendEvenHubWhenScreenOffSetting, voiceProviderSetting, wakeWordActionSetting, type ConfigSettingString } from "../ui/dashboard-settings"; @@ -482,9 +483,11 @@ class DashboardController { this.syncVoiceWake(); // Display mode size first: a reflow repositions every surface too, so // the band-position check after it then has nothing left to do. - this.applyDisplayModeIfChanged(); + const resized = this.applyDisplayModeIfChanged(); this.applyVerticalPositionIfChanged(); - this.applyAppLayoutsIfChanged(); + const appsResized = this.applyAppLayoutsIfChanged(); + // Either reflow hands windows their new edges along with their size. + if (!resized && !appsResized) this.applyWindowEdgesIfChanged(); this.syncAssistantBridgeIfChanged(); this.syncLockScreenSettingIfChanged(); // A Preview color or scaling change should show on the mirror at once, @@ -578,9 +581,9 @@ class DashboardController { * that support resizing receive the new viewport. Other worker windows are * closed and launched again at the new size. */ - private applyDisplayModeIfChanged(): void { + private applyDisplayModeIfChanged(): boolean { const sizes = viewportSizesKey(); - if (sizes === this.lastViewportSizes) return; + if (sizes === this.lastViewportSizes) return false; this.lastViewportSizes = sizes; // The reflow below reconfigures every surface, covering any move too. this.lastWindowPlacement = windowPlacementKey(); @@ -612,6 +615,22 @@ class DashboardController { })().catch((error) => { this.appendLog(`display mode change failed: ${this.formatError(error)}`); }); + return true; + } + + /** + * A setting changed what some windows' edges touch (geometry.ts + * windowEdges: a border line, or blank space) without resizing them, as + * the Border setting does under a top status bar: those windows lay out + * again with the padding their new edges suggest. Their surfaces stay as + * they are. + */ + private applyWindowEdgesIfChanged(): void { + for (const window of Array.from(shell.getWindows())) { + const laidOut = window.layoutSize?.(); + if (!laidOut || !window.relayout || sameEdges(laidOut.edges, windowEdges(window.appId))) continue; + window.relayout(); + } } /** @@ -660,10 +679,10 @@ class DashboardController { }; } - private applyAppLayoutsIfChanged(): void { + private applyAppLayoutsIfChanged(): boolean { const layouts = this.appLayouts(); const changed = Object.keys(layouts).filter((appId) => layouts[appId] !== this.lastAppLayouts[appId]); - if (!changed.length) return; + if (!changed.length) return false; this.lastAppLayouts = layouts; void (async () => { for (const window of Array.from(shell.getWindows())) { @@ -674,6 +693,7 @@ class DashboardController { shell.foregroundWindow()?.requestRender(); this.requestShellRender(); })().catch((error) => this.appendLog(`app layout change failed: ${this.formatError(error)}`)); + return true; } private applyVerticalPositionIfChanged(): void { diff --git a/app/g2/ios-preview-controller.ts b/app/g2/ios-preview-controller.ts index 0ffb15fb..a1d83172 100644 --- a/app/g2/ios-preview-controller.ts +++ b/app/g2/ios-preview-controller.ts @@ -47,7 +47,7 @@ import { makeInputEvent, type InputEvent, type InputEventPayload } from '../ui/g import { noopLayerActions, type ContinuousCaptureOptions, type LayerActions } from '../ui/layers' import { TextViewerLayer } from '../apps/files/text-viewer' import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell' -import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, type ScreenRect } from '../ui/shell/geometry' +import { appViewportRect, isOnSwitcherEdge, sidebarStripVisible, windowEdges, type ScreenRect } from '../ui/shell/geometry' import { mirrorToPanelFraction, phonePreviewCrop } from './phone-preview-crop' import { onAnySettingChanged, previewColorSetting, lockScreenEnabledSetting, getBrightnessPreferences, getStringSettingById, @@ -504,7 +504,9 @@ export class IosPreviewController { await target.setSurfaceVisible(window.surfaceId, shell.foregroundWindow()?.windowId === window.windowId) } private relayout(): void { - const layout = shell.getWindows().map(window => JSON.stringify(appViewportRect(window.heightMode, window.appId))).join(";") + // Each window's rect, and what its edges touch (which sets its padding). + const layout = shell.getWindows().map(window => + JSON.stringify([appViewportRect(window.heightMode, window.appId), windowEdges(window.appId)])).join(";") if (layout === this.lastLayout) return this.lastLayout = layout for (const window of shell.getWindows()) { diff --git a/app/health/health-glance.ts b/app/health/health-glance.ts index 7b690331..337b7888 100644 --- a/app/health/health-glance.ts +++ b/app/health/health-glance.ts @@ -23,6 +23,7 @@ import { GrayImage, type UiFont } from "../graphics/image"; import { lineStep } from "../ui/metrics"; import { GESTURE_SCROLL } from "../ui/gestures"; +import type { Edges } from "../ui/viewport"; import { drawSleepLanes, drawMetricChart, @@ -47,10 +48,11 @@ import { } from "./health-types"; import { stageLabel, type SleepStageName } from "./sleep-stages"; -const MARGIN = 10; const GUTTER = 14; -/** Below this the two columns stack instead. */ -const TWO_COLUMN_MIN_WIDTH = 380; +/** Below this content width the two columns stack instead. */ +const TWO_COLUMN_MIN_WIDTH = 360; +/** Clear space between a page's content and its footer hint. */ +const FOOTER_GAP = 16; export type GlanceFonts = { small: UiFont; large: UiFont }; @@ -108,7 +110,30 @@ export type GlanceData = { fixture: boolean; }; -export type GlanceSize = { width: number; height: number }; +/** + * The window a page is painted for: its size, and the padding suggested for + * each of its edges (`ui/viewport.ts`), which is how far in the page's + * outermost content sits. A size with no padding is a window nothing borders. + */ +export type GlanceSize = { width: number; height: number; padding?: Edges }; + +/** The rect a page lays out in: the window inside its edge padding. */ +type Frame = { left: number; top: number; right: number; bottom: number }; + +function frameOf(size: GlanceSize): Frame { + const { padding } = size; + return { + left: padding?.left ?? 0, + top: padding?.top ?? 0, + right: size.width - (padding?.right ?? 0), + bottom: size.height - (padding?.bottom ?? 0), + }; +} + +/** Height the footer hint takes off the bottom of a page's content: its line and the gap above it. */ +function footerHeight(fonts: GlanceFonts): number { + return fonts.small.lineHeight + FOOTER_GAP; +} /** Paint whichever page the cursor is on. */ export function drawGlancePage( @@ -165,10 +190,11 @@ function drawHeader( right: string, ): number { const { small } = fonts; - image.drawText(small, MARGIN, MARGIN, title, INK.title); - drawTextRight(image, small, size.width - MARGIN, MARGIN, right, INK.label); - const bottom = MARGIN + small.lineHeight + 3; - image.drawLine(MARGIN, bottom, size.width - MARGIN, bottom, INK.axis); + const frame = frameOf(size); + image.drawText(small, frame.left, frame.top, title, INK.title); + drawTextRight(image, small, frame.right, frame.top, right, INK.label); + const bottom = frame.top + small.lineHeight + 3; + image.drawLine(frame.left, bottom, frame.right - 1, bottom, INK.axis); return bottom; } @@ -186,8 +212,8 @@ function drawFooter( hint: string, ): void { const { small } = fonts; - const y = size.height - small.lineHeight - 2; - image.drawText(small, MARGIN, y, hint, INK.faint); + const frame = frameOf(size); + image.drawText(small, frame.left, frame.bottom - small.lineHeight, hint, INK.faint); } // =========================================================================== @@ -220,24 +246,24 @@ function drawOverview( ): void { const { small, large } = fonts; const step = lineStep(small); + const frame = frameOf(size); const headerBottom = drawHeader(image, size, fonts, "HEALTH", shortDate(data.nowMs)); const summary = data.summary; if (!summary) { - image.drawText(small, MARGIN, headerBottom + step, "Waiting for ring data...", INK.dim); + image.drawText(small, frame.left, headerBottom + step, "Waiting for ring data...", INK.dim); drawFooter(image, size, fonts, ""); return; } - const twoColumn = size.width >= TWO_COLUMN_MIN_WIDTH; - const columnWidth = twoColumn - ? Math.floor((size.width - MARGIN * 2 - GUTTER) / 2) - : size.width - MARGIN * 2; - const rightX = twoColumn ? MARGIN + columnWidth + GUTTER : MARGIN; + const contentWidth = frame.right - frame.left; + const twoColumn = contentWidth >= TWO_COLUMN_MIN_WIDTH; + const columnWidth = twoColumn ? Math.floor((contentWidth - GUTTER) / 2) : contentWidth; + const rightX = twoColumn ? frame.left + columnWidth + GUTTER : frame.left; const top = headerBottom + 6; const todayBottom = drawTodayColumn(image, summary, { - x: MARGIN, + x: frame.left, y: top, width: columnWidth, fonts, @@ -259,7 +285,7 @@ function drawOverview( // artefact instead of as structure. It also matters more than it looks: on // the glasses an unlit pixel is TRANSPARENT, so every line drawn past the // content is one more thing between the wearer and the world. - const divider = MARGIN + columnWidth + Math.floor(GUTTER / 2); + const divider = frame.left + columnWidth + Math.floor(GUTTER / 2); image.drawLine(divider, top, divider, Math.max(todayBottom, nightBottom), INK.faint); } @@ -371,6 +397,7 @@ function drawMetricDetail( ): void { const { small, large } = fonts; const step = lineStep(small); + const frame = frameOf(size); const headerBottom = drawHeader( image, size, @@ -388,12 +415,12 @@ function drawMetricDetail( // headline is the day's total rather than a range. const total = metric === "steps" ? data.summary?.steps ?? 0 : data.summary?.calories ?? 0; const text = total > 0 ? total.toLocaleString() : "--"; - image.drawText(large, MARGIN, y, text, INK.title); + image.drawText(large, frame.left, y, text, INK.title); const width = large.measureText(text); if (unit) { image.drawText( small, - MARGIN + width + 6, + frame.left + width + 6, y + large.lineHeight - small.lineHeight - 1, unit, INK.label, @@ -401,17 +428,16 @@ function drawMetricDetail( } y += large.lineHeight + 6; } else { - image.drawText(small, MARGIN, y, rangeAverageText(summary, unit), INK.line); + image.drawText(small, frame.left, y, rangeAverageText(summary, unit), INK.line); y += step + 4; } const points = data.hourly[metric] ?? []; - const footerHeight = small.lineHeight + 8; - const chartHeight = size.height - y - MARGIN - footerHeight; + const chartHeight = frame.bottom - footerHeight(fonts) - y; if (chartHeight >= 40) { drawMetricChart( image, - { x: MARGIN, y, width: size.width - MARGIN * 2, height: chartHeight }, + { x: frame.left, y, width: frame.right - frame.left, height: chartHeight }, { metric, points, @@ -482,35 +508,35 @@ function drawSleepDetail( const { small, large } = fonts; const step = lineStep(small); const sleep = data.summary?.sleep ?? null; + const frame = frameOf(size); const headerBottom = drawHeader(image, size, fonts, "SLEEP", "last night"); let y = headerBottom + 6; if (!sleep) { - image.drawText(small, MARGIN, y, "No sleep record", INK.dim); + image.drawText(small, frame.left, y, "No sleep record", INK.dim); drawFooter(image, size, fonts, `${GESTURE_SCROLL} Calories · Summary`); return; } // Total and efficiency on one line, so the lanes get the height. const totalText = formatDuration(sleep.totalSec); - image.drawText(large, MARGIN, y, totalText, INK.title); + image.drawText(large, frame.left, y, totalText, INK.title); drawTextRight( image, small, - size.width - MARGIN, + frame.right, y + large.lineHeight - small.lineHeight - 1, `${Math.round(sleep.efficiencyPercent)}% efficiency`, INK.label, ); y += large.lineHeight + 6; - const footerHeight = small.lineHeight + 8; const captionHeight = step; - const laneHeight = size.height - y - MARGIN - footerHeight - captionHeight; + const laneHeight = frame.bottom - footerHeight(fonts) - captionHeight - y; if (laneHeight >= 40) { drawSleepLanes( image, - { x: MARGIN, y, width: size.width - MARGIN * 2, height: laneHeight }, + { x: frame.left, y, width: frame.right - frame.left, height: laneHeight }, { bands: data.stageBands, font: small, @@ -523,6 +549,6 @@ function drawSleepDetail( y += laneHeight; } - image.drawText(small, MARGIN, y, hypnogramCaptionShort(), INK.faint); + image.drawText(small, frame.left, y, hypnogramCaptionShort(), INK.faint); drawFooter(image, size, fonts, `${GESTURE_SCROLL} Calories · Summary`); } diff --git a/app/ui/dashboard-settings.ts b/app/ui/dashboard-settings.ts index 98720497..18bfac35 100644 --- a/app/ui/dashboard-settings.ts +++ b/app/ui/dashboard-settings.ts @@ -1797,26 +1797,29 @@ export class EditTextSettingLayer implements Layer { const font = getDefaultSmallFont(); // Sized to the hosting stack (full lens in the dashboard window, app // viewport in the settings app). - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const step = lineStep(font); - image.drawText(font, 22, 16, this.setting.glassesEditTitle, 220); - const messageTop = 24 + 2 * font.lineHeight; - const message = wrapText(font, "Look at the phone app to type a value.", width - 48); + // A text column inside the padding suggested for each window edge. + const x = padding.left; + const textWidth = width - padding.left - padding.right; + image.drawText(font, x, padding.top, this.setting.glassesEditTitle, 220); + const messageTop = padding.top + 8 + 2 * font.lineHeight; + const message = wrapText(font, "Look at the phone app to type a value.", textWidth); for (let index = 0; index < message.length; index++) { - image.drawText(font, 22, messageTop + index * step, message[index]!, 200); + image.drawText(font, x, messageTop + index * step, message[index]!, 200); } const valueY = messageTop + (message.length + 2) * step; - image.drawText(font, 22, valueY, truncateSetting(this.setting.get(), 52), 220); + image.drawText(font, x, valueY, truncateSetting(this.setting.get(), 52), 220); const error = this.setting.validationError(); - const errorLines = error ? wrapText(font, error, width - 48) : []; + const errorLines = error ? wrapText(font, error, textWidth) : []; errorLines.forEach((line, index) => - image.drawText(font, 22, messageTop + (message.length + 4 + index) * step, line, 180)); + image.drawText(font, x, messageTop + (message.length + 4 + index) * step, line, 180)); // The back hint at the bottom, unless a short window (the RayNeo iO's) // leaves it no room under the value and error. const contentBottom = errorLines.length ? messageTop + (message.length + 4 + errorLines.length) * step : valueY + step; - const hintY = height - 24 - font.lineHeight; - if (hintY >= contentBottom) image.drawText(font, 22, hintY, `${GESTURE_DOUBLE_CLICK} back`, 110); + const hintY = height - padding.bottom - font.lineHeight; + if (hintY >= contentBottom) image.drawText(font, x, hintY, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } diff --git a/app/ui/dashboard/settings-panel.ts b/app/ui/dashboard/settings-panel.ts index 1e0bcf40..cb113433 100644 --- a/app/ui/dashboard/settings-panel.ts +++ b/app/ui/dashboard/settings-panel.ts @@ -7,6 +7,7 @@ import { isMenuItemDisabled, MenuItem, MenuLayer, openModalMenu } from "../menu" import { Menu } from "../menu-core"; import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../metrics"; import { shell } from "../shell/shell"; +import type { Viewport } from "../viewport"; /** * One left-column entry. `items` are the right-column rows (reusing the shared @@ -29,8 +30,12 @@ export type SettingsSection = { }) => number | void; }; -const PAD = 8; const LEFT_W = 150; +/** Between the left column's rows and the divider, and between the divider and the right column's content. */ +const DIVIDER_GAP_LEFT = 8; +const DIVIDER_GAP_RIGHT = 14; +/** How far a right-column row's selection box overhangs the column's content on the left. */ +const RIGHT_ROW_OVERHANG = 2; /** Gap between a row's selection box and the next row. */ const ROW_GAP = 2; /** Horizontal inset of row text (and custom row content) from its selection box. */ @@ -114,27 +119,27 @@ export class SettingsPanelLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const viewport = ctx.stack.getBaseSize(); + const { width, height, padding } = viewport; const image = new GrayImage(width, height, 0); const appFocused = ctx.stack.isFocused(); const section = this.section(); const rightItems = section.items; this.rightMenu.setItems(rightItems); - const top = PAD; - const listBottom = height - PAD; + // Fixed content keeps the padding suggested for the window's edges. The + // two lists scroll, so they run to the top and bottom edges themselves, + // with that padding inside them (before the first row, after the last). + const { top, bottom: listBottom, divX, rightX, rightW } = panelColumns(viewport); // Left column: section labels. - this.leftMenu.paint(image, { x: PAD - 2, y: top, width: LEFT_W, height: listBottom - top }, + this.leftMenu.paint(image, { x: padding.left, y: 0, width: LEFT_W, height, padTop: padding.top, padBottom: padding.bottom }, appFocused && this.focus === "left"); // Column divider (a light rule, not a full border). - const divX = PAD + LEFT_W + 6; image.drawLine(divX, top, divX, listBottom, 40); // Right column: the highlighted section's contents. - const rightX = divX + 14; - const rightW = width - rightX - PAD; let rightTop = top; if (section.renderDetail && !this.detailCrowdsList(section, ctx, font, { x: rightX, y: top, width: rightW, height: listBottom - top })) { const used = section.renderDetail({ image, x: rightX, y: top, width: rightW, height: listBottom - top, ctx }); @@ -144,12 +149,20 @@ export class SettingsPanelLayer implements Layer { // Once focus is in the right column, the selected item's extended // description (when it has one) is drawn over the bottom of the pane by // the companion SettingsDescriptionOverlayLayer. The list stops above - // that band, so the selected row always stays clear of it. - const listHeight = listBottom - this.descriptionHeight(font, rightW) - rightTop; - if (listHeight > 0) { + // that band, so the selected row always stays clear of it. Under a + // detail, and above that band, the list ends short of the window's + // edge, where it needs no padding of its own. + const description = this.descriptionHeight(font, rightW); + const fromEdge = rightTop === top; + const listTop = fromEdge ? 0 : rightTop; + const listEnd = description ? listBottom - description : height; + if (listEnd > rightTop) { this.paintCtx = ctx; try { - this.rightMenu.paint(image, { x: rightX - 2, y: rightTop, width: rightW + 2, height: listHeight }, appFocused); + this.rightMenu.paint(image, { + x: rightX - RIGHT_ROW_OVERHANG, y: listTop, width: rightW + RIGHT_ROW_OVERHANG, height: listEnd - listTop, + padTop: fromEdge ? padding.top : 0, padBottom: description ? 0 : padding.bottom, + }, appFocused); } finally { this.paintCtx = null; } @@ -202,10 +215,7 @@ export class SettingsPanelLayer implements Layer { */ paintDescriptionOverlay(image: GrayImage, ctx: LayerContext): void { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); - const listBottom = height - PAD; - const rightX = PAD + LEFT_W + 6 + 14; - const rightW = width - rightX - PAD; + const { bottom: listBottom, rightX, rightW } = panelColumns(ctx.stack.getBaseSize()); const lines = this.descriptionLines(font, rightW); if (!lines.length) return; const top = listBottom - (lines.length * lineStep(font) + 10); @@ -273,6 +283,19 @@ export class SettingsPanelLayer implements Layer { } } +/** + * The panel's fixed geometry in a viewport: the rows fixed content spans + * (inside the padding suggested for the top and bottom edges), the divider's + * x, and the right column's content span. The left column's rows start at + * the left edge's padding; the right column ends at the right edge's. + */ +function panelColumns(viewport: Viewport): { top: number; bottom: number; divX: number; rightX: number; rightW: number } { + const { width, height, padding } = viewport; + const divX = padding.left + LEFT_W + DIVIDER_GAP_LEFT; + const rightX = divX + DIVIDER_GAP_RIGHT; + return { top: padding.top, bottom: height - padding.bottom, divX, rightX, rightW: width - rightX - padding.right }; +} + /** Scroll and watch swipe-up/down both move a column's selection. */ function scrollEventFor(event: InputEvent): InputEvent | null { if (event.type === "scroll-up" || event.type === "swipe-up") return { ...event, type: "scroll-up" } as InputEvent; diff --git a/app/ui/document/document-view.ts b/app/ui/document/document-view.ts index c4d5813d..1040febb 100644 --- a/app/ui/document/document-view.ts +++ b/app/ui/document/document-view.ts @@ -3,7 +3,9 @@ * region: nested nodes indent, todo nodes get checkboxes, links underline, * code blocks get a shaded box. Selection moves over nodes (a node's wrapped * lines highlight as one unit) and the scroll position follows it, matching - * the house list style in ui/menu.ts. + * the house list style in ui/menu.ts. Scrolling is by whole lines: the + * region's top always shows the start of a line (or the padding before the + * first one), and a line that would be cut off at its bottom is left out. */ import { GrayImage, type UiFont } from "../../graphics/image"; import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts"; @@ -22,6 +24,16 @@ const MARKER_GUTTER = 16; const RIGHT_PAD = 10; const CODE_INSET = 6; +/** + * The size of the region a DocumentView scrolls in. padTop and padBottom are + * blank space inside the scrolled content, before the first line and after + * the last (default none), which scrolls away with the lines. A view whose + * region runs to a window's top or bottom edge passes the padding suggested + * for that edge (viewport.ts): its end lines then keep clear of a border + * line there, while lines scrolled past them use the room up to the edge. + */ +export type DocumentViewSize = { width: number; height: number; padTop?: number; padBottom?: number }; + type SegmentStyle = "plain" | "link" | "code"; type Segment = { text: string; x: number; style: SegmentStyle }; @@ -46,18 +58,29 @@ export class DocumentView { private lines: LayoutLine[] = []; private selectable: DocNode[] = []; private selectedIndex = 0; + /** Index of the line at the region's top; at 0 the top padding shows above it. */ private scrollLine = 0; + private width: number; + private height: number; + private padTop: number; + private padBottom: number; - constructor( - private width: number, - private height: number, - ) {} + constructor(size: DocumentViewSize) { + this.width = size.width; + this.height = size.height; + this.padTop = size.padTop ?? 0; + this.padBottom = size.padBottom ?? 0; + } /** Re-wrap the document for a region of a new size, keeping the selection in view. */ - resize(width: number, height: number): void { - if (width === this.width && height === this.height) return; - this.width = width; - this.height = height; + resize(size: DocumentViewSize): void { + const padTop = size.padTop ?? 0; + const padBottom = size.padBottom ?? 0; + if (size.width === this.width && size.height === this.height && padTop === this.padTop && padBottom === this.padBottom) return; + this.width = size.width; + this.height = size.height; + this.padTop = padTop; + this.padBottom = padBottom; if (this.root) this.setDocument(this.root); } @@ -101,6 +124,7 @@ export class DocumentView { paint(target: GrayImage, x: number, y: number, focused: boolean): void { const image = new GrayImage(this.width, this.height, 0); const selected = this.selectedNode(); + const firstLineY = this.leadAt(this.scrollLine); const visibleCount = this.visibleCountFrom(this.scrollLine); const lastVisible = Math.min(this.lines.length, this.scrollLine + visibleCount); @@ -109,7 +133,7 @@ export class DocumentView { let top: number | null = null; let bottom = 0; let indentX = 0; - let lineY = 0; + let lineY = firstLineY; for (let index = this.scrollLine; index < lastVisible; index++) { const line = this.lines[index]!; if (line.node === selected) { @@ -127,7 +151,7 @@ export class DocumentView { } } - let lineY = 0; + let lineY = firstLineY; for (let index = this.scrollLine; index < lastVisible; index++) { const line = this.lines[index]!; const isSelected = line.node === selected; @@ -143,7 +167,9 @@ export class DocumentView { } if (this.lines.length > visibleCount) { - drawListScrollbar(image, this.width - 3, 0, this.height, this.scrollLine, visibleCount, this.lines.length); + // The track keeps inside the padding, as the document's end lines do. + const trackHeight = Math.max(0, this.height - this.padTop - this.padBottom); + drawListScrollbar(image, this.width - 3, this.padTop, trackHeight, this.scrollLine, visibleCount, this.lines.length); } target.bitBlt(image, x, y); } @@ -257,8 +283,14 @@ export class DocumentView { // ------------------------------------------------------------------------- // Scrolling + /** Blank space above the line at the region's top: the top padding, until it scrolls away. */ + private leadAt(scrollLine: number): number { + return scrollLine === 0 ? this.padTop : 0; + } + + /** How many whole lines the region shows when scrolled to `start` (never fewer than one). */ private visibleCountFrom(start: number): number { - let used = 0; + let used = this.leadAt(start); let count = 0; for (let index = start; index < this.lines.length; index++) { used += this.lines[index]!.height; @@ -268,11 +300,22 @@ export class DocumentView { return Math.max(1, count); } + /** + * Whether the region, scrolled to `start`, shows the lines through `end` + * whole, and after the document's last line the bottom padding too. + */ + private showsThrough(start: number, end: number): boolean { + let used = this.leadAt(start) + (end === this.lines.length - 1 ? this.padBottom : 0); + for (let index = start; index <= end && used <= this.height; index++) used += this.lines[index]!.height; + return used <= this.height; + } + + /** The furthest the view scrolls: the first line from which the rest fits, bottom padding included. */ private maxScrollLine(): number { - let used = 0; + let used = this.padBottom; for (let index = this.lines.length - 1; index >= 0; index--) { used += this.lines[index]!.height; - if (used > this.height) return index + 1; + if (used + this.leadAt(index) > this.height) return Math.min(index + 1, this.lines.length - 1); } return 0; } @@ -289,9 +332,15 @@ export class DocumentView { } } if (first < 0) return; + // Lines no selection reaches belong with the end node next to them: + // selecting the first node shows the document's start, and the last its + // end, with the padding there, when that fits along with the node. + const end = this.lines.length - 1; + if (this.selectedIndex === 0 && this.showsThrough(0, last)) first = 0; + if (this.selectedIndex === this.selectable.length - 1 && this.showsThrough(first, end)) last = end; let scroll = this.scrollLine; // Scroll down the minimum needed; a node taller than the view pins its top. - while (last >= scroll + this.visibleCountFrom(scroll) && scroll < first) scroll++; + while (scroll < first && !this.showsThrough(scroll, last)) scroll++; if (first < scroll) scroll = first; this.scrollLine = clamp(scroll, 0, this.maxScrollLine()); } diff --git a/app/ui/font-picker.ts b/app/ui/font-picker.ts index 7698904c..ea409b67 100644 --- a/app/ui/font-picker.ts +++ b/app/ui/font-picker.ts @@ -35,6 +35,11 @@ import type { Layer, LayerContext } from "./layers"; /** Pixel sizes offered for TTF faces (also by the onboarding's text-size page). */ export const FONT_SIZE_CHOICES = [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 22, 24, 26, 28] as const; const DEFAULT_TTF_SIZE = 16; +/** Row text (and the title, in line with it) from the rows' highlight boxes. */ +const ROW_TEXT_INSET = 10; +/** The title: below the window's top padding, and the gap under its line. */ +const TITLE_OFFSET_Y = 2; +const TITLE_GAP = 10; const PREVIEW_TEXT = "The quick brown fox jumps over 0123456789"; const INHERIT_UI_FONT_LABEL = "Same as UI font"; const UI_BITMAP_FACES: FontPickerOptions["bitmapFaces"] = [ @@ -132,27 +137,31 @@ export class FontPickerLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); const focused = ctx.stack.isFocused(); - image.drawText(font, 22, 14, this.options.title, 220); + // The rows' highlight boxes span the window inside the padding suggested + // for its sides; the title and the rows' text sit ROW_TEXT_INSET in. + const rowX = padding.left + ROW_TEXT_INSET; + const rowWidth = width - padding.left - padding.right - 2 * ROW_TEXT_INSET; + const titleY = padding.top + TITLE_OFFSET_Y; + image.drawText(font, rowX, titleY, this.options.title, 220); - const rowX = 22; - const rowWidth = width - 2 * rowX; const rowHeight = listRowHeight(font); - const rowsTop = 24 + font.lineHeight; + const rowsTop = titleY + font.lineHeight + TITLE_GAP; const rowsHeight = ROWS.length * rowHeight; + const bottom = height - padding.bottom; const preview = this.previewFont(); const info = `Line height: ${preview.lineHeight}px`; // Preview under the rows, where its sample line and the info line fit // (the G2's window): a separator, then the sample line in the draft font. const previewTop = rowsTop + rowsHeight + 10; - if (previewTop + 8 + preview.lineHeight + 6 + font.lineHeight <= height) { - this.menu.paint(image, { x: rowX - 10, y: rowsTop, width: rowWidth + 20, height: rowsHeight }, focused); - image.drawLine(rowX - 10, previewTop, rowX + rowWidth + 10, previewTop, 60); + if (previewTop + 8 + preview.lineHeight + 6 + font.lineHeight <= bottom) { + this.menu.paint(image, { x: rowX - ROW_TEXT_INSET, y: rowsTop, width: rowWidth + 2 * ROW_TEXT_INSET, height: rowsHeight }, focused); + image.drawLine(rowX - ROW_TEXT_INSET, previewTop, rowX + rowWidth + ROW_TEXT_INSET, previewTop, 60); const sample = - preview.measureText(PREVIEW_TEXT) <= rowWidth + 20 ? PREVIEW_TEXT : "The quick brown fox 0123"; + preview.measureText(PREVIEW_TEXT) <= rowWidth + 2 * ROW_TEXT_INSET ? PREVIEW_TEXT : "The quick brown fox 0123"; preview.drawText(image, rowX - 4, previewTop + 8, sample, 230); image.drawText(font, rowX - 4, previewTop + 8 + preview.lineHeight + 6, info, 110); return image; @@ -161,12 +170,11 @@ export class FontPickerLayer implements Layer { // Else beside them (a short window, as on the RayNeo iO), the sample // wrapped to the column, as many lines as fit above the info line. const columnWidth = Math.round(rowWidth / 2); - const bottom = height - 8; - this.menu.paint(image, { x: rowX - 10, y: rowsTop, width: columnWidth + 20, height: Math.min(rowsHeight, bottom - rowsTop) }, focused); - const dividerX = rowX + columnWidth + 20; + this.menu.paint(image, { x: rowX - ROW_TEXT_INSET, y: rowsTop, width: columnWidth + 2 * ROW_TEXT_INSET, height: Math.min(rowsHeight, bottom - rowsTop) }, focused); + const dividerX = rowX + columnWidth + 2 * ROW_TEXT_INSET; image.drawLine(dividerX, rowsTop, dividerX, Math.min(rowsTop + rowsHeight, bottom), 60); const previewX = dividerX + 12; - const lines = wrapText(preview, PREVIEW_TEXT, width - previewX - 12); + const lines = wrapText(preview, PREVIEW_TEXT, width - padding.right - previewX); const infoY = bottom - font.lineHeight; let y = rowsTop + 4; for (const line of lines) { @@ -178,11 +186,11 @@ export class FontPickerLayer implements Layer { return image; } - /** Row box = highlight box; text sits 10px in from its left edge and 10px from its right. */ + /** Row box = highlight box; text sits ROW_TEXT_INSET in from its left and right edges. */ private drawRow({ image, item: row, x, y, width, selected }: MenuDrawArgs): void { const font = getDefaultSmallFont(); - const textX = x + 10; - const textWidth = width - 20; + const textX = x + ROW_TEXT_INSET; + const textWidth = width - 2 * ROW_TEXT_INSET; if (row === "save") { image.drawText(font, textX, y + LIST_ROW_TEXT_INSET, "Save", selected ? 255 : 200); return; diff --git a/app/ui/icon-grid.ts b/app/ui/icon-grid.ts index ba50e892..6037e8a1 100644 --- a/app/ui/icon-grid.ts +++ b/app/ui/icon-grid.ts @@ -22,12 +22,11 @@ const DEFAULT_COLUMNS = 5; const DEFAULT_LABEL_VALUE = 210; /** Wide rows: text inset from the grid's left and right edges. */ const WIDE_TEXT_X = 20; -/** The scrollbar's columns at the right edge, which animations leave alone. */ +/** Columns a grid with a scrollbar keeps for it at the box's right edge, clear of its rows. */ const SCROLLBAR_GUTTER = 5; -/** How far a row-mode highlight band is inset from the box's left and right edges. */ -export const ICON_GRID_BAND_INSET_X = 4; +const SCROLLBAR_WIDTH = 3; /** How far a grid row's highlight (band or cell) is inset from the row's top and bottom. */ -export const ICON_GRID_HIGHLIGHT_INSET_Y = 2; +const HIGHLIGHT_INSET_Y = 2; export type IconGridCell = { label: string; @@ -69,7 +68,10 @@ export type IconGridOptions = { * always uses), even if the host reset the selection while navigating. */ onBack: (ctx: LayerContext) => boolean | void; - /** Draw a scrollbar at the right edge when rows overflow. Default false. */ + /** + * Draw a scrollbar at the box's right edge when rows overflow; the rows + * then always keep its few columns free. Default false. + */ scrollbar?: boolean; }; @@ -79,7 +81,16 @@ type GridRow = type GridMode = "row" | "item"; -type GridLayout = { box: MenuBox; rowH: number; colW: number }; +/** + * `lead` is the blank space before the first row, inside the scrolled + * content (see paint); `scrollOffset` is the content offset of the box's top. + */ +type GridLayout = { box: MenuBox; rowH: number; colW: number; lead: number; scrollOffset: number }; + +/** The part of a grid's box its rows take: all of it, less a scrollbar's gutter. */ +function rowsBox(box: MenuBox, scrollbar: boolean): MenuBox { + return scrollbar ? { ...box, width: Math.max(0, box.width - SCROLLBAR_GUTTER) } : box; +} /** * The selection highlight in image coordinates. `key` names what is @@ -101,6 +112,12 @@ type Highlight = { key: number; x: number; y: number; width: number; height: num * many rows fit. Space left below the last full row shows the top of the * next row, cut off at the box's edge, as a hint that there is more to scroll to. * + * The box is the scroll viewport, and a row-mode highlight band spans its + * width: a host insets it from a window's sides by the padding suggested + * for them (viewport.ts), and runs it to the window's top and bottom edges, + * giving their padding as the box's padTop and padBottom, which the grid + * keeps inside the scrolled content. + * * Ring input navigates on two levels, halving the scrolls to reach an item: * in row mode scroll picks a row and a click drops into item mode on it, * defaulting to the middle column; in item mode scroll traverses items @@ -196,21 +213,34 @@ export class IconGrid { } } - paint(image: GrayImage, box: MenuBox, focused: boolean): void { + /** + * Paint the grid into `box`, its scroll viewport. The box's padTop keeps + * the first row's highlight that far below the box's top while the grid + * is scrolled to its start, and padBottom keeps the last row's that far + * above its bottom at the end; rows scrolled past either are cut off at + * the box's edge itself. + */ + paint(image: GrayImage, viewportBox: MenuBox, focused: boolean): void { const font = getDefaultSmallFont(); const rows = this.buildRows(); this.clampSelection(rows); + const box = rowsBox(viewportBox, !!this.options.scrollbar); const rowH = iconGridMinRowHeight(font, ICON_GRID_ICON_SIZE, LABEL_GAP); - const fullRows = Math.max(1, Math.floor(box.height / rowH)); + // A row's highlight already sits HIGHLIGHT_INSET_Y inside it. + const lead = Math.max(0, (box.padTop ?? 0) - HIGHLIGHT_INSET_Y); + const tail = Math.max(0, (box.padBottom ?? 0) - HIGHLIGHT_INSET_Y); + // Rows that fit whole wherever the grid is scrolled to: the rest of the + // box shows the next row cut off. + const fullRows = Math.max(1, Math.floor((box.height - lead - tail) / rowH)); // Scroll to keep the selected row among the fully-visible rows. this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, fullRows, rows.length); - const layout: GridLayout = { box, rowH, colW: box.width / this.columns }; + const layout: GridLayout = { box, rowH, colW: box.width / this.columns, lead, scrollOffset: scrollOffsetFor(this.scrollRow, rowH, lead) }; this.layout = layout; const highlight = this.highlight(rows, layout, font, focused); const bottom = box.y + box.height; for (let rowIndex = this.scrollRow; rowIndex < rows.length; rowIndex++) { - const y = box.y + (rowIndex - this.scrollRow) * rowH; + const y = rowTop(layout, rowIndex); if (y >= bottom) break; if (highlight && rowIndex === this.selectedRow) { drawSelectionHighlight(image, highlight.x, highlight.y, highlight.width, highlight.height, focused, highlight.radius); @@ -218,11 +248,13 @@ export class IconGrid { this.drawRow(image, rows[rowIndex]!, rowIndex, box.x, y, layout, font, bottom); } - const scrollbar = !!this.options.scrollbar && rows.length > fullRows; - if (scrollbar) { - drawListScrollbar(image, box.x + box.width - SCROLLBAR_GUTTER, box.y, box.height, this.scrollRow, fullRows, rows.length); + if (this.options.scrollbar && rows.length > fullRows) { + // Fixed, unlike the rows: it keeps inside the box's padding. + const trackTop = box.y + (box.padTop ?? 0); + const trackHeight = box.height - (box.padTop ?? 0) - (box.padBottom ?? 0); + drawListScrollbar(image, viewportBox.x + viewportBox.width - SCROLLBAR_WIDTH, trackTop, trackHeight, this.scrollRow, fullRows, rows.length); } - this.paintMotion(image, rows, layout, font, focused, highlight, scrollbar); + this.paintMotion(image, rows, layout, font, focused, highlight); } async handleInput(event: InputEvent, ctx: LayerContext): Promise { @@ -240,10 +272,10 @@ export class IconGrid { */ async hitTest(x: number, y: number, ctx: LayerContext): Promise { if (!this.layout) return false; - const { box, rowH, colW } = this.layout; + const { box, rowH, colW, lead, scrollOffset } = this.layout; if (x < box.x || x >= box.x + box.width || y < box.y || y >= box.y + box.height) return false; - const rowIndex = this.scrollRow + Math.floor((y - box.y) / rowH); - const row = this.buildRows()[rowIndex]; + const rowIndex = Math.floor((y - box.y + scrollOffset - lead) / rowH); + const row = rowIndex >= 0 ? this.buildRows()[rowIndex] : undefined; if (!row) return false; if (row.kind === "cells") { const col = Math.floor((x - box.x) / colW); @@ -269,19 +301,18 @@ export class IconGrid { const row = rows[this.selectedRow]; if (!row) return null; const { box, rowH, colW } = layout; - const y = box.y + (this.selectedRow - this.scrollRow) * rowH; + const y = rowTop(layout, this.selectedRow); if (row.kind === "wide") { const textY = y + wideTextOffset(rowH, font); return { key: row.firstIndex, x: box.x + WIDE_TEXT_X - 6, y: textY - 2, width: box.width - 2 * WIDE_TEXT_X + 12, height: font.lineHeight + 4, radius: 4 }; } - const insetY = ICON_GRID_HIGHLIGHT_INSET_Y; + const insetY = HIGHLIGHT_INSET_Y; if (this.mode === "item" || (!focused && shell.lastInputWasWatch())) { return { key: row.firstIndex + this.selectedCol, x: Math.round(box.x + this.selectedCol * colW) + 6, y: y + insetY, width: Math.round(colW) - 12, height: rowH - 2 * insetY, radius: 6 }; } - return { key: -1 - this.selectedRow, x: box.x + ICON_GRID_BAND_INSET_X, y: y + insetY, - width: box.width - 2 * ICON_GRID_BAND_INSET_X, height: rowH - 2 * insetY, radius: 6 }; + return { key: -1 - this.selectedRow, x: box.x, y: y + insetY, width: box.width, height: rowH - 2 * insetY, radius: 6 }; } /** @@ -325,16 +356,16 @@ export class IconGrid { * moving, retain the display list that animates them over the static paint. */ private paintMotion(image: GrayImage, rows: readonly GridRow[], layout: GridLayout, font: UiFont, - focused: boolean, highlight: Highlight | null, scrollbar: boolean): void { + focused: boolean, highlight: Highlight | null): void { const now = Date.now(); - const scrollOffset = this.scrollRow * layout.rowH; + const { scrollOffset } = layout; const scroll = this.scrollMotion.paint(scrollOffset, layout.box, now); const slide = highlight ? this.highlightMotion.paint(highlight.key, scrollOffset, highlight.x, highlight.y, highlight.width, highlight.height, now, !!scroll) : undefined; if (!scroll && !slide) return; const { box } = layout; - const clip: ListClip = { x: 0, y: 0, width: box.width - (scrollbar ? SCROLLBAR_GUTTER : 0), height: box.height }; + const clip: ListClip = { x: 0, y: 0, width: box.width, height: box.height }; const list = this.motionList(rows, layout, font, focused, highlight, clip, scroll, slide); if (!list) { this.scrollMotion.cancel(); @@ -353,11 +384,10 @@ export class IconGrid { private motionList(rows: readonly GridRow[], layout: GridLayout, font: UiFont, focused: boolean, highlight: Highlight | null, clip: ListClip, scroll: MenuScrollAnimation | undefined, slide: MenuHighlightAnimation | undefined): DisplayList | null { - const { box, rowH } = layout; - const scrollOffset = this.scrollRow * rowH; + const { box, rowH, lead, scrollOffset } = layout; const offsets = scroll ? [scroll.from, scroll.to, scroll.peak ?? scroll.to] : [scrollOffset]; - const firstRow = Math.max(0, Math.floor(Math.min(...offsets) / rowH)); - const lastRow = Math.min(rows.length, Math.ceil((Math.max(...offsets) + box.height) / rowH)); + const firstRow = Math.max(0, Math.floor((Math.min(...offsets) - lead) / rowH)); + const lastRow = Math.min(rows.length, Math.ceil((Math.max(...offsets) + box.height - lead) / rowH)); const content = new GrayImage(box.width, Math.max(1, (lastRow - firstRow) * rowH)); for (let rowIndex = firstRow; rowIndex < lastRow; rowIndex++) { this.drawRow(content, rows[rowIndex]!, rowIndex, 0, (rowIndex - firstRow) * rowH, layout, font); @@ -380,7 +410,7 @@ export class IconGrid { background: focused ? q(MENU_HIGHLIGHT_FILL) : 0, border: q(MENU_HIGHLIGHT_STROKE), clip }); } // Without a scroll the rows stay put; a literal keeps every compiled call small. - const rowsY = scroll ? E.i32(firstRow * rowH).sub(offset) : (firstRow - this.scrollRow) * rowH; + const rowsY = scroll ? E.i32(lead + firstRow * rowH).sub(offset) : lead + firstRow * rowH - scrollOffset; calls.push({ op: DrawOp.DRAWS, x: 0, y: rowsY, records: replay.records, count: replay.count, source: content, clip }); return { resources: [], timeline: { token: timeline.token, startedAt: timeline.startedAt }, calls }; } @@ -569,6 +599,20 @@ export class IconGrid { } } +/** + * Content offset of the box's top with `scrollRow` as the first row shown: + * that row's top, or for the first row the content's, so the space before + * it shows too. + */ +function scrollOffsetFor(scrollRow: number, rowH: number, lead: number): number { + return scrollRow === 0 ? 0 : lead + scrollRow * rowH; +} + +/** Image y of a row's top in the layout's box as scrolled. */ +function rowTop(layout: GridLayout, rowIndex: number): number { + return layout.box.y + layout.lead + rowIndex * layout.rowH - layout.scrollOffset; +} + function rowLength(row: GridRow): number { return row.kind === "wide" ? 1 : row.items.length; } diff --git a/app/ui/layers.ts b/app/ui/layers.ts index e55502b4..1c010619 100644 --- a/app/ui/layers.ts +++ b/app/ui/layers.ts @@ -4,6 +4,7 @@ import { dimPlanes, type Plane } from "../graphics/plane"; import { spanCurrent } from "../native/frame-timings"; import { type ConfigSettingBoolean, type ConfigSettingString, type VoiceProvider } from "./dashboard-settings"; import { directionalFallback, isDirectionalInput, type InputEvent } from "./gestures"; +import { toViewport, type Viewport, type ViewportSize } from "./viewport"; export type TextSettingsEditToggle = { setting: ConfigSettingBoolean; @@ -155,18 +156,19 @@ export class LayerStack { private readonly shellKeys = new WeakMap(); private readonly ctx: LayerContext; /** - * The base viewport's size, or null to follow the paired glasses' screen - * as it is now: the shell's own stack lives for the whole process, and + * The base viewport, or null to follow the paired glasses' screen as it + * is now: the shell's own stack lives for the whole process, and * re-pairing with the other glasses type changes the screen under it. */ - private baseSize: { width: number; height: number } | null; + private baseSize: Viewport | null; private readonly focusedFn: () => boolean; private lastBaseDim: number | false = 1; constructor( baseLayer: Layer, actions: LayerActions, - baseSize?: { width: number; height: number }, + // A bare size is a viewport nothing borders (see toViewport). + baseSize?: ViewportSize | Viewport, // Whether this stack is the current input target; drives the strength of // selection highlights (a visible-but-unfocused window dims its selection). // Defaults to always-focused for shell overlays and standalone stacks. @@ -177,7 +179,7 @@ export class LayerStack { stack: this, actions, }; - this.baseSize = baseSize ? { width: baseSize.width, height: baseSize.height } : null; + this.baseSize = baseSize ? toViewport(baseSize) : null; this.focusedFn = isFocused; } @@ -247,13 +249,22 @@ export class LayerStack { return this.layers.length === 1; } - getBaseSize(): { width: number; height: number } { - return this.baseSize ? { ...this.baseSize } : { width: screenWidth(), height: screenHeight() }; + /** + * The viewport layers lay out in: its size, what each of its edges + * touches, and the padding suggested for them (see viewport.ts). A host + * that gave only a size, and the shell's own screen-sized stack, have + * edges nothing borders and no padding. + */ + getBaseSize(): Viewport { + return toViewport(this.baseSize ?? { width: screenWidth(), height: screenHeight() }); } - /** Resize the base viewport (e.g. an EvenHub window switching to the tall canvas). */ - setBaseSize(size: { width: number; height: number }): void { - this.baseSize = { width: size.width, height: size.height }; + /** + * Resize the base viewport (e.g. an EvenHub window switching to the tall + * canvas), or change what its edges touch. + */ + setBaseSize(size: ViewportSize | Viewport): void { + this.baseSize = toViewport(size); } setActions(actions: Partial): void { diff --git a/app/ui/listening-header.ts b/app/ui/listening-header.ts index a93a2370..ffbb764a 100644 --- a/app/ui/listening-header.ts +++ b/app/ui/listening-header.ts @@ -2,9 +2,8 @@ import { type GrayImage, type UiFont } from "../graphics/image"; import { truncateText } from "../graphics/textwrap"; import { voiceControlBridge } from "../native/voice-control"; import { lineStep } from "./metrics"; +import type { Edges } from "./viewport"; -const HEADER_X = 24; -const HEADER_Y = 20; const TITLE_GRAY = 200; const STATUS_GRAY = 110; @@ -66,8 +65,9 @@ export class MicLevelMeter { /** * The one-line header of the listening views (Transcribe, Translate): * "Title · status", the status truncated to fit, then the mic level meter at - * the line's end when one is given (while listening). Returns the y at which - * the view's body starts. + * the line's end when one is given (while listening). It spans the image + * inside `padding`, the padding suggested for the window's edges + * (viewport.ts). Returns the y at which the view's body starts. */ export function paintListeningHeader( image: GrayImage, @@ -75,18 +75,20 @@ export function paintListeningHeader( title: string, status: string, meter: MicLevelMeter | null, + padding: Edges, ): number { - let textRight = image.width - HEADER_X; + const x = padding.left, y = padding.top; + let textRight = image.width - padding.right; if (meter) { const meterX = textRight - meter.width; // Roughly cap height, sitting on the text's baseline. - meter.paint(image, meterX, HEADER_Y + font.ascent, Math.max(4, Math.round(font.ascent * 0.6))); + meter.paint(image, meterX, y + font.ascent, Math.max(4, Math.round(font.ascent * 0.6))); textRight = meterX - METER_GAP; } - image.drawText(font, HEADER_X, HEADER_Y, title, TITLE_GRAY); + image.drawText(font, x, y, title, TITLE_GRAY); if (status) { - const statusX = HEADER_X + Math.ceil(font.measureText(title)); - image.drawText(font, statusX, HEADER_Y, truncateText(font, ` · ${status}`, textRight - statusX), STATUS_GRAY); + const statusX = x + Math.ceil(font.measureText(title)); + image.drawText(font, statusX, y, truncateText(font, ` · ${status}`, textRight - statusX), STATUS_GRAY); } - return HEADER_Y + lineStep(font) + 2; + return y + lineStep(font) + 2; } diff --git a/app/ui/menu-core.ts b/app/ui/menu-core.ts index 99ce3ee1..f0258073 100644 --- a/app/ui/menu-core.ts +++ b/app/ui/menu-core.ts @@ -18,8 +18,15 @@ const MAX_SCROLL_STRIP_BYTES = 65536; export const BOUNCE_FRACTION = 0.4; export const MAX_BOUNCE = 24; -/** A rectangle in image coordinates. */ -export type MenuBox = { x: number; y: number; width: number; height: number }; +/** + * A rectangle in image coordinates. padTop and padBottom are blank rows + * inside the scrolled content, before the first row and after the last + * (default none): they scroll away with the rows. A list whose box runs to + * a window's top or bottom edge passes the padding suggested for that edge + * (viewport.ts), so its end rows keep clear of a border line there while + * rows scrolled past them are cut off right at the edge. + */ +export type MenuBox = { x: number; y: number; width: number; height: number; padTop?: number; padBottom?: number }; export type MenuDrawArgs = { /** @@ -73,6 +80,7 @@ export type MenuOptions = { highlight?: MenuHighlightStyle | false; }; +/** Row tops and pitches in content coordinates; `total` includes the box's padTop and padBottom. */ type MenuLayout = { tops: number[]; heights: number[]; total: number }; /** @@ -144,7 +152,7 @@ export class Menu { /** True when the content did not fit the box given to the last paint. */ get overflows(): boolean { if (!this.lastBox) return false; - return this.layout(this.lastBox.width).total > this.lastBox.height; + return this.layout(this.lastBox).total > this.lastBox.height; } /** @@ -154,15 +162,15 @@ export class Menu { * viewport keeps the selected row at the same on-screen position when it can. */ setItems(items: readonly T[], selectedIndex?: number | null): void { - const width = this.lastBox?.width; - const before = this.selected !== null && width !== undefined - ? this.layout(width).tops[this.selected]! - this.scrollY + const box = this.lastBox; + const before = this.selected !== null && box + ? this.layout(box).tops[this.selected]! - this.scrollY : null; this.itemList = items; if (selectedIndex !== undefined) this.deselected = selectedIndex === null; this.selected = this.settle(selectedIndex === undefined ? this.selected : selectedIndex); - if (before !== null && this.selected !== null && width !== undefined) { - this.scrollY = Math.max(0, this.layout(width).tops[this.selected]! - before); + if (before !== null && this.selected !== null && box) { + this.scrollY = Math.max(0, this.layout(box).tops[this.selected]! - before); } } @@ -197,7 +205,7 @@ export class Menu { this.selected = wrapped; this.deselected = false; } else if (from === null && this.lastBox) { - this.scrollY = delta > 0 ? 0 : this.maxScroll(this.layout(this.lastBox.width), this.lastBox.height); + this.scrollY = delta > 0 ? 0 : this.maxScroll(this.layout(this.lastBox), this.lastBox.height); } return; } @@ -234,7 +242,7 @@ export class Menu { indexAt(y: number): number | null { const box = this.lastBox; if (!box || y < box.y || y >= box.y + box.height) return null; - const layout = this.layout(box.width); + const layout = this.layout(box); const contentY = y - box.y + this.scrollY; for (let index = 0; index < this.itemList.length; index++) { if (contentY >= layout.tops[index]! && contentY < layout.tops[index]! + layout.heights[index]!) { @@ -247,7 +255,7 @@ export class Menu { paint(image: GrayImage, box: MenuBox, focused: boolean): void { this.lastBox = box; this.reconcile(); - const layout = this.layout(box.width); + const layout = this.layout(box); this.ensureVisible(layout, box.height); const now = Date.now(); const scroll = this.scrollMotion.paint(this.scrollY, box, now); @@ -297,7 +305,7 @@ export class Menu { drawScrollbar(image: GrayImage, x: number, y: number, height: number): void { const box = this.lastBox; if (!box) return; - const layout = this.layout(box.width); + const layout = this.layout(box); if (layout.total <= box.height) return; const maxScroll = this.maxScroll(layout, box.height); const thumbHeight = Math.max(SCROLLBAR_MIN_THUMB, (height * box.height / layout.total) | 0); @@ -378,7 +386,7 @@ export class Menu { private bounce(delta: -1 | 1): void { const count = this.itemList.length; if (!this.lastBox || !count) return; - const layout = this.layout(this.lastBox.width); + const layout = this.layout(this.lastBox); const pitch = layout.heights[delta > 0 ? count - 1 : 0]!; const amount = Math.min(MAX_BOUNCE, Math.round(pitch * BOUNCE_FRACTION)); if (amount > 0) this.scrollMotion.bounce(delta * amount); @@ -417,20 +425,30 @@ export class Menu { this.selected = this.settle(this.selected); } - private layout(width: number): MenuLayout { + private layout(box: MenuBox): MenuLayout { const tops: number[] = []; const heights: number[] = []; - let total = 0; + let total = Math.max(0, (box.padTop ?? 0) | 0); for (const item of this.itemList) { - const height = Math.max(0, this.options.getHeight(item, width) | 0); + const height = Math.max(0, this.options.getHeight(item, box.width) | 0); tops.push(total); heights.push(height); total += height; } if (this.itemList.length) total -= this.options.rowGap ?? 0; + total += Math.max(0, (box.padBottom ?? 0) | 0); return { tops, heights, total }; } + /** + * Where the viewport's top rests to show row `index` from its start: the + * row's top, or for the first row the content's, so the padding above it + * shows too. + */ + private restTop(layout: MenuLayout, index: number): number { + return index === 0 ? 0 : layout.tops[index]!; + } + private maxScroll(layout: MenuLayout, viewportHeight: number): number { return Math.max(0, layout.total - viewportHeight); } @@ -443,16 +461,21 @@ export class Menu { } const gap = this.options.rowGap ?? 0; const selected = this.selected; + const last = this.itemList.length - 1; const top = layout.tops[selected]!; const bottom = top + layout.heights[selected]! - gap; + // The content's padding counts as part of its end rows: showing the + // first row shows the padding above it, and the last the padding below. + const startOf = (index: number) => this.restTop(layout, index); + const endOf = (index: number) => index === last ? layout.total : layout.tops[index]! + layout.heights[index]! - gap; // Keep the neighbors in view too when all three fit, so the user sees // what the next step will reach. - const contextTop = selected > 0 ? layout.tops[selected - 1]! : top; - const contextBottom = selected + 1 < this.itemList.length - ? layout.tops[selected + 1]! + layout.heights[selected + 1]! - gap : bottom; + const contextTop = startOf(Math.max(0, selected - 1)); + const contextBottom = endOf(Math.min(last, selected + 1)); + const paddedFits = endOf(selected) - startOf(selected) <= viewportHeight; const fits = contextBottom - contextTop <= viewportHeight; - const wantTop = fits ? contextTop : top; - const wantBottom = fits ? contextBottom : bottom; + const wantTop = fits ? contextTop : paddedFits ? startOf(selected) : top; + const wantBottom = fits ? contextBottom : paddedFits ? endOf(selected) : bottom; let target = this.scrollY; if (wantTop < target) { target = wantTop; @@ -462,8 +485,8 @@ export class Menu { // Snap to the first row that starts inside the viewport: the wanted // rows still fit (their bottom is within viewportHeight of that top). for (let index = 0; index <= selected; index++) { - if (layout.tops[index]! >= target) { - target = layout.tops[index]!; + if (startOf(index) >= target) { + target = startOf(index); break; } } @@ -474,7 +497,7 @@ export class Menu { // the list. The selected row stays visible, since it ends by the list's end. const maxScroll = this.maxScroll(layout, viewportHeight); if (target > maxScroll) { - const aligned = layout.tops.find((rowTop) => rowTop >= maxScroll); + const aligned = layout.tops.map((_, index) => startOf(index)).find((rowTop) => rowTop >= maxScroll); target = aligned !== undefined && aligned <= target ? aligned : maxScroll; } this.scrollY = Math.max(0, target); @@ -483,16 +506,17 @@ export class Menu { /** With nothing selectable, up/down page the content by rows. Returns false at the end being scrolled past. */ private scrollContent(delta: -1 | 1): boolean { if (!this.lastBox) return false; - const layout = this.layout(this.lastBox.width); + const layout = this.layout(this.lastBox); const maxScroll = this.maxScroll(layout, this.lastBox.height); + const stops = layout.tops.map((_, index) => this.restTop(layout, index)); if (delta > 0) { if (this.scrollY >= maxScroll) return false; - const next = layout.tops.find((top) => top > this.scrollY); + const next = stops.find((top) => top > this.scrollY); this.scrollY = Math.min(maxScroll, next ?? maxScroll); } else { if (this.scrollY <= 0) return false; let previous = 0; - for (const top of layout.tops) if (top < this.scrollY) previous = top; + for (const top of stops) if (top < this.scrollY) previous = top; this.scrollY = Math.max(0, previous); } return true; diff --git a/app/ui/menu.ts b/app/ui/menu.ts index 104b3925..067a7577 100644 --- a/app/ui/menu.ts +++ b/app/ui/menu.ts @@ -34,6 +34,11 @@ const MENU_FOOTER_GAP = 8; const MENU_ROW_INSET = 12; /** Horizontal inset of row text from the row box. */ const MENU_ROW_TEXT_INSET = 10; +/** A page menu's title: how far below the window's top padding, and the gap under its band. */ +const PAGE_TITLE_OFFSET_Y = 2; +const PAGE_TITLE_GAP = 4; +/** Room a page menu keeps right of its rows for the scrollbar. */ +const PAGE_SCROLLBAR_GUTTER = 6; export type MenuLayout = { /** Stereo depth of the menu surface, including the selected row. */ @@ -64,6 +69,23 @@ export type MenuLayout = { squareCorners?: boolean; }; +/** + * A menu laid out as a page of its window rather than a box floating over + * it (an app's root menu, a settings sub-screen): opaque, with no outline. + * Its rows start at the padding suggested for the window's left edge + * (viewport.ts) and its title lines up with their text. The title and a + * footer stay put, inside the top and bottom padding; the rows scroll + * between them, or right to the window's top or bottom edge where there is + * none, with that edge's padding inside the list. + */ +export type MenuPageLayout = { + page: true; + /** Width of the rows. Default (and at most): the window's, inside its side padding. */ + width?: number; + /** Dim hint line pinned above the window's bottom padding, below the rows. */ + footer?: string; +}; + export type MenuItemRenderArgs = { image: GrayImage; x: number; @@ -231,16 +253,16 @@ export class MenuLayer implements Layer { /** The context of the paint in progress, handed to item render callbacks. */ private paintCtx: LayerContext | null = null; - get depth(): number { return this.layout.depth ?? 0; } + get depth(): number { return "page" in this.layout ? 0 : this.layout.depth ?? 0; } get dimUnderneath(): false | number { - return this.layout.dimUnderneath ?? false; + return "page" in this.layout ? false : this.layout.dimUnderneath ?? false; } constructor( private readonly title: string | null, private items: MenuItem[], - private readonly layout: MenuLayout = { + private readonly layout: MenuLayout | MenuPageLayout = { x: DEFAULT_MENU_X, y: DEFAULT_MENU_Y, width: DEFAULT_MENU_WIDTH, @@ -301,22 +323,24 @@ export class MenuLayer implements Layer { } paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { + if ("page" in this.layout) return this.paintPage(ctx, this.layout); + const layout = this.layout; const font = getDefaultSmallFont(); const rowHeight = listRowHeight(font); const base = ctx.stack.getBaseSize(); - const image = this.layout.opaque ? new GrayImage(base.width, base.height, 0) : paintBelow(); - const { y, width } = this.layout; - const x = this.layout.x === "center" ? ((image.width - width) / 2) | 0 : this.layout.x; + const image = layout.opaque ? new GrayImage(base.width, base.height, 0) : paintBelow(); + const { y, width } = layout; + const x = layout.x === "center" ? ((image.width - width) / 2) | 0 : layout.x; const chromeTop = (this.title ? menuTitleHeight(font) : 0) + MENU_BODY_PADDING; - const minHeight = this.layout.minHeight ?? defaultMenuMinHeight(); + const minHeight = layout.minHeight ?? defaultMenuMinHeight(); // Cap to the surface being painted on: a window's stack image may be much // shorter than the full screen (min-height windows). const maxHeight = Math.min( - this.layout.maxHeight ?? image.height - y - DEFAULT_MENU_Y, + layout.maxHeight ?? image.height - y - DEFAULT_MENU_Y, image.height - y, - (this.layout.squareCorners || this.depth !== 0) ? Math.floor(65530 / Math.ceil(width / 2)) : Infinity, + (layout.squareCorners || this.depth !== 0) ? Math.floor(65530 / Math.ceil(width / 2)) : Infinity, ); - const footerHeight = this.layout.footer ? MENU_FOOTER_GAP + lineStep(font) : 0; + const footerHeight = layout.footer ? MENU_FOOTER_GAP + lineStep(font) : 0; const contentHeight = chromeTop + this.items.length * rowHeight + footerHeight + MENU_BODY_PADDING; const height = clamp(contentHeight, Math.min(minHeight, maxHeight), maxHeight); const visibleRowCount = Math.max( @@ -326,17 +350,17 @@ export class MenuLayer implements Layer { // Fill 1, not 0: identical after 4bpp quantization, but 0 is the // transparent color key when a menu paints on the shell surface. - if (this.layout.squareCorners) image.fillRect(x, y, width, height, 1); + if (layout.squareCorners) image.fillRect(x, y, width, height, 1); else image.fillRoundedRect(x, y, width, height, 1); - if (this.layout.showBorder !== false) { - if (this.layout.squareCorners) image.drawRect(x, y, width, height, 72); + if (layout.showBorder !== false) { + if (layout.squareCorners) image.drawRect(x, y, width, height, 72); else image.drawRoundedRect(x, y, width, height, 72); } if (this.title) { image.drawText(font, x + 12, y + 8, this.title, 220); } - if (this.layout.footer) { - image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, this.layout.footer, 110); + if (layout.footer) { + image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, layout.footer, 110); } const bodyY = y + chromeTop; @@ -352,6 +376,42 @@ export class MenuLayer implements Layer { return image; } + /** Paint as a page of the window (see MenuPageLayout). */ + private paintPage(ctx: LayerContext, layout: MenuPageLayout): GrayImage { + const font = getDefaultSmallFont(); + const { width, height, padding } = ctx.stack.getBaseSize(); + const image = new GrayImage(width, height, 0); + const x = padding.left; + const available = Math.max(0, width - padding.left - padding.right - PAGE_SCROLLBAR_GUTTER); + const rowsWidth = Math.min(layout.width ?? available, available); + // Fixed parts keep inside the window's padding; the list's box runs to + // an edge nothing fixed stands before, taking that edge's padding as its own. + let top = 0; + let fixedTop = padding.top; + if (this.title) { + const titleY = padding.top + PAGE_TITLE_OFFSET_Y; + image.drawText(font, x + MENU_ROW_TEXT_INSET, titleY, this.title, 220); + top = fixedTop = titleY + menuTitleHeight(font) + PAGE_TITLE_GAP; + } + let bottom = height; + let fixedBottom = height - padding.bottom; + if (layout.footer) { + const footerY = height - padding.bottom - font.lineHeight; + image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, layout.footer, 110); + bottom = fixedBottom = footerY - MENU_FOOTER_GAP; + } + if (bottom <= top) return image; + this.paintCtx = ctx; + try { + this.menu.paint(image, { x, y: top, width: rowsWidth, height: bottom - top, padTop: fixedTop - top, padBottom: bottom - fixedBottom }, + ctx.stack.isFocused()); + } finally { + this.paintCtx = null; + } + this.menu.drawScrollbar(image, x + rowsWidth + PAGE_SCROLLBAR_GUTTER - 3, fixedTop, Math.max(0, fixedBottom - fixedTop)); + return image; + } + async handleInput(event: InputEvent, ctx: LayerContext): Promise { switch (event.type) { case "double-click": @@ -413,20 +473,23 @@ export class TextPageLayer implements Layer { // Size to the stack, not the lens: in an app window the stack is the // window's viewport, and the compositor rejects a frame larger than its // surface (leaving the previous frame on screen). - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - image.drawText(font, 18, 14, this.title, 220); - image.drawRect(12, 12, width - 24, height - 24, 52); + // The page's own outline sits at the padding suggested for each window + // edge; everything else is placed from it. + const box = { x: padding.left, y: padding.top, width: width - padding.left - padding.right, height: height - padding.top - padding.bottom }; + image.drawText(font, box.x + 6, box.y + 2, this.title, 220); + image.drawRect(box.x, box.y, box.width, box.height, 52); - const wrapped = wrapText(font, this.body, width - 60); + const wrapped = wrapText(font, this.body, box.width - 36); const step = lineStep(font); - const bodyTop = 28 + font.lineHeight; - const footerY = height - 48; + const bodyTop = box.y + 16 + font.lineHeight; + const footerY = box.y + box.height - 36; const maxBodyLines = Math.max(0, Math.floor((footerY - bodyTop - 8) / step)); for (let index = 0; index < Math.min(wrapped.length, maxBodyLines); index++) { - image.drawText(font, 24, bodyTop + index * step, wrapped[index]!, 190); + image.drawText(font, box.x + 12, bodyTop + index * step, wrapped[index]!, 190); } - image.drawText(font, 24, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110); + image.drawText(font, box.x + 12, footerY, `${GESTURE_DOUBLE_CLICK} back`, 110); return image; } diff --git a/app/ui/notification-filter.ts b/app/ui/notification-filter.ts index 123f5d9e..12c694b4 100644 --- a/app/ui/notification-filter.ts +++ b/app/ui/notification-filter.ts @@ -10,11 +10,10 @@ import { drawToggleMenuItem, MenuLayer, type MenuItem } from "./menu"; /** Refresh rows as new sources arrive, keeping the current selection by package. */ export class NotificationFilterLayer implements Layer { private menu: MenuLayer | null = null; - private menuWidth = 0; /** The package behind each current row, parallel to the menu's items. */ private packages: string[] = []; - private currentMenu(ctx: LayerContext): MenuLayer { + private currentMenu(): MenuLayer { readActiveNotifications(ALL_NOTIFICATIONS); const sources = readNotificationSources().sort((a, b) => a.appName.localeCompare(b.appName) || a.packageName.localeCompare(b.packageName)); @@ -32,14 +31,8 @@ export class NotificationFilterLayer implements Layer { const index = sources.findIndex((source) => source.packageName === selectedPackage); this.packages = sources.map((source) => source.packageName); - const width = ctx.stack.getBaseSize().width; - if (!this.menu || width !== this.menuWidth) { - this.menuWidth = width; - this.menu = new MenuLayer("Notification filter", items, { - x: 8, y: 8, width: width - 16, - opaque: true, showBorder: false, - footer: "On = show popups on glasses", - }).selectItem(Math.max(0, index)); + if (!this.menu) { + this.menu = new MenuLayer("Notification filter", items, { page: true, footer: "On = show popups on glasses" }).selectItem(Math.max(0, index)); } else { this.menu.setItems(items, index >= 0 ? index : undefined); } @@ -47,10 +40,10 @@ export class NotificationFilterLayer implements Layer { } paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { - return this.currentMenu(ctx).paint(ctx, paintBelow); + return this.currentMenu().paint(ctx, paintBelow); } async handleInput(event: InputEvent, ctx: LayerContext): Promise { - await this.currentMenu(ctx).handleInput(event, ctx); + await this.currentMenu().handleInput(event, ctx); } } diff --git a/app/ui/notifications.ts b/app/ui/notifications.ts index cf1fe2fc..7860923f 100644 --- a/app/ui/notifications.ts +++ b/app/ui/notifications.ts @@ -24,12 +24,17 @@ import { noteStaleDataUsed, renderPassAllowsStaleData } from "../util/render-fre import { type InputEvent } from "./gestures"; import { type Layer, type LayerContext, type PaintBelow } from "./layers"; import { Menu, type MenuBox } from "./menu-core"; +import type { Viewport } from "./viewport"; -// Title position, shared with the other list apps (terminal, calendar). -const TITLE_X = 18; -const TITLE_Y = 10; -const LIST_TOP = 38; -const CARD_X = 20; +// The list's title, as in the other list apps (terminal, calendar): this far +// in from the cards' left edge (in line with their icons) and below the +// window's top padding; the first card starts LIST_TOP_GAP under its top. +const TITLE_INSET_X = 10; +const TITLE_OFFSET_Y = 2; +const LIST_TOP_GAP = 28; +/** The empty-list message: indented from the title, and this far under its top. */ +const EMPTY_MESSAGE_INDENT = 6; +const EMPTY_MESSAGE_GAP = 62; const ICON_SIZE = NOTIFICATION_ICON_SIZE; const ICON_TEXT_GAP = 8; const CARD_TEXT_X = 10 + ICON_SIZE + ICON_TEXT_GAP; @@ -37,23 +42,19 @@ const CARD_GAP = 6; const MAX_NOTIFICATIONS = 50; // Right-hand action menu of the detail view. const DETAIL_MENU_WIDTH = 148; -/** Top of the action menu's first row box, and the space kept below its last. */ -const DETAIL_MENU_TOP = 22; -const DETAIL_MENU_BOTTOM_MARGIN = 17; +/** Gap between the content column and the action menu's row boxes. */ +const DETAIL_MENU_GAP = 12; const DETAIL_MENU_ROW_GAP = 3; /** Row text inset from the row box: horizontal, then vertical. */ const DETAIL_MENU_TEXT_X = 8; const DETAIL_MENU_TEXT_Y = 4; -const DETAIL_CONTENT_X = 24; -const DETAIL_TITLE_Y = 21; /** Gaps below the title's line box and below the icon/app-name header row. */ const DETAIL_HEADER_GAP = 3; const DETAIL_BODY_GAP = 4; /** Untitled text lines that attached images (custom layout, picture) leave room for above them, when there are that many; they take the rest. */ const DETAIL_PICTURE_TEXT_LINES = 2; -/** Gap between the text and an attached picture, and the space kept below the picture. */ +/** Gap between the text and an attached picture. */ const DETAIL_PICTURE_GAP = 6; -const DETAIL_PICTURE_BOTTOM_MARGIN = 12; /** An attached picture with less height than this to fit in (a large UI font) is left out. */ const DETAIL_PICTURE_MIN_HEIGHT = 32; /** Likewise a custom layout; Dexcom's reading is about 40 high. */ @@ -62,8 +63,6 @@ const DETAIL_CUSTOM_VIEW_MIN_HEIGHT = 16; const LARGE_ICON_SIZE = 32; const LARGE_ICON_GAP = 8; // Disable-source confirmation: actions on top, the explanation below them. -const CONFIRM_X = 24; -const CONFIRM_Y = 8; const CONFIRM_TEXT_X = 10; const CONFIRM_PROMPT_GAP = 16; @@ -137,10 +136,14 @@ export class NotificationsListLayer implements Layer { paint(ctx: LayerContext): GrayImage { const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); - const cardWidth = width - 2 * CARD_X; + // Cards span the window between the padding suggested for its sides. + const cardX = padding.left; + const cardWidth = width - padding.left - padding.right; const cardTextWidth = cardWidth - CARD_TEXT_X - 14; + const titleX = cardX + TITLE_INSET_X; + const titleY = padding.top + TITLE_OFFSET_Y; const notifications = readActiveNotifications(MAX_NOTIFICATIONS); const selectedIndex = this.resolveSelectedIndex(notifications); const layouts = notifications.map((notification, index) => @@ -148,37 +151,41 @@ export class NotificationsListLayer implements Layer { ); if (!notifications.length) { - image.drawText(font, TITLE_X, TITLE_Y, "Notifications", 220); + image.drawText(font, titleX, titleY, "Notifications", 220); const message = notificationEmptyMessage(); - const messageLines = wrapText(font, message, width - 48); + const messageX = titleX + EMPTY_MESSAGE_INDENT; + const messageLines = wrapText(font, message, width - padding.right - messageX); for (let index = 0; index < messageLines.length; index++) { - image.drawText(font, 24, 72 + index * lineStep(font), messageLines[index]!, 190); + image.drawText(font, messageX, titleY + EMPTY_MESSAGE_GAP + index * lineStep(font), messageLines[index]!, 190); } return image; } const focused = ctx.stack.isFocused(); - const listBottom = height; - const scrollY = scrollForSelected(layouts, selectedIndex, listBottom - LIST_TOP); - // The title scrolls away with the list. Card text is drawn as deferred - // glyphs (always composited above raster fills), so a fixed title would - // show through cards scrolled over it; moving it with the content keeps - // it above the first card instead. - if (TITLE_Y - scrollY + font.lineHeight > 0) { - image.drawText(font, TITLE_X, TITLE_Y - scrollY, "Notifications", 220); + // The whole window scrolls, title and all, so cards scrolled past either + // end are cut off at the window's own edges; the padding suggested for + // the top and bottom edges lies inside the scrolled content, above the + // title and below the last card. + const listTop = titleY + LIST_TOP_GAP; + const scrollY = scrollForSelected(layouts, selectedIndex, height - listTop, padding.bottom); + // Card text is drawn as deferred glyphs (always composited above raster + // fills), so a fixed title would show through cards scrolled over it; + // moving it with the content keeps it above the first card instead. + if (titleY - scrollY + font.lineHeight > 0) { + image.drawText(font, titleX, titleY - scrollY, "Notifications", 220); } - let cursorY = LIST_TOP - scrollY; + let cursorY = listTop - scrollY; for (let index = 0; index < layouts.length; index++) { const layout = layouts[index]!; - if (cursorY + layout.height >= LIST_TOP && cursorY <= listBottom) { + if (cursorY + layout.height > 0 && cursorY < height) { // Icons are only resolved for cards actually drawn, so a long list // does not fetch icons for everything below the fold. const icon = iconForNotification(layout.notification.key); const largeIcon = largeIconForNotification(layout.notification); - drawNotificationCard(image, font, layout, CARD_X, cursorY, cardWidth, index === selectedIndex, focused, icon, largeIcon); + drawNotificationCard(image, font, layout, cardX, cursorY, cardWidth, index === selectedIndex, focused, icon, largeIcon); } cursorY += layout.height + CARD_GAP; - if (cursorY > listBottom + 80) break; + if (cursorY > height + 80) break; } return image; @@ -250,8 +257,8 @@ export class SingleNotificationLayer implements Layer { paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage { if (this.confirmation) return this.paintConfirmation(ctx); const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); - const image = new GrayImage(width, height, 0); + const viewport = ctx.stack.getBaseSize(); + const image = new GrayImage(viewport.width, viewport.height, 0); const notification = readActiveNotifications(ALL_NOTIFICATIONS).find((item) => item.key === this.notificationKey); if (!notification) { @@ -259,8 +266,9 @@ 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, notificationActionMenuBox(width, height), ctx.stack.isFocused()); + const layout = notificationDetailLayout(viewport); + drawDetailContent(image, font, notification, iconForNotification(notification.key), layout); + this.detailMenu.paint(image, layout.menuBox, ctx.stack.isFocused()); this.lastImage = image; return image; } @@ -330,24 +338,28 @@ export class SingleNotificationLayer implements Layer { private paintConfirmation(ctx: LayerContext): GrayImage { const { source, menu } = this.confirmation!; const font = getDefaultSmallFont(); - const { width, height } = ctx.stack.getBaseSize(); + const { width, height, padding } = ctx.stack.getBaseSize(); const image = new GrayImage(width, height, 0); + const contentWidth = width - padding.left - padding.right; // Put actions first so they remain reachable even with a large UI font. const menuBox: MenuBox = { - x: CONFIRM_X, - y: CONFIRM_Y, - width: width - 2 * CONFIRM_X, - height: Math.min(menu.items.length * listRowHeight(font), height - CONFIRM_Y), + x: padding.left, + y: padding.top, + width: contentWidth, + height: Math.min(menu.items.length * listRowHeight(font), height - padding.top), }; menu.paint(image, menuBox, ctx.stack.isFocused()); const top = menuBox.y + menuBox.height + CONFIRM_PROMPT_GAP; const prompt = `Turn off notification popups from ${source.appName || source.packageName}? You can turn them back on in the Notifications app's Notification filter.`; - const lines = wrapText(font, prompt, width - 2 * CONFIRM_X); - const maxLines = Math.max(1, Math.floor((height - top - 12) / lineStep(font))); + // In line with the actions' labels. + const promptX = menuBox.x + CONFIRM_TEXT_X; + const promptWidth = contentWidth - 2 * CONFIRM_TEXT_X; + const lines = wrapText(font, prompt, promptWidth); + const maxLines = Math.max(1, Math.floor((height - padding.bottom - top) / lineStep(font))); for (let index = 0; index < Math.min(lines.length, maxLines); index++) { const text = index === maxLines - 1 && lines.length > maxLines - ? truncateText(font, lines[index] + "...", width - 2 * CONFIRM_X) : lines[index]!; - image.drawText(font, CONFIRM_X, top + index * lineStep(font), text, 210); + ? truncateText(font, lines[index] + "...", promptWidth) : lines[index]!; + image.drawText(font, promptX, top + index * lineStep(font), text, 210); } return image; } @@ -443,14 +455,20 @@ function drawNotificationCard( } } -function scrollForSelected(layouts: CardLayout[], selectedIndex: number, viewportHeight: number): number { +/** + * How far the list is scrolled to centre the selected card in the rows from + * the first card's resting top to the window's bottom (`viewportHeight` of + * them), without scrolling past the last card and the `bottomPadding` kept + * under it. + */ +function scrollForSelected(layouts: CardLayout[], selectedIndex: number, viewportHeight: number, bottomPadding: number): number { if (selectedIndex < 0) return 0; let selectedTop = 0; for (let index = 0; index < selectedIndex; index++) { selectedTop += layouts[index]!.height + CARD_GAP; } const selectedBottom = selectedTop + layouts[selectedIndex]!.height; - const contentHeight = layouts.reduce((sum, layout) => sum + layout.height + CARD_GAP, 0); + const contentHeight = layouts.reduce((sum, layout) => sum + layout.height + CARD_GAP, -CARD_GAP) + bottomPadding; const maxScroll = Math.max(0, contentHeight - viewportHeight); const centered = selectedTop - Math.max(0, (viewportHeight - (selectedBottom - selectedTop)) / 2); return clamp(centered | 0, 0, maxScroll); @@ -461,14 +479,12 @@ function drawDetailContent( font: UiFont, notification: AndroidNotification, icon: GrayImage | null, - width: number, - height: number, + layout: NotificationDetailLayout, ): void { - const contentX = NOTIFICATION_CONTENT_X; - const contentWidth = notificationContentWidth(width); + const { contentX, contentWidth, bottom } = layout; const step = lineStep(font); - const bottom = height - DETAIL_PICTURE_BOTTOM_MARGIN; - let top = drawNotificationHeader(image, font, icon, `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`); + let top = drawNotificationHeader(image, font, icon, + `${notification.appName || notification.packageName} ${formatRelativeTime(notification.postTime)}`, layout); // The title, beside the large icon (a sender's avatar, a site's logo) when // there is one. A picture or custom layout can be the whole notification @@ -561,24 +577,44 @@ function drawClippedLines( return shown; } -/** Left edge of a notification detail view's content column. */ -export const NOTIFICATION_CONTENT_X = DETAIL_CONTENT_X; +/** + * Where a notification detail view's parts go in its viewport: a content + * column from the padding suggested for the left edge, and an action column + * of DETAIL_MENU_WIDTH ending at the right edge's. The content keeps inside + * the top and bottom padding (`top`, `bottom`). The action menu scrolls, so + * its box runs to the top and bottom edges with their padding inside it. + */ +export type NotificationDetailLayout = { + contentX: number; + contentWidth: number; + top: number; + bottom: number; + menuBox: MenuBox; +}; -/** 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; +export function notificationDetailLayout(viewport: Viewport): NotificationDetailLayout { + const { width, height, padding } = viewport; + const menuX = width - padding.right - DETAIL_MENU_WIDTH; + return { + contentX: padding.left, + contentWidth: menuX - DETAIL_MENU_GAP - padding.left, + top: padding.top, + bottom: height - padding.bottom, + menuBox: { x: menuX, y: 0, width: DETAIL_MENU_WIDTH, height, padTop: padding.top, padBottom: padding.bottom }, + }; } /** * 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); +export function drawNotificationHeader(image: GrayImage, font: UiFont, icon: GrayImage | null, sourceLabel: string, + layout: NotificationDetailLayout): number { + const { contentX, top } = layout; + image.drawText(font, contentX, top, "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 headerTop = top + font.lineHeight + DETAIL_HEADER_GAP; const headerHeight = Math.max(ICON_SIZE, font.lineHeight); let sourceX = contentX; if (icon) { @@ -616,16 +652,6 @@ export function createNotificationActionMenu(wraps: }); } -/** The action column's row boxes: right of the content, from DETAIL_MENU_TOP to the bottom margin. */ -export function notificationActionMenuBox(width: number, height: number): MenuBox { - return { - x: width - DETAIL_MENU_WIDTH - 32, - y: DETAIL_MENU_TOP, - width: DETAIL_MENU_WIDTH, - height: height - DETAIL_MENU_TOP - DETAIL_MENU_BOTTOM_MARGIN, - }; -} - function buildDetailMenu(notification: AndroidNotification, origin: SingleNotificationLayerOrigin): DetailMenuItem[] { return [ { kind: "back", label: "Back" }, diff --git a/app/ui/shell/geometry.ts b/app/ui/shell/geometry.ts index ae3c4447..d6ad356e 100644 --- a/app/ui/shell/geometry.ts +++ b/app/ui/shell/geometry.ts @@ -12,6 +12,7 @@ import { type WindowBorder, windowBorderSetting, } from "../dashboard-settings"; +import { type EdgeKind, type Edges, uniformEdges, type Viewport, viewportWith } from "../viewport"; /** The paired glasses' screen, which the shell lays out in (graphics/screen-size.ts). */ export { screenHeight, screenWidth }; @@ -481,6 +482,37 @@ export function appViewportSize(mode: WindowHeightMode, appId?: string): { width }; } +/** + * What each edge of an app's windows touches (see viewport.ts EdgeKind): a + * line the chrome draws right outside it, or nothing. A framed window has a + * line on every side. Otherwise the top or bottom has one where a status bar + * with rows of its own, or a bottom switcher row, lies past it and the + * Border setting divides the two; a side has one only beside a side strip, + * whose separator it is. An immersive window has the whole panel, and so + * none. + */ +export function windowEdges(appId?: string): Edges { + if (isImmersive(appId)) return uniformEdges("offscreen"); + if (windowFramed(appId)) return uniformEdges("border"); + const position = switcherPosition(); + const line = (drawn: boolean): EdgeKind => (drawn ? "border" : "offscreen"); + const statusLine = (edge: "top" | "bottom") => windowEdgeHeight(edge, appId) === TOP_BAR_HEIGHT && statusBarDivider(); + return { + top: line(statusLine("top")), + right: line(position === "right"), + bottom: line(position === "bottom" ? switcherRowHeight(appId) > 0 && switcherRowDivider(appId) : statusLine("bottom")), + left: line(position === "left"), + }; +} + +/** + * A window's viewport as its layouts get it: the app-content size for its + * height mode, what its edges touch, and the padding suggested for them. + */ +export function appViewport(mode: WindowHeightMode, appId?: string): Viewport { + return viewportWith(appViewportSize(mode, appId), windowEdges(appId)); +} + /** Screen rect of a window's app-content viewport (between its header and footer). */ export function appViewportRect(mode: WindowHeightMode, appId?: string): { x: number; diff --git a/app/ui/shell/in-process-window.ts b/app/ui/shell/in-process-window.ts index dc5a25e3..d0b7990c 100644 --- a/app/ui/shell/in-process-window.ts +++ b/app/ui/shell/in-process-window.ts @@ -8,7 +8,7 @@ import { type MenuItem } from "../menu"; import { WindowMenuLayer } from "../window-menu"; import { windowIcon } from "./chrome-layer"; import { type IconName } from "../../graphics/icons"; -import { appViewportSize, setAppImmersive, type WindowHeightMode } from "./geometry"; +import { appViewport, setAppImmersive, type WindowHeightMode } from "./geometry"; import { shell, type ShellWindow } from "./shell"; /** @@ -111,7 +111,7 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces const requestRender = () => renderInNewFrame(0); let heightMode = options.heightMode ?? "min"; setAppImmersive(options.appId, options.immersive ?? false); - const viewportSize = () => appViewportSize(heightMode, options.appId); + const viewportSize = () => appViewport(heightMode, options.appId); const stack = new LayerStack( options.baseLayer, { ...options.actions, requestRender }, diff --git a/app/ui/shell/modal-layer.ts b/app/ui/shell/modal-layer.ts index 05bd9027..edacb857 100644 --- a/app/ui/shell/modal-layer.ts +++ b/app/ui/shell/modal-layer.ts @@ -3,6 +3,7 @@ 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"; +import { uniformEdges, type Viewport, viewportWith } from "../viewport"; // The modal covers most of the min-height app viewport, leaving a little of // the foreground app visible around the edges. Both its size and position are @@ -11,13 +12,16 @@ import { appViewportRect, appViewportSize, SHELL_OPAQUE_BLACK } from "./geometry const MODAL_MARGIN = 14; const MODAL_PADDING = 4; -/** Content (inner layer stack) size of the modal box. */ -function modalInterior(): { width: number; height: number } { +/** + * The modal box's content (inner layer stack) viewport: every edge is just + * inside the box's border. + */ +function modalInterior(): Viewport { const viewport = appViewportSize("min"); - return { + return viewportWith({ 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, - }; + }, uniformEdges("border")); } /** Screen rect of the modal box, aligned to the min-height window band. */ diff --git a/app/ui/shell/shell.ts b/app/ui/shell/shell.ts index fd6ba8fc..683bc97c 100644 --- a/app/ui/shell/shell.ts +++ b/app/ui/shell/shell.ts @@ -89,6 +89,7 @@ import { screenWidth, } from "./geometry"; import type { AppSwitcherPosition } from "../dashboard-settings"; +import type { Viewport } from "../viewport"; /** * The shell: owns the window registry, focus, screen on/off, and the shell @@ -169,11 +170,11 @@ export type ShellWindow = { */ relayout?: () => void; /** - * The viewport size the window is laid out at (it changes on relayout), so - * the controller can tell a window whose size fell behind the geometry from - * one whose surface merely moved. + * The viewport the window is laid out at (it changes on relayout), so the + * controller can tell a window whose size, or whose edges, fell behind the + * geometry from one whose surface merely moved. */ - layoutSize?: () => { width: number; height: number }; + layoutSize?: () => Viewport; /** * A touch from the phone's mirror at (x, y) in app-viewport coordinates. * True if the window acted on it; otherwise the controller sends a select. diff --git a/app/ui/shell/worker-window.ts b/app/ui/shell/worker-window.ts index 59728842..2e38e1dc 100644 --- a/app/ui/shell/worker-window.ts +++ b/app/ui/shell/worker-window.ts @@ -3,7 +3,8 @@ import { GrayImage } from "../../graphics/image"; import { windowIcon } from "./chrome-layer"; import { type IconActivity, type IconName } from "../../graphics/icons"; import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry"; -import { appViewportSize, type WindowHeightMode } from "./geometry"; +import { appViewport, type WindowHeightMode } from "./geometry"; +import { type Viewport } from "../viewport"; import * as frameTimings from "../../native/frame-timings"; import { shell, type ShellWindow } from "./shell"; import { publishWorkerState } from "./worker-state"; @@ -20,8 +21,10 @@ export type WorkerAppMessage = /** Release app-wide resources, then acknowledge with worker-stopped. */ | { type: "shutdown" } | { type: "navigation-sensors"; event: NavigationSensorEvent } - | { type: "open-window"; windowId: string; surfaceId: string; title: string; viewport: { width: number; height: number } } - | { type: "resize-window"; windowId: string; viewport: { width: number; height: number } } + /** `viewport` carries what the window's edges touch and the padding suggested for them (viewport.ts). */ + | { type: "open-window"; windowId: string; surfaceId: string; title: string; viewport: Viewport } + /** The window's size changed, or what its edges touch did (so its padding). */ + | { type: "resize-window"; windowId: string; viewport: Viewport } | { type: "close-window"; windowId: string } | { type: "input"; windowId: string; event: unknown; frameId: number; focused: boolean } | { type: "text-input"; windowId: string; text: string; submit?: boolean } @@ -462,7 +465,7 @@ export class WorkerAppHost { const surfaceId = `window:${spec.windowId}`; const heightMode = spec.heightMode ?? "min"; this.openWindows.add(spec.windowId); - let viewport = appViewportSize(heightMode, this.options.appId); + let viewport = appViewport(heightMode, this.options.appId); this.post({ type: "open-window", windowId: spec.windowId, @@ -482,7 +485,7 @@ export class WorkerAppHost { // (a game in progress, a route): the bottom switcher's height changes // with what it holds, and full-height windows give way to it. relayout: () => { - viewport = appViewportSize(window.heightMode, this.options.appId); + viewport = appViewport(window.heightMode, this.options.appId); this.post({ type: "resize-window", windowId: spec.windowId, viewport }); }, layoutSize: () => viewport, diff --git a/app/ui/viewport.ts b/app/ui/viewport.ts new file mode 100644 index 00000000..afd4d860 --- /dev/null +++ b/app/ui/viewport.ts @@ -0,0 +1,82 @@ +/** + * A window's viewport as its layouts see it: its size, what lies past each + * of its edges, and how much padding to leave at each because of that. Pure + * data, so it crosses to app workers in their open/resize messages. + */ + +/** + * What a viewport edge touches: a line the shell draws right outside it + * ("border": a side of the window frame, the status bar's divider, the app + * switcher's separator), or nothing ("offscreen": the end of the screen, or + * space the shell leaves blank, which looks the same on the glasses; chrome + * with no line before it, a status bar or switcher row, counts too). + */ +export type EdgeKind = "border" | "offscreen"; + +export type Edges = { top: T; right: T; bottom: T; left: T }; + +export type ViewportSize = { width: number; height: number }; + +export type Viewport = ViewportSize & { + edges: Edges; + /** + * Suggested distance to keep between each edge and the content nearest it + * (see suggestedPadding). A layout puts its outermost element this far in, + * rather than hard-coding a margin, so it has room beside a border line + * and wastes none beside blank space. A vertical scroll container is the + * exception: its viewport runs to the top and bottom edges whatever they + * touch, and the padding goes inside the scrolled content, before the + * first element and after the last. + */ + padding: Edges; +}; + +/** Suggested padding between a border line and the content beside it. */ +export const BORDER_EDGE_PADDING = 8; + +/** Suggested content padding for each edge: room beside a border, none beside blank space. */ +export function suggestedPadding(edges: Edges): Edges { + const pad = (edge: EdgeKind) => (edge === "border" ? BORDER_EDGE_PADDING : 0); + return { top: pad(edges.top), right: pad(edges.right), bottom: pad(edges.bottom), left: pad(edges.left) }; +} + +export function uniformEdges(kind: EdgeKind): Edges { + return { top: kind, right: kind, bottom: kind, left: kind }; +} + +/** A viewport of the given size and edges, with the padding suggested for them. */ +export function viewportWith(size: ViewportSize, edges: Edges): Viewport { + return { width: size.width, height: size.height, edges: { ...edges }, padding: suggestedPadding(edges) }; +} + +/** + * Complete a bare size as a viewport nothing borders (no padding); one that + * already says what its edges touch is copied as it is. + */ +export function toViewport(size: ViewportSize | Viewport): Viewport { + if ("edges" in size && "padding" in size) { + return { width: size.width, height: size.height, edges: { ...size.edges }, padding: { ...size.padding } }; + } + return viewportWith(size, uniformEdges("offscreen")); +} + +export function sameEdges(a: Edges, b: Edges): boolean { + return a.top === b.top && a.right === b.right && a.bottom === b.bottom && a.left === b.left; +} + +export function sameViewport(a: Viewport, b: Viewport): boolean { + return a.width === b.width && a.height === b.height && sameEdges(a.edges, b.edges) + && a.padding.top === b.padding.top && a.padding.right === b.padding.right + && a.padding.bottom === b.padding.bottom && a.padding.left === b.padding.left; +} + +/** The rect inside a viewport's suggested padding, in its own coordinates. */ +export function paddedRect(viewport: Viewport): { x: number; y: number; width: number; height: number } { + const { padding } = viewport; + return { + x: padding.left, + y: padding.top, + width: Math.max(0, viewport.width - padding.left - padding.right), + height: Math.max(0, viewport.height - padding.top - padding.bottom), + }; +} diff --git a/app/ui/window-menu.ts b/app/ui/window-menu.ts index 07aab3a6..52288672 100644 --- a/app/ui/window-menu.ts +++ b/app/ui/window-menu.ts @@ -4,6 +4,7 @@ import { GESTURE_LONG_PRESS, GESTURE_SHORT_THEN_LONG_PRESS, gestureHints, type I import { type Layer, type LayerActions, LayerStack, noopLayerActions } from "./layers"; import { CONTEXT_MENU_DIM, MenuLayer, type MenuItem, type MenuLayout } from "./menu"; import type { WorkerAppReply } from "./shell/worker-window"; +import type { Viewport, ViewportSize } from "./viewport"; /** * The window context menu, opened by tap-then-hold: an app's own menu, @@ -65,7 +66,8 @@ export type WindowMenuOptions = { claimsLongPress?: () => boolean; /** Paint the window content the menu draws over (a fresh, mutable image). */ paintBase: () => GrayImage; - size: { width: number; height: number }; + /** The window's viewport; a bare size will do, as the menu itself floats clear of the edges. */ + size: ViewportSize | Viewport; isFocused: () => boolean; }; @@ -92,7 +94,7 @@ export class WindowMenu { constructor(private readonly options: WindowMenuOptions) {} - resize(size: { width: number; height: number }): void { + resize(size: ViewportSize | Viewport): void { this.options.size = size; this.stack?.setBaseSize(size); } diff --git a/tests/ai-chat.test.cjs b/tests/ai-chat.test.cjs index 769a74ed..d18f3931 100644 --- a/tests/ai-chat.test.cjs +++ b/tests/ai-chat.test.cjs @@ -4,6 +4,7 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); function load(file, deps, globals = {}) { const exports = {}; @@ -256,6 +257,7 @@ function shellEnv({ wakeAction = 'voice-input', skipConfirmation = false } = {}) const timings = { spanCurrent: (_, fn) => fn(), span: (_, __, fn) => fn(), spanAsync: (_, __, fn) => fn(), startFrame: () => 0, annotateFrame() {}, finishFrame() {}, runWithFrame: (_, fn) => fn() }; const layers = load('app/ui/layers.ts', { + './viewport': require('../.test-build/app/ui/viewport.js'), '../graphics/image': images, '../graphics/plane': { dimPlanes: (planes) => planes }, '../native/frame-timings': timings, './gestures': gestures, '../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 }, @@ -295,7 +297,7 @@ function shellEnv({ wakeAction = 'voice-input', skipConfirmation = false } = {}) '../../graphics/image': images, '../../native/frame-timings': timings, '../../util/render-freshness': { beginRenderPass() {}, endRenderPass: () => false }, '../layers': layers, '../window-menu': { WindowMenuLayer: Menu }, - './chrome-layer': { windowIcon: () => () => null }, './geometry': { appViewportSize: () => ({ width: 576, height: 260 }), setAppImmersive: () => false }, + './chrome-layer': { windowIcon: () => () => null }, './geometry': { appViewport: () => ({ width: 576, height: 260 }), setAppImmersive: () => false }, './shell': { shell }, }); const input = [], menus = []; @@ -399,7 +401,9 @@ function chatLayerEnv() { }); createAiChatWindow({ actions: {}, setSurfaceVisible() {}, onClosed() {} }); const layer = windowOptions.baseLayer; - return { ...env, layer, paint: (width = 576, height = 260) => layer.paint({ stack: { getBaseSize: () => ({ width, height }) } }) }; + // A framed window (8px of padding all round) unless a viewport says otherwise. + return { ...env, layer, paint: (width = 576, height = 260, viewport = bordered(width, height)) => + layer.paint({ stack: { getBaseSize: () => viewport } }) }; } test('scrollback stays anchored during streaming, follows at the bottom and resets on session switch', () => { @@ -441,3 +445,35 @@ test('chat paints at both window heights with pending speech pinned beneath hist } } }); + +test('chat keeps inside the padding of the edges a border runs along, and uses the room where there is none', () => { + const env = chatLayerEnv(); + const session = env.store.ensureSession(); + for (let i = 0; i < 30; i++) session.transcript.push({ role: i % 2 ? 'assistant' : 'user', text: `Message ${i}: sample conversation text.` }); + // The rows and columns holding any ink, text included. + const inkBounds = (image) => { + const baked = image.withDrawsBaked(); + const bounds = { left: Infinity, top: Infinity, right: -1, bottom: -1 }; + for (let y = 0; y < baked.height; y++) for (let x = 0; x < baked.width; x++) { + if (!baked.getPixel(x, y)) continue; + bounds.left = Math.min(bounds.left, x); bounds.right = Math.max(bounds.right, x); + bounds.top = Math.min(bounds.top, y); bounds.bottom = Math.max(bounds.bottom, y); + } + return bounds; + }; + for (const viewport of [ + bordered(576, 260), unbordered(576, 260), edged(576, 260, 'top', 'left'), edged(576, 452, 'bottom', 'right'), + bordered(476, 155), unbordered(476, 155), edged(640, 480, 'bottom'), + ]) { + const { width, height, padding } = viewport; + const where = JSON.stringify(viewport); + const ink = inkBounds(env.paint(width, height, viewport)); + assert.ok(env.layer.maxFirstLine > 0, where); + // The footer's rule starts right at the left padding and the scrollbar hugs the right. + assert.equal(ink.left, padding.left, where); + assert.equal(ink.right, width - padding.right - 1, where); + // The title's line starts at the top padding and the footer's last line ends at the bottom. + assert.ok(ink.top >= padding.top && ink.top < padding.top + 12, where); + assert.ok(ink.bottom < height - padding.bottom && ink.bottom >= height - padding.bottom - 12, where); + } +}); diff --git a/tests/app-layout.test.cjs b/tests/app-layout.test.cjs index c7135f5a..c5fc1216 100644 --- a/tests/app-layout.test.cjs +++ b/tests/app-layout.test.cjs @@ -4,6 +4,7 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { bordered, unbordered, edged, sameViewport } = require('./helpers/viewport.cjs'); const read = (file) => fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); const js = (source) => ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, @@ -18,6 +19,7 @@ function geometry(screen = { width: 640, height: 480 }) { }).map(([name, value]) => [name, { value, get() { return this.value; } }])); const context = { exports: {}, require: (name) => { if (name === '../../graphics/screen-size') return { screenWidth: () => screen.width, screenHeight: () => screen.height }; + if (name === '../viewport') return require('../.test-build/app/ui/viewport.js'); assert.equal(name, '../dashboard-settings'); return settings; } }; @@ -346,6 +348,96 @@ test('on a RayNeo iO the chrome paints the screen\'s size, its strip and row wit } }); +test('each window edge is labelled by whether a border line runs along it, with padding only there', () => { + const g = geometry(); + // top, right, bottom, left: B = a border line right outside it, o = nothing. + const edges = (appId) => { + const e = g.windowEdges(appId); + return [e.top, e.right, e.bottom, e.left].map((kind) => (kind === 'border' ? 'B' : 'o')).join(''); + }; + const set = (switcher, statusBar = 'top', border = 'all', visibility = 'always') => { + g.settings.appSwitcherPositionSetting.value = switcher; + g.settings.statusBarPositionSetting.value = statusBar; + g.settings.windowBorderSetting.value = border; + g.settings.statusBarVisibilitySetting.value = visibility; + }; + // A side strip: the top bar's divider and the strip's separator; the + // other side and the bottom are the screen's edge or blank. The Border + // setting doesn't reach it. + set('left'); + assert.equal(edges(), 'BooB'); + set('right', 'top', 'none'); + assert.equal(edges(), 'BBoo'); + // A bottom row: the frame all round; with only the status line, the edge + // the status bar is past; with no border, nothing. + set('bottom'); + assert.equal(edges(), 'BBBB'); + set('bottom', 'bottom'); + assert.equal(edges(), 'BBBB'); + set('bottom', 'top', 'status'); + assert.equal(edges(), 'Booo'); + set('bottom', 'bottom', 'status'); + assert.equal(edges(), 'ooBo'); + set('bottom', 'top', 'none'); + assert.equal(edges(), 'oooo'); + set('bottom', 'bottom', 'none'); + assert.equal(edges(), 'oooo'); + // A popup: the frame, or the line by a status bar with rows of its own + // (one that only overlays has none). + set('popup'); + assert.equal(edges(), 'BBBB'); + set('popup', 'bottom', 'status'); + assert.equal(edges(), 'ooBo'); + set('popup', 'top', 'status'); + assert.equal(edges(), 'Booo'); + set('popup', 'top', 'status', 'switcher'); + assert.equal(edges(), 'oooo'); + set('popup', 'top', 'all', 'switcher'); + assert.equal(edges(), 'BBBB'); + set('popup', 'top', 'none'); + assert.equal(edges(), 'oooo'); + // Full Screen has no room for the frame's sides: the status bar's divider + // and the switcher row's separator are the lines left. + g.settings.displayLayoutSetting.value = 'fullscreen'; + set('bottom'); + assert.equal(edges(), 'BoBo'); + set('bottom', 'bottom'); + assert.equal(edges(), 'ooBo'); + set('bottom', 'top', 'status'); + assert.equal(edges(), 'Booo'); + set('popup'); + assert.equal(edges(), 'Booo'); + set('popup', 'bottom'); + assert.equal(edges(), 'ooBo'); + // An immersive window has the whole panel. + g.settings.displayLayoutSetting.value = 'band-middle'; + set('bottom'); + g.setAppImmersive('onboarding', true); + assert.equal(edges('onboarding'), 'oooo'); + assert.equal(edges('launcher'), 'BBBB'); + set('left'); + assert.equal(edges('onboarding'), 'oooo'); + g.setAppImmersive('onboarding', false); + + // The viewport a window lays out in carries the labels and the padding + // suggested for them: 8px beside a line, none beside blank space. + const plain = (value) => JSON.parse(JSON.stringify(value)); + set('left'); + assert.deepEqual(plain(g.appViewport('min')), { + width: 576, height: 260, + edges: { top: 'border', right: 'offscreen', bottom: 'offscreen', left: 'border' }, + padding: { top: 8, right: 0, bottom: 0, left: 8 }, + }); + set('bottom', 'bottom', 'none'); + assert.deepEqual(plain(g.appViewport('min').padding), { top: 0, right: 0, bottom: 0, left: 0 }); + set('bottom', 'bottom'); + assert.deepEqual(plain(g.appViewport('min').padding), { top: 8, right: 8, bottom: 8, left: 8 }); + for (const mode of ['min', 'medium', 'max']) { + const { width, height } = g.appViewport(mode, 'terminal'); + assert.deepEqual({ width, height }, { ...g.appViewportSize(mode, 'terminal') }); + } +}); + test('the phone mirror zooms to the area the shell uses, centred at the panel\'s 4:3', () => { const g = geometry(); const preview = (mode, appId) => { @@ -1053,33 +1145,42 @@ test('with the selection out on a notification, the windows stay scrolled as the }); // Exercise the real worker message handler with platform work stubbed out. -function resizeHandler(file, globals) { +// `own` names the worker's own functions and constants the handler runs with. +function resizeHandler(file, globals, own = []) { const source = ts.createSourceFile(file, read(file), ts.ScriptTarget.Latest, true); const handler = source.statements.find((s) => ts.isExpressionStatement(s) && ts.isBinaryExpression(s.expression) && s.expression.left.getText(source) === 'global.onmessage'); assert.ok(handler); + const named = source.statements.filter((s) => ts.isFunctionDeclaration(s) && own.includes(s.name?.text) + || ts.isVariableStatement(s) && s.declarationList.declarations.some((d) => own.includes(d.name.getText(source)))); + assert.equal(named.length, own.length); const context = { global: {}, ...globals }; - vm.runInNewContext(js(handler.getText(source)), context); + vm.runInNewContext(js([...named, handler].map((s) => s.getText(source)).join('\n')), context); return (viewport) => context.global.onmessage({ data: { type: 'resize-window', windowId: 'test', viewport } }); } test('Navigate resize preserves its active route and window, and remeasures an open menu', () => { let paints = 0; let menuSize; - const window = { windowId: 'test', viewportWidth: 640, viewportHeight: 452, + const window = { windowId: 'test', viewportWidth: 640, viewportHeight: 452, viewport: bordered(640, 452), menu: { resize: (size) => { menuSize = size; } }, lastSubmittedFingerprint: 'old' }; const route = { destination: 'Home' }; const resize = resizeHandler('app/apps/navigate/navigate-app.worker.ts', { - window, route, phase: 'navigating', render: () => { paints++; }, + window, route, phase: 'navigating', render: () => { paints++; }, sameViewport, }); - resize({ width: 576, height: 260 }); + resize(bordered(576, 260)); assert.equal(window.viewportHeight, 260); assert.equal(window.viewportWidth, 576); + assert.deepEqual(window.viewport, bordered(576, 260)); assert.equal(window.lastSubmittedFingerprint, ''); assert.equal(route.destination, 'Home'); - assert.deepEqual(menuSize, { width: 576, height: 260 }); - resize({ width: 576, height: 260 }); + assert.deepEqual(menuSize, bordered(576, 260)); + resize(bordered(576, 260)); assert.equal(paints, 1); + // What the edges touch changing is a new layout too, at the same size. + resize(edged(576, 260, 'top')); + assert.equal(paints, 2); + assert.deepEqual(menuSize, edged(576, 260, 'top')); }); test('Terminal resize keeps the view identity, negotiates its grid and skips position-only changes', () => { @@ -1123,13 +1224,17 @@ test('the games and Roam resize in place, keeping their state, and repaint when } let paints = 0, viewSize; const window = { windowId: 'test', viewportWidth: 576, viewportHeight: 443, menu: null, lastSubmittedFingerprint: 'old', - view: { resize: (...size) => { viewSize = size; } } }; - const resize = resizeHandler('app/apps/roam/roam-app.worker.ts', { - window, render: () => paints++, DOC_MARGIN: 6, HEADER_HEIGHT: 30, BOTTOM_MARGIN: 6, - }); - resize({ width: 576, height: 415 }); - // The document re-wraps for the region under its header. - assert.deepEqual([window.viewportHeight, viewSize, window.lastSubmittedFingerprint, paints], [415, [564, 379], '', 1]); + view: { resize: (size) => { viewSize = { ...size }; } } }; + const resize = resizeHandler('app/apps/roam/roam-app.worker.ts', { window, render: () => paints++ }, + ['HEADER_HEIGHT', 'headerBottom', 'documentBox']); + resize(bordered(576, 415)); + // The document re-wraps for the region under its header: between the side + // padding and down to the window's bottom edge, whose padding goes inside it. + assert.deepEqual([window.viewportHeight, viewSize, window.lastSubmittedFingerprint, paints], + [415, { x: 8, y: 32, width: 560, height: 383, padBottom: 8 }, '', 1]); + assert.deepEqual(window.viewport, bordered(576, 415)); + resize(unbordered(576, 415)); + assert.deepEqual(viewSize, { x: 0, y: 24, width: 576, height: 391, padBottom: 0 }); }); test('resizing a terminal reconnects with the new grid and ignores callbacks from the old socket', () => { diff --git a/tests/brightness-picker.test.cjs b/tests/brightness-picker.test.cjs index 1052869b..45d72b2d 100644 --- a/tests/brightness-picker.test.cjs +++ b/tests/brightness-picker.test.cjs @@ -23,6 +23,7 @@ const { BdfFont } = load('app/graphics/bdffont.ts'); const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u18n.bdf'), 'utf8')); const fonts = { getDefaultSmallFont: () => font }; const layers = load('app/ui/layers.ts', { + './viewport': require('../.test-build/app/ui/viewport.js'), '../graphics/image': graphics, '../graphics/plane': plane, './gestures': gestures, '../native/frame-timings': { spanCurrent: (_name, paint) => paint() }, '../graphics/screen-size': { screenWidth: () => 640, screenHeight: () => 480 }, diff --git a/tests/buzzer.test.cjs b/tests/buzzer.test.cjs index ea8e1e91..538caa0d 100644 --- a/tests/buzzer.test.cjs +++ b/tests/buzzer.test.cjs @@ -9,7 +9,7 @@ test('iOS game worker forwards an immutable byte payload through its host and ig const load = loader({ global: { isIOS: true, postMessage: data => worker.onmessage({ data }) } }, { '../../graphics/image': {}, './chrome-layer': { windowIcon() {} }, '../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } }, - './geometry': { appViewportSize: () => ({ width: 576, height: 260 }) }, + './geometry': { appViewport: () => ({ width: 576, height: 260 }) }, '../../native/frame-timings': {}, './worker-state': {}, './shell': { shell: { registerWindow() {} } }, }); diff --git a/tests/compass-readout.test.cjs b/tests/compass-readout.test.cjs index fd772a58..0d7b06bf 100644 --- a/tests/compass-readout.test.cjs +++ b/tests/compass-readout.test.cjs @@ -4,6 +4,7 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); function load(file, modules = {}, globals = {}) { const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); @@ -23,7 +24,7 @@ const font = (size) => BdfFont.parse(fs.readFileSync(path.join(__dirname, `../ap const small = font(18), large = font(32); const diagnostics = { magneticAccuracy: 3, magneticAnomalies: 2, orientationSource: 3, flags: 0x8c, sampleTimeMs: 1234 }; -function compass(width = 576, height = 260, glasses = 'g2') { +function compass(width = 576, height = 260, glasses = 'g2', viewport = bordered(width, height)) { let listener, window; class RecordingImage extends graphics.GrayImage { texts = []; @@ -60,7 +61,7 @@ function compass(width = 576, height = 260, glasses = 'g2') { './calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} }, './heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) }, }, { setInterval: () => 1, clearInterval() {} }); - const ctx = { stack: { getBaseSize: () => ({ width, height }), pop() {} } }; + const ctx = { stack: { getBaseSize: () => viewport, pop() {} } }; app.createCompassAppWindow({ onClosed() {} }); return { menu: () => window.menuItems(), paint: () => window.baseLayer.paint(ctx), @@ -96,23 +97,40 @@ test('calibration dots track firmware magnetic accuracy and never go stale', () }); test('calibration readout fits the viewport without overlapping the heading or status', () => { - for (const [width, height] of [[240, 260], [320, 260], [576, 260], [640, 260], [576, 288], [576, 140]]) { - const app = compass(width, height); app.heading({ ...diagnostics, magneticAccuracy: 2 }); - const image = app.paint(); - const label = calibrationText(image); - assert.ok(label.x >= 0 && label.y >= 0, `${width}x${height}`); - assert.ok(label.x + label.width <= width && label.y + label.height <= image.height, `${width}x${height}`); - for (const other of image.texts.filter((t) => t !== label)) { - assert.ok(!overlaps(label, other), `${width}x${height}: overlaps "${other.text}"`); - } - if (process.env.COMPASS_PREVIEW && width === 576 && height === 260) { - fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([ - Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels), - ])); + for (const [width, height] of [[240, 260], [320, 260], [576, 260], [640, 260], [576, 288], [576, 140], [476, 155]]) { + for (const viewport of [bordered(width, height), unbordered(width, height), edged(width, height, 'top', 'left'), edged(width, height, 'bottom')]) { + const where = `${width}x${height} ${JSON.stringify(viewport.padding)}`; + const app = compass(width, height, 'g2', viewport); app.heading({ ...diagnostics, magneticAccuracy: 2 }); + const image = app.paint(); + const label = calibrationText(image); + const { padding } = viewport; + // Every line of text keeps inside the padding of the window's edges. + for (const text of image.texts) { + assert.ok(text.x >= padding.left && text.y >= padding.top, `${where}: "${text.text}"`); + assert.ok(text.x + text.width <= width - padding.right && text.y + text.height <= height - padding.bottom, `${where}: "${text.text}"`); + } + for (const other of image.texts.filter((t) => t !== label)) { + assert.ok(!overlaps(label, other), `${where}: overlaps "${other.text}"`); + } + if (process.env.COMPASS_PREVIEW && width === 576 && height === 260 && viewport.padding.top && viewport.padding.right) { + fs.writeFileSync(process.env.COMPASS_PREVIEW, Buffer.concat([ + Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels), + ])); + } } } }); +test('the calibration readout sits in the top-right corner, at the padding of those edges', () => { + const corner = (viewport) => { + const label = calibrationText(compass(576, 260, 'g2', viewport).paint()); + return [label.x + label.width, label.y]; + }; + assert.deepEqual(corner(bordered(576, 260)), [568, 8]); + assert.deepEqual(corner(unbordered(576, 260)), [576, 0]); + assert.deepEqual(corner(edged(576, 260, 'top')), [576, 8]); +}); + test('glasses without a magnetometer say so and offer no calibration', () => { const app = compass(576, 260, 'g1'); const image = app.paint(); diff --git a/tests/compass-texture-cache.test.cjs b/tests/compass-texture-cache.test.cjs index 702daebe..cfcddb33 100644 --- a/tests/compass-texture-cache.test.cjs +++ b/tests/compass-texture-cache.test.cjs @@ -4,6 +4,7 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { bordered, unbordered } = require('./helpers/viewport.cjs'); function load(file, modules = {}, extra = '', globals = {}) { const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8') + extra; @@ -124,7 +125,8 @@ test('heading updates reuse the texture; brightness, layout and clipping changes const layer = new compass.CompassLayer(() => {}); layer.enabled = true; let height = 260; - const ctx = { stack: { getBaseSize: () => ({ width: viewportWidth, height }) } }; + let viewport = bordered; + const ctx = { stack: { getBaseSize: () => viewport(viewportWidth, height) } }; const texture = () => layer.paint(ctx).draws.find((draw) => draw.kind === 'image').source; const waiting = texture(); layer.rawHeading = 0; @@ -143,7 +145,11 @@ test('heading updates reuse the texture; brightness, layout and clipping changes const wide = texture(); assert.notEqual(wide, tall); layer.firmwareStatus = 'Calibration status '.repeat(12); - assert.notEqual(texture(), wide); + const wrapped = texture(); + assert.notEqual(wrapped, wide); + // Losing the borders moves the rose toward the edges, at the same size. + viewport = unbordered; + assert.notEqual(texture(), wrapped); viewportWidth = 576; }); diff --git a/tests/date-picker.test.cjs b/tests/date-picker.test.cjs index d94c8057..378cd76f 100644 --- a/tests/date-picker.test.cjs +++ b/tests/date-picker.test.cjs @@ -22,6 +22,7 @@ const { BdfFont } = load('app/graphics/bdffont.ts'); const font = (name) => BdfFont.parse(fs.readFileSync(path.join(__dirname, `../app/fonts/terminus/${name}.bdf`), 'utf8')); const fonts = { getDefaultSmallFont: () => font('ter-u12n'), getDefaultMediumFont: () => font('ter-u16n') }; const layers = load('app/ui/layers.ts', { + './viewport': require('../.test-build/app/ui/viewport.js'), '../graphics/image': graphics, '../graphics/plane': plane, './gestures': gestures, '../native/frame-timings': { spanCurrent: (_name, paint) => paint() }, }); diff --git a/tests/evenhub-rayneo.test.cjs b/tests/evenhub-rayneo.test.cjs index f0995ae4..933630e8 100644 --- a/tests/evenhub-rayneo.test.cjs +++ b/tests/evenhub-rayneo.test.cjs @@ -5,6 +5,7 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); +const { bordered } = require('./helpers/viewport.cjs'); const gestures = loader({}, {})('app/ui/gestures.ts'); const font = { lineHeight: 10, measureText: (text) => text.length * 6 }; @@ -74,7 +75,7 @@ const loadStore = () => { const stackCalls = { push: 0, clearToBase: 0 }; const ctx = { stack: { - getBaseSize: () => ({ width: 476, height: 180 }), + getBaseSize: () => bordered(476, 180), isFocused: () => true, clearToBase() { stackCalls.clearToBase++; }, push() { stackCalls.push++; }, @@ -159,7 +160,7 @@ const loadWarning = () => { // The real stack calls onRemoved as it pops. const ctx = { stack: { - getBaseSize: () => ({ width: 476, height: 180 }), + getBaseSize: () => bordered(476, 180), isFocused: () => true, pop() { outcome.popped++; dialog.onRemoved(); }, }, diff --git a/tests/evenhub-signin.test.cjs b/tests/evenhub-signin.test.cjs index c7bf17cc..06b50933 100644 --- a/tests/evenhub-signin.test.cjs +++ b/tests/evenhub-signin.test.cjs @@ -9,6 +9,7 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); +const { bordered } = require('./helpers/viewport.cjs'); const setting = (initial = '') => { let value = initial; @@ -74,7 +75,7 @@ const makeCtx = () => { const stack = { cleared: 0, pushed: [], - getBaseSize: () => ({ width: 576, height: 136 }), + getBaseSize: () => bordered(576, 136), isFocused: () => true, clearToBase() { stack.cleared++; stack.pushed.length = 0; }, push(layer) { stack.pushed.push(layer); }, diff --git a/tests/evenhub-store-navigation.test.cjs b/tests/evenhub-store-navigation.test.cjs index f3d6c6e6..a718d62b 100644 --- a/tests/evenhub-store-navigation.test.cjs +++ b/tests/evenhub-store-navigation.test.cjs @@ -6,13 +6,14 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); const gestures = loader({}, {})('app/ui/gestures.ts'); const font = { lineHeight: 10, measureText: (text) => text.length * 6 }; -/** Records drawn text; every other drawing call is a no-op. */ +/** Records drawn text (and where, in `drawn.positions`); every other drawing call is a no-op. */ const makeImageClass = (drawn) => class { - drawText(_font, _x, _y, text) { drawn.push(text); } + drawText(_font, x, y, text) { drawn.push(text); (drawn.positions ??= new Map()).set(text, { x, y }); } fillRect() {} fillRoundedRect() {} drawRoundedRect() {} @@ -20,6 +21,8 @@ const makeImageClass = (drawn) => class { /** Stand-in for ui/menu-core's Menu: draws every row in order, scrolls the selection by one. */ class FakeMenu { + /** The box and scrollbar track of the latest paint, whichever tab's menu it was. */ + static painted = { box: null, scrollbar: null }; constructor(options) { this.options = options; this.items = options.items; @@ -37,9 +40,10 @@ class FakeMenu { if (event.type === 'scroll-up') this.selectedIndex = Math.max(0, this.selectedIndex - 1); } paint(image, box) { + FakeMenu.painted.box = box; this.items.forEach((item, index) => this.options.draw({ image, item, index, x: box.x, y: box.y, width: box.width })); } - drawScrollbar() {} + drawScrollbar(_image, x, y, height) { FakeMenu.painted.scrollbar = { x, y, height }; } } const storeApp = (packageId, name, version) => ({ @@ -93,9 +97,9 @@ const loadStore = ({ updates = [] } = {}) => { return { layer, drawn, installs }; }; -const makeCtx = () => ({ +const makeCtx = (viewport = bordered(576, 288)) => ({ stack: { - getBaseSize: () => ({ width: 576, height: 288 }), + getBaseSize: () => viewport, isFocused: () => true, clearToBase() {}, push() {}, @@ -191,6 +195,47 @@ test('the Updates tab has no Update All row when nothing can be installed', asyn assert.ok(!painted.includes('Update All')); }); +test('the list scrolls to the window\'s bottom edge under the fixed header, inside the side padding', async () => { + const updates = [{ installed: { packageId: 'c', name: 'Gamma', version: '1' } }]; + const layout = async (viewport) => { + const { layer, drawn } = loadStore({ updates }); + const ctx = makeCtx(viewport); + await openUpdatesTab(layer, ctx); + repaint(layer, drawn, ctx); + return { ...FakeMenu.painted, title: drawn.positions.get('EvenHub'), row: drawn.positions.get('Gamma') }; + }; + + // Framed: rows start at the left padding and stop short of the scrollbar, + // which hugs the right padding. The header keeps the top padding; the list + // box runs to the bottom edge with that edge's padding inside it. + const framed = await layout(bordered(576, 288)); + assert.equal(framed.box.x, 8); + assert.equal(framed.box.x + framed.box.width, 576 - 8 - 6); + assert.equal(framed.box.y + framed.box.height, 288, 'the box reaches the bottom edge'); + assert.equal(framed.box.padTop, 0, 'no padding under the fixed header'); + assert.equal(framed.box.padBottom, 8); + assert.deepEqual(framed.title, { x: 14, y: 11 }, 'a focused tab\'s highlight, 3px above the title line, sits at the padding'); + assert.equal(framed.row.x, framed.title.x, 'the title lines up with the row text'); + assert.deepEqual(framed.scrollbar, { x: 576 - 8 - 3, y: framed.box.y, height: 288 - 8 - framed.box.y }); + + // Nothing bordering the window: content runs to every edge. + const bare = await layout(unbordered(576, 288)); + assert.equal(bare.box.x, 0); + assert.equal(bare.box.padBottom, 0); + assert.equal(bare.box.y + bare.box.height, 288); + assert.deepEqual(bare.title, { x: 6, y: 3 }); + assert.equal(bare.box.y, framed.box.y - 8, 'the header moves up by the top padding'); + assert.equal(bare.scrollbar.x, 576 - 3, 'the scrollbar stays clear of the rows'); + assert.ok(bare.box.x + bare.box.width <= bare.scrollbar.x); + + // Each edge follows its own padding. + const mixed = await layout(edged(576, 288, 'top', 'left')); + assert.equal(mixed.box.x, 8); + assert.equal(mixed.box.x + mixed.box.width, 576 - 6); + assert.equal(mixed.box.y, framed.box.y); + assert.equal(mixed.box.padBottom, 0); +}); + // ---- permission dialog --------------------------------------------------- const loadDialog = (detailLines) => { @@ -216,7 +261,7 @@ const loadDialog = (detailLines) => { () => { outcome.confirmed = true; }, () => { outcome.confirmed = false; }, ); - const ctx = { stack: { getBaseSize: () => ({ width: 576, height: 136 }), isFocused: () => true, pop() {} } }; + const ctx = { stack: { getBaseSize: () => bordered(576, 136), isFocused: () => true, pop() {} } }; const paint = () => { drawn.length = 0; dialog.paint(ctx, () => new ImageClass()); diff --git a/tests/font-picker.test.cjs b/tests/font-picker.test.cjs index 5f890b99..e058c482 100644 --- a/tests/font-picker.test.cjs +++ b/tests/font-picker.test.cjs @@ -1,5 +1,6 @@ const test = require('node:test'); const assert = require('node:assert/strict'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); @@ -55,7 +56,9 @@ const glyphRuns = (image, f) => image.draws.filter((d) => d.kind === 'glyph' && test('the font picker previews under its rows where they fit, else beside them, on the window either way', () => { // At 192 rows either fits, by the font's metrics: only the bounds are checked. - for (const [size, beside] of [[{ width: 576, height: 260 }, false], [{ width: 476, height: 192 }, null], [{ width: 476, height: 155 }, true]]) { + // Framed windows, and ones nothing borders (so no padding: the rows reach the edges). + for (const [size, beside] of [[bordered(576, 260), false], [bordered(476, 192), null], [bordered(476, 155), true], + [unbordered(576, 260), false], [unbordered(476, 155), true], [edged(576, 260, 'top', 'left'), false]]) { const { layers, ctx } = stackOf(size); picker.uiFontPickerMenuItem().onSelect(ctx); const image = layers[0].paint(ctx); @@ -71,6 +74,8 @@ test('the font picker previews under its rows where they fit, else beside them, const rowsRight = Math.max(...rows.map((d) => d.x)), rowsBottom = Math.max(...rows.map((d) => d.y)); if (beside === true) assert.ok(Math.min(...sample.map((d) => d.x)) > rowsRight, 'the sample right of the rows'); if (beside === false) assert.ok(Math.min(...sample.map((d) => d.y)) > rowsBottom, 'the sample under the rows'); + // The rows' text keeps the same inset from wherever the padding puts their boxes. + assert.equal(Math.min(...rows.map((d) => d.x)), size.padding.left + 10); } }); diff --git a/tests/helpers/viewport.cjs b/tests/helpers/viewport.cjs new file mode 100644 index 00000000..9c31563b --- /dev/null +++ b/tests/helpers/viewport.cjs @@ -0,0 +1,17 @@ +// Viewports for layout tests: what ctx.stack.getBaseSize() (or a worker's +// open-window message) hands a layout. Needs `tsc -p tests/tsconfig.json`. +const viewport = require('../../.test-build/app/ui/viewport.js'); + +/** A window with a border line along every edge (a framed window): 8px of padding all round. */ +const bordered = (width, height) => viewport.viewportWith({ width, height }, viewport.uniformEdges('border')); + +/** A window nothing borders (Border: None, or immersive): no padding. */ +const unbordered = (width, height) => viewport.viewportWith({ width, height }, viewport.uniformEdges('offscreen')); + +/** A window with border lines along just the named edges, e.g. edged(576, 260, 'top', 'left'). */ +const edged = (width, height, ...borders) => viewport.viewportWith({ width, height }, { + top: 'offscreen', right: 'offscreen', bottom: 'offscreen', left: 'offscreen', + ...Object.fromEntries(borders.map((edge) => [edge, 'border'])), +}); + +module.exports = { ...viewport, bordered, unbordered, edged }; diff --git a/tests/icon-grid.test.cjs b/tests/icon-grid.test.cjs index fc1e784f..5047c832 100644 --- a/tests/icon-grid.test.cjs +++ b/tests/icon-grid.test.cjs @@ -124,18 +124,53 @@ function shown(image, at) { return withClock(at, () => Array.from(image.withDrawsBaked().pixels, (v) => Math.min(15, (v + 8) >> 4))); } -test('a box topped at the band inset less the highlight inset pads the first row band evenly', () => { - // The launcher's root grid (launcher-app.ts ROOT_GRID_TOP) relies on this. - const { grid } = makeGrid(); - const { ICON_GRID_BAND_INSET_X: insetX, ICON_GRID_HIGHLIGHT_INSET_Y: insetY } = loadIconGrid(); - const image = paint(grid, 1000, { x: 0, y: insetX - insetY, width: 500, height: 190 }); +/** Bounds of the selection highlight's stroke in a painted image: [top, left, right margin]. */ +function bandBounds(image) { const stroke = build('ui/menu-core').MENU_HIGHLIGHT_STROKE; let left = 500, right = -1, top = 200; image.pixels.forEach((v, i) => { if (v !== stroke) return; left = Math.min(left, i % 500); right = Math.max(right, i % 500); top = Math.min(top, (i / 500) | 0); }); - assert.deepEqual([top, left, 500 - 1 - right], [insetX, insetX, insetX]); + return [top, left, 500 - 1 - right]; +} + +test('a box inset at the sides, with as much padTop, pads the first row band evenly', () => { + // The launcher's grid (launcher-app.ts) relies on this: its box runs to the + // window's top edge, with the edge's suggested padding as padTop. + const pad = 8; + const image = paint(makeGrid().grid, 1000, { x: pad, y: 0, width: 500 - 2 * pad, height: 190, padTop: pad }); + assert.deepEqual(bandBounds(image), [pad, pad, pad]); + // Beside blank space there is no padding: the band spans the box, its own + // 2px inside its row. + assert.deepEqual(bandBounds(paint(makeGrid().grid, 1000, { x: 0, y: 0, width: 500, height: 190 })), [2, 0, 0]); +}); + +test('padTop scrolls away with the first row, and the scroll still animates to the static paint', async () => { + const box = { x: 0, y: 0, width: 500, height: 180, padTop: 8, padBottom: 8 }; + const { grid } = makeGrid({ items: Array.from({ length: 20 }, (_, i) => `item${i}`) }); + const iconYs = (image) => [...new Set(image.draws.filter((d) => d.kind === 'image' && !d.presentation).map((d) => d.y))]; + // The first row starts below the padding (less the band's own 2px inset). + assert.deepEqual(iconYs(paint(grid, 500, box)), [6 + 4, 6 + 4 + ROW_H]); + await grid.handleInput(ring('scroll-down'), null); + paint(grid, 1000, box); + // Once the band's slide onto the second row is over. + const settledBefore = paint(grid, 2000, box); + await grid.handleInput(ring('scroll-down'), null); + const moving = paint(grid, 3000, box); + assert.ok(animation(moving), 'the scroll is retained as a display list'); + assert.deepEqual(shown(moving, 3000), shown(settledBefore, 2000)); + const end = 3000 + MENU_HIGHLIGHT_DURATION_MS; + const settled = paint(grid, end, box); + // Scrolled, the rows run from the box's top edge. + assert.deepEqual(iconYs(settled), [4, 4 + ROW_H]); + assert.deepEqual(shown(moving, end), shown(settled, end)); + // A touch lands on the row under it either way. + const { grid: touched, log } = makeGrid(); + paint(touched, 1000, box); + assert.equal(await touched.hitTest(50, 3, null), false, 'the padding is no row'); + assert.equal(await touched.hitTest(50, 6 + ROW_H + 10, null), true); + assert.deepEqual(log.activated.map((a) => a.index), [5]); }); test('rows keep their content height instead of stretching, and the next row peeks clipped', () => { @@ -203,8 +238,9 @@ test('item selection slides the highlight between cells without redrawing past t assert.equal(list.calls[0].clip.width, 495, 'the scrollbar gutter is left to the static paint'); const rect = list.calls[1]; const { evaluate } = build('graphics/draw-expression'); - assert.equal(evaluate(rect.x, 0).value, 200 + 6, 'starts on the middle cell'); - assert.equal(evaluate(rect.x, MENU_HIGHLIGHT_DURATION_MS).value, 300 + 6, 'rests on the next cell'); + // The rows keep clear of the gutter: five 99px columns in the 495 left of it. + assert.equal(evaluate(rect.x, 0).value, 198 + 6, 'starts on the middle cell'); + assert.equal(evaluate(rect.x, MENU_HIGHLIGHT_DURATION_MS).value, 297 + 6, 'rests on the next cell'); assert.deepEqual(shown(moving, 2000), shown(before, 1000)); const end = 2000 + MENU_HIGHLIGHT_DURATION_MS; assert.deepEqual(shown(moving, end), shown(paint(grid, end), end)); diff --git a/tests/input-events.test.cjs b/tests/input-events.test.cjs index 5ced69bf..abcfcdcd 100644 --- a/tests/input-events.test.cjs +++ b/tests/input-events.test.cjs @@ -4,6 +4,7 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { bordered, unbordered } = require('./helpers/viewport.cjs'); const read = (file) => fs.readFileSync(path.join(__dirname, '..', file), 'utf8'); const js = (source) => ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, @@ -103,8 +104,16 @@ test('Input events captures while visible, pauses for history, and unsubscribes }); let visible = true, renders = 0, removed = 0, pops = 0; const layer = new InputEventsLayer(() => renders++, () => visible, () => removed++); - const ctx = { stack: { getBaseSize: () => ({ width: 576, height: 260 }), + let viewport = bordered(576, 260); + const ctx = { stack: { getBaseSize: () => viewport, pop: () => pops++, popThrough: (page) => { assert.equal(page, layer); page.onRemoved(); } } }; + const inBounds = (image) => { + for (const text of image.texts) { + assert.ok(text.x >= 0 && text.x + text.width <= image.width, text.text); + assert.ok(text.y >= 0 && text.y + text.height <= image.height, text.text); + } + }; + const at = (image, start) => image.texts.find(t => t.text.startsWith(start)); try { for (const type of ['ring-press','click','ring-press','ring-press','double-click','long-press','long-press-release','short-then-long-press']) { const e = { ...event(type, 1000 + renders * 75), ringInput: { tick: 4294967200 + renders, type: 1, aux:0, speed:0 } }; @@ -117,10 +126,19 @@ test('Input events captures while visible, pauses for history, and unsubscribes assert.ok(image.texts.some(t => t.text.startsWith("*"))); layer.menuItems()[2].onSelect(ctx); assert.ok(layer.paint(ctx).texts.some(t => t.text.startsWith("Phone receive time"))); - for (const text of image.texts) { - assert.ok(text.x >= 0 && text.x + text.width <= image.width, text.text); - assert.ok(text.y >= 0 && text.y + text.height <= image.height, text.text); - } + inBounds(image); + // Framed: the title and the hint line sit at the padding; the log is between them. + assert.deepEqual([at(image, 'Input events').x, at(image, 'Input events').y], [8, 8]); + assert.equal(at(image, 'Unfiltered').y + font.lineHeight, 260 - 8); + assert.ok(Math.max(...image.texts.map(t => t.x + t.width)) <= 576 - 8, 'nothing runs into the right padding'); + // Nothing bordering the window: the same lines run to its edges. + viewport = unbordered(576, 260); + const bare = layer.paint(ctx); + inBounds(bare); + assert.deepEqual([at(bare, 'Input events').x, at(bare, 'Input events').y], [0, 0]); + assert.equal(at(bare, 'Unfiltered').y + font.lineHeight, 260); + assert.ok(bare.texts.length >= image.texts.length, 'the log has no less room'); + viewport = bordered(576, 260); if (process.env.INPUT_EVENTS_PREVIEW) fs.writeFileSync(process.env.INPUT_EVENTS_PREVIEW, Buffer.concat([ Buffer.from(`P5\n${image.width} ${image.height}\n255\n`), Buffer.from(image.withDrawsBaked().pixels), ])); diff --git a/tests/ios-calendar.test.cjs b/tests/ios-calendar.test.cjs index b85b0554..88acbd03 100644 --- a/tests/ios-calendar.test.cjs +++ b/tests/ios-calendar.test.cjs @@ -4,6 +4,7 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { bordered, unbordered } = require('./helpers/viewport.cjs'); function load(file, modules = {}, globals = {}) { const sandbox = { exports: {}, require: id => { @@ -150,9 +151,9 @@ function screenFixture() { const f = provider(), timers = new Map(); let nextTimer = 0; const font = { lineHeight: 12, measureText: text => text.length * 6 }; class Image { - constructor(width = 300, height = 288) { this.width = width; this.height = height; this.text = []; } - drawText(_font, _x, _y, text) { this.text.push(text); } - fillRoundedRect() {} drawRoundedRect() {} + constructor(width = 300, height = 288) { this.width = width; this.height = height; this.text = []; this.at = {}; this.boxes = []; } + drawText(_font, x, y, text) { this.text.push(text); this.at[text] = [x, y]; } + fillRoundedRect(x, y, width, height) { this.boxes.push([x, y, width, height]); } drawRoundedRect() {} } const common = { 'graphics/image': { GrayImage: Image }, 'graphics/ui-fonts': { getDefaultSmallFont: () => font, getDefaultMediumFont: () => font }, @@ -173,7 +174,8 @@ function screenFixture() { test('Calendar and Glanceboard show permission, loading, error and event states with widget cleanup', () => { const f = screenFixture(); let renders = 0, requests = 0; const widget = new f.CalendarWidget(), layer = new f.CalendarLayer(() => requests++); - const ctx = { stack: { getBaseSize: () => ({ width: 300, height: 288 }) } }; + let viewport = bordered(300, 288); + const ctx = { stack: { getBaseSize: () => viewport } }; const paintWidget = () => { const image = new f.Image(); widget.paint(image); return image.text.join(' '); }; widget.start(() => renders++); widget.start(() => renders++); assert.equal(f.timers.size, 1); f.permission(false); assert.match(layer.paint(ctx).text.join(' '), /Grant calendar permission/); @@ -187,6 +189,16 @@ test('Calendar and Glanceboard show permission, loading, error and event states f.reads[1].done(JSON.stringify([f.event('event')]), null); assert.match(layer.paint(ctx).text.join(' '), /Meeting/); assert.match(paintWidget(), /Meeting/); + // Beside border lines the selection box keeps the suggested padding and the + // title lines up with row text; with nothing past the edges they run to them. + const framed = layer.paint(ctx); + assert.deepEqual(framed.at.Calendar, [18, 10]); + assert.deepEqual(framed.boxes[0], [8, 54, 284, 32]); + assert.equal(framed.at.Office[0], 18); + viewport = unbordered(300, 288); + const bare = layer.paint(ctx); + assert.deepEqual(bare.at.Calendar, [10, 2]); + assert.deepEqual(bare.boxes[0], [0, 46, 300, 32]); widget.stop(); const before = renders; f.bridge.changeHandler(); assert.equal(renders, before); assert.equal(f.timers.size, 0); }); diff --git a/tests/ios-glanceboard.test.cjs b/tests/ios-glanceboard.test.cjs index caaee81f..b9d55157 100644 --- a/tests/ios-glanceboard.test.cjs +++ b/tests/ios-glanceboard.test.cjs @@ -191,7 +191,10 @@ function fixture() { : ({ 0: 'click', 1: 'scroll-up', 2: 'scroll-down', 3: 'double-click', 9: 'long-press', 10: 'long-press-release', 11: 'short-then-long-press' })[event.eventType] ?? 'unknown', source: 'ring', }) }, - '../ui/shell/geometry': { appViewportRect: () => ({ x: 0, y: 0, width: 640, height: 480 }) }, + '../ui/shell/geometry': { + appViewportRect: () => ({ x: 0, y: 0, width: 640, height: 480 }), + windowEdges: () => ({ top: 'offscreen', right: 'offscreen', bottom: 'offscreen', left: 'offscreen' }), + }, }; const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', modules, { ...clock, console: { log() {}, warn() {}, error: text => errors.push(text) }, diff --git a/tests/listening-header.test.cjs b/tests/listening-header.test.cjs index 19ffc044..0c234b60 100644 --- a/tests/listening-header.test.cjs +++ b/tests/listening-header.test.cjs @@ -81,20 +81,23 @@ test('the header puts title and status on one line, truncated before the meter', const h = harness(); const meter = new h.MicLevelMeter(); const image = fakeImage(300); - const bodyTop = h.paintListeningHeader(image, font, 'Translate', 'Listening (ElevenLabs)...', meter); - assert.equal(bodyTop, 20 + 18 + 2); - assert.deepEqual(image.texts.map(({ x, y, gray }) => [x, y, gray]), [[24, 20, 200], [24 + 54, 20, 110]]); + // Inside the padding suggested for the window's edges: here a border at + // the top and left, blank space at the right. + const padding = { top: 8, right: 0, bottom: 8, left: 8 }; + const bodyTop = h.paintListeningHeader(image, font, 'Translate', 'Listening (ElevenLabs)...', meter, padding); + assert.equal(bodyTop, 8 + 18 + 2); + assert.deepEqual(image.texts.map(({ x, y, gray }) => [x, y, gray]), [[8, 8, 200], [8 + 54, 8, 110]]); assert.equal(image.texts[0].text, 'Translate'); const status = image.texts[1].text; assert.ok(status.startsWith(' · Listening') && status.endsWith('...'), status); const meterX = Math.min(...image.rects.map(rect => rect.x)); - assert.equal(meterX, 300 - 24 - meter.width); - assert.ok(24 + 54 + status.length * 6 <= meterX - 10, 'status stops short of the meter'); + assert.equal(meterX, 300 - meter.width, 'the meter ends at the unpadded right edge'); + assert.ok(8 + 54 + status.length * 6 <= meterX - 10, 'status stops short of the meter'); // Segments sit on the text baseline. - assert.ok(image.rects.every(rect => rect.y + rect.h === 20 + font.ascent)); + assert.ok(image.rects.every(rect => rect.y + rect.h === 8 + font.ascent)); const idle = fakeImage(300); - h.paintListeningHeader(idle, font, 'Transcribe', 'Starting microphone...', null); + h.paintListeningHeader(idle, font, 'Transcribe', 'Starting microphone...', null, padding); assert.equal(idle.rects.length, 0); assert.equal(idle.texts[1].text, ' · Starting microphone...'); }); diff --git a/tests/menu-core.test.cjs b/tests/menu-core.test.cjs index 633ad57c..a1de800c 100644 --- a/tests/menu-core.test.cjs +++ b/tests/menu-core.test.cjs @@ -75,6 +75,40 @@ test('scrolling keeps the selection and its neighbors visible and aligns the vie assert.equal(menu.scrollTop, 0); }); +test('padTop and padBottom are blank rows inside the scrolled content, shown only at its ends', async () => { + // A box that runs to a window's edges: 8px of padding scrolls with the rows. + const box = { x: 10, y: 5, width: 80, height: 60, padTop: 8, padBottom: 8 }; + const { menu, paint, image } = stringMenu(); + const step = async (type) => { await menu.handleInput({ type }); paint(true, box); return menu.scrollTop; }; + const rows = paint(true, box); + assert.equal(menu.scrollTop, 0); + assert.equal(rows[1].y, 5 + 8 + 20, 'rows start below the padding'); + assert.equal(menu.indexAt(5 + 4), null, 'the padding is no row'); + assert.equal(menu.indexAt(5 + 8), 0); + assert.equal(menu.overflows, true); + // Rows scrolled off are cut at the box's edge: the viewport rests on a row + // top, without the padding, until the first row comes back. + assert.equal(await step('scroll-down'), 0); + assert.equal(await step('scroll-down'), 28, 'b sits right at the top edge'); + assert.equal(menu.indexAt(5), 1); + assert.equal(await step('scroll-down'), 28); + assert.equal(await step('scroll-down'), 68, 'the last row shows with the padding under it'); + assert.equal(menu.selectedIndex, 4); + assert.equal(await step('scroll-up'), 68); + assert.equal(await step('scroll-up'), 28); + assert.equal(await step('scroll-up'), 28); + assert.equal(await step('scroll-up'), 0, 'back at the first row, its padding shows again'); + // Content that fits just sits below the top padding. + const short = stringMenu({ items: ['a', 'b'] }); + const drawn = short.paint(true, box); + assert.equal(short.menu.overflows, false); + assert.equal(drawn[1].y, 5 + 8 + 20); + // A scrollbar spans the padded content. + const before = image.pixels.slice(); + menu.drawScrollbar(image, 95, 5, 60); + assert.ok(image.pixels.some((value, index) => value !== before[index])); +}); + test('without wrap the ends call the exit callbacks and hold the selection', async () => { const { menu, exits } = stringMenu(); await menu.handleInput({ type: 'scroll-up' }); diff --git a/tests/menu-display-list.test.cjs b/tests/menu-display-list.test.cjs index f8e25202..8fb9f0d5 100644 --- a/tests/menu-display-list.test.cjs +++ b/tests/menu-display-list.test.cjs @@ -50,6 +50,7 @@ test('later opaque planes add an occlusion to a replayed selection',()=>{ }); const {LayerStack,noopLayerActions}=load('app/ui/layers.ts',{ + './viewport':require('../.test-build/app/ui/viewport.js'), '../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},'../graphics/screen-size':{screenWidth:()=>640,screenHeight:()=>480},'./gestures':{}, }); test('context menu surface and selected row both use +4 without changing the app screen',()=>{ @@ -121,7 +122,9 @@ test('unselected sidebar icons retain -2 depth through shell cropping, including }); test('shell modal carries its inner menu highlight animation instead of baking its first frame',async()=>{ const {ShellModalLayer}=load('app/ui/shell/modal-layer.ts',{ + '../viewport':require('../.test-build/app/ui/viewport.js'), '../../graphics/image':graphics,'../../graphics/plane':planes,'../gestures':{},'../layers':load('app/ui/layers.ts',{ + './viewport':require('../.test-build/app/ui/viewport.js'), '../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},'../graphics/screen-size':{screenWidth:()=>640,screenHeight:()=>480}, './gestures':{isDirectionalInput:()=>false}, }), @@ -147,6 +150,7 @@ test('a stack without its own size follows the screen when the glasses type chan // RayNeo iO must not leave its overlays painting on a 540x220 canvas. const screen={width:540,height:220}; const {LayerStack}=load('app/ui/layers.ts',{ + './viewport':require('../.test-build/app/ui/viewport.js'), '../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()}, '../graphics/screen-size':{screenWidth:()=>screen.width,screenHeight:()=>screen.height},'./gestures':{}, }); @@ -156,10 +160,18 @@ test('a stack without its own size follows the screen when the glasses type chan const sizes=()=>JSON.parse(JSON.stringify(stack.paint().map(p=>[p.image.width,p.image.height]))); assert.deepEqual(sizes(),[[540,220],[540,220]]); screen.width=640;screen.height=480; - assert.deepEqual({...stack.getBaseSize()},{width:640,height:480}); + const plain=value=>JSON.parse(JSON.stringify(value)); + const unbordered={edges:{top:'offscreen',right:'offscreen',bottom:'offscreen',left:'offscreen'},padding:{top:0,right:0,bottom:0,left:0}}; + assert.deepEqual(plain(stack.getBaseSize()),{width:640,height:480,...unbordered}); assert.deepEqual(sizes(),[[640,480],[640,480]]); - // An explicit size (a window's viewport, the modal's interior) stays put. + // An explicit size (a window's viewport, the modal's interior) stays put; + // given bare, nothing borders it, so it suggests no padding. const sized=new LayerStack({paint:()=>new graphics.GrayImage(100,90,0)},noopLayerActions,{width:100,height:90}); screen.width=540;screen.height=220; - assert.deepEqual({...sized.getBaseSize()},{width:100,height:90}); + assert.deepEqual(plain(sized.getBaseSize()),{width:100,height:90,...unbordered}); + // A host that says what its edges touch hands layers the padding for them. + const viewport=require('../.test-build/app/ui/viewport.js'); + const edges={top:'border',right:'offscreen',bottom:'offscreen',left:'border'}; + sized.setBaseSize(viewport.viewportWith({width:100,height:90},edges)); + assert.deepEqual(plain(sized.getBaseSize()),{width:100,height:90,edges,padding:{top:8,right:0,bottom:0,left:8}}); }); diff --git a/tests/music-settings.test.cjs b/tests/music-settings.test.cjs index fdcfa1d8..6d5db2b7 100644 --- a/tests/music-settings.test.cjs +++ b/tests/music-settings.test.cjs @@ -1,5 +1,6 @@ const test = require('node:test'); const assert = require('node:assert/strict'); +const { bordered } = require('./helpers/viewport.cjs'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); @@ -177,7 +178,7 @@ test('Music settings keeps toggles and selection when a new app arrives and supp }; const { MusicSettingsLayer } = load('app/apps/music/music-settings.ts', (name) => uiDeps[name]); const layer = new MusicSettingsLayer(); - const ctx = { stack: { getBaseSize: () => ({ width: 540, height: 224 }), isFocused: () => true, pop: () => closes++ } }; + const ctx = { stack: { getBaseSize: () => bordered(540, 224), isFocused: () => true, pop: () => closes++ } }; const paint = () => layer.paint(ctx, () => new RecordingImage(540, 224)); const input = (type) => layer.handleInput({ type }, ctx); assert.equal(refreshes, 1); diff --git a/tests/navigate-worker.test.cjs b/tests/navigate-worker.test.cjs index 2de253dd..0d55a93b 100644 --- a/tests/navigate-worker.test.cjs +++ b/tests/navigate-worker.test.cjs @@ -5,6 +5,7 @@ const vm = require('node:vm'); const ts = require('typescript'); const path = require('node:path'); const { loader } = require('./helpers/load-typescript.cjs'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); const flush = () => new Promise(resolve => setImmediate(resolve)); const route = { coordinates: [[-122.4, 37.7], [-122.4, 37.71]], distanceMeters: 1112, durationSec: 600, @@ -13,7 +14,7 @@ const route = { coordinates: [[-122.4, 37.7], [-122.4, 37.71]], distanceMeters: const fix = latitude => ({ latitude, longitude: -122.4, timestampMs: Date.now(), bearingDeg: 0, speedMps: 1.5, accuracyMeters: 5 }); function fixture() { const messages = [], timers = new Set(), timeouts = new Set(), holds = [], maps = [], errors = [], requests = []; - let callbacks, compass, delayRoute = null, delayMap = null, routeError = null, distanceUnit = 'miles', timeFormat = '24h'; + let callbacks, compass, delayRoute = null, delayMap = null, routeError = null, distanceUnit = 'miles', timeFormat = '24h', paints = 0; const routeMath = loader()('app/apps/navigate/route-follower.ts'); const tracker = { running: false, isRunning() { return this.running; }, start() { this.running = true; }, stop() { this.running = false; } }; const sensor = { LocationTracker: function (value) { callbacks = value; return tracker; }, COMPASS_CHANGED: 15, @@ -22,7 +23,8 @@ function fixture() { handleNavigationSensorEvent: event => { if (event.kind === 'location') callbacks.onLocation(event.location); } }; const modules = { '@nativescript/core/globals': {}, - '../../graphics/bdffont': { getFont: () => ({}) }, '../../graphics/ui-fonts': { getDefaultSmallFont: () => ({}) }, + // The footer hint's line height sets how much of the window the map-mode map gets. + '../../graphics/bdffont': { getFont: () => ({}) }, '../../graphics/ui-fonts': { getDefaultSmallFont: () => ({ lineHeight: 12 }) }, '../../native/settings-store': { onSettingsStoreChanged() {} }, '../../ui/dashboard-settings': { distanceUnitSetting: { get: () => distanceUnit } }, '../../ui/clock-format': loader({}, { './dashboard-settings': { timeFormatSetting: { get: () => timeFormat } } })('app/ui/clock-format.ts'), @@ -34,7 +36,8 @@ function fixture() { '../../native/frame-timings': { startFrame: () => 1, finishFrame() {}, logFrame() {}, span: (_id, _name, fn) => fn(), runWithFrame: (_id, fn) => fn() }, '../../native/active-display': { getActiveDisplay: () => null }, '../../ui/menu-core': require('../.test-build/app/ui/menu-core.js'), - '../../ui/window-menu': { WindowMenu: class { paint() { return []; } isOpen() { return false; } resize() {} } }, + '../../ui/window-menu': { WindowMenu: class { paint() { paints++; return []; } isOpen() { return false; } resize() {} } }, + '../../ui/viewport': require('../.test-build/app/ui/viewport.js'), '../../graphics/plane': { planesFingerprint: () => 'frame' }, '../../native/mapbox': { isMapboxConfigured: () => true, @@ -54,9 +57,9 @@ function fixture() { console: { log() {}, warn() {}, error: error => errors.push(error) }, require: id => modules[id] ?? {}, }); const send = data => global.onmessage({ data }); - send({ type: 'open-window', windowId: 'navigate:main', surfaceId: 'window:navigate:main', title: 'Navigate', viewport: { width: 576, height: 480 } }); + send({ type: 'open-window', windowId: 'navigate:main', surfaceId: 'window:navigate:main', title: 'Navigate', viewport: bordered(576, 480) }); send({ type: 'foreground', windowId: 'navigate:main', foreground: true, focused: true }); - return { ...exports.probe, send, timers, holds, maps, errors, tracker, requests, + return { ...exports.probe, send, timers, holds, maps, errors, tracker, requests, paints: () => paints, input: type => send({ type: 'input', windowId: 'navigate:main', focused: true, frameId: 1, event: { type } }), tick: () => { for (const callback of timers) callback(); }, expireFix: () => { for (const callback of timeouts) callback(); timeouts.clear(); }, @@ -114,8 +117,9 @@ test('front page map entry follows location without routing, supports zoom and r h.fix(37.7); await flush(); assert.equal(h.state().phase, 'map'); assert.deepEqual(h.requests, []); + // The map fills the window down to the footer hint, which rests on the bottom padding. assert.equal(h.maps.at(-1).width, 576); - assert.equal(h.maps.at(-1).height, 450); + assert.equal(h.maps.at(-1).height, 452); assert.equal(h.maps.at(-1).routePath, undefined); assert.equal(h.maps.at(-1).camera.latitude, 37.7); assert.equal(h.maps.at(-1).camera.bearingDeg, 0); @@ -147,9 +151,20 @@ test('map suspends fetching and compass while hidden and resizes to the viewport assert.equal(h.holds.at(-1), false); h.send({ type: 'screen', on: true }); await flush(); assert.equal(h.maps.at(-1).camera.latitude, 37.71); - h.send({ type: 'resize-window', windowId: 'navigate:main', viewport: { width: 540, height: 260 } }); await flush(); + const resize = async viewport => { h.send({ type: 'resize-window', windowId: 'navigate:main', viewport }); await flush(); }; + await resize(bordered(540, 260)); assert.equal(h.maps.at(-1).width, 540); - assert.equal(h.maps.at(-1).height, 230); + assert.equal(h.maps.at(-1).height, 232); + // With no border below, the footer hint moves down to the edge and the map follows it. + await resize(edged(540, 260, 'top', 'left')); + assert.equal(h.maps.at(-1).height, 240); + // A change to the other edges lays the window out again, but keeps the map it has. + const fetched = h.maps.length, painted = h.paints(); + await resize(unbordered(540, 260)); + assert.equal(h.maps.length, fetched); + assert.equal(h.paints(), painted + 1); + await resize(unbordered(540, 260)); + assert.equal(h.paints(), painted + 1); h.send({ type: 'close-window' }); assert.equal(h.tracker.running, false); assert.equal(h.timers.size, 0); diff --git a/tests/nightscout.test.cjs b/tests/nightscout.test.cjs index 34340701..1749c71b 100644 --- a/tests/nightscout.test.cjs +++ b/tests/nightscout.test.cjs @@ -4,6 +4,7 @@ const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); const ts = require('typescript'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); const alerts = require('../.test-build/app/apps/nightscout/nightscout-alerts.js'); const detector = require('../.test-build/app/apps/nightscout/missed-bolus-detector.js'); const now = 1_800_000_000_000; @@ -53,6 +54,7 @@ class Image { drawText(font, ...args) { this.draws.push(['text', ...args]); } drawLine(...args) { this.draws.push(['line', ...args]); } fillRect(...args) { this.draws.push(['rect', ...args]); } + drawRect(...args) { this.draws.push(['frame', ...args]); } } const font = { measureText: text => text.length * 6, lineHeight: 12, hasGlyph: () => true }; @@ -197,17 +199,22 @@ test('tray keeps every pixel row when a TTF font measures the reading fractional } }); -test('main page highlights only the breached field and removes highlights on recovery', () => { - const f = fixture(); - const page = load('app/apps/nightscout/nightscout.ts', { +/** nightscout.ts (the app window's page) over a fixture's bridge and settings. */ +function loadMainPage(f) { + return load('app/apps/nightscout/nightscout.ts', { ...f.deps, '../../graphics/textwrap': { truncateText: (font, text) => text }, '../../ui/gestures': {}, '../../ui/layers': {}, '../../ui/menu': {}, '../../ui/dashboard/settings-panel': {}, '../../ui/metrics': { lineStep: () => 16 }, '~/util/date-util': { formatAgeShortFromTimestamp: () => '1m', formatTimestamp: () => '12:00' }, }, { Date: { now: () => now } }); +} + +test('main page highlights only the breached field and removes highlights on recovery', () => { + const f = fixture(); + const page = loadMainPage(f); const layer = new page.NightscoutLayer(); - const paint = () => layer.paint({ stack: { getBaseSize: () => ({ width: 576, height: 260 }) } }); + const paint = () => layer.paint({ stack: { getBaseSize: () => bordered(576, 260) } }); for (const [field, value, label] of [['reservoirUnits', 10, '10U'], ['batteryVoltage', 1, '1.00V'], ['cageTimestampMs', now - 73 * 3_600_000, 'CAGE 1m'], ['loopTimestampMs', now - 16 * 60_000, 'Loop 1m']]) { f.state[field] = value; const image = paint(); @@ -219,6 +226,37 @@ test('main page highlights only the breached field and removes highlights on rec } }); +test('main page puts its title, readout and graph at the padding suggested for each edge', () => { + const f = fixture(); + Object.assign(f.state, { history: [{ timestampMs: now - 60_000, sgv: 120 }], basal: [], carbs: [], boluses: [] }); + const layer = new (loadMainPage(f).NightscoutLayer)(); + const paint = viewport => layer.paint({ stack: { getBaseSize: () => viewport } }).draws; + const texts = draws => draws.filter(d => d[0] === 'text'); + const graph = draws => draws.find(d => d[0] === 'frame').slice(1, 5); + + // A border on every edge: 8px in from each. The caption is the last line above the bottom padding. + const framed = paint(bordered(576, 260)); + assert.deepEqual(texts(framed)[0].slice(1, 4), [8, 8, 'Nightscout']); + assert.deepEqual(texts(framed)[1].slice(1, 4), [8, 64, '100'], 'readout, centered between the title (to y=20) and the graph (from y=120)'); + assert.deepEqual(graph(framed), [8, 120, 560, 114]); + assert.deepEqual(texts(framed).at(-1).slice(1, 3), [8, 240]); + + // No borders: the page runs to the window's edges and the graph takes the room. + const bare = paint(unbordered(576, 260)); + assert.deepEqual(texts(bare)[0].slice(1, 3), [0, 0]); + assert.deepEqual(graph(bare), [0, 112, 576, 130]); + assert.deepEqual(texts(bare).at(-1).slice(1, 3), [0, 248]); + + // Each edge is padded on its own. + assert.deepEqual(graph(paint(edged(576, 260, 'top', 'left'))), [8, 120, 568, 122]); + assert.deepEqual(graph(paint(edged(576, 260, 'bottom', 'right'))), [0, 112, 568, 122]); + + // A window too short for the graph and its caption gives the graph the caption's room, inside the image. + const short = paint(bordered(476, 155)); + assert.deepEqual(graph(short), [8, 120, 460, 27]); + assert.ok(!texts(short).some(d => /glucose history/.test(d[3]))); +}); + test('bridge exposes numeric measurements and treats absent values as unknown', async () => { let pump = { reservoir: '10.5', battery: { voltage: '1.15' } }; const module = load('app/native/nightscout-bridge.ts', { diff --git a/tests/notification-filter.test.cjs b/tests/notification-filter.test.cjs index bb7d116a..5c414dbb 100644 --- a/tests/notification-filter.test.cjs +++ b/tests/notification-filter.test.cjs @@ -1,5 +1,6 @@ const test = require('node:test'); const assert = require('node:assert/strict'); +const { bordered } = require('./helpers/viewport.cjs'); const fs = require('node:fs'); const path = require('node:path'); const vm = require('node:vm'); @@ -133,7 +134,7 @@ function ui(fontSize = 12) { dependencies['./menu'] = load('app/ui/menu.ts', requireModule); 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 ctx = { stack: { getBaseSize: () => bordered(540, 224), isFocused: () => true, pop: () => 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) }); diff --git a/tests/onboarding-layout.test.cjs b/tests/onboarding-layout.test.cjs index fc2122f8..4162ec45 100644 --- a/tests/onboarding-layout.test.cjs +++ b/tests/onboarding-layout.test.cjs @@ -1,6 +1,7 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); +const { bordered, unbordered } = require('./helpers/viewport.cjs'); // Roboto's line heights at the onboarding sizes, and a fixed advance. const FONTS = { title: 31, body: 24, detail: 19, hero: 52 }; @@ -53,7 +54,8 @@ test('the layout page fits four 40%-wide diagrams with captions on the immersive const page = new LayoutPage(); assert.equal(page.immersive, true); const image = recorder(640, 480); - page.paint(image, true); + // An immersive page's edges are the screen's: no padding. + page.paint(image, true, unbordered(640, 480).padding); // Each diagram's outline spans the display's width at 40%. const areas = image.calls.filter((call) => call.kind === 'outline' && call.w === 256); assert.equal(areas.length, 4); @@ -79,6 +81,16 @@ test('the layout page fits four 40%-wide diagrams with captions on the immersive // Bottom, recommended, is the selected cell at first, filled while focused. const box = image.calls.find((call) => call.kind === 'fill' && call.value === 15); assert.ok(box.x <= areas[0].x && box.y <= areas[0].y && box.y + box.h >= bottom.y); + // With nothing bordering the page, its title sits 10px in (where list rows' text starts) and 2px down. + const title = text('Layout')[0]; + assert.deepEqual([title.x, title.y], [10, 2]); + // Beside border lines the heading keeps their padding, and the grid stays centred. + const framed = recorder(640, 480); + page.paint(framed, true, bordered(640, 480).padding); + const framedTitle = framed.calls.find((call) => call.kind === 'text' && call.text === 'Layout'); + assert.deepEqual([framedTitle.x, framedTitle.y], [18, 10]); + const framedAreas = framed.calls.filter((call) => call.kind === 'outline' && call.w === 256); + assert.deepEqual(framedAreas.map((area) => area.x), areas.map((area) => area.x)); }); test('the layout page starts on Bottom whatever is set, swipes in reading order, and a tap saves the choice', () => { @@ -137,12 +149,24 @@ test('the text size page offers 12 to 18px, starts on the recommended 16px, and assert.deepEqual(rows.filter((texts) => texts.length > 1).length, 1); // The size column holds the note; the preview is paragraphs of text, no menu. const image = recorder(640, 480); - page.paint(image, true); + page.paint(image, true, unbordered(640, 480).padding); assert.ok(menu.box.width >= 10 + '16 px'.length * 8 + 8 + 'Recommended'.length * 8); const preview = image.calls.filter((call) => call.kind === 'text' && call.x > menu.box.x + menu.box.width); assert.equal(preview[0].text.startsWith('This is how text will look'), true); assert.ok(preview.length > 3); assert.equal(image.calls.some((call) => call.kind === 'text' && call.text === 'Timers'), false); + // Unpadded, the size list starts at the window's left edge and the preview's frame reaches its right and bottom edges. + const frame = image.calls.find((call) => call.kind === 'outline'); + assert.equal(menu.box.x, 0); + assert.deepEqual([frame.x + frame.w, frame.y + frame.h], [640, 480]); + // The list scrolls, so it runs to the bottom edge whatever borders it, the edge's padding inside it. + assert.deepEqual([menu.box.y + menu.box.height, menu.box.padBottom], [480, 0]); + const framed = recorder(640, 480); + page.paint(framed, true, bordered(640, 480).padding); + const paddedFrame = framed.calls.find((call) => call.kind === 'outline'); + assert.equal(menu.box.x, 8); + assert.deepEqual([paddedFrame.x + paddedFrame.w, paddedFrame.y + paddedFrame.h], [632, 472]); + assert.deepEqual([menu.box.y + menu.box.height, menu.box.padBottom], [480, 8]); // A tap saves; coming back keeps the size chosen. page.handleInput({ type: 'scroll-up' }, { next: () => {} }); page.handleInput({ type: 'click' }, { next: () => {} }); @@ -186,7 +210,7 @@ test('on a RayNeo iO the layout page recommends and starts on Left, and fits the assert.equal(recommendedLayout(), 'left'); const page = new LayoutPage(); const image = recorder(540, 220); - page.paint(image, true); + page.paint(image, true, unbordered(540, 220).padding); const captions = image.calls.filter((call) => call.kind === 'text').map((call) => call.text); assert.ok(captions.includes('Left (Recommended)')); assert.ok(captions.includes('Bottom')); @@ -289,9 +313,19 @@ test('the display mode page offers Full Screen only where the layout allows it, assert.equal(menu.selectedItem, 'fullscreen'); // The selected mode's description sits beside the list, within the window (a band's). const image = recorder(576, 260); - page.paint(image, true); + page.paint(image, true, bordered(576, 260).padding); const description = image.calls.filter((call) => call.kind === 'text' && call.x >= menu.box.x + menu.box.width); assert.match(description.map((call) => call.text).join(' '), /Depth is turned off/); + // Beside border lines: the title at the UI-wide (18, 10), the list's rows from the padding, the list to the bottom edge. + const title = (calls) => calls.find((call) => call.kind === 'text' && call.text === 'Display mode'); + assert.deepEqual([title(image.calls).x, title(image.calls).y], [18, 10]); + assert.deepEqual([menu.box.x, menu.box.y + menu.box.height, menu.box.padBottom], [8, 260, 8]); + assert.ok(description.every((call) => call.x + call.text.length * 8 <= 576 - 18 && call.y + FONTS.detail <= 260 - 8)); + // With no border on the left or bottom, the page runs to those edges. + const open = recorder(576, 260); + page.paint(open, true, { top: 8, right: 8, bottom: 0, left: 0 }); + assert.deepEqual([title(open.calls).x, title(open.calls).y], [10, 10]); + assert.deepEqual([menu.box.x, menu.box.y + menu.box.height, menu.box.padBottom], [0, 260, 0]); let next = 0; const nav = { next: () => next++ }; page.handleInput({ type: 'scroll-up' }, nav); diff --git a/tests/settings-panel.test.cjs b/tests/settings-panel.test.cjs index 0ccefd00..34dc7c9e 100644 --- a/tests/settings-panel.test.cjs +++ b/tests/settings-panel.test.cjs @@ -15,6 +15,7 @@ function load(file, modules) { } const graphics = require('../.test-build/app/graphics/image.js'); +const { bordered, unbordered } = require('./helpers/viewport.cjs'); const metrics = require('../.test-build/app/ui/metrics.js'); const menuCore = require('../.test-build/app/ui/menu-core.js'); const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} }); @@ -34,8 +35,8 @@ const { SettingsPanelLayer } = load('app/ui/dashboard/settings-panel.ts', { '../shell/shell': { shell: { yieldFocusToSidebar: () => yields++ } }, }); -/** A panel over three sections; records which item callbacks fire. */ -function panel() { +/** A panel over three sections, in a framed window unless given another; records which item callbacks fire. */ +function panel(viewport = bordered(576, 260)) { const selected = []; const item = (label, extra = {}) => ({ label, onSelect: () => selected.push(label), ...extra }); const layer = new SettingsPanelLayer([ @@ -43,7 +44,7 @@ function panel() { { label: 'Voice', items: Array.from({ length: 20 }, (_, i) => item(`Voice ${i}`, { description: 'Help text for this option.' })) }, { label: 'About', items: [], renderDetail: () => {} }, ]); - const ctx = { stack: { getBaseSize: () => ({ width: 576, height: 260 }), isFocused: () => true } }; + const ctx = { stack: { getBaseSize: () => viewport, isFocused: () => true } }; const input = (type) => layer.handleInput({ type, timestampMs: 0 }, ctx); return { layer, input, paint: () => layer.paint(ctx), selected }; } @@ -104,7 +105,7 @@ test('the selected row stays clear of its description band', async () => { await p.input('scroll-up'); const row = selections(p.paint())[1]; const overlay = new graphics.GrayImage(576, 260); - p.layer.paintDescriptionOverlay(overlay, { stack: { getBaseSize: () => ({ width: 576, height: 260 }) } }); + p.layer.paintDescriptionOverlay(overlay, { stack: { getBaseSize: () => bordered(576, 260) } }); let bandTop = -1; for (let y = 0; y < 260 && bandTop < 0; y++) if (overlay.getPixel(300, y) !== 0) bandTop = y; assert.ok(bandTop > 0, 'the band is drawn'); @@ -118,7 +119,7 @@ test('a section\'s detail gives its rows the pane while focus is in them, where // About's blurb under its 72px logo: about 145px. const about = { label: 'About', items: docs.map((label) => ({ label, onSelect() {} })), renderDetail: ({ image, x, y }) => { detailDraws.push(image); image.fillRect(x, y, 72, 72, 90); return 145; } }; - for (const [size, keeps] of [[{ width: 576, height: 260 }, true], [{ width: 476, height: 192 }, false], [{ width: 476, height: 155 }, false]]) { + for (const [size, keeps] of [[bordered(576, 260), true], [bordered(476, 192), false], [bordered(476, 155), false]]) { const layer = new SettingsPanelLayer([about]); const ctx = { stack: { getBaseSize: () => size, isFocused: () => true } }; const paint = () => { detailDraws.length = 0; const image = layer.paint(ctx); return { image, drawn: detailDraws.includes(image) }; }; @@ -133,3 +134,26 @@ test('a section\'s detail gives its rows the pane while focus is in them, where assert.ok(row.y + row.source.height <= size.height - 8); } }); + +test('the columns keep the padding suggested for each window edge, and scroll right up to the top and bottom', async () => { + // Where the left column's first-row highlight (a retained selection) starts. + const first = (viewport) => { const row = selections(panel(viewport).paint())[0]; return [row.x, row.y]; }; + assert.deepEqual(first(bordered(576, 260)), [8, 8], 'beside border lines, 8px in'); + assert.deepEqual(first(unbordered(576, 260)), [0, 0], 'beside blank space, right at the edges'); + // The right column ends at the right edge's padding. + for (const [viewport, right] of [[bordered(576, 260), 568], [unbordered(576, 260), 576]]) { + const p = panel(viewport); + await p.input('click'); + const row = selections(p.paint())[1]; + assert.equal(row.x + row.source.width, right); + } + // A long list scrolled to its middle is cut off at the window's own top + // and bottom edges, not at the padding: a row starts at y 0. + const p = panel(bordered(576, 260)); + await p.input('scroll-down'); + await p.input('click'); + for (let i = 0; i < 10; i++) await p.input('scroll-down'); + const image = p.paint().withDrawsBaked(); + const lit = (y) => { for (let x = 180; x < 560; x++) if (image.getPixel(x, y) > 40) return true; return false; }; + assert.ok([0, 1, 2, 3, 4, 5, 6, 7].some(lit), 'rows show in the top padding once scrolled'); +}); diff --git a/tests/terminal-activity.test.cjs b/tests/terminal-activity.test.cjs index af8678c3..441745dd 100644 --- a/tests/terminal-activity.test.cjs +++ b/tests/terminal-activity.test.cjs @@ -149,7 +149,7 @@ test('host applies activity only to its own open windows and cleans up on close' '../../graphics/image': { GrayImage: Image }, './chrome-layer': { windowIcon: (icon, letter, glyph, phase) => phase }, '../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } }, - './geometry': { appViewportSize: () => ({ width: 576, height: 260 }) }, + './geometry': { appViewport: () => ({ width: 576, height: 260 }) }, '../../native/frame-timings': {}, './worker-state': {}, './shell': { shell: { registerWindow: w => shellWindows.set(w.windowId, w) } }, diff --git a/tests/terminal-header.test.cjs b/tests/terminal-header.test.cjs index 1ad62a4a..38045051 100644 --- a/tests/terminal-header.test.cjs +++ b/tests/terminal-header.test.cjs @@ -1,30 +1,50 @@ const test = require('node:test'); const assert = require('node:assert/strict'); -const { HUB_TITLE_X, layoutHubHeader } = require('../.test-build/app/apps/terminal/hub-header.js'); +const { HUB_TITLE_INSET_X, layoutHubHeader } = require('../.test-build/app/apps/terminal/hub-header.js'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); const font = size => ({ measureText: text => [...text].length * size, getGlyph: () => ({ dwidthX: size }) }); +// Framed all round: the title 8px in plus its inset, and 2px below the top padding. +const TITLE_X = 8 + HUB_TITLE_INSET_X; test('terminal short status stays inline; exact-fit status does not move the list', () => { const f = font(7), title = 'Terminal', status = 'Connected.'; - const width = HUB_TITLE_X + f.measureText(title) + 16 + f.measureText(status) + 12; - const header = layoutHubHeader(f, title, status, width, 18); + const width = TITLE_X + f.measureText(title) + 16 + f.measureText(status) + 8; + const header = layoutHubHeader(f, title, status, bordered(width, 260), 18); + assert.deepEqual([header.titleX, header.titleY], [TITLE_X, 10]); assert.equal(header.y, 10); assert.equal(header.listTop, 34); assert.deepEqual(header.lines, [status]); - assert.equal(layoutHubHeader(f, title, status, width - 1, 18).y, 28); + assert.equal(layoutHubHeader(f, title, status, bordered(width - 1, 260), 18).y, 28); }); test('terminal long error wraps in full-width block and moves menu below all lines at each font size', () => { const status = 'Connection failed. The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.'; for (const size of [5, 7, 12, 20]) for (const width of [320, 576, 640]) { const f = font(size), step = size * 2; - const header = layoutHubHeader(f, 'Terminal - Connections', status, width, step); - assert.equal(header.x, HUB_TITLE_X); assert.equal(header.y, 10 + step); + const header = layoutHubHeader(f, 'Terminal - Connections', status, bordered(width, 260), step); + assert.equal(header.x, TITLE_X); assert.equal(header.y, 10 + step); assert.equal(header.lines.join(' '), status); - assert.ok(header.lines.every(line => f.measureText(line) <= width - HUB_TITLE_X - 12)); + assert.ok(header.lines.every(line => f.measureText(line) <= width - TITLE_X - 8)); assert.ok(header.listTop >= header.y + header.lines.length * step); assert.equal(header.listTop, 16 + step * (1 + header.lines.length)); } }); test('terminal explicit newlines and long unbroken messages remain complete', () => { - const h = layoutHubHeader(font(7), 'Terminal', 'First\nSecond', 640, 18); - assert.equal(h.x, HUB_TITLE_X); assert.deepEqual(h.lines, ['First', 'Second']); + const h = layoutHubHeader(font(7), 'Terminal', 'First\nSecond', bordered(640, 260), 18); + assert.equal(h.x, TITLE_X); assert.deepEqual(h.lines, ['First', 'Second']); const word = 'x'.repeat(200); - const wrapped = layoutHubHeader(font(7), 'Terminal', word, 320, 18); + const wrapped = layoutHubHeader(font(7), 'Terminal', word, bordered(320, 260), 18); assert.equal(wrapped.lines.join(''), word); }); +test('terminal header keeps the padding of just the edges a border runs along', () => { + const f = font(7), title = 'Terminal', status = 'Connected.'; + // Nothing bordered: the title starts its inset from the left edge, at the top edge's offset. + const open = layoutHubHeader(f, title, status, unbordered(576, 260), 18); + assert.deepEqual([open.titleX, open.titleY, open.y, open.listTop], [HUB_TITLE_INSET_X, 2, 2, 26]); + // An inline status may run right up to an unbordered right edge, but stops 8px short of a bordered one. + const fit = HUB_TITLE_INSET_X + f.measureText(title) + 16 + f.measureText(status); + assert.equal(layoutHubHeader(f, title, status, unbordered(fit, 260), 18).lines.length, 1); + assert.equal(layoutHubHeader(f, title, status, unbordered(fit, 260), 18).y, 2); + assert.equal(layoutHubHeader(f, title, status, edged(fit, 260, 'right'), 18).y, 2 + 18); + // Each edge on its own. + const topLeft = layoutHubHeader(f, title, 'First\nSecond', edged(576, 260, 'top', 'left'), 18); + assert.deepEqual([topLeft.titleX, topLeft.titleY, topLeft.x, topLeft.y], [TITLE_X, 10, TITLE_X, 28]); + const bottom = layoutHubHeader(f, title, 'First\nSecond', edged(576, 260, 'bottom'), 18); + assert.deepEqual([bottom.titleX, bottom.titleY, bottom.listTop], [HUB_TITLE_INSET_X, 2, 8 + 18 * 3]); +}); diff --git a/tests/terminal-in-place-view.test.cjs b/tests/terminal-in-place-view.test.cjs index fb5abe1a..b0550b07 100644 --- a/tests/terminal-in-place-view.test.cjs +++ b/tests/terminal-in-place-view.test.cjs @@ -1,10 +1,12 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); +const { bordered, unbordered } = require('./helpers/viewport.cjs'); const HUB = 'terminal:hub'; -const MIN = { width: 552, height: 260 }; -const MAX = { width: 552, height: 452 }; +// Framed windows: the shell's messages carry what each edge touches and its padding. +const MIN = bordered(552, 260); +const MAX = bordered(552, 452); const font = { lineHeight: 12, measureText: (text) => text.length * 6, getGlyph: () => null }; const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); @@ -15,9 +17,10 @@ function fakeImageModule() { this.width = width; this.height = height; this.texts = []; + this.positions = []; return new Proxy(this, { get: (target, key) => (key in target ? target[key] : () => {}) }); } - drawText(_font, _x, _y, text) { this.texts.push(text); } + drawText(_font, x, y, text) { this.texts.push(text); this.positions.push([text, x, y]); } drawMenuSelection(row) { this.texts.push(...row.texts.map((text) => `[${text}]`)); } } return { GrayImage }; @@ -94,8 +97,8 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = '../../graphics/image': image, '../graphics/image': image, '../../graphics/plane': { - planesFingerprint: (planes) => JSON.stringify([planes[0].width, planes[0].height, planes[0].texts]), - flattenPlanesWithDraws: (planes) => ({ image: { ...planes[0], width: planes[0].width, height: planes[0].height, texts: planes[0].texts, to8bppBuffer: () => ({ buffer: planes[0].texts }) }, draws: [] }), + planesFingerprint: (planes) => JSON.stringify([planes[0].width, planes[0].height, planes[0].texts, planes[0].positions]), + flattenPlanesWithDraws: (planes) => ({ image: { ...planes[0], width: planes[0].width, height: planes[0].height, texts: planes[0].texts, to8bppBuffer: () => ({ buffer: planes[0] }) }, draws: [] }), }, '../../graphics/glyph-wire': { prepareFrameDraws: () => null }, '../../graphics/ui-fonts': { @@ -109,7 +112,7 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = }, '../../native/active-display': { getActiveDisplay: () => ({ - submitSurfaceFrame: (texts, surfaceId, _x, _y, width, height) => frames.push({ surfaceId, width, height, texts }), + submitSurfaceFrame: ({ texts, positions }, surfaceId, _x, _y, width, height) => frames.push({ surfaceId, width, height, texts, positions }), }), }, '../../ui/gestures': { GESTURE_DOUBLE_CLICK: 'double-tap' }, @@ -148,6 +151,8 @@ function terminal({ sessions = ['vim', 'top'], presets = '', bellWake = true } = return { clients, frames, replies, menus, send, input, openWindow, repliesOf, lastFrame: () => frames[frames.length - 1], + // Where the last frame drew a line of text, as [x, y]. + textAt: (text) => frames[frames.length - 1].positions.find((entry) => entry[0] === text).slice(1), clearReplies: () => { replies.length = 0; }, openMenu: (windowId = HUB) => input('short-then-long-press', windowId), menuLabels: () => Array.from(menus.find((menu) => menu.isOpen()).opened, (item) => item.label), @@ -399,14 +404,49 @@ test('closing the window while it shows a session stops that session', () => { test('a later resize of a session in place reconnects at the new grid, and the list then gets its own size', () => { const t = hubShowingSession(); - t.answerHeight({ width: 552, height: 400 }); + t.answerHeight(bordered(552, 400)); assert.equal(t.clients[1].stopped, false); assert.deepEqual(t.clients[1].viewport, [92, 33]); t.input('double-click'); - t.answerHeight({ width: 552, height: 240 }); + t.answerHeight(bordered(552, 240)); assert.equal(t.lastFrame().height, 240); }); +test('the list lays out inside the padding of its window\'s bordered edges, and follows a change in them', async () => { + const t = terminal(); + t.openWindow(HUB, MIN); + // The title lines up with the rows' text: 6px inside their boxes, which start at the left padding. + assert.deepEqual(t.textAt('Terminal'), [8 + 6, 8 + 2]); + assert.deepEqual(t.textAt('Connected.'), [8 + 6 + 'Terminal'.length * 6 + 16, 8 + 2]); + // The window menu's stack gets the whole viewport, for the pages it shows. + const plain = (value) => JSON.parse(JSON.stringify(value)); + assert.deepEqual(plain(t.menus[0].options.size), plain(MIN)); + + // Same size, nothing bordering it any more (Border: None): a repaint with no padding. + const painted = t.frames.length; + t.send({ type: 'resize-window', windowId: HUB, viewport: unbordered(552, 260) }); + await new Promise((resolve) => setTimeout(resolve, 80)); + assert.equal(t.frames.length, painted + 1); + assert.deepEqual(t.textAt('Terminal'), [6, 2]); + assert.deepEqual(plain(t.menus[0].options.size), plain(unbordered(552, 260))); +}); + +test('a session keeps its connection and grid when only its window\'s edges change', async () => { + const t = hubShowingSession(); + const painted = t.frames.length; + t.send({ type: 'resize-window', windowId: HUB, viewport: unbordered(552, 452) }); + await new Promise((resolve) => setTimeout(resolve, 80)); + assert.equal(t.clients[1].stopped, false); + assert.equal(t.clients[1].viewport, undefined, 'no new grid negotiated'); + assert.equal(t.clients.length, 2); + assert.equal(t.frames.length, painted); + + // Back in the list, the hub takes the padding the shell answers with. + t.input('double-click'); + t.answerHeight(unbordered(552, 260)); + assert.deepEqual(t.textAt('Terminal'), [6, 2]); +}); + test('the shell host resizes the surface before answering with the new viewport', async () => { const worker = { posted: [], postMessage(message) { this.posted.push(message); } }; const surfaces = []; @@ -417,7 +457,7 @@ test('the shell host resizes the surface before answering with the new viewport' '../../graphics/image': {}, './chrome-layer': { windowIcon() {} }, '../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } }, - './geometry': { appViewportSize: (mode) => (mode === 'max' ? MAX : MIN) }, + './geometry': { appViewport: (mode) => (mode === 'max' ? MAX : MIN) }, '../../native/frame-timings': {}, './worker-state': {}, './shell': { shell: { diff --git a/tests/transcribe.test.cjs b/tests/transcribe.test.cjs index 6ef9b8b8..1eef452c 100644 --- a/tests/transcribe.test.cjs +++ b/tests/transcribe.test.cjs @@ -2,9 +2,12 @@ const test = require('node:test'); const assert = require('node:assert/strict'); const { loader } = require('./helpers/load-typescript.cjs'); const load = loader(); -const { TranscriptModel, TranscriptScroll, wrapTranscribeText } = load('app/apps/transcribe/transcript-model.ts'); +const { TranscriptModel, TranscriptScroll, transcriptLayout, wrapTranscribeText } = load('app/apps/transcribe/transcript-model.ts'); const { SpeechPauseDetector } = load('app/native/speech-pause.ts'); const { TimedTranscript } = load('app/native/transcript-format.ts'); +const { bordered, unbordered, edged } = require('./helpers/viewport.cjs'); + +const plain = value => JSON.parse(JSON.stringify(value)); function pcm(amplitude, ms = 50) { const bytes = new Uint8Array(ms * 16 * 2); @@ -82,6 +85,39 @@ test('short transcripts become scrollable without disabling automatic following' assert.equal(s.layout(12, 10), 2); }); +test('the transcript lines up with the header inside the side padding and scrolls to the bottom edge', () => { + // 16px lines stepping 20, under a header that ends at y=38. + const framed = transcriptLayout(bordered(576, 260), 38, 16, 20); + assert.deepEqual(plain(framed), { + // In line with the header's title at the left padding; the right keeps the scrollbar's gutter. + textX: 8, textWidth: 576 - 8 - 8 - 8, pageLines: 10, drawnLines: 11, + scrollbar: { x: 576 - 8 - 2, y: 38, width: 2, height: 260 - 8 - 38 }, + }); + // Following the tail, the last line rests above the bottom padding; scrolled + // back, one more line fits above the window's edge itself. + const lineBottom = (firstLine, total) => 38 + (Math.min(total, firstLine + framed.drawnLines) - 1 - firstLine) * 20 + 16; + const s = new TranscriptScroll(); + assert.equal(s.layout(40, framed.pageLines), 30); + assert.equal(lineBottom(30, 40), 234); + s.scroll(-3); + assert.equal(s.layout(40, framed.pageLines), 27); + assert.equal(lineBottom(27, 40), 254); + assert.ok(lineBottom(27, 40) > 260 - 8 && lineBottom(27, 40) <= 260); + + // With nothing bordering the window, the column and the page gain the padding's room. + assert.deepEqual(plain(transcriptLayout(unbordered(576, 260), 38, 16, 20)), { + textX: 0, textWidth: 576 - 8, pageLines: 11, drawnLines: 11, + scrollbar: { x: 574, y: 38, width: 2, height: 222 }, + }); + const leftOnly = transcriptLayout(edged(576, 260, 'left'), 38, 16, 20); + assert.deepEqual([leftOnly.textX, leftOnly.textWidth, leftOnly.scrollbar.x], [8, 576 - 8 - 8, 574]); + // The scrollbar never stands over the text column. + for (const layout of [framed, leftOnly, transcriptLayout(bordered(476, 155), 38, 16, 20)]) { + assert.ok(layout.textX + layout.textWidth < layout.scrollbar.x); + assert.ok(layout.pageLines >= 1 && layout.drawnLines >= layout.pageLines); + } +}); + test('pause detector uses 1.5 seconds of PCM, fires once per pause, and ignores leading silence', () => { const d = new SpeechPauseDetector(); for (let i = 0; i < 60; i++) assert.equal(d.accept(pcm(0)), false); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index cc93de2f..7f1d06f5 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -26,6 +26,7 @@ "../app/graphics/plane.ts", "../app/graphics/shell-scene.ts", "../app/ui/metrics.ts", + "../app/ui/viewport.ts", "../app/ui/menu-core.ts", "../app/phone-ui/phone-gestures.ts", "../app/native/java-direct-buffer.ts", diff --git a/tests/worker-lifecycle.test.cjs b/tests/worker-lifecycle.test.cjs index cf483106..5ca7fe94 100644 --- a/tests/worker-lifecycle.test.cjs +++ b/tests/worker-lifecycle.test.cjs @@ -11,7 +11,7 @@ function fixture() { const load = loader({ global: {}, setTimeout: (fn, ms) => { timers.set(fn, ms); return fn; }, clearTimeout: fn => timers.delete(fn) }, { '../../graphics/image': {}, './chrome-layer': { windowIcon() {} }, '../../assistant/tool-registry': { toolRegistry: { removeAppTools() {} } }, - './geometry': { appViewportSize: () => ({ width: 576, height: 288 }) }, + './geometry': { appViewport: () => ({ width: 576, height: 288 }) }, '../../native/frame-timings': {}, './worker-state': { publishWorkerState: (...args) => published.push(args) }, './shell': { shell: { registerWindow: w => shellWindows.set(w.windowId, w), setTrayIcon() {}, focusWindow: id => focused.push(id), isWindowFocused: () => false } }, diff --git a/tools/health-preview.cjs b/tools/health-preview.cjs index 37c9aad2..7f617cbf 100644 --- a/tools/health-preview.cjs +++ b/tools/health-preview.cjs @@ -281,7 +281,13 @@ function writePng(name, image) { } // --- the glasses: 640x480, the G2 lens ------------------------------------ -const LENS = { width: 640, height: 480 }; +// As a window with a border line along every edge: the padding the shell +// suggests there (app/ui/viewport.ts BORDER_EDGE_PADDING). +const EDGE_PADDING = 8; +const LENS = { + width: 640, height: 480, + padding: { top: EDGE_PADDING, right: EDGE_PADDING, bottom: EDGE_PADDING, left: EDGE_PADDING }, +}; GLANCE_PAGES.forEach((page, index) => { const name = page.kind === "overview" ? "overview" : page.kind === "sleep" ? "sleep" : page.metric;