| 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 |
| 59 - (size_t)unreadCount { |
| 60 return unreadCount_; |
| 61 } |
| 62 |
| 60 - (message_center::StatusItemClickedCallack)callback { | 63 - (message_center::StatusItemClickedCallack)callback { |
| 61 return callback_.get(); | 64 return callback_.get(); |
| 62 } | 65 } |
| 63 | 66 |
| 64 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { | 67 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { |
| 65 callback_.reset(Block_copy(callback)); | 68 callback_.reset(Block_copy(callback)); |
| 66 } | 69 } |
| 67 | 70 |
| 68 - (void)setUnreadCount:(size_t)unreadCount { | 71 - (void)setUnreadCount:(size_t)unreadCount withQuietMode:(BOOL)quietMode { |
| 69 unreadCount_ = unreadCount; | 72 unreadCount_ = unreadCount; |
| 73 quietMode_ = quietMode; |
| 70 | 74 |
| 71 NSRect frame = [self frame]; | 75 NSRect frame = [self frame]; |
| 72 frame.size.width = kStatusItemLength; | 76 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]; | 77 [self setFrame:frame]; |
| 84 | 78 |
| 85 [self setNeedsDisplay:YES]; | 79 [self setNeedsDisplay:YES]; |
| 86 } | 80 } |
| 87 | 81 |
| 88 - (void)setHighlight:(BOOL)highlight { | 82 - (void)setHighlight:(BOOL)highlight { |
| 89 highlight_ = highlight; | 83 highlight_ = highlight; |
| 90 [self setNeedsDisplay:YES]; | 84 [self setNeedsDisplay:YES]; |
| 91 } | 85 } |
| 92 | 86 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 120 } | 114 } |
| 121 | 115 |
| 122 - (void)drawRect:(NSRect)dirtyRect { | 116 - (void)drawRect:(NSRect)dirtyRect { |
| 123 NSRect frame = [self bounds]; | 117 NSRect frame = [self bounds]; |
| 124 | 118 |
| 125 // Draw the background color. | 119 // Draw the background color. |
| 126 BOOL highlight = [self shouldHighlight]; | 120 BOOL highlight = [self shouldHighlight]; |
| 127 [statusItem_ drawStatusBarBackgroundInRect:frame | 121 [statusItem_ drawStatusBarBackgroundInRect:frame |
| 128 withHighlight:highlight]; | 122 withHighlight:highlight]; |
| 129 | 123 |
| 124 int resource_id = [self getTrayResourceId]; |
| 130 // Draw the icon. | 125 // Draw the icon. |
| 131 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 126 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 132 NSImage* image = rb.GetNativeImageNamed( | 127 NSImage* image = rb.GetNativeImageNamed(resource_id).ToNSImage(); |
| 133 highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage(); | |
| 134 NSSize size = [image size]; | 128 NSSize size = [image size]; |
| 135 NSRect drawRect = NSMakeRect(kMargin, | 129 NSRect drawRect = NSMakeRect(kMargin, |
| 136 floorf((NSHeight(frame) - size.height) / 2), | 130 floorf((NSHeight(frame) - size.height) / 2), |
| 137 size.width, | 131 size.width, |
| 138 size.height); | 132 size.height); |
| 139 [image drawInRect:drawRect | 133 [image drawInRect:drawRect |
| 140 fromRect:NSZeroRect | 134 fromRect:NSZeroRect |
| 141 operation:NSCompositeSourceOver | 135 operation:NSCompositeSourceOver |
| 142 fraction:1.0]; | 136 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 } | 137 } |
| 152 | 138 |
| 153 - (NSArray*)accessibilityActionNames { | 139 - (NSArray*)accessibilityActionNames { |
| 154 return @[ NSAccessibilityPressAction ]; | 140 return @[ NSAccessibilityPressAction ]; |
| 155 } | 141 } |
| 156 | 142 |
| 157 - (void)accessibilityPerformAction:(NSString*)action { | 143 - (void)accessibilityPerformAction:(NSString*)action { |
| 158 if ([action isEqualToString:NSAccessibilityPressAction]) { | 144 if ([action isEqualToString:NSAccessibilityPressAction]) { |
| 159 if (callback_) | 145 if (callback_) |
| 160 callback_.get()(); | 146 callback_.get()(); |
| 161 return; | 147 return; |
| 162 } | 148 } |
| 163 [super accessibilityPerformAction:action]; | 149 [super accessibilityPerformAction:action]; |
| 164 } | 150 } |
| 165 | 151 |
| 166 // Private ///////////////////////////////////////////////////////////////////// | 152 // Private ///////////////////////////////////////////////////////////////////// |
| 167 | 153 |
| 168 - (BOOL)shouldHighlight { | 154 - (BOOL)shouldHighlight { |
| 169 return highlight_ || inMouseEventSequence_; | 155 return highlight_ || inMouseEventSequence_; |
| 170 } | 156 } |
| 171 | 157 |
| 172 - (NSAttributedString*)unreadCountString { | 158 - (int)getTrayResourceId { |
| 173 if (unreadCount_ == 0) | 159 BOOL highlight = [self shouldHighlight]; |
| 174 return nil; | 160 BOOL hasUnreadItems = unreadCount_ > 0; |
| 175 | 161 int kResourceIds[2][2][2] = { |
| 176 NSString* count = nil; | 162 { |
| 177 if (unreadCount_ > 9) | 163 { IDR_TRAY_EMPTY, IDR_TRAY_EMPTY_PRESSED }, |
| 178 count = @"9+"; | 164 { IDR_TRAY_ATTENTION, IDR_TRAY_ATTENTION_PRESSED }, |
| 179 else | 165 }, |
| 180 count = [NSString stringWithFormat:@"%" PRIuS, unreadCount_]; | 166 { |
| 181 | 167 { IDR_TRAY_DO_NOT_DISTURB_EMPTY, |
| 182 NSColor* fontColor = [self shouldHighlight] ? [NSColor whiteColor] | 168 IDR_TRAY_DO_NOT_DISTURB_EMPTY_PRESSED }, |
| 183 : [NSColor blackColor]; | 169 { IDR_TRAY_DO_NOT_DISTURB_ATTENTION, |
| 184 NSDictionary* attributes = @{ | 170 IDR_TRAY_DO_NOT_DISTURB_ATTENTION_PRESSED }, |
| 185 NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12], | 171 }, |
| 186 NSForegroundColorAttributeName: fontColor, | |
| 187 }; | 172 }; |
| 188 return [[[NSAttributedString alloc] initWithString:count | 173 return kResourceIds[quietMode_][hasUnreadItems][highlight]; |
| 189 attributes:attributes] autorelease]; | |
| 190 } | 174 } |
| 191 | 175 |
| 192 @end | 176 @end |
| OLD | NEW |