|
1 | | -import { |
2 | | - DUO_POSES, |
3 | | - type DuoCommand, |
4 | | - type DuoControlState, |
5 | | -} from "@t3tools/client-runtime/device/duo-control"; |
| 1 | +import type { DuoCommand, DuoControlState } from "@t3tools/client-runtime/device/duo-control"; |
6 | 2 | import type { DeviceScreenSize } from "@t3tools/client-runtime/device/stream"; |
7 | 3 | import { DeviceDuoGlyph } from "./DeviceDuoGlyph"; |
8 | 4 | import { Button } from "~/components/ui/button"; |
9 | 5 | import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; |
10 | 6 |
|
11 | | -/** Physical presets live beside the device. Pinching supplies continuous hinge control. */ |
| 7 | +const FOLDS = [ |
| 8 | + { id: "closed", angle: 0 }, |
| 9 | + { id: "half", angle: 90 }, |
| 10 | + { id: "open", angle: 180 }, |
| 11 | +] as const; |
| 12 | +const STANDS = [ |
| 13 | + { id: "laptop", label: "Laptop stand" }, |
| 14 | + { id: "tent", label: "Tent stand" }, |
| 15 | +] as const; |
| 16 | + |
| 17 | +/** |
| 18 | + * Fold shapes move only the hinge, so the device opens around whichever edge it |
| 19 | + * currently rests on: a vertical phone opens as a book into a landscape tablet, |
| 20 | + * a horizontal one as a laptop into a portrait tablet. Stands are native presets |
| 21 | + * that also place the device. Pinching supplies continuous hinge control. |
| 22 | + */ |
12 | 23 | export function DeviceDuoControls(props: { |
13 | 24 | screen: DeviceScreenSize; |
14 | 25 | state: DuoControlState; |
15 | 26 | enabled: boolean; |
16 | 27 | onCommand: (command: DuoCommand) => void; |
17 | 28 | }) { |
18 | | - const angle = props.screen.hingeAngle; |
19 | | - const fold = angle == null ? null : angle === 0 ? "closed" : angle === 180 ? "open" : "book"; |
20 | | - const selected = (id: (typeof DUO_POSES)[number]["id"]) => |
21 | | - id === "laptop" || id === "tent" ? props.screen.hingePose === id : fold === id; |
| 29 | + const { screen } = props; |
| 30 | + const angle = screen.hingeAngle ?? (screen.screenId === 1 ? 0 : 180); |
| 31 | + const stand = screen.hingePose === "laptop" || screen.hingePose === "tent"; |
| 32 | + // The inner panel is mounted a quarter turn from the cover. |
| 33 | + const landscape = screen.orientation.startsWith("landscape"); |
| 34 | + const phoneVertical = screen.screenId === 3 ? landscape : !landscape; |
| 35 | + const fold = angle === 0 ? "closed" : angle === 180 ? "open" : "half"; |
| 36 | + const foldLabels = { |
| 37 | + closed: "Closed", |
| 38 | + half: phoneVertical ? "Book" : "Laptop", |
| 39 | + open: "Open", |
| 40 | + }; |
| 41 | + const button = ( |
| 42 | + key: string, |
| 43 | + label: string, |
| 44 | + pressed: boolean, |
| 45 | + onClick: () => void, |
| 46 | + glyph: React.ReactNode, |
| 47 | + ) => ( |
| 48 | + <Tooltip key={key}> |
| 49 | + <TooltipTrigger |
| 50 | + render={ |
| 51 | + <Button |
| 52 | + size="icon" |
| 53 | + variant={pressed ? "secondary" : "ghost"} |
| 54 | + disabled={!props.enabled} |
| 55 | + aria-label={label} |
| 56 | + aria-pressed={pressed} |
| 57 | + data-pressed={pressed ? "" : undefined} |
| 58 | + onClick={onClick} |
| 59 | + /> |
| 60 | + } |
| 61 | + > |
| 62 | + {glyph} |
| 63 | + </TooltipTrigger> |
| 64 | + <TooltipPopup side="left">{label}</TooltipPopup> |
| 65 | + </Tooltip> |
| 66 | + ); |
| 67 | + const group = |
| 68 | + "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"; |
22 | 69 | return ( |
23 | 70 | <div aria-label="iPhone Duo stands" className="flex flex-col items-center gap-2"> |
24 | | - {([DUO_POSES.slice(0, 3), DUO_POSES.slice(3)] as const).map((poses, index) => ( |
25 | | - <div |
26 | | - key={poses[0]?.id} |
27 | | - role="group" |
28 | | - aria-label={index === 0 ? "Fold shape" : "Device stance"} |
29 | | - 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" |
30 | | - > |
31 | | - {poses.map((pose) => ( |
32 | | - <Tooltip key={pose.id}> |
33 | | - <TooltipTrigger |
34 | | - render={ |
35 | | - <Button |
36 | | - size="icon" |
37 | | - variant={selected(pose.id) ? "secondary" : "ghost"} |
38 | | - disabled={!props.enabled} |
39 | | - aria-label={`${pose.label} stand`} |
40 | | - aria-pressed={selected(pose.id)} |
41 | | - data-pressed={selected(pose.id) ? "" : undefined} |
42 | | - onClick={() => props.onCommand({ control: "pose", value: pose.id })} |
43 | | - /> |
44 | | - } |
45 | | - > |
46 | | - <DeviceDuoGlyph pose={pose.id} /> |
47 | | - </TooltipTrigger> |
48 | | - <TooltipPopup side="left"> |
49 | | - {pose.label} |
50 | | - {pose.id === "book" ? " / bookshelf" : ""} |
51 | | - </TooltipPopup> |
52 | | - </Tooltip> |
53 | | - ))} |
54 | | - </div> |
55 | | - ))} |
| 71 | + <div role="group" aria-label="Fold shape" className={group}> |
| 72 | + {FOLDS.map(({ id, angle: value }) => |
| 73 | + button( |
| 74 | + id, |
| 75 | + foldLabels[id], |
| 76 | + !stand && fold === id, |
| 77 | + () => props.onCommand({ control: "angle", value }), |
| 78 | + <DeviceDuoGlyph pose={id === "half" ? "book" : id} vertical={phoneVertical} />, |
| 79 | + ), |
| 80 | + )} |
| 81 | + </div> |
| 82 | + <div role="group" aria-label="Device stance" className={group}> |
| 83 | + {STANDS.map(({ id, label }) => |
| 84 | + button( |
| 85 | + id, |
| 86 | + label, |
| 87 | + screen.hingePose === id, |
| 88 | + () => props.onCommand({ control: "pose", value: id }), |
| 89 | + <DeviceDuoGlyph pose={id} />, |
| 90 | + ), |
| 91 | + )} |
| 92 | + </div> |
56 | 93 | {props.state.error ? ( |
57 | 94 | <Tooltip> |
58 | 95 | <TooltipTrigger |
|
0 commit comments