Upstream version 5.34.104.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 }
34
35 void TopIconAnimationView::AddObserver(TopIconAnimationObserver* observer) {
36   observers_.AddObserver(observer);
37 }
38
39 void TopIconAnimationView::RemoveObserver(TopIconAnimationObserver* observer) {
40   observers_.RemoveObserver(observer);
41 }
42
43 void TopIconAnimationView::TransformView() {
44   // Transform used for scaling down the icon and move it back inside to the
45   // original folder icon.
46   const float kIconTransformScale = 0.33333f;
47   gfx::Transform transform;
48   transform.Translate(scaled_rect_.x() - layer()->bounds().x(),
49                       scaled_rect_.y() - layer()->bounds().y());
50   transform.Scale(kIconTransformScale, kIconTransformScale);
51
52   if (open_folder_) {
53     // Transform to a scaled down icon inside the original folder icon.
54     layer()->SetTransform(transform);
55   }
56
57   // Animate the icon to its target location and scale when opening or
58   // closing a folder.
59   ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
60   settings.AddObserver(this);
61   settings.SetTweenType(gfx::Tween::EASE_IN_OUT_2);
62   settings.SetTransitionDuration(
63       base::TimeDelta::FromMilliseconds(kFolderTransitionInDurationMs));
64   layer()->SetTransform(open_folder_ ? gfx::Transform() : transform);
65 }
66
67 gfx::Size TopIconAnimationView::GetPreferredSize() {
68   return icon_size_;
69 }
70
71 void TopIconAnimationView::Layout() {
72   icon_->SetBoundsRect(GetContentsBounds());
73 }
74
75 void TopIconAnimationView::OnImplicitAnimationsCompleted() {
76   SetVisible(false);
77   FOR_EACH_OBSERVER(TopIconAnimationObserver,
78                     observers_,
79                     OnTopIconAnimationsComplete());
80   delete this;
81 }
82
83 }  // namespace app_list