From d42e11e2a5f48eda5f1891cae5b2cecf00bac338 Mon Sep 17 00:00:00 2001 From: James Babcock Date: Fri, 24 Jul 2026 01:52:12 -0700 Subject: [PATCH] Fix scrolling in Terminal --- app/ui/apps/buzzer-demo.ts | 9 +--- app/ui/apps/file-browser.ts | 9 +--- app/ui/apps/weather.ts | 9 +--- app/ui/menu.ts | 75 ++++++++++++++++++++++-------- app/ui/shell/chrome-layer.ts | 9 +--- app/ui/shell/launcher-app.ts | 9 +--- app/workers/terminal-app.worker.ts | 33 +++++++++---- 7 files changed, 90 insertions(+), 63 deletions(-) diff --git a/app/ui/apps/buzzer-demo.ts b/app/ui/apps/buzzer-demo.ts index 84bb27f8..351303dc 100644 --- a/app/ui/apps/buzzer-demo.ts +++ b/app/ui/apps/buzzer-demo.ts @@ -3,7 +3,7 @@ import { GrayImage } from "../../graphics/image"; import { clamp } from "../../util/numeric-util"; import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../layers"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures"; -import { drawSelectionHighlight } from "../menu"; +import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../menu"; import { buildSoundSequencePayload, CFW_SEQ_MAX, @@ -43,12 +43,7 @@ export class BuzzerDemoLayer implements Layer { const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT; const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0); - if (this.selectedIndex < this.scrollRow) { - this.scrollRow = this.selectedIndex; - } else if (this.selectedIndex >= this.scrollRow + visibleRows) { - this.scrollRow = this.selectedIndex - visibleRows + 1; - } - this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, SOUND_EFFECTS.length - visibleRows)); + this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, SOUND_EFFECTS.length); const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows); for (let index = this.scrollRow; index < lastVisible; index++) { diff --git a/app/ui/apps/file-browser.ts b/app/ui/apps/file-browser.ts index f24729cb..723d8149 100644 --- a/app/ui/apps/file-browser.ts +++ b/app/ui/apps/file-browser.ts @@ -2,7 +2,7 @@ import { getDefaultSmallFont, type BdfFont } from "../../graphics/bdffont"; import { GrayImage } from "../../graphics/image"; import { clamp } from "../../util/numeric-util"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK } from "../gestures"; -import { drawSelectionHighlight } from "../menu"; +import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../menu"; import { hasAllFilesAccess, listDirectory, @@ -59,12 +59,7 @@ export class FileBrowserLayer implements Layer { const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT; const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0); - if (this.selectedIndex < this.scrollRow) { - this.scrollRow = this.selectedIndex; - } else if (this.selectedIndex >= this.scrollRow + visibleRows) { - this.scrollRow = this.selectedIndex - visibleRows + 1; - } - this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, rows.length - visibleRows)); + this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, rows.length); const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows); for (let index = this.scrollRow; index < lastVisible; index++) { diff --git a/app/ui/apps/weather.ts b/app/ui/apps/weather.ts index fcb3b864..9b050f6b 100644 --- a/app/ui/apps/weather.ts +++ b/app/ui/apps/weather.ts @@ -5,7 +5,7 @@ import { clamp } from "../../util/numeric-util"; import { type ForecastPeriod, type WeatherState } from "../../native/weather"; import { GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_SCROLL } from "../gestures"; import { type DashboardInputEvent, type Layer, type LayerContext } from "../layers"; -import { drawSelectionHighlight } from "../menu"; +import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../menu"; const PAGE_X = 18; const HEADER_Y = 8; @@ -134,12 +134,7 @@ export class WeatherLayer implements Layer { } this.selectedIndex = clamp(this.selectedIndex, 0, forecast.length - 1); const visibleRows = Math.max(1, Math.floor((height - FOOTER_HEIGHT - FORECAST_TOP) / FORECAST_ROW_HEIGHT)); - if (this.selectedIndex < this.scrollRow) { - this.scrollRow = this.selectedIndex; - } else if (this.selectedIndex >= this.scrollRow + visibleRows) { - this.scrollRow = this.selectedIndex - visibleRows + 1; - } - this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, forecast.length - visibleRows)); + this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedIndex, visibleRows, forecast.length); image.drawText(font, PAGE_X, FORECAST_HEADER_Y, "Upcoming", 150); image.drawText(font, FORECAST_TEMP_X, FORECAST_HEADER_Y, "Temp", 105); diff --git a/app/ui/menu.ts b/app/ui/menu.ts index e316c6d4..7da9d923 100644 --- a/app/ui/menu.ts +++ b/app/ui/menu.ts @@ -67,6 +67,46 @@ export function drawSelectionHighlight( image.drawRoundedRect(x, y, width, height, MENU_HIGHLIGHT_SELECTED_BORDER_STROKE, radius); } +/** + * Move a list's scroll position the minimum distance needed to keep the + * selected row inside the visible window, clamped to the list bounds. + * Returns the new scroll row (the index of the first visible item). + */ +export function scrollToKeepSelectionVisible( + scrollRow: number, + selectedIndex: number, + visibleRowCount: number, + itemCount: number, +): number { + if (selectedIndex < scrollRow) { + scrollRow = selectedIndex; + } else if (selectedIndex >= scrollRow + visibleRowCount) { + scrollRow = selectedIndex - visibleRowCount + 1; + } + return clamp(scrollRow, 0, Math.max(0, itemCount - visibleRowCount)); +} + +/** + * Draw a vertical scrollbar beside a row-scrolled list: a dim track with a + * proportional thumb positioned by scroll fraction. Only call when the list + * actually overflows (itemCount > visibleRowCount). + */ +export function drawListScrollbar( + image: GrayImage, + trackX: number, + trackY: number, + trackHeight: number, + scrollRow: number, + visibleRowCount: number, + itemCount: number, +): void { + image.fillRect(trackX, trackY, 3, trackHeight, 30); + const thumbHeight = Math.max(8, (trackHeight * visibleRowCount / itemCount) | 0); + const maxScrollRow = Math.max(1, itemCount - visibleRowCount); + const thumbY = trackY + (((trackHeight - thumbHeight) * clamp(scrollRow, 0, maxScrollRow) / maxScrollRow) | 0); + image.fillRect(trackX, thumbY, 3, thumbHeight, 120); +} + /** * Draw a ">" submenu indicator inset at the right edge of a row's selection * highlight box, vertically centered within it. Pass the same rect as the @@ -151,7 +191,12 @@ export class MenuLayer implements Layer { const contentHeight = chromeTop + this.items.length * MENU_ROW_HEIGHT + MENU_BODY_PADDING; const height = clamp(contentHeight, Math.min(minHeight, maxHeight), maxHeight); const visibleRowCount = Math.max(1, ((height - chromeTop - MENU_BODY_PADDING) / MENU_ROW_HEIGHT) | 0); - this.clampScrollToSelection(visibleRowCount); + this.scrollRow = scrollToKeepSelectionVisible( + this.scrollRow, + this.selectedIndex, + visibleRowCount, + this.items.length, + ); const image = paintBelow(); // Fill 1, not 0: identical after 4bpp quantization, but 0 is the @@ -197,30 +242,20 @@ export class MenuLayer implements Layer { } if (this.items.length > visibleRowCount) { - this.drawScrollbar(image, x + width - 7, bodyY, visibleRowCount); + drawListScrollbar( + image, + x + width - 7, + bodyY, + visibleRowCount * MENU_ROW_HEIGHT - 4, + this.scrollRow, + visibleRowCount, + this.items.length, + ); } return image; } - private clampScrollToSelection(visibleRowCount: number): void { - if (this.selectedIndex < this.scrollRow) { - this.scrollRow = this.selectedIndex; - } else if (this.selectedIndex >= this.scrollRow + visibleRowCount) { - this.scrollRow = this.selectedIndex - visibleRowCount + 1; - } - this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, this.items.length - visibleRowCount)); - } - - private drawScrollbar(image: GrayImage, trackX: number, trackY: number, visibleRowCount: number): void { - const trackHeight = visibleRowCount * MENU_ROW_HEIGHT - 4; - image.fillRect(trackX, trackY, 3, trackHeight, 30); - const thumbHeight = Math.max(8, (trackHeight * visibleRowCount / this.items.length) | 0); - const maxScrollRow = this.items.length - visibleRowCount; - const thumbY = trackY + (((trackHeight - thumbHeight) * this.scrollRow / maxScrollRow) | 0); - image.fillRect(trackX, thumbY, 3, thumbHeight, 120); - } - async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise { if (!this.items.length) { if (event.type === "double-click") { diff --git a/app/ui/shell/chrome-layer.ts b/app/ui/shell/chrome-layer.ts index 73ae09a4..fc313388 100644 --- a/app/ui/shell/chrome-layer.ts +++ b/app/ui/shell/chrome-layer.ts @@ -4,10 +4,10 @@ import { BATTERY_ICON_WIDTH, drawBattery } from "../../graphics/battery"; import { readActiveNotificationIcons } from "../../native/notification-icons"; import { readPhoneBatteryState } from "../../native/phone-battery"; import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../util/render-freshness"; -import { clamp } from "../../util/numeric-util"; import { renderIcon, type IconName } from "../../graphics/icons"; import { batteryDisplayModeSetting, timeFormatSetting } from "../dashboard-settings"; import { Layer } from "../layers"; +import { scrollToKeepSelectionVisible } from "../menu"; import { SHELL_OPAQUE_BLACK, SIDEBAR_WIDTH, TOP_BAR_HEIGHT } from "./geometry"; const ICON_SIZE = 32; @@ -112,12 +112,7 @@ export class ShellChromeLayer implements Layer { const listHeight = G2_LENS_HEIGHT - listTop - 10; const visibleCount = Math.max(1, ((listHeight + ICON_SPACING) / itemStride) | 0); const count = state.windows.length; - if (state.selectedIndex < this.scrollRow) { - this.scrollRow = state.selectedIndex; - } else if (state.selectedIndex >= this.scrollRow + visibleCount) { - this.scrollRow = state.selectedIndex - visibleCount + 1; - } - this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, count - visibleCount)); + this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, state.selectedIndex, visibleCount, count); const lastVisible = Math.min(count, this.scrollRow + visibleCount); // The selection is a "diversion" of the sidebar/main separator line: the diff --git a/app/ui/shell/launcher-app.ts b/app/ui/shell/launcher-app.ts index 362de996..618265b9 100644 --- a/app/ui/shell/launcher-app.ts +++ b/app/ui/shell/launcher-app.ts @@ -8,7 +8,7 @@ import { GESTURE_SCROLL, } from "../gestures"; import { DashboardInputEvent, Layer, LayerActions, LayerContext } from "../layers"; -import { drawSelectionHighlight } from "../menu"; +import { drawSelectionHighlight, scrollToKeepSelectionVisible } from "../menu"; import { createInProcessWindow } from "./in-process-window"; import { shell, type ShellWindow } from "./shell"; @@ -89,12 +89,7 @@ class LauncherGridLayer implements Layer { const colW = width / COLS; // Scroll to keep the selected row among the fully-visible rows. - if (this.selectedRow < this.scrollRow) { - this.scrollRow = this.selectedRow; - } else if (this.selectedRow > this.scrollRow + FULL_ROWS - 1) { - this.scrollRow = this.selectedRow - (FULL_ROWS - 1); - } - this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, rows - FULL_ROWS)); + this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, FULL_ROWS, rows); const rowY = (row: number) => GRID_TOP + (row - this.scrollRow) * rowH; diff --git a/app/workers/terminal-app.worker.ts b/app/workers/terminal-app.worker.ts index 53acba0e..ba8bbafb 100644 --- a/app/workers/terminal-app.worker.ts +++ b/app/workers/terminal-app.worker.ts @@ -31,7 +31,12 @@ import { } from "../ui/dashboard-settings"; import { TerminalEmulator } from "../ui/apps/terminal-emulator"; import type { DashboardInputEvent } from "../ui/layers"; -import type { MenuItem } from "../ui/menu"; +import { + drawListScrollbar, + drawSelectionHighlight, + scrollToKeepSelectionVisible, + type MenuItem, +} from "../ui/menu"; import { defaultWindowMenuItems, WindowMenu } from "../ui/window-menu"; import type { WorkerAppMessage, WorkerAppReply } from "../ui/shell/worker-window"; @@ -67,6 +72,7 @@ type BaseWindow = { type HubWindow = BaseWindow & { kind: "hub"; selectedIndex: number; + scrollRow: number; }; type ViewWindow = BaseWindow & { @@ -201,6 +207,7 @@ function openWindow(windowId: string, surfaceId: string, viewport: { width: numb lastSubmittedFingerprint: "", menu: null, selectedIndex: 0, + scrollRow: 0, }); if (!controlClient) { startControlClient(); @@ -593,20 +600,30 @@ function paintHub(window: HubWindow): GrayImage { const items = hubItems(); window.selectedIndex = Math.max(0, Math.min(window.selectedIndex, items.length - 1)); - for (let index = 0; index < items.length; index++) { - const y = listTop + index * HUB_ROW_HEIGHT; - if (y + HUB_ROW_HEIGHT > viewportHeight - 30) break; + const visibleRowCount = Math.max(1, ((viewportHeight - 30 - listTop) / HUB_ROW_HEIGHT) | 0); + window.scrollRow = scrollToKeepSelectionVisible(window.scrollRow, window.selectedIndex, visibleRowCount, items.length); + const lastVisibleRow = Math.min(items.length, window.scrollRow + visibleRowCount); + for (let index = window.scrollRow; index < lastVisibleRow; index++) { + const y = listTop + (index - window.scrollRow) * HUB_ROW_HEIGHT; const selected = index === window.selectedIndex; if (selected) { // Match the shell convention: fill only when this window has focus, so // an outline-only selection signals the sidebar owns input. - if (window.focused) { - image.fillRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 15); - } - image.drawRoundedRect(20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, 45); + drawSelectionHighlight(image, 20, y - 2, viewportWidth - 40, HUB_ROW_HEIGHT - 1, window.focused, 8); } image.drawText(terminalFont, 32, y + 2, items[index]!.label, selected ? 255 : 200); } + if (items.length > visibleRowCount) { + drawListScrollbar( + image, + viewportWidth - 10, + listTop, + visibleRowCount * HUB_ROW_HEIGHT - 4, + window.scrollRow, + visibleRowCount, + items.length, + ); + } image.drawText(terminalFont, 24, viewportHeight - 24, `${GESTURE_DOUBLE_CLICK} back`, 110); return image;