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