OLD | NEW |
---|---|
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/chromeos/network/network_icon.h" | 5 #include "ui/chromeos/network/network_icon.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chromeos/network/device_state.h" | 9 #include "chromeos/network/device_state.h" |
10 #include "chromeos/network/network_connection_handler.h" | 10 #include "chromeos/network/network_connection_handler.h" |
11 #include "chromeos/network/network_state.h" | 11 #include "chromeos/network/network_state.h" |
12 #include "chromeos/network/network_state_handler.h" | 12 #include "chromeos/network/network_state_handler.h" |
13 #include "chromeos/network/portal_detector/network_portal_detector.h" | 13 #include "chromeos/network/portal_detector/network_portal_detector.h" |
14 #include "grit/ui_chromeos_resources.h" | 14 #include "grit/ui_chromeos_resources.h" |
15 #include "grit/ui_chromeos_strings.h" | 15 #include "grit/ui_chromeos_strings.h" |
16 #include "third_party/cros_system_api/dbus/service_constants.h" | 16 #include "third_party/cros_system_api/dbus/service_constants.h" |
17 #include "third_party/skia/include/core/SkPaint.h" | |
18 #include "third_party/skia/include/core/SkPath.h" | |
17 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
18 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/base/webui/web_ui_util.h" | 21 #include "ui/base/webui/web_ui_util.h" |
20 #include "ui/chromeos/material_design_icon_controller.h" | 22 #include "ui/chromeos/material_design_icon_controller.h" |
21 #include "ui/chromeos/network/network_icon_animation.h" | 23 #include "ui/chromeos/network/network_icon_animation.h" |
22 #include "ui/chromeos/network/network_icon_animation_observer.h" | 24 #include "ui/chromeos/network/network_icon_animation_observer.h" |
23 #include "ui/gfx/canvas.h" | 25 #include "ui/gfx/canvas.h" |
24 #include "ui/gfx/color_palette.h" | 26 #include "ui/gfx/color_palette.h" |
25 #include "ui/gfx/geometry/insets.h" | 27 #include "ui/gfx/geometry/insets.h" |
26 #include "ui/gfx/geometry/rect.h" | 28 #include "ui/gfx/geometry/rect.h" |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 return canvas.ExtractImageRep(); | 267 return canvas.ExtractImageRep(); |
266 } | 268 } |
267 | 269 |
268 private: | 270 private: |
269 const gfx::ImageSkia icon_; | 271 const gfx::ImageSkia icon_; |
270 const Badges badges_; | 272 const Badges badges_; |
271 | 273 |
272 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); | 274 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); |
273 }; | 275 }; |
274 | 276 |
275 // Depicts a given signal strength using arcs (for WiFi connections). | 277 // Depicts a given signal strength using arcs (e.g. for WiFi connections) or |
276 class ArcsImageSource : public gfx::CanvasImageSource { | 278 // bars (e.g. for cell connections). |
279 class NetworkIconImageSourceMd : public gfx::CanvasImageSource { | |
277 public: | 280 public: |
278 ArcsImageSource(IconType icon_type, float signal_strength) | 281 NetworkIconImageSourceMd(ImageType image_type, |
282 IconType icon_type, | |
283 int signal_strength) | |
279 : CanvasImageSource(GetSizeForIconType(icon_type), false), | 284 : CanvasImageSource(GetSizeForIconType(icon_type), false), |
285 image_type_(image_type), | |
280 icon_type_(icon_type), | 286 icon_type_(icon_type), |
281 signal_strength_(signal_strength) { | 287 signal_strength_(signal_strength) { |
288 if (image_type_ == NONE) | |
289 image_type_ = ARCS; | |
290 | |
282 DCHECK_GE(signal_strength, 0); | 291 DCHECK_GE(signal_strength, 0); |
283 DCHECK_LT(signal_strength, kNumArcsImages); | 292 DCHECK_LT(signal_strength, |
293 image_type_ == ARCS ? kNumArcsImages : kNumBarsImages); | |
tdanderson
2016/07/29 21:43:29
Maybe consolidate these two constants into kNumNet
stevenjb
2016/07/29 21:47:05
FWIW, the number of arcs/bars images used to diffe
Evan Stade
2016/07/29 22:25:18
It looks like a lot of simplification is possible
| |
284 } | 294 } |
285 ~ArcsImageSource() override {} | 295 ~NetworkIconImageSourceMd() override {} |
286 | 296 |
287 // gfx::CanvasImageSource: | 297 // gfx::CanvasImageSource: |
288 void Draw(gfx::Canvas* canvas) override { | 298 void Draw(gfx::Canvas* canvas) override { |
299 if (image_type_ == ARCS) | |
300 DrawArcs(canvas); | |
301 else | |
302 DrawBars(canvas); | |
303 } | |
304 | |
305 private: | |
306 void DrawArcs(gfx::Canvas* canvas) { | |
289 gfx::RectF oval_bounds((gfx::Rect(size()))); | 307 gfx::RectF oval_bounds((gfx::Rect(size()))); |
290 // Padding between top of arc and top of canvas. | |
291 const int kIconInset = 2; | |
292 oval_bounds.Inset(gfx::Insets(kIconInset)); | 308 oval_bounds.Inset(gfx::Insets(kIconInset)); |
293 // Double the width and height. The new midpoint should be the former | 309 // Double the width and height. The new midpoint should be the former |
294 // bottom center. | 310 // bottom center. |
295 oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2, | 311 oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2, |
296 -oval_bounds.height()); | 312 -oval_bounds.height()); |
297 | 313 |
298 const SkScalar kAngleAboveHorizontal = 51.f; | 314 const SkScalar kAngleAboveHorizontal = 51.f; |
299 const SkScalar kStartAngle = 180.f + kAngleAboveHorizontal; | 315 const SkScalar kStartAngle = 180.f + kAngleAboveHorizontal; |
300 const SkScalar kSweepAngle = 180.f - 2 * kAngleAboveHorizontal; | 316 const SkScalar kSweepAngle = 180.f - 2 * kAngleAboveHorizontal; |
301 | 317 |
302 SkPaint paint; | 318 SkPaint paint; |
303 paint.setAntiAlias(true); | 319 paint.setAntiAlias(true); |
304 paint.setStyle(SkPaint::kFill_Style); | 320 paint.setStyle(SkPaint::kFill_Style); |
305 const SkColor base_color = GetBaseColorForIconType(icon_type_); | 321 const SkColor base_color = GetBaseColorForIconType(icon_type_); |
306 // Background. Skip drawing for full signal. | 322 // Background. Skip drawing for full signal. |
307 if (signal_strength_ != kNumArcsImages - 1) { | 323 if (signal_strength_ != kNumArcsImages - 1) { |
308 // TODO(estade): share this alpha with other things in ash (battery, | 324 paint.setColor(SkColorSetA(base_color, kBgAlpha)); |
309 // etc.). | |
310 paint.setColor(SkColorSetA(base_color, 0x4D)); | |
311 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), kStartAngle, | 325 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), kStartAngle, |
312 kSweepAngle, true, paint); | 326 kSweepAngle, true, paint); |
313 } | 327 } |
314 // Foreground (signal strength). | 328 // Foreground (signal strength). |
315 if (signal_strength_ != 0) { | 329 if (signal_strength_ != 0) { |
316 paint.setColor(base_color); | 330 paint.setColor(base_color); |
317 // Percent of the height of the background wedge that we draw the | 331 // Percent of the height of the background wedge that we draw the |
318 // foreground wedge, indexed by signal strength. | 332 // foreground wedge, indexed by signal strength. |
319 static const float kWedgeHeightPercentages[] = {0.f, 0.375f, 0.5833f, | 333 static const float kWedgeHeightPercentages[] = {0.f, 0.375f, 0.5833f, |
320 0.75f, 1.f}; | 334 0.75f, 1.f}; |
321 const float wedge_percent = kWedgeHeightPercentages[signal_strength_]; | 335 const float wedge_percent = kWedgeHeightPercentages[signal_strength_]; |
322 oval_bounds.Inset( | 336 oval_bounds.Inset( |
323 gfx::InsetsF((oval_bounds.height() / 2) * (1.f - wedge_percent))); | 337 gfx::InsetsF((oval_bounds.height() / 2) * (1.f - wedge_percent))); |
324 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), kStartAngle, | 338 canvas->sk_canvas()->drawArc(gfx::RectFToSkRect(oval_bounds), kStartAngle, |
325 kSweepAngle, true, paint); | 339 kSweepAngle, true, paint); |
326 } | 340 } |
327 } | 341 } |
328 | 342 |
329 private: | 343 void DrawBars(gfx::Canvas* canvas) { |
344 // Undo the canvas's device scaling and round values to the nearest whole | |
345 // number so we can draw on exact pixel boundaries. | |
346 const float dsf = canvas->UndoDeviceScaleFactor(); | |
347 auto scale = [dsf](int dimension) { return std::round(dimension * dsf); }; | |
348 | |
349 // Length of short side of an isosceles right triangle. | |
350 const SkScalar kFullTriangleSide = | |
351 SkIntToScalar(size().width() - kIconInset * 2); | |
352 | |
353 auto make_triangle = [scale, kFullTriangleSide](SkScalar side) { | |
354 SkPath triangle; | |
355 triangle.moveTo(scale(kIconInset), scale(kIconInset + kFullTriangleSide)); | |
356 triangle.rLineTo(scale(side), 0); | |
357 triangle.rLineTo(0, -scale(side)); | |
358 triangle.close(); | |
359 return triangle; | |
360 }; | |
361 | |
362 SkPaint paint; | |
363 paint.setAntiAlias(true); | |
364 paint.setStyle(SkPaint::kFill_Style); | |
365 const SkColor base_color = GetBaseColorForIconType(icon_type_); | |
366 // Background. Skip drawing for full signal. | |
367 if (signal_strength_ != kNumBarsImages - 1) { | |
368 paint.setColor(SkColorSetA(base_color, kBgAlpha)); | |
369 canvas->DrawPath(make_triangle(kFullTriangleSide), paint); | |
370 } | |
371 // Foreground (signal strength). | |
372 if (signal_strength_ != 0) { | |
373 paint.setColor(base_color); | |
374 // As a percentage of the bg triangle, the length of one of the short | |
375 // sides of the fg triangle, indexed by signal strength. | |
376 static const float kTriangleSidePercents[] = {0.f, 0.5f, 0.625f, 0.75f, | |
377 1.f}; | |
378 canvas->DrawPath(make_triangle(kTriangleSidePercents[signal_strength_] * | |
379 kFullTriangleSide), | |
380 paint); | |
381 } | |
382 } | |
383 | |
384 ImageType image_type_; | |
330 IconType icon_type_; | 385 IconType icon_type_; |
331 // On a scale of 0 to kNumArcsImages - 1, how connected we are. | 386 |
387 // On a scale of 0 to kNum{Arcs,Bars}Images - 1, how connected we are. | |
332 int signal_strength_; | 388 int signal_strength_; |
333 | 389 |
334 DISALLOW_COPY_AND_ASSIGN(ArcsImageSource); | 390 // Padding between outside of icon and edge of the canvas, in dp. This value |
391 // stays the same regardless of the canvas size (which depends on | |
392 // |icon_type_|). | |
393 static constexpr int kIconInset = 2; | |
394 | |
395 // TODO(estade): share this alpha with other things in ash (battery, etc.). | |
tdanderson
2016/07/29 21:43:29
Consider referencing crbug.com/623987
Evan Stade
2016/07/29 22:25:18
Done.
| |
396 static constexpr int kBgAlpha = 0x4D; | |
397 | |
398 DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSourceMd); | |
335 }; | 399 }; |
336 | 400 |
337 //------------------------------------------------------------------------------ | 401 //------------------------------------------------------------------------------ |
338 // Utilities for extracting icon images. | 402 // Utilities for extracting icon images. |
339 | 403 |
340 bool IconTypeIsDark(IconType icon_type) { | 404 bool IconTypeIsDark(IconType icon_type) { |
341 return (icon_type != ICON_TYPE_TRAY); | 405 return (icon_type != ICON_TYPE_TRAY); |
342 } | 406 } |
343 | 407 |
344 bool IconTypeHasVPNBadge(IconType icon_type) { | 408 bool IconTypeHasVPNBadge(IconType icon_type) { |
(...skipping 22 matching lines...) Expand all Loading... | |
367 if (type == shill::kTypeWifi) | 431 if (type == shill::kTypeWifi) |
368 return ARCS; | 432 return ARCS; |
369 else if (type == shill::kTypeCellular || type == shill::kTypeWimax) | 433 else if (type == shill::kTypeCellular || type == shill::kTypeWimax) |
370 return BARS; | 434 return BARS; |
371 return NONE; | 435 return NONE; |
372 } | 436 } |
373 | 437 |
374 gfx::ImageSkia GetImageForIndex(ImageType image_type, | 438 gfx::ImageSkia GetImageForIndex(ImageType image_type, |
375 IconType icon_type, | 439 IconType icon_type, |
376 int index) { | 440 int index) { |
377 if (md_icon_controller::UseMaterialDesignNetworkIcons() && | 441 if (md_icon_controller::UseMaterialDesignNetworkIcons()) { |
378 image_type == ARCS) { | 442 gfx::CanvasImageSource* source = |
379 ArcsImageSource* source = new ArcsImageSource(icon_type, index); | 443 new NetworkIconImageSourceMd(image_type, icon_type, index); |
380 return gfx::ImageSkia(source, source->size()); | 444 return gfx::ImageSkia(source, source->size()); |
381 } | 445 } |
382 | 446 |
383 int num_images = NumImagesForType(image_type); | 447 int num_images = NumImagesForType(image_type); |
384 if (index < 0 || index >= num_images) | 448 if (index < 0 || index >= num_images) |
385 return gfx::ImageSkia(); | 449 return gfx::ImageSkia(); |
386 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); | 450 gfx::ImageSkia* images = BaseImageForType(image_type, icon_type); |
387 int width = images->width(); | 451 int width = images->width(); |
388 int height = images->height() / num_images; | 452 int height = images->height() / num_images; |
389 return gfx::ImageSkiaOperations::ExtractSubset(*images, | 453 return gfx::ImageSkiaOperations::ExtractSubset(*images, |
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
980 iter != networks.end(); ++iter) { | 1044 iter != networks.end(); ++iter) { |
981 network_paths.insert((*iter)->path()); | 1045 network_paths.insert((*iter)->path()); |
982 } | 1046 } |
983 PurgeIconMap(ICON_TYPE_TRAY, network_paths); | 1047 PurgeIconMap(ICON_TYPE_TRAY, network_paths); |
984 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); | 1048 PurgeIconMap(ICON_TYPE_DEFAULT_VIEW, network_paths); |
985 PurgeIconMap(ICON_TYPE_LIST, network_paths); | 1049 PurgeIconMap(ICON_TYPE_LIST, network_paths); |
986 } | 1050 } |
987 | 1051 |
988 } // namespace network_icon | 1052 } // namespace network_icon |
989 } // namespace ui | 1053 } // namespace ui |
OLD | NEW |