From f6fa4c41459b20ba60c2e51d392f92919419c026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 17 Oct 2024 14:01:16 +0200 Subject: [PATCH] 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 (cherry picked from commit c467edcf0ac8d5aa4c36b2f512e3c762cbd5d373) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/cocoa/qnsview_complextext.mm | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/plugins/platforms/cocoa/qnsview_complextext.mm b/src/plugins/platforms/cocoa/qnsview_complextext.mm index cb512d5893d..f84b0b2dacb 100644 --- a/src/plugins/platforms/cocoa/qnsview_complextext.mm +++ b/src/plugins/platforms/cocoa/qnsview_complextext.mm @@ -43,6 +43,8 @@ qCDebug(lcQpaKeys).nospace() << "Inserting \"" << text << "\"" << ", replacing range " << replacementRange; + NSString *string = [self stringForText:text]; + if (m_composingText.isEmpty()) { // The input method may have transformed the incoming key event // to text that doesn't match what the original key event would @@ -54,7 +56,7 @@ || currentEvent.type == NSEventTypeKeyUp ? currentEvent.characters : nil; - if ([text isEqualToString:eventText]) { + if ([string isEqualToString:eventText]) { // We do not send input method events for simple text input, // and instead let handleKeyEvent send the key event. qCDebug(lcQpaKeys) << "Ignoring text insertion for simple text"; @@ -66,8 +68,7 @@ if (queryInputMethod(self.focusObject)) { QInputMethodEvent inputMethodEvent; - const bool isAttributedString = [text isKindOfClass:NSAttributedString.class]; - QString commitString = QString::fromNSString(isAttributedString ? [text string] : text); + QString commitString = QString::fromNSString(string); // Ensure we have a valid replacement range replacementRange = [self sanitizeReplacementRange:replacementRange]; @@ -166,7 +167,7 @@ << ", replacing range " << replacementRange; const bool isAttributedString = [text isKindOfClass:NSAttributedString.class]; - QString preeditString = QString::fromNSString(isAttributedString ? [text string] : text); + QString preeditString = QString::fromNSString([self stringForText:text]); QList preeditAttributes; @@ -597,6 +598,11 @@ return {replaceFrom, replaceLength}; } +- (NSString*)stringForText:(id)text +{ + return [text isKindOfClass:NSAttributedString.class] ? [text string] : text; +} + @end @implementation QNSView (ServicesMenu)