Commit 1871b5f0 authored by rsesek@chromium.org's avatar rsesek@chromium.org

[Mac][MC] Adjust the size of the status item view based on the unread count.

This also adjusts the baseline and min-X of the unread count.

BUG=243935

Review URL: https://chromiumcodereview.appspot.com/16159005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@202962 0039d316-1c4b-4281-b951-d872f2087c98
parent 1797d8fa
...@@ -12,15 +12,29 @@ ...@@ -12,15 +12,29 @@
namespace { namespace {
// The width of the status bar. // The width of the status bar item when it's just the icon.
const CGFloat kStatusItemLength = 45; const CGFloat kStatusItemLength = 26;
// The amount of space between the edge of the status item and where the icon // The amount of space between the left and right edges and the content of the
// should start drawing. // status item.
const CGFloat kInset = 6; 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 } // 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 @implementation MCStatusItemView
@synthesize unreadCount = unreadCount_; @synthesize unreadCount = unreadCount_;
...@@ -46,6 +60,21 @@ const CGFloat kInset = 6; ...@@ -46,6 +60,21 @@ const CGFloat kInset = 6;
- (void)setUnreadCount:(size_t)unreadCount { - (void)setUnreadCount:(size_t)unreadCount {
unreadCount_ = unreadCount; unreadCount_ = unreadCount;
NSRect frame = [self frame];
frame.size.width = kStatusItemLength;
NSAttributedString* countString = [self unreadCountString];
if (countString) {
// Get the subpixel bounding rectangle for the string. -size doesn't yield
// correct results for pixel-precise drawing, since it doesn't use the
// device metrics.
NSRect boundingRect =
[countString boundingRectWithSize:NSZeroSize
options:NSStringDrawingUsesDeviceMetrics];
frame.size.width += roundf(NSWidth(boundingRect)) + kMargin;
}
[self setFrame:frame];
[self setNeedsDisplay:YES]; [self setNeedsDisplay:YES];
} }
...@@ -87,7 +116,7 @@ const CGFloat kInset = 6; ...@@ -87,7 +116,7 @@ const CGFloat kInset = 6;
NSRect frame = [self bounds]; NSRect frame = [self bounds];
// Draw the background color. // Draw the background color.
BOOL highlight = highlight_ || inMouseEventSequence_; BOOL highlight = [self shouldHighlight];
[statusItem_ drawStatusBarBackgroundInRect:frame [statusItem_ drawStatusBarBackgroundInRect:frame
withHighlight:highlight]; withHighlight:highlight];
...@@ -96,7 +125,7 @@ const CGFloat kInset = 6; ...@@ -96,7 +125,7 @@ const CGFloat kInset = 6;
NSImage* image = rb.GetNativeImageNamed( NSImage* image = rb.GetNativeImageNamed(
highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage(); highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage();
NSSize size = [image size]; NSSize size = [image size];
NSRect drawRect = NSMakeRect(kInset, NSRect drawRect = NSMakeRect(kMargin,
floorf((NSHeight(frame) - size.height) / 2), floorf((NSHeight(frame) - size.height) / 2),
size.width, size.width,
size.height); size.height);
...@@ -106,31 +135,38 @@ const CGFloat kInset = 6; ...@@ -106,31 +135,38 @@ const CGFloat kInset = 6;
fraction:1.0]; fraction:1.0];
// Draw the unread count. // Draw the unread count.
if (unreadCount_ > 0) { NSAttributedString* countString = [self unreadCountString];
NSString* count = nil; if (countString) {
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);
NSPoint countPoint = NSMakePoint( NSPoint countPoint = NSMakePoint(
floorf(NSMinX(textSlice) + (NSWidth(textSlice) - stringSize.width) / 2), NSMaxX(drawRect) + kUnreadCountPadding, kUnreadCountMinY);
floorf(NSMinY(textSlice) + [countString drawAtPoint:countPoint];
(NSHeight(textSlice) - stringSize.height) / 2));
[count drawAtPoint:countPoint withAttributes:attributes];
} }
} }
// 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 @end
...@@ -54,14 +54,32 @@ TEST_F(StatusItemViewTest, Callback) { ...@@ -54,14 +54,32 @@ TEST_F(StatusItemViewTest, Callback) {
} }
TEST_F(StatusItemViewTest, UnreadCount) { TEST_F(StatusItemViewTest, UnreadCount) {
CGFloat initial_width = NSWidth([view_ frame]);
CGFloat width = initial_width;
[view_ setUnreadCount:2]; [view_ setUnreadCount:2];
[view_ display]; [view_ display];
EXPECT_GT(NSWidth([view_ frame]), width);
width = NSWidth([view_ frame]);
[view_ setUnreadCount:10]; [view_ setUnreadCount:10];
[view_ display]; [view_ display];
EXPECT_GT(NSWidth([view_ frame]), width);
width = NSWidth([view_ frame]);
CGFloat max_width = width;
[view_ setUnreadCount:0]; [view_ setUnreadCount:0];
[view_ display]; [view_ display];
EXPECT_LT(NSWidth([view_ frame]), width);
width = NSWidth([view_ frame]);
EXPECT_CGFLOAT_EQ(width, initial_width);
[view_ setUnreadCount:1000]; [view_ setUnreadCount:1000];
[view_ display]; [view_ display];
EXPECT_GT(NSWidth([view_ frame]), width);
width = NSWidth([view_ frame]);
EXPECT_CGFLOAT_EQ(width, max_width);
} }
TEST_F(StatusItemViewTest, Highlight) { TEST_F(StatusItemViewTest, Highlight) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment