Chromium Code Reviews| 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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 25 // The lower-left Y coordinate of the unread count number. | 25 // The lower-left Y coordinate of the unread count number. |
| 26 const CGFloat kUnreadCountMinY = 4; | 26 const CGFloat kUnreadCountMinY = 4; |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 @interface MCStatusItemView (Private) | 30 @interface MCStatusItemView (Private) |
| 31 // Whether or not the status item should be drawn highlighted. | 31 // Whether or not the status item should be drawn highlighted. |
| 32 - (BOOL)shouldHighlight; | 32 - (BOOL)shouldHighlight; |
| 33 | 33 |
| 34 // Returns an autoreleased, styled string for the unread count. | 34 - (int)getTrayResourceId; |
| 35 - (NSAttributedString*)unreadCountString; | 35 |
| 36 @end | 36 @end |
| 37 | 37 |
| 38 @implementation MCStatusItemView | 38 @implementation MCStatusItemView |
| 39 | 39 |
| 40 @synthesize unreadCount = unreadCount_; | |
| 41 @synthesize highlight = highlight_; | 40 @synthesize highlight = highlight_; |
| 42 | 41 |
| 43 - (id)init { | 42 - (id)init { |
| 44 statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength: | 43 statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength: |
| 45 NSVariableStatusItemLength] retain]); | 44 NSVariableStatusItemLength] retain]); |
| 46 CGFloat thickness = [[statusItem_ statusBar] thickness]; | 45 CGFloat thickness = [[statusItem_ statusBar] thickness]; |
| 47 | 46 |
| 48 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness); | 47 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness); |
| 49 if ((self = [super initWithFrame:frame])) { | 48 if ((self = [super initWithFrame:frame])) { |
| 50 [statusItem_ setView:self]; | 49 [statusItem_ setView:self]; |
| 51 } | 50 } |
| 52 return self; | 51 return self; |
| 53 } | 52 } |
| 54 | 53 |
| 55 - (void)removeItem { | 54 - (void)removeItem { |
| 56 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem_]; | 55 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem_]; |
| 57 statusItem_.reset(); | 56 statusItem_.reset(); |
| 58 } | 57 } |
| 59 | 58 |
| 60 - (message_center::StatusItemClickedCallack)callback { | 59 - (message_center::StatusItemClickedCallack)callback { |
| 61 return callback_.get(); | 60 return callback_.get(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { | 63 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { |
| 65 callback_.reset(Block_copy(callback)); | 64 callback_.reset(Block_copy(callback)); |
| 66 } | 65 } |
| 67 | 66 |
| 68 - (void)setUnreadCount:(size_t)unreadCount { | 67 - (void)setUnreadCount:(size_t)unreadCount withQuietMode:(BOOL)quietMode { |
| 69 unreadCount_ = unreadCount; | 68 unreadCount_ = unreadCount; |
| 69 quietMode_ = quietMode; | |
| 70 | 70 |
| 71 NSRect frame = [self frame]; | 71 NSRect frame = [self frame]; |
| 72 frame.size.width = kStatusItemLength; | 72 frame.size.width = kStatusItemLength; |
| 73 NSAttributedString* countString = [self unreadCountString]; | |
| 74 if (countString) { | |
| 75 // Get the subpixel bounding rectangle for the string. -size doesn't yield | |
| 76 // correct results for pixel-precise drawing, since it doesn't use the | |
| 77 // device metrics. | |
| 78 NSRect boundingRect = | |
| 79 [countString boundingRectWithSize:NSZeroSize | |
| 80 options:NSStringDrawingUsesDeviceMetrics]; | |
| 81 frame.size.width += roundf(NSWidth(boundingRect)) + kMargin; | |
| 82 } | |
| 83 [self setFrame:frame]; | 73 [self setFrame:frame]; |
| 84 | 74 |
| 85 [self setNeedsDisplay:YES]; | 75 [self setNeedsDisplay:YES]; |
| 86 } | 76 } |
| 87 | 77 |
| 88 - (void)setHighlight:(BOOL)highlight { | 78 - (void)setHighlight:(BOOL)highlight { |
| 89 highlight_ = highlight; | 79 highlight_ = highlight; |
| 90 [self setNeedsDisplay:YES]; | 80 [self setNeedsDisplay:YES]; |
| 91 } | 81 } |
| 92 | 82 |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 120 } | 110 } |
| 121 | 111 |
| 122 - (void)drawRect:(NSRect)dirtyRect { | 112 - (void)drawRect:(NSRect)dirtyRect { |
| 123 NSRect frame = [self bounds]; | 113 NSRect frame = [self bounds]; |
| 124 | 114 |
| 125 // Draw the background color. | 115 // Draw the background color. |
| 126 BOOL highlight = [self shouldHighlight]; | 116 BOOL highlight = [self shouldHighlight]; |
| 127 [statusItem_ drawStatusBarBackgroundInRect:frame | 117 [statusItem_ drawStatusBarBackgroundInRect:frame |
| 128 withHighlight:highlight]; | 118 withHighlight:highlight]; |
| 129 | 119 |
| 120 int resource_id = [self getTrayResourceId]; | |
| 130 // Draw the icon. | 121 // Draw the icon. |
| 131 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 122 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 132 NSImage* image = rb.GetNativeImageNamed( | 123 NSImage* image = rb.GetNativeImageNamed(resource_id).ToNSImage(); |
| 133 highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage(); | |
| 134 NSSize size = [image size]; | 124 NSSize size = [image size]; |
| 135 NSRect drawRect = NSMakeRect(kMargin, | 125 NSRect drawRect = NSMakeRect(kMargin, |
| 136 floorf((NSHeight(frame) - size.height) / 2), | 126 floorf((NSHeight(frame) - size.height) / 2), |
| 137 size.width, | 127 size.width, |
| 138 size.height); | 128 size.height); |
| 139 [image drawInRect:drawRect | 129 [image drawInRect:drawRect |
| 140 fromRect:NSZeroRect | 130 fromRect:NSZeroRect |
| 141 operation:NSCompositeSourceOver | 131 operation:NSCompositeSourceOver |
| 142 fraction:1.0]; | 132 fraction:1.0]; |
| 143 | |
| 144 // Draw the unread count. | |
| 145 NSAttributedString* countString = [self unreadCountString]; | |
| 146 if (countString) { | |
| 147 NSPoint countPoint = NSMakePoint( | |
| 148 NSMaxX(drawRect) + kUnreadCountPadding, kUnreadCountMinY); | |
| 149 [countString drawAtPoint:countPoint]; | |
| 150 } | |
| 151 } | 133 } |
| 152 | 134 |
| 153 - (NSArray*)accessibilityActionNames { | 135 - (NSArray*)accessibilityActionNames { |
| 154 return @[ NSAccessibilityPressAction ]; | 136 return @[ NSAccessibilityPressAction ]; |
| 155 } | 137 } |
| 156 | 138 |
| 157 - (void)accessibilityPerformAction:(NSString*)action { | 139 - (void)accessibilityPerformAction:(NSString*)action { |
| 158 if ([action isEqualToString:NSAccessibilityPressAction]) { | 140 if ([action isEqualToString:NSAccessibilityPressAction]) { |
| 159 if (callback_) | 141 if (callback_) |
| 160 callback_.get()(); | 142 callback_.get()(); |
| 161 return; | 143 return; |
| 162 } | 144 } |
| 163 [super accessibilityPerformAction:action]; | 145 [super accessibilityPerformAction:action]; |
| 164 } | 146 } |
| 165 | 147 |
| 166 // Private ///////////////////////////////////////////////////////////////////// | 148 // Private ///////////////////////////////////////////////////////////////////// |
| 167 | 149 |
| 168 - (BOOL)shouldHighlight { | 150 - (BOOL)shouldHighlight { |
| 169 return highlight_ || inMouseEventSequence_; | 151 return highlight_ || inMouseEventSequence_; |
| 170 } | 152 } |
| 171 | 153 |
| 172 - (NSAttributedString*)unreadCountString { | 154 - (int)getTrayResourceId { |
| 173 if (unreadCount_ == 0) | 155 BOOL highlight = [self shouldHighlight]; |
| 174 return nil; | 156 BOOL hasUnreadItems = unreadCount_ > 0; |
|
Nico
2013/07/31 02:40:56
optional nit: What do you think about making this
dewittj
2013/07/31 21:58:41
Done.
| |
| 175 | 157 |
| 176 NSString* count = nil; | 158 if (quietMode_) { |
| 177 if (unreadCount_ > 9) | 159 if (hasUnreadItems) { |
| 178 count = @"9+"; | 160 if (highlight) |
| 179 else | 161 return IDR_TRAY_DO_NOT_DISTURB_ATTENTION_PRESSED; |
| 180 count = [NSString stringWithFormat:@"%" PRIuS, unreadCount_]; | 162 return IDR_TRAY_DO_NOT_DISTURB_ATTENTION; |
| 181 | 163 } |
| 182 NSColor* fontColor = [self shouldHighlight] ? [NSColor whiteColor] | 164 if (highlight) |
| 183 : [NSColor blackColor]; | 165 return IDR_TRAY_DO_NOT_DISTURB_EMPTY_PRESSED; |
| 184 NSDictionary* attributes = @{ | 166 return IDR_TRAY_DO_NOT_DISTURB_EMPTY; |
| 185 NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12], | 167 } |
| 186 NSForegroundColorAttributeName: fontColor, | 168 if (hasUnreadItems) { |
| 187 }; | 169 if (highlight) |
| 188 return [[[NSAttributedString alloc] initWithString:count | 170 return IDR_TRAY_ATTENTION_PRESSED; |
| 189 attributes:attributes] autorelease]; | 171 return IDR_TRAY_ATTENTION; |
| 172 } | |
| 173 if (highlight) | |
| 174 return IDR_TRAY_EMPTY_PRESSED; | |
| 175 return IDR_TRAY_EMPTY; | |
| 190 } | 176 } |
| 191 | 177 |
| 192 @end | 178 @end |
| OLD | NEW |