Fix scrolling in Terminal

This commit is contained in:
James Babcock committed 2026-07-24 01:52:12 -07:00
1 parent 8ffccad7f0
commit d42e11e2a5
7 files changed
+90 -63

No files matched your search

+2 -7
View File
@@ -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++) {
+2 -7
View File
@@ -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++) {
+2 -7
View File
@@ -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);
+55 -20
View File
@@ -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<void> {
if (!this.items.length) {
if (event.type === "double-click") {
+2 -7
View File
@@ -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
+2 -7
View File
@@ -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;