Open History to see the last commit
1// @effect-diagnostics globalTimers:off - The stream owns this browser control queue and its timeout.
2import type { DeviceScreenSize } from "./stream.ts";
3
4export type DuoPose = "closed" | "book" | "open" | "laptop" | "tent";
5export type DuoOrientation =
6 | "portrait"
7 | "landscape_left"
8 | "portrait_upside_down"
9 | "landscape_right";
10export type DuoCommand =
11 | { control: "angle"; value: number }
12 | { control: "pose"; value: DuoPose }
13 | { control: "table"; value: boolean }
14 | { control: "physical"; value: "faceup" | "facedown" }
15 | { control: "orientation"; value: DuoOrientation };
16export type DuoControlState = {
17 pending: boolean;
18 requested: DuoCommand | null;
19 error: string | null;
20};
21
22/**
23 * A fold across the closed position hands off in steps, and the outgoing display first reports
24 * the incoming one's orientation, so an unsettled screen's orientation does not say how the
25 * phone is held. Stands such as Tent can also rest unsettled on the cover.
26 */
27export function duoScreenSettled(screen: Pick<DeviceScreenSize, "screenId" | "hingeAngle">) {
28 const angle = screen.hingeAngle ?? (screen.screenId === 1 ? 0 : 180);
29 return (screen.screenId === 1) === (angle === 0);
30}
31
32/**
33 * The inner panel is mounted a quarter turn from the cover, so its landscape
34 * orientation means a vertical phone.
35 */
36export function duoFoldState(
37 screen: Pick<DeviceScreenSize, "orientation" | "screenId" | "hingeAngle" | "hingePose">,
38) {
39 const angle = screen.hingeAngle ?? (screen.screenId === 1 ? 0 : 180);
40 const landscape = screen.orientation.startsWith("landscape");
41 return {
42 fold: angle === 0 ? "closed" : angle === 180 ? "open" : "half",
43 stand: screen.hingePose === "laptop" || screen.hingePose === "tent",
44 phoneVertical: screen.screenId === 1 ? !landscape : landscape,
45 settled: duoScreenSettled(screen),
46 } as const;
47}
48
49export function duoHoldOrientation(
50 vertical: boolean,
51 screenId: number | undefined,
52): DuoOrientation {
53 if (screenId === 1) return vertical ? "portrait" : "landscape_left";
54 return vertical ? "landscape_left" : "portrait_upside_down";
55}
56
57export function createDuoControl(options: {
58 send: (request: { requestId: number; command: DuoCommand }) => boolean;
59 onChange: (state: DuoControlState) => void;
60 timeoutMs?: number;
61}) {
62 let nextId = 1;
63 let active: { requestId: number; command: DuoCommand } | null = null;
64 let queued: DuoCommand | null = null;
65 let timer: ReturnType<typeof setTimeout> | null = null;
66 const publish = (error: string | null = null) =>
67 options.onChange({ pending: !!active, requested: queued ?? active?.command ?? null, error });
68 const clear = (error: string | null = null) => {
69 if (timer) clearTimeout(timer);
70 timer = null;
71 active = null;
72 queued = null;
73 publish(error);
74 };
75 const drain = () => {
76 if (active || !queued) return;
77 active = { requestId: nextId++, command: queued };
78 queued = null;
79 const id = active.requestId;
80 timer = setTimeout(() => {
81 if (active?.requestId === id) clear("Device control timed out. Its position is unknown.");
82 }, options.timeoutMs ?? 5_000);
83 publish();
84 if (!options.send(active)) clear("Device is disconnected.");
85 };
86 return {
87 enqueue(command: DuoCommand) {
88 if (
89 command.control === "angle" &&
90 (!Number.isFinite(command.value) || command.value < 0 || command.value > 180)
91 )
92 return;
93 queued = command;
94 if (active) publish();
95 else drain();
96 },
97 receive(reply: { requestId: number; ok: boolean; error?: string }) {
98 if (!active || reply.requestId !== active.requestId) return;
99 if (timer) clearTimeout(timer);
100 timer = null;
101 active = null;
102 if (!reply.ok) {
103 clear(reply.error ?? "Device control failed. Its position is unknown.");
104 return;
105 }
106 if (queued) drain();
107 else publish();
108 },
109 clear,
110 };
111}
112
113export function createDuoPinch(options: {
114 angle: () => number;
115 contains: (x: number, y: number) => boolean;
116 change: (angle: number | null) => void;
117}) {
118 let angle: number | null = null;
119 return {
120 begin(x: number, y: number) {
121 if (!options.contains(x, y)) return false;
122 angle = Math.max(0, Math.min(180, options.angle()));
123 return true;
124 },
125 move(logScale: number) {
126 if (angle === null || !Number.isFinite(logScale)) return;
127 const next = Math.max(0, Math.min(180, angle + logScale * 120));
128 if (next === angle) return;
129 angle = next;
130 options.change(next);
131 },
132 end() {
133 if (angle === null) return;
134 angle = null;
135 options.change(null);
136 },
137 get active() {
138 return angle !== null;
139 },
140 };
141}