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 #include "chrome/browser/ui/cocoa/notifications/message_center_tray_bridge.h"
8 #include "base/i18n/number_formatting.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/browser_process.h"
11 #include "grit/chromium_strings.h"
12 #include "grit/generated_resources.h"
13 #include "grit/ui_strings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #import "ui/base/l10n/l10n_util_mac.h"
16 #import "ui/message_center/cocoa/popup_collection.h"
17 #import "ui/message_center/cocoa/status_item_view.h"
18 #import "ui/message_center/cocoa/tray_controller.h"
19 #import "ui/message_center/cocoa/tray_view_controller.h"
20 #include "ui/message_center/message_center.h"
21 #include "ui/message_center/message_center_tray.h"
22 #include "ui/message_center/message_center_util.h"
24 namespace message_center {
26 MessageCenterTrayDelegate* CreateMessageCenterTray() {
27 return new MessageCenterTrayBridge(g_browser_process->message_center());
30 } // namespace message_center
32 MessageCenterTrayBridge::MessageCenterTrayBridge(
33 message_center::MessageCenter* message_center)
34 : message_center_(message_center),
35 tray_(new message_center::MessageCenterTray(this, message_center)),
36 weak_ptr_factory_(this) {
37 if (message_center::GetMessageCenterShowState() ==
38 message_center::MESSAGE_CENTER_SHOW_ALWAYS) {
43 MessageCenterTrayBridge::~MessageCenterTrayBridge() {
44 [status_item_view_ removeItem];
47 void MessageCenterTrayBridge::OnMessageCenterTrayChanged() {
48 // Update the status item on the next run of the message loop so that if a
49 // popup is displayed, the item doesn't flash the unread count.
50 base::MessageLoop::current()->PostTask(FROM_HERE,
51 base::Bind(&MessageCenterTrayBridge::UpdateStatusItem,
52 weak_ptr_factory_.GetWeakPtr()));
54 [tray_controller_ onMessageCenterTrayChanged];
57 bool MessageCenterTrayBridge::ShowPopups() {
58 popup_collection_.reset(
59 [[MCPopupCollection alloc] initWithMessageCenter:message_center_]);
63 void MessageCenterTrayBridge::HidePopups() {
64 popup_collection_.reset();
67 bool MessageCenterTrayBridge::ShowMessageCenter() {
75 void MessageCenterTrayBridge::HideMessageCenter() {
76 [status_item_view_ setHighlight:NO];
77 [tray_controller_ close];
78 tray_controller_.autorelease();
82 bool MessageCenterTrayBridge::ShowNotifierSettings() {
83 // This method needs to be implemented when the context menu of each
84 // notification is ready and it contains 'settings' menu item.
88 bool MessageCenterTrayBridge::IsContextMenuEnabled() const {
89 // Cocoa message center doesn't support context menu yet.
93 message_center::MessageCenterTray*
94 MessageCenterTrayBridge::GetMessageCenterTray() {
98 void MessageCenterTrayBridge::UpdateStatusItem() {
99 // Only show the status item if there are notifications.
100 message_center::MessageCenterShowState show_state =
101 message_center::GetMessageCenterShowState();
102 if (show_state == message_center::MESSAGE_CENTER_SHOW_UNREAD &&
103 message_center_->NotificationCount() == 0) {
104 [status_item_view_ removeItem];
105 status_item_view_.reset();
107 } else if (show_state == message_center::MESSAGE_CENTER_SHOW_NEVER) {
111 if (!status_item_view_) {
112 status_item_view_.reset([[MCStatusItemView alloc] init]);
113 [status_item_view_ setCallback:^{ tray_->ToggleMessageCenterBubble(); }];
116 // We want a static message center icon while it's visible.
117 if (message_center()->IsMessageCenterVisible())
120 size_t unread_count = message_center_->UnreadNotificationCount();
121 bool quiet_mode = message_center_->IsQuietMode();
122 [status_item_view_ setUnreadCount:unread_count withQuietMode:quiet_mode];
124 if (unread_count > 0) {
125 base::string16 unread_count_string = base::FormatNumber(unread_count);
126 [status_item_view_ setToolTip:
127 l10n_util::GetNSStringF(IDS_MESSAGE_CENTER_TOOLTIP_UNREAD,
128 unread_count_string)];
130 [status_item_view_ setToolTip:
131 l10n_util::GetNSString(IDS_MESSAGE_CENTER_TOOLTIP)];
135 void MessageCenterTrayBridge::OpenTrayWindow() {
136 DCHECK(!tray_controller_);
137 tray_controller_.reset(
138 [[MCTrayController alloc] initWithMessageCenterTray:tray_.get()]);
139 [[tray_controller_ viewController] setTrayTitle:
140 l10n_util::GetNSStringF(IDS_MESSAGE_CENTER_FOOTER_WITH_PRODUCT_TITLE,
141 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))];
145 DCHECK(status_item_view_);
146 [status_item_view_ setHighlight:YES];
147 NSRect frame = [[status_item_view_ window] frame];
148 [tray_controller_ showTrayAtRightOf:NSMakePoint(NSMinX(frame),
150 atLeftOf:NSMakePoint(NSMaxX(frame),