autofill_popup.cc 9.5 KB

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