OLD | NEW |
---|---|
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 998 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 | 1018 |
1019 base::string16 label_text = dangerous_download_label_->text(); | 1019 dangerous_download_label_->SetSize( |
1020 AdjustTextAndGetSize(dangerous_download_label_)); | |
1021 dangerous_download_label_sized_ = true; | |
1022 } | |
1023 | |
1024 // static | |
1025 gfx::Size DownloadItemView::AdjustTextAndGetSize(views::Label* label) { | |
1026 gfx::Size size = label->GetPreferredSize(); | |
1027 | |
1028 // If the label is already narrower than kDangerousTextWidth, we don't need to | |
Peter Kasting
2016/12/08 21:53:52
Nit: Can we move the definition of kDangerousTextW
Jialiu Lin
2016/12/08 22:44:03
Agree, this is the only place using this const, I'
| |
1029 // linebreak it, as it will fit on a single line. | |
1030 if (size.width() <= kDangerousTextWidth) | |
1031 return size; | |
1032 | |
1033 base::string16 label_text = label->text(); | |
1020 base::TrimWhitespace(label_text, base::TRIM_ALL, &label_text); | 1034 base::TrimWhitespace(label_text, base::TRIM_ALL, &label_text); |
1021 DCHECK_EQ(base::string16::npos, label_text.find('\n')); | 1035 DCHECK_EQ(base::string16::npos, label_text.find('\n')); |
1022 | 1036 |
1023 // Make the label big so that GetPreferredSize() is not constrained by the | 1037 // Make the label big so that GetPreferredSize() is not constrained by the |
1024 // current width. | 1038 // current width. |
1025 dangerous_download_label_->SetBounds(0, 0, 1000, 1000); | 1039 label->SetBounds(0, 0, 1000, 1000); |
1026 | 1040 |
1027 // Use a const string from here. BreakIterator requies that text.data() not | 1041 // Use a const string from here. BreakIterator requies that text.data() not |
1028 // change during its lifetime. | 1042 // change during its lifetime. |
1029 const base::string16 original_text(label_text); | 1043 const base::string16 original_text(label_text); |
1030 // Using BREAK_WORD can work in most cases, but it can also break | 1044 // 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 | 1045 // lines where it should not. Using BREAK_LINE is safer although |
1032 // slower for Chinese/Japanese. This is not perf-critical at all, though. | 1046 // slower for Chinese/Japanese. This is not perf-critical at all, though. |
1033 base::i18n::BreakIterator iter(original_text, | 1047 base::i18n::BreakIterator iter(original_text, |
1034 base::i18n::BreakIterator::BREAK_LINE); | 1048 base::i18n::BreakIterator::BREAK_LINE); |
1035 bool status = iter.Init(); | 1049 bool status = iter.Init(); |
1036 DCHECK(status); | 1050 DCHECK(status); |
1037 | 1051 |
1052 // Go through the string and try each line break (starting with no line break) | |
1053 // searching for the optimal line break position. Stop if we find one that | |
1054 // yields minimum label width. | |
1038 base::string16 prev_text = original_text; | 1055 base::string16 prev_text = original_text; |
1039 gfx::Size size = dangerous_download_label_->GetPreferredSize(); | 1056 for (gfx::Size min_width_size = size; iter.Advance(); min_width_size = size) { |
1040 int min_width = size.width(); | |
1041 | |
1042 // 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 | |
1044 // yields one that is less than kDangerousTextWidth wide. This is to prevent | |
1045 // a short string (e.g.: "This file is malicious") from being broken up | |
1046 // unnecessarily. | |
1047 while (iter.Advance() && min_width > kDangerousTextWidth) { | |
1048 size_t pos = iter.pos(); | 1057 size_t pos = iter.pos(); |
1049 if (pos >= original_text.length()) | 1058 if (pos >= original_text.length()) |
1050 break; | 1059 break; |
1051 base::string16 current_text = original_text; | 1060 base::string16 current_text = original_text; |
1052 // This can be a low surrogate codepoint, but u_isUWhiteSpace will | 1061 // This can be a low surrogate codepoint, but u_isUWhiteSpace will |
1053 // return false and inserting a new line after a surrogate pair | 1062 // return false and inserting a new line after a surrogate pair |
1054 // is perfectly ok. | 1063 // is perfectly ok. |
1055 base::char16 line_end_char = current_text[pos - 1]; | 1064 base::char16 line_end_char = current_text[pos - 1]; |
1056 if (u_isUWhiteSpace(line_end_char)) | 1065 if (u_isUWhiteSpace(line_end_char)) |
1057 current_text.replace(pos - 1, 1, 1, base::char16('\n')); | 1066 current_text.replace(pos - 1, 1, 1, base::char16('\n')); |
1058 else | 1067 else |
1059 current_text.insert(pos, 1, base::char16('\n')); | 1068 current_text.insert(pos, 1, base::char16('\n')); |
1060 dangerous_download_label_->SetText(current_text); | 1069 label->SetText(current_text); |
1061 size = dangerous_download_label_->GetPreferredSize(); | 1070 size = label->GetPreferredSize(); |
1062 | 1071 |
1063 // If the width is growing again, it means we passed the optimal width spot. | 1072 // If the width is growing again, it means we passed the optimal width spot. |
1064 if (size.width() > min_width) { | 1073 if (size.width() > min_width_size.width()) { |
1065 dangerous_download_label_->SetText(prev_text); | 1074 label->SetText(prev_text); |
1066 break; | 1075 return min_width_size; |
1067 } else { | |
1068 min_width = size.width(); | |
1069 } | 1076 } |
1070 prev_text = current_text; | 1077 prev_text = current_text; |
1071 } | 1078 } |
1072 | 1079 return size; |
1073 dangerous_download_label_->SetSize(size); | |
1074 dangerous_download_label_sized_ = true; | |
1075 } | 1080 } |
1076 | 1081 |
1077 void DownloadItemView::Reenable() { | 1082 void DownloadItemView::Reenable() { |
1078 disabled_while_opening_ = false; | 1083 disabled_while_opening_ = false; |
1079 SetEnabled(true); // Triggers a repaint. | 1084 SetEnabled(true); // Triggers a repaint. |
1080 } | 1085 } |
1081 | 1086 |
1082 void DownloadItemView::ReleaseDropdown() { | 1087 void DownloadItemView::ReleaseDropdown() { |
1083 SetDropdownState(NORMAL); | 1088 SetDropdownState(NORMAL); |
1084 } | 1089 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1119 SchedulePaint(); | 1124 SchedulePaint(); |
1120 } | 1125 } |
1121 | 1126 |
1122 SkColor DownloadItemView::GetTextColor() const { | 1127 SkColor DownloadItemView::GetTextColor() const { |
1123 return GetTextColorForThemeProvider(GetThemeProvider()); | 1128 return GetTextColorForThemeProvider(GetThemeProvider()); |
1124 } | 1129 } |
1125 | 1130 |
1126 SkColor DownloadItemView::GetDimmedTextColor() const { | 1131 SkColor DownloadItemView::GetDimmedTextColor() const { |
1127 return SkColorSetA(GetTextColor(), 0xC7); | 1132 return SkColorSetA(GetTextColor(), 0xC7); |
1128 } | 1133 } |
OLD | NEW |