Skip to content

Commit

Permalink
More progress on WKWebView scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Sep 21, 2019
1 parent 0ee70dd commit a5be60c
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 23 deletions.
12 changes: 12 additions & 0 deletions Blink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
D2071B83215101DC000156D7 /* blink-compat.c in Sources */ = {isa = PBXBuildFile; fileRef = D2071B82215101DC000156D7 /* blink-compat.c */; };
D2071B86215135A9000156D7 /* openssl-compat.c in Sources */ = {isa = PBXBuildFile; fileRef = D2071B84215135A9000156D7 /* openssl-compat.c */; };
D2094662204D3FC5003C4F72 /* cacert.pem in Resources */ = {isa = PBXBuildFile; fileRef = D209465B204D3FC5003C4F72 /* cacert.pem */; };
D215AC30233521B600E164C3 /* WKWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D215AC2F233521B600E164C3 /* WKWebView.swift */; };
D2179F2D2136A5DC00B0850A /* GeoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = D2179F2C2136A5DC00B0850A /* GeoManager.m */; };
D2179F2F2136DBC600B0850A /* geo.m in Sources */ = {isa = PBXBuildFile; fileRef = D2179F2E2136DBC600B0850A /* geo.m */; };
D21A3FD721943BE200269705 /* dark-settings-ipad-29pt.png in Resources */ = {isa = PBXBuildFile; fileRef = D21A3FC621943BE200269705 /* dark-settings-ipad-29pt.png */; };
Expand Down Expand Up @@ -517,6 +518,7 @@
D2071B84215135A9000156D7 /* openssl-compat.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "openssl-compat.c"; sourceTree = "<group>"; };
D2071B87215135EF000156D7 /* openssl-compat.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "openssl-compat.h"; sourceTree = "<group>"; };
D209465B204D3FC5003C4F72 /* cacert.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = cacert.pem; sourceTree = "<group>"; };
D215AC2F233521B600E164C3 /* WKWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKWebView.swift; sourceTree = "<group>"; };
D215E59B2010C77E00D893EB /* TermJS.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TermJS.h; sourceTree = "<group>"; };
D2179F2B2136A5DC00B0850A /* GeoManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeoManager.h; sourceTree = "<group>"; };
D2179F2C2136A5DC00B0850A /* GeoManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GeoManager.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -755,6 +757,7 @@
0716B5231CFFAB9300268B5B /* Blink */ = {
isa = PBXGroup;
children = (
D215AC282335218E00E164C3 /* WebKit */,
D265FBC7231904B50017EAC4 /* Foundation */,
D2A52225231304A90010AC04 /* UIKit */,
D241CB93230406E2003D64A5 /* SmarterKeys */,
Expand Down Expand Up @@ -1195,6 +1198,14 @@
path = ViewControllers;
sourceTree = "<group>";
};
D215AC282335218E00E164C3 /* WebKit */ = {
isa = PBXGroup;
children = (
D215AC2F233521B600E164C3 /* WKWebView.swift */,
);
path = WebKit;
sourceTree = "<group>";
};
D21A3FC521943BE200269705 /* DarkAppIcon */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1830,6 +1841,7 @@
D2A5221E230D279B0010AC04 /* SmarterTermInput.swift in Sources */,
D241CBD523040734003D64A5 /* KBKey.swift in Sources */,
07E3AEB31D8F6775007BC086 /* BKSupportViewController.m in Sources */,
D215AC30233521B600E164C3 /* WKWebView.swift in Sources */,
D2A86D6920EB86F2000DF1DA /* SSHClientOptions.m in Sources */,
D27AD9BC222FDD3D00379872 /* xcall.m in Sources */,
07AA065B1E08B61F008310B7 /* PasscodeLockConfiguration.swift in Sources */,
Expand Down
18 changes: 14 additions & 4 deletions Blink/SpaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ class SpaceController: UICollectionViewController {
return termCell;
}

_touchOverlay.frame = view.bounds
view.addSubview(_touchOverlay)
_touchOverlay.touchDelegate = self
// _touchOverlay.frame = view.bounds
// view.addSubview(_touchOverlay)
// _touchOverlay.touchDelegate = self

_commandsHUD.delegate = self
_registerForNotifications()
Expand Down Expand Up @@ -869,7 +869,17 @@ extension SpaceController: UICollectionViewDelegateFlowLayout{
extension SpaceController: UICollectionViewDropDelegate {

func collectionView(_ collectionView: UICollectionView, dropSessionDidEnter session: UIDropSession) {
collectionView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
// collectionView.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
// collectionView.isPagingEnabled = false
}

func collectionView(_ collectionView: UICollectionView, dropSessionDidExit session: UIDropSession) {
// collectionView.transform = CGAffineTransform.identity
}

func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: UIDropSession) {
// collectionView.transform = CGAffineTransform.identity
// collectionView.isPagingEnabled = true
}

func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
Expand Down
24 changes: 8 additions & 16 deletions Blink/TermView.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,12 @@ - (void)_keyboardWillHide:(id)sender

@end

@implementation UIView (Scrolling)

- (void)dropTouches {
for (UIGestureRecognizer *rec in self.gestureRecognizers) {
BOOL isEnabled = rec.isEnabled;
[rec setEnabled:NO];
[rec setEnabled:isEnabled];
}
for (UIView *view in self.subviews) {
[view dropTouches];
}
}

@end


@interface TermView () <WKScriptMessageHandler>
@end

@implementation TermView {
WKWebView *_webView;
WKWebViewScroller *_scroller;

BOOL _focused;
BOOL _jsIsBusy;
Expand Down Expand Up @@ -173,6 +158,7 @@ - (void)_actualLayoutSubviews {

if (!CGRectEqualToRect(_webView.frame, webViewFrame)) {
_webView.frame = webViewFrame;
// _scroller.frame = webViewFrame;
}

_currentBounds = self.bounds;
Expand Down Expand Up @@ -208,6 +194,8 @@ - (void)_addWebView
_webView.scrollView.scrollEnabled = NO;
_webView.scrollView.panGestureRecognizer.enabled = NO;

_scroller = [_webView createScrollerWithJsScrollerPath:@"t.scrollPort_.scroller_"];

[self addSubview:_webView];
}

Expand Down Expand Up @@ -375,6 +363,10 @@ - (void)userContentController:(WKUserContentController *)userContentController
[_device viewWinSizeChanged:__winSizeFromJSON(data)];
} else if ([operation isEqualToString:@"terminalReady"]) {
[self _onTerminalReady:data];
// for (id interaction in _webView.subviews.firstObject.subviews.firstObject.interactions) {
// [_webView.subviews.firstObject.subviews.firstObject removeInteraction:interaction];
// }

[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0 initialSpringVelocity:0 options:kNilOptions animations:^{
_coverView.alpha = 0;
} completion:^(BOOL finished) {
Expand Down
121 changes: 121 additions & 0 deletions Blink/WebKit/WKWebView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//////////////////////////////////////////////////////////////////////////////////
//
// B L I N K
//
// Copyright (C) 2016-2019 Blink Mobile Shell Project
//
// This file is part of Blink.
//
// Blink is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Blink is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Blink. If not, see <http://www.gnu.org/licenses/>.
//
// In addition, Blink is also subject to certain additional terms under
// GNU GPL version 3 section 7.
//
// You should have received a copy of these additional terms immediately
// following the terms and conditions of the GNU General Public License
// which accompanied the Blink Source Code. If not, see
// <http://www.github.com/blinksh/blink>.
//
////////////////////////////////////////////////////////////////////////////////


import UIKit
import WebKit

extension WKWebView {

@objc(WKWebViewScroller)
class Scroller: UIScrollView, UIScrollViewDelegate {
@objc weak var wkWebView: WKWebView? = nil

let _jsScrollerPath: String

fileprivate init(frame: CGRect, jsScrollerPath: String) {
_jsScrollerPath = jsScrollerPath
super.init(frame: frame)

alwaysBounceVertical = true
alwaysBounceHorizontal = false
isDirectionalLockEnabled = true
keyboardDismissMode = .interactive
delaysContentTouches = false

self.delegate = self
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
wkWebView?.evaluateJavaScript("\(_jsScrollerPath).reportScroll(\(contentOffset.x), \(contentOffset.y));", completionHandler: nil)
}

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
if view === self {
return nil
}
return view
}

override func didMoveToSuperview() {
if (self.superview != nil) {
self.superview?.addGestureRecognizer(panGestureRecognizer)
}

}

}

class MessageHandler: NSObject, WKScriptMessageHandler {

private var _scroller: Scroller

init(scroller: Scroller) {
_scroller = scroller
}

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard
let msg = message.body as? [String: Any],
let op = msg["op"] as? String
else {
return
}

switch op {
case "resize":
_scroller.contentSize = NSCoder.cgSize(for: msg["contentSize"] as? String ?? "")
case "scrollTo":
let animated = msg["animated"] as? Bool == true
let x: CGFloat = msg["x"] as? CGFloat ?? 0
let y: CGFloat = msg["y"] as? CGFloat ?? 0
_scroller.setContentOffset(CGPoint(x: x, y: y), animated: animated)
default: break
}
}
}

@objc func createScroller(jsScrollerPath: String) -> Scroller {
let scroller = Scroller(frame: bounds, jsScrollerPath: jsScrollerPath)
scroller.wkWebView = self
scroller.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let handler = MessageHandler(scroller: scroller)
self.configuration.userContentController.add(handler, name: "wkScroller")
addSubview(scroller)
return scroller
}

}
4 changes: 2 additions & 2 deletions Resources/hterm_all.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion get_frameworks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

DEPS_VERSION="1.2.3"
DEPS_VERSION="1.2.4"

GHROOT="https://github.com/blinksh"

Expand Down

0 comments on commit a5be60c

Please sign in to comment.