forked from Blendman974/faceclaw
EvenHub extensions: Fonts, window icons, touch-start events
This commit is contained in:
1 parent
4ee0af86cc
commit
84baee9c76
25 files changed
+1884
-127
No files matched your search
+207
-48
@@ -63,6 +63,16 @@ import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/to
|
||||
import { getCurrentLocation } from "../../native/location";
|
||||
import { LocationTracker, type TrackedLocation } from "../../native/location-tracker";
|
||||
import { ensureFineLocationPermission } from "../../native/location-permissions";
|
||||
import {
|
||||
buildEvenHubDeviceStatus,
|
||||
OFFLINE_DEVICE_STATUS_SOURCE,
|
||||
sameDeviceStatus,
|
||||
type EvenHubDeviceStatus,
|
||||
type EvenHubDeviceStatusSource,
|
||||
} from "./device-status";
|
||||
import { parseWindowIcon, WindowIconRenderer } from "./window-icon";
|
||||
import { availableFontFamilies, containerFace, measureContainerText } from "./fonts";
|
||||
import { parseFontSpec } from "./font-spec";
|
||||
|
||||
const UPNG = require("upng-js");
|
||||
|
||||
@@ -284,19 +294,29 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
|
||||
if (i >= 0) arr.splice(i, 1);
|
||||
};
|
||||
}
|
||||
// Compass: enable on first listener, disable when the last one leaves.
|
||||
var compassListeners = [];
|
||||
function addCompass(cb) {
|
||||
compassListeners.push(cb);
|
||||
if (compassListeners.length === 1) send("setCompass", [true]);
|
||||
var off = on("compass", cb);
|
||||
return function () {
|
||||
off();
|
||||
var i = compassListeners.indexOf(cb);
|
||||
if (i >= 0) compassListeners.splice(i, 1);
|
||||
if (compassListeners.length === 0) send("setCompass", [false]);
|
||||
// Events the host only produces on request (compass, touch-down, text
|
||||
// input): it is told to start on the first listener and to stop when the
|
||||
// last one leaves. Each unsubscribe function works once.
|
||||
function counted(name, setMethod) {
|
||||
var count = 0;
|
||||
return function (cb) {
|
||||
var off = on(name, cb);
|
||||
if (++count === 1) send(setMethod, [true]);
|
||||
var active = true;
|
||||
return function () {
|
||||
if (!active) return;
|
||||
active = false;
|
||||
off();
|
||||
if (--count === 0) send(setMethod, [false]);
|
||||
};
|
||||
};
|
||||
}
|
||||
function setWindowIcon(icon) {
|
||||
if (!icon) return call("setWindowIcon", [null]);
|
||||
// Typed arrays (ImageData.data) don't survive JSON; send a plain array.
|
||||
var data = Array.prototype.slice.call(icon.data || []);
|
||||
return call("setWindowIcon", [{ width: icon.width, height: icon.height, data: data }]);
|
||||
}
|
||||
// Voice-assistant tools: specs go to the host; handlers stay here and run when
|
||||
// the host invokes a tool via __fcExtInvokeTool.
|
||||
var toolHandlers = {};
|
||||
@@ -335,10 +355,18 @@ export function buildFaceclawExtensionsScript(versionString: string): string {
|
||||
getConfiguredApiKeys: function () { return call("getConfiguredApiKeys"); },
|
||||
requestApiKeyAccess: function (services) { return call("requestApiKeyAccess", [services || []]); },
|
||||
playBuzzer: function (steps) { return call("playBuzzer", [steps || []]); },
|
||||
addCompassListener: addCompass,
|
||||
addCompassListener: counted("compass", "setCompass"),
|
||||
createLayout: function (layout) { return call("createLayout", [layout || {}]); },
|
||||
replaceLayout: function (layout) { return call("replaceLayout", [layout || {}]); },
|
||||
setAssistantTools: setAssistantTools
|
||||
setAssistantTools: setAssistantTools,
|
||||
addTouchDownListener: counted("touchDown", "setTouchDown"),
|
||||
addTextInputListener: counted("textInput", "setTextInput"),
|
||||
startVoiceInput: function () { return call("startVoiceInput"); },
|
||||
setWindowIcon: setWindowIcon,
|
||||
setAttention: function (attention) { send("setAttention", [!!attention]); },
|
||||
setKeepScreenOn: function (keepOn) { send("setKeepScreenOn", [!!keepOn]); },
|
||||
getFonts: function () { return call("getFonts"); },
|
||||
measureText: function (text, font) { return call("measureText", [String(text), font || null]); }
|
||||
};
|
||||
window.getFaceclawExtensions = function () { return extensions; };
|
||||
})();
|
||||
@@ -364,6 +392,14 @@ export type EvenHubWindowHooks = {
|
||||
pushOverlay: (layer: Layer) => void;
|
||||
/** Switch the window between the stock 576x288 band and the full 576x452 canvas. */
|
||||
setTallCanvas: (tall: boolean) => void;
|
||||
/** Show or clear the attention dot on the window's switcher icon. */
|
||||
setAttention: (attention: boolean) => void;
|
||||
/** Offer (or stop offering) the window as the shell's "Type Into App" destination. */
|
||||
setAcceptsTextInput: (accept: boolean) => void;
|
||||
/** Open the shell's voice dialog aimed at the window; false if it can't open now. */
|
||||
startVoiceInput: () => boolean;
|
||||
/** The app changed its window icon: repaint the switcher. */
|
||||
iconChanged: () => void;
|
||||
/** The shell window id (unique per running instance; used for tool registration). */
|
||||
windowId: string;
|
||||
};
|
||||
@@ -414,6 +450,21 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
private shellFocused = true;
|
||||
/** API-key services the user has granted this app this session. */
|
||||
private readonly grantedApiKeys = new Set<string>();
|
||||
/** The app listens for touch-down (extension addTouchDownListener). */
|
||||
private touchDownRequested = false;
|
||||
/** The app listens for text input, so the shell offers it as a Type Into App destination. */
|
||||
private textInputRequested = false;
|
||||
/** The app asked for the attention dot on its switcher icon. */
|
||||
private attention = false;
|
||||
/** The app asked the idle screen timeout to wait while it is in the foreground. */
|
||||
private keepScreenOnRequested = false;
|
||||
/** The app's own switcher icon; null draws the package icon. */
|
||||
private windowIcon: WindowIconRenderer | null = null;
|
||||
/** The paired glasses' serial, read once so getGlassesInfo and every status push agree. */
|
||||
private glassesSerial: string | null | undefined = undefined;
|
||||
/** The last deviceStatusChanged sent, and the subscriptions that refresh it. */
|
||||
private lastDeviceStatus: EvenHubDeviceStatus | null = null;
|
||||
private readonly deviceStatusOffs: (() => void)[] = [];
|
||||
private log: (message: string) => void;
|
||||
|
||||
constructor(
|
||||
@@ -422,6 +473,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
log: (message: string) => void,
|
||||
remoteUrl = "",
|
||||
private readonly sendBuzzerSequence: (payload: Uint8Array) => Promise<void> | void = () => {},
|
||||
private readonly deviceStatusSource: EvenHubDeviceStatusSource = OFFLINE_DEVICE_STATUS_SOURCE,
|
||||
) {
|
||||
this.manifest = manifest;
|
||||
this.distDir = distDir;
|
||||
@@ -447,14 +499,8 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
// Launched to drive the glasses (from the on-glasses file browser), so
|
||||
// apps that branch on this should take their glasses path.
|
||||
this.pushMessage("evenAppLaunchSource", { launchSource: "glassesMenu" });
|
||||
this.pushMessage("deviceStatusChanged", {
|
||||
sn: "FACECLAW-G2",
|
||||
connectType: "connected",
|
||||
isWearing: true,
|
||||
batteryLevel: 100,
|
||||
isCharging: false,
|
||||
isInCase: false,
|
||||
});
|
||||
this.pushDeviceStatus();
|
||||
this.deviceStatusOffs.push(this.deviceStatusSource.subscribe(() => this.pushDeviceStatus()));
|
||||
}
|
||||
|
||||
// ----- glasses window wiring -----
|
||||
@@ -462,6 +508,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
attachWindow(hooks: EvenHubWindowHooks): void {
|
||||
this.windowHooks = hooks;
|
||||
this.shellWindowId = hooks.windowId;
|
||||
// Requests the app made before the window existed.
|
||||
if (this.textInputRequested) hooks.setAcceptsTextInput(true);
|
||||
if (this.attention) hooks.setAttention(true);
|
||||
if (this.windowIcon) hooks.iconChanged();
|
||||
}
|
||||
|
||||
windowClosed(): void {
|
||||
@@ -491,18 +541,29 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
* locally with events only for clicks and boundary scrolls.
|
||||
*/
|
||||
handleGesture(event: InputEvent): void {
|
||||
// Touch-down has no stock event (an unknown eventType would parse as a
|
||||
// click), so it goes only to apps that subscribed through the extension.
|
||||
if (event.type === "ring-press") {
|
||||
if (this.touchDownRequested) {
|
||||
this.pushExtEvent("touchDown", { source: extensionInputSource(event.source), timestampMs: event.timestampMs });
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!this.page) return;
|
||||
const capture = this.page ? eventCaptureContainer(this.page) : undefined;
|
||||
const capture = eventCaptureContainer(this.page);
|
||||
// The stock eventSource can't tell the watch from the ring, and scrolls
|
||||
// carry none; the extension field names the device when it is known.
|
||||
const inputSource = "source" in event && event.source ? extensionInputSource(event.source) : undefined;
|
||||
switch (event.type) {
|
||||
case "click":
|
||||
if (capture?.kind === "list") {
|
||||
this.emitListEvent(capture, CLICK_EVENT);
|
||||
this.emitListEvent(capture, CLICK_EVENT, inputSource);
|
||||
} else {
|
||||
this.emitSysEvent(CLICK_EVENT, gestureSource(event.source));
|
||||
this.emitSysEvent(CLICK_EVENT, gestureSource(event.source), inputSource);
|
||||
}
|
||||
break;
|
||||
case "double-click":
|
||||
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source));
|
||||
this.emitSysEvent(DOUBLE_CLICK_EVENT, gestureSource(event.source), inputSource);
|
||||
break;
|
||||
case "long-press":
|
||||
case "short-then-long-press":
|
||||
@@ -516,9 +577,9 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
// given exclusive use of the gesture.
|
||||
const eventType = event.type === "long-press-release" ? LONG_PRESS_RELEASE_EVENT : LONG_PRESS_EVENT;
|
||||
if (capture?.kind === "list") {
|
||||
this.emitListEvent(capture, eventType);
|
||||
this.emitListEvent(capture, eventType, inputSource);
|
||||
} else {
|
||||
this.emitSysEvent(eventType, gestureSource(event.source));
|
||||
this.emitSysEvent(eventType, gestureSource(event.source), inputSource);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -526,9 +587,9 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
case "scroll-down": {
|
||||
const eventType = event.type === "scroll-up" ? SCROLL_TOP_EVENT : SCROLL_BOTTOM_EVENT;
|
||||
if (capture?.kind === "list") {
|
||||
this.scrollList(capture, eventType);
|
||||
this.scrollList(capture, eventType, inputSource);
|
||||
} else {
|
||||
this.emitSysEvent(eventType, 0);
|
||||
this.emitSysEvent(eventType, 0, inputSource);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -551,11 +612,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
this.pushMessage("evenHubEvent", { type: "menuItemClickEvent", jsonData: { itemID } });
|
||||
}
|
||||
|
||||
private scrollList(list: EvenHubListContainer, eventType: number): void {
|
||||
private scrollList(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
|
||||
// Swipe up selects the previous item, swipe down the next (stock direction).
|
||||
const delta = eventType === SCROLL_TOP_EVENT ? -1 : 1;
|
||||
// At a boundary the selection stays put (the list bounces) and the app hears about it.
|
||||
if (!stepListSelection(list, EvenHubFont.get(), delta)) this.emitListEvent(list, eventType);
|
||||
if (!stepListSelection(list, containerFace(list.font), delta)) this.emitListEvent(list, eventType, inputSource);
|
||||
this.windowHooks?.requestRender();
|
||||
}
|
||||
|
||||
@@ -820,19 +881,10 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
return ApplicationSettings.getString(this.storageKey(readString(data, "key", "")), "");
|
||||
case "getUserInfo":
|
||||
return { uid: 0, name: "Faceclaw", avatar: "", country: "" };
|
||||
case "getGlassesInfo":
|
||||
return {
|
||||
model: "g2",
|
||||
sn: "FACECLAW-G2",
|
||||
status: {
|
||||
sn: "FACECLAW-G2",
|
||||
connectType: "connected",
|
||||
isWearing: true,
|
||||
batteryLevel: 100,
|
||||
isCharging: false,
|
||||
isInCase: false,
|
||||
},
|
||||
};
|
||||
case "getGlassesInfo": {
|
||||
const status = this.deviceStatus();
|
||||
return { model: "g2", sn: status.sn, status };
|
||||
}
|
||||
case "audioControl":
|
||||
return this.audioControl(data);
|
||||
case "getAppLocation":
|
||||
@@ -1001,17 +1053,18 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
this.pushMessage("evenHubEvent", { type: payloadType, jsonData: elideZeroFields(jsonData) });
|
||||
}
|
||||
|
||||
private emitSysEvent(eventType: number, eventSource: number): void {
|
||||
this.pushEvenHubEvent("sysEvent", { eventType, eventSource });
|
||||
private emitSysEvent(eventType: number, eventSource: number, inputSource?: ExtensionInputSource): void {
|
||||
this.pushEvenHubEvent("sysEvent", { eventType, eventSource, [INPUT_SOURCE_FIELD]: inputSource });
|
||||
}
|
||||
|
||||
private emitListEvent(list: EvenHubListContainer, eventType: number): void {
|
||||
private emitListEvent(list: EvenHubListContainer, eventType: number, inputSource?: ExtensionInputSource): void {
|
||||
this.pushEvenHubEvent("listEvent", {
|
||||
containerID: list.id,
|
||||
containerName: list.name,
|
||||
currentSelectItemIndex: list.selectedIndex,
|
||||
currentSelectItemName: list.itemNames[list.selectedIndex] ?? "",
|
||||
eventType,
|
||||
[INPUT_SOURCE_FIELD]: inputSource,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1027,7 +1080,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
result = await this.dispatchExtension(method, args.slice(1));
|
||||
} catch (error) {
|
||||
ok = false;
|
||||
result = String(error);
|
||||
result = error instanceof Error ? error.message : String(error);
|
||||
this.log(`evenhub: extension call failed: ${error}`);
|
||||
}
|
||||
// id 0 is a fire-and-forget send; nothing is awaiting it.
|
||||
@@ -1060,12 +1113,96 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
case "setAssistantTools":
|
||||
this.setAssistantTools(Array.isArray(params[0]) ? params[0] : []);
|
||||
return null;
|
||||
case "setTouchDown":
|
||||
this.touchDownRequested = params[0] === true;
|
||||
return null;
|
||||
case "setTextInput":
|
||||
this.setTextInputRequested(params[0] === true);
|
||||
return null;
|
||||
case "startVoiceInput":
|
||||
// Without a text listener the dialog would have nowhere in the app to send to.
|
||||
return this.textInputRequested && (this.windowHooks?.startVoiceInput() ?? false);
|
||||
case "setWindowIcon":
|
||||
this.setWindowIcon(params[0]);
|
||||
return null;
|
||||
case "setAttention":
|
||||
this.attention = params[0] === true;
|
||||
this.windowHooks?.setAttention(this.attention);
|
||||
return null;
|
||||
case "setKeepScreenOn":
|
||||
this.keepScreenOnRequested = params[0] === true;
|
||||
return null;
|
||||
case "getFonts":
|
||||
return availableFontFamilies();
|
||||
case "measureText":
|
||||
// No font measures in the stock font, as a container without one draws.
|
||||
return measureContainerText(String(params[0] ?? ""), parseFontSpec(params[1]));
|
||||
default:
|
||||
this.log(`evenhub: unknown extension method ${method}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ----- device status (stock getGlassesInfo / deviceStatusChanged) -----
|
||||
|
||||
private deviceStatus(): EvenHubDeviceStatus {
|
||||
const input = this.deviceStatusSource.read();
|
||||
if (this.glassesSerial === undefined) this.glassesSerial = input.serial;
|
||||
return buildEvenHubDeviceStatus({ ...input, serial: this.glassesSerial });
|
||||
}
|
||||
|
||||
/** Push deviceStatusChanged if the status differs from the last one sent. */
|
||||
private pushDeviceStatus(): void {
|
||||
if (this.closed) return;
|
||||
const status = this.deviceStatus();
|
||||
if (this.lastDeviceStatus && sameDeviceStatus(status, this.lastDeviceStatus)) return;
|
||||
this.lastDeviceStatus = status;
|
||||
this.pushMessage("deviceStatusChanged", status);
|
||||
}
|
||||
|
||||
// ----- window controls and text input (extensions) -----
|
||||
|
||||
/** Whether the idle screen timeout should wait while this window is in the foreground. */
|
||||
keepsScreenOn(): boolean {
|
||||
return this.keepScreenOnRequested;
|
||||
}
|
||||
|
||||
/** The app's own switcher icon at `size`, or null to draw the package icon. */
|
||||
renderWindowIcon(size: number): GrayImage | null {
|
||||
return this.windowIcon?.render(size) ?? null;
|
||||
}
|
||||
|
||||
private setWindowIcon(raw: unknown): void {
|
||||
if (raw == null) {
|
||||
this.windowIcon = null;
|
||||
} else {
|
||||
const image = parseWindowIcon(raw);
|
||||
if (!image) {
|
||||
throw new Error(
|
||||
"setWindowIcon: expected {width, height, data} of at most 256x256, with data holding " +
|
||||
"width*height grayscale or width*height*4 RGBA bytes",
|
||||
);
|
||||
}
|
||||
this.windowIcon = new WindowIconRenderer(image);
|
||||
}
|
||||
this.windowHooks?.iconChanged();
|
||||
}
|
||||
|
||||
private setTextInputRequested(accept: boolean): void {
|
||||
if (this.textInputRequested === accept) return;
|
||||
this.textInputRequested = accept;
|
||||
this.windowHooks?.setAcceptsTextInput(accept);
|
||||
}
|
||||
|
||||
/**
|
||||
* Text the shell routed to this window: the voice dialog's Type Into App,
|
||||
* the phone keyboard, the watch, or remote input. Only arrives while the
|
||||
* app listens (setAcceptsTextInput).
|
||||
*/
|
||||
deliverTextInput(text: string, options?: { submit?: boolean }): void {
|
||||
this.pushExtEvent("textInput", options?.submit === undefined ? { text } : { text, submit: options.submit });
|
||||
}
|
||||
|
||||
// ----- compass (EvenHubCompassClient) -----
|
||||
|
||||
deliverCompass(reading: EvenHubCompassReading): void {
|
||||
@@ -1249,6 +1386,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
this.pendingToolCalls.clear();
|
||||
this.locationTracker?.stop();
|
||||
this.locationTracker = null;
|
||||
for (const off of this.deviceStatusOffs.splice(0)) off();
|
||||
if (!this.systemExitSent) {
|
||||
// Best effort: lets the app's teardown handlers run before the webview dies.
|
||||
this.emitSysEvent(SYSTEM_EXIT_EVENT, 0);
|
||||
@@ -1451,6 +1589,27 @@ function luminance(r: number, g: number, b: number): number {
|
||||
return Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Input-device names in the extension API: the touchDown event's source, and
|
||||
* the `faceclawInputSource` field added to stock gesture events.
|
||||
*/
|
||||
type ExtensionInputSource = "ring" | "left-touchpad" | "right-touchpad" | "watch";
|
||||
|
||||
const INPUT_SOURCE_FIELD = "faceclawInputSource";
|
||||
|
||||
function extensionInputSource(source: InputSource): ExtensionInputSource {
|
||||
switch (source) {
|
||||
case "ring":
|
||||
return "ring";
|
||||
case "left-arm":
|
||||
return "left-touchpad";
|
||||
case "right-arm":
|
||||
return "right-touchpad";
|
||||
case "watch":
|
||||
return "watch";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ring/left-arm/right-arm -> PB EventSourceType. The watch is reported to
|
||||
* EvenHub apps as the ring: they only know stock hardware, and the watch's
|
||||
|
||||
Reference in new issue
Block a user