Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / ui / views / controls / throbber.cc
blobdc4643366178d2152645662bb84f2d1db75bd29e
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"
17 namespace views {
19 Throbber::Throbber() : checked_(false), checkmark_(nullptr) {
22 Throbber::~Throbber() {
23 Stop();
26 void Throbber::Start() {
27 if (IsRunning())
28 return;
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() {
38 if (!IsRunning())
39 return;
41 timer_.Stop();
42 SchedulePaint();
45 void Throbber::SetChecked(bool checked) {
46 if (checked == checked_)
47 return;
49 checked_ = checked;
50 SchedulePaint();
53 gfx::Size Throbber::GetPreferredSize() const {
54 const int kDefaultDiameter = 16;
55 return gfx::Size(kDefaultDiameter, kDefaultDiameter);
58 void Throbber::OnPaint(gfx::Canvas* canvas) {
59 if (!IsRunning()) {
60 if (checked_) {
61 if (!checkmark_) {
62 checkmark_ = ui::ResourceBundle::GetSharedInstance()
63 .GetImageNamed(IDR_CHECKMARK)
64 .ToImageSkia();
67 int checkmark_x = (width() - checkmark_->width()) / 2;
68 int checkmark_y = (height() - checkmark_->height()) / 2;
69 canvas->DrawImageInt(*checkmark_, checkmark_x, checkmark_y);
71 return;
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() {
99 stop_timer_.Stop();
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() {
109 Throbber::Start();
112 void SmoothedThrobber::Stop() {
113 if (!IsRunning())
114 start_timer_.Stop();
116 stop_timer_.Stop();
117 stop_timer_.Start(FROM_HERE,
118 base::TimeDelta::FromMilliseconds(stop_delay_ms_), this,
119 &SmoothedThrobber::StopDelayOver);
122 void SmoothedThrobber::StopDelayOver() {
123 Throbber::Stop();
126 } // namespace views