askrobots / network-computer-ios
Public
iPhone client for network-computer: see and drive a remote Linux/Mac desktop over WebRTC, phone as trackpad and keyboard, desktop on a TV via AirPlay.
VideoView.swift · 1.6 KB · 58 lines
| 1 | import SwiftUI |
| 2 | import WebRTC |
| 3 | |
| 4 | /// Metal-backed renderer for the remote desktop. |
| 5 | struct VideoView: UIViewRepresentable { |
| 6 | let track: RTCVideoTrack? |
| 7 | |
| 8 | func makeUIView(context: Context) -> RTCMTLVideoView { |
| 9 | let v = RTCMTLVideoView() |
| 10 | v.videoContentMode = .scaleAspectFit |
| 11 | v.backgroundColor = .black |
| 12 | return v |
| 13 | } |
| 14 | |
| 15 | func updateUIView(_ v: RTCMTLVideoView, context: Context) { |
| 16 | if context.coordinator.track !== track { |
| 17 | context.coordinator.track?.remove(v) |
| 18 | track?.add(v) |
| 19 | context.coordinator.track = track |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func makeCoordinator() -> Coordinator { Coordinator() } |
| 24 | final class Coordinator { var track: RTCVideoTrack? } |
| 25 | } |
| 26 | |
| 27 | struct TrackpadRepresentable: UIViewRepresentable { |
| 28 | let trackpadMode: Bool |
| 29 | let send: (InputEvent) -> Void |
| 30 | |
| 31 | func makeUIView(context: Context) -> TrackpadView { |
| 32 | let v = TrackpadView(frame: .zero) |
| 33 | v.send = send |
| 34 | return v |
| 35 | } |
| 36 | func updateUIView(_ v: TrackpadView, context: Context) { |
| 37 | v.trackpadMode = trackpadMode |
| 38 | v.send = send |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | struct KeyCaptureRepresentable: UIViewRepresentable { |
| 43 | @Binding var active: Bool |
| 44 | let send: (InputEvent) -> Void |
| 45 | |
| 46 | func makeUIView(context: Context) -> KeyCaptureView { |
| 47 | let v = KeyCaptureView(frame: .zero) |
| 48 | v.send = send |
| 49 | return v |
| 50 | } |
| 51 | func updateUIView(_ v: KeyCaptureView, context: Context) { |
| 52 | v.send = send |
| 53 | DispatchQueue.main.async { |
| 54 | if active, !v.isFirstResponder { v.becomeFirstResponder() } |
| 55 | if !active, v.isFirstResponder { v.resignFirstResponder() } |
| 56 | } |
| 57 | } |
| 58 | } |