Web app improvements

This commit is contained in:
Ilya Laktyushin 2022-04-08 15:36:59 +04:00
parent 43947a3dd3
commit 0659e69918
2 changed files with 44 additions and 1 deletions

View File

@ -262,7 +262,7 @@ public final class WebAppController: ViewController, AttachmentContainable {
} }
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) { func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
Queue.mainQueue().after(0.65, { Queue.mainQueue().after(1.0, {
let transition = ContainedViewLayoutTransition.animated(duration: 0.2, curve: .linear) let transition = ContainedViewLayoutTransition.animated(duration: 0.2, curve: .linear)
transition.updateAlpha(layer: webView.layer, alpha: 1.0) transition.updateAlpha(layer: webView.layer, alpha: 1.0)
if let placeholderNode = self.placeholderNode { if let placeholderNode = self.placeholderNode {
@ -283,9 +283,14 @@ public final class WebAppController: ViewController, AttachmentContainable {
decisionHandler(.prompt) decisionHandler(.prompt)
} }
private var targetContentOffset: CGPoint?
func scrollViewDidScroll(_ scrollView: UIScrollView) { func scrollViewDidScroll(_ scrollView: UIScrollView) {
let contentOffset = scrollView.contentOffset.y let contentOffset = scrollView.contentOffset.y
self.controller?.navigationBar?.updateBackgroundAlpha(min(30.0, contentOffset) / 30.0, transition: .immediate) self.controller?.navigationBar?.updateBackgroundAlpha(min(30.0, contentOffset) / 30.0, transition: .immediate)
if let targetContentOffset = self.targetContentOffset, scrollView.contentOffset != targetContentOffset {
scrollView.contentOffset = targetContentOffset
}
} }
private var validLayout: (ContainerViewLayout, CGFloat)? private var validLayout: (ContainerViewLayout, CGFloat)?
@ -298,8 +303,15 @@ public final class WebAppController: ViewController, AttachmentContainable {
let viewportFrame = CGRect(origin: CGPoint(x: layout.safeInsets.left, y: navigationBarHeight), size: CGSize(width: layout.size.width - layout.safeInsets.left - layout.safeInsets.right, height: max(1.0, layout.size.height - navigationBarHeight - layout.intrinsicInsets.bottom - layout.additionalInsets.bottom))) let viewportFrame = CGRect(origin: CGPoint(x: layout.safeInsets.left, y: navigationBarHeight), size: CGSize(width: layout.size.width - layout.safeInsets.left - layout.safeInsets.right, height: max(1.0, layout.size.height - navigationBarHeight - layout.intrinsicInsets.bottom - layout.additionalInsets.bottom)))
if previousLayout != nil && (previousLayout?.inputHeight ?? 0.0).isZero, let inputHeight = layout.inputHeight, inputHeight > 44.0, transition.isAnimated { if previousLayout != nil && (previousLayout?.inputHeight ?? 0.0).isZero, let inputHeight = layout.inputHeight, inputHeight > 44.0, transition.isAnimated {
webView.scrollToActiveElement(layout: layout, transition: transition)
Queue.mainQueue().after(0.4, { Queue.mainQueue().after(0.4, {
let contentOffset = webView.scrollView.contentOffset
transition.updateFrame(view: webView, frame: frame) transition.updateFrame(view: webView, frame: frame)
webView.scrollView.contentOffset = contentOffset
self.targetContentOffset = contentOffset
Queue.mainQueue().after(0.1) {
self.targetContentOffset = nil
}
}) })
} else { } else {
transition.updateFrame(view: webView, frame: frame) transition.updateFrame(view: webView, frame: frame)

View File

@ -4,6 +4,10 @@ import Display
import WebKit import WebKit
import SwiftSignalKit import SwiftSignalKit
private let findActiveElementY = """
document.activeElement.getBoundingClientRect().y
"""
private class WeakGameScriptMessageHandler: NSObject, WKScriptMessageHandler { private class WeakGameScriptMessageHandler: NSObject, WKScriptMessageHandler {
private let f: (WKScriptMessage) -> () private let f: (WKScriptMessage) -> ()
@ -104,6 +108,10 @@ final class WebAppWebView: WKWebView {
} }
contentView?.removeInteraction(dragInteraction) contentView?.removeInteraction(dragInteraction)
}) })
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
} }
} }
@ -122,6 +130,29 @@ final class WebAppWebView: WKWebView {
self.didTouchOnce = true self.didTouchOnce = true
} }
func scrollToActiveElement(layout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
self.evaluateJavaScript(findActiveElementY, completionHandler: { result, _ in
if let result = result as? CGFloat {
Queue.mainQueue().async {
let convertedY = result - self.scrollView.contentOffset.y
let viewportHeight = self.frame.height - (layout.inputHeight ?? 0.0)
if convertedY < 0.0 || convertedY > viewportHeight {
let targetOffset: CGFloat
if convertedY < 0.0 {
targetOffset = max(0.0, result - 36.0)
} else {
targetOffset = max(0.0, result + 60.0 - viewportHeight)
}
transition.animateView({
self.scrollView.contentOffset = CGPoint(x: 0.0, y: targetOffset)
})
// transition.updateBounds(layer: self.scrollView.layer, bounds: CGRect(x: 0.0, y: targetOffset, width: self.scrollView.layer.bounds.width, height: self.scrollView.layer.bounds.height))
}
}
}
})
}
override var inputAccessoryView: UIView? { override var inputAccessoryView: UIView? {
return nil return nil
} }