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.
SessionView.swift · 4.0 KB · 87 lines
| 1 | import SwiftUI |
| 2 | |
| 3 | /// The live session. With an external display the phone becomes a trackpad and |
| 4 | /// keyboard; without one the video is on the phone with the trackpad layered on top. |
| 5 | struct SessionView: View { |
| 6 | @ObservedObject var store = SessionStore.shared |
| 7 | @ObservedObject var settings = Settings.shared |
| 8 | @State private var keyboard = false |
| 9 | @State private var showStats = true |
| 10 | @State private var stickyMods: Set<String> = [] |
| 11 | |
| 12 | private let modKeys: [(String, String)] = [("esc", "Escape"), ("tab", "Tab"), ("ctrl", "ControlLeft"), |
| 13 | ("alt", "AltLeft"), ("cmd", "MetaLeft"), ("⌫", "Backspace"), |
| 14 | ("←", "ArrowLeft"), ("↑", "ArrowUp"), ("↓", "ArrowDown"), ("→", "ArrowRight")] |
| 15 | |
| 16 | var body: some View { |
| 17 | ZStack(alignment: .top) { |
| 18 | Color.black.ignoresSafeArea() |
| 19 | if store.externalDisplayConnected { |
| 20 | VStack { |
| 21 | Spacer() |
| 22 | Text("Desktop is on the external display").foregroundStyle(.secondary) |
| 23 | Text("This screen is your trackpad").foregroundStyle(.tertiary).font(.footnote) |
| 24 | Spacer() |
| 25 | } |
| 26 | } else { |
| 27 | VideoView(track: store.videoTrack).ignoresSafeArea() |
| 28 | } |
| 29 | TrackpadRepresentable(trackpadMode: settings.trackpadMode) { store.send($0) } |
| 30 | .ignoresSafeArea() |
| 31 | KeyCaptureRepresentable(active: $keyboard) { store.send($0) } |
| 32 | .frame(width: 1, height: 1).opacity(0.01) |
| 33 | |
| 34 | VStack(spacing: 6) { |
| 35 | if showStats { statsLine } |
| 36 | Spacer() |
| 37 | modifierBar |
| 38 | } |
| 39 | .padding(.horizontal, 8) |
| 40 | } |
| 41 | .statusBarHidden(true) |
| 42 | .onDisappear { store.disconnect() } |
| 43 | } |
| 44 | |
| 45 | private var statsLine: some View { |
| 46 | let s = store.stats |
| 47 | return HStack(spacing: 10) { |
| 48 | Text(s.relayed ? "RELAY" : "direct").bold().foregroundStyle(s.relayed ? .orange : .green) |
| 49 | Text(String(format: "%.0f ms", s.rttMs)) |
| 50 | Text("\(s.width)x\(s.height)") |
| 51 | Text(String(format: "%.0f fps", s.fps)) |
| 52 | Text(String(format: "%.1f Mb/s", s.mbps)) |
| 53 | Spacer() |
| 54 | Button { keyboard.toggle() } label: { Image(systemName: "keyboard") } |
| 55 | Button { showStats.toggle() } label: { Image(systemName: "info.circle") } |
| 56 | Button { store.disconnect() } label: { Image(systemName: "xmark.circle.fill") } |
| 57 | } |
| 58 | .font(.system(size: 12, design: .monospaced)) |
| 59 | .padding(6) |
| 60 | .background(.black.opacity(0.55), in: RoundedRectangle(cornerRadius: 6)) |
| 61 | .foregroundStyle(.white) |
| 62 | } |
| 63 | |
| 64 | /// Keys the software keyboard lacks. ctrl/alt/cmd are sticky: tap to hold, tap to release. |
| 65 | private var modifierBar: some View { |
| 66 | ScrollView(.horizontal, showsIndicators: false) { |
| 67 | HStack(spacing: 6) { |
| 68 | ForEach(modKeys, id: \.1) { label, code in |
| 69 | let sticky = ["ControlLeft", "AltLeft", "MetaLeft"].contains(code) |
| 70 | Button { |
| 71 | if sticky { |
| 72 | if stickyMods.contains(code) { stickyMods.remove(code); store.send(InputEvent(t: "ku", code: code)) } |
| 73 | else { stickyMods.insert(code); store.send(InputEvent(t: "kd", code: code)) } |
| 74 | } else { |
| 75 | store.send(InputEvent(t: "kd", code: code)); store.send(InputEvent(t: "ku", code: code)) |
| 76 | } |
| 77 | } label: { |
| 78 | Text(label).font(.system(size: 13, design: .monospaced)).padding(.horizontal, 10).padding(.vertical, 6) |
| 79 | .background(stickyMods.contains(code) ? Color.accentColor : Color.white.opacity(0.15), in: RoundedRectangle(cornerRadius: 6)) |
| 80 | .foregroundStyle(.white) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | .padding(.bottom, 4) |
| 86 | } |
| 87 | } |