OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/message_center/message_popup_bubble.h" | 5 #include "ui/message_center/message_popup_bubble.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "ui/message_center/message_view.h" | 9 #include "ui/message_center/message_view.h" |
10 #include "ui/message_center/notification.h" | 10 #include "ui/message_center/notification.h" |
| 11 #include "ui/message_center/notification_types.h" |
11 #include "ui/message_center/notification_view.h" | 12 #include "ui/message_center/notification_view.h" |
12 #include "ui/notifications/notification_types.h" | |
13 #include "ui/views/bubble/tray_bubble_view.h" | 13 #include "ui/views/bubble/tray_bubble_view.h" |
14 #include "ui/views/layout/box_layout.h" | 14 #include "ui/views/layout/box_layout.h" |
15 #include "ui/views/view.h" | 15 #include "ui/views/view.h" |
16 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
17 | 17 |
18 namespace message_center { | 18 namespace message_center { |
19 namespace { | 19 namespace { |
20 | 20 |
21 const int kAutocloseHighPriorityDelaySeconds = 25; | 21 const int kAutocloseHighPriorityDelaySeconds = 25; |
22 const int kAutocloseDefaultDelaySeconds = 8; | 22 const int kAutocloseDefaultDelaySeconds = 8; |
23 | 23 |
24 } // namespace | 24 } // namespace |
25 | 25 |
26 // Popup notifications contents. | 26 // Popup notifications contents. |
27 class PopupBubbleContentsView : public views::View { | 27 class PopupBubbleContentsView : public views::View { |
28 public: | 28 public: |
29 explicit PopupBubbleContentsView(NotificationList::Delegate* list_delegate); | 29 explicit PopupBubbleContentsView(NotificationList::Delegate* list_delegate); |
30 | 30 |
31 void Update(const NotificationList::Notifications& popup_notifications); | 31 void Update(const NotificationList::PopupNotifications& popup_notifications); |
32 | 32 |
33 size_t NumMessageViews() const { | 33 size_t NumMessageViews() const { |
34 return content_->child_count(); | 34 return content_->child_count(); |
35 } | 35 } |
36 | 36 |
37 private: | 37 private: |
38 NotificationList::Delegate* list_delegate_; | 38 NotificationList::Delegate* list_delegate_; |
39 views::View* content_; | 39 views::View* content_; |
40 | 40 |
41 DISALLOW_COPY_AND_ASSIGN(PopupBubbleContentsView); | 41 DISALLOW_COPY_AND_ASSIGN(PopupBubbleContentsView); |
(...skipping 10 matching lines...) Expand all Loading... |
52 AddChildView(content_); | 52 AddChildView(content_); |
53 | 53 |
54 if (get_use_acceleration_when_possible()) { | 54 if (get_use_acceleration_when_possible()) { |
55 content_->SetPaintToLayer(true); | 55 content_->SetPaintToLayer(true); |
56 content_->SetFillsBoundsOpaquely(false); | 56 content_->SetFillsBoundsOpaquely(false); |
57 content_->layer()->SetMasksToBounds(true); | 57 content_->layer()->SetMasksToBounds(true); |
58 } | 58 } |
59 } | 59 } |
60 | 60 |
61 void PopupBubbleContentsView::Update( | 61 void PopupBubbleContentsView::Update( |
62 const NotificationList::Notifications& popup_notifications) { | 62 const NotificationList::PopupNotifications& popup_notifications) { |
63 content_->RemoveAllChildViews(true); | 63 content_->RemoveAllChildViews(true); |
64 for (NotificationList::Notifications::const_iterator iter = | 64 for (NotificationList::PopupNotifications::const_iterator iter = |
65 popup_notifications.begin(); | 65 popup_notifications.begin(); |
66 iter != popup_notifications.end(); ++iter) { | 66 iter != popup_notifications.end(); ++iter) { |
67 MessageView* view = | 67 content_->AddChildView( |
68 NotificationView::ViewForNotification(*iter, list_delegate_); | 68 NotificationView::ViewForNotification(*(*iter), list_delegate_)); |
69 view->SetUpView(); | |
70 content_->AddChildView(view); | |
71 } | 69 } |
72 content_->SizeToPreferredSize(); | 70 content_->SizeToPreferredSize(); |
73 content_->InvalidateLayout(); | 71 content_->InvalidateLayout(); |
74 Layout(); | 72 Layout(); |
75 if (GetWidget()) | 73 if (GetWidget()) |
76 GetWidget()->GetRootView()->SchedulePaint(); | 74 GetWidget()->GetRootView()->SchedulePaint(); |
77 } | 75 } |
78 | 76 |
79 // The timer to call OnAutoClose for |notification|. | 77 // The timer to call OnAutoClose for |notification|. |
80 class MessagePopupBubble::AutocloseTimer { | 78 class MessagePopupBubble::AutocloseTimer { |
81 public: | 79 public: |
82 AutocloseTimer(const Notification& notification, MessagePopupBubble* bubble); | 80 AutocloseTimer(Notification* notification, MessagePopupBubble* bubble); |
83 | 81 |
84 void Start(); | 82 void Start(); |
85 | 83 |
86 void Suspend(); | 84 void Suspend(); |
87 | 85 |
88 private: | 86 private: |
89 const std::string id_; | 87 const std::string id_; |
90 base::TimeDelta delay_; | 88 base::TimeDelta delay_; |
91 base::Time start_time_; | 89 base::Time start_time_; |
92 MessagePopupBubble* bubble_; | 90 MessagePopupBubble* bubble_; |
93 base::OneShotTimer<MessagePopupBubble> timer_; | 91 base::OneShotTimer<MessagePopupBubble> timer_; |
94 | 92 |
95 DISALLOW_COPY_AND_ASSIGN(AutocloseTimer); | 93 DISALLOW_COPY_AND_ASSIGN(AutocloseTimer); |
96 }; | 94 }; |
97 | 95 |
98 MessagePopupBubble::AutocloseTimer::AutocloseTimer( | 96 MessagePopupBubble::AutocloseTimer::AutocloseTimer( |
99 const Notification& notification, | 97 Notification* notification, |
100 MessagePopupBubble* bubble) | 98 MessagePopupBubble* bubble) |
101 : id_(notification.id), | 99 : id_(notification->id()), |
102 bubble_(bubble) { | 100 bubble_(bubble) { |
103 int seconds = kAutocloseDefaultDelaySeconds; | 101 int seconds = kAutocloseDefaultDelaySeconds; |
104 if (notification.priority > ui::notifications::DEFAULT_PRIORITY) | 102 if (notification->priority() > DEFAULT_PRIORITY) |
105 seconds = kAutocloseHighPriorityDelaySeconds; | 103 seconds = kAutocloseHighPriorityDelaySeconds; |
106 delay_ = base::TimeDelta::FromSeconds(seconds); | 104 delay_ = base::TimeDelta::FromSeconds(seconds); |
107 } | 105 } |
108 | 106 |
109 void MessagePopupBubble::AutocloseTimer::Start() { | 107 void MessagePopupBubble::AutocloseTimer::Start() { |
110 start_time_ = base::Time::Now(); | 108 start_time_ = base::Time::Now(); |
111 timer_.Start(FROM_HERE, | 109 timer_.Start(FROM_HERE, |
112 delay_, | 110 delay_, |
113 base::Bind(&MessagePopupBubble::OnAutoClose, | 111 base::Bind(&MessagePopupBubble::OnAutoClose, |
114 base::Unretained(bubble_), id_)); | 112 base::Unretained(bubble_), id_)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 contents_view_ = new PopupBubbleContentsView(list_delegate()); | 144 contents_view_ = new PopupBubbleContentsView(list_delegate()); |
147 bubble_view()->AddChildView(contents_view_); | 145 bubble_view()->AddChildView(contents_view_); |
148 UpdateBubbleView(); | 146 UpdateBubbleView(); |
149 } | 147 } |
150 | 148 |
151 void MessagePopupBubble::OnBubbleViewDestroyed() { | 149 void MessagePopupBubble::OnBubbleViewDestroyed() { |
152 contents_view_ = NULL; | 150 contents_view_ = NULL; |
153 } | 151 } |
154 | 152 |
155 void MessagePopupBubble::UpdateBubbleView() { | 153 void MessagePopupBubble::UpdateBubbleView() { |
156 NotificationList::Notifications popups; | 154 NotificationList::PopupNotifications popups = |
157 list_delegate()->GetNotificationList()->GetPopupNotifications(&popups); | 155 list_delegate()->GetNotificationList()->GetPopupNotifications(); |
158 | 156 |
159 if (popups.size() == 0) { | 157 if (popups.size() == 0) { |
160 if (bubble_view()) | 158 if (bubble_view()) |
161 bubble_view()->delegate()->HideBubble(bubble_view()); // deletes |this| | 159 bubble_view()->delegate()->HideBubble(bubble_view()); // deletes |this| |
162 return; | 160 return; |
163 } | 161 } |
164 | 162 |
165 contents_view_->Update(popups); | 163 contents_view_->Update(popups); |
166 bubble_view()->Show(); | 164 bubble_view()->Show(); |
167 bubble_view()->UpdateBubble(); | 165 bubble_view()->UpdateBubble(); |
168 | 166 |
169 std::set<std::string> old_popup_ids; | 167 std::set<std::string> old_popup_ids; |
170 old_popup_ids.swap(popup_ids_); | 168 old_popup_ids.swap(popup_ids_); |
171 | 169 |
172 // Start autoclose timers. | 170 // Start autoclose timers. |
173 for (NotificationList::Notifications::const_iterator iter = popups.begin(); | 171 for (NotificationList::PopupNotifications::const_iterator iter = |
| 172 popups.begin(); |
174 iter != popups.end(); ++iter) { | 173 iter != popups.end(); ++iter) { |
| 174 std::string id = (*iter)->id(); |
175 std::map<std::string, AutocloseTimer*>::const_iterator timer_iter = | 175 std::map<std::string, AutocloseTimer*>::const_iterator timer_iter = |
176 autoclose_timers_.find(iter->id); | 176 autoclose_timers_.find(id); |
177 if (timer_iter == autoclose_timers_.end()) { | 177 if (timer_iter == autoclose_timers_.end()) { |
178 AutocloseTimer *timer = new AutocloseTimer(*iter, this); | 178 AutocloseTimer *timer = new AutocloseTimer(*iter, this); |
179 autoclose_timers_[iter->id] = timer; | 179 autoclose_timers_[id] = timer; |
180 timer->Start(); | 180 timer->Start(); |
181 } | 181 } |
182 popup_ids_.insert(iter->id); | 182 popup_ids_.insert(id); |
183 old_popup_ids.erase(iter->id); | 183 old_popup_ids.erase(id); |
184 } | 184 } |
185 | 185 |
186 // Stops timers whose notifications are gone. | 186 // Stops timers whose notifications are gone. |
187 for (std::set<std::string>::const_iterator iter = old_popup_ids.begin(); | 187 for (std::set<std::string>::const_iterator iter = old_popup_ids.begin(); |
188 iter != old_popup_ids.end(); ++iter) { | 188 iter != old_popup_ids.end(); ++iter) { |
189 DeleteTimer(*iter); | 189 DeleteTimer(*iter); |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 void MessagePopupBubble::OnMouseEnteredView() { | 193 void MessagePopupBubble::OnMouseEnteredView() { |
(...skipping 23 matching lines...) Expand all Loading... |
217 scoped_ptr<AutocloseTimer> release_timer(iter->second); | 217 scoped_ptr<AutocloseTimer> release_timer(iter->second); |
218 autoclose_timers_.erase(iter); | 218 autoclose_timers_.erase(iter); |
219 } | 219 } |
220 } | 220 } |
221 | 221 |
222 size_t MessagePopupBubble::NumMessageViewsForTest() const { | 222 size_t MessagePopupBubble::NumMessageViewsForTest() const { |
223 return contents_view_->NumMessageViews(); | 223 return contents_view_->NumMessageViews(); |
224 } | 224 } |
225 | 225 |
226 } // namespace message_center | 226 } // namespace message_center |
OLD | NEW |