Commit cb59b02a authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Refactor GetWarningIcon() a bit.

* Use temps to replace repeated expressions.
* Change name to make clear that it's not always the warning icon.
* Return ImageModel so callers can use the size; this fixes a bug where
  callsites used the wrong size in cases where the "help or error icon"
  would have been painted.  This removes GetWarningIconSize().

This does not yet fix a couple of behavioral oddities around the
selection of icons and colors (didn't want to mix noticeable behavior
changes with cleanup).  I will do that in a subsequent CL.

Bug: none
Change-Id: I44d5e56ea3729b2bc7c6498cbf32f342068ad77e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2315218
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791368}
parent f6d063cc
...@@ -343,7 +343,7 @@ void DownloadItemView::Layout() { ...@@ -343,7 +343,7 @@ void DownloadItemView::Layout() {
if (is_download_warning(mode_)) { if (is_download_warning(mode_)) {
gfx::Point child_origin( gfx::Point child_origin(
kStartPadding + GetWarningIconSize() + kStartPadding, kStartPadding + GetIcon().Size().width() + kStartPadding,
CenterY(warning_label_->height())); CenterY(warning_label_->height()));
warning_label_->SetPosition(child_origin); warning_label_->SetPosition(child_origin);
...@@ -360,7 +360,7 @@ void DownloadItemView::Layout() { ...@@ -360,7 +360,7 @@ void DownloadItemView::Layout() {
scan_button_->SetBoundsRect(gfx::Rect(child_origin, button_size)); scan_button_->SetBoundsRect(gfx::Rect(child_origin, button_size));
} else if (is_mixed_content(mode_)) { } else if (is_mixed_content(mode_)) {
gfx::Point child_origin( gfx::Point child_origin(
kStartPadding + GetWarningIconSize() + kStartPadding, kStartPadding + GetIcon().Size().width() + kStartPadding,
CenterY(warning_label_->height())); CenterY(warning_label_->height()));
warning_label_->SetPosition(child_origin); warning_label_->SetPosition(child_origin);
...@@ -373,7 +373,7 @@ void DownloadItemView::Layout() { ...@@ -373,7 +373,7 @@ void DownloadItemView::Layout() {
discard_button_->SetBoundsRect(gfx::Rect(child_origin, button_size)); discard_button_->SetBoundsRect(gfx::Rect(child_origin, button_size));
} else if (mode_ == Mode::kDeepScanning) { } else if (mode_ == Mode::kDeepScanning) {
gfx::Point child_origin( gfx::Point child_origin(
kStartPadding + GetWarningIconSize() + kStartPadding, kStartPadding + GetIcon().Size().width() + kStartPadding,
CenterY(deep_scanning_label_->height())); CenterY(deep_scanning_label_->height()));
deep_scanning_label_->SetPosition(child_origin); deep_scanning_label_->SetPosition(child_origin);
...@@ -601,7 +601,8 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const { ...@@ -601,7 +601,8 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const {
if (has_warning_label(mode_)) { if (has_warning_label(mode_)) {
// Width. // Width.
width = kStartPadding + GetWarningIconSize() + kStartPadding + const gfx::Size icon_size = GetIcon().Size();
width = kStartPadding + icon_size.width() + kStartPadding +
warning_label_->width() + kLabelPadding; warning_label_->width() + kLabelPadding;
gfx::Size button_size = GetButtonSize(); gfx::Size button_size = GetButtonSize();
if (save_button_->GetVisible() && discard_button_->GetVisible()) if (save_button_->GetVisible() && discard_button_->GetVisible())
...@@ -610,16 +611,17 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const { ...@@ -610,16 +611,17 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const {
// Height: make sure the button fits and the warning icon fits. // Height: make sure the button fits and the warning icon fits.
child_height = child_height =
std::max({child_height, button_size.height(), GetWarningIconSize()}); std::max({child_height, button_size.height(), icon_size.height()});
} else if (mode_ == Mode::kDeepScanning) { } else if (mode_ == Mode::kDeepScanning) {
width = kStartPadding + GetWarningIconSize() + kStartPadding + const gfx::Size icon_size = GetIcon().Size();
width = kStartPadding + icon_size.width() + kStartPadding +
deep_scanning_label_->width() + kLabelPadding; deep_scanning_label_->width() + kLabelPadding;
if (open_now_button_->GetVisible()) { if (open_now_button_->GetVisible()) {
width += open_now_button_->GetPreferredSize().width(); width += open_now_button_->GetPreferredSize().width();
// Height: make sure the button fits and the warning icon fits. // Height: make sure the button fits and the warning icon fits.
child_height = child_height =
std::max({child_height, open_now_button_->GetPreferredSize().height(), std::max({child_height, open_now_button_->GetPreferredSize().height(),
GetWarningIconSize()}); icon_size.height()});
width += kEndPadding; width += kEndPadding;
} }
} else { } else {
...@@ -668,11 +670,12 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) { ...@@ -668,11 +670,12 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) {
bool use_new_warnings = UseNewWarnings(); bool use_new_warnings = UseNewWarnings();
bool show_warning_icon = mode_ != Mode::kNormal; bool show_warning_icon = mode_ != Mode::kNormal;
if (show_warning_icon && !use_new_warnings) { if (show_warning_icon && !use_new_warnings) {
int icon_x = const gfx::ImageSkia icon = ui::ThemedVectorIcon(GetIcon().GetVectorIcon())
(base::i18n::IsRTL() ? width() - GetWarningIconSize() - kStartPadding .GetImageSkia(GetNativeTheme());
: kStartPadding); const int icon_x =
int icon_y = CenterY(GetWarningIconSize()); GetMirroredXWithWidthInView(kStartPadding, icon.size().width());
canvas->DrawImageInt(GetWarningIcon(), icon_x, icon_y); const int icon_y = CenterY(icon.size().height());
canvas->DrawImageInt(icon, icon_x, icon_y);
OnPaintBorder(canvas); OnPaintBorder(canvas);
return; return;
...@@ -737,12 +740,14 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) { ...@@ -737,12 +740,14 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) {
// Overlay the warning icon if appropriate. // Overlay the warning icon if appropriate.
if (show_warning_icon && use_new_warnings) { if (show_warning_icon && use_new_warnings) {
constexpr int kDangerIconOffset = 8; constexpr int kDangerIconOffset = 8;
int icon_x = const gfx::ImageSkia icon =
(base::i18n::IsRTL() ? width() - GetWarningIconSize() - kStartPadding ui::ThemedVectorIcon(GetIcon().GetVectorIcon())
: kStartPadding) + .GetImageSkia(GetNativeTheme());
const int icon_x =
GetMirroredXWithWidthInView(kStartPadding, icon.size().width()) +
kDangerIconOffset; kDangerIconOffset;
int icon_y = CenterY(GetWarningIconSize()) + kDangerIconOffset; const int icon_y = CenterY(icon.size().height()) + kDangerIconOffset;
canvas->DrawImageInt(GetWarningIcon(), icon_x, icon_y); canvas->DrawImageInt(icon, icon_x, icon_y);
} }
} }
...@@ -1063,26 +1068,28 @@ void DownloadItemView::PaintDownloadProgress( ...@@ -1063,26 +1068,28 @@ void DownloadItemView::PaintDownloadProgress(
canvas->DrawPath(progress, progress_flags); canvas->DrawPath(progress, progress_flags);
} }
gfx::ImageSkia DownloadItemView::GetWarningIcon() { ui::ImageModel DownloadItemView::GetIcon() const {
// TODO(pkasting): Use a child view (ImageView subclass?) to display the icon // TODO(pkasting): Use a child view (ImageView subclass?) to display the icon
// instead of recomputing this and drawing manually. // instead of recomputing this and drawing manually.
// TODO(drubery): Replace this with a constexpr variable when the new UX is // TODO(pkasting): Some names/icons here are backwards.
// fully launched. // TODO(drubery): Replace these sizes with layout provider constants when the
const int error_icon_size = UseNewWarnings() ? 20 : 27; // new UX is fully launched.
const int help_or_error_icon_size = UseNewWarnings() ? 20 : 27;
const auto kWarning = ui::ImageModel::FromVectorIcon(
vector_icons::kErrorIcon, ui::NativeTheme::kColorId_AlertSeverityMedium,
help_or_error_icon_size);
const auto kError = ui::ImageModel::FromVectorIcon(
vector_icons::kWarningIcon, ui::NativeTheme::kColorId_AlertSeverityHigh,
UseNewWarnings() ? 20 : 24);
switch (model_->GetDangerType()) { switch (model_->GetDangerType()) {
case download::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: case download::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT:
if (safe_browsing::AdvancedProtectionStatusManagerFactory::GetForProfile( return safe_browsing::AdvancedProtectionStatusManagerFactory::
model()->profile()) GetForProfile(model_->profile())
->IsUnderAdvancedProtection()) { ->IsUnderAdvancedProtection()
return gfx::CreateVectorIcon( ? kWarning
vector_icons::kErrorIcon, error_icon_size, : kError;
GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_AlertSeverityMedium));
}
FALLTHROUGH;
case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT: case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: case download::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST:
...@@ -1091,24 +1098,16 @@ gfx::ImageSkia DownloadItemView::GetWarningIcon() { ...@@ -1091,24 +1098,16 @@ gfx::ImageSkia DownloadItemView::GetWarningIcon() {
case download::DOWNLOAD_DANGER_TYPE_BLOCKED_TOO_LARGE: case download::DOWNLOAD_DANGER_TYPE_BLOCKED_TOO_LARGE:
case download::DOWNLOAD_DANGER_TYPE_BLOCKED_PASSWORD_PROTECTED: case download::DOWNLOAD_DANGER_TYPE_BLOCKED_PASSWORD_PROTECTED:
case download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_BLOCK: case download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_BLOCK:
return gfx::CreateVectorIcon( return kError;
vector_icons::kWarningIcon, GetWarningIconSize(),
GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_AlertSeverityHigh));
case download::DOWNLOAD_DANGER_TYPE_ASYNC_SCANNING: case download::DOWNLOAD_DANGER_TYPE_ASYNC_SCANNING:
case download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_WARNING: case download::DOWNLOAD_DANGER_TYPE_SENSITIVE_CONTENT_WARNING:
return gfx::CreateVectorIcon( return ui::ImageModel::FromVectorIcon(
vector_icons::kErrorIcon, error_icon_size, vector_icons::kErrorIcon, ui::NativeTheme::kColorId_DefaultIconColor,
GetNativeTheme()->GetSystemColor( help_or_error_icon_size);
ui::NativeTheme::kColorId_DefaultIconColor));
case download::DOWNLOAD_DANGER_TYPE_PROMPT_FOR_SCANNING: case download::DOWNLOAD_DANGER_TYPE_PROMPT_FOR_SCANNING:
return gfx::CreateVectorIcon( return ui::ImageModel::FromVectorIcon(
vector_icons::kHelpIcon, error_icon_size, vector_icons::kHelpIcon, ui::NativeTheme::kColorId_DefaultIconColor,
GetNativeTheme()->GetSystemColor( help_or_error_icon_size);
ui::NativeTheme::kColorId_DefaultIconColor));
case download::DOWNLOAD_DANGER_TYPE_BLOCKED_UNSUPPORTED_FILETYPE: case download::DOWNLOAD_DANGER_TYPE_BLOCKED_UNSUPPORTED_FILETYPE:
case download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_SAFE: case download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_SAFE:
case download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_OPENED_DANGEROUS: case download::DOWNLOAD_DANGER_TYPE_DEEP_SCANNED_OPENED_DANGEROUS:
...@@ -1122,15 +1121,9 @@ gfx::ImageSkia DownloadItemView::GetWarningIcon() { ...@@ -1122,15 +1121,9 @@ gfx::ImageSkia DownloadItemView::GetWarningIcon() {
switch (model_->GetMixedContentStatus()) { switch (model_->GetMixedContentStatus()) {
case download::DownloadItem::MixedContentStatus::BLOCK: case download::DownloadItem::MixedContentStatus::BLOCK:
return gfx::CreateVectorIcon( return kError;
vector_icons::kWarningIcon, GetWarningIconSize(),
GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_AlertSeverityHigh));
case download::DownloadItem::MixedContentStatus::WARN: case download::DownloadItem::MixedContentStatus::WARN:
return gfx::CreateVectorIcon( return kWarning;
vector_icons::kErrorIcon, error_icon_size,
GetNativeTheme()->GetSystemColor(
ui::NativeTheme::kColorId_AlertSeverityMedium));
case download::DownloadItem::MixedContentStatus::UNKNOWN: case download::DownloadItem::MixedContentStatus::UNKNOWN:
case download::DownloadItem::MixedContentStatus::SAFE: case download::DownloadItem::MixedContentStatus::SAFE:
case download::DownloadItem::MixedContentStatus::VALIDATED: case download::DownloadItem::MixedContentStatus::VALIDATED:
...@@ -1139,7 +1132,7 @@ gfx::ImageSkia DownloadItemView::GetWarningIcon() { ...@@ -1139,7 +1132,7 @@ gfx::ImageSkia DownloadItemView::GetWarningIcon() {
} }
NOTREACHED(); NOTREACHED();
return gfx::ImageSkia(); return ui::ImageModel();
} }
std::pair<base::string16, int> DownloadItemView::GetStatusTextAndStyle() const { std::pair<base::string16, int> DownloadItemView::GetStatusTextAndStyle() const {
...@@ -1303,13 +1296,6 @@ bool DownloadItemView::SubmitDownloadToFeedbackService( ...@@ -1303,13 +1296,6 @@ bool DownloadItemView::SubmitDownloadToFeedbackService(
#endif #endif
} }
// static
int DownloadItemView::GetWarningIconSize() {
// TODO(drubery): Replace this method with a constexpr variable when the new
// UX is fully launched.
return UseNewWarnings() ? 20 : 24;
}
void DownloadItemView::ExecuteCommand(DownloadCommands::Command command) { void DownloadItemView::ExecuteCommand(DownloadCommands::Command command) {
commands_.ExecuteCommand(command); commands_.ExecuteCommand(command);
} }
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "chrome/browser/download/download_commands.h" #include "chrome/browser/download/download_commands.h"
#include "chrome/browser/download/download_ui_model.h" #include "chrome/browser/download/download_ui_model.h"
#include "chrome/browser/icon_loader.h" #include "chrome/browser/icon_loader.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_types.h" #include "ui/base/ui_base_types.h"
#include "ui/gfx/animation/slide_animation.h" #include "ui/gfx/animation/slide_animation.h"
...@@ -159,7 +160,7 @@ class DownloadItemView : public views::View, ...@@ -159,7 +160,7 @@ class DownloadItemView : public views::View,
int percent_done) const; int percent_done) const;
// When not in normal mode, returns the current help/warning/error icon. // When not in normal mode, returns the current help/warning/error icon.
gfx::ImageSkia GetWarningIcon(); ui::ImageModel GetIcon() const;
// Returns the text and style to use for the status label. // Returns the text and style to use for the status label.
std::pair<base::string16, int> GetStatusTextAndStyle() const; std::pair<base::string16, int> GetStatusTextAndStyle() const;
...@@ -205,9 +206,6 @@ class DownloadItemView : public views::View, ...@@ -205,9 +206,6 @@ class DownloadItemView : public views::View,
bool SubmitDownloadToFeedbackService( bool SubmitDownloadToFeedbackService(
DownloadCommands::Command download_command); DownloadCommands::Command download_command);
// Returns the height/width of the warning icon, in dp.
static int GetWarningIconSize();
// Forwards |command| to |commands_|; useful for callbacks. // Forwards |command| to |commands_|; useful for callbacks.
void ExecuteCommand(DownloadCommands::Command command); void ExecuteCommand(DownloadCommands::Command command);
......
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