From fa6e490374afc9cc64cbe25c031bbec7cd7bef44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 19 Aug 2021 23:56:29 +0200 Subject: [PATCH] macOS: Respect default marked text attributes When the marked text was not an attributed string with built in styling we used to fall back to a hard-coded underline style. We now pick up the default marked text style via the markedTextAttributes property of a temporarily created NSView, which by default is a yellow background color. The implementation in NSView respects text system configuration toggles such as NSMarkedTextAttribute and NSMarkedTextColor, so by setting the user default NSMarkedTextAttribute to "Underline" the marked text will look like our old hard-coded default. This can be done in many ways, including passing `-NSMarkedTextAttribute Underline` on the command line, or by QSettings::setValue("NSMarkedTextAttribute", "Underline"); Pick-to: 6.2 Change-Id: Iede74836ed1449e77018c13733a675f8e9d84f7d Reviewed-by: Volker Hilsheimer --- .../platforms/cocoa/qnsview_complextext.mm | 62 ++++++++++--------- 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/plugins/platforms/cocoa/qnsview_complextext.mm b/src/plugins/platforms/cocoa/qnsview_complextext.mm index f329a32758..a22d19d0ed 100644 --- a/src/plugins/platforms/cocoa/qnsview_complextext.mm +++ b/src/plugins/platforms/cocoa/qnsview_complextext.mm @@ -133,37 +133,38 @@ preeditAttributes << QInputMethodEvent::Attribute( QInputMethodEvent::Cursor, selectedRange.location + selectedRange.length, true); - if (isAttributedString) { - // Create attributes for individual sections of preedit text - int index = 0; - int composingLength = preeditString.length(); - while (index < composingLength) { - NSRange effectiveRange; - NSRange range = NSMakeRange(index, composingLength - index); - NSDictionary *attributes = [text attributesAtIndex:index - longestEffectiveRange:&effectiveRange inRange:range]; + int index = 0; + int composingLength = preeditString.length(); + while (index < composingLength) { + NSRange range = NSMakeRange(index, composingLength - index); - if (NSNumber *underlineStyle = attributes[NSUnderlineStyleAttributeName]) { - QColor underlineColor(Qt::black); - if (NSColor *color = attributes[NSUnderlineColorAttributeName]) - underlineColor = qt_mac_toQColor(color); + static NSDictionary *defaultMarkedTextAttributes = []{ + NSTextView *textView = [[NSTextView new] autorelease]; + return [textView.markedTextAttributes retain]; + }(); - QTextCharFormat format; - format.setFontUnderline(true); - format.setUnderlineColor(underlineColor); - preeditAttributes << QInputMethodEvent::Attribute( - QInputMethodEvent::TextFormat, - effectiveRange.location, effectiveRange.length, - format); - } + NSDictionary *attributes = isAttributedString + ? [text attributesAtIndex:index longestEffectiveRange:&range inRange:range] + : defaultMarkedTextAttributes; - index = effectiveRange.location + effectiveRange.length; - } - } else { + qCDebug(lcQpaKeys) << "Decorating range" << range << "based on" << attributes; QTextCharFormat format; - format.setFontUnderline(true); - preeditAttributes << QInputMethodEvent::Attribute( - QInputMethodEvent::TextFormat, 0, preeditString.length(), format); + + if (NSNumber *underlineStyle = attributes[NSUnderlineStyleAttributeName]) + format.setFontUnderline(true); // FIXME: Support all NSUnderlineStyles + if (NSColor *underlineColor = attributes[NSUnderlineColorAttributeName]) + format.setUnderlineColor(qt_mac_toQColor(underlineColor)); + if (NSColor *foregroundColor = attributes[NSForegroundColorAttributeName]) + format.setForeground(qt_mac_toQColor(foregroundColor)); + if (NSColor *backgroundColor = attributes[NSBackgroundColorAttributeName]) + format.setBackground(qt_mac_toQColor(backgroundColor)); + + if (format != QTextCharFormat()) { + preeditAttributes << QInputMethodEvent::Attribute( + QInputMethodEvent::TextFormat, range.location, range.length, format); + } + + index = range.location + range.length; } m_composingText = preeditString; @@ -181,7 +182,12 @@ - (NSArray *)validAttributesForMarkedText { - return @[NSUnderlineColorAttributeName, NSUnderlineStyleAttributeName]; + return @[ + NSUnderlineColorAttributeName, + NSUnderlineStyleAttributeName, + NSForegroundColorAttributeName, + NSBackgroundColorAttributeName + ]; } - (BOOL)hasMarkedText