This source file includes following definitions.
- popup_
- Reposition
- Close
- AnimateToState
- Show
#include "chrome/browser/download/download_started_animation.h"
#include <math.h>
#include <gtk/gtk.h>
#include "base/message_loop/message_loop.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "grit/theme_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/rect.h"
namespace {
const int kMoveTimeMs = 600;
const int kFrameRateHz = 60;
class DownloadStartedAnimationGtk : public gfx::LinearAnimation {
public:
explicit DownloadStartedAnimationGtk(content::WebContents* web_contents);
virtual ~DownloadStartedAnimationGtk();
private:
void Reposition();
void Close();
virtual void AnimateToState(double state) OVERRIDE;
GtkWidget* popup_;
int width_;
int height_;
gfx::Rect web_contents_bounds_;
DISALLOW_COPY_AND_ASSIGN(DownloadStartedAnimationGtk);
};
DownloadStartedAnimationGtk::DownloadStartedAnimationGtk(
content::WebContents* web_contents)
: gfx::LinearAnimation(kMoveTimeMs, kFrameRateHz, NULL),
popup_(NULL) {
static GdkPixbuf* kDownloadImage = NULL;
if (!kDownloadImage) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
kDownloadImage = rb.GetNativeImageNamed(
IDR_DOWNLOAD_ANIMATION_BEGIN).ToGdkPixbuf();
}
width_ = gdk_pixbuf_get_width(kDownloadImage);
height_ = gdk_pixbuf_get_height(kDownloadImage);
web_contents->GetView()->GetContainerBounds(&web_contents_bounds_);
if (web_contents_bounds_.height() < height_)
return;
popup_ = gtk_window_new(GTK_WINDOW_POPUP);
GtkWidget* image = gtk_image_new_from_pixbuf(kDownloadImage);
gtk_container_add(GTK_CONTAINER(popup_), image);
GdkBitmap* mask = gdk_pixmap_new(NULL, width_, height_, 1);
gdk_pixbuf_render_threshold_alpha(kDownloadImage, mask,
0, 0,
0, 0, -1, -1,
0xff);
gtk_widget_shape_combine_mask(popup_, mask, 0, 0);
g_object_unref(mask);
Reposition();
gtk_widget_show_all(popup_);
gtk_window_present(GTK_WINDOW(popup_));
Start();
}
DownloadStartedAnimationGtk::~DownloadStartedAnimationGtk() {
}
void DownloadStartedAnimationGtk::Reposition() {
DCHECK(popup_);
gtk_window_move(GTK_WINDOW(popup_),
web_contents_bounds_.x(),
static_cast<int>(web_contents_bounds_.bottom() -
height_ - height_ * (1 - GetCurrentValue())));
}
void DownloadStartedAnimationGtk::Close() {
DCHECK(popup_);
gtk_widget_destroy(popup_);
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
void DownloadStartedAnimationGtk::AnimateToState(double state) {
if (state >= 1.0) {
Close();
} else {
Reposition();
double opacity = std::min(1.0 - pow(GetCurrentValue() - 0.5, 2) * 4.0,
static_cast<double>(1.0));
gtk_window_set_opacity(GTK_WINDOW(popup_), opacity);
}
}
}
void DownloadStartedAnimation::Show(content::WebContents* web_contents) {
new DownloadStartedAnimationGtk(web_contents);
}