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() {
33 // Required due to RequiresNotificationWhenAnimatorDestroyed() returning true.
34 // See ui::LayerAnimationObserver for details.
35 StopObservingImplicitAnimations();
38 void TopIconAnimationView::AddObserver(TopIconAnimationObserver
* observer
) {
39 observers_
.AddObserver(observer
);
42 void TopIconAnimationView::RemoveObserver(TopIconAnimationObserver
* observer
) {
43 observers_
.RemoveObserver(observer
);
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
);
56 // Transform to a scaled down icon inside the original folder icon.
57 layer()->SetTransform(transform
);
60 // Animate the icon to its target location and scale when opening or
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
);
70 gfx::Size
TopIconAnimationView::GetPreferredSize() const {
74 void TopIconAnimationView::Layout() {
75 icon_
->SetBoundsRect(GetContentsBounds());
78 void TopIconAnimationView::OnImplicitAnimationsCompleted() {
80 FOR_EACH_OBSERVER(TopIconAnimationObserver
,
82 OnTopIconAnimationsComplete());
86 bool TopIconAnimationView::RequiresNotificationWhenAnimatorDestroyed() const {
90 } // namespace app_list