Commit 1dcacd3f authored by Bret Sepulveda's avatar Bret Sepulveda Committed by Commit Bot

Reland "Add new class ColorTrackingIconView to deduplicate subclasses."

This is a reland of 85ae69b0. Fixes
namespace conflict with branded builds. Verified by local compile.

Original change's description:
> Add new class ColorTrackingIconView to deduplicate subclasses.
>
> Upon investigating the associated bug, I noticed that there are a few
> subclasses of ImageView that exist solely to override OnThemeChanged.
> Since I also needed a subclass like this to fix the bug, I decided to
> make a utility class to eliminate this ad-hoc subclassing, in the style
> of ColorTrackingVectorImageButton (in image_button_factory.cc).
>
> Bug: 1125510
> Change-Id: I975a9b6a4676bb6b2a454172f211f406934d7869
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2397737
> Commit-Queue: Bret Sepulveda <bsep@chromium.org>
> Reviewed-by: Trent Apted <tapted@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#804901}

Bug: 1125510
Change-Id: Icbe7240ab8159f1c61f510f03dab7e5d6c5d563b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2401018
Commit-Queue: Bret Sepulveda <bsep@chromium.org>
Auto-Submit: Bret Sepulveda <bsep@chromium.org>
Reviewed-by: default avatarTrent Apted <tapted@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805751}
parent 9fa4ad1b
......@@ -10,7 +10,6 @@
#include "base/strings/utf_string_conversions.h"
#include "build/branding_buildflags.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/media_router/cast_dialog_helper.h"
#include "chrome/grit/generated_resources.h"
......@@ -21,11 +20,11 @@
#include "ui/base/ui_base_types.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/animation/ink_drop_impl.h"
#include "ui/views/controls/color_tracking_icon_view.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/controls/throbber.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/vector_icons.h"
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
......@@ -36,25 +35,6 @@ namespace media_router {
namespace {
// A view that represents the primary icon for a sink issue. This class is used
// to ensure its color is kept in sync with current theme.
class SinkIssueIconView : public views::ImageView {
public:
SinkIssueIconView() {
SetBorder(views::CreateEmptyBorder(kPrimaryIconBorder));
}
~SinkIssueIconView() override = default;
// views::ImageView:
void OnThemeChanged() override {
views::ImageView::OnThemeChanged();
const SkColor icon_color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_DefaultIconColor);
SetImage(gfx::CreateVectorIcon(::vector_icons::kInfoOutlineIcon,
kPrimaryIconSize, icon_color));
}
};
gfx::ImageSkia CreateSinkIcon(SinkIconType icon_type, bool enabled = true) {
const gfx::VectorIcon* vector_icon;
switch (icon_type) {
......@@ -112,7 +92,10 @@ std::unique_ptr<views::View> CreatePrimaryIconForSink(
return CreatePrimaryIconView(gfx::CreateVectorIcon(
kGenericStopIcon, kPrimaryIconSize, gfx::kGoogleBlue500));
} else if (sink.issue) {
return std::make_unique<SinkIssueIconView>();
auto icon = std::make_unique<views::ColorTrackingIconView>(
::vector_icons::kInfoOutlineIcon, kPrimaryIconSize);
icon->SetBorder(views::CreateEmptyBorder(kPrimaryIconBorder));
return icon;
} else if (sink.state == UIMediaSinkState::CONNECTING ||
sink.state == UIMediaSinkState::DISCONNECTING) {
return CreateThrobber();
......
......@@ -36,6 +36,7 @@
#include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/color_tracking_icon_view.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
......@@ -61,23 +62,6 @@ enum PasswordItemsViewColumnSetType {
UNDO_COLUMN_SET
};
// And ImageView that holds a kGlobeIcon of gfx::kFaviconSize and adapts to
// changes in theme color. Used as a fallback option when the page has no
// favicon.
class GlobeIconImageView : public views::ImageView {
public:
GlobeIconImageView() = default;
~GlobeIconImageView() override = default;
// views::View:
void OnThemeChanged() override {
views::ImageView::OnThemeChanged();
const SkColor icon_color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_DefaultIconColor);
SetImage(gfx::CreateVectorIcon(kGlobeIcon, gfx::kFaviconSize, icon_color));
}
};
PasswordItemsViewColumnSetType InferColumnSetTypeFromCredentials(
const std::vector<autofill::PasswordForm>& credentials) {
if (std::any_of(credentials.begin(), credentials.end(),
......@@ -310,7 +294,8 @@ void PasswordItemsView::PasswordRow::AddPasswordRow(
// Use a globe fallback until the actual favicon is loaded.
if (parent_->favicon_.IsEmpty()) {
layout->AddView(std::make_unique<GlobeIconImageView>());
layout->AddView(std::make_unique<views::ColorTrackingIconView>(
kGlobeIcon, gfx::kFaviconSize));
} else {
auto favicon_view = std::make_unique<views::ImageView>();
favicon_view->SetImage(parent_->favicon_.AsImageSkia());
......
......@@ -10,44 +10,29 @@
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/views/hover_button.h"
#include "chrome/grit/generated_resources.h"
#include "components/send_tab_to_self/target_device_info.h"
#include "components/sync/protocol/sync.pb.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/color_tracking_icon_view.h"
namespace send_tab_to_self {
namespace {
// IconView wraps the vector icon to track the color of the current Widget's
// NativeTheme.
class IconView : public views::ImageView {
public:
explicit IconView(const sync_pb::SyncEnums::DeviceType device_type)
: vector_icon_{device_type == sync_pb::SyncEnums::TYPE_PHONE
? &kHardwareSmartphoneIcon
: &kHardwareComputerIcon} {
constexpr auto kPrimaryIconBorder = gfx::Insets(6);
SetBorder(views::CreateEmptyBorder(kPrimaryIconBorder));
}
~IconView() override = default;
// views::View:
void OnThemeChanged() override {
views::ImageView::OnThemeChanged();
const SkColor icon_color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_DefaultIconColor);
SetImage(
gfx::CreateVectorIcon(*vector_icon_, kPrimaryIconSize, icon_color));
}
private:
std::unique_ptr<views::ColorTrackingIconView> CreateIcon(
const sync_pb::SyncEnums::DeviceType device_type) {
static constexpr int kPrimaryIconSize = 20;
const gfx::VectorIcon* vector_icon_;
};
auto icon = std::make_unique<views::ColorTrackingIconView>(
device_type == sync_pb::SyncEnums::TYPE_PHONE ? kHardwareSmartphoneIcon
: kHardwareComputerIcon,
kPrimaryIconSize);
constexpr auto kPrimaryIconBorder = gfx::Insets(6);
icon->SetBorder(views::CreateEmptyBorder(kPrimaryIconBorder));
return icon;
}
base::string16 GetLastUpdatedTime(const TargetDeviceInfo& device_info) {
int time_in_days =
......@@ -72,7 +57,7 @@ SendTabToSelfBubbleDeviceButton::SendTabToSelfBubbleDeviceButton(
const TargetDeviceInfo& device_info,
int button_tag)
: HoverButton(button_listener,
std::make_unique<IconView>(device_info.device_type),
CreateIcon(device_info.device_type),
base::UTF8ToUTF16(device_info.device_name),
GetLastUpdatedTime(device_info)) {
device_name_ = device_info.device_name;
......
......@@ -26,10 +26,10 @@
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/border.h"
#include "ui/views/bubble/bubble_frame_view.h"
#include "ui/views/controls/color_tracking_icon_view.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/styled_label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "url/origin.h"
#if defined(OS_CHROMEOS)
......@@ -39,23 +39,6 @@
namespace {
class VectorIconView : public views::ImageView {
public:
explicit VectorIconView(const gfx::VectorIcon& icon) : icon_(icon) {}
// views::ImageView
void OnThemeChanged() override {
ImageView::OnThemeChanged();
constexpr int kPrimaryIconSize = 20;
const SkColor color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_DefaultIconColor);
SetImage(gfx::CreateVectorIcon(icon_, kPrimaryIconSize, color));
}
private:
const gfx::VectorIcon& icon_;
};
class HeaderImageView : public NonAccessibleImageView {
public:
explicit HeaderImageView(const views::BubbleFrameView* frame_view,
......@@ -283,6 +266,7 @@ void SharingDialogView::Init() {
}
void SharingDialogView::InitListView() {
constexpr int kPrimaryIconSize = 20;
int tag = 0;
const gfx::Insets device_border =
gfx::Insets(kSharingDialogSpacing, kSharingDialogSpacing * 2,
......@@ -297,10 +281,11 @@ void SharingDialogView::InitListView() {
// Devices:
LogSharingDevicesToShow(data_.prefix, kSharingUiDialog, data_.devices.size());
for (const auto& device : data_.devices) {
auto icon = std::make_unique<VectorIconView>(
auto icon = std::make_unique<views::ColorTrackingIconView>(
device->device_type() == sync_pb::SyncEnums::TYPE_TABLET
? kTabletIcon
: kHardwareSmartphoneIcon);
: kHardwareSmartphoneIcon,
kPrimaryIconSize);
auto dialog_button = std::make_unique<HoverButton>(
this, std::move(icon), base::UTF8ToUTF16(device->client_name()),
......@@ -317,7 +302,8 @@ void SharingDialogView::InitListView() {
for (const auto& app : data_.apps) {
std::unique_ptr<views::ImageView> icon;
if (app.vector_icon) {
icon = std::make_unique<VectorIconView>(*app.vector_icon);
icon = std::make_unique<views::ColorTrackingIconView>(*app.vector_icon,
kPrimaryIconSize);
} else {
icon = std::make_unique<views::ImageView>();
icon->SetImage(app.image.AsImageSkia());
......
......@@ -120,6 +120,7 @@ component("views") {
"controls/button/menu_button_controller.h",
"controls/button/radio_button.h",
"controls/button/toggle_button.h",
"controls/color_tracking_icon_view.h",
"controls/combobox/combobox.h",
"controls/combobox/combobox_listener.h",
"controls/combobox/combobox_util.h",
......@@ -332,6 +333,7 @@ component("views") {
"controls/button/menu_button_controller.cc",
"controls/button/radio_button.cc",
"controls/button/toggle_button.cc",
"controls/color_tracking_icon_view.cc",
"controls/combobox/combobox.cc",
"controls/combobox/combobox_util.cc",
"controls/combobox/empty_combobox_model.cc",
......
// Copyright 2020 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 "ui/views/controls/color_tracking_icon_view.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/native_theme/native_theme.h"
namespace views {
ColorTrackingIconView::ColorTrackingIconView(const gfx::VectorIcon& icon,
int icon_size)
: icon_(icon), icon_size_(icon_size) {}
void ColorTrackingIconView::OnThemeChanged() {
ImageView::OnThemeChanged();
const SkColor color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_DefaultIconColor);
SetImage(gfx::CreateVectorIcon(icon_, icon_size_, color));
}
} // namespace views
// Copyright 2020 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 UI_VIEWS_CONTROLS_COLOR_TRACKING_ICON_VIEW_H_
#define UI_VIEWS_CONTROLS_COLOR_TRACKING_ICON_VIEW_H_
#include "ui/views/controls/image_view.h"
namespace gfx {
struct VectorIcon;
}
namespace views {
// An ImageView that displays |icon| at |icon_size|. Tracks theme changes so the
// icon is always the correct color.
class VIEWS_EXPORT ColorTrackingIconView : public ImageView {
public:
ColorTrackingIconView(const gfx::VectorIcon& icon, int icon_size);
// ImageView:
void OnThemeChanged() override;
private:
const gfx::VectorIcon& icon_;
const int icon_size_;
};
} // namespace views
#endif // UI_VIEWS_CONTROLS_COLOR_TRACKING_ICON_VIEW_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