autofill_popup.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 <memory>
  6. #include <vector>
  7. #include "base/feature_list.h"
  8. #include "base/i18n/rtl.h"
  9. #include "components/autofill/core/common/autofill_features.h"
  10. #include "content/public/browser/render_frame_host.h"
  11. #include "electron/buildflags/buildflags.h"
  12. #include "mojo/public/cpp/bindings/associated_remote.h"
  13. #include "shell/browser/osr/osr_render_widget_host_view.h"
  14. #include "shell/browser/osr/osr_view_proxy.h"
  15. #include "shell/browser/ui/autofill_popup.h"
  16. #include "shell/common/api/api.mojom.h"
  17. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  18. #include "ui/color/color_id.h"
  19. #include "ui/color/color_provider.h"
  20. #include "ui/display/screen.h"
  21. #include "ui/gfx/geometry/point.h"
  22. #include "ui/gfx/geometry/rect.h"
  23. #include "ui/gfx/geometry/rect_conversions.h"
  24. #include "ui/gfx/geometry/vector2d.h"
  25. #include "ui/gfx/text_utils.h"
  26. namespace electron {
  27. namespace {
  28. void CalculatePopupXAndWidthHorizontallyCentered(
  29. int popup_preferred_width,
  30. const gfx::Rect& content_area_bounds,
  31. const gfx::Rect& element_bounds,
  32. bool is_rtl,
  33. gfx::Rect* popup_bounds) {
  34. // The preferred horizontal starting point for the pop-up is at the horizontal
  35. // center of the field.
  36. int preferred_starting_point =
  37. std::clamp(element_bounds.x() + (element_bounds.size().width() / 2),
  38. content_area_bounds.x(), content_area_bounds.right());
  39. // The space available to the left and to the right.
  40. int space_to_right = content_area_bounds.right() - preferred_starting_point;
  41. int space_to_left = preferred_starting_point - content_area_bounds.x();
  42. // Calculate the pop-up width. This is either the preferred pop-up width, or
  43. // alternatively the maximum space available if there is not sufficient space
  44. // for the preferred width.
  45. int popup_width =
  46. std::min(popup_preferred_width, space_to_left + space_to_right);
  47. // Calculates the space that is available to grow into the preferred
  48. // direction. In RTL, this is the space to the right side of the content
  49. // area, in LTR this is the space to the left side of the content area.
  50. int space_to_grow_in_preferred_direction =
  51. is_rtl ? space_to_left : space_to_right;
  52. // Calculate how much the pop-up needs to grow into the non-preferred
  53. // direction.
  54. int amount_to_grow_in_unpreferred_direction =
  55. std::max(0, popup_width - space_to_grow_in_preferred_direction);
  56. popup_bounds->set_width(popup_width);
  57. if (is_rtl) {
  58. // Note, in RTL the |pop_up_width| must be subtracted to achieve
  59. // right-alignment of the pop-up with the element.
  60. popup_bounds->set_x(preferred_starting_point - popup_width +
  61. amount_to_grow_in_unpreferred_direction);
  62. } else {
  63. popup_bounds->set_x(preferred_starting_point -
  64. amount_to_grow_in_unpreferred_direction);
  65. }
  66. }
  67. void CalculatePopupXAndWidth(int popup_preferred_width,
  68. const gfx::Rect& content_area_bounds,
  69. const gfx::Rect& element_bounds,
  70. bool is_rtl,
  71. gfx::Rect* popup_bounds) {
  72. int right_growth_start = std::clamp(
  73. element_bounds.x(), content_area_bounds.x(), content_area_bounds.right());
  74. int left_growth_end =
  75. std::clamp(element_bounds.right(), content_area_bounds.x(),
  76. content_area_bounds.right());
  77. int right_available = content_area_bounds.right() - right_growth_start;
  78. int left_available = left_growth_end - content_area_bounds.x();
  79. int popup_width = std::min(popup_preferred_width,
  80. std::max(left_available, right_available));
  81. // Prefer to grow towards the end (right for LTR, left for RTL). But if there
  82. // is not enough space available in the desired direction and more space in
  83. // the other direction, reverse it.
  84. bool grow_left = false;
  85. if (is_rtl) {
  86. grow_left =
  87. left_available >= popup_width || left_available >= right_available;
  88. } else {
  89. grow_left =
  90. right_available < popup_width && right_available < left_available;
  91. }
  92. popup_bounds->set_width(popup_width);
  93. popup_bounds->set_x(grow_left ? left_growth_end - popup_width
  94. : right_growth_start);
  95. }
  96. void CalculatePopupYAndHeight(int popup_preferred_height,
  97. const gfx::Rect& content_area_bounds,
  98. const gfx::Rect& element_bounds,
  99. gfx::Rect* popup_bounds) {
  100. int top_growth_end = std::clamp(element_bounds.y(), content_area_bounds.y(),
  101. content_area_bounds.bottom());
  102. int bottom_growth_start =
  103. std::clamp(element_bounds.bottom(), content_area_bounds.y(),
  104. content_area_bounds.bottom());
  105. int top_available = top_growth_end - content_area_bounds.y();
  106. int bottom_available = content_area_bounds.bottom() - bottom_growth_start;
  107. popup_bounds->set_height(popup_preferred_height);
  108. popup_bounds->set_y(top_growth_end);
  109. if (bottom_available >= popup_preferred_height ||
  110. bottom_available >= top_available) {
  111. popup_bounds->AdjustToFit(
  112. gfx::Rect(popup_bounds->x(), element_bounds.bottom(),
  113. popup_bounds->width(), bottom_available));
  114. } else {
  115. popup_bounds->AdjustToFit(gfx::Rect(popup_bounds->x(),
  116. content_area_bounds.y(),
  117. popup_bounds->width(), top_available));
  118. }
  119. }
  120. gfx::Rect CalculatePopupBounds(const gfx::Size& desired_size,
  121. const gfx::Rect& content_area_bounds,
  122. const gfx::Rect& element_bounds,
  123. bool is_rtl,
  124. bool horizontally_centered) {
  125. gfx::Rect popup_bounds;
  126. if (horizontally_centered) {
  127. CalculatePopupXAndWidthHorizontallyCentered(
  128. desired_size.width(), content_area_bounds, element_bounds, is_rtl,
  129. &popup_bounds);
  130. } else {
  131. CalculatePopupXAndWidth(desired_size.width(), content_area_bounds,
  132. element_bounds, is_rtl, &popup_bounds);
  133. }
  134. CalculatePopupYAndHeight(desired_size.height(), content_area_bounds,
  135. element_bounds, &popup_bounds);
  136. return popup_bounds;
  137. }
  138. } // namespace
  139. AutofillPopup::AutofillPopup() {
  140. bold_font_list_ = gfx::FontList().DeriveWithWeight(gfx::Font::Weight::BOLD);
  141. smaller_font_list_ =
  142. gfx::FontList().DeriveWithSizeDelta(kSmallerFontSizeDelta);
  143. }
  144. AutofillPopup::~AutofillPopup() {
  145. Hide();
  146. }
  147. void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
  148. content::RenderFrameHost* embedder_frame_host,
  149. bool offscreen,
  150. views::View* parent,
  151. const gfx::RectF& r) {
  152. Hide();
  153. frame_host_ = frame_host;
  154. element_bounds_ = gfx::ToEnclosedRect(r);
  155. gfx::Vector2d height_offset(0, element_bounds_.height());
  156. gfx::Point menu_position(element_bounds_.origin() + height_offset);
  157. views::View::ConvertPointToScreen(parent, &menu_position);
  158. popup_bounds_ = gfx::Rect(menu_position, element_bounds_.size());
  159. parent_ = parent;
  160. parent_->AddObserver(this);
  161. view_ = new AutofillPopupView(this, parent->GetWidget());
  162. if (offscreen) {
  163. auto* rwhv = embedder_frame_host ? embedder_frame_host->GetView()
  164. : frame_host->GetView();
  165. auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(rwhv);
  166. view_->view_proxy_ = std::make_unique<OffscreenViewProxy>(view_);
  167. osr_rwhv->AddViewProxy(view_->view_proxy_.get());
  168. }
  169. // Do this after OSR setup, we check for view_proxy_ when showing
  170. view_->Show();
  171. }
  172. void AutofillPopup::Hide() {
  173. if (parent_) {
  174. parent_->RemoveObserver(this);
  175. parent_ = nullptr;
  176. }
  177. if (view_) {
  178. view_->Hide();
  179. view_ = nullptr;
  180. }
  181. }
  182. void AutofillPopup::SetItems(const std::vector<std::u16string>& values,
  183. const std::vector<std::u16string>& labels) {
  184. DCHECK(view_);
  185. values_ = values;
  186. labels_ = labels;
  187. UpdatePopupBounds();
  188. view_->OnSuggestionsChanged();
  189. if (view_) // could be hidden after the change
  190. view_->DoUpdateBoundsAndRedrawPopup();
  191. }
  192. void AutofillPopup::AcceptSuggestion(int index) {
  193. mojo::AssociatedRemote<mojom::ElectronAutofillAgent> autofill_agent;
  194. frame_host_->GetRemoteAssociatedInterfaces()->GetInterface(&autofill_agent);
  195. autofill_agent->AcceptDataListSuggestion(value_at(index));
  196. }
  197. void AutofillPopup::UpdatePopupBounds() {
  198. DCHECK(parent_);
  199. gfx::Point origin(element_bounds_.origin());
  200. views::View::ConvertPointToScreen(parent_, &origin);
  201. gfx::Rect bounds(origin, element_bounds_.size());
  202. gfx::Size preferred_size =
  203. gfx::Size(GetDesiredPopupWidth(), GetDesiredPopupHeight());
  204. popup_bounds_ =
  205. CalculatePopupBounds(preferred_size, parent_->GetBoundsInScreen(), bounds,
  206. base::i18n::IsRTL(), false);
  207. }
  208. gfx::Rect AutofillPopup::popup_bounds_in_view() {
  209. gfx::Point origin(popup_bounds_.origin());
  210. views::View::ConvertPointFromScreen(parent_, &origin);
  211. return gfx::Rect(origin, popup_bounds_.size());
  212. }
  213. void AutofillPopup::OnViewBoundsChanged(views::View* view) {
  214. UpdatePopupBounds();
  215. view_->DoUpdateBoundsAndRedrawPopup();
  216. }
  217. void AutofillPopup::OnViewIsDeleting(views::View* view) {
  218. Hide();
  219. }
  220. int AutofillPopup::GetDesiredPopupHeight() {
  221. return 2 * kPopupBorderThickness + values_.size() * kRowHeight;
  222. }
  223. int AutofillPopup::GetDesiredPopupWidth() {
  224. int popup_width = element_bounds_.width();
  225. for (size_t i = 0; i < values_.size(); ++i) {
  226. int row_size = kEndPadding + 2 * kPopupBorderThickness +
  227. gfx::GetStringWidth(value_at(i), GetValueFontListForRow(i)) +
  228. gfx::GetStringWidth(label_at(i), GetLabelFontListForRow(i));
  229. if (!label_at(i).empty())
  230. row_size += kNamePadding + kEndPadding;
  231. popup_width = std::max(popup_width, row_size);
  232. }
  233. return popup_width;
  234. }
  235. gfx::Rect AutofillPopup::GetRowBounds(int index) {
  236. int top = kPopupBorderThickness + index * kRowHeight;
  237. return gfx::Rect(kPopupBorderThickness, top,
  238. popup_bounds_.width() - 2 * kPopupBorderThickness,
  239. kRowHeight);
  240. }
  241. const gfx::FontList& AutofillPopup::GetValueFontListForRow(int index) const {
  242. return bold_font_list_;
  243. }
  244. const gfx::FontList& AutofillPopup::GetLabelFontListForRow(int index) const {
  245. return smaller_font_list_;
  246. }
  247. ui::ColorId AutofillPopup::GetBackgroundColorIDForRow(int index) const {
  248. return (view_ && index == view_->GetSelectedLine())
  249. ? ui::kColorResultsTableHoveredBackground
  250. : ui::kColorResultsTableNormalBackground;
  251. }
  252. int AutofillPopup::LineFromY(int y) const {
  253. int current_height = kPopupBorderThickness;
  254. for (size_t i = 0; i < values_.size(); ++i) {
  255. current_height += kRowHeight;
  256. if (y <= current_height)
  257. return i;
  258. }
  259. return values_.size() - 1;
  260. }
  261. } // namespace electron