Commit 3010ae58 authored by Steven Bennetts's avatar Steven Bennetts Committed by Commit Bot

Move NetworkIconImageSource and SignalStrengthImageSource to ash/public

This allows the image source to be available to Tether notifications.

This also adds kTrayImageBgAlpha to ash_constants.h for use in all
tray icon code.

Bug: 826370
Change-Id: Ib3acedfc9c87e2e6f826cc6d2190a3be0a20eba8
Reviewed-on: https://chromium-review.googlesource.com/1082655Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Reviewed-by: default avatarJames Cook <jamescook@chromium.org>
Commit-Queue: Steven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#564903}
parent d5bec62f
...@@ -61,6 +61,8 @@ component("cpp") { ...@@ -61,6 +61,8 @@ component("cpp") {
"menu_utils.h", "menu_utils.h",
"mus_property_mirror_ash.cc", "mus_property_mirror_ash.cc",
"mus_property_mirror_ash.h", "mus_property_mirror_ash.h",
"network_icon_image_source.cc",
"network_icon_image_source.h",
"power_utils.cc", "power_utils.cc",
"power_utils.h", "power_utils.h",
"remote_shelf_item_delegate.cc", "remote_shelf_item_delegate.cc",
......
// Copyright 2018 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 "ash/public/cpp/network_icon_image_source.h"
#include "ash/public/cpp/ash_constants.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/skia_util.h"
#include "ui/gfx/vector_icon_types.h"
namespace ash {
namespace network_icon {
namespace {
// Padding between outside of icon and edge of the canvas, in dp. This value
// stays the same regardless of the canvas size.
constexpr int kSignalStrengthImageInset = 2;
// TODO(estade): share this alpha with other things in ash (battery, etc.).
// See https://crbug.com/623987 and https://crbug.com/632827
constexpr int kSignalStrengthImageBgAlpha = 0x4D;
SkPath CreateArcPath(gfx::RectF oval, float start_angle, float sweep_angle) {
SkPath path;
path.setIsVolatile(true);
path.setFillType(SkPath::kWinding_FillType);
path.moveTo(oval.CenterPoint().x(), oval.CenterPoint().y());
path.arcTo(gfx::RectFToSkRect(oval), start_angle, sweep_angle, false);
path.close();
return path;
}
} // namespace
//------------------------------------------------------------------------------
// NetworkIconImageSource
NetworkIconImageSource::NetworkIconImageSource(const gfx::Size& size,
const gfx::ImageSkia& icon,
const Badges& badges)
: CanvasImageSource(size, false), icon_(icon), badges_(badges) {}
NetworkIconImageSource::~NetworkIconImageSource() = default;
void NetworkIconImageSource::Draw(gfx::Canvas* canvas) {
const int width = size().width();
const int height = size().height();
// The base icon is centered in both dimensions.
const int icon_x = (width - icon_.width()) / 2;
const int icon_y = (height - icon_.height()) / 2;
canvas->DrawImageInt(icon_, icon_x, icon_y);
auto paint_badge = [&canvas](const Badge& badge, int x, int y,
int badge_size = 0) {
gfx::ScopedCanvas scoped(canvas);
canvas->Translate(gfx::Vector2d(x, y));
if (badge_size)
gfx::PaintVectorIcon(canvas, *badge.icon, badge_size, badge.color);
else
gfx::PaintVectorIcon(canvas, *badge.icon, badge.color);
};
// The center badge is scaled and centered over the icon.
if (badges_.center.icon)
paint_badge(badges_.center, icon_x, icon_y, icon_.width());
// The other badges are flush against the edges of the canvas, except at the
// top, where the badge is only 1dp higher than the base image.
const int top_badge_y = icon_y - 1;
if (badges_.top_left.icon)
paint_badge(badges_.top_left, 0, top_badge_y);
if (badges_.bottom_left.icon) {
paint_badge(
badges_.bottom_left, 0,
height - gfx::GetDefaultSizeOfVectorIcon(*badges_.bottom_left.icon));
}
if (badges_.bottom_right.icon) {
const int badge_size =
gfx::GetDefaultSizeOfVectorIcon(*badges_.bottom_right.icon);
paint_badge(badges_.bottom_right, width - badge_size, height - badge_size);
}
}
bool NetworkIconImageSource::HasRepresentationAtAllScales() const {
return true;
}
//------------------------------------------------------------------------------
// SignalStrengthImageSource
SignalStrengthImageSource::SignalStrengthImageSource(ImageType image_type,
SkColor color,
const gfx::Size& size,
int signal_strength)
: CanvasImageSource(size, false /* is_opaque */),
image_type_(image_type),
color_(color),
signal_strength_(signal_strength) {
if (image_type_ == NONE)
image_type_ = ARCS;
DCHECK_GE(signal_strength, 0);
DCHECK_LT(signal_strength, kNumNetworkImages);
}
SignalStrengthImageSource::~SignalStrengthImageSource() = default;
// gfx::CanvasImageSource:
void SignalStrengthImageSource::Draw(gfx::Canvas* canvas) {
if (image_type_ == ARCS)
DrawArcs(canvas);
else
DrawBars(canvas);
}
bool SignalStrengthImageSource::HasRepresentationAtAllScales() const {
return true;
}
void SignalStrengthImageSource::DrawArcs(gfx::Canvas* canvas) {
gfx::RectF oval_bounds((gfx::Rect(size())));
oval_bounds.Inset(gfx::Insets(kSignalStrengthImageInset));
// Double the width and height. The new midpoint should be the former
// bottom center.
oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2,
-oval_bounds.height());
constexpr SkScalar kAngleAboveHorizontal = 51.f;
constexpr SkScalar kStartAngle = 180.f + kAngleAboveHorizontal;
constexpr SkScalar kSweepAngle = 180.f - 2 * kAngleAboveHorizontal;
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
// Background. Skip drawing for full signal.
if (signal_strength_ != kNumNetworkImages - 1) {
flags.setColor(SkColorSetA(color_, kSignalStrengthImageBgAlpha));
canvas->sk_canvas()->drawPath(
CreateArcPath(oval_bounds, kStartAngle, kSweepAngle), flags);
}
// Foreground (signal strength).
if (signal_strength_ != 0) {
flags.setColor(color_);
// Percent of the height of the background wedge that we draw the
// foreground wedge, indexed by signal strength.
static constexpr float kWedgeHeightPercentages[] = {0.f, 0.375f, 0.5833f,
0.75f, 1.f};
const float wedge_percent = kWedgeHeightPercentages[signal_strength_];
oval_bounds.Inset(
gfx::InsetsF((oval_bounds.height() / 2) * (1.f - wedge_percent)));
canvas->sk_canvas()->drawPath(
CreateArcPath(oval_bounds, kStartAngle, kSweepAngle), flags);
}
}
void SignalStrengthImageSource::DrawBars(gfx::Canvas* canvas) {
// Undo the canvas's device scaling and round values to the nearest whole
// number so we can draw on exact pixel boundaries.
const float dsf = canvas->UndoDeviceScaleFactor();
auto scale = [dsf](SkScalar dimension) {
return std::round(dimension * dsf);
};
// Length of short side of an isosceles right triangle, in dip.
const SkScalar kFullTriangleSide =
SkIntToScalar(size().width()) - kSignalStrengthImageInset * 2;
auto make_triangle = [scale, kFullTriangleSide](SkScalar side) {
SkPath triangle;
triangle.moveTo(scale(kSignalStrengthImageInset),
scale(kSignalStrengthImageInset + kFullTriangleSide));
triangle.rLineTo(scale(side), 0);
triangle.rLineTo(0, -scale(side));
triangle.close();
return triangle;
};
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
// Background. Skip drawing for full signal.
if (signal_strength_ != kNumNetworkImages - 1) {
flags.setColor(SkColorSetA(color_, kSignalStrengthImageBgAlpha));
canvas->DrawPath(make_triangle(kFullTriangleSide), flags);
}
// Foreground (signal strength).
if (signal_strength_ != 0) {
flags.setColor(color_);
// As a percentage of the bg triangle, the length of one of the short
// sides of the fg triangle, indexed by signal strength.
static constexpr float kTriangleSidePercents[] = {0.f, 0.375f, 0.5833f,
0.75f, 1.f};
canvas->DrawPath(make_triangle(kTriangleSidePercents[signal_strength_] *
kFullTriangleSide),
flags);
}
}
} // namespace network_icon
} // namespace ash
// Copyright 2018 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.
#ifndef ASH_PUBLIC_CPP_NETWORK_ICON_IMAGE_SOURCE_H_
#define ASH_PUBLIC_CPP_NETWORK_ICON_IMAGE_SOURCE_H_
#include "ash/public/cpp/ash_public_export.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/canvas_image_source.h"
#include "ui/gfx/image/image_skia.h"
namespace gfx {
struct VectorIcon;
}
namespace ash {
namespace network_icon {
// Number of images for signal strength arcs or bars for wireless networks.
constexpr int kNumNetworkImages = 5;
// 'NONE' will default to ARCS behavior where appropriate (e.g. no network).
enum ImageType { ARCS, BARS, NONE };
// Describes a single badge which is defined by a vector icon.
struct Badge {
bool operator==(const Badge& other) const {
return other.icon == icon && other.color == color;
}
bool operator!=(const Badge& other) const { return !(other == *this); }
const gfx::VectorIcon* icon = nullptr;
SkColor color;
};
// Struct to pass a collection of badges to NetworkIconImageSource.
struct Badges {
Badge top_left = {};
Badge center = {};
Badge bottom_left = {};
Badge bottom_right = {};
};
// Provides an image source for assembling a network icons.
class ASH_PUBLIC_EXPORT NetworkIconImageSource : public gfx::CanvasImageSource {
public:
NetworkIconImageSource(const gfx::Size& size,
const gfx::ImageSkia& icon,
const Badges& badges);
~NetworkIconImageSource() override;
// gfx::CanvasImageSource:
void Draw(gfx::Canvas* canvas) override;
bool HasRepresentationAtAllScales() const override;
private:
const gfx::ImageSkia icon_;
const Badges badges_;
DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource);
};
// Provides an image source for wireless signal strength icons.
class ASH_PUBLIC_EXPORT SignalStrengthImageSource
: public gfx::CanvasImageSource {
public:
SignalStrengthImageSource(ImageType image_type,
SkColor color,
const gfx::Size& size,
int signal_strength);
~SignalStrengthImageSource() override;
// gfx::CanvasImageSource:
void Draw(gfx::Canvas* canvas) override;
bool HasRepresentationAtAllScales() const override;
private:
void DrawArcs(gfx::Canvas* canvas);
void DrawBars(gfx::Canvas* canvas);
ImageType image_type_;
SkColor color_;
// On a scale of 0 to kNumNetworkImages - 1, how connected we are.
int signal_strength_;
DISALLOW_COPY_AND_ASSIGN(SignalStrengthImageSource);
};
} // namespace network_icon
} // namespace ash
#endif // ASH_PUBLIC_CPP_NETWORK_ICON_IMAGE_SOURCE_H_
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "ash/system/network/network_icon.h" #include "ash/system/network/network_icon.h"
#include "ash/public/cpp/ash_features.h" #include "ash/public/cpp/ash_features.h"
#include "ash/public/cpp/network_icon_image_source.h"
#include "ash/resources/vector_icons/vector_icons.h" #include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h" #include "ash/strings/grit/ash_strings.h"
#include "ash/system/network/network_icon_animation.h" #include "ash/system/network/network_icon_animation.h"
...@@ -20,17 +21,10 @@ ...@@ -20,17 +21,10 @@
#include "chromeos/network/portal_detector/network_portal_detector.h" #include "chromeos/network/portal_detector/network_portal_detector.h"
#include "chromeos/network/tether_constants.h" #include "chromeos/network/tether_constants.h"
#include "third_party/cros_system_api/dbus/service_constants.h" #include "third_party/cros_system_api/dbus/service_constants.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/image/image_skia_operations.h" #include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_source.h" #include "ui/gfx/image/image_skia_source.h"
#include "ui/gfx/paint_vector_icon.h" #include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/skia_util.h" #include "ui/gfx/skia_util.h"
#include "ui/gfx/vector_icon_types.h" #include "ui/gfx/vector_icon_types.h"
...@@ -47,16 +41,6 @@ namespace network_icon { ...@@ -47,16 +41,6 @@ namespace network_icon {
namespace { namespace {
SkPath CreateArcPath(gfx::RectF oval, float start_angle, float sweep_angle) {
SkPath path;
path.setIsVolatile(true);
path.setFillType(SkPath::kWinding_FillType);
path.moveTo(oval.CenterPoint().x(), oval.CenterPoint().y());
path.arcTo(gfx::RectFToSkRect(oval), start_angle, sweep_angle, false);
path.close();
return path;
}
// Constants for offseting the badge displayed on top of the signal strength // Constants for offseting the badge displayed on top of the signal strength
// icon. The badge will extend outside of the base icon bounds by these amounts. // icon. The badge will extend outside of the base icon bounds by these amounts.
// All values are in dp. // All values are in dp.
...@@ -66,26 +50,6 @@ SkPath CreateArcPath(gfx::RectF oval, float start_angle, float sweep_angle) { ...@@ -66,26 +50,6 @@ SkPath CreateArcPath(gfx::RectF oval, float start_angle, float sweep_angle) {
const int kTrayIconBadgeOffset = 3; const int kTrayIconBadgeOffset = 3;
const int kMenuIconBadgeOffset = 2; const int kMenuIconBadgeOffset = 2;
// Describes a single badge which is defined by a vector icon.
struct Badge {
bool operator==(const Badge& other) const {
return other.icon == icon && other.color == color;
}
bool operator!=(const Badge& other) const { return !(other == *this); }
const gfx::VectorIcon* icon;
SkColor color;
};
//------------------------------------------------------------------------------
// Struct to pass a collection of badges to NetworkIconImageSource.
struct Badges {
Badge top_left = {};
Badge center = {};
Badge bottom_left = {};
Badge bottom_right = {};
};
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// class used for maintaining a map of network state and images. // class used for maintaining a map of network state and images.
class NetworkIconImpl { class NetworkIconImpl {
...@@ -197,21 +161,9 @@ void PurgeIconMap(IconType icon_type, ...@@ -197,21 +161,9 @@ void PurgeIconMap(IconType icon_type,
// Amount to fade icons while connecting. // Amount to fade icons while connecting.
const double kConnectingImageAlpha = 0.5; const double kConnectingImageAlpha = 0.5;
// Images for strength arcs for wireless networks or strength bars for cellular
// networks.
const int kNumNetworkImages = 5;
// Number of discrete images to use for alpha fade animation // Number of discrete images to use for alpha fade animation
const int kNumFadeImages = 10; const int kNumFadeImages = 10;
// Padding between outside of icon and edge of the canvas, in dp. This value
// stays the same regardless of the canvas size.
static constexpr int kSignalStrengthImageInset = 2;
// TODO(estade): share this alpha with other things in ash (battery, etc.).
// See crbug.com/623987 and crbug.com/632827
static constexpr int kSignalStrengthImageBgAlpha = 0x4D;
SkColor GetDefaultColorForIconType(IconType icon_type) { SkColor GetDefaultColorForIconType(IconType icon_type) {
if (icon_type == ICON_TYPE_TRAY) if (icon_type == ICON_TYPE_TRAY)
return kTrayIconColor; return kTrayIconColor;
...@@ -229,81 +181,20 @@ bool IconTypeHasVPNBadge(IconType icon_type) { ...@@ -229,81 +181,20 @@ bool IconTypeHasVPNBadge(IconType icon_type) {
return (icon_type != ICON_TYPE_LIST && icon_type != ICON_TYPE_MENU_LIST); return (icon_type != ICON_TYPE_LIST && icon_type != ICON_TYPE_MENU_LIST);
} }
// This defines how we assemble a network icon. gfx::Size GetSizeForBaseIconSize(const gfx::Size& base_icon_size) {
class NetworkIconImageSource : public gfx::CanvasImageSource { gfx::Size size = base_icon_size;
public: const int badge_offset = base_icon_size.width() == kTrayIconSize
static gfx::ImageSkia CreateImage(const gfx::ImageSkia& icon, ? kTrayIconBadgeOffset
const Badges& badges) { : kMenuIconBadgeOffset;
auto* source = new NetworkIconImageSource(icon, badges); size.Enlarge(badge_offset * 2, badge_offset * 2);
return gfx::ImageSkia(base::WrapUnique(source), source->size()); return size;
} }
~NetworkIconImageSource() override = default;
// gfx::CanvasImageSource:
void Draw(gfx::Canvas* canvas) override {
const int width = size().width();
const int height = size().height();
// The base icon is centered in both dimensions.
const int icon_x = (width - icon_.width()) / 2;
const int icon_y = (height - icon_.height()) / 2;
canvas->DrawImageInt(icon_, icon_x, icon_y);
auto paint_badge = [&canvas](const Badge& badge, int x, int y,
int badge_size = 0) {
gfx::ScopedCanvas scoped(canvas);
canvas->Translate(gfx::Vector2d(x, y));
if (badge_size)
gfx::PaintVectorIcon(canvas, *badge.icon, badge_size, badge.color);
else
gfx::PaintVectorIcon(canvas, *badge.icon, badge.color);
};
// The center badge is scaled and centered over the icon.
if (badges_.center.icon)
paint_badge(badges_.center, icon_x, icon_y, icon_.width());
// The other badges are flush against the edges of the canvas, except at the
// top, where the badge is only 1dp higher than the base image.
const int top_badge_y = icon_y - 1;
if (badges_.top_left.icon)
paint_badge(badges_.top_left, 0, top_badge_y);
if (badges_.bottom_left.icon) {
paint_badge(
badges_.bottom_left, 0,
height - gfx::GetDefaultSizeOfVectorIcon(*badges_.bottom_left.icon));
}
if (badges_.bottom_right.icon) {
const int badge_size =
gfx::GetDefaultSizeOfVectorIcon(*badges_.bottom_right.icon);
paint_badge(badges_.bottom_right, width - badge_size,
height - badge_size);
}
}
bool HasRepresentationAtAllScales() const override { return true; }
private:
NetworkIconImageSource(const gfx::ImageSkia& icon, const Badges& badges)
: CanvasImageSource(GetSizeForBaseIconSize(icon.size()), false),
icon_(icon),
badges_(badges) {}
static gfx::Size GetSizeForBaseIconSize(const gfx::Size& base_icon_size) {
gfx::Size size = base_icon_size;
const int badge_offset = base_icon_size.width() == kTrayIconSize
? kTrayIconBadgeOffset
: kMenuIconBadgeOffset;
size.Enlarge(badge_offset * 2, badge_offset * 2);
return size;
}
const gfx::ImageSkia icon_;
const Badges badges_;
DISALLOW_COPY_AND_ASSIGN(NetworkIconImageSource); gfx::ImageSkia CreateNetworkIconImage(const gfx::ImageSkia& icon,
}; const Badges& badges) {
return gfx::CanvasImageSource::MakeImageSkia<NetworkIconImageSource>(
GetSizeForBaseIconSize(icon.size()), icon, badges);
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Utilities for extracting icon images. // Utilities for extracting icon images.
...@@ -334,12 +225,17 @@ ImageType ImageTypeForNetwork(const NetworkState* network, IconType icon_type) { ...@@ -334,12 +225,17 @@ ImageType ImageTypeForNetwork(const NetworkState* network, IconType icon_type) {
return ImageTypeForNetworkType(GetEffectiveNetworkType(network, icon_type)); return ImageTypeForNetworkType(GetEffectiveNetworkType(network, icon_type));
} }
gfx::Size GetSizeForIconType(IconType icon_type) {
int side = icon_type == ICON_TYPE_TRAY ? kTrayIconSize : kMenuIconSize;
return gfx::Size(side, side);
}
gfx::ImageSkia GetImageForIndex(ImageType image_type, gfx::ImageSkia GetImageForIndex(ImageType image_type,
IconType icon_type, IconType icon_type,
int index) { int index) {
gfx::CanvasImageSource* source = return gfx::CanvasImageSource::MakeImageSkia<SignalStrengthImageSource>(
new SignalStrengthImageSource(image_type, icon_type, index); image_type, GetDefaultColorForIconType(icon_type),
return gfx::ImageSkia(base::WrapUnique(source), source->size()); GetSizeForIconType(icon_type), index);
} }
// Returns an image to represent either a fully connected network or a // Returns an image to represent either a fully connected network or a
...@@ -476,7 +372,7 @@ gfx::ImageSkia GetConnectingVpnImage(IconType icon_type) { ...@@ -476,7 +372,7 @@ gfx::ImageSkia GetConnectingVpnImage(IconType icon_type) {
} else { } else {
icon = ConnectingVpnImage(animation); icon = ConnectingVpnImage(animation);
} }
return NetworkIconImageSource::CreateImage(icon, badges); return CreateNetworkIconImage(icon, badges);
} }
gfx::ImageSkia GetConnectingImage(IconType icon_type, gfx::ImageSkia GetConnectingImage(IconType icon_type,
...@@ -487,7 +383,7 @@ gfx::ImageSkia GetConnectingImage(IconType icon_type, ...@@ -487,7 +383,7 @@ gfx::ImageSkia GetConnectingImage(IconType icon_type,
ImageType image_type = ImageTypeForNetworkType(network_type); ImageType image_type = ImageTypeForNetworkType(network_type);
double animation = NetworkIconAnimation::GetInstance()->GetAnimation(); double animation = NetworkIconAnimation::GetInstance()->GetAnimation();
return NetworkIconImageSource::CreateImage( return CreateNetworkIconImage(
*ConnectingWirelessImage(image_type, icon_type, animation), Badges()); *ConnectingWirelessImage(image_type, icon_type, animation), Badges());
} }
...@@ -611,8 +507,8 @@ void NetworkIconImpl::GetBadges(const NetworkState* network, Badges* badges) { ...@@ -611,8 +507,8 @@ void NetworkIconImpl::GetBadges(const NetworkState* network, Badges* badges) {
const DeviceState* device = const DeviceState* device =
NetworkHandler::Get()->network_state_handler()->GetDeviceState( NetworkHandler::Get()->network_state_handler()->GetDeviceState(
network->device_path()); network->device_path());
LOG_IF(WARNING, !device) << "Could not find device state for " LOG_IF(WARNING, !device)
<< network->device_path(); << "Could not find device state for " << network->device_path();
if (!device || !device->provider_requires_roaming()) { if (!device || !device->provider_requires_roaming()) {
badges->bottom_right = {&kNetworkBadgeRoamingIcon, icon_color}; badges->bottom_right = {&kNetworkBadgeRoamingIcon, icon_color};
} }
...@@ -632,7 +528,7 @@ void NetworkIconImpl::GenerateImage(const NetworkState* network) { ...@@ -632,7 +528,7 @@ void NetworkIconImpl::GenerateImage(const NetworkState* network) {
gfx::ImageSkia icon = GetIcon(network, icon_type_, strength_index_); gfx::ImageSkia icon = GetIcon(network, icon_type_, strength_index_);
Badges badges; Badges badges;
GetBadges(network, &badges); GetBadges(network, &badges);
image_ = NetworkIconImageSource::CreateImage(icon, badges); image_ = CreateNetworkIconImage(icon, badges);
} }
namespace { namespace {
...@@ -658,134 +554,6 @@ NetworkIconImpl* FindAndUpdateImageImpl(const NetworkState* network, ...@@ -658,134 +554,6 @@ NetworkIconImpl* FindAndUpdateImageImpl(const NetworkState* network,
} // namespace } // namespace
//------------------------------------------------------------------------------
// SignalStrengthImageSource
SignalStrengthImageSource::SignalStrengthImageSource(ImageType image_type,
SkColor color,
const gfx::Size& size,
int signal_strength)
: CanvasImageSource(size, false),
image_type_(image_type /* is_opaque */),
color_(color),
signal_strength_(signal_strength) {
if (image_type_ == NONE)
image_type_ = ARCS;
DCHECK_GE(signal_strength, 0);
DCHECK_LT(signal_strength, kNumNetworkImages);
}
SignalStrengthImageSource::SignalStrengthImageSource(ImageType image_type,
IconType icon_type,
int signal_strength)
: SignalStrengthImageSource(image_type,
GetDefaultColorForIconType(icon_type),
GetSizeForIconType(icon_type),
signal_strength) {}
SignalStrengthImageSource::~SignalStrengthImageSource() = default;
void SignalStrengthImageSource::set_color(SkColor color) {
color_ = color;
}
// gfx::CanvasImageSource:
void SignalStrengthImageSource::Draw(gfx::Canvas* canvas) {
if (image_type_ == ARCS)
DrawArcs(canvas);
else
DrawBars(canvas);
}
bool SignalStrengthImageSource::HasRepresentationAtAllScales() const {
return true;
}
gfx::Size SignalStrengthImageSource::GetSizeForIconType(IconType icon_type) {
int side = icon_type == ICON_TYPE_TRAY ? kTrayIconSize : kMenuIconSize;
return gfx::Size(side, side);
}
void SignalStrengthImageSource::DrawArcs(gfx::Canvas* canvas) {
gfx::RectF oval_bounds((gfx::Rect(size())));
oval_bounds.Inset(gfx::Insets(kSignalStrengthImageInset));
// Double the width and height. The new midpoint should be the former
// bottom center.
oval_bounds.Inset(-oval_bounds.width() / 2, 0, -oval_bounds.width() / 2,
-oval_bounds.height());
const SkScalar kAngleAboveHorizontal = 51.f;
const SkScalar kStartAngle = 180.f + kAngleAboveHorizontal;
const SkScalar kSweepAngle = 180.f - 2 * kAngleAboveHorizontal;
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
// Background. Skip drawing for full signal.
if (signal_strength_ != kNumNetworkImages - 1) {
flags.setColor(SkColorSetA(color_, kSignalStrengthImageBgAlpha));
canvas->sk_canvas()->drawPath(
CreateArcPath(oval_bounds, kStartAngle, kSweepAngle), flags);
}
// Foreground (signal strength).
if (signal_strength_ != 0) {
flags.setColor(color_);
// Percent of the height of the background wedge that we draw the
// foreground wedge, indexed by signal strength.
static constexpr float kWedgeHeightPercentages[] = {0.f, 0.375f, 0.5833f,
0.75f, 1.f};
const float wedge_percent = kWedgeHeightPercentages[signal_strength_];
oval_bounds.Inset(
gfx::InsetsF((oval_bounds.height() / 2) * (1.f - wedge_percent)));
canvas->sk_canvas()->drawPath(
CreateArcPath(oval_bounds, kStartAngle, kSweepAngle), flags);
}
}
void SignalStrengthImageSource::DrawBars(gfx::Canvas* canvas) {
// Undo the canvas's device scaling and round values to the nearest whole
// number so we can draw on exact pixel boundaries.
const float dsf = canvas->UndoDeviceScaleFactor();
auto scale = [dsf](SkScalar dimension) {
return std::round(dimension * dsf);
};
// Length of short side of an isosceles right triangle, in dip.
const SkScalar kFullTriangleSide =
SkIntToScalar(size().width()) - kSignalStrengthImageInset * 2;
auto make_triangle = [scale, kFullTriangleSide](SkScalar side) {
SkPath triangle;
triangle.moveTo(scale(kSignalStrengthImageInset),
scale(kSignalStrengthImageInset + kFullTriangleSide));
triangle.rLineTo(scale(side), 0);
triangle.rLineTo(0, -scale(side));
triangle.close();
return triangle;
};
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setStyle(cc::PaintFlags::kFill_Style);
// Background. Skip drawing for full signal.
if (signal_strength_ != kNumNetworkImages - 1) {
flags.setColor(SkColorSetA(color_, kSignalStrengthImageBgAlpha));
canvas->DrawPath(make_triangle(kFullTriangleSide), flags);
}
// Foreground (signal strength).
if (signal_strength_ != 0) {
flags.setColor(color_);
// As a percentage of the bg triangle, the length of one of the short
// sides of the fg triangle, indexed by signal strength.
static constexpr float kTriangleSidePercents[] = {0.f, 0.375f, 0.5833f,
0.75f, 1.f};
canvas->DrawPath(make_triangle(kTriangleSidePercents[signal_strength_] *
kFullTriangleSide),
flags);
}
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Public interface // Public interface
...@@ -812,7 +580,7 @@ gfx::ImageSkia GetImageForWiFiEnabledState(bool enabled, IconType icon_type) { ...@@ -812,7 +580,7 @@ gfx::ImageSkia GetImageForWiFiEnabledState(bool enabled, IconType icon_type) {
badges.center = {&kNetworkBadgeOffIcon, badges.center = {&kNetworkBadgeOffIcon,
GetDefaultColorForIconType(icon_type)}; GetDefaultColorForIconType(icon_type)};
} }
return NetworkIconImageSource::CreateImage(image, badges); return CreateNetworkIconImage(image, badges);
} }
gfx::ImageSkia GetImageForDisconnectedCellNetwork() { gfx::ImageSkia GetImageForDisconnectedCellNetwork() {
...@@ -822,15 +590,13 @@ gfx::ImageSkia GetImageForDisconnectedCellNetwork() { ...@@ -822,15 +590,13 @@ gfx::ImageSkia GetImageForDisconnectedCellNetwork() {
gfx::ImageSkia GetImageForNewWifiNetwork(SkColor icon_color, gfx::ImageSkia GetImageForNewWifiNetwork(SkColor icon_color,
SkColor badge_color) { SkColor badge_color) {
SignalStrengthImageSource* source =
new SignalStrengthImageSource(ImageTypeForNetworkType(shill::kTypeWifi),
ICON_TYPE_LIST, kNumNetworkImages - 1);
source->set_color(icon_color);
gfx::ImageSkia icon = gfx::ImageSkia icon =
gfx::ImageSkia(base::WrapUnique(source), source->size()); gfx::CanvasImageSource::MakeImageSkia<SignalStrengthImageSource>(
ImageTypeForNetworkType(shill::kTypeWifi), icon_color,
GetSizeForIconType(ICON_TYPE_LIST), kNumNetworkImages - 1);
Badges badges; Badges badges;
badges.bottom_right = {&kNetworkBadgeAddOtherIcon, badge_color}; badges.bottom_right = {&kNetworkBadgeAddOtherIcon, badge_color};
return NetworkIconImageSource::CreateImage(icon, badges); return CreateNetworkIconImage(icon, badges);
} }
base::string16 GetLabelForNetwork(const chromeos::NetworkState* network, base::string16 GetLabelForNetwork(const chromeos::NetworkState* network,
......
...@@ -28,49 +28,9 @@ enum IconType { ...@@ -28,49 +28,9 @@ enum IconType {
ICON_TYPE_MENU_LIST, // dark icons without VPN badges; separate status ICON_TYPE_MENU_LIST, // dark icons without VPN badges; separate status
}; };
// 'NONE' will default to ARCS behavior where appropriate (e.g. no network or
// if a new type gets added).
enum ImageType { ARCS, BARS, NONE };
// Strength of a wireless signal. // Strength of a wireless signal.
enum class SignalStrength { NONE, WEAK, MEDIUM, STRONG, NOT_WIRELESS }; enum class SignalStrength { NONE, WEAK, MEDIUM, STRONG, NOT_WIRELESS };
// Depicts a given signal strength using arcs (e.g. for WiFi connections) or
// bars (e.g. for cell connections).
class SignalStrengthImageSource : public gfx::CanvasImageSource {
public:
ASH_EXPORT SignalStrengthImageSource(ImageType image_type,
SkColor color,
const gfx::Size& size,
int signal_strength);
// This version intuits color and size from icon_type.
ASH_EXPORT SignalStrengthImageSource(ImageType image_type,
IconType icon_type,
int signal_strength);
~SignalStrengthImageSource() override;
void set_color(SkColor color);
// gfx::CanvasImageSource:
void Draw(gfx::Canvas* canvas) override;
bool HasRepresentationAtAllScales() const override;
private:
static gfx::Size GetSizeForIconType(IconType icon_type);
void DrawArcs(gfx::Canvas* canvas);
void DrawBars(gfx::Canvas* canvas);
ImageType image_type_;
SkColor color_;
// On a scale of 0 to kNumNetworkImages - 1, how connected we are.
int signal_strength_;
DISALLOW_COPY_AND_ASSIGN(SignalStrengthImageSource);
};
// Gets the image for provided |network|. |network| must not be NULL. // Gets the image for provided |network|. |network| must not be NULL.
// |icon_type| determines the color theme and whether or not to show the VPN // |icon_type| determines the color theme and whether or not to show the VPN
// badge. This caches badged icons per network per |icon_type|. // badge. This caches badged icons per network per |icon_type|.
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "chrome/browser/ui/ash/auto_connect_notifier.h" #include "chrome/browser/ui/ash/auto_connect_notifier.h"
#include "ash/system/network/network_icon.h" #include "ash/public/cpp/network_icon_image_source.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/ui/ash/network/tether_notification_presenter.h" #include "chrome/browser/ui/ash/network/tether_notification_presenter.h"
#include "ash/public/cpp/network_icon_image_source.h"
#include "ash/public/cpp/vector_icons/vector_icons.h" #include "ash/public/cpp/vector_icons/vector_icons.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "ash/system/network/network_icon.h"
#include "base/callback.h" #include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment