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

Side by Side Diff: chrome/browser/ui/views/download/download_item_view.cc

Issue 2556573002: Fix SizeLabelToMinWidth() function such that no unnecessary space (Closed)
Patch Set: nit 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/views/download/download_item_view.h" 5 #include "chrome/browser/ui/views/download/download_item_view.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <vector> 10 #include <vector>
(...skipping 997 matching lines...) Expand 10 before | Expand all | Expand 10 after
1008 size.SetToMax(save_button_->GetPreferredSize()); 1008 size.SetToMax(save_button_->GetPreferredSize());
1009 return size; 1009 return size;
1010 } 1010 }
1011 1011
1012 // This method computes the minimum width of the label for displaying its text 1012 // This method computes the minimum width of the label for displaying its text
1013 // on 2 lines. It just breaks the string in 2 lines on the spaces and keeps the 1013 // on 2 lines. It just breaks the string in 2 lines on the spaces and keeps the
1014 // configuration with minimum width. 1014 // configuration with minimum width.
1015 void DownloadItemView::SizeLabelToMinWidth() { 1015 void DownloadItemView::SizeLabelToMinWidth() {
1016 if (dangerous_download_label_sized_) 1016 if (dangerous_download_label_sized_)
1017 return; 1017 return;
1018 gfx::Size size = dangerous_download_label_->GetPreferredSize();
1019 // If the size of the label already smaller than kDangerousDownload, it can
1020 // fit into a single line. This is to prevent a short string (e.g.: "This file
1021 // is malicious") from being broken up unnecessarily.
Peter Kasting 2016/12/06 05:48:20 Nit: Grammar and constant naming problems. How ab
Jialiu Lin 2016/12/06 23:42:33 Ah.. auto complete.. Thanks for catching this.
1022 if (size.width() <= kDangerousTextWidth) {
1023 dangerous_download_label_->SetSize(size);
1024 dangerous_download_label_sized_ = true;
Peter Kasting 2016/12/06 05:48:20 Nit: Instead of doing this here, set it immediatel
Jialiu Lin 2016/12/06 23:42:33 There are actually two things to be set in this fu
Peter Kasting 2016/12/08 01:05:56 Return a tuple? Return the text to use, and recal
Jialiu Lin 2016/12/08 04:51:58 Done.
1025 return;
1026 }
1018 1027
1019 base::string16 label_text = dangerous_download_label_->text(); 1028 base::string16 label_text = dangerous_download_label_->text();
1020 base::TrimWhitespace(label_text, base::TRIM_ALL, &label_text); 1029 base::TrimWhitespace(label_text, base::TRIM_ALL, &label_text);
1021 DCHECK_EQ(base::string16::npos, label_text.find('\n')); 1030 DCHECK_EQ(base::string16::npos, label_text.find('\n'));
1022 1031
1023 // Make the label big so that GetPreferredSize() is not constrained by the 1032 // Make the label big so that GetPreferredSize() is not constrained by the
1024 // current width. 1033 // current width.
1025 dangerous_download_label_->SetBounds(0, 0, 1000, 1000); 1034 dangerous_download_label_->SetBounds(0, 0, 1000, 1000);
1026 1035
1027 // Use a const string from here. BreakIterator requies that text.data() not 1036 // Use a const string from here. BreakIterator requies that text.data() not
1028 // change during its lifetime. 1037 // change during its lifetime.
1029 const base::string16 original_text(label_text); 1038 const base::string16 original_text(label_text);
1030 // Using BREAK_WORD can work in most cases, but it can also break 1039 // Using BREAK_WORD can work in most cases, but it can also break
1031 // lines where it should not. Using BREAK_LINE is safer although 1040 // lines where it should not. Using BREAK_LINE is safer although
1032 // slower for Chinese/Japanese. This is not perf-critical at all, though. 1041 // slower for Chinese/Japanese. This is not perf-critical at all, though.
1033 base::i18n::BreakIterator iter(original_text, 1042 base::i18n::BreakIterator iter(original_text,
1034 base::i18n::BreakIterator::BREAK_LINE); 1043 base::i18n::BreakIterator::BREAK_LINE);
1035 bool status = iter.Init(); 1044 bool status = iter.Init();
1036 DCHECK(status); 1045 DCHECK(status);
1037 1046
1038 base::string16 prev_text = original_text; 1047 base::string16 prev_text = original_text;
1039 gfx::Size size = dangerous_download_label_->GetPreferredSize(); 1048
Peter Kasting 2016/12/06 05:48:20 Nit: Use at most one blank line at a time within a
Jialiu Lin 2016/12/06 23:42:33 Done.
1040 int min_width = size.width(); 1049
1050 gfx::Size min_width_size = size;
1041 1051
1042 // Go through the string and try each line break (starting with no line break) 1052 // Go through the string and try each line break (starting with no line break)
1043 // searching for the optimal line break position. Stop if we find one that 1053 // searching for the optimal line break position. Stop if we find one that
1044 // yields one that is less than kDangerousTextWidth wide. This is to prevent 1054 // yields minimum label width.
1045 // a short string (e.g.: "This file is malicious") from being broken up 1055 while (iter.Advance()) {
1046 // unnecessarily.
1047 while (iter.Advance() && min_width > kDangerousTextWidth) {
1048 size_t pos = iter.pos(); 1056 size_t pos = iter.pos();
1049 if (pos >= original_text.length()) 1057 if (pos >= original_text.length())
1050 break; 1058 break;
1051 base::string16 current_text = original_text; 1059 base::string16 current_text = original_text;
1052 // This can be a low surrogate codepoint, but u_isUWhiteSpace will 1060 // This can be a low surrogate codepoint, but u_isUWhiteSpace will
1053 // return false and inserting a new line after a surrogate pair 1061 // return false and inserting a new line after a surrogate pair
1054 // is perfectly ok. 1062 // is perfectly ok.
1055 base::char16 line_end_char = current_text[pos - 1]; 1063 base::char16 line_end_char = current_text[pos - 1];
1056 if (u_isUWhiteSpace(line_end_char)) 1064 if (u_isUWhiteSpace(line_end_char))
1057 current_text.replace(pos - 1, 1, 1, base::char16('\n')); 1065 current_text.replace(pos - 1, 1, 1, base::char16('\n'));
1058 else 1066 else
1059 current_text.insert(pos, 1, base::char16('\n')); 1067 current_text.insert(pos, 1, base::char16('\n'));
1060 dangerous_download_label_->SetText(current_text); 1068 dangerous_download_label_->SetText(current_text);
1061 size = dangerous_download_label_->GetPreferredSize(); 1069 size = dangerous_download_label_->GetPreferredSize();
1062 1070
1063 // If the width is growing again, it means we passed the optimal width spot. 1071 // If the width is growing again, it means we passed the optimal width spot.
1064 if (size.width() > min_width) { 1072 if (size.width() >= min_width_size.width()) {
Peter Kasting 2016/12/06 05:48:20 Changing this to >= seems worse. The effect is th
Jialiu Lin 2016/12/06 23:42:33 Agree, change to '>' will place as much text on th
1065 dangerous_download_label_->SetText(prev_text); 1073 dangerous_download_label_->SetText(prev_text);
1066 break; 1074 dangerous_download_label_->SetSize(min_width_size);
1075 dangerous_download_label_sized_ = true;
1076 return;
1067 } else { 1077 } else {
Peter Kasting 2016/12/06 05:48:20 Nit: No else after return
Jialiu Lin 2016/12/06 23:42:33 Done.
1068 min_width = size.width(); 1078 min_width_size = size;
1079 prev_text = current_text;
1069 } 1080 }
1070 prev_text = current_text;
1071 } 1081 }
1072 1082
1073 dangerous_download_label_->SetSize(size); 1083 dangerous_download_label_->SetSize(size);
1074 dangerous_download_label_sized_ = true; 1084 dangerous_download_label_sized_ = true;
1075 } 1085 }
1076 1086
1077 void DownloadItemView::Reenable() { 1087 void DownloadItemView::Reenable() {
1078 disabled_while_opening_ = false; 1088 disabled_while_opening_ = false;
1079 SetEnabled(true); // Triggers a repaint. 1089 SetEnabled(true); // Triggers a repaint.
1080 } 1090 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1119 SchedulePaint(); 1129 SchedulePaint();
1120 } 1130 }
1121 1131
1122 SkColor DownloadItemView::GetTextColor() const { 1132 SkColor DownloadItemView::GetTextColor() const {
1123 return GetTextColorForThemeProvider(GetThemeProvider()); 1133 return GetTextColorForThemeProvider(GetThemeProvider());
1124 } 1134 }
1125 1135
1126 SkColor DownloadItemView::GetDimmedTextColor() const { 1136 SkColor DownloadItemView::GetDimmedTextColor() const {
1127 return SkColorSetA(GetTextColor(), 0xC7); 1137 return SkColorSetA(GetTextColor(), 0xC7);
1128 } 1138 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698