| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/ui/gtk/throbber_gtk.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/ui/gtk/theme_service_gtk.h" |
| 9 #include "chrome/common/chrome_notification_types.h" |
| 10 #include "content/public/browser/notification_source.h" |
| 11 #include "grit/ui_resources.h" |
| 12 #include "ui/base/animation/tween.h" |
| 13 #include "ui/gfx/image/cairo_cached_surface.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 // The length of a single cycle of the animation in milliseconds. |
| 18 const int kThrobberDurationMs = 750; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 ThrobberGtk::ThrobberGtk(ThemeServiceGtk* theme_service) |
| 23 : theme_service_(theme_service), |
| 24 ALLOW_THIS_IN_INITIALIZER_LIST(animation_(this)), |
| 25 frames_(NULL), |
| 26 num_frames_(0) { |
| 27 DCHECK(theme_service_); |
| 28 Init(); |
| 29 } |
| 30 |
| 31 ThrobberGtk::~ThrobberGtk() { |
| 32 widget_.Destroy(); |
| 33 } |
| 34 |
| 35 void ThrobberGtk::Start() { |
| 36 animation_.Show(); |
| 37 } |
| 38 |
| 39 void ThrobberGtk::Stop() { |
| 40 animation_.Reset(); |
| 41 } |
| 42 |
| 43 void ThrobberGtk::AnimationEnded(const ui::Animation* animation) { |
| 44 animation_.Reset(); |
| 45 animation_.Show(); |
| 46 } |
| 47 |
| 48 void ThrobberGtk::AnimationProgressed(const ui::Animation* animation) { |
| 49 gtk_widget_queue_draw(widget_.get()); |
| 50 } |
| 51 |
| 52 void ThrobberGtk::Observe(int type, |
| 53 const content::NotificationSource& source, |
| 54 const content::NotificationDetails& details) { |
| 55 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| 56 LoadFrames(); |
| 57 gtk_widget_queue_draw(widget_.get()); |
| 58 } |
| 59 |
| 60 gboolean ThrobberGtk::OnExpose(GtkWidget* widget, GdkEventExpose* expose) { |
| 61 cairo_t* cairo_context = |
| 62 gdk_cairo_create(GDK_DRAWABLE(gtk_widget_get_window(widget))); |
| 63 GtkAllocation allocation; |
| 64 gtk_widget_get_allocation(widget, &allocation); |
| 65 cairo_translate(cairo_context, allocation.x, allocation.y); |
| 66 |
| 67 gfx::CairoCachedSurface* cairo_frames = frames_->ToCairo(); |
| 68 const int frame = |
| 69 static_cast<int>(animation_.GetCurrentValue() * (num_frames_ - 1)); |
| 70 const int image_size = cairo_frames->Height(); |
| 71 const int image_offset = frame * image_size; |
| 72 |
| 73 cairo_frames->SetSource(cairo_context, widget, -image_offset, 0); |
| 74 cairo_rectangle(cairo_context, 0, 0, image_size, image_size); |
| 75 cairo_fill(cairo_context); |
| 76 cairo_destroy(cairo_context); |
| 77 |
| 78 return TRUE; |
| 79 } |
| 80 |
| 81 void ThrobberGtk::Init() { |
| 82 animation_.SetSlideDuration(kThrobberDurationMs); |
| 83 animation_.SetTweenType(ui::Tween::LINEAR); |
| 84 widget_.Own(gtk_image_new()); |
| 85 gtk_widget_set_can_focus(widget_.get(), FALSE); |
| 86 g_signal_connect(widget_.get(), "expose-event", G_CALLBACK(OnExposeThunk), |
| 87 this); |
| 88 |
| 89 theme_service_->InitThemesFor(this); |
| 90 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 91 content::Source<ThemeService>(theme_service_)); |
| 92 } |
| 93 |
| 94 void ThrobberGtk::LoadFrames() { |
| 95 frames_ = theme_service_->GetImageNamed(IDR_THROBBER); |
| 96 DCHECK(frames_); |
| 97 const int width = frames_->ToCairo()->Width(); |
| 98 const int height = frames_->ToCairo()->Height(); |
| 99 DCHECK_EQ(0, width % height); |
| 100 num_frames_ = width / height; |
| 101 |
| 102 gtk_widget_set_size_request(widget_.get(), height, height); |
| 103 } |
| OLD | NEW |