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/gfx/paint_vector_icon.h"
14 #include "ui/gfx/vector_icons_public.h"
15 #include "ui/native_theme/common_theme.h"
16 #include "ui/native_theme/native_theme.h"
17 #include "ui/resources/grit/ui_resources.h"
18 #include "ui/views/resources/grit/views_resources.h"
22 // The default diameter of a Throbber. If you change this, also change
24 static const int kDefaultDiameter
= 16;
25 // The size of the checkmark, in DIP. This magic number matches the default
26 // diamater plus padding inherent in the checkmark SVG.
27 static const int kCheckmarkDipSize
= 18;
29 Throbber::Throbber() : checked_(false) {
32 Throbber::~Throbber() {
36 void Throbber::Start() {
40 start_time_
= base::TimeTicks::Now();
41 const int kFrameTimeMs
= 30;
42 timer_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(kFrameTimeMs
), this,
43 &Throbber::SchedulePaint
);
44 SchedulePaint(); // paint right away
47 void Throbber::Stop() {
55 void Throbber::SetChecked(bool checked
) {
56 if (checked
== checked_
)
63 gfx::Size
Throbber::GetPreferredSize() const {
64 return gfx::Size(kDefaultDiameter
, kDefaultDiameter
);
67 void Throbber::OnPaint(gfx::Canvas
* canvas
) {
68 SkColor color
= GetNativeTheme()->GetSystemColor(
69 ui::NativeTheme::kColorId_ThrobberSpinningColor
);
73 canvas
->Translate(gfx::Vector2d((width() - kCheckmarkDipSize
) / 2,
74 (height() - kCheckmarkDipSize
) / 2));
75 gfx::PaintVectorIcon(canvas
, gfx::VectorIconId::CHECK_CIRCLE
,
76 kCheckmarkDipSize
, color
);
81 base::TimeDelta elapsed_time
= base::TimeTicks::Now() - start_time_
;
82 gfx::PaintThrobberSpinning(canvas
, GetContentsBounds(), color
, elapsed_time
);
85 bool Throbber::IsRunning() const {
86 return timer_
.IsRunning();
89 // Smoothed throbber ---------------------------------------------------------
91 // Delay after work starts before starting throbber, in milliseconds.
92 static const int kStartDelay
= 200;
94 // Delay after work stops before stopping, in milliseconds.
95 static const int kStopDelay
= 50;
97 SmoothedThrobber::SmoothedThrobber()
98 : start_delay_ms_(kStartDelay
), stop_delay_ms_(kStopDelay
) {
101 SmoothedThrobber::~SmoothedThrobber() {}
103 void SmoothedThrobber::Start() {
106 if (!IsRunning() && !start_timer_
.IsRunning()) {
107 start_timer_
.Start(FROM_HERE
,
108 base::TimeDelta::FromMilliseconds(start_delay_ms_
), this,
109 &SmoothedThrobber::StartDelayOver
);
113 void SmoothedThrobber::StartDelayOver() {
117 void SmoothedThrobber::Stop() {
122 stop_timer_
.Start(FROM_HERE
,
123 base::TimeDelta::FromMilliseconds(stop_delay_ms_
), this,
124 &SmoothedThrobber::StopDelayOver
);
127 void SmoothedThrobber::StopDelayOver() {