autofill_popup.cc 11 KB

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