Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(168)

Side by Side Diff: ui/message_center/cocoa/status_item_view.mm

Issue 16159005: [Mac][MC] Adjust the size of the status item view based on the unread count. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nit Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | ui/message_center/cocoa/status_item_view_unittest.mm » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ui/message_center/cocoa/status_item_view.h" 5 #import "ui/message_center/cocoa/status_item_view.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "grit/ui_resources.h" 10 #include "grit/ui_resources.h"
11 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/base/resource/resource_bundle.h"
12 12
13 namespace { 13 namespace {
14 14
15 // The width of the status bar. 15 // The width of the status bar item when it's just the icon.
16 const CGFloat kStatusItemLength = 45; 16 const CGFloat kStatusItemLength = 26;
17 17
18 // The amount of space between the edge of the status item and where the icon 18 // The amount of space between the left and right edges and the content of the
19 // should start drawing. 19 // status item.
20 const CGFloat kInset = 6; 20 const CGFloat kMargin = 5;
21
22 // The amount of space between the icon and the unread count number.
23 const CGFloat kUnreadCountPadding = 3;
24
25 // The lower-left Y coordinate of the unread count number.
26 const CGFloat kUnreadCountMinY = 4;
21 27
22 } // namespace 28 } // namespace
23 29
30 @interface MCStatusItemView (Private)
31 // Whether or not the status item should be drawn highlighted.
32 - (BOOL)shouldHighlight;
33
34 // Returns an autoreleased, styled string for the unread count.
35 - (NSAttributedString*)unreadCountString;
36 @end
37
24 @implementation MCStatusItemView 38 @implementation MCStatusItemView
25 39
26 @synthesize unreadCount = unreadCount_; 40 @synthesize unreadCount = unreadCount_;
27 @synthesize highlight = highlight_; 41 @synthesize highlight = highlight_;
28 42
29 - (id)initWithStatusItem:(NSStatusItem*)item { 43 - (id)initWithStatusItem:(NSStatusItem*)item {
30 CGFloat thickness = [[item statusBar] thickness]; 44 CGFloat thickness = [[item statusBar] thickness];
31 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness); 45 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness);
32 if ((self = [super initWithFrame:frame])) { 46 if ((self = [super initWithFrame:frame])) {
33 statusItem_.reset([item retain]); 47 statusItem_.reset([item retain]);
34 [statusItem_ setView:self]; 48 [statusItem_ setView:self];
35 } 49 }
36 return self; 50 return self;
37 } 51 }
38 52
39 - (message_center::StatusItemClickedCallack)callback { 53 - (message_center::StatusItemClickedCallack)callback {
40 return callback_.get(); 54 return callback_.get();
41 } 55 }
42 56
43 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { 57 - (void)setCallback:(message_center::StatusItemClickedCallack)callback {
44 callback_.reset(Block_copy(callback)); 58 callback_.reset(Block_copy(callback));
45 } 59 }
46 60
47 - (void)setUnreadCount:(size_t)unreadCount { 61 - (void)setUnreadCount:(size_t)unreadCount {
48 unreadCount_ = unreadCount; 62 unreadCount_ = unreadCount;
63
64 NSRect frame = [self frame];
65 frame.size.width = kStatusItemLength;
66 NSAttributedString* countString = [self unreadCountString];
67 if (countString) {
68 // Get the subpixel bounding rectangle for the string. -size doesn't yield
69 // correct results for pixel-precise drawing, since it doesn't use the
70 // device metrics.
71 NSRect boundingRect =
72 [countString boundingRectWithSize:NSZeroSize
73 options:NSStringDrawingUsesDeviceMetrics];
74 frame.size.width += roundf(NSWidth(boundingRect)) + kMargin;
75 }
76 [self setFrame:frame];
77
49 [self setNeedsDisplay:YES]; 78 [self setNeedsDisplay:YES];
50 } 79 }
51 80
52 - (void)setHighlight:(BOOL)highlight { 81 - (void)setHighlight:(BOOL)highlight {
53 highlight_ = highlight; 82 highlight_ = highlight;
54 [self setNeedsDisplay:YES]; 83 [self setNeedsDisplay:YES];
55 } 84 }
56 85
57 - (void)mouseDown:(NSEvent*)event { 86 - (void)mouseDown:(NSEvent*)event {
58 inMouseEventSequence_ = YES; 87 inMouseEventSequence_ = YES;
(...skipping 21 matching lines...) Expand all
80 } 109 }
81 110
82 - (void)otherMouseUp:(NSEvent*)event { 111 - (void)otherMouseUp:(NSEvent*)event {
83 [self mouseUp:event]; 112 [self mouseUp:event];
84 } 113 }
85 114
86 - (void)drawRect:(NSRect)dirtyRect { 115 - (void)drawRect:(NSRect)dirtyRect {
87 NSRect frame = [self bounds]; 116 NSRect frame = [self bounds];
88 117
89 // Draw the background color. 118 // Draw the background color.
90 BOOL highlight = highlight_ || inMouseEventSequence_; 119 BOOL highlight = [self shouldHighlight];
91 [statusItem_ drawStatusBarBackgroundInRect:frame 120 [statusItem_ drawStatusBarBackgroundInRect:frame
92 withHighlight:highlight]; 121 withHighlight:highlight];
93 122
94 // Draw the icon. 123 // Draw the icon.
95 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 124 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
96 NSImage* image = rb.GetNativeImageNamed( 125 NSImage* image = rb.GetNativeImageNamed(
97 highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage(); 126 highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage();
98 NSSize size = [image size]; 127 NSSize size = [image size];
99 NSRect drawRect = NSMakeRect(kInset, 128 NSRect drawRect = NSMakeRect(kMargin,
100 floorf((NSHeight(frame) - size.height) / 2), 129 floorf((NSHeight(frame) - size.height) / 2),
101 size.width, 130 size.width,
102 size.height); 131 size.height);
103 [image drawInRect:drawRect 132 [image drawInRect:drawRect
104 fromRect:NSZeroRect 133 fromRect:NSZeroRect
105 operation:NSCompositeSourceOver 134 operation:NSCompositeSourceOver
106 fraction:1.0]; 135 fraction:1.0];
107 136
108 // Draw the unread count. 137 // Draw the unread count.
109 if (unreadCount_ > 0) { 138 NSAttributedString* countString = [self unreadCountString];
110 NSString* count = nil; 139 if (countString) {
111 if (unreadCount_ > 9)
112 count = @"9+";
113 else
114 count = [NSString stringWithFormat:@"%"PRIuS, unreadCount_];
115
116 NSColor* fontColor = highlight ? [NSColor whiteColor]
117 : [NSColor blackColor];
118 NSDictionary* attributes = @{
119 NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12],
120 NSForegroundColorAttributeName: fontColor,
121 };
122
123 // Center the string inside the remaining space of the status item.
124 NSSize stringSize = [count sizeWithAttributes:attributes];
125 NSRect iconSlice, textSlice;
126 NSDivideRect(frame, &iconSlice, &textSlice, NSMaxX(drawRect), NSMinXEdge);
127 NSPoint countPoint = NSMakePoint( 140 NSPoint countPoint = NSMakePoint(
128 floorf(NSMinX(textSlice) + (NSWidth(textSlice) - stringSize.width) / 2), 141 NSMaxX(drawRect) + kUnreadCountPadding, kUnreadCountMinY);
129 floorf(NSMinY(textSlice) + 142 [countString drawAtPoint:countPoint];
130 (NSHeight(textSlice) - stringSize.height) / 2));
131
132 [count drawAtPoint:countPoint withAttributes:attributes];
133 } 143 }
134 } 144 }
135 145
146 // Private /////////////////////////////////////////////////////////////////////
147
148 - (BOOL)shouldHighlight {
149 return highlight_ || inMouseEventSequence_;
150 }
151
152 - (NSAttributedString*)unreadCountString {
153 if (unreadCount_ == 0)
154 return nil;
155
156 NSString* count = nil;
157 if (unreadCount_ > 9)
158 count = @"9+";
159 else
160 count = [NSString stringWithFormat:@"%"PRIuS, unreadCount_];
161
162 NSColor* fontColor = [self shouldHighlight] ? [NSColor whiteColor]
163 : [NSColor blackColor];
164 NSDictionary* attributes = @{
165 NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12],
166 NSForegroundColorAttributeName: fontColor,
167 };
168 return [[[NSAttributedString alloc] initWithString:count
169 attributes:attributes] autorelease];
170 }
171
136 @end 172 @end
OLDNEW
« no previous file with comments | « no previous file | ui/message_center/cocoa/status_item_view_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698