import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react"; import { HostRemoteControl } from "../components/living/HostRemoteControl"; import { RemoteControl } from "../components/living/RemoteControl"; import { PlaybackControls } from "../components/living/PlaybackControls"; import { RemoteTouchSettings } from "../components/living/PlaybackSettings"; import { StageTv } from "../components/living/Stage"; import { StatusIndicator } from "../components/living/StatusIndicator"; import { useTheaterMode } from "../components/living/use-theater-mode"; import { RemoteKeyboard } from "../components/living/RemoteKeyboard"; import { RemoteMouse, placeRemotePointer, remotePointerRoom } from "../components/living/RemoteMouse"; import { Couch } from "../components/living/Couch"; import { STATUS_CATALOG } from "../ui/media-status"; import { Btn } from "../components/living/primitives"; import type { RemoteControlRequest } from "../native/wire"; import type { RemoteInputEvent } from "../remote-control/input-session"; import { picturePoint, pictureRect, rotatePicturePoint, videoRotation } from "../media/video-geometry"; import { RemoteTouchpad } from "../remote-control/touchpad"; import { RemoteViewport } from "../remote-control/viewport"; import type { RoomPreferences } from "../lib/room-preferences"; import type { RemoteControlMode, RemoteInputPlatform } from "../remote-control/protocol"; import { useCopy } from "../ui/copy"; import { testMedia } from "./PlaybackPreviewPage"; import { PreviewInput } from "./preview-input"; const participants = [ { peerId: "preview-player-one", name: "Player 1" }, { peerId: "preview-player-two", name: "Player 2" }, { peerId: "preview-gamepad", name: "Player 3" }, ]; export function RemoteControlsPreview() { const { lang, t } = useCopy(); const en = lang === "en"; const [platform, setPlatform] = useState("windows"); const [mode, setMode] = useState("off"); const [simultaneous, setSimultaneous] = useState(false); const [requests, setRequests] = useState<(RemoteControlRequest & { name: string })[]>([]); const [keyboardOpen, setKeyboardOpen] = useState(false); const [lastInput, setLastInput] = useState(""); const mouseVideo = useRef(null); const mouseLayer = useRef(null); const mouseSurface = useRef(null); const [pointer, setPointer] = useState(null); const [touchMode, setTouchMode] = useState("direct"); const [releaseKey, setReleaseKey] = useState(10); const [stopKey, setStopKey] = useState(10); const [systemShortcut, setSystemShortcut] = useState(false); const [stopShortcutLabel, setStopShortcutLabel] = useState(null); const stopSnapshot = useRef({ key: stopKey, label: stopShortcutLabel, system: systemShortcut }); stopSnapshot.current = { key: stopKey, label: stopShortcutLabel, system: systemShortcut }; const shortcut = useCallback(async (key?: number) => { const current = stopSnapshot.current; if (key !== undefined) setStopKey(key); if (current.system) { const label = key !== undefined ? "Super + Escape" : current.label; if (key !== undefined) { current.key = key; current.label = label; setStopShortcutLabel(label); } return { key: key ?? current.key, available: label !== null, stopShortcutLabel: label }; } return { key: key ?? current.key, available: true }; }, []); const input = useCallback((event: RemoteInputEvent) => setLastInput(JSON.stringify(event)), []); const placePointer = useCallback((position: { x: number; y: number }) => { const layer = mouseLayer.current, video = mouseVideo.current; if (!layer || !video) return; const picture = pictureRect(video); if (!picture) return; const rect = layer.getBoundingClientRect(); const point = rotatePicturePoint(position, videoRotation(video)); placeRemotePointer(layer, picture.left - rect.left + point.x * picture.width, picture.top - rect.top + point.y * picture.height); }, []); useEffect(() => { const element = mouseVideo.current!; const surface = mouseSurface.current!; const view = new RemoteViewport(element, surface, () => placePointer(current.position)); const current = new RemoteTouchpad(() => { const rect = pictureRect(element); return rect ? { ...rect, rotation: videoRotation(element) } : null; }, input, deltaY => setLastInput(JSON.stringify({ type: "wheel", deltaY })), position => { view.follow(position, remotePointerRoom(mouseLayer.current)); placePointer(position); }, view); setPointer(current); const sample = testMedia(false, false); element.srcObject = sample.stream; void element.play().catch(() => {}); const wheel = (event: WheelEvent) => { event.preventDefault(); current.wheel(event.deltaY); }; surface.addEventListener("wheel", wheel, { passive: false }); const position = () => { view.refresh(); current.refresh(); }; const resize = new ResizeObserver(position); resize.observe(surface); element.addEventListener("resize", position); position(); return () => { resize.disconnect(); element.removeEventListener("resize", position); surface.removeEventListener("wheel", wheel); current.reset(); view.reset(); setPointer(null); element.srcObject = null; sample.stop(); }; }, [input, placePointer]); const closeKeyboard = () => { setKeyboardOpen(false); document.getElementById("preview-remote-keyboard-toggle")?.focus(); }; return <>

{t("remote.inputTools")}

{en ? "Expand the mini mouse and drag its lower half to move the pointer. Hold and move L or R to drag, or hold a button with one finger and move the lower half with another. Pinch or pan the picture with two fingers to adjust the local view. Tap the wheel to middle-click, swipe to scroll, or hold to drag. Try direct touch or the compact trackpad below. Input stays in the local log." : "展开迷你鼠标,拖动下半部移动光标。按住 L 或 R 后移动即可拖拽,也可用一指按住按钮、另一指拖动下半部。在画面上双指缩放或平移可调整本地视图。滚轮可轻点中击、滑动滚动、长按后拖动。下方可切换直接触摸或光标触控板,输入仅记录在本地。"}

setKeyboardOpen(value => !value)} />

{en ? "Latest input" : "最近一次输入"} {lastInput || (en ? "Touch the picture directly, or tap the mini mouse for pointer controls." : "直接触摸画面,或轻点迷你鼠标展开光标控制。")}

{ setSimultaneous(enabled); setRequests([]); }} onShortcut={platform === "linux" && !systemShortcut ? undefined : shortcut} onPolicy={async value => { setMode(value); setRequests([]); }} onDecide={async (id, allow) => setRequests(current => allow ? current.map(request => request.id === id ? { ...request, state: "active" } : request) : current.filter(request => request.id !== id))} />
{ const grant = requests.find(request => request.peerId === person.peerId && request.state !== "pending"); return { key: person.peerId, name: person.name, status: STATUS_CATALOG.connected, selectable: false, collaboration: Boolean(grant) }; })} />
; } function PermissionRequestPreview() { const { lang, t } = useCopy(); const en = lang === "en"; const [channel] = useState(() => new PreviewInput()); const state = useSyncExternalStore(channel.subscribe, channel.getSnapshot); const video = useRef(null); const [stream, setStream] = useState(null); const [theater, setTheater] = useTheaterMode(); useEffect(() => { const sample = testMedia(false, false); const element = video.current!; element.srcObject = sample.stream; setStream(sample.stream); void element.play().catch(() => {}); return () => { element.srcObject = null; sample.stop(); }; }, []); const hasRequest = state.state === "pending" || state.state === "active" || state.state === "suspended"; return

{en ? "Request remote control" : "权限申请"}

{en ? "Use the mouse button in the playback bar, then handle the request in the Host's remote-control window. The local sample picture does not control your computer." : "点击播控上的鼠标按钮,再到房主的远控浮窗处理请求。这里是本地示例画面,不会操作电脑。"}

}>
{en ? "Hover the picture to reveal its normal playback controls." : "鼠标移入画面,即可呼出正式播控。"}
; }