Heavily reworks much of the iOS app rotation code, simplifying it.

This commit is contained in:
Joshua A. Horton 2018-11-16 15:13:52 +07:00
parent 321604732d
commit 034680d9f4
5 changed files with 82 additions and 40 deletions

View file

@ -136,6 +136,9 @@ open class InputViewController: UIInputViewController, KeymanWebDelegate {
containerView!.backgroundColor = bgColor
containerView!.addSubview(kmInputView)
view.addSubview(containerView!)
Manager.shared.initKeyboardSize()
Manager.shared.resizeKeyboard()
}
open override func viewDidAppear(_ animated: Bool) {
@ -158,19 +161,16 @@ open class InputViewController: UIInputViewController, KeymanWebDelegate {
super.viewWillDisappear(animated)
}
// open override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
// Manager.shared.inputViewWillRotate(to: toInterfaceOrientation, duration: duration)
// super.willRotate(to: toInterfaceOrientation, duration: duration)
// open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
// Manager.shared.setKeyboardSize(size: size)
// super.viewWillTransition(to: size, with: coordinator)
// coordinator.animateAlongsideTransition(in: nil, animation: nil, completion: {
// _ in
// self.updateViewConstraints()
// Manager.shared.resizeKeyboard(with: size)
// })
// }
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animateAlongsideTransition(in: nil, animation: nil, completion: {
_ in
self.updateViewConstraints()
})
}
open override func textDidChange(_ textInput: UITextInput?) {
let contextBeforeInput = textDocumentProxy.documentContextBeforeInput ?? ""
let contextAfterInput = textDocumentProxy.documentContextAfterInput ?? ""

View file

@ -31,6 +31,25 @@ class KeymanWebViewController: UIViewController {
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
Manager.shared.viewWillTransition(to: size, with: coordinator)
coordinator.animateAlongsideTransition(in: nil, animation: {
_ in
// What happens if we check this stuff at the rotation's end?
if let v = self.parent?.view! {
Manager.shared.resizeKeyboard(with: v.frame.size)
}
}, completion: {
_ in
if let v = self.parent?.view! {
Manager.shared.resizeKeyboard(with: v.frame.size)
self.updateViewConstraints()
}
})
}
override func loadView() {
let config = WKWebViewConfiguration()
@ -45,6 +64,8 @@ class KeymanWebViewController: UIViewController {
webView = WKWebView(frame: frame ?? .zero, configuration: config)
webView.isOpaque = false
//webView.autoresizingMask = UIViewAutoresizing.flexibleWidth
webView.autoresizingMask = UIViewAutoresizing.flexibleHeight.union(.flexibleWidth)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.backgroundColor = UIColor.clear
webView.navigationDelegate = self

View file

@ -59,8 +59,11 @@ public class Manager: NSObject, HTTPDownloadDelegate, UIGestureRecognizerDelegat
/// Display the help bubble on first use.
public var isKeymanHelpOn = true
public var isSystemKeyboard = false
/// Stores the keyboard view's current size.
private var kbSize: CGSize = CGSize.zero
// TODO: Change API to not disable removing as well
/// Allow users to add new keyboards in the keyboard picker.
@ -189,6 +192,10 @@ public class Manager: NSObject, HTTPDownloadDelegate, UIGestureRecognizerDelegat
* set the queue running, this should be perfectly fine.
*/
sharedQueue = HTTPDownloader.init(self)
// The system isn't actually able to get the proper keyboard size data yet,
// so we need to clear the initialization done by this method.
kbSize = CGSize.zero
}
// MARK: - Keyboard management
@ -879,18 +886,14 @@ public class Manager: NSObject, HTTPDownloadDelegate, UIGestureRecognizerDelegat
// MARK: - View management
public var keyboardHeight: CGFloat {
if isSystemKeyboard {
return keyboardHeight(isPortrait: InputViewController.isPortrait)
} else {
return keyboardHeight(isPortrait: UIDevice.current.orientation.isPortrait)
}
return kbSize.height
}
func keyboardHeight(with orientation: UIInterfaceOrientation) -> CGFloat {
return keyboardHeight(isPortrait: orientation.isPortrait)
}
// func keyboardHeight(with orientation: UIInterfaceOrientation) -> CGFloat {
// return initKeyboardHeight(isPortrait: orientation.isPortrait)
// }
func keyboardHeight(isPortrait: Bool) -> CGFloat {
func initKeyboardHeight(isPortrait: Bool) -> CGFloat {
let parentHeight: CGFloat = keymanWeb.parent != nil ? keymanWeb.parent!.view.frame.height : CGFloat(100.0)
if UIDevice.current.userInterfaceIdiom == .pad {
if isPortrait {
@ -910,9 +913,27 @@ public class Manager: NSObject, HTTPDownloadDelegate, UIGestureRecognizerDelegat
var keyboardWidth: CGFloat {
return UIScreen.main.bounds.width
}
func initKeyboardSize() {
kbSize.width = UIScreen.main.bounds.width
if isSystemKeyboard {
kbSize.height = initKeyboardHeight(isPortrait: InputViewController.isPortrait)
} else {
kbSize.height = initKeyboardHeight(isPortrait: UIDevice.current.orientation.isPortrait)
}
}
func setKeyboardSize(size: CGSize) {
kbSize = size
}
var keyboardSize: CGSize {
return CGSize(width: keyboardWidth, height: keyboardHeight)
if kbSize.equalTo(CGSize.zero) {
initKeyboardSize()
}
return kbSize
}
private var keymanScrollView: UIScrollView {
@ -1088,21 +1109,18 @@ public class Manager: NSObject, HTTPDownloadDelegate, UIGestureRecognizerDelegat
}
// Keyman interaction
private func resizeKeyboard() {
let newSize = keyboardSize
keymanWeb.frame = CGRect(origin: .zero, size: newSize)
keymanWeb.setOskWidth(Int(newSize.width))
keymanWeb.setOskHeight(Int(newSize.height))
func resizeKeyboard() {
resizeKeyboard(with: keyboardSize)
}
func resizeKeyboard(with orientation: UIInterfaceOrientation) {
// TODO: Update to use new size instead of orientation since viewWillRotate() is deprecated
func resizeKeyboard(with size: CGSize) {
setKeyboardSize(size: size)
// TODO: Refactor to use resizeKeyboard()
let kbWidth = keyboardWidth
let kbHeight = keyboardHeight(with: orientation)
keymanWeb.frame = CGRect(x: 0.0, y: 0.0, width: kbWidth, height: kbHeight)
let kbWidth = size.width
let kbHeight = size.height
keymanWeb.frame = keymanWeb.parent?.view.frame //CGRect(x: 0.0, y: 0.0, width: kbWidth, height: kbHeight)
keymanWeb.setOskWidth(Int(kbWidth))
keymanWeb.setOskHeight(Int(kbHeight))
}
@ -1431,12 +1449,6 @@ public class Manager: NSObject, HTTPDownloadDelegate, UIGestureRecognizerDelegat
dismissSubKeys()
dismissKeyPreview()
dismissKeyboardMenu()
var orientation = UIInterfaceOrientation.portrait
if size.width > size.height {
orientation = UIInterfaceOrientation.landscapeLeft;
}
resizeKeyboard(with: orientation)
if isKeymanHelpOn {
helpBubbleView?.removeFromSuperview()

View file

@ -50,6 +50,7 @@
function setOskHeight(height) {
oskHeight = height;
var kmw=window['keyman'];
kmw.osk.show(true);
kmw['correctOSKTextSize']();
}

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>