Cache the attributed string

This commit is contained in:
Cheng Zhao 2017-11-28 17:26:09 +09:00
parent eab3342065
commit 590578c187

View file

@ -31,6 +31,7 @@ const CGFloat kVerticalTitleMargin = 2;
base::scoped_nsobject<NSImage> image_; base::scoped_nsobject<NSImage> image_;
base::scoped_nsobject<NSImage> alternateImage_; base::scoped_nsobject<NSImage> alternateImage_;
base::scoped_nsobject<NSString> title_; base::scoped_nsobject<NSString> title_;
base::scoped_nsobject<NSMutableAttributedString> attributedTitle_;
base::scoped_nsobject<NSStatusItem> statusItem_; base::scoped_nsobject<NSStatusItem> statusItem_;
} }
@ -121,10 +122,7 @@ const CGFloat kVerticalTitleMargin = 2;
// Draw title. // Draw title.
NSRect titleDrawRect = NSMakeRect( NSRect titleDrawRect = NSMakeRect(
[self iconWidth], -kVerticalTitleMargin, [self titleWidth], thickness); [self iconWidth], -kVerticalTitleMargin, [self titleWidth], thickness);
[attributedTitle_ drawInRect:titleDrawRect];
base::scoped_nsobject<NSAttributedString> titleParsed(
[self attributedTitleWithParams:title_]);
[titleParsed drawInRect:titleDrawRect];
} }
} }
@ -175,17 +173,7 @@ const CGFloat kVerticalTitleMargin = 2;
- (CGFloat)titleWidth { - (CGFloat)titleWidth {
if (!title_) if (!title_)
return 0; return 0;
return [attributedTitle_ size].width;
if (ANSI_) {
base::scoped_nsobject<NSMutableAttributedString> attributedTitle(
[title_ attributedStringParsingANSICodes]);
return [attributedTitle size].width + 10;
}
base::scoped_nsobject<NSAttributedString> attributes(
[[NSAttributedString alloc] initWithString:title_
attributes:[self titleAttributes]]);
return [attributes size].width;
} }
- (NSColor*)colorWithHighlight:(BOOL)highlight { - (NSColor*)colorWithHighlight:(BOOL)highlight {
@ -225,34 +213,35 @@ const CGFloat kVerticalTitleMargin = 2;
} else { } else {
title_.reset(); title_.reset();
} }
[self updateAttributedTitle];
[self updateDimensions]; [self updateDimensions];
} }
- (NSAttributedString*)attributedTitleWithParams:(NSString*)fullTitle { - (void)updateAttributedTitle {
NSString* title = [fullTitle
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSDictionary* attributes = NSDictionary* attributes =
[self titleAttributesWithHighlight:[self isHighlighted]]; [self titleAttributesWithHighlight:[self isHighlighted]];
base::scoped_nsobject<NSMutableAttributedString> attributedTitle(
[[NSMutableAttributedString alloc] initWithString:title
attributes:attributes]);
if (ANSI_) { if (ANSI_) {
attributedTitle.reset([title attributedStringParsingANSICodes]); NSCharacterSet* whites = [NSCharacterSet whitespaceCharacterSet];
[attributedTitle addAttributes:attributes NSString* title = [title_ stringByTrimmingCharactersInSet:whites];
range:NSMakeRange(0, [attributedTitle length])]; attributedTitle_.reset([title attributedStringParsingANSICodes]);
return attributedTitle.release(); [attributedTitle_ addAttributes:attributes
range:NSMakeRange(0, [attributedTitle_ length])];
return;
} }
attributedTitle_.reset([[NSMutableAttributedString alloc]
initWithString:title_
attributes:attributes]);
//NSFontAttributeName:[NSFont menuBarFontOfSize:0], //NSFontAttributeName:[NSFont menuBarFontOfSize:0],
//NSForegroundColorAttributeName:[self colorWithHighlight: highlight] //NSForegroundColorAttributeName:[self colorWithHighlight: highlight]
[attributedTitle addAttributes:attributes [attributedTitle_ addAttributes:attributes
range:NSMakeRange(0, [attributedTitle length])]; range:NSMakeRange(0, [attributedTitle_ length])];
[attributedTitle addAttribute:NSForegroundColorAttributeName [attributedTitle_ addAttribute:NSForegroundColorAttributeName
value:[self colorWithHighlight: [self isDarkMode]] value:[self colorWithHighlight: [self isDarkMode]]
range:NSMakeRange(0, [attributedTitle length])]; range:NSMakeRange(0, [attributedTitle_ length])];
return attributedTitle.release();
} }
- (void)setMenuController:(AtomMenuController*)menu { - (void)setMenuController:(AtomMenuController*)menu {