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;
23 const int kShowSettingsCommand
= 1;
25 // The model of the context menu for a notification card.
26 class NotificationMenuModel
: public ui::SimpleMenuModel
,
27 public ui::SimpleMenuModel::Delegate
{
29 NotificationMenuModel(MessageCenterTray
* tray
,
30 const NotifierId
& notifier_id
,
31 const base::string16
& display_source
);
32 ~NotificationMenuModel() override
;
34 // Overridden from ui::SimpleMenuModel::Delegate:
35 bool IsCommandIdChecked(int command_id
) const override
;
36 bool IsCommandIdEnabled(int command_id
) const override
;
37 bool GetAcceleratorForCommandId(int command_id
,
38 ui::Accelerator
* accelerator
) override
;
39 void ExecuteCommand(int command_id
, int event_flags
) override
;
42 MessageCenterTray
* tray_
;
43 NotifierId notifier_id_
;
44 DISALLOW_COPY_AND_ASSIGN(NotificationMenuModel
);
47 NotificationMenuModel::NotificationMenuModel(
48 MessageCenterTray
* tray
,
49 const NotifierId
& notifier_id
,
50 const base::string16
& display_source
)
51 : ui::SimpleMenuModel(this),
53 notifier_id_(notifier_id
) {
54 // Add 'disable notifications' menu item.
55 if (!display_source
.empty()) {
56 AddItem(kTogglePermissionCommand
,
57 l10n_util::GetStringFUTF16(IDS_MESSAGE_CENTER_NOTIFIER_DISABLE
,
60 // Add settings menu item.
61 AddItem(kShowSettingsCommand
,
62 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_SETTINGS
));
65 NotificationMenuModel::~NotificationMenuModel() {
68 bool NotificationMenuModel::IsCommandIdChecked(int command_id
) const {
72 bool NotificationMenuModel::IsCommandIdEnabled(int command_id
) const {
73 return tray_
->delegate()->IsContextMenuEnabled();
76 bool NotificationMenuModel::GetAcceleratorForCommandId(
78 ui::Accelerator
* accelerator
) {
82 void NotificationMenuModel::ExecuteCommand(int command_id
, int event_flags
) {
84 case kTogglePermissionCommand
:
85 tray_
->message_center()->DisableNotificationsByNotifier(notifier_id_
);
87 case kShowSettingsCommand
:
88 tray_
->ShowNotifierSettingsBubble();
97 MessageCenterTray::MessageCenterTray(
98 MessageCenterTrayDelegate
* delegate
,
99 message_center::MessageCenter
* message_center
)
100 : message_center_(message_center
),
101 message_center_visible_(false),
102 popups_visible_(false),
103 delegate_(delegate
) {
104 message_center_
->AddObserver(this);
107 MessageCenterTray::~MessageCenterTray() {
108 message_center_
->RemoveObserver(this);
111 bool MessageCenterTray::ShowMessageCenterBubble() {
112 if (message_center_visible_
)
115 HidePopupBubbleInternal();
117 message_center_visible_
= delegate_
->ShowMessageCenter();
118 message_center_
->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER
);
119 NotifyMessageCenterTrayChanged();
120 return message_center_visible_
;
123 bool MessageCenterTray::HideMessageCenterBubble() {
124 if (!message_center_visible_
)
126 delegate_
->HideMessageCenter();
127 MarkMessageCenterHidden();
131 void MessageCenterTray::MarkMessageCenterHidden() {
132 if (!message_center_visible_
)
134 message_center_visible_
= false;
135 message_center_
->SetVisibility(message_center::VISIBILITY_TRANSIENT
);
137 // Some notifications (like system ones) should appear as popups again
138 // after the message center is closed.
139 if (message_center_
->HasPopupNotifications()) {
144 NotifyMessageCenterTrayChanged();
147 void MessageCenterTray::ToggleMessageCenterBubble() {
148 if (message_center_visible_
)
149 HideMessageCenterBubble();
151 ShowMessageCenterBubble();
154 void MessageCenterTray::ShowPopupBubble() {
155 if (message_center_visible_
)
158 if (popups_visible_
) {
159 NotifyMessageCenterTrayChanged();
163 if (!message_center_
->HasPopupNotifications())
166 popups_visible_
= delegate_
->ShowPopups();
168 NotifyMessageCenterTrayChanged();
171 bool MessageCenterTray::HidePopupBubble() {
172 if (!popups_visible_
)
174 HidePopupBubbleInternal();
175 NotifyMessageCenterTrayChanged();
180 void MessageCenterTray::HidePopupBubbleInternal() {
181 if (!popups_visible_
)
184 delegate_
->HidePopups();
185 popups_visible_
= false;
188 void MessageCenterTray::ShowNotifierSettingsBubble() {
190 HidePopupBubbleInternal();
192 message_center_visible_
= delegate_
->ShowNotifierSettings();
193 message_center_
->SetVisibility(message_center::VISIBILITY_SETTINGS
);
195 NotifyMessageCenterTrayChanged();
198 scoped_ptr
<ui::MenuModel
> MessageCenterTray::CreateNotificationMenuModel(
199 const NotifierId
& notifier_id
,
200 const base::string16
& display_source
) {
201 return scoped_ptr
<ui::MenuModel
>(new NotificationMenuModel(
202 this, notifier_id
, display_source
));
205 void MessageCenterTray::OnNotificationAdded(
206 const std::string
& notification_id
) {
207 OnMessageCenterChanged();
210 void MessageCenterTray::OnNotificationRemoved(
211 const std::string
& notification_id
,
213 OnMessageCenterChanged();
216 void MessageCenterTray::OnNotificationUpdated(
217 const std::string
& notification_id
) {
218 OnMessageCenterChanged();
221 void MessageCenterTray::OnNotificationClicked(
222 const std::string
& notification_id
) {
224 OnMessageCenterChanged();
227 void MessageCenterTray::OnNotificationButtonClicked(
228 const std::string
& notification_id
,
231 OnMessageCenterChanged();
234 void MessageCenterTray::OnNotificationDisplayed(
235 const std::string
& notification_id
,
236 const DisplaySource source
) {
237 NotifyMessageCenterTrayChanged();
240 void MessageCenterTray::OnQuietModeChanged(bool in_quiet_mode
) {
241 NotifyMessageCenterTrayChanged();
244 void MessageCenterTray::OnBlockingStateChanged(NotificationBlocker
* blocker
) {
245 OnMessageCenterChanged();
248 void MessageCenterTray::OnMessageCenterChanged() {
249 if (message_center_visible_
&& message_center_
->NotificationCount() == 0)
250 HideMessageCenterBubble();
252 if (popups_visible_
&& !message_center_
->HasPopupNotifications())
253 HidePopupBubbleInternal();
254 else if (!popups_visible_
&& message_center_
->HasPopupNotifications())
257 NotifyMessageCenterTrayChanged();
260 void MessageCenterTray::NotifyMessageCenterTrayChanged() {
261 delegate_
->OnMessageCenterTrayChanged();
264 } // namespace message_center