Commit ad22c35b authored by Mohamed Amir Yosef's avatar Mohamed Amir Yosef Committed by Commit Bot

[Autofill] Support Dark Mode in Autofill Dropdown Icons

This CL makes sure that icons in the autofill dropdown menu adapt
to changes in the theme by introducing a subclass of ImageView
that listens to theme changes.

Screenshots:
Before: https://screenshot.googleplex.com/5SNap3FrsrsMQQo.png
After: https://screenshot.googleplex.com/8W7PmdUttXYgGwU.png


Bug: 1115952
Change-Id: I0d7b952b325d8314963dabf596871815e4ecb3b3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2421449
Commit-Queue: Mohamed Amir Yosef <mamir@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809254}
parent a485b3ae
...@@ -108,66 +108,97 @@ void BuildColumnSet(views::GridLayout* layout) { ...@@ -108,66 +108,97 @@ void BuildColumnSet(views::GridLayout* layout) {
views::GridLayout::ColumnSize::kUsePreferred, 0, 0); views::GridLayout::ColumnSize::kUsePreferred, 0, 0);
} }
gfx::ImageSkia GetIconImageByName(const std::string& icon_str) { // An image view that shows a vector icon and tracks changes in the theme.
class VectorIconView : public views::ImageView {
public:
explicit VectorIconView(const gfx::VectorIcon& icon) : icon_(icon) {}
// views::ImageView
void OnThemeChanged() override {
ImageView::OnThemeChanged();
const SkColor color = GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_DefaultIconColor);
SetImage(gfx::CreateVectorIcon(icon_, gfx::kFaviconSize, color));
}
private:
const gfx::VectorIcon& icon_;
};
std::unique_ptr<views::ImageView> ImageViewFromImageSkia(
const gfx::ImageSkia& image_skia) {
if (image_skia.isNull())
return nullptr;
auto image_view = std::make_unique<views::ImageView>();
image_view->SetImage(image_skia);
return image_view;
}
std::unique_ptr<views::ImageView> ImageViewFromVectorIcon(
const gfx::VectorIcon& vector_icon) {
return std::make_unique<VectorIconView>(vector_icon);
}
std::unique_ptr<views::ImageView> GetIconImageViewByName(
const std::string& icon_str) {
if (icon_str.empty()) if (icon_str.empty())
return gfx::ImageSkia(); return nullptr;
// For http warning message, get icon images from VectorIcon, which is the // For http warning message, get icon images from VectorIcon, which is the
// same as security indicator icons in location bar. // same as security indicator icons in location bar.
if (icon_str == "httpWarning") { if (icon_str == "httpWarning")
return gfx::CreateVectorIcon(omnibox::kHttpIcon, gfx::kFaviconSize, return ImageViewFromVectorIcon(omnibox::kHttpIcon);
gfx::kChromeIconGrey);
}
if (icon_str == "httpsInvalid") { if (icon_str == "httpsInvalid") {
return gfx::CreateVectorIcon(omnibox::kNotSecureWarningIcon, return ImageViewFromImageSkia(gfx::CreateVectorIcon(
gfx::kFaviconSize, gfx::kGoogleRed700); omnibox::kNotSecureWarningIcon, gfx::kFaviconSize, gfx::kGoogleRed700));
}
if (icon_str == "keyIcon") {
return gfx::CreateVectorIcon(kKeyIcon, gfx::kFaviconSize,
gfx::kChromeIconGrey);
}
if (icon_str == "globeIcon") {
return gfx::CreateVectorIcon(kGlobeIcon, gfx::kFaviconSize,
gfx::kChromeIconGrey);
}
if (icon_str == "settingsIcon") {
return gfx::CreateVectorIcon(vector_icons::kSettingsIcon, gfx::kFaviconSize,
gfx::kChromeIconGrey);
}
if (icon_str == "empty") {
return gfx::CreateVectorIcon(omnibox::kHttpIcon, gfx::kFaviconSize,
gfx::kChromeIconGrey);
} }
if (icon_str == "keyIcon")
return ImageViewFromVectorIcon(kKeyIcon);
if (icon_str == "globeIcon")
return ImageViewFromVectorIcon(kGlobeIcon);
if (icon_str == "settingsIcon")
return ImageViewFromVectorIcon(vector_icons::kSettingsIcon);
if (icon_str == "empty")
return ImageViewFromVectorIcon(omnibox::kHttpIcon);
if (icon_str == "google") { if (icon_str == "google") {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING) #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
return gfx::CreateVectorIcon(kGoogleGLogoIcon, gfx::kFaviconSize, return ImageViewFromImageSkia(gfx::CreateVectorIcon(
gfx::kPlaceholderColor); kGoogleGLogoIcon, gfx::kFaviconSize, gfx::kPlaceholderColor));
#else #else
return gfx::ImageSkia(); return nullptr;
#endif #endif
} }
#if !BUILDFLAG(GOOGLE_CHROME_BRANDING) #if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
if (icon_str == "googlePay" || icon_str == "googlePayDark") { if (icon_str == "googlePay" || icon_str == "googlePayDark") {
return gfx::ImageSkia(); return nullptr;
} }
#endif #endif
// For other suggestion entries, get icon from PNG files. // For other suggestion entries, get icon from PNG files.
int icon_id = autofill::GetIconResourceID(icon_str); int icon_id = autofill::GetIconResourceID(icon_str);
DCHECK_NE(icon_id, 0); DCHECK_NE(icon_id, 0);
return *ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon_id); return ImageViewFromImageSkia(
*ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(icon_id));
} }
gfx::ImageSkia GetIconImage(const autofill::Suggestion& suggestion) { std::unique_ptr<views::ImageView> GetIconImageView(
if (!suggestion.custom_icon.IsEmpty()) const autofill::Suggestion& suggestion) {
return suggestion.custom_icon.AsImageSkia(); if (!suggestion.custom_icon.IsEmpty()) {
return ImageViewFromImageSkia(suggestion.custom_icon.AsImageSkia());
}
return GetIconImageByName(suggestion.icon); return GetIconImageViewByName(suggestion.icon);
} }
gfx::ImageSkia GetStoreIndicatorIconImage( std::unique_ptr<views::ImageView> GetStoreIndicatorIconImageView(
const autofill::Suggestion& suggestion) { const autofill::Suggestion& suggestion) {
return GetIconImageByName(suggestion.store_indicator_icon); return GetIconImageViewByName(suggestion.store_indicator_icon);
} }
} // namespace } // namespace
...@@ -259,7 +290,6 @@ class AutofillPopupItemView : public AutofillPopupRowView { ...@@ -259,7 +290,6 @@ class AutofillPopupItemView : public AutofillPopupRowView {
// Returns the font weight to be applied to primary info. // Returns the font weight to be applied to primary info.
virtual gfx::Font::Weight GetPrimaryTextWeight() const = 0; virtual gfx::Font::Weight GetPrimaryTextWeight() const = 0;
void AddIcon(gfx::ImageSkia icon);
void AddSpacerWithSize(int spacer_width, void AddSpacerWithSize(int spacer_width,
bool resize, bool resize,
views::BoxLayout* layout); views::BoxLayout* layout);
...@@ -503,10 +533,11 @@ void AutofillPopupItemView::CreateContent() { ...@@ -503,10 +533,11 @@ void AutofillPopupItemView::CreateContent() {
std::vector<Suggestion> suggestions = controller->GetSuggestions(); std::vector<Suggestion> suggestions = controller->GetSuggestions();
const gfx::ImageSkia icon = GetIconImage(suggestions[line_number()]); std::unique_ptr<views::ImageView> icon =
GetIconImageView(suggestions[line_number()]);
if (!icon.isNull()) { if (icon) {
AddIcon(icon); AddChildView(std::move(icon));
AddSpacerWithSize(GetHorizontalMargin(), AddSpacerWithSize(GetHorizontalMargin(),
/*resize=*/false, layout_manager); /*resize=*/false, layout_manager);
} }
...@@ -543,12 +574,12 @@ void AutofillPopupItemView::CreateContent() { ...@@ -543,12 +574,12 @@ void AutofillPopupItemView::CreateContent() {
} }
AddChildView(std::move(all_labels)); AddChildView(std::move(all_labels));
const gfx::ImageSkia store_indicator_icon = std::unique_ptr<views::ImageView> store_indicator_icon =
GetStoreIndicatorIconImage(suggestions[line_number()]); GetStoreIndicatorIconImageView(suggestions[line_number()]);
if (!store_indicator_icon.isNull()) { if (store_indicator_icon) {
AddSpacerWithSize(GetHorizontalMargin(), AddSpacerWithSize(GetHorizontalMargin(),
/*resize=*/true, layout_manager); /*resize=*/true, layout_manager);
AddIcon(store_indicator_icon); AddChildView(std::move(icon));
} }
} }
...@@ -636,12 +667,6 @@ AutofillPopupItemView::CreateLabelWithStyleAndContext( ...@@ -636,12 +667,6 @@ AutofillPopupItemView::CreateLabelWithStyleAndContext(
return label; return label;
} }
void AutofillPopupItemView::AddIcon(gfx::ImageSkia icon) {
auto image_view = std::make_unique<views::ImageView>();
image_view->SetImage(icon);
AddChildView(std::move(image_view));
}
void AutofillPopupItemView::AddSpacerWithSize(int spacer_width, void AutofillPopupItemView::AddSpacerWithSize(int spacer_width,
bool resize, bool resize,
views::BoxLayout* layout) { views::BoxLayout* layout) {
...@@ -781,7 +806,7 @@ void AutofillPopupFooterView::CreateContent() { ...@@ -781,7 +806,7 @@ void AutofillPopupFooterView::CreateContent() {
views::BoxLayout::CrossAxisAlignment::kCenter); views::BoxLayout::CrossAxisAlignment::kCenter);
const Suggestion suggestion = controller->GetSuggestions()[line_number()]; const Suggestion suggestion = controller->GetSuggestions()[line_number()];
const gfx::ImageSkia icon = GetIconImage(suggestion); std::unique_ptr<views::ImageView> icon = GetIconImageView(suggestion);
const bool use_leading_icon = const bool use_leading_icon =
base::Contains(kItemTypesUsingLeadingIcons, frontend_id()); base::Contains(kItemTypesUsingLeadingIcons, frontend_id());
...@@ -790,8 +815,8 @@ void AutofillPopupFooterView::CreateContent() { ...@@ -790,8 +815,8 @@ void AutofillPopupFooterView::CreateContent() {
SetEnabled(false); SetEnabled(false);
AddChildView(std::make_unique<views::Throbber>())->Start(); AddChildView(std::make_unique<views::Throbber>())->Start();
AddSpacerWithSize(GetHorizontalMargin(), /*resize=*/false, layout_manager); AddSpacerWithSize(GetHorizontalMargin(), /*resize=*/false, layout_manager);
} else if (!icon.isNull() && use_leading_icon) { } else if (icon && use_leading_icon) {
AddIcon(icon); AddChildView(std::move(icon));
AddSpacerWithSize(GetHorizontalMargin(), /*resize=*/false, layout_manager); AddSpacerWithSize(GetHorizontalMargin(), /*resize=*/false, layout_manager);
} }
...@@ -810,9 +835,9 @@ void AutofillPopupFooterView::CreateContent() { ...@@ -810,9 +835,9 @@ void AutofillPopupFooterView::CreateContent() {
DISTANCE_BETWEEN_PRIMARY_AND_SECONDARY_LABELS_HORIZONTAL), DISTANCE_BETWEEN_PRIMARY_AND_SECONDARY_LABELS_HORIZONTAL),
/*resize=*/true, layout_manager); /*resize=*/true, layout_manager);
if (!icon.isNull() && !use_leading_icon) { if (icon && !use_leading_icon) {
AddSpacerWithSize(GetHorizontalMargin(), /*resize=*/false, layout_manager); AddSpacerWithSize(GetHorizontalMargin(), /*resize=*/false, layout_manager);
AddIcon(icon); AddChildView(std::move(icon));
} }
} }
......
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