TypeScript
fix(web): iPhone Duo fold controls follow the phone's orientation (#16630)
Gabriel De Andrade2991331515ed
diff --git a/apps/web/src/components/device/DeviceDuoControls.tsx b/apps/web/src/components/device/DeviceDuoControls.tsxindex 1759ab07123..57922a0243d 100644--- a/apps/web/src/components/device/DeviceDuoControls.tsx+++ b/apps/web/src/components/device/DeviceDuoControls.tsx@@ -1,5 +1,5 @@import {- DUO_POSES,+ duoFoldState,type DuoCommand,type DuoControlState,} from "@t3tools/client-runtime/device/duo-control";@@ -8,51 +8,87 @@ import { DeviceDuoGlyph } from "./DeviceDuoGlyph";import { Button } from "~/components/ui/button";import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";-/** Physical presets live beside the device. Pinching supplies continuous hinge control. */+const FOLDS = [+ { id: "closed", angle: 0 },+ { id: "half", angle: 90 },+ { id: "open", angle: 180 },+] as const;+const STANDS = [+ { id: "laptop", label: "Laptop stand" },+ { id: "tent", label: "Tent stand" },+] as const;++/**+ * Fold shapes move only the hinge, so the device opens around whichever edge it+ * currently rests on: a vertical phone opens as a book into a landscape tablet,+ * a horizontal one as a laptop into a portrait tablet. Stands are native presets+ * that also place the device. Pinching supplies continuous hinge control.+ */export function DeviceDuoControls(props: {screen: DeviceScreenSize;state: DuoControlState;enabled: boolean;onCommand: (command: DuoCommand) => void;}) {- const angle = props.screen.hingeAngle;- const fold = angle == null ? null : angle === 0 ? "closed" : angle === 180 ? "open" : "book";- const selected = (id: (typeof DUO_POSES)[number]["id"]) =>- id === "laptop" || id === "tent" ? props.screen.hingePose === id : fold === id;+ const { screen } = props;+ const { fold, stand, phoneVertical } = duoFoldState(screen);+ const foldLabels = {+ closed: "Closed",+ half: phoneVertical ? "Book" : "Laptop",+ open: "Open",+ };+ const button = (+ key: string,+ label: string,+ pressed: boolean,+ onClick: () => void,+ glyph: React.ReactNode,+ ) => (+ <Tooltip key={key}>+ <TooltipTrigger+ render={+ <Button+ size="icon"+ variant={pressed ? "secondary" : "ghost"}+ disabled={!props.enabled}+ aria-label={label}+ aria-pressed={pressed}+ data-pressed={pressed ? "" : undefined}+ onClick={onClick}+ />+ }+ >+ {glyph}+ </TooltipTrigger>+ <TooltipPopup side="left">{label}</TooltipPopup>+ </Tooltip>+ );+ const group =+ "pointer-events-auto flex shrink-0 flex-col items-center gap-1 rounded-full border border-border/50 bg-background/80 p-1 shadow-sm";return (<div aria-label="iPhone Duo stands" className="flex flex-col items-center gap-2">- {([DUO_POSES.slice(0, 3), DUO_POSES.slice(3)] as const).map((poses, index) => (- <div- key={poses[0]?.id}- role="group"- aria-label={index === 0 ? "Fold shape" : "Device stance"}- className="pointer-events-auto flex shrink-0 flex-col items-center gap-1 rounded-full border border-border/50 bg-background/80 p-1 shadow-sm"- >- {poses.map((pose) => (- <Tooltip key={pose.id}>- <TooltipTrigger- render={- <Button- size="icon"- variant={selected(pose.id) ? "secondary" : "ghost"}- disabled={!props.enabled}- aria-label={`${pose.label} stand`}- aria-pressed={selected(pose.id)}- data-pressed={selected(pose.id) ? "" : undefined}- onClick={() => props.onCommand({ control: "pose", value: pose.id })}- />- }- >- <DeviceDuoGlyph pose={pose.id} />- </TooltipTrigger>- <TooltipPopup side="left">- {pose.label}- {pose.id === "book" ? " / bookshelf" : ""}- </TooltipPopup>- </Tooltip>- ))}- </div>- ))}+ <div role="group" aria-label="Fold shape" className={group}>+ {FOLDS.map(({ id, angle: value }) =>+ button(+ id,+ foldLabels[id],+ !stand && fold === id,+ () => props.onCommand({ control: "angle", value }),+ <DeviceDuoGlyph pose={id === "half" ? "book" : id} rotated={!phoneVertical} />,+ ),+ )}+ </div>+ <div role="group" aria-label="Device stance" className={group}>+ {STANDS.map(({ id, label }) =>+ button(+ id,+ label,+ screen.hingePose === id,+ () => props.onCommand({ control: "pose", value: id }),+ <DeviceDuoGlyph pose={id} />,+ ),+ )}+ </div>{props.state.error ? (<Tooltip><TooltipTriggerdiff --git a/apps/web/src/components/device/DeviceDuoGlyph.tsx b/apps/web/src/components/device/DeviceDuoGlyph.tsxindex a179e3afe26..9ce2fcc6981 100644--- a/apps/web/src/components/device/DeviceDuoGlyph.tsx+++ b/apps/web/src/components/device/DeviceDuoGlyph.tsx@@ -1,41 +1,47 @@import type { DuoPose } from "@t3tools/client-runtime/device/duo-control";+import { cn } from "~/lib/utils";-// Bitrig 0.25's toolbar glyphs. The fold outlines follow the native toolbar;-// the rounded stance contours are ported from SimulatorFoldingPoseGlyph's SwiftUI paths.+// Fold outlines follow Simulator's Duo toolbar; the rounded stance contours are+// ported from SimulatorFoldingPoseGlyph's SwiftUI paths.const stancePaths = {laptop:"M2.7351 12.9857 L15.8128 11.4159 L14.4114 3.3787 C14.1943 2.134 13.1275 1.2319 12.0283 1.3639 L2.9308 2.4559 C1.8317 2.5879 1.1167 3.7038 1.3337 4.9485 L2.7351 12.9857 Z M2.7351 12.9857 L15.8128 11.4159 L19.9103 12.8914 C20.5449 13.1199 20.1683 13.4121 19.0692 13.5441 L9.9717 14.6361 C8.8725 14.7681 7.4672 14.6898 6.8326 14.4612 L2.7351 12.9857 Z",tent: "M3.2522 2.7267 L18.7478 1.4816 L16.696 9.7017 C16.3782 10.9748 15.0649 12.0915 13.7626 12.1962 L2.983 13.0623 C1.6807 13.167 0.8826 12.2199 1.2004 10.9468 L3.2522 2.7267 Z M3.2522 2.7267 L18.7478 1.4816 L20.7996 10.6909 C21.1174 12.1172 20.3193 13.3581 19.017 13.4628 L8.2374 14.3289 C6.9351 14.4336 5.6218 13.3623 5.304 11.936 L3.2522 2.7267 Z",} as const;-export function DeviceDuoGlyph({ pose }: { pose: DuoPose }) {+/**+ * Fold glyphs draw the physical device held as a vertical phone: it opens like a book into a+ * landscape tablet. `rotated` turns the whole device a quarter clockwise for a horizontal phone,+ * camera and home indicator included, as Simulator does.+ */+export function DeviceDuoGlyph({ pose, rotated = false }: { pose: DuoPose; rotated?: boolean }) {const stance = pose === "laptop" || pose === "tent";return (<svg- viewBox={stance ? "0 0 22 16" : "0 0 24 32"}+ viewBox={stance ? "0 0 22 16" : "0 0 24 24"}fill="none"stroke="currentColor"- strokeWidth={stance ? 1.25 : 1.8}+ strokeWidth={stance ? 1.25 : 1.35}strokeLinecap="round"strokeLinejoin="round"- className="size-7 shrink-0"+ className={cn("size-7 shrink-0", rotated && "rotate-90")}aria-hidden>{stance ? <path d={stancePaths[pose]} /> : null}{pose === "closed" ? (<>- <rect x="2" y="9" width="20" height="13" rx="2.2" />- <path d="M19.3 12.5v6" />- <circle cx="5" cy="12" r=".5" fill="currentColor" stroke="none" />+ <rect x="5.8" y="3" width="12.4" height="18" rx="2.2" />+ <circle cx="15.4" cy="5.7" r=".6" fill="currentColor" stroke="none" />+ <path d="M10.6 18.2h2.8" /></>) : null}{pose === "book" ? (- <path d="M4.2 6.5h15.6c1 0 1.6.8 1.2 1.7L17.7 16l3.3 7.8c.4.9-.2 1.7-1.2 1.7H4.2c-1 0-1.6-.8-1.2-1.7L6.3 16 3 8.2c-.4-.9.2-1.7 1.2-1.7Z" />+ <path d="M4.6 18.1V5.9Q4.6 4.9 5.9 4.9L12 7.5L18.1 4.9Q19.4 4.9 19.4 5.9V18.1Q19.4 19.1 18.1 19.1L12 16.5L5.9 19.1Q4.6 19.1 4.6 18.1Z" />) : null}{pose === "open" ? (<>- <rect x="2" y="2" width="20" height="28" rx="2.4" />- <path d="M10 27h4" />+ <rect x="2.5" y="5" width="19" height="14" rx="2.4" />+ <path d="M10.5 16.3h3" /></>) : null}</svg>diff --git a/packages/client-runtime/src/device/duoControl.test.ts b/packages/client-runtime/src/device/duoControl.test.tsindex 019c383a56d..f325d3a25de 100644--- a/packages/client-runtime/src/device/duoControl.test.ts+++ b/packages/client-runtime/src/device/duoControl.test.ts@@ -1,5 +1,5 @@import { afterEach, expect, it, vi } from "vite-plus/test";-import { createDuoControl, createDuoPinch, type DuoCommand } from "./duoControl.ts";+import { createDuoControl, createDuoPinch, duoFoldState, type DuoCommand } from "./duoControl.ts";afterEach(() => vi.useRealTimers());it("keeps a failed send visible, including a disconnect while draining queued motion", () => {@@ -107,3 +107,52 @@ it("pinches only a hit device, accumulates independently of native readback, claexpect(change).toHaveBeenCalledTimes(count);expect(pinch.active).toBe(false);});++// Screen configs as an iPhone Duo simulator reports them for each way of holding the device.+it.each([+ [+ "vertical phone, closed",+ { screenId: 1, orientation: "portrait", hingeAngle: 0 },+ "closed",+ true,+ ],+ [+ "horizontal phone, closed",+ { screenId: 1, orientation: "landscape_right", hingeAngle: 0 },+ "closed",+ false,+ ],+ [+ "vertical phone opened as a book",+ { screenId: 3, orientation: "landscape_left", hingeAngle: 180 },+ "open",+ true,+ ],+ [+ "horizontal phone opened as a laptop",+ { screenId: 3, orientation: "portrait_upside_down", hingeAngle: 180 },+ "open",+ false,+ ],+ ["half-open book", { screenId: 3, orientation: "landscape_left", hingeAngle: 90 }, "half", true],+] as const)("reads a %s", (_name, screen, fold, phoneVertical) => {+ expect(duoFoldState(screen)).toEqual({ fold, stand: false, phoneVertical });+});++it("marks native stands so the fold group does not also claim them", () => {+ expect(+ duoFoldState({ screenId: 3, orientation: "portrait", hingeAngle: 90, hingePose: "laptop" }),+ ).toEqual({ fold: "half", stand: true, phoneVertical: false });+});++it("falls back like the 3D view when hinge fields are missing", () => {+ expect(duoFoldState({ screenId: 1, orientation: "portrait" })).toMatchObject({+ fold: "closed",+ phoneVertical: true,+ });+ // Without a display ID the 3D view draws the open inner panel, so the controls do too.+ expect(duoFoldState({ orientation: "landscape_left" })).toMatchObject({+ fold: "open",+ phoneVertical: true,+ });+});diff --git a/packages/client-runtime/src/device/duoControl.ts b/packages/client-runtime/src/device/duoControl.tsindex b9f61ec997d..e5be353ca62 100644--- a/packages/client-runtime/src/device/duoControl.ts+++ b/packages/client-runtime/src/device/duoControl.ts@@ -1,12 +1,8 @@// @effect-diagnostics globalTimers:off - The stream owns this browser control queue and its timeout.-export const DUO_POSES = [- { id: "closed", label: "Closed", angle: 0 },- { id: "book", label: "Book", angle: 90 },- { id: "open", label: "Open", angle: 180 },- { id: "laptop", label: "Laptop", angle: 90 },- { id: "tent", label: "Tent", angle: 80 },-] as const;-export type DuoPose = (typeof DUO_POSES)[number]["id"];+import type { DeviceScreenSize } from "./stream.ts";++/** Native hinge presets. Each one also sets the device's physical orientation. */+export type DuoPose = "closed" | "book" | "open" | "laptop" | "tent";export type DuoOrientation =| "portrait"| "landscape_left"@@ -24,6 +20,23 @@ export type DuoControlState = {error: string | null;};+/**+ * The fold the device is in and the way it is held. Missing hinge fields fall back the same way+ * the 3D view does, so controls never disagree with what is drawn. The inner panel is mounted a+ * quarter turn from the cover, so its landscape orientation means a vertical phone.+ */+export function duoFoldState(+ screen: Pick<DeviceScreenSize, "orientation" | "screenId" | "hingeAngle" | "hingePose">,+) {+ const angle = screen.hingeAngle ?? (screen.screenId === 1 ? 0 : 180);+ const landscape = screen.orientation.startsWith("landscape");+ return {+ fold: angle === 0 ? "closed" : angle === 180 ? "open" : "half",+ stand: screen.hingePose === "laptop" || screen.hingePose === "tent",+ phoneVertical: screen.screenId === 1 ? !landscape : landscape,+ } as const;+}+/** One in-flight native transaction. Hinge motion coalesces; presets replace queued motion. Nothing replays after reconnect. */export function createDuoControl(options: {send: (request: { requestId: number; command: DuoCommand }) => boolean;