From 85f254316ca68ffb51c53f92ffcd532f1a4c68bc Mon Sep 17 00:00:00 2001 From: Sebastien Bramille Date: Sat, 11 Nov 2017 19:05:06 +0000 Subject: [PATCH] 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 00000000000..fbae02b2c9e --- /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 00000000000..ec4a351da4d --- /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 4e8f42ce6fd..e7ca044634c 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 0851d832019..edc60b55971 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 f83be716af2..2559aeb6125 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',