Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Unified Diff: chrome/browser/ui/views/download/download_item_view.cc

Issue 2556573002: Fix SizeLabelToMinWidth() function such that no unnecessary space (Closed)
Patch Set: 2nd round comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/views/download/download_item_view.cc
diff --git a/chrome/browser/ui/views/download/download_item_view.cc b/chrome/browser/ui/views/download/download_item_view.cc
index 99b5344d90e8e6a49eaaed4000c68bd01650753a..5e1f2989933c8082664edf2513f786a67f3afef3 100644
--- a/chrome/browser/ui/views/download/download_item_view.cc
+++ b/chrome/browser/ui/views/download/download_item_view.cc
@@ -183,7 +183,7 @@ DownloadItemView::DownloadItemView(DownloadItem* download_item,
save_button_(nullptr),
discard_button_(nullptr),
dropdown_button_(new DropDownButton(this)),
- dangerous_download_label_(nullptr),
+ dangerous_download_label_(new views::Label()),
dangerous_download_label_sized_(false),
disabled_while_opening_(false),
creation_time_(base::Time::Now()),
@@ -223,6 +223,9 @@ DownloadItemView::~DownloadItemView() {
// without keeping it.
if (sampling_event_)
sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kIgnore);
+
+ delete dangerous_download_label_;
+ dangerous_download_label_ = nullptr;
Peter Kasting 2016/12/08 05:10:23 This potentially double-frees. Views is expecting
Jialiu Lin 2016/12/08 07:47:40 Ack.
}
// Progress animation handlers.
@@ -343,7 +346,8 @@ void DownloadItemView::OnDownloadUpdated(DownloadItem* download_item) {
}
void DownloadItemView::OnDownloadDestroyed(DownloadItem* download) {
- shelf_->RemoveDownloadView(this); // This will delete us!
+ if (shelf_)
+ shelf_->RemoveDownloadView(this); // This will delete us!
}
void DownloadItemView::OnDownloadOpened(DownloadItem* download) {
@@ -795,12 +799,16 @@ void DownloadItemView::SubmitDownloadWhenFeedbackServiceEnabled(
}
void DownloadItemView::LoadIcon() {
- IconManager* im = g_browser_process->icon_manager();
- last_download_item_path_ = download()->GetTargetFilePath();
- im->LoadIcon(last_download_item_path_, IconLoader::SMALL,
- base::Bind(&DownloadItemView::OnExtractIconComplete,
- base::Unretained(this)),
- &cancelable_task_tracker_);
+ // If there is no download shelf assigned, this is called in a test, then no
+ // need to load the icon.
Peter Kasting 2016/12/08 05:10:23 Does it actually hurt to load this icon in a test?
Jialiu Lin 2016/12/08 07:47:40 Unfortunately LoadIcon function involves shelf_. S
+ if (shelf_) {
+ IconManager* im = g_browser_process->icon_manager();
+ last_download_item_path_ = download()->GetTargetFilePath();
+ im->LoadIcon(last_download_item_path_, IconLoader::SMALL,
+ base::Bind(&DownloadItemView::OnExtractIconComplete,
+ base::Unretained(this)),
+ &cancelable_task_tracker_);
+ }
}
void DownloadItemView::LoadIconIfItemPathChanged() {
@@ -970,7 +978,7 @@ void DownloadItemView::ShowWarningDialog() {
base::string16 dangerous_label =
model_.GetWarningText(font_list_, kTextWidth);
- dangerous_download_label_ = new views::Label(dangerous_label);
+ dangerous_download_label_->SetText(dangerous_label);
dangerous_download_label_->SetMultiLine(true);
dangerous_download_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
dangerous_download_label_->SetAutoColorReadabilityEnabled(false);
@@ -1016,6 +1024,18 @@ void DownloadItemView::SizeLabelToMinWidth() {
if (dangerous_download_label_sized_)
return;
+ dangerous_download_label_->SetSize(GetLabelSize());
+ dangerous_download_label_sized_ = true;
+}
+
+gfx::Size DownloadItemView::GetLabelSize() {
+ gfx::Size size = dangerous_download_label_->GetPreferredSize();
+
+ // If the label is already narrower than kDangerousTextWidth, we don't need to
+ // linebreak it, as it will fit on a single line.
+ if (size.width() <= kDangerousTextWidth)
+ return size;
+
base::string16 label_text = dangerous_download_label_->text();
base::TrimWhitespace(label_text, base::TRIM_ALL, &label_text);
DCHECK_EQ(base::string16::npos, label_text.find('\n'));
@@ -1035,16 +1055,11 @@ void DownloadItemView::SizeLabelToMinWidth() {
bool status = iter.Init();
DCHECK(status);
- base::string16 prev_text = original_text;
- gfx::Size size = dangerous_download_label_->GetPreferredSize();
- int min_width = size.width();
-
// Go through the string and try each line break (starting with no line break)
- // searching for the optimal line break position. Stop if we find one that
- // yields one that is less than kDangerousTextWidth wide. This is to prevent
- // a short string (e.g.: "This file is malicious") from being broken up
- // unnecessarily.
- while (iter.Advance() && min_width > kDangerousTextWidth) {
+ // searching for the optimal line break position. Stop if we find one that
+ // yields minimum label width.
+ base::string16 prev_text = original_text;
+ for (gfx::Size min_width_size = size; iter.Advance(); min_width_size = size) {
size_t pos = iter.pos();
if (pos >= original_text.length())
break;
@@ -1061,17 +1076,13 @@ void DownloadItemView::SizeLabelToMinWidth() {
size = dangerous_download_label_->GetPreferredSize();
// If the width is growing again, it means we passed the optimal width spot.
- if (size.width() > min_width) {
+ if (size.width() > min_width_size.width()) {
dangerous_download_label_->SetText(prev_text);
- break;
- } else {
- min_width = size.width();
+ return min_width_size;
}
prev_text = current_text;
}
-
- dangerous_download_label_->SetSize(size);
- dangerous_download_label_sized_ = true;
+ return size;
}
void DownloadItemView::Reenable() {

Powered by Google App Engine
This is Rietveld 408576698