1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/message_center/message_popup_bubble.h"
#include "base/bind.h"
#include "base/stl_util.h"
#include "ui/message_center/message_view.h"
#include "ui/message_center/notification_view.h"
#include "ui/notifications/notification_types.h"
#include "ui/views/bubble/tray_bubble_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace message_center {
namespace {
const int kAutocloseHighPriorityDelaySeconds = 25;
const int kAutocloseDefaultDelaySeconds = 8;
} // namespace
// Popup notifications contents.
class PopupBubbleContentsView : public views::View {
public:
explicit PopupBubbleContentsView(NotificationList::Delegate* list_delegate);
void Update(const NotificationList::Notifications& popup_notifications);
size_t NumMessageViews() const {
return content_->child_count();
}
private:
NotificationList::Delegate* list_delegate_;
views::View* content_;
DISALLOW_COPY_AND_ASSIGN(PopupBubbleContentsView);
};
PopupBubbleContentsView::PopupBubbleContentsView(
NotificationList::Delegate* list_delegate)
: list_delegate_(list_delegate) {
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1));
content_ = new views::View;
content_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1));
AddChildView(content_);
if (get_use_acceleration_when_possible()) {
content_->SetPaintToLayer(true);
content_->SetFillsBoundsOpaquely(false);
content_->layer()->SetMasksToBounds(true);
}
}
void PopupBubbleContentsView::Update(
const NotificationList::Notifications& popup_notifications) {
content_->RemoveAllChildViews(true);
for (NotificationList::Notifications::const_iterator iter =
popup_notifications.begin();
iter != popup_notifications.end(); ++iter) {
MessageView* view =
NotificationView::ViewForNotification(*iter, list_delegate_);
view->SetUpView();
content_->AddChildView(view);
}
content_->SizeToPreferredSize();
content_->InvalidateLayout();
Layout();
if (GetWidget())
GetWidget()->GetRootView()->SchedulePaint();
}
// The timer to call OnAutoClose for |notification|.
class MessagePopupBubble::AutocloseTimer {
public:
AutocloseTimer(const NotificationList::Notification& notification,
MessagePopupBubble* bubble);
void Start();
void Suspend();
private:
const std::string id_;
base::TimeDelta delay_;
base::Time start_time_;
MessagePopupBubble* bubble_;
base::OneShotTimer<MessagePopupBubble> timer_;
DISALLOW_COPY_AND_ASSIGN(AutocloseTimer);
};
MessagePopupBubble::AutocloseTimer::AutocloseTimer(
const NotificationList::Notification& notification,
MessagePopupBubble* bubble)
: id_(notification.id),
bubble_(bubble) {
int seconds = kAutocloseDefaultDelaySeconds;
if (notification.priority > ui::notifications::DEFAULT_PRIORITY)
seconds = kAutocloseHighPriorityDelaySeconds;
delay_ = base::TimeDelta::FromSeconds(seconds);
}
void MessagePopupBubble::AutocloseTimer::Start() {
start_time_ = base::Time::Now();
timer_.Start(FROM_HERE,
delay_,
base::Bind(&MessagePopupBubble::OnAutoClose,
base::Unretained(bubble_), id_));
}
void MessagePopupBubble::AutocloseTimer::Suspend() {
base::TimeDelta passed = base::Time::Now() - start_time_;
delay_ = std::max(base::TimeDelta(), delay_ - passed);
timer_.Reset();
}
// MessagePopupBubble
MessagePopupBubble::MessagePopupBubble(NotificationList::Delegate* delegate)
: MessageBubbleBase(delegate),
contents_view_(NULL) {
}
MessagePopupBubble::~MessagePopupBubble() {
STLDeleteContainerPairSecondPointers(autoclose_timers_.begin(),
autoclose_timers_.end());
}
views::TrayBubbleView::InitParams MessagePopupBubble::GetInitParams(
views::TrayBubbleView::AnchorAlignment anchor_alignment) {
views::TrayBubbleView::InitParams init_params =
GetDefaultInitParams(anchor_alignment);
init_params.arrow_color = kBackgroundColor;
init_params.close_on_deactivate = false;
return init_params;
}
void MessagePopupBubble::InitializeContents(
views::TrayBubbleView* new_bubble_view) {
set_bubble_view(new_bubble_view);
contents_view_ = new PopupBubbleContentsView(list_delegate());
bubble_view()->AddChildView(contents_view_);
UpdateBubbleView();
}
void MessagePopupBubble::OnBubbleViewDestroyed() {
contents_view_ = NULL;
}
void MessagePopupBubble::UpdateBubbleView() {
NotificationList::Notifications popups;
list_delegate()->GetNotificationList()->GetPopupNotifications(&popups);
if (popups.size() == 0) {
if (bubble_view())
bubble_view()->delegate()->HideBubble(bubble_view()); // deletes |this|
return;
}
contents_view_->Update(popups);
bubble_view()->Show();
bubble_view()->UpdateBubble();
std::set<std::string> old_popup_ids;
old_popup_ids.swap(popup_ids_);
// Start autoclose timers.
for (NotificationList::Notifications::const_iterator iter = popups.begin();
iter != popups.end(); ++iter) {
std::map<std::string, AutocloseTimer*>::const_iterator timer_iter =
autoclose_timers_.find(iter->id);
if (timer_iter == autoclose_timers_.end()) {
AutocloseTimer *timer = new AutocloseTimer(*iter, this);
autoclose_timers_[iter->id] = timer;
timer->Start();
}
popup_ids_.insert(iter->id);
old_popup_ids.erase(iter->id);
}
// Stops timers whose notifications are gone.
for (std::set<std::string>::const_iterator iter = old_popup_ids.begin();
iter != old_popup_ids.end(); ++iter) {
DeleteTimer(*iter);
}
}
void MessagePopupBubble::OnMouseEnteredView() {
for (std::map<std::string, AutocloseTimer*>::iterator iter =
autoclose_timers_.begin(); iter != autoclose_timers_.end(); ++iter) {
iter->second->Suspend();
}
}
void MessagePopupBubble::OnMouseExitedView() {
for (std::map<std::string, AutocloseTimer*>::iterator iter =
autoclose_timers_.begin(); iter != autoclose_timers_.end(); ++iter) {
iter->second->Start();
}
}
void MessagePopupBubble::OnAutoClose(const std::string& id) {
DeleteTimer(id);
list_delegate()->GetNotificationList()->MarkSinglePopupAsShown(id, false);
UpdateBubbleView();
}
void MessagePopupBubble::DeleteTimer(const std::string& id) {
std::map<std::string, AutocloseTimer*>::iterator iter =
autoclose_timers_.find(id);
if (iter != autoclose_timers_.end()) {
scoped_ptr<AutocloseTimer> release_timer(iter->second);
autoclose_timers_.erase(iter);
}
}
size_t MessagePopupBubble::NumMessageViewsForTest() const {
return contents_view_->NumMessageViews();
}
} // namespace message_center
|