OLD | NEW |
---|---|
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 kStatusItemLengthIcon = 27; |
17 | |
18 // The width of the status bar item when it's the icon and a number. | |
19 const CGFloat kStatusItemLengthNumber = 35; | |
20 | |
21 // The width of the status bar item when it's the icon, a number, and a + sign. | |
sail
2013/05/28 19:40:22
I was a little surprised by this approach. I thoug
| |
22 const CGFloat kStatusItemLengthNumberPlus = 43; | |
17 | 23 |
18 // The amount of space between the edge of the status item and where the icon | 24 // The amount of space between the edge of the status item and where the icon |
19 // should start drawing. | 25 // should start drawing. |
20 const CGFloat kInset = 6; | 26 const CGFloat kInset = 5; |
27 | |
28 // The amount of space between the icon and the unread count number. | |
29 const CGFloat kUnreadCountLeftMargin = 3; | |
30 | |
31 // The lower-left Y coordinate of the unread count number. | |
32 const CGFloat kUnreadCountMinY = 4; | |
21 | 33 |
22 } // namespace | 34 } // namespace |
23 | 35 |
24 @implementation MCStatusItemView | 36 @implementation MCStatusItemView |
25 | 37 |
26 @synthesize unreadCount = unreadCount_; | 38 @synthesize unreadCount = unreadCount_; |
27 @synthesize highlight = highlight_; | 39 @synthesize highlight = highlight_; |
28 | 40 |
29 - (id)initWithStatusItem:(NSStatusItem*)item { | 41 - (id)initWithStatusItem:(NSStatusItem*)item { |
30 CGFloat thickness = [[item statusBar] thickness]; | 42 CGFloat thickness = [[item statusBar] thickness]; |
31 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness); | 43 NSRect frame = NSMakeRect(0, 0, kStatusItemLengthIcon, thickness); |
32 if ((self = [super initWithFrame:frame])) { | 44 if ((self = [super initWithFrame:frame])) { |
33 statusItem_.reset([item retain]); | 45 statusItem_.reset([item retain]); |
34 [statusItem_ setView:self]; | 46 [statusItem_ setView:self]; |
35 } | 47 } |
36 return self; | 48 return self; |
37 } | 49 } |
38 | 50 |
39 - (message_center::StatusItemClickedCallack)callback { | 51 - (message_center::StatusItemClickedCallack)callback { |
40 return callback_.get(); | 52 return callback_.get(); |
41 } | 53 } |
42 | 54 |
43 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { | 55 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { |
44 callback_.reset(Block_copy(callback)); | 56 callback_.reset(Block_copy(callback)); |
45 } | 57 } |
46 | 58 |
47 - (void)setUnreadCount:(size_t)unreadCount { | 59 - (void)setUnreadCount:(size_t)unreadCount { |
48 unreadCount_ = unreadCount; | 60 unreadCount_ = unreadCount; |
61 | |
62 NSRect frame = [self frame]; | |
63 if (unreadCount_ == 0) | |
64 frame.size.width = kStatusItemLengthIcon; | |
65 else if (unreadCount_ < 10) | |
66 frame.size.width = kStatusItemLengthNumber; | |
67 else | |
68 frame.size.width = kStatusItemLengthNumberPlus; | |
69 [self setFrame:frame]; | |
70 | |
49 [self setNeedsDisplay:YES]; | 71 [self setNeedsDisplay:YES]; |
50 } | 72 } |
51 | 73 |
52 - (void)setHighlight:(BOOL)highlight { | 74 - (void)setHighlight:(BOOL)highlight { |
53 highlight_ = highlight; | 75 highlight_ = highlight; |
54 [self setNeedsDisplay:YES]; | 76 [self setNeedsDisplay:YES]; |
55 } | 77 } |
56 | 78 |
57 - (void)mouseDown:(NSEvent*)event { | 79 - (void)mouseDown:(NSEvent*)event { |
58 DCHECK(!inMouseEventSequence_); | 80 DCHECK(!inMouseEventSequence_); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 else | 121 else |
100 count = [NSString stringWithFormat:@"%"PRIuS, unreadCount_]; | 122 count = [NSString stringWithFormat:@"%"PRIuS, unreadCount_]; |
101 | 123 |
102 NSColor* fontColor = highlight ? [NSColor whiteColor] | 124 NSColor* fontColor = highlight ? [NSColor whiteColor] |
103 : [NSColor blackColor]; | 125 : [NSColor blackColor]; |
104 NSDictionary* attributes = @{ | 126 NSDictionary* attributes = @{ |
105 NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12], | 127 NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12], |
106 NSForegroundColorAttributeName: fontColor, | 128 NSForegroundColorAttributeName: fontColor, |
107 }; | 129 }; |
108 | 130 |
109 // Center the string inside the remaining space of the status item. | |
110 NSSize stringSize = [count sizeWithAttributes:attributes]; | |
111 NSRect iconSlice, textSlice; | |
112 NSDivideRect(frame, &iconSlice, &textSlice, NSMaxX(drawRect), NSMinXEdge); | |
113 NSPoint countPoint = NSMakePoint( | 131 NSPoint countPoint = NSMakePoint( |
114 floorf(NSMinX(textSlice) + (NSWidth(textSlice) - stringSize.width) / 2), | 132 NSMaxX(drawRect) + kUnreadCountLeftMargin, kUnreadCountMinY); |
115 floorf(NSMinY(textSlice) + | |
116 (NSHeight(textSlice) - stringSize.height) / 2)); | |
117 | |
118 [count drawAtPoint:countPoint withAttributes:attributes]; | 133 [count drawAtPoint:countPoint withAttributes:attributes]; |
119 } | 134 } |
120 } | 135 } |
121 | 136 |
122 @end | 137 @end |
OLD | NEW |