forked from Blendman974/faceclaw
Simplify glanceboard-trigger angle setting; fix incorrectly-rotated accelerometer readings on G1
The glanceboard head-tilt option was more complicated than necessary, because it wanted the user to take two angle measurements, a baseline angle and a tilt angle. That was a workaround to not knowing the accelerometer's orientation/calibration, but actually, we do know those. (Except, as it turns out, on the G1, the accelerometer orientation is different than it is on the G2, and there was a bug where EvenHub apps got their accelerometer readings in the wrong orientation. So for example the EvenHub Level app didn't work correctly.)
This commit is contained in:
1 parent
37d81d0308
commit
e09f8eae96
17 files changed
+255
-281
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { getDefaultMediumFont, getDefaultSmallFont } from "../../graphics/ui-fonts";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import { addImuListener, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
|
||||
import { addImuListener, imuPitchDegrees, imuSourceLabel, setImuReportEnabled, type ImuReading } from "../../native/imu";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { Layer, type LayerContext } from "../../ui/layers";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
@@ -23,7 +23,8 @@ const BAR_RIGHT_INSET = 4;
|
||||
/**
|
||||
* Accelerometer demo: turns the IMU stream on while this page is visible (the
|
||||
* Developer window is foregrounded and the screen is on), off otherwise, and
|
||||
* shows the latest x/y/z reading with a simple per-axis bar.
|
||||
* shows the latest x/y/z reading (Faceclaw's frame, imu-types.ts) with a
|
||||
* simple per-axis bar, and the head's pitch.
|
||||
*
|
||||
* The IMU stream is experimental — the firmware may never populate it — so the
|
||||
* page also reports how many samples have arrived.
|
||||
@@ -92,7 +93,8 @@ export class AccelerometerDemoLayer implements Layer {
|
||||
const bodyX = padding.left + BODY_INDENT;
|
||||
|
||||
image.drawText(small, padding.left, top, "Accelerometer", 220);
|
||||
const status = this.enabled ? "IMU on" : "IMU off";
|
||||
const pitch = this.reading ? `pitch ${Math.round(imuPitchDegrees(this.reading))}° ` : "";
|
||||
const status = pitch + (this.enabled ? "IMU on" : "IMU off");
|
||||
image.drawText(small, right - small.measureText(status), top, status, 150);
|
||||
|
||||
if (this.reading) {
|
||||
|
||||
@@ -5,16 +5,9 @@ import { enumSettingMenuItem, onAnySettingChanged, toggleSettingMenuItem } from
|
||||
import { uiFontOverridePickerMenuItem } from "../../ui/font-picker";
|
||||
import { type InputEvent } from "../../ui/gestures";
|
||||
import { type Layer, type LayerContext } from "../../ui/layers";
|
||||
import { drawRightValueMenuItem, drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu";
|
||||
import {
|
||||
captureHeadAngleStep,
|
||||
glassesHeadUpTrigger,
|
||||
headAngleCaptureRunning,
|
||||
headAngleCaptureStep,
|
||||
resetHeadAngleCapture,
|
||||
syncGlanceAngleOriginal,
|
||||
} from "../../g2/head-angle";
|
||||
import { getHeadUpStatus, headUpControlsAvailable } from "../../native/glasses-controls";
|
||||
import { drawSelectionHighlight, isMenuItemDisabled, MenuLayer, openModalMenu, submenuItem, type MenuItem } from "../../ui/menu";
|
||||
import { liveHeadPitchFooter } from "../../g2/head-angle";
|
||||
import { headUpControlsAvailable } from "../../native/glasses-controls";
|
||||
import { Menu } from "../../ui/menu-core";
|
||||
import { LIST_ROW_TEXT_INSET, lineStep, listRowHeight } from "../../ui/metrics";
|
||||
import { createInProcessWindow, type InProcessAppOptions, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
@@ -342,8 +335,13 @@ function openGlanceSettings(ctx: LayerContext): void {
|
||||
toggleSettingMenuItem(glanceShowOnHeadTiltSetting),
|
||||
enumSettingMenuItem(glanceTiltTimeoutSetting),
|
||||
// Only Android can change the glasses' head-up setting (glasses-controls.ts).
|
||||
// The controller's settings listener sends a new angle to the glasses.
|
||||
...(headUpControlsAvailable() ? [enumSettingMenuItem(glanceAngleSetting), headAngleCaptureMenuItem()] : []),
|
||||
// The controller's settings listener sends a new angle to the glasses; the
|
||||
// picker shows the head's current angle to choose by.
|
||||
...(headUpControlsAvailable()
|
||||
? [enumSettingMenuItem(glanceAngleSetting, {
|
||||
pickerFooter: liveHeadPitchFooter(() => shell.isWindowVisible(GLANCEBOARD_WINDOW_ID)),
|
||||
})]
|
||||
: []),
|
||||
toggleSettingMenuItem(glanceShowLinesSetting),
|
||||
uiFontOverridePickerMenuItem(glanceFont, {
|
||||
title: "Glanceboard font",
|
||||
@@ -353,56 +351,10 @@ function openGlanceSettings(ctx: LayerContext): void {
|
||||
enumSettingMenuItem(glanceDepthSetting),
|
||||
...widgetSettingsMenuItems(),
|
||||
];
|
||||
resetHeadAngleCapture();
|
||||
headAngleCapture = null;
|
||||
const layer = new MenuLayer("Glanceboard settings", items(), PAGE_MENU_LAYOUT);
|
||||
ctx.stack.push(layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* "Set to current head angle", in two selects: one with the head level, one
|
||||
* tilted up to where the display should wake (head-angle.ts). The row shows
|
||||
* the step or the last result; otherwise the glasses' current trigger. A
|
||||
* result is shown only while the Head tilt angle still has the value it was
|
||||
* shown with, and only until the menu is reopened.
|
||||
*/
|
||||
let headAngleCapture: { status: string; angleSetting: string } | null = null;
|
||||
|
||||
function headAngleCaptureMenuItem(): MenuItem {
|
||||
const label = "Set to current head angle";
|
||||
const setStatus = (status: string) => {
|
||||
headAngleCapture = { status, angleSetting: glanceAngleSetting.get() };
|
||||
};
|
||||
return {
|
||||
label,
|
||||
description:
|
||||
"Two steps. Look straight ahead, keep still and select; then tilt your head up to where the display should wake, keep still and select again. " +
|
||||
"Faceclaw reads the glasses' angle for about a second each time and saves the difference, rounded to 5°, as the Head tilt angle. Keep your head level side to side.",
|
||||
onSelect: async (ctx) => {
|
||||
// Selects during a measurement are ignored; it would share the IMU stream.
|
||||
if (headAngleCaptureRunning()) return;
|
||||
setStatus("Measuring...");
|
||||
ctx.actions.requestRender();
|
||||
const status = await captureHeadAngleStep();
|
||||
if (status !== null) setStatus(status);
|
||||
ctx.actions.requestRender();
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
const status = getHeadUpStatus();
|
||||
syncGlanceAngleOriginal(status);
|
||||
let value: string;
|
||||
if (headAngleCapture && headAngleCapture.angleSetting === glanceAngleSetting.get() &&
|
||||
(headAngleCaptureRunning() || headAngleCaptureStep() === "tilt" || !headAngleCapture.status.startsWith("Now"))) {
|
||||
value = headAngleCapture.status;
|
||||
} else {
|
||||
const current = glassesHeadUpTrigger(status);
|
||||
value = current === null ? "" : current === 0 ? "glasses: off" : `glasses: ${current}°`;
|
||||
}
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, label, value);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* One row per widget on the board, in slot order: a submenu of the widget's
|
||||
* settings, or a disabled row for a widget that has none.
|
||||
|
||||
@@ -173,7 +173,7 @@ export const glanceAngleSetting = new ConfigSettingEnum<GlanceAngle>({
|
||||
values: GLANCE_ANGLE_VALUES,
|
||||
formatValue: (value) => (value === "glasses" ? "Glasses default" : `${value}°`),
|
||||
description:
|
||||
"How far you tilt your head up to wake the display. Glasses default uses the glasses' own setting from the Even app. " +
|
||||
"How far you tilt your head up to wake the display; the list shows your head's angle now. Glasses default uses the glasses' own setting from the Even app. " +
|
||||
"The board hides when your head drops 20° below this angle; under 25°, looking straight ahead may not count, so it hides after the tap duration instead.",
|
||||
});
|
||||
|
||||
|
||||
+81
-82
@@ -1,97 +1,100 @@
|
||||
import { addImuListener, setImuReportEnabled, type ImuReading } from "../native/imu";
|
||||
import { addImuListener, imuPitchDegrees, setImuReportEnabled, type ImuReading } from "../native/imu";
|
||||
import { getHeadUpStatus, setHeadUpTrigger, type HeadUpStatus } from "../native/glasses-controls";
|
||||
import { getStringSetting, setStringSetting } from "../native/settings-store";
|
||||
import { glanceAngleSetting, glanceAngleTriggerDegrees, type GlanceAngle } from "../apps/glanceboard/glanceboard-settings";
|
||||
import { angleBetweenDegrees, glanceAngleForTilt, meanVector, type Vec3 } from "./head-tilt-math";
|
||||
import { glanceAngleTriggerDegrees } from "../apps/glanceboard/glanceboard-settings";
|
||||
|
||||
// Same ImuReportPace code the accelerometer demo uses (not literal Hz).
|
||||
const IMU_REPORT_PACE = 200;
|
||||
/** How long to average once samples start arriving. */
|
||||
const SAMPLE_WINDOW_MS = 1_200;
|
||||
/** Give up when the glasses send nothing for this long. */
|
||||
const FIRST_SAMPLE_TIMEOUT_MS = 3_000;
|
||||
const MIN_SAMPLES = 3;
|
||||
/** The level reading is used for a tilt reading taken within this long. */
|
||||
const LEVEL_READING_VALID_MS = 60_000;
|
||||
/** How often the live reading checks visibility and repaints a changed value. */
|
||||
const LIVE_TICK_MS = 250;
|
||||
/** Weight of each new sample in the smoothed pitch. */
|
||||
const LIVE_SMOOTHING = 0.3;
|
||||
/** A reading older than this (or none this long after the stream started) is shown as missing. */
|
||||
const LIVE_STALE_MS = 3_000;
|
||||
|
||||
/**
|
||||
* Average the glasses' accelerometer (gravity) over about a second, with the
|
||||
* report stream on only for that long.
|
||||
* The head's pitch, live, for the Head tilt angle picker. The IMU report
|
||||
* stream runs while `visible()` holds (checked every tick, so it stops while
|
||||
* the screen is off or the window is in the background) and until stop().
|
||||
* text() is the footer line; a changed value repaints at most once a tick.
|
||||
*/
|
||||
async function measureGravity(): Promise<{ ok: true; gravity: Vec3 } | { ok: false; reason: string }> {
|
||||
const samples: ImuReading[] = [];
|
||||
let firstAt = 0;
|
||||
const unsubscribe = addImuListener((reading) => {
|
||||
class LiveHeadPitch {
|
||||
private pitch: number | null = null;
|
||||
private lastSampleAt = 0;
|
||||
private streaming = false;
|
||||
private streamingSince = 0;
|
||||
private shown = "";
|
||||
private readonly unsubscribe: () => void;
|
||||
private readonly timer: ReturnType<typeof setInterval>;
|
||||
|
||||
constructor(
|
||||
private readonly visible: () => boolean,
|
||||
private readonly repaint: () => void,
|
||||
) {
|
||||
this.unsubscribe = addImuListener((reading) => this.onReading(reading));
|
||||
this.timer = setInterval(() => this.tick(), LIVE_TICK_MS);
|
||||
this.tick();
|
||||
}
|
||||
|
||||
text(): string {
|
||||
return this.shown;
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
clearInterval(this.timer);
|
||||
this.unsubscribe();
|
||||
if (this.streaming) setImuReportEnabled(false, IMU_REPORT_PACE);
|
||||
this.streaming = false;
|
||||
}
|
||||
|
||||
private onReading(reading: ImuReading): void {
|
||||
// Glasses temples only (1 right, 3 left, 0 unspecified); the ring is 2.
|
||||
if (reading.source === 2) return;
|
||||
if (!Number.isFinite(reading.x) || !Number.isFinite(reading.y) || !Number.isFinite(reading.z)) return;
|
||||
if (firstAt === 0) firstAt = Date.now();
|
||||
samples.push(reading);
|
||||
});
|
||||
setImuReportEnabled(true, IMU_REPORT_PACE);
|
||||
try {
|
||||
const start = Date.now();
|
||||
while (true) {
|
||||
await delay(100);
|
||||
const now = Date.now();
|
||||
if (firstAt === 0 && now - start >= FIRST_SAMPLE_TIMEOUT_MS) break;
|
||||
if (firstAt !== 0 && now - firstAt >= SAMPLE_WINDOW_MS) break;
|
||||
if (!this.streaming || reading.source === 2) return;
|
||||
if (!(Math.hypot(reading.x, reading.y, reading.z) > 1e-6)) return;
|
||||
const pitch = imuPitchDegrees(reading);
|
||||
this.pitch = this.pitch === null ? pitch : this.pitch + LIVE_SMOOTHING * (pitch - this.pitch);
|
||||
this.lastSampleAt = Date.now();
|
||||
}
|
||||
|
||||
private tick(): void {
|
||||
const now = Date.now();
|
||||
const visible = this.visible();
|
||||
if (visible !== this.streaming) {
|
||||
setImuReportEnabled(visible, IMU_REPORT_PACE);
|
||||
this.streaming = visible;
|
||||
this.streamingSince = now;
|
||||
this.pitch = null;
|
||||
}
|
||||
const text = this.format(now);
|
||||
if (text !== this.shown) {
|
||||
this.shown = text;
|
||||
this.repaint();
|
||||
}
|
||||
} finally {
|
||||
setImuReportEnabled(false, IMU_REPORT_PACE);
|
||||
unsubscribe();
|
||||
}
|
||||
if (samples.length < MIN_SAMPLES) {
|
||||
return { ok: false, reason: samples.length === 0 ? "no sensor data" : "too few samples" };
|
||||
|
||||
private format(now: number): string {
|
||||
if (this.pitch !== null && now - this.lastSampleAt < LIVE_STALE_MS) return `Head now: ${Math.round(this.pitch)}°`;
|
||||
return now - Math.max(this.streamingSince, this.lastSampleAt) < LIVE_STALE_MS ? "Head now: …" : "Head now: no data";
|
||||
}
|
||||
const gravity = meanVector(samples);
|
||||
return gravity ? { ok: true, gravity } : { ok: false, reason: "sensor reads zero" };
|
||||
}
|
||||
|
||||
let levelReading: { gravity: Vec3; atMs: number } | null = null;
|
||||
let measuring = false;
|
||||
|
||||
/** Which reading the next capture takes: the level one, or the tilted one after it. */
|
||||
export function headAngleCaptureStep(): "level" | "tilt" {
|
||||
return levelReading && Date.now() - levelReading.atMs < LEVEL_READING_VALID_MS ? "tilt" : "level";
|
||||
}
|
||||
|
||||
export function headAngleCaptureRunning(): boolean {
|
||||
return measuring;
|
||||
}
|
||||
|
||||
/**
|
||||
* One step of "Set to current head angle". The first call reads the head
|
||||
* level; the next (within a minute) reads it tilted up, stores the angle
|
||||
* between the two, rounded to 5°, as the Head tilt angle, and the
|
||||
* controller's settings listener sends it to the glasses. Returns a short
|
||||
* status for the menu row, or null when a capture is already running.
|
||||
* The Head tilt angle picker's footer: the head's current pitch, read while
|
||||
* the picker is open and `visible()` holds (enumSettingMenuItem pickerFooter).
|
||||
*/
|
||||
export async function captureHeadAngleStep(): Promise<string | null> {
|
||||
if (measuring) return null;
|
||||
measuring = true;
|
||||
try {
|
||||
const step = headAngleCaptureStep();
|
||||
const result = await measureGravity();
|
||||
if (result.ok === false) return `Failed: ${result.reason}`;
|
||||
if (step === "level") {
|
||||
levelReading = { gravity: result.gravity, atMs: Date.now() };
|
||||
return "Now tilt up, select";
|
||||
}
|
||||
const level = levelReading!.gravity;
|
||||
levelReading = null;
|
||||
const angle = glanceAngleForTilt(angleBetweenDegrees(level, result.gravity));
|
||||
if (angle.ok === false) return `Failed: ${angle.reason}`;
|
||||
glanceAngleSetting.set(String(angle.rounded) as GlanceAngle);
|
||||
return getHeadUpStatus() ? `Set to ${angle.rounded}°` : `Saved ${angle.rounded}° (offline)`;
|
||||
} finally {
|
||||
measuring = false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Forget a level reading (the settings menu was reopened). */
|
||||
export function resetHeadAngleCapture(): void {
|
||||
if (!measuring) levelReading = null;
|
||||
export function liveHeadPitchFooter(visible: () => boolean): { text: () => string; open: (repaint: () => void) => () => void } {
|
||||
let reader: LiveHeadPitch | null = null;
|
||||
return {
|
||||
text: () => reader?.text() ?? "",
|
||||
open: (repaint) => {
|
||||
const opened = new LiveHeadPitch(visible, repaint);
|
||||
reader = opened;
|
||||
return () => {
|
||||
opened.stop();
|
||||
if (reader === opened) reader = null;
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// The glasses' own head-up switch and angle from before Faceclaw first changed
|
||||
@@ -146,7 +149,3 @@ export function glassesHeadUpTrigger(status: HeadUpStatus | null = getHeadUpStat
|
||||
if (!status || status.switch < 0) return null;
|
||||
return status.switch === 1 ? status.angle + status.calibration : 0;
|
||||
}
|
||||
|
||||
function delay(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* Math for "Set to current head angle", kept free of NativeScript imports so
|
||||
* tests/head-tilt-math.test.cjs can run it under plain node.
|
||||
*
|
||||
* The temple's accelerometer axes are not documented (nor whether the report
|
||||
* stream applies the firmware's mount matrix), so nothing here assumes which
|
||||
* axis points forward. Instead the capture takes two readings, one with the
|
||||
* head level and one tilted up, and the tilt is the angle between the two
|
||||
* gravity vectors. That works for any mounting and any angle; what it cannot
|
||||
* tell is direction (tilting down by 30° reads the same as up by 30°), so the
|
||||
* row's instructions say to tilt up, and head roll between the two readings
|
||||
* adds to the result.
|
||||
*/
|
||||
|
||||
export type Vec3 = { x: number; y: number; z: number };
|
||||
|
||||
/** The setting's range; the firmware itself accepts 1..90. */
|
||||
export const MIN_GLANCE_ANGLE = 5;
|
||||
export const MAX_GLANCE_ANGLE = 60;
|
||||
|
||||
/** Mean of the samples, or null when there are none or they sum to zero. */
|
||||
export function meanVector(samples: readonly Vec3[]): Vec3 | null {
|
||||
if (samples.length === 0) return null;
|
||||
let x = 0, y = 0, z = 0;
|
||||
for (const s of samples) {
|
||||
x += s.x;
|
||||
y += s.y;
|
||||
z += s.z;
|
||||
}
|
||||
const mean = { x: x / samples.length, y: y / samples.length, z: z / samples.length };
|
||||
return Math.hypot(mean.x, mean.y, mean.z) > 1e-6 ? mean : null;
|
||||
}
|
||||
|
||||
/** Angle between two gravity readings, in degrees (0..180). */
|
||||
export function angleBetweenDegrees(a: Vec3, b: Vec3): number {
|
||||
const dot = a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
const cross = Math.hypot(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
|
||||
// atan2 stays accurate near 0° and 180°, where acos of the normalized dot does not.
|
||||
return (Math.atan2(cross, dot) * 180) / Math.PI;
|
||||
}
|
||||
|
||||
/** Nearest multiple of 5, kept within the setting's range. */
|
||||
export function roundGlanceAngle(degrees: number): number {
|
||||
const rounded = Math.round(degrees / 5) * 5;
|
||||
return Math.min(MAX_GLANCE_ANGLE, Math.max(MIN_GLANCE_ANGLE, rounded));
|
||||
}
|
||||
|
||||
/**
|
||||
* The setting value for a measured tilt, or why it cannot be used: a tilt that
|
||||
* would round below the minimum is reported (a head that did not move, not a
|
||||
* 5° trigger), and so is one past the maximum.
|
||||
*/
|
||||
export function glanceAngleForTilt(degrees: number): { ok: true; rounded: number } | { ok: false; reason: string } {
|
||||
if (!Number.isFinite(degrees)) return { ok: false, reason: "no reading" };
|
||||
if (degrees < MIN_GLANCE_ANGLE - 2.5) return { ok: false, reason: "head not tilted" };
|
||||
if (degrees >= MAX_GLANCE_ANGLE + 2.5) return { ok: false, reason: `over ${MAX_GLANCE_ANGLE}°` };
|
||||
return { ok: true, rounded: roundGlanceAngle(degrees) };
|
||||
}
|
||||
@@ -1,7 +1,23 @@
|
||||
/** Platform-neutral IMU types, shared by imu.ts and imu.ios.ts. */
|
||||
|
||||
/**
|
||||
* An accelerometer sample in g, in Faceclaw's frame for every glasses model
|
||||
* (the G2's own; the session rotates other models' samples into it, see
|
||||
* ImuOrientation.kt): +x forward, +y to the wearer's left, +z up through the
|
||||
* top of the head. At rest it is the "up" direction: head level reads
|
||||
* (0, 0, 1), looking 45° up (0.71, 0, 0.71), the head tilted 45° to the right
|
||||
* (0, 0.71, 0.71).
|
||||
*/
|
||||
export type ImuReading = { x: number; y: number; z: number; source: number };
|
||||
|
||||
/**
|
||||
* How far the head is tilted up from level, in degrees (negative: down), from
|
||||
* a reading taken at rest. Rolling the head to the side does not change it.
|
||||
*/
|
||||
export function imuPitchDegrees(reading: { x: number; y: number; z: number }): number {
|
||||
return (Math.atan2(reading.x, Math.hypot(reading.y, reading.z)) * 180) / Math.PI;
|
||||
}
|
||||
|
||||
/** Map an IMU sample's EventSource to a short label. */
|
||||
export function imuSourceLabel(source: number): string {
|
||||
switch (source) {
|
||||
|
||||
@@ -1724,9 +1724,18 @@ type SettingsMenuOptions<T> = {
|
||||
label?: string
|
||||
}
|
||||
|
||||
type EnumSettingMenuOptions<T> = SettingsMenuOptions<T> & {
|
||||
/**
|
||||
* A live line under the picker's values, such as a sensor reading to pick
|
||||
* by. open() runs as the picker opens, with a function that repaints it,
|
||||
* and returns what to run when the picker closes.
|
||||
*/
|
||||
pickerFooter?: { text: () => string; open: (repaint: () => void) => () => void }
|
||||
}
|
||||
|
||||
export function enumSettingMenuItem<TValue extends string, TId extends string = string>(
|
||||
setting: ConfigSettingEnum<TValue, TId>,
|
||||
opts?: SettingsMenuOptions<TValue>
|
||||
opts?: EnumSettingMenuOptions<TValue>
|
||||
): MenuItem {
|
||||
const label = opts?.label ?? setting.label;
|
||||
return {
|
||||
@@ -1754,7 +1763,11 @@ export function enumSettingMenuItem<TValue extends string, TId extends string =
|
||||
);
|
||||
},
|
||||
}));
|
||||
openModalMenu(ctx, label, items, Math.max(0, setting.values.indexOf(current)));
|
||||
const footer = opts?.pickerFooter;
|
||||
openModalMenu(ctx, label, items, Math.max(0, setting.values.indexOf(current)), {
|
||||
footer: footer?.text,
|
||||
onClosed: footer?.open(() => ctx.actions.requestRender()),
|
||||
});
|
||||
},
|
||||
render: ({ image, x, y, width }) => {
|
||||
drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width, label, setting.displayValue(setting.get()));
|
||||
|
||||
+33
-6
@@ -57,7 +57,7 @@ export type MenuLayout = {
|
||||
* Dim hint line pinned to the bottom edge inside the box, below the items
|
||||
* (gesture help such as "●— system menu"). Takes a line off the item area.
|
||||
*/
|
||||
footer?: string;
|
||||
footer?: MenuFooter;
|
||||
/** Brightness factor for everything beneath the menu (see Layer.dimUnderneath). Default: none. */
|
||||
dimUnderneath?: number;
|
||||
/**
|
||||
@@ -83,9 +83,16 @@ export type MenuPageLayout = {
|
||||
/** Width of the rows. Default (and at most): the window's, inside its side padding. */
|
||||
width?: number;
|
||||
/** Dim hint line pinned above the window's bottom padding, below the rows. */
|
||||
footer?: string;
|
||||
footer?: MenuFooter;
|
||||
};
|
||||
|
||||
/** A menu's footer line: fixed text, or read on every paint (a live value). */
|
||||
export type MenuFooter = string | (() => string);
|
||||
|
||||
function footerText(footer: MenuFooter): string {
|
||||
return typeof footer === "function" ? footer() : footer;
|
||||
}
|
||||
|
||||
export type MenuItemRenderArgs = {
|
||||
image: GrayImage;
|
||||
x: number;
|
||||
@@ -360,7 +367,7 @@ export class MenuLayer implements Layer {
|
||||
image.drawText(font, x + 12, y + 8, this.title, 220);
|
||||
}
|
||||
if (layout.footer) {
|
||||
image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, layout.footer, 110);
|
||||
image.drawText(font, x + 22, y + height - MENU_BODY_PADDING - font.lineHeight, footerText(layout.footer), 110);
|
||||
}
|
||||
|
||||
const bodyY = y + chromeTop;
|
||||
@@ -397,7 +404,7 @@ export class MenuLayer implements Layer {
|
||||
let fixedBottom = height - padding.bottom;
|
||||
if (layout.footer) {
|
||||
const footerY = height - padding.bottom - font.lineHeight;
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, layout.footer, 110);
|
||||
image.drawText(font, x + MENU_ROW_TEXT_INSET, footerY, footerText(layout.footer), 110);
|
||||
bottom = fixedBottom = footerY - MENU_FOOTER_GAP;
|
||||
}
|
||||
if (bottom <= top) return image;
|
||||
@@ -431,17 +438,26 @@ export class MenuLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
export type ModalMenuOptions = {
|
||||
/** A dim line under the items (see MenuLayout.footer). */
|
||||
footer?: MenuFooter;
|
||||
/** Runs when the menu leaves the stack, however it closes. */
|
||||
onClosed?: () => void;
|
||||
};
|
||||
|
||||
/** Open a centered, bordered menu over the current layer. */
|
||||
export function openModalMenu(
|
||||
ctx: LayerContext,
|
||||
title: string,
|
||||
items: MenuItem[],
|
||||
initialSelectedIndex = 0,
|
||||
options: ModalMenuOptions = {},
|
||||
): void {
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const font = getDefaultSmallFont();
|
||||
const menuWidth = Math.min(320, width - 40);
|
||||
const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font);
|
||||
const footerHeight = options.footer ? MENU_FOOTER_GAP + lineStep(font) : 0;
|
||||
const naturalHeight = menuTitleHeight(font) + 2 * MENU_BODY_PADDING + items.length * listRowHeight(font) + footerHeight;
|
||||
// 20px clear above and below, or 8 in a short window (the RayNeo iO's),
|
||||
// where those rows are worth more than the margin.
|
||||
const marginY = height < SHORT_WINDOW_HEIGHT ? 8 : 20;
|
||||
@@ -454,8 +470,19 @@ export function openModalMenu(
|
||||
maxHeight: menuHeight,
|
||||
// Floats nearer than the content it opens over, like the window and system menus.
|
||||
depth: 4,
|
||||
footer: options.footer,
|
||||
};
|
||||
ctx.stack.push(new MenuLayer(title, items, layout).selectItem(initialSelectedIndex));
|
||||
ctx.stack.push(new ModalMenuLayer(title, items, layout, options.onClosed).selectItem(initialSelectedIndex));
|
||||
}
|
||||
|
||||
class ModalMenuLayer extends MenuLayer {
|
||||
constructor(title: string, items: MenuItem[], layout: MenuLayout, private readonly onClosed?: () => void) {
|
||||
super(title, items, layout);
|
||||
}
|
||||
|
||||
onRemoved(): void {
|
||||
this.onClosed?.();
|
||||
}
|
||||
}
|
||||
|
||||
export function isMenuItemDisabled(item: MenuItem): boolean {
|
||||
|
||||
Reference in new issue
Block a user