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 "ui/base/resource/resource_bundle.h"
8 #include "ui/gfx/animation/tween.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/image/image.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/gfx/paint_throbber.h"
13 #include "ui/native_theme/common_theme.h"
14 #include "ui/native_theme/native_theme.h"
15 #include "ui/resources/grit/ui_resources.h"
19 Throbber::Throbber() : checked_(false), checkmark_(nullptr) {
22 Throbber::~Throbber() {
26 void Throbber::Start() {
30 start_time_
= base::TimeTicks::Now();
31 const int kFrameTimeMs
= 30;
32 timer_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(kFrameTimeMs
), this,
33 &Throbber::SchedulePaint
);
34 SchedulePaint(); // paint right away
37 void Throbber::Stop() {
45 void Throbber::SetChecked(bool checked
) {
46 if (checked
== checked_
)
53 gfx::Size
Throbber::GetPreferredSize() const {
54 const int kDefaultDiameter
= 16;
55 return gfx::Size(kDefaultDiameter
, kDefaultDiameter
);
58 void Throbber::OnPaint(gfx::Canvas
* canvas
) {
62 checkmark_
= ui::ResourceBundle::GetSharedInstance()
63 .GetImageNamed(IDR_CHECKMARK
)
67 int checkmark_x
= (width() - checkmark_
->width()) / 2;
68 int checkmark_y
= (height() - checkmark_
->height()) / 2;
69 canvas
->DrawImageInt(*checkmark_
, checkmark_x
, checkmark_y
);
74 SkColor color
= GetNativeTheme()->GetSystemColor(
75 ui::NativeTheme::kColorId_ThrobberSpinningColor
);
76 base::TimeDelta elapsed_time
= base::TimeTicks::Now() - start_time_
;
77 gfx::PaintThrobberSpinning(canvas
, GetContentsBounds(), color
, elapsed_time
);
80 bool Throbber::IsRunning() const {
81 return timer_
.IsRunning();
84 // Smoothed throbber ---------------------------------------------------------
86 // Delay after work starts before starting throbber, in milliseconds.
87 static const int kStartDelay
= 200;
89 // Delay after work stops before stopping, in milliseconds.
90 static const int kStopDelay
= 50;
92 SmoothedThrobber::SmoothedThrobber()
93 : start_delay_ms_(kStartDelay
), stop_delay_ms_(kStopDelay
) {
96 SmoothedThrobber::~SmoothedThrobber() {}
98 void SmoothedThrobber::Start() {
101 if (!IsRunning() && !start_timer_
.IsRunning()) {
102 start_timer_
.Start(FROM_HERE
,
103 base::TimeDelta::FromMilliseconds(start_delay_ms_
), this,
104 &SmoothedThrobber::StartDelayOver
);
108 void SmoothedThrobber::StartDelayOver() {
112 void SmoothedThrobber::Stop() {
117 stop_timer_
.Start(FROM_HERE
,
118 base::TimeDelta::FromMilliseconds(stop_delay_ms_
), this,
119 &SmoothedThrobber::StopDelayOver
);
122 void SmoothedThrobber::StopDelayOver() {