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 UpdatePopupBounds(); |
Ilya Sherman
2013/01/17 01:36:19
Mightn't this cause the popup to jump w.r.t. where
csharp
2013/01/17 16:31:20
Ya, I'm not sure what the best option here is sinc
| |
178 popup_bounds_.set_height(GetPopupRequiredHeight()); | |
179 #endif | 209 #endif |
180 | 210 |
181 view_->UpdateBoundsAndRedrawPopup(); | 211 view_->UpdateBoundsAndRedrawPopup(); |
182 } | 212 } |
183 | 213 |
184 void AutofillPopupControllerImpl::MouseHovered(int x, int y) { | 214 void AutofillPopupControllerImpl::MouseHovered(int x, int y) { |
185 SetSelectedLine(LineFromY(y)); | 215 SetSelectedLine(LineFromY(y)); |
186 | 216 |
187 bool delete_icon_hovered = DeleteIconIsUnder(x, y); | 217 bool delete_icon_hovered = DeleteIconIsUnder(x, y); |
188 if (delete_icon_hovered != delete_icon_hovered_) { | 218 if (delete_icon_hovered != delete_icon_hovered_) { |
189 delete_icon_hovered_ = delete_icon_hovered; | 219 delete_icon_hovered_ = delete_icon_hovered; |
190 InvalidateRow(selected_line()); | 220 InvalidateRow(selected_line()); |
191 } | 221 } |
192 } | 222 } |
193 | 223 |
194 void AutofillPopupControllerImpl::MouseClicked(int x, int y) { | 224 void AutofillPopupControllerImpl::MouseClicked(int x, int y) { |
195 MouseHovered(x, y); | 225 MouseHovered(x, y); |
196 | 226 |
197 if (delete_icon_hovered_) | 227 if (delete_icon_hovered_) |
198 RemoveSelectedLine(); | 228 RemoveSelectedLine(); |
199 else | 229 else |
200 AcceptSelectedLine(); | 230 AcceptSelectedLine(); |
201 } | 231 } |
202 | 232 |
203 void AutofillPopupControllerImpl::MouseExitedPopup() { | 233 void AutofillPopupControllerImpl::MouseExitedPopup() { |
204 SetSelectedLine(kNoSelection); | 234 SetSelectedLine(kNoSelection); |
205 } | 235 } |
206 | 236 |
207 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { | 237 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { |
208 delegate_->DidAcceptSuggestion(names_[index], identifiers_[index]); | 238 delegate_->DidAcceptSuggestion(full_names_[index], identifiers_[index]); |
209 } | 239 } |
210 | 240 |
211 int AutofillPopupControllerImpl::GetIconResourceID( | 241 int AutofillPopupControllerImpl::GetIconResourceID( |
212 const string16& resource_name) { | 242 const string16& resource_name) { |
213 for (size_t i = 0; i < arraysize(kDataResources); ++i) { | 243 for (size_t i = 0; i < arraysize(kDataResources); ++i) { |
214 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) | 244 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) |
215 return kDataResources[i].id; | 245 return kDataResources[i].id; |
216 } | 246 } |
217 | 247 |
218 return -1; | 248 return -1; |
219 } | 249 } |
220 | 250 |
221 bool AutofillPopupControllerImpl::CanDelete(size_t index) { | 251 bool AutofillPopupControllerImpl::CanDelete(size_t index) const { |
222 // TODO(isherman): AddressBook suggestions on Mac should not be drawn as | 252 // TODO(isherman): AddressBook suggestions on Mac should not be drawn as |
223 // deleteable. | 253 // deleteable. |
224 int id = identifiers_[index]; | 254 int id = identifiers_[index]; |
225 return id > 0 || | 255 return id > 0 || |
226 id == WebAutofillClient::MenuItemIDAutocompleteEntry || | 256 id == WebAutofillClient::MenuItemIDAutocompleteEntry || |
227 id == WebAutofillClient::MenuItemIDPasswordEntry; | 257 id == WebAutofillClient::MenuItemIDPasswordEntry; |
228 } | 258 } |
229 | 259 |
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) { | 260 gfx::Rect AutofillPopupControllerImpl::GetRowBounds(size_t index) { |
276 int top = 0; | 261 int top = 0; |
277 for (size_t i = 0; i < index; ++i) { | 262 for (size_t i = 0; i < index; ++i) { |
278 top += GetRowHeightFromId(identifiers()[i]); | 263 top += GetRowHeightFromId(identifiers()[i]); |
279 } | 264 } |
280 | 265 |
281 return gfx::Rect( | 266 return gfx::Rect( |
282 0, | 267 0, |
283 top, | 268 top, |
284 popup_bounds_.width(), | 269 popup_bounds_.width(), |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 bool AutofillPopupControllerImpl::RemoveSelectedLine() { | 399 bool AutofillPopupControllerImpl::RemoveSelectedLine() { |
415 if (selected_line_ == kNoSelection) | 400 if (selected_line_ == kNoSelection) |
416 return false; | 401 return false; |
417 | 402 |
418 DCHECK_GE(selected_line_, 0); | 403 DCHECK_GE(selected_line_, 0); |
419 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); | 404 DCHECK_LT(selected_line_, static_cast<int>(names_.size())); |
420 | 405 |
421 if (!CanDelete(selected_line_)) | 406 if (!CanDelete(selected_line_)) |
422 return false; | 407 return false; |
423 | 408 |
424 delegate_->RemoveSuggestion(names_[selected_line_], | 409 delegate_->RemoveSuggestion(full_names_[selected_line_], |
425 identifiers_[selected_line_]); | 410 identifiers_[selected_line_]); |
426 | 411 |
427 // Remove the deleted element. | 412 // Remove the deleted element. |
428 names_.erase(names_.begin() + selected_line_); | 413 names_.erase(names_.begin() + selected_line_); |
414 full_names_.erase(full_names_.begin() + selected_line_); | |
429 subtexts_.erase(subtexts_.begin() + selected_line_); | 415 subtexts_.erase(subtexts_.begin() + selected_line_); |
430 icons_.erase(icons_.begin() + selected_line_); | 416 icons_.erase(icons_.begin() + selected_line_); |
431 identifiers_.erase(identifiers_.begin() + selected_line_); | 417 identifiers_.erase(identifiers_.begin() + selected_line_); |
432 | 418 |
433 SetSelectedLine(kNoSelection); | 419 SetSelectedLine(kNoSelection); |
434 | 420 |
435 if (HasSuggestions()) { | 421 if (HasSuggestions()) { |
436 delegate_->ClearPreviewedForm(); | 422 delegate_->ClearPreviewedForm(); |
437 UpdateBoundsAndRedrawPopup(); | 423 UpdateBoundsAndRedrawPopup(); |
438 } else { | 424 } else { |
(...skipping 10 matching lines...) Expand all Loading... | |
449 current_height += GetRowHeightFromId(identifiers()[i]); | 435 current_height += GetRowHeightFromId(identifiers()[i]); |
450 | 436 |
451 if (y <= current_height) | 437 if (y <= current_height) |
452 return i; | 438 return i; |
453 } | 439 } |
454 | 440 |
455 // The y value goes beyond the popup so stop the selection at the last line. | 441 // The y value goes beyond the popup so stop the selection at the last line. |
456 return identifiers().size() - 1; | 442 return identifiers().size() - 1; |
457 } | 443 } |
458 | 444 |
459 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) { | 445 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier) const { |
460 if (identifier == WebAutofillClient::MenuItemIDSeparator) | 446 if (identifier == WebAutofillClient::MenuItemIDSeparator) |
461 return kSeparatorHeight; | 447 return kSeparatorHeight; |
462 | 448 |
463 return kRowHeight; | 449 return kRowHeight; |
464 } | 450 } |
465 | 451 |
466 bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) { | 452 bool AutofillPopupControllerImpl::DeleteIconIsUnder(int x, int y) { |
467 #if defined(OS_ANDROID) | 453 #if defined(OS_ANDROID) |
468 return false; | 454 return false; |
469 #else | 455 #else |
470 if (!CanDelete(selected_line())) | 456 if (!CanDelete(selected_line())) |
471 return false; | 457 return false; |
472 | 458 |
473 int row_start_y = 0; | 459 int row_start_y = 0; |
474 for (int i = 0; i < selected_line(); ++i) { | 460 for (int i = 0; i < selected_line(); ++i) { |
475 row_start_y += GetRowHeightFromId(identifiers()[i]); | 461 row_start_y += GetRowHeightFromId(identifiers()[i]); |
476 } | 462 } |
477 | 463 |
478 gfx::Rect delete_icon_bounds = gfx::Rect( | 464 gfx::Rect delete_icon_bounds = gfx::Rect( |
479 GetPopupRequiredWidth() - kDeleteIconWidth - kIconPadding, | 465 popup_bounds().width() - kDeleteIconWidth - kIconPadding, |
480 row_start_y + ((kRowHeight - kDeleteIconHeight) / 2), | 466 row_start_y + ((kRowHeight - kDeleteIconHeight) / 2), |
481 kDeleteIconWidth, | 467 kDeleteIconWidth, |
482 kDeleteIconHeight); | 468 kDeleteIconHeight); |
483 | 469 |
484 return delete_icon_bounds.Contains(x, y); | 470 return delete_icon_bounds.Contains(x, y); |
485 #endif | 471 #endif |
486 } | 472 } |
487 | 473 |
488 bool AutofillPopupControllerImpl::CanAccept(int id) { | 474 bool AutofillPopupControllerImpl::CanAccept(int id) { |
489 return id != WebAutofillClient::MenuItemIDSeparator && | 475 return id != WebAutofillClient::MenuItemIDSeparator && |
490 id != WebAutofillClient::MenuItemIDWarningMessage; | 476 id != WebAutofillClient::MenuItemIDWarningMessage; |
491 } | 477 } |
492 | 478 |
493 bool AutofillPopupControllerImpl::HasSuggestions() { | 479 bool AutofillPopupControllerImpl::HasSuggestions() { |
494 return identifiers_.size() != 0 && | 480 return identifiers_.size() != 0 && |
495 (identifiers_[0] > 0 || | 481 (identifiers_[0] > 0 || |
496 identifiers_[0] == | 482 identifiers_[0] == |
497 WebAutofillClient::MenuItemIDAutocompleteEntry || | 483 WebAutofillClient::MenuItemIDAutocompleteEntry || |
498 identifiers_[0] == WebAutofillClient::MenuItemIDPasswordEntry || | 484 identifiers_[0] == WebAutofillClient::MenuItemIDPasswordEntry || |
499 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); | 485 identifiers_[0] == WebAutofillClient::MenuItemIDDataListEntry); |
500 } | 486 } |
501 | 487 |
502 void AutofillPopupControllerImpl::ShowView() { | 488 void AutofillPopupControllerImpl::ShowView() { |
503 view_->Show(); | 489 view_->Show(); |
504 } | 490 } |
505 | 491 |
506 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { | 492 void AutofillPopupControllerImpl::InvalidateRow(size_t row) { |
507 view_->InvalidateRow(row); | 493 view_->InvalidateRow(row); |
508 } | 494 } |
495 | |
496 #if !defined(OS_ANDROID) | |
497 int AutofillPopupControllerImpl::GetDesiredPopupWidth() const { | |
498 if (!name_font_.platform_font() || !subtext_font_.platform_font()) { | |
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::GetDesiredPopupHeight() const { | |
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) const { | |
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 = GetDesiredPopupWidth(); | |
546 int popup_height = GetDesiredPopupHeight(); | |
547 // This is the top left point of the popup if the popup is above the element | |
548 // and grows to the left (since that is the highest and furthest left the | |
549 // popup go could). | |
550 gfx::Point top_left_corner_of_popup = element_bounds().origin() + | |
551 gfx::Vector2d(element_bounds().width() - popup_required_width, | |
552 -popup_height); | |
553 | |
554 // This is the bottom right point of the popup if the popup is below the | |
555 // element and grows to the right (since the is the lowest and furthest right | |
556 // the popup could go). | |
557 gfx::Point bottom_right_corner_of_popup = element_bounds().origin() + | |
558 gfx::Vector2d(popup_required_width, | |
559 element_bounds().height() + popup_height); | |
560 | |
561 gfx::Display top_left_display = GetDisplayNearestPoint( | |
562 top_left_corner_of_popup); | |
563 gfx::Display bottom_right_display = GetDisplayNearestPoint( | |
564 bottom_right_corner_of_popup); | |
565 | |
566 std::pair<int, int> popup_x_and_width = CalculatePopupXAndWidth( | |
567 top_left_display, bottom_right_display, popup_required_width); | |
568 std::pair<int, int> popup_y_and_height = CalculatePopupYAndHeight( | |
569 top_left_display, bottom_right_display, popup_height); | |
570 | |
571 popup_bounds_ = gfx::Rect(popup_x_and_width.first, | |
572 popup_y_and_height.first, | |
573 popup_x_and_width.second, | |
574 popup_y_and_height.second); | |
575 } | |
576 #endif // !defined(OS_ANDROID) | |
577 | |
578 gfx::Display AutofillPopupControllerImpl::GetDisplayNearestPoint( | |
579 const gfx::Point& point) const { | |
580 return gfx::Screen::GetScreenFor(container_view())->GetDisplayNearestPoint( | |
581 point); | |
582 } | |
583 | |
584 std::pair<int, int> AutofillPopupControllerImpl::CalculatePopupXAndWidth( | |
585 const gfx::Display& left_display, | |
586 const gfx::Display& right_display, | |
587 int popup_required_width) const { | |
588 int leftmost_display_x = left_display.bounds().x() * | |
589 left_display.device_scale_factor(); | |
590 int rightmost_display_x = right_display.GetSizeInPixel().width() + | |
591 right_display.bounds().x() * right_display.device_scale_factor(); | |
592 | |
593 // Calculate the right and left cutoff positions, which are the the left and | |
Ilya Sherman
2013/01/17 01:36:19
nit: "the the" -> "the"
csharp
2013/01/17 16:31:20
Done.
| |
594 // right parts of the element bounds, capped to screen space. | |
595 int left_cutoff = std::max(leftmost_display_x, | |
596 std::min(rightmost_display_x, | |
597 element_bounds().x())); | |
Ilya Sherman
2013/01/17 01:36:19
I don't understand this std::min -- what is it for
csharp
2013/01/17 16:31:20
Ok, I've tried cleaning up the comment and variabl
Ilya Sherman
2013/01/18 01:52:48
Ah, I think I understand what you mean. Subtle.
| |
598 int right_cutoff = std::max(leftmost_display_x, | |
599 std::min(rightmost_display_x, | |
600 element_bounds().right())); | |
601 | |
602 int right_available = rightmost_display_x - left_cutoff; | |
603 int left_available = right_cutoff - leftmost_display_x; | |
604 | |
605 int popup_width = std::min(popup_required_width, | |
606 std::max(right_available, left_available)); | |
607 | |
608 // If there is enough space for the popup on the right, show it there, | |
609 // otherwise choose the larger size. | |
610 if (right_available >= popup_width || right_available >= left_available) | |
611 return std::make_pair(left_cutoff, popup_width); | |
612 else | |
613 return std::make_pair(right_cutoff - popup_width, popup_width); | |
614 } | |
615 | |
616 std::pair<int,int> AutofillPopupControllerImpl::CalculatePopupYAndHeight( | |
617 const gfx::Display& top_display, | |
618 const gfx::Display& bottom_display, | |
619 int popup_required_height) const { | |
620 int topmost_display_y = top_display.bounds().y() * | |
621 top_display.device_scale_factor(); | |
622 int bottommost_display_y = bottom_display.GetSizeInPixel().height() + | |
623 (bottom_display.bounds().y() * | |
624 bottom_display.device_scale_factor()); | |
625 | |
626 // Calculate the right and left cutoff positions, which are the the left and | |
627 // right parts of the element bounds, capped to screen space. | |
Ilya Sherman
2013/01/17 01:36:19
nit: s/right and left/top and bottom/
csharp
2013/01/17 16:31:20
Done.
| |
628 int top_cutoff = std::max(topmost_display_y, | |
629 std::min(bottommost_display_y, | |
630 element_bounds().y())); | |
631 int bottom_cutoff = std::max(topmost_display_y, | |
632 std::min(bottommost_display_y, | |
633 element_bounds().bottom())); | |
634 | |
635 int top_available = bottom_cutoff - topmost_display_y; | |
636 int bottom_available = bottommost_display_y - top_cutoff; | |
637 | |
638 // TODO(csharp): Restrict the popup height to what is available. | |
639 if (bottom_available >= popup_required_height || | |
640 bottom_available >= top_available) { | |
641 // The popup can appear below the field. | |
642 return std::make_pair(bottom_cutoff, popup_required_height); | |
643 } else { | |
644 // The popup must appear above the field. | |
645 return std::make_pair(top_cutoff - popup_required_height, | |
646 popup_required_height); | |
647 } | |
648 } | |
OLD | NEW |