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/autofill/autofill_popup_controller_impl.h" | 5 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" | 9 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" |
10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
11 #include "content/public/browser/native_web_keyboard_event.h" | 11 #include "content/public/browser/native_web_keyboard_event.h" |
12 #include "grit/webkit_resources.h" | 12 #include "grit/webkit_resources.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
14 #include "ui/base/events/event.h" | 14 #include "ui/base/events/event.h" |
15 #include "ui/base/text/text_elider.h" | |
16 #include "ui/gfx/display.h" | |
17 #include "ui/gfx/screen.h" | |
18 #include "ui/gfx/vector2d.h" | |
15 | 19 |
16 using WebKit::WebAutofillClient; | 20 using WebKit::WebAutofillClient; |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 // Used to indicate that no line is currently selected by the user. | 24 // Used to indicate that no line is currently selected by the user. |
21 const int kNoSelection = -1; | 25 const int kNoSelection = -1; |
22 | 26 |
23 // Size difference between name and subtext in pixels. | 27 // Size difference between name and subtext in pixels. |
24 const int kLabelFontSizeDelta = -2; | 28 const int kLabelFontSizeDelta = -2; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 const std::vector<string16>& icons, | 115 const std::vector<string16>& icons, |
112 const std::vector<int>& identifiers) { | 116 const std::vector<int>& identifiers) { |
113 names_ = names; | 117 names_ = names; |
114 subtexts_ = subtexts; | 118 subtexts_ = subtexts; |
115 icons_ = icons; | 119 icons_ = icons; |
116 identifiers_ = identifiers; | 120 identifiers_ = identifiers; |
117 | 121 |
118 #if !defined(OS_ANDROID) | 122 #if !defined(OS_ANDROID) |
119 // Android displays the long text with ellipsis using the view attributes. | 123 // Android displays the long text with ellipsis using the view attributes. |
120 | 124 |
121 // TODO(csharp): Fix crbug.com/156163 and use better logic when clipping. | 125 // Don't let the popup have a smaller width then then element it is a popup |
Ilya Sherman
2013/01/10 23:34:36
nit: "then then" -> "than the"
csharp
2013/01/11 17:51:12
Done.
| |
126 // for. If the text field is already going of the screen, then the popup | |
127 // should follow. | |
Ilya Sherman
2013/01/10 23:34:36
As I mention below, this is almost certainly wrong
csharp
2013/01/11 17:51:12
Done.
| |
128 int max_popup_width = std::max(MaxVisiblePopupWidth(), | |
129 element_bounds().width()); | |
130 | |
122 for (size_t i = 0; i < names_.size(); ++i) { | 131 for (size_t i = 0; i < names_.size(); ++i) { |
Ilya Sherman
2013/01/10 23:34:36
nit: Please add a comment indicating that this loo
csharp
2013/01/11 17:51:12
Done.
| |
123 if (names_[i].length() > 15) | 132 int total_text_length = name_font().GetStringWidth(names[i])+ |
Ilya Sherman
2013/01/10 23:34:36
nit: Add a space before the plus sign.
csharp
2013/01/11 17:51:12
Done.
| |
124 names_[i].erase(15); | 133 subtext_font().GetStringWidth(subtexts_[i]); |
125 if (subtexts[i].length() > 15) | 134 // The line can have no strings if it represents a UI element, such as |
126 subtexts_[i].erase(15); | 135 // a separator line. |
136 if (total_text_length == 0) | |
137 continue; | |
138 | |
139 int row_available_width = max_popup_width - RowWidthWithoutText(i); | |
Ilya Sherman
2013/01/10 23:34:36
Optional nit: Drop the "row_" prefix IMO
csharp
2013/01/11 17:51:12
Done.
| |
140 | |
141 // Each field recieves space in proportion to it length. | |
Ilya Sherman
2013/01/10 23:34:36
nit: "it length" -> "its length"
csharp
2013/01/11 17:51:12
Done.
| |
142 int name_size = row_available_width * | |
143 name_font().GetStringWidth(names_[i]) / total_text_length; | |
144 names_[i] = ui::ElideText(names_[i], | |
145 name_font(), | |
146 name_size, | |
147 ui::ELIDE_AT_END); | |
148 | |
149 int subtext_size = row_available_width * | |
150 subtext_font().GetStringWidth(subtexts[i]) / total_text_length; | |
151 subtexts_[i] = ui::ElideText(subtexts_[i], | |
152 subtext_font(), | |
153 subtext_size, | |
154 ui::ELIDE_AT_END); | |
127 } | 155 } |
128 #endif | 156 #endif |
129 | 157 |
130 if (!view_) { | 158 if (!view_) { |
131 view_ = AutofillPopupView::Create(this); | 159 view_ = AutofillPopupView::Create(this); |
132 ShowView(); | 160 ShowView(); |
133 } else { | 161 } else { |
134 UpdateBoundsAndRedrawPopup(); | 162 UpdateBoundsAndRedrawPopup(); |
135 } | 163 } |
136 } | 164 } |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 if (name_font_.platform_font() == NULL || | 230 if (name_font_.platform_font() == NULL || |
203 subtext_font_.platform_font() == NULL) { | 231 subtext_font_.platform_font() == NULL) { |
204 // We can't calculate the size of the popup if the fonts | 232 // We can't calculate the size of the popup if the fonts |
205 // aren't present. | 233 // aren't present. |
206 return 0; | 234 return 0; |
207 } | 235 } |
208 | 236 |
209 int popup_width = element_bounds().width(); | 237 int popup_width = element_bounds().width(); |
210 DCHECK_EQ(names().size(), subtexts().size()); | 238 DCHECK_EQ(names().size(), subtexts().size()); |
211 for (size_t i = 0; i < names().size(); ++i) { | 239 for (size_t i = 0; i < names().size(); ++i) { |
212 int row_size = kEndPadding + | 240 int row_size = name_font_.GetStringWidth(names()[i]) + |
213 name_font_.GetStringWidth(names()[i]) + | 241 subtext_font_.GetStringWidth(subtexts()[i]) + |
214 kNamePadding + | 242 RowWidthWithoutText(i); |
215 subtext_font_.GetStringWidth(subtexts()[i]); | |
216 | |
217 // Add the Autofill icon size, if required. | |
218 if (!icons()[i].empty()) | |
219 row_size += kAutofillIconWidth + kIconPadding; | |
220 | |
221 // Add delete icon, if required. | |
222 if (CanDelete(i)) | |
223 row_size += kDeleteIconWidth + kIconPadding; | |
224 | |
225 // Add the padding at the end | |
226 row_size += kEndPadding; | |
227 | 243 |
228 popup_width = std::max(popup_width, row_size); | 244 popup_width = std::max(popup_width, row_size); |
229 } | 245 } |
230 | 246 |
231 return popup_width; | 247 return popup_width; |
232 } | 248 } |
233 | 249 |
234 int AutofillPopupControllerImpl::GetPopupRequiredHeight() { | 250 int AutofillPopupControllerImpl::GetPopupRequiredHeight() { |
235 int popup_height = 0; | 251 int popup_height = 0; |
236 | 252 |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
497 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); | 513 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); |
498 } | 514 } |
499 | 515 |
500 void AutofillPopupControllerImpl::ShowView() { | 516 void AutofillPopupControllerImpl::ShowView() { |
501 view_->Show(); | 517 view_->Show(); |
502 } | 518 } |
503 | 519 |
504 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { | 520 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { |
505 view_->InvalidateRow(row); | 521 view_->InvalidateRow(row); |
506 } | 522 } |
523 | |
524 int AutofillPopupControllerImpl::RowWidthWithoutText(int row) { | |
525 int row_size = kEndPadding + kNamePadding; | |
526 | |
527 // Add the Autofill icon size, if required. | |
528 if (!icons_[row].empty()) | |
529 row_size += kAutofillIconWidth + kIconPadding; | |
530 | |
531 // Add delete icon, if required. | |
Ilya Sherman
2013/01/10 23:34:36
nit: "Add the delete icon size", or perhaps "Add s
csharp
2013/01/11 17:51:12
Done.
| |
532 if (CanDelete(row)) | |
533 row_size += kDeleteIconWidth + kIconPadding; | |
534 | |
535 // Add the padding at the end | |
536 row_size += kEndPadding; | |
537 | |
538 return row_size; | |
539 } | |
540 | |
541 | |
Ilya Sherman
2013/01/10 23:34:36
nit: Extra newline; please remove.
csharp
2013/01/11 17:51:12
Done.
| |
542 int AutofillPopupControllerImpl::MaxVisiblePopupWidth() { | |
543 gfx::Point right_corner_of_popup = element_bounds().origin() + | |
Ilya Sherman
2013/01/10 23:34:36
When anchoring to the left edge of the field, we s
csharp
2013/01/11 17:51:12
Ah, I hadn't been aware of that issue. Fixed.
| |
544 gfx::Vector2d(GetPopupRequiredWidth(), 0); | |
545 gfx::Screen* screen = | |
546 gfx::Screen::GetScreenFor(container_view()); | |
547 gfx::Display display = screen->GetDisplayNearestPoint(right_corner_of_popup); | |
Ilya Sherman
2013/01/10 23:34:36
In addition to anchoring to the left edge of the f
csharp
2013/01/11 17:51:12
Done.
| |
548 | |
549 int rightmost_display_x = display.GetSizeInPixel().width() + | |
Ilya Sherman
2013/01/10 23:34:36
nit: Extra space after the equals sign.
csharp
2013/01/11 17:51:12
Done.
| |
550 display.bounds().x() * display.device_scale_factor(); | |
551 | |
552 return rightmost_display_x - element_bounds().origin().x(); | |
553 } | |
OLD | NEW |