From 034680d9f413dfbfa5457a2d9be062406e26eecf Mon Sep 17 00:00:00 2001 From: "Joshua A. Horton" Date: Fri, 16 Nov 2018 15:13:52 +0700 Subject: [PATCH] Heavily reworks much of the iOS app rotation code, simplifying it. --- .../Classes/InputViewController.swift | 22 +++--- .../Classes/KeymanWebViewController.swift | 21 ++++++ .../KMEI/KeymanEngine/Classes/Manager.swift | 70 +++++++++++-------- .../Contents/Resources/keyboard.html | 1 + .../xcshareddata/WorkspaceSettings.xcsettings | 8 +++ 5 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 ios/keymanios.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings diff --git a/ios/engine/KMEI/KeymanEngine/Classes/InputViewController.swift b/ios/engine/KMEI/KeymanEngine/Classes/InputViewController.swift index 0a598ad105..ea32f423b3 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/InputViewController.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/InputViewController.swift @@ -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 ?? "" diff --git a/ios/engine/KMEI/KeymanEngine/Classes/KeymanWebViewController.swift b/ios/engine/KMEI/KeymanEngine/Classes/KeymanWebViewController.swift index 1dee45104d..f46b6bf083 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/KeymanWebViewController.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/KeymanWebViewController.swift @@ -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 diff --git a/ios/engine/KMEI/KeymanEngine/Classes/Manager.swift b/ios/engine/KMEI/KeymanEngine/Classes/Manager.swift index 66212fa4bc..90bfee5e2e 100644 --- a/ios/engine/KMEI/KeymanEngine/Classes/Manager.swift +++ b/ios/engine/KMEI/KeymanEngine/Classes/Manager.swift @@ -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() diff --git a/ios/engine/KMEI/KeymanEngine/resources/Keyman.bundle/Contents/Resources/keyboard.html b/ios/engine/KMEI/KeymanEngine/resources/Keyman.bundle/Contents/Resources/keyboard.html index a1129401c6..9a68a67d8d 100644 --- a/ios/engine/KMEI/KeymanEngine/resources/Keyman.bundle/Contents/Resources/keyboard.html +++ b/ios/engine/KMEI/KeymanEngine/resources/Keyman.bundle/Contents/Resources/keyboard.html @@ -50,6 +50,7 @@ function setOskHeight(height) { oskHeight = height; var kmw=window['keyman']; + kmw.osk.show(true); kmw['correctOSKTextSize'](); } diff --git a/ios/keymanios.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/ios/keymanios.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000000..949b678982 --- /dev/null +++ b/ios/keymanios.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + BuildSystemType + Original + +