autofill_popup.cc 12 KB

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