OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ash/system/web_notification/popup_bubble.h" | |
6 | |
7 #include "ash/system/tray/tray_bubble_view.h" | |
8 #include "ash/system/web_notification/web_notification_view.h" | |
9 #include "ui/views/layout/box_layout.h" | |
10 #include "ui/views/view.h" | |
11 #include "ui/views/widget/widget.h" | |
12 | |
13 namespace message_center { | |
14 | |
15 const int kAutocloseDelaySeconds = 5; | |
16 | |
17 // Popup notifications contents. | |
18 class PopupBubbleContentsView : public views::View { | |
19 public: | |
20 explicit PopupBubbleContentsView( | |
21 WebNotificationList::Delegate* list_delegate) | |
22 : list_delegate_(list_delegate) { | |
23 SetLayoutManager( | |
24 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); | |
25 | |
26 content_ = new views::View; | |
27 content_->SetLayoutManager( | |
28 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1)); | |
29 AddChildView(content_); | |
30 | |
31 content_->SetPaintToLayer(true); | |
32 content_->SetFillsBoundsOpaquely(false); | |
33 content_->layer()->SetMasksToBounds(true); | |
34 } | |
35 | |
36 void Update(const WebNotificationList::Notifications& popup_notifications) { | |
37 content_->RemoveAllChildViews(true); | |
38 for (WebNotificationList::Notifications::const_iterator iter = | |
39 popup_notifications.begin(); | |
40 iter != popup_notifications.end(); ++iter) { | |
41 WebNotificationView* view = new WebNotificationView( | |
42 list_delegate_, *iter, 0); | |
43 content_->AddChildView(view); | |
44 } | |
45 content_->SizeToPreferredSize(); | |
46 content_->InvalidateLayout(); | |
47 Layout(); | |
48 if (GetWidget()) | |
49 GetWidget()->GetRootView()->SchedulePaint(); | |
50 } | |
51 | |
52 size_t NumMessageViews() const { | |
53 return content_->child_count(); | |
54 } | |
55 | |
56 private: | |
57 WebNotificationList::Delegate* list_delegate_; | |
58 views::View* content_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(PopupBubbleContentsView); | |
61 }; | |
62 | |
63 // PopupBubble | |
64 PopupBubble::PopupBubble(WebNotificationList::Delegate* delegate) : | |
65 WebNotificationBubble(delegate), | |
66 contents_view_(NULL), | |
67 num_popups_(0) { | |
68 } | |
69 | |
70 PopupBubble::~PopupBubble() { | |
71 } | |
72 | |
73 TrayBubbleView::InitParams PopupBubble::GetInitParams( | |
74 TrayBubbleView::AnchorAlignment anchor_alignment) { | |
75 TrayBubbleView::InitParams init_params = | |
76 GetDefaultInitParams(anchor_alignment); | |
77 init_params.top_color = kBackgroundColor; | |
78 init_params.arrow_color = kBackgroundColor; | |
79 init_params.close_on_deactivate = false; | |
80 return init_params; | |
81 } | |
82 | |
83 void PopupBubble::InitializeContents(TrayBubbleView* bubble_view) { | |
84 bubble_view_ = bubble_view; | |
85 contents_view_ = new PopupBubbleContentsView(list_delegate_); | |
86 bubble_view_->AddChildView(contents_view_); | |
87 UpdateBubbleView(); | |
88 } | |
89 | |
90 void PopupBubble::OnBubbleViewDestroyed() { | |
91 contents_view_ = NULL; | |
92 } | |
93 | |
94 void PopupBubble::UpdateBubbleView() { | |
95 WebNotificationList::Notifications popup_notifications; | |
96 list_delegate_->GetNotificationList()->GetPopupNotifications( | |
97 &popup_notifications); | |
98 if (popup_notifications.size() == 0) { | |
99 if (bubble_view()) | |
100 bubble_view()->delegate()->HideBubble(bubble_view()); // deletes |this| | |
101 return; | |
102 } | |
103 // Only reset the timer when the number of visible notifications changes. | |
104 if (num_popups_ != popup_notifications.size()) { | |
105 num_popups_ = popup_notifications.size(); | |
106 StartAutoCloseTimer(); | |
107 } | |
108 contents_view_->Update(popup_notifications); | |
109 bubble_view_->Show(); | |
110 bubble_view_->UpdateBubble(); | |
111 } | |
112 | |
113 void PopupBubble::OnMouseEnteredView() { | |
114 StopAutoCloseTimer(); | |
115 } | |
116 | |
117 void PopupBubble::OnMouseExitedView() { | |
118 StartAutoCloseTimer(); | |
119 } | |
120 | |
121 void PopupBubble::StartAutoCloseTimer() { | |
122 autoclose_.Start(FROM_HERE, | |
123 base::TimeDelta::FromSeconds( | |
124 kAutocloseDelaySeconds), | |
125 this, &PopupBubble::OnAutoClose); | |
126 } | |
127 | |
128 void PopupBubble::StopAutoCloseTimer() { | |
129 autoclose_.Stop(); | |
130 } | |
131 | |
132 void PopupBubble::OnAutoClose() { | |
133 list_delegate_->GetNotificationList()->MarkPopupsAsShown(); | |
134 num_popups_ = 0; | |
135 UpdateBubbleView(); | |
136 } | |
137 | |
138 size_t PopupBubble::NumMessageViewsForTest() const { | |
139 return contents_view_->NumMessageViews(); | |
140 } | |
141 | |
142 } // namespace message_center | |
OLD | NEW |