Skip to content

Commit

Permalink
1f pan - drag
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Sep 21, 2019
1 parent 34a3017 commit 2b0282f
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Blink/SpaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ class SpaceController: UICollectionViewController {

public override func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
collectionView.isScrollEnabled = false
currentTerm()?.termDevice.view?.dropTouches()
// currentTerm()?.termDevice.view?.dropTouches()
}

public override func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
Expand Down
11 changes: 11 additions & 0 deletions Blink/UIKit/UIView+Touches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
import UIKit

extension UIView {
func dropSuperViewTouches() {
if let superview = superview {
if let recognizers = gestureRecognizers {
for r in recognizers {
r.dropTouches()
}
}
superview.dropSuperViewTouches()
}
}

func dropTouches() {
if let recognizers = gestureRecognizers {
for r in recognizers {
Expand Down
129 changes: 101 additions & 28 deletions Blink/WebKit/WKWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,26 @@ class UIScrollViewWithoutHitTest: UIScrollView {
}
}

/**
Gestures:

- 1 finger tap - reports click
- 2 finger pan - reports mouse wheel
- 2 finger long press and 1 finger pan reports mouse move
*/

@objc class WKWebViewGesturesInteraction: NSObject, UIInteraction {
var view: UIView? = nil
private var _wkWebView: WKWebView? = nil
private var _scrollView = UIScrollViewWithoutHitTest()
private var _jsScrollerPath: String
private var _panGestureRecognizer = UIPanGestureRecognizer()
private var _tapGestureRecognizer = UITapGestureRecognizer()
private var _2fPanRecognizer = UIPanGestureRecognizer()
private var _2fLongPressRecognizer = UILongPressGestureRecognizer()
private var _1fTapRecognizer = UITapGestureRecognizer()
private var _1fPanRecognizer = UIPanGestureRecognizer()

@objc init(jsScrollerPath: String) {
_jsScrollerPath = jsScrollerPath
super.init()
_scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
_scrollView.alwaysBounceVertical = true
_scrollView.alwaysBounceHorizontal = false
_scrollView.isDirectionalLockEnabled = true
_scrollView.keyboardDismissMode = .interactive
_scrollView.delaysContentTouches = false
_scrollView.delegate = self

_panGestureRecognizer.minimumNumberOfTouches = 2
_panGestureRecognizer.maximumNumberOfTouches = 2
_panGestureRecognizer.addTarget(self, action: #selector(_onPan(_:)))
_panGestureRecognizer.delegate = self

_tapGestureRecognizer.numberOfTapsRequired = 1
_tapGestureRecognizer.numberOfTouchesRequired = 1
_tapGestureRecognizer.addTarget(self, action: #selector(_onTap(_:)))
_tapGestureRecognizer.delegate = self
var allRecognizers:[UIGestureRecognizer] {
[_2fLongPressRecognizer, _2fPanRecognizer, _1fTapRecognizer, _1fPanRecognizer, _scrollView.panGestureRecognizer]
}

func willMove(to view: UIView?) {
Expand All @@ -90,26 +82,60 @@ class UIScrollViewWithoutHitTest: UIScrollView {


webView.addSubview(_scrollView)
webView.addGestureRecognizer(_scrollView.panGestureRecognizer)
webView.configuration.userContentController.add(self, name: "wkScroller")
webView.addGestureRecognizer(_panGestureRecognizer)
webView.addGestureRecognizer(_tapGestureRecognizer)


for r in allRecognizers {
webView.addGestureRecognizer(r)
}

_wkWebView = webView
} else {
_scrollView.removeFromSuperview()
}
}

@objc init(jsScrollerPath: String) {
_jsScrollerPath = jsScrollerPath
super.init()
_scrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
_scrollView.alwaysBounceVertical = true
_scrollView.alwaysBounceHorizontal = false
_scrollView.isDirectionalLockEnabled = true
_scrollView.keyboardDismissMode = .interactive
_scrollView.delaysContentTouches = false
_scrollView.delegate = self

_2fPanRecognizer.minimumNumberOfTouches = 2
_2fPanRecognizer.maximumNumberOfTouches = 2
_2fPanRecognizer.delegate = self
_2fPanRecognizer.addTarget(self, action: #selector(_on2fPan(_:)))

_1fTapRecognizer.numberOfTapsRequired = 1
_1fTapRecognizer.numberOfTouchesRequired = 1
_1fTapRecognizer.delegate = self
_1fTapRecognizer.addTarget(self, action: #selector(_on1fTap(_:)))

_1fPanRecognizer.minimumNumberOfTouches = 1
_1fPanRecognizer.maximumNumberOfTouches = 1
_1fPanRecognizer.delegate = self
_1fPanRecognizer.addTarget(self, action: #selector(_on1fPan(_:)))

_2fLongPressRecognizer.numberOfTouchesRequired = 2
_2fLongPressRecognizer.numberOfTapsRequired = 0
_2fLongPressRecognizer.delegate = self
_2fLongPressRecognizer.addTarget(self, action: #selector(_on2fLongPress(_:)))
}

private var _reportedY:CGFloat = 0

@objc func _onPan(_ recognizer: UIPanGestureRecognizer) {
@objc func _on2fPan(_ recognizer: UIPanGestureRecognizer) {
let point = recognizer.location(in: recognizer.view)

switch recognizer.state {
case .began:
_scrollView.panGestureRecognizer.dropTouches()
_2fLongPressRecognizer.dropTouches()

_scrollView.isScrollEnabled = false
_scrollView.showsVerticalScrollIndicator = false
_reportedY = point.y
Expand All @@ -130,7 +156,7 @@ class UIScrollViewWithoutHitTest: UIScrollView {
}
}

@objc func _onTap(_ recognizer: UITapGestureRecognizer) {
@objc func _on1fTap(_ recognizer: UITapGestureRecognizer) {
let point = recognizer.location(in: recognizer.view)
switch recognizer.state {
case .recognized:
Expand All @@ -140,12 +166,59 @@ class UIScrollViewWithoutHitTest: UIScrollView {
}
}

private var _is2fLongPressing = false

@objc func _on2fLongPress(_ recognizer: UILongPressGestureRecognizer) {
switch recognizer.state {
case .began:
debugPrint("2f start");
_scrollView.isScrollEnabled = false
recognizer.view?.superview?.dropSuperViewTouches()
_2fPanRecognizer.dropTouches()
_is2fLongPressing = true
case .ended: fallthrough
case .failed: fallthrough
case .cancelled:
debugPrint("2f end");
_is2fLongPressing = false
_scrollView.isScrollEnabled = true
default: break
}
}

@objc func _on1fPan(_ recognizer: UIPanGestureRecognizer) {
let point = recognizer.location(in: recognizer.view)
switch recognizer.state {
case .began:
_scrollView.panGestureRecognizer.dropTouches()
recognizer.view?.superview?.dropSuperViewTouches()
debugPrint("start");
_wkWebView?.evaluateJavaScript("term_reportMouseEvent(\"mousedown\", \(point.x), \(point.y), 1);", completionHandler: nil)
case .changed:
debugPrint("changed");
_wkWebView?.evaluateJavaScript("term_reportMouseEvent(\"mousemove\", \(point.x), \(point.y), 1);", completionHandler: nil)
case .ended: fallthrough
case .cancelled:
debugPrint("ended");
_wkWebView?.evaluateJavaScript("term_reportMouseEvent(\"mouseup\", \(point.x), \(point.y), 1);", completionHandler: nil)
default: break
}
}

}

extension WKWebViewGesturesInteraction: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if (_1fPanRecognizer == gestureRecognizer) {
debugPrint("shoud begin", _is2fLongPressing)
return _is2fLongPressing
}
return true
}
}

extension WKWebViewGesturesInteraction: UIScrollViewDelegate {
Expand Down
8 changes: 4 additions & 4 deletions Resources/term.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ function _setTermCoordinates(event, x, y) {
}

function term_reportTapInPoint(x, y) {
term_reportMouseEvent('mousedown', x, y);
term_reportMouseEvent('mouseup', x, y);
term_reportMouseEvent('mousedown', x, y, 1);
term_reportMouseEvent('mouseup', x, y, 1);
}

function term_reportMouseEvent(name, x, y) {
var event = new MouseEvent(name, {});
function term_reportMouseEvent(name, x, y, buttons) {
var event = new MouseEvent(name, {buttons});
_setTermCoordinates(event, x, y);
t.onMouse(event);
}
Expand Down

0 comments on commit 2b0282f

Please sign in to comment.