From 77fe16faec9f3070cfb4361a513f66e41e58465b Mon Sep 17 00:00:00 2001 From: daboe01 Date: Wed, 1 Jul 2020 16:33:07 +0200 Subject: [PATCH] Fixed: background color attribute was ignored by CPTextView (#2899) --- AppKit/CPTextView/CPLayoutManager.j | 16 +++++++++----- Tests/Manual/CPTextView/AppController.j | 29 ++++++++++++++----------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/AppKit/CPTextView/CPLayoutManager.j b/AppKit/CPTextView/CPLayoutManager.j index 83ee76081..eedea1fcc 100644 --- a/AppKit/CPTextView/CPLayoutManager.j +++ b/AppKit/CPTextView/CPLayoutManager.j @@ -1085,10 +1085,10 @@ var _objectsInRange = function(aList, aRange) - (id)createDOMElementWithText:(CPString)aString andFont:(CPFont)aFont andColor:(CPColor)aColor { - return [self createDOMElementWithText:aString andFont:aFont andColor:aColor andUnderline:nil]; + return [self createDOMElementWithText:aString andFont:aFont andColor:aColor andBackgroundColor:nil andUnderline:nil ]; } -- (id)createDOMElementWithText:(CPString)aString andFont:(CPFont)aFont andColor:(CPColor)aColor andUnderline:(CPUnderlineStyle)aUnderline +- (id)createDOMElementWithText:(CPString)aString andFont:(CPFont)aFont andColor:(CPColor)fgColor andBackgroundColor:(CPColor)bgColor andUnderline:(CPUnderlineStyle)aUnderline { #if PLATFORM(DOM) var style, @@ -1129,8 +1129,11 @@ var _objectsInRange = function(aList, aRange) } } - if (aColor) - style.color = [aColor cssString]; + if (fgColor) + style.color = [fgColor cssString]; + + if (bgColor) + style.backgroundColor = [bgColor cssString]; if (CPFeatureIsCompatible(CPJavaScriptInnerTextFeature)) span.innerText = aString; @@ -1185,8 +1188,9 @@ var _objectsInRange = function(aList, aRange) else { var color = [attributes objectForKey:CPForegroundColorAttributeName], + bgcolor = [attributes objectForKey:CPBackgroundColorAttributeName], font = [attributes objectForKey:CPFontAttributeName] || [textStorage font] || [CPFont systemFontOfSize:12.0], - run = {_range:CPMakeRangeCopy(effectiveRange), color:color, font:font, elem:nil, string:string}; + run = {_range:CPMakeRangeCopy(effectiveRange), color:color, font:font, elem:nil, string:string, bgcolor:bgcolor}; _runs.push(run); } @@ -1284,7 +1288,7 @@ var _objectsInRange = function(aList, aRange) var run = runs[i]; if (!run.elem && CPRectIntersectsRect([_textContainer._textView exposedRect], _fragmentRect)) - run.elem = [self createDOMElementWithText:run.string andFont:run.font andColor:run.color andUnderline:run.underline]; + run.elem = [self createDOMElementWithText:run.string andFont:run.font andColor:run.color andBackgroundColor:run.bgcolor andUnderline:run.underline]; if (run.DOMactive && !run.DOMpatched) continue; diff --git a/Tests/Manual/CPTextView/AppController.j b/Tests/Manual/CPTextView/AppController.j index 141543a6c..7cc4eae45 100755 --- a/Tests/Manual/CPTextView/AppController.j +++ b/Tests/Manual/CPTextView/AppController.j @@ -67,19 +67,17 @@ _textView2._isRichText = NO; [_textView setBackgroundColor:[CPColor whiteColor]]; [_textView2 setBackgroundColor:[CPColor whiteColor]]; - + var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(20, 70, 520, 510)]; var scrollView2 = [[CPScrollView alloc] initWithFrame:CGRectMake(560, 70, 520, 510)]; - // [scrollView setAutohidesScrollers:YES]; + [scrollView setDocumentView:_textView]; [scrollView2 setDocumentView:_textView2]; - // + [contentView addSubview: scrollView]; [contentView addSubview: scrollView2]; - // - // [_textView setDelegate:self]; - // - // build our menu + + // build our menu var mainMenu = [CPApp mainMenu]; while ([mainMenu numberOfItems] > 0) @@ -114,17 +112,22 @@ var tempButton = [[CPButton alloc] initWithFrame:CGRectMake(0, 0, 64, 28)] [_textView insertText:[CPTextStorage attributedStringWithAttachment:tempButton]]; -// var centeredParagraph=[CPParagraphStyle new]; -// [centeredParagraph setAlignment: CPCenterTextAlignment]; -// [_textView insertText:[[CPAttributedString alloc] initWithString:@"Fusce\n" -// attributes:[CPDictionary dictionaryWithObjects:[centeredParagraph, [CPFont boldFontWithName:"Arial" size:18], [CPColor redColor]] -// forKeys:[CPParagraphStyleAttributeName, CPFontAttributeName, CPForegroundColorAttributeName]]]]; + var centeredParagraph=[CPParagraphStyle new]; + [centeredParagraph setAlignment: CPCenterTextAlignment]; + [_textView insertText:"\n"]; + [_textView insertText:[[CPAttributedString alloc] initWithString:@"Fusce\n" + attributes:[CPDictionary dictionaryWithObjects:[centeredParagraph, [CPFont boldFontWithName:"Arial" size:18], [CPColor redColor], [CPColor yellowColor]] + forKeys:[CPParagraphStyleAttributeName, CPFontAttributeName, CPForegroundColorAttributeName, CPBackgroundColorAttributeName]]]]; + [_textView insertText:"\n"]; + [_textView insertText:[[CPAttributedString alloc] initWithString:@"Yellow\n" + attributes:[CPDictionary dictionaryWithObjects:[[CPFont boldFontWithName:"Arial" size:25], [CPColor yellowColor]] + forKeys:[CPFontAttributeName, CPBackgroundColorAttributeName]]]]; [theWindow orderFront:self]; [CPMenu setMenuBarVisible:YES]; } -- (void) makeRTF:sender +- (void) makeRTF:(id)sender { [_textView2 setString: [_CPRTFProducer produceRTF:[_textView textStorage] documentAttributes: @{}] ]; var tc = [_CPRTFParser new];