From a56b915f71eb5ceb10b6c2c585eefb88657f4abf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 27 Aug 2021 15:47:39 +0200 Subject: [PATCH] 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. Pick-to: 6.2 Change-Id: I148e4701318a59c7e0d9441d157199d7c8606f43 Reviewed-by: Volker Hilsheimer --- .../platforms/cocoa/qnsview_complextext.mm | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/plugins/platforms/cocoa/qnsview_complextext.mm b/src/plugins/platforms/cocoa/qnsview_complextext.mm index 3edbbae479..3a494701e1 100644 --- a/src/plugins/platforms/cocoa/qnsview_complextext.mm +++ b/src/plugins/platforms/cocoa/qnsview_complextext.mm @@ -258,12 +258,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;