summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/ios
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2017-01-03 18:03:17 +0100
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2017-01-06 16:29:48 +0000
commit3c99bddb8469416b0042db6a39ec2e9d82b147a1 (patch)
treee6253c0ee534ba6de59a820ab8babdc51958ef1d /src/plugins/platforms/ios
parent34f82b8abcb279542b6350e70609c549e39caafb (diff)
iOS: Center IM cursor rectangle within available space when showing keyboard
The cursor rectangle is translated into screen coordinates, and compared against the screen geometry after subtracting the future keyboard rect (which is already in screen coordinates). If the two do not overlap completely, the root view is shifted accordingly so that the cursor rectangle is placed in the center of the available space. A future improvement would be to first check if centering the input item's clip rectangle would bring the cursor within the available geometry, before falling back to using the cursor rectangle. This would look better for multi-line text inputs where the cursor is not in the center. Task-number: QTBUG-46747 Change-Id: If9b551b4d297e2a1f6d7f84b81628fa65c08edfd Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src/plugins/platforms/ios')
-rw-r--r--src/plugins/platforms/ios/qiosinputcontext.mm35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/plugins/platforms/ios/qiosinputcontext.mm b/src/plugins/platforms/ios/qiosinputcontext.mm
index 68088540c6..9608b6eae7 100644
--- a/src/plugins/platforms/ios/qiosinputcontext.mm
+++ b/src/plugins/platforms/ios/qiosinputcontext.mm
@@ -498,15 +498,32 @@ void QIOSInputContext::scrollToCursor()
return;
}
- const int margin = 20;
- QRectF translatedCursorPos = qApp->inputMethod()->cursorRectangle();
- translatedCursorPos.translate(focusView().qwindow->geometry().topLeft());
-
- qreal keyboardY = [rootView convertRect:m_keyboardState.keyboardEndRect fromView:nil].origin.y;
- int statusBarY = qGuiApp->primaryScreen()->availableGeometry().y();
+ QWindow *focusWindow = qApp->focusWindow();
+ QRect cursorRect = qApp->inputMethod()->cursorRectangle().translated(focusWindow->geometry().topLeft()).toRect();
+ if (cursorRect.isNull()) {
+ scroll(0);
+ return;
+ }
- scroll((translatedCursorPos.bottomLeft().y() < keyboardY - margin) ? 0
- : qMin(rootView.bounds.size.height - keyboardY, translatedCursorPos.y() - statusBarY - margin));
+ // Add some padding so that the cusor does not end up directly above the keyboard
+ static const int kCursorRectPadding = 20;
+ cursorRect.adjust(0, -kCursorRectPadding, 0, kCursorRectPadding);
+
+ // We explicitly ask for the geometry of the screen instead of the availableGeometry,
+ // as we hide the statusbar when scrolling the screen, so the available geometry will
+ // include the space taken by the status bar at the moment.
+ QRect screenGeometry = focusWindow->screen()->geometry();
+ QRect keyboardGeometry = QRectF::fromCGRect(m_keyboardState.keyboardEndRect).toRect();
+ QRect availableGeometry = (QRegion(screenGeometry) - keyboardGeometry).boundingRect();
+
+ if (!availableGeometry.contains(cursorRect, true)) {
+ qImDebug() << "cursor rect" << cursorRect << "not fully within" << availableGeometry;
+ int scrollToCenter = -(availableGeometry.center() - cursorRect.center()).y();
+ int scrollToBottom = focusWindow->screen()->geometry().bottom() - availableGeometry.bottom();
+ scroll(qMin(scrollToCenter, scrollToBottom));
+ } else {
+ scroll(0);
+ }
}
void QIOSInputContext::scroll(int y)
@@ -519,6 +536,8 @@ void QIOSInputContext::scroll(int y)
if (CATransform3DEqualToTransform(translationTransform, rootView.layer.sublayerTransform))
return;
+ qImDebug() << "scrolling root view to y =" << -y;
+
QPointer<QIOSInputContext> self = this;
[UIView animateWithDuration:m_keyboardState.animationDuration delay:0
options:(m_keyboardState.animationCurve << 16) | UIViewAnimationOptionBeginFromCurrentState