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