diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-08-27 15:47:39 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-08-27 19:49:55 +0200 |
commit | a56b915f71eb5ceb10b6c2c585eefb88657f4abf (patch) | |
tree | 4e085146f0b815238525edf0c465bcb51823506a /src/plugins/platforms/cocoa | |
parent | f97e9360496eafe0425897b4aa07028965584cba (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.
Pick-to: 6.2
Change-Id: I148e4701318a59c7e0d9441d157199d7c8606f43
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/plugins/platforms/cocoa')
-rw-r--r-- | src/plugins/platforms/cocoa/qnsview_complextext.mm | 16 |
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 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; |