-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathActionBar.tsx
More file actions
386 lines (377 loc) · 13.3 KB
/
Copy pathActionBar.tsx
File metadata and controls
386 lines (377 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { Fragment, useCallback, useEffect, useRef } from "react";
import { MdOutlineContentPasteGo } from "react-icons/md";
import {
LuCable,
LuCamera,
LuExternalLink,
LuHardDrive,
LuMaximize,
LuScanText,
LuSettings,
LuSignal,
LuTerminal,
LuX,
} from "react-icons/lu";
import { FaKeyboard } from "react-icons/fa6";
import { Popover, PopoverButton, PopoverPanel } from "@headlessui/react";
import { CommandLineIcon } from "@heroicons/react/20/solid";
import { SplitButtonGroup, SplitButtonPrimary, SplitButtonCaret } from "@components/SplitButton";
import { cx } from "@/cva.config";
import {
useHidStore,
useMountMediaStore,
useSettingsStore,
useUiStore,
useVideoStore,
} from "@hooks/stores";
import { useDeviceUiNavigation } from "@hooks/useAppNavigation";
import { Button } from "@components/Button";
import Container from "@components/Container";
import PasteModal from "@components/popovers/PasteModal";
import WakeOnLanModal from "@components/popovers/WakeOnLan/Index";
import MountPopopover from "@components/popovers/MountPopover";
import ExtensionPopover from "@components/popovers/ExtensionPopover";
import { JsonRpcResponse, useJsonRpc } from "@hooks/useJsonRpc";
import { m } from "@localizations/messages.js";
export default function Actionbar({
requestFullscreen,
takeScreenshot,
}: {
requestFullscreen: () => Promise<void>;
takeScreenshot: () => void;
}) {
const { navigateTo } = useDeviceUiNavigation();
const { isVirtualKeyboardEnabled, setVirtualKeyboardEnabled } = useHidStore();
const {
setDisableVideoFocusTrap,
terminalType,
setTerminalType,
toggleSidebarView,
isOcrMode,
setOcrMode,
usbSerialConsoleEnabled,
setUsbSerialConsoleEnabled,
isEmbedMode,
} = useUiStore();
const { remoteVirtualMediaState } = useMountMediaStore();
const { width: videoWidth, height: videoHeight } = useVideoStore();
const { developerMode } = useSettingsStore();
const { send } = useJsonRpc();
useEffect(() => {
send("getUsbDevices", {}, (resp: JsonRpcResponse) => {
if ("error" in resp) return;
const devices = resp.result as { serial_console?: boolean };
setUsbSerialConsoleEnabled(devices.serial_console === true);
});
}, [send, setUsbSerialConsoleEnabled]);
// This is the only way to get a reliable state change for the popover
// at time of writing this there is no mount, or unmount event for the popover
const isOpen = useRef<boolean>(false);
const checkIfStateChanged = useCallback(
(open: boolean) => {
if (open !== isOpen.current) {
isOpen.current = open;
if (!open) {
setTimeout(() => {
setDisableVideoFocusTrap(false);
console.debug("Popover is closing. Returning focus trap to video");
}, 0);
}
}
},
[setDisableVideoFocusTrap],
);
return (
<Container className="border-b border-b-slate-800/20 bg-white dark:border-b-slate-300/20 dark:bg-slate-900">
<div
onKeyUp={e => e.stopPropagation()}
onKeyDown={e => e.stopPropagation()}
className="flex flex-wrap items-center justify-between gap-x-4 gap-y-2 py-1.5"
>
<div className="relative flex flex-wrap items-center gap-x-2 gap-y-2">
{developerMode && usbSerialConsoleEnabled ? (
<SplitButtonGroup>
<SplitButtonPrimary
icon={({ className }) => <CommandLineIcon className={className} />}
label={m.kvm_terminal()}
onClick={() => setTerminalType(terminalType === "kvm" ? "none" : "kvm")}
/>
<SplitButtonCaret
menuItems={[
{
label: "USB Serial Console",
icon: LuTerminal,
onClick: () => setTerminalType(terminalType === "cdcacm" ? "none" : "cdcacm"),
active: terminalType === "cdcacm",
},
]}
/>
</SplitButtonGroup>
) : developerMode ? (
<Button
size="XS"
theme="light"
text={m.kvm_terminal()}
LeadingIcon={({ className }) => <CommandLineIcon className={className} />}
onClick={() => setTerminalType(terminalType === "kvm" ? "none" : "kvm")}
/>
) : usbSerialConsoleEnabled ? (
<Button
size="XS"
theme="light"
text="USB Serial Console"
LeadingIcon={LuTerminal}
onClick={() => setTerminalType(terminalType === "cdcacm" ? "none" : "cdcacm")}
/>
) : null}
<Popover>
<SplitButtonGroup>
<PopoverButton
as={SplitButtonPrimary}
icon={MdOutlineContentPasteGo}
label={m.paste_text()}
onClick={() => setDisableVideoFocusTrap(true)}
/>
<SplitButtonCaret
menuItems={[
{
label: m.action_bar_copy_text(),
icon: LuScanText,
onClick: () => setOcrMode(!isOcrMode),
active: isOcrMode,
disabled: videoWidth === 0 || videoHeight === 0,
},
]}
/>
</SplitButtonGroup>
<PopoverPanel
anchor="bottom start"
transition
className={cx(
"z-10 flex w-[420px] origin-top flex-col overflow-visible!",
"flex origin-top flex-col transition duration-300 ease-out data-closed:translate-y-8 data-closed:opacity-0",
)}
>
{({ open }) => {
checkIfStateChanged(open);
return (
<div className="mx-auto w-full max-w-xl">
<PasteModal />
</div>
);
}}
</PopoverPanel>
</Popover>
<div className="relative">
<Popover>
<PopoverButton as={Fragment}>
<Button
size="XS"
theme="light"
text={m.action_bar_virtual_media()}
LeadingIcon={({ className }) => {
return (
<>
<LuHardDrive className={className} />
<div
className={cx(className, "h-2 w-2 rounded-full bg-blue-700", {
hidden: !remoteVirtualMediaState,
})}
/>
</>
);
}}
onClick={() => {
setDisableVideoFocusTrap(true);
}}
/>
</PopoverButton>
<PopoverPanel
anchor="bottom start"
transition
className={cx(
"z-10 flex w-[420px] origin-top flex-col overflow-visible!",
"flex origin-top flex-col transition duration-300 ease-out data-closed:translate-y-8 data-closed:opacity-0",
)}
>
{({ open }) => {
checkIfStateChanged(open);
return (
<div className="mx-auto w-full max-w-xl">
<MountPopopover />
</div>
);
}}
</PopoverPanel>
</Popover>
</div>
<div>
<Popover>
<PopoverButton as={Fragment}>
<Button
size="XS"
theme="light"
text={m.action_bar_wake_on_lan()}
onClick={() => {
setDisableVideoFocusTrap(true);
}}
LeadingIcon={({ className }) => (
<svg
className={className}
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="m15 20 3-3h2a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h2l3 3z" />
<path d="M6 8v1" />
<path d="M10 8v1" />
<path d="M14 8v1" />
<path d="M18 8v1" />
</svg>
)}
/>
</PopoverButton>
<PopoverPanel
anchor="bottom start"
transition
style={{
transitionProperty: "opacity",
}}
className={cx(
"z-10 flex w-[420px] origin-top flex-col overflow-visible!",
"flex origin-top flex-col transition duration-300 ease-out data-closed:translate-y-8 data-closed:opacity-0",
)}
>
{({ open }) => {
checkIfStateChanged(open);
return (
<div className="mx-auto w-full max-w-xl">
<WakeOnLanModal />
</div>
);
}}
</PopoverPanel>
</Popover>
</div>
<div className="hidden lg:block">
<Button
size="XS"
theme="light"
text={m.action_bar_virtual_keyboard()}
LeadingIcon={FaKeyboard}
onClick={() => setVirtualKeyboardEnabled(!isVirtualKeyboardEnabled)}
/>
</div>
</div>
<div className="flex flex-wrap items-center gap-x-2 gap-y-2">
<Popover>
<PopoverButton as={Fragment}>
<Button
size="XS"
theme="light"
text={m.action_bar_extension()}
LeadingIcon={LuCable}
onClick={() => {
setDisableVideoFocusTrap(true);
}}
/>
</PopoverButton>
<PopoverPanel
anchor="bottom start"
transition
className={cx(
"z-10 flex w-[420px] flex-col overflow-visible!",
"flex origin-top flex-col transition duration-300 ease-out data-closed:translate-y-8 data-closed:opacity-0",
)}
>
{({ open }) => {
checkIfStateChanged(open);
return <ExtensionPopover />;
}}
</PopoverPanel>
</Popover>
<div className="block lg:hidden">
<Button
size="XS"
theme="light"
text={m.action_bar_virtual_keyboard()}
LeadingIcon={FaKeyboard}
onClick={() => setVirtualKeyboardEnabled(!isVirtualKeyboardEnabled)}
/>
</div>
<div className="hidden md:block">
<Button
size="XS"
theme="light"
text={m.action_bar_connection_stats()}
LeadingIcon={({ className }) => (
<LuSignal className={cx(className, "mb-0.5 text-green-500")} strokeWidth={4} />
)}
onClick={() => {
toggleSidebarView("connection-stats");
}}
/>
</div>
{!isEmbedMode && (
<div>
<Button
size="XS"
theme="light"
text={m.action_bar_settings()}
LeadingIcon={LuSettings}
onClick={() => {
setDisableVideoFocusTrap(true);
navigateTo("/settings");
}}
/>
</div>
)}
<div className="hidden items-center gap-x-2 lg:flex">
<div className="h-4 w-px bg-slate-300 dark:bg-slate-600" />
<Button
size="XS"
theme="light"
text={m.action_bar_screenshot()}
LeadingIcon={LuCamera}
onClick={() => takeScreenshot()}
/>
{isEmbedMode ? (
<Button
size="XS"
theme="light"
text={m.close()}
LeadingIcon={LuX}
onClick={() => window.close()}
/>
) : (
<SplitButtonGroup>
<SplitButtonPrimary
icon={LuMaximize}
label={m.action_bar_fullscreen()}
onClick={() => requestFullscreen()}
/>
<SplitButtonCaret
menuItems={[
{
label: m.action_bar_compact_window(),
icon: LuExternalLink,
onClick: () => {
const url = new URL(window.location.href);
url.searchParams.set("embed", "");
window.open(url.toString(), "_blank", "noopener");
},
},
]}
/>
</SplitButtonGroup>
)}
</div>
</div>
</div>
</Container>
);
}