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"
23 namespace message_center {
25 MessageCenterTrayDelegate* CreateMessageCenterTray() {
26 return new MessageCenterTrayBridge(g_browser_process->message_center());
29 } // namespace message_center
31 MessageCenterTrayBridge::MessageCenterTrayBridge(
32 message_center::MessageCenter* message_center)
33 : message_center_(message_center),
34 tray_(new message_center::MessageCenterTray(this, message_center)),
35 weak_ptr_factory_(this) {
38 MessageCenterTrayBridge::~MessageCenterTrayBridge() {
39 [status_item_view_ removeItem];
42 void MessageCenterTrayBridge::OnMessageCenterTrayChanged() {
43 // Update the status item on the next run of the message loop so that if a
44 // popup is displayed, the item doesn't flash the unread count.
45 base::MessageLoop::current()->PostTask(FROM_HERE,
46 base::Bind(&MessageCenterTrayBridge::UpdateStatusItem,
47 weak_ptr_factory_.GetWeakPtr()));
49 [tray_controller_ onMessageCenterTrayChanged];
52 bool MessageCenterTrayBridge::ShowPopups() {
53 popup_collection_.reset(
54 [[MCPopupCollection alloc] initWithMessageCenter:message_center_]);
58 void MessageCenterTrayBridge::HidePopups() {
59 popup_collection_.reset();
62 bool MessageCenterTrayBridge::ShowMessageCenter() {
70 void MessageCenterTrayBridge::HideMessageCenter() {
71 [status_item_view_ setHighlight:NO];
72 [tray_controller_ close];
73 tray_controller_.autorelease();
77 bool MessageCenterTrayBridge::ShowNotifierSettings() {
78 // This method needs to be implemented when the context menu of each
79 // notification is ready and it contains 'settings' menu item.
83 bool MessageCenterTrayBridge::IsContextMenuEnabled() const {
84 // Cocoa message center doesn't support context menu yet.
88 message_center::MessageCenterTray*
89 MessageCenterTrayBridge::GetMessageCenterTray() {
93 void MessageCenterTrayBridge::UpdateStatusItem() {
94 if (!status_item_view_) {
95 status_item_view_.reset([[MCStatusItemView alloc] init]);
96 [status_item_view_ setCallback:^{ tray_->ToggleMessageCenterBubble(); }];
99 // We want a static message center icon while it's visible.
100 if (message_center()->IsMessageCenterVisible())
103 size_t unread_count = message_center_->UnreadNotificationCount();
104 bool quiet_mode = message_center_->IsQuietMode();
105 [status_item_view_ setUnreadCount:unread_count withQuietMode:quiet_mode];
107 if (unread_count > 0) {
108 base::string16 unread_count_string = base::FormatNumber(unread_count);
109 [status_item_view_ setToolTip:
110 l10n_util::GetNSStringF(IDS_MESSAGE_CENTER_TOOLTIP_UNREAD,
111 unread_count_string)];
113 [status_item_view_ setToolTip:
114 l10n_util::GetNSString(IDS_MESSAGE_CENTER_TOOLTIP)];
118 void MessageCenterTrayBridge::OpenTrayWindow() {
119 DCHECK(!tray_controller_);
120 tray_controller_.reset(
121 [[MCTrayController alloc] initWithMessageCenterTray:tray_.get()]);
122 [[tray_controller_ viewController] setTrayTitle:
123 l10n_util::GetNSStringF(IDS_MESSAGE_CENTER_FOOTER_WITH_PRODUCT_TITLE,
124 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))];
128 [status_item_view_ setHighlight:YES];
129 NSRect frame = [[status_item_view_ window] frame];
130 [tray_controller_ showTrayAtRightOf:NSMakePoint(NSMinX(frame),
132 atLeftOf:NSMakePoint(NSMaxX(frame),