macOS: Handle NSAttributedString in insertText:replacementRange:

The text being inserted is not necessarily an NSString, but can also be
an NSAttributedString, as seen e.g. when Writing Tools inserts a
replacement.

Change-Id: I03585008d105332abd3b470f24305664766213f0
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
(cherry picked from commit c467edcf0ac8d5aa4c36b2f512e3c762cbd5d373)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2024-10-17 14:01:16 +02:00 committed by Qt Cherry-pick Bot
parent c5e38fd385
commit f6fa4c4145

View File

@ -43,6 +43,8 @@
qCDebug(lcQpaKeys).nospace() << "Inserting \"" << text << "\"" qCDebug(lcQpaKeys).nospace() << "Inserting \"" << text << "\""
<< ", replacing range " << replacementRange; << ", replacing range " << replacementRange;
NSString *string = [self stringForText:text];
if (m_composingText.isEmpty()) { if (m_composingText.isEmpty()) {
// The input method may have transformed the incoming key event // The input method may have transformed the incoming key event
// to text that doesn't match what the original key event would // to text that doesn't match what the original key event would
@ -54,7 +56,7 @@
|| currentEvent.type == NSEventTypeKeyUp || currentEvent.type == NSEventTypeKeyUp
? currentEvent.characters : nil; ? currentEvent.characters : nil;
if ([text isEqualToString:eventText]) { if ([string isEqualToString:eventText]) {
// We do not send input method events for simple text input, // We do not send input method events for simple text input,
// and instead let handleKeyEvent send the key event. // and instead let handleKeyEvent send the key event.
qCDebug(lcQpaKeys) << "Ignoring text insertion for simple text"; qCDebug(lcQpaKeys) << "Ignoring text insertion for simple text";
@ -66,8 +68,7 @@
if (queryInputMethod(self.focusObject)) { if (queryInputMethod(self.focusObject)) {
QInputMethodEvent inputMethodEvent; QInputMethodEvent inputMethodEvent;
const bool isAttributedString = [text isKindOfClass:NSAttributedString.class]; QString commitString = QString::fromNSString(string);
QString commitString = QString::fromNSString(isAttributedString ? [text string] : text);
// Ensure we have a valid replacement range // Ensure we have a valid replacement range
replacementRange = [self sanitizeReplacementRange:replacementRange]; replacementRange = [self sanitizeReplacementRange:replacementRange];
@ -166,7 +167,7 @@
<< ", replacing range " << replacementRange; << ", replacing range " << replacementRange;
const bool isAttributedString = [text isKindOfClass:NSAttributedString.class]; const bool isAttributedString = [text isKindOfClass:NSAttributedString.class];
QString preeditString = QString::fromNSString(isAttributedString ? [text string] : text); QString preeditString = QString::fromNSString([self stringForText:text]);
QList<QInputMethodEvent::Attribute> preeditAttributes; QList<QInputMethodEvent::Attribute> preeditAttributes;
@ -597,6 +598,11 @@
return {replaceFrom, replaceLength}; return {replaceFrom, replaceLength};
} }
- (NSString*)stringForText:(id)text
{
return [text isKindOfClass:NSAttributedString.class] ? [text string] : text;
}
@end @end
@implementation QNSView (ServicesMenu) @implementation QNSView (ServicesMenu)