autofill_popup.cc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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
  3. // found in the LICENSE file.
  4. #include <algorithm>
  5. #include <utility>
  6. #include <vector>
  7. #include "atom/browser/native_window_views.h"
  8. #include "atom/browser/ui/autofill_popup.h"
  9. #include "atom/common/api/api_messages.h"
  10. #include "ui/display/display.h"
  11. #include "ui/display/screen.h"
  12. #include "ui/gfx/geometry/point.h"
  13. #include "ui/gfx/geometry/rect.h"
  14. #include "ui/gfx/geometry/rect_conversions.h"
  15. #include "ui/gfx/geometry/vector2d.h"
  16. #include "ui/gfx/text_utils.h"
  17. #if defined(ENABLE_OSR)
  18. #include "atom/browser/osr/osr_render_widget_host_view.h"
  19. #include "atom/browser/osr/osr_view_proxy.h"
  20. #endif
  21. namespace atom {
  22. namespace {
  23. std::pair<int, int> CalculatePopupXAndWidth(
  24. const display::Display& left_display,
  25. const display::Display& right_display,
  26. int popup_required_width,
  27. const gfx::Rect& element_bounds,
  28. bool is_rtl) {
  29. int leftmost_display_x = left_display.bounds().x();
  30. int rightmost_display_x =
  31. right_display.GetSizeInPixel().width() + right_display.bounds().x();
  32. // Calculate the start coordinates for the popup if it is growing right or
  33. // the end position if it is growing to the left, capped to screen space.
  34. int right_growth_start = std::max(
  35. leftmost_display_x, std::min(rightmost_display_x, element_bounds.x()));
  36. int left_growth_end =
  37. std::max(leftmost_display_x,
  38. std::min(rightmost_display_x, element_bounds.right()));
  39. int right_available = rightmost_display_x - right_growth_start;
  40. int left_available = left_growth_end - leftmost_display_x;
  41. int popup_width =
  42. std::min(popup_required_width, std::max(right_available, left_available));
  43. std::pair<int, int> grow_right(right_growth_start, popup_width);
  44. std::pair<int, int> grow_left(left_growth_end - popup_width, popup_width);
  45. // Prefer to grow towards the end (right for LTR, left for RTL). But if there
  46. // is not enough space available in the desired direction and more space in
  47. // the other direction, reverse it.
  48. if (is_rtl) {
  49. return left_available >= popup_width || left_available >= right_available
  50. ? grow_left
  51. : grow_right;
  52. }
  53. return right_available >= popup_width || right_available >= left_available
  54. ? grow_right
  55. : grow_left;
  56. }
  57. std::pair<int, int> CalculatePopupYAndHeight(
  58. const display::Display& top_display,
  59. const display::Display& bottom_display,
  60. int popup_required_height,
  61. const gfx::Rect& element_bounds) {
  62. int topmost_display_y = top_display.bounds().y();
  63. int bottommost_display_y =
  64. bottom_display.GetSizeInPixel().height() + bottom_display.bounds().y();
  65. // Calculate the start coordinates for the popup if it is growing down or
  66. // the end position if it is growing up, capped to screen space.
  67. int top_growth_end = std::max(
  68. topmost_display_y, std::min(bottommost_display_y, element_bounds.y()));
  69. int bottom_growth_start =
  70. std::max(topmost_display_y,
  71. std::min(bottommost_display_y, element_bounds.bottom()));
  72. int top_available = bottom_growth_start - topmost_display_y;
  73. int bottom_available = bottommost_display_y - top_growth_end;
  74. // TODO(csharp): Restrict the popup height to what is available.
  75. if (bottom_available >= popup_required_height ||
  76. bottom_available >= top_available) {
  77. // The popup can appear below the field.
  78. return std::make_pair(bottom_growth_start, popup_required_height);
  79. } else {
  80. // The popup must appear above the field.
  81. return std::make_pair(top_growth_end - popup_required_height,
  82. popup_required_height);
  83. }
  84. }
  85. display::Display GetDisplayNearestPoint(const gfx::Point& point) {
  86. return display::Screen::GetScreen()->GetDisplayNearestPoint(point);
  87. }
  88. } // namespace
  89. AutofillPopup::AutofillPopup() {
  90. bold_font_list_ = gfx::FontList().DeriveWithWeight(gfx::Font::Weight::BOLD);
  91. smaller_font_list_ =
  92. gfx::FontList().DeriveWithSizeDelta(kSmallerFontSizeDelta);
  93. }
  94. AutofillPopup::~AutofillPopup() {
  95. Hide();
  96. }
  97. void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
  98. bool offscreen,
  99. views::View* parent,
  100. const gfx::RectF& r) {
  101. Hide();
  102. frame_host_ = frame_host;
  103. element_bounds_ = gfx::ToEnclosedRect(r);
  104. parent_ = parent;
  105. parent_->AddObserver(this);
  106. view_ = new AutofillPopupView(this, parent->GetWidget());
  107. view_->Show();
  108. #if defined(ENABLE_OSR)
  109. if (offscreen) {
  110. auto* osr_rwhv =
  111. static_cast<OffScreenRenderWidgetHostView*>(frame_host_->GetView());
  112. view_->view_proxy_.reset(new OffscreenViewProxy(view_));
  113. osr_rwhv->AddViewProxy(view_->view_proxy_.get());
  114. }
  115. #endif
  116. }
  117. void AutofillPopup::Hide() {
  118. if (parent_) {
  119. parent_->RemoveObserver(this);
  120. parent_ = nullptr;
  121. }
  122. if (view_) {
  123. view_->Hide();
  124. view_ = nullptr;
  125. }
  126. }
  127. void AutofillPopup::SetItems(const std::vector<base::string16>& values,
  128. const std::vector<base::string16>& labels) {
  129. DCHECK(view_);
  130. values_ = values;
  131. labels_ = labels;
  132. UpdatePopupBounds();
  133. view_->OnSuggestionsChanged();
  134. if (view_) // could be hidden after the change
  135. view_->DoUpdateBoundsAndRedrawPopup();
  136. }
  137. void AutofillPopup::AcceptSuggestion(int index) {
  138. frame_host_->Send(new AtomAutofillFrameMsg_AcceptSuggestion(
  139. frame_host_->GetRoutingID(), GetValueAt(index)));
  140. }
  141. void AutofillPopup::UpdatePopupBounds() {
  142. DCHECK(parent_);
  143. gfx::Point origin(element_bounds_.origin());
  144. views::View::ConvertPointToScreen(parent_, &origin);
  145. gfx::Rect bounds(origin, element_bounds_.size());
  146. int desired_width = GetDesiredPopupWidth();
  147. int desired_height = GetDesiredPopupHeight();
  148. bool is_rtl = false;
  149. gfx::Point top_left_corner_of_popup =
  150. origin + gfx::Vector2d(bounds.width() - desired_width, -desired_height);
  151. // This is the bottom right point of the popup if the popup is below the
  152. // element and grows to the right (since the is the lowest and furthest right
  153. // the popup could go).
  154. gfx::Point bottom_right_corner_of_popup =
  155. origin + gfx::Vector2d(desired_width, bounds.height() + desired_height);
  156. display::Display top_left_display =
  157. GetDisplayNearestPoint(top_left_corner_of_popup);
  158. display::Display bottom_right_display =
  159. GetDisplayNearestPoint(bottom_right_corner_of_popup);
  160. std::pair<int, int> popup_x_and_width = CalculatePopupXAndWidth(
  161. top_left_display, bottom_right_display, desired_width, bounds, is_rtl);
  162. std::pair<int, int> popup_y_and_height = CalculatePopupYAndHeight(
  163. top_left_display, bottom_right_display, desired_height, bounds);
  164. popup_bounds_ =
  165. gfx::Rect(popup_x_and_width.first, popup_y_and_height.first,
  166. popup_x_and_width.second, popup_y_and_height.second);
  167. popup_bounds_in_view_ =
  168. gfx::Rect(popup_bounds_in_view_.origin(),
  169. gfx::Size(popup_x_and_width.second, popup_y_and_height.second));
  170. }
  171. void AutofillPopup::OnViewBoundsChanged(views::View* view) {
  172. UpdatePopupBounds();
  173. view_->DoUpdateBoundsAndRedrawPopup();
  174. }
  175. void AutofillPopup::OnViewIsDeleting(views::View* view) {
  176. Hide();
  177. }
  178. int AutofillPopup::GetDesiredPopupHeight() {
  179. return 2 * kPopupBorderThickness + values_.size() * kRowHeight;
  180. }
  181. int AutofillPopup::GetDesiredPopupWidth() {
  182. int popup_width = element_bounds_.width();
  183. for (size_t i = 0; i < values_.size(); ++i) {
  184. int row_size =
  185. kEndPadding + 2 * kPopupBorderThickness +
  186. gfx::GetStringWidth(GetValueAt(i), GetValueFontListForRow(i)) +
  187. gfx::GetStringWidth(GetLabelAt(i), GetLabelFontListForRow(i));
  188. if (GetLabelAt(i).length() > 0)
  189. row_size += kNamePadding + kEndPadding;
  190. popup_width = std::max(popup_width, row_size);
  191. }
  192. return popup_width;
  193. }
  194. gfx::Rect AutofillPopup::GetRowBounds(int index) {
  195. int top = kPopupBorderThickness + index * kRowHeight;
  196. return gfx::Rect(kPopupBorderThickness, top,
  197. popup_bounds_.width() - 2 * kPopupBorderThickness,
  198. kRowHeight);
  199. }
  200. const gfx::FontList& AutofillPopup::GetValueFontListForRow(int index) const {
  201. return bold_font_list_;
  202. }
  203. const gfx::FontList& AutofillPopup::GetLabelFontListForRow(int index) const {
  204. return smaller_font_list_;
  205. }
  206. ui::NativeTheme::ColorId AutofillPopup::GetBackgroundColorIDForRow(
  207. int index) const {
  208. return (view_ && index == view_->GetSelectedLine())
  209. ? ui::NativeTheme::kColorId_ResultsTableHoveredBackground
  210. : ui::NativeTheme::kColorId_ResultsTableNormalBackground;
  211. }
  212. int AutofillPopup::GetLineCount() {
  213. return values_.size();
  214. }
  215. base::string16 AutofillPopup::GetValueAt(int i) {
  216. return values_.at(i);
  217. }
  218. base::string16 AutofillPopup::GetLabelAt(int i) {
  219. return labels_.at(i);
  220. }
  221. int AutofillPopup::LineFromY(int y) const {
  222. int current_height = kPopupBorderThickness;
  223. for (size_t i = 0; i < values_.size(); ++i) {
  224. current_height += kRowHeight;
  225. if (y <= current_height)
  226. return i;
  227. }
  228. return values_.size() - 1;
  229. }
  230. } // namespace atom