network_icon_image_source.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2018 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.
  4. #ifndef ASH_PUBLIC_CPP_NETWORK_ICON_IMAGE_SOURCE_H_
  5. #define ASH_PUBLIC_CPP_NETWORK_ICON_IMAGE_SOURCE_H_
  6. #include "ash/public/cpp/ash_public_export.h"
  7. #include "third_party/skia/include/core/SkColor.h"
  8. #include "ui/gfx/canvas.h"
  9. #include "ui/gfx/color_palette.h"
  10. #include "ui/gfx/geometry/size.h"
  11. #include "ui/gfx/image/canvas_image_source.h"
  12. #include "ui/gfx/image/image_skia.h"
  13. namespace gfx {
  14. struct VectorIcon;
  15. }
  16. namespace ash {
  17. namespace network_icon {
  18. // Number of images for signal strength arcs or bars for wireless networks.
  19. constexpr int kNumNetworkImages = 5;
  20. // 'NONE' will default to ARCS behavior where appropriate (e.g. no network).
  21. enum ImageType { ARCS, BARS, NONE };
  22. // Describes a single badge which is defined by a vector icon.
  23. struct Badge {
  24. bool operator==(const Badge& other) const {
  25. return other.icon == icon && other.color == color;
  26. }
  27. bool operator!=(const Badge& other) const { return !(other == *this); }
  28. const gfx::VectorIcon* icon = nullptr;
  29. SkColor color = gfx::kPlaceholderColor;
  30. };
  31. // Struct to pass a collection of badges to NetworkIconImageSource.
  32. struct Badges {
  33. Badge top_left;
  34. Badge center;
  35. Badge bottom_left;
  36. Badge bottom_right;
  37. };
  38. // Provides an image source for assembling a network icons.
  39. class ASH_PUBLIC_EXPORT NetworkIconImageSource : public gfx::CanvasImageSource {
  40. public:
  41. NetworkIconImageSource(const gfx::Size& size,
  42. const gfx::ImageSkia& icon,
  43. const Badges& badges);
  44. NetworkIconImageSource(const NetworkIconImageSource&) = delete;
  45. NetworkIconImageSource& operator=(const NetworkIconImageSource&) = delete;
  46. ~NetworkIconImageSource() override;
  47. // gfx::CanvasImageSource:
  48. void Draw(gfx::Canvas* canvas) override;
  49. bool HasRepresentationAtAllScales() const override;
  50. private:
  51. const gfx::ImageSkia icon_;
  52. const Badges badges_;
  53. };
  54. // Provides an image source for wireless signal strength icons.
  55. class ASH_PUBLIC_EXPORT SignalStrengthImageSource
  56. : public gfx::CanvasImageSource {
  57. public:
  58. SignalStrengthImageSource(ImageType image_type,
  59. SkColor color,
  60. const gfx::Size& size,
  61. int signal_strength,
  62. int padding = 2);
  63. SignalStrengthImageSource(const SignalStrengthImageSource&) = delete;
  64. SignalStrengthImageSource& operator=(const SignalStrengthImageSource&) =
  65. delete;
  66. ~SignalStrengthImageSource() override;
  67. // gfx::CanvasImageSource:
  68. void Draw(gfx::Canvas* canvas) override;
  69. bool HasRepresentationAtAllScales() const override;
  70. private:
  71. void DrawArcs(gfx::Canvas* canvas);
  72. void DrawBars(gfx::Canvas* canvas);
  73. ImageType image_type_;
  74. SkColor color_;
  75. // On a scale of 0 to kNumNetworkImages - 1, how connected we are.
  76. int signal_strength_;
  77. // Padding between outside of icon and edge of the canvas, in dp. This value
  78. // stays the same regardless of the canvas size.
  79. const int padding_;
  80. };
  81. // Returns the sized full strength unbadged image for a Wi-Fi network. Used
  82. // for wireless network notifications.
  83. ASH_PUBLIC_EXPORT gfx::ImageSkia GetImageForWifiNetwork(SkColor color,
  84. gfx::Size size);
  85. } // namespace network_icon
  86. } // namespace ash
  87. #endif // ASH_PUBLIC_CPP_NETWORK_ICON_IMAGE_SOURCE_H_