forked from Blendman974/faceclaw
First iteration of glanceboard
This commit is contained in:
1 parent
a3660fac61
commit
8a340fc4ad
36 files changed
+2874
-357
No files matched your search
@@ -22,6 +22,7 @@ import freecellApp from "./freecell";
|
|||||||
import pinballApp from "./pinball";
|
import pinballApp from "./pinball";
|
||||||
import developerApp from "./developer";
|
import developerApp from "./developer";
|
||||||
import evenhubApp from "./evenhub";
|
import evenhubApp from "./evenhub";
|
||||||
|
import glanceboardApp from "./glanceboard";
|
||||||
import settingsApp from "./settings";
|
import settingsApp from "./settings";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,5 +54,6 @@ export const ALL_APPS: readonly AppDefinition[] = [
|
|||||||
pinballApp,
|
pinballApp,
|
||||||
developerApp,
|
developerApp,
|
||||||
evenhubApp,
|
evenhubApp,
|
||||||
|
glanceboardApp,
|
||||||
settingsApp,
|
settingsApp,
|
||||||
];
|
];
|
||||||
@@ -32,6 +32,31 @@ export type AppDefinition = {
|
|||||||
showInLauncher?: boolean;
|
showInLauncher?: boolean;
|
||||||
/** Present on the app that handles text shared via the Android share intent. */
|
/** Present on the app that handles text shared via the Android share intent. */
|
||||||
openSharedText?: (ctx: AppContext, title: string, text: string) => void;
|
openSharedText?: (ctx: AppContext, title: string, text: string) => void;
|
||||||
|
/**
|
||||||
|
* Present on the app that supplies the Glanceboard: the sleep-time display
|
||||||
|
* the controller shows on a tap or hold while the shell is asleep (see
|
||||||
|
* app/g2/glance-host.ts). At most one app provides it.
|
||||||
|
*/
|
||||||
|
glanceboard?: GlanceboardProvider;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** A board the glance host can show while the shell sleeps. */
|
||||||
|
export type GlanceboardProvider = {
|
||||||
|
/** Board pixel size; the host centres it in the standard 576x288 band. */
|
||||||
|
size: { width: number; height: number };
|
||||||
|
/** Whether sleep-time gestures should show the board at all (user setting). */
|
||||||
|
isEnabled: () => boolean;
|
||||||
|
/** A fresh board; requestRender asks the host to repaint it. */
|
||||||
|
createBoard: (requestRender: () => void) => GlanceBoardInstance;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GlanceBoardInstance = {
|
||||||
|
/** The board is about to show: start widgets (subscriptions, hardware). */
|
||||||
|
start: () => void;
|
||||||
|
/** The board was hidden: stop everything start began. */
|
||||||
|
stop: () => void;
|
||||||
|
/** Paint the board at its size. */
|
||||||
|
paint: () => GrayImage;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AppLaunchParams = {
|
export type AppLaunchParams = {
|
||||||
|
|||||||
+10
-322
@@ -23,6 +23,7 @@ import {
|
|||||||
import { shell } from "../../ui/shell/shell";
|
import { shell } from "../../ui/shell/shell";
|
||||||
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
import { ensureLocationPermission, hasLocationPermission } from "../../g2/android-permissions";
|
||||||
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
import { isCompassCalibrated, normalizeHeading } from "./calibration";
|
||||||
|
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "./compass-rose";
|
||||||
import { CompassCalibrationLayer } from "./calibration-layer";
|
import { CompassCalibrationLayer } from "./calibration-layer";
|
||||||
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
|
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "./declination";
|
||||||
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
|
import { getNorthReference, resolveHeading, setNorthReference, type NorthReference } from "./heading";
|
||||||
@@ -41,59 +42,6 @@ const EDGE_PAD = 8;
|
|||||||
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
|
/** Cap on the rose's radius, so a tall window doesn't get a comical one. */
|
||||||
const MAX_ROSE_RADIUS = 98;
|
const MAX_ROSE_RADIUS = 98;
|
||||||
|
|
||||||
/**
|
|
||||||
* The rose is drawn as a flat disc lying in front of the wearer, tilted away
|
|
||||||
* from vertical: `TILT_SQUASH` is how much that tilt shortens the depth axis,
|
|
||||||
* and `TILT_PERSPECTIVE` how much nearer parts of the disc are magnified
|
|
||||||
* relative to farther ones. Together they turn the ring into an egg-shaped
|
|
||||||
* oval — wider and taller at the bottom — and give the rose's points their
|
|
||||||
* foreshortening for free.
|
|
||||||
*/
|
|
||||||
const TILT_SQUASH = 0.42;
|
|
||||||
const TILT_PERSPECTIVE = 0.22;
|
|
||||||
/** Screen height of the projected ring, per unit of radius. */
|
|
||||||
const RING_HEIGHT_PER_RADIUS =
|
|
||||||
TILT_SQUASH * (1 / (1 + TILT_PERSPECTIVE) + 1 / (1 - TILT_PERSPECTIVE));
|
|
||||||
/** Screen half-width of the projected ring, per unit of radius. */
|
|
||||||
const RING_HALF_WIDTH_PER_RADIUS = 1 / Math.sqrt(1 - TILT_PERSPECTIVE * TILT_PERSPECTIVE);
|
|
||||||
|
|
||||||
/** Tip radii of the rose's long and short points, as a fraction of the ring. */
|
|
||||||
const CARDINAL_TIP = 0.9;
|
|
||||||
const INTERCARDINAL_TIP = 0.54;
|
|
||||||
/** Radius the point bases sit on: how fat the rose is at its waist. */
|
|
||||||
const POINT_BASE_RADIUS = 0.17;
|
|
||||||
/** Half-angle of a point's base. 22.5° makes neighbouring bases just meet. */
|
|
||||||
const POINT_BASE_HALF_ANGLE = 22.5;
|
|
||||||
/**
|
|
||||||
* A polar grid lying in the same plane as the disc, to give it somewhere to
|
|
||||||
* sit and to fill the empty flanks either side of it. Rings are multiples of
|
|
||||||
* the disc's radius; spokes are offset by half a step so none runs up the
|
|
||||||
* middle into the heading tick.
|
|
||||||
*/
|
|
||||||
const PLANE_RINGS = [1.35, 1.8, 2.5, 3.6, 5.6];
|
|
||||||
const PLANE_SPOKE_STEP = 30;
|
|
||||||
/** Grid brightness at the disc's rim, and how fast it falls off with distance. */
|
|
||||||
const PLANE_NEAR_VALUE = 78;
|
|
||||||
const PLANE_FALLOFF = 0.55;
|
|
||||||
/** Dimmer than this is left unpainted, so the grid ends by vanishing. */
|
|
||||||
const PLANE_CUTOFF = 22;
|
|
||||||
/** Height over which the grid fades out as it approaches the readout. */
|
|
||||||
const PLANE_FADE_SPAN = 45;
|
|
||||||
|
|
||||||
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
|
||||||
const TICK_HEIGHT = 7;
|
|
||||||
const TICK_HALF_WIDTH = 4;
|
|
||||||
/** Depth of the disc's side wall, as a fraction of the ring's radius. */
|
|
||||||
const DISC_DEPTH = 0.1;
|
|
||||||
/**
|
|
||||||
* Rim angle at which the silhouette turns vertical. Between this and its
|
|
||||||
* mirror the wall faces the wearer; outside it the rim curves back away, so a
|
|
||||||
* wall drawn there would hook inward instead of ending on the vertical edge.
|
|
||||||
*/
|
|
||||||
const WALL_START_ANGLE = (Math.acos(-TILT_PERSPECTIVE) * 180) / Math.PI;
|
|
||||||
/** Side-wall shading, from the edges of the silhouette to the nearest point. */
|
|
||||||
const WALL_DARK = 20;
|
|
||||||
const WALL_LIT = 62;
|
|
||||||
|
|
||||||
class CompassLayer implements Layer {
|
class CompassLayer implements Layer {
|
||||||
private rawHeading: number | null = null;
|
private rawHeading: number | null = null;
|
||||||
@@ -233,21 +181,15 @@ class CompassLayer implements Layer {
|
|||||||
// The rose is sized and placed first — it hangs off the bottom edge — and
|
// The rose is sized and placed first — it hangs off the bottom edge — and
|
||||||
// the readout then floats in whatever room is left above it.
|
// the readout then floats in whatever room is left above it.
|
||||||
const textHeight = readoutHeight + 4 + statusLines.length * smallStep;
|
const textHeight = readoutHeight + 4 + statusLines.length * smallStep;
|
||||||
const radius = Math.max(
|
const { cy, radius, ringTop } = layoutCompassRose({
|
||||||
24,
|
width,
|
||||||
Math.min(
|
height,
|
||||||
MAX_ROSE_RADIUS,
|
cx,
|
||||||
Math.floor(
|
topClearance: TOP_PAD + textHeight + STACK_GAP,
|
||||||
(height - BOTTOM_PAD - TOP_PAD - textHeight - STACK_GAP - TICK_HEIGHT)
|
bottomPad: BOTTOM_PAD,
|
||||||
/ (RING_HEIGHT_PER_RADIUS + DISC_DEPTH),
|
edgePad: EDGE_PAD,
|
||||||
),
|
maxRadius: MAX_ROSE_RADIUS,
|
||||||
Math.floor((Math.min(cx, width - cx) - EDGE_PAD) / RING_HALF_WIDTH_PER_RADIUS),
|
});
|
||||||
),
|
|
||||||
);
|
|
||||||
// The wall hangs below the rim, so the near rim sits a wall's depth up
|
|
||||||
// from the bottom padding rather than on it.
|
|
||||||
const cy = height - BOTTOM_PAD - discDepth(radius) - (radius * TILT_SQUASH) / (1 - TILT_PERSPECTIVE);
|
|
||||||
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
|
||||||
|
|
||||||
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
let y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||||
const fade = heading === null ? 0.45 : 1;
|
const fade = heading === null ? 0.45 : 1;
|
||||||
@@ -356,257 +298,3 @@ function frameStatusText(): string {
|
|||||||
return "Magnetic heading - waiting for location";
|
return "Magnetic heading - waiting for location";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function cardinalDirection(heading: number): string {
|
|
||||||
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
|
||||||
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Point {
|
|
||||||
x: number;
|
|
||||||
y: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Project a point of the rose's plane onto the screen. `angle` is degrees
|
|
||||||
* clockwise from the far side of the disc — the direction the wearer faces —
|
|
||||||
* and `radius` is a fraction of the rose's outer ring.
|
|
||||||
*/
|
|
||||||
function projectRose(cx: number, cy: number, scale: number, radius: number, angle: number): Point {
|
|
||||||
const radians = (angle * Math.PI) / 180;
|
|
||||||
const across = radius * Math.sin(radians);
|
|
||||||
const away = radius * Math.cos(radians);
|
|
||||||
const depth = 1 + TILT_PERSPECTIVE * away;
|
|
||||||
return {
|
|
||||||
x: cx + (across * scale) / depth,
|
|
||||||
y: cy - (away * scale * TILT_SQUASH) / depth,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Bounds of every orientation of the rotating points. The longest tip sweeps
|
|
||||||
* a circle in the disc's plane; perspective projects it to an offset ellipse.
|
|
||||||
* Round outward so all rasterized triangle pixels fit inside the rectangle.
|
|
||||||
*/
|
|
||||||
function rotatingRoseBounds(cx: number, cy: number, radius: number): {
|
|
||||||
x: number; y: number; width: number; height: number;
|
|
||||||
} {
|
|
||||||
const perspective = TILT_PERSPECTIVE * CARDINAL_TIP;
|
|
||||||
const halfWidth = radius * CARDINAL_TIP / Math.sqrt(1 - perspective * perspective);
|
|
||||||
const depth = radius * CARDINAL_TIP * TILT_SQUASH;
|
|
||||||
const x = Math.floor(cx - halfWidth);
|
|
||||||
const y = Math.floor(cy - depth / (1 + perspective));
|
|
||||||
return {
|
|
||||||
x,
|
|
||||||
y,
|
|
||||||
width: Math.ceil(cx + halfWidth) - x,
|
|
||||||
height: Math.ceil(cy + depth / (1 - perspective)) - y,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Keep the fixed pixels inside the rotating rose's dirty rectangle as one
|
|
||||||
* immutable texture. The wire planner can omit them from each raster delta
|
|
||||||
* and restore them with a cached image draw. Outside that rectangle the
|
|
||||||
* background stays raster, since heading updates do not change it.
|
|
||||||
*
|
|
||||||
* The texture uses transparent zero for the empty disc interior, so it can
|
|
||||||
* overlay the rotating points. Its only ink over the points is the fixed
|
|
||||||
* heading tick, which belongs on top of them.
|
|
||||||
*/
|
|
||||||
function createCompassBackground(
|
|
||||||
width: number, height: number, cx: number, cy: number, radius: number, clipY: number, fade: number,
|
|
||||||
): GrayImage {
|
|
||||||
const image = new GrayImage(width, height, 0);
|
|
||||||
drawPlaneGrid(image, cx, cy, radius, clipY);
|
|
||||||
drawDiscWall(image, cx, cy, radius, fade);
|
|
||||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
|
||||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
|
||||||
|
|
||||||
const bounds = rotatingRoseBounds(cx, cy, radius);
|
|
||||||
const left = Math.max(0, bounds.x);
|
|
||||||
const top = Math.max(0, bounds.y);
|
|
||||||
const right = Math.min(width, bounds.x + bounds.width);
|
|
||||||
const bottom = Math.min(height, bounds.y + bounds.height);
|
|
||||||
if (right > left && bottom > top) {
|
|
||||||
const texture = new GrayImage(right - left, bottom - top, 0);
|
|
||||||
texture.bitBlt(image, 0, 0, { sx: left, sy: top, width: texture.width, height: texture.height });
|
|
||||||
image.fillRect(left, top, texture.width, texture.height, 0);
|
|
||||||
image.drawImage(texture, left, top);
|
|
||||||
}
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Draw only the rotating points, with the brightest one aimed at north. */
|
|
||||||
function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: number, heading: number | null): void {
|
|
||||||
// Without a reading there is nothing to aim, so show the rose in its resting
|
|
||||||
// orientation, dimmed, rather than an empty ring.
|
|
||||||
const fade = heading === null ? 0.45 : 1;
|
|
||||||
const bearing = heading ?? 0;
|
|
||||||
// Short points first, so the long ones sit on top of them at the waist. Only
|
|
||||||
// north is drawn at full brightness: the other seven are there to make the
|
|
||||||
// rose read as a rose, and competing with north would defeat the point.
|
|
||||||
for (const degrees of [45, 135, 225, 315]) {
|
|
||||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, INTERCARDINAL_TIP, 85 * fade, 40 * fade);
|
|
||||||
}
|
|
||||||
for (const degrees of [90, 180, 270]) {
|
|
||||||
drawRosePoint(image, cx, cy, radius, degrees - bearing, CARDINAL_TIP, 120 * fade, 55 * fade);
|
|
||||||
}
|
|
||||||
drawRosePoint(image, cx, cy, radius, -bearing, CARDINAL_TIP, 255 * fade, 140 * fade);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* One point of the rose: a tapered spike split down its axis into a lit and a
|
|
||||||
* shaded facet, which is what keeps the eight points readable once perspective
|
|
||||||
* has squashed the ones lying across the wearer's line of sight.
|
|
||||||
*/
|
|
||||||
function drawRosePoint(
|
|
||||||
image: GrayImage,
|
|
||||||
cx: number,
|
|
||||||
cy: number,
|
|
||||||
radius: number,
|
|
||||||
angle: number,
|
|
||||||
tipRadius: number,
|
|
||||||
litValue: number,
|
|
||||||
shadedValue: number,
|
|
||||||
): void {
|
|
||||||
const centre = { x: cx, y: cy };
|
|
||||||
const tip = projectRose(cx, cy, radius, tipRadius, angle);
|
|
||||||
const clockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle + POINT_BASE_HALF_ANGLE);
|
|
||||||
const anticlockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle - POINT_BASE_HALF_ANGLE);
|
|
||||||
fillTriangle(image, tip, centre, clockwise, litValue);
|
|
||||||
fillTriangle(image, tip, centre, anticlockwise, shadedValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The side wall below the wearer-facing arc of the rim, which turns the tilted
|
|
||||||
* disc into a shallow cylinder. The arc runs between the two points where the
|
|
||||||
* silhouette turns vertical, so the wall ends flush with that vertical edge,
|
|
||||||
* and it is shaded from dark there to lit where it faces the wearer head-on.
|
|
||||||
*/
|
|
||||||
function drawDiscWall(image: GrayImage, cx: number, cy: number, radius: number, fade: number): void {
|
|
||||||
const depth = discDepth(radius);
|
|
||||||
const segments = 48;
|
|
||||||
const sweep = 360 - WALL_START_ANGLE * 2;
|
|
||||||
const rimValue = 105 * fade;
|
|
||||||
let previous = projectRose(cx, cy, radius, 1, WALL_START_ANGLE);
|
|
||||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
|
||||||
for (let i = 1; i <= segments; i++) {
|
|
||||||
const angle = WALL_START_ANGLE + (i * sweep) / segments;
|
|
||||||
const point = projectRose(cx, cy, radius, 1, angle);
|
|
||||||
// Quads rather than per-column lines, so no gap opens up where the rim
|
|
||||||
// runs nearly horizontal across the bottom of the oval.
|
|
||||||
const value = (WALL_DARK + (WALL_LIT - WALL_DARK) * wallFacing(angle)) * fade;
|
|
||||||
const top = { x: previous.x, y: previous.y };
|
|
||||||
const next = { x: point.x, y: point.y };
|
|
||||||
fillTriangle(image, top, next, { x: next.x, y: next.y + depth }, value);
|
|
||||||
fillTriangle(image, top, { x: top.x, y: top.y + depth }, { x: next.x, y: next.y + depth }, value);
|
|
||||||
image.drawLine(top.x, top.y + depth, next.x, next.y + depth, rimValue);
|
|
||||||
previous = point;
|
|
||||||
}
|
|
||||||
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** 1 where the wall faces the wearer head-on, 0 at the silhouette's edges. */
|
|
||||||
function wallFacing(angle: number): number {
|
|
||||||
const towardWearer = -Math.cos((angle * Math.PI) / 180);
|
|
||||||
return (towardWearer - TILT_PERSPECTIVE) / (1 - TILT_PERSPECTIVE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The grid the disc sits on, drawn before the disc so the disc occludes it.
|
|
||||||
* Brightness falls off with distance and fades to nothing as the plane climbs
|
|
||||||
* towards `clipY`, so the grid dissolves into the black under the readout
|
|
||||||
* rather than stopping at a hard edge.
|
|
||||||
*/
|
|
||||||
function drawPlaneGrid(image: GrayImage, cx: number, cy: number, radius: number, clipY: number): void {
|
|
||||||
const shade = (point: Point, ringRadius: number): number => {
|
|
||||||
if (point.y > image.height || Math.abs(point.x - cx) > image.width) return 0;
|
|
||||||
const fade = Math.max(0, Math.min(1, (point.y - clipY) / PLANE_FADE_SPAN));
|
|
||||||
const value = (PLANE_NEAR_VALUE / (1 + PLANE_FALLOFF * (ringRadius - 1))) * fade;
|
|
||||||
return value >= PLANE_CUTOFF ? value : 0;
|
|
||||||
};
|
|
||||||
const trace = (
|
|
||||||
steps: number,
|
|
||||||
at: (step: number) => { point: Point; ringRadius: number },
|
|
||||||
): void => {
|
|
||||||
let previous: Point | null = null;
|
|
||||||
let previousValue = 0;
|
|
||||||
for (let i = 0; i <= steps; i++) {
|
|
||||||
const { point, ringRadius } = at(i);
|
|
||||||
const value = shade(point, ringRadius);
|
|
||||||
if (previous !== null && previousValue > 0 && value > 0) {
|
|
||||||
image.drawLine(previous.x, previous.y, point.x, point.y, (previousValue + value) / 2);
|
|
||||||
}
|
|
||||||
previous = point;
|
|
||||||
previousValue = value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const ringRadius of PLANE_RINGS) {
|
|
||||||
trace(96, (step) => ({ point: projectRose(cx, cy, radius, ringRadius, (step * 360) / 96), ringRadius }));
|
|
||||||
}
|
|
||||||
// Spokes run from just outside the disc to the outermost ring, so the grid
|
|
||||||
// closes on itself instead of trailing off into loose ends.
|
|
||||||
const outer = PLANE_RINGS[PLANE_RINGS.length - 1]!;
|
|
||||||
for (let angle = PLANE_SPOKE_STEP / 2; angle < 360; angle += PLANE_SPOKE_STEP) {
|
|
||||||
trace(24, (step) => {
|
|
||||||
const ringRadius = 1.05 + (step * (outer - 1.05)) / 24;
|
|
||||||
return { point: projectRose(cx, cy, radius, ringRadius, angle), ringRadius };
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Screen depth of the disc's side wall at the given ring radius. */
|
|
||||||
function discDepth(radius: number): number {
|
|
||||||
return Math.max(3, Math.round(radius * DISC_DEPTH));
|
|
||||||
}
|
|
||||||
|
|
||||||
/** The ring bounding the rose: the tilted disc's rim, drawn as a polygon. */
|
|
||||||
function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
|
||||||
const segments = 72;
|
|
||||||
let previous = projectRose(cx, cy, radius, 1, 0);
|
|
||||||
for (let i = 1; i <= segments; i++) {
|
|
||||||
const point = projectRose(cx, cy, radius, 1, (i * 360) / segments);
|
|
||||||
image.drawLine(previous.x, previous.y, point.x, point.y, value);
|
|
||||||
previous = point;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** A fixed mark just inside the top rim: the heading the wearer is facing. */
|
|
||||||
function drawHeadingTick(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
|
||||||
const top = projectRose(cx, cy, radius, 1, 0);
|
|
||||||
const tickTop = top.y + 1;
|
|
||||||
fillTriangle(
|
|
||||||
image,
|
|
||||||
{ x: cx, y: tickTop + TICK_HEIGHT - 1 },
|
|
||||||
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
|
||||||
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
|
||||||
value,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function fillTriangle(image: GrayImage, a: Point, b: Point, c: Point, value: number): void {
|
|
||||||
const left = Math.max(0, Math.floor(Math.min(a.x, b.x, c.x)));
|
|
||||||
const right = Math.min(image.width - 1, Math.ceil(Math.max(a.x, b.x, c.x)));
|
|
||||||
const top = Math.max(0, Math.floor(Math.min(a.y, b.y, c.y)));
|
|
||||||
const bottom = Math.min(image.height - 1, Math.ceil(Math.max(a.y, b.y, c.y)));
|
|
||||||
const shade = Math.round(value);
|
|
||||||
for (let y = top; y <= bottom; y++) {
|
|
||||||
for (let x = left; x <= right; x++) {
|
|
||||||
const point = { x: x + 0.5, y: y + 0.5 };
|
|
||||||
const ab = edge(a, b, point);
|
|
||||||
const bc = edge(b, c, point);
|
|
||||||
const ca = edge(c, a, point);
|
|
||||||
// Accept either winding, so callers don't have to order their vertices.
|
|
||||||
if ((ab >= 0 && bc >= 0 && ca >= 0) || (ab <= 0 && bc <= 0 && ca <= 0)) {
|
|
||||||
image.setPixel(x, y, shade);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Twice the signed area of the triangle a-b-p; its sign is p's side of a-b. */
|
|
||||||
function edge(a: Point, b: Point, p: Point): number {
|
|
||||||
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,352 @@
|
|||||||
|
/**
|
||||||
|
* The compass rose renderer: a tilted disc with eight rotating points, drawn
|
||||||
|
* from a heading into any GrayImage. Pure drawing code shared by the Compass
|
||||||
|
* app's full-window view and the Glanceboard's compass widget; the callers
|
||||||
|
* own the magnetometer lifecycle and the readout text around it.
|
||||||
|
*/
|
||||||
|
import { GrayImage } from "../../graphics/image";
|
||||||
|
import { normalizeHeading } from "./calibration";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The rose is drawn as a flat disc lying in front of the wearer, tilted away
|
||||||
|
* from vertical: `TILT_SQUASH` is how much that tilt shortens the depth axis,
|
||||||
|
* and `TILT_PERSPECTIVE` how much nearer parts of the disc are magnified
|
||||||
|
* relative to farther ones. Together they turn the ring into an egg-shaped
|
||||||
|
* oval — wider and taller at the bottom — and give the rose's points their
|
||||||
|
* foreshortening for free.
|
||||||
|
*/
|
||||||
|
const TILT_SQUASH = 0.42;
|
||||||
|
const TILT_PERSPECTIVE = 0.22;
|
||||||
|
/** Screen height of the projected ring, per unit of radius. */
|
||||||
|
const RING_HEIGHT_PER_RADIUS =
|
||||||
|
TILT_SQUASH * (1 / (1 + TILT_PERSPECTIVE) + 1 / (1 - TILT_PERSPECTIVE));
|
||||||
|
/** Screen half-width of the projected ring, per unit of radius. */
|
||||||
|
const RING_HALF_WIDTH_PER_RADIUS = 1 / Math.sqrt(1 - TILT_PERSPECTIVE * TILT_PERSPECTIVE);
|
||||||
|
|
||||||
|
/** Tip radii of the rose's long and short points, as a fraction of the ring. */
|
||||||
|
const CARDINAL_TIP = 0.9;
|
||||||
|
const INTERCARDINAL_TIP = 0.54;
|
||||||
|
/** Radius the point bases sit on: how fat the rose is at its waist. */
|
||||||
|
const POINT_BASE_RADIUS = 0.17;
|
||||||
|
/** Half-angle of a point's base. 22.5° makes neighbouring bases just meet. */
|
||||||
|
const POINT_BASE_HALF_ANGLE = 22.5;
|
||||||
|
/**
|
||||||
|
* A polar grid lying in the same plane as the disc, to give it somewhere to
|
||||||
|
* sit and to fill the empty flanks either side of it. Rings are multiples of
|
||||||
|
* the disc's radius; spokes are offset by half a step so none runs up the
|
||||||
|
* middle into the heading tick.
|
||||||
|
*/
|
||||||
|
const PLANE_RINGS = [1.35, 1.8, 2.5, 3.6, 5.6];
|
||||||
|
const PLANE_SPOKE_STEP = 30;
|
||||||
|
/** Grid brightness at the disc's rim, and how fast it falls off with distance. */
|
||||||
|
const PLANE_NEAR_VALUE = 78;
|
||||||
|
const PLANE_FALLOFF = 0.55;
|
||||||
|
/** Dimmer than this is left unpainted, so the grid ends by vanishing. */
|
||||||
|
const PLANE_CUTOFF = 22;
|
||||||
|
/** Height over which the grid fades out as it approaches the readout. */
|
||||||
|
const PLANE_FADE_SPAN = 45;
|
||||||
|
|
||||||
|
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||||
|
const TICK_HEIGHT = 7;
|
||||||
|
const TICK_HALF_WIDTH = 4;
|
||||||
|
/** Depth of the disc's side wall, as a fraction of the ring's radius. */
|
||||||
|
const DISC_DEPTH = 0.1;
|
||||||
|
/**
|
||||||
|
* Rim angle at which the silhouette turns vertical. Between this and its
|
||||||
|
* mirror the wall faces the wearer; outside it the rim curves back away, so a
|
||||||
|
* wall drawn there would hook inward instead of ending on the vertical edge.
|
||||||
|
*/
|
||||||
|
const WALL_START_ANGLE = (Math.acos(-TILT_PERSPECTIVE) * 180) / Math.PI;
|
||||||
|
/** Side-wall shading, from the edges of the silhouette to the nearest point. */
|
||||||
|
const WALL_DARK = 20;
|
||||||
|
const WALL_LIT = 62;
|
||||||
|
|
||||||
|
/** Size of the fixed tick that marks the direction the wearer is facing. */
|
||||||
|
export { TICK_HEIGHT };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fit a rose into a box: the largest radius whose projected ring plus disc
|
||||||
|
* wall fits between `topClearance` (the bottom of whatever text sits above)
|
||||||
|
* and the box's bottom padding, and whose width clears the side padding
|
||||||
|
* around `cx`. Returns the centre and radius to hand to drawCompassRose.
|
||||||
|
*/
|
||||||
|
export function layoutCompassRose(options: {
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
cx: number;
|
||||||
|
/** Lowest y already used above the rose (readout text, its gap). */
|
||||||
|
topClearance: number;
|
||||||
|
bottomPad: number;
|
||||||
|
edgePad: number;
|
||||||
|
maxRadius: number;
|
||||||
|
minRadius?: number;
|
||||||
|
}): { cx: number; cy: number; radius: number; ringTop: number } {
|
||||||
|
const { width, height, cx } = options;
|
||||||
|
const radius = Math.max(
|
||||||
|
options.minRadius ?? 24,
|
||||||
|
Math.min(
|
||||||
|
options.maxRadius,
|
||||||
|
Math.floor((height - options.bottomPad - options.topClearance - TICK_HEIGHT) / (RING_HEIGHT_PER_RADIUS + DISC_DEPTH)),
|
||||||
|
Math.floor((Math.min(cx, width - cx) - options.edgePad) / RING_HALF_WIDTH_PER_RADIUS),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// The wall hangs below the rim, so the near rim sits a wall's depth up
|
||||||
|
// from the bottom padding rather than on it.
|
||||||
|
const cy = height - options.bottomPad - discDepth(radius) - (radius * TILT_SQUASH) / (1 - TILT_PERSPECTIVE);
|
||||||
|
const ringTop = cy - (radius * TILT_SQUASH) / (1 + TILT_PERSPECTIVE);
|
||||||
|
return { cx, cy, radius, ringTop };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function cardinalDirection(heading: number): string {
|
||||||
|
const names = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
|
||||||
|
return names[Math.round(normalizeHeading(heading) / 45) % names.length]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Point {
|
||||||
|
x: number;
|
||||||
|
y: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project a point of the rose's plane onto the screen. `angle` is degrees
|
||||||
|
* clockwise from the far side of the disc — the direction the wearer faces —
|
||||||
|
* and `radius` is a fraction of the rose's outer ring.
|
||||||
|
*/
|
||||||
|
function projectRose(cx: number, cy: number, scale: number, radius: number, angle: number): Point {
|
||||||
|
const radians = (angle * Math.PI) / 180;
|
||||||
|
const across = radius * Math.sin(radians);
|
||||||
|
const away = radius * Math.cos(radians);
|
||||||
|
const depth = 1 + TILT_PERSPECTIVE * away;
|
||||||
|
return {
|
||||||
|
x: cx + (across * scale) / depth,
|
||||||
|
y: cy - (away * scale * TILT_SQUASH) / depth,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bounds of every orientation of the rotating points. The longest tip sweeps
|
||||||
|
* a circle in the disc's plane; perspective projects it to an offset ellipse.
|
||||||
|
* Round outward so all rasterized triangle pixels fit inside the rectangle.
|
||||||
|
*/
|
||||||
|
function rotatingRoseBounds(cx: number, cy: number, radius: number): {
|
||||||
|
x: number; y: number; width: number; height: number;
|
||||||
|
} {
|
||||||
|
const perspective = TILT_PERSPECTIVE * CARDINAL_TIP;
|
||||||
|
const halfWidth = radius * CARDINAL_TIP / Math.sqrt(1 - perspective * perspective);
|
||||||
|
const depth = radius * CARDINAL_TIP * TILT_SQUASH;
|
||||||
|
const x = Math.floor(cx - halfWidth);
|
||||||
|
const y = Math.floor(cy - depth / (1 + perspective));
|
||||||
|
return {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width: Math.ceil(cx + halfWidth) - x,
|
||||||
|
height: Math.ceil(cy + depth / (1 - perspective)) - y,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep the fixed pixels inside the rotating rose's dirty rectangle as one
|
||||||
|
* immutable texture. The wire planner can omit them from each raster delta
|
||||||
|
* and restore them with a cached image draw. Outside that rectangle the
|
||||||
|
* background stays raster, since heading updates do not change it.
|
||||||
|
*
|
||||||
|
* The texture uses transparent zero for the empty disc interior, so it can
|
||||||
|
* overlay the rotating points. Its only ink over the points is the fixed
|
||||||
|
* heading tick, which belongs on top of them.
|
||||||
|
*/
|
||||||
|
export function createCompassBackground(
|
||||||
|
width: number, height: number, cx: number, cy: number, radius: number, clipY: number, fade: number,
|
||||||
|
): GrayImage {
|
||||||
|
const image = new GrayImage(width, height, 0);
|
||||||
|
drawPlaneGrid(image, cx, cy, radius, clipY);
|
||||||
|
drawDiscWall(image, cx, cy, radius, fade);
|
||||||
|
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||||
|
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||||
|
|
||||||
|
const bounds = rotatingRoseBounds(cx, cy, radius);
|
||||||
|
const left = Math.max(0, bounds.x);
|
||||||
|
const top = Math.max(0, bounds.y);
|
||||||
|
const right = Math.min(width, bounds.x + bounds.width);
|
||||||
|
const bottom = Math.min(height, bounds.y + bounds.height);
|
||||||
|
if (right > left && bottom > top) {
|
||||||
|
const texture = new GrayImage(right - left, bottom - top, 0);
|
||||||
|
texture.bitBlt(image, 0, 0, { sx: left, sy: top, width: texture.width, height: texture.height });
|
||||||
|
image.fillRect(left, top, texture.width, texture.height, 0);
|
||||||
|
image.drawImage(texture, left, top);
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Draw only the rotating points, with the brightest one aimed at north. */
|
||||||
|
export function drawCompassRose(image: GrayImage, cx: number, cy: number, radius: number, heading: number | null): void {
|
||||||
|
// Without a reading there is nothing to aim, so show the rose in its resting
|
||||||
|
// orientation, dimmed, rather than an empty ring.
|
||||||
|
const fade = heading === null ? 0.45 : 1;
|
||||||
|
const bearing = heading ?? 0;
|
||||||
|
// Short points first, so the long ones sit on top of them at the waist. Only
|
||||||
|
// north is drawn at full brightness: the other seven are there to make the
|
||||||
|
// rose read as a rose, and competing with north would defeat the point.
|
||||||
|
for (const degrees of [45, 135, 225, 315]) {
|
||||||
|
drawRosePoint(image, cx, cy, radius, degrees - bearing, INTERCARDINAL_TIP, 85 * fade, 40 * fade);
|
||||||
|
}
|
||||||
|
for (const degrees of [90, 180, 270]) {
|
||||||
|
drawRosePoint(image, cx, cy, radius, degrees - bearing, CARDINAL_TIP, 120 * fade, 55 * fade);
|
||||||
|
}
|
||||||
|
drawRosePoint(image, cx, cy, radius, -bearing, CARDINAL_TIP, 255 * fade, 140 * fade);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One point of the rose: a tapered spike split down its axis into a lit and a
|
||||||
|
* shaded facet, which is what keeps the eight points readable once perspective
|
||||||
|
* has squashed the ones lying across the wearer's line of sight.
|
||||||
|
*/
|
||||||
|
function drawRosePoint(
|
||||||
|
image: GrayImage,
|
||||||
|
cx: number,
|
||||||
|
cy: number,
|
||||||
|
radius: number,
|
||||||
|
angle: number,
|
||||||
|
tipRadius: number,
|
||||||
|
litValue: number,
|
||||||
|
shadedValue: number,
|
||||||
|
): void {
|
||||||
|
const centre = { x: cx, y: cy };
|
||||||
|
const tip = projectRose(cx, cy, radius, tipRadius, angle);
|
||||||
|
const clockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle + POINT_BASE_HALF_ANGLE);
|
||||||
|
const anticlockwise = projectRose(cx, cy, radius, POINT_BASE_RADIUS, angle - POINT_BASE_HALF_ANGLE);
|
||||||
|
fillTriangle(image, tip, centre, clockwise, litValue);
|
||||||
|
fillTriangle(image, tip, centre, anticlockwise, shadedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The side wall below the wearer-facing arc of the rim, which turns the tilted
|
||||||
|
* disc into a shallow cylinder. The arc runs between the two points where the
|
||||||
|
* silhouette turns vertical, so the wall ends flush with that vertical edge,
|
||||||
|
* and it is shaded from dark there to lit where it faces the wearer head-on.
|
||||||
|
*/
|
||||||
|
function drawDiscWall(image: GrayImage, cx: number, cy: number, radius: number, fade: number): void {
|
||||||
|
const depth = discDepth(radius);
|
||||||
|
const segments = 48;
|
||||||
|
const sweep = 360 - WALL_START_ANGLE * 2;
|
||||||
|
const rimValue = 105 * fade;
|
||||||
|
let previous = projectRose(cx, cy, radius, 1, WALL_START_ANGLE);
|
||||||
|
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||||
|
for (let i = 1; i <= segments; i++) {
|
||||||
|
const angle = WALL_START_ANGLE + (i * sweep) / segments;
|
||||||
|
const point = projectRose(cx, cy, radius, 1, angle);
|
||||||
|
// Quads rather than per-column lines, so no gap opens up where the rim
|
||||||
|
// runs nearly horizontal across the bottom of the oval.
|
||||||
|
const value = (WALL_DARK + (WALL_LIT - WALL_DARK) * wallFacing(angle)) * fade;
|
||||||
|
const top = { x: previous.x, y: previous.y };
|
||||||
|
const next = { x: point.x, y: point.y };
|
||||||
|
fillTriangle(image, top, next, { x: next.x, y: next.y + depth }, value);
|
||||||
|
fillTriangle(image, top, { x: top.x, y: top.y + depth }, { x: next.x, y: next.y + depth }, value);
|
||||||
|
image.drawLine(top.x, top.y + depth, next.x, next.y + depth, rimValue);
|
||||||
|
previous = point;
|
||||||
|
}
|
||||||
|
image.drawLine(previous.x, previous.y, previous.x, previous.y + depth, rimValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 1 where the wall faces the wearer head-on, 0 at the silhouette's edges. */
|
||||||
|
function wallFacing(angle: number): number {
|
||||||
|
const towardWearer = -Math.cos((angle * Math.PI) / 180);
|
||||||
|
return (towardWearer - TILT_PERSPECTIVE) / (1 - TILT_PERSPECTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The grid the disc sits on, drawn before the disc so the disc occludes it.
|
||||||
|
* Brightness falls off with distance and fades to nothing as the plane climbs
|
||||||
|
* towards `clipY`, so the grid dissolves into the black under the readout
|
||||||
|
* rather than stopping at a hard edge.
|
||||||
|
*/
|
||||||
|
function drawPlaneGrid(image: GrayImage, cx: number, cy: number, radius: number, clipY: number): void {
|
||||||
|
const shade = (point: Point, ringRadius: number): number => {
|
||||||
|
if (point.y > image.height || Math.abs(point.x - cx) > image.width) return 0;
|
||||||
|
const fade = Math.max(0, Math.min(1, (point.y - clipY) / PLANE_FADE_SPAN));
|
||||||
|
const value = (PLANE_NEAR_VALUE / (1 + PLANE_FALLOFF * (ringRadius - 1))) * fade;
|
||||||
|
return value >= PLANE_CUTOFF ? value : 0;
|
||||||
|
};
|
||||||
|
const trace = (
|
||||||
|
steps: number,
|
||||||
|
at: (step: number) => { point: Point; ringRadius: number },
|
||||||
|
): void => {
|
||||||
|
let previous: Point | null = null;
|
||||||
|
let previousValue = 0;
|
||||||
|
for (let i = 0; i <= steps; i++) {
|
||||||
|
const { point, ringRadius } = at(i);
|
||||||
|
const value = shade(point, ringRadius);
|
||||||
|
if (previous !== null && previousValue > 0 && value > 0) {
|
||||||
|
image.drawLine(previous.x, previous.y, point.x, point.y, (previousValue + value) / 2);
|
||||||
|
}
|
||||||
|
previous = point;
|
||||||
|
previousValue = value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const ringRadius of PLANE_RINGS) {
|
||||||
|
trace(96, (step) => ({ point: projectRose(cx, cy, radius, ringRadius, (step * 360) / 96), ringRadius }));
|
||||||
|
}
|
||||||
|
// Spokes run from just outside the disc to the outermost ring, so the grid
|
||||||
|
// closes on itself instead of trailing off into loose ends.
|
||||||
|
const outer = PLANE_RINGS[PLANE_RINGS.length - 1]!;
|
||||||
|
for (let angle = PLANE_SPOKE_STEP / 2; angle < 360; angle += PLANE_SPOKE_STEP) {
|
||||||
|
trace(24, (step) => {
|
||||||
|
const ringRadius = 1.05 + (step * (outer - 1.05)) / 24;
|
||||||
|
return { point: projectRose(cx, cy, radius, ringRadius, angle), ringRadius };
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Screen depth of the disc's side wall at the given ring radius. */
|
||||||
|
function discDepth(radius: number): number {
|
||||||
|
return Math.max(3, Math.round(radius * DISC_DEPTH));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The ring bounding the rose: the tilted disc's rim, drawn as a polygon. */
|
||||||
|
function drawTiltedRing(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||||
|
const segments = 72;
|
||||||
|
let previous = projectRose(cx, cy, radius, 1, 0);
|
||||||
|
for (let i = 1; i <= segments; i++) {
|
||||||
|
const point = projectRose(cx, cy, radius, 1, (i * 360) / segments);
|
||||||
|
image.drawLine(previous.x, previous.y, point.x, point.y, value);
|
||||||
|
previous = point;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A fixed mark just inside the top rim: the heading the wearer is facing. */
|
||||||
|
function drawHeadingTick(image: GrayImage, cx: number, cy: number, radius: number, value: number): void {
|
||||||
|
const top = projectRose(cx, cy, radius, 1, 0);
|
||||||
|
const tickTop = top.y + 1;
|
||||||
|
fillTriangle(
|
||||||
|
image,
|
||||||
|
{ x: cx, y: tickTop + TICK_HEIGHT - 1 },
|
||||||
|
{ x: cx - TICK_HALF_WIDTH, y: tickTop },
|
||||||
|
{ x: cx + TICK_HALF_WIDTH, y: tickTop },
|
||||||
|
value,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fillTriangle(image: GrayImage, a: Point, b: Point, c: Point, value: number): void {
|
||||||
|
const left = Math.max(0, Math.floor(Math.min(a.x, b.x, c.x)));
|
||||||
|
const right = Math.min(image.width - 1, Math.ceil(Math.max(a.x, b.x, c.x)));
|
||||||
|
const top = Math.max(0, Math.floor(Math.min(a.y, b.y, c.y)));
|
||||||
|
const bottom = Math.min(image.height - 1, Math.ceil(Math.max(a.y, b.y, c.y)));
|
||||||
|
const shade = Math.round(value);
|
||||||
|
for (let y = top; y <= bottom; y++) {
|
||||||
|
for (let x = left; x <= right; x++) {
|
||||||
|
const point = { x: x + 0.5, y: y + 0.5 };
|
||||||
|
const ab = edge(a, b, point);
|
||||||
|
const bc = edge(b, c, point);
|
||||||
|
const ca = edge(c, a, point);
|
||||||
|
// Accept either winding, so callers don't have to order their vertices.
|
||||||
|
if ((ab >= 0 && bc >= 0 && ca >= 0) || (ab <= 0 && bc <= 0 && ca <= 0)) {
|
||||||
|
image.setPixel(x, y, shade);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Twice the signed area of the triangle a-b-p; its sign is p's side of a-b. */
|
||||||
|
function edge(a: Point, b: Point, p: Point): number {
|
||||||
|
return (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x);
|
||||||
|
}
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
import { GrayImage } from "../../graphics/image";
|
||||||
|
import { onAnySettingChanged } from "../../ui/dashboard-settings";
|
||||||
|
import { glanceSlotSettings, type GlanceSlotChoice } from "./glanceboard-settings";
|
||||||
|
import { QUADRANT_LAYOUT, type GlanceLayout } from "./layout";
|
||||||
|
import { type GlanceWidget } from "./widget";
|
||||||
|
import { findGlanceWidget } from "./widgets";
|
||||||
|
|
||||||
|
/** Brightness of the hairlines between slots. */
|
||||||
|
const DIVIDER_VALUE = 40;
|
||||||
|
|
||||||
|
type Slot = { choice: GlanceSlotChoice; widget: GlanceWidget | null };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A board: the layout's slots filled with the widgets the settings choose,
|
||||||
|
* painted as one image. Widgets are created and started when the board
|
||||||
|
* starts and stopped when it stops; a slot setting changed meanwhile swaps
|
||||||
|
* just that slot's widget.
|
||||||
|
*/
|
||||||
|
export class GlanceBoard {
|
||||||
|
private slots: Slot[] = [];
|
||||||
|
private started = false;
|
||||||
|
private unsubscribeSettings: (() => void) | null = null;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly requestRender: () => void,
|
||||||
|
readonly layout: GlanceLayout = QUADRANT_LAYOUT,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
get width(): number {
|
||||||
|
return this.layout.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
get height(): number {
|
||||||
|
return this.layout.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
start(): void {
|
||||||
|
if (this.started) return;
|
||||||
|
this.started = true;
|
||||||
|
this.slots = glanceSlotSettings(this.layout).map((setting) => this.createSlot(setting.get()));
|
||||||
|
this.unsubscribeSettings = onAnySettingChanged(() => this.applySlotSettings());
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
if (!this.started) return;
|
||||||
|
this.started = false;
|
||||||
|
this.unsubscribeSettings?.();
|
||||||
|
this.unsubscribeSettings = null;
|
||||||
|
for (const slot of this.slots) slot.widget?.stop();
|
||||||
|
this.slots = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
private createSlot(choice: GlanceSlotChoice): Slot {
|
||||||
|
const widget = choice === "none" ? null : (findGlanceWidget(choice)?.create() ?? null);
|
||||||
|
widget?.start(this.requestRender);
|
||||||
|
return { choice, widget };
|
||||||
|
}
|
||||||
|
|
||||||
|
private applySlotSettings(): void {
|
||||||
|
if (!this.started) return;
|
||||||
|
let changed = false;
|
||||||
|
glanceSlotSettings(this.layout).forEach((setting, index) => {
|
||||||
|
const choice = setting.get();
|
||||||
|
const slot = this.slots[index];
|
||||||
|
if (!slot || slot.choice === choice) return;
|
||||||
|
slot.widget?.stop();
|
||||||
|
this.slots[index] = this.createSlot(choice);
|
||||||
|
changed = true;
|
||||||
|
});
|
||||||
|
if (changed) this.requestRender();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Paint the whole board (layout-sized). Widgets each get a fresh slot canvas. */
|
||||||
|
paint(): GrayImage {
|
||||||
|
const image = new GrayImage(this.layout.width, this.layout.height, 0);
|
||||||
|
this.layout.slots.forEach((slot, index) => {
|
||||||
|
const widget = this.slots[index]?.widget;
|
||||||
|
if (!widget) return;
|
||||||
|
const canvas = new GrayImage(slot.rect.width, slot.rect.height, 0);
|
||||||
|
try {
|
||||||
|
widget.paint(canvas);
|
||||||
|
} catch (error) {
|
||||||
|
console.warn(`glanceboard widget ${this.slots[index]?.choice} paint failed`, error);
|
||||||
|
}
|
||||||
|
canvas.composeInto(image, slot.rect.x, slot.rect.y);
|
||||||
|
});
|
||||||
|
drawDividers(image, this.layout);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Hairlines along every slot edge that is not the board's own edge. */
|
||||||
|
function drawDividers(image: GrayImage, layout: GlanceLayout): void {
|
||||||
|
for (const { rect } of layout.slots) {
|
||||||
|
if (rect.x > 0) image.drawLine(rect.x, rect.y, rect.x, rect.y + rect.height - 1, DIVIDER_VALUE);
|
||||||
|
if (rect.y > 0) image.drawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y, DIVIDER_VALUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
import { GrayImage } from "../../graphics/image";
|
||||||
|
import { enumSettingMenuItem, toggleSettingMenuItem } from "../../ui/dashboard-settings";
|
||||||
|
import { type InputEvent } from "../../ui/gestures";
|
||||||
|
import { type Layer, type LayerContext, type PaintBelow } from "../../ui/layers";
|
||||||
|
import { MenuLayer, type MenuItem } from "../../ui/menu";
|
||||||
|
import { createInProcessWindow, YieldAtRootLayer, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||||
|
import { GlanceBoard } from "./board";
|
||||||
|
import { glanceboardEnabledSetting, glanceSlotSettings } from "./glanceboard-settings";
|
||||||
|
import { QUADRANT_LAYOUT } from "./layout";
|
||||||
|
|
||||||
|
export const GLANCEBOARD_WINDOW_ID = "glanceboard";
|
||||||
|
export const GLANCEBOARD_SURFACE_ID = "window:glanceboard";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Glanceboard app window: where the board is configured. A list of the
|
||||||
|
* layout's slots (each a widget picker), the sleep-gesture toggle, and a
|
||||||
|
* live preview of the board as it will look when glanced at.
|
||||||
|
*/
|
||||||
|
class GlanceboardConfigLayer implements Layer {
|
||||||
|
private menu: MenuLayer | null = null;
|
||||||
|
|
||||||
|
private currentMenu(ctx: LayerContext): MenuLayer {
|
||||||
|
if (!this.menu) {
|
||||||
|
const { width } = ctx.stack.getBaseSize();
|
||||||
|
const items: MenuItem[] = [
|
||||||
|
{
|
||||||
|
label: "Preview",
|
||||||
|
description: "Show the board as it appears when the display is asleep and tapped.",
|
||||||
|
onSelect: (menuCtx) => menuCtx.stack.push(new GlancePreviewLayer(() => menuCtx.actions.requestRender())),
|
||||||
|
},
|
||||||
|
...glanceSlotSettings(QUADRANT_LAYOUT).map((setting) => enumSettingMenuItem(setting)),
|
||||||
|
toggleSettingMenuItem(glanceboardEnabledSetting),
|
||||||
|
];
|
||||||
|
this.menu = new MenuLayer("Glanceboard", items, {
|
||||||
|
x: 8, y: 8, width: width - 16,
|
||||||
|
opaque: true, showBorder: false,
|
||||||
|
footer: "Asleep: tap shows, hold keeps, double-tap wakes",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return this.menu;
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage {
|
||||||
|
return this.currentMenu(ctx).paint(ctx, paintBelow);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleInput(event: InputEvent, ctx: LayerContext): Promise<void> {
|
||||||
|
await this.currentMenu(ctx).handleInput(event, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The live board in the window; any click or double-click returns to the list. */
|
||||||
|
class GlancePreviewLayer implements Layer {
|
||||||
|
private readonly board: GlanceBoard;
|
||||||
|
|
||||||
|
constructor(requestRender: () => void) {
|
||||||
|
this.board = new GlanceBoard(requestRender);
|
||||||
|
this.board.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(ctx: LayerContext): GrayImage {
|
||||||
|
const { width, height } = ctx.stack.getBaseSize();
|
||||||
|
const board = this.board.paint();
|
||||||
|
if (board.width === width && board.height === height) return board;
|
||||||
|
// A viewport of another size (a per-app display override) shows the board
|
||||||
|
// top-left, cropped or padded, rather than scaled.
|
||||||
|
const image = new GrayImage(width, height, 0);
|
||||||
|
board.composeInto(image, 0, 0);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
handleInput(event: InputEvent, ctx: LayerContext): void {
|
||||||
|
if (event.type === "click" || event.type === "double-click") ctx.stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
onRemoved(): void {
|
||||||
|
this.board.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createGlanceboardAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||||
|
return createInProcessWindow({
|
||||||
|
appId: "glanceboard",
|
||||||
|
windowId: GLANCEBOARD_WINDOW_ID,
|
||||||
|
title: "Glanceboard",
|
||||||
|
iconLetter: "Gb",
|
||||||
|
icon: "eye",
|
||||||
|
closeable: true,
|
||||||
|
// 576x288: the board's own size, so the preview is pixel-exact.
|
||||||
|
heightMode: "medium",
|
||||||
|
actions: options.actions,
|
||||||
|
baseLayer: new YieldAtRootLayer(new GlanceboardConfigLayer()),
|
||||||
|
submitFrame: options.submitFrame,
|
||||||
|
setSurfaceVisible: options.setSurfaceVisible,
|
||||||
|
removeSurface: options.removeSurface,
|
||||||
|
reconfigureSurface: options.reconfigureSurface,
|
||||||
|
onClosed: options.onClosed,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { ConfigSettingBoolean, ConfigSettingEnum } from "../../ui/dashboard-settings";
|
||||||
|
import { QUADRANT_LAYOUT, type GlanceLayout } from "./layout";
|
||||||
|
import { type GlanceWidgetId } from "./widget";
|
||||||
|
|
||||||
|
export type GlanceSlotChoice = GlanceWidgetId | "none";
|
||||||
|
|
||||||
|
const SLOT_CHOICES: readonly GlanceSlotChoice[] = ["none", "system-card", "nightscout", "compass", "music"];
|
||||||
|
|
||||||
|
const SLOT_CHOICE_LABELS: Record<GlanceSlotChoice, string> = {
|
||||||
|
none: "Empty",
|
||||||
|
"system-card": "System card",
|
||||||
|
nightscout: "Nightscout",
|
||||||
|
compass: "Compass",
|
||||||
|
music: "Music",
|
||||||
|
};
|
||||||
|
|
||||||
|
export function glanceSlotChoiceLabel(choice: GlanceSlotChoice): string {
|
||||||
|
return SLOT_CHOICE_LABELS[choice] ?? choice;
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_QUADRANT_CHOICES: readonly GlanceSlotChoice[] = ["system-card", "music", "compass", "nightscout"];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Whether sleep-time gestures show the board at all. Off restores the plain
|
||||||
|
* behaviour: a tap or hold while asleep does nothing, double-tap wakes.
|
||||||
|
*/
|
||||||
|
export const glanceboardEnabledSetting = new ConfigSettingBoolean({
|
||||||
|
id: "glanceboard-enabled",
|
||||||
|
label: "Show on tap while asleep",
|
||||||
|
storageKey: "glanceboard.enabled",
|
||||||
|
defaultValue: true,
|
||||||
|
description:
|
||||||
|
"While the display is asleep, a single tap shows the Glanceboard for a few seconds and a long-press holds it up until released. Double-tap still wakes the regular UI.",
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One picker per layout slot, keyed by layout id and slot index so a future
|
||||||
|
* layout gets its own stored choices rather than inheriting the quadrants'.
|
||||||
|
*/
|
||||||
|
export function glanceSlotSettings(layout: GlanceLayout = QUADRANT_LAYOUT): ConfigSettingEnum<GlanceSlotChoice>[] {
|
||||||
|
return layout.slots.map(
|
||||||
|
(slot, index) =>
|
||||||
|
new ConfigSettingEnum<GlanceSlotChoice>({
|
||||||
|
id: `glanceboard-${layout.id}-slot-${index}`,
|
||||||
|
label: slot.label,
|
||||||
|
storageKey: `glanceboard.${layout.id}.slot.${index}`,
|
||||||
|
defaultValue: (layout === QUADRANT_LAYOUT ? DEFAULT_QUADRANT_CHOICES[index] : undefined) ?? "none",
|
||||||
|
values: SLOT_CHOICES,
|
||||||
|
formatValue: glanceSlotChoiceLabel,
|
||||||
|
description: `Which widget fills the ${slot.label.toLowerCase()} slot of the Glanceboard.`,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { type AppDefinition } from "../app-definition";
|
||||||
|
import { GlanceBoard } from "./board";
|
||||||
|
import { createGlanceboardAppWindow, GLANCEBOARD_SURFACE_ID, GLANCEBOARD_WINDOW_ID } from "./glanceboard-app";
|
||||||
|
import { glanceboardEnabledSetting } from "./glanceboard-settings";
|
||||||
|
import { QUADRANT_LAYOUT } from "./layout";
|
||||||
|
|
||||||
|
const glanceboardApp: AppDefinition = {
|
||||||
|
appId: "glanceboard",
|
||||||
|
title: "Glanceboard",
|
||||||
|
icon: "eye",
|
||||||
|
launch: (ctx) => ctx.launchInProcessApp(GLANCEBOARD_WINDOW_ID, GLANCEBOARD_SURFACE_ID, createGlanceboardAppWindow),
|
||||||
|
glanceboard: {
|
||||||
|
size: { width: QUADRANT_LAYOUT.width, height: QUADRANT_LAYOUT.height },
|
||||||
|
isEnabled: () => glanceboardEnabledSetting.get(),
|
||||||
|
createBoard: (requestRender) => new GlanceBoard(requestRender, QUADRANT_LAYOUT),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default glanceboardApp;
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
/**
|
||||||
|
* Board geometry. A layout names its slots and places them on the board;
|
||||||
|
* the Glanceboard app's slot pickers and the board renderer are both driven
|
||||||
|
* by it, so a replacement layout (different slot count, sizes, positions) is
|
||||||
|
* a matter of swapping this object out and reusing the same widgets.
|
||||||
|
*/
|
||||||
|
export const GLANCEBOARD_WIDTH = 576;
|
||||||
|
export const GLANCEBOARD_HEIGHT = 288;
|
||||||
|
|
||||||
|
export type GlanceSlotRect = { x: number; y: number; width: number; height: number };
|
||||||
|
|
||||||
|
export type GlanceLayout = {
|
||||||
|
id: string;
|
||||||
|
width: number;
|
||||||
|
height: number;
|
||||||
|
slots: readonly { label: string; rect: GlanceSlotRect }[];
|
||||||
|
};
|
||||||
|
|
||||||
|
/** The first layout: four equal quadrants, each 288x144. */
|
||||||
|
export const QUADRANT_LAYOUT: GlanceLayout = {
|
||||||
|
id: "quadrants",
|
||||||
|
width: GLANCEBOARD_WIDTH,
|
||||||
|
height: GLANCEBOARD_HEIGHT,
|
||||||
|
slots: [
|
||||||
|
{ label: "Top left", rect: { x: 0, y: 0, width: 288, height: 144 } },
|
||||||
|
{ label: "Top right", rect: { x: 288, y: 0, width: 288, height: 144 } },
|
||||||
|
{ label: "Bottom left", rect: { x: 0, y: 144, width: 288, height: 144 } },
|
||||||
|
{ label: "Bottom right", rect: { x: 288, y: 144, width: 288, height: 144 } },
|
||||||
|
],
|
||||||
|
};
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import { type GrayImage } from "../../graphics/image";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Glanceboard widget: a small self-contained view that paints into a slot
|
||||||
|
* of the board. Widgets are sized by the layout (the first layout is four
|
||||||
|
* 288x144 quadrants) and own their data subscriptions and any hardware they
|
||||||
|
* need between start and stop, so a board that is not showing costs nothing.
|
||||||
|
*
|
||||||
|
* Layouts are expected to be swapped out and iterated on; widgets are the
|
||||||
|
* stable part, so keep them independent of any particular board geometry
|
||||||
|
* beyond the slot size they are handed.
|
||||||
|
*/
|
||||||
|
export interface GlanceWidget {
|
||||||
|
/**
|
||||||
|
* The board became visible (or this widget was added to a visible board):
|
||||||
|
* subscribe to data, start timers, enable hardware. `requestRender` asks
|
||||||
|
* the board to repaint when something changed.
|
||||||
|
*/
|
||||||
|
start(requestRender: () => void): void;
|
||||||
|
/** Undo start. Called once for every start; must be safe to call twice. */
|
||||||
|
stop(): void;
|
||||||
|
/**
|
||||||
|
* Paint the widget into `image`, a fresh 0-filled canvas of the slot's
|
||||||
|
* size. Deferred draws (text, drawImage) are welcome and keep the cheap
|
||||||
|
* texture-cache wire path.
|
||||||
|
*/
|
||||||
|
paint(image: GrayImage): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type GlanceWidgetId = "system-card" | "nightscout" | "compass" | "music";
|
||||||
|
|
||||||
|
export type GlanceWidgetDefinition = {
|
||||||
|
id: GlanceWidgetId;
|
||||||
|
/** Picker label in the Glanceboard app. */
|
||||||
|
label: string;
|
||||||
|
create: () => GlanceWidget;
|
||||||
|
};
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
import { type GrayImage } from "../../../graphics/image";
|
||||||
|
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||||
|
import { hasLocationPermission } from "../../../g2/android-permissions";
|
||||||
|
import { addCompassListener, COMPASS_CHANGED, setCompassEnabled } from "../../../native/compass";
|
||||||
|
import { isCompassCalibrated, normalizeHeading } from "../../compass/calibration";
|
||||||
|
import { cardinalDirection, createCompassBackground, drawCompassRose, layoutCompassRose, TICK_HEIGHT } from "../../compass/compass-rose";
|
||||||
|
import { getDeclinationAvailability, onDeclinationChanged, refreshDeclination } from "../../compass/declination";
|
||||||
|
import { getNorthReference, resolveHeading } from "../../compass/heading";
|
||||||
|
import { type GlanceWidget } from "../widget";
|
||||||
|
|
||||||
|
/** Owner token for the refcounted magnetometer, distinct from the Compass app's. */
|
||||||
|
const COMPASS_OWNER = "glanceboard";
|
||||||
|
const TOP_PAD = 4;
|
||||||
|
const BOTTOM_PAD = 4;
|
||||||
|
const EDGE_PAD = 8;
|
||||||
|
const MAX_ROSE_RADIUS = 60;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Compass app's rose at card size: heading readout above, tilted rose
|
||||||
|
* below. Turns the magnetometer on for as long as the board shows it (the
|
||||||
|
* Compass app keeps its own hold, so the two never switch each other off).
|
||||||
|
* Never prompts for location: true north is used when a declination is
|
||||||
|
* already known, otherwise the readout says it is magnetic.
|
||||||
|
*/
|
||||||
|
export class CompassWidget implements GlanceWidget {
|
||||||
|
private rawHeading: number | null = null;
|
||||||
|
private unsubscribe: (() => void) | null = null;
|
||||||
|
private unsubscribeDeclination: (() => void) | null = null;
|
||||||
|
private enabled = false;
|
||||||
|
private background: { key: string; image: GrayImage } | null = null;
|
||||||
|
|
||||||
|
start(requestRender: () => void): void {
|
||||||
|
this.unsubscribe = addCompassListener((event) => {
|
||||||
|
if (event.command === COMPASS_CHANGED && event.headingDegrees >= 0) {
|
||||||
|
this.rawHeading = normalizeHeading(event.headingDegrees);
|
||||||
|
requestRender();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.unsubscribeDeclination = onDeclinationChanged(requestRender);
|
||||||
|
if (!this.enabled) {
|
||||||
|
this.enabled = true;
|
||||||
|
setCompassEnabled(true, COMPASS_OWNER);
|
||||||
|
}
|
||||||
|
if (getNorthReference() === "true" && hasLocationPermission()) refreshDeclination();
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
this.unsubscribe?.();
|
||||||
|
this.unsubscribe = null;
|
||||||
|
this.unsubscribeDeclination?.();
|
||||||
|
this.unsubscribeDeclination = null;
|
||||||
|
if (this.enabled) {
|
||||||
|
this.enabled = false;
|
||||||
|
setCompassEnabled(false, COMPASS_OWNER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private statusText(): string | null {
|
||||||
|
if (this.rawHeading === null) return "Waiting for compass";
|
||||||
|
if (!isCompassCalibrated()) return "Uncalibrated";
|
||||||
|
if (getNorthReference() === "true" && getDeclinationAvailability() !== "available") return "Magnetic";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(image: GrayImage): void {
|
||||||
|
const medium = getDefaultMediumFont();
|
||||||
|
const small = getDefaultSmallFont();
|
||||||
|
const { width, height } = image;
|
||||||
|
const heading = this.rawHeading === null ? null : resolveHeading(this.rawHeading).displayDegrees;
|
||||||
|
const headingText = heading === null ? "--°" : `${Math.round(heading)}° ${cardinalDirection(heading)}`;
|
||||||
|
const status = this.statusText();
|
||||||
|
const textHeight = medium.lineHeight + (status ? small.lineHeight + 2 : 0);
|
||||||
|
const cx = width / 2;
|
||||||
|
const { cy, radius, ringTop } = layoutCompassRose({
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
cx,
|
||||||
|
topClearance: TOP_PAD + textHeight + 4,
|
||||||
|
bottomPad: BOTTOM_PAD,
|
||||||
|
edgePad: EDGE_PAD,
|
||||||
|
maxRadius: MAX_ROSE_RADIUS,
|
||||||
|
minRadius: 16,
|
||||||
|
});
|
||||||
|
const fade = heading === null ? 0.45 : 1;
|
||||||
|
const y = Math.max(TOP_PAD, Math.round((ringTop - TICK_HEIGHT - textHeight) / 2));
|
||||||
|
const clipY = y + textHeight + 4;
|
||||||
|
const key = [width, height, cx, cy, radius, clipY, fade].join(",");
|
||||||
|
if (this.background?.key !== key) {
|
||||||
|
this.background = { key, image: createCompassBackground(width, height, cx, cy, radius, clipY, fade) };
|
||||||
|
}
|
||||||
|
// composeInto keeps the background's cached interior texture as a
|
||||||
|
// deferred image draw, so the wire planner can restore it from cache.
|
||||||
|
this.background.image.composeInto(image, 0, 0);
|
||||||
|
drawCompassRose(image, cx, cy, radius, heading);
|
||||||
|
image.drawText(medium, Math.round(cx - medium.measureText(headingText) / 2), y, headingText, heading === null ? 150 : 255);
|
||||||
|
if (status) {
|
||||||
|
image.drawText(small, Math.round(cx - small.measureText(status) / 2), y + medium.lineHeight + 2, status, 125);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { type GlanceWidgetDefinition, type GlanceWidgetId } from "../widget";
|
||||||
|
import { CompassWidget } from "./compass-widget";
|
||||||
|
import { MusicWidget } from "./music-widget";
|
||||||
|
import { NightscoutWidget } from "./nightscout-widget";
|
||||||
|
import { SystemCardWidget } from "./system-card";
|
||||||
|
|
||||||
|
/** Every widget a board slot can hold. */
|
||||||
|
export const GLANCE_WIDGETS: readonly GlanceWidgetDefinition[] = [
|
||||||
|
{ id: "system-card", label: "System card", create: () => new SystemCardWidget() },
|
||||||
|
{ id: "nightscout", label: "Nightscout", create: () => new NightscoutWidget() },
|
||||||
|
{ id: "compass", label: "Compass", create: () => new CompassWidget() },
|
||||||
|
{ id: "music", label: "Music", create: () => new MusicWidget() },
|
||||||
|
];
|
||||||
|
|
||||||
|
export function findGlanceWidget(id: GlanceWidgetId | string): GlanceWidgetDefinition | null {
|
||||||
|
return GLANCE_WIDGETS.find((widget) => widget.id === id) ?? null;
|
||||||
|
}
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
import { type GrayImage } from "../../../graphics/image";
|
||||||
|
import { getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||||
|
import { truncateText } from "../../../graphics/textwrap";
|
||||||
|
import { mediaControllerBridge, type MediaControllerState } from "../../../native/media-controller";
|
||||||
|
import { clamp } from "../../../util/numeric-util";
|
||||||
|
import { type GlanceWidget } from "../widget";
|
||||||
|
|
||||||
|
const PAD = 8;
|
||||||
|
const ART_SIZE = 96;
|
||||||
|
const ART_X = PAD;
|
||||||
|
const PROGRESS_BAR_HEIGHT = 5;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Now playing: album art, title/artist/album and a progress bar for the
|
||||||
|
* active Android media session, the Music app's header at card size. The
|
||||||
|
* media bridge runs for the whole session; the widget subscribes and ticks
|
||||||
|
* once a second only while something is playing.
|
||||||
|
*/
|
||||||
|
export class MusicWidget implements GlanceWidget {
|
||||||
|
private media: MediaControllerState = mediaControllerBridge.snapshot();
|
||||||
|
private unsubscribe: (() => void) | null = null;
|
||||||
|
private progressTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
|
private art: GrayImage | null = null;
|
||||||
|
private artKey = "";
|
||||||
|
|
||||||
|
start(requestRender: () => void): void {
|
||||||
|
this.unsubscribe = mediaControllerBridge.onStateChange((state) => {
|
||||||
|
this.media = state;
|
||||||
|
if (state.playbackState === "playing" && state.durationMs > 0 && state.positionMs >= 0) {
|
||||||
|
this.progressTimer ??= setInterval(requestRender, 1_000);
|
||||||
|
} else {
|
||||||
|
this.stopProgressTimer();
|
||||||
|
}
|
||||||
|
requestRender();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
this.unsubscribe?.();
|
||||||
|
this.unsubscribe = null;
|
||||||
|
this.stopProgressTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private stopProgressTimer(): void {
|
||||||
|
if (this.progressTimer !== null) clearInterval(this.progressTimer);
|
||||||
|
this.progressTimer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(image: GrayImage): void {
|
||||||
|
const medium = getDefaultMediumFont();
|
||||||
|
const small = getDefaultSmallFont();
|
||||||
|
const media = mediaControllerBridge.snapshot();
|
||||||
|
this.media = media;
|
||||||
|
if (!media.available || (!media.title && !media.artist)) {
|
||||||
|
image.drawText(small, PAD, PAD, "Music", 150);
|
||||||
|
const text = media.accessEnabled ? "Nothing playing" : "Notification access needed";
|
||||||
|
image.drawText(small, PAD, PAD + small.lineHeight + 6, text, 140);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const artY = Math.max(PAD, ((image.height - ART_SIZE) / 2) | 0);
|
||||||
|
this.drawArt(image, media, artY);
|
||||||
|
|
||||||
|
const textX = ART_X + ART_SIZE + 12;
|
||||||
|
const textWidth = image.width - textX - PAD;
|
||||||
|
let y = artY;
|
||||||
|
image.drawText(medium, textX, y, truncateText(medium, media.title || "Unknown title", textWidth), 230);
|
||||||
|
y += medium.lineHeight + 2;
|
||||||
|
if (media.artist) {
|
||||||
|
image.drawText(small, textX, y, truncateText(small, media.artist, textWidth), 170);
|
||||||
|
y += small.lineHeight + 2;
|
||||||
|
}
|
||||||
|
if (media.album) {
|
||||||
|
image.drawText(small, textX, y, truncateText(small, media.album, textWidth), 120);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Progress pinned to the art's bottom edge; the state label sits beside it.
|
||||||
|
const barY = artY + ART_SIZE - PROGRESS_BAR_HEIGHT;
|
||||||
|
const timeY = barY - small.lineHeight - 3;
|
||||||
|
const stateLabel = media.playbackState === "playing" ? "" : media.playbackState === "paused" ? "Paused" : media.playbackState;
|
||||||
|
if (media.durationMs > 0 && media.positionMs >= 0) {
|
||||||
|
const elapsed = formatMediaTime(media.positionMs);
|
||||||
|
const duration = formatMediaTime(media.durationMs);
|
||||||
|
image.drawText(small, textX, timeY, elapsed, 140);
|
||||||
|
image.drawText(small, textX + textWidth - small.measureText(duration), timeY, duration, 140);
|
||||||
|
if (stateLabel) {
|
||||||
|
image.drawText(small, textX + (((textWidth - small.measureText(stateLabel)) / 2) | 0), timeY, stateLabel, 140);
|
||||||
|
}
|
||||||
|
image.drawRect(textX, barY, textWidth, PROGRESS_BAR_HEIGHT, 55);
|
||||||
|
const progress = clamp(media.positionMs / media.durationMs, 0, 1);
|
||||||
|
image.fillRect(textX + 1, barY + 1, Math.round((textWidth - 2) * progress), PROGRESS_BAR_HEIGHT - 2, 170);
|
||||||
|
} else if (stateLabel) {
|
||||||
|
image.drawText(small, textX, timeY, stateLabel, 140);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private drawArt(image: GrayImage, media: MediaControllerState, artY: number): void {
|
||||||
|
const key = `${media.packageName}|${media.title}|${media.album}`;
|
||||||
|
// Retry while null: players often publish metadata before the bitmap.
|
||||||
|
if (key !== this.artKey || this.art === null) {
|
||||||
|
this.artKey = key;
|
||||||
|
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
|
||||||
|
}
|
||||||
|
if (this.art) {
|
||||||
|
const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0);
|
||||||
|
const dy = artY + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0);
|
||||||
|
image.bitBlt(this.art, dx, dy);
|
||||||
|
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
|
||||||
|
} else {
|
||||||
|
image.drawRect(ART_X, artY, ART_SIZE, ART_SIZE, 60);
|
||||||
|
const font = getDefaultSmallFont();
|
||||||
|
image.drawText(font, ART_X + Math.round((ART_SIZE - font.measureText("no art")) / 2),
|
||||||
|
artY + Math.round((ART_SIZE - font.lineHeight) / 2), "no art", 90);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatMediaTime(milliseconds: number): string {
|
||||||
|
const totalSeconds = Math.max(0, Math.floor(milliseconds / 1_000));
|
||||||
|
const seconds = totalSeconds % 60;
|
||||||
|
const totalMinutes = Math.floor(totalSeconds / 60);
|
||||||
|
const minutes = totalMinutes % 60;
|
||||||
|
const hours = Math.floor(totalMinutes / 60);
|
||||||
|
return hours > 0
|
||||||
|
? `${hours}:${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`
|
||||||
|
: `${minutes}:${String(seconds).padStart(2, "0")}`;
|
||||||
|
}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
import { type GrayImage } from "../../../graphics/image";
|
||||||
|
import { getDefaultLargeFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||||
|
import { truncateText } from "../../../graphics/textwrap";
|
||||||
|
import { nightscoutBridge, type NightscoutState } from "../../../native/nightscout-bridge";
|
||||||
|
import { isNightscoutSettingsConfigured } from "../../../ui/dashboard-settings";
|
||||||
|
import { lineStep } from "../../../ui/metrics";
|
||||||
|
import { formatAgeShortFromTimestamp } from "~/util/date-util";
|
||||||
|
import {
|
||||||
|
drawDirectionIndicator,
|
||||||
|
drawNightscoutGraph,
|
||||||
|
drawNightscoutValueStrikeThrough,
|
||||||
|
formatDelta,
|
||||||
|
isNightscoutPointStale,
|
||||||
|
} from "../../nightscout/nightscout";
|
||||||
|
import { type GlanceWidget } from "../widget";
|
||||||
|
|
||||||
|
const PAD = 8;
|
||||||
|
/** Width of the readout column left of the graph. */
|
||||||
|
const READOUT_WIDTH = 96;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current glucose with delta, trend and age beside the Nightscout app's
|
||||||
|
* 2-hour graph. The bridge polls on its own for the whole session, so the
|
||||||
|
* widget only subscribes; a minute tick keeps the age and staleness moving
|
||||||
|
* during a stalled fetch.
|
||||||
|
*/
|
||||||
|
export class NightscoutWidget implements GlanceWidget {
|
||||||
|
private state: NightscoutState = nightscoutBridge.snapshot();
|
||||||
|
private unsubscribe: (() => void) | null = null;
|
||||||
|
private tick: ReturnType<typeof setInterval> | null = null;
|
||||||
|
|
||||||
|
start(requestRender: () => void): void {
|
||||||
|
this.unsubscribe = nightscoutBridge.onStateChange((state) => {
|
||||||
|
this.state = state;
|
||||||
|
requestRender();
|
||||||
|
});
|
||||||
|
this.tick = setInterval(requestRender, 60_000);
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
this.unsubscribe?.();
|
||||||
|
this.unsubscribe = null;
|
||||||
|
if (this.tick !== null) clearInterval(this.tick);
|
||||||
|
this.tick = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(image: GrayImage): void {
|
||||||
|
const small = getDefaultSmallFont();
|
||||||
|
const large = getDefaultLargeFont();
|
||||||
|
const state = this.state;
|
||||||
|
const nowMs = Date.now();
|
||||||
|
const step = lineStep(small);
|
||||||
|
image.drawText(small, PAD, PAD, "Nightscout", 150);
|
||||||
|
|
||||||
|
if (!isNightscoutSettingsConfigured() || state.configurationMissing) {
|
||||||
|
image.drawText(small, PAD, PAD + step + 4, "Not configured", 140);
|
||||||
|
image.drawText(small, PAD, PAD + 2 * step + 4, "Set the site URL in the Nightscout app", 100);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const latest = state.latest;
|
||||||
|
if (!state.available || !latest) {
|
||||||
|
image.drawText(small, PAD, PAD + step + 4, "No data", 140);
|
||||||
|
image.drawText(small, PAD, PAD + 2 * step + 4, truncateText(small, state.status, image.width - 2 * PAD), 100);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Readout column: big value, units, then delta + trend and the age.
|
||||||
|
const glucoseText = `${latest.sgv}`;
|
||||||
|
const glucoseY = PAD + step;
|
||||||
|
const glucoseWidth = large.measureText(glucoseText);
|
||||||
|
image.drawText(large, PAD, glucoseY, glucoseText, 230);
|
||||||
|
if (isNightscoutPointStale(latest, nowMs)) {
|
||||||
|
drawNightscoutValueStrikeThrough(image, PAD, glucoseY + (large.lineHeight >> 1), glucoseWidth);
|
||||||
|
}
|
||||||
|
const unitsX = PAD + glucoseWidth + 4;
|
||||||
|
if (unitsX + small.measureText(state.units) <= PAD + READOUT_WIDTH) {
|
||||||
|
image.drawText(small, unitsX, glucoseY + large.lineHeight - small.lineHeight, state.units, 140);
|
||||||
|
}
|
||||||
|
let y = glucoseY + large.lineHeight + 2;
|
||||||
|
const deltaText = `${formatDelta(state.delta)} `;
|
||||||
|
image.drawText(small, PAD, y, deltaText, 180);
|
||||||
|
drawDirectionIndicator(image, small, PAD + small.measureText(deltaText), y, state.direction, 180);
|
||||||
|
y += step;
|
||||||
|
image.drawText(small, PAD, y, `${formatAgeShortFromTimestamp(latest.timestampMs, nowMs)} ago`, 130);
|
||||||
|
|
||||||
|
const graphX = PAD + READOUT_WIDTH + PAD;
|
||||||
|
drawNightscoutGraph(
|
||||||
|
image,
|
||||||
|
{ x: graphX, y: PAD, width: image.width - graphX - PAD, height: image.height - 2 * PAD },
|
||||||
|
state,
|
||||||
|
nowMs,
|
||||||
|
small,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,173 @@
|
|||||||
|
import { BATTERY_ICON_WIDTH, drawBattery } from "../../../graphics/battery";
|
||||||
|
import { type GrayImage, type UiFont } from "../../../graphics/image";
|
||||||
|
import { getDefaultLargeFont, getDefaultMediumFont, getDefaultSmallFont } from "../../../graphics/ui-fonts";
|
||||||
|
import { onAndroidNotificationPosted, readActiveNotificationIcons } from "../../../native/notification-icons";
|
||||||
|
import { readPhoneBatteryState } from "../../../native/phone-battery";
|
||||||
|
import { formatClockDate, formatClockTime } from "../../../ui/clock-format";
|
||||||
|
import {
|
||||||
|
batteryDisplayModeSetting,
|
||||||
|
batteryIndicatorVisible,
|
||||||
|
glassesBatteryVisibilitySetting,
|
||||||
|
onAnySettingChanged,
|
||||||
|
phoneBatteryVisibilitySetting,
|
||||||
|
ringBatteryVisibilitySetting,
|
||||||
|
type BatteryIndicatorVisibility,
|
||||||
|
} from "../../../ui/dashboard-settings";
|
||||||
|
import { shell } from "../../../ui/shell/shell";
|
||||||
|
import { noteStaleDataUsed, renderPassAllowsStaleData } from "../../../util/render-freshness";
|
||||||
|
import { type GlanceWidget } from "../widget";
|
||||||
|
|
||||||
|
const PAD = 8;
|
||||||
|
const NOTIFICATION_ICON_SIZE = 24;
|
||||||
|
const NOTIFICATION_ICON_GAP = 4;
|
||||||
|
const BATTERY_ITEM_GAP = 12;
|
||||||
|
const BATTERY_LABEL_VALUE = 150;
|
||||||
|
|
||||||
|
type BatteryItem = { label: string; percent: number; charging: boolean };
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Date and time, the phone's notification icons, and the phone/G2/R1
|
||||||
|
* battery indicators: the top bar's contents, laid out for a card. Battery
|
||||||
|
* style and per-device visibility follow Settings > Display > Battery
|
||||||
|
* indicators, so the card agrees with the bar.
|
||||||
|
*/
|
||||||
|
export class SystemCardWidget implements GlanceWidget {
|
||||||
|
private requestRender: (() => void) | null = null;
|
||||||
|
private minuteTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
private unsubscribeNotifications: (() => void) | null = null;
|
||||||
|
private unsubscribeSettings: (() => void) | null = null;
|
||||||
|
|
||||||
|
start(requestRender: () => void): void {
|
||||||
|
this.requestRender = requestRender;
|
||||||
|
this.scheduleMinuteTick();
|
||||||
|
this.unsubscribeNotifications = onAndroidNotificationPosted(() => this.requestRender?.());
|
||||||
|
this.unsubscribeSettings = onAnySettingChanged(() => this.requestRender?.());
|
||||||
|
}
|
||||||
|
|
||||||
|
stop(): void {
|
||||||
|
this.requestRender = null;
|
||||||
|
if (this.minuteTimer !== null) clearTimeout(this.minuteTimer);
|
||||||
|
this.minuteTimer = null;
|
||||||
|
this.unsubscribeNotifications?.();
|
||||||
|
this.unsubscribeNotifications = null;
|
||||||
|
this.unsubscribeSettings?.();
|
||||||
|
this.unsubscribeSettings = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Repaint on the minute boundary, so the clock never shows a stale minute. */
|
||||||
|
private scheduleMinuteTick(): void {
|
||||||
|
if (this.minuteTimer !== null) clearTimeout(this.minuteTimer);
|
||||||
|
const now = Date.now();
|
||||||
|
const untilNextMinute = 60_000 - (now % 60_000) + 50;
|
||||||
|
this.minuteTimer = setTimeout(() => {
|
||||||
|
this.minuteTimer = null;
|
||||||
|
if (!this.requestRender) return;
|
||||||
|
this.requestRender();
|
||||||
|
this.scheduleMinuteTick();
|
||||||
|
}, untilNextMinute);
|
||||||
|
}
|
||||||
|
|
||||||
|
paint(image: GrayImage): void {
|
||||||
|
const large = getDefaultLargeFont();
|
||||||
|
const medium = getDefaultMediumFont();
|
||||||
|
const small = getDefaultSmallFont();
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
// Clock and date down the left; batteries take the right edge.
|
||||||
|
const batteryLeft = drawBatteryBlock(image, small, image.width - PAD, PAD);
|
||||||
|
const textWidth = Math.max(0, batteryLeft - BATTERY_ITEM_GAP - PAD);
|
||||||
|
const timeText = formatClockTime(now);
|
||||||
|
const timeFont = large.measureText(timeText) <= textWidth ? large : medium;
|
||||||
|
let y = PAD;
|
||||||
|
image.drawText(timeFont, PAD, y, timeText, 230);
|
||||||
|
y += timeFont.lineHeight + 2;
|
||||||
|
image.drawText(medium, PAD, y, formatClockDate(now), 170);
|
||||||
|
|
||||||
|
// Notification icons along the bottom, as many as fit.
|
||||||
|
const iconY = image.height - PAD - NOTIFICATION_ICON_SIZE;
|
||||||
|
const maxIcons = Math.max(0, ((image.width - 2 * PAD + NOTIFICATION_ICON_GAP) / (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP)) | 0);
|
||||||
|
if (maxIcons > 0) {
|
||||||
|
const { icons, stale } = readActiveNotificationIcons(maxIcons, renderPassAllowsStaleData());
|
||||||
|
if (stale) noteStaleDataUsed();
|
||||||
|
for (let index = 0; index < icons.length; index++) {
|
||||||
|
image.drawImage(icons[index]!, PAD + index * (NOTIFICATION_ICON_SIZE + NOTIFICATION_ICON_GAP), iconY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function collectBatteryItems(): BatteryItem[] {
|
||||||
|
const items: BatteryItem[] = [];
|
||||||
|
const push = (visibility: BatteryIndicatorVisibility, label: string, percent: number | null, charging: boolean | null) => {
|
||||||
|
if (percent === null || !Number.isFinite(percent)) return;
|
||||||
|
const clamped = Math.max(0, Math.min(100, Math.round(percent)));
|
||||||
|
if (!batteryIndicatorVisible(visibility, clamped)) return;
|
||||||
|
items.push({ label, percent: clamped, charging: Boolean(charging) });
|
||||||
|
};
|
||||||
|
const phone = readPhoneBatteryState();
|
||||||
|
push(phoneBatteryVisibilitySetting.get(), "Phone", phone.battery, phone.charging);
|
||||||
|
const levels = shell.getBatteryLevels();
|
||||||
|
push(glassesBatteryVisibilitySetting.get(), "G2", levels.headset, levels.headsetCharging);
|
||||||
|
if (levels.ring !== null && Number.isInteger(levels.ring) && levels.ring >= 0 && levels.ring <= 100) {
|
||||||
|
push(ringBatteryVisibilitySetting.get(), "R1", levels.ring, levels.ringCharging);
|
||||||
|
}
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The battery indicators right-aligned at `right`, in the configured style.
|
||||||
|
* Side-by-side styles put the label beside the gauge (or percentage) on one
|
||||||
|
* line; stacked styles centre the label above it. Returns the block's left
|
||||||
|
* edge (or `right` when nothing is shown).
|
||||||
|
*/
|
||||||
|
function drawBatteryBlock(image: GrayImage, font: UiFont, right: number, top: number): number {
|
||||||
|
const items = collectBatteryItems();
|
||||||
|
if (!items.length) return right;
|
||||||
|
const mode = batteryDisplayModeSetting.get();
|
||||||
|
const percentage = mode === "percentage" || mode === "stacked-percentage";
|
||||||
|
const stacked = mode === "stacked" || mode === "stacked-percentage";
|
||||||
|
const gaugeHeight = drawBattery(0, false).height;
|
||||||
|
let x = right;
|
||||||
|
for (let index = items.length - 1; index >= 0; index--) {
|
||||||
|
const item = items[index]!;
|
||||||
|
const percentText = `${item.percent}%`;
|
||||||
|
const labelWidth = font.measureText(item.label);
|
||||||
|
const valueWidth = percentage ? font.measureText(percentText) : BATTERY_ICON_WIDTH;
|
||||||
|
if (stacked) {
|
||||||
|
const itemWidth = Math.max(labelWidth, valueWidth);
|
||||||
|
x -= itemWidth;
|
||||||
|
image.drawText(font, x + (((itemWidth - labelWidth) / 2) | 0), top, item.label, BATTERY_LABEL_VALUE);
|
||||||
|
drawBatteryValue(image, font, x + (((itemWidth - valueWidth) / 2) | 0), top + font.lineHeight + 2, item, percentage, gaugeHeight);
|
||||||
|
} else {
|
||||||
|
x -= labelWidth + 5 + valueWidth;
|
||||||
|
image.drawText(font, x, top, item.label, BATTERY_LABEL_VALUE);
|
||||||
|
drawBatteryValue(image, font, x + labelWidth + 5, top, item, percentage, gaugeHeight);
|
||||||
|
}
|
||||||
|
x -= BATTERY_ITEM_GAP;
|
||||||
|
}
|
||||||
|
return x + BATTERY_ITEM_GAP;
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawBatteryValue(
|
||||||
|
image: GrayImage,
|
||||||
|
font: UiFont,
|
||||||
|
x: number,
|
||||||
|
lineTop: number,
|
||||||
|
item: BatteryItem,
|
||||||
|
percentage: boolean,
|
||||||
|
gaugeHeight: number,
|
||||||
|
): void {
|
||||||
|
if (percentage) {
|
||||||
|
const percentText = `${item.percent}%`;
|
||||||
|
if (item.charging) {
|
||||||
|
// Inverted text marks charging, as in the top bar.
|
||||||
|
image.fillRect(x - 2, lineTop - 1, font.measureText(percentText) + 4, font.lineHeight + 2, 255);
|
||||||
|
image.drawText(font, x, lineTop, percentText, 1);
|
||||||
|
} else {
|
||||||
|
image.drawText(font, x, lineTop, percentText, 200);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const icon = drawBattery(item.percent, item.charging);
|
||||||
|
image.bitBlt(icon, x, lineTop + Math.max(0, ((font.lineHeight - gaugeHeight) / 2) | 0), { transparentZero: true });
|
||||||
|
}
|
||||||
@@ -24,11 +24,11 @@ import { openSettingsSubMenu } from "../../ui/dashboard/settings-panel";
|
|||||||
import { lineStep } from "../../ui/metrics";
|
import { lineStep } from "../../ui/metrics";
|
||||||
|
|
||||||
const nightscoutLargeFont = getDefaultLargeFont();
|
const nightscoutLargeFont = getDefaultLargeFont();
|
||||||
const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
|
export const NIGHTSCOUT_STALE_MS = 15 * 60 * 1000;
|
||||||
const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
|
const NIGHTSCOUT_GRAPH_WINDOW_MS = 2 * 60 * 60 * 1000;
|
||||||
const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
|
const NIGHTSCOUT_GRAPH_TIME_QUANTUM_MS = 60 * 1000;
|
||||||
|
|
||||||
function drawDirectionIndicator(
|
export function drawDirectionIndicator(
|
||||||
image: GrayImage,
|
image: GrayImage,
|
||||||
font: UiFont,
|
font: UiFont,
|
||||||
x: number,
|
x: number,
|
||||||
@@ -85,7 +85,7 @@ function truncateLine(text: string, maxChars: number): string {
|
|||||||
return `${text.slice(0, Math.max(0, maxChars - 3))}...`;
|
return `${text.slice(0, Math.max(0, maxChars - 3))}...`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatDelta(delta: number | null): string {
|
export function formatDelta(delta: number | null): string {
|
||||||
if (delta === null) return "--";
|
if (delta === null) return "--";
|
||||||
return `${delta >= 0 ? "+" : ""}${Math.round(delta)}`;
|
return `${delta >= 0 ? "+" : ""}${Math.round(delta)}`;
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,7 @@ function formatBolusLabel(value: number): string {
|
|||||||
return Number.isInteger(value) ? `${value}` : value.toFixed(1);
|
return Number.isInteger(value) ? `${value}` : value.toFixed(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawNightscoutGraph(
|
export function drawNightscoutGraph(
|
||||||
image: GrayImage,
|
image: GrayImage,
|
||||||
bounds: { x: number; y: number; width: number; height: number },
|
bounds: { x: number; y: number; width: number; height: number },
|
||||||
nightscout: NightscoutState,
|
nightscout: NightscoutState,
|
||||||
@@ -431,10 +431,10 @@ export function nightscoutMenuItems(): MenuItem[] {
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
|
export function isNightscoutPointStale(point: NightscoutState["latest"], nowMs: number): boolean {
|
||||||
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
|
return point !== null && nowMs - point.timestampMs > NIGHTSCOUT_STALE_MS;
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
|
export function drawNightscoutValueStrikeThrough(image: GrayImage, x: number, y: number, width: number): void {
|
||||||
image.drawLine(x, y, x + width, y, 180);
|
image.drawLine(x, y, x + width, y, 180);
|
||||||
}
|
}
|
||||||
@@ -43,6 +43,8 @@ const MIRROR_TOUCH_GESTURES: Record<Exclude<MirrorTouchKind, "tap">, WearRemoteI
|
|||||||
"swipe-right": "swipe-right",
|
"swipe-right": "swipe-right",
|
||||||
};
|
};
|
||||||
import { findSoundEffect, playSoundEffect } from "../ui/sound-effects";
|
import { findSoundEffect, playSoundEffect } from "../ui/sound-effects";
|
||||||
|
import { GlanceHost } from "./glance-host";
|
||||||
|
import { glanceEventForGesture, type GlanceEvent } from "./glance-state";
|
||||||
import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboarding-state";
|
import { isWelcomeSoundPending, setWelcomeSoundPending } from "../phone-ui/onboarding-state";
|
||||||
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||||
import { voiceControlBridge } from "../native/voice-control";
|
import { voiceControlBridge } from "../native/voice-control";
|
||||||
@@ -52,6 +54,7 @@ import { prepareFrameDraws } from "../graphics/glyph-wire";
|
|||||||
import { getDefaultMediumFont } from "../graphics/ui-fonts";
|
import { getDefaultMediumFont } from "../graphics/ui-fonts";
|
||||||
import { wrapText } from "../graphics/textwrap";
|
import { wrapText } from "../graphics/textwrap";
|
||||||
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
|
import { rawInputEventToInputEvent, shell, type ShellInputOutcome } from "../ui/shell/shell";
|
||||||
|
import { type InputEvent } from "../ui/gestures";
|
||||||
import { registerSystemTools } from "../assistant/system-tools";
|
import { registerSystemTools } from "../assistant/system-tools";
|
||||||
import { updateGlassesPresence } from "./glasses-presence";
|
import { updateGlassesPresence } from "./glasses-presence";
|
||||||
import { timerEngine } from "../apps/timer/timer-engine";
|
import { timerEngine } from "../apps/timer/timer-engine";
|
||||||
@@ -252,6 +255,26 @@ class DashboardController {
|
|||||||
private screenTimeoutTimer: ReturnType<typeof setInterval> | null = null;
|
private screenTimeoutTimer: ReturnType<typeof setInterval> | null = null;
|
||||||
private evenHubSuspendTimer: ReturnType<typeof setTimeout> | null = null;
|
private evenHubSuspendTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
private evenHubSessionSuspended = false;
|
private evenHubSessionSuspended = false;
|
||||||
|
/**
|
||||||
|
* The Glanceboard: the alternate sleep-time display, on its own surface
|
||||||
|
* above the shell. Fed sleep-time input from handleInputEvent; it uses the
|
||||||
|
* same wake barrier as the shell and hands the compositor back to the
|
||||||
|
* screen-off path when it hides.
|
||||||
|
*/
|
||||||
|
private readonly glance = new GlanceHost({
|
||||||
|
getDisplay: () => this.display,
|
||||||
|
getProvider: () => ALL_APPS.find((app) => app.glanceboard)?.glanceboard ?? null,
|
||||||
|
canShow: () => (this.phase === "connected" || this.isPreviewDisplayActive()) && !this.glassesLocked,
|
||||||
|
ensureSessionActive: (frameId) => this.ensureEvenHubSessionActive(frameId),
|
||||||
|
onHiddenWhileAsleep: () => {
|
||||||
|
if (!shell.isScreenOn()) this.handleScreenStateChanged(false);
|
||||||
|
},
|
||||||
|
onVisibilityChanged: () => {
|
||||||
|
this.schedulePreviewUpdate();
|
||||||
|
this.emit();
|
||||||
|
},
|
||||||
|
appendLog: (message) => this.appendLog(message),
|
||||||
|
});
|
||||||
private evenHubResumePromise: Promise<boolean> | null = null;
|
private evenHubResumePromise: Promise<boolean> | null = null;
|
||||||
/** Faceclaw's firmware at the required revision reported in; its extensions can be used. */
|
/** Faceclaw's firmware at the required revision reported in; its extensions can be used. */
|
||||||
private customFirmwareConfirmed = false;
|
private customFirmwareConfirmed = false;
|
||||||
@@ -359,6 +382,8 @@ class DashboardController {
|
|||||||
this.emit();
|
this.emit();
|
||||||
},
|
},
|
||||||
onScreenStateChanged: (on) => {
|
onScreenStateChanged: (on) => {
|
||||||
|
// Any wake of the regular UI replaces a showing Glanceboard.
|
||||||
|
if (on) this.glance.dismiss();
|
||||||
this.handleScreenStateChanged(on);
|
this.handleScreenStateChanged(on);
|
||||||
if (on) this.requestShellRender();
|
if (on) this.requestShellRender();
|
||||||
this.emit();
|
this.emit();
|
||||||
@@ -788,6 +813,7 @@ class DashboardController {
|
|||||||
if (
|
if (
|
||||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||||
shell.isScreenOn() ||
|
shell.isScreenOn() ||
|
||||||
|
this.glance.isVisible() ||
|
||||||
this.phase !== "connected" ||
|
this.phase !== "connected" ||
|
||||||
!this.communicator ||
|
!this.communicator ||
|
||||||
this.evenHubSessionSuspended
|
this.evenHubSessionSuspended
|
||||||
@@ -801,9 +827,11 @@ class DashboardController {
|
|||||||
if (
|
if (
|
||||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||||
shell.isScreenOn() ||
|
shell.isScreenOn() ||
|
||||||
|
this.glance.isVisible() ||
|
||||||
this.phase !== "connected" ||
|
this.phase !== "connected" ||
|
||||||
this.communicator !== communicator
|
this.communicator !== communicator
|
||||||
) {
|
) {
|
||||||
|
// A showing Glanceboard re-arms this when it hides.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -829,6 +857,7 @@ class DashboardController {
|
|||||||
if (
|
if (
|
||||||
!suspendEvenHubWhenScreenOffSetting.get() ||
|
!suspendEvenHubWhenScreenOffSetting.get() ||
|
||||||
shell.isScreenOn() ||
|
shell.isScreenOn() ||
|
||||||
|
this.glance.isVisible() ||
|
||||||
this.phase !== "connected" ||
|
this.phase !== "connected" ||
|
||||||
this.communicator !== communicator
|
this.communicator !== communicator
|
||||||
) {
|
) {
|
||||||
@@ -1250,6 +1279,7 @@ class DashboardController {
|
|||||||
const target = this.previewTarget;
|
const target = this.previewTarget;
|
||||||
if (!target) return;
|
if (!target) return;
|
||||||
this.previewTarget = null;
|
this.previewTarget = null;
|
||||||
|
this.glance.reset();
|
||||||
target.release();
|
target.release();
|
||||||
this.clearDashboardTimer();
|
this.clearDashboardTimer();
|
||||||
this.appendLog("Preview-only display released.");
|
this.appendLog("Preview-only display released.");
|
||||||
@@ -1294,6 +1324,7 @@ class DashboardController {
|
|||||||
this.customFirmwareConfirmed = false;
|
this.customFirmwareConfirmed = false;
|
||||||
this.faceclawWakeLeaseState = null;
|
this.faceclawWakeLeaseState = null;
|
||||||
this.lockSurfaceConfigured = false;
|
this.lockSurfaceConfigured = false;
|
||||||
|
this.glance.reset();
|
||||||
this.evenHubResumePromise = null;
|
this.evenHubResumePromise = null;
|
||||||
this.connectRunning = true;
|
this.connectRunning = true;
|
||||||
|
|
||||||
@@ -1549,6 +1580,7 @@ class DashboardController {
|
|||||||
}
|
}
|
||||||
this.communicator = null;
|
this.communicator = null;
|
||||||
this.lockSurfaceConfigured = false;
|
this.lockSurfaceConfigured = false;
|
||||||
|
this.glance.reset();
|
||||||
this.customFirmwareConfirmed = false;
|
this.customFirmwareConfirmed = false;
|
||||||
this.faceclawWakeLeaseState = null;
|
this.faceclawWakeLeaseState = null;
|
||||||
this.evenHubResumePromise = null;
|
this.evenHubResumePromise = null;
|
||||||
@@ -1683,6 +1715,7 @@ class DashboardController {
|
|||||||
const communicator = this.communicator;
|
const communicator = this.communicator;
|
||||||
this.communicator = null;
|
this.communicator = null;
|
||||||
this.lockSurfaceConfigured = false;
|
this.lockSurfaceConfigured = false;
|
||||||
|
this.glance.reset();
|
||||||
this.evenHubSessionSuspended = false;
|
this.evenHubSessionSuspended = false;
|
||||||
this.evenHubResumePromise = null;
|
this.evenHubResumePromise = null;
|
||||||
|
|
||||||
@@ -1756,14 +1789,14 @@ class DashboardController {
|
|||||||
* finger-down/finger-up (the watch) hold for as long as the user does.
|
* finger-down/finger-up (the watch) hold for as long as the user does.
|
||||||
*/
|
*/
|
||||||
async injectSyntheticRingInput(kind: WearRemoteInputKind, origin: SyntheticInputOrigin = "ring"): Promise<void> {
|
async injectSyntheticRingInput(kind: WearRemoteInputKind, origin: SyntheticInputOrigin = "ring"): Promise<void> {
|
||||||
// Match the ring while the display is dark: only a double-click wakes it,
|
// Match the ring while the display is dark: a double-click wakes it
|
||||||
// and that wake is handled by Shell.receiveInput. This check also protects
|
// (handled by Shell.receiveInput) and a tap or hold shows the
|
||||||
// against a watch acting on a stale state snapshot.
|
// Glanceboard; scrolls and the menu gesture mean nothing. This check also
|
||||||
|
// protects against a watch acting on a stale state snapshot.
|
||||||
if (
|
if (
|
||||||
origin === "watch" &&
|
origin === "watch" &&
|
||||||
!shell.isScreenOn() &&
|
!shell.isScreenOn() &&
|
||||||
kind !== "double-click" &&
|
(kind === "scroll-up" || kind === "scroll-down" || kind === "short-then-long-press")
|
||||||
kind !== "long-press-release"
|
|
||||||
) {
|
) {
|
||||||
this.appendLog(`${kind} (watch scheme) ignored while display is off`);
|
this.appendLog(`${kind} (watch scheme) ignored while display is off`);
|
||||||
return;
|
return;
|
||||||
@@ -1794,7 +1827,12 @@ class DashboardController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!shell.isScreenOn()) {
|
if (!shell.isScreenOn()) {
|
||||||
|
// Asleep, the mirror offers the sleep gestures: double-tap wakes, a
|
||||||
|
// tap shows the Glanceboard for its timeout. The mirror's hold has no
|
||||||
|
// real release (the synthetic long-press releases at once, which would
|
||||||
|
// only flash the board), so it counts as a tap here too.
|
||||||
if (kind === "double-tap") await this.injectSyntheticRingInput("double-click", "watch");
|
if (kind === "double-tap") await this.injectSyntheticRingInput("double-click", "watch");
|
||||||
|
else if (kind === "tap" || kind === "long-press") await this.injectSyntheticRingInput("click", "watch");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (kind !== "tap") {
|
if (kind !== "tap") {
|
||||||
@@ -2056,6 +2094,20 @@ class DashboardController {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!shell.isScreenOn()) {
|
||||||
|
// Sleep-time gestures belong to the Glanceboard: a tap or a hold
|
||||||
|
// shows it (the shell would ignore them), and a double-tap while it
|
||||||
|
// is up dismisses it and falls through to the normal wake below.
|
||||||
|
const glanceEvent = this.glanceEventFor(inputEvent, event);
|
||||||
|
if (glanceEvent?.type === "dismiss") {
|
||||||
|
this.glance.dismiss();
|
||||||
|
} else if (glanceEvent) {
|
||||||
|
frameTimings.annotateFrame(frameId, `glanceboard ${glanceEvent.type}`);
|
||||||
|
await this.glance.handleEvent(glanceEvent, frameId);
|
||||||
|
frameOwned = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
const wakewordShouldWake =
|
const wakewordShouldWake =
|
||||||
event.kind === "even-ai" &&
|
event.kind === "even-ai" &&
|
||||||
event.eventType === EvenAIStatus.EVEN_AI_WAKE_UP &&
|
event.eventType === EvenAIStatus.EVEN_AI_WAKE_UP &&
|
||||||
@@ -2129,6 +2181,20 @@ class DashboardController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Glanceboard event a sleep-time input means, or null when the shell
|
||||||
|
* should see the input as usual. A display-wake carries the head-tilt
|
||||||
|
* gesture (HEAD_UP_EVENT) as a press; the double-tap display-wake stays the
|
||||||
|
* regular UI's wake.
|
||||||
|
*/
|
||||||
|
private glanceEventFor(inputEvent: InputEvent, event: RawInputEvent): GlanceEvent | null {
|
||||||
|
if (!this.glance.isEnabled()) return null;
|
||||||
|
if (inputEvent.type === "display-wake") {
|
||||||
|
return event.eventType === OsEventTypeList.HEAD_UP_EVENT ? { type: "press" } : null;
|
||||||
|
}
|
||||||
|
return glanceEventForGesture(inputEvent.type, this.glance.isVisible());
|
||||||
|
}
|
||||||
|
|
||||||
/** Launch or focus an in-process singleton app (notifications, debug tests). */
|
/** Launch or focus an in-process singleton app (notifications, debug tests). */
|
||||||
private async launchInProcessApp(
|
private async launchInProcessApp(
|
||||||
windowId: string,
|
windowId: string,
|
||||||
|
|||||||
@@ -71,6 +71,12 @@ export const OsEventTypeList = {
|
|||||||
RING_LONG_PRESS_RELEASE_EVENT: 10,
|
RING_LONG_PRESS_RELEASE_EVENT: 10,
|
||||||
/** CFW extension: the 2.2.9 tap-then-hold Menu gesture. */
|
/** CFW extension: the 2.2.9 tap-then-hold Menu gesture. */
|
||||||
SHORT_THEN_LONG_PRESS_EVENT: 11,
|
SHORT_THEN_LONG_PRESS_EVENT: 11,
|
||||||
|
/**
|
||||||
|
* CFW extension (head-tilt wake branch): the IMU head-up gesture, forwarded
|
||||||
|
* while the display sleeps. Reaches the phone as a display-wake carrying
|
||||||
|
* this eventType; the glance host treats it as a press.
|
||||||
|
*/
|
||||||
|
HEAD_UP_EVENT: 12,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const OsEventTypeName: Record<number, string> = {
|
export const OsEventTypeName: Record<number, string> = {
|
||||||
@@ -86,4 +92,5 @@ export const OsEventTypeName: Record<number, string> = {
|
|||||||
9: "RING_LONG_PRESS_EVENT",
|
9: "RING_LONG_PRESS_EVENT",
|
||||||
10: "RING_LONG_PRESS_RELEASE_EVENT",
|
10: "RING_LONG_PRESS_RELEASE_EVENT",
|
||||||
11: "SHORT_THEN_LONG_PRESS_EVENT",
|
11: "SHORT_THEN_LONG_PRESS_EVENT",
|
||||||
|
12: "HEAD_UP_EVENT",
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,262 @@
|
|||||||
|
/**
|
||||||
|
* Shows the Glanceboard while the shell sleeps. The board is not a window:
|
||||||
|
* it lives on its own full-screen opaque compositor surface above the shell
|
||||||
|
* (below only the lock screen), so it never composites with the sidebar,
|
||||||
|
* top bar, or the sleeping app's retained frame. The host owns that surface,
|
||||||
|
* the visibility state machine (glance-state.ts), its auto-hide timer, and
|
||||||
|
* the board's render loop; the controller feeds it sleep-time input and the
|
||||||
|
* wake barrier, and takes the compositor back to blank when the board hides.
|
||||||
|
*/
|
||||||
|
import { GrayImage, G2_LENS_HEIGHT, G2_LENS_WIDTH } from "../graphics/image";
|
||||||
|
import { prepareFrameDraws } from "../graphics/glyph-wire";
|
||||||
|
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../graphics/plane";
|
||||||
|
import * as frameTimings from "../native/frame-timings";
|
||||||
|
import { type DisplayTarget } from "../native/preview-display";
|
||||||
|
import { minWindowTop } from "../ui/shell/geometry";
|
||||||
|
import { beginRenderPass, endRenderPass } from "../util/render-freshness";
|
||||||
|
import { type GlanceBoardInstance, type GlanceboardProvider } from "../apps/app-definition";
|
||||||
|
import { GLANCE_HIDDEN, reduceGlance, type GlanceEvent, type GlanceState } from "./glance-state";
|
||||||
|
|
||||||
|
export const GLANCE_SURFACE_ID = "glance";
|
||||||
|
/** Above the shell (1) and every window (0); below the lock screen (1000). */
|
||||||
|
export const GLANCE_SURFACE_Z_ORDER = 900;
|
||||||
|
|
||||||
|
export type GlanceHostOptions = {
|
||||||
|
getDisplay: () => DisplayTarget | null;
|
||||||
|
getProvider: () => GlanceboardProvider | null;
|
||||||
|
/** Whether a board may show right now (connected or preview, not locked). */
|
||||||
|
canShow: () => boolean;
|
||||||
|
/**
|
||||||
|
* The controller's wake barrier: restore the EvenHub session if it was
|
||||||
|
* suspended and unblank the compositor. Resolves once the frame is visible.
|
||||||
|
*/
|
||||||
|
ensureSessionActive: (frameId: number) => Promise<boolean>;
|
||||||
|
/**
|
||||||
|
* The board hid on its own (timer or release) with the shell still
|
||||||
|
* asleep: the controller re-blanks and re-arms the screen-off power save.
|
||||||
|
*/
|
||||||
|
onHiddenWhileAsleep: () => void;
|
||||||
|
/** Visibility changed either way (phone mirror refresh). */
|
||||||
|
onVisibilityChanged: () => void;
|
||||||
|
appendLog: (message: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export class GlanceHost {
|
||||||
|
private state: GlanceState = GLANCE_HIDDEN;
|
||||||
|
private board: GlanceBoardInstance | null = null;
|
||||||
|
/** Whether the surface is currently visible on the compositor. */
|
||||||
|
private shown = false;
|
||||||
|
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
private configuredFor: DisplayTarget | null = null;
|
||||||
|
/** Serializes show/hide against each other and against in-flight renders. */
|
||||||
|
private queue: Promise<void> = Promise.resolve();
|
||||||
|
private rendering = false;
|
||||||
|
private renderPending = false;
|
||||||
|
private nextRenderWantsFreshData = false;
|
||||||
|
|
||||||
|
constructor(private readonly options: GlanceHostOptions) {}
|
||||||
|
|
||||||
|
isVisible(): boolean {
|
||||||
|
return this.state.visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Whether sleep-time gestures should reach the board at all. */
|
||||||
|
isEnabled(): boolean {
|
||||||
|
return this.options.getProvider()?.isEnabled() ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply a glance event from sleep-time input. The frame is owned from here:
|
||||||
|
* a show submits it with the board's first frame, anything else finishes it.
|
||||||
|
*/
|
||||||
|
async handleEvent(event: GlanceEvent, frameId: number): Promise<void> {
|
||||||
|
const before = this.state;
|
||||||
|
this.state = reduceGlance(before, event, Date.now());
|
||||||
|
this.armHideTimer();
|
||||||
|
if (this.state.visible === before.visible) {
|
||||||
|
frameTimings.finishFrame(frameId, this.state.visible ? "glanceboard stays up" : "glance event while hidden");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.state.visible) {
|
||||||
|
await this.enqueue(() => this.show(frameId));
|
||||||
|
} else {
|
||||||
|
frameTimings.finishFrame(frameId, "glanceboard hidden");
|
||||||
|
await this.enqueue(() => this.hide(true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hide because the regular UI is waking (double-tap, a notification, ...):
|
||||||
|
* the shell's own wake path handles the compositor from here, so no re-blank.
|
||||||
|
*/
|
||||||
|
dismiss(): void {
|
||||||
|
if (!this.state.visible && !this.shown && !this.board) return;
|
||||||
|
this.state = GLANCE_HIDDEN;
|
||||||
|
this.armHideTimer();
|
||||||
|
void this.enqueue(() => this.hide(false));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The display target went away (disconnect, preview teardown): forget everything. */
|
||||||
|
reset(): void {
|
||||||
|
this.state = GLANCE_HIDDEN;
|
||||||
|
this.armHideTimer();
|
||||||
|
this.board?.stop();
|
||||||
|
this.board = null;
|
||||||
|
this.shown = false;
|
||||||
|
this.configuredFor = null;
|
||||||
|
this.renderPending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enqueue(operation: () => Promise<void>): Promise<void> {
|
||||||
|
const run = this.queue.then(operation, operation).catch((error) => {
|
||||||
|
this.options.appendLog(`glanceboard operation failed: ${String(error)}`);
|
||||||
|
});
|
||||||
|
this.queue = run;
|
||||||
|
return run;
|
||||||
|
}
|
||||||
|
|
||||||
|
private armHideTimer(): void {
|
||||||
|
if (this.hideTimer !== null) clearTimeout(this.hideTimer);
|
||||||
|
this.hideTimer = null;
|
||||||
|
const hideAtMs = this.state.hideAtMs;
|
||||||
|
if (!this.state.visible || hideAtMs === null) return;
|
||||||
|
this.hideTimer = setTimeout(() => {
|
||||||
|
this.hideTimer = null;
|
||||||
|
void this.handleEvent({ type: "timeout" }, frameTimings.startFrame("glanceboard timeout"));
|
||||||
|
}, Math.max(0, hideAtMs - Date.now()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private async configureSurface(display: DisplayTarget): Promise<void> {
|
||||||
|
await display.configureSurface(GLANCE_SURFACE_ID, {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: G2_LENS_WIDTH,
|
||||||
|
height: G2_LENS_HEIGHT,
|
||||||
|
zOrder: GLANCE_SURFACE_Z_ORDER,
|
||||||
|
transparency: "opaque",
|
||||||
|
});
|
||||||
|
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
|
||||||
|
this.configuredFor = display;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async show(frameId: number): Promise<void> {
|
||||||
|
if (!this.state.visible) {
|
||||||
|
// Hidden again before this ran (a quick release); nothing to do.
|
||||||
|
frameTimings.finishFrame(frameId, "glanceboard show superseded");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const display = this.options.getDisplay();
|
||||||
|
const provider = this.options.getProvider();
|
||||||
|
if (!display || !provider || !this.options.canShow()) {
|
||||||
|
this.state = GLANCE_HIDDEN;
|
||||||
|
this.armHideTimer();
|
||||||
|
frameTimings.finishFrame(frameId, "glanceboard unavailable");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.configuredFor !== display) await this.configureSurface(display);
|
||||||
|
if (!this.board) {
|
||||||
|
this.board = provider.createBoard(() => this.requestRender());
|
||||||
|
this.board.start();
|
||||||
|
}
|
||||||
|
// Submit the first frame and reveal the surface while the compositor is
|
||||||
|
// still blanked, so the unblank below composites the board on top of the
|
||||||
|
// sleeping UI in one step rather than flashing the app first.
|
||||||
|
await this.render(frameId);
|
||||||
|
await display.setSurfaceVisible(GLANCE_SURFACE_ID, true);
|
||||||
|
this.shown = true;
|
||||||
|
const ready = await this.options.ensureSessionActive(frameId);
|
||||||
|
if (!ready) this.options.appendLog("glanceboard: wake barrier did not complete");
|
||||||
|
this.options.onVisibilityChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async hide(stillAsleep: boolean): Promise<void> {
|
||||||
|
const wasShown = this.shown;
|
||||||
|
this.shown = false;
|
||||||
|
const display = this.options.getDisplay();
|
||||||
|
if (wasShown && display && this.configuredFor === display) {
|
||||||
|
// Blank before revealing what is under the board: hiding the surface
|
||||||
|
// first recomposites the sleeping UI's retained frames and transmits
|
||||||
|
// them, a visible flash of the main UI before the screen goes dark.
|
||||||
|
// Blanked, the surface change composites to the same black frame. (The
|
||||||
|
// controller's screen-off path below blanks again, harmlessly.)
|
||||||
|
if (stillAsleep) {
|
||||||
|
await display.setScreenBlanked(true);
|
||||||
|
}
|
||||||
|
await display.setSurfaceVisible(GLANCE_SURFACE_ID, false);
|
||||||
|
}
|
||||||
|
this.board?.stop();
|
||||||
|
this.board = null;
|
||||||
|
this.renderPending = false;
|
||||||
|
if (wasShown && stillAsleep) this.options.onHiddenWhileAsleep();
|
||||||
|
if (wasShown) this.options.onVisibilityChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private requestRender(): void {
|
||||||
|
if (!this.board) return;
|
||||||
|
if (this.rendering) {
|
||||||
|
this.renderPending = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void this.enqueue(() => this.render(frameTimings.startFrame("render:glanceboard")));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Paint the board into a full-screen frame and submit it to the surface. */
|
||||||
|
private async render(frameId: number): Promise<void> {
|
||||||
|
const board = this.board;
|
||||||
|
const display = this.options.getDisplay();
|
||||||
|
if (!board || !display || this.configuredFor !== display) {
|
||||||
|
frameTimings.finishFrame(frameId, "discarded: glanceboard not showing");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.rendering = true;
|
||||||
|
try {
|
||||||
|
frameTimings.annotateFrame(frameId, "glanceboard");
|
||||||
|
const wantFreshData = this.nextRenderWantsFreshData;
|
||||||
|
this.nextRenderWantsFreshData = false;
|
||||||
|
beginRenderPass(!wantFreshData);
|
||||||
|
const paintStartedAtMs = Date.now();
|
||||||
|
const frame = frameTimings.span(frameId, "paint", () =>
|
||||||
|
frameTimings.runWithFrame(frameId, () => {
|
||||||
|
const boardImage = board.paint();
|
||||||
|
const full = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||||
|
// Centred horizontally in the panel, in the standard window band
|
||||||
|
// vertically (Display > Vertical position).
|
||||||
|
const x = Math.max(0, ((G2_LENS_WIDTH - boardImage.width) / 2) | 0);
|
||||||
|
const y = Math.max(0, Math.min(G2_LENS_HEIGHT - boardImage.height, minWindowTop()));
|
||||||
|
boardImage.composeInto(full, x, y);
|
||||||
|
return full;
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const paintUsedStaleData = endRenderPass();
|
||||||
|
const paintMs = Date.now() - paintStartedAtMs;
|
||||||
|
const planes: Plane[] = [{ image: frame, x: 0, y: 0 }];
|
||||||
|
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
|
||||||
|
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||||
|
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||||
|
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
|
||||||
|
await frameTimings.spanAsync(frameId, "submit", () =>
|
||||||
|
display.submitSurfaceFrame(
|
||||||
|
GLANCE_SURFACE_ID,
|
||||||
|
buffer,
|
||||||
|
{ x: 0, y: 0, width: image.width, height: image.height },
|
||||||
|
fingerprint,
|
||||||
|
paintMs,
|
||||||
|
frameId,
|
||||||
|
preparedDraws,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (paintUsedStaleData) {
|
||||||
|
// Same contract as the windows: a paint that used cached data (the
|
||||||
|
// notification icons) gets one follow-up with fresh data.
|
||||||
|
this.nextRenderWantsFreshData = true;
|
||||||
|
this.renderPending = true;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
this.rendering = false;
|
||||||
|
if (this.renderPending && this.board) {
|
||||||
|
this.renderPending = false;
|
||||||
|
this.requestRender();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
/**
|
||||||
|
* The Glanceboard's visibility rules, as a pure reducer so they can be tested
|
||||||
|
* without the display plumbing. The board is an alternate sleep-time display:
|
||||||
|
* while the shell is asleep a press (single tap, or a head-tilt wake) shows it
|
||||||
|
* for a short while, a hold (long-press) shows it until the press is released,
|
||||||
|
* and a double-tap dismisses it in favour of the regular UI. GlanceHost feeds
|
||||||
|
* these events in and applies the resulting visibility to the compositor.
|
||||||
|
*
|
||||||
|
* No NativeScript imports: this module is compiled and run under plain node
|
||||||
|
* by tests/glance-state.test.cjs.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** How long a press keeps the board up; each further press restarts it. */
|
||||||
|
export const GLANCE_TIMEOUT_MS = 3000;
|
||||||
|
|
||||||
|
export type GlanceState = {
|
||||||
|
visible: boolean;
|
||||||
|
/** A long-press is holding the board up; the timer does not run meanwhile. */
|
||||||
|
holding: boolean;
|
||||||
|
/** When the board hides on its own (epoch ms), or null while held / hidden. */
|
||||||
|
hideAtMs: number | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GlanceEvent =
|
||||||
|
/** Single tap or head-tilt wake: show, and (re)start the auto-hide timer. */
|
||||||
|
| { type: "press" }
|
||||||
|
/** Long-press: show and keep showing until the release. */
|
||||||
|
| { type: "hold" }
|
||||||
|
/** The long-press ended. */
|
||||||
|
| { type: "release" }
|
||||||
|
/** The auto-hide timer fired. */
|
||||||
|
| { type: "timeout" }
|
||||||
|
/** Double-tap (or any other wake of the regular UI): hide now. */
|
||||||
|
| { type: "dismiss" };
|
||||||
|
|
||||||
|
export const GLANCE_HIDDEN: GlanceState = Object.freeze({ visible: false, holding: false, hideAtMs: null });
|
||||||
|
|
||||||
|
export function reduceGlance(state: GlanceState, event: GlanceEvent, nowMs: number): GlanceState {
|
||||||
|
switch (event.type) {
|
||||||
|
case "press":
|
||||||
|
// A tap during a hold changes nothing: the hold decides when it ends.
|
||||||
|
if (state.holding) return state;
|
||||||
|
return { visible: true, holding: false, hideAtMs: nowMs + GLANCE_TIMEOUT_MS };
|
||||||
|
case "hold":
|
||||||
|
return { visible: true, holding: true, hideAtMs: null };
|
||||||
|
case "release":
|
||||||
|
// Releases also follow presses the board was never holding for (a
|
||||||
|
// tap-then-hold, a release delivered late); those mean nothing here.
|
||||||
|
return state.holding ? GLANCE_HIDDEN : state;
|
||||||
|
case "timeout":
|
||||||
|
if (!state.visible || state.holding || state.hideAtMs === null || nowMs < state.hideAtMs) return state;
|
||||||
|
return GLANCE_HIDDEN;
|
||||||
|
case "dismiss":
|
||||||
|
return GLANCE_HIDDEN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Which glance event, if any, a shell input gesture means while the screen is
|
||||||
|
* off. A double-tap only concerns the board while it is showing (it then
|
||||||
|
* dismisses it before waking the regular UI); everything else the shell
|
||||||
|
* already handles or ignores while asleep.
|
||||||
|
*/
|
||||||
|
export function glanceEventForGesture(
|
||||||
|
gesture: "click" | "double-click" | "long-press" | "long-press-release" | "head-tilt" | string,
|
||||||
|
boardVisible: boolean,
|
||||||
|
): GlanceEvent | null {
|
||||||
|
switch (gesture) {
|
||||||
|
case "click":
|
||||||
|
case "head-tilt":
|
||||||
|
return { type: "press" };
|
||||||
|
case "long-press":
|
||||||
|
return { type: "hold" };
|
||||||
|
case "long-press-release":
|
||||||
|
return { type: "release" };
|
||||||
|
case "double-click":
|
||||||
|
return boardVisible ? { type: "dismiss" } : null;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,8 @@ const ICON_STROKE_WIDTH = 2;
|
|||||||
export const ICON_SVGS = {
|
export const ICON_SVGS = {
|
||||||
"message-circle":
|
"message-circle":
|
||||||
'<svg viewBox="0 0 24 24" fill="none"><path d="M21 11.5a8.4 8.4 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.4 8.4 0 0 1-3.8-.9L3 21l1.9-5.7a8.4 8.4 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.4 8.4 0 0 1 3.8-.9h.5a8.5 8.5 0 0 1 8 8z"/></svg>',
|
'<svg viewBox="0 0 24 24" fill="none"><path d="M21 11.5a8.4 8.4 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.4 8.4 0 0 1-3.8-.9L3 21l1.9-5.7a8.4 8.4 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.4 8.4 0 0 1 3.8-.9h.5a8.5 8.5 0 0 1 8 8z"/></svg>',
|
||||||
|
eye:
|
||||||
|
'<svg viewBox="0 0 24 24" fill="none"><path d="M2.062 12.348a1 1 0 0 1 0-.696 10.75 10.75 0 0 1 19.876 0 1 1 0 0 1 0 .696 10.75 10.75 0 0 1-19.876 0"/><circle cx="12" cy="12" r="3"/></svg>',
|
||||||
"layout-grid":
|
"layout-grid":
|
||||||
'<svg viewBox="0 0 24 24" fill="none"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></svg>',
|
'<svg viewBox="0 0 24 24" fill="none"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="14" y="3" rx="1"/><rect width="7" height="7" x="14" y="14" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/></svg>',
|
||||||
timer:
|
timer:
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Clock/date text shared by the shell's top bar and the Glanceboard's System
|
||||||
|
* Card, so the two never disagree on the time-format setting or the date
|
||||||
|
* spelling.
|
||||||
|
*/
|
||||||
|
import { timeFormatSetting } from "./dashboard-settings";
|
||||||
|
|
||||||
|
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
||||||
|
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
||||||
|
|
||||||
|
/** Format a clock time, honoring the 12/24-hour setting ("14:05" / "2:05 PM"). */
|
||||||
|
export function formatClockTime(now: Date): string {
|
||||||
|
const minutes = String(now.getMinutes()).padStart(2, "0");
|
||||||
|
const hour24 = now.getHours();
|
||||||
|
if (timeFormatSetting.get() === "12h") {
|
||||||
|
const hour12 = ((hour24 + 11) % 12) + 1;
|
||||||
|
return `${hour12}:${minutes} ${hour24 < 12 ? "AM" : "PM"}`;
|
||||||
|
}
|
||||||
|
return `${hour24}:${minutes}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Short date as the top bar shows it: "Thu 11 Sep". */
|
||||||
|
export function formatClockDate(now: Date): string {
|
||||||
|
return `${WEEKDAYS[now.getDay()]} ${now.getDate()} ${MONTHS[now.getMonth()]}`;
|
||||||
|
}
|
||||||
@@ -13,9 +13,9 @@ import {
|
|||||||
glassesBatteryVisibilitySetting,
|
glassesBatteryVisibilitySetting,
|
||||||
phoneBatteryVisibilitySetting,
|
phoneBatteryVisibilitySetting,
|
||||||
ringBatteryVisibilitySetting,
|
ringBatteryVisibilitySetting,
|
||||||
timeFormatSetting,
|
|
||||||
type BatteryIndicatorVisibility,
|
type BatteryIndicatorVisibility,
|
||||||
} from "../dashboard-settings";
|
} from "../dashboard-settings";
|
||||||
|
import { formatClockDate, formatClockTime } from "../clock-format";
|
||||||
import { getFont } from "../../graphics/bdffont";
|
import { getFont } from "../../graphics/bdffont";
|
||||||
import { Layer } from "../layers";
|
import { Layer } from "../layers";
|
||||||
import { scrollToKeepSelectionVisible } from "../menu";
|
import { scrollToKeepSelectionVisible } from "../menu";
|
||||||
@@ -92,8 +92,6 @@ export function sidebarContentLeft(windowCount: number): number {
|
|||||||
}
|
}
|
||||||
const NOTIFICATION_ICON_SIZE = 24;
|
const NOTIFICATION_ICON_SIZE = 24;
|
||||||
const BORDER_VALUE = 40;
|
const BORDER_VALUE = 40;
|
||||||
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
||||||
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
||||||
|
|
||||||
export type ShellChromeWindow = {
|
export type ShellChromeWindow = {
|
||||||
windowId: string;
|
windowId: string;
|
||||||
@@ -321,8 +319,7 @@ export class ShellChromeLayer implements Layer {
|
|||||||
image.drawLine(barLeft, barTop + TOP_BAR_HEIGHT - 1, G2_LENS_WIDTH - 1, barTop + TOP_BAR_HEIGHT - 1, BORDER_VALUE);
|
image.drawLine(barLeft, barTop + TOP_BAR_HEIGHT - 1, G2_LENS_WIDTH - 1, barTop + TOP_BAR_HEIGHT - 1, BORDER_VALUE);
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const clock = `${WEEKDAYS[now.getDay()]} ${now.getDate()} ${MONTHS[now.getMonth()]} ` +
|
const clock = `${formatClockDate(now)} ${formatClockTime(now)}`;
|
||||||
formatClockTime(now);
|
|
||||||
const clockX = barLeft + 10;
|
const clockX = barLeft + 10;
|
||||||
const textY = barTop + Math.max(0, ((TOP_BAR_HEIGHT - font.lineHeight) / 2) | 0);
|
const textY = barTop + Math.max(0, ((TOP_BAR_HEIGHT - font.lineHeight) / 2) | 0);
|
||||||
image.drawText(font, clockX, textY, clock, 210);
|
image.drawText(font, clockX, textY, clock, 210);
|
||||||
@@ -607,17 +604,6 @@ function tabLeftInset(yy: number, top: number, bottom: number, radius: number):
|
|||||||
return Math.max(0, Math.round(radius - Math.sqrt(Math.max(0, radius * radius - dy * dy))));
|
return Math.max(0, Math.round(radius - Math.sqrt(Math.max(0, radius * radius - dy * dy))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Format the top-bar clock time, honoring the 12/24-hour setting. */
|
|
||||||
function formatClockTime(now: Date): string {
|
|
||||||
const minutes = String(now.getMinutes()).padStart(2, "0");
|
|
||||||
const hour24 = now.getHours();
|
|
||||||
if (timeFormatSetting.get() === "12h") {
|
|
||||||
const hour12 = ((hour24 + 11) % 12) + 1;
|
|
||||||
return `${hour12}:${minutes} ${hour24 < 12 ? "AM" : "PM"}`;
|
|
||||||
}
|
|
||||||
return `${hour24}:${minutes}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Small triangle marker for sidebar overflow; direction -1 = up, 1 = down. */
|
/** Small triangle marker for sidebar overflow; direction -1 = up, 1 = down. */
|
||||||
function drawChevron(image: GrayImage, centerX: number, y: number, direction: -1 | 1): void {
|
function drawChevron(image: GrayImage, centerX: number, y: number, direction: -1 | 1): void {
|
||||||
const half = 5;
|
const half = 5;
|
||||||
|
|||||||
@@ -0,0 +1,356 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
const vm = require('node:vm');
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
function load(file, deps, globals = {}) {
|
||||||
|
const exports = {};
|
||||||
|
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||||
|
vm.runInNewContext(ts.transpileModule(source, {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||||
|
}).outputText, { exports, console, setTimeout, clearTimeout, ...globals, require(name) {
|
||||||
|
assert.ok(name in deps, `Unexpected import ${name} in ${file}`);
|
||||||
|
return deps[name];
|
||||||
|
} });
|
||||||
|
return exports;
|
||||||
|
}
|
||||||
|
const callbacks = () => ({ onTextDelta() {}, onToolActivity() {}, onTurnDone() {}, onError() {} });
|
||||||
|
const config = (model = 'terra', effort = 'low') => ({ kind: 'direct', llm: {
|
||||||
|
provider: model === 'sonnet' ? 'anthropic' : 'openai', selection: model, model, effort, apiKey: 'secret-key',
|
||||||
|
} });
|
||||||
|
function sessions() {
|
||||||
|
const turns = [];
|
||||||
|
let synchronousError = false;
|
||||||
|
const { AssistantSession } = load('app/assistant/session.ts', {
|
||||||
|
'../prompts': { ASSISTANT_SYSTEM_PROMPT_BASE: '', buildAssistantSystemPrompt: () => '', describeAssistantContext: () => '' },
|
||||||
|
'./bridge-client': { assistantBridge: {} },
|
||||||
|
'./direct-backend': { DirectAssistantBackend: class { runTurn(options) {
|
||||||
|
const turn = { ...options, cancelled: false };
|
||||||
|
turns.push(turn);
|
||||||
|
if (synchronousError) options.callbacks.onError('failed');
|
||||||
|
return { cancel() { turn.cancelled = true; } };
|
||||||
|
} } },
|
||||||
|
'./tool-registry': { toolRegistry: { listTools: () => [] } },
|
||||||
|
});
|
||||||
|
function done(text) {
|
||||||
|
const turn = turns.at(-1);
|
||||||
|
turn.messages.push({ role: 'assistant', content: text });
|
||||||
|
turn.callbacks.onTextDelta(text, text);
|
||||||
|
turn.callbacks.onTurnDone({ stopReason: 'end_turn' });
|
||||||
|
}
|
||||||
|
return { AssistantSession, turns, done, setError() { synchronousError = true; } };
|
||||||
|
}
|
||||||
|
function conversations(saved = '') {
|
||||||
|
const env = sessions();
|
||||||
|
const { AssistantConversations } = load('app/assistant/conversations.ts', {
|
||||||
|
'./session': { AssistantSession: env.AssistantSession },
|
||||||
|
'./models': { ASSISTANT_MODEL_VALUES: ['auto', 'terra', 'sonnet'] },
|
||||||
|
});
|
||||||
|
let stored = '';
|
||||||
|
const store = new AssistantConversations((model, effort) => config(model, effort), () => 'terra', (value) => { stored = value; }, saved);
|
||||||
|
return { ...env, store, saved: () => stored };
|
||||||
|
}
|
||||||
|
|
||||||
|
test('voice and chat reuse selected history; sessions isolate context and persist without credentials', () => {
|
||||||
|
const env = conversations();
|
||||||
|
const firstId = env.store.current().id;
|
||||||
|
const voice = env.store.ensureSession();
|
||||||
|
voice.sendUtterance('Remember the blue door', {}, callbacks());
|
||||||
|
assert.equal(env.store.create(), false);
|
||||||
|
env.done('I will remember.');
|
||||||
|
const chat = env.store.ensureSession();
|
||||||
|
assert.equal(chat, voice);
|
||||||
|
chat.sendUtterance('What color?', {}, callbacks());
|
||||||
|
assert.equal(env.turns.at(-1).messages[0].content, 'Remember the blue door');
|
||||||
|
assert.equal(chat.transcript[1].text, 'I will remember.');
|
||||||
|
env.done('Blue.');
|
||||||
|
assert.ok(env.store.create());
|
||||||
|
env.store.ensureSession().sendUtterance('A separate topic', {}, callbacks());
|
||||||
|
assert.equal(env.turns.at(-1).messages.length, 1);
|
||||||
|
env.done('New topic.');
|
||||||
|
assert.ok(env.store.select(firstId));
|
||||||
|
assert.equal(env.store.ensureSession(), voice);
|
||||||
|
const restored = conversations(env.saved());
|
||||||
|
assert.equal(restored.store.current().id, firstId);
|
||||||
|
assert.equal(restored.store.ensureSession().transcript.at(-1).text, 'Blue.');
|
||||||
|
assert.equal(env.saved().includes('secret-key'), false);
|
||||||
|
restored.store.ensureSession().sendUtterance('Continue after restart', {}, callbacks());
|
||||||
|
assert.equal(restored.turns.at(-1).messages[0].content, 'Remember the blue door');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('model and effort changes retain transcript and apply only to the chosen session', () => {
|
||||||
|
const env = conversations();
|
||||||
|
const original = env.store.current().id;
|
||||||
|
env.store.ensureSession().sendUtterance('hello', {}, callbacks());
|
||||||
|
assert.equal(env.store.configure('sonnet', 'high'), false);
|
||||||
|
env.done('hi');
|
||||||
|
env.store.configure('sonnet', 'high');
|
||||||
|
env.store.ensureSession().sendUtterance('follow up', {}, callbacks());
|
||||||
|
assert.equal(env.turns.at(-1).provider, 'anthropic');
|
||||||
|
assert.equal(env.turns.at(-1).effort, 'high');
|
||||||
|
assert.equal(env.turns.at(-1).messages[0].content, 'hello');
|
||||||
|
env.done('still here');
|
||||||
|
env.store.create();
|
||||||
|
assert.equal(env.store.current().model, 'terra');
|
||||||
|
assert.equal(env.store.current().reasoning, 'default');
|
||||||
|
env.store.select(original);
|
||||||
|
assert.equal(env.store.current().reasoning, 'high');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cancelled tools and late callbacks cannot mutate the next turn or its history', () => {
|
||||||
|
const env = sessions();
|
||||||
|
const session = new env.AssistantSession(config());
|
||||||
|
session.sendUtterance('first', {}, callbacks());
|
||||||
|
const old = env.turns[0];
|
||||||
|
old.callbacks.onTextDelta('partial', 'partial');
|
||||||
|
session.cancel();
|
||||||
|
session.sendUtterance('second', {}, callbacks());
|
||||||
|
old.messages.push({ role: 'user', content: [{ type: 'tool_result', tool_use_id: 'old', content: 'late' }] });
|
||||||
|
old.callbacks.onTextDelta('late', 'late');
|
||||||
|
old.callbacks.onTurnDone({ stopReason: 'end_turn' });
|
||||||
|
assert.equal(session.isTurnActive(), true);
|
||||||
|
assert.equal(session.transcript[1].text, 'partial');
|
||||||
|
assert.equal(JSON.stringify(env.turns[1].messages).includes('tool_result'), false);
|
||||||
|
env.done('new answer');
|
||||||
|
assert.equal(session.isTurnActive(), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('synchronous provider errors do not leave a permanently active turn', () => {
|
||||||
|
const env = sessions(); env.setError();
|
||||||
|
const session = new env.AssistantSession(config());
|
||||||
|
session.sendUtterance('hello', {}, callbacks());
|
||||||
|
assert.equal(session.isTurnActive(), false);
|
||||||
|
assert.equal(session.status, 'failed');
|
||||||
|
});
|
||||||
|
|
||||||
|
function draftEnv({ prepare = async () => true, start = async () => {}, initialStatus = 'Ready' } = {}) {
|
||||||
|
const transcripts = new Set(), statuses = new Set(), timers = new Map();
|
||||||
|
const sent = [];
|
||||||
|
let starts = 0, stops = 0, mic = false, timerId = 0;
|
||||||
|
const { VoiceDraft } = load('app/apps/ai-chat/voice-draft.ts', {
|
||||||
|
'../../native/voice-control': { voiceControlBridge: {
|
||||||
|
onTranscript(cb) { transcripts.add(cb); return () => transcripts.delete(cb); },
|
||||||
|
onStatus(cb) { statuses.add(cb); cb({ status: initialStatus }); return () => statuses.delete(cb); },
|
||||||
|
} },
|
||||||
|
'../../ui/shell/voice-activity': { voiceActivity: { setActive(value) { mic = value; } } },
|
||||||
|
}, {
|
||||||
|
setTimeout(cb) { timers.set(++timerId, cb); return timerId; },
|
||||||
|
clearTimeout(id) { timers.delete(id); },
|
||||||
|
});
|
||||||
|
const draft = new VoiceDraft({ startVoiceCapture: async () => { starts++; await start(); }, stopVoiceCapture: () => { stops++; } }, prepare, () => {}, (text) => sent.push(text));
|
||||||
|
return { draft, sent, starts: () => starts, stops: () => stops, mic: () => mic,
|
||||||
|
transcript(text, isFinal = false) { for (const cb of [...transcripts]) cb({ text, isFinal }); },
|
||||||
|
status(status) { for (const cb of [...statuses]) cb({ status }); },
|
||||||
|
timeout() { for (const cb of [...timers.values()]) cb(); },
|
||||||
|
subscriptions: () => transcripts.size + statuses.size,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
test('hold displays partials, release waits for final and sends exactly once', async () => {
|
||||||
|
const env = draftEnv();
|
||||||
|
await env.draft.start();
|
||||||
|
env.transcript('hello');
|
||||||
|
assert.equal(env.draft.text, 'hello');
|
||||||
|
assert.deepEqual(env.sent, []);
|
||||||
|
env.draft.release();
|
||||||
|
assert.equal(env.stops(), 1);
|
||||||
|
env.transcript('hello world', true);
|
||||||
|
env.timeout();
|
||||||
|
env.transcript('duplicate', true);
|
||||||
|
assert.deepEqual(env.sent, ['hello world']);
|
||||||
|
assert.equal(env.subscriptions(), 0);
|
||||||
|
assert.equal(env.mic(), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('release before permission resolves never starts a microphone', async () => {
|
||||||
|
let ready;
|
||||||
|
const env = draftEnv({ prepare: () => new Promise((resolve) => { ready = resolve; }) });
|
||||||
|
const start = env.draft.start();
|
||||||
|
env.draft.release(); ready(true); await start;
|
||||||
|
assert.equal(env.starts(), 0);
|
||||||
|
assert.equal(env.draft.active, false);
|
||||||
|
assert.deepEqual(env.sent, []);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('release during async mic startup stops capture after it starts', async () => {
|
||||||
|
let started;
|
||||||
|
const env = draftEnv({ start: () => new Promise((resolve) => { started = resolve; }) });
|
||||||
|
const pending = env.draft.start();
|
||||||
|
await new Promise(setImmediate);
|
||||||
|
env.transcript('quick message');
|
||||||
|
env.draft.release(); started(); await pending;
|
||||||
|
assert.equal(env.stops(), 1);
|
||||||
|
env.timeout();
|
||||||
|
assert.deepEqual(env.sent, ['quick message']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('empty capture, rejection and closing never submit stale speech', async () => {
|
||||||
|
for (const cancel of ['empty', 'rejection', 'close']) {
|
||||||
|
const env = draftEnv({ initialStatus: 'old error' });
|
||||||
|
await env.draft.start();
|
||||||
|
assert.equal(env.starts(), 1);
|
||||||
|
if (cancel !== 'empty') env.transcript('should not send');
|
||||||
|
if (cancel === 'rejection') env.status('Ignored — not your enrolled voice');
|
||||||
|
if (cancel === 'close') env.draft.cancel();
|
||||||
|
env.draft.release(); env.timeout(); env.transcript('late', true);
|
||||||
|
assert.deepEqual(env.sent, []);
|
||||||
|
assert.equal(env.subscriptions(), 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function shellEnv() {
|
||||||
|
const gestures = load('app/ui/gestures.ts', {});
|
||||||
|
class Image { constructor(width = 576, height = 260) { this.width = width; this.height = height; } }
|
||||||
|
const images = { GrayImage: Image, G2_LENS_WIDTH: 640, G2_LENS_HEIGHT: 480 };
|
||||||
|
const timings = { spanCurrent: (_, fn) => fn(), span: (_, __, fn) => fn(), spanAsync: (_, __, fn) => fn(),
|
||||||
|
startFrame: () => 0, annotateFrame() {}, finishFrame() {}, runWithFrame: (_, fn) => fn() };
|
||||||
|
const layers = load('app/ui/layers.ts', {
|
||||||
|
'../graphics/image': images, '../graphics/plane': { dimPlanes: (planes) => planes },
|
||||||
|
'../native/frame-timings': timings, './gestures': gestures,
|
||||||
|
});
|
||||||
|
class Menu {
|
||||||
|
constructor(title, items) { this.title = title; this.items = items; this.selectedIndex = 0; }
|
||||||
|
selectItem(index) { this.selectedIndex = index; return this; }
|
||||||
|
paint(_ctx, below) { return below(); }
|
||||||
|
handleInput(event, ctx) { if (event.type === 'double-click') ctx.stack.pop(); }
|
||||||
|
}
|
||||||
|
const settings = { wakeWordActionSetting: { get: () => 'voice-input' }, brightnessSetting: { get: () => 'auto' } };
|
||||||
|
const { shell } = load('app/ui/shell/shell.ts', {
|
||||||
|
'../../graphics/image': images, '../../graphics/plane': {}, '../../graphics/ui-fonts': {}, '../../g2/events': {},
|
||||||
|
'../gestures': gestures, '../layers': layers, '../menu': { MenuLayer: Menu },
|
||||||
|
'./voice-input': {}, './keyboard-input': {}, './voice-activity': {}, './assistant': {},
|
||||||
|
'../../assistant/conversations': {}, '../../assistant/models': {}, '../../native/settings-store': {},
|
||||||
|
'../notifications': {}, '../dashboard-settings': settings, './ambient-cards': {},
|
||||||
|
'./chrome-layer': { ShellChromeLayer: class {} }, './modal-layer': {}, './tool-debug-layer': {},
|
||||||
|
'./brightness-picker-layer': {},
|
||||||
|
'../../assistant/tool-registry': {}, './geometry': { sidebarWidth: () => 64, minWindowTop: () => 96, TOP_BAR_HEIGHT: 28 },
|
||||||
|
});
|
||||||
|
const { createInProcessWindow } = load('app/ui/shell/in-process-window.ts', {
|
||||||
|
'../../graphics/image': images, '../../native/frame-timings': timings,
|
||||||
|
'../../util/render-freshness': { beginRenderPass() {}, endRenderPass: () => false },
|
||||||
|
'../layers': layers, '../window-menu': { WindowMenuLayer: Menu },
|
||||||
|
'./chrome-layer': { windowIcon: () => () => null }, './geometry': { appViewportSize: () => ({ width: 576, height: 260 }) },
|
||||||
|
'./shell': { shell },
|
||||||
|
});
|
||||||
|
const input = [], menus = [];
|
||||||
|
let microphone = false;
|
||||||
|
const app = createInProcessWindow({
|
||||||
|
appId: 'ai-chat', windowId: 'ai-chat', title: 'AI Chat', iconLetter: 'AI', closeable: true, holdToTalk: true,
|
||||||
|
baseLayer: { paint: () => new Image(), handleInput(event) {
|
||||||
|
input.push(event.type);
|
||||||
|
if (event.type === 'long-press') microphone = true;
|
||||||
|
if (event.type === 'long-press-release') microphone = false;
|
||||||
|
if (event.type === 'double-click') shell.yieldFocusToSidebar();
|
||||||
|
} },
|
||||||
|
menuItems: () => [{ label: 'New session', onSelect() {} }],
|
||||||
|
onAppMenuOpened() { menus.push('app'); microphone = false; },
|
||||||
|
onSystemMenuOpened() { menus.push('system'); microphone = false; },
|
||||||
|
isVoiceCapturing: () => microphone,
|
||||||
|
actions: layers.noopLayerActions, submitFrame: async () => {}, setSurfaceVisible() {},
|
||||||
|
});
|
||||||
|
shell.registerWindow(app.window);
|
||||||
|
shell.focusWindow('ai-chat');
|
||||||
|
return { shell, app, input, menus, microphone: () => microphone, event: (type) => gestures.makeInputEvent({ type }) };
|
||||||
|
}
|
||||||
|
|
||||||
|
test('chat holds bypass the escape timer; release, menus and double tap follow the requested routing', async () => {
|
||||||
|
const env = shellEnv();
|
||||||
|
await env.shell.receiveInput(env.event('long-press'));
|
||||||
|
assert.equal(env.microphone(), true);
|
||||||
|
assert.equal(env.shell.escapeMenuTimer, null);
|
||||||
|
await env.shell.receiveInput(env.event('long-press-release'));
|
||||||
|
assert.equal(env.microphone(), false);
|
||||||
|
assert.deepEqual(env.input, ['long-press', 'long-press-release']);
|
||||||
|
await env.shell.receiveInput(env.event('click'));
|
||||||
|
assert.deepEqual(env.menus, ['app']);
|
||||||
|
assert.equal(env.app.stack.isAtBase(), false);
|
||||||
|
await env.shell.receiveInput(env.event('short-then-long-press'));
|
||||||
|
assert.deepEqual(env.menus, ['app', 'system']);
|
||||||
|
assert.equal(env.app.stack.isAtBase(), true);
|
||||||
|
assert.equal(env.shell.stack.isAtBase(), false);
|
||||||
|
await env.shell.receiveInput(env.event('long-press-release'));
|
||||||
|
assert.deepEqual(env.input, ['long-press', 'long-press-release']);
|
||||||
|
env.shell.stack.clearToBase();
|
||||||
|
env.shell.focusWindow('ai-chat');
|
||||||
|
await env.shell.receiveInput(env.event('double-click'));
|
||||||
|
assert.equal(env.shell.isWindowFocused('ai-chat'), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ordinary apps retain long-press system menu and games retain the escape timer', async () => {
|
||||||
|
const env = shellEnv();
|
||||||
|
env.app.window.holdToTalk = false;
|
||||||
|
await env.shell.receiveInput(env.event('long-press'));
|
||||||
|
assert.deepEqual(env.menus, ['system']);
|
||||||
|
assert.deepEqual(env.input, []);
|
||||||
|
env.shell.stack.clearToBase();
|
||||||
|
env.app.window.claimsLongPress = () => true;
|
||||||
|
await env.shell.receiveInput(env.event('long-press'));
|
||||||
|
assert.notEqual(env.shell.escapeMenuTimer, null);
|
||||||
|
await env.shell.receiveInput(env.event('long-press-release'));
|
||||||
|
assert.equal(env.shell.escapeMenuTimer, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
function chatLayerEnv() {
|
||||||
|
const env = conversations();
|
||||||
|
const textwrap = load('app/graphics/textwrap.ts', {});
|
||||||
|
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
|
||||||
|
const graphics = load('app/graphics/image.ts', { './textwrap': textwrap });
|
||||||
|
const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u12n.bdf'), 'utf8'));
|
||||||
|
let windowOptions;
|
||||||
|
const { createAiChatWindow } = load('app/apps/ai-chat/ai-chat-app.ts', {
|
||||||
|
'../../graphics/image': graphics, '../../graphics/ui-fonts': { getDefaultSmallFont: () => font },
|
||||||
|
'../../graphics/textwrap': textwrap, '../../assistant/models': { assistantModelLabel: (s) => s },
|
||||||
|
'../../ui/dashboard-settings': { assistantBackendSetting: { get: () => 'direct' } },
|
||||||
|
'../../ui/gestures': load('app/ui/gestures.ts', {}), '../../ui/window-menu': {},
|
||||||
|
'../../ui/shell/in-process-window': { createInProcessWindow(options) {
|
||||||
|
windowOptions = options; return { requestRender() {} };
|
||||||
|
} },
|
||||||
|
'../../ui/shell/shell': { shell: { getAssistantConversations: () => env.store } },
|
||||||
|
'./voice-draft': { VoiceDraft: class { active = false; text = ''; status = ''; cancel() {} } },
|
||||||
|
});
|
||||||
|
createAiChatWindow({ actions: {}, setSurfaceVisible() {}, onClosed() {} });
|
||||||
|
const layer = windowOptions.baseLayer;
|
||||||
|
return { ...env, layer, paint: (width = 576, height = 260) => layer.paint({ stack: { getBaseSize: () => ({ width, height }) } }) };
|
||||||
|
}
|
||||||
|
|
||||||
|
test('scrollback stays anchored during streaming, follows at the bottom and resets on session switch', () => {
|
||||||
|
const env = chatLayerEnv();
|
||||||
|
const session = env.store.ensureSession();
|
||||||
|
for (let i = 0; i < 20; i++) session.transcript.push({ role: i % 2 ? 'assistant' : 'user', text: `Message ${i}: sample conversation text.` });
|
||||||
|
env.paint();
|
||||||
|
assert.ok(env.layer.maxFirstLine > 0);
|
||||||
|
env.layer.handleInput({ type: 'swipe-up' });
|
||||||
|
const first = env.layer.firstLine;
|
||||||
|
session.transcript.push({ role: 'assistant', text: 'New streamed text '.repeat(30) });
|
||||||
|
env.paint();
|
||||||
|
assert.equal(env.layer.firstLine, first);
|
||||||
|
for (let i = 0; i < 50; i++) env.layer.handleInput({ type: 'swipe-down' });
|
||||||
|
assert.equal(env.layer.firstLine, null);
|
||||||
|
env.layer.handleInput({ type: 'scroll-up' });
|
||||||
|
env.store.create(); env.paint();
|
||||||
|
assert.equal(env.layer.firstLine, null);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('chat paints at both window heights with pending speech pinned beneath history', () => {
|
||||||
|
const env = chatLayerEnv();
|
||||||
|
env.store.ensureSession().transcript.push({ role: 'user', text: 'Plan my afternoon.' }, {
|
||||||
|
role: 'assistant', text: 'You have a meeting at two. There is time for a walk beforehand. '.repeat(8),
|
||||||
|
});
|
||||||
|
env.layer.draft.active = true;
|
||||||
|
env.layer.draft.status = 'Listening... release to send';
|
||||||
|
env.layer.draft.text = 'Actually, leave some time to pick up lunch on the way.';
|
||||||
|
for (const [width, height] of [[576, 260], [576, 452], [640, 452]]) {
|
||||||
|
const image = env.paint(width, height);
|
||||||
|
assert.equal(image.width, width); assert.equal(image.height, height);
|
||||||
|
assert.equal(image.to8bppBuffer().length, width * height);
|
||||||
|
if (process.env.FACECLAW_CHAT_RENDER_DIR) {
|
||||||
|
const pixels = image.to8bppBuffer();
|
||||||
|
const rgba = new Uint8Array(pixels.length * 4);
|
||||||
|
for (let i = 0; i < pixels.length; i++) rgba.set([pixels[i], pixels[i], pixels[i], 255], i * 4);
|
||||||
|
const png = require('upng-js').encode([rgba.buffer], width, height, 0);
|
||||||
|
fs.writeFileSync(path.join(process.env.FACECLAW_CHAT_RENDER_DIR, `chat-${width}x${height}.png`), Buffer.from(png));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
const vm = require('node:vm');
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
function load(file, modules = {}) {
|
||||||
|
const source = fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||||
|
const context = { exports: {}, require: (name) => modules[name] ?? {}, console };
|
||||||
|
vm.runInNewContext(ts.transpileModule(source, {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||||
|
}).outputText, context, { filename: file });
|
||||||
|
return context.exports;
|
||||||
|
}
|
||||||
|
|
||||||
|
const textwrap = load('app/graphics/textwrap.ts');
|
||||||
|
const graphics = load('app/graphics/image.ts', { './textwrap': textwrap });
|
||||||
|
const plane = load('app/graphics/plane.ts', { './image': graphics });
|
||||||
|
const gestures = load('app/ui/gestures.ts');
|
||||||
|
const numeric = load('app/util/numeric-util.ts');
|
||||||
|
const { BdfFont } = load('app/graphics/bdffont.ts');
|
||||||
|
const font = BdfFont.parse(fs.readFileSync(path.join(__dirname, '../app/fonts/terminus/ter-u18n.bdf'), 'utf8'));
|
||||||
|
const fonts = { getDefaultSmallFont: () => font };
|
||||||
|
const layers = load('app/ui/layers.ts', {
|
||||||
|
'../graphics/image': graphics, '../graphics/plane': plane, './gestures': gestures,
|
||||||
|
'../native/frame-timings': { spanCurrent: (_name, paint) => paint() },
|
||||||
|
});
|
||||||
|
const menu = load('app/ui/menu.ts', {
|
||||||
|
'../graphics/image': graphics, '../graphics/ui-fonts': fonts, '../graphics/textwrap': textwrap,
|
||||||
|
'../util/numeric-util': numeric, './gestures': gestures, './metrics': load('app/ui/metrics.ts'),
|
||||||
|
});
|
||||||
|
|
||||||
|
function fixture(initial = '50', viewport = { x: 64, y: 124, width: 576, height: 260 }) {
|
||||||
|
let value = initial, writes = 0, closed = 0;
|
||||||
|
const settings = {
|
||||||
|
brightnessSetting: { get: () => value, set: (next) => { value = next; writes++; } },
|
||||||
|
brightnessSettingToLevel: (value) => value === 'auto' ? null : Number(value),
|
||||||
|
};
|
||||||
|
const geometry = {
|
||||||
|
appViewportRect: () => viewport, SHELL_OPAQUE_BLACK: 1,
|
||||||
|
sidebarWidth: () => viewport.x, minWindowTop: () => viewport.y - 28, TOP_BAR_HEIGHT: 28,
|
||||||
|
};
|
||||||
|
const picker = load('app/ui/shell/brightness-picker-layer.ts', {
|
||||||
|
'../../graphics/ui-fonts': fonts, '../../util/numeric-util': numeric,
|
||||||
|
'../dashboard-settings': settings, '../gestures': gestures, './geometry': geometry,
|
||||||
|
});
|
||||||
|
const base = { paint: () => new graphics.GrayImage(640, 480), handleInput() {} };
|
||||||
|
const stack = new layers.LayerStack(base, layers.noopLayerActions);
|
||||||
|
const open = () => stack.push(new picker.BrightnessPickerLayer(() => closed++));
|
||||||
|
open();
|
||||||
|
return { stack, open, settings, geometry, picker, value: () => value, writes: () => writes,
|
||||||
|
closed: () => closed, input: (type) => stack.handleInput(gestures.makeInputEvent({ type, source: 'watch' })) };
|
||||||
|
}
|
||||||
|
|
||||||
|
test('swipes change the saved level by 10, clamp at both ends, and both tap gestures confirm', async () => {
|
||||||
|
const f = fixture();
|
||||||
|
await f.input('swipe-up'); assert.equal(f.value(), '60');
|
||||||
|
await f.input('swipe-down'); assert.equal(f.value(), '50');
|
||||||
|
for (let i = 0; i < 8; i++) await f.input('scroll-up');
|
||||||
|
assert.equal(f.value(), '100'); assert.equal(f.writes(), 7);
|
||||||
|
await f.input('click'); assert.ok(f.stack.isAtBase()); assert.equal(f.closed(), 1);
|
||||||
|
f.open();
|
||||||
|
for (let i = 0; i < 12; i++) await f.input('scroll-down');
|
||||||
|
assert.equal(f.value(), '0'); assert.equal(f.writes(), 17);
|
||||||
|
await f.input('double-click'); assert.ok(f.stack.isAtBase()); assert.equal(f.closed(), 2);
|
||||||
|
assert.equal(f.value(), '0');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('enabling Auto while the picker is open prevents manual changes', async () => {
|
||||||
|
const f = fixture();
|
||||||
|
f.settings.brightnessSetting.set('auto');
|
||||||
|
await f.input('scroll-up'); await f.input('scroll-down');
|
||||||
|
assert.equal(f.value(), 'auto'); assert.equal(f.writes(), 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the thin bar fills upward in proportion to brightness in each viewport', () => {
|
||||||
|
for (const viewport of [
|
||||||
|
{ x: 64, y: 28, width: 576, height: 260 },
|
||||||
|
{ x: 64, y: 220, width: 576, height: 260 },
|
||||||
|
{ x: 0, y: 28, width: 640, height: 452 },
|
||||||
|
]) {
|
||||||
|
for (const level of [0, 10, 50, 100]) {
|
||||||
|
const f = fixture(String(level), viewport);
|
||||||
|
const image = f.stack.paint().at(-1).image;
|
||||||
|
const fill = [];
|
||||||
|
for (let y = 0; y < image.height; y++) {
|
||||||
|
for (let x = 0; x < image.width; x++) {
|
||||||
|
if (image.getPixel(x, y) === 255) fill.push({ x, y });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.equal(fill.length, 12 * level);
|
||||||
|
if (level) {
|
||||||
|
assert.equal(new Set(fill.map((p) => p.x)).size, 12);
|
||||||
|
assert.equal(new Set(fill.map((p) => p.y)).size, level);
|
||||||
|
assert.ok(fill.every((p) => p.x >= viewport.x && p.x < viewport.x + viewport.width
|
||||||
|
&& p.y >= viewport.y && p.y < viewport.y + viewport.height));
|
||||||
|
const bottom = fill.at(-1);
|
||||||
|
assert.equal(image.getPixel(bottom.x, bottom.y + 1), 1);
|
||||||
|
assert.equal(image.getPixel(bottom.x, bottom.y + 2), 150);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('system menu hides brightness in Auto and opens its picker between Voice input and Debug', async () => {
|
||||||
|
const f = fixture('auto');
|
||||||
|
const { shell } = load('app/ui/shell/shell.ts', {
|
||||||
|
'../../graphics/image': graphics, '../layers': layers, '../menu': menu, '../gestures': gestures,
|
||||||
|
'../dashboard-settings': f.settings, './geometry': f.geometry, './brightness-picker-layer': f.picker,
|
||||||
|
'./chrome-layer': { ShellChromeLayer: class {} },
|
||||||
|
});
|
||||||
|
shell.registerWindow({ windowId: 'test', appId: 'test', closeable: true, handleInput() {} });
|
||||||
|
shell.openSystemMenu('test');
|
||||||
|
let opened = shell.stack.layers.at(-1);
|
||||||
|
assert.deepEqual(Array.from(opened.items, (item) => item.label),
|
||||||
|
['Close window', 'Focus app switcher', 'Voice input', 'Debug']);
|
||||||
|
shell.stack.clearToBase();
|
||||||
|
f.settings.brightnessSetting.set('40');
|
||||||
|
shell.openSystemMenu('test');
|
||||||
|
opened = shell.stack.layers.at(-1);
|
||||||
|
assert.deepEqual(Array.from(opened.items, (item) => item.label),
|
||||||
|
['Close window', 'Focus app switcher', 'Voice input', 'Brightness', 'Debug']);
|
||||||
|
await shell.stack.handleInput(gestures.makeInputEvent({ type: 'scroll-down' }));
|
||||||
|
await shell.stack.handleInput(gestures.makeInputEvent({ type: 'scroll-down' }));
|
||||||
|
await shell.stack.handleInput(gestures.makeInputEvent({ type: 'click', source: 'ring' }));
|
||||||
|
assert.ok(shell.stack.topMatches((layer) => layer instanceof f.picker.BrightnessPickerLayer));
|
||||||
|
await shell.stack.handleInput(gestures.makeInputEvent({ type: 'double-click', source: 'ring' }));
|
||||||
|
assert.ok(shell.stack.isAtBase());
|
||||||
|
assert.equal(shell.focus, 'sidebar');
|
||||||
|
});
|
||||||
@@ -42,9 +42,14 @@ function compass(width = 576, height = 260) {
|
|||||||
RecordingImage.prototype.clone = function () {
|
RecordingImage.prototype.clone = function () {
|
||||||
const image = baseClone.call(this); Object.setPrototypeOf(image, RecordingImage.prototype); image.texts = []; return image;
|
const image = baseClone.call(this); Object.setPrototypeOf(image, RecordingImage.prototype); image.texts = []; return image;
|
||||||
};
|
};
|
||||||
|
const calibration = { isCompassCalibrated: () => true, normalizeHeading: (v) => v };
|
||||||
|
const rose = load('app/apps/compass/compass-rose.ts', {
|
||||||
|
'../../graphics/image': { GrayImage: RecordingImage }, './calibration': calibration,
|
||||||
|
});
|
||||||
const app = load('app/apps/compass/compass-app.ts', {
|
const app = load('app/apps/compass/compass-app.ts', {
|
||||||
'../../graphics/ui-fonts': { getDefaultSmallFont: () => small, getDefaultLargeFont: () => large },
|
'../../graphics/ui-fonts': { getDefaultSmallFont: () => small, getDefaultLargeFont: () => large },
|
||||||
'../../graphics/image': { GrayImage: RecordingImage }, '../../graphics/textwrap': textwrap,
|
'../../graphics/image': { GrayImage: RecordingImage }, '../../graphics/textwrap': textwrap,
|
||||||
|
'./compass-rose': rose,
|
||||||
'../../native/compass': { COMPASS_CHANGED: 15, COMPASS_CALIBRATION_STARTED: 16, COMPASS_CALIBRATION_COMPLETE: 17,
|
'../../native/compass': { COMPASS_CHANGED: 15, COMPASS_CALIBRATION_STARTED: 16, COMPASS_CALIBRATION_COMPLETE: 17,
|
||||||
addCompassListener: (fn) => { listener = fn; return () => {}; }, setCompassEnabled() {} },
|
addCompassListener: (fn) => { listener = fn; return () => {}; }, setCompassEnabled() {} },
|
||||||
'../../ui/metrics': { lineStep: (font) => font.lineHeight + 2 },
|
'../../ui/metrics': { lineStep: (font) => font.lineHeight + 2 },
|
||||||
@@ -55,7 +60,7 @@ function compass(width = 576, height = 260) {
|
|||||||
},
|
},
|
||||||
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
|
'../../ui/shell/shell': { shell: { isWindowVisible: () => true } },
|
||||||
'../../g2/android-permissions': { hasLocationPermission: () => false },
|
'../../g2/android-permissions': { hasLocationPermission: () => false },
|
||||||
'./calibration': { isCompassCalibrated: () => true, normalizeHeading: (v) => v },
|
'./calibration': calibration,
|
||||||
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
|
'./calibration-layer': {}, './declination': { onDeclinationChanged: () => () => {} },
|
||||||
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
|
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (v) => ({ displayDegrees: v }) },
|
||||||
'./debug': debug,
|
'./debug': debug,
|
||||||
|
|||||||
@@ -61,6 +61,10 @@ function compass({ style, width = 576, height = 260 } = {}) {
|
|||||||
'../../g2/android-permissions': { hasLocationPermission: () => false },
|
'../../g2/android-permissions': { hasLocationPermission: () => false },
|
||||||
'./calibration': { isCompassCalibrated: () => true, normalizeHeading: (n) => ((n % 360) + 360) % 360 },
|
'./calibration': { isCompassCalibrated: () => true, normalizeHeading: (n) => ((n % 360) + 360) % 360 },
|
||||||
'./calibration-layer': {},
|
'./calibration-layer': {},
|
||||||
|
'./compass-rose': load('app/apps/compass/compass-rose.ts', {
|
||||||
|
'../../graphics/image': { ...graphics, GrayImage: RecordingImage },
|
||||||
|
'./calibration': { normalizeHeading: (n) => ((n % 360) + 360) % 360 },
|
||||||
|
}),
|
||||||
'./declination': { onDeclinationChanged: () => () => {} },
|
'./declination': { onDeclinationChanged: () => () => {} },
|
||||||
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) },
|
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) },
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,6 +26,14 @@ const font = (size) => BdfFont.parse(fs.readFileSync(
|
|||||||
const small = font(12);
|
const small = font(12);
|
||||||
const large = font(24);
|
const large = font(24);
|
||||||
let viewportWidth = 576;
|
let viewportWidth = 576;
|
||||||
|
const calibration = { isCompassCalibrated: () => true, normalizeHeading: (n) => ((n % 360) + 360) % 360 };
|
||||||
|
// The rose renderer's private helpers are re-exported for referenceFrame.
|
||||||
|
const rose = load('app/apps/compass/compass-rose.ts', {
|
||||||
|
'../../graphics/image': graphics,
|
||||||
|
'./calibration': calibration,
|
||||||
|
}, `
|
||||||
|
export { rotatingRoseBounds, drawPlaneGrid, drawDiscWall, drawTiltedRing, drawHeadingTick };
|
||||||
|
`);
|
||||||
const compass = load('app/apps/compass/compass-app.ts', {
|
const compass = load('app/apps/compass/compass-app.ts', {
|
||||||
'../../graphics/ui-fonts': { getDefaultSmallFont: () => small, getDefaultLargeFont: () => large },
|
'../../graphics/ui-fonts': { getDefaultSmallFont: () => small, getDefaultLargeFont: () => large },
|
||||||
'../../graphics/image': graphics,
|
'../../graphics/image': graphics,
|
||||||
@@ -36,20 +44,26 @@ const compass = load('app/apps/compass/compass-app.ts', {
|
|||||||
'../../ui/shell/in-process-window': {},
|
'../../ui/shell/in-process-window': {},
|
||||||
'../../ui/shell/shell': {},
|
'../../ui/shell/shell': {},
|
||||||
'../../g2/android-permissions': {},
|
'../../g2/android-permissions': {},
|
||||||
'./calibration': { isCompassCalibrated: () => true, normalizeHeading: (n) => ((n % 360) + 360) % 360 },
|
'./calibration': calibration,
|
||||||
'./calibration-layer': {},
|
'./calibration-layer': {},
|
||||||
|
'./compass-rose': rose,
|
||||||
|
'./debug': { compassDebugLines: () => [], isCompassDebugEnabled: () => false, setCompassDebugEnabled: () => {} },
|
||||||
'./declination': {},
|
'./declination': {},
|
||||||
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) },
|
'./heading': { getNorthReference: () => 'magnetic', resolveHeading: (n) => ({ displayDegrees: n }) },
|
||||||
}, `
|
}, `
|
||||||
export { CompassLayer, rotatingRoseBounds, createCompassBackground, drawCompassRose };
|
export { CompassLayer };
|
||||||
|
const rose_1 = require('./compass-rose');
|
||||||
|
export const rotatingRoseBounds = rose_1.rotatingRoseBounds;
|
||||||
|
export const createCompassBackground = rose_1.createCompassBackground;
|
||||||
|
export const drawCompassRose = rose_1.drawCompassRose;
|
||||||
export function referenceFrame(width, height, cx, cy, radius, clipY, heading) {
|
export function referenceFrame(width, height, cx, cy, radius, clipY, heading) {
|
||||||
const image = new GrayImage(width, height, 0);
|
const image = new GrayImage(width, height, 0);
|
||||||
const fade = heading === null ? 0.45 : 1;
|
const fade = heading === null ? 0.45 : 1;
|
||||||
drawPlaneGrid(image, cx, cy, radius, clipY);
|
rose_1.drawPlaneGrid(image, cx, cy, radius, clipY);
|
||||||
drawDiscWall(image, cx, cy, radius, fade);
|
rose_1.drawDiscWall(image, cx, cy, radius, fade);
|
||||||
drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
rose_1.drawTiltedRing(image, cx, cy, radius, 105 * fade);
|
||||||
drawCompassRose(image, cx, cy, radius, heading);
|
rose_1.drawCompassRose(image, cx, cy, radius, heading);
|
||||||
drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
rose_1.drawHeadingTick(image, cx, cy, radius, 255 * fade);
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
// Pins the firmware-extension parsing and the compatibility / onboarding
|
||||||
|
// classification that the firmware check page and the main screen share.
|
||||||
|
const test = require("node:test");
|
||||||
|
const assert = require("node:assert/strict");
|
||||||
|
|
||||||
|
const compat = require("../.test-build/app/g2/firmware-compat.js");
|
||||||
|
|
||||||
|
const REQUIRED = compat.REQUIRED_FACECLAW_FIRMWARE_VERSION;
|
||||||
|
const info = (extension, left = "2.2.9.22", right = "2.2.9.22") => ({ leftVersion: left, rightVersion: right, extension });
|
||||||
|
|
||||||
|
test("parseFirmwareExtension recognizes each slot format", () => {
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension(""), { kind: "none" });
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension(" "), { kind: "none" });
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension("Faceclaw/1"), { kind: "faceclaw", version: 1 });
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension(" Faceclaw/12 "), { kind: "faceclaw", version: 12 });
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension("EVENCFW/22 img640 imgz rle"), {
|
||||||
|
kind: "legacy-faceclaw",
|
||||||
|
text: "EVENCFW/22 img640 imgz rle",
|
||||||
|
});
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension("EVENCFW"), { kind: "legacy-faceclaw", text: "EVENCFW" });
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension("OtherCFW/3"), { kind: "other", text: "OtherCFW/3" });
|
||||||
|
// A Faceclaw prefix without a usable number is not ours.
|
||||||
|
assert.deepEqual(compat.parseFirmwareExtension("Faceclaw/x"), { kind: "other", text: "Faceclaw/x" });
|
||||||
|
});
|
||||||
|
|
||||||
|
test("the required revision and newer revisions are compatible", () => {
|
||||||
|
assert.equal(compat.hasCompatibleFirmware(info(`Faceclaw/${REQUIRED}`)), true);
|
||||||
|
assert.equal(compat.firmwareIncompatibilityMessage(info(`Faceclaw/${REQUIRED}`)), null);
|
||||||
|
assert.equal(compat.hasCompatibleFirmware(info(`Faceclaw/${REQUIRED + 5}`)), true);
|
||||||
|
assert.equal(compat.firmwareIncompatibilityMessage(info(`Faceclaw/${REQUIRED + 5}`)), null);
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(info(`Faceclaw/${REQUIRED + 5}`)).kind, "custom");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("older Faceclaw revisions and legacy EVENCFW builds need a reflash", () => {
|
||||||
|
const older = info(`Faceclaw/${REQUIRED - 1}`);
|
||||||
|
assert.equal(compat.hasCompatibleFirmware(older), false);
|
||||||
|
assert.match(compat.firmwareIncompatibilityMessage(older), /revision 0/);
|
||||||
|
assert.match(compat.firmwareIncompatibilityMessage(older), new RegExp(`requires revision ${REQUIRED}`));
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(older).kind, "older-faceclaw");
|
||||||
|
|
||||||
|
const legacy = info("EVENCFW/22 img640 fbguard wearnotify");
|
||||||
|
assert.equal(compat.hasCompatibleFirmware(legacy), false);
|
||||||
|
assert.match(compat.firmwareIncompatibilityMessage(legacy), /older version of Faceclaw's custom firmware/);
|
||||||
|
const classified = compat.classifyOnboardingFirmware(legacy);
|
||||||
|
assert.equal(classified.kind, "older-faceclaw");
|
||||||
|
assert.equal(classified.version, "2.2.9.22");
|
||||||
|
assert.equal(classified.extension.kind, "legacy-faceclaw");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("custom firmware from another source is flashable with its own messaging", () => {
|
||||||
|
const other = info("OtherCFW/3", "2.2.9.22", "2.2.9.22");
|
||||||
|
assert.equal(compat.hasCompatibleFirmware(other), false);
|
||||||
|
assert.match(compat.firmwareIncompatibilityMessage(other), /different source/);
|
||||||
|
assert.match(compat.firmwareIncompatibilityMessage(other), /OtherCFW\/3/);
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(other).kind, "other-custom");
|
||||||
|
assert.match(compat.describeFirmwareExtension({ kind: "other", text: "OtherCFW/3" }), /different source/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("stock firmware classifies by base version and warns on the main screen", () => {
|
||||||
|
assert.equal(compat.firmwareIncompatibilityMessage(info("", "", "")), null, "nothing reported is not a verdict");
|
||||||
|
assert.match(compat.firmwareIncompatibilityMessage(info("")), /stock firmware/);
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(info("", "", "")).kind, "unknown");
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(info("", "2.2.9.22", "2.2.9.22")).kind, "flashable-stock");
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(info("", "2.2.4.34", "")).kind, "flashable-stock");
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(info("", "2.2.10.10", "2.2.10.10")).kind, "newer-stock-validated");
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(info("", "2.3.0.1", "2.3.0.1")).kind, "newer-stock-unvalidated");
|
||||||
|
// Faceclaw firmware on a newer base is still judged by its revision, not the base.
|
||||||
|
assert.equal(compat.classifyOnboardingFirmware(info(`Faceclaw/${REQUIRED}`, "2.3.0.1", "2.3.0.1")).kind, "custom");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("reportedFirmwareVersion picks the higher arm", () => {
|
||||||
|
assert.equal(compat.reportedFirmwareVersion(info("", "2.2.9.22", "2.2.10.10")), "2.2.10.10");
|
||||||
|
assert.equal(compat.reportedFirmwareVersion(info("", "", "2.2.9.22")), "2.2.9.22");
|
||||||
|
assert.equal(compat.reportedFirmwareVersion(info("", "", "")), "");
|
||||||
|
});
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package com.faceclaw.app;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class RingBatteryProtocolTest {
|
||||||
|
private static byte[] hex(String value) {
|
||||||
|
byte[] result = new byte[value.length() / 2];
|
||||||
|
for (int i = 0; i < result.length; ++i)
|
||||||
|
result[i] = (byte)Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
private static BleProtocol.RingBatterySnapshot parse(byte[] payload) {
|
||||||
|
byte[] frame = BleProtocol.framePb(payload, 9, BleProtocol.FLAG_NOTIFY, 15).get(0);
|
||||||
|
return BleProtocol.parseRingBattery(BleProtocol.parseFrame(frame).pb);
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Same golden payload as g2flash/tests/ring_battery_test.c.
|
||||||
|
byte[] packet = hex("08031000d206055242010752");
|
||||||
|
BleProtocol.RingBatterySnapshot result = parse(packet);
|
||||||
|
assert result.battery == 82 && result.charging == 1;
|
||||||
|
packet[10] = 3;
|
||||||
|
for (int level = 0; level <= 100; ++level) {
|
||||||
|
packet[11] = (byte)level;
|
||||||
|
result = parse(packet);
|
||||||
|
assert result.battery == level && result.charging == 0;
|
||||||
|
}
|
||||||
|
for (int flags : new int[] {0, 1}) {
|
||||||
|
packet[10] = (byte)flags; packet[11] = (byte)255;
|
||||||
|
result = parse(packet);
|
||||||
|
assert result.battery == -1 && result.charging == -1;
|
||||||
|
}
|
||||||
|
packet = hex("08031000d206055242010752");
|
||||||
|
for (int n = 0; n < packet.length; ++n)
|
||||||
|
assert parse(Arrays.copyOf(packet, n)) == null;
|
||||||
|
for (int offset : new int[] {7, 8, 9, 10, 11}) {
|
||||||
|
byte previous = packet[offset]; packet[offset] = (byte)255;
|
||||||
|
assert parse(packet) == null;
|
||||||
|
packet[offset] = previous;
|
||||||
|
}
|
||||||
|
assert parse(hex("08031000")) == null; // older firmware
|
||||||
|
assert parse(hex("d206055242010252")) == null; // valid without connected
|
||||||
|
assert parse(hex("d206055242010352")).battery == 82;
|
||||||
|
assert parse(hex("d2060552420100ff")).battery == -1;
|
||||||
|
// Invalid unknown value and charging-without-valid must be rejected.
|
||||||
|
assert parse(hex("d206055242010052")) == null;
|
||||||
|
assert parse(hex("d2060552420105ff")) == null;
|
||||||
|
assert parse(hex("d206055242010364")).battery == 100;
|
||||||
|
assert parse(hex("d206055242010365")) == null;
|
||||||
|
assert BleProtocol.parseRingBattery(null) == null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+59
@@ -0,0 +1,59 @@
|
|||||||
|
// Execute the production worker, replacing only NativeScript/platform services.
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
const vm = require('node:vm');
|
||||||
|
const ts = require('typescript');
|
||||||
|
const root = path.resolve(__dirname, '../..');
|
||||||
|
|
||||||
|
function load(file, modules = {}, extra = '', globals = {}) {
|
||||||
|
const context = { exports: {}, console, ...globals, require: (name) => {
|
||||||
|
if (!(name in modules)) throw new Error(`Unexpected dependency: ${name}`);
|
||||||
|
return modules[name];
|
||||||
|
} };
|
||||||
|
const source = fs.readFileSync(path.join(root, file), 'utf8');
|
||||||
|
vm.runInNewContext(ts.transpileModule(source, {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||||
|
}).outputText + extra, context, { filename: file });
|
||||||
|
return context.exports;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { BdfFont } = load('app/graphics/bdffont.ts', { '@nativescript/core': {} });
|
||||||
|
const font = (size) => BdfFont.parse(fs.readFileSync(path.join(root, `app/fonts/terminus/ter-u${size}n.bdf`), 'utf8'));
|
||||||
|
const fonts = { terminus24: font(24), terminus32: font(32) };
|
||||||
|
const smallFont = font(18);
|
||||||
|
const graphics = load('app/graphics/image.ts', { './textwrap': load('app/graphics/textwrap.ts') });
|
||||||
|
|
||||||
|
module.exports = function worker() {
|
||||||
|
let now = 10000;
|
||||||
|
let highScore = '0';
|
||||||
|
const noop = () => {};
|
||||||
|
const global = { postMessage: noop };
|
||||||
|
const api = load('app/apps/pinball/pinball-app.worker.ts', {
|
||||||
|
'@nativescript/core/globals': {},
|
||||||
|
'../../graphics/image': graphics,
|
||||||
|
'../../graphics/plane': {}, '../../graphics/glyph-wire': {},
|
||||||
|
'../../graphics/bdffont': { getFont: (name) => fonts[name] },
|
||||||
|
'../../graphics/ui-fonts': { getDefaultSmallFont: () => smallFont },
|
||||||
|
'../../native/frame-timings': { finishFrame: noop, logFrame: noop },
|
||||||
|
'../../native/active-display': {},
|
||||||
|
'../../native/settings-store': { getStringSetting: () => highScore, setStringSetting: (_, value) => { highScore = value; } },
|
||||||
|
'../../ui/sound-effects': {},
|
||||||
|
'../../ui/window-menu': { WindowMenu: class { open() {} } },
|
||||||
|
'../../ui/gestures': { directionalFallback: (event) => event, GESTURE_CLICK: '●', GESTURE_DOUBLE_CLICK: '●●', GESTURE_SCROLL: '↕', GESTURE_LONG_PRESS: '—' },
|
||||||
|
'../../util/numeric-util': { clamp: (v, lo, hi) => Math.max(lo, Math.min(hi, v)) },
|
||||||
|
}, `
|
||||||
|
renderAndSubmit = () => {};
|
||||||
|
playSfx = () => {};
|
||||||
|
exports.api = { windows, launchBall, ballDrained, stepPhysics, tick, flip, nudge,
|
||||||
|
checkTargets, checkRollovers, collideBumpers, resetGame, paintContent, TARGETS, ROLLOVERS };
|
||||||
|
`, { global, Date: { now: () => now }, setInterval: () => 1, clearInterval: noop }).api;
|
||||||
|
const message = (data) => global.onmessage({ data });
|
||||||
|
message({ type: 'open-window', windowId: 'test', surfaceId: 'test', title: 'Pinball', viewport: { width: 576, height: 260 } });
|
||||||
|
message({ type: 'foreground', windowId: 'test', foreground: true, focused: true });
|
||||||
|
const w = api.windows.get('test');
|
||||||
|
return { ...api, w, message, highScore: () => Number(highScore),
|
||||||
|
input: (type) => message({ type: 'input', windowId: 'test', focused: true, frameId: 1, event: { type } }),
|
||||||
|
advance: (seconds) => { for (let i = 0; i < Math.round(seconds * 120); i++) { now += 1000 / 120; api.stepPhysics(w); } },
|
||||||
|
elapse: (seconds) => { now += seconds * 1000; },
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
const test = require("node:test");
|
||||||
|
const assert = require("node:assert/strict");
|
||||||
|
|
||||||
|
const {
|
||||||
|
GLANCE_HIDDEN,
|
||||||
|
GLANCE_TIMEOUT_MS,
|
||||||
|
glanceEventForGesture,
|
||||||
|
reduceGlance,
|
||||||
|
} = require("../.test-build/app/g2/glance-state.js");
|
||||||
|
|
||||||
|
const T0 = 1_000_000;
|
||||||
|
|
||||||
|
test("a press shows the board and arms the auto-hide timer", () => {
|
||||||
|
const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0);
|
||||||
|
assert.deepEqual(shown, { visible: true, holding: false, hideAtMs: T0 + GLANCE_TIMEOUT_MS });
|
||||||
|
// A timer firing early is ignored; on time it hides.
|
||||||
|
assert.equal(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS - 1), shown);
|
||||||
|
assert.deepEqual(reduceGlance(shown, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), GLANCE_HIDDEN);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("a further press renews the timer from the new press", () => {
|
||||||
|
const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0);
|
||||||
|
const renewed = reduceGlance(shown, { type: "press" }, T0 + 2000);
|
||||||
|
assert.equal(renewed.hideAtMs, T0 + 2000 + GLANCE_TIMEOUT_MS);
|
||||||
|
assert.equal(reduceGlance(renewed, { type: "timeout" }, T0 + GLANCE_TIMEOUT_MS), renewed);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("a hold shows the board without a timer and the release hides it", () => {
|
||||||
|
const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0);
|
||||||
|
assert.deepEqual(held, { visible: true, holding: true, hideAtMs: null });
|
||||||
|
assert.equal(reduceGlance(held, { type: "timeout" }, T0 + 60_000), held);
|
||||||
|
// A tap mid-hold neither hides nor starts a timer.
|
||||||
|
assert.equal(reduceGlance(held, { type: "press" }, T0 + 500), held);
|
||||||
|
assert.deepEqual(reduceGlance(held, { type: "release" }, T0 + 1000), GLANCE_HIDDEN);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("a release that was not holding the board is ignored", () => {
|
||||||
|
assert.equal(reduceGlance(GLANCE_HIDDEN, { type: "release" }, T0), GLANCE_HIDDEN);
|
||||||
|
const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0);
|
||||||
|
assert.equal(reduceGlance(shown, { type: "release" }, T0 + 100), shown);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("dismiss hides from any state", () => {
|
||||||
|
const held = reduceGlance(GLANCE_HIDDEN, { type: "hold" }, T0);
|
||||||
|
assert.deepEqual(reduceGlance(held, { type: "dismiss" }, T0), GLANCE_HIDDEN);
|
||||||
|
const shown = reduceGlance(GLANCE_HIDDEN, { type: "press" }, T0);
|
||||||
|
assert.deepEqual(reduceGlance(shown, { type: "dismiss" }, T0), GLANCE_HIDDEN);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("gesture mapping: taps and head-tilt press, long-press holds, double-tap dismisses only a visible board", () => {
|
||||||
|
assert.deepEqual(glanceEventForGesture("click", false), { type: "press" });
|
||||||
|
assert.deepEqual(glanceEventForGesture("head-tilt", false), { type: "press" });
|
||||||
|
assert.deepEqual(glanceEventForGesture("long-press", false), { type: "hold" });
|
||||||
|
assert.deepEqual(glanceEventForGesture("long-press-release", true), { type: "release" });
|
||||||
|
assert.equal(glanceEventForGesture("double-click", false), null);
|
||||||
|
assert.deepEqual(glanceEventForGesture("double-click", true), { type: "dismiss" });
|
||||||
|
assert.equal(glanceEventForGesture("scroll-up", true), null);
|
||||||
|
assert.equal(glanceEventForGesture("short-then-long-press", true), null);
|
||||||
|
});
|
||||||
@@ -0,0 +1,231 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const os = require('node:os');
|
||||||
|
const path = require('node:path');
|
||||||
|
const vm = require('node:vm');
|
||||||
|
const { execFileSync } = require('node:child_process');
|
||||||
|
const ts = require('typescript');
|
||||||
|
|
||||||
|
const source = (file) => fs.readFileSync(path.join(__dirname, '..', file), 'utf8');
|
||||||
|
function load(file, requireModule, globals = {}) {
|
||||||
|
const context = { exports: {}, require: requireModule, console, ...globals };
|
||||||
|
vm.runInNewContext(ts.transpileModule(source(file), {
|
||||||
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
||||||
|
}).outputText, context);
|
||||||
|
return context.exports;
|
||||||
|
}
|
||||||
|
function store(settings = new Map()) {
|
||||||
|
const listeners = [];
|
||||||
|
const storage = {
|
||||||
|
getStringSetting: (key, fallback) => settings.get(key) ?? fallback,
|
||||||
|
setStringSetting: (key, value) => {
|
||||||
|
settings.set(key, value);
|
||||||
|
for (const listener of listeners) listener(key);
|
||||||
|
},
|
||||||
|
onSettingsStoreChanged: (listener) => { listeners.push(listener); return () => {}; },
|
||||||
|
};
|
||||||
|
return { prefs: load('app/native/media-apps.ts', () => storage), storage };
|
||||||
|
}
|
||||||
|
const chrome = { packageName: 'com.android.chrome', appName: 'Chrome' };
|
||||||
|
const player = { packageName: 'com.player', appName: 'Player' };
|
||||||
|
|
||||||
|
test('defaults apply before discovery; explicit choices survive restart, renames and rediscovery', () => {
|
||||||
|
const settings = new Map();
|
||||||
|
let { prefs } = store(settings);
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(chrome.packageName), false);
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(player.packageName), true);
|
||||||
|
assert.equal(prefs.isMediaAppEnabled('com.android.chrome.music'), true);
|
||||||
|
prefs.rememberMediaApps([chrome, player, player, { packageName: '', appName: 'Invalid' }]);
|
||||||
|
assert.equal(prefs.readMediaApps().length, 2);
|
||||||
|
prefs.setMediaAppEnabled({ ...chrome, title: 'Do not store metadata' }, true);
|
||||||
|
prefs.setMediaAppEnabled(player, false);
|
||||||
|
({ prefs } = store(settings));
|
||||||
|
prefs.rememberMediaApps([{ ...player, appName: 'Renamed player' }, chrome]);
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(chrome.packageName), true);
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(player.packageName), false);
|
||||||
|
assert.equal(prefs.readMediaApps().find((a) => a.packageName === player.packageName).appName, 'Renamed player');
|
||||||
|
assert.ok(!Array.from(settings.values()).join('').includes('Do not store metadata'));
|
||||||
|
prefs.rememberMediaApps([]);
|
||||||
|
assert.equal(prefs.readMediaApps().length, 2);
|
||||||
|
prefs.setMediaAppEnabled(player, true);
|
||||||
|
prefs.setMediaAppEnabled(chrome, false);
|
||||||
|
assert.equal(store(settings).prefs.isMediaAppEnabled(player.packageName), true);
|
||||||
|
assert.equal(store(settings).prefs.isMediaAppEnabled(chrome.packageName), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('malformed saved entries preserve defaults, and discovery does not freeze defaults', () => {
|
||||||
|
for (const raw of ['invalid', '{}', '[null, {}, {"packageName": 1}]',
|
||||||
|
'[{"packageName":"com.android.chrome","enabled":"true"}]']) {
|
||||||
|
const { prefs } = store(new Map([['music.apps', raw]]));
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(chrome.packageName), false);
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(player.packageName), true);
|
||||||
|
}
|
||||||
|
const { prefs } = store();
|
||||||
|
prefs.rememberMediaApps([chrome]);
|
||||||
|
assert.equal(prefs.readMediaApps()[0].enabled, undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
function bridges() {
|
||||||
|
const { prefs, storage } = store();
|
||||||
|
let browserListener, controllerListener, ignored, queries = 0, disconnects = 0, plays = 0;
|
||||||
|
const browsable = [chrome, player].map((app) => ({ ...app, serviceClass: `${app.packageName}.Browser` }));
|
||||||
|
class NativeBrowser {
|
||||||
|
setListener(listener) { browserListener = listener; }
|
||||||
|
listBrowsableAppsJson() { queries++; return JSON.stringify(browsable); }
|
||||||
|
connect(id) { browserListener.onConnectResult(id, true, 'root', ''); }
|
||||||
|
disconnect() { disconnects++; }
|
||||||
|
playFromMediaId() { plays++; }
|
||||||
|
}
|
||||||
|
class NativeController {
|
||||||
|
setIgnoredPackagesJson(json) { ignored = JSON.parse(json); }
|
||||||
|
setListener(listener) { controllerListener = listener; }
|
||||||
|
start() { assert.ok(ignored.includes(chrome.packageName)); }
|
||||||
|
}
|
||||||
|
const dependencies = {
|
||||||
|
'@nativescript/core': { Utils: { android: { getApplicationContext: () => ({}) } } },
|
||||||
|
'./media-apps': prefs,
|
||||||
|
'./settings-store': storage,
|
||||||
|
'../graphics/image': {},
|
||||||
|
'./image-files': {},
|
||||||
|
};
|
||||||
|
const globals = { global: { isAndroid: true }, com: { faceclaw: { app: {
|
||||||
|
FaceclawMediaBrowser: NativeBrowser,
|
||||||
|
FaceclawMediaBrowserListener: function(listener) { return listener; },
|
||||||
|
FaceclawMediaController: NativeController,
|
||||||
|
FaceclawMediaControllerListener: function(listener) { return listener; },
|
||||||
|
} } } };
|
||||||
|
const requireModule = (name) => { assert.ok(name in dependencies, name); return dependencies[name]; };
|
||||||
|
const browser = load('app/native/media-browser.ts', requireModule, globals).mediaBrowserBridge;
|
||||||
|
const controller = load('app/native/media-controller.ts', requireModule, globals).mediaControllerBridge;
|
||||||
|
return { prefs, browser, controller, browsable, queries: () => queries, disconnects: () => disconnects,
|
||||||
|
plays: () => plays, ignored: () => ignored, observed: (apps) => controllerListener.onSessionAppsChanged(JSON.stringify(apps)) };
|
||||||
|
}
|
||||||
|
|
||||||
|
test('browse discovery remembers ignored apps and cached results reflect toggles immediately', async () => {
|
||||||
|
const b = bridges();
|
||||||
|
assert.deepEqual(Array.from(b.browser.listBrowsableApps(), (a) => a.packageName), [player.packageName]);
|
||||||
|
assert.equal(b.prefs.readMediaApps().length, 2);
|
||||||
|
b.prefs.setMediaAppEnabled(chrome, true);
|
||||||
|
assert.equal(b.browser.listBrowsableApps().length, 2);
|
||||||
|
assert.equal(b.queries(), 1);
|
||||||
|
await b.browser.connect(b.browsable[0]);
|
||||||
|
b.browser.playFromMediaId('song');
|
||||||
|
assert.equal(b.plays(), 1);
|
||||||
|
b.prefs.setMediaAppEnabled(chrome, false);
|
||||||
|
assert.equal(b.disconnects(), 1);
|
||||||
|
b.browser.playFromMediaId('song');
|
||||||
|
assert.equal(b.plays(), 1);
|
||||||
|
await assert.rejects(b.browser.connect(b.browsable[0]), /ignored/);
|
||||||
|
b.prefs.setMediaAppEnabled(player, false);
|
||||||
|
assert.equal(b.browser.listBrowsableApps().length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('native controller gets defaults before start and records all session owners, retaining ended sessions', async () => {
|
||||||
|
const b = bridges();
|
||||||
|
await b.controller.start();
|
||||||
|
b.observed([chrome, player]);
|
||||||
|
b.observed([]);
|
||||||
|
assert.equal(b.prefs.readMediaApps().length, 2);
|
||||||
|
b.prefs.setMediaAppEnabled(chrome, true);
|
||||||
|
b.prefs.setMediaAppEnabled(player, false);
|
||||||
|
assert.ok(!b.ignored().includes(chrome.packageName));
|
||||||
|
assert.ok(b.ignored().includes(player.packageName));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Music settings keeps toggles and selection when a new app arrives and supports back', async () => {
|
||||||
|
const { prefs } = store();
|
||||||
|
let refreshes = 0, closes = 0;
|
||||||
|
const textwrap = load('app/graphics/textwrap.ts');
|
||||||
|
const graphics = load('app/graphics/image.ts', () => textwrap);
|
||||||
|
const { BdfFont } = load('app/graphics/bdffont.ts', () => ({}));
|
||||||
|
const font = BdfFont.parse(source('app/fonts/terminus/ter-u20n.bdf'));
|
||||||
|
class RecordingImage extends graphics.GrayImage {
|
||||||
|
texts = [];
|
||||||
|
drawText(font, x, y, text, value) { this.texts.push({ x, y, text }); super.drawText(font, x, y, text, value); }
|
||||||
|
}
|
||||||
|
const deps = {
|
||||||
|
'../graphics/image': { ...graphics, GrayImage: RecordingImage },
|
||||||
|
'../graphics/textwrap': textwrap,
|
||||||
|
'../graphics/ui-fonts': { getDefaultSmallFont: () => font },
|
||||||
|
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) },
|
||||||
|
'./metrics': load('app/ui/metrics.ts'), './gestures': {},
|
||||||
|
};
|
||||||
|
const menu = load('app/ui/menu.ts', (name) => deps[name]);
|
||||||
|
const uiDeps = {
|
||||||
|
'../../graphics/image': deps['../graphics/image'],
|
||||||
|
'../../graphics/textwrap': textwrap,
|
||||||
|
'../../graphics/ui-fonts': deps['../graphics/ui-fonts'],
|
||||||
|
'../../native/media-apps': prefs,
|
||||||
|
'../../native/media-browser': { mediaBrowserBridge: { listBrowsableApps: (refresh) => {
|
||||||
|
assert.equal(refresh, true); refreshes++; prefs.rememberMediaApps([player]); return [];
|
||||||
|
} } },
|
||||||
|
'../../ui/menu': menu,
|
||||||
|
};
|
||||||
|
const { MusicSettingsLayer } = load('app/apps/music/music-settings.ts', (name) => uiDeps[name]);
|
||||||
|
const layer = new MusicSettingsLayer();
|
||||||
|
const ctx = { stack: { getBaseSize: () => ({ width: 540, height: 224 }), isFocused: () => true, pop: () => closes++ } };
|
||||||
|
const paint = () => layer.paint(ctx, () => new RecordingImage(540, 224));
|
||||||
|
const input = (type) => layer.handleInput({ type }, ctx);
|
||||||
|
assert.equal(refreshes, 1);
|
||||||
|
paint();
|
||||||
|
await input('click');
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(player.packageName), false);
|
||||||
|
prefs.rememberMediaApps([{ packageName: 'com.alpha', appName: 'Alpha' }]);
|
||||||
|
const image = paint();
|
||||||
|
assert.ok(image.texts.some(({ text }) => text === 'Player'));
|
||||||
|
assert.ok(image.texts.some(({ text }) => text === 'Alpha'));
|
||||||
|
assert.ok(image.texts.every(({ y }) => y >= 0 && y + font.lineHeight <= image.height));
|
||||||
|
await input('click');
|
||||||
|
assert.equal(prefs.isMediaAppEnabled(player.packageName), true);
|
||||||
|
await input('double-click');
|
||||||
|
assert.equal(closes, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('native session selection skips ignored playing and idle apps, including the all-ignored case', () => {
|
||||||
|
// Execute the production selection method with lightweight media-session doubles.
|
||||||
|
const java = source('App_Resources/Android/src/main/java/com/faceclaw/app/FaceclawMediaController.java');
|
||||||
|
const method = java.slice(java.indexOf(' private MediaController chooseController('),
|
||||||
|
java.indexOf(' private void emitSessionAppsLocked('));
|
||||||
|
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-media-java-'));
|
||||||
|
const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name;
|
||||||
|
try {
|
||||||
|
const file = path.join(directory, 'MediaSelectionTest.java');
|
||||||
|
fs.writeFileSync(file, `import java.util.*;
|
||||||
|
public class MediaSelectionTest {
|
||||||
|
Set<String> ignoredPackages = new HashSet<>();
|
||||||
|
static class PlaybackState {
|
||||||
|
static final int STATE_PLAYING = 3;
|
||||||
|
int state; PlaybackState(int state) { this.state = state; } int getState() { return state; }
|
||||||
|
}
|
||||||
|
static class MediaController {
|
||||||
|
String name; PlaybackState state;
|
||||||
|
MediaController(String name, int state) { this.name = name; this.state = new PlaybackState(state); }
|
||||||
|
String getPackageName() { return name; } PlaybackState getPlaybackState() { return state; }
|
||||||
|
}
|
||||||
|
${method}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MediaSelectionTest test = new MediaSelectionTest();
|
||||||
|
MediaController ignored = new MediaController("browser", 3);
|
||||||
|
MediaController paused = new MediaController("player", 2);
|
||||||
|
MediaController playing = new MediaController("music", 3);
|
||||||
|
test.ignoredPackages.add("browser");
|
||||||
|
assert test.chooseController(Arrays.asList(ignored, paused)) == paused;
|
||||||
|
assert test.chooseController(Arrays.asList(ignored, paused, playing)) == playing;
|
||||||
|
assert test.chooseController(Arrays.asList(ignored)) == null;
|
||||||
|
ignored.state = null;
|
||||||
|
assert test.chooseController(Arrays.asList(ignored, paused)) == paused;
|
||||||
|
test.ignoredPackages.add("music");
|
||||||
|
assert test.chooseController(Arrays.asList(ignored, paused, playing)) == paused;
|
||||||
|
test.ignoredPackages.remove("browser"); ignored.state = new PlaybackState(3);
|
||||||
|
assert test.chooseController(Arrays.asList(ignored, paused)) == ignored;
|
||||||
|
assert test.chooseController(null) == null;
|
||||||
|
assert test.chooseController(Collections.emptyList()) == null;
|
||||||
|
}
|
||||||
|
}`);
|
||||||
|
execFileSync(javaBin('javac'), ['-d', directory, file], { stdio: 'pipe' });
|
||||||
|
execFileSync(javaBin('java'), ['-ea', '-cp', directory, 'MediaSelectionTest'], { stdio: 'pipe' });
|
||||||
|
} finally {
|
||||||
|
fs.rmSync(directory, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const worker = require('./fixtures/pinball-worker.cjs');
|
||||||
|
|
||||||
|
function lane(h, index) {
|
||||||
|
h.w.ballX = h.ROLLOVERS[index].x;
|
||||||
|
h.w.ballY = h.ROLLOVERS[index].y;
|
||||||
|
h.checkRollovers(h.w);
|
||||||
|
h.w.ballY = 60;
|
||||||
|
h.checkRollovers(h.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
function target(h, index) {
|
||||||
|
const t = h.TARGETS[index];
|
||||||
|
h.w.ballX = t.x + (index < 3 ? 7 : -7);
|
||||||
|
h.w.ballY = t.y;
|
||||||
|
h.w.ballVx = index < 3 ? -50 : 50;
|
||||||
|
h.checkTargets(h.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
test('skill shot awards only the marked first lane; rollovers debounce and raise multiplier to 5x', () => {
|
||||||
|
const h = worker();
|
||||||
|
h.launchBall(h.w);
|
||||||
|
lane(h, 0);
|
||||||
|
assert.equal(h.w.score, 800);
|
||||||
|
lane(h, 0);
|
||||||
|
assert.equal(h.w.score, 800);
|
||||||
|
lane(h, 1); lane(h, 2);
|
||||||
|
assert.equal(h.w.score, 1400);
|
||||||
|
assert.equal(h.w.multiplier, 2);
|
||||||
|
for (let round = 0; round < 6; round++) for (let i = 0; i < 3; i++) lane(h, i);
|
||||||
|
assert.equal(h.w.multiplier, 5);
|
||||||
|
const missed = worker();
|
||||||
|
missed.launchBall(missed.w);
|
||||||
|
lane(missed, 1); lane(missed, 0);
|
||||||
|
assert.equal(missed.w.score, 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('six distinct drop targets light a repeatable, escalating center jackpot', () => {
|
||||||
|
const h = worker();
|
||||||
|
h.launchBall(h.w);
|
||||||
|
for (let i = 0; i < 6; i++) { target(h, i); target(h, i); }
|
||||||
|
assert.equal(h.w.score, 1900);
|
||||||
|
assert.ok(h.w.targetsDown.every(Boolean));
|
||||||
|
h.w.ballX = 121; h.w.ballY = 110;
|
||||||
|
h.collideBumpers(h.w);
|
||||||
|
assert.equal(h.w.score, 4000);
|
||||||
|
assert.equal(h.w.jackpots, 1);
|
||||||
|
assert.ok(h.w.targetsDown.every((value) => !value));
|
||||||
|
for (let i = 0; i < 6; i++) target(h, i);
|
||||||
|
h.w.ballX = 121; h.w.ballY = 110;
|
||||||
|
h.collideBumpers(h.w);
|
||||||
|
assert.equal(h.w.score, 9000);
|
||||||
|
assert.equal(h.w.jackpots, 2);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ball saver preserves objectives once, then a drain pays multiplied bonus and resets the ball', () => {
|
||||||
|
const h = worker();
|
||||||
|
h.launchBall(h.w);
|
||||||
|
target(h, 0);
|
||||||
|
h.w.multiplier = 3;
|
||||||
|
h.ballDrained(h.w);
|
||||||
|
assert.equal(h.w.ballsLeft, 3);
|
||||||
|
assert.equal(h.w.ballState, 'ready');
|
||||||
|
assert.equal(h.w.targetsDown[0], true);
|
||||||
|
h.launchBall(h.w);
|
||||||
|
assert.equal(h.w.ballSaveRemaining, 0);
|
||||||
|
assert.equal(h.w.skillShotRemaining, 0);
|
||||||
|
h.ballDrained(h.w);
|
||||||
|
assert.equal(h.w.ballsLeft, 2);
|
||||||
|
assert.equal(h.w.lastBonus, 300);
|
||||||
|
assert.equal(h.w.score, 450);
|
||||||
|
assert.equal(h.highScore(), 450);
|
||||||
|
assert.equal(h.w.multiplier, 1);
|
||||||
|
assert.equal(h.w.skillLane, 1);
|
||||||
|
assert.ok(h.w.targetsDown.every((value) => !value));
|
||||||
|
h.launchBall(h.w);
|
||||||
|
assert.equal(h.w.ballSaveRemaining, 8);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('tilt disables awards, objective progress, saver and drain bonus', () => {
|
||||||
|
const h = worker();
|
||||||
|
h.launchBall(h.w); target(h, 0);
|
||||||
|
h.nudge(h.w); h.nudge(h.w); h.nudge(h.w);
|
||||||
|
assert.equal(h.w.tilted, true);
|
||||||
|
lane(h, 0); target(h, 1);
|
||||||
|
h.w.ballX = 121; h.w.ballY = 110; h.collideBumpers(h.w);
|
||||||
|
assert.equal(h.w.score, 150);
|
||||||
|
assert.equal(h.w.targetsDown[1], false);
|
||||||
|
assert.ok(h.w.rolloverLit.every((value) => !value));
|
||||||
|
h.ballDrained(h.w);
|
||||||
|
assert.equal(h.w.ballsLeft, 2);
|
||||||
|
assert.equal(h.w.lastBonus, 0);
|
||||||
|
assert.equal(h.w.score, 150);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('pause, background, screen-off and menus preserve gameplay timers', () => {
|
||||||
|
for (const suspend of [
|
||||||
|
(h) => h.input('double-click'),
|
||||||
|
(h) => h.input('short-then-long-press'),
|
||||||
|
(h) => h.message({ type: 'foreground', windowId: 'test', foreground: false, focused: false }),
|
||||||
|
(h) => h.message({ type: 'screen', on: false }),
|
||||||
|
]) {
|
||||||
|
const h = worker(); h.launchBall(h.w); suspend(h);
|
||||||
|
h.elapse(60); h.tick(h.w);
|
||||||
|
assert.equal(h.w.ballSaveRemaining, 8);
|
||||||
|
assert.equal(h.w.skillShotRemaining, 8);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('expired saver drains normally; final bonus is included in game over and restart keeps high score', () => {
|
||||||
|
const h = worker();
|
||||||
|
for (let i = 0; i < 3; i++) {
|
||||||
|
h.launchBall(h.w); target(h, 0);
|
||||||
|
h.w.ballSaveRemaining = 0;
|
||||||
|
h.ballDrained(h.w);
|
||||||
|
}
|
||||||
|
assert.equal(h.w.phase, 'game-over');
|
||||||
|
assert.equal(h.w.score, 750);
|
||||||
|
assert.equal(h.w.lastBonus, 100);
|
||||||
|
h.resetGame(h.w);
|
||||||
|
assert.equal(h.w.score, 0);
|
||||||
|
assert.equal(h.w.ballsLeft, 3);
|
||||||
|
assert.equal(h.w.lastBonus, 0);
|
||||||
|
assert.equal(h.w.highScore, 750);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('live simulation consumes skill and save time; wall-clock pauses do not', () => {
|
||||||
|
const h = worker(); h.launchBall(h.w);
|
||||||
|
h.advance(0.25);
|
||||||
|
assert.ok(Math.abs(h.w.ballSaveRemaining - 7.75) < 0.001);
|
||||||
|
assert.ok(Math.abs(h.w.skillShotRemaining - 7.75) < 0.001);
|
||||||
|
h.input('double-click'); h.elapse(90); h.input('click');
|
||||||
|
h.advance(0.25);
|
||||||
|
assert.ok(Math.abs(h.w.ballSaveRemaining - 7.5) < 0.001);
|
||||||
|
assert.ok(Math.abs(h.w.skillShotRemaining - 7.5) < 0.001);
|
||||||
|
h.w.skillShotRemaining = 0;
|
||||||
|
lane(h, 0);
|
||||||
|
assert.equal(h.w.score, 50);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('production physics stays finite and within the table at every launch power', () => {
|
||||||
|
for (let power = 1; power <= 5; power++) {
|
||||||
|
const h = worker(); h.w.launchPower = power; h.launchBall(h.w);
|
||||||
|
let enteredPlayfield = false;
|
||||||
|
for (let step = 0; step < 120 * 30 && h.w.ballState === 'live'; step++) {
|
||||||
|
if (h.w.ballVy > 0 && h.w.ballY > 195) for (const f of h.w.flippers) h.flip(h.w, f);
|
||||||
|
h.advance(1 / 120);
|
||||||
|
assert.ok(Number.isFinite(h.w.ballX) && Number.isFinite(h.w.ballY));
|
||||||
|
assert.ok(h.w.ballX >= 9 && h.w.ballX <= 261);
|
||||||
|
if (h.w.ballX < 226) enteredPlayfield = true;
|
||||||
|
}
|
||||||
|
assert.ok(enteredPlayfield, `power ${power} enters playfield`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('ready, live jackpot, paused and game-over frames render at the glasses viewport size', () => {
|
||||||
|
const h = worker();
|
||||||
|
for (const phase of ['playing', 'paused', 'game-over']) {
|
||||||
|
h.w.phase = phase;
|
||||||
|
for (const lit of [false, true]) {
|
||||||
|
h.w.targetsDown.fill(lit);
|
||||||
|
const image = h.paintContent(h.w);
|
||||||
|
assert.equal(image.width, 576);
|
||||||
|
assert.equal(image.height, 260);
|
||||||
|
assert.ok(image.withDrawsBaked().pixels.some((p) => p > 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const os = require('node:os');
|
||||||
|
const path = require('node:path');
|
||||||
|
const { execFileSync } = require('node:child_process');
|
||||||
|
|
||||||
|
test('ring battery decodes valid, unavailable, legacy, and malformed reports', () => {
|
||||||
|
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'faceclaw-ring-java-'));
|
||||||
|
const javaBin = (name) => process.env.JAVA_HOME ? path.join(process.env.JAVA_HOME, 'bin', name) : name;
|
||||||
|
try {
|
||||||
|
execFileSync(javaBin('javac'), ['-d', directory,
|
||||||
|
path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/g2protocol/BleProtocol.java'),
|
||||||
|
path.join(__dirname, '../App_Resources/Android/src/main/java/com/faceclaw/app/util/CollectionUtils.java'),
|
||||||
|
path.join(__dirname, 'fixtures/RingBatteryProtocolTest.java')], { stdio: 'pipe' });
|
||||||
|
execFileSync(javaBin('java'), ['-ea', '-cp', directory, 'com.faceclaw.app.RingBatteryProtocolTest'], { stdio: 'pipe' });
|
||||||
|
} finally {
|
||||||
|
fs.rmSync(directory, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
"../app/g2/even-advertisement.ts",
|
"../app/g2/even-advertisement.ts",
|
||||||
"../app/g2/glasses-artwork.ts",
|
"../app/g2/glasses-artwork.ts",
|
||||||
"../app/g2/glasses-display-state.ts",
|
"../app/g2/glasses-display-state.ts",
|
||||||
|
"../app/g2/glance-state.ts",
|
||||||
"../app/g2/pairing-candidates.ts",
|
"../app/g2/pairing-candidates.ts",
|
||||||
"../app/apps/calculator/math/*.ts",
|
"../app/apps/calculator/math/*.ts",
|
||||||
"../app/apps/microphones/mic-protocol.ts",
|
"../app/apps/microphones/mic-protocol.ts",
|
||||||
|
|||||||
Reference in new issue
Block a user