diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-08-19 23:56:29 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2021-08-21 01:28:26 +0200 |
commit | fa6e490374afc9cc64cbe25c031bbec7cd7bef44 (patch) | |
tree | 8c93d98d7cb50ed90d748d40134eec98c97faee2 /src | |
parent | 88550e1210efb92ff4a5dfc22890e63f604a909d (diff) |
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 <volker.hilsheimer@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/platforms/cocoa/qnsview_complextext.mm | 68 |
1 files changed, 37 insertions, 31 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]; - - if (NSNumber *underlineStyle = attributes[NSUnderlineStyleAttributeName]) { - QColor underlineColor(Qt::black); - if (NSColor *color = attributes[NSUnderlineColorAttributeName]) - underlineColor = qt_mac_toQColor(color); - - QTextCharFormat format; - format.setFontUnderline(true); - format.setUnderlineColor(underlineColor); - preeditAttributes << QInputMethodEvent::Attribute( - QInputMethodEvent::TextFormat, - effectiveRange.location, effectiveRange.length, - format); - } - - index = effectiveRange.location + effectiveRange.length; - } - } else { + int index = 0; + int composingLength = preeditString.length(); + while (index < composingLength) { + NSRange range = NSMakeRange(index, composingLength - index); + + static NSDictionary *defaultMarkedTextAttributes = []{ + NSTextView *textView = [[NSTextView new] autorelease]; + return [textView.markedTextAttributes retain]; + }(); + + NSDictionary *attributes = isAttributedString + ? [text attributesAtIndex:index longestEffectiveRange:&range inRange:range] + : defaultMarkedTextAttributes; + + 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<NSString *> *)validAttributesForMarkedText { - return @[NSUnderlineColorAttributeName, NSUnderlineStyleAttributeName]; + return @[ + NSUnderlineColorAttributeName, + NSUnderlineStyleAttributeName, + NSForegroundColorAttributeName, + NSBackgroundColorAttributeName + ]; } - (BOOL)hasMarkedText |