Get foreground tab on Android
[chromium-blink-merge.git] / ui / message_center / message_center_tray.cc
blobf5bf22e3cae4686cfb61cd6fa75170333ab8aec0
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 "grit/ui_strings.h"
10 #include "ui/base/l10n/l10n_util.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"
15 namespace message_center {
17 MessageCenterTray::MessageCenterTray(
18 MessageCenterTrayDelegate* delegate,
19 message_center::MessageCenter* message_center)
20 : message_center_(message_center),
21 message_center_visible_(false),
22 popups_visible_(false),
23 delegate_(delegate) {
24 message_center_->AddObserver(this);
27 MessageCenterTray::~MessageCenterTray() {
28 message_center_->RemoveObserver(this);
31 bool MessageCenterTray::ShowMessageCenterBubble() {
32 if (message_center_visible_)
33 return true;
35 HidePopupBubbleInternal();
37 message_center_visible_ = delegate_->ShowMessageCenter();
38 message_center_->SetVisibility(message_center::VISIBILITY_MESSAGE_CENTER);
39 NotifyMessageCenterTrayChanged();
40 return message_center_visible_;
43 bool MessageCenterTray::HideMessageCenterBubble() {
44 if (!message_center_visible_)
45 return false;
46 delegate_->HideMessageCenter();
47 MarkMessageCenterHidden();
48 return true;
51 void MessageCenterTray::MarkMessageCenterHidden() {
52 if (!message_center_visible_)
53 return;
54 message_center_visible_ = false;
56 // Some notifications (like system ones) should appear as popups again
57 // after the message center is closed.
58 if (message_center_->HasPopupNotifications()) {
59 ShowPopupBubble();
60 return;
63 message_center_->SetVisibility(message_center::VISIBILITY_TRANSIENT);
65 NotifyMessageCenterTrayChanged();
68 void MessageCenterTray::ToggleMessageCenterBubble() {
69 if (message_center_visible_)
70 HideMessageCenterBubble();
71 else
72 ShowMessageCenterBubble();
75 void MessageCenterTray::ShowPopupBubble() {
76 if (message_center_visible_)
77 return;
79 if (popups_visible_) {
80 NotifyMessageCenterTrayChanged();
81 return;
84 if (!message_center_->HasPopupNotifications())
85 return;
87 popups_visible_ = delegate_->ShowPopups();
89 NotifyMessageCenterTrayChanged();
92 bool MessageCenterTray::HidePopupBubble() {
93 if (!popups_visible_)
94 return false;
95 HidePopupBubbleInternal();
96 NotifyMessageCenterTrayChanged();
98 return true;
101 void MessageCenterTray::HidePopupBubbleInternal() {
102 if (!popups_visible_)
103 return;
105 delegate_->HidePopups();
106 popups_visible_ = false;
109 void MessageCenterTray::ShowNotifierSettingsBubble() {
110 if (popups_visible_)
111 HidePopupBubbleInternal();
113 message_center_visible_ = delegate_->ShowNotifierSettings();
114 message_center_->SetVisibility(message_center::VISIBILITY_SETTINGS);
116 NotifyMessageCenterTrayChanged();
119 void MessageCenterTray::OnNotificationAdded(
120 const std::string& notification_id) {
121 OnMessageCenterChanged();
124 void MessageCenterTray::OnNotificationRemoved(
125 const std::string& notification_id,
126 bool by_user) {
127 OnMessageCenterChanged();
130 void MessageCenterTray::OnNotificationUpdated(
131 const std::string& notification_id) {
132 OnMessageCenterChanged();
135 void MessageCenterTray::OnNotificationClicked(
136 const std::string& notification_id) {
137 if (popups_visible_)
138 OnMessageCenterChanged();
141 void MessageCenterTray::OnNotificationButtonClicked(
142 const std::string& notification_id,
143 int button_index) {
144 if (popups_visible_)
145 OnMessageCenterChanged();
148 void MessageCenterTray::OnNotificationDisplayed(
149 const std::string& notification_id) {
150 NotifyMessageCenterTrayChanged();
153 void MessageCenterTray::OnQuietModeChanged(bool in_quiet_mode) {
154 NotifyMessageCenterTrayChanged();
157 void MessageCenterTray::OnMessageCenterChanged() {
158 if (message_center_visible_ && message_center_->NotificationCount() == 0)
159 HideMessageCenterBubble();
161 if (popups_visible_ && !message_center_->HasPopupNotifications())
162 HidePopupBubbleInternal();
163 else if (!popups_visible_ && message_center_->HasPopupNotifications())
164 ShowPopupBubble();
166 NotifyMessageCenterTrayChanged();
169 void MessageCenterTray::NotifyMessageCenterTrayChanged() {
170 delegate_->OnMessageCenterTrayChanged();
173 } // namespace message_center