Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

duoControl.ts

126 lines4.3 KB

1// @effect-diagnostics globalTimers:off - The stream owns this browser control queue and its timeout.
2import type { DeviceScreenSize } from "./stream.ts";
3​
4/** Native hinge presets. Each one also sets the device's physical orientation. */
5export type DuoPose = "closed" | "book" | "open" | "laptop" | "tent";
6export type DuoOrientation =
7 | "portrait"
8 | "landscape_left"
9 | "portrait_upside_down"
10 | "landscape_right";
11export type DuoCommand =
12 | { control: "angle"; value: number }
13 | { control: "pose"; value: DuoPose }
14 | { control: "table"; value: boolean }
15 | { control: "physical"; value: "faceup" | "facedown" }
16 | { control: "orientation"; value: DuoOrientation };
17export type DuoControlState = {
18 pending: boolean;
19 requested: DuoCommand | null;
20 error: string | null;
21};
22​
23/**
24 * The fold the device is in and the way it is held. Missing hinge fields fall back the same way
25 * the 3D view does, so controls never disagree with what is drawn. The inner panel is mounted a
26 * quarter turn from the cover, so its landscape orientation means a vertical phone.
27 */
28export function duoFoldState(
29 screen: Pick<DeviceScreenSize, "orientation" | "screenId" | "hingeAngle" | "hingePose">,
30) {
31 const angle = screen.hingeAngle ?? (screen.screenId === 1 ? 0 : 180);
32 const landscape = screen.orientation.startsWith("landscape");
33 return {
34 fold: angle === 0 ? "closed" : angle === 180 ? "open" : "half",
35 stand: screen.hingePose === "laptop" || screen.hingePose === "tent",
36 phoneVertical: screen.screenId === 1 ? !landscape : landscape,
37 } as const;
39​
40/** One in-flight native transaction. Hinge motion coalesces; presets replace queued motion. Nothing replays after reconnect. */
41export function createDuoControl(options: {
42 send: (request: { requestId: number; command: DuoCommand }) => boolean;
43 onChange: (state: DuoControlState) => void;
44 timeoutMs?: number;
45}) {
46 let nextId = 1;
47 let active: { requestId: number; command: DuoCommand } | null = null;
48 let queued: DuoCommand | null = null;
49 let timer: ReturnType<typeof setTimeout> | null = null;
50 const publish = (error: string | null = null) =>
51 options.onChange({ pending: !!active, requested: queued ?? active?.command ?? null, error });
52 const clear = (error: string | null = null) => {
53 if (timer) clearTimeout(timer);
54 timer = null;
55 active = null;
56 queued = null;
57 publish(error);
58 };
59 const drain = () => {
60 if (active || !queued) return;
61 active = { requestId: nextId++, command: queued };
62 queued = null;
63 const id = active.requestId;
64 timer = setTimeout(() => {
65 if (active?.requestId === id) clear("Device control timed out. Its position is unknown.");
66 }, options.timeoutMs ?? 5_000);
67 publish();
68 if (!options.send(active)) clear("Device is disconnected.");
69 };
70 return {
71 enqueue(command: DuoCommand) {
72 if (
73 command.control === "angle" &&
74 (!Number.isFinite(command.value) || command.value < 0 || command.value > 180)
75 )
76 return;
77 queued = command;
78 if (active) publish();
79 else drain();
80 },
81 receive(reply: { requestId: number; ok: boolean; error?: string }) {
82 if (!active || reply.requestId !== active.requestId) return;
83 if (timer) clearTimeout(timer);
84 timer = null;
85 active = null;
86 if (!reply.ok) {
87 clear(reply.error ?? "Device control failed. Its position is unknown.");
88 return;
89 }
90 if (queued) drain();
91 else publish();
92 },
93 clear,
94 };
96​
97/** A pinch keeps its own accumulator across asynchronous native acknowledgements. */
98export function createDuoPinch(options: {
99 angle: () => number;
100 contains: (x: number, y: number) => boolean;
101 change: (angle: number | null) => void;
102}) {
103 let angle: number | null = null;
104 return {
105 begin(x: number, y: number) {
106 if (!options.contains(x, y)) return false;
107 angle = Math.max(0, Math.min(180, options.angle()));
108 return true;
109 },
110 move(logScale: number) {
111 if (angle === null || !Number.isFinite(logScale)) return;
112 const next = Math.max(0, Math.min(180, angle + logScale * 120));
113 if (next === angle) return;
114 angle = next;
115 options.change(next);
116 },
117 end() {
118 if (angle === null) return;
119 angle = null;
120 options.change(null);
121 },
122 get active() {
123 return angle !== null;
124 },
125 };