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>
bb10
Tor Arne Vestbø 2021-08-19 23:56:29 +02:00
parent 88550e1210
commit fa6e490374
1 changed files with 34 additions and 28 deletions

View File

@ -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<NSString *> *)validAttributesForMarkedText
{
return @[NSUnderlineColorAttributeName, NSUnderlineStyleAttributeName];
return @[
NSUnderlineColorAttributeName,
NSUnderlineStyleAttributeName,
NSForegroundColorAttributeName,
NSBackgroundColorAttributeName
];
}
- (BOOL)hasMarkedText