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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (inform_delegate_of_destruction_) | 111 if (inform_delegate_of_destruction_) |
105 delegate_->ControllerDestroyed(); | 112 delegate_->ControllerDestroyed(); |
106 } | 113 } |
107 | 114 |
108 void AutofillPopupControllerImpl::Show( | 115 void AutofillPopupControllerImpl::Show( |
109 const std::vector<string16>& names, | 116 const std::vector<string16>& names, |
110 const std::vector<string16>& subtexts, | 117 const std::vector<string16>& subtexts, |
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; |
| 121 full_names_ = names; |
114 subtexts_ = subtexts; | 122 subtexts_ = subtexts; |
115 icons_ = icons; | 123 icons_ = icons; |
116 identifiers_ = identifiers; | 124 identifiers_ = identifiers; |
117 | 125 |
118 #if !defined(OS_ANDROID) | 126 #if !defined(OS_ANDROID) |
119 // Android displays the long text with ellipsis using the view attributes. | 127 // Android displays the long text with ellipsis using the view attributes. |
120 | 128 |
121 // TODO(csharp): Fix crbug.com/156163 and use better logic when clipping. | 129 UpdatePopupBounds(); |
| 130 int popup_width = popup_bounds().width(); |
| 131 |
| 132 // Elide the name and subtext strings so that the popup fits in the available |
| 133 // space. |
122 for (size_t i = 0; i < names_.size(); ++i) { | 134 for (size_t i = 0; i < names_.size(); ++i) { |
123 if (names_[i].length() > 15) | 135 int name_width = name_font().GetStringWidth(names_[i]); |
124 names_[i].erase(15); | 136 int subtext_width = subtext_font().GetStringWidth(subtexts_[i]); |
125 if (subtexts[i].length() > 15) | 137 int total_text_length = name_width + subtext_width; |
126 subtexts_[i].erase(15); | 138 |
| 139 // The line can have no strings if it represents a UI element, such as |
| 140 // a separator line. |
| 141 if (total_text_length == 0) |
| 142 continue; |
| 143 |
| 144 int available_width = popup_width - RowWidthWithoutText(i); |
| 145 |
| 146 // Each field recieves space in proportion to its length. |
| 147 int name_size = available_width * name_width / total_text_length; |
| 148 names_[i] = ui::ElideText(names_[i], |
| 149 name_font(), |
| 150 name_size, |
| 151 ui::ELIDE_AT_END); |
| 152 |
| 153 int subtext_size = available_width * subtext_width / total_text_length; |
| 154 subtexts_[i] = ui::ElideText(subtexts_[i], |
| 155 subtext_font(), |
| 156 subtext_size, |
| 157 ui::ELIDE_AT_END); |
127 } | 158 } |
128 #endif | 159 #endif |
129 | 160 |
130 if (!view_) { | 161 if (!view_) { |
131 view_ = AutofillPopupView::Create(this); | 162 view_ = AutofillPopupView::Create(this); |
132 ShowView(); | 163 ShowView(); |
133 } else { | 164 } else { |
134 UpdateBoundsAndRedrawPopup(); | 165 UpdateBoundsAndRedrawPopup(); |
135 } | 166 } |
136 } | 167 } |
(...skipping 30 matching lines...) Expand all Loading... |
167 return false; | 198 return false; |
168 } | 199 } |
169 } | 200 } |
170 | 201 |
171 void AutofillPopupControllerImpl::ViewDestroyed() { | 202 void AutofillPopupControllerImpl::ViewDestroyed() { |
172 delete this; | 203 delete this; |
173 } | 204 } |
174 | 205 |
175 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { | 206 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() { |
176 #if !defined(OS_ANDROID) | 207 #if !defined(OS_ANDROID) |
177 popup_bounds_.set_width(GetPopupRequiredWidth()); | 208 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup, |
178 popup_bounds_.set_height(GetPopupRequiredHeight()); | 209 // the popup could end up jumping from above the element to below it. |
| 210 // It is unclear if it is better to keep the popup where it was, or if it |
| 211 // should try and move to its desired position. |
| 212 UpdatePopupBounds(); |
179 #endif | 213 #endif |
180 | 214 |
181 view_->UpdateBoundsAndRedrawPopup(); | 215 view_->UpdateBoundsAndRedrawPopup(); |
182 } | 216 } |
183 | 217 |
184 void AutofillPopupControllerImpl::MouseHovered(int x, int y) { | 218 void AutofillPopupControllerImpl::MouseHovered(int x, int y) { |
185 SetSelectedLine(LineFromY(y)); | 219 SetSelectedLine(LineFromY(y)); |
186 | 220 |
187 bool delete_icon_hovered = DeleteIconIsUnder(x, y); | 221 bool delete_icon_hovered = DeleteIconIsUnder(x, y); |
188 if (delete_icon_hovered != delete_icon_hovered_) { | 222 if (delete_icon_hovered != delete_icon_hovered_) { |
189 delete_icon_hovered_ = delete_icon_hovered; | 223 delete_icon_hovered_ = delete_icon_hovered; |
190 InvalidateRow(selected_line()); | 224 InvalidateRow(selected_line()); |
191 } | 225 } |
192 } | 226 } |
193 | 227 |
194 void AutofillPopupControllerImpl::MouseClicked(int x, int y) { | 228 void AutofillPopupControllerImpl::MouseClicked(int x, int y) { |
195 MouseHovered(x, y); | 229 MouseHovered(x, y); |
196 | 230 |
197 if (delete_icon_hovered_) | 231 if (delete_icon_hovered_) |
198 RemoveSelectedLine(); | 232 RemoveSelectedLine(); |
199 else | 233 else |
200 AcceptSelectedLine(); | 234 AcceptSelectedLine(); |
201 } | 235 } |
202 | 236 |
203 void AutofillPopupControllerImpl::MouseExitedPopup() { | 237 void AutofillPopupControllerImpl::MouseExitedPopup() { |
204 SetSelectedLine(kNoSelection); | 238 SetSelectedLine(kNoSelection); |
205 } | 239 } |
206 | 240 |
207 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { | 241 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { |
208 delegate_->DidAcceptSuggestion(names_[index], identifiers_[index]); | 242 delegate_->DidAcceptSuggestion(full_names_[index], identifiers_[index]); |
209 } | 243 } |
210 | 244 |
211 int AutofillPopupControllerImpl::GetIconResourceID( | 245 int AutofillPopupControllerImpl::GetIconResourceID( |
212 const string16& resource_name) { | 246 const string16& resource_name) { |
213 for (size_t i = 0; i < arraysize(kDataResources); ++i) { | 247 for (size_t i = 0; i < arraysize(kDataResources); ++i) { |
214 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) | 248 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) |
215 return kDataResources[i].id; | 249 return kDataResources[i].id; |
216 } | 250 } |
217 | 251 |
218 return -1; | 252 return -1; |
219 } | 253 } |
220 | 254 |
221 bool AutofillPopupControllerImpl::CanDelete(size_t index) { | 255 bool AutofillPopupControllerImpl::CanDelete(size_t index) const { |
222 // TODO(isherman): AddressBook suggestions on Mac should not be drawn as | 256 // TODO(isherman): AddressBook suggestions on Mac should not be drawn as |
223 // deleteable. | 257 // deleteable. |
224 int id = identifiers_[index]; | 258 int id = identifiers_[index]; |
225 return id > 0 || | 259 return id > 0 || |
226 id == WebAutofillClient::MenuItemIDAutocompleteEntry || | 260 id == WebAutofillClient::MenuItemIDAutocompleteEntry || |
227 id == WebAutofillClient::MenuItemIDPasswordEntry; | 261 id == WebAutofillClient::MenuItemIDPasswordEntry; |
228 } | 262 } |
229 | 263 |
230 #if !defined(OS_ANDROID) | |
231 int AutofillPopupControllerImpl::GetPopupRequiredWidth() { | |
232 if (name_font_.platform_font() == NULL || | |
233 subtext_font_.platform_font() == NULL) { | |
234 // We can't calculate the size of the popup if the fonts | |
235 // aren't present. | |
236 return 0; | |
237 } | |
238 | |
239 int popup_width = element_bounds().width(); | |
240 DCHECK_EQ(names().size(), subtexts().size()); | |
241 for (size_t i = 0; i < names().size(); ++i) { | |
242 int row_size = kEndPadding + | |
243 name_font_.GetStringWidth(names()[i]) + | |
244 kNamePadding + | |
245 subtext_font_.GetStringWidth(subtexts()[i]); | |
246 | |
247 // Add the Autofill icon size, if required. | |
248 if (!icons()[i].empty()) | |
249 row_size += kAutofillIconWidth + kIconPadding; | |
250 | |
251 // Add delete icon, if required. | |
252 if (CanDelete(i)) | |
253 row_size += kDeleteIconWidth + kIconPadding; | |
254 | |
255 // Add the padding at the end | |
256 row_size += kEndPadding; | |
257 | |
258 popup_width = std::max(popup_width, row_size); | |
259 } | |
260 | |
261 return popup_width; | |
262 } | |
263 | |
264 int AutofillPopupControllerImpl::GetPopupRequiredHeight() { | |
265 int popup_height = 0; | |
266 | |
267 for (size_t i = 0; i < identifiers().size(); ++i) { | |
268 popup_height += GetRowHeightFromId(identifiers()[i]); | |
269 } | |
270 | |
271 return popup_height; | |
272 } | |
273 #endif // !defined(OS_ANDROID) | |
274 | |
275 gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) { | 264 gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) { |
276 int top = 0; | 265 int top = 0; |
277 for (size_t i = 0; i < index; ++i) { | 266 for (size_t i = 0; i < index; ++i) { |
278 top += GetRowHeightFromId(identifiers()[i]); | 267 top += GetRowHeightFromId(identifiers()[i]); |
279 } | 268 } |
280 | 269 |
281 return gfx::Rect( | 270 return gfx::Rect( |
282 0, | 271 0, |
283 top, | 272 top, |
284 popup_bounds_.width(), | 273 popup_bounds_.width(), |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 bool AutofillPopupControllerImpl::RemoveSelectedLine() { | 403 bool AutofillPopupControllerImpl::RemoveSelectedLine() { |
415 if (selected_line_ == kNoSelection) | 404 if (selected_line_ == kNoSelection) |
416 return false; | 405 return false; |
417 | 406 |
418 DCHECK_GE(selected_line_, 0); | 407 DCHECK_GE(selected_line_, 0); |
419 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); | 408 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); |
420 | 409 |
421 if (!CanDelete(selected_line_)) | 410 if (!CanDelete(selected_line_)) |
422 return false; | 411 return false; |
423 | 412 |
424 delegate_->RemoveSuggestion(names_[selected_line_], | 413 delegate_->RemoveSuggestion(full_names_[selected_line_], |
425 identifiers_[selected_line_]); | 414 identifiers_[selected_line_]); |
426 | 415 |
427 // Remove the deleted element. | 416 // Remove the deleted element. |
428 names_.erase(names_.begin() + selected_line_); | 417 names_.erase(names_.begin() + selected_line_); |
| 418 full_names_.erase(full_names_.begin() + selected_line_); |
429 subtexts_.erase(subtexts_.begin() + selected_line_); | 419 subtexts_.erase(subtexts_.begin() + selected_line_); |
430 icons_.erase(icons_.begin() + selected_line_); | 420 icons_.erase(icons_.begin() + selected_line_); |
431 identifiers_.erase(identifiers_.begin() + selected_line_); | 421 identifiers_.erase(identifiers_.begin() + selected_line_); |
432 | 422 |
433 SetSelectedLine(kNoSelection); | 423 SetSelectedLine(kNoSelection); |
434 | 424 |
435 if (HasSuggestions()) { | 425 if (HasSuggestions()) { |
436 delegate_->ClearPreviewedForm(); | 426 delegate_->ClearPreviewedForm(); |
437 UpdateBoundsAndRedrawPopup(); | 427 UpdateBoundsAndRedrawPopup(); |
438 } else { | 428 } else { |
(...skipping 10 matching lines...) Expand all Loading... |
449 current_height += GetRowHeightFromId(identifiers()[i]); | 439 current_height += GetRowHeightFromId(identifiers()[i]); |
450 | 440 |
451 if (y <= current_height) | 441 if (y <= current_height) |
452 return i; | 442 return i; |
453 } | 443 } |
454 | 444 |
455 // The y value goes beyond the popup so stop the selection at the last line. | 445 // The y value goes beyond the popup so stop the selection at the last line. |
456 return identifiers().size() - 1; | 446 return identifiers().size() - 1; |
457 } | 447 } |
458 | 448 |
459 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) { | 449 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) const { |
460 if (identifier == WebAutofillClient::MenuItemIDSeparator) | 450 if (identifier == WebAutofillClient::MenuItemIDSeparator) |
461 return kSeparatorHeight; | 451 return kSeparatorHeight; |
462 | 452 |
463 return kRowHeight; | 453 return kRowHeight; |
464 } | 454 } |
465 | 455 |
466 bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) { | 456 bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) { |
467 #if defined(OS_ANDROID) | 457 #if defined(OS_ANDROID) |
468 return false; | 458 return false; |
469 #else | 459 #else |
470 if (!CanDelete(selected_line())) | 460 if (!CanDelete(selected_line())) |
471 return false; | 461 return false; |
472 | 462 |
473 int row_start_y = 0; | 463 int row_start_y = 0; |
474 for (int i = 0; i < selected_line(); ++i) { | 464 for (int i = 0; i < selected_line(); ++i) { |
475 row_start_y += GetRowHeightFromId(identifiers()[i]); | 465 row_start_y += GetRowHeightFromId(identifiers()[i]); |
476 } | 466 } |
477 | 467 |
478 gfx::Rect delete_icon_bounds = gfx::Rect( | 468 gfx::Rect delete_icon_bounds = gfx::Rect( |
479 GetPopupRequiredWidth() - kDeleteIconWidth - kIconPadding, | 469 popup_bounds().width() - kDeleteIconWidth - kIconPadding, |
480 row_start_y + ((kRowHeight - kDeleteIconHeight) / 2), | 470 row_start_y + ((kRowHeight - kDeleteIconHeight) / 2), |
481 kDeleteIconWidth, | 471 kDeleteIconWidth, |
482 kDeleteIconHeight); | 472 kDeleteIconHeight); |
483 | 473 |
484 return delete_icon_bounds.Contains(x, y); | 474 return delete_icon_bounds.Contains(x, y); |
485 #endif | 475 #endif |
486 } | 476 } |
487 | 477 |
488 bool AutofillPopupControllerImpl::CanAccept(int id) { | 478 bool AutofillPopupControllerImpl::CanAccept(int id) { |
489 return id != WebAutofillClient::MenuItemIDSeparator && | 479 return id != WebAutofillClient::MenuItemIDSeparator && |
490 id != WebAutofillClient::MenuItemIDWarningMessage; | 480 id != WebAutofillClient::MenuItemIDWarningMessage; |
491 } | 481 } |
492 | 482 |
493 bool AutofillPopupControllerImpl::HasSuggestions() { | 483 bool AutofillPopupControllerImpl::HasSuggestions() { |
494 return identifiers_.size() != 0 && | 484 return identifiers_.size() != 0 && |
495 (identifiers_[0] > 0 || | 485 (identifiers_[0] > 0 || |
496 identifiers_[0] == | 486 identifiers_[0] == |
497 WebAutofillClient::MenuItemIDAutocompleteEntry || | 487 WebAutofillClient::MenuItemIDAutocompleteEntry || |
498 identifiers_[0] == WebAutofillClient::MenuItemIDPasswordEntry || | 488 identifiers_[0] == WebAutofillClient::MenuItemIDPasswordEntry || |
499 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); | 489 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); |
500 } | 490 } |
501 | 491 |
502 void AutofillPopupControllerImpl::ShowView() { | 492 void AutofillPopupControllerImpl::ShowView() { |
503 view_->Show(); | 493 view_->Show(); |
504 } | 494 } |
505 | 495 |
506 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { | 496 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { |
507 view_->InvalidateRow(row); | 497 view_->InvalidateRow(row); |
508 } | 498 } |
| 499 |
| 500 #if !defined(OS_ANDROID) |
| 501 int AutofillPopupControllerImpl::GetDesiredPopupWidth() const { |
| 502 if (!name_font_.platform_font() || !subtext_font_.platform_font()) { |
| 503 // We can't calculate the size of the popup if the fonts |
| 504 // aren't present. |
| 505 return 0; |
| 506 } |
| 507 |
| 508 int popup_width = element_bounds().width(); |
| 509 DCHECK_EQ(names().size(), subtexts().size()); |
| 510 for (size_t i = 0; i < names().size(); ++i) { |
| 511 int row_size = name_font_.GetStringWidth(names()[i]) + |
| 512 subtext_font_.GetStringWidth(subtexts()[i]) + |
| 513 RowWidthWithoutText(i); |
| 514 |
| 515 popup_width = std::max(popup_width, row_size); |
| 516 } |
| 517 |
| 518 return popup_width; |
| 519 } |
| 520 |
| 521 int AutofillPopupControllerImpl::GetDesiredPopupHeight() const { |
| 522 int popup_height = 0; |
| 523 |
| 524 for (size_t i = 0; i < identifiers().size(); ++i) { |
| 525 popup_height += GetRowHeightFromId(identifiers()[i]); |
| 526 } |
| 527 |
| 528 return popup_height; |
| 529 } |
| 530 |
| 531 int AutofillPopupControllerImpl::RowWidthWithoutText(int row) const { |
| 532 int row_size = kEndPadding + kNamePadding; |
| 533 |
| 534 // Add the Autofill icon size, if required. |
| 535 if (!icons_[row].empty()) |
| 536 row_size += kAutofillIconWidth + kIconPadding; |
| 537 |
| 538 // Add the delete icon size, if required. |
| 539 if (CanDelete(row)) |
| 540 row_size += kDeleteIconWidth + kIconPadding; |
| 541 |
| 542 // Add the padding at the end |
| 543 row_size += kEndPadding; |
| 544 |
| 545 return row_size; |
| 546 } |
| 547 |
| 548 void AutofillPopupControllerImpl::UpdatePopupBounds() { |
| 549 int popup_required_width = GetDesiredPopupWidth(); |
| 550 int popup_height = GetDesiredPopupHeight(); |
| 551 // This is the top left point of the popup if the popup is above the element |
| 552 // and grows to the left (since that is the highest and furthest left the |
| 553 // popup go could). |
| 554 gfx::Point top_left_corner_of_popup = element_bounds().origin() + |
| 555 gfx::Vector2d(element_bounds().width() - popup_required_width, |
| 556 -popup_height); |
| 557 |
| 558 // This is the bottom right point of the popup if the popup is below the |
| 559 // element and grows to the right (since the is the lowest and furthest right |
| 560 // the popup could go). |
| 561 gfx::Point bottom_right_corner_of_popup = element_bounds().origin() + |
| 562 gfx::Vector2d(popup_required_width, |
| 563 element_bounds().height() + popup_height); |
| 564 |
| 565 gfx::Display top_left_display = GetDisplayNearestPoint( |
| 566 top_left_corner_of_popup); |
| 567 gfx::Display bottom_right_display = GetDisplayNearestPoint( |
| 568 bottom_right_corner_of_popup); |
| 569 |
| 570 std::pair<int, int> popup_x_and_width = CalculatePopupXAndWidth( |
| 571 top_left_display, bottom_right_display, popup_required_width); |
| 572 std::pair<int, int> popup_y_and_height = CalculatePopupYAndHeight( |
| 573 top_left_display, bottom_right_display, popup_height); |
| 574 |
| 575 popup_bounds_ = gfx::Rect(popup_x_and_width.first, |
| 576 popup_y_and_height.first, |
| 577 popup_x_and_width.second, |
| 578 popup_y_and_height.second); |
| 579 } |
| 580 #endif // !defined(OS_ANDROID) |
| 581 |
| 582 gfx::Display AutofillPopupControllerImpl::GetDisplayNearestPoint( |
| 583 const gfx::Point& point) const { |
| 584 return gfx::Screen::GetScreenFor(container_view())->GetDisplayNearestPoint( |
| 585 point); |
| 586 } |
| 587 |
| 588 std::pair<int, int> AutofillPopupControllerImpl::CalculatePopupXAndWidth( |
| 589 const gfx::Display& left_display, |
| 590 const gfx::Display& right_display, |
| 591 int popup_required_width) const { |
| 592 int leftmost_display_x = left_display.bounds().x() * |
| 593 left_display.device_scale_factor(); |
| 594 int rightmost_display_x = right_display.GetSizeInPixel().width() + |
| 595 right_display.bounds().x() * right_display.device_scale_factor(); |
| 596 |
| 597 // Calculate the start coordinates for the popup if it is growing right or |
| 598 // the end position if it is growing to the left, capped to screen space. |
| 599 int right_growth_start = std::max(leftmost_display_x, |
| 600 std::min(rightmost_display_x, |
| 601 element_bounds().x())); |
| 602 int left_growth_end = std::max(leftmost_display_x, |
| 603 std::min(rightmost_display_x, |
| 604 element_bounds().right())); |
| 605 |
| 606 int right_available = rightmost_display_x - right_growth_start; |
| 607 int left_available = left_growth_end - leftmost_display_x; |
| 608 |
| 609 int popup_width = std::min(popup_required_width, |
| 610 std::max(right_available, left_available)); |
| 611 |
| 612 // If there is enough space for the popup on the right, show it there, |
| 613 // otherwise choose the larger size. |
| 614 if (right_available >= popup_width || right_available >= left_available) |
| 615 return std::make_pair(right_growth_start, popup_width); |
| 616 else |
| 617 return std::make_pair(left_growth_end - popup_width, popup_width); |
| 618 } |
| 619 |
| 620 std::pair<int,int> AutofillPopupControllerImpl::CalculatePopupYAndHeight( |
| 621 const gfx::Display& top_display, |
| 622 const gfx::Display& bottom_display, |
| 623 int popup_required_height) const { |
| 624 int topmost_display_y = top_display.bounds().y() * |
| 625 top_display.device_scale_factor(); |
| 626 int bottommost_display_y = bottom_display.GetSizeInPixel().height() + |
| 627 (bottom_display.bounds().y() * |
| 628 bottom_display.device_scale_factor()); |
| 629 |
| 630 // Calculate the start coordinates for the popup if it is growing down or |
| 631 // the end position if it is growing up, capped to screen space. |
| 632 int top_growth_end = std::max(topmost_display_y, |
| 633 std::min(bottommost_display_y, |
| 634 element_bounds().y())); |
| 635 int bottom_growth_start = std::max(topmost_display_y, |
| 636 std::min(bottommost_display_y, |
| 637 element_bounds().bottom())); |
| 638 |
| 639 int top_available = bottom_growth_start - topmost_display_y; |
| 640 int bottom_available = bottommost_display_y - top_growth_end; |
| 641 |
| 642 // TODO(csharp): Restrict the popup height to what is available. |
| 643 if (bottom_available >= popup_required_height || |
| 644 bottom_available >= top_available) { |
| 645 // The popup can appear below the field. |
| 646 return std::make_pair(bottom_growth_start, popup_required_height); |
| 647 } else { |
| 648 // The popup must appear above the field. |
| 649 return std::make_pair(top_growth_end - popup_required_height, |
| 650 popup_required_height); |
| 651 } |
| 652 } |
OLD | NEW |