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
touch_pad.dart · 6.9 KB · 205 lines
| 1 | import 'package:flutter/gestures.dart'; |
| 2 | import 'package:flutter/widgets.dart'; |
| 3 | import 'protocol.dart'; |
| 4 | |
| 5 | /// Touch on the desk's picture, the usual way for a remote desktop on a |
| 6 | /// tablet or phone: |
| 7 | /// |
| 8 | /// tap left click where the finger is |
| 9 | /// double tap double click there |
| 10 | /// touch and hold, let go right click there (context menus) |
| 11 | /// two-finger tap right click too |
| 12 | /// touch and hold, move drag (windows, text selection) |
| 13 | /// one-finger drag move the pointer, like a trackpad |
| 14 | /// two-finger drag scroll |
| 15 | /// |
| 16 | /// A mouse or trackpad on the device (iPad, Mac) right-clicks as usual. |
| 17 | /// Positions are measured against the picture, which is fitted inside the |
| 18 | /// view ("contain"), not against the view: with bars around it, a touch would |
| 19 | /// otherwise land off target. |
| 20 | class TouchPad extends StatefulWidget { |
| 21 | const TouchPad({ |
| 22 | super.key, |
| 23 | required this.child, |
| 24 | required this.videoSize, |
| 25 | required this.onEvent, |
| 26 | this.onInputKind, |
| 27 | this.sensitivity = 2.0, |
| 28 | }); |
| 29 | |
| 30 | final Widget child; |
| 31 | final Size Function() |
| 32 | videoSize; // the desk picture's size in pixels (0x0 until known) |
| 33 | final void Function(InputEvent) onEvent; |
| 34 | |
| 35 | /// "touch" or "pointer" (mouse, trackpad, pen) at every press: the desk's |
| 36 | /// apps suit their controls to it. |
| 37 | final void Function(String kind)? onInputKind; |
| 38 | final double sensitivity; |
| 39 | |
| 40 | @override |
| 41 | State<TouchPad> createState() => _TouchPadState(); |
| 42 | } |
| 43 | |
| 44 | class _TouchPadState extends State<TouchPad> { |
| 45 | Size _box = Size.zero; |
| 46 | Offset _down = Offset.zero; // where the latest tap or hold began |
| 47 | bool _dragging = false; // touch-and-hold turned into a drag |
| 48 | int _fingers = 0; // fingers in the current scale gesture |
| 49 | // fingers down right now, counted directly: a second finger turns a tap |
| 50 | // into a two-finger gesture (the tap recognizer alone would still click) |
| 51 | final Map<int, Offset> _touches = {}; |
| 52 | int _mostTouches = 0; |
| 53 | double _touchMoved = 0; |
| 54 | DateTime _firstTouch = DateTime.now(); |
| 55 | Offset _lastTwoFingerSpot = Offset.zero; // the first of two fingers to lift |
| 56 | |
| 57 | void _send(InputEvent e) => widget.onEvent(e); |
| 58 | |
| 59 | Offset _norm(Offset p) { |
| 60 | final v = widget.videoSize(); |
| 61 | final bw = _box.width, bh = _box.height; |
| 62 | if (bw <= 0 || bh <= 0) return Offset.zero; |
| 63 | if (v.width <= 0 || v.height <= 0) { |
| 64 | return Offset((p.dx / bw).clamp(0.0, 1.0), (p.dy / bh).clamp(0.0, 1.0)); |
| 65 | } |
| 66 | final k = (bw / v.width) < (bh / v.height) ? bw / v.width : bh / v.height; |
| 67 | final w = v.width * k, h = v.height * k; |
| 68 | final left = (bw - w) / 2, top = (bh - h) / 2; |
| 69 | return Offset( |
| 70 | ((p.dx - left) / w).clamp(0.0, 1.0), |
| 71 | ((p.dy - top) / h).clamp(0.0, 1.0), |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | void _moveTo(Offset p) { |
| 76 | final n = _norm(p); |
| 77 | _send(InputEvent('mm', x: n.dx, y: n.dy)); |
| 78 | } |
| 79 | |
| 80 | void _click(int button, {int times = 1}) { |
| 81 | for (var i = 0; i < times; i++) { |
| 82 | _send(InputEvent('md', b: button)); |
| 83 | _send(InputEvent('mu', b: button)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @override |
| 88 | Widget build(BuildContext context) { |
| 89 | return LayoutBuilder( |
| 90 | builder: (context, box) { |
| 91 | _box = Size(box.maxWidth, box.maxHeight); |
| 92 | return Listener( |
| 93 | onPointerDown: (e) { |
| 94 | widget.onInputKind?.call(e.kind == PointerDeviceKind.touch ? 'touch' : 'pointer'); |
| 95 | if (e.kind != PointerDeviceKind.touch) { |
| 96 | _mostTouches = 0; // a mouse click is never part of a finger tap |
| 97 | return; |
| 98 | } |
| 99 | if (_touches.isEmpty) { |
| 100 | _mostTouches = 0; |
| 101 | _touchMoved = 0; |
| 102 | _firstTouch = DateTime.now(); |
| 103 | } |
| 104 | _touches[e.pointer] = e.localPosition; |
| 105 | if (_touches.length > _mostTouches) _mostTouches = _touches.length; |
| 106 | }, |
| 107 | onPointerMove: (e) { |
| 108 | final was = _touches[e.pointer]; |
| 109 | if (was == null) return; |
| 110 | _touchMoved += (e.localPosition - was).distance; |
| 111 | _touches[e.pointer] = e.localPosition; |
| 112 | }, |
| 113 | onPointerUp: (e) => _touchUp(e.pointer), |
| 114 | onPointerCancel: (e) => _touchUp(e.pointer), |
| 115 | child: GestureDetector( |
| 116 | behavior: HitTestBehavior.opaque, |
| 117 | // tap and double tap |
| 118 | onTapDown: (d) => _down = d.localPosition, |
| 119 | onTap: () { |
| 120 | if (_mostTouches < 2) { |
| 121 | _moveTo(_down); |
| 122 | _click(0); |
| 123 | } |
| 124 | }, |
| 125 | onDoubleTapDown: (d) => _down = d.localPosition, |
| 126 | onDoubleTap: () { |
| 127 | if (_mostTouches < 2) { |
| 128 | _moveTo(_down); |
| 129 | _click(0, times: 2); |
| 130 | } |
| 131 | }, |
| 132 | // touch and hold: right click, or a drag if the finger moves |
| 133 | onLongPressStart: (d) { |
| 134 | _down = d.localPosition; |
| 135 | _dragging = false; |
| 136 | _moveTo(_down); |
| 137 | }, |
| 138 | onLongPressMoveUpdate: (d) { |
| 139 | if (!_dragging) { |
| 140 | _dragging = true; |
| 141 | _send(InputEvent('md', b: 0)); |
| 142 | } |
| 143 | _moveTo(d.localPosition); |
| 144 | }, |
| 145 | onLongPressEnd: (d) { |
| 146 | if (_dragging) { |
| 147 | _send(InputEvent('mu', b: 0)); |
| 148 | } else { |
| 149 | _click(2); |
| 150 | } |
| 151 | _dragging = false; |
| 152 | }, |
| 153 | // one finger moves the pointer; two fingers scroll, or right click on a tap |
| 154 | onScaleStart: (d) => _fingers = d.pointerCount, |
| 155 | onScaleUpdate: (d) { |
| 156 | if (d.pointerCount > _fingers) _fingers = d.pointerCount; |
| 157 | if (d.pointerCount >= 2) { |
| 158 | final dy = -d.focalPointDelta.dy * 3, |
| 159 | dx = -d.focalPointDelta.dx * 3; |
| 160 | if (dx != 0 || dy != 0) _send(InputEvent('wh', dx: dx, dy: dy)); |
| 161 | } else if (_fingers == 1) { |
| 162 | _send( |
| 163 | InputEvent( |
| 164 | 'mr', |
| 165 | dx: d.focalPointDelta.dx * widget.sensitivity, |
| 166 | dy: d.focalPointDelta.dy * widget.sensitivity, |
| 167 | ), |
| 168 | ); |
| 169 | } |
| 170 | }, |
| 171 | onScaleEnd: (d) => _fingers = 0, |
| 172 | // a mouse or trackpad's own right button |
| 173 | onSecondaryTapDown: (d) => _down = d.localPosition, |
| 174 | onSecondaryTap: () { |
| 175 | _moveTo(_down); |
| 176 | _click(2); |
| 177 | }, |
| 178 | child: widget.child, |
| 179 | ), |
| 180 | ); |
| 181 | }, |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | // all fingers up: two fingers that neither moved nor lingered were a tap |
| 186 | void _touchUp(int pointer) { |
| 187 | final at = _touches.remove(pointer); |
| 188 | if (at == null || _touches.isNotEmpty) { |
| 189 | if (at != null) _lastTwoFingerSpot = at; |
| 190 | return; |
| 191 | } |
| 192 | final quick = |
| 193 | DateTime.now().difference(_firstTouch) < |
| 194 | const Duration(milliseconds: 400); |
| 195 | if (_mostTouches == 2 && _touchMoved < 12 && quick) { |
| 196 | _moveTo( |
| 197 | Offset( |
| 198 | (at.dx + _lastTwoFingerSpot.dx) / 2, |
| 199 | (at.dy + _lastTwoFingerSpot.dy) / 2, |
| 200 | ), |
| 201 | ); |
| 202 | _click(2); |
| 203 | } |
| 204 | } |
| 205 | } |