summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/titlebar_throb_animation.cc
blob: 538aa847a57b9547230c870a3b5a1deb675a17f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2012 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/ui/gtk/titlebar_throb_animation.h"
#include "grit/theme_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/skbitmap_operations.h"

namespace {

// We don't bother to clean up these or the pixbufs they contain when we exit.
std::vector<GdkPixbuf*>* g_throbber_frames = NULL;
std::vector<GdkPixbuf*>* g_throbber_waiting_frames = NULL;

// Load |resource_id| from the ResourceBundle and split it into a series of
// square GdkPixbufs that get stored in |frames|.
void MakeThrobberFrames(int resource_id, std::vector<GdkPixbuf*>* frames) {
  ui::ResourceBundle &rb = ui::ResourceBundle::GetSharedInstance();
  SkBitmap frame_strip = rb.GetImageNamed(resource_id).AsBitmap();

  // Each frame of the animation is a square, so we use the height as the
  // frame size.
  int frame_size = frame_strip.height();
  size_t num_frames = frame_strip.width() / frame_size;

  // Make a separate GdkPixbuf for each frame of the animation.
  for (size_t i = 0; i < num_frames; ++i) {
    SkBitmap frame = SkBitmapOperations::CreateTiledBitmap(frame_strip,
        i * frame_size, 0, frame_size, frame_size);
    frames->push_back(gfx::GdkPixbufFromSkBitmap(frame));
  }
}

}  // namespace

// TODO(tc): Handle anti-clockwise spinning when waiting for a connection.

TitlebarThrobAnimation::TitlebarThrobAnimation()
    : current_frame_(0),
      current_waiting_frame_(0) {
}

GdkPixbuf* TitlebarThrobAnimation::GetNextFrame(bool is_waiting) {
  InitFrames();
  if (is_waiting) {
    return (*g_throbber_waiting_frames)[current_waiting_frame_++ %
        g_throbber_waiting_frames->size()];
  } else {
    return (*g_throbber_frames)[current_frame_++ % g_throbber_frames->size()];
  }
}

void TitlebarThrobAnimation::Reset() {
  current_frame_ = 0;
  current_waiting_frame_ = 0;
}

// static
void TitlebarThrobAnimation::InitFrames() {
  if (g_throbber_frames)
    return;

  // We load the light version of the throbber since it'll be in the titlebar.
  g_throbber_frames = new std::vector<GdkPixbuf*>;
  MakeThrobberFrames(IDR_THROBBER_LIGHT, g_throbber_frames);

  g_throbber_waiting_frames = new std::vector<GdkPixbuf*>;
  MakeThrobberFrames(IDR_THROBBER_WAITING_LIGHT, g_throbber_waiting_frames);
}