summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Arne Vestbø <tor.arne.vestbo@qt.io>2021-08-27 15:47:39 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-08-27 17:54:55 +0000
commitbd0b2075a3717e4eb60b12b3dfe6965b3c2375ec (patch)
treed0bb74aac504680348850c17e821c218472c8c76
parent51679efcf97dfea86431912eeb9489088c0804ed (diff)
macOS: Handle replacement range during marking of text
The input method may include already committed text when marking text, in which case the replacement range reflects the position and length of the existing text. We handle this the same way we do replacement ranges in insertText. Change-Id: I148e4701318a59c7e0d9441d157199d7c8606f43 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit a56b915f71eb5ceb10b6c2c585eefb88657f4abf) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/plugins/platforms/cocoa/qnsview_complextext.mm16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/plugins/platforms/cocoa/qnsview_complextext.mm b/src/plugins/platforms/cocoa/qnsview_complextext.mm
index d9df96e0cd..2d9a42cd74 100644
--- a/src/plugins/platforms/cocoa/qnsview_complextext.mm
+++ b/src/plugins/platforms/cocoa/qnsview_complextext.mm
@@ -228,12 +228,28 @@
index = range.location + range.length;
}
+ // Ensure we have a valid replacement range
+ replacementRange = [self sanitizeReplacementRange:replacementRange];
+
+ // Qt's QInputMethodEvent has different semantics for the replacement
+ // range than AppKit does, so we need to sanitize the range first.
+ auto [replaceFrom, replaceLength] = [self inputMethodRangeForRange:replacementRange];
+
+ // Update the composition, now that we've computed the replacement range
m_composingText = preeditString;
if (QObject *focusObject = m_platformWindow->window()->focusObject()) {
m_composingFocusObject = focusObject;
if (queryInputMethod(focusObject)) {
QInputMethodEvent event(preeditString, preeditAttributes);
+ if (replaceLength > 0) {
+ // The input method may extend the preedit into already
+ // committed text. If so, we need to replace existing text
+ // by committing an empty string.
+ qCDebug(lcQpaKeys) << "Replacing from" << replaceFrom << "with length"
+ << replaceLength << "based on replacement range" << replacementRange;
+ event.setCommitString(QString(), replaceFrom, replaceLength);
+ }
QCoreApplication::sendEvent(focusObject, &event);
// prevent handleKeyEvent from sending a key event
m_sendKeyEvent = false;