Chromium Code Reviews| Index: ui/message_center/cocoa/status_item_view.mm |
| diff --git a/ui/message_center/cocoa/status_item_view.mm b/ui/message_center/cocoa/status_item_view.mm |
| index b884ae6dea2c467a91d1a58ee81a1e1a504b6408..0fa39de65bea1b80e01cd89a3180bf2416ae25e6 100644 |
| --- a/ui/message_center/cocoa/status_item_view.mm |
| +++ b/ui/message_center/cocoa/status_item_view.mm |
| @@ -12,15 +12,29 @@ |
| namespace { |
| -// The width of the status bar. |
| -const CGFloat kStatusItemLength = 45; |
| +// The width of the status bar item when it's just the icon. |
| +const CGFloat kStatusItemLength = 26; |
| -// The amount of space between the edge of the status item and where the icon |
| -// should start drawing. |
| -const CGFloat kInset = 6; |
| +// The amount of space between the left and right edges and the content of the |
| +// status item. |
| +const CGFloat kMargin = 5; |
| + |
| +// The amount of space between the icon and the unread count number. |
| +const CGFloat kUnreadCountPadding = 3; |
| + |
| +// The lower-left Y coordinate of the unread count number. |
| +const CGFloat kUnreadCountMinY = 4; |
| } // namespace |
| +@interface MCStatusItemView (Private) |
| +// Whether or not the status item should be drawn highlighted. |
| +- (BOOL)shouldHighlight; |
| + |
| +// Returns an autoreleased, styled string for the unread count. |
| +- (NSAttributedString*)unreadCountString; |
| +@end |
| + |
| @implementation MCStatusItemView |
| @synthesize unreadCount = unreadCount_; |
| @@ -46,6 +60,23 @@ const CGFloat kInset = 6; |
| - (void)setUnreadCount:(size_t)unreadCount { |
| unreadCount_ = unreadCount; |
| + |
| + NSRect frame = [self frame]; |
| + if (unreadCount_ == 0) { |
|
sail
2013/05/29 16:58:43
Small nit. It might be better to do:
frame.size.wi
Robert Sesek
2013/05/29 17:52:31
Done.
|
| + frame.size.width = kStatusItemLength; |
| + } else { |
| + // Get the subpixel bounding rectangle for the string. -size doesn't yield |
|
sail
2013/05/29 16:58:43
I didn't know that. Should I stop using -[NSAttrib
Robert Sesek
2013/05/29 17:52:31
No, it's fine for most cases. -size basically retu
|
| + // correct results for pixel-precise drawing, since it doesn't use the |
| + // device metrics. |
| + NSAttributedString* countString = [self unreadCountString]; |
| + NSRect boundingRect = |
| + [countString boundingRectWithSize:NSZeroSize |
| + options:NSStringDrawingUsesDeviceMetrics]; |
| + frame.size.width = |
| + kStatusItemLength + roundf(NSWidth(boundingRect)) + kMargin; |
| + } |
| + [self setFrame:frame]; |
| + |
| [self setNeedsDisplay:YES]; |
| } |
| @@ -87,7 +118,7 @@ const CGFloat kInset = 6; |
| NSRect frame = [self bounds]; |
| // Draw the background color. |
| - BOOL highlight = highlight_ || inMouseEventSequence_; |
| + BOOL highlight = [self shouldHighlight]; |
| [statusItem_ drawStatusBarBackgroundInRect:frame |
| withHighlight:highlight]; |
| @@ -96,7 +127,7 @@ const CGFloat kInset = 6; |
| NSImage* image = rb.GetNativeImageNamed( |
| highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage(); |
| NSSize size = [image size]; |
| - NSRect drawRect = NSMakeRect(kInset, |
| + NSRect drawRect = NSMakeRect(kMargin, |
| floorf((NSHeight(frame) - size.height) / 2), |
| size.width, |
| size.height); |
| @@ -106,31 +137,38 @@ const CGFloat kInset = 6; |
| fraction:1.0]; |
| // Draw the unread count. |
| - if (unreadCount_ > 0) { |
| - NSString* count = nil; |
| - if (unreadCount_ > 9) |
| - count = @"9+"; |
| - else |
| - count = [NSString stringWithFormat:@"%"PRIuS, unreadCount_]; |
| - |
| - NSColor* fontColor = highlight ? [NSColor whiteColor] |
| - : [NSColor blackColor]; |
| - NSDictionary* attributes = @{ |
| - NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12], |
| - NSForegroundColorAttributeName: fontColor, |
| - }; |
| - |
| - // Center the string inside the remaining space of the status item. |
| - NSSize stringSize = [count sizeWithAttributes:attributes]; |
| - NSRect iconSlice, textSlice; |
| - NSDivideRect(frame, &iconSlice, &textSlice, NSMaxX(drawRect), NSMinXEdge); |
| + NSAttributedString* countString = [self unreadCountString]; |
| + if (countString) { |
| NSPoint countPoint = NSMakePoint( |
| - floorf(NSMinX(textSlice) + (NSWidth(textSlice) - stringSize.width) / 2), |
| - floorf(NSMinY(textSlice) + |
| - (NSHeight(textSlice) - stringSize.height) / 2)); |
| - |
| - [count drawAtPoint:countPoint withAttributes:attributes]; |
| + NSMaxX(drawRect) + kUnreadCountPadding, kUnreadCountMinY); |
| + [countString drawAtPoint:countPoint]; |
| } |
| } |
| +// Private ///////////////////////////////////////////////////////////////////// |
| + |
| +- (BOOL)shouldHighlight { |
| + return highlight_ || inMouseEventSequence_; |
| +} |
| + |
| +- (NSAttributedString*)unreadCountString { |
| + if (unreadCount_ == 0) |
| + return nil; |
| + |
| + NSString* count = nil; |
| + if (unreadCount_ > 9) |
| + count = @"9+"; |
| + else |
| + count = [NSString stringWithFormat:@"%"PRIuS, unreadCount_]; |
| + |
| + NSColor* fontColor = [self shouldHighlight] ? [NSColor whiteColor] |
| + : [NSColor blackColor]; |
| + NSDictionary* attributes = @{ |
| + NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12], |
| + NSForegroundColorAttributeName: fontColor, |
| + }; |
| + return [[[NSAttributedString alloc] initWithString:count |
| + attributes:attributes] autorelease]; |
| +} |
| + |
| @end |