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 <algorithm> | |
8 #include <utility> | |
9 | |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
9 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" | 12 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" |
10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 13 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
11 #include "content/public/browser/native_web_keyboard_event.h" | 14 #include "content/public/browser/native_web_keyboard_event.h" |
12 #include "grit/webkit_resources.h" | 15 #include "grit/webkit_resources.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
14 #include "ui/base/events/event.h" | 17 #include "ui/base/events/event.h" |
18 #include "ui/base/text/text_elider.h" | |
19 #include "ui/gfx/display.h" | |
20 #include "ui/gfx/screen.h" | |
21 #include "ui/gfx/vector2d.h" | |
15 | 22 |
16 using WebKit::WebAutofillClient; | 23 using WebKit::WebAutofillClient; |
17 | 24 |
18 namespace { | 25 namespace { |
19 | 26 |
20 // Used to indicate that no line is currently selected by the user. | 27 // Used to indicate that no line is currently selected by the user. |
21 const int kNoSelection = -1; | 28 const int kNoSelection = -1; |
22 | 29 |
23 // Size difference between name and subtext in pixels. | 30 // Size difference between name and subtext in pixels. |
24 const int kLabelFontSizeDelta = -2; | 31 const int kLabelFontSizeDelta = -2; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 const std::vector<string16>& icons, | 118 const std::vector<string16>& icons, |
112 const std::vector<int>& identifiers) { | 119 const std::vector<int>& identifiers) { |
113 names_ = names; | 120 names_ = names; |
114 subtexts_ = subtexts; | 121 subtexts_ = subtexts; |
115 icons_ = icons; | 122 icons_ = icons; |
116 identifiers_ = identifiers; | 123 identifiers_ = identifiers; |
117 | 124 |
118 #if !defined(OS_ANDROID) | 125 #if !defined(OS_ANDROID) |
119 // Android displays the long text with ellipsis using the view attributes. | 126 // Android displays the long text with ellipsis using the view attributes. |
120 | 127 |
121 // TODO(csharp): Fix crbug.com/156163 and use better logic when clipping. | 128 UpdatePopupBounds(); |
129 int popup_width = popup_bounds().width(); | |
130 | |
131 // Elide the name and subtext strings so that the popup fits in the available | |
132 // space. | |
122 for (size_t i = 0; i < names_.size(); ++i) { | 133 for (size_t i = 0; i < names_.size(); ++i) { |
123 if (names_[i].length() > 15) | 134 int name_width = name_font().GetStringWidth(names_[i]); |
124 names_[i].erase(15); | 135 int subtext_width = subtext_font().GetStringWidth(subtexts_[i]); |
125 if (subtexts[i].length() > 15) | 136 int total_text_length = name_width + subtext_width; |
126 subtexts_[i].erase(15); | 137 |
138 // The line can have no strings if it represents a UI element, such as | |
139 // a separator line. | |
140 if (total_text_length == 0) | |
141 continue; | |
142 | |
143 int available_width = popup_width - RowWidthWithoutText(i); | |
144 | |
145 // Each field recieves space in proportion to its length. | |
146 int name_size = available_width * name_width / total_text_length; | |
147 names_[i] = ui::ElideText(names_[i], | |
148 name_font(), | |
149 name_size, | |
150 ui::ELIDE_AT_END); | |
151 | |
152 int subtext_size = available_width * subtext_width / total_text_length; | |
153 subtexts_[i] = ui::ElideText(subtexts_[i], | |
154 subtext_font(), | |
155 subtext_size, | |
156 ui::ELIDE_AT_END); | |
127 } | 157 } |
128 #endif | 158 #endif |
129 | 159 |
130 if (!view_) { | 160 if (!view_) { |
131 view_ = AutofillPopupView::Create(this); | 161 view_ = AutofillPopupView::Create(this); |
132 ShowView(); | 162 ShowView(); |
133 } else { | 163 } else { |
134 UpdateBoundsAndRedrawPopup(); | 164 UpdateBoundsAndRedrawPopup(); |
135 } | 165 } |
136 } | 166 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 | 222 |
193 bool AutofillPopupControllerImpl::CanDelete(size_t index) { | 223 bool AutofillPopupControllerImpl::CanDelete(size_t index) { |
194 // TODO(isherman): AddressBook suggestions on Mac should not be drawn as | 224 // TODO(isherman): AddressBook suggestions on Mac should not be drawn as |
195 // deleteable. | 225 // deleteable. |
196 int id = identifiers_[index]; | 226 int id = identifiers_[index]; |
197 return id > 0 || | 227 return id > 0 || |
198 id == WebAutofillClient::MenuItemIDAutocompleteEntry || | 228 id == WebAutofillClient::MenuItemIDAutocompleteEntry || |
199 id == WebAutofillClient::MenuItemIDPasswordEntry; | 229 id == WebAutofillClient::MenuItemIDPasswordEntry; |
200 } | 230 } |
201 | 231 |
202 #if !defined(OS_ANDROID) | |
203 int AutofillPopupControllerImpl::GetPopupRequiredWidth() { | |
204 if (name_font_.platform_font() == NULL || | |
205 subtext_font_.platform_font() == NULL) { | |
206 // We can't calculate the size of the popup if the fonts | |
207 // aren't present. | |
208 return 0; | |
209 } | |
210 | |
211 int popup_width = element_bounds().width(); | |
212 DCHECK_EQ(names().size(), subtexts().size()); | |
213 for (size_t i = 0; i < names().size(); ++i) { | |
214 int row_size = kEndPadding + | |
215 name_font_.GetStringWidth(names()[i]) + | |
216 kNamePadding + | |
217 subtext_font_.GetStringWidth(subtexts()[i]); | |
218 | |
219 // Add the Autofill icon size, if required. | |
220 if (!icons()[i].empty()) | |
221 row_size += kAutofillIconWidth + kIconPadding; | |
222 | |
223 // Add delete icon, if required. | |
224 if (CanDelete(i)) | |
225 row_size += kDeleteIconWidth + kIconPadding; | |
226 | |
227 // Add the padding at the end | |
228 row_size += kEndPadding; | |
229 | |
230 popup_width = std::max(popup_width, row_size); | |
231 } | |
232 | |
233 return popup_width; | |
234 } | |
235 | |
236 int AutofillPopupControllerImpl::GetPopupRequiredHeight() { | |
237 int popup_height = 0; | |
238 | |
239 for (size_t i = 0; i < identifiers().size(); ++i) { | |
240 popup_height += GetRowHeightFromId(identifiers()[i]); | |
241 } | |
242 | |
243 return popup_height; | |
244 } | |
245 #endif // !defined(OS_ANDROID) | |
246 | |
247 gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) { | 232 gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) { |
248 int top = 0; | 233 int top = 0; |
249 for (size_t i = 0; i < index; ++i) { | 234 for (size_t i = 0; i < index; ++i) { |
250 top += GetRowHeightFromId(identifiers()[i]); | 235 top += GetRowHeightFromId(identifiers()[i]); |
251 } | 236 } |
252 | 237 |
253 return gfx::Rect( | 238 return gfx::Rect( |
254 0, | 239 0, |
255 top, | 240 top, |
256 popup_bounds_.width(), | 241 popup_bounds_.width(), |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
499 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); | 484 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); |
500 } | 485 } |
501 | 486 |
502 void AutofillPopupControllerImpl::ShowView() { | 487 void AutofillPopupControllerImpl::ShowView() { |
503 view_->Show(); | 488 view_->Show(); |
504 } | 489 } |
505 | 490 |
506 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { | 491 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { |
507 view_->InvalidateRow(row); | 492 view_->InvalidateRow(row); |
508 } | 493 } |
494 | |
495 #if !defined(OS_ANDROID) | |
496 int AutofillPopupControllerImpl::GetPopupRequiredWidth() { | |
Ilya Sherman
2013/01/15 01:34:45
nit: Perhaps name this "GetDesiredPopupWidth()", s
csharp
2013/01/15 22:06:39
Done.
| |
497 if (name_font_.platform_font() == NULL || | |
Ilya Sherman
2013/01/15 01:34:45
nit: if (!name_font_.platform_font() || !name_subt
csharp
2013/01/15 22:06:39
Done.
| |
498 subtext_font_.platform_font() == NULL) { | |
499 // We can't calculate the size of the popup if the fonts | |
500 // aren't present. | |
501 return 0; | |
502 } | |
503 | |
504 int popup_width = element_bounds().width(); | |
505 DCHECK_EQ(names().size(), subtexts().size()); | |
506 for (size_t i = 0; i < names().size(); ++i) { | |
507 int row_size = name_font_.GetStringWidth(names()[i]) + | |
508 subtext_font_.GetStringWidth(subtexts()[i]) + | |
509 RowWidthWithoutText(i); | |
510 | |
511 popup_width = std::max(popup_width, row_size); | |
512 } | |
513 | |
514 return popup_width; | |
515 } | |
516 | |
517 int AutofillPopupControllerImpl::GetPopupRequiredHeight() { | |
518 int popup_height = 0; | |
519 | |
520 for (size_t i = 0; i < identifiers().size(); ++i) { | |
521 popup_height += GetRowHeightFromId(identifiers()[i]); | |
522 } | |
523 | |
524 return popup_height; | |
525 } | |
526 | |
527 int AutofillPopupControllerImpl::RowWidthWithoutText(int row) { | |
528 int row_size = kEndPadding + kNamePadding; | |
529 | |
530 // Add the Autofill icon size, if required. | |
531 if (!icons_[row].empty()) | |
532 row_size += kAutofillIconWidth + kIconPadding; | |
533 | |
534 // Add the delete icon size, if required. | |
535 if (CanDelete(row)) | |
536 row_size += kDeleteIconWidth + kIconPadding; | |
537 | |
538 // Add the padding at the end | |
539 row_size += kEndPadding; | |
540 | |
541 return row_size; | |
542 } | |
543 | |
544 void AutofillPopupControllerImpl::UpdatePopupBounds() { | |
545 int popup_required_width = GetPopupRequiredWidth(); | |
546 int popup_height = GetPopupRequiredHeight(); | |
547 // This is the top left point if the popup is above the element and | |
548 // grows to the left (since that is the highest and furthest left the | |
549 // popup go could). | |
Ilya Sherman
2013/01/15 01:34:45
nit: It's a little weird to describe top_left_corn
csharp
2013/01/15 22:06:39
Done.
| |
550 gfx::Point top_left_corner_of_popup = element_bounds().origin() + | |
551 gfx::Vector2d(element_bounds().width() - popup_required_width, | |
552 -popup_height); | |
553 gfx::Point bottom_right_corner_of_popup = element_bounds().origin() + | |
554 gfx::Vector2d(popup_required_width, | |
555 element_bounds().height() + popup_height); | |
556 | |
557 gfx::Screen* screen = | |
558 gfx::Screen::GetScreenFor(container_view()); | |
559 gfx::Display top_left_display = screen->GetDisplayNearestPoint( | |
560 element_bounds().origin()); | |
Ilya Sherman
2013/01/15 01:34:45
Hmm, why isn't this top_left_corner_of_popup?
csharp
2013/01/15 22:06:39
I blame a brain fart :) Fixed
| |
561 gfx::Display bottom_right_display = screen->GetDisplayNearestPoint( | |
562 bottom_right_corner_of_popup); | |
563 | |
564 std::pair<int, int> popup_x_and_width = CalculatePopupXAndWidth( | |
565 top_left_display, bottom_right_display, popup_required_width); | |
566 std::pair<int, int> popup_y_and_height = CalculatePopupYAndHeight( | |
567 top_left_display, bottom_right_display, popup_height); | |
568 | |
569 popup_bounds_ = gfx::Rect(popup_x_and_width.first, | |
570 popup_y_and_height.first, | |
571 popup_x_and_width.second, | |
572 popup_y_and_height.second); | |
573 } | |
574 #endif // !defined(OS_ANDROID) | |
575 | |
576 std::pair<int, int> AutofillPopupControllerImpl::CalculatePopupXAndWidth( | |
577 const gfx::Display& left_display, | |
578 const gfx::Display& right_display, | |
579 int popup_required_width) { | |
580 int leftmost_display_x = left_display.bounds().x() * | |
581 left_display.device_scale_factor(); | |
582 int rightmost_display_x = right_display.GetSizeInPixel().width() + | |
583 right_display.bounds().x() * right_display.device_scale_factor(); | |
584 | |
585 // Calculate the leftmost and rightmost values the popup can have without | |
Ilya Sherman
2013/01/15 01:34:45
nit: s/values/coordinates
csharp
2013/01/15 22:06:39
Done.
| |
586 // going off the screen. | |
587 int popup_left_cutoff = std::max(element_bounds().origin().x(), | |
588 leftmost_display_x); | |
589 int popup_right_cutoff = std::min(element_bounds().top_right().x(), | |
590 rightmost_display_x); | |
Ilya Sherman
2013/01/15 01:34:45
Optional nit: Mebbe drop the "popup_" prefix?
csharp
2013/01/15 22:06:39
Done.
| |
591 | |
592 int right_available = rightmost_display_x - popup_left_cutoff; | |
593 int left_available = popup_right_cutoff - leftmost_display_x; | |
594 | |
595 int popup_width = std::min(popup_required_width, | |
596 std::max(right_available, left_available)); | |
597 | |
598 // If there is enough space for the popup on the right, show it there, | |
599 // otherwise choose the larger size. | |
600 if (right_available >= popup_width || right_available >= left_available) | |
601 return std::make_pair(popup_left_cutoff, popup_width); | |
602 else | |
603 return std::make_pair(popup_right_cutoff - popup_width, popup_width); | |
604 } | |
605 | |
606 std::pair<int,int> AutofillPopupControllerImpl::CalculatePopupYAndHeight( | |
607 const gfx::Display& top_display, | |
608 const gfx::Display& bottom_display, | |
609 int popup_required_height) { | |
610 // Find the correct top position of the popup so that it doesn't go off | |
611 // the screen. | |
612 int bottom_display_y = bottom_display.GetSizeInPixel().height() + | |
613 (bottom_display.bounds().y() * | |
614 bottom_display.device_scale_factor()); | |
615 int bottom_of_field = element_bounds().bottom(); | |
616 | |
617 if (bottom_display_y < bottom_of_field + popup_required_height) { | |
618 // The popup must appear above the field. | |
619 return std::make_pair(element_bounds().y() - popup_required_height, | |
620 popup_required_height); | |
621 } else { | |
622 // The popup can appear below the field. | |
623 return std::make_pair(bottom_of_field, popup_required_height); | |
624 } | |
625 } | |
OLD | NEW |