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 "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"
14 TopIconAnimationView::TopIconAnimationView(const gfx::ImageSkia& icon,
15 const gfx::Rect& scaled_rect,
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(
24 skia::ImageOperations::RESIZE_BEST, icon_size_));
25 icon_->SetImage(resized);
28 SetPaintToLayer(true);
29 SetFillsBoundsOpaquely(false);
32 TopIconAnimationView::~TopIconAnimationView() {
35 void TopIconAnimationView::AddObserver(TopIconAnimationObserver* observer) {
36 observers_.AddObserver(observer);
39 void TopIconAnimationView::RemoveObserver(TopIconAnimationObserver* observer) {
40 observers_.RemoveObserver(observer);
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);
53 // Transform to a scaled down icon inside the original folder icon.
54 layer()->SetTransform(transform);
57 // Animate the icon to its target location and scale when opening or
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);
67 gfx::Size TopIconAnimationView::GetPreferredSize() {
71 void TopIconAnimationView::Layout() {
72 icon_->SetBoundsRect(GetContentsBounds());
75 void TopIconAnimationView::OnImplicitAnimationsCompleted() {
77 FOR_EACH_OBSERVER(TopIconAnimationObserver,
79 OnTopIconAnimationsComplete());
83 } // namespace app_list