iOS: Ensure that the cursor rect doesn't trigger an inverted scroll

If the cursor is at the top of the screen, it may end up with a cursor
rect that extends beyond the screen after we pad it. We need to make
sure it's constrained by the screen geometry before checking if it's
within the available geometry.

Task-number: QTBUG-65041
Change-Id: I115f49d359b3c2e10219a6b8aa5ad051f44256a7
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Tor Arne Vestbø 2017-12-08 02:06:31 +01:00
parent 3d720f38fa
commit b0f142e353
1 changed files with 14 additions and 10 deletions

View File

@ -500,23 +500,25 @@ void QIOSInputContext::scrollToCursor()
QWindow *focusWindow = qApp->focusWindow();
QRect cursorRect = qApp->inputMethod()->cursorRectangle().translated(focusWindow->geometry().topLeft()).toRect();
if (cursorRect.isNull()) {
scroll(0);
return;
}
// 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
// as we hide the status bar 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();
if (!cursorRect.isNull()) {
// Add some padding so that the cursor does not end up directly above the keyboard
static const int kCursorRectPadding = 20;
cursorRect.adjust(0, -kCursorRectPadding, 0, kCursorRectPadding);
// Make sure the cursor rect is still within the screen geometry after padding
cursorRect &= screenGeometry;
}
QRect keyboardGeometry = QRectF::fromCGRect(m_keyboardState.keyboardEndRect).toRect();
QRect availableGeometry = (QRegion(screenGeometry) - keyboardGeometry).boundingRect();
if (!availableGeometry.contains(cursorRect, true)) {
if (!cursorRect.isNull() && !availableGeometry.contains(cursorRect)) {
qImDebug() << "cursor rect" << cursorRect << "not fully within" << availableGeometry;
int scrollToCenter = -(availableGeometry.center() - cursorRect.center()).y();
int scrollToBottom = focusWindow->screen()->geometry().bottom() - availableGeometry.bottom();
@ -528,6 +530,8 @@ void QIOSInputContext::scrollToCursor()
void QIOSInputContext::scroll(int y)
{
Q_ASSERT(y >= 0);
UIView *rootView = scrollableRootView();
if (!rootView)
return;