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.
5 #include "ui/views/controls/throbber.h"
7 #include "base/time/time.h"
8 #include "grit/ui_resources.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/canvas.h"
11 #include "ui/gfx/image/image.h"
12 #include "ui/gfx/image/image_skia.h"
15 using base::TimeDelta
;
19 Throbber::Throbber(int frame_time_ms
,
20 bool paint_while_stopped
)
22 paint_while_stopped_(paint_while_stopped
),
24 frame_time_(TimeDelta::FromMilliseconds(frame_time_ms
)) {
25 SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
26 IDR_THROBBER
).ToImageSkia());
29 Throbber::~Throbber() {
33 void Throbber::Start() {
37 start_time_
= Time::Now();
39 timer_
.Start(FROM_HERE
, frame_time_
- TimeDelta::FromMilliseconds(10),
40 this, &Throbber::Run
);
44 SchedulePaint(); // paint right away
47 void Throbber::Stop() {
54 SchedulePaint(); // Important if we're not painting while stopped
57 void Throbber::SetFrames(const gfx::ImageSkia
* frames
) {
59 DCHECK(frames_
->width() > 0 && frames_
->height() > 0);
60 DCHECK(frames_
->width() % frames_
->height() == 0);
61 frame_count_
= frames_
->width() / frames_
->height();
62 PreferredSizeChanged();
65 void Throbber::Run() {
71 gfx::Size
Throbber::GetPreferredSize() const {
72 return gfx::Size(frames_
->height(), frames_
->height());
75 void Throbber::OnPaint(gfx::Canvas
* canvas
) {
76 if (!running_
&& !paint_while_stopped_
)
79 const TimeDelta elapsed_time
= Time::Now() - start_time_
;
80 const int current_frame
=
81 static_cast<int>(elapsed_time
/ frame_time_
) % frame_count_
;
83 int image_size
= frames_
->height();
84 int image_offset
= current_frame
* image_size
;
85 canvas
->DrawImageInt(*frames_
,
86 image_offset
, 0, image_size
, image_size
,
87 0, 0, image_size
, image_size
,
93 // Smoothed throbber ---------------------------------------------------------
96 // Delay after work starts before starting throbber, in milliseconds.
97 static const int kStartDelay
= 200;
99 // Delay after work stops before stopping, in milliseconds.
100 static const int kStopDelay
= 50;
103 SmoothedThrobber::SmoothedThrobber(int frame_time_ms
)
104 : Throbber(frame_time_ms
, /* paint_while_stopped= */ false),
105 start_delay_ms_(kStartDelay
),
106 stop_delay_ms_(kStopDelay
) {
109 SmoothedThrobber::~SmoothedThrobber() {}
111 void SmoothedThrobber::Start() {
114 if (!running_
&& !start_timer_
.IsRunning()) {
115 start_timer_
.Start(FROM_HERE
, TimeDelta::FromMilliseconds(start_delay_ms_
),
116 this, &SmoothedThrobber::StartDelayOver
);
120 void SmoothedThrobber::StartDelayOver() {
124 void SmoothedThrobber::Stop() {
129 stop_timer_
.Start(FROM_HERE
, TimeDelta::FromMilliseconds(stop_delay_ms_
),
130 this, &SmoothedThrobber::StopDelayOver
);
133 void SmoothedThrobber::StopDelayOver() {
137 // Checkmark throbber ---------------------------------------------------------
139 CheckmarkThrobber::CheckmarkThrobber()
140 : Throbber(kFrameTimeMs
, false),
142 checkmark_(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
143 IDR_CHECKMARK
).ToImageSkia()) {
146 void CheckmarkThrobber::SetChecked(bool checked
) {
147 bool changed
= checked
!= checked_
;
154 void CheckmarkThrobber::OnPaint(gfx::Canvas
* canvas
) {
156 // Let the throbber throb...
157 Throbber::OnPaint(canvas
);
160 // Otherwise we paint our tick mark or nothing depending on our state.
162 int checkmark_x
= (width() - checkmark_
->width()) / 2;
163 int checkmark_y
= (height() - checkmark_
->height()) / 2;
164 canvas
->DrawImageInt(*checkmark_
, checkmark_x
, checkmark_y
);