mac: Make the tray icon behave more like the official one

This commit is contained in:
Cheng Zhao 2015-07-30 11:57:34 +08:00
parent 0e779e20c3
commit be24d3e78c

View file

@ -14,6 +14,8 @@ namespace {
// By default, OS X sets 4px to tray image as left and right padding margin. // By default, OS X sets 4px to tray image as left and right padding margin.
const CGFloat kHorizontalMargin = 4; const CGFloat kHorizontalMargin = 4;
// OS X tends to make the title 2px lower.
const CGFloat kVerticalTitleMargin = 2;
} // namespace } // namespace
@ -33,24 +35,33 @@ const CGFloat kHorizontalMargin = 4;
@implementation StatusItemView @implementation StatusItemView
- (id)initWithIcon:(atom::TrayIconCocoa*)icon { - (id)initWithImage:(NSImage*)image icon:(atom::TrayIconCocoa*)icon {
image_.reset([image copy]);
trayIcon_ = icon; trayIcon_ = icon;
isHighlightEnable_ = YES; isHighlightEnable_ = YES;
statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength:
NSVariableStatusItemLength] retain]); // Get the initial size.
CGFloat itemLength = [[statusItem_ statusBar] thickness]; NSStatusBar* statusBar = [NSStatusBar systemStatusBar];
NSRect frame = NSMakeRect(0, NSRect frame = NSMakeRect(0, 0, [self fullWidth], [statusBar thickness]);
0,
itemLength,
itemLength);
if ((self = [super initWithFrame:frame])) { if ((self = [super initWithFrame:frame])) {
image_view_.reset([[[NSImageView alloc] initWithFrame:frame] retain]); // Setup the image view.
NSRect iconFrame = frame;
iconFrame.size.width = [self iconWidth];
image_view_.reset([[NSImageView alloc] initWithFrame:iconFrame]);
[image_view_ setImageScaling:NSImageScaleNone];
[image_view_ setImageAlignment:NSImageAlignCenter];
[self addSubview:image_view_];
// Unregister image_view_ as a dragged destination, allows its parent view // Unregister image_view_ as a dragged destination, allows its parent view
// (StatusItemView) handle dragging events. // (StatusItemView) handle dragging events.
[image_view_ unregisterDraggedTypes]; [image_view_ unregisterDraggedTypes];
[self addSubview:image_view_]; NSArray* types = [NSArray arrayWithObjects:NSFilenamesPboardType, nil];
[self registerForDraggedTypes: [self registerForDraggedTypes:types];
[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
// Create the status item.
statusItem_.reset([[[NSStatusBar systemStatusBar]
statusItemWithLength:NSWidth(frame)] retain]);
[statusItem_ setView:self]; [statusItem_ setView:self];
} }
return self; return self;
@ -66,68 +77,94 @@ const CGFloat kHorizontalMargin = 4;
// ---------------- // ----------------
// | icon | title | // | icon | title |
/// ---------------- /// ----------------
BOOL highlight = [self shouldHighlight];
CGFloat titleWidth = [self titleWidth];
CGFloat statusItemHeight = [[statusItem_ statusBar] thickness];
CGFloat iconWidth =((highlight && alternateImage_) ?
[alternateImage_ size].width : [image_ size].width) +
2 * kHorizontalMargin;
NSRect iconRect = NSMakeRect(0, 0, iconWidth, statusItemHeight);
// Calculate the total status item bounds. // Draw background.
CGFloat statusItemWidth = iconWidth + titleWidth; BOOL highlight = [self shouldHighlight];
// If title is set, need to add right margin to the title. CGFloat thickness = [[statusItem_ statusBar] thickness];
if (title_) { NSRect statusItemBounds = NSMakeRect(0, 0, [statusItem_ length], thickness);
statusItemWidth += kHorizontalMargin;
}
NSRect statusItemBounds = NSMakeRect(0,
0,
statusItemWidth,
statusItemHeight);
[statusItem_ drawStatusBarBackgroundInRect:statusItemBounds [statusItem_ drawStatusBarBackgroundInRect:statusItemBounds
withHighlight:highlight]; withHighlight:highlight];
[statusItem_ setLength:statusItemWidth];
// Custom ImageView // Make use of NSImageView to draw the image, which can correctly draw
[image_view_ setFrame: iconRect]; // template image under dark menu bar.
if (highlight && alternateImage_) { if (highlight && alternateImage_ &&
[image_view_ image] != alternateImage_.get()) {
[image_view_ setImage:alternateImage_]; [image_view_ setImage:alternateImage_];
} else { } else if ([image_view_ image] != image_.get()) {
[image_view_ setImage:image_]; [image_view_ setImage:image_];
} }
if (title_) { if (title_) {
NSRect titleDrawRect = NSMakeRect(iconWidth, // Highlight the text when icon is highlighted or in dark mode.
0, highlight |= [self isDarkMode];
statusItemWidth - kHorizontalMargin, // Draw title.
statusItemHeight); NSRect titleDrawRect = NSMakeRect(
[self iconWidth], -kVerticalTitleMargin, [self titleWidth], thickness);
[title_ drawInRect:titleDrawRect [title_ drawInRect:titleDrawRect
withAttributes:[self titleAttributes]]; withAttributes:[self titleAttributesWithHighlight:highlight]];
} }
} }
- (BOOL) isDarkMode { - (BOOL)isDarkMode {
return [[[NSAppearance currentAppearance] name] hasPrefix: NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSAppearanceNameVibrantDark]; NSString* mode = [defaults stringForKey:@"AppleInterfaceStyle"];
return mode && [mode isEqualToString:@"Dark"];
} }
// The width of the full status item.
- (CGFloat)fullWidth {
if (title_)
return [self iconWidth] + [self titleWidth] + kHorizontalMargin;
else
return [self iconWidth];
}
// The width of the icon.
- (CGFloat)iconWidth {
CGFloat thickness = [[NSStatusBar systemStatusBar] thickness];
CGFloat imageHeight = [image_ size].height;
CGFloat imageWidth = [image_ size].width;
CGFloat iconWidth = imageWidth;
if (imageWidth < thickness) {
// Image's width must be larger than menu bar's height.
iconWidth = thickness;
} else {
CGFloat verticalMargin = thickness - imageHeight;
// Image must have same horizontal vertical margin.
if (verticalMargin > 0 && imageWidth != imageHeight)
iconWidth = imageWidth + verticalMargin;
CGFloat horizontalMargin = thickness - imageWidth;
// Image must have at least kHorizontalMargin horizontal margin on each
// side.
if (horizontalMargin < 2 * kHorizontalMargin)
iconWidth = imageWidth + 2 * kHorizontalMargin;
}
return iconWidth;
}
// The width of the title.
- (CGFloat)titleWidth { - (CGFloat)titleWidth {
if (!title_) return 0; if (!title_)
NSAttributedString* attributes = return 0;
base::scoped_nsobject<NSAttributedString> attributes(
[[NSAttributedString alloc] initWithString:title_ [[NSAttributedString alloc] initWithString:title_
attributes:[self titleAttributes]]; attributes:[self titleAttributes]]);
return [attributes size].width; return [attributes size].width;
} }
- (NSDictionary*)titleAttributes { - (NSDictionary*)titleAttributesWithHighlight:(BOOL)highlight {
NSFont* font = [NSFont menuBarFontOfSize:0]; NSFont* font = [NSFont menuBarFontOfSize:0];
NSColor* foregroundColor = NSColor* foregroundColor = highlight ?
[self isDarkMode] ? [NSColor whiteColor] : [NSColor blackColor]; [NSColor whiteColor] :
[NSColor colorWithRed:0.265625 green:0.25390625 blue:0.234375 alpha:1.0];
return [NSDictionary dictionaryWithObjectsAndKeys: return [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName, font, NSFontAttributeName,
foregroundColor, NSForegroundColorAttributeName, foregroundColor, NSForegroundColorAttributeName,
nil]; nil];
}
- (NSDictionary*)titleAttributes {
return [self titleAttributesWithHighlight:[self isDarkMode]];
} }
- (void)setImage:(NSImage*)image { - (void)setImage:(NSImage*)image {
@ -144,7 +181,11 @@ const CGFloat kHorizontalMargin = 4;
} }
- (void)setTitle:(NSString*)title { - (void)setTitle:(NSString*)title {
title_.reset([title copy]); if (title.length > 0)
title_.reset([title copy]);
else
title_.reset();
[statusItem_ setLength:[self fullWidth]];
[self setNeedsDisplay:YES]; [self setNeedsDisplay:YES];
} }
@ -188,7 +229,7 @@ const CGFloat kHorizontalMargin = 4;
- (void)popContextMenu { - (void)popContextMenu {
if (menuController_ && ![menuController_ isMenuOpen]) { if (menuController_ && ![menuController_ isMenuOpen]) {
// redraw the dray icon to show highlight if it is enabled. // Redraw the dray icon to show highlight if it is enabled.
[self setNeedsDisplay:YES]; [self setNeedsDisplay:YES];
[statusItem_ popUpStatusItemMenu:[menuController_ menu]]; [statusItem_ popUpStatusItemMenu:[menuController_ menu]];
// The popUpStatusItemMenu returns only after the showing menu is closed. // The popUpStatusItemMenu returns only after the showing menu is closed.
@ -222,8 +263,8 @@ const CGFloat kHorizontalMargin = 4;
} }
- (BOOL)shouldHighlight { - (BOOL)shouldHighlight {
BOOL is_menu_open = [menuController_ isMenuOpen]; BOOL isMenuOpen = menuController_ && [menuController_ isMenuOpen];
return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open); return isHighlightEnable_ && (inMouseEventSequence_ || isMenuOpen);
} }
- (gfx::Rect)getBoundsFromEvent:(NSEvent*)event { - (gfx::Rect)getBoundsFromEvent:(NSEvent*)event {
@ -238,7 +279,6 @@ const CGFloat kHorizontalMargin = 4;
namespace atom { namespace atom {
TrayIconCocoa::TrayIconCocoa() { TrayIconCocoa::TrayIconCocoa() {
status_item_view_.reset([[StatusItemView alloc] initWithIcon:this]);
} }
TrayIconCocoa::~TrayIconCocoa() { TrayIconCocoa::~TrayIconCocoa() {
@ -246,7 +286,13 @@ TrayIconCocoa::~TrayIconCocoa() {
} }
void TrayIconCocoa::SetImage(const gfx::Image& image) { void TrayIconCocoa::SetImage(const gfx::Image& image) {
[status_item_view_ setImage:image.AsNSImage()]; if (status_item_view_) {
[status_item_view_ setImage:image.AsNSImage()];
} else {
status_item_view_.reset(
[[StatusItemView alloc] initWithImage:image.AsNSImage()
icon:this]);
}
} }
void TrayIconCocoa::SetPressedImage(const gfx::Image& image) { void TrayIconCocoa::SetPressedImage(const gfx::Image& image) {