| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "views/controls/progress_bar.h" | 5 #include "views/controls/progress_bar.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "gfx/canvas_skia.h" | 11 #include "gfx/canvas_skia.h" |
| 12 #include "gfx/color_utils.h" | 12 #include "gfx/color_utils.h" |
| 13 #include "gfx/font.h" | 13 #include "gfx/font.h" |
| 14 #include "gfx/insets.h" | 14 #include "gfx/insets.h" |
| 15 #include "third_party/skia/include/effects/SkGradientShader.h" | 15 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 16 #include "third_party/skia/include/effects/SkBlurMaskFilter.h" | 16 #include "third_party/skia/include/effects/SkBlurMaskFilter.h" |
| 17 #include "views/background.h" | 17 #include "views/background.h" |
| 18 #include "views/border.h" | 18 #include "views/border.h" |
| 19 #include "views/painter.h" | 19 #include "views/painter.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Corner radius for the progress bar's border. | 23 // Corner radius for the progress bar's border. |
| 24 const int kCornerRadius = 3; | 24 const int kCornerRadius = 3; |
| 25 | 25 |
| 26 // Progress bar's border width | 26 // Progress bar's border width |
| 27 const int kBorderWidth = 1; | 27 const int kBorderWidth = 1; |
| 28 | 28 |
| 29 static void AddRoundRectPathWithPadding(int x, int y, |
| 30 int w, int h, |
| 31 int corner_radius, |
| 32 SkScalar padding, |
| 33 SkPath* path) { |
| 34 DCHECK(path); |
| 35 if (path == NULL) |
| 36 return; |
| 37 SkRect rect; |
| 38 rect.set( |
| 39 SkIntToScalar(x) + padding, SkIntToScalar(y) + padding, |
| 40 SkIntToScalar(x + w) - padding, SkIntToScalar(y + h) - padding); |
| 41 path->addRoundRect( |
| 42 rect, |
| 43 SkIntToScalar(corner_radius - padding), |
| 44 SkIntToScalar(corner_radius - padding)); |
| 45 } |
| 46 |
| 29 static void AddRoundRectPath(int x, int y, | 47 static void AddRoundRectPath(int x, int y, |
| 30 int w, int h, | 48 int w, int h, |
| 31 int corner_radius, | 49 int corner_radius, |
| 32 SkPath* path) { | 50 SkPath* path) { |
| 33 DCHECK(path); | 51 static const SkScalar half = SkIntToScalar(1) / 2; |
| 34 if (path == NULL) | 52 AddRoundRectPathWithPadding(x, y, w, h, corner_radius, half, path); |
| 35 return; | 53 } |
| 36 SkScalar half = SkIntToScalar(1) / 2; | 54 |
| 37 SkRect rect; | 55 static void FillRoundRect(gfx::Canvas* canvas, |
| 38 rect.set( | 56 int x, int y, |
| 39 SkIntToScalar(x) + half, SkIntToScalar(y) + half, | 57 int w, int h, |
| 40 SkIntToScalar(x + w) - half, SkIntToScalar(y + h) - half); | 58 int corner_radius, |
| 41 path->addRoundRect( | 59 const SkColor colors[], |
| 42 rect, | 60 const SkScalar points[], |
| 43 SkIntToScalar(corner_radius - half), | 61 int count, |
| 44 SkIntToScalar(corner_radius - half)); | 62 bool gradient_horizontal) { |
| 63 SkPath path; |
| 64 AddRoundRectPath(x, y, w, h, corner_radius, &path); |
| 65 SkPaint paint; |
| 66 paint.setStyle(SkPaint::kFill_Style); |
| 67 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 68 |
| 69 SkPoint p[2]; |
| 70 p[0].set(SkIntToScalar(x), SkIntToScalar(y)); |
| 71 if (gradient_horizontal) { |
| 72 p[1].set(SkIntToScalar(x + w), SkIntToScalar(y)); |
| 73 } else { |
| 74 p[1].set(SkIntToScalar(x), SkIntToScalar(y + h)); |
| 75 } |
| 76 SkShader* s = SkGradientShader::CreateLinear( |
| 77 p, colors, points, count, SkShader::kClamp_TileMode, NULL); |
| 78 paint.setShader(s); |
| 79 // Need to unref shader, otherwise never deleted. |
| 80 s->unref(); |
| 81 |
| 82 canvas->AsCanvasSkia()->drawPath(path, paint); |
| 45 } | 83 } |
| 46 | 84 |
| 47 static void FillRoundRect(gfx::Canvas* canvas, | 85 static void FillRoundRect(gfx::Canvas* canvas, |
| 48 int x, int y, | 86 int x, int y, |
| 49 int w, int h, | 87 int w, int h, |
| 50 int corner_radius, | 88 int corner_radius, |
| 51 SkColor gradient_start_color, | 89 SkColor gradient_start_color, |
| 52 SkColor gradient_end_color, | 90 SkColor gradient_end_color, |
| 53 bool gradient_horizontal) { | 91 bool gradient_horizontal) { |
| 54 SkPath path; | |
| 55 AddRoundRectPath(x, y, w, h, corner_radius, &path); | |
| 56 SkPaint paint; | |
| 57 paint.setStyle(SkPaint::kFill_Style); | |
| 58 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
| 59 if (gradient_start_color != gradient_end_color) { | 92 if (gradient_start_color != gradient_end_color) { |
| 60 SkPoint p[2]; | |
| 61 p[0].set(SkIntToScalar(x), SkIntToScalar(y)); | |
| 62 if (gradient_horizontal) { | |
| 63 p[1].set(SkIntToScalar(x + w), SkIntToScalar(y)); | |
| 64 } else { | |
| 65 p[1].set(SkIntToScalar(x), SkIntToScalar(y + h)); | |
| 66 } | |
| 67 SkColor colors[2] = { gradient_start_color, gradient_end_color }; | 93 SkColor colors[2] = { gradient_start_color, gradient_end_color }; |
| 68 SkShader* s = SkGradientShader::CreateLinear( | 94 FillRoundRect(canvas, x, y, w, h, corner_radius, |
| 69 p, colors, NULL, 2, SkShader::kClamp_TileMode, NULL); | 95 colors, NULL, 2, gradient_horizontal); |
| 70 paint.setShader(s); | |
| 71 // Need to unref shader, otherwise never deleted. | |
| 72 s->unref(); | |
| 73 } else { | 96 } else { |
| 97 SkPath path; |
| 98 AddRoundRectPath(x, y, w, h, corner_radius, &path); |
| 99 SkPaint paint; |
| 100 paint.setStyle(SkPaint::kFill_Style); |
| 101 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 74 paint.setColor(gradient_start_color); | 102 paint.setColor(gradient_start_color); |
| 103 canvas->AsCanvasSkia()->drawPath(path, paint); |
| 75 } | 104 } |
| 76 canvas->AsCanvasSkia()->drawPath(path, paint); | |
| 77 } | 105 } |
| 78 | 106 |
| 79 static void StrokeRoundRect(gfx::Canvas* canvas, | 107 static void StrokeRoundRect(gfx::Canvas* canvas, |
| 80 int x, int y, | 108 int x, int y, |
| 81 int w, int h, | 109 int w, int h, |
| 82 int corner_radius, | 110 int corner_radius, |
| 83 SkColor stroke_color, | 111 SkColor stroke_color, |
| 84 int stroke_width) { | 112 int stroke_width) { |
| 85 SkPath path; | 113 SkPath path; |
| 86 AddRoundRectPath(x, y, w, h, corner_radius, &path); | 114 AddRoundRectPath(x, y, w, h, corner_radius, &path); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 107 } | 135 } |
| 108 | 136 |
| 109 ProgressBar::~ProgressBar() { | 137 ProgressBar::~ProgressBar() { |
| 110 } | 138 } |
| 111 | 139 |
| 112 gfx::Size ProgressBar::GetPreferredSize() { | 140 gfx::Size ProgressBar::GetPreferredSize() { |
| 113 return gfx::Size(100, 16); | 141 return gfx::Size(100, 16); |
| 114 } | 142 } |
| 115 | 143 |
| 116 void ProgressBar::Paint(gfx::Canvas* canvas) { | 144 void ProgressBar::Paint(gfx::Canvas* canvas) { |
| 145 #if defined(OS_CHROMEOS) |
| 146 const SkColor background_colors[] = { |
| 147 SkColorSetRGB(0xBB, 0xBB, 0xBB), |
| 148 SkColorSetRGB(0xE7, 0xE7, 0xE7), |
| 149 SkColorSetRGB(0xFE, 0xFE, 0xFE) |
| 150 }; |
| 151 |
| 152 const SkScalar background_points[] = { |
| 153 SkDoubleToScalar(0), |
| 154 SkDoubleToScalar(0.1), |
| 155 SkDoubleToScalar(1) |
| 156 }; |
| 157 const SkColor background_border_color = SkColorSetRGB(0xA1, 0xA1, 0xA1); |
| 158 |
| 159 // Draw background. |
| 160 FillRoundRect(canvas, |
| 161 0, 0, width(), height(), |
| 162 kCornerRadius, |
| 163 background_colors, |
| 164 background_points, |
| 165 arraysize(background_colors), |
| 166 false); |
| 167 StrokeRoundRect(canvas, |
| 168 0, 0, |
| 169 width(), height(), |
| 170 kCornerRadius, |
| 171 background_border_color, |
| 172 kBorderWidth); |
| 173 |
| 174 if (progress_ * width() > 1) { |
| 175 int progress_width = progress_ * width() / kMaxProgress; |
| 176 |
| 177 bool enabled = IsEnabled(); |
| 178 |
| 179 const SkColor bar_color_start = enabled ? |
| 180 SkColorSetRGB(100, 116, 147) : |
| 181 SkColorSetRGB(229, 232, 237); |
| 182 const SkColor bar_color_end = enabled ? |
| 183 SkColorSetRGB(65, 73, 87) : |
| 184 SkColorSetRGB(224, 225, 227); |
| 185 |
| 186 const SkColor bar_outer_color = enabled ? |
| 187 SkColorSetRGB(0x4A, 0x4A, 0x4A) : |
| 188 SkColorSetARGB(0x80, 0x4A, 0x4A, 0x4A); |
| 189 |
| 190 const SkColor bar_inner_border_color = |
| 191 SkColorSetARGB(0x3F, 0xFF, 0xFF, 0xFF); // 0.25 white |
| 192 const SkColor bar_inner_shadow_color = |
| 193 SkColorSetARGB(0x54, 0xFF, 0xFF, 0xFF); // 0.33 white |
| 194 |
| 195 // Draw bar background |
| 196 FillRoundRect(canvas, |
| 197 0, 0, progress_width, height(), |
| 198 kCornerRadius, |
| 199 bar_color_start, |
| 200 bar_color_end, |
| 201 false); |
| 202 |
| 203 // Draw inner stroke and shadow if wide enough. |
| 204 if (progress_width > 2 * kBorderWidth) { |
| 205 canvas->AsCanvasSkia()->save(); |
| 206 |
| 207 SkPath inner_path; |
| 208 AddRoundRectPathWithPadding( |
| 209 0, 0, progress_width, height(), |
| 210 kCornerRadius, |
| 211 SkIntToScalar(kBorderWidth), |
| 212 &inner_path); |
| 213 canvas->AsCanvasSkia()->clipPath(inner_path); |
| 214 |
| 215 // Draw bar inner stroke |
| 216 StrokeRoundRect(canvas, |
| 217 kBorderWidth, kBorderWidth, |
| 218 progress_width - 2 * kBorderWidth, |
| 219 height() - 2 * kBorderWidth, |
| 220 kCornerRadius - kBorderWidth, |
| 221 bar_inner_border_color, |
| 222 kBorderWidth); |
| 223 |
| 224 // Draw bar inner shadow |
| 225 StrokeRoundRect(canvas, |
| 226 0, kBorderWidth, progress_width, height(), |
| 227 kCornerRadius, |
| 228 bar_inner_shadow_color, |
| 229 kBorderWidth); |
| 230 |
| 231 canvas->AsCanvasSkia()->restore(); |
| 232 } |
| 233 |
| 234 // Draw bar stroke |
| 235 StrokeRoundRect(canvas, |
| 236 0, 0, progress_width, height(), |
| 237 kCornerRadius, |
| 238 bar_outer_color, |
| 239 kBorderWidth); |
| 240 } |
| 241 #else |
| 117 SkColor bar_color_start = SkColorSetRGB(81, 138, 223); | 242 SkColor bar_color_start = SkColorSetRGB(81, 138, 223); |
| 118 SkColor bar_color_end = SkColorSetRGB(51, 103, 205); | 243 SkColor bar_color_end = SkColorSetRGB(51, 103, 205); |
| 119 SkColor background_color_start = SkColorSetRGB(212, 212, 212); | 244 SkColor background_color_start = SkColorSetRGB(212, 212, 212); |
| 120 SkColor background_color_end = SkColorSetRGB(252, 252, 252); | 245 SkColor background_color_end = SkColorSetRGB(252, 252, 252); |
| 121 SkColor border_color = SkColorSetRGB(144, 144, 144); | 246 SkColor border_color = SkColorSetRGB(144, 144, 144); |
| 122 | 247 |
| 123 FillRoundRect(canvas, | 248 FillRoundRect(canvas, |
| 124 0, 0, width(), height(), | 249 0, 0, width(), height(), |
| 125 kCornerRadius, | 250 kCornerRadius, |
| 126 background_color_start, | 251 background_color_start, |
| 127 background_color_end, | 252 background_color_end, |
| 128 false); | 253 false); |
| 129 if (progress_ * width() > 1) { | 254 if (progress_ * width() > 1) { |
| 130 FillRoundRect(canvas, | 255 FillRoundRect(canvas, |
| 131 0, 0, | 256 0, 0, |
| 132 progress_ * width() / kMaxProgress, height(), | 257 progress_ * width() / kMaxProgress, height(), |
| 133 kCornerRadius, | 258 kCornerRadius, |
| 134 bar_color_start, | 259 bar_color_start, |
| 135 bar_color_end, | 260 bar_color_end, |
| 136 false); | 261 false); |
| 137 } | 262 } |
| 138 StrokeRoundRect(canvas, | 263 StrokeRoundRect(canvas, |
| 139 0, 0, | 264 0, 0, |
| 140 width(), height(), | 265 width(), height(), |
| 141 kCornerRadius, | 266 kCornerRadius, |
| 142 border_color, | 267 border_color, |
| 143 kBorderWidth); | 268 kBorderWidth); |
| 269 #endif |
| 144 } | 270 } |
| 145 | 271 |
| 146 std::string ProgressBar::GetClassName() const { | 272 std::string ProgressBar::GetClassName() const { |
| 147 return kViewClassName; | 273 return kViewClassName; |
| 148 } | 274 } |
| 149 | 275 |
| 150 void ProgressBar::SetProgress(int progress) { | 276 void ProgressBar::SetProgress(int progress) { |
| 151 progress_ = progress; | 277 progress_ = progress; |
| 152 if (progress_ < 0) | 278 if (progress_ < 0) |
| 153 progress_ = 0; | 279 progress_ = 0; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 311 |
| 186 AccessibilityTypes::Role ProgressBar::GetAccessibleRole() { | 312 AccessibilityTypes::Role ProgressBar::GetAccessibleRole() { |
| 187 return AccessibilityTypes::ROLE_PROGRESSBAR; | 313 return AccessibilityTypes::ROLE_PROGRESSBAR; |
| 188 } | 314 } |
| 189 | 315 |
| 190 AccessibilityTypes::State ProgressBar::GetAccessibleState() { | 316 AccessibilityTypes::State ProgressBar::GetAccessibleState() { |
| 191 return AccessibilityTypes::STATE_READONLY; | 317 return AccessibilityTypes::STATE_READONLY; |
| 192 } | 318 } |
| 193 | 319 |
| 194 } // namespace views | 320 } // namespace views |
| OLD | NEW |