askrobots / network-computer-flutter
Public
One Flutter client for network-computer: drive a remote Linux/Mac desktop from Android, iPhone, iPad, or desktop over WebRTC.
Dan Quellhorst
No colour (compare) in ⋯: the desk's picture in grey on this device
c74471a
1 month, 3 weeks ago
History
session_screen.dart · 17.4 KB · 406 lines
| 1 | import 'package:flutter/foundation.dart'; |
| 2 | import 'dart:io'; |
| 3 | import 'package:flutter/material.dart'; |
| 4 | import 'package:shared_preferences/shared_preferences.dart'; |
| 5 | import 'package:flutter/services.dart'; |
| 6 | import 'package:flutter_webrtc/flutter_webrtc.dart'; |
| 7 | import 'package:provider/provider.dart'; |
| 8 | import 'protocol.dart'; |
| 9 | import 'peer.dart'; |
| 10 | import 'session_store.dart'; |
| 11 | import 'soft_keyboard.dart'; |
| 12 | import 'touch_pad.dart'; |
| 13 | |
| 14 | /// The live session. Touch as a trackpad by default: drag moves the pointer |
| 15 | /// relatively, tap clicks, two-finger drag scrolls (see touch_pad.dart). The |
| 16 | /// keyboard button brings up the device's keyboard with a bar of the keys it |
| 17 | /// lacks (see soft_keyboard.dart); a hardware keyboard types directly. |
| 18 | class SessionScreen extends StatefulWidget { |
| 19 | const SessionScreen({super.key}); |
| 20 | @override |
| 21 | State<SessionScreen> createState() => _SessionScreenState(); |
| 22 | } |
| 23 | |
| 24 | class _SessionScreenState extends State<SessionScreen> { |
| 25 | bool showStats = false; |
| 26 | // no colour: the desk's picture in grey on this device (the desk is |
| 27 | // unchanged), an A/B for whether things read by contrast alone |
| 28 | bool gray = false; |
| 29 | bool keyboardOpen = false; |
| 30 | double sensitivity = 2.0; |
| 31 | int _noticeSeen = 0; |
| 32 | |
| 33 | void _send(InputEvent e) => context.read<SessionStore>().send(e); |
| 34 | |
| 35 | @override |
| 36 | void initState() { |
| 37 | super.initState(); |
| 38 | SharedPreferences.getInstance().then((p) { |
| 39 | if (mounted && (p.getBool('gray') ?? false)) setState(() => gray = true); |
| 40 | }); |
| 41 | // Hardware keyboards (iPad, Bluetooth, USB, this Mac) are caught here, |
| 42 | // whatever has focus: tapping the voice button used to take focus from |
| 43 | // the keyboard's text field, and keys stopped reaching the desk. |
| 44 | HardwareKeyboard.instance.addHandler(_onHardwareKey); |
| 45 | } |
| 46 | |
| 47 | @override |
| 48 | void dispose() { |
| 49 | HardwareKeyboard.instance.removeHandler(_onHardwareKey); |
| 50 | super.dispose(); |
| 51 | } |
| 52 | |
| 53 | @override |
| 54 | Widget build(BuildContext context) { |
| 55 | final store = context.watch<SessionStore>(); |
| 56 | if (Platform.isIOS || Platform.isAndroid) { |
| 57 | store.deviceClass = MediaQuery.of(context).size.shortestSide < 600 ? 'phone' : 'tablet'; |
| 58 | } |
| 59 | if (store.noticeSeq != _noticeSeen) { |
| 60 | _noticeSeen = store.noticeSeq; |
| 61 | final msg = store.notice; |
| 62 | WidgetsBinding.instance.addPostFrameCallback((_) { |
| 63 | if (!mounted) return; |
| 64 | ScaffoldMessenger.of(context) |
| 65 | ..hideCurrentSnackBar() |
| 66 | ..showSnackBar(SnackBar(content: Text(msg), duration: const Duration(seconds: 2))); |
| 67 | }); |
| 68 | } |
| 69 | return Scaffold( |
| 70 | backgroundColor: Colors.black, |
| 71 | body: SafeArea( |
| 72 | // the key bar sits under the picture, not over it, so nothing on the |
| 73 | // desk hides behind it; the device's keyboard pushes both up |
| 74 | child: Column(children: [ |
| 75 | Expanded( |
| 76 | // expand: with only positioned children a loose Stack collapses |
| 77 | // to 0x0 and the whole session draws black |
| 78 | child: Stack( |
| 79 | fit: StackFit.expand, |
| 80 | children: [ |
| 81 | // video + trackpad |
| 82 | Positioned.fill(child: _trackpad(store)), |
| 83 | // top bar |
| 84 | _topBar(store), |
| 85 | if (showStats) _statsCard(store.stats), |
| 86 | // bottom left: voice panel and button |
| 87 | Positioned( |
| 88 | left: 0, bottom: 0, |
| 89 | child: Column( |
| 90 | mainAxisSize: MainAxisSize.min, |
| 91 | crossAxisAlignment: CrossAxisAlignment.start, |
| 92 | children: [ |
| 93 | if (store.voicePanel) _voicePanel(store), |
| 94 | _voiceButton(store), |
| 95 | ], |
| 96 | ), |
| 97 | ), |
| 98 | ], |
| 99 | ), |
| 100 | ), |
| 101 | if (keyboardOpen) SoftKeyboard(onEvent: _send), |
| 102 | ]), |
| 103 | ), |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | Widget _trackpad(SessionStore store) => TouchPad( |
| 108 | videoSize: () => Size(store.renderer.videoWidth.toDouble(), store.renderer.videoHeight.toDouble()), |
| 109 | onEvent: _send, |
| 110 | onInputKind: store.reportInput, |
| 111 | sensitivity: sensitivity, |
| 112 | child: _video(store), |
| 113 | ); |
| 114 | |
| 115 | Widget _video(SessionStore store) { |
| 116 | final view = RTCVideoView(store.renderer, objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain); |
| 117 | if (!gray) return view; |
| 118 | return ColorFiltered( |
| 119 | colorFilter: const ColorFilter.matrix(<double>[ |
| 120 | 0.2126, 0.7152, 0.0722, 0, 0, |
| 121 | 0.2126, 0.7152, 0.0722, 0, 0, |
| 122 | 0.2126, 0.7152, 0.0722, 0, 0, |
| 123 | 0, 0, 0, 1, 0, |
| 124 | ]), |
| 125 | child: view, |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | Future<void> _toggleGray() async { |
| 130 | setState(() => gray = !gray); |
| 131 | (await SharedPreferences.getInstance()).setBool('gray', gray); |
| 132 | } |
| 133 | |
| 134 | Widget _topBar(SessionStore store) { |
| 135 | final s = store.stats; |
| 136 | return Positioned( |
| 137 | top: 8, left: 8, right: 8, |
| 138 | child: Row(children: [ |
| 139 | _pill(Icon(Icons.circle, size: 10, |
| 140 | color: s.relayed ? const Color(0xFFFFB454) : const Color(0xFF3AD29F)), |
| 141 | '${s.relayed ? 'relay' : 'direct'} ${s.rttMs.toStringAsFixed(0)}ms' |
| 142 | '${store.mode == 'secure' ? '' : ' · insecure'}'), |
| 143 | const Spacer(), |
| 144 | _round(store.micOn ? Icons.mic : Icons.mic_off, |
| 145 | () => context.read<SessionStore>().toggleMic(), |
| 146 | active: store.micOn), |
| 147 | // on a phone the camera waits in ⋯ until it is on; on, it is always in |
| 148 | // sight (red), so nobody forgets the desk can see them |
| 149 | if (store.cameraOn || !_narrow) |
| 150 | _round(store.cameraOn ? Icons.videocam : Icons.videocam_off, |
| 151 | () => context.read<SessionStore>().toggleCamera(), |
| 152 | active: store.cameraOn), |
| 153 | _round(Icons.search, () => context.read<SessionStore>().launch()), |
| 154 | _round(Icons.keyboard, () => setState(() => keyboardOpen = !keyboardOpen)), |
| 155 | _more(), |
| 156 | _round(Icons.close, () => context.read<SessionStore>().disconnect()), |
| 157 | ]), |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | bool get _narrow => MediaQuery.of(context).size.width < 520; |
| 162 | bool get _mobile => |
| 163 | defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.android; |
| 164 | |
| 165 | /// Less frequent things, so the bar fits a phone. |
| 166 | Widget _more() => Padding( |
| 167 | padding: const EdgeInsets.only(left: 6), |
| 168 | child: Material( |
| 169 | color: const Color(0xCC1C2129), |
| 170 | shape: const CircleBorder(), |
| 171 | child: PopupMenuButton<String>( |
| 172 | icon: const Icon(Icons.more_horiz, size: 18), |
| 173 | onSelected: (v) { |
| 174 | final store = context.read<SessionStore>(); |
| 175 | switch (v) { |
| 176 | case 'screen': _screenSheet(); break; |
| 177 | case 'clipup': store.sendClipboard(); break; |
| 178 | case 'files': store.sendFiles(); break; |
| 179 | case 'clipdown': store.getClipboard(); break; |
| 180 | case 'stats': setState(() => showStats = !showStats); break; |
| 181 | case 'gray': _toggleGray(); break; |
| 182 | case 'camera': store.toggleCamera(); break; |
| 183 | case 'flip': store.flipCamera(); break; |
| 184 | } |
| 185 | }, |
| 186 | itemBuilder: (_) => [ |
| 187 | if (_narrow && !context.read<SessionStore>().cameraOn) |
| 188 | const PopupMenuItem(value: 'camera', child: ListTile(leading: Icon(Icons.videocam), title: Text('Camera on (the desk\'s webcam)'))), |
| 189 | if (_mobile && context.read<SessionStore>().cameraOn) |
| 190 | const PopupMenuItem(value: 'flip', child: ListTile(leading: Icon(Icons.cameraswitch), title: Text('Switch camera'))), |
| 191 | const PopupMenuItem(value: 'screen', child: ListTile(leading: Icon(Icons.monitor), title: Text('Screen size…'))), |
| 192 | const PopupMenuItem(value: 'files', child: ListTile(leading: Icon(Icons.upload_file), title: Text('Send a photo or file to the desk'))), |
| 193 | const PopupMenuItem(value: 'clipup', child: ListTile(leading: Icon(Icons.upload), title: Text("Send this clipboard to the desk"))), |
| 194 | const PopupMenuItem(value: 'clipdown', child: ListTile(leading: Icon(Icons.download), title: Text("Get the desk's clipboard"))), |
| 195 | PopupMenuItem(value: 'gray', child: ListTile(leading: const Icon(Icons.contrast), |
| 196 | title: Text(gray ? 'Colour' : 'No colour (compare)'))), |
| 197 | const PopupMenuItem(value: 'stats', child: ListTile(leading: Icon(Icons.bar_chart), title: Text('Stats'))), |
| 198 | ], |
| 199 | ), |
| 200 | ), |
| 201 | ); |
| 202 | |
| 203 | /// Screen size for the desk: match this device (or twice as sharp), or a preset. |
| 204 | void _screenSheet() { |
| 205 | final store = context.read<SessionStore>(); |
| 206 | final mq = MediaQuery.of(context); |
| 207 | int r8(double v) => (v / 8).floor() * 8; |
| 208 | (int, int) fit(double w, double h) { |
| 209 | final f = [1.0, 2560 / w, 1600 / h].reduce((a, b) => a < b ? a : b); |
| 210 | return (r8(w * f), r8(h * f)); |
| 211 | } |
| 212 | final m1 = fit(mq.size.width, mq.size.height); |
| 213 | final m2 = fit(mq.size.width * mq.devicePixelRatio, mq.size.height * mq.devicePixelRatio); |
| 214 | final sizes = <(String, int, int)>[ |
| 215 | ('Match this screen (${m1.$1}×${m1.$2})', m1.$1, m1.$2), |
| 216 | ('Match, sharp (${m2.$1}×${m2.$2}): use a bigger scale', m2.$1, m2.$2), |
| 217 | ('1280 × 720', 1280, 720), ('1600 × 900', 1600, 900), ('1920 × 1080', 1920, 1080), |
| 218 | ]; |
| 219 | var scale = store.displayScale; |
| 220 | showModalBottomSheet( |
| 221 | context: context, |
| 222 | backgroundColor: const Color(0xF21C2129), |
| 223 | builder: (ctx) => StatefulBuilder(builder: (ctx, set) => SafeArea( |
| 224 | child: Column(mainAxisSize: MainAxisSize.min, children: [ |
| 225 | const Padding(padding: EdgeInsets.all(12), child: Text('Desk screen size')), |
| 226 | Wrap(spacing: 6, children: [1.0, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0].map((v) => ChoiceChip( |
| 227 | label: Text('${(v * 100).round()}%'), |
| 228 | selected: scale == v, |
| 229 | onSelected: (_) => set(() => scale = v), |
| 230 | )).toList()), |
| 231 | ...sizes.map((sz) => ListTile( |
| 232 | title: Text(sz.$1), |
| 233 | selected: store.displayW == sz.$2 && store.displayH == sz.$3, |
| 234 | onTap: () { Navigator.pop(ctx); store.setDisplay(sz.$2, sz.$3, scale); }, |
| 235 | )), |
| 236 | ]), |
| 237 | )), |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | Widget _voiceButton(SessionStore store) => Padding( |
| 242 | padding: const EdgeInsets.all(12), |
| 243 | child: Material( |
| 244 | color: store.voiceOn ? const Color(0xFFD33333) : const Color(0xE61C2129), |
| 245 | shape: const CircleBorder(), |
| 246 | elevation: 4, |
| 247 | child: InkWell( |
| 248 | customBorder: const CircleBorder(), |
| 249 | onTap: () => context.read<SessionStore>().toggleVoice(), |
| 250 | child: const SizedBox(width: 56, height: 56, child: Icon(Icons.record_voice_over, size: 26)), |
| 251 | ), |
| 252 | ), |
| 253 | ); |
| 254 | |
| 255 | Widget _voicePanel(SessionStore store) => Padding( |
| 256 | padding: const EdgeInsets.only(left: 12), |
| 257 | child: Container( |
| 258 | width: MediaQuery.of(context).size.width.clamp(0, 440) - 24, |
| 259 | constraints: BoxConstraints(maxHeight: MediaQuery.of(context).size.height * 0.4), |
| 260 | decoration: BoxDecoration( |
| 261 | color: const Color(0xE6121620), |
| 262 | borderRadius: BorderRadius.circular(10), |
| 263 | ), |
| 264 | child: Column(mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ |
| 265 | Padding( |
| 266 | padding: const EdgeInsets.fromLTRB(12, 4, 4, 0), |
| 267 | child: Row(children: [ |
| 268 | Text(store.voiceState, style: const TextStyle(fontWeight: FontWeight.bold)), |
| 269 | const Spacer(), |
| 270 | const Text('keep listening', style: TextStyle(fontSize: 12, color: Color(0xFFAABBCC))), |
| 271 | Switch(value: store.keepListening, |
| 272 | onChanged: (v) => context.read<SessionStore>().setKeepListening(v)), |
| 273 | IconButton(icon: const Icon(Icons.close, size: 18), |
| 274 | onPressed: () => context.read<SessionStore>().hideVoicePanel()), |
| 275 | ]), |
| 276 | ), |
| 277 | Flexible(child: ListView( |
| 278 | shrinkWrap: true, |
| 279 | reverse: true, // newest at the bottom, scrolled into view |
| 280 | padding: const EdgeInsets.fromLTRB(12, 0, 12, 10), |
| 281 | children: store.voiceLines.reversed.map(_voiceLine).toList(), |
| 282 | )), |
| 283 | ]), |
| 284 | ), |
| 285 | ); |
| 286 | |
| 287 | Widget _voiceLine((String, String) l) { |
| 288 | final (kind, text) = l; |
| 289 | final (prefix, color) = switch (kind) { |
| 290 | 'heard' => ('You: ', Colors.white), |
| 291 | 'said' => ('Desk: ', const Color(0xFFBFE0FF)), |
| 292 | 'did' => ('✓ ', const Color(0xFF99DD99)), |
| 293 | _ => ('⚠ ', const Color(0xFFFFBB88)), |
| 294 | }; |
| 295 | return Padding( |
| 296 | padding: const EdgeInsets.only(top: 5), |
| 297 | child: Text('$prefix$text', style: TextStyle(color: color, fontSize: 14)), |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | Widget _pill(Widget leading, String text) => Container( |
| 302 | padding: const EdgeInsets.symmetric(horizontal: 11, vertical: 6), |
| 303 | decoration: BoxDecoration( |
| 304 | color: const Color(0xCC1C2129), |
| 305 | borderRadius: BorderRadius.circular(20), |
| 306 | ), |
| 307 | child: Row(mainAxisSize: MainAxisSize.min, children: [ |
| 308 | leading, const SizedBox(width: 7), |
| 309 | Text(text, style: const TextStyle(fontSize: 12.5)), |
| 310 | ]), |
| 311 | ); |
| 312 | |
| 313 | Widget _round(IconData icon, VoidCallback onTap, {bool active = false}) => Padding( |
| 314 | padding: const EdgeInsets.only(left: 6), |
| 315 | child: Material( |
| 316 | color: active ? const Color(0xFFFF5D5D) : const Color(0xCC1C2129), |
| 317 | shape: const CircleBorder(), |
| 318 | child: InkWell( |
| 319 | customBorder: const CircleBorder(), |
| 320 | onTap: onTap, |
| 321 | child: SizedBox(width: 40, height: 40, child: Icon(icon, size: 18)), |
| 322 | ), |
| 323 | ), |
| 324 | ); |
| 325 | |
| 326 | Widget _statsCard(PeerStats s) => Positioned( |
| 327 | top: 56, left: 12, |
| 328 | child: Container( |
| 329 | padding: const EdgeInsets.all(12), |
| 330 | decoration: BoxDecoration( |
| 331 | color: const Color(0xCC12151A), |
| 332 | borderRadius: BorderRadius.circular(10), |
| 333 | ), |
| 334 | child: DefaultTextStyle( |
| 335 | style: const TextStyle(fontFamily: 'monospace', fontSize: 12, color: Color(0xFF9AA4B2)), |
| 336 | child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [ |
| 337 | Text('${s.width}×${s.height} ${s.fps.toStringAsFixed(0)} fps'), |
| 338 | Text('${s.mbps.toStringAsFixed(1)} Mb/s ${s.codec}'), |
| 339 | Text('${s.path} ${s.rttMs.toStringAsFixed(0)} ms'), |
| 340 | Text('lost ${s.lost} jitter ${s.jitterMs.toStringAsFixed(1)} ms'), |
| 341 | ]), |
| 342 | ), |
| 343 | ), |
| 344 | ); |
| 345 | |
| 346 | final Map<LogicalKeyboardKey, String> _downAs = {}; // so each keyup matches its keydown |
| 347 | |
| 348 | bool _onHardwareKey(KeyEvent e) { |
| 349 | if (!mounted) return false; |
| 350 | if (e is KeyRepeatEvent) return true; |
| 351 | if (e is KeyUpEvent) { |
| 352 | final code = _downAs.remove(e.logicalKey); |
| 353 | if (code != null) _send(InputEvent('ku', code: code)); |
| 354 | return code != null; |
| 355 | } |
| 356 | final code = _mapKey(e); |
| 357 | if (code == null) return false; |
| 358 | _downAs[e.logicalKey] = code; |
| 359 | _send(InputEvent('kd', code: code)); |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | // Characters go as the US key that types them (the desk stays US), so any |
| 364 | // layout works; the rest by name. Cmd acts as Ctrl on the desk. |
| 365 | static const _usCode = { |
| 366 | '!': 'Digit1', '@': 'Digit2', '#': 'Digit3', r'$': 'Digit4', '%': 'Digit5', '^': 'Digit6', |
| 367 | '&': 'Digit7', '*': 'Digit8', '(': 'Digit9', ')': 'Digit0', '`': 'Backquote', '~': 'Backquote', |
| 368 | '-': 'Minus', '_': 'Minus', '=': 'Equal', '+': 'Equal', '[': 'BracketLeft', '{': 'BracketLeft', |
| 369 | ']': 'BracketRight', '}': 'BracketRight', '\\': 'Backslash', '|': 'Backslash', ';': 'Semicolon', |
| 370 | ':': 'Semicolon', "'": 'Quote', '"': 'Quote', ',': 'Comma', '<': 'Comma', '.': 'Period', |
| 371 | '>': 'Period', '/': 'Slash', '?': 'Slash', ' ': 'Space', |
| 372 | }; |
| 373 | static final _named = { |
| 374 | LogicalKeyboardKey.enter: 'Enter', LogicalKeyboardKey.numpadEnter: 'Enter', |
| 375 | LogicalKeyboardKey.backspace: 'Backspace', LogicalKeyboardKey.delete: 'Delete', |
| 376 | LogicalKeyboardKey.tab: 'Tab', LogicalKeyboardKey.escape: 'Escape', LogicalKeyboardKey.space: 'Space', |
| 377 | LogicalKeyboardKey.arrowUp: 'ArrowUp', LogicalKeyboardKey.arrowDown: 'ArrowDown', |
| 378 | LogicalKeyboardKey.arrowLeft: 'ArrowLeft', LogicalKeyboardKey.arrowRight: 'ArrowRight', |
| 379 | LogicalKeyboardKey.home: 'Home', LogicalKeyboardKey.end: 'End', |
| 380 | LogicalKeyboardKey.pageUp: 'PageUp', LogicalKeyboardKey.pageDown: 'PageDown', |
| 381 | LogicalKeyboardKey.shiftLeft: 'ShiftLeft', LogicalKeyboardKey.shiftRight: 'ShiftRight', |
| 382 | LogicalKeyboardKey.controlLeft: 'ControlLeft', LogicalKeyboardKey.controlRight: 'ControlRight', |
| 383 | LogicalKeyboardKey.altLeft: 'AltLeft', LogicalKeyboardKey.altRight: 'AltRight', |
| 384 | LogicalKeyboardKey.metaLeft: 'ControlLeft', LogicalKeyboardKey.metaRight: 'ControlRight', |
| 385 | LogicalKeyboardKey.capsLock: 'CapsLock', |
| 386 | LogicalKeyboardKey.f1: 'F1', LogicalKeyboardKey.f2: 'F2', LogicalKeyboardKey.f3: 'F3', |
| 387 | LogicalKeyboardKey.f4: 'F4', LogicalKeyboardKey.f5: 'F5', LogicalKeyboardKey.f6: 'F6', |
| 388 | LogicalKeyboardKey.f7: 'F7', LogicalKeyboardKey.f8: 'F8', LogicalKeyboardKey.f9: 'F9', |
| 389 | LogicalKeyboardKey.f10: 'F10', LogicalKeyboardKey.f11: 'F11', LogicalKeyboardKey.f12: 'F12', |
| 390 | }; |
| 391 | |
| 392 | String? _mapKey(KeyEvent e) { |
| 393 | final named = _named[e.logicalKey]; |
| 394 | if (named != null) return named; |
| 395 | var ch = e.character; |
| 396 | if (ch == null || ch.isEmpty || ch.codeUnitAt(0) < 32) { // with Ctrl held: a control code |
| 397 | final l = e.logicalKey.keyLabel; |
| 398 | ch = l.length == 1 ? l.toLowerCase() : null; |
| 399 | } |
| 400 | if (ch == null || ch.length != 1) return null; |
| 401 | final c = ch.toUpperCase().codeUnitAt(0); |
| 402 | if (c >= 65 && c <= 90) return 'Key${ch.toUpperCase()}'; |
| 403 | if (c >= 48 && c <= 57) return 'Digit$ch'; |
| 404 | return _usCode[ch]; |
| 405 | } |
| 406 | } |