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
3 // found in the LICENSE file.
5 #import "ui/message_center/cocoa/status_item_view.h"
9 #include "base/format_macros.h"
10 #include "grit/ui_resources.h"
11 #include "ui/base/resource/resource_bundle.h"
13 // The width of the status bar item when it's just the icon.
14 const CGFloat kStatusItemLength = 26;
16 // The amount of space between the left and right edges and the content of the
18 const CGFloat kMargin = 5;
21 @interface MCStatusItemView (Private)
22 // Whether or not the status item should be drawn highlighted.
23 - (BOOL)shouldHighlight;
25 - (int)getTrayResourceId;
28 @implementation MCStatusItemView
30 @synthesize highlight = highlight_;
33 statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength:
34 NSVariableStatusItemLength] retain]);
35 CGFloat thickness = [[statusItem_ statusBar] thickness];
37 NSRect frame = NSMakeRect(0, 0, kStatusItemLength, thickness);
38 if ((self = [super initWithFrame:frame])) {
39 [statusItem_ setView:self];
45 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem_];
49 - (size_t)unreadCount {
53 - (message_center::StatusItemClickedCallback)callback {
54 return callback_.get();
57 - (void)setCallback:(message_center::StatusItemClickedCallback)callback {
58 callback_.reset(callback, base::scoped_policy::RETAIN);
61 - (void)setUnreadCount:(size_t)unreadCount withQuietMode:(BOOL)quietMode {
62 unreadCount_ = unreadCount;
63 quietMode_ = quietMode;
65 NSRect frame = [self frame];
66 frame.size.width = kStatusItemLength;
67 [self setFrame:frame];
69 [self setNeedsDisplay:YES];
72 - (void)setHighlight:(BOOL)highlight {
73 highlight_ = highlight;
74 [self setNeedsDisplay:YES];
77 - (void)mouseDown:(NSEvent*)event {
78 inMouseEventSequence_ = YES;
79 [self setNeedsDisplay:YES];
85 - (void)mouseUp:(NSEvent*)event {
86 inMouseEventSequence_ = NO;
87 [self setNeedsDisplay:YES];
90 - (void)rightMouseDown:(NSEvent*)event {
91 [self mouseDown:event];
94 - (void)rightMouseUp:(NSEvent*)event {
98 - (void)otherMouseDown:(NSEvent*)event {
99 [self mouseDown:event];
102 - (void)otherMouseUp:(NSEvent*)event {
103 [self mouseUp:event];
106 - (void)drawRect:(NSRect)dirtyRect {
107 NSRect frame = [self bounds];
109 // Draw the background color.
110 BOOL highlight = [self shouldHighlight];
111 [statusItem_ drawStatusBarBackgroundInRect:frame
112 withHighlight:highlight];
114 int resource_id = [self getTrayResourceId];
116 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
117 NSImage* image = rb.GetNativeImageNamed(resource_id).ToNSImage();
118 NSSize size = [image size];
119 NSRect drawRect = NSMakeRect(kMargin,
120 floorf((NSHeight(frame) - size.height) / 2),
123 [image drawInRect:drawRect
125 operation:NSCompositeSourceOver
129 - (NSArray*)accessibilityActionNames {
130 return @[ NSAccessibilityPressAction ];
133 - (void)accessibilityPerformAction:(NSString*)action {
134 if ([action isEqualToString:NSAccessibilityPressAction]) {
139 [super accessibilityPerformAction:action];
142 // Private /////////////////////////////////////////////////////////////////////
144 - (BOOL)shouldHighlight {
145 return highlight_ || inMouseEventSequence_;
148 - (int)getTrayResourceId {
149 BOOL highlight = [self shouldHighlight];
150 BOOL hasUnreadItems = unreadCount_ > 0;
151 int kResourceIds[2][2][2] = {
153 { IDR_TRAY_EMPTY, IDR_TRAY_EMPTY_PRESSED },
154 { IDR_TRAY_ATTENTION, IDR_TRAY_ATTENTION_PRESSED },
157 { IDR_TRAY_DO_NOT_DISTURB_EMPTY,
158 IDR_TRAY_DO_NOT_DISTURB_EMPTY_PRESSED },
159 { IDR_TRAY_DO_NOT_DISTURB_ATTENTION,
160 IDR_TRAY_DO_NOT_DISTURB_ATTENTION_PRESSED },
163 return kResourceIds[quietMode_][hasUnreadItems][highlight];