Merge branch 'main' of https://github.com/jimrandomh/faceclaw into microphone-fixes

This commit is contained in:
James Babcock committed 2026-09-27 17:57:23 -07:00
commit 80f16c0cd6
40 files changed
+975 -160

No files matched your search

+6 -39
View File
@@ -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. */
+11 -2
View File
@@ -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,
+126
View File
@@ -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;
}
+70 -33
View File
@@ -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";
@@ -109,30 +110,61 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
};
})();
(function () {
// Host-driven timers. Chromium heavily throttles a page's own
// setTimeout/setInterval when the phone screen is off, which slows
// timer-driven apps to a crawl. Replace them with a queue fired by
// window.__fcTimerTick(), which the native host calls at ~60Hz regardless of
// Host-driven timers and requestAnimationFrame. Chromium heavily throttles a
// page's own setTimeout/setInterval when the phone screen is off, and pauses
// or throttles a hidden page's rAF, which slows timer-driven apps to a crawl
// and stalls canvas apps that render on a rAF loop. Replace them with queues
// fired by window.__fcTick(), which the native host calls regardless of
// screen state (host-initiated evaluateJavascript escapes the throttle).
if (window.__fcTimerTick) return;
// (The glasses render via BLE, not the phone's display, so vsync alignment
// doesn't matter — a steady frame rate does.)
//
// __fcTick returns how many ms until it next has work (-1: none), and the
// host sleeps until then instead of evaluating JS at 60Hz for an idle page.
// Work added outside a tick that is due before the host's next tick wakes it
// through the bridge.
if (window.__fcTick) return;
var FRAME_MS = 16;
var timers = new Map();
var nextId = 1;
var rafs = new Map();
var nextRafId = 1;
var inTick = false;
// When the host will next call __fcTick; Infinity while it is idle.
var hostNextTickAt = Infinity;
function needTickBy(due) {
if (inTick || due >= hostNextTickAt) return;
hostNextTickAt = Date.now();
var bridge = window.__faceclawEvenHub;
if (bridge && bridge.wakeTimers) bridge.wakeTimers();
}
window.setTimeout = function (cb, delay) {
var args = Array.prototype.slice.call(arguments, 2);
var id = nextId++;
timers.set(id, { cb: cb, args: args, due: Date.now() + (+delay || 0), interval: 0 });
var due = Date.now() + (+delay || 0);
timers.set(id, { cb: cb, args: args, due: due, interval: 0 });
needTickBy(due);
return id;
};
window.setInterval = function (cb, delay) {
var args = Array.prototype.slice.call(arguments, 2);
var id = nextId++;
var d = +delay || 0;
timers.set(id, { cb: cb, args: args, due: Date.now() + d, interval: d });
var due = Date.now() + d;
timers.set(id, { cb: cb, args: args, due: due, interval: d });
needTickBy(due);
return id;
};
window.clearTimeout = function (id) { timers.delete(id); };
window.clearInterval = function (id) { timers.delete(id); };
window.__fcTimerTick = function () {
window.requestAnimationFrame = function (cb) {
var id = nextRafId++;
rafs.set(id, cb);
needTickBy(Date.now() + FRAME_MS);
return id;
};
window.cancelAnimationFrame = function (id) { rafs.delete(id); };
function runTimers() {
var now = Date.now();
var dueIds = [];
timers.forEach(function (entry, id) { if (entry.due <= now) dueIds.push(id); });
@@ -150,24 +182,8 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
}
try { entry.cb.apply(null, entry.args); } catch (e) { if (window.console) console.error(e); }
}
};
})();
(function () {
// Host-driven requestAnimationFrame, same idea as the timers above: a hidden
// page's rAF is paused/throttled with the screen off, stalling canvas apps
// that render on a rAF loop. Drive it from window.__fcRafTick(), called by the
// native ticker each frame. (The glasses render via BLE, not the phone's
// display, so vsync alignment doesn't matter — a steady frame rate does.)
if (window.__fcRafTick) return;
var rafs = new Map();
var nextRafId = 1;
window.requestAnimationFrame = function (cb) {
var id = nextRafId++;
rafs.set(id, cb);
return id;
};
window.cancelAnimationFrame = function (id) { rafs.delete(id); };
window.__fcRafTick = function () {
}
function runRafs() {
if (rafs.size === 0) return;
var ts = window.performance && performance.now ? performance.now() : Date.now();
// Snapshot and clear: callbacks that re-request during this frame run on
@@ -177,6 +193,32 @@ export const EVENHUB_BRIDGE_INJECT_SCRIPT = `
current.forEach(function (cb) {
try { cb(ts); } catch (e) { if (window.console) console.error(e); }
});
}
function inTickDo(fn) {
inTick = true;
try { fn(); } finally { inTick = false; }
}
// Fixed-rate entry points for hosts that don't use __fcTick's schedule (the
// iOS host calls both every frame).
window.__fcTimerTick = function () { inTickDo(runTimers); };
window.__fcRafTick = function () { inTickDo(runRafs); };
window.__fcTick = function () {
inTick = true;
try {
runTimers();
runRafs();
} finally {
inTick = false;
}
var now = Date.now();
var delay = rafs.size ? FRAME_MS : -1;
timers.forEach(function (entry) {
var d = entry.due - now;
if (delay < 0 || d < delay) delay = d;
});
if (delay >= 0) delay = Math.max(FRAME_MS, Math.ceil(delay));
hostNextTickAt = delay < 0 ? Infinity : now + delay;
return delay;
};
})();
`;
@@ -502,13 +544,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();
}
+6 -12
View File
@@ -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);
@@ -94,16 +96,7 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
webView.addJavascriptInterface(new com.faceclaw.app.FaceclawEvenHubJsBridge(listener), "__faceclawEvenHub");
// Surface the app's console in logcat (tag FaceclawEvenHubConsole).
const ChromeClient = (android.webkit.WebChromeClient as any).extend({
onConsoleMessage: (message: any): boolean => {
android.util.Log.i(
"FaceclawEvenHubConsole",
`${message.message()} (${message.sourceId()}:${message.lineNumber()})`,
);
return true;
},
});
webView.setWebChromeClient(new ChromeClient());
webView.setWebChromeClient(new com.faceclaw.app.FaceclawEvenHubChromeClient());
const nativeHost = com.faceclaw.app.FaceclawEvenHubWebViewHost.getInstance();
nativeHost.attach(activity, webView);
@@ -125,7 +118,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(),
};
}
+18 -4
View File
@@ -2537,6 +2537,19 @@ class DashboardController {
this.pendingShellRenderCauseFrameId = 0;
const frameId = frameTimings.startFrame("render:shell", causeFrameId);
frameTimings.annotateFrame(frameId, shell.describeInputTarget());
// Nothing to send a frame to: stop here, BEFORE painting. This check used
// to run only after the paint, which made a no-display state a
// main-thread busy loop: paint (~55 ms) -> endRenderPass reports stale
// data -> requestShellRender re-queues (shellRenderQueued, since a render
// is in progress) -> the display check discards the work -> the do/while
// in requestShellRender goes straight round again. Measured on a device
// with the glasses not presenting: 40 of 40 frames discarded, ~19 a
// second, enough to starve input and ANR. Leaving early costs nothing and
// breaks the loop, because the re-request lives downstream of this point.
if (!this.display || this.phase === "charging") {
frameTimings.finishFrame(frameId, "discarded: shell render with no display target");
return;
}
const wantFreshData = this.nextShellRenderWantsFreshData;
this.nextShellRenderWantsFreshData = false;
if (wantFreshData) frameTimings.logFrame(frameId, "follow-up repaint that must not use cached data");
@@ -2555,11 +2568,12 @@ class DashboardController {
this.nextShellRenderWantsFreshData = true;
this.requestShellRender(frameId);
}
// The display-target check that used to live here now runs before the
// paint, at the top of this function. Nothing between the two can change
// it -- paintScene is synchronous and there is no await in between -- so
// repeating it here is dead code, and the compiler agrees: the early
// return narrows this.phase, making the "charging" comparison impossible.
const display = this.display;
if (!display || this.phase === "charging") {
frameTimings.finishFrame(frameId, "discarded: shell render with no display target");
return;
}
await frameTimings.spanAsync(frameId, "submit", () => display.submitShellScene(planes, paintMs, frameId));
// Backpressure: the next shell render waits for this one to reach the
// glasses. Timing out here means the loop was blocked for the full timeout
+33 -5
View File
@@ -95,6 +95,34 @@ function inMode15UncertainRange(cp: number): boolean {
return false;
}
/**
* advanceOf's result per (codePoint, nextCodePoint) pair. Pretext's metrics
* are static data, so this never goes stale; it saves wrap/draw three
* getTextWidth calls (each splitting strings and walking the font chain) for
* every character of every repaint.
*/
const kernedAdvances = new Map<number, number>();
function kernedAdvance(codePoint: number, nextCodePoint: number): number {
// Codepoints are < 0x110000, so this key is unique and exact as a double.
const key = codePoint * 0x110000 + nextCodePoint;
let advance = kernedAdvances.get(key);
if (advance === undefined) {
const a = String.fromCodePoint(codePoint);
if (!nextCodePoint) {
advance = getTextWidth(a);
} else {
const b = String.fromCodePoint(nextCodePoint);
advance = getTextWidth(a + b) - getTextWidth(b);
}
// Pairs are bounded by the text actually shown, but CJK-heavy apps could
// still accumulate a lot of them; start over rather than grow forever.
if (kernedAdvances.size >= 65536) kernedAdvances.clear();
kernedAdvances.set(key, advance);
}
return advance;
}
/** The 4bpp level an 8-bit value packs to (BmpUtil's GRAY_TO_NIBBLE). */
const quantNibble = grayToNibble;
@@ -178,15 +206,15 @@ export class EvenHubFont implements FwTextFont {
* exactly: width(a+b) - width(b) isolates a's kerned advance.
*/
advanceOf(codePoint: number, nextCodePoint = 0): number {
const a = String.fromCodePoint(codePoint);
if (!nextCodePoint) return getTextWidth(a);
const b = String.fromCodePoint(nextCodePoint);
return getTextWidth(a + b) - getTextWidth(b);
return kernedAdvance(codePoint, nextCodePoint);
}
/** Single-line pixel width including kerning (pretext-exact). */
measureLine(text: string): number {
return getTextWidth(text);
const cps = Array.from(text, (c) => c.codePointAt(0)!);
let width = 0;
for (let i = 0; i < cps.length; i++) width += kernedAdvance(cps[i]!, cps[i + 1] ?? 0);
return width;
}
/**
+3 -3
View File
@@ -1,5 +1,5 @@
import { nightscoutApiTokenSetting, nightscoutSiteUrlSetting } from "../ui/dashboard-settings";
import { fetchWithUserAgent } from "../util/http";
import { fetchTextWithUserAgent } from "../util/http";
export type NightscoutPoint = {
timestampMs: number;
@@ -305,11 +305,11 @@ export class NightscoutBridge {
}
async function fetchJson<T>(url: string): Promise<T> {
const response = await fetchWithUserAgent(url);
const response = await fetchTextWithUserAgent(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return (await response.json()) as T;
return response.json<T>();
}
function parseTreatments(treatments: NightscoutTreatmentResponse[], nowMs: number): {
+18 -1
View File
@@ -18,6 +18,7 @@ import {
} from "~/assistant/models";
import { isLocalModelReady } from "../native/llama";
import { drawRightValueMenuItem, drawToggleMenuItem, MenuItem, openModalMenu } from "./menu";
import { MENU_ANIMATION_KEY } from "./menu-animation-pref";
import { LIST_ROW_TEXT_INSET, lineStep } from "./metrics";
import { Layer, type LayerContext } from "./layers";
import { GrayImage } from "~/graphics/image";
@@ -361,11 +362,27 @@ export const autoBrightnessCurveSetting = new ConfigSettingString({
});
const SCREEN_FADE_MS = 280;
export const menuAnimationSetting = new ConfigSettingBoolean({
id: "menu-animation",
label: "Menu animation",
storageKey: MENU_ANIMATION_KEY,
defaultValue: true,
description: "Slide the highlight, scroll, and bounce at the ends in menus, lists, and the launcher and Files icon grids. When off, they move instantly.",
});
export const screenFadeSetting = new ConfigSettingBoolean({
id: "screen-fade",
label: "Screen fade",
storageKey: "display.screenFade",
defaultValue: true,
description: "Fade the display in and out when the screen turns on or off. When off, it switches instantly.",
});
export function getBrightnessPreferences() {
const level = brightnessSettingToLevel(brightnessSetting.get());
return { auto: level === null, level: level ?? 50,
minimum: Number(autoBrightnessMinSetting.get()), maximum: Number(autoBrightnessMaxSetting.get()),
curve: autoBrightnessCurveSetting.getValidValue(), fadeMs: SCREEN_FADE_MS };
curve: autoBrightnessCurveSetting.getValidValue(), fadeMs: screenFadeSetting.get() ? SCREEN_FADE_MS : 0 };
}
export const screenTimeoutSetting = new ConfigSettingEnum<ScreenTimeoutSetting>({
+18
View File
@@ -60,9 +60,11 @@ import {
phoneRotationSetting,
ringConnectionModeSetting,
sonioxApiKeySetting,
screenFadeSetting,
enumSettingMenuItem,
firmwareDebugFlagsSetting,
lockScreenEnabledSetting,
menuAnimationSetting,
saveVoiceRecordingsSetting,
showBleBandwidthSetting,
suspendEvenHubWhenScreenOffSetting,
@@ -114,6 +116,8 @@ function settingsSections(): SettingsSection[] {
enumSettingMenuItem(displayModeSetting),
// Submenu: top-bar battery indicator style plus per-device visibility.
batteryIndicatorsMenuItem(),
// Submenu: toggles for menu/icon-grid motion and the screen on/off fade.
animationsMenuItem(),
// Controls the top-bar clock (24-hour vs 12-hour).
enumSettingMenuItem(timeFormatSetting),
// Opens the modal font picker (face, weight, size) for UI text.
@@ -312,6 +316,20 @@ function batteryIndicatorsMenuItem(): MenuItem {
};
}
/** The Display section's "Animations" row: opens a modal submenu of animation toggles. */
function animationsMenuItem(): MenuItem {
return {
label: "Animations",
description: "Turn off menu and icon-grid motion, or the fade when the screen turns on and off.",
onSelect: (ctx) => {
openSettingsSubMenu(ctx, "Animations", [
toggleSettingMenuItem(menuAnimationSetting),
toggleSettingMenuItem(screenFadeSetting),
]);
},
};
}
const LOCAL_MODEL_GB = `${(LOCAL_MODEL.sizeBytes / 1e9).toFixed(1)}GB`;
// While a download is running, re-render on progress updates so the row's
+18
View File
@@ -0,0 +1,18 @@
/**
* Whether menus and icon grids animate on the glasses (Settings > Display >
* Animations > Menu animation). The motion classes can't read the settings
* store themselves (it is native, and they load under Node in unit tests),
* so ui/menu.ts installs a reader in each isolate that paints menus. Without
* one, menus animate.
*/
export const MENU_ANIMATION_KEY = "display.menuAnimation";
let readMenuAnimation: () => boolean = () => true;
export function menuAnimationEnabled(): boolean {
return readMenuAnimation();
}
export function setMenuAnimationReader(read: () => boolean): void {
readMenuAnimation = read;
}
+3 -1
View File
@@ -1,3 +1,5 @@
import { menuAnimationEnabled } from "./menu-animation-pref";
export const MENU_HIGHLIGHT_DURATION_MS = 240;
export type MenuHighlightAnimation = { dx: number; dy: number; startedAt: number; token: number; durationMs: number };
@@ -30,7 +32,7 @@ export class MenuHighlightMotion {
fromX += this.animation.dx * remaining;
fromY += this.animation.dy * remaining;
}
const eligible = previous && previous.index !== index && this.fromIndex === previous.index &&
const eligible = menuAnimationEnabled() && previous && previous.index !== index && this.fromIndex === previous.index &&
(previous.scroll === scroll || scrolling) && previous.width === width && previous.height === height;
this.animation = eligible ? { dx: Math.round(fromX - x), dy: Math.round(fromY - y), startedAt: now, token: nextAnimationToken(), durationMs: MENU_HIGHLIGHT_DURATION_MS } : undefined;
this.fromIndex = null;
+4 -2
View File
@@ -1,4 +1,5 @@
import { DrawExpression as E, ExprOp } from "../graphics/draw-expression";
import { menuAnimationEnabled } from "./menu-animation-pref";
import { MENU_HIGHLIGHT_DURATION_MS, nextAnimationToken } from "./menu-highlight-motion";
/** Longest scroll, in pixels, that animates; longer jumps snap. */
@@ -72,16 +73,17 @@ export class MenuScrollMotion {
/** Record this paint's settled offset; returns the animation in flight, if any. */
paint(scroll: number, box: Box, now: number): MenuScrollAnimation | undefined {
const previous = this.previous;
const enabled = menuAnimationEnabled();
const sameBox = !!previous && previous.box.x === box.x && previous.box.y === box.y &&
previous.box.width === box.width && previous.box.height === box.height;
if (!previous || !sameBox || previous.scroll !== scroll) {
const from = previous ? this.offsetAt(previous.scroll, now) : scroll;
const eligible = sameBox && this.navigated && from !== scroll && Math.abs(scroll - from) <= MAX_ANIMATED_SCROLL;
const eligible = enabled && sameBox && this.navigated && from !== scroll && Math.abs(scroll - from) <= MAX_ANIMATED_SCROLL;
this.animation = eligible
? { from, to: scroll, startedAt: now, token: nextAnimationToken(), durationMs: MENU_HIGHLIGHT_DURATION_MS }
: undefined;
this.previous = { scroll, box: { ...box } };
} else if (this.bounceBy) {
} else if (this.bounceBy && enabled) {
const from = this.offsetAt(scroll, now);
this.animation = Math.abs(scroll - from) <= MAX_ANIMATED_SCROLL
? { from, peak: scroll + this.bounceBy, to: scroll, startedAt: now, token: nextAnimationToken(), durationMs: MENU_BOUNCE_DURATION_MS }
+7
View File
@@ -1,12 +1,19 @@
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage, type UiFont } from "../graphics/image";
import { wrapText } from "../graphics/textwrap";
import { getDefaultSmallFont } from "../graphics/ui-fonts";
import { getBooleanSetting } from "../native/settings-store";
import { clamp } from "../util/numeric-util";
import { Layer, LayerContext, PaintBelow } from "./layers";
import { MENU_ANIMATION_KEY, setMenuAnimationReader } from "./menu-animation-pref";
import { Menu, MENU_HIGHLIGHT_FILL, MENU_HIGHLIGHT_STROKE } from "./menu-core";
import { GESTURE_DOUBLE_CLICK, InputEvent } from "./gestures";
import { LIST_ROW_TEXT_INSET, centeredTextY, lineStep, listRowHeight, menuTitleHeight } from "./metrics";
// Menu<T> and IconGrid consult this (through their motion classes) at each
// paint; a SharedPreferences read, so changes apply to the next navigation.
setMenuAnimationReader(() => getBooleanSetting(MENU_ANIMATION_KEY, true));
const DEFAULT_MENU_X = 8;
const DEFAULT_MENU_Y = 8;
const DEFAULT_MENU_WIDTH = 272;
+52
View File
@@ -16,6 +16,58 @@ export function fetchWithUserAgent(url: string, init?: RequestInit): Promise<Res
return fetch(url, { ...init, headers: withUserAgent(init?.headers) });
}
/** The parts of a fetch() Response that fetchTextWithUserAgent provides. */
export interface TextResponse {
ok: boolean;
status: number;
text(): Promise<string>;
json<T = unknown>(): Promise<T>;
}
/**
* fetchWithUserAgent for text/JSON bodies, decoded on the Java side.
* NativeScript's fetch() polyfill reads every body as a blob, and on Android
* that copies it from the Java byte[] into JS one bridge call per byte
* (Uint8Array.from in http-request's toArrayBuffer): ~160ms of main thread
* for one Nightscout refresh. Here the body crosses once, as a string.
*/
export async function fetchTextWithUserAgent(
url: string,
init?: { method?: string; headers?: HeadersInit; body?: string },
): Promise<TextResponse> {
// Required lazily so the Node test harness can load this module (via
// fetchWithUserAgent's users) without mocking @nativescript/core. Only the
// http module: the @nativescript/core barrel would pull the UI classes
// (Frame's FragmentClass etc.) into the app workers that import this file,
// and their duplicate extends break the static binding generator.
const { request } = require("@nativescript/core/http") as typeof import("@nativescript/core/http");
const response = await request({
url,
method: init?.method ?? "GET",
headers: withUserAgent(init?.headers),
content: init?.body,
});
const status = response.statusCode;
let body: string | undefined;
const read = (): string => {
if (body === undefined) {
const content = response.content as any;
if (!content) body = "";
// HttpResponseEncoding.UTF8 is 0, so content.toString() can't ask for
// UTF-8 explicitly on Android; decode the raw ByteArrayOutputStream.
else if (global.isAndroid) body = String(content.raw.toString("UTF-8"));
else body = content.toString();
}
return body!;
};
return {
ok: status >= 200 && status < 300,
status,
text: async () => read(),
json: async () => JSON.parse(read()),
};
}
/** Copies headers into a plain record, adding our User-Agent if absent. */
export function withUserAgent(headers?: HeadersInit): Record<string, string> {
const merged: Record<string, string> = {};
+1 -1
View File
@@ -1,2 +1,2 @@
export const FACECLAW_VERSION = "0.7.2";
export const FACECLAW_VERSION = "0.8.0";
export const USER_AGENT = "Faceclaw/"+FACECLAW_VERSION;