From bd1ad684dee1355226c5f6946034369b98186a55 Mon Sep 17 00:00:00 2001 From: "estade@chromium.org" Date: Fri, 15 May 2009 22:19:17 +0000 Subject: GTK: Add download shelf first show animation. http://crbug.com/8631 Review URL: http://codereview.chromium.org/113428 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16196 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/views/browser_views.vcproj | 6 +- chrome/browser/views/download_started_animation.cc | 117 -------------- chrome/browser/views/download_started_animation.h | 63 -------- .../views/download_started_animation_win.cc | 178 +++++++++++++++++++++ 4 files changed, 179 insertions(+), 185 deletions(-) delete mode 100644 chrome/browser/views/download_started_animation.cc delete mode 100644 chrome/browser/views/download_started_animation.h create mode 100644 chrome/browser/views/download_started_animation_win.cc (limited to 'chrome/browser/views') diff --git a/chrome/browser/views/browser_views.vcproj b/chrome/browser/views/browser_views.vcproj index d2594d0..75e2939 100644 --- a/chrome/browser/views/browser_views.vcproj +++ b/chrome/browser/views/browser_views.vcproj @@ -546,11 +546,7 @@ > - - GetContainerBounds(&tab_contents_bounds_); - if (tab_contents_bounds_.height() < kDownloadImage->height()) - return; - - registrar_.Add( - this, - NotificationType::TAB_CONTENTS_HIDDEN, - Source(tab_contents_)); - registrar_.Add( - this, - NotificationType::TAB_CONTENTS_DESTROYED, - Source(tab_contents_)); - - SetImage(kDownloadImage); - - gfx::Rect rc(0, 0, 0, 0); - popup_ = new views::WidgetWin; - popup_->set_window_style(WS_POPUP); - popup_->set_window_ex_style(WS_EX_LAYERED | WS_EX_TOOLWINDOW | - WS_EX_TRANSPARENT); - popup_->SetLayeredAlpha(0x00); - popup_->Init(tab_contents_->GetNativeView(), rc, false); - popup_->SetContentsView(this); - Reposition(); - popup_->Show(); - - Start(); -} - -void DownloadStartedAnimation::Reposition() { - if (!tab_contents_) - return; - - // Align the image with the bottom left of the web contents (so that it - // points to the newly created download). - gfx::Size size = GetPreferredSize(); - popup_->MoveWindow( - tab_contents_bounds_.x(), - static_cast(tab_contents_bounds_.bottom() - - size.height() - size.height() * (1 - GetCurrentValue())), - size.width(), - size.height()); -} - -void DownloadStartedAnimation::Close() { - if (!tab_contents_) - return; - - registrar_.Remove( - this, - NotificationType::TAB_CONTENTS_HIDDEN, - Source(tab_contents_)); - registrar_.Remove( - this, - NotificationType::TAB_CONTENTS_DESTROYED, - Source(tab_contents_)); - tab_contents_ = NULL; - popup_->Close(); -} - -void DownloadStartedAnimation::AnimateToState(double state) { - if (state >= 1.0) { - Close(); - } else { - Reposition(); - - // Start at zero, peak halfway and end at zero. - double opacity = std::min(1.0 - pow(GetCurrentValue() - 0.5, 2) * 4.0, - static_cast(1.0)); - - popup_->SetLayeredAlpha( - static_cast(opacity * 255.0)); - SchedulePaint(); // Reposition() calls MoveWindow() which never picks up - // alpha changes, so we need to force a paint. - } -} - -void DownloadStartedAnimation::Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - Close(); -} diff --git a/chrome/browser/views/download_started_animation.h b/chrome/browser/views/download_started_animation.h deleted file mode 100644 index ec84244..0000000 --- a/chrome/browser/views/download_started_animation.h +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_VIEWS_DOWNLOAD_STARTED_ANIMATION_H_ -#define CHROME_BROWSER_VIEWS_DOWNLOAD_STARTED_ANIMATION_H_ - -#include "app/animation.h" -#include "base/gfx/rect.h" -#include "chrome/common/notification_registrar.h" -#include "views/controls/image_view.h" - -namespace views { -class WidgetWin; -}; -class TabContents; - -// DownloadStartAnimation creates an animation (which begins running -// immediately) that animates an image downward from the center of the frame -// provided on the constructor, while simultaneously fading it out. To use, -// simply call "new DownloadStartAnimation"; the class cleans itself up when it -// finishes animating. -class DownloadStartedAnimation : public Animation, - public NotificationObserver, - public views::ImageView { - public: - DownloadStartedAnimation(TabContents* tab_contents); - - private: - // Move the animation to wherever it should currently be. - void Reposition(); - - // Shut down the animation cleanly. - void Close(); - - // Animation - virtual void AnimateToState(double state); - - // NotificationObserver - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details); - - // We use a HWND for the popup so that it may float above any HWNDs in our UI. - views::WidgetWin* popup_; - - // The content area holding us. - TabContents* tab_contents_; - - // The content area at the start of the animation. We store this so that the - // download shelf's resizing of the content area doesn't cause the animation - // to move around. This means that once started, the animation won't move - // with the parent window, but it's so fast that this shouldn't cause too - // much heartbreak. - gfx::Rect tab_contents_bounds_; - - // A scoped container for notification registries. - NotificationRegistrar registrar_; - - DISALLOW_COPY_AND_ASSIGN(DownloadStartedAnimation); -}; - -#endif // CHROME_BROWSER_VIEWS_DOWNLOAD_STARTED_ANIMATION_H_ diff --git a/chrome/browser/views/download_started_animation_win.cc b/chrome/browser/views/download_started_animation_win.cc new file mode 100644 index 0000000..6ae160b --- /dev/null +++ b/chrome/browser/views/download_started_animation_win.cc @@ -0,0 +1,178 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/download/download_started_animation.h" + +#include "app/animation.h" +#include "app/resource_bundle.h" +#include "base/gfx/rect.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/notification_registrar.h" +#include "chrome/common/notification_service.h" +#include "grit/theme_resources.h" +#include "views/controls/image_view.h" +#include "views/widget/widget_win.h" + +// How long to spend moving downwards and fading out after waiting. +static const int kMoveTimeMs = 600; + +// The animation framerate. +static const int kFrameRateHz = 60; + +// What fraction of the frame height to move downward from the frame center. +// Note that setting this greater than 0.5 will mean moving past the bottom of +// the frame. +static const double kMoveFraction = 1.0 / 3.0; + +namespace { + +// DownloadStartAnimation creates an animation (which begins running +// immediately) that animates an image downward from the center of the frame +// provided on the constructor, while simultaneously fading it out. To use, +// simply call "new DownloadStartAnimation"; the class cleans itself up when it +// finishes animating. +class DownloadStartedAnimationWin : public Animation, + public NotificationObserver, + public views::ImageView { + public: + DownloadStartedAnimationWin(TabContents* tab_contents); + + private: + // Move the animation to wherever it should currently be. + void Reposition(); + + // Shut down the animation cleanly. + void Close(); + + // Animation + virtual void AnimateToState(double state); + + // NotificationObserver + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details); + + // We use a HWND for the popup so that it may float above any HWNDs in our UI. + views::WidgetWin* popup_; + + // The content area holding us. + TabContents* tab_contents_; + + // The content area at the start of the animation. We store this so that the + // download shelf's resizing of the content area doesn't cause the animation + // to move around. This means that once started, the animation won't move + // with the parent window, but it's so fast that this shouldn't cause too + // much heartbreak. + gfx::Rect tab_contents_bounds_; + + // A scoped container for notification registries. + NotificationRegistrar registrar_; + + DISALLOW_COPY_AND_ASSIGN(DownloadStartedAnimationWin); +}; + +DownloadStartedAnimationWin::DownloadStartedAnimationWin( + TabContents* tab_contents) + : Animation(kMoveTimeMs, kFrameRateHz, NULL), + popup_(NULL), + tab_contents_(tab_contents) { + static SkBitmap* kDownloadImage = NULL; + if (!kDownloadImage) { + kDownloadImage = ResourceBundle::GetSharedInstance().GetBitmapNamed( + IDR_DOWNLOAD_ANIMATION_BEGIN); + } + + // If we're too small to show the download image, then don't bother - + // the shelf will be enough. + tab_contents_->GetContainerBounds(&tab_contents_bounds_); + if (tab_contents_bounds_.height() < kDownloadImage->height()) + return; + + registrar_.Add( + this, + NotificationType::TAB_CONTENTS_HIDDEN, + Source(tab_contents_)); + registrar_.Add( + this, + NotificationType::TAB_CONTENTS_DESTROYED, + Source(tab_contents_)); + + SetImage(kDownloadImage); + + gfx::Rect rc(0, 0, 0, 0); + popup_ = new views::WidgetWin; + popup_->set_window_style(WS_POPUP); + popup_->set_window_ex_style(WS_EX_LAYERED | WS_EX_TOOLWINDOW | + WS_EX_TRANSPARENT); + popup_->SetLayeredAlpha(0x00); + popup_->Init(tab_contents_->GetNativeView(), rc, false); + popup_->SetContentsView(this); + Reposition(); + popup_->Show(); + + Start(); +} + +void DownloadStartedAnimationWin::Reposition() { + if (!tab_contents_) + return; + + // Align the image with the bottom left of the web contents (so that it + // points to the newly created download). + gfx::Size size = GetPreferredSize(); + popup_->MoveWindow( + tab_contents_bounds_.x(), + static_cast(tab_contents_bounds_.bottom() - + size.height() - size.height() * (1 - GetCurrentValue())), + size.width(), + size.height()); +} + +void DownloadStartedAnimationWin::Close() { + if (!tab_contents_) + return; + + registrar_.Remove( + this, + NotificationType::TAB_CONTENTS_HIDDEN, + Source(tab_contents_)); + registrar_.Remove( + this, + NotificationType::TAB_CONTENTS_DESTROYED, + Source(tab_contents_)); + tab_contents_ = NULL; + popup_->Close(); +} + +void DownloadStartedAnimationWin::AnimateToState(double state) { + if (state >= 1.0) { + Close(); + } else { + Reposition(); + + // Start at zero, peak halfway and end at zero. + double opacity = std::min(1.0 - pow(GetCurrentValue() - 0.5, 2) * 4.0, + static_cast(1.0)); + + popup_->SetLayeredAlpha( + static_cast(opacity * 255.0)); + SchedulePaint(); // Reposition() calls MoveWindow() which never picks up + // alpha changes, so we need to force a paint. + } +} + +void DownloadStartedAnimationWin::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + Close(); +} + +} // namespace + +// static +void DownloadStartedAnimation::Show(TabContents* tab_contents) { + // The animation will delete itself when it's finished or when the tab + // contents is hidden or destroyed. + new DownloadStartedAnimationWin(tab_contents); +} -- cgit v1.1