Add animations to launcher and Files app

This commit is contained in:
James Babcock committed 2026-09-25 13:25:51 -07:00
1 parent 7ad9fe10ad
commit b0a98f5942
24 files changed
+1608 -738

No files matched your search

+54 -316
View File
@@ -2,10 +2,10 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { truncateText, truncateLeft } from "../../graphics/textwrap";
import { GrayImage, type UiFont } from "../../graphics/image";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import { drawListScrollbar, drawSelectionHighlight, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
import { type MenuItem } from "../../ui/menu";
import { Menu, type MenuDrawArgs } from "../../ui/menu-core";
import { centeredTextY, iconGridMinRowHeight, tightRowHeight } from "../../ui/metrics";
import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { centeredTextY, tightRowHeight } from "../../ui/metrics";
import { ConfigSettingEnum } from "../../ui/dashboard-settings";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import {
@@ -16,9 +16,8 @@ import {
statPath,
type DirectoryEntry,
} from "../../native/file-access";
import { directionalFallback, isWatchInput, type InputEvent } from "../../ui/gestures";
import { directionalFallback, type InputEvent } from "../../ui/gestures";
import { Layer, type LayerContext } from "../../ui/layers";
import { shell } from "../../ui/shell/shell";
const LIST_X = 20;
/** List view: the selection box extends this far left and right of the row text. */
@@ -29,10 +28,6 @@ function headerHeight(font: UiFont): number {
return 10 + font.lineHeight;
}
const GRID_COLS = 5;
const ICON_SIZE = 44;
const LABEL_GAP = 2;
export type FilesViewMode = "icons" | "list";
export const filesViewModeSetting = new ConfigSettingEnum<FilesViewMode>({
@@ -105,23 +100,14 @@ type EntryItem = {
type BrowserItem = { kind: "grant"; label: string } | { kind: "info"; label: string } | EntryItem;
type IconRow =
| { kind: "special"; item: BrowserItem; flatIndex: number }
| { kind: "grid"; items: EntryItem[]; firstIndex: number };
type IconMode = "row" | "item";
/**
* Filesystem browser with two views. The top "Places" level lists bookmarks
* and the storage roots; descending browses real directories. Icons view is a
* 5-column grid with launcher-style two-level selection (scroll picks a row,
* click drops into the row, scroll picks the item); list view is a flat list.
* and the storage roots; descending browses real directories. Icons view is
* the launcher's IconGrid (two-level ring selection, four-way watch
* selection; notice rows span the full width); list view is a flat list.
* Click descends into a directory or picks a supported file; double-click
* backs out one level (item selection, then parent directory, then Places,
* then leaving the browser). That is the ring's scheme; in icons view the
* watch (see handleIconsWatchInput) skips row mode and moves one cell at a
* time in four directions, exactly as in the launcher. Sized to its hosting
* stack.
* then leaving the browser). Sized to its hosting stack.
*/
export class FileBrowserLayer implements Layer {
// Watch swipes are spatial in icons view: up/down move between rows,
@@ -136,7 +122,7 @@ export class FileBrowserLayer implements Layer {
private entries: DirectoryEntry[] | null = null;
private listingFailed = false;
/** List view. Its selected index is the flat-row index both views translate through (setViewMode). */
/** List view. Both views index the same flatRows(), which is how setViewMode carries the selection across. */
private readonly listMenu = new Menu<BrowserItem>({
wrap: false,
rowGap: 1,
@@ -144,10 +130,18 @@ export class FileBrowserLayer implements Layer {
getHeight: () => tightRowHeight(getDefaultSmallFont()),
draw: (args) => this.drawListRow(args),
});
private selectedRow = 0;
private selectedCol = 0;
private iconMode: IconMode = "row";
private scrollRow = 0;
/** Icons view. */
private readonly grid = new IconGrid<BrowserItem>({
items: () => this.flatRows(),
isWide: (item) => item.kind !== "entry",
describe: (item, selected) =>
item.kind === "entry"
? { label: item.label, icon: gridIcon(item), labelValue: item.supported ? 210 : 100 }
: { label: item.label, labelValue: itemValue(item, selected) },
onActivate: (item, _index, ctx) => this.activateItem(item, ctx),
onBack: () => this.navigateUp(),
scrollbar: true,
});
constructor(private readonly options: FileBrowserOptions) {}
@@ -155,7 +149,6 @@ export class FileBrowserLayer implements Layer {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const rows = this.flatRows();
image.drawText(font, 20, 8, "Files", 220);
const pathLabel = this.location === null ? "Places" : this.location;
@@ -163,17 +156,18 @@ export class FileBrowserLayer implements Layer {
image.drawText(font, pathX, 8, truncateLeft(font, pathLabel, width - pathX - 20), 130);
if (filesViewModeSetting.get() === "list") {
this.paintList(image, font, rows, ctx);
this.paintList(image, font, ctx);
} else {
this.paintIcons(image, font, rows, ctx);
const headerH = headerHeight(font);
this.grid.paint(image, { x: 0, y: headerH, width, height: height - headerH - 6 }, ctx.stack.isFocused());
}
return image;
}
private paintList(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
private paintList(image: GrayImage, font: UiFont, ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
const headerH = headerHeight(font);
this.listMenu.setItems(rows);
this.listMenu.setItems(this.flatRows());
// Row boxes start one pixel above the header's bottom edge.
this.listMenu.paint(
image,
@@ -188,96 +182,19 @@ export class FileBrowserLayer implements Layer {
image.drawText(font, x + LIST_HIGHLIGHT_PAD, centeredTextY(font, y, height), label, itemValue(item, selected));
}
private paintIcons(image: GrayImage, font: UiFont, rows: BrowserItem[], ctx: LayerContext): void {
const { width, height } = ctx.stack.getBaseSize();
const focused = ctx.stack.isFocused();
const iconRows = buildIconRows(rows);
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const gridTop = headerHeight(font);
const gridBottom = height - 6;
const visibleRows = Math.max(1, Math.floor((gridBottom - gridTop) / iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP)));
const rowH = Math.floor((gridBottom - gridTop) / visibleRows);
const colW = width / GRID_COLS;
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, visibleRows, iconRows.length);
const lastVisible = Math.min(iconRows.length, this.scrollRow + visibleRows);
for (let rowIndex = this.scrollRow; rowIndex < lastVisible; rowIndex++) {
const row = iconRows[rowIndex]!;
const y = gridTop + (rowIndex - this.scrollRow) * rowH;
const selected = rowIndex === this.selectedRow;
if (row.kind === "special") {
const textY = y + (((rowH - font.lineHeight) / 2) | 0);
if (selected) {
drawSelectionHighlight(image, LIST_X - 6, textY - 2, width - 2 * LIST_X + 12, font.lineHeight + 4, focused, 4);
}
image.drawText(font, LIST_X, textY, truncateText(font, row.item.label, width - 2 * LIST_X), itemValue(row.item, selected));
continue;
}
if (selected) {
// Same preview rule as the launcher: while defocused with the watch
// as the last-used source, show the cell a click would land on (watch
// focus enters item mode directly; see onFocus) instead of a row band
// the watch scheme never shows.
const cellHighlight = this.iconMode === "item" || (!focused && shell.lastInputWasWatch());
if (cellHighlight) {
this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1);
drawSelectionHighlight(image, this.selectedCol * colW + 4, y + 1, colW - 8, rowH - 2, focused, 6);
} else {
drawSelectionHighlight(image, 4, y + 1, width - 8, rowH - 2, focused, 6);
}
}
for (let col = 0; col < row.items.length; col++) {
const item = row.items[col]!;
const centerX = col * colW + colW / 2;
const blockTop = y + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
const icon = renderIcon(item.icon, ICON_SIZE);
if (icon) {
image.bitBlt(item.supported ? icon : dimmedIcon(item.icon, icon), Math.round(centerX - icon.width / 2), Math.round(blockTop), {
transparentZero: true,
});
}
const label = truncateText(font, item.label, colW - 8);
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, item.supported ? 210 : 100);
}
}
if (iconRows.length > visibleRows) {
drawListScrollbar(image, width - 5, gridTop, visibleRows * rowH - 4, this.scrollRow, visibleRows, iconRows.length);
}
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (filesViewModeSetting.get() === "list") {
// The flat list has no spatial meaning for swipes; give them the
// standard scroll / select / back meanings (the layer opted into
// directional delivery for the icons view, so the stack won't).
await this.handleListInput(directionalFallback(event), ctx);
} else if (isWatchInput(event)) {
await this.handleIconsWatchInput(event, ctx);
} else {
await this.handleIconsInput(event, ctx);
await this.grid.handleInput(event, ctx);
}
}
/**
* Focus arriving from the watch goes straight to item selection, as in the
* launcher: the watch has left/right swipes, so it never needs row mode.
* Any other source keeps the two-level scheme and enters in row mode.
*/
onFocus(lastInput: InputEvent | null): void {
if (filesViewModeSetting.get() !== "icons") return;
if (lastInput && isWatchInput(lastInput)) {
this.iconMode = "item";
const iconRows = buildIconRows(this.flatRows());
this.clampColToRow(iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))]);
} else {
this.iconMode = "row";
}
if (filesViewModeSetting.get() === "icons") this.grid.onFocus(lastInput);
}
private async handleListInput(event: InputEvent, ctx: LayerContext): Promise<void> {
@@ -300,147 +217,6 @@ export class FileBrowserLayer implements Layer {
}
}
private async handleIconsInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const rows = this.flatRows();
const iconRows = buildIconRows(rows);
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const row = iconRows[this.selectedRow];
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.iconMode === "item") {
// Item selection traverses linearly: past a row's edge it continues
// onto the adjacent row (a special row counts as a single item).
if (row?.kind === "grid") {
const next = clamp(this.selectedCol, 0, row.items.length - 1) + delta;
if (next >= 0 && next < row.items.length) {
this.selectedCol = next;
return;
}
}
const adjacentIndex = this.selectedRow + delta;
if (adjacentIndex < 0 || adjacentIndex >= iconRows.length) return;
this.selectedRow = adjacentIndex;
const adjacent = iconRows[adjacentIndex]!;
if (adjacent.kind === "grid") {
this.selectedCol = delta > 0 ? 0 : adjacent.items.length - 1;
}
} else {
this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1));
}
return;
}
case "click": {
if (!row) return;
if (row.kind === "special") {
await this.activateItem(row.item, ctx);
return;
}
if (this.iconMode === "row") {
this.iconMode = "item";
// Default to the middle column (clamped to the row's item count).
this.selectedCol = Math.min(Math.floor(GRID_COLS / 2), row.items.length - 1);
} else {
const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)];
if (item) await this.activateItem(item, ctx);
}
return;
}
case "double-click":
if (this.iconMode === "item") {
this.iconMode = "row";
} else {
this.navigateUp();
}
return;
default:
return;
}
}
/**
* The watch's scheme for the icons view — the launcher's: there is no row
* mode, the selection is always one cell (a special row counts as its
* single full-width item). Up/down (and the crown) move between rows
* keeping the column, right/left move within the row, and left from the
* leftmost column keeps going out — parent directory, Places, then the
* sidebar. Row mode is restored on the way out so a ring user finds the
* grid as they left it.
*/
private async handleIconsWatchInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const iconRows = buildIconRows(this.flatRows());
this.selectedRow = clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1));
const row = iconRows[this.selectedRow];
if (this.iconMode === "row") {
this.iconMode = "item";
this.clampColToRow(row);
}
switch (event.type) {
case "swipe-up":
case "swipe-down":
case "scroll-up":
case "scroll-down": {
const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1;
this.selectedRow = clamp(this.selectedRow + delta, 0, Math.max(0, iconRows.length - 1));
// Keep the column; a shorter row (or a special one) clamps it.
this.clampColToRow(iconRows[this.selectedRow]);
return;
}
case "swipe-right":
if (row?.kind === "grid") {
this.selectedCol = clamp(this.selectedCol + 1, 0, row.items.length - 1);
}
return;
case "swipe-left":
if (row?.kind === "grid" && this.selectedCol > 0) {
this.selectedCol--;
} else {
this.watchNavigateUp();
}
return;
case "click": {
if (!row) return;
if (row.kind === "special") {
await this.activateItem(row.item, ctx);
} else {
const item = row.items[clamp(this.selectedCol, 0, row.items.length - 1)];
if (item) await this.activateItem(item, ctx);
}
// Descending into a directory resets the selection for the ring (row
// mode at the top); the watch scheme stays in single-item selection.
this.iconMode = "item";
return;
}
case "double-click":
this.watchNavigateUp();
return;
default:
return;
}
}
private clampColToRow(row: IconRow | undefined): void {
if (row?.kind === "grid") {
this.selectedCol = clamp(this.selectedCol, 0, row.items.length - 1);
}
}
/**
* navigateUp for the watch scheme: inside the browser the selection stays
* in item mode (resetSelection/selectPath put it back in row mode for the
* ring); leaving from Places restores row mode on the way out.
*/
private watchNavigateUp(): void {
if (this.location === null) {
this.iconMode = "row";
this.options.onLeave();
return;
}
this.navigateUp();
this.iconMode = "item";
}
/**
* App-specific entries for the window long-press menu: the view switch,
* plus bookmark/unbookmark for the selected entry (in icons view only once
@@ -480,34 +256,23 @@ export class FileBrowserLayer implements Layer {
this.listMenu.setItems(rows);
return this.listMenu.selectedItem;
}
const iconRows = buildIconRows(rows);
const row = iconRows[clamp(this.selectedRow, 0, iconRows.length - 1)];
if (!row) return null;
if (row.kind === "special") return row.item;
if (this.iconMode !== "item") return null;
return row.items[clamp(this.selectedCol, 0, row.items.length - 1)] ?? null;
return this.grid.selectedItem;
}
private setViewMode(mode: FilesViewMode): void {
if (filesViewModeSetting.get() === mode) return;
const rows = this.flatRows();
const iconRows = buildIconRows(rows);
this.listMenu.setItems(rows);
this.listMenu.setItems(this.flatRows());
if (mode === "list") {
// Carry the icon-grid selection into the flat list (clamped column when
// only a row was selected).
const row = iconRows[clamp(this.selectedRow, 0, Math.max(0, iconRows.length - 1))];
if (row) {
this.listMenu.select(
row.kind === "special" ? row.flatIndex : row.firstIndex + clamp(this.selectedCol, 0, row.items.length - 1),
);
}
// Carry the icon-grid selection into the flat list (the remembered
// column when only a row was selected).
const index = this.grid.cursorIndex;
if (index !== null) this.listMenu.select(index);
} else {
this.setGridSelectionFromFlatIndex(rows, this.listMenu.selectedIndex ?? 0);
this.iconMode = "row";
const index = this.listMenu.selectedIndex ?? 0;
this.grid.resetSelection();
this.grid.selectIndex(index);
}
filesViewModeSetting.set(mode);
this.scrollRow = 0;
this.listMenu.scrollTop = 0;
}
@@ -534,11 +299,12 @@ export class FileBrowserLayer implements Layer {
this.resetSelection();
}
private navigateUp(): void {
/** Back out one level; true when that left the browser (from Places). */
private navigateUp(): boolean {
const from = this.location;
if (from === null) {
this.options.onLeave();
return;
return true;
}
if (from === this.navRoot || from === "/") {
this.location = null;
@@ -546,20 +312,18 @@ export class FileBrowserLayer implements Layer {
this.resetSelection();
this.selectPath(this.navRoot ?? from);
this.navRoot = null;
return;
return false;
}
const parentRaw = from.slice(0, from.lastIndexOf("/"));
this.navigateTo(parentRaw.length ? parentRaw : "/");
this.selectPath(from);
return false;
}
private resetSelection(): void {
this.listMenu.select(0);
this.listMenu.scrollTop = 0;
this.selectedRow = 0;
this.selectedCol = 0;
this.iconMode = "row";
this.scrollRow = 0;
this.grid.resetSelection();
}
/** Select the item with the given path (e.g. the directory just left). */
@@ -570,27 +334,17 @@ export class FileBrowserLayer implements Layer {
// Items first, then the selection, so the viewport scrolls minimally from the top.
this.listMenu.setItems(rows);
this.listMenu.select(index);
this.setGridSelectionFromFlatIndex(rows, index);
}
private setGridSelectionFromFlatIndex(rows: BrowserItem[], index: number): void {
const specials = leadingSpecialCount(rows);
if (index < specials) {
this.selectedRow = index;
this.selectedCol = 0;
return;
}
const ordinal = index - specials;
this.selectedRow = specials + Math.floor(ordinal / GRID_COLS);
this.selectedCol = ordinal % GRID_COLS;
this.grid.selectIndex(index);
}
private flatRows(): BrowserItem[] {
const rows: BrowserItem[] = [];
if (!hasAllFilesAccess()) {
rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" });
}
if (this.location === null) {
// Only on Places: without All Files access some folders (e.g. Download)
// may still be readable, and the prompt shouldn't crowd their listings.
if (!hasAllFilesAccess()) {
rows.push({ kind: "grant", label: "Grant file access (opens phone Settings)" });
}
for (const path of getBookmarkedPaths()) {
rows.push(this.bookmarkItem(path));
}
@@ -663,27 +417,6 @@ export class FileBrowserLayer implements Layer {
}
}
function buildIconRows(flat: BrowserItem[]): IconRow[] {
const rows: IconRow[] = [];
let index = 0;
// Special rows (grant prompt, empty/unreadable info) lead the list; each
// gets a full-width row of its own. Entries follow, chunked into grid rows.
while (index < flat.length && flat[index]!.kind !== "entry") {
rows.push({ kind: "special", item: flat[index]!, flatIndex: index });
index++;
}
for (; index < flat.length; index += GRID_COLS) {
rows.push({ kind: "grid", items: flat.slice(index, index + GRID_COLS) as EntryItem[], firstIndex: index });
}
return rows;
}
function leadingSpecialCount(rows: BrowserItem[]): number {
let count = 0;
while (count < rows.length && rows[count]!.kind !== "entry") count++;
return count;
}
function itemValue(item: BrowserItem, selected: boolean): number {
if (item.kind === "info") return 120;
if (item.kind === "grant") return selected ? 255 : 210;
@@ -691,6 +424,11 @@ function itemValue(item: BrowserItem, selected: boolean): number {
return selected ? 255 : 200;
}
function gridIcon(item: EntryItem): GrayImage | null {
const icon = renderIcon(item.icon, ICON_GRID_ICON_SIZE);
return icon && !item.supported ? dimmedIcon(item.icon, icon) : icon;
}
// Unsupported files draw their grid icon at half brightness; rendered once
// per icon name and cached (all grid icons share one size).
const dimmedIconCache = new Map<IconName, GrayImage>();
+53 -275
View File
@@ -4,11 +4,10 @@ import { truncateText } from "../../graphics/textwrap";
import { GrayImage } from "../../graphics/image";
import { type Plane } from "../../graphics/plane";
import { renderIcon, type IconName } from "../../graphics/icons";
import { clamp } from "../../util/numeric-util";
import { InputEvent, isWatchInput } from "../../ui/gestures";
import { InputEvent } from "../../ui/gestures";
import { IconGrid, ICON_GRID_ICON_SIZE } from "../../ui/icon-grid";
import { Layer, LayerActions, LayerContext } from "../../ui/layers";
import { drawSelectionHighlight, MenuLayer, scrollToKeepSelectionVisible, type MenuItem } from "../../ui/menu";
import { iconGridMinRowHeight } from "../../ui/metrics";
import { MenuLayer, type MenuItem } from "../../ui/menu";
import { WINDOW_MENU_LAYOUT } from "../../ui/window-menu";
import { onAnySettingChanged } from "../../ui/dashboard-settings";
import { createInProcessWindow } from "../../ui/shell/in-process-window";
@@ -45,12 +44,7 @@ export type LauncherOptions = {
export const LAUNCHER_WINDOW_ID = "launcher";
export const LAUNCHER_SURFACE_ID = "window:launcher";
const COLS = 5;
const GRID_TOP = 6;
const ICON_SIZE = 44;
const LABEL_GAP = 2;
type LauncherMode = "row" | "item";
/** One cell of the grid: an app, or a folder holding some of the apps. */
type LauncherGridEntry =
@@ -58,15 +52,11 @@ type LauncherGridEntry =
| { kind: "folder"; label: string; name: string };
/**
* The launcher grid: app and folder icons with labels, arranged in a grid.
* Navigation has two levels so the max number of swipes to any app is halved:
* entering from the sidebar starts in "row" mode (scroll picks a row); a tap
* drops into "item" mode on that row, defaulting to the middle column (scroll
* picks the item); a tap launches an app or opens a folder (the same grid,
* restricted to that folder's apps). Double-click backs out one level (item →
* row, folder → top grid), and from the top level yields to the sidebar.
* That is the ring's scheme; the watch (see handleWatchInput) skips row mode
* and moves one cell at a time in four directions.
* The launcher grid: app and folder icons with labels, in an IconGrid (which
* owns layout, selection and the ring/watch navigation schemes). Opening a
* folder shows the same grid restricted to that folder's apps; backing out
* of a folder returns to the top grid, and from the top grid yields to the
* sidebar.
*
* The folder grouping lives in the settings store (see launcher-folders.ts)
* and is re-read every paint, so assistant folder tools take effect without
@@ -78,28 +68,24 @@ class LauncherGridLayer implements Layer {
// open folder if there is one, else yields to the sidebar — "left" points
// toward the sidebar all the way out.
readonly acceptsDirectional = true;
private mode: LauncherMode = "row";
private selectedRow = 0;
private selectedCol = 1;
private scrollRow = 0;
/** Folder whose contents the grid is showing, or null for the top grid. */
private currentFolder: string | null = null;
private readonly grid = new IconGrid<LauncherGridEntry>({
items: () => this.entries(),
describe: (entry) => ({
label: entry.label,
icon: entry.kind === "app"
? entry.renderIcon?.(ICON_GRID_ICON_SIZE) ?? renderIcon(entry.icon, ICON_GRID_ICON_SIZE)
: renderIcon("folder-filled", ICON_GRID_ICON_SIZE),
}),
onActivate: (entry) => this.openEntry(entry),
onBack: () => this.back(),
});
constructor(private readonly options: LauncherOptions) {}
/**
* Focus arriving from the watch goes straight to item selection: the watch
* has left/right swipes, so it never needs row mode (see handleWatchInput),
* and starting there would paint a row band its scheme can't produce. Any
* other source keeps the two-level scheme and enters in row mode.
*/
onFocus(lastInput: InputEvent | null): void {
if (lastInput && isWatchInput(lastInput)) {
this.mode = "item";
this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(this.entries().length, this.selectedRow) - 1);
} else {
this.mode = "row";
}
this.grid.onFocus(lastInput);
}
/**
@@ -150,22 +136,38 @@ class LauncherGridLayer implements Layer {
return entries.sort((a, b) => a.label.localeCompare(b.label));
}
private rowCount(entryCount: number): number {
return Math.max(1, Math.ceil(entryCount / COLS));
}
private itemsInRow(entryCount: number, row: number): number {
return Math.max(0, Math.min(COLS, entryCount - row * COLS));
}
/** Leave the current folder, putting the selection back on its cell. */
private exitFolder(): void {
const folder = this.currentFolder;
this.currentFolder = null;
this.mode = "row";
this.grid.resetSelection();
const index = this.entries().findIndex((entry) => entry.kind === "folder" && entry.name === folder);
this.selectedRow = index >= 0 ? Math.floor(index / COLS) : 0;
this.selectedCol = index >= 0 ? index % COLS : 0;
if (index >= 0) this.grid.selectIndex(index);
}
/** Enter a folder or launch an app; true when that left the grid (a launch). */
private async openEntry(entry: LauncherGridEntry): Promise<boolean> {
if (entry.kind === "folder") {
this.currentFolder = entry.name;
this.grid.resetSelection();
return false;
}
// Return to the top grid before launching, so the launcher never
// shows (even briefly) stale folder contents when re-entered.
if (this.currentFolder !== null) this.exitFolder();
this.grid.enterRowMode();
await this.options.launchApp(entry.appId);
return true;
}
/** Leave the open folder, else yield to the sidebar; true when that left the grid. */
private back(): boolean {
if (this.currentFolder !== null) {
this.exitFolder();
return false;
}
shell.yieldFocusToSidebar();
return true;
}
/**
@@ -174,8 +176,7 @@ class LauncherGridLayer implements Layer {
* so anywhere else the menu falls back to just the defaults.
*/
menuItems(): MenuItem[] {
if (this.mode !== "item") return [];
const entry = this.entries()[this.selectedRow * COLS + this.selectedCol];
const entry = this.grid.selectedItem;
if (!entry || entry.kind !== "app") return [];
return [
{
@@ -202,7 +203,7 @@ class LauncherGridLayer implements Layer {
confirmCtx.stack.pop();
setAppFolder(entry.appId, null);
await this.options.uninstallApp(entry.appId);
this.mode = "row";
this.grid.enterRowMode();
},
},
{ label: "Cancel", onSelect: (confirmCtx) => confirmCtx.stack.pop() },
@@ -257,246 +258,23 @@ class LauncherGridLayer implements Layer {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const focused = ctx.stack.isFocused();
const entries = this.entries();
const rows = this.rowCount(entries.length);
this.selectedRow = clamp(this.selectedRow, 0, rows - 1);
this.selectedCol = clamp(
this.selectedCol,
0,
Math.max(0, this.itemsInRow(entries.length, this.selectedRow) - 1),
);
// The folder-name header band scales with the font.
const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : GRID_TOP;
if (this.currentFolder !== null) {
image.drawText(font, 8, GRID_TOP - 2, truncateText(font, this.currentFolder, width - 16), 160);
}
const gridBottom = height - 4;
// Rows are exactly as tall as their content (icon + label + breathing
// room), so row height tracks the font instead of stretching to fill the
// viewport. Leftover space below the last full row shows the top of the
// next row when there are more apps to scroll to.
const rowH = iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP);
const fullRows = Math.max(1, Math.floor((gridBottom - gridTop) / rowH));
const colW = width / COLS;
// Scroll to keep the selected row among the fully-visible rows.
this.scrollRow = scrollToKeepSelectionVisible(this.scrollRow, this.selectedRow, fullRows, rows);
const rowY = (row: number) => gridTop + (row - this.scrollRow) * rowH;
// Selection highlight: a row band, or a single cell in item mode. While
// defocused with the watch as the last-used source, the cell outline is
// shown even in row mode: watch focus enters item mode directly (see
// onFocus), so the outline previews the cell a click would land on
// rather than a row band the watch scheme never shows.
const cellHighlight = this.mode === "item" || (!focused && shell.lastInputWasWatch());
const selY = rowY(this.selectedRow);
if (cellHighlight) {
drawSelectionHighlight(image, this.selectedCol * colW + 6, selY + 2, colW - 12, rowH - 4, focused, 6);
} else {
drawSelectionHighlight(image, 4, selY + 2, width - 8, rowH - 4, focused, 6);
}
for (let index = 0; index < entries.length; index++) {
const entry = entries[index]!;
const row = Math.floor(index / COLS);
if (row < this.scrollRow) continue;
const blockTop = rowY(row) + Math.max(2, (rowH - ICON_SIZE - font.lineHeight - LABEL_GAP) / 2);
if (blockTop >= gridBottom) break; // fully below the grid
const centerX = (index % COLS) * colW + colW / 2;
const icon = entry.kind === "app"
? entry.renderIcon?.(ICON_SIZE) ?? renderIcon(entry.icon, ICON_SIZE)
: renderIcon("folder-filled", ICON_SIZE);
if (icon) {
// Clip the icon at the grid bottom so a peeking row shows only its top.
// A fully visible icon goes through the deferred-image path (texture
// cacheable); the clipped peeking row falls back to a raster blit,
// since cached draws are whole-image only.
// Icons narrower/shorter than ICON_SIZE (a non-square logo scaled to
// fit) are centered in the square the row reserves for them.
const iconX = Math.round(centerX - icon.width / 2);
const iconY = Math.round(blockTop + Math.max(0, (ICON_SIZE - icon.height) / 2));
const clipHeight = Math.min(icon.height, Math.floor(gridBottom - iconY));
if (clipHeight >= icon.height) {
image.drawImage(icon, iconX, iconY);
} else if (clipHeight > 0) {
image.bitBlt(icon, iconX, iconY, { height: clipHeight, transparentZero: true });
}
}
const labelY = Math.round(blockTop + ICON_SIZE + LABEL_GAP);
if (labelY + font.lineHeight <= gridBottom) {
const label = truncateText(font, entry.label, colW - 8);
image.drawText(font, Math.round(centerX - font.measureText(label) / 2), labelY, label, 210);
}
}
this.grid.paint(image, { x: 0, y: gridTop, width, height: gridBottom - gridTop }, ctx.stack.isFocused());
return image;
}
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
if (isWatchInput(event)) {
await this.handleWatchInput(event, ctx);
return;
}
const entries = this.entries();
const rows = this.rowCount(entries.length);
switch (event.type) {
case "scroll-up":
case "scroll-down": {
const delta = event.type === "scroll-down" ? 1 : -1;
if (this.mode === "row") {
this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1);
} else {
// Item selection traverses the grid linearly: past a row's edge it
// continues onto the adjacent row (stopping at the grid's ends).
const itemCount = this.itemsInRow(entries.length, this.selectedRow);
const next = clamp(this.selectedCol, 0, itemCount - 1) + delta;
if (next >= 0 && next < itemCount) {
this.selectedCol = next;
} else if (next < 0 && this.selectedRow > 0) {
this.selectedRow--;
this.selectedCol = this.itemsInRow(entries.length, this.selectedRow) - 1;
} else if (next >= itemCount && this.selectedRow < rows - 1) {
this.selectedRow++;
this.selectedCol = 0;
}
}
return;
}
case "click": {
if (this.mode === "row") {
this.mode = "item";
// Default to the middle column (clamped to the row's item count).
this.selectedCol = Math.min(
Math.floor(COLS / 2),
this.itemsInRow(entries.length, this.selectedRow) - 1,
);
} else {
await this.openSelected(entries);
}
return;
}
case "double-click":
if (this.mode === "item") {
this.mode = "row";
} else if (this.currentFolder !== null) {
this.exitFolder();
} else {
shell.yieldFocusToSidebar();
}
return;
default:
return;
}
await this.grid.handleInput(event, ctx);
}
/**
* The watch's scheme: there is no row mode to enter first. The selection is
* always one cell; up/down/left/right (and the crown) move it spatially,
* select opens it, back leaves the folder or the grid, and stepping left
* past the first column leaves for the sidebar, which is where "left" goes.
* Row mode is restored on the way out so a ring user finds the grid as
* they left it.
*/
private async handleWatchInput(event: InputEvent, ctx: LayerContext): Promise<void> {
const entries = this.entries();
const rows = this.rowCount(entries.length);
if (this.mode === "row") {
this.mode = "item";
this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(entries.length, this.selectedRow) - 1);
}
const leave = () => {
this.mode = "row";
shell.yieldFocusToSidebar();
};
switch (event.type) {
case "swipe-up":
case "swipe-down":
case "scroll-up":
case "scroll-down": {
const delta = event.type === "swipe-down" || event.type === "scroll-down" ? 1 : -1;
this.selectedRow = clamp(this.selectedRow + delta, 0, rows - 1);
// Keep the column; a shorter last row clamps it.
this.selectedCol = clamp(this.selectedCol, 0, this.itemsInRow(entries.length, this.selectedRow) - 1);
return;
}
case "swipe-right":
this.selectedCol = clamp(this.selectedCol + 1, 0, this.itemsInRow(entries.length, this.selectedRow) - 1);
return;
case "swipe-left":
if (this.selectedCol > 0) {
this.selectedCol--;
} else if (this.currentFolder !== null) {
this.exitFolder();
this.mode = "item";
} else {
leave();
}
return;
case "click":
await this.openSelected(entries, true);
return;
case "double-click":
if (this.currentFolder !== null) {
this.exitFolder();
this.mode = "item";
} else {
leave();
}
return;
default:
return;
}
}
/**
* A touch on the phone's mirror: the cell under it becomes the selection
* and opens (the same as a watch select on it). Uses the geometry paint
* lays the grid out with, so it lands on what the mirror showed.
*/
/** A touch on the phone's mirror opens the cell under it. */
async hitTest(x: number, y: number, ctx: LayerContext): Promise<boolean> {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const entries = this.entries();
const gridTop = this.currentFolder !== null ? GRID_TOP + font.lineHeight + 4 : GRID_TOP;
const gridBottom = height - 4;
if (y < gridTop || y >= gridBottom || x < 0 || x >= width) return false;
const rowH = iconGridMinRowHeight(font, ICON_SIZE, LABEL_GAP);
const colW = width / COLS;
const row = this.scrollRow + Math.floor((y - gridTop) / rowH);
const col = Math.floor(x / colW);
if (row < 0 || row >= this.rowCount(entries.length)) return false;
if (col < 0 || col >= this.itemsInRow(entries.length, row)) return false;
this.mode = "item";
this.selectedRow = row;
this.selectedCol = col;
await this.openSelected(entries);
return true;
}
/**
* Open the selected cell: enter a folder, or launch the app. A folder
* entered by watch input stays in single-item selection (the watch scheme
* has no row mode), starting at the folder's first cell.
*/
private async openSelected(entries: readonly LauncherGridEntry[], watch = false): Promise<void> {
const entry = entries[this.selectedRow * COLS + this.selectedCol];
if (!entry) return;
if (entry.kind === "folder") {
this.currentFolder = entry.name;
this.mode = watch ? "item" : "row";
this.selectedRow = 0;
this.selectedCol = watch ? 0 : this.selectedCol;
this.scrollRow = 0;
} else {
// Return to the top grid before launching, so the launcher never
// shows (even briefly) stale folder contents when re-entered.
if (this.currentFolder !== null) this.exitFolder();
this.mode = "row";
await this.options.launchApp(entry.appId);
}
return await this.grid.hitTest(x, y, ctx);
}
}