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_
This diff is collapsed.
...@@ -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