OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "chrome/browser/download/download_started_animation.h" | |
6 | |
7 #include "content/browser/tab_contents/tab_contents.h" | |
8 #include "content/public/browser/notification_details.h" | |
9 #include "content/public/browser/notification_registrar.h" | |
10 #include "content/public/browser/notification_source.h" | |
11 #include "content/public/browser/notification_types.h" | |
12 #include "grit/theme_resources.h" | |
13 #include "ui/base/animation/linear_animation.h" | |
14 #include "ui/base/resource/resource_bundle.h" | |
15 #include "ui/gfx/rect.h" | |
16 #include "views/controls/image_view.h" | |
17 #include "views/widget/widget.h" | |
18 | |
19 // How long to spend moving downwards and fading out after waiting. | |
20 static const int kMoveTimeMs = 600; | |
21 | |
22 // The animation framerate. | |
23 static const int kFrameRateHz = 60; | |
24 | |
25 // What fraction of the frame height to move downward from the frame center. | |
26 // Note that setting this greater than 0.5 will mean moving past the bottom of | |
27 // the frame. | |
28 static const double kMoveFraction = 1.0 / 3.0; | |
29 | |
30 namespace { | |
31 | |
32 // DownloadStartAnimation creates an animation (which begins running | |
33 // immediately) that animates an image downward from the center of the frame | |
34 // provided on the constructor, while simultaneously fading it out. To use, | |
35 // simply call "new DownloadStartAnimation"; the class cleans itself up when it | |
36 // finishes animating. | |
37 class DownloadStartedAnimationWin : public ui::LinearAnimation, | |
38 public content::NotificationObserver, | |
39 public views::ImageView { | |
40 public: | |
41 explicit DownloadStartedAnimationWin(TabContents* tab_contents); | |
42 | |
43 private: | |
44 // Move the animation to wherever it should currently be. | |
45 void Reposition(); | |
46 | |
47 // Shut down the animation cleanly. | |
48 void Close(); | |
49 | |
50 // Animation | |
51 virtual void AnimateToState(double state); | |
52 | |
53 // content::NotificationObserver | |
54 virtual void Observe(int type, | |
55 const content::NotificationSource& source, | |
56 const content::NotificationDetails& details); | |
57 | |
58 // We use a HWND for the popup so that it may float above any HWNDs in our UI. | |
59 views::Widget* popup_; | |
60 | |
61 // The content area holding us. | |
62 TabContents* tab_contents_; | |
63 | |
64 // The content area at the start of the animation. We store this so that the | |
65 // download shelf's resizing of the content area doesn't cause the animation | |
66 // to move around. This means that once started, the animation won't move | |
67 // with the parent window, but it's so fast that this shouldn't cause too | |
68 // much heartbreak. | |
69 gfx::Rect tab_contents_bounds_; | |
70 | |
71 // A scoped container for notification registries. | |
72 content::NotificationRegistrar registrar_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(DownloadStartedAnimationWin); | |
75 }; | |
76 | |
77 DownloadStartedAnimationWin::DownloadStartedAnimationWin( | |
78 TabContents* tab_contents) | |
79 : ui::LinearAnimation(kMoveTimeMs, kFrameRateHz, NULL), | |
80 popup_(NULL), | |
81 tab_contents_(tab_contents) { | |
82 static SkBitmap* kDownloadImage = NULL; | |
83 if (!kDownloadImage) { | |
84 kDownloadImage = ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
85 IDR_DOWNLOAD_ANIMATION_BEGIN); | |
86 } | |
87 | |
88 // If we're too small to show the download image, then don't bother - | |
89 // the shelf will be enough. | |
90 tab_contents_->GetContainerBounds(&tab_contents_bounds_); | |
91 if (tab_contents_bounds_.height() < kDownloadImage->height()) | |
92 return; | |
93 | |
94 registrar_.Add( | |
95 this, | |
96 content::NOTIFICATION_TAB_CONTENTS_HIDDEN, | |
97 content::Source<TabContents>(tab_contents_)); | |
98 registrar_.Add( | |
99 this, | |
100 content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | |
101 content::Source<TabContents>(tab_contents_)); | |
102 | |
103 SetImage(kDownloadImage); | |
104 | |
105 popup_ = new views::Widget; | |
106 | |
107 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | |
108 params.transparent = true; | |
109 params.accept_events = false; | |
110 params.parent = tab_contents_->GetNativeView(); | |
111 popup_->Init(params); | |
112 popup_->SetOpacity(0x00); | |
113 popup_->SetContentsView(this); | |
114 Reposition(); | |
115 popup_->Show(); | |
116 | |
117 Start(); | |
118 } | |
119 | |
120 void DownloadStartedAnimationWin::Reposition() { | |
121 if (!tab_contents_) | |
122 return; | |
123 | |
124 // Align the image with the bottom left of the web contents (so that it | |
125 // points to the newly created download). | |
126 gfx::Size size = GetPreferredSize(); | |
127 int x = base::i18n::IsRTL() ? | |
128 tab_contents_bounds_.right() - size.width() : tab_contents_bounds_.x(); | |
129 popup_->SetBounds(gfx::Rect( | |
130 x, | |
131 static_cast<int>(tab_contents_bounds_.bottom() - | |
132 size.height() - size.height() * (1 - GetCurrentValue())), | |
133 size.width(), | |
134 size.height())); | |
135 } | |
136 | |
137 void DownloadStartedAnimationWin::Close() { | |
138 if (!tab_contents_) | |
139 return; | |
140 | |
141 registrar_.Remove( | |
142 this, | |
143 content::NOTIFICATION_TAB_CONTENTS_HIDDEN, | |
144 content::Source<TabContents>(tab_contents_)); | |
145 registrar_.Remove( | |
146 this, | |
147 content::NOTIFICATION_TAB_CONTENTS_DESTROYED, | |
148 content::Source<TabContents>(tab_contents_)); | |
149 tab_contents_ = NULL; | |
150 popup_->Close(); | |
151 } | |
152 | |
153 void DownloadStartedAnimationWin::AnimateToState(double state) { | |
154 if (state >= 1.0) { | |
155 Close(); | |
156 } else { | |
157 Reposition(); | |
158 | |
159 // Start at zero, peak halfway and end at zero. | |
160 double opacity = std::min(1.0 - pow(GetCurrentValue() - 0.5, 2) * 4.0, | |
161 static_cast<double>(1.0)); | |
162 | |
163 popup_->SetOpacity( | |
164 static_cast<SkColor>(opacity * 255.0)); | |
165 SchedulePaint(); // Reposition() calls MoveWindow() which never picks up | |
166 // alpha changes, so we need to force a paint. | |
167 } | |
168 } | |
169 | |
170 void DownloadStartedAnimationWin::Observe( | |
171 int type, | |
172 const content::NotificationSource& source, | |
173 const content::NotificationDetails& details) { | |
174 Close(); | |
175 } | |
176 | |
177 } // namespace | |
178 | |
179 // static | |
180 void DownloadStartedAnimation::Show(TabContents* tab_contents) { | |
181 // The animation will delete itself when it's finished or when the tab | |
182 // contents is hidden or destroyed. | |
183 new DownloadStartedAnimationWin(tab_contents); | |
184 } | |
OLD | NEW |