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. | |
| 35 - (NSAttributedString*)unreadCountString; | |
| 36 @end | 34 @end |
| 37 | 35 |
| 38 @implementation MCStatusItemView | 36 @implementation MCStatusItemView |
| 39 | 37 |
| 40 @synthesize unreadCount = unreadCount_; | |
| 41 @synthesize highlight = highlight_; | 38 @synthesize highlight = highlight_; |
| 42 | 39 |
| 43 - (id)init { | 40 - (id)init { |
| 44 statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength: | 41 statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength: |
| 45 NSVariableStatusItemLength] retain]); | 42 NSVariableStatusItemLength] retain]); |
| 46 CGFloat thickness = [[statusItem_ statusBar] thickness]; | 43 CGFloat thickness = [[statusItem_ statusBar] thickness]; |
| 47 | 44 |
| 48 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness); | 45 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness); |
| 49 if ((self = [super initWithFrame:frame])) { | 46 if ((self = [super initWithFrame:frame])) { |
| 50 [statusItem_ setView:self]; | 47 [statusItem_ setView:self]; |
| 51 } | 48 } |
| 52 return self; | 49 return self; |
| 53 } | 50 } |
| 54 | 51 |
| 55 - (void)removeItem { | 52 - (void)removeItem { |
| 56 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem_]; | 53 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem_]; |
| 57 statusItem_.reset(); | 54 statusItem_.reset(); |
| 58 } | 55 } |
| 59 | 56 |
| 60 - (message_center::StatusItemClickedCallack)callback { | 57 - (message_center::StatusItemClickedCallack)callback { |
| 61 return callback_.get(); | 58 return callback_.get(); |
| 62 } | 59 } |
| 63 | 60 |
| 64 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { | 61 - (void)setCallback:(message_center::StatusItemClickedCallack)callback { |
| 65 callback_.reset(Block_copy(callback)); | 62 callback_.reset(Block_copy(callback)); |
| 66 } | 63 } |
| 67 | 64 |
| 68 - (void)setUnreadCount:(size_t)unreadCount { | 65 - (void)setUnreadCount:(size_t)unreadCount withQuietMode:(BOOL)quietMode { |
| 69 unreadCount_ = unreadCount; | 66 unreadCount_ = unreadCount; |
| 67 quietMode_ = quietMode; | |
| 70 | 68 |
| 71 NSRect frame = [self frame]; | 69 NSRect frame = [self frame]; |
| 72 frame.size.width = kStatusItemLength; | 70 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]; | 71 [self setFrame:frame]; |
| 84 | 72 |
| 85 [self setNeedsDisplay:YES]; | 73 [self setNeedsDisplay:YES]; |
| 86 } | 74 } |
| 87 | 75 |
| 88 - (void)setHighlight:(BOOL)highlight { | 76 - (void)setHighlight:(BOOL)highlight { |
| 89 highlight_ = highlight; | 77 highlight_ = highlight; |
| 90 [self setNeedsDisplay:YES]; | 78 [self setNeedsDisplay:YES]; |
| 91 } | 79 } |
| 92 | 80 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 119 [self mouseUp:event]; | 107 [self mouseUp:event]; |
| 120 } | 108 } |
| 121 | 109 |
| 122 - (void)drawRect:(NSRect)dirtyRect { | 110 - (void)drawRect:(NSRect)dirtyRect { |
| 123 NSRect frame = [self bounds]; | 111 NSRect frame = [self bounds]; |
| 124 | 112 |
| 125 // Draw the background color. | 113 // Draw the background color. |
| 126 BOOL highlight = [self shouldHighlight]; | 114 BOOL highlight = [self shouldHighlight]; |
| 127 [statusItem_ drawStatusBarBackgroundInRect:frame | 115 [statusItem_ drawStatusBarBackgroundInRect:frame |
| 128 withHighlight:highlight]; | 116 withHighlight:highlight]; |
| 117 BOOL hasUnreadItems = unreadCount_ > 0; | |
| 118 | |
| 119 int resource_id = IDR_TRAY_EMPTY; | |
| 120 if (highlight) { | |
|
Nico
2013/07/31 00:09:41
This whole block looks like something that would l
dewittj
2013/07/31 00:37:09
Done. I also reordered the boolean checks to matc
| |
| 121 if (hasUnreadItems) { | |
| 122 if (quietMode_) | |
| 123 resource_id = IDR_TRAY_DO_NOT_DISTURB_ATTENTION_PRESSED; | |
| 124 else | |
| 125 resource_id = IDR_TRAY_ATTENTION_PRESSED; | |
| 126 } else if (quietMode_) { | |
| 127 resource_id = IDR_TRAY_DO_NOT_DISTURB_EMPTY_PRESSED; | |
| 128 } else { | |
| 129 resource_id = IDR_TRAY_EMPTY_PRESSED; | |
| 130 } | |
| 131 } else { | |
| 132 if (hasUnreadItems) { | |
| 133 if (quietMode_) | |
| 134 resource_id = IDR_TRAY_DO_NOT_DISTURB_ATTENTION; | |
| 135 else | |
| 136 resource_id = IDR_TRAY_ATTENTION; | |
| 137 } else if (quietMode_) { | |
| 138 resource_id = IDR_TRAY_DO_NOT_DISTURB_EMPTY; | |
| 139 } | |
| 140 } | |
| 129 | 141 |
| 130 // Draw the icon. | 142 // Draw the icon. |
| 131 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 143 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 132 NSImage* image = rb.GetNativeImageNamed( | 144 NSImage* image = rb.GetNativeImageNamed(resource_id).ToNSImage(); |
| 133 highlight ? IDR_TRAY_ICON_PRESSED : IDR_TRAY_ICON_REGULAR).ToNSImage(); | |
| 134 NSSize size = [image size]; | 145 NSSize size = [image size]; |
| 135 NSRect drawRect = NSMakeRect(kMargin, | 146 NSRect drawRect = NSMakeRect(kMargin, |
| 136 floorf((NSHeight(frame) - size.height) / 2), | 147 floorf((NSHeight(frame) - size.height) / 2), |
| 137 size.width, | 148 size.width, |
| 138 size.height); | 149 size.height); |
| 139 [image drawInRect:drawRect | 150 [image drawInRect:drawRect |
| 140 fromRect:NSZeroRect | 151 fromRect:NSZeroRect |
| 141 operation:NSCompositeSourceOver | 152 operation:NSCompositeSourceOver |
| 142 fraction:1.0]; | 153 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 } | 154 } |
| 152 | 155 |
| 153 - (NSArray*)accessibilityActionNames { | 156 - (NSArray*)accessibilityActionNames { |
| 154 return @[ NSAccessibilityPressAction ]; | 157 return @[ NSAccessibilityPressAction ]; |
| 155 } | 158 } |
| 156 | 159 |
| 157 - (void)accessibilityPerformAction:(NSString*)action { | 160 - (void)accessibilityPerformAction:(NSString*)action { |
| 158 if ([action isEqualToString:NSAccessibilityPressAction]) { | 161 if ([action isEqualToString:NSAccessibilityPressAction]) { |
| 159 if (callback_) | 162 if (callback_) |
| 160 callback_.get()(); | 163 callback_.get()(); |
| 161 return; | 164 return; |
| 162 } | 165 } |
| 163 [super accessibilityPerformAction:action]; | 166 [super accessibilityPerformAction:action]; |
| 164 } | 167 } |
| 165 | 168 |
| 166 // Private ///////////////////////////////////////////////////////////////////// | 169 // Private ///////////////////////////////////////////////////////////////////// |
| 167 | 170 |
| 168 - (BOOL)shouldHighlight { | 171 - (BOOL)shouldHighlight { |
| 169 return highlight_ || inMouseEventSequence_; | 172 return highlight_ || inMouseEventSequence_; |
| 170 } | 173 } |
| 171 | 174 |
| 172 - (NSAttributedString*)unreadCountString { | |
| 173 if (unreadCount_ == 0) | |
| 174 return nil; | |
| 175 | |
| 176 NSString* count = nil; | |
| 177 if (unreadCount_ > 9) | |
| 178 count = @"9+"; | |
| 179 else | |
| 180 count = [NSString stringWithFormat:@"%" PRIuS, unreadCount_]; | |
| 181 | |
| 182 NSColor* fontColor = [self shouldHighlight] ? [NSColor whiteColor] | |
| 183 : [NSColor blackColor]; | |
| 184 NSDictionary* attributes = @{ | |
| 185 NSFontAttributeName: [NSFont fontWithName:@"Helvetica-Bold" size:12], | |
| 186 NSForegroundColorAttributeName: fontColor, | |
| 187 }; | |
| 188 return [[[NSAttributedString alloc] initWithString:count | |
| 189 attributes:attributes] autorelease]; | |
| 190 } | |
| 191 | |
| 192 @end | 175 @end |
| OLD | NEW |