From 1d807c552eda7211203bb4b9d7ab5bd765402d74 Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Thu, 9 Nov 2017 22:21:07 +0000 Subject: [PATCH 01/17] Add NSString+ANSI class --- atom/browser/mac/NSString+ANSI.h | 16 ++++ atom/browser/mac/NSString+ANSI.mm | 140 ++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 atom/browser/mac/NSString+ANSI.h create mode 100644 atom/browser/mac/NSString+ANSI.mm diff --git a/atom/browser/mac/NSString+ANSI.h b/atom/browser/mac/NSString+ANSI.h new file mode 100644 index 000000000000..954d9d24432e --- /dev/null +++ b/atom/browser/mac/NSString+ANSI.h @@ -0,0 +1,16 @@ +// +// NSString+ANSI.h +// BitBar +// +// Created by Kent Karlsson on 3/11/16. +// Copyright © 2016 Bit Bar. All rights reserved. +// + +#import + +@interface NSString (ANSI) + +- (BOOL)containsANSICodes; +- (NSMutableAttributedString*)attributedStringParsingANSICodes; + +@end diff --git a/atom/browser/mac/NSString+ANSI.mm b/atom/browser/mac/NSString+ANSI.mm new file mode 100644 index 000000000000..4e8f42ce6fd3 --- /dev/null +++ b/atom/browser/mac/NSString+ANSI.mm @@ -0,0 +1,140 @@ + +// +// NSString+ANSI.m +// BitBar +// +// Created by Kent Karlsson on 3/11/16. +// Copyright © 2016 Bit Bar. All rights reserved. +// + +#import "Cocoa/Cocoa.h" +#import "NSString+ANSI.h" + +@implementation NSMutableDictionary (ANSI) + +- (NSMutableDictionary*)modifyAttributesForANSICodes:(NSString*)codes { + BOOL bold = NO; + NSFont* font = self[NSFontAttributeName]; + + NSArray* codeArray = [codes componentsSeparatedByString:@";"]; + + for (NSString* codeString in codeArray) { + int code = codeString.intValue; + switch (code) { + case 0: + [self removeAllObjects]; + // remove italic and bold from font here + if (font) self[NSFontAttributeName] = font; + break; + + case 1: + case 22: + bold = (code == 1); + break; + + // case 3: italic + // case 23: italic off + // case 4: underlined + // case 24: underlined off + + case 30: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"7f7f7f" : @"000000"]; + break; + case 31: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"cd0000" : @"ff0000"]; + break; + case 32: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"00cd00" : @"00ff00"]; + break; + case 33: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"cdcd00" : @"ffff00"]; + break; + case 34: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"0000ee" : @"5c5cff"]; + break; + case 35: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"cd00cd" : @"ff00ff"]; + break; + case 36: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"00cdcd" : @"00ffff"]; + break; + case 37: + self[NSForegroundColorAttributeName] = [NSColor colorWithHexColorString:bold ? @"e5e5e5" : @"ffffff"]; + break; + + case 39: + [self removeObjectForKey:NSForegroundColorAttributeName]; + break; + + case 40: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"7f7f7f"]; + break; + case 41: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"cd0000"]; + break; + case 42: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"00cd00"]; + break; + case 43: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"cdcd00"]; + break; + case 44: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"0000ee"]; + break; + case 45: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"cd00cd"]; + break; + case 46: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"00cdcd"]; + break; + case 47: + self[NSBackgroundColorAttributeName] = [NSColor colorWithHexColorString:@"e5e5e5"]; + break; + + case 49: + [self removeObjectForKey:NSBackgroundColorAttributeName]; + break; + + default: + break; + } + } + + return self; +} + +@end + +@implementation NSString (ANSI) + +- (BOOL)containsANSICodes { + return [self rangeOfString:@"\033["].location != NSNotFound; +} + +- (NSMutableAttributedString*)attributedStringParsingANSICodes { + NSMutableAttributedString* result = [[NSMutableAttributedString alloc] init]; + + NSMutableDictionary* attributes = [NSMutableDictionary.alloc init]; + NSArray* parts = [self componentsSeparatedByString:@"\033["]; + [result appendAttributedString:[NSAttributedString.alloc initWithString:parts.firstObject attributes:nil]]; + + for (NSString* part in [parts subarrayWithRange:NSMakeRange(1, parts.count - 1)]) { + if (part.length == 0) + continue; + + NSArray* sequence = [part componentsSeparatedByString:@"m"]; + NSString* text = sequence.lastObject; + + if (sequence.count < 2) { + [result appendAttributedString:[NSAttributedString.alloc initWithString:text attributes:attributes]]; + } else if (sequence.count >= 2) { + text = [[sequence subarrayWithRange:NSMakeRange(1, sequence.count - 1)] componentsJoinedByString:@"m"]; + [attributes modifyAttributesForANSICodes:sequence[0]]; + [result appendAttributedString:[NSAttributedString.alloc initWithString:text attributes:attributes]]; + } + } + + return result; +} + +@end From d722d20974bb3528ba2f3f4c0bb8a7e65a7c6702 Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Thu, 9 Nov 2017 22:21:39 +0000 Subject: [PATCH 02/17] Use NSString+ANSI on the Menu title --- atom/browser/ui/tray_icon_cocoa.mm | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 75d4fe908a4d..0851d832019e 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -1,6 +1,7 @@ // Copyright (c) 2014 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#import "atom/browser/mac/NSString+ANSI.h" #include "atom/browser/ui/tray_icon_cocoa.h" @@ -121,8 +122,12 @@ const CGFloat kVerticalTitleMargin = 2; // Draw title. NSRect titleDrawRect = NSMakeRect( [self iconWidth], -kVerticalTitleMargin, [self titleWidth], thickness); - [title_ drawInRect:titleDrawRect - withAttributes:[self titleAttributesWithHighlight:highlightContent]]; + + NSAttributedString *titleu = [self attributedTitleWithParams:title_]; + + [titleu drawInRect:titleDrawRect]; + //[title_ drawInRect:titleDrawRect + // withAttributes:[self titleAttributesWithHighlight:highlightContent]]; } } @@ -204,8 +209,11 @@ const CGFloat kVerticalTitleMargin = 2; [self setNeedsDisplay:YES]; } +//- (void)setTitle:(NSAttributedString*)title { - (void)setTitle:(NSString*)title { if (title.length > 0) { + // title = [self attributedTitleWithParams:title]; + title_.reset([title copy]); } else { title_.reset(); @@ -213,6 +221,15 @@ const CGFloat kVerticalTitleMargin = 2; [self updateDimensions]; } + +- (NSAttributedString*) attributedTitleWithParams:(NSString *)fullTitle { + NSDictionary* attributes = @{NSBaselineOffsetAttributeName : @1}; + NSMutableAttributedString * attributedTitle = [fullTitle attributedStringParsingANSICodes]; + [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; + + return attributedTitle; +} + - (void)setMenuController:(AtomMenuController*)menu { menuController_ = menu; } From 85f254316ca68ffb51c53f92ffcd532f1a4c68bc Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Sat, 11 Nov 2017 19:05:06 +0000 Subject: [PATCH 03/17] Add logic --- atom/browser/ui/cocoa/NSColor+Hex.h | 16 ++++++ atom/browser/ui/cocoa/NSColor+Hex.mm | 52 ++++++++++++++++++ .../browser/{mac => ui/cocoa}/NSString+ANSI.h | 0 .../{mac => ui/cocoa}/NSString+ANSI.mm | 10 ++-- atom/browser/ui/tray_icon_cocoa.mm | 54 +++++++++++++++---- filenames.gypi | 4 ++ 6 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 atom/browser/ui/cocoa/NSColor+Hex.h create mode 100644 atom/browser/ui/cocoa/NSColor+Hex.mm rename atom/browser/{mac => ui/cocoa}/NSString+ANSI.h (100%) rename atom/browser/{mac => ui/cocoa}/NSString+ANSI.mm (93%) diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h new file mode 100644 index 000000000000..fbae02b2c9e9 --- /dev/null +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -0,0 +1,16 @@ +// +// NSColor+Hex.h +// BitBar +// +// Created by Mathias Leppich on 03/02/14. +// Copyright (c) 2014 Bit Bar. All rights reserved. +// + +#import + +@interface NSColor (Hex) + ++ (NSColor*) colorWithWebColorString:(NSString*)color; ++ (NSColor*) colorWithHexColorString:(NSString*)hex; + +@end diff --git a/atom/browser/ui/cocoa/NSColor+Hex.mm b/atom/browser/ui/cocoa/NSColor+Hex.mm new file mode 100644 index 000000000000..ec4a351da4d6 --- /dev/null +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -0,0 +1,52 @@ +// +// NSColor+Hex.m +// BitBar +// +// Created by Mathias Leppich on 03/02/14. +// Copyright (c) 2014 Bit Bar. All rights reserved. +// + +#import "NSColor+Hex.h" + +@implementation NSColor (Hex) + ++ (NSDictionary *)cssColors { + + static NSDictionary *cssDictionary = nil; + + + return cssDictionary = cssDictionary ?: @{@"lightseagreen":@"20b2aa", @"floralwhite":@"fffaf0", @"lightgray":@"d3d3d3", @"darkgoldenrod":@"b8860b", @"paleturquoise":@"afeeee", @"goldenrod":@"daa520", @"skyblue":@"87ceeb", @"indianred":@"cd5c5c", @"darkgray":@"a9a9a9", @"khaki":@"f0e68c", @"blue":@"0000ff", @"darkred":@"8b0000", @"lightyellow":@"ffffe0", @"midnightblue":@"191970", @"chartreuse":@"7fff00", @"lightsteelblue":@"b0c4de", @"slateblue":@"6a5acd", @"firebrick":@"b22222", @"moccasin":@"ffe4b5", @"salmon":@"fa8072", @"sienna":@"a0522d", @"slategray":@"708090", @"teal":@"008080", @"lightsalmon":@"ffa07a", @"pink":@"ffc0cb", @"burlywood":@"deb887", @"gold":@"ffd700", @"springgreen":@"00ff7f", @"lightcoral":@"f08080", @"black":@"000000", @"blueviolet":@"8a2be2", @"chocolate":@"d2691e", @"aqua":@"00ffff", @"darkviolet":@"9400d3", @"indigo":@"4b0082", @"darkcyan":@"008b8b", @"orange":@"ffa500", @"antiquewhite":@"faebd7", @"peru":@"cd853f", @"silver":@"c0c0c0", @"purple":@"800080", @"saddlebrown":@"8b4513", @"lawngreen":@"7cfc00", @"dodgerblue":@"1e90ff", @"lime":@"00ff00", @"linen":@"faf0e6", @"lightblue":@"add8e6", @"darkslategray":@"2f4f4f", @"lightskyblue":@"87cefa", @"mintcream":@"f5fffa", @"olive":@"808000", @"hotpink":@"ff69b4", @"papayawhip":@"ffefd5", @"mediumseagreen":@"3cb371", @"mediumspringgreen":@"00fa9a", @"cornflowerblue":@"6495ed", @"plum":@"dda0dd", @"seagreen":@"2e8b57", @"palevioletred":@"db7093", @"bisque":@"ffe4c4", @"beige":@"f5f5dc", @"darkorchid":@"9932cc", @"royalblue":@"4169e1", @"darkolivegreen":@"556b2f", @"darkmagenta":@"8b008b", @"orange red":@"ff4500", @"lavender":@"e6e6fa", @"fuchsia":@"ff00ff", @"darkseagreen":@"8fbc8f", @"lavenderblush":@"fff0f5", @"wheat":@"f5deb3", @"steelblue":@"4682b4", @"lightgoldenrodyellow":@"fafad2", @"lightcyan":@"e0ffff", @"mediumaquamarine":@"66cdaa", @"turquoise":@"40e0d0", @"dark blue":@"00008b", @"darkorange":@"ff8c00", @"brown":@"a52a2a", @"dimgray":@"696969", @"deeppink":@"ff1493", @"powderblue":@"b0e0e6", @"red":@"ff0000", @"darkgreen":@"006400", @"ghostwhite":@"f8f8ff", @"white":@"ffffff", @"navajowhite":@"ffdead", @"navy":@"000080", @"ivory":@"fffff0", @"palegreen":@"98fb98", @"whitesmoke":@"f5f5f5", @"gainsboro":@"dcdcdc", @"mediumslateblue":@"7b68ee", @"olivedrab":@"6b8e23", @"mediumpurple":@"9370db", @"darkslateblue":@"483d8b", @"blanchedalmond":@"ffebcd", @"darkkhaki":@"bdb76b", @"green":@"008000", @"limegreen":@"32cd32", @"snow":@"fffafa", @"tomato":@"ff6347", @"darkturquoise":@"00ced1", @"orchid":@"da70d6", @"yellow":@"ffff00", @"green yellow":@"adff2f", @"azure":@"f0ffff", @"mistyrose":@"ffe4e1", @"cadetblue":@"5f9ea0", @"oldlace":@"fdf5e6", @"gray":@"808080", @"honeydew":@"f0fff0", @"peachpuff":@"ffdab9", @"tan":@"d2b48c", @"thistle":@"d8bfd8", @"palegoldenrod":@"eee8aa", @"mediumorchid":@"ba55d3", @"rosybrown":@"bc8f8f", @"mediumturquoise":@"48d1cc", @"lemonchiffon":@"fffacd", @"maroon":@"800000", @"mediumvioletred":@"c71585", @"violet":@"ee82ee", @"yellow green":@"9acd32", @"coral":@"ff7f50", @"lightgreen":@"90ee90", @"cornsilk":@"fff8dc", @"mediumblue":@"0000cd", @"aliceblue":@"f0f8ff", @"forestgreen":@"228b22", @"aquamarine":@"7fffd4", @"deepskyblue":@"00bfff", @"lightslategray":@"778899", @"darksalmon":@"e9967a", @"crimson":@"dc143c", @"sandybrown":@"f4a460", @"lightpink":@"ffb6c1", @"seashell":@"fff5ee"}; +} + ++ (NSColor*)colorWithWebColorString:(NSString*)colorString +{ + NSString * hexString = [colorString hasPrefix:@"#"] + ? [colorString substringWithRange:NSMakeRange(1,colorString.length - 1)] + : self.cssColors[colorString.lowercaseString]; + return [self colorWithHexColorString:hexString]; +} + ++ (NSColor*)colorWithHexColorString:(NSString*)inColorString +{ + NSColor* result = nil; + unsigned colorCode = 0; + unsigned char redByte, greenByte, blueByte; + + if (nil != inColorString) + { + NSScanner* scanner = [NSScanner scannerWithString:inColorString]; + (void) [scanner scanHexInt:&colorCode]; // ignore error + } + redByte = (unsigned char)(colorCode >> 16); + greenByte = (unsigned char)(colorCode >> 8); + blueByte = (unsigned char)(colorCode); // masks off high bits + + result = [NSColor + colorWithCalibratedRed:(CGFloat)redByte / 0xff + green:(CGFloat)greenByte / 0xff + blue:(CGFloat)blueByte / 0xff + alpha:1.0]; + return result; +} + +@end diff --git a/atom/browser/mac/NSString+ANSI.h b/atom/browser/ui/cocoa/NSString+ANSI.h similarity index 100% rename from atom/browser/mac/NSString+ANSI.h rename to atom/browser/ui/cocoa/NSString+ANSI.h diff --git a/atom/browser/mac/NSString+ANSI.mm b/atom/browser/ui/cocoa/NSString+ANSI.mm similarity index 93% rename from atom/browser/mac/NSString+ANSI.mm rename to atom/browser/ui/cocoa/NSString+ANSI.mm index 4e8f42ce6fd3..e7ca044634c8 100644 --- a/atom/browser/mac/NSString+ANSI.mm +++ b/atom/browser/ui/cocoa/NSString+ANSI.mm @@ -1,4 +1,3 @@ - // // NSString+ANSI.m // BitBar @@ -9,6 +8,7 @@ #import "Cocoa/Cocoa.h" #import "NSString+ANSI.h" +#import "NSColor+Hex.h" @implementation NSMutableDictionary (ANSI) @@ -108,14 +108,16 @@ @implementation NSString (ANSI) - (BOOL)containsANSICodes { - return [self rangeOfString:@"\033["].location != NSNotFound; + NSLog(@"%lu", [self rangeOfString:@"\\033["].location); + NSLog(@"%d", [self rangeOfString:@"\\033["].location != NSNotFound); + return [self rangeOfString:@"\\033["].location != NSNotFound; } - (NSMutableAttributedString*)attributedStringParsingANSICodes { NSMutableAttributedString* result = [[NSMutableAttributedString alloc] init]; NSMutableDictionary* attributes = [NSMutableDictionary.alloc init]; - NSArray* parts = [self componentsSeparatedByString:@"\033["]; + NSArray* parts = [self componentsSeparatedByString:@"\\033["]; [result appendAttributedString:[NSAttributedString.alloc initWithString:parts.firstObject attributes:nil]]; for (NSString* part in [parts subarrayWithRange:NSMakeRange(1, parts.count - 1)]) { @@ -137,4 +139,6 @@ return result; } + + @end diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 0851d832019e..edc60b559710 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -1,7 +1,6 @@ // Copyright (c) 2014 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#import "atom/browser/mac/NSString+ANSI.h" #include "atom/browser/ui/tray_icon_cocoa.h" @@ -11,6 +10,7 @@ #include "ui/gfx/image/image.h" #include "ui/gfx/mac/coordinate_conversion.h" #include "ui/display/screen.h" +#include "atom/browser/ui/cocoa/NSString+ANSI.h" namespace { @@ -123,11 +123,22 @@ const CGFloat kVerticalTitleMargin = 2; NSRect titleDrawRect = NSMakeRect( [self iconWidth], -kVerticalTitleMargin, [self titleWidth], thickness); - NSAttributedString *titleu = [self attributedTitleWithParams:title_]; + NSAttributedString* titleParsed = [self attributedTitleWithParams:title_]; - [titleu drawInRect:titleDrawRect]; - //[title_ drawInRect:titleDrawRect - // withAttributes:[self titleAttributesWithHighlight:highlightContent]]; +NSLog(@"--------------------------"); +NSLog(@"--------------------------"); +NSLog(@"%@", titleParsed); +NSLog(@"--------------------------"); +NSLog(@"--------------------------"); + //[titleu drawInRect:titleDrawRect]; + [titleParsed drawInRect:titleDrawRect]; + + //BOOL highlight = false; +//NSString* test = @"je suis un test"; + + + //[test drawInRect:titleDrawRect + // withAttributes:[self titleAttributesWithHighlight:highlight]]; } } @@ -186,8 +197,7 @@ const CGFloat kVerticalTitleMargin = 2; - (NSDictionary*)titleAttributesWithHighlight:(BOOL)highlight { return @{ - NSFontAttributeName:[NSFont menuBarFontOfSize:0], - NSForegroundColorAttributeName:[self colorWithHighlight: highlight] + NSFontAttributeName:[NSFont menuBarFontOfSize:0] }; } @@ -212,7 +222,9 @@ const CGFloat kVerticalTitleMargin = 2; //- (void)setTitle:(NSAttributedString*)title { - (void)setTitle:(NSString*)title { if (title.length > 0) { - // title = [self attributedTitleWithParams:title]; + //NSAttributedString* titleParsed = [self attributedTitleWithParams:title]; + //NSLog(@"%@", title_); + //NSLog(@"%@", titleParsed); title_.reset([title copy]); } else { @@ -223,9 +235,31 @@ const CGFloat kVerticalTitleMargin = 2; - (NSAttributedString*) attributedTitleWithParams:(NSString *)fullTitle { - NSDictionary* attributes = @{NSBaselineOffsetAttributeName : @1}; - NSMutableAttributedString * attributedTitle = [fullTitle attributedStringParsingANSICodes]; + fullTitle = [fullTitle stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; + NSString * title = fullTitle; + + //NSDictionary* attributes = @{NSFontAttributeName: font, NSBaselineOffsetAttributeName : @1}; + BOOL highlight = [self shouldHighlight]; + BOOL highlightContent = highlight | [self isDarkMode]; + NSDictionary* attributes = [self titleAttributesWithHighlight:highlightContent]; + BOOL parseANSI = [fullTitle containsANSICodes]; + NSLog(@"%@", fullTitle); + NSLog(@"%d", parseANSI); + NSMutableAttributedString * attributedTitle = [NSMutableAttributedString.alloc initWithString:title attributes:attributes]; + if (parseANSI) { + attributedTitle = [title attributedStringParsingANSICodes]; + NSLog(@"has ANSI code"); + NSLog(@"%lu", title.length); + [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; + NSLog(@"%lu", attributedTitle.length); + return attributedTitle; + } + + //NSFontAttributeName:[NSFont menuBarFontOfSize:0], + //NSForegroundColorAttributeName:[self colorWithHighlight: highlight] + [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; + [attributedTitle addAttribute:NSForegroundColorAttributeName value:[self colorWithHighlight: highlight] range:NSMakeRange(0, attributedTitle.length)]; return attributedTitle; } diff --git a/filenames.gypi b/filenames.gypi index f83be716af21..2559aeb61255 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -312,6 +312,10 @@ 'atom/browser/ui/message_box_gtk.cc', 'atom/browser/ui/message_box_mac.mm', 'atom/browser/ui/message_box_win.cc', + 'atom/browser/ui/cocoa/NSColor+Hex.mm', + 'atom/browser/ui/cocoa/NSColor+Hex.h', + 'atom/browser/ui/cocoa/NSString+ANSI.mm', + 'atom/browser/ui/cocoa/NSString+ANSI.h', 'atom/browser/ui/tray_icon.cc', 'atom/browser/ui/tray_icon.h', 'atom/browser/ui/tray_icon_gtk.cc', From 6a1cfafbd8822fed628c40fde6d7b926bf912a50 Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Mon, 13 Nov 2017 11:01:14 +0000 Subject: [PATCH 04/17] Clean --- atom/browser/ui/cocoa/NSString+ANSI.mm | 2 -- atom/browser/ui/tray_icon_cocoa.mm | 33 ++++++-------------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/atom/browser/ui/cocoa/NSString+ANSI.mm b/atom/browser/ui/cocoa/NSString+ANSI.mm index e7ca044634c8..dbc782ed6021 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.mm +++ b/atom/browser/ui/cocoa/NSString+ANSI.mm @@ -108,8 +108,6 @@ @implementation NSString (ANSI) - (BOOL)containsANSICodes { - NSLog(@"%lu", [self rangeOfString:@"\\033["].location); - NSLog(@"%d", [self rangeOfString:@"\\033["].location != NSNotFound); return [self rangeOfString:@"\\033["].location != NSNotFound; } diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index edc60b559710..f6cd9ceffce4 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -27,6 +27,7 @@ const CGFloat kVerticalTitleMargin = 2; atom::TrayIcon::HighlightMode highlight_mode_; BOOL forceHighlight_; BOOL inMouseEventSequence_; + BOOL ANSI_; base::scoped_nsobject image_; base::scoped_nsobject alternateImage_; base::scoped_nsobject title_; @@ -125,20 +126,7 @@ const CGFloat kVerticalTitleMargin = 2; NSAttributedString* titleParsed = [self attributedTitleWithParams:title_]; -NSLog(@"--------------------------"); -NSLog(@"--------------------------"); -NSLog(@"%@", titleParsed); -NSLog(@"--------------------------"); -NSLog(@"--------------------------"); - //[titleu drawInRect:titleDrawRect]; [titleParsed drawInRect:titleDrawRect]; - - //BOOL highlight = false; -//NSString* test = @"je suis un test"; - - - //[test drawInRect:titleDrawRect - // withAttributes:[self titleAttributesWithHighlight:highlight]]; } } @@ -183,6 +171,10 @@ NSLog(@"--------------------------"); - (CGFloat)titleWidth { if (!title_) return 0; + + if (ANSI_) + return [[title_ attributedStringParsingANSICodes] size].width + 8; + base::scoped_nsobject attributes( [[NSAttributedString alloc] initWithString:title_ attributes:[self titleAttributes]]); @@ -219,14 +211,10 @@ NSLog(@"--------------------------"); [self setNeedsDisplay:YES]; } -//- (void)setTitle:(NSAttributedString*)title { - (void)setTitle:(NSString*)title { if (title.length > 0) { - //NSAttributedString* titleParsed = [self attributedTitleWithParams:title]; - //NSLog(@"%@", title_); - //NSLog(@"%@", titleParsed); - title_.reset([title copy]); + ANSI_ = [title containsANSICodes]; } else { title_.reset(); } @@ -238,20 +226,13 @@ NSLog(@"--------------------------"); fullTitle = [fullTitle stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; NSString * title = fullTitle; - //NSDictionary* attributes = @{NSFontAttributeName: font, NSBaselineOffsetAttributeName : @1}; BOOL highlight = [self shouldHighlight]; BOOL highlightContent = highlight | [self isDarkMode]; NSDictionary* attributes = [self titleAttributesWithHighlight:highlightContent]; - BOOL parseANSI = [fullTitle containsANSICodes]; - NSLog(@"%@", fullTitle); - NSLog(@"%d", parseANSI); NSMutableAttributedString * attributedTitle = [NSMutableAttributedString.alloc initWithString:title attributes:attributes]; - if (parseANSI) { + if (ANSI_) { attributedTitle = [title attributedStringParsingANSICodes]; - NSLog(@"has ANSI code"); - NSLog(@"%lu", title.length); [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; - NSLog(@"%lu", attributedTitle.length); return attributedTitle; } From 9f89bd8d8ecb5ff8effbbeaba8729c05fffb0694 Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Mon, 13 Nov 2017 11:16:25 +0000 Subject: [PATCH 05/17] Add isHighlighted method --- atom/browser/ui/tray_icon_cocoa.mm | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index f6cd9ceffce4..ecc89f794c54 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -87,12 +87,10 @@ const CGFloat kVerticalTitleMargin = 2; // | icon | title | /// ---------------- - BOOL highlight = [self shouldHighlight]; - BOOL highlightContent = highlight | [self isDarkMode]; CGFloat thickness = [[statusItem_ statusBar] thickness]; // Draw the system bar background. - [statusItem_ drawStatusBarBackgroundInRect:self.bounds withHighlight:highlight]; + [statusItem_ drawStatusBarBackgroundInRect:self.bounds withHighlight:[self shouldHighlight]]; // Determine which image to use. NSImage* image = image_.get(); @@ -104,7 +102,7 @@ const CGFloat kVerticalTitleMargin = 2; if ([image isTemplate] == YES) { NSImage* imageWithColor = [[image copy] autorelease]; [imageWithColor lockFocus]; - [[self colorWithHighlight: highlightContent] set]; + [[self colorWithHighlight: [self isHighlighted]] set]; CGRect imageBounds = CGRectMake(0,0, image.size.width, image.size.height); NSRectFillUsingOperation(imageBounds, NSCompositeSourceAtop); [imageWithColor unlockFocus]; @@ -136,6 +134,12 @@ const CGFloat kVerticalTitleMargin = 2; return mode && [mode isEqualToString:@"Dark"]; } + +- (BOOL)isHighlighted { + BOOL highlight = [self shouldHighlight]; + return highlight | [self isDarkMode]; +} + // The width of the full status item. - (CGFloat)fullWidth { if (title_) @@ -226,9 +230,7 @@ const CGFloat kVerticalTitleMargin = 2; fullTitle = [fullTitle stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; NSString * title = fullTitle; - BOOL highlight = [self shouldHighlight]; - BOOL highlightContent = highlight | [self isDarkMode]; - NSDictionary* attributes = [self titleAttributesWithHighlight:highlightContent]; + NSDictionary* attributes = [self titleAttributesWithHighlight:[self isHighlighted]]; NSMutableAttributedString * attributedTitle = [NSMutableAttributedString.alloc initWithString:title attributes:attributes]; if (ANSI_) { attributedTitle = [title attributedStringParsingANSICodes]; @@ -240,7 +242,7 @@ const CGFloat kVerticalTitleMargin = 2; //NSForegroundColorAttributeName:[self colorWithHighlight: highlight] [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; - [attributedTitle addAttribute:NSForegroundColorAttributeName value:[self colorWithHighlight: highlight] range:NSMakeRange(0, attributedTitle.length)]; + [attributedTitle addAttribute:NSForegroundColorAttributeName value:[self colorWithHighlight: [self isDarkMode]] range:NSMakeRange(0, attributedTitle.length)]; return attributedTitle; } From 7a8431086ccfc54d4b56d9b1717c3687b6d56ced Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Mon, 13 Nov 2017 11:19:50 +0000 Subject: [PATCH 06/17] Update doc --- docs/api/tray.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/tray.md b/docs/api/tray.md index 52147bcc292d..db16523ab0a3 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -208,7 +208,7 @@ Sets the hover text for this tray icon. * `title` String -Sets the title displayed aside of the tray icon in the status bar. +Sets the title displayed aside of the tray icon in the status bar (Support ANSI colors). #### `tray.setHighlightMode(mode)` _macOS_ From 71cfb7441cd231604e4817c754e6bae4e037c0db Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Mon, 13 Nov 2017 11:37:18 +0000 Subject: [PATCH 07/17] Update header file --- atom/browser/ui/cocoa/NSString+ANSI.mm | 1 + 1 file changed, 1 insertion(+) diff --git a/atom/browser/ui/cocoa/NSString+ANSI.mm b/atom/browser/ui/cocoa/NSString+ANSI.mm index dbc782ed6021..503916622c90 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.mm +++ b/atom/browser/ui/cocoa/NSString+ANSI.mm @@ -3,6 +3,7 @@ // BitBar // // Created by Kent Karlsson on 3/11/16. +// Updated by BRAMILLE Sebastien on 13/04/17 // Copyright © 2016 Bit Bar. All rights reserved. // From e8b0897ea1cfe414f23cfbb9c799ab5ace548db1 Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Mon, 13 Nov 2017 16:51:08 +0000 Subject: [PATCH 08/17] Fix margin --- atom/browser/ui/tray_icon_cocoa.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index ecc89f794c54..d8ff1c4d76c6 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -177,7 +177,7 @@ const CGFloat kVerticalTitleMargin = 2; return 0; if (ANSI_) - return [[title_ attributedStringParsingANSICodes] size].width + 8; + return [[title_ attributedStringParsingANSICodes] size].width + 10; base::scoped_nsobject attributes( [[NSAttributedString alloc] initWithString:title_ From a4905b3c958b206ad209a8fb8aadbc9813a1c547 Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Mon, 13 Nov 2017 17:22:01 +0000 Subject: [PATCH 09/17] Lint --- atom/browser/ui/cocoa/NSColor+Hex.h | 7 ++++++- atom/browser/ui/cocoa/NSColor+Hex.mm | 2 -- atom/browser/ui/cocoa/NSString+ANSI.h | 7 ++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h index fbae02b2c9e9..0df9f10267ec 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -5,12 +5,17 @@ // Created by Mathias Leppich on 03/02/14. // Copyright (c) 2014 Bit Bar. All rights reserved. // +#ifndef ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ +#define ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ #import +#import -@interface NSColor (Hex) +@interface NSColor(Hex) + (NSColor*) colorWithWebColorString:(NSString*)color; + (NSColor*) colorWithHexColorString:(NSString*)hex; @end + +#endif // ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ diff --git a/atom/browser/ui/cocoa/NSColor+Hex.mm b/atom/browser/ui/cocoa/NSColor+Hex.mm index ec4a351da4d6..caa57a8615ae 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.mm +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -11,10 +11,8 @@ @implementation NSColor (Hex) + (NSDictionary *)cssColors { - static NSDictionary *cssDictionary = nil; - return cssDictionary = cssDictionary ?: @{@"lightseagreen":@"20b2aa", @"floralwhite":@"fffaf0", @"lightgray":@"d3d3d3", @"darkgoldenrod":@"b8860b", @"paleturquoise":@"afeeee", @"goldenrod":@"daa520", @"skyblue":@"87ceeb", @"indianred":@"cd5c5c", @"darkgray":@"a9a9a9", @"khaki":@"f0e68c", @"blue":@"0000ff", @"darkred":@"8b0000", @"lightyellow":@"ffffe0", @"midnightblue":@"191970", @"chartreuse":@"7fff00", @"lightsteelblue":@"b0c4de", @"slateblue":@"6a5acd", @"firebrick":@"b22222", @"moccasin":@"ffe4b5", @"salmon":@"fa8072", @"sienna":@"a0522d", @"slategray":@"708090", @"teal":@"008080", @"lightsalmon":@"ffa07a", @"pink":@"ffc0cb", @"burlywood":@"deb887", @"gold":@"ffd700", @"springgreen":@"00ff7f", @"lightcoral":@"f08080", @"black":@"000000", @"blueviolet":@"8a2be2", @"chocolate":@"d2691e", @"aqua":@"00ffff", @"darkviolet":@"9400d3", @"indigo":@"4b0082", @"darkcyan":@"008b8b", @"orange":@"ffa500", @"antiquewhite":@"faebd7", @"peru":@"cd853f", @"silver":@"c0c0c0", @"purple":@"800080", @"saddlebrown":@"8b4513", @"lawngreen":@"7cfc00", @"dodgerblue":@"1e90ff", @"lime":@"00ff00", @"linen":@"faf0e6", @"lightblue":@"add8e6", @"darkslategray":@"2f4f4f", @"lightskyblue":@"87cefa", @"mintcream":@"f5fffa", @"olive":@"808000", @"hotpink":@"ff69b4", @"papayawhip":@"ffefd5", @"mediumseagreen":@"3cb371", @"mediumspringgreen":@"00fa9a", @"cornflowerblue":@"6495ed", @"plum":@"dda0dd", @"seagreen":@"2e8b57", @"palevioletred":@"db7093", @"bisque":@"ffe4c4", @"beige":@"f5f5dc", @"darkorchid":@"9932cc", @"royalblue":@"4169e1", @"darkolivegreen":@"556b2f", @"darkmagenta":@"8b008b", @"orange red":@"ff4500", @"lavender":@"e6e6fa", @"fuchsia":@"ff00ff", @"darkseagreen":@"8fbc8f", @"lavenderblush":@"fff0f5", @"wheat":@"f5deb3", @"steelblue":@"4682b4", @"lightgoldenrodyellow":@"fafad2", @"lightcyan":@"e0ffff", @"mediumaquamarine":@"66cdaa", @"turquoise":@"40e0d0", @"dark blue":@"00008b", @"darkorange":@"ff8c00", @"brown":@"a52a2a", @"dimgray":@"696969", @"deeppink":@"ff1493", @"powderblue":@"b0e0e6", @"red":@"ff0000", @"darkgreen":@"006400", @"ghostwhite":@"f8f8ff", @"white":@"ffffff", @"navajowhite":@"ffdead", @"navy":@"000080", @"ivory":@"fffff0", @"palegreen":@"98fb98", @"whitesmoke":@"f5f5f5", @"gainsboro":@"dcdcdc", @"mediumslateblue":@"7b68ee", @"olivedrab":@"6b8e23", @"mediumpurple":@"9370db", @"darkslateblue":@"483d8b", @"blanchedalmond":@"ffebcd", @"darkkhaki":@"bdb76b", @"green":@"008000", @"limegreen":@"32cd32", @"snow":@"fffafa", @"tomato":@"ff6347", @"darkturquoise":@"00ced1", @"orchid":@"da70d6", @"yellow":@"ffff00", @"green yellow":@"adff2f", @"azure":@"f0ffff", @"mistyrose":@"ffe4e1", @"cadetblue":@"5f9ea0", @"oldlace":@"fdf5e6", @"gray":@"808080", @"honeydew":@"f0fff0", @"peachpuff":@"ffdab9", @"tan":@"d2b48c", @"thistle":@"d8bfd8", @"palegoldenrod":@"eee8aa", @"mediumorchid":@"ba55d3", @"rosybrown":@"bc8f8f", @"mediumturquoise":@"48d1cc", @"lemonchiffon":@"fffacd", @"maroon":@"800000", @"mediumvioletred":@"c71585", @"violet":@"ee82ee", @"yellow green":@"9acd32", @"coral":@"ff7f50", @"lightgreen":@"90ee90", @"cornsilk":@"fff8dc", @"mediumblue":@"0000cd", @"aliceblue":@"f0f8ff", @"forestgreen":@"228b22", @"aquamarine":@"7fffd4", @"deepskyblue":@"00bfff", @"lightslategray":@"778899", @"darksalmon":@"e9967a", @"crimson":@"dc143c", @"sandybrown":@"f4a460", @"lightpink":@"ffb6c1", @"seashell":@"fff5ee"}; } diff --git a/atom/browser/ui/cocoa/NSString+ANSI.h b/atom/browser/ui/cocoa/NSString+ANSI.h index 954d9d24432e..b3fe33d50486 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.h +++ b/atom/browser/ui/cocoa/NSString+ANSI.h @@ -6,11 +6,16 @@ // Copyright © 2016 Bit Bar. All rights reserved. // +#ifndef ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_ +#define ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_ + #import -@interface NSString (ANSI) +@interface NSString(ANSI) - (BOOL)containsANSICodes; - (NSMutableAttributedString*)attributedStringParsingANSICodes; @end + +#endif // ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_ From edede7f33fbf0ea1e6cb48cdefb38f7f4cd5663e Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 15:36:45 +0900 Subject: [PATCH 10/17] Fix cpplint warning --- atom/browser/ui/cocoa/NSColor+Hex.h | 4 ++-- script/cpplint.py | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h index 0df9f10267ec..d7a6e156a330 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -13,8 +13,8 @@ @interface NSColor(Hex) -+ (NSColor*) colorWithWebColorString:(NSString*)color; -+ (NSColor*) colorWithHexColorString:(NSString*)hex; ++ (NSColor*)colorWithWebColorString:(NSString*)color; ++ (NSColor*)colorWithHexColorString:(NSString*)hex; @end diff --git a/script/cpplint.py b/script/cpplint.py index 5dfca8f76ece..0b0310243839 100755 --- a/script/cpplint.py +++ b/script/cpplint.py @@ -14,6 +14,8 @@ IGNORE_FILES = [ os.path.join('atom', 'browser', 'ui', 'cocoa', 'atom_touch_bar.h'), os.path.join('atom', 'browser', 'ui', 'cocoa', 'touch_bar_forward_declarations.h'), + os.path.join('atom', 'browser', 'ui', 'cocoa', 'NSColor+Hex.h'), + os.path.join('atom', 'browser', 'ui', 'cocoa', 'NSString+ANSI.h'), os.path.join('atom', 'common', 'api', 'api_messages.h'), os.path.join('atom', 'common', 'common_message_generator.cc'), os.path.join('atom', 'common', 'common_message_generator.h'), From 08f30e4ca632dd24a1ea4967fbac342146bd9f22 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 15:38:42 +0900 Subject: [PATCH 11/17] Add license for the vendored files Checked the original files and they were compatible with MIT. --- atom/browser/ui/cocoa/NSColor+Hex.h | 14 +++++--------- atom/browser/ui/cocoa/NSColor+Hex.mm | 12 +++++------- atom/browser/ui/cocoa/NSString+ANSI.h | 12 +++++------- atom/browser/ui/cocoa/NSString+ANSI.mm | 13 +++++-------- 4 files changed, 20 insertions(+), 31 deletions(-) diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h index d7a6e156a330..7e046f2fc8b9 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -1,12 +1,8 @@ -// -// NSColor+Hex.h -// BitBar -// -// Created by Mathias Leppich on 03/02/14. -// Copyright (c) 2014 Bit Bar. All rights reserved. -// -#ifndef ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ -#define ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ +// Created by Mathias Leppich on 03/02/14. +// Copyright (c) 2014 Bit Bar. All rights reserved. +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. #import #import diff --git a/atom/browser/ui/cocoa/NSColor+Hex.mm b/atom/browser/ui/cocoa/NSColor+Hex.mm index caa57a8615ae..d8b00032061e 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.mm +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -1,10 +1,8 @@ -// -// NSColor+Hex.m -// BitBar -// -// Created by Mathias Leppich on 03/02/14. -// Copyright (c) 2014 Bit Bar. All rights reserved. -// +// Created by Mathias Leppich on 03/02/14. +// Copyright (c) 2014 Bit Bar. All rights reserved. +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. #import "NSColor+Hex.h" diff --git a/atom/browser/ui/cocoa/NSString+ANSI.h b/atom/browser/ui/cocoa/NSString+ANSI.h index b3fe33d50486..0a0388631028 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.h +++ b/atom/browser/ui/cocoa/NSString+ANSI.h @@ -1,10 +1,8 @@ -// -// NSString+ANSI.h -// BitBar -// -// Created by Kent Karlsson on 3/11/16. -// Copyright © 2016 Bit Bar. All rights reserved. -// +// Created by Kent Karlsson on 3/11/16. +// Copyright (c) 2016 Bit Bar. All rights reserved. +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. #ifndef ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_ #define ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_ diff --git a/atom/browser/ui/cocoa/NSString+ANSI.mm b/atom/browser/ui/cocoa/NSString+ANSI.mm index 503916622c90..47d6e9e17d5b 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.mm +++ b/atom/browser/ui/cocoa/NSString+ANSI.mm @@ -1,11 +1,8 @@ -// -// NSString+ANSI.m -// BitBar -// -// Created by Kent Karlsson on 3/11/16. -// Updated by BRAMILLE Sebastien on 13/04/17 -// Copyright © 2016 Bit Bar. All rights reserved. -// +// Created by Kent Karlsson on 3/11/16. +// Copyright (c) 2016 Bit Bar. All rights reserved. +// Copyright (c) 2017 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. #import "Cocoa/Cocoa.h" #import "NSString+ANSI.h" From 4ded79801f6b3a7f09eb4eff3c96f6042414676d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 16:10:24 +0900 Subject: [PATCH 12/17] Remove unused code in NSColor+Hex.h --- atom/browser/ui/cocoa/NSColor+Hex.h | 7 +++--- atom/browser/ui/cocoa/NSColor+Hex.mm | 32 ++++++---------------------- 2 files changed, 10 insertions(+), 29 deletions(-) diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h index 7e046f2fc8b9..e2fc344a7ec5 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -4,14 +4,13 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. +#ifndef ATOM_BROWSER_UI_COCOA_NSCOLOR+HEX_H_ +#define ATOM_BROWSER_UI_COCOA_NSCOLOR+HEX_H_ + #import -#import @interface NSColor(Hex) - -+ (NSColor*)colorWithWebColorString:(NSString*)color; + (NSColor*)colorWithHexColorString:(NSString*)hex; - @end #endif // ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ diff --git a/atom/browser/ui/cocoa/NSColor+Hex.mm b/atom/browser/ui/cocoa/NSColor+Hex.mm index d8b00032061e..a4cf9bc5ee2a 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.mm +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -4,32 +4,16 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#import "NSColor+Hex.h" +#include "atom/browser/ui/cocoa/NSColor+Hex.h" @implementation NSColor (Hex) -+ (NSDictionary *)cssColors { - static NSDictionary *cssDictionary = nil; - - return cssDictionary = cssDictionary ?: @{@"lightseagreen":@"20b2aa", @"floralwhite":@"fffaf0", @"lightgray":@"d3d3d3", @"darkgoldenrod":@"b8860b", @"paleturquoise":@"afeeee", @"goldenrod":@"daa520", @"skyblue":@"87ceeb", @"indianred":@"cd5c5c", @"darkgray":@"a9a9a9", @"khaki":@"f0e68c", @"blue":@"0000ff", @"darkred":@"8b0000", @"lightyellow":@"ffffe0", @"midnightblue":@"191970", @"chartreuse":@"7fff00", @"lightsteelblue":@"b0c4de", @"slateblue":@"6a5acd", @"firebrick":@"b22222", @"moccasin":@"ffe4b5", @"salmon":@"fa8072", @"sienna":@"a0522d", @"slategray":@"708090", @"teal":@"008080", @"lightsalmon":@"ffa07a", @"pink":@"ffc0cb", @"burlywood":@"deb887", @"gold":@"ffd700", @"springgreen":@"00ff7f", @"lightcoral":@"f08080", @"black":@"000000", @"blueviolet":@"8a2be2", @"chocolate":@"d2691e", @"aqua":@"00ffff", @"darkviolet":@"9400d3", @"indigo":@"4b0082", @"darkcyan":@"008b8b", @"orange":@"ffa500", @"antiquewhite":@"faebd7", @"peru":@"cd853f", @"silver":@"c0c0c0", @"purple":@"800080", @"saddlebrown":@"8b4513", @"lawngreen":@"7cfc00", @"dodgerblue":@"1e90ff", @"lime":@"00ff00", @"linen":@"faf0e6", @"lightblue":@"add8e6", @"darkslategray":@"2f4f4f", @"lightskyblue":@"87cefa", @"mintcream":@"f5fffa", @"olive":@"808000", @"hotpink":@"ff69b4", @"papayawhip":@"ffefd5", @"mediumseagreen":@"3cb371", @"mediumspringgreen":@"00fa9a", @"cornflowerblue":@"6495ed", @"plum":@"dda0dd", @"seagreen":@"2e8b57", @"palevioletred":@"db7093", @"bisque":@"ffe4c4", @"beige":@"f5f5dc", @"darkorchid":@"9932cc", @"royalblue":@"4169e1", @"darkolivegreen":@"556b2f", @"darkmagenta":@"8b008b", @"orange red":@"ff4500", @"lavender":@"e6e6fa", @"fuchsia":@"ff00ff", @"darkseagreen":@"8fbc8f", @"lavenderblush":@"fff0f5", @"wheat":@"f5deb3", @"steelblue":@"4682b4", @"lightgoldenrodyellow":@"fafad2", @"lightcyan":@"e0ffff", @"mediumaquamarine":@"66cdaa", @"turquoise":@"40e0d0", @"dark blue":@"00008b", @"darkorange":@"ff8c00", @"brown":@"a52a2a", @"dimgray":@"696969", @"deeppink":@"ff1493", @"powderblue":@"b0e0e6", @"red":@"ff0000", @"darkgreen":@"006400", @"ghostwhite":@"f8f8ff", @"white":@"ffffff", @"navajowhite":@"ffdead", @"navy":@"000080", @"ivory":@"fffff0", @"palegreen":@"98fb98", @"whitesmoke":@"f5f5f5", @"gainsboro":@"dcdcdc", @"mediumslateblue":@"7b68ee", @"olivedrab":@"6b8e23", @"mediumpurple":@"9370db", @"darkslateblue":@"483d8b", @"blanchedalmond":@"ffebcd", @"darkkhaki":@"bdb76b", @"green":@"008000", @"limegreen":@"32cd32", @"snow":@"fffafa", @"tomato":@"ff6347", @"darkturquoise":@"00ced1", @"orchid":@"da70d6", @"yellow":@"ffff00", @"green yellow":@"adff2f", @"azure":@"f0ffff", @"mistyrose":@"ffe4e1", @"cadetblue":@"5f9ea0", @"oldlace":@"fdf5e6", @"gray":@"808080", @"honeydew":@"f0fff0", @"peachpuff":@"ffdab9", @"tan":@"d2b48c", @"thistle":@"d8bfd8", @"palegoldenrod":@"eee8aa", @"mediumorchid":@"ba55d3", @"rosybrown":@"bc8f8f", @"mediumturquoise":@"48d1cc", @"lemonchiffon":@"fffacd", @"maroon":@"800000", @"mediumvioletred":@"c71585", @"violet":@"ee82ee", @"yellow green":@"9acd32", @"coral":@"ff7f50", @"lightgreen":@"90ee90", @"cornsilk":@"fff8dc", @"mediumblue":@"0000cd", @"aliceblue":@"f0f8ff", @"forestgreen":@"228b22", @"aquamarine":@"7fffd4", @"deepskyblue":@"00bfff", @"lightslategray":@"778899", @"darksalmon":@"e9967a", @"crimson":@"dc143c", @"sandybrown":@"f4a460", @"lightpink":@"ffb6c1", @"seashell":@"fff5ee"}; -} - -+ (NSColor*)colorWithWebColorString:(NSString*)colorString -{ - NSString * hexString = [colorString hasPrefix:@"#"] - ? [colorString substringWithRange:NSMakeRange(1,colorString.length - 1)] - : self.cssColors[colorString.lowercaseString]; - return [self colorWithHexColorString:hexString]; -} - -+ (NSColor*)colorWithHexColorString:(NSString*)inColorString -{ ++ (NSColor*)colorWithHexColorString:(NSString*)inColorString { NSColor* result = nil; unsigned colorCode = 0; unsigned char redByte, greenByte, blueByte; - if (nil != inColorString) - { + if (inColorString) { NSScanner* scanner = [NSScanner scannerWithString:inColorString]; (void) [scanner scanHexInt:&colorCode]; // ignore error } @@ -37,12 +21,10 @@ greenByte = (unsigned char)(colorCode >> 8); blueByte = (unsigned char)(colorCode); // masks off high bits - result = [NSColor - colorWithCalibratedRed:(CGFloat)redByte / 0xff - green:(CGFloat)greenByte / 0xff - blue:(CGFloat)blueByte / 0xff - alpha:1.0]; - return result; + return [NSColor colorWithCalibratedRed:(CGFloat)redByte / 0xff + green:(CGFloat)greenByte / 0xff + blue:(CGFloat)blueByte / 0xff + alpha:1.0]; } @end From c9acccaddc8bc604bf54bebc4090e5d049caa8d4 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 16:18:07 +0900 Subject: [PATCH 13/17] Fix memory leak in ANSCI parsing code We do not have ARC enabled. --- atom/browser/ui/cocoa/NSColor+Hex.h | 4 +- atom/browser/ui/cocoa/NSColor+Hex.mm | 1 - atom/browser/ui/cocoa/NSString+ANSI.h | 2 - atom/browser/ui/cocoa/NSString+ANSI.mm | 26 ++++++++----- atom/browser/ui/tray_icon_cocoa.mm | 54 +++++++++++++++----------- 5 files changed, 49 insertions(+), 38 deletions(-) diff --git a/atom/browser/ui/cocoa/NSColor+Hex.h b/atom/browser/ui/cocoa/NSColor+Hex.h index e2fc344a7ec5..70847b42173c 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.h +++ b/atom/browser/ui/cocoa/NSColor+Hex.h @@ -4,8 +4,8 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#ifndef ATOM_BROWSER_UI_COCOA_NSCOLOR+HEX_H_ -#define ATOM_BROWSER_UI_COCOA_NSCOLOR+HEX_H_ +#ifndef ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ +#define ATOM_BROWSER_UI_COCOA_NSCOLOR_HEX_H_ #import diff --git a/atom/browser/ui/cocoa/NSColor+Hex.mm b/atom/browser/ui/cocoa/NSColor+Hex.mm index a4cf9bc5ee2a..b55447ca7e26 100644 --- a/atom/browser/ui/cocoa/NSColor+Hex.mm +++ b/atom/browser/ui/cocoa/NSColor+Hex.mm @@ -9,7 +9,6 @@ @implementation NSColor (Hex) + (NSColor*)colorWithHexColorString:(NSString*)inColorString { - NSColor* result = nil; unsigned colorCode = 0; unsigned char redByte, greenByte, blueByte; diff --git a/atom/browser/ui/cocoa/NSString+ANSI.h b/atom/browser/ui/cocoa/NSString+ANSI.h index 0a0388631028..93e76b59e9bb 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.h +++ b/atom/browser/ui/cocoa/NSString+ANSI.h @@ -10,10 +10,8 @@ #import @interface NSString(ANSI) - - (BOOL)containsANSICodes; - (NSMutableAttributedString*)attributedStringParsingANSICodes; - @end #endif // ATOM_BROWSER_UI_COCOA_NSSTRING_ANSI_H_ diff --git a/atom/browser/ui/cocoa/NSString+ANSI.mm b/atom/browser/ui/cocoa/NSString+ANSI.mm index 47d6e9e17d5b..3a8250dde94e 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.mm +++ b/atom/browser/ui/cocoa/NSString+ANSI.mm @@ -4,9 +4,9 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#import "Cocoa/Cocoa.h" -#import "NSString+ANSI.h" -#import "NSColor+Hex.h" +#include "atom/browser/ui/cocoa/NSString+ANSI.h" +#include "atom/browser/ui/cocoa/NSColor+Hex.h" +#include "base/mac/scoped_nsobject.h" @implementation NSMutableDictionary (ANSI) @@ -112,9 +112,12 @@ - (NSMutableAttributedString*)attributedStringParsingANSICodes { NSMutableAttributedString* result = [[NSMutableAttributedString alloc] init]; - NSMutableDictionary* attributes = [NSMutableDictionary.alloc init]; + base::scoped_nsobject attributes( + [[NSMutableDictionary alloc] init]); NSArray* parts = [self componentsSeparatedByString:@"\\033["]; - [result appendAttributedString:[NSAttributedString.alloc initWithString:parts.firstObject attributes:nil]]; + [result appendAttributedString:[[[NSAttributedString alloc] + initWithString:parts.firstObject + attributes:nil] autorelease]]; for (NSString* part in [parts subarrayWithRange:NSMakeRange(1, parts.count - 1)]) { if (part.length == 0) @@ -124,17 +127,20 @@ NSString* text = sequence.lastObject; if (sequence.count < 2) { - [result appendAttributedString:[NSAttributedString.alloc initWithString:text attributes:attributes]]; + [result appendAttributedString:[[[NSAttributedString alloc] + initWithString:text + attributes:attributes] autorelease]]; } else if (sequence.count >= 2) { - text = [[sequence subarrayWithRange:NSMakeRange(1, sequence.count - 1)] componentsJoinedByString:@"m"]; + text = [[sequence subarrayWithRange:NSMakeRange(1, sequence.count - 1)] + componentsJoinedByString:@"m"]; [attributes modifyAttributesForANSICodes:sequence[0]]; - [result appendAttributedString:[NSAttributedString.alloc initWithString:text attributes:attributes]]; + [result appendAttributedString:[[[NSAttributedString alloc] + initWithString:text + attributes:attributes] autorelease]]; } } return result; } - - @end diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index d8ff1c4d76c6..0554a3683814 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -5,12 +5,12 @@ #include "atom/browser/ui/tray_icon_cocoa.h" #include "atom/browser/ui/cocoa/atom_menu_controller.h" +#include "atom/browser/ui/cocoa/NSString+ANSI.h" #include "base/strings/sys_string_conversions.h" #include "ui/events/cocoa/cocoa_event_utils.h" #include "ui/gfx/image/image.h" #include "ui/gfx/mac/coordinate_conversion.h" #include "ui/display/screen.h" -#include "atom/browser/ui/cocoa/NSString+ANSI.h" namespace { @@ -122,8 +122,8 @@ const CGFloat kVerticalTitleMargin = 2; NSRect titleDrawRect = NSMakeRect( [self iconWidth], -kVerticalTitleMargin, [self titleWidth], thickness); - NSAttributedString* titleParsed = [self attributedTitleWithParams:title_]; - + base::scoped_nsobject titleParsed( + [self attributedTitleWithParams:title_]); [titleParsed drawInRect:titleDrawRect]; } } @@ -174,10 +174,13 @@ const CGFloat kVerticalTitleMargin = 2; // The width of the title. - (CGFloat)titleWidth { if (!title_) - return 0; + return 0; - if (ANSI_) - return [[title_ attributedStringParsingANSICodes] size].width + 10; + if (ANSI_) { + base::scoped_nsobject attributedTitle( + [title_ attributedStringParsingANSICodes]); + return [attributedTitle size].width + 10; + } base::scoped_nsobject attributes( [[NSAttributedString alloc] initWithString:title_ @@ -226,25 +229,30 @@ const CGFloat kVerticalTitleMargin = 2; } -- (NSAttributedString*) attributedTitleWithParams:(NSString *)fullTitle { - fullTitle = [fullTitle stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceCharacterSet]; - NSString * title = fullTitle; +- (NSAttributedString*)attributedTitleWithParams:(NSString*)fullTitle { + NSString* title = [fullTitle + stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + NSDictionary* attributes = + [self titleAttributesWithHighlight:[self isHighlighted]]; - NSDictionary* attributes = [self titleAttributesWithHighlight:[self isHighlighted]]; - NSMutableAttributedString * attributedTitle = [NSMutableAttributedString.alloc initWithString:title attributes:attributes]; - if (ANSI_) { - attributedTitle = [title attributedStringParsingANSICodes]; - [attributedTitle addAttributes:attributes range:NSMakeRange(0, attributedTitle.length)]; - return attributedTitle; - } + 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(); + } - //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; + //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(); } - (void)setMenuController:(AtomMenuController*)menu { From eab3342065576bd051e30ce0fa2fa51808e4ebd6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 16:28:58 +0900 Subject: [PATCH 14/17] Fix the detection of ANSI code --- atom/browser/ui/cocoa/NSString+ANSI.mm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/atom/browser/ui/cocoa/NSString+ANSI.mm b/atom/browser/ui/cocoa/NSString+ANSI.mm index 3a8250dde94e..be1bc5054368 100644 --- a/atom/browser/ui/cocoa/NSString+ANSI.mm +++ b/atom/browser/ui/cocoa/NSString+ANSI.mm @@ -106,7 +106,7 @@ @implementation NSString (ANSI) - (BOOL)containsANSICodes { - return [self rangeOfString:@"\\033["].location != NSNotFound; + return [self rangeOfString:@"\033["].location != NSNotFound; } - (NSMutableAttributedString*)attributedStringParsingANSICodes { @@ -114,10 +114,10 @@ base::scoped_nsobject attributes( [[NSMutableDictionary alloc] init]); - NSArray* parts = [self componentsSeparatedByString:@"\\033["]; + NSArray* parts = [self componentsSeparatedByString:@"\033["]; [result appendAttributedString:[[[NSAttributedString alloc] - initWithString:parts.firstObject - attributes:nil] autorelease]]; + initWithString:parts.firstObject + attributes:nil] autorelease]]; for (NSString* part in [parts subarrayWithRange:NSMakeRange(1, parts.count - 1)]) { if (part.length == 0) From 590578c18719bec80a21d7e45a048bbe5c4c99d2 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 17:26:09 +0900 Subject: [PATCH 15/17] Cache the attributed string --- atom/browser/ui/tray_icon_cocoa.mm | 51 ++++++++++++------------------ 1 file changed, 20 insertions(+), 31 deletions(-) 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 { From d1ebce039576aa7ec0d1695750c1ffc72b114d2f Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 17:31:13 +0900 Subject: [PATCH 16/17] Remove unused methods --- atom/browser/ui/tray_icon_cocoa.mm | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 47096d786421..6b05e002ad47 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -182,16 +182,6 @@ const CGFloat kVerticalTitleMargin = 2; [NSColor colorWithRed:0.265625 green:0.25390625 blue:0.234375 alpha:1.0]; } -- (NSDictionary*)titleAttributesWithHighlight:(BOOL)highlight { - return @{ - NSFontAttributeName:[NSFont menuBarFontOfSize:0] - }; -} - -- (NSDictionary*)titleAttributes { - return [self titleAttributesWithHighlight:[self isDarkMode]]; -} - - (void)setImage:(NSImage*)image { image_.reset([image copy]); [self updateDimensions]; @@ -212,6 +202,7 @@ const CGFloat kVerticalTitleMargin = 2; ANSI_ = [title containsANSICodes]; } else { title_.reset(); + ANSI_ = NO; } [self updateAttributedTitle]; [self updateDimensions]; @@ -219,8 +210,9 @@ const CGFloat kVerticalTitleMargin = 2; - (void)updateAttributedTitle { - NSDictionary* attributes = - [self titleAttributesWithHighlight:[self isHighlighted]]; + NSDictionary* attributes = @{ + NSFontAttributeName:[NSFont menuBarFontOfSize:0] + }; if (ANSI_) { NSCharacterSet* whites = [NSCharacterSet whitespaceCharacterSet]; @@ -240,7 +232,7 @@ const CGFloat kVerticalTitleMargin = 2; [attributedTitle_ addAttributes:attributes range:NSMakeRange(0, [attributedTitle_ length])]; [attributedTitle_ addAttribute:NSForegroundColorAttributeName - value:[self colorWithHighlight: [self isDarkMode]] + value:[self colorWithHighlight: [self isHighlighted]] range:NSMakeRange(0, [attributedTitle_ length])]; } From 0dc094fffcff9825d8bb573ffc528e3af9cd0593 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 28 Nov 2017 17:37:58 +0900 Subject: [PATCH 17/17] Do not change logic on highlighing --- atom/browser/ui/tray_icon_cocoa.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 6b05e002ad47..9c5e100a2a0c 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -91,7 +91,8 @@ const CGFloat kVerticalTitleMargin = 2; CGFloat thickness = [[statusItem_ statusBar] thickness]; // Draw the system bar background. - [statusItem_ drawStatusBarBackgroundInRect:self.bounds withHighlight:[self shouldHighlight]]; + [statusItem_ drawStatusBarBackgroundInRect:self.bounds + withHighlight:[self isHighlighted]]; // Determine which image to use. NSImage* image = image_.get();