Skip to content

Commit

Permalink
Android: Fix deleting of new lines when using backspace
Browse files Browse the repository at this point in the history
With some keyboards (ASOP, SwiftKey) it was not deleting any new lines
when using backspace. So this ensures that it is correctly deleting at
these points. Tested with the Samsung, Gboard and SwiftKey keyboards.

Fixes: QTBUG-74824
Fixes: QTBUG-57798
Change-Id: Id2e4f96c18c3fec0e7f444b55dd3db2653625fd0
Done-with: Vova Mshanetskiy <[email protected]>
Reviewed-by: Paul Olav Tvete <[email protected]>
  • Loading branch information
AndyShawQt committed May 3, 2019
1 parent 6c0ced4 commit 8f24dba
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/plugins/platforms/android/qandroidinputcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,15 +978,25 @@ jboolean QAndroidInputContext::deleteSurroundingText(jint leftLength, jint right
m_composingText.clear();
m_composingTextStart = -1;

QString text = query->value(Qt::ImSurroundingText).toString();
if (text.isEmpty())
return JNI_TRUE;

if (leftLength < 0) {
rightLength += -leftLength;
leftLength = 0;
}

QVariant textBeforeCursor = query->value(Qt::ImTextBeforeCursor);
QVariant textAfterCursor = query->value(Qt::ImTextAfterCursor);
if (textBeforeCursor.isValid() && textAfterCursor.isValid()) {
leftLength = qMin(leftLength, textBeforeCursor.toString().length());
rightLength = qMin(rightLength, textAfterCursor.toString().length());
} else {
int cursorPos = query->value(Qt::ImCursorPosition).toInt();
leftLength = qMin(leftLength, cursorPos);
rightLength = qMin(rightLength, query->value(Qt::ImSurroundingText).toString().length() - cursorPos);
}

if (leftLength == 0 && rightLength == 0)
return JNI_TRUE;

QInputMethodEvent event;
event.setCommitString(QString(), -leftLength, leftLength+rightLength);
sendInputMethodEvent(&event);
Expand Down Expand Up @@ -1075,6 +1085,14 @@ const QAndroidInputContext::ExtractedText &QAndroidInputContext::getExtractedTex
int cpos = localPos + composeLength; //actual cursor pos relative to the current block

int localOffset = 0; // start of extracted text relative to the current block
if (blockPos > 0) {
QString prevBlockEnding = query->value(Qt::ImTextBeforeCursor).toString();
prevBlockEnding.chop(localPos);
if (prevBlockEnding.endsWith(QLatin1Char('\n'))) {
localOffset = -qMin(20, prevBlockEnding.length());
blockText = prevBlockEnding.right(-localOffset) + blockText;
}
}

// It is documented that we should try to return hintMaxChars
// characters, but that's not what the standard Android controls do, and
Expand Down

0 comments on commit 8f24dba

Please sign in to comment.