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.
TrackpadView.swift · 3.8 KB · 107 lines
| 1 | import UIKit |
| 2 | |
| 3 | /// Touch surface. Trackpad mode: one finger moves the pointer relatively, tap clicks, |
| 4 | /// two-finger tap right-clicks, two-finger drag scrolls, long-press then drag drags. |
| 5 | /// Direct mode: the touch position maps to the mirrored screen absolutely. |
| 6 | final class TrackpadView: UIView { |
| 7 | var send: ((InputEvent) -> Void)? |
| 8 | var trackpadMode = true |
| 9 | var sensitivity: CGFloat = 2.0 |
| 10 | |
| 11 | private var lastPan: CGPoint = .zero |
| 12 | private var dragging = false |
| 13 | |
| 14 | override init(frame: CGRect) { |
| 15 | super.init(frame: frame) |
| 16 | isMultipleTouchEnabled = true |
| 17 | backgroundColor = .clear |
| 18 | |
| 19 | let pan = UIPanGestureRecognizer(target: self, action: #selector(onPan)) |
| 20 | pan.maximumNumberOfTouches = 1 |
| 21 | addGestureRecognizer(pan) |
| 22 | |
| 23 | let scroll = UIPanGestureRecognizer(target: self, action: #selector(onScroll)) |
| 24 | scroll.minimumNumberOfTouches = 2 |
| 25 | scroll.maximumNumberOfTouches = 2 |
| 26 | addGestureRecognizer(scroll) |
| 27 | |
| 28 | let tap = UITapGestureRecognizer(target: self, action: #selector(onTap)) |
| 29 | addGestureRecognizer(tap) |
| 30 | |
| 31 | let rightTap = UITapGestureRecognizer(target: self, action: #selector(onRightTap)) |
| 32 | rightTap.numberOfTouchesRequired = 2 |
| 33 | addGestureRecognizer(rightTap) |
| 34 | |
| 35 | let hold = UILongPressGestureRecognizer(target: self, action: #selector(onHold)) |
| 36 | hold.minimumPressDuration = 0.35 |
| 37 | addGestureRecognizer(hold) |
| 38 | pan.require(toFail: hold) |
| 39 | } |
| 40 | required init?(coder: NSCoder) { fatalError() } |
| 41 | |
| 42 | private func normalised(_ p: CGPoint) -> (Double, Double) { |
| 43 | (Double(max(0, min(1, p.x / bounds.width))), Double(max(0, min(1, p.y / bounds.height)))) |
| 44 | } |
| 45 | |
| 46 | @objc private func onPan(_ g: UIPanGestureRecognizer) { |
| 47 | let p = g.location(in: self) |
| 48 | switch g.state { |
| 49 | case .began: lastPan = p |
| 50 | case .changed: |
| 51 | if trackpadMode { |
| 52 | let d = CGPoint(x: (p.x - lastPan.x) * sensitivity, y: (p.y - lastPan.y) * sensitivity) |
| 53 | send?(InputEvent(t: "mr", dx: Double(d.x), dy: Double(d.y))) |
| 54 | } else { |
| 55 | let (x, y) = normalised(p) |
| 56 | send?(InputEvent(t: "mm", x: x, y: y)) |
| 57 | } |
| 58 | lastPan = p |
| 59 | case .ended, .cancelled: |
| 60 | if dragging { send?(InputEvent(t: "mu", b: 0)); dragging = false } |
| 61 | default: break |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @objc private func onScroll(_ g: UIPanGestureRecognizer) { |
| 66 | guard g.state == .changed else { return } |
| 67 | let t = g.translation(in: self) |
| 68 | send?(InputEvent(t: "wh", dx: Double(-t.x), dy: Double(-t.y))) |
| 69 | g.setTranslation(.zero, in: self) |
| 70 | } |
| 71 | |
| 72 | @objc private func onTap(_ g: UITapGestureRecognizer) { |
| 73 | if !trackpadMode { |
| 74 | let (x, y) = normalised(g.location(in: self)) |
| 75 | send?(InputEvent(t: "mm", x: x, y: y)) |
| 76 | } |
| 77 | send?(InputEvent(t: "md", b: 0)) |
| 78 | send?(InputEvent(t: "mu", b: 0)) |
| 79 | } |
| 80 | |
| 81 | @objc private func onRightTap(_ g: UITapGestureRecognizer) { |
| 82 | send?(InputEvent(t: "md", b: 2)) |
| 83 | send?(InputEvent(t: "mu", b: 2)) |
| 84 | } |
| 85 | |
| 86 | @objc private func onHold(_ g: UILongPressGestureRecognizer) { |
| 87 | let p = g.location(in: self) |
| 88 | switch g.state { |
| 89 | case .began: |
| 90 | if !trackpadMode { let (x, y) = normalised(p); send?(InputEvent(t: "mm", x: x, y: y)) } |
| 91 | send?(InputEvent(t: "md", b: 0)) |
| 92 | dragging = true |
| 93 | lastPan = p |
| 94 | case .changed: |
| 95 | if trackpadMode { |
| 96 | send?(InputEvent(t: "mr", dx: Double((p.x - lastPan.x) * sensitivity), dy: Double((p.y - lastPan.y) * sensitivity))) |
| 97 | } else { |
| 98 | let (x, y) = normalised(p); send?(InputEvent(t: "mm", x: x, y: y)) |
| 99 | } |
| 100 | lastPan = p |
| 101 | case .ended, .cancelled: |
| 102 | send?(InputEvent(t: "mu", b: 0)) |
| 103 | dragging = false |
| 104 | default: break |
| 105 | } |
| 106 | } |
| 107 | } |