Fix folder icon animation in app list.
[chromium-blink-merge.git] / ui / app_list / views / top_icon_animation_view.cc
blob2f65f6113e1d1d3d174fe6786db4d40b4982bdfd
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.
5 #include "ui/app_list/views/top_icon_animation_view.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "ui/app_list/app_list_constants.h"
9 #include "ui/compositor/scoped_layer_animation_settings.h"
10 #include "ui/gfx/image/image_skia_operations.h"
11 #include "ui/views/controls/image_view.h"
13 namespace app_list {
15 TopIconAnimationView::TopIconAnimationView(const gfx::ImageSkia& icon,
16 const gfx::Rect& scaled_rect,
17 bool open_folder)
18 : icon_size_(kGridIconDimension, kGridIconDimension),
19 icon_(new views::ImageView),
20 scaled_rect_(scaled_rect),
21 open_folder_(open_folder) {
22 DCHECK(!icon.isNull());
23 gfx::ImageSkia resized(gfx::ImageSkiaOperations::CreateResizedImage(
24 icon,
25 skia::ImageOperations::RESIZE_BEST, icon_size_));
26 icon_->SetImage(resized);
27 AddChildView(icon_);
29 SetPaintToLayer(true);
30 SetFillsBoundsOpaquely(false);
33 TopIconAnimationView::~TopIconAnimationView() {
34 // Required due to RequiresNotificationWhenAnimatorDestroyed() returning true.
35 // See ui::LayerAnimationObserver for details.
36 StopObservingImplicitAnimations();
39 void TopIconAnimationView::AddObserver(TopIconAnimationObserver* observer) {
40 observers_.AddObserver(observer);
43 void TopIconAnimationView::RemoveObserver(TopIconAnimationObserver* observer) {
44 observers_.RemoveObserver(observer);
47 void TopIconAnimationView::TransformView() {
48 // This view will delete itself on animation completion.
49 set_owned_by_client();
51 // Transform used for scaling down the icon and move it back inside to the
52 // original folder icon. The transform's origin is this view's origin.
53 gfx::Transform transform;
54 transform.Translate(scaled_rect_.x() - bounds().x(),
55 scaled_rect_.y() - bounds().y());
56 transform.Scale(
57 static_cast<double>(scaled_rect_.width()) / bounds().width(),
58 static_cast<double>(scaled_rect_.height()) / bounds().height());
60 if (open_folder_) {
61 // Transform to a scaled down icon inside the original folder icon.
62 layer()->SetTransform(transform);
65 // Animate the icon to its target location and scale when opening or
66 // closing a folder.
67 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
68 settings.AddObserver(this);
69 settings.SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
70 settings.SetTransitionDuration(
71 base::TimeDelta::FromMilliseconds(kFolderTransitionInDurationMs));
72 layer()->SetTransform(open_folder_ ? gfx::Transform() : transform);
75 gfx::Size TopIconAnimationView::GetPreferredSize() const {
76 return icon_size_;
79 void TopIconAnimationView::Layout() {
80 icon_->SetBoundsRect(GetContentsBounds());
83 void TopIconAnimationView::OnImplicitAnimationsCompleted() {
84 SetVisible(false);
85 FOR_EACH_OBSERVER(TopIconAnimationObserver,
86 observers_,
87 OnTopIconAnimationsComplete());
88 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
91 bool TopIconAnimationView::RequiresNotificationWhenAnimatorDestroyed() const {
92 return true;
95 } // namespace app_list