Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / system / tray / tray_background_view.cc
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/tray/tray_background_view.h"
6
7 #include "ash/root_window_controller.h"
8 #include "ash/screen_util.h"
9 #include "ash/shelf/shelf_layout_manager.h"
10 #include "ash/shelf/shelf_widget.h"
11 #include "ash/shell.h"
12 #include "ash/shell_window_ids.h"
13 #include "ash/system/status_area_widget.h"
14 #include "ash/system/status_area_widget_delegate.h"
15 #include "ash/system/tray/system_tray.h"
16 #include "ash/system/tray/tray_constants.h"
17 #include "ash/system/tray/tray_event_filter.h"
18 #include "ash/wm/window_animations.h"
19 #include "base/command_line.h"
20 #include "grit/ash_resources.h"
21 #include "ui/accessibility/ax_view_state.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_event_dispatcher.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/base/ui_base_switches_util.h"
26 #include "ui/compositor/layer.h"
27 #include "ui/compositor/layer_animation_element.h"
28 #include "ui/compositor/scoped_layer_animation_settings.h"
29 #include "ui/events/event_constants.h"
30 #include "ui/gfx/animation/tween.h"
31 #include "ui/gfx/canvas.h"
32 #include "ui/gfx/image/image_skia.h"
33 #include "ui/gfx/image/image_skia_operations.h"
34 #include "ui/gfx/rect.h"
35 #include "ui/gfx/screen.h"
36 #include "ui/gfx/skia_util.h"
37 #include "ui/gfx/transform.h"
38 #include "ui/views/background.h"
39 #include "ui/views/layout/box_layout.h"
40
41 namespace {
42
43 const int kTrayBackgroundAlpha = 100;
44 const int kTrayBackgroundHoverAlpha = 150;
45 const SkColor kTrayBackgroundPressedColor = SkColorSetRGB(66, 129, 244);
46
47 const int kAnimationDurationForPopupMs = 200;
48
49 // Duration of opacity animation for visibility changes.
50 const int kAnimationDurationForVisibilityMs = 250;
51
52 // When becoming visible delay the animation so that StatusAreaWidgetDelegate
53 // can animate sibling views out of the position to be occuped by the
54 // TrayBackgroundView.
55 const int kShowAnimationDelayMs = 100;
56
57 }  // namespace
58
59 using views::TrayBubbleView;
60
61 namespace ash {
62
63 // static
64 const char TrayBackgroundView::kViewClassName[] = "tray/TrayBackgroundView";
65
66 // Used to track when the anchor widget changes position on screen so that the
67 // bubble position can be updated.
68 class TrayBackgroundView::TrayWidgetObserver : public views::WidgetObserver {
69  public:
70   explicit TrayWidgetObserver(TrayBackgroundView* host)
71       : host_(host) {
72   }
73
74   void OnWidgetBoundsChanged(views::Widget* widget,
75                              const gfx::Rect& new_bounds) override {
76     host_->AnchorUpdated();
77   }
78
79   void OnWidgetVisibilityChanged(views::Widget* widget, bool visible) override {
80     host_->AnchorUpdated();
81   }
82
83  private:
84   TrayBackgroundView* host_;
85
86   DISALLOW_COPY_AND_ASSIGN(TrayWidgetObserver);
87 };
88
89 class TrayBackground : public views::Background {
90  public:
91   const static int kImageTypeDefault = 0;
92   const static int kImageTypeOnBlack = 1;
93   const static int kImageTypePressed = 2;
94   const static int kNumStates = 3;
95
96   const static int kImageHorizontal = 0;
97   const static int kImageVertical = 1;
98   const static int kNumOrientations = 2;
99
100   explicit TrayBackground(TrayBackgroundView* tray_background_view) :
101       tray_background_view_(tray_background_view) {
102     set_alpha(kTrayBackgroundAlpha);
103     ResourceBundle& rb = ResourceBundle::GetSharedInstance();
104     leading_images_[kImageHorizontal][kImageTypeDefault] =
105         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_LEFT).ToImageSkia();
106     middle_images_[kImageHorizontal][kImageTypeDefault] =
107         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_CENTER).ToImageSkia();
108     trailing_images_[kImageHorizontal][kImageTypeDefault] =
109         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_RIGHT).ToImageSkia();
110
111     leading_images_[kImageHorizontal][kImageTypeOnBlack] =
112         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_LEFT_ONBLACK).ToImageSkia();
113     middle_images_[kImageHorizontal][kImageTypeOnBlack] =
114         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_CENTER_ONBLACK).ToImageSkia();
115     trailing_images_[kImageHorizontal][kImageTypeOnBlack] =
116         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_RIGHT_ONBLACK).ToImageSkia();
117
118     leading_images_[kImageHorizontal][kImageTypePressed] =
119         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_LEFT_PRESSED).ToImageSkia();
120     middle_images_[kImageHorizontal][kImageTypePressed] =
121         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_CENTER_PRESSED).ToImageSkia();
122     trailing_images_[kImageHorizontal][kImageTypePressed] =
123         rb.GetImageNamed(IDR_AURA_TRAY_BG_HORIZ_RIGHT_PRESSED).ToImageSkia();
124
125     leading_images_[kImageVertical][kImageTypeDefault] =
126         rb.GetImageNamed(IDR_AURA_TRAY_BG_VERTICAL_TOP).ToImageSkia();
127     middle_images_[kImageVertical][kImageTypeDefault] =
128         rb.GetImageNamed(
129             IDR_AURA_TRAY_BG_VERTICAL_CENTER).ToImageSkia();
130     trailing_images_[kImageVertical][kImageTypeDefault] =
131         rb.GetImageNamed(IDR_AURA_TRAY_BG_VERTICAL_BOTTOM).ToImageSkia();
132
133     leading_images_[kImageVertical][kImageTypeOnBlack] =
134         rb.GetImageNamed(IDR_AURA_TRAY_BG_VERTICAL_TOP_ONBLACK).ToImageSkia();
135     middle_images_[kImageVertical][kImageTypeOnBlack] =
136         rb.GetImageNamed(
137             IDR_AURA_TRAY_BG_VERTICAL_CENTER_ONBLACK).ToImageSkia();
138     trailing_images_[kImageVertical][kImageTypeOnBlack] =
139         rb.GetImageNamed(
140             IDR_AURA_TRAY_BG_VERTICAL_BOTTOM_ONBLACK).ToImageSkia();
141
142     leading_images_[kImageVertical][kImageTypePressed] =
143         rb.GetImageNamed(IDR_AURA_TRAY_BG_VERTICAL_TOP_PRESSED).ToImageSkia();
144     middle_images_[kImageVertical][kImageTypePressed] =
145         rb.GetImageNamed(
146             IDR_AURA_TRAY_BG_VERTICAL_CENTER_PRESSED).ToImageSkia();
147     trailing_images_[kImageVertical][kImageTypePressed] =
148         rb.GetImageNamed(
149             IDR_AURA_TRAY_BG_VERTICAL_BOTTOM_PRESSED).ToImageSkia();
150   }
151
152   ~TrayBackground() override {}
153
154   SkColor color() { return color_; }
155   void set_color(SkColor color) { color_ = color; }
156   void set_alpha(int alpha) { color_ = SkColorSetARGB(alpha, 0, 0, 0); }
157
158  private:
159   ShelfWidget* GetShelfWidget() const {
160     return RootWindowController::ForWindow(tray_background_view_->
161         status_area_widget()->GetNativeWindow())->shelf();
162   }
163
164   // Overridden from views::Background.
165   void Paint(gfx::Canvas* canvas, views::View* view) const override {
166     int orientation = kImageHorizontal;
167     ShelfWidget* shelf_widget = GetShelfWidget();
168     if (shelf_widget &&
169         !shelf_widget->shelf_layout_manager()->IsHorizontalAlignment())
170       orientation = kImageVertical;
171
172     int state = kImageTypeDefault;
173     if (tray_background_view_->draw_background_as_active())
174       state = kImageTypePressed;
175     else if (shelf_widget && shelf_widget->GetDimsShelf())
176       state = kImageTypeOnBlack;
177     else
178       state = kImageTypeDefault;
179
180     const gfx::ImageSkia* leading = leading_images_[orientation][state];
181     const gfx::ImageSkia* middle = middle_images_[orientation][state];
182     const gfx::ImageSkia* trailing = trailing_images_[orientation][state];
183
184     gfx::Rect bounds(view->GetLocalBounds());
185     gfx::Point leading_location, trailing_location;
186     gfx::Rect middle_bounds;
187
188     if (orientation == kImageHorizontal) {
189       leading_location = gfx::Point(0, 0);
190       trailing_location = gfx::Point(bounds.width() - trailing->width(), 0);
191       middle_bounds = gfx::Rect(
192           leading->width(),
193           0,
194           bounds.width() - (leading->width() + trailing->width()),
195           bounds.height());
196     } else {
197       leading_location = gfx::Point(0, 0);
198       trailing_location = gfx::Point(0, bounds.height() - trailing->height());
199       middle_bounds = gfx::Rect(
200           0,
201           leading->height(),
202           bounds.width(),
203           bounds.height() - (leading->height() + trailing->height()));
204     }
205
206     canvas->DrawImageInt(*leading,
207                          leading_location.x(),
208                          leading_location.y());
209
210     canvas->DrawImageInt(*trailing,
211                          trailing_location.x(),
212                          trailing_location.y());
213
214     canvas->TileImageInt(*middle,
215                          middle_bounds.x(),
216                          middle_bounds.y(),
217                          middle_bounds.width(),
218                          middle_bounds.height());
219   }
220
221   SkColor color_;
222   // Reference to the TrayBackgroundView for which this is a background.
223   TrayBackgroundView* tray_background_view_;
224
225   // References to the images used as backgrounds, they are owned by the
226   // resource bundle class.
227   const gfx::ImageSkia* leading_images_[kNumOrientations][kNumStates];
228   const gfx::ImageSkia* middle_images_[kNumOrientations][kNumStates];
229   const gfx::ImageSkia* trailing_images_[kNumOrientations][kNumStates];
230
231   DISALLOW_COPY_AND_ASSIGN(TrayBackground);
232 };
233
234 TrayBackgroundView::TrayContainer::TrayContainer(ShelfAlignment alignment)
235     : alignment_(alignment) {
236   UpdateLayout();
237 }
238
239 void TrayBackgroundView::TrayContainer::SetAlignment(ShelfAlignment alignment) {
240   if (alignment_ == alignment)
241     return;
242   alignment_ = alignment;
243   UpdateLayout();
244 }
245
246 gfx::Size TrayBackgroundView::TrayContainer::GetPreferredSize() const {
247   if (size_.IsEmpty())
248     return views::View::GetPreferredSize();
249   return size_;
250 }
251
252 void TrayBackgroundView::TrayContainer::ChildPreferredSizeChanged(
253     views::View* child) {
254   PreferredSizeChanged();
255 }
256
257 void TrayBackgroundView::TrayContainer::ChildVisibilityChanged(View* child) {
258   PreferredSizeChanged();
259 }
260
261 void TrayBackgroundView::TrayContainer::ViewHierarchyChanged(
262     const ViewHierarchyChangedDetails& details) {
263   if (details.parent == this)
264     PreferredSizeChanged();
265 }
266
267 void TrayBackgroundView::TrayContainer::UpdateLayout() {
268   // Adjust the size of status tray dark background by adding additional
269   // empty border.
270   if (alignment_ == SHELF_ALIGNMENT_BOTTOM ||
271       alignment_ == SHELF_ALIGNMENT_TOP) {
272     SetBorder(views::Border::CreateEmptyBorder(
273         kPaddingFromEdgeOfShelf,
274         kPaddingFromEdgeOfShelf,
275         kPaddingFromEdgeOfShelf,
276         kPaddingFromEdgeOfShelf));
277
278     views::BoxLayout* layout =
279         new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
280     layout->SetDefaultFlex(1);
281     views::View::SetLayoutManager(layout);
282   } else {
283     SetBorder(views::Border::CreateEmptyBorder(
284         kPaddingFromEdgeOfShelf,
285         kPaddingFromEdgeOfShelf,
286         kPaddingFromEdgeOfShelf,
287         kPaddingFromEdgeOfShelf));
288
289     views::BoxLayout* layout =
290         new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0);
291     layout->SetDefaultFlex(1);
292     views::View::SetLayoutManager(layout);
293   }
294   PreferredSizeChanged();
295 }
296
297 ////////////////////////////////////////////////////////////////////////////////
298 // TrayBackgroundView
299
300 TrayBackgroundView::TrayBackgroundView(StatusAreaWidget* status_area_widget)
301     : status_area_widget_(status_area_widget),
302       tray_container_(NULL),
303       shelf_alignment_(SHELF_ALIGNMENT_BOTTOM),
304       background_(NULL),
305       hide_background_animator_(this, 0, kTrayBackgroundAlpha),
306       hover_background_animator_(
307           this,
308           0,
309           kTrayBackgroundHoverAlpha - kTrayBackgroundAlpha),
310       hovered_(false),
311       draw_background_as_active_(false),
312       widget_observer_(new TrayWidgetObserver(this)) {
313   set_notify_enter_exit_on_child(true);
314
315   // Initially we want to paint the background, but without the hover effect.
316   hide_background_animator_.SetPaintsBackground(
317       true, BACKGROUND_CHANGE_IMMEDIATE);
318   hover_background_animator_.SetPaintsBackground(
319       false, BACKGROUND_CHANGE_IMMEDIATE);
320
321   tray_container_ = new TrayContainer(shelf_alignment_);
322   SetContents(tray_container_);
323   tray_event_filter_.reset(new TrayEventFilter);
324
325   SetPaintToLayer(true);
326   SetFillsBoundsOpaquely(false);
327   // Start the tray items not visible, because visibility changes are animated.
328   views::View::SetVisible(false);
329 }
330
331 TrayBackgroundView::~TrayBackgroundView() {
332   if (GetWidget())
333     GetWidget()->RemoveObserver(widget_observer_.get());
334 }
335
336 void TrayBackgroundView::Initialize() {
337   GetWidget()->AddObserver(widget_observer_.get());
338   SetTrayBorder();
339 }
340
341 void TrayBackgroundView::SetVisible(bool visible) {
342   if (visible == layer()->GetTargetVisibility())
343     return;
344
345   if (visible) {
346     // The alignment of the shelf can change while the TrayBackgroundView is
347     // hidden. Reset the offscreen transform so that the animation to becoming
348     // visible reflects the current layout.
349     HideTransformation();
350     // SetVisible(false) is defered until the animation for hiding is done.
351     // Otherwise the view is immediately hidden and the animation does not
352     // render.
353     views::View::SetVisible(true);
354     // If SetVisible(true) is called while animating to not visible, then
355     // views::View::SetVisible(true) is a no-op. When the previous animation
356     // ends layer->SetVisible(false) is called. To prevent this
357     // layer->SetVisible(true) immediately interrupts the animation of this
358     // property, and keeps the layer visible.
359     layer()->SetVisible(true);
360   }
361
362   ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
363   animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
364       kAnimationDurationForVisibilityMs));
365   animation.SetPreemptionStrategy(
366       ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
367
368   if (visible) {
369     animation.SetTweenType(gfx::Tween::EASE_OUT);
370     // Show is delayed so as to allow time for other children of
371     // StatusAreaWidget to begin animating to their new positions.
372     layer()->GetAnimator()->SchedulePauseForProperties(
373         base::TimeDelta::FromMilliseconds(kShowAnimationDelayMs),
374         ui::LayerAnimationElement::OPACITY |
375         ui::LayerAnimationElement::TRANSFORM);
376     layer()->SetOpacity(1.0f);
377     gfx::Transform transform;
378     transform.Translate(0.0f, 0.0f);
379     layer()->SetTransform(transform);
380   } else {
381     // Listen only to the hide animation. As we cannot turn off visibility
382     // until the animation is over.
383     animation.AddObserver(this);
384     animation.SetTweenType(gfx::Tween::EASE_IN);
385     layer()->SetOpacity(0.0f);
386     layer()->SetVisible(false);
387     HideTransformation();
388   }
389 }
390
391 const char* TrayBackgroundView::GetClassName() const {
392   return kViewClassName;
393 }
394
395 void TrayBackgroundView::OnMouseEntered(const ui::MouseEvent& event) {
396   hovered_ = true;
397 }
398
399 void TrayBackgroundView::OnMouseExited(const ui::MouseEvent& event) {
400   hovered_ = false;
401 }
402
403 void TrayBackgroundView::ChildPreferredSizeChanged(views::View* child) {
404   PreferredSizeChanged();
405 }
406
407 void TrayBackgroundView::GetAccessibleState(ui::AXViewState* state) {
408   state->role = ui::AX_ROLE_BUTTON;
409   state->name = GetAccessibleNameForTray();
410 }
411
412 void TrayBackgroundView::AboutToRequestFocusFromTabTraversal(bool reverse) {
413   // Return focus to the login view. See crbug.com/120500.
414   views::View* v = GetNextFocusableView();
415   if (v)
416     v->AboutToRequestFocusFromTabTraversal(reverse);
417 }
418
419 bool TrayBackgroundView::PerformAction(const ui::Event& event) {
420   return false;
421 }
422
423 gfx::Rect TrayBackgroundView::GetFocusBounds() {
424   // The tray itself expands to the right and bottom edge of the screen to make
425   // sure clicking on the edges brings up the popup. However, the focus border
426   // should be only around the container.
427   return GetContentsBounds();
428 }
429
430 void TrayBackgroundView::OnGestureEvent(ui::GestureEvent* event) {
431   if (switches::IsTouchFeedbackEnabled()) {
432     if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
433       SetDrawBackgroundAsActive(true);
434     } else if (event->type() ==  ui::ET_GESTURE_SCROLL_BEGIN ||
435                event->type() ==  ui::ET_GESTURE_TAP_CANCEL) {
436       SetDrawBackgroundAsActive(false);
437     }
438   }
439   ActionableView::OnGestureEvent(event);
440 }
441
442 void TrayBackgroundView::UpdateBackground(int alpha) {
443   // The animator should never fire when the alternate shelf layout is used.
444   if (!background_ || draw_background_as_active_)
445     return;
446   background_->set_alpha(hide_background_animator_.alpha() +
447                          hover_background_animator_.alpha());
448   SchedulePaint();
449 }
450
451 void TrayBackgroundView::SetContents(views::View* contents) {
452   SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
453   AddChildView(contents);
454 }
455
456 void TrayBackgroundView::SetPaintsBackground(
457     bool value, BackgroundAnimatorChangeType change_type) {
458   hide_background_animator_.SetPaintsBackground(value, change_type);
459 }
460
461 void TrayBackgroundView::SetContentsBackground() {
462   background_ = new TrayBackground(this);
463   tray_container_->set_background(background_);
464 }
465
466 ShelfLayoutManager* TrayBackgroundView::GetShelfLayoutManager() {
467   return ShelfLayoutManager::ForShelf(GetWidget()->GetNativeView());
468 }
469
470 void TrayBackgroundView::SetShelfAlignment(ShelfAlignment alignment) {
471   shelf_alignment_ = alignment;
472   SetTrayBorder();
473   tray_container_->SetAlignment(alignment);
474 }
475
476 void TrayBackgroundView::SetTrayBorder() {
477   views::View* parent = status_area_widget_->status_area_widget_delegate();
478   // Tray views are laid out right-to-left or bottom-to-top
479   bool on_edge = (this == parent->child_at(0));
480   int left_edge, top_edge, right_edge, bottom_edge;
481   if (shelf_alignment() == SHELF_ALIGNMENT_BOTTOM) {
482     top_edge = ShelfLayoutManager::kShelfItemInset;
483     left_edge = 0;
484     bottom_edge = kShelfSize -
485         ShelfLayoutManager::kShelfItemInset - kShelfItemHeight;
486     right_edge = on_edge ? kPaddingFromEdgeOfShelf : 0;
487   } else if (shelf_alignment() == SHELF_ALIGNMENT_LEFT) {
488     top_edge = 0;
489     left_edge = kShelfSize -
490         ShelfLayoutManager::kShelfItemInset - kShelfItemHeight;
491     bottom_edge = on_edge ? kPaddingFromEdgeOfShelf : 0;
492     right_edge = ShelfLayoutManager::kShelfItemInset;
493   } else { // SHELF_ALIGNMENT_RIGHT
494     top_edge = 0;
495     left_edge = ShelfLayoutManager::kShelfItemInset;
496     bottom_edge = on_edge ? kPaddingFromEdgeOfShelf : 0;
497     right_edge = kShelfSize -
498         ShelfLayoutManager::kShelfItemInset - kShelfItemHeight;
499   }
500   SetBorder(views::Border::CreateEmptyBorder(
501       top_edge, left_edge, bottom_edge, right_edge));
502 }
503
504 void TrayBackgroundView::OnImplicitAnimationsCompleted() {
505   // If there is another animation in the queue, the reverse animation was
506   // triggered before the completion of animating to invisible. Do not turn off
507   // the visibility so that the next animation may render. The value of
508   // layer()->GetTargetVisibility() can be incorrect if the hide animation was
509   // aborted to schedule an animation to become visible. As the new animation
510   // is not yet added to the queue. crbug.com/374236
511   if(layer()->GetAnimator()->is_animating() ||
512      layer()->GetTargetVisibility())
513     return;
514   views::View::SetVisible(false);
515 }
516
517 void TrayBackgroundView::HideTransformation() {
518   gfx::Transform transform;
519   if (shelf_alignment_ == SHELF_ALIGNMENT_BOTTOM ||
520       shelf_alignment_ == SHELF_ALIGNMENT_TOP)
521     transform.Translate(width(), 0.0f);
522   else
523     transform.Translate(0.0f, height());
524   layer()->SetTransform(transform);
525 }
526
527 void TrayBackgroundView::InitializeBubbleAnimations(
528     views::Widget* bubble_widget) {
529   wm::SetWindowVisibilityAnimationType(
530       bubble_widget->GetNativeWindow(),
531       wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
532   wm::SetWindowVisibilityAnimationTransition(
533       bubble_widget->GetNativeWindow(),
534       wm::ANIMATE_HIDE);
535   wm::SetWindowVisibilityAnimationDuration(
536       bubble_widget->GetNativeWindow(),
537       base::TimeDelta::FromMilliseconds(kAnimationDurationForPopupMs));
538 }
539
540 aura::Window* TrayBackgroundView::GetBubbleWindowContainer() const {
541   return ash::Shell::GetContainer(
542       tray_container()->GetWidget()->GetNativeWindow()->GetRootWindow(),
543       ash::kShellWindowId_SettingBubbleContainer);
544 }
545
546 gfx::Rect TrayBackgroundView::GetBubbleAnchorRect(
547     views::Widget* anchor_widget,
548     TrayBubbleView::AnchorType anchor_type,
549     TrayBubbleView::AnchorAlignment anchor_alignment) const {
550   gfx::Rect rect;
551   if (anchor_widget && anchor_widget->IsVisible()) {
552     rect = anchor_widget->GetWindowBoundsInScreen();
553     if (anchor_type == TrayBubbleView::ANCHOR_TYPE_TRAY) {
554       if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM) {
555         bool rtl = base::i18n::IsRTL();
556         rect.Inset(
557             rtl ? kBubblePaddingHorizontalSide : 0,
558             kBubblePaddingHorizontalBottom,
559             rtl ? 0 : kBubblePaddingHorizontalSide,
560             0);
561       } else if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_LEFT) {
562         rect.Inset(0, 0, kBubblePaddingVerticalSide + 4,
563                    kBubblePaddingVerticalBottom);
564       } else if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_RIGHT) {
565         rect.Inset(kBubblePaddingVerticalSide, 0, 0,
566                    kBubblePaddingVerticalBottom);
567       } else {
568         // TODO(bruthig) May need to handle other ANCHOR_ALIGNMENT_ values.
569         // ie. ANCHOR_ALIGNMENT_TOP
570         DCHECK(false) << "Unhandled anchor alignment.";
571       }
572     } else if (anchor_type == TrayBubbleView::ANCHOR_TYPE_BUBBLE) {
573       // Invert the offsets to align with the bubble below.
574       // Note that with the alternate shelf layout the tips are not shown and
575       // the offsets for left and right alignment do not need to be applied.
576       int vertical_alignment = 0;
577       int horizontal_alignment = kBubblePaddingVerticalBottom;
578       if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_LEFT)
579         rect.Inset(vertical_alignment, 0, 0, horizontal_alignment);
580       else if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_RIGHT)
581         rect.Inset(0, 0, vertical_alignment, horizontal_alignment);
582     } else {
583       DCHECK(false) << "Unhandled anchor type.";
584     }
585   } else {
586     aura::Window* target_root = anchor_widget ?
587         anchor_widget->GetNativeView()->GetRootWindow() :
588         Shell::GetPrimaryRootWindow();
589     rect = target_root->bounds();
590     if (anchor_type == TrayBubbleView::ANCHOR_TYPE_TRAY) {
591       if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM) {
592         rect = gfx::Rect(
593             base::i18n::IsRTL() ?
594             kPaddingFromRightEdgeOfScreenBottomAlignment :
595             rect.width() - kPaddingFromRightEdgeOfScreenBottomAlignment,
596             rect.height() - kPaddingFromBottomOfScreenBottomAlignment,
597             0, 0);
598         rect = ScreenUtil::ConvertRectToScreen(target_root, rect);
599       } else if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_LEFT) {
600         rect = gfx::Rect(
601             kPaddingFromRightEdgeOfScreenBottomAlignment,
602             rect.height() - kPaddingFromBottomOfScreenBottomAlignment,
603             1, 1);
604         rect = ScreenUtil::ConvertRectToScreen(target_root, rect);
605       } else if (anchor_alignment == TrayBubbleView::ANCHOR_ALIGNMENT_RIGHT) {
606         rect = gfx::Rect(
607             rect.width() - kPaddingFromRightEdgeOfScreenBottomAlignment,
608             rect.height() - kPaddingFromBottomOfScreenBottomAlignment,
609             1, 1);
610         rect = ScreenUtil::ConvertRectToScreen(target_root, rect);
611       } else {
612         // TODO(bruthig) May need to handle other ANCHOR_ALIGNMENT_ values.
613         // ie. ANCHOR_ALIGNMENT_TOP
614         DCHECK(false) << "Unhandled anchor alignment.";
615       }
616     } else {
617       rect = gfx::Rect(
618           base::i18n::IsRTL() ?
619           kPaddingFromRightEdgeOfScreenBottomAlignment :
620           rect.width() - kPaddingFromRightEdgeOfScreenBottomAlignment,
621           rect.height() - kPaddingFromBottomOfScreenBottomAlignment,
622           0, 0);
623     }
624   }
625   return rect;
626 }
627
628 TrayBubbleView::AnchorAlignment TrayBackgroundView::GetAnchorAlignment() const {
629   switch (shelf_alignment_) {
630     case SHELF_ALIGNMENT_BOTTOM:
631       return TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM;
632     case SHELF_ALIGNMENT_LEFT:
633       return TrayBubbleView::ANCHOR_ALIGNMENT_LEFT;
634     case SHELF_ALIGNMENT_RIGHT:
635       return TrayBubbleView::ANCHOR_ALIGNMENT_RIGHT;
636     case SHELF_ALIGNMENT_TOP:
637       return TrayBubbleView::ANCHOR_ALIGNMENT_TOP;
638   }
639   NOTREACHED();
640   return TrayBubbleView::ANCHOR_ALIGNMENT_BOTTOM;
641 }
642
643 void TrayBackgroundView::SetDrawBackgroundAsActive(bool visible) {
644   if (draw_background_as_active_ == visible)
645     return;
646   draw_background_as_active_ = visible;
647   if (!background_)
648     return;
649
650   // Do not change gradually, changing color between grey and blue is weird.
651   if (draw_background_as_active_)
652     background_->set_color(kTrayBackgroundPressedColor);
653   else if (hovered_)
654     background_->set_alpha(kTrayBackgroundHoverAlpha);
655   else
656     background_->set_alpha(kTrayBackgroundAlpha);
657   SchedulePaint();
658 }
659
660 void TrayBackgroundView::UpdateBubbleViewArrow(
661     views::TrayBubbleView* bubble_view) {
662   // Nothing to do here.
663 }
664
665 }  // namespace ash