Animations in EvenHub menus; closer style matching
This commit is contained in:
1 parent
0217d4b1ea
commit
aeeea5ead3
9 files changed
+349
-55
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.MutableContextWrapper
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
@@ -54,9 +55,20 @@ import java.util.ArrayList
|
||||
* timers/onResume asserted on attach. The main Looper keeps ticking under the
|
||||
* foreground service, so apps keep driving their glasses windows in the
|
||||
* background.
|
||||
*
|
||||
* Activity recreation: this host outlives any one activity (it is a process
|
||||
* singleton, and apps keep running while the activity is destroyed and
|
||||
* recreated, e.g. after an app update or a config change). The overlay lives in
|
||||
* one activity's content view, so attach and showOnPhone first move it, with
|
||||
* every WebView in it, into the activity they are given if it isn't already
|
||||
* there; otherwise "show" would raise it in a dead window. WebViews are created
|
||||
* on a MutableContextWrapper whose base is re-pointed at the same time, so the
|
||||
* dialogs Chromium opens from the page (e.g. a <select>'s picker) use the live
|
||||
* activity too.
|
||||
*/
|
||||
class FaceclawEvenHubWebViewHost {
|
||||
private var overlay: FrameLayout? = null
|
||||
private var overlayActivity: Activity? = null
|
||||
private var shown = false
|
||||
|
||||
private val mainHandler = Handler(Looper.getMainLooper())
|
||||
@@ -86,20 +98,38 @@ class FaceclawEvenHubWebViewHost {
|
||||
}
|
||||
}
|
||||
|
||||
/** The overlay, in [activity]'s content view (moved there, with its WebViews, if it was elsewhere). */
|
||||
private fun ensureOverlay(activity: Activity): FrameLayout {
|
||||
overlay?.let { return it }
|
||||
val existing = overlay
|
||||
if (existing != null && overlayActivity === activity && existing.parent != null) return existing
|
||||
val content = activity.findViewById<ViewGroup>(android.R.id.content)
|
||||
val created = FrameLayout(activity)
|
||||
if (existing != null) {
|
||||
(existing.parent as ViewGroup?)?.removeView(existing)
|
||||
for (web in webViews) {
|
||||
existing.removeView(web)
|
||||
rebaseContext(web, activity)
|
||||
created.addView(web, FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
||||
}
|
||||
}
|
||||
overlay = created
|
||||
// index 0 = behind the NativeScript content view.
|
||||
overlayActivity = activity
|
||||
// index 0 = behind the NativeScript content view (raised again below if shown).
|
||||
content.addView(created, 0, FrameLayout.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
|
||||
if (shown) created.bringToFront()
|
||||
return created
|
||||
}
|
||||
|
||||
private fun rebaseContext(web: WebView, activity: Activity) {
|
||||
(web.context as? MutableContextWrapper)?.baseContext = activity
|
||||
}
|
||||
|
||||
/** Add a WebView to the host, full-size and rendering, hidden behind the UI. */
|
||||
fun attach(activity: Activity, web: WebView) {
|
||||
val o = ensureOverlay(activity)
|
||||
rebaseContext(web, activity)
|
||||
web.visibility = View.VISIBLE
|
||||
if (web.parent == null) {
|
||||
o.addView(web, FrameLayout.LayoutParams(
|
||||
@@ -124,9 +154,9 @@ class FaceclawEvenHubWebViewHost {
|
||||
}
|
||||
}
|
||||
|
||||
/** Bring an app's WebView to the front so it is visible on the phone. */
|
||||
fun showOnPhone(web: WebView) {
|
||||
val o = overlay ?: return
|
||||
/** Bring an app's WebView to the front of [activity] so it is visible on the phone. */
|
||||
fun showOnPhone(activity: Activity?, web: WebView) {
|
||||
val o = if (activity != null) ensureOverlay(activity) else overlay ?: return
|
||||
web.bringToFront()
|
||||
o.bringToFront()
|
||||
shown = true
|
||||
|
||||
@@ -7,8 +7,10 @@
|
||||
* match what apps expect.
|
||||
*
|
||||
* Known deviations from stock, acceptable for now:
|
||||
* - List rendering is a plain vertical list; itemWidth-based horizontal
|
||||
* layouts are not implemented yet.
|
||||
* - List rendering is a plain vertical list. A non-zero itemWidth sets the
|
||||
* selection outline's width and truncates long items to fit, as on stock;
|
||||
* whether stock also lays narrow items out side by side is unknown.
|
||||
* - Lists scroll with our menus' animation and bounce (see list-menu.ts).
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { EvenHubFont } from "../../graphics/evenhub-font";
|
||||
@@ -19,12 +21,12 @@ import {
|
||||
type EvenHubPage,
|
||||
type EvenHubTextContainer,
|
||||
} from "./containers";
|
||||
import { paintListItems } from "./list-menu";
|
||||
|
||||
export const EVENHUB_SCREEN_WIDTH = 576;
|
||||
export const EVENHUB_SCREEN_HEIGHT = 288;
|
||||
|
||||
const TEXT_WHITE = 255;
|
||||
const LIST_ROW_PADDING = 4;
|
||||
|
||||
/**
|
||||
* SDK 0.0.14 textColor levels 0..4 as grey values. Level 4 is the default and
|
||||
@@ -90,44 +92,9 @@ function paintImageContainer(image: GrayImage, container: EvenHubImageContainer)
|
||||
}
|
||||
}
|
||||
|
||||
/** Horizontal padding between the selection outline and the item text. */
|
||||
const LIST_SELECT_PADDING = 8;
|
||||
const LIST_SELECT_RADIUS = 8;
|
||||
|
||||
function paintListContainer(image: GrayImage, container: EvenHubListContainer, focused: boolean): void {
|
||||
paintBorder(image, container);
|
||||
const font = EvenHubFont.get();
|
||||
const inset = container.borderWidth + container.paddingLength;
|
||||
const rowHeight = font.lineHeight + LIST_ROW_PADDING;
|
||||
const left = container.x + inset;
|
||||
const top = container.y + inset;
|
||||
const innerWidth = Math.max(1, container.width - 2 * inset);
|
||||
// Text is indented by the selection padding so the outline sits inside the list.
|
||||
const textX = left + LIST_SELECT_PADDING;
|
||||
const visibleRows = Math.max(1, Math.floor((container.height - 2 * inset) / rowHeight));
|
||||
// Keep the selection in view.
|
||||
let firstRow = 0;
|
||||
if (container.selectedIndex >= visibleRows) firstRow = container.selectedIndex - visibleRows + 1;
|
||||
for (let row = 0; row < visibleRows; row++) {
|
||||
const index = firstRow + row;
|
||||
if (index >= container.itemNames.length) break;
|
||||
const name = container.itemNames[index]!;
|
||||
const y = top + row * rowHeight;
|
||||
if (index === container.selectedIndex) {
|
||||
// Selection: a rounded outline sized to the text (not the full row width),
|
||||
// with padding on each side and no fill — matching stock's appearance.
|
||||
const textWidth = font.measureLine(name);
|
||||
image.drawRoundedRect(
|
||||
left,
|
||||
y,
|
||||
Math.min(textWidth + 2 * LIST_SELECT_PADDING, innerWidth),
|
||||
rowHeight - 1,
|
||||
focused ? TEXT_WHITE : 130,
|
||||
LIST_SELECT_RADIUS,
|
||||
);
|
||||
}
|
||||
font.drawText(image, textX, y + Math.floor(LIST_ROW_PADDING / 2), name, TEXT_WHITE);
|
||||
}
|
||||
paintListItems(image, container, EvenHubFont.get(), focused);
|
||||
}
|
||||
|
||||
/** Bottom-to-top paint order for a page's containers. */
|
||||
|
||||
@@ -57,7 +57,9 @@ export type EvenHubListContainer = {
|
||||
zOrderIndex: number | undefined;
|
||||
preserve: boolean;
|
||||
itemNames: string[];
|
||||
/** Fixed width of each item's selection outline; 0 is auto (the outline hugs the text). */
|
||||
itemWidth: number;
|
||||
/** isItemSelectBorderEn: whether the selected item is outlined. A protobuf field, so absent means off. */
|
||||
selectBorder: boolean;
|
||||
/** Selection is host-local: scroll moves it with no app round-trip. */
|
||||
selectedIndex: number;
|
||||
@@ -82,6 +84,9 @@ export type EvenHubPage = {
|
||||
menuItems: EvenHubMenuItem[];
|
||||
};
|
||||
|
||||
/** Stock draws at most a 5px border; wider requests are clamped. */
|
||||
const MAX_BORDER_WIDTH = 5;
|
||||
|
||||
/** Firmware brightness levels for text; 4 is the device default. */
|
||||
const MIN_TEXT_BRIGHTNESS = 0;
|
||||
const MAX_TEXT_BRIGHTNESS = 4;
|
||||
@@ -129,6 +134,10 @@ export function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === "object" && !Array.isArray(value) ? (value as Record<string, unknown>) : {};
|
||||
}
|
||||
|
||||
function readBorderWidth(json: Record<string, unknown>): number {
|
||||
return Math.max(0, Math.min(MAX_BORDER_WIDTH, readNumber(json, "borderWidth", 0)));
|
||||
}
|
||||
|
||||
function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer {
|
||||
return {
|
||||
kind: "text",
|
||||
@@ -138,7 +147,7 @@ function parseTextContainer(json: Record<string, unknown>): EvenHubTextContainer
|
||||
y: readNumber(json, "yPosition", 0),
|
||||
width: readNumber(json, "width", 0),
|
||||
height: readNumber(json, "height", 0),
|
||||
borderWidth: readNumber(json, "borderWidth", 0),
|
||||
borderWidth: readBorderWidth(json),
|
||||
borderRadius: readNumber(json, "borderRadius", 0),
|
||||
paddingLength: readNumber(json, "paddingLength", 0),
|
||||
isEventCapture: readNumber(json, "isEventCapture", 0) !== 0,
|
||||
@@ -230,7 +239,7 @@ function parseListContainer(json: Record<string, unknown>): EvenHubListContainer
|
||||
y: readNumber(json, "yPosition", 0),
|
||||
width: readNumber(json, "width", 0),
|
||||
height: readNumber(json, "height", 0),
|
||||
borderWidth: readNumber(json, "borderWidth", 0),
|
||||
borderWidth: readBorderWidth(json),
|
||||
borderRadius: readNumber(json, "borderRadius", 0),
|
||||
paddingLength: readNumber(json, "paddingLength", 0),
|
||||
isEventCapture: readNumber(json, "isEventCapture", 0) !== 0,
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* EvenHub list containers, drawn and navigated by the shared Menu<T> in the
|
||||
* stock list style: the selected item gets a rounded outline sized to its
|
||||
* text, with no fill, and every other item's text is dimmed to 2/15
|
||||
* brightness. Scrolling animates and the ends bounce, like our own menus, but the
|
||||
* selection itself moves instantly (no highlight slide), as on stock.
|
||||
*
|
||||
* Native-free (the font is passed in) so it can be unit-tested under Node.
|
||||
*/
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import type { EvenHubListContainer } from "./containers";
|
||||
|
||||
/** The subset of EvenHubFont a list needs. */
|
||||
export type EvenHubListFont = {
|
||||
readonly lineHeight: number;
|
||||
measureLine(text: string): number;
|
||||
drawText(image: GrayImage, x: number, y: number, text: string, value?: number): void;
|
||||
};
|
||||
|
||||
/** Space between the selection outline and the item text, horizontally and vertically. */
|
||||
export const LIST_ITEM_PADDING_X = 12;
|
||||
export const LIST_ITEM_PADDING_Y = 6;
|
||||
const LIST_SELECT_RADIUS = 8;
|
||||
const SELECTED_TEXT = 255;
|
||||
/** 4-bit level 3 (3/15). */
|
||||
export const DESELECTED_TEXT = 50;
|
||||
const OUTLINE_FOCUSED = 255;
|
||||
const OUTLINE_UNFOCUSED = 130;
|
||||
|
||||
export function listRowHeight(font: EvenHubListFont): number {
|
||||
return font.lineHeight + 2 * LIST_ITEM_PADDING_Y;
|
||||
}
|
||||
|
||||
/**
|
||||
* One Menu per container object, so its scroll motion persists across paints.
|
||||
* A rebuilt page has new container objects and so starts without motion.
|
||||
*/
|
||||
const menus = new WeakMap<EvenHubListContainer, Menu<string>>();
|
||||
|
||||
const ELLIPSIS = "...";
|
||||
|
||||
/** `text`, or its longest prefix that fits in `maxWidth` with "..." appended. */
|
||||
export function truncateToWidth(font: EvenHubListFont, text: string, maxWidth: number): string {
|
||||
if (font.measureLine(text) <= maxWidth) return text;
|
||||
const chars = Array.from(text);
|
||||
// Binary search for the longest prefix that fits with the ellipsis.
|
||||
let low = 0, high = chars.length - 1;
|
||||
while (low < high) {
|
||||
const mid = (low + high + 1) >> 1;
|
||||
if (font.measureLine(chars.slice(0, mid).join("") + ELLIPSIS) <= maxWidth) low = mid;
|
||||
else high = mid - 1;
|
||||
}
|
||||
return chars.slice(0, low).join("") + ELLIPSIS;
|
||||
}
|
||||
|
||||
/** The container's menu, synced to its items and selection (the container is the source of truth). */
|
||||
function listMenu(list: EvenHubListContainer, font: EvenHubListFont): Menu<string> {
|
||||
let menu = menus.get(list);
|
||||
if (!menu) {
|
||||
const rowHeight = listRowHeight(font);
|
||||
menu = new Menu<string>({
|
||||
items: list.itemNames,
|
||||
selectedIndex: list.selectedIndex,
|
||||
getHeight: () => rowHeight,
|
||||
// The draw callback shows the selection, so there is no highlight to slide.
|
||||
highlight: false,
|
||||
// No wrap and no exit callbacks: the ends bounce. The session tells the app.
|
||||
wrap: false,
|
||||
draw: ({ image, item, x, y, width, height, selected, focused }) => {
|
||||
// itemWidth 0 is auto: the outline hugs the text. A fixed itemWidth
|
||||
// sets the outline's width exactly, and text that doesn't fit inside
|
||||
// its padding is cut short with "...".
|
||||
const fixedWidth = list.itemWidth > 0 ? Math.min(list.itemWidth, width) : 0;
|
||||
const text = fixedWidth ? truncateToWidth(font, item, fixedWidth - 2 * LIST_ITEM_PADDING_X) : item;
|
||||
if (selected && list.selectBorder) {
|
||||
image.drawRoundedRect(
|
||||
x,
|
||||
y,
|
||||
fixedWidth || Math.min(font.measureLine(text) + 2 * LIST_ITEM_PADDING_X, width),
|
||||
height - 1,
|
||||
focused ? OUTLINE_FOCUSED : OUTLINE_UNFOCUSED,
|
||||
LIST_SELECT_RADIUS,
|
||||
);
|
||||
}
|
||||
font.drawText(image, x + LIST_ITEM_PADDING_X, y + LIST_ITEM_PADDING_Y, text,
|
||||
selected ? SELECTED_TEXT : DESELECTED_TEXT);
|
||||
},
|
||||
});
|
||||
menus.set(list, menu);
|
||||
}
|
||||
if (menu.items !== list.itemNames) menu.setItems(list.itemNames, list.selectedIndex);
|
||||
else if (menu.selectedIndex !== list.selectedIndex) menu.select(list.selectedIndex);
|
||||
return menu;
|
||||
}
|
||||
|
||||
/** Paint the list's items inside its border and padding. */
|
||||
export function paintListItems(
|
||||
image: GrayImage,
|
||||
list: EvenHubListContainer,
|
||||
font: EvenHubListFont,
|
||||
focused: boolean,
|
||||
): void {
|
||||
const inset = list.borderWidth + list.paddingLength;
|
||||
const rowHeight = listRowHeight(font);
|
||||
// A row cut by the bottom edge is drawn clipped, as a hint that there is
|
||||
// more. A container shorter than one row still shows one whole row.
|
||||
listMenu(list, font).paint(image, {
|
||||
x: list.x + inset,
|
||||
y: list.y + inset,
|
||||
width: Math.max(1, list.width - 2 * inset),
|
||||
height: Math.max(rowHeight, list.height - 2 * inset),
|
||||
}, focused);
|
||||
}
|
||||
|
||||
/**
|
||||
* Step the selection (swipe up = previous). Returns false when it was already
|
||||
* at that end: the selection stays put and the list bounces.
|
||||
*/
|
||||
export function stepListSelection(list: EvenHubListContainer, font: EvenHubListFont, delta: -1 | 1): boolean {
|
||||
const menu = listMenu(list, font);
|
||||
const before = list.selectedIndex;
|
||||
menu.moveSelection(delta);
|
||||
list.selectedIndex = menu.selectedIndex ?? 0;
|
||||
return list.selectedIndex !== before;
|
||||
}
|
||||
@@ -52,6 +52,7 @@ import {
|
||||
type EvenHubPage,
|
||||
} from "./containers";
|
||||
import { compositePage } from "./compositor";
|
||||
import { stepListSelection } from "./list-menu";
|
||||
import { type EvenHubManifest } from "./ehpk";
|
||||
import { permissionsIncludeMicrophone, permissionsInclude } from "./permissions";
|
||||
import { evenHubMicRouter, type EvenHubMicClient } from "./mic-router";
|
||||
@@ -502,13 +503,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
private scrollList(list: EvenHubListContainer, eventType: number): void {
|
||||
// Swipe up selects the previous item, swipe down the next (stock direction).
|
||||
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
|
||||
const next = list.selectedIndex + delta;
|
||||
if (next < 0 || next >= list.itemNames.length) {
|
||||
// At a boundary the selection stays put and the app hears about it.
|
||||
this.emitListEvent(list, eventType);
|
||||
return;
|
||||
}
|
||||
list.selectedIndex = next;
|
||||
// At a boundary the selection stays put (the list bounces) and the app hears about it.
|
||||
if (!stepListSelection(list, EvenHubFont.get(), delta)) this.emitListEvent(list, eventType);
|
||||
this.windowHooks?.requestRender();
|
||||
}
|
||||
|
||||
|
||||
@@ -54,7 +54,9 @@ function originRule(url: string): string {
|
||||
export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
const activity = Application.android?.foregroundActivity ?? Application.android?.startActivity;
|
||||
const context = activity ?? Utils.android.getApplicationContext();
|
||||
const webView = new com.faceclaw.app.FaceclawEvenHubWebView(context);
|
||||
// A mutable wrapper, so the host can re-point the WebView at a recreated
|
||||
// activity (see FaceclawEvenHubWebViewHost).
|
||||
const webView = new com.faceclaw.app.FaceclawEvenHubWebView(new android.content.MutableContextWrapper(context));
|
||||
const settings = webView.getSettings();
|
||||
settings.setJavaScriptEnabled(true);
|
||||
settings.setDomStorageEnabled(true);
|
||||
@@ -125,7 +127,8 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
console.warn(`evenhub webview destroy failed: ${error}`);
|
||||
}
|
||||
},
|
||||
showOnPhone: () => nativeHost.showOnPhone(webView),
|
||||
showOnPhone: () =>
|
||||
nativeHost.showOnPhone(Application.android?.foregroundActivity ?? Application.android?.startActivity ?? null, webView),
|
||||
hideOnPhone: () => nativeHost.hideOnPhone(),
|
||||
};
|
||||
}
|
||||
@@ -89,3 +89,17 @@ test("an omitted or out-of-range textColor falls back to the device default", ()
|
||||
assert.equal(page.containers[0].textColor, undefined);
|
||||
}
|
||||
});
|
||||
|
||||
test("borderWidth is clamped to stock's maximum of 5", () => {
|
||||
const page = parsePage({
|
||||
listObject: [{ containerID: 1, containerName: "l", borderWidth: 9 }],
|
||||
textObject: [{ containerID: 2, containerName: "t", borderWidth: 12 }, { containerID: 3, containerName: "u", borderWidth: 3 }],
|
||||
});
|
||||
assert.deepEqual(page.containers.map((c) => c.borderWidth), [5, 5, 3]);
|
||||
});
|
||||
|
||||
test("a list without isItemSelectBorderEn has no selection border (protobuf default 0)", () => {
|
||||
const page = parsePage({ listObject: [{ containerID: 1, containerName: "l", itemContainer: { itemName: ["a"] } }] });
|
||||
assert.equal(page.containers[0].selectBorder, false);
|
||||
assert.equal(page.containers[0].itemWidth, 0);
|
||||
});
|
||||
@@ -0,0 +1,148 @@
|
||||
// EvenHub list containers in the stock style, on the shared Menu: dim
|
||||
// unselected text, a text-sized outline, animated scrolling and bounce, and a
|
||||
// selection that moves instantly.
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const {
|
||||
paintListItems, stepListSelection, listRowHeight, truncateToWidth, DESELECTED_TEXT, LIST_ITEM_PADDING_X, LIST_ITEM_PADDING_Y,
|
||||
} = require("../.test-build/app/apps/evenhub/list-menu.js");
|
||||
const { GrayImage } = require("../.test-build/app/graphics/image.js");
|
||||
const { DrawOp } = require("../.test-build/app/graphics/display-list.js");
|
||||
|
||||
/** Each character is a 6x10 block of ink at 10px advance. */
|
||||
const font = {
|
||||
lineHeight: 20,
|
||||
measureLine: (text) => text.length * 10,
|
||||
drawText: (image, x, y, text, value = 255) => {
|
||||
for (let i = 0; i < text.length; i++) image.fillRect(x + i * 10, y + 4, 6, 10, value);
|
||||
},
|
||||
};
|
||||
|
||||
function list(itemNames, height) {
|
||||
return {
|
||||
kind: "list", id: 1, name: "list", x: 0, y: 0, width: 200, height,
|
||||
borderWidth: 0, borderRadius: 0, paddingLength: 0, isEventCapture: true, zOrderIndex: undefined,
|
||||
preserve: false, itemNames, itemWidth: 0, selectBorder: true, selectedIndex: 0,
|
||||
};
|
||||
}
|
||||
|
||||
function paint(container, now, focused = true) {
|
||||
const original = Date.now;
|
||||
Date.now = () => now;
|
||||
try {
|
||||
const image = new GrayImage(200, 200, 0);
|
||||
paintListItems(image, container, font, focused);
|
||||
return image;
|
||||
} finally {
|
||||
Date.now = original;
|
||||
}
|
||||
}
|
||||
|
||||
const displayLists = (image) => image.draws.map((draw) => draw.presentation?.displayList).filter(Boolean);
|
||||
const pixel = (image, x, y) => image.withDrawsBaked().pixels[y * image.width + x];
|
||||
|
||||
test("rows are padded 6px around the text; unselected text is at level 2/15", () => {
|
||||
const rowHeight = listRowHeight(font);
|
||||
assert.equal(rowHeight, 32);
|
||||
const container = list(["ab", "cd"], 100);
|
||||
const image = paint(container, 1000);
|
||||
const textX = LIST_ITEM_PADDING_X, textY = LIST_ITEM_PADDING_Y + 4;
|
||||
assert.equal(pixel(image, textX, textY), 255, "selected text is full brightness");
|
||||
assert.equal(pixel(image, textX, rowHeight + textY), DESELECTED_TEXT, "unselected text is 2/15");
|
||||
assert.equal(DESELECTED_TEXT + 8 >> 4, 2);
|
||||
// The outline hugs the text plus padding, not the row width.
|
||||
const outlineRight = 2 * 10 + 2 * LIST_ITEM_PADDING_X - 1;
|
||||
assert.equal(pixel(image, outlineRight, rowHeight / 2), 255);
|
||||
assert.equal(pixel(image, outlineRight + 2, rowHeight / 2), 0);
|
||||
});
|
||||
|
||||
test("the unfocused outline is dimmer", () => {
|
||||
const image = paint(list(["ab"], 100), 1000, false);
|
||||
assert.equal(pixel(image, 0, 16), 130);
|
||||
});
|
||||
|
||||
test("selection within the viewport changes instantly, with no animation", () => {
|
||||
const container = list(["a", "b", "c", "d", "e", "f"], 128);
|
||||
paint(container, 1000);
|
||||
assert.equal(stepListSelection(container, font, 1), true);
|
||||
assert.equal(container.selectedIndex, 1);
|
||||
const image = paint(container, 2000);
|
||||
assert.deepEqual(displayLists(image), []);
|
||||
});
|
||||
|
||||
test("navigation that scrolls animates a strip", () => {
|
||||
const container = list(["a", "b", "c", "d", "e", "f"], 128);
|
||||
paint(container, 1000);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
stepListSelection(container, font, 1);
|
||||
paint(container, 1000 + i);
|
||||
}
|
||||
assert.equal(container.selectedIndex, 3);
|
||||
const scrolled = displayLists(paint(container, 5000));
|
||||
assert.deepEqual(scrolled, [], "settled paints don't animate");
|
||||
stepListSelection(container, font, 1);
|
||||
const [strip] = displayLists(paint(container, 6000));
|
||||
assert.ok(strip?.calls.some((call) => call.op === DrawOp.RECT_COPY), "the scroll animates");
|
||||
assert.ok(!strip.calls.some((call) => call.op === DrawOp.ROUNDED_RECT), "no separate sliding highlight");
|
||||
});
|
||||
|
||||
test("stepping past an end keeps the selection and bounces", () => {
|
||||
const container = list(["a", "b", "c", "d", "e", "f"], 128);
|
||||
paint(container, 1000);
|
||||
assert.equal(stepListSelection(container, font, -1), false);
|
||||
assert.equal(container.selectedIndex, 0);
|
||||
const [bounce] = displayLists(paint(container, 2000));
|
||||
assert.ok(bounce?.calls.some((call) => call.op === DrawOp.RECT_COPY), "the list bounces");
|
||||
});
|
||||
|
||||
test("a row cut by the bottom edge is drawn clipped", () => {
|
||||
// 100px fits three 32px rows and the top 4px of the fourth, whose ink
|
||||
// starts 10px into the row, so a 16px taller box shows its first 10 lines.
|
||||
const clipped = paint(list(["a", "b", "c", "d"], 100), 1000);
|
||||
const textTop = 3 * 32 + LIST_ITEM_PADDING_Y + 4;
|
||||
assert.equal(pixel(clipped, LIST_ITEM_PADDING_X, textTop), 0, "below the edge");
|
||||
const partial = paint(list(["a", "b", "c", "d"], 3 * 32 + 16), 1000);
|
||||
assert.equal(pixel(partial, LIST_ITEM_PADDING_X, 3 * 32 + 16), 0, "nothing past the edge");
|
||||
assert.equal(pixel(partial, LIST_ITEM_PADDING_X, textTop), DESELECTED_TEXT, "the visible part is drawn");
|
||||
});
|
||||
|
||||
test("at the end of a list with a partial row, the top row stays whole", () => {
|
||||
const container = list(["a", "b", "c", "d", "e"], 3 * 32 + 16);
|
||||
paint(container, 1000);
|
||||
for (let i = 0; i < 4; i++) {
|
||||
stepListSelection(container, font, 1);
|
||||
paint(container, 1000 + i);
|
||||
}
|
||||
const image = paint(container, 9000);
|
||||
// As in every Menu, the viewport never starts on a partial row: c, d and
|
||||
// e show whole, leaving 16px empty below e rather than cutting c in half.
|
||||
assert.equal(pixel(image, LIST_ITEM_PADDING_X, LIST_ITEM_PADDING_Y + 4), DESELECTED_TEXT, "c at the top");
|
||||
assert.equal(pixel(image, LIST_ITEM_PADDING_X, 64 + LIST_ITEM_PADDING_Y + 4), 255, "e selected, third row");
|
||||
});
|
||||
|
||||
test("with the selection border off, the selected item has no outline", () => {
|
||||
const container = { ...list(["ab"], 100), selectBorder: false };
|
||||
const image = paint(container, 1000);
|
||||
assert.equal(pixel(image, 0, 16), 0, "no outline edge");
|
||||
assert.equal(pixel(image, LIST_ITEM_PADDING_X, LIST_ITEM_PADDING_Y + 4), 255, "text still selected-bright");
|
||||
});
|
||||
|
||||
test("a fixed itemWidth sets the outline's width exactly", () => {
|
||||
const container = { ...list(["ab"], 100), itemWidth: 150 };
|
||||
const image = paint(container, 1000);
|
||||
assert.equal(pixel(image, 149, 16), 255, "right edge at itemWidth");
|
||||
assert.equal(pixel(image, 150, 16), 0);
|
||||
});
|
||||
|
||||
test("text wider than a fixed itemWidth is truncated with an ellipsis", () => {
|
||||
// 10px per char: 100 - 2 * 12 = 76px holds "abcd..." (70px) but not "abcde..." (80px).
|
||||
assert.equal(truncateToWidth(font, "abcdefghij", 76), "abcd...");
|
||||
assert.equal(truncateToWidth(font, "abcdefg", 76), "abcdefg", "fits: unchanged");
|
||||
assert.equal(truncateToWidth(font, "abcdefgh", 20), "...", "room for nothing but the ellipsis");
|
||||
const drawn = [];
|
||||
const recording = { ...font, drawText: (image, x, y, text, value) => { drawn.push(text); font.drawText(image, x, y, text, value); } };
|
||||
const image = new GrayImage(200, 200, 0);
|
||||
paintListItems(image, { ...list(["abcdefghij", "ab"], 100), itemWidth: 100 }, recording, true);
|
||||
assert.deepEqual(drawn, ["abcd...", "ab"]);
|
||||
});
|
||||
@@ -45,6 +45,7 @@
|
||||
"../app/apps/microphones/radar-dots.ts",
|
||||
"../app/apps/microphones/name-guess.ts",
|
||||
"../app/apps/evenhub/containers.ts",
|
||||
"../app/apps/evenhub/list-menu.ts",
|
||||
"../app/apps/timer/timer-model.ts",
|
||||
"../app/apps/teleprompter/script-tracker.ts",
|
||||
"../app/apps/nightscout/nightscout-alerts.ts",
|
||||
|
||||
Reference in new issue
Block a user