1 // Copyright (c) 2012 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 "chrome/browser/download/download_started_animation.h"
7 #include "content/public/browser/web_contents.h"
8 #include "grit/theme_resources.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/animation/linear_animation.h"
11 #include "ui/gfx/rect.h"
12 #include "ui/views/controls/image_view.h"
13 #include "ui/views/widget/widget.h"
15 // How long to spend moving downwards and fading out after waiting.
16 const int kMoveTimeMs
= 600;
18 // The animation framerate.
19 const int kFrameRateHz
= 60;
23 // DownloadStartAnimation creates an animation (which begins running
24 // immediately) that animates an image downward from the center of the frame
25 // provided on the constructor, while simultaneously fading it out. To use,
26 // simply call "new DownloadStartAnimation"; the class cleans itself up when it
27 // finishes animating.
28 class DownloadStartedAnimationViews
: public gfx::LinearAnimation
,
29 public views::ImageView
{
31 explicit DownloadStartedAnimationViews(content::WebContents
* web_contents
);
34 // Move the animation to wherever it should currently be.
37 // Shut down the animation cleanly.
41 virtual void AnimateToState(double state
) OVERRIDE
;
43 // We use a TYPE_POPUP for the popup so that it may float above any windows in
45 views::Widget
* popup_
;
47 // The content area at the start of the animation. We store this so that the
48 // download shelf's resizing of the content area doesn't cause the animation
49 // to move around. This means that once started, the animation won't move
50 // with the parent window, but it's so fast that this shouldn't cause too
52 gfx::Rect web_contents_bounds_
;
54 DISALLOW_COPY_AND_ASSIGN(DownloadStartedAnimationViews
);
57 DownloadStartedAnimationViews::DownloadStartedAnimationViews(
58 content::WebContents
* web_contents
)
59 : gfx::LinearAnimation(kMoveTimeMs
, kFrameRateHz
, NULL
),
61 static gfx::ImageSkia
* kDownloadImage
= NULL
;
62 if (!kDownloadImage
) {
63 kDownloadImage
= ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
64 IDR_DOWNLOAD_ANIMATION_BEGIN
);
67 // If we're too small to show the download image, then don't bother -
68 // the shelf will be enough.
69 web_contents_bounds_
= web_contents
->GetContainerBounds();
70 if (web_contents_bounds_
.height() < kDownloadImage
->height())
73 SetImage(kDownloadImage
);
75 popup_
= new views::Widget
;
77 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
78 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
79 params
.accept_events
= false;
80 params
.parent
= web_contents
->GetNativeView();
82 popup_
->SetOpacity(0x00);
83 popup_
->SetContentsView(this);
90 void DownloadStartedAnimationViews::Reposition() {
91 // Align the image with the bottom left of the web contents (so that it
92 // points to the newly created download).
93 gfx::Size size
= GetPreferredSize();
94 int x
= base::i18n::IsRTL() ?
95 web_contents_bounds_
.right() - size
.width() : web_contents_bounds_
.x();
96 popup_
->SetBounds(gfx::Rect(
98 static_cast<int>(web_contents_bounds_
.bottom() -
99 size
.height() - size
.height() * (1 - GetCurrentValue())),
104 void DownloadStartedAnimationViews::Close() {
108 void DownloadStartedAnimationViews::AnimateToState(double state
) {
114 // Start at zero, peak halfway and end at zero.
115 double opacity
= std::min(1.0 - pow(GetCurrentValue() - 0.5, 2) * 4.0,
116 static_cast<double>(1.0));
118 popup_
->SetOpacity(static_cast<unsigned char>(opacity
* 255.0));
125 void DownloadStartedAnimation::Show(content::WebContents
* web_contents
) {
126 // The animation will delete itself when it's finished.
127 new DownloadStartedAnimationViews(web_contents
);