Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / top_icon_animation_view.cc
1 // Copyright 2014 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 "ui/app_list/views/top_icon_animation_view.h"
6
7 #include "ui/app_list/app_list_constants.h"
8 #include "ui/compositor/scoped_layer_animation_settings.h"
9 #include "ui/gfx/image/image_skia_operations.h"
10 #include "ui/views/controls/image_view.h"
11
12 namespace app_list {
13
14 TopIconAnimationView::TopIconAnimationView(const gfx::ImageSkia& icon,
15                                            const gfx::Rect& scaled_rect,
16                                            bool open_folder)
17     : icon_size_(kPreferredIconDimension, kPreferredIconDimension),
18       icon_(new views::ImageView),
19       scaled_rect_(scaled_rect),
20       open_folder_(open_folder) {
21   DCHECK(!icon.isNull());
22   gfx::ImageSkia resized(gfx::ImageSkiaOperations::CreateResizedImage(
23       icon,
24       skia::ImageOperations::RESIZE_BEST, icon_size_));
25   icon_->SetImage(resized);
26   AddChildView(icon_);
27
28   SetPaintToLayer(true);
29   SetFillsBoundsOpaquely(false);
30 }
31
32 TopIconAnimationView::~TopIconAnimationView() {
33   // Required due to RequiresNotificationWhenAnimatorDestroyed() returning true.
34   // See ui::LayerAnimationObserver for details.
35   StopObservingImplicitAnimations();
36 }
37
38 void TopIconAnimationView::AddObserver(TopIconAnimationObserver* observer) {
39   observers_.AddObserver(observer);
40 }
41
42 void TopIconAnimationView::RemoveObserver(TopIconAnimationObserver* observer) {
43   observers_.RemoveObserver(observer);
44 }
45
46 void TopIconAnimationView::TransformView() {
47   // Transform used for scaling down the icon and move it back inside to the
48   // original folder icon.
49   const float kIconTransformScale = 0.33333f;
50   gfx::Transform transform;
51   transform.Translate(scaled_rect_.x() - layer()->bounds().x(),
52                       scaled_rect_.y() - layer()->bounds().y());
53   transform.Scale(kIconTransformScale, kIconTransformScale);
54
55   if (open_folder_) {
56     // Transform to a scaled down icon inside the original folder icon.
57     layer()->SetTransform(transform);
58   }
59
60   // Animate the icon to its target location and scale when opening or
61   // closing a folder.
62   ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
63   settings.AddObserver(this);
64   settings.SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
65   settings.SetTransitionDuration(
66       base::TimeDelta::FromMilliseconds(kFolderTransitionInDurationMs));
67   layer()->SetTransform(open_folder_ ? gfx::Transform() : transform);
68 }
69
70 gfx::Size TopIconAnimationView::GetPreferredSize() {
71   return icon_size_;
72 }
73
74 void TopIconAnimationView::Layout() {
75   icon_->SetBoundsRect(GetContentsBounds());
76 }
77
78 void TopIconAnimationView::OnImplicitAnimationsCompleted() {
79   SetVisible(false);
80   FOR_EACH_OBSERVER(TopIconAnimationObserver,
81                     observers_,
82                     OnTopIconAnimationsComplete());
83   delete this;
84 }
85
86 bool TopIconAnimationView::RequiresNotificationWhenAnimatorDestroyed() const {
87   return true;
88 }
89
90 }  // namespace app_list