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

DeviceControlsRail.tsx

318 lines9.9 KB

1import type { DeviceHostSummary, DevicePlatform } from "@t3tools/contracts";
2import {
3 Camera,
4 ChevronLeft,
5 Home,
6 Keyboard,
7 Box,
8 Maximize,
9 MoreHorizontal,
10 PictureInPicture2,
11 Power,
12 RotateCcw,
13 SlidersHorizontal,
14 Smartphone,
15 Square,
16 Type,
17 X,
18 MoonIcon,
19 SunIcon,
20} from "lucide-react";
21import type { ReactNode } from "react";
22import { Button } from "~/components/ui/button";
23import {
24 Menu,
25 MenuItem,
26 MenuPopup,
27 MenuSeparator,
28 MenuTrigger,
29 MenuRadioGroup,
30 MenuRadioItem,
31 MenuRadioItemIndicator,
32} from "~/components/ui/menu";
33import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
34import type { DeviceStreamHandle, DeviceViewControls } from "./DeviceStreamView";
35import type { DeviceControls } from "./useDeviceControls";
36​
37export function DeviceControlsRail(props: {
38 platform: DevicePlatform;
39 hostKind: DeviceHostSummary["kind"] | undefined;
40 handle: DeviceStreamHandle | null;
41 view: DeviceViewControls;
42 controls: DeviceControls;
43 screenshotPending: boolean;
44 onScreenshot: () => void;
45 toolsOpen: boolean;
46 onTools: () => void;
47 onFloat: () => void;
48 onClose: () => void;
49 onPowerOff: () => void;
50}) {
51 const { view, handle, controls } = props;
52 const popupSide = "left";
53 const settings = controls.detail?.settings;
54 const inputDisabled = !handle?.inputConnected;
55 const runsHostCommands = props.hostKind === "local" || props.hostKind === "ssh";
56 const nextAppearance = settings?.appearance === "dark" ? "light" : "dark";
57 return (
58 <aside
59 aria-label="Device controls"
60 data-layout="rail"
61 className="pointer-events-none absolute inset-y-0 right-0 z-10 flex w-14 flex-col items-center gap-2 overflow-y-auto [justify-content:safe_center] py-3 pr-2 [scrollbar-width:none]"
62 >
63 <div className="pointer-events-auto flex shrink-0 flex-col items-center gap-1 overflow-y-auto rounded-full border border-border/50 bg-background/80 p-2 shadow-sm [scrollbar-width:none]">
64 <RailButton
65 tooltipSide={popupSide}
66 label="Home"
67 disabled={inputDisabled}
68 onClick={() => handle?.pressButton("home")}
69 >
70 <Home />
71 </RailButton>
72 {props.platform === "android" ? (
73 <>
74 <RailButton
75 tooltipSide={popupSide}
76 label="Back"
77 disabled={inputDisabled}
78 onClick={() => handle?.pressButton("back")}
79 >
80 <ChevronLeft />
81 </RailButton>
82 <RailButton
83 tooltipSide={popupSide}
84 label="Recents"
85 disabled={inputDisabled}
86 onClick={() => handle?.pressButton("recents")}
87 >
88 <Square />
89 </RailButton>
90 {runsHostCommands ? (
91 <Menu>
92 <MenuTrigger
93 render={
94 <Button
95 size="icon-sm"
96 variant="ghost"
97 aria-label="Rotate device"
98 disabled={controls.disabled}
99 />
100 }
101 >
102 <RotateCcw />
103 </MenuTrigger>
104 <MenuPopup side={popupSide}>
105 <MenuItem
106 onClick={() => void controls.act({ type: "setOrientation", value: "portrait" })}
107 >
108 Portrait
109 </MenuItem>
110 <MenuItem
111 onClick={() =>
112 void controls.act({ type: "setOrientation", value: "landscape_left" })
113 }
114 >
115 Landscape
116 </MenuItem>
117 </MenuPopup>
118 </Menu>
119 ) : null}
120 </>
121 ) : props.hostKind === "simlock" ? null : (
122 <RailButton
123 tooltipSide={popupSide}
124 label="Rotate device"
125 disabled={inputDisabled || !!view.keyboard?.attached}
126 onClick={() => handle?.rotate()}
127 >
128 <RotateCcw />
129 </RailButton>
130 )}
131 {runsHostCommands ? (
132 <>
133 <RailDivider />
134 <RailButton
135 tooltipSide={popupSide}
136 label={`Switch device to ${nextAppearance} mode`}
137 disabled={controls.disabled || !settings?.appearance}
138 onClick={() => void controls.act({ type: "setAppearance", value: nextAppearance })}
139 >
140 {settings?.appearance === "dark" ? <SunIcon /> : <MoonIcon />}
141 </RailButton>
142 <Menu>
143 <MenuTrigger
144 render={
145 <Button
146 size="icon-sm"
147 variant="ghost"
148 aria-label="Device text size"
149 title="Device text size"
150 disabled={controls.disabled || !settings?.textSize}
151 />
152 }
153 >
154 <Type />
155 </MenuTrigger>
156 <MenuPopup side={popupSide} className="min-w-40">
157 <MenuRadioGroup
158 value={settings?.textSize ?? ""}
159 onValueChange={(value) => {
160 if (
161 value === "small" ||
162 value === "default" ||
163 value === "large" ||
164 value === "extra-large"
165 )
166 void controls.act({ type: "setTextSize", value });
167 }}
168 >
169 {(
170 [
171 ["small", "Small"],
172 ["default", "Default"],
173 ["large", "Large"],
174 ["extra-large", "Extra large"],
175 ] as const
176 ).map(([value, label]) => (
177 <MenuRadioItem key={value} value={value}>
178 <span className="flex items-center gap-2">
179 <span className="flex-1">{label}</span>
180 <MenuRadioItemIndicator />
181 </span>
182 </MenuRadioItem>
183 ))}
184 </MenuRadioGroup>
185 </MenuPopup>
186 </Menu>
187 <RailButton
188 tooltipSide={popupSide}
189 label="Device tools"
190 pressed={props.toolsOpen}
191 onClick={props.onTools}
192 >
193 <SlidersHorizontal />
194 </RailButton>
195 </>
196 ) : null}
197 {props.hostKind === "simlock" ? null : (
198 <RailButton
199 tooltipSide={popupSide}
200 label={props.screenshotPending ? "Capturing screenshot" : "Save screenshot"}
201 disabled={!view.streaming || props.screenshotPending}
202 onClick={props.onScreenshot}
203 >
204 <Camera />
205 </RailButton>
206 )}
207 <Menu>
208 <MenuTrigger
209 render={
210 <Button
211 size="icon-sm"
212 variant="ghost"
213 aria-label="More device actions"
214 title="More device actions"
215 />
216 }
217 >
218 <MoreHorizontal />
219 </MenuTrigger>
220 <MenuPopup side={popupSide} align="end">
221 <MenuItem onClick={props.onFloat}>
222 <PictureInPicture2 />
223 Float device over chat
224 </MenuItem>
225 <MenuItem onClick={props.onClose}>
226 <X />
227 Close device panel
228 </MenuItem>
229 <MenuSeparator />
230 <MenuItem variant="destructive" onClick={props.onPowerOff}>
231 <Power />
232 {props.hostKind === "simlock" ? "Return device to the pool" : "Power off device"}
233 </MenuItem>
234 </MenuPopup>
235 </Menu>
236 <RailDivider />
237 <RailButton
238 tooltipSide={popupSide}
239 label="3D view"
240 pressed={view.phone}
241 disabled={!view.streaming || !!view.phoneUnavailableReason}
242 description={view.phoneUnavailableReason ?? undefined}
243 onClick={view.showPhone}
244 >
245 <Box />
246 </RailButton>
247 <RailButton
248 tooltipSide={popupSide}
249 label="Flat view"
250 pressed={!view.phone}
251 disabled={!view.streaming}
252 onClick={view.showFlat}
253 >
254 <Smartphone />
255 </RailButton>
256 {view.keyboard ? (
257 <RailButton
258 tooltipSide={popupSide}
259 label={view.keyboard.attached ? "Detach Magic Keyboard" : "Attach Magic Keyboard"}
260 pressed={view.keyboard.attached}
261 onClick={view.keyboard.toggle}
262 >
263 <Keyboard />
264 </RailButton>
265 ) : null}
266 {view.phone ? (
267 <RailButton tooltipSide={popupSide} label="Restore 3D view" onClick={view.resetView}>
268 <Maximize />
269 </RailButton>
270 ) : null}
271 </div>
272 {view.foldingControls}
273 </aside>
274 );
276​
277function RailDivider() {
278 return <div aria-hidden className="my-1 h-px w-5 shrink-0 bg-border/70" />;
280​
281function RailButton(props: {
282 tooltipSide: "left" | "bottom";
283 label: string;
284 description?: string | undefined;
285 disabled?: boolean;
286 pressed?: boolean;
287 onClick: () => void;
288 children: ReactNode;
289}) {
290 const button = (
291 <Button
292 size="icon-sm"
293 variant={props.pressed ? "secondary" : "ghost"}
294 aria-label={props.label}
295 aria-pressed={props.pressed}
296 disabled={props.disabled}
297 onClick={props.onClick}
298 >
299 {props.children}
300 </Button>
301 );
302 return (
303 <Tooltip>
304 {props.disabled && props.description ? (
305 <TooltipTrigger
306 render={
307 <span className="inline-flex" tabIndex={0} role="group" aria-label={props.label} />
308 }
309 >
310 {button}
311 </TooltipTrigger>
312 ) : (
313 <TooltipTrigger render={button} />
314 )}
315 <TooltipPopup side={props.tooltipSide}>{props.description ?? props.label}</TooltipPopup>
316 </Tooltip>
317 );