autofill_popup.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. content::RenderFrameHost* embedder_frame_host,
  99. bool offscreen,
  100. views::View* parent,
  101. const gfx::RectF& r) {
  102. Hide();
  103. frame_host_ = frame_host;
  104. element_bounds_ = gfx::ToEnclosedRect(r);
  105. gfx::Vector2d height_offset(0, element_bounds_.height());
  106. gfx::Point menu_position(element_bounds_.origin() + height_offset);
  107. views::View::ConvertPointToScreen(parent, &menu_position);
  108. popup_bounds_ = gfx::Rect(menu_position, element_bounds_.size());
  109. parent_ = parent;
  110. parent_->AddObserver(this);
  111. view_ = new AutofillPopupView(this, parent->GetWidget());
  112. view_->Show();
  113. #if defined(ENABLE_OSR)
  114. if (offscreen) {
  115. auto* rwhv = frame_host->GetView();
  116. if (embedder_frame_host != nullptr) {
  117. rwhv = embedder_frame_host->GetView();
  118. }
  119. auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(rwhv);
  120. view_->view_proxy_.reset(new OffscreenViewProxy(view_));
  121. osr_rwhv->AddViewProxy(view_->view_proxy_.get());
  122. }
  123. #endif
  124. }
  125. void AutofillPopup::Hide() {
  126. if (parent_) {
  127. parent_->RemoveObserver(this);
  128. parent_ = nullptr;
  129. }
  130. if (view_) {
  131. view_->Hide();
  132. view_ = nullptr;
  133. }
  134. }
  135. void AutofillPopup::SetItems(const std::vector<base::string16>& values,
  136. const std::vector<base::string16>& labels) {
  137. DCHECK(view_);
  138. values_ = values;
  139. labels_ = labels;
  140. UpdatePopupBounds();
  141. view_->OnSuggestionsChanged();
  142. if (view_) // could be hidden after the change
  143. view_->DoUpdateBoundsAndRedrawPopup();
  144. }
  145. void AutofillPopup::AcceptSuggestion(int index) {
  146. frame_host_->Send(new AtomAutofillFrameMsg_AcceptSuggestion(
  147. frame_host_->GetRoutingID(), GetValueAt(index)));
  148. }
  149. void AutofillPopup::UpdatePopupBounds() {
  150. DCHECK(parent_);
  151. gfx::Point origin(element_bounds_.origin());
  152. views::View::ConvertPointToScreen(parent_, &origin);
  153. gfx::Rect bounds(origin, element_bounds_.size());
  154. int desired_width = GetDesiredPopupWidth();
  155. int desired_height = GetDesiredPopupHeight();
  156. bool is_rtl = false;
  157. gfx::Point top_left_corner_of_popup =
  158. origin + gfx::Vector2d(bounds.width() - desired_width, -desired_height);
  159. // This is the bottom right point of the popup if the popup is below the
  160. // element and grows to the right (since the is the lowest and furthest right
  161. // the popup could go).
  162. gfx::Point bottom_right_corner_of_popup =
  163. origin + gfx::Vector2d(desired_width, bounds.height() + desired_height);
  164. display::Display top_left_display =
  165. GetDisplayNearestPoint(top_left_corner_of_popup);
  166. display::Display bottom_right_display =
  167. GetDisplayNearestPoint(bottom_right_corner_of_popup);
  168. std::pair<int, int> popup_x_and_width = CalculatePopupXAndWidth(
  169. top_left_display, bottom_right_display, desired_width, bounds, is_rtl);
  170. std::pair<int, int> popup_y_and_height = CalculatePopupYAndHeight(
  171. top_left_display, bottom_right_display, desired_height, bounds);
  172. popup_bounds_ =
  173. gfx::Rect(popup_x_and_width.first, popup_y_and_height.first,
  174. popup_x_and_width.second, popup_y_and_height.second);
  175. }
  176. gfx::Rect AutofillPopup::popup_bounds_in_view() {
  177. gfx::Point origin(popup_bounds_.origin());
  178. views::View::ConvertPointFromScreen(parent_, &origin);
  179. return gfx::Rect(origin, popup_bounds_.size());
  180. }
  181. void AutofillPopup::OnViewBoundsChanged(views::View* view) {
  182. UpdatePopupBounds();
  183. view_->DoUpdateBoundsAndRedrawPopup();
  184. }
  185. void AutofillPopup::OnViewIsDeleting(views::View* view) {
  186. Hide();
  187. }
  188. int AutofillPopup::GetDesiredPopupHeight() {
  189. return 2 * kPopupBorderThickness + values_.size() * kRowHeight;
  190. }
  191. int AutofillPopup::GetDesiredPopupWidth() {
  192. int popup_width = element_bounds_.width();
  193. for (size_t i = 0; i < values_.size(); ++i) {
  194. int row_size =
  195. kEndPadding + 2 * kPopupBorderThickness +
  196. gfx::GetStringWidth(GetValueAt(i), GetValueFontListForRow(i)) +
  197. gfx::GetStringWidth(GetLabelAt(i), GetLabelFontListForRow(i));
  198. if (GetLabelAt(i).length() > 0)
  199. row_size += kNamePadding + kEndPadding;
  200. popup_width = std::max(popup_width, row_size);
  201. }
  202. return popup_width;
  203. }
  204. gfx::Rect AutofillPopup::GetRowBounds(int index) {
  205. int top = kPopupBorderThickness + index * kRowHeight;
  206. return gfx::Rect(kPopupBorderThickness, top,
  207. popup_bounds_.width() - 2 * kPopupBorderThickness,
  208. kRowHeight);
  209. }
  210. const gfx::FontList& AutofillPopup::GetValueFontListForRow(int index) const {
  211. return bold_font_list_;
  212. }
  213. const gfx::FontList& AutofillPopup::GetLabelFontListForRow(int index) const {
  214. return smaller_font_list_;
  215. }
  216. ui::NativeTheme::ColorId AutofillPopup::GetBackgroundColorIDForRow(
  217. int index) const {
  218. return (view_ && index == view_->GetSelectedLine())
  219. ? ui::NativeTheme::kColorId_ResultsTableHoveredBackground
  220. : ui::NativeTheme::kColorId_ResultsTableNormalBackground;
  221. }
  222. int AutofillPopup::GetLineCount() {
  223. return values_.size();
  224. }
  225. base::string16 AutofillPopup::GetValueAt(int i) {
  226. return values_.at(i);
  227. }
  228. base::string16 AutofillPopup::GetLabelAt(int i) {
  229. return labels_.at(i);
  230. }
  231. int AutofillPopup::LineFromY(int y) const {
  232. int current_height = kPopupBorderThickness;
  233. for (size_t i = 0; i < values_.size(); ++i) {
  234. current_height += kRowHeight;
  235. if (y <= current_height)
  236. return i;
  237. }
  238. return values_.size() - 1;
  239. }
  240. } // namespace atom