1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "views/controls/progress_bar.h"
#include <string>
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "gfx/canvas.h"
#include "gfx/color_utils.h"
#include "gfx/font.h"
#include "gfx/insets.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "third_party/skia/include/effects/SkBlurMaskFilter.h"
#include "views/background.h"
#include "views/border.h"
#include "views/painter.h"
namespace {
// Corner radius for the progress bar's border.
const int kCornerRadius = 3;
// Progress bar's border width
const int kBorderWidth = 1;
static void AddRoundRectPath(int x, int y,
int w, int h,
int corner_radius,
SkPath* path) {
DCHECK(path);
if (path == NULL)
return;
SkScalar half = SkIntToScalar(1) / 2;
SkRect rect;
rect.set(
SkIntToScalar(x) + half, SkIntToScalar(y) + half,
SkIntToScalar(x + w) - half, SkIntToScalar(y + h) - half);
path->addRoundRect(
rect,
SkIntToScalar(corner_radius - half),
SkIntToScalar(corner_radius - half));
}
static void FillRoundRect(gfx::Canvas* canvas,
int x, int y,
int w, int h,
int corner_radius,
SkColor gradient_start_color,
SkColor gradient_end_color,
bool gradient_horizontal) {
SkPath path;
AddRoundRectPath(x, y, w, h, corner_radius, &path);
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setFlags(SkPaint::kAntiAlias_Flag);
if (gradient_start_color != gradient_end_color) {
SkPoint p[2];
p[0].set(SkIntToScalar(x), SkIntToScalar(y));
if (gradient_horizontal) {
p[1].set(SkIntToScalar(x + w), SkIntToScalar(y));
} else {
p[1].set(SkIntToScalar(x), SkIntToScalar(y + h));
}
SkColor colors[2] = { gradient_start_color, gradient_end_color };
SkShader* s = SkGradientShader::CreateLinear(
p, colors, NULL, 2, SkShader::kClamp_TileMode, NULL);
paint.setShader(s);
// Need to unref shader, otherwise never deleted.
s->unref();
} else {
paint.setColor(gradient_start_color);
}
canvas->drawPath(path, paint);
}
static void StrokeRoundRect(gfx::Canvas* canvas,
int x, int y,
int w, int h,
int corner_radius,
SkColor stroke_color,
int stroke_width) {
SkPath path;
AddRoundRectPath(x, y, w, h, corner_radius, &path);
SkPaint paint;
paint.setShader(NULL);
paint.setColor(stroke_color);
paint.setStyle(SkPaint::kStroke_Style);
paint.setFlags(SkPaint::kAntiAlias_Flag);
paint.setStrokeWidth(SkIntToScalar(stroke_width));
canvas->drawPath(path, paint);
}
} // anonymous namespace
namespace views {
// static
const char ProgressBar::kViewClassName[] = "views/ProgressBar";
// static: progress bar's maximum value.
const int ProgressBar::kMaxProgress = 100;
ProgressBar::ProgressBar(): progress_(0) {
}
ProgressBar::~ProgressBar() {
}
gfx::Size ProgressBar::GetPreferredSize() {
return gfx::Size(100, 16);
}
void ProgressBar::Paint(gfx::Canvas* canvas) {
SkColor bar_color_start = SkColorSetRGB(81, 138, 223);
SkColor bar_color_end = SkColorSetRGB(51, 103, 205);
SkColor background_color_start = SkColorSetRGB(212, 212, 212);
SkColor background_color_end = SkColorSetRGB(252, 252, 252);
SkColor border_color = SkColorSetRGB(144, 144, 144);
FillRoundRect(canvas,
0, 0, width(), height(),
kCornerRadius,
background_color_start,
background_color_end,
false);
if (progress_ * width() > 1) {
FillRoundRect(canvas,
0, 0,
progress_ * width() / kMaxProgress, height(),
kCornerRadius,
bar_color_start,
bar_color_end,
false);
}
StrokeRoundRect(canvas,
0, 0,
width(), height(),
kCornerRadius,
border_color,
kBorderWidth);
}
std::string ProgressBar::GetClassName() const {
return kViewClassName;
}
void ProgressBar::SetProgress(int progress) {
progress_ = progress;
if (progress_ < 0)
progress_ = 0;
else if (progress_ > kMaxProgress)
progress_ = kMaxProgress;
SchedulePaint();
}
int ProgressBar::GetProgress() const {
return progress_;
}
void ProgressBar::AddProgress(int tick) {
SetProgress(progress_ + tick);
}
void ProgressBar::SetTooltipText(const std::wstring& tooltip_text) {
tooltip_text_ = tooltip_text;
}
bool ProgressBar::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) {
DCHECK(tooltip);
if (tooltip == NULL)
return false;
tooltip->assign(tooltip_text_);
return !tooltip_text_.empty();
}
void ProgressBar::SetEnabled(bool enabled) {
if (enabled == enabled_)
return;
View::SetEnabled(enabled);
// TODO(denisromanov): Need to switch progress bar color here?
}
bool ProgressBar::GetAccessibleRole(AccessibilityTypes::Role* role) {
DCHECK(role);
if (role == NULL)
return false;
*role = AccessibilityTypes::ROLE_TEXT;
return true;
}
bool ProgressBar::GetAccessibleName(std::wstring* name) {
DCHECK(name);
if (name == NULL)
return false;
if (!accessible_name_.empty()) {
*name = accessible_name_;
return true;
}
return false;
}
bool ProgressBar::GetAccessibleState(AccessibilityTypes::State* state) {
DCHECK(state);
if (state == NULL)
return false;
*state = AccessibilityTypes::STATE_READONLY;
return true;
}
void ProgressBar::SetAccessibleName(const std::wstring& name) {
accessible_name_.assign(name);
}
} // namespace views
|