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 "ui/message_center/message_center_tray.h"
7 #include "base/observer_list.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/base/l10n/l10n_util.h"
10 #include "ui/base/models/simple_menu_model.h"
11 #include "ui/message_center/message_center.h"
12 #include "ui/message_center/message_center_tray_delegate.h"
13 #include "ui/message_center/message_center_types.h"
14 #include "ui/message_center/notification_blocker.h"
15 #include "ui/strings/grit/ui_strings.h"
17 namespace message_center
{
22 const int kTogglePermissionCommand
= 0;
24 #if defined(OS_CHROMEOS)
25 const int kShowSettingsCommand
= 1;
28 // The model of the context menu for a notification card.
29 class NotificationMenuModel
: public ui::SimpleMenuModel
,
30 public ui::SimpleMenuModel::Delegate
{
32 NotificationMenuModel(MessageCenterTray
* tray
,
33 const NotifierId
& notifier_id
,
34 const base::string16
& display_source
);
35 ~NotificationMenuModel() override
;
37 // Overridden from ui::SimpleMenuModel::Delegate:
38 bool IsCommandIdChecked(int command_id
) const override
;
39 bool IsCommandIdEnabled(int command_id
) const override
;
40 bool GetAcceleratorForCommandId(int command_id
,
41 ui::Accelerator
* accelerator
) override
;
42 void ExecuteCommand(int command_id
, int event_flags
) override
;
45 MessageCenterTray
* tray_
;
46 NotifierId notifier_id_
;
47 DISALLOW_COPY_AND_ASSIGN(NotificationMenuModel
);
50 NotificationMenuModel::NotificationMenuModel(
51 MessageCenterTray
* tray
,
52 const NotifierId
& notifier_id
,
53 const base::string16
& display_source
)
54 : ui::SimpleMenuModel(this),
56 notifier_id_(notifier_id
) {
57 if (!display_source
.empty()) {
58 AddItem(kTogglePermissionCommand
,
59 l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_NOTIFIER_DISABLE
,
64 // Add settings menu item.
65 AddItem(kShowSettingsCommand
,
66 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS
));
70 NotificationMenuModel::~NotificationMenuModel() {
73 bool NotificationMenuModel::IsCommandIdChecked(int command_id
) const {
77 bool NotificationMenuModel::IsCommandIdEnabled(int command_id
) const {
78 return tray_
->delegate()->IsContextMenuEnabled();
81 bool NotificationMenuModel::GetAcceleratorForCommandId(
83 ui::Accelerator
* accelerator
) {
87 void NotificationMenuModel::ExecuteCommand(int command_id
, int event_flags
) {
89 case kTogglePermissionCommand
:
90 tray_
->message_center()->DisableNotificationsByNotifier(notifier_id_
);
93 case kShowSettingsCommand
:
94 tray_
->ShowNotifierSettingsBubble();
104 MessageCenterTray::MessageCenterTray(
105 MessageCenterTrayDelegate
* delegate
,
106 message_center::MessageCenter
* message_center
)
107 : message_center_(message_center
),
108 message_center_visible_(false),
109 popups_visible_(false),
110 delegate_(delegate
) {
111 message_center_
->AddObserver(this);
114 MessageCenterTray::~MessageCenterTray() {
115 message_center_
->RemoveObserver(this);
118 bool MessageCenterTray::ShowMessageCenterBubble() {
119 if (message_center_visible_
)
122 HidePopupBubbleInternal();
124 message_center_visible_
= delegate_
->ShowMessageCenter();
125 if (message_center_visible_
) {
126 message_center_
->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
127 NotifyMessageCenterTrayChanged();
129 return message_center_visible_
;
132 bool MessageCenterTray::HideMessageCenterBubble() {
133 if (!message_center_visible_
)
135 delegate_
->HideMessageCenter();
136 MarkMessageCenterHidden();
140 void MessageCenterTray::MarkMessageCenterHidden() {
141 if (!message_center_visible_
)
143 message_center_visible_
= false;
144 message_center_
->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
146 // Some notifications (like system ones) should appear as popups again
147 // after the message center is closed.
148 if (message_center_
->HasPopupNotifications()) {
153 NotifyMessageCenterTrayChanged();
156 void MessageCenterTray::ShowPopupBubble() {
157 if (message_center_visible_
)
160 if (popups_visible_
) {
161 NotifyMessageCenterTrayChanged();
165 if (!message_center_
->HasPopupNotifications())
168 popups_visible_
= delegate_
->ShowPopups();
170 NotifyMessageCenterTrayChanged();
173 bool MessageCenterTray::HidePopupBubble() {
174 if (!popups_visible_
)
176 HidePopupBubbleInternal();
177 NotifyMessageCenterTrayChanged();
182 void MessageCenterTray::HidePopupBubbleInternal() {
183 if (!popups_visible_
)
186 delegate_
->HidePopups();
187 popups_visible_
= false;
190 void MessageCenterTray::ShowNotifierSettingsBubble() {
192 HidePopupBubbleInternal();
194 message_center_visible_
= delegate_
->ShowNotifierSettings();
195 message_center_
->SetVisibility(message_center::VISIBILITY_SETTINGS
);
197 NotifyMessageCenterTrayChanged();
200 scoped_ptr
<ui::MenuModel
> MessageCenterTray::CreateNotificationMenuModel(
201 const NotifierId
& notifier_id
,
202 const base::string16
& display_source
) {
203 #if !defined(OS_CHROMEOS)
204 // Only web pages are configurable on non-chromeos platforms.
205 if (notifier_id
.type
!= NotifierId::WEB_PAGE
) {
210 return make_scoped_ptr(
211 new NotificationMenuModel(this, notifier_id
, display_source
));
214 void MessageCenterTray::OnNotificationAdded(
215 const std::string
& notification_id
) {
216 OnMessageCenterChanged();
219 void MessageCenterTray::OnNotificationRemoved(
220 const std::string
& notification_id
,
222 OnMessageCenterChanged();
225 void MessageCenterTray::OnNotificationUpdated(
226 const std::string
& notification_id
) {
227 OnMessageCenterChanged();
230 void MessageCenterTray::OnNotificationClicked(
231 const std::string
& notification_id
) {
233 OnMessageCenterChanged();
236 void MessageCenterTray::OnNotificationButtonClicked(
237 const std::string
& notification_id
,
240 OnMessageCenterChanged();
243 void MessageCenterTray::OnNotificationDisplayed(
244 const std::string
& notification_id
,
245 const DisplaySource source
) {
246 NotifyMessageCenterTrayChanged();
249 void MessageCenterTray::OnQuietModeChanged(bool in_quiet_mode
) {
250 NotifyMessageCenterTrayChanged();
253 void MessageCenterTray::OnBlockingStateChanged(NotificationBlocker
* blocker
) {
254 OnMessageCenterChanged();
257 void MessageCenterTray::OnMessageCenterChanged() {
258 if (message_center_visible_
&& message_center_
->NotificationCount() == 0)
259 HideMessageCenterBubble();
261 if (popups_visible_
&& !message_center_
->HasPopupNotifications())
262 HidePopupBubbleInternal();
263 else if (!popups_visible_
&& message_center_
->HasPopupNotifications())
266 NotifyMessageCenterTrayChanged();
269 void MessageCenterTray::NotifyMessageCenterTrayChanged() {
270 delegate_
->OnMessageCenterTrayChanged();
273 } // namespace message_center