Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: ui/views/controls/throbber.cc

Issue 1103903003: Clean up Throbber. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix spaces Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« ui/views/controls/throbber.h ('K') | « ui/views/controls/throbber.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/throbber.h" 5 #include "ui/views/controls/throbber.h"
6 6
7 #include "base/time/time.h"
8 #include "ui/base/resource/resource_bundle.h" 7 #include "ui/base/resource/resource_bundle.h"
9 #include "ui/gfx/canvas.h" 8 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/image/image.h" 9 #include "ui/gfx/image/image.h"
11 #include "ui/gfx/image/image_skia.h" 10 #include "ui/gfx/image/image_skia.h"
12 #include "ui/resources/grit/ui_resources.h" 11 #include "ui/resources/grit/ui_resources.h"
13 12
14 using base::Time;
tfarina 2015/04/24 23:08:19 Would you mind keep the using declarations? They d
Evan Stade 2015/04/24 23:15:32 in my experience, we don't usually use using direc
15 using base::TimeDelta;
16
17 namespace views { 13 namespace views {
18 14
19 Throbber::Throbber(int frame_time_ms, 15 Throbber::Throbber(int frame_time_ms, bool paint_while_stopped)
20 bool paint_while_stopped) 16 : paint_while_stopped_(paint_while_stopped),
21 : running_(false),
22 paint_while_stopped_(paint_while_stopped),
23 frames_(NULL), 17 frames_(NULL),
24 frame_time_(TimeDelta::FromMilliseconds(frame_time_ms)) { 18 frame_time_(base::TimeDelta::FromMilliseconds(frame_time_ms)) {
25 SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed( 19 SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
26 IDR_THROBBER).ToImageSkia()); 20 IDR_THROBBER).ToImageSkia());
27 } 21 }
28 22
29 Throbber::~Throbber() { 23 Throbber::~Throbber() {
30 Stop(); 24 Stop();
31 } 25 }
32 26
33 void Throbber::Start() { 27 void Throbber::Start() {
34 if (running_) 28 if (IsRunning())
35 return; 29 return;
36 30
37 start_time_ = Time::Now(); 31 start_time_ = base::TimeTicks::Now();
38 32 timer_.Start(FROM_HERE, frame_time_ - base::TimeDelta::FromMilliseconds(10),
39 timer_.Start(FROM_HERE, frame_time_ - TimeDelta::FromMilliseconds(10), 33 this, &Throbber::SchedulePaint);
40 this, &Throbber::Run);
41
42 running_ = true;
43
44 SchedulePaint(); // paint right away 34 SchedulePaint(); // paint right away
45 } 35 }
46 36
47 void Throbber::Stop() { 37 void Throbber::Stop() {
48 if (!running_) 38 if (!IsRunning())
49 return; 39 return;
50 40
51 timer_.Stop(); 41 timer_.Stop();
52
53 running_ = false;
54 SchedulePaint(); // Important if we're not painting while stopped 42 SchedulePaint(); // Important if we're not painting while stopped
55 } 43 }
56 44
57 void Throbber::SetFrames(const gfx::ImageSkia* frames) { 45 void Throbber::SetFrames(const gfx::ImageSkia* frames) {
58 frames_ = frames; 46 frames_ = frames;
59 DCHECK(frames_->width() > 0 && frames_->height() > 0); 47 DCHECK(frames_->width() > 0 && frames_->height() > 0);
60 DCHECK(frames_->width() % frames_->height() == 0); 48 DCHECK(frames_->width() % frames_->height() == 0);
61 frame_count_ = frames_->width() / frames_->height(); 49 frame_count_ = frames_->width() / frames_->height();
62 PreferredSizeChanged(); 50 PreferredSizeChanged();
63 } 51 }
64 52
65 void Throbber::Run() {
66 DCHECK(running_);
67
68 SchedulePaint();
69 }
70
71 gfx::Size Throbber::GetPreferredSize() const { 53 gfx::Size Throbber::GetPreferredSize() const {
72 return gfx::Size(frames_->height(), frames_->height()); 54 return gfx::Size(frames_->height(), frames_->height());
73 } 55 }
74 56
75 void Throbber::OnPaint(gfx::Canvas* canvas) { 57 void Throbber::OnPaint(gfx::Canvas* canvas) {
76 if (!running_ && !paint_while_stopped_) 58 if (!IsRunning() && !paint_while_stopped_)
77 return; 59 return;
78 60
79 const TimeDelta elapsed_time = Time::Now() - start_time_; 61 const base::TimeDelta elapsed_time = base::TimeTicks::Now() - start_time_;
80 const int current_frame = 62 const int current_frame =
81 static_cast<int>(elapsed_time / frame_time_) % frame_count_; 63 static_cast<int>(elapsed_time / frame_time_) % frame_count_;
82 64
83 int image_size = frames_->height(); 65 int image_size = frames_->height();
84 int image_offset = current_frame * image_size; 66 int image_offset = current_frame * image_size;
85 canvas->DrawImageInt(*frames_, 67 canvas->DrawImageInt(*frames_,
86 image_offset, 0, image_size, image_size, 68 image_offset, 0, image_size, image_size,
87 0, 0, image_size, image_size, 69 0, 0, image_size, image_size,
88 false); 70 false);
89 } 71 }
90 72
91 73 bool Throbber::IsRunning() const {
74 return timer_.IsRunning();
75 }
92 76
93 // Smoothed throbber --------------------------------------------------------- 77 // Smoothed throbber ---------------------------------------------------------
94 78
95
96 // Delay after work starts before starting throbber, in milliseconds. 79 // Delay after work starts before starting throbber, in milliseconds.
97 static const int kStartDelay = 200; 80 static const int kStartDelay = 200;
98 81
99 // Delay after work stops before stopping, in milliseconds. 82 // Delay after work stops before stopping, in milliseconds.
100 static const int kStopDelay = 50; 83 static const int kStopDelay = 50;
101 84
102
103 SmoothedThrobber::SmoothedThrobber(int frame_time_ms) 85 SmoothedThrobber::SmoothedThrobber(int frame_time_ms)
104 : Throbber(frame_time_ms, /* paint_while_stopped= */ false), 86 : Throbber(frame_time_ms, /* paint_while_stopped= */ false),
105 start_delay_ms_(kStartDelay), 87 start_delay_ms_(kStartDelay),
106 stop_delay_ms_(kStopDelay) { 88 stop_delay_ms_(kStopDelay) {
107 } 89 }
108 90
109 SmoothedThrobber::~SmoothedThrobber() {} 91 SmoothedThrobber::~SmoothedThrobber() {}
110 92
111 void SmoothedThrobber::Start() { 93 void SmoothedThrobber::Start() {
112 stop_timer_.Stop(); 94 stop_timer_.Stop();
113 95
114 if (!running() && !start_timer_.IsRunning()) { 96 if (!IsRunning() && !start_timer_.IsRunning()) {
115 start_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(start_delay_ms_), 97 start_timer_.Start(FROM_HERE,
116 this, &SmoothedThrobber::StartDelayOver); 98 base::TimeDelta::FromMilliseconds(start_delay_ms_), this,
99 &SmoothedThrobber::StartDelayOver);
117 } 100 }
118 } 101 }
119 102
120 void SmoothedThrobber::StartDelayOver() { 103 void SmoothedThrobber::StartDelayOver() {
121 Throbber::Start(); 104 Throbber::Start();
122 } 105 }
123 106
124 void SmoothedThrobber::Stop() { 107 void SmoothedThrobber::Stop() {
125 if (!running()) 108 if (!IsRunning())
126 start_timer_.Stop(); 109 start_timer_.Stop();
127 110
128 stop_timer_.Stop(); 111 stop_timer_.Stop();
129 stop_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(stop_delay_ms_), 112 stop_timer_.Start(FROM_HERE,
130 this, &SmoothedThrobber::StopDelayOver); 113 base::TimeDelta::FromMilliseconds(stop_delay_ms_), this,
114 &SmoothedThrobber::StopDelayOver);
131 } 115 }
132 116
133 void SmoothedThrobber::StopDelayOver() { 117 void SmoothedThrobber::StopDelayOver() {
134 Throbber::Stop(); 118 Throbber::Stop();
135 } 119 }
136 120
137 // Material throbber ----------------------------------------------------------- 121 // Material throbber -----------------------------------------------------------
138 122
139 // The length of a frame in milliseconds. 123 // The length of a frame in milliseconds.
140 // TODO(estade): remove the +10 when the -10 is removed from Throbber::Start(). 124 // TODO(estade): remove the +10 when the -10 is removed from Throbber::Start().
(...skipping 22 matching lines...) Expand all
163 return Throbber::GetPreferredSize(); 147 return Throbber::GetPreferredSize();
164 148
165 return gfx::Size(preferred_diameter_, preferred_diameter_); 149 return gfx::Size(preferred_diameter_, preferred_diameter_);
166 } 150 }
167 151
168 int MaterialThrobber::GetHeightForWidth(int w) const { 152 int MaterialThrobber::GetHeightForWidth(int w) const {
169 return w; 153 return w;
170 } 154 }
171 155
172 void MaterialThrobber::OnPaint(gfx::Canvas* canvas) { 156 void MaterialThrobber::OnPaint(gfx::Canvas* canvas) {
173 if (!running()) { 157 if (!IsRunning()) {
174 if (checked_) { 158 if (checked_) {
175 if (!checkmark_) { 159 if (!checkmark_) {
176 checkmark_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( 160 checkmark_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
177 IDR_CHECKMARK).ToImageSkia(); 161 IDR_CHECKMARK).ToImageSkia();
178 } 162 }
179 163
180 int checkmark_x = (width() - checkmark_->width()) / 2; 164 int checkmark_x = (width() - checkmark_->width()) / 2;
181 int checkmark_y = (height() - checkmark_->height()) / 2; 165 int checkmark_y = (height() - checkmark_->height()) / 2;
182 canvas->DrawImageInt(*checkmark_, checkmark_x, checkmark_y); 166 canvas->DrawImageInt(*checkmark_, checkmark_x, checkmark_y);
183 } 167 }
184 return; 168 return;
185 } 169 }
186 170
187 gfx::Rect bounds = GetContentsBounds(); 171 gfx::Rect bounds = GetContentsBounds();
188 // Inset by half the stroke width to make sure the whole arc is inside 172 // Inset by half the stroke width to make sure the whole arc is inside
189 // the visible rect. 173 // the visible rect.
190 SkScalar stroke_width = SkIntToScalar(bounds.width()) / 10.0; 174 SkScalar stroke_width = SkIntToScalar(bounds.width()) / 10.0;
191 gfx::Rect oval = bounds; 175 gfx::Rect oval = bounds;
192 int inset = SkScalarCeilToInt(stroke_width / 2.0); 176 int inset = SkScalarCeilToInt(stroke_width / 2.0);
193 oval.Inset(inset, inset); 177 oval.Inset(inset, inset);
194 178
195 // Calculate start and end points. The angles are counter-clockwise because 179 // Calculate start and end points. The angles are counter-clockwise because
196 // the throbber spins counter-clockwise. The finish angle starts at 12 o'clock 180 // the throbber spins counter-clockwise. The finish angle starts at 12 o'clock
197 // (90 degrees) and rotates steadily. The start angle trails 180 degrees 181 // (90 degrees) and rotates steadily. The start angle trails 180 degrees
198 // behind, except for the first half revolution, when it stays at 12 o'clock. 182 // behind, except for the first half revolution, when it stays at 12 o'clock.
199 base::TimeDelta revolution_time = base::TimeDelta::FromMilliseconds(1320); 183 base::TimeDelta revolution_time = base::TimeDelta::FromMilliseconds(1320);
200 base::TimeDelta elapsed_time = base::Time::Now() - start_time(); 184 base::TimeDelta elapsed_time = base::TimeTicks::Now() - start_time();
201 int64_t twelve_oclock = 90; 185 int64_t twelve_oclock = 90;
202 int64_t finish_angle = twelve_oclock + 360 * elapsed_time / revolution_time; 186 int64_t finish_angle = twelve_oclock + 360 * elapsed_time / revolution_time;
203 int64_t start_angle = std::max(finish_angle - 180, twelve_oclock); 187 int64_t start_angle = std::max(finish_angle - 180, twelve_oclock);
204 188
205 SkPath path; 189 SkPath path;
206 // Negate the angles to convert to the clockwise numbers Skia expects. 190 // Negate the angles to convert to the clockwise numbers Skia expects.
207 path.arcTo(gfx::RectToSkRect(oval), -start_angle, 191 path.arcTo(gfx::RectToSkRect(oval), -start_angle,
208 -(finish_angle - start_angle), true); 192 -(finish_angle - start_angle), true);
209 193
210 SkPaint paint; 194 SkPaint paint;
211 // TODO(estade): find a place for this color. 195 // TODO(estade): find a place for this color.
212 paint.setColor(SkColorSetRGB(0x42, 0x85, 0xF4)); 196 paint.setColor(SkColorSetRGB(0x42, 0x85, 0xF4));
213 paint.setStrokeCap(SkPaint::kRound_Cap); 197 paint.setStrokeCap(SkPaint::kRound_Cap);
214 paint.setStrokeWidth(stroke_width); 198 paint.setStrokeWidth(stroke_width);
215 paint.setStyle(SkPaint::kStroke_Style); 199 paint.setStyle(SkPaint::kStroke_Style);
216 paint.setAntiAlias(true); 200 paint.setAntiAlias(true);
217 canvas->DrawPath(path, paint); 201 canvas->DrawPath(path, paint);
218 } 202 }
219 203
220 } // namespace views 204 } // namespace views
OLDNEW
« ui/views/controls/throbber.h ('K') | « ui/views/controls/throbber.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698