Commit 04fb6459 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Clean up DownloadItemView construction some.

* Construct more things in the declaration
* Make more things const
* Slightly improve variable names
* Use AddChildView(std::make_unique<...>(...)) idiom for brevity
* Remove various unnecessary setter calls
* Enable auto color readability; set background colors correctly to make
  this work properly; use a solid background for the overall view while
  at it

Bug: none
Change-Id: Ie13ccdb0e620d2f80736daa8a41590a8dfc5116f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2333500
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarShakti Sahu <shaktisahu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#795302}
parent dcf751a6
...@@ -45,7 +45,6 @@ ...@@ -45,7 +45,6 @@
#include "chrome/browser/ui/tab_modal_confirm_dialog.h" #include "chrome/browser/ui/tab_modal_confirm_dialog.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/chrome_typography.h" #include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/download/download_shelf_context_menu_view.h"
#include "chrome/browser/ui/views/download/download_shelf_view.h" #include "chrome/browser/ui/views/download/download_shelf_view.h"
#include "chrome/browser/ui/views/safe_browsing/deep_scanning_modal_dialog.h" #include "chrome/browser/ui/views/safe_browsing/deep_scanning_modal_dialog.h"
#include "chrome/browser/ui/views/safe_browsing/prompt_for_scanning_modal_dialog.h" #include "chrome/browser/ui/views/safe_browsing/prompt_for_scanning_modal_dialog.h"
...@@ -147,13 +146,13 @@ class TransparentButton : public views::Button { ...@@ -147,13 +146,13 @@ class TransparentButton : public views::Button {
public: public:
METADATA_HEADER(TransparentButton); METADATA_HEADER(TransparentButton);
explicit TransparentButton(views::ButtonListener* listener) explicit TransparentButton(DownloadItemView* parent) : Button(parent) {
: Button(listener) {
SetFocusForPlatform(); SetFocusForPlatform();
views::InstallRectHighlightPathGenerator(this); views::InstallRectHighlightPathGenerator(this);
SetInkDropMode(InkDropMode::ON); SetInkDropMode(InkDropMode::ON);
set_context_menu_controller(parent);
} }
~TransparentButton() override {} ~TransparentButton() override = default;
// Button subclasses need to provide this because the default color is // Button subclasses need to provide this because the default color is
// kPlaceholderColor. In theory we could statically compute it in the // kPlaceholderColor. In theory we could statically compute it in the
...@@ -230,12 +229,12 @@ bool has_warning_label(DownloadItemView::Mode mode) { ...@@ -230,12 +229,12 @@ bool has_warning_label(DownloadItemView::Mode mode) {
} // namespace } // namespace
DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr download, DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr model,
DownloadShelfView* parent, DownloadShelfView* shelf,
views::View* accessible_alert) views::View* accessible_alert)
: AnimationDelegateViews(this), : AnimationDelegateViews(this),
model_(std::move(download)), model_(std::move(model)),
shelf_(parent), shelf_(shelf),
mode_(Mode::kNormal), mode_(Mode::kNormal),
indeterminate_progress_timer_( indeterminate_progress_timer_(
FROM_HERE, FROM_HERE,
...@@ -253,70 +252,54 @@ DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr download, ...@@ -253,70 +252,54 @@ DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr download,
base::BindRepeating(&DownloadItemView::AnnounceAccessibleAlert, base::BindRepeating(&DownloadItemView::AnnounceAccessibleAlert,
base::Unretained(this))) { base::Unretained(this))) {
views::InstallRectHighlightPathGenerator(this); views::InstallRectHighlightPathGenerator(this);
model_->AddObserver(this); observer_.Add(this->model());
// TODO(pkasting): Use bespoke file-scope subclasses for some of these child // TODO(pkasting): Use bespoke file-scope subclasses for some of these child
// views to localize functionality and simplify this class. // views to localize functionality and simplify this class.
auto open_button = std::make_unique<TransparentButton>(this); open_button_ = AddChildView(std::make_unique<TransparentButton>(this));
open_button->set_context_menu_controller(this);
open_button_ = AddChildView(std::move(open_button)); file_name_label_ = AddChildView(
std::make_unique<views::StyledLabel>(base::string16(), nullptr));
auto file_name_label = file_name_label_->SetTextContext(CONTEXT_DOWNLOAD_SHELF);
std::make_unique<views::StyledLabel>(base::string16(), nullptr); file_name_label_->GetViewAccessibility().OverrideIsIgnored(true);
file_name_label->SetTextContext(CONTEXT_DOWNLOAD_SHELF); const base::string16 filename = ElidedFilename(*file_name_label_);
file_name_label->GetViewAccessibility().OverrideIsIgnored(true); file_name_label_->SetText(filename);
const base::string16 filename = ElidedFilename(*file_name_label); file_name_label_->set_can_process_events_within_subtree(false);
file_name_label->SetText(filename);
file_name_label->set_can_process_events_within_subtree(false); status_label_ = AddChildView(std::make_unique<views::Label>(
file_name_label_ = AddChildView(std::move(file_name_label)); base::string16(), CONTEXT_DOWNLOAD_SHELF_STATUS));
status_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
auto status_label = std::make_unique<views::Label>(
l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING), warning_label_ = AddChildView(
CONTEXT_DOWNLOAD_SHELF_STATUS); std::make_unique<views::StyledLabel>(base::string16(), nullptr));
status_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); warning_label_->SetTextContext(CONTEXT_DOWNLOAD_SHELF);
status_label->GetViewAccessibility().OverrideIsIgnored(true); warning_label_->set_can_process_events_within_subtree(false);
status_label_ = AddChildView(std::move(status_label));
deep_scanning_label_ = AddChildView(
auto warning_label = std::make_unique<views::StyledLabel>( std::make_unique<views::StyledLabel>(base::string16(), nullptr));
base::string16(), /*listener=*/nullptr); deep_scanning_label_->SetTextContext(CONTEXT_DOWNLOAD_SHELF);
warning_label->SetTextContext(CONTEXT_DOWNLOAD_SHELF); deep_scanning_label_->set_can_process_events_within_subtree(false);
warning_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
warning_label->SetAutoColorReadabilityEnabled(false); open_now_button_ = AddChildView(views::MdTextButton::Create(
warning_label->set_can_process_events_within_subtree(false); this, l10n_util::GetStringUTF16(IDS_OPEN_DOWNLOAD_NOW)));
warning_label_ = AddChildView(std::move(warning_label));
save_button_ =
auto deep_scanning_label = std::make_unique<views::StyledLabel>( AddChildView(views::MdTextButton::Create(this, base::string16()));
base::string16(), /*listener=*/nullptr);
deep_scanning_label->SetTextContext(CONTEXT_DOWNLOAD_SHELF); discard_button_ = AddChildView(views::MdTextButton::Create(
deep_scanning_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); this, l10n_util::GetStringUTF16(IDS_DISCARD_DOWNLOAD)));
deep_scanning_label->SetAutoColorReadabilityEnabled(false);
deep_scanning_label->set_can_process_events_within_subtree(false); scan_button_ = AddChildView(views::MdTextButton::Create(
deep_scanning_label_ = AddChildView(std::move(deep_scanning_label)); this, l10n_util::GetStringUTF16(IDS_SCAN_DOWNLOAD)));
auto open_now_button = views::MdTextButton::Create( dropdown_button_ = AddChildView(views::CreateVectorImageButton(this));
this, l10n_util::GetStringUTF16(IDS_OPEN_DOWNLOAD_NOW)); dropdown_button_->SetAccessibleName(l10n_util::GetStringUTF16(
open_now_button_ = AddChildView(std::move(open_now_button));
auto save_button = views::MdTextButton::Create(this, base::string16());
save_button_ = AddChildView(std::move(save_button));
auto discard_button = views::MdTextButton::Create(
this, l10n_util::GetStringUTF16(IDS_DISCARD_DOWNLOAD));
discard_button_ = AddChildView(std::move(discard_button));
auto scan_button = views::MdTextButton::Create(
this, l10n_util::GetStringUTF16(IDS_SCAN_DOWNLOAD));
scan_button_ = AddChildView(std::move(scan_button));
auto dropdown_button = views::CreateVectorImageButton(this);
dropdown_button->SetAccessibleName(l10n_util::GetStringUTF16(
IDS_DOWNLOAD_ITEM_DROPDOWN_BUTTON_ACCESSIBLE_TEXT)); IDS_DOWNLOAD_ITEM_DROPDOWN_BUTTON_ACCESSIBLE_TEXT));
dropdown_button->SetBorder(views::CreateEmptyBorder(gfx::Insets(10))); dropdown_button_->SetBorder(views::CreateEmptyBorder(gfx::Insets(10)));
dropdown_button->set_has_ink_drop_action_on_click(false); dropdown_button_->set_has_ink_drop_action_on_click(false);
dropdown_button->SetFocusForPlatform(); dropdown_button_->SetFocusForPlatform();
dropdown_button->SizeToPreferredSize(); dropdown_button_->SizeToPreferredSize();
dropdown_button_ = AddChildView(std::move(dropdown_button));
complete_animation_.SetSlideDuration(base::TimeDelta::FromMilliseconds(2500)); complete_animation_.SetSlideDuration(base::TimeDelta::FromMilliseconds(2500));
complete_animation_.SetTweenType(gfx::Tween::LINEAR); complete_animation_.SetTweenType(gfx::Tween::LINEAR);
...@@ -325,9 +308,7 @@ DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr download, ...@@ -325,9 +308,7 @@ DownloadItemView::DownloadItemView(DownloadUIModel::DownloadUIModelPtr download,
OnDownloadUpdated(); OnDownloadUpdated();
} }
DownloadItemView::~DownloadItemView() { DownloadItemView::~DownloadItemView() = default;
model_->RemoveObserver(this);
}
void DownloadItemView::Layout() { void DownloadItemView::Layout() {
// TODO(crbug.com/1005568): Replace Layout()/CalculatePreferredSize() with a // TODO(crbug.com/1005568): Replace Layout()/CalculatePreferredSize() with a
...@@ -591,12 +572,7 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const { ...@@ -591,12 +572,7 @@ gfx::Size DownloadItemView::CalculatePreferredSize() const {
} }
void DownloadItemView::OnPaintBackground(gfx::Canvas* canvas) { void DownloadItemView::OnPaintBackground(gfx::Canvas* canvas) {
// Make sure to draw |this| opaquely. Since the toolbar color can be partially View::OnPaintBackground(canvas);
// transparent, start with a black backdrop (which is the default initialized
// color for opaque canvases).
canvas->DrawColor(SK_ColorBLACK);
canvas->DrawColor(
GetThemeProvider()->GetColor(ThemeProperties::COLOR_DOWNLOAD_SHELF));
// Draw the separator as part of the background. It will be covered by the // Draw the separator as part of the background. It will be covered by the
// focus ring when the view has focus. // focus ring when the view has focus.
...@@ -703,10 +679,13 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) { ...@@ -703,10 +679,13 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) {
void DownloadItemView::OnThemeChanged() { void DownloadItemView::OnThemeChanged() {
views::View::OnThemeChanged(); views::View::OnThemeChanged();
SkColor background_color = const SkColor background_color =
GetThemeProvider()->GetColor(ThemeProperties::COLOR_DOWNLOAD_SHELF); GetThemeProvider()->GetColor(ThemeProperties::COLOR_DOWNLOAD_SHELF);
SetBackground(views::CreateSolidBackground(background_color));
file_name_label_->SetDisplayedOnBackgroundColor(background_color); file_name_label_->SetDisplayedOnBackgroundColor(background_color);
status_label_->SetBackgroundColor(background_color); status_label_->SetBackgroundColor(background_color);
warning_label_->SetDisplayedOnBackgroundColor(background_color);
deep_scanning_label_->SetDisplayedOnBackgroundColor(background_color);
shelf_->ConfigureButtonForTheme(open_now_button_); shelf_->ConfigureButtonForTheme(open_now_button_);
shelf_->ConfigureButtonForTheme(save_button_); shelf_->ConfigureButtonForTheme(save_button_);
...@@ -1207,14 +1186,12 @@ void DownloadItemView::ShowContextMenuImpl(const gfx::Rect& rect, ...@@ -1207,14 +1186,12 @@ void DownloadItemView::ShowContextMenuImpl(const gfx::Rect& rect,
static_cast<views::internal::RootView*>(GetWidget()->GetRootView()) static_cast<views::internal::RootView*>(GetWidget()->GetRootView())
->SetMouseHandler(nullptr); ->SetMouseHandler(nullptr);
if (!context_menu_.get())
context_menu_ = std::make_unique<DownloadShelfContextMenuView>(this);
const auto release_dropdown = [](DownloadItemView* view) { const auto release_dropdown = [](DownloadItemView* view) {
view->SetDropdownPressed(false); view->SetDropdownPressed(false);
// Make sure any new status from activating a context menu option is read. // Make sure any new status from activating a context menu option is read.
view->announce_accessible_alert_soon_ = true; view->announce_accessible_alert_soon_ = true;
}; };
context_menu_->Run( context_menu_.Run(
GetWidget()->GetTopLevelWidget(), rect, source_type, GetWidget()->GetTopLevelWidget(), rect, source_type,
base::BindRepeating(std::move(release_dropdown), base::Unretained(this))); base::BindRepeating(std::move(release_dropdown), base::Unretained(this)));
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/scoped_observer.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/task/cancelable_task_tracker.h" #include "base/task/cancelable_task_tracker.h"
#include "base/time/time.h" #include "base/time/time.h"
...@@ -19,6 +20,7 @@ ...@@ -19,6 +20,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 "chrome/browser/ui/views/download/download_shelf_context_menu_view.h"
#include "ui/base/models/image_model.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"
...@@ -31,7 +33,6 @@ ...@@ -31,7 +33,6 @@
#include "ui/views/controls/button/button.h" #include "ui/views/controls/button/button.h"
#include "ui/views/view.h" #include "ui/views/view.h"
class DownloadShelfContextMenuView;
class DownloadShelfView; class DownloadShelfView;
namespace content { namespace content {
...@@ -68,8 +69,8 @@ class DownloadItemView : public views::View, ...@@ -68,8 +69,8 @@ class DownloadItemView : public views::View,
public: public:
enum class Mode; enum class Mode;
DownloadItemView(DownloadUIModel::DownloadUIModelPtr download, DownloadItemView(DownloadUIModel::DownloadUIModelPtr model,
DownloadShelfView* parent, DownloadShelfView* shelf,
views::View* accessible_alert); views::View* accessible_alert);
DownloadItemView(const DownloadItemView&) = delete; DownloadItemView(const DownloadItemView&) = delete;
DownloadItemView& operator=(const DownloadItemView&) = delete; DownloadItemView& operator=(const DownloadItemView&) = delete;
...@@ -209,13 +210,13 @@ class DownloadItemView : public views::View, ...@@ -209,13 +210,13 @@ class DownloadItemView : public views::View,
void ExecuteCommand(DownloadCommands::Command command); void ExecuteCommand(DownloadCommands::Command command);
// The model controlling this object's state. // The model controlling this object's state.
DownloadUIModel::DownloadUIModelPtr model_; const DownloadUIModel::DownloadUIModelPtr model_;
// A utility object to help execute commands on the model. // A utility object to help execute commands on the model.
DownloadCommands commands_{model()}; DownloadCommands commands_{model()};
// The download shelf that owns us. // The download shelf that owns us.
DownloadShelfView* shelf_; DownloadShelfView* const shelf_;
// Mode of the download item view. // Mode of the download item view.
Mode mode_; Mode mode_;
...@@ -257,7 +258,7 @@ class DownloadItemView : public views::View, ...@@ -257,7 +258,7 @@ class DownloadItemView : public views::View,
// Whether the dropdown is currently pressed. // Whether the dropdown is currently pressed.
bool dropdown_pressed_ = false; bool dropdown_pressed_ = false;
std::unique_ptr<DownloadShelfContextMenuView> context_menu_; DownloadShelfContextMenuView context_menu_{this};
base::RepeatingTimer indeterminate_progress_timer_; base::RepeatingTimer indeterminate_progress_timer_;
...@@ -286,6 +287,8 @@ class DownloadItemView : public views::View, ...@@ -286,6 +287,8 @@ class DownloadItemView : public views::View,
// Forces reading the current alert text the next time it updates. // Forces reading the current alert text the next time it updates.
bool announce_accessible_alert_soon_ = false; bool announce_accessible_alert_soon_ = false;
ScopedObserver<DownloadUIModel, DownloadUIModel::Observer> observer_{this};
// Method factory used to delay reenabling of the item when opening the // Method factory used to delay reenabling of the item when opening the
// downloaded file. // downloaded file.
base::WeakPtrFactory<DownloadItemView> weak_ptr_factory_{this}; base::WeakPtrFactory<DownloadItemView> weak_ptr_factory_{this};
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/check.h" #include "base/check.h"
#include "base/i18n/rtl.h" #include "base/i18n/rtl.h"
#include "chrome/browser/download/download_item_model.h" #include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/ui/views/download/download_item_view.h"
#include "components/download/public/common/download_item.h" #include "components/download/public/common/download_item.h"
#include "content/public/browser/page_navigator.h" #include "content/public/browser/page_navigator.h"
#include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/point.h"
......
...@@ -11,9 +11,10 @@ ...@@ -11,9 +11,10 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/download/download_shelf_context_menu.h" #include "chrome/browser/download/download_shelf_context_menu.h"
#include "chrome/browser/ui/views/download/download_item_view.h"
#include "ui/base/ui_base_types.h" #include "ui/base/ui_base_types.h"
class DownloadItemView;
namespace gfx { namespace gfx {
class Rect; class Rect;
} }
......
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