Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ash / system / tray / system_tray_bubble.cc
blob77d1056de1872d941b68319374146c449bda3d9b
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.
5 #include "ash/system/tray/system_tray_bubble.h"
7 #include "ash/shell.h"
8 #include "ash/system/tray/system_tray.h"
9 #include "ash/system/tray/system_tray_delegate.h"
10 #include "ash/system/tray/system_tray_item.h"
11 #include "ash/system/tray/tray_bubble_wrapper.h"
12 #include "ash/system/tray/tray_constants.h"
13 #include "base/message_loop.h"
14 #include "ui/aura/window.h"
15 #include "ui/compositor/layer.h"
16 #include "ui/compositor/layer_animation_observer.h"
17 #include "ui/compositor/scoped_layer_animation_settings.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/views/layout/box_layout.h"
20 #include "ui/views/view.h"
21 #include "ui/views/widget/widget.h"
23 using views::TrayBubbleView;
25 namespace ash {
27 namespace {
29 // Normally a detailed view is the same size as the default view. However,
30 // when showing a detailed view directly (e.g. clicking on a notification),
31 // we may not know the height of the default view, or the default view may
32 // be too short, so we use this as a default and minimum height for any
33 // detailed view.
34 const int kDetailedBubbleMaxHeight = kTrayPopupItemHeight * 5;
36 // Duration of swipe animation used when transitioning from a default to
37 // detailed view or vice versa.
38 const int kSwipeDelayMS = 150;
40 // A view with some special behaviour for tray items in the popup:
41 // - optionally changes background color on hover.
42 class TrayPopupItemContainer : public views::View {
43 public:
44 TrayPopupItemContainer(views::View* view,
45 bool change_background,
46 bool draw_border)
47 : hover_(false),
48 change_background_(change_background) {
49 set_notify_enter_exit_on_child(true);
50 if (draw_border) {
51 set_border(
52 views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kBorderLightColor));
54 views::BoxLayout* layout = new views::BoxLayout(
55 views::BoxLayout::kVertical, 0, 0, 0);
56 layout->set_spread_blank_space(true);
57 SetLayoutManager(layout);
58 SetPaintToLayer(view->layer() != NULL);
59 if (view->layer())
60 SetFillsBoundsOpaquely(view->layer()->fills_bounds_opaquely());
61 AddChildView(view);
62 SetVisible(view->visible());
65 virtual ~TrayPopupItemContainer() {}
67 private:
68 // Overridden from views::View.
69 virtual void ChildVisibilityChanged(View* child) OVERRIDE {
70 if (visible() == child->visible())
71 return;
72 SetVisible(child->visible());
73 PreferredSizeChanged();
76 virtual void ChildPreferredSizeChanged(View* child) OVERRIDE {
77 PreferredSizeChanged();
80 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE {
81 hover_ = true;
82 SchedulePaint();
85 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE {
86 hover_ = false;
87 SchedulePaint();
90 virtual void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE {
91 if (child_count() == 0)
92 return;
94 views::View* view = child_at(0);
95 if (!view->background()) {
96 canvas->FillRect(gfx::Rect(size()), (hover_ && change_background_) ?
97 kHoverBackgroundColor : kBackgroundColor);
101 bool hover_;
102 bool change_background_;
104 DISALLOW_COPY_AND_ASSIGN(TrayPopupItemContainer);
107 // Implicit animation observer that deletes itself and the layer at the end of
108 // the animation.
109 class AnimationObserverDeleteLayer : public ui::ImplicitAnimationObserver {
110 public:
111 explicit AnimationObserverDeleteLayer(ui::Layer* layer)
112 : layer_(layer) {
115 virtual ~AnimationObserverDeleteLayer() {
118 virtual void OnImplicitAnimationsCompleted() OVERRIDE {
119 MessageLoopForUI::current()->DeleteSoon(FROM_HERE, this);
122 private:
123 scoped_ptr<ui::Layer> layer_;
125 DISALLOW_COPY_AND_ASSIGN(AnimationObserverDeleteLayer);
128 } // namespace
130 namespace internal {
132 // SystemTrayBubble
134 SystemTrayBubble::SystemTrayBubble(
135 ash::SystemTray* tray,
136 const std::vector<ash::SystemTrayItem*>& items,
137 BubbleType bubble_type)
138 : tray_(tray),
139 bubble_view_(NULL),
140 items_(items),
141 bubble_type_(bubble_type),
142 autoclose_delay_(0) {
145 SystemTrayBubble::~SystemTrayBubble() {
146 DestroyItemViews();
147 // Reset the host pointer in bubble_view_ in case its destruction is deferred.
148 if (bubble_view_)
149 bubble_view_->reset_delegate();
152 void SystemTrayBubble::UpdateView(
153 const std::vector<ash::SystemTrayItem*>& items,
154 BubbleType bubble_type) {
155 DCHECK(bubble_type != BUBBLE_TYPE_NOTIFICATION);
157 scoped_ptr<ui::Layer> scoped_layer;
158 if (bubble_type != bubble_type_) {
159 base::TimeDelta swipe_duration =
160 base::TimeDelta::FromMilliseconds(kSwipeDelayMS);
161 scoped_layer.reset(bubble_view_->RecreateLayer());
162 // Keep the reference to layer as we need it after releasing it.
163 ui::Layer* layer = scoped_layer.get();
164 DCHECK(layer);
165 layer->SuppressPaint();
167 // When transitioning from detailed view to default view, animate the
168 // existing view (slide out towards the right).
169 if (bubble_type == BUBBLE_TYPE_DEFAULT) {
170 // Make sure the old view is visibile over the new view during the
171 // animation.
172 layer->parent()->StackAbove(layer, bubble_view_->layer());
173 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
174 settings.AddObserver(
175 new AnimationObserverDeleteLayer(scoped_layer.release()));
176 settings.SetTransitionDuration(swipe_duration);
177 settings.SetTweenType(ui::Tween::EASE_OUT);
178 gfx::Transform transform;
179 transform.Translate(layer->bounds().width(), 0.0);
180 layer->SetTransform(transform);
184 // Add a shadow layer to make the old layer darker as the animation
185 // progresses.
186 ui::Layer* shadow = new ui::Layer(ui::LAYER_SOLID_COLOR);
187 shadow->SetColor(SK_ColorBLACK);
188 shadow->SetOpacity(0.01f);
189 shadow->SetBounds(layer->bounds());
190 layer->Add(shadow);
191 layer->StackAtTop(shadow);
193 // Animate the darkening effect a little longer than the swipe-in. This
194 // is to make sure the darkening animation does not end up finishing
195 // early, because the dark layer goes away at the end of the animation,
196 // and there is a brief moment when the old view is still visible, but
197 // it does not have the shadow layer on top.
198 ui::ScopedLayerAnimationSettings settings(shadow->GetAnimator());
199 settings.AddObserver(new AnimationObserverDeleteLayer(shadow));
200 settings.SetTransitionDuration(swipe_duration +
201 base::TimeDelta::FromMilliseconds(150));
202 settings.SetTweenType(ui::Tween::LINEAR);
203 shadow->SetOpacity(0.15f);
208 DestroyItemViews();
209 bubble_view_->RemoveAllChildViews(true);
211 items_ = items;
212 bubble_type_ = bubble_type;
213 CreateItemViews(
214 Shell::GetInstance()->system_tray_delegate()->GetUserLoginStatus());
216 // Close bubble view if we failed to create the item view.
217 if (!bubble_view_->has_children()) {
218 Close();
219 return;
222 bubble_view_->GetWidget()->GetContentsView()->Layout();
223 // Make sure that the bubble is large enough for the default view.
224 if (bubble_type_ == BUBBLE_TYPE_DEFAULT) {
225 bubble_view_->SetMaxHeight(0); // Clear max height limit.
228 if (scoped_layer.get()) {
229 // When transitioning from default view to detailed view, animate the new
230 // view (slide in from the right).
231 if (bubble_type == BUBBLE_TYPE_DETAILED) {
232 ui::Layer* new_layer = bubble_view_->layer();
233 gfx::Rect bounds = new_layer->bounds();
234 gfx::Transform transform;
235 transform.Translate(bounds.width(), 0.0);
236 new_layer->SetTransform(transform);
238 ui::ScopedLayerAnimationSettings settings(new_layer->GetAnimator());
239 settings.AddObserver(
240 new AnimationObserverDeleteLayer(scoped_layer.release()));
241 settings.SetTransitionDuration(
242 base::TimeDelta::FromMilliseconds(kSwipeDelayMS));
243 settings.SetTweenType(ui::Tween::EASE_OUT);
244 new_layer->SetTransform(gfx::Transform());
250 void SystemTrayBubble::InitView(views::View* anchor,
251 user::LoginStatus login_status,
252 TrayBubbleView::InitParams* init_params) {
253 DCHECK(bubble_view_ == NULL);
255 if (bubble_type_ == BUBBLE_TYPE_DETAILED &&
256 init_params->max_height < kDetailedBubbleMaxHeight) {
257 init_params->max_height = kDetailedBubbleMaxHeight;
258 } else if (bubble_type_ == BUBBLE_TYPE_NOTIFICATION) {
259 init_params->close_on_deactivate = false;
261 bubble_view_ = TrayBubbleView::Create(
262 tray_->GetBubbleWindowContainer(), anchor, tray_, init_params);
263 bubble_view_->set_adjust_if_offscreen(false);
264 CreateItemViews(login_status);
266 if (bubble_view_->CanActivate()) {
267 bubble_view_->NotifyAccessibilityEvent(
268 ui::AccessibilityTypes::EVENT_ALERT, true);
272 void SystemTrayBubble::DestroyItemViews() {
273 for (std::vector<ash::SystemTrayItem*>::iterator it = items_.begin();
274 it != items_.end();
275 ++it) {
276 switch (bubble_type_) {
277 case BUBBLE_TYPE_DEFAULT:
278 (*it)->DestroyDefaultView();
279 break;
280 case BUBBLE_TYPE_DETAILED:
281 (*it)->DestroyDetailedView();
282 break;
283 case BUBBLE_TYPE_NOTIFICATION:
284 (*it)->DestroyNotificationView();
285 break;
290 void SystemTrayBubble::BubbleViewDestroyed() {
291 bubble_view_ = NULL;
294 void SystemTrayBubble::StartAutoCloseTimer(int seconds) {
295 autoclose_.Stop();
296 autoclose_delay_ = seconds;
297 if (autoclose_delay_) {
298 autoclose_.Start(FROM_HERE,
299 base::TimeDelta::FromSeconds(autoclose_delay_),
300 this, &SystemTrayBubble::Close);
304 void SystemTrayBubble::StopAutoCloseTimer() {
305 autoclose_.Stop();
308 void SystemTrayBubble::RestartAutoCloseTimer() {
309 if (autoclose_delay_)
310 StartAutoCloseTimer(autoclose_delay_);
313 void SystemTrayBubble::Close() {
314 tray_->HideBubbleWithView(bubble_view());
317 void SystemTrayBubble::SetVisible(bool is_visible) {
318 if (!bubble_view_)
319 return;
320 views::Widget* bubble_widget = bubble_view_->GetWidget();
321 if (is_visible)
322 bubble_widget->Show();
323 else
324 bubble_widget->Hide();
327 bool SystemTrayBubble::IsVisible() {
328 return bubble_view() && bubble_view()->GetWidget()->IsVisible();
331 bool SystemTrayBubble::ShouldShowLauncher() const {
332 for (std::vector<ash::SystemTrayItem*>::const_iterator it = items_.begin();
333 it != items_.end();
334 ++it) {
335 if ((*it)->ShouldShowLauncher())
336 return true;
338 return false;
341 void SystemTrayBubble::CreateItemViews(user::LoginStatus login_status) {
342 std::vector<views::View*> item_views;
343 for (size_t i = 0; i < items_.size(); ++i) {
344 views::View* view = NULL;
345 switch (bubble_type_) {
346 case BUBBLE_TYPE_DEFAULT:
347 view = items_[i]->CreateDefaultView(login_status);
348 break;
349 case BUBBLE_TYPE_DETAILED:
350 view = items_[i]->CreateDetailedView(login_status);
351 break;
352 case BUBBLE_TYPE_NOTIFICATION:
353 view = items_[i]->CreateNotificationView(login_status);
354 break;
356 if (view)
357 item_views.push_back(view);
360 bool is_default_bubble = bubble_type_ == BUBBLE_TYPE_DEFAULT;
361 for (size_t i = 0; i < item_views.size(); ++i) {
362 // For default view, draw bottom border for each item, except the last
363 // 2 items, which are the bottom header row and the one just above it.
364 bubble_view_->AddChildView(new TrayPopupItemContainer(
365 item_views[i], is_default_bubble,
366 is_default_bubble && (i < item_views.size() - 2)));
370 } // namespace internal
371 } // namespace ash