2017-05-19 19:35:13 +00:00
|
|
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-05-19 19:52:18 +00:00
|
|
|
#include <algorithm>
|
2019-09-16 22:12:00 +00:00
|
|
|
#include <memory>
|
2017-05-19 19:52:18 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2021-10-06 02:21:00 +00:00
|
|
|
#include "base/feature_list.h"
|
2019-02-11 19:38:58 +00:00
|
|
|
#include "base/i18n/rtl.h"
|
2021-10-06 02:21:00 +00:00
|
|
|
#include "components/autofill/core/common/autofill_features.h"
|
2018-10-01 20:00:53 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2019-10-28 22:12:35 +00:00
|
|
|
#include "mojo/public/cpp/bindings/associated_remote.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window_views.h"
|
|
|
|
#include "shell/browser/ui/autofill_popup.h"
|
|
|
|
#include "shell/common/api/api.mojom.h"
|
2019-06-12 22:08:22 +00:00
|
|
|
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
|
2022-02-25 18:17:35 +00:00
|
|
|
#include "ui/color/color_id.h"
|
|
|
|
#include "ui/color/color_provider.h"
|
2017-05-19 19:35:13 +00:00
|
|
|
#include "ui/display/display.h"
|
|
|
|
#include "ui/display/screen.h"
|
|
|
|
#include "ui/gfx/geometry/point.h"
|
|
|
|
#include "ui/gfx/geometry/rect.h"
|
2018-04-05 00:53:51 +00:00
|
|
|
#include "ui/gfx/geometry/rect_conversions.h"
|
2017-05-19 19:35:13 +00:00
|
|
|
#include "ui/gfx/geometry/vector2d.h"
|
2017-05-19 19:52:18 +00:00
|
|
|
#include "ui/gfx/text_utils.h"
|
2017-05-19 19:35:13 +00:00
|
|
|
|
2018-10-01 20:00:53 +00:00
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/osr/osr_render_widget_host_view.h"
|
|
|
|
#include "shell/browser/osr/osr_view_proxy.h"
|
2018-04-05 00:53:51 +00:00
|
|
|
#endif
|
|
|
|
|
2017-05-19 19:35:13 +00:00
|
|
|
namespace electron {
|
|
|
|
|
2023-01-19 18:44:23 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-11-24 08:45:59 +00:00
|
|
|
void CalculatePopupXAndWidthHorizontallyCentered(
|
|
|
|
int popup_preferred_width,
|
|
|
|
const gfx::Rect& content_area_bounds,
|
|
|
|
const gfx::Rect& element_bounds,
|
|
|
|
bool is_rtl,
|
2023-01-19 18:44:23 +00:00
|
|
|
gfx::Rect* popup_bounds) {
|
2021-11-24 08:45:59 +00:00
|
|
|
// The preferred horizontal starting point for the pop-up is at the horizontal
|
|
|
|
// center of the field.
|
|
|
|
int preferred_starting_point =
|
2023-05-23 19:58:58 +00:00
|
|
|
std::clamp(element_bounds.x() + (element_bounds.size().width() / 2),
|
|
|
|
content_area_bounds.x(), content_area_bounds.right());
|
2021-11-24 08:45:59 +00:00
|
|
|
|
|
|
|
// The space available to the left and to the right.
|
|
|
|
int space_to_right = content_area_bounds.right() - preferred_starting_point;
|
|
|
|
int space_to_left = preferred_starting_point - content_area_bounds.x();
|
|
|
|
|
|
|
|
// Calculate the pop-up width. This is either the preferred pop-up width, or
|
|
|
|
// alternatively the maximum space available if there is not sufficient space
|
|
|
|
// for the preferred width.
|
|
|
|
int popup_width =
|
|
|
|
std::min(popup_preferred_width, space_to_left + space_to_right);
|
|
|
|
|
|
|
|
// Calculates the space that is available to grow into the preferred
|
|
|
|
// direction. In RTL, this is the space to the right side of the content
|
|
|
|
// area, in LTR this is the space to the left side of the content area.
|
|
|
|
int space_to_grow_in_preferred_direction =
|
|
|
|
is_rtl ? space_to_left : space_to_right;
|
|
|
|
|
|
|
|
// Calculate how much the pop-up needs to grow into the non-preferred
|
|
|
|
// direction.
|
2022-06-16 07:46:11 +00:00
|
|
|
int amount_to_grow_in_unpreferred_direction =
|
2021-11-24 08:45:59 +00:00
|
|
|
std::max(0, popup_width - space_to_grow_in_preferred_direction);
|
|
|
|
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds->set_width(popup_width);
|
2021-11-24 08:45:59 +00:00
|
|
|
if (is_rtl) {
|
|
|
|
// Note, in RTL the |pop_up_width| must be subtracted to achieve
|
|
|
|
// right-alignment of the pop-up with the element.
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds->set_x(preferred_starting_point - popup_width +
|
|
|
|
amount_to_grow_in_unpreferred_direction);
|
2021-11-24 08:45:59 +00:00
|
|
|
} else {
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds->set_x(preferred_starting_point -
|
|
|
|
amount_to_grow_in_unpreferred_direction);
|
2021-11-24 08:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalculatePopupXAndWidth(int popup_preferred_width,
|
|
|
|
const gfx::Rect& content_area_bounds,
|
|
|
|
const gfx::Rect& element_bounds,
|
|
|
|
bool is_rtl,
|
2023-01-19 18:44:23 +00:00
|
|
|
gfx::Rect* popup_bounds) {
|
2023-05-23 19:58:58 +00:00
|
|
|
int right_growth_start = std::clamp(
|
2021-11-24 08:45:59 +00:00
|
|
|
element_bounds.x(), content_area_bounds.x(), content_area_bounds.right());
|
|
|
|
int left_growth_end =
|
2023-05-23 19:58:58 +00:00
|
|
|
std::clamp(element_bounds.right(), content_area_bounds.x(),
|
|
|
|
content_area_bounds.right());
|
2021-11-24 08:45:59 +00:00
|
|
|
|
|
|
|
int right_available = content_area_bounds.right() - right_growth_start;
|
|
|
|
int left_available = left_growth_end - content_area_bounds.x();
|
|
|
|
|
|
|
|
int popup_width = std::min(popup_preferred_width,
|
|
|
|
std::max(left_available, right_available));
|
|
|
|
|
|
|
|
// Prefer to grow towards the end (right for LTR, left for RTL). But if there
|
|
|
|
// is not enough space available in the desired direction and more space in
|
|
|
|
// the other direction, reverse it.
|
|
|
|
bool grow_left = false;
|
|
|
|
if (is_rtl) {
|
|
|
|
grow_left =
|
|
|
|
left_available >= popup_width || left_available >= right_available;
|
|
|
|
} else {
|
|
|
|
grow_left =
|
|
|
|
right_available < popup_width && right_available < left_available;
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds->set_width(popup_width);
|
|
|
|
popup_bounds->set_x(grow_left ? left_growth_end - popup_width
|
|
|
|
: right_growth_start);
|
2021-11-24 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CalculatePopupYAndHeight(int popup_preferred_height,
|
|
|
|
const gfx::Rect& content_area_bounds,
|
|
|
|
const gfx::Rect& element_bounds,
|
2023-01-19 18:44:23 +00:00
|
|
|
gfx::Rect* popup_bounds) {
|
2023-05-23 19:58:58 +00:00
|
|
|
int top_growth_end = std::clamp(element_bounds.y(), content_area_bounds.y(),
|
|
|
|
content_area_bounds.bottom());
|
2021-11-24 08:45:59 +00:00
|
|
|
int bottom_growth_start =
|
2023-05-23 19:58:58 +00:00
|
|
|
std::clamp(element_bounds.bottom(), content_area_bounds.y(),
|
|
|
|
content_area_bounds.bottom());
|
2021-11-24 08:45:59 +00:00
|
|
|
|
|
|
|
int top_available = top_growth_end - content_area_bounds.y();
|
|
|
|
int bottom_available = content_area_bounds.bottom() - bottom_growth_start;
|
|
|
|
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds->set_height(popup_preferred_height);
|
|
|
|
popup_bounds->set_y(top_growth_end);
|
2021-11-24 08:45:59 +00:00
|
|
|
|
|
|
|
if (bottom_available >= popup_preferred_height ||
|
|
|
|
bottom_available >= top_available) {
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds->AdjustToFit(
|
|
|
|
gfx::Rect(popup_bounds->x(), element_bounds.bottom(),
|
|
|
|
popup_bounds->width(), bottom_available));
|
2021-11-24 08:45:59 +00:00
|
|
|
} else {
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds->AdjustToFit(gfx::Rect(popup_bounds->x(),
|
|
|
|
content_area_bounds.y(),
|
|
|
|
popup_bounds->width(), top_available));
|
2021-11-24 08:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Rect CalculatePopupBounds(const gfx::Size& desired_size,
|
|
|
|
const gfx::Rect& content_area_bounds,
|
|
|
|
const gfx::Rect& element_bounds,
|
|
|
|
bool is_rtl,
|
|
|
|
bool horizontally_centered) {
|
2023-01-19 18:44:23 +00:00
|
|
|
gfx::Rect popup_bounds;
|
2021-11-24 08:45:59 +00:00
|
|
|
|
|
|
|
if (horizontally_centered) {
|
|
|
|
CalculatePopupXAndWidthHorizontallyCentered(
|
|
|
|
desired_size.width(), content_area_bounds, element_bounds, is_rtl,
|
2023-01-19 18:44:23 +00:00
|
|
|
&popup_bounds);
|
2021-11-24 08:45:59 +00:00
|
|
|
} else {
|
|
|
|
CalculatePopupXAndWidth(desired_size.width(), content_area_bounds,
|
2023-01-19 18:44:23 +00:00
|
|
|
element_bounds, is_rtl, &popup_bounds);
|
2021-11-24 08:45:59 +00:00
|
|
|
}
|
|
|
|
CalculatePopupYAndHeight(desired_size.height(), content_area_bounds,
|
2023-01-19 18:44:23 +00:00
|
|
|
element_bounds, &popup_bounds);
|
2021-11-24 08:45:59 +00:00
|
|
|
|
2023-01-19 18:44:23 +00:00
|
|
|
return popup_bounds;
|
2021-11-24 08:45:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 18:44:23 +00:00
|
|
|
} // namespace
|
|
|
|
|
2018-04-05 00:53:51 +00:00
|
|
|
AutofillPopup::AutofillPopup() {
|
2018-04-18 01:55:30 +00:00
|
|
|
bold_font_list_ = gfx::FontList().DeriveWithWeight(gfx::Font::Weight::BOLD);
|
2017-05-19 19:52:18 +00:00
|
|
|
smaller_font_list_ =
|
2018-04-05 00:53:51 +00:00
|
|
|
gfx::FontList().DeriveWithSizeDelta(kSmallerFontSizeDelta);
|
2017-05-19 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AutofillPopup::~AutofillPopup() {
|
|
|
|
Hide();
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
|
2018-06-18 15:15:37 +00:00
|
|
|
content::RenderFrameHost* embedder_frame_host,
|
2018-04-18 01:55:30 +00:00
|
|
|
bool offscreen,
|
|
|
|
views::View* parent,
|
|
|
|
const gfx::RectF& r) {
|
2018-04-05 00:53:51 +00:00
|
|
|
Hide();
|
|
|
|
|
2017-05-26 01:38:27 +00:00
|
|
|
frame_host_ = frame_host;
|
2018-04-05 00:53:51 +00:00
|
|
|
element_bounds_ = gfx::ToEnclosedRect(r);
|
2017-05-19 19:52:18 +00:00
|
|
|
|
2018-06-18 15:15:37 +00:00
|
|
|
gfx::Vector2d height_offset(0, element_bounds_.height());
|
|
|
|
gfx::Point menu_position(element_bounds_.origin() + height_offset);
|
|
|
|
views::View::ConvertPointToScreen(parent, &menu_position);
|
|
|
|
popup_bounds_ = gfx::Rect(menu_position, element_bounds_.size());
|
|
|
|
|
2018-04-05 00:53:51 +00:00
|
|
|
parent_ = parent;
|
|
|
|
parent_->AddObserver(this);
|
|
|
|
|
|
|
|
view_ = new AutofillPopupView(this, parent->GetWidget());
|
2017-05-19 19:52:18 +00:00
|
|
|
|
2018-10-01 20:00:53 +00:00
|
|
|
#if BUILDFLAG(ENABLE_OSR)
|
2017-05-21 17:55:19 +00:00
|
|
|
if (offscreen) {
|
2018-06-18 15:15:37 +00:00
|
|
|
auto* rwhv = frame_host->GetView();
|
|
|
|
if (embedder_frame_host != nullptr) {
|
|
|
|
rwhv = embedder_frame_host->GetView();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(rwhv);
|
2019-09-16 22:12:00 +00:00
|
|
|
view_->view_proxy_ = std::make_unique<OffscreenViewProxy>(view_);
|
2017-05-21 17:55:19 +00:00
|
|
|
osr_rwhv->AddViewProxy(view_->view_proxy_.get());
|
|
|
|
}
|
2017-06-26 09:13:05 +00:00
|
|
|
#endif
|
2019-04-17 21:10:04 +00:00
|
|
|
|
|
|
|
// Do this after OSR setup, we check for view_proxy_ when showing
|
|
|
|
view_->Show();
|
2017-05-19 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutofillPopup::Hide() {
|
2018-04-05 00:53:51 +00:00
|
|
|
if (parent_) {
|
|
|
|
parent_->RemoveObserver(this);
|
|
|
|
parent_ = nullptr;
|
|
|
|
}
|
2017-05-21 17:55:19 +00:00
|
|
|
if (view_) {
|
2017-05-19 19:35:13 +00:00
|
|
|
view_->Hide();
|
2017-05-21 17:55:19 +00:00
|
|
|
view_ = nullptr;
|
2017-05-19 19:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
void AutofillPopup::SetItems(const std::vector<std::u16string>& values,
|
|
|
|
const std::vector<std::u16string>& labels) {
|
2018-04-05 00:53:51 +00:00
|
|
|
DCHECK(view_);
|
2017-05-19 19:35:13 +00:00
|
|
|
values_ = values;
|
|
|
|
labels_ = labels;
|
2018-04-05 00:53:51 +00:00
|
|
|
UpdatePopupBounds();
|
|
|
|
view_->OnSuggestionsChanged();
|
|
|
|
if (view_) // could be hidden after the change
|
|
|
|
view_->DoUpdateBoundsAndRedrawPopup();
|
2017-05-19 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutofillPopup::AcceptSuggestion(int index) {
|
2019-10-28 22:12:35 +00:00
|
|
|
mojo::AssociatedRemote<mojom::ElectronAutofillAgent> autofill_agent;
|
|
|
|
frame_host_->GetRemoteAssociatedInterfaces()->GetInterface(&autofill_agent);
|
2019-06-12 22:08:22 +00:00
|
|
|
autofill_agent->AcceptDataListSuggestion(GetValueAt(index));
|
2017-05-19 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 00:53:51 +00:00
|
|
|
void AutofillPopup::UpdatePopupBounds() {
|
|
|
|
DCHECK(parent_);
|
|
|
|
gfx::Point origin(element_bounds_.origin());
|
|
|
|
views::View::ConvertPointToScreen(parent_, &origin);
|
|
|
|
|
2019-02-11 19:38:58 +00:00
|
|
|
gfx::Rect bounds(origin, element_bounds_.size());
|
2020-04-13 23:39:26 +00:00
|
|
|
gfx::Size preferred_size =
|
|
|
|
gfx::Size(GetDesiredPopupWidth(), GetDesiredPopupHeight());
|
2021-10-06 02:21:00 +00:00
|
|
|
|
2023-01-19 18:44:23 +00:00
|
|
|
popup_bounds_ =
|
|
|
|
CalculatePopupBounds(preferred_size, parent_->GetBoundsInScreen(), bounds,
|
|
|
|
base::i18n::IsRTL(), false);
|
2018-05-15 01:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Rect AutofillPopup::popup_bounds_in_view() {
|
|
|
|
gfx::Point origin(popup_bounds_.origin());
|
|
|
|
views::View::ConvertPointFromScreen(parent_, &origin);
|
|
|
|
|
|
|
|
return gfx::Rect(origin, popup_bounds_.size());
|
2018-04-05 00:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AutofillPopup::OnViewBoundsChanged(views::View* view) {
|
|
|
|
UpdatePopupBounds();
|
|
|
|
view_->DoUpdateBoundsAndRedrawPopup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AutofillPopup::OnViewIsDeleting(views::View* view) {
|
|
|
|
Hide();
|
2017-05-19 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AutofillPopup::GetDesiredPopupHeight() {
|
|
|
|
return 2 * kPopupBorderThickness + values_.size() * kRowHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AutofillPopup::GetDesiredPopupWidth() {
|
|
|
|
int popup_width = element_bounds_.width();
|
|
|
|
|
2017-05-23 09:41:59 +00:00
|
|
|
for (size_t i = 0; i < values_.size(); ++i) {
|
2018-04-18 01:55:30 +00:00
|
|
|
int row_size =
|
|
|
|
kEndPadding + 2 * kPopupBorderThickness +
|
|
|
|
gfx::GetStringWidth(GetValueAt(i), GetValueFontListForRow(i)) +
|
|
|
|
gfx::GetStringWidth(GetLabelAt(i), GetLabelFontListForRow(i));
|
2020-10-22 20:24:59 +00:00
|
|
|
if (!GetLabelAt(i).empty())
|
2017-05-19 19:35:13 +00:00
|
|
|
row_size += kNamePadding + kEndPadding;
|
|
|
|
|
|
|
|
popup_width = std::max(popup_width, row_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return popup_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Rect AutofillPopup::GetRowBounds(int index) {
|
|
|
|
int top = kPopupBorderThickness + index * kRowHeight;
|
|
|
|
|
|
|
|
return gfx::Rect(kPopupBorderThickness, top,
|
|
|
|
popup_bounds_.width() - 2 * kPopupBorderThickness,
|
|
|
|
kRowHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfx::FontList& AutofillPopup::GetValueFontListForRow(int index) const {
|
|
|
|
return bold_font_list_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const gfx::FontList& AutofillPopup::GetLabelFontListForRow(int index) const {
|
|
|
|
return smaller_font_list_;
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:17:35 +00:00
|
|
|
ui::ColorId AutofillPopup::GetBackgroundColorIDForRow(int index) const {
|
2017-05-21 17:55:19 +00:00
|
|
|
return (view_ && index == view_->GetSelectedLine())
|
2022-02-25 18:17:35 +00:00
|
|
|
? ui::kColorResultsTableHoveredBackground
|
|
|
|
: ui::kColorResultsTableNormalBackground;
|
2017-05-19 19:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AutofillPopup::GetLineCount() {
|
|
|
|
return values_.size();
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string AutofillPopup::GetValueAt(int i) {
|
2017-05-19 19:35:13 +00:00
|
|
|
return values_.at(i);
|
|
|
|
}
|
|
|
|
|
2021-03-16 16:18:45 +00:00
|
|
|
std::u16string AutofillPopup::GetLabelAt(int i) {
|
2017-05-19 19:35:13 +00:00
|
|
|
return labels_.at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
int AutofillPopup::LineFromY(int y) const {
|
|
|
|
int current_height = kPopupBorderThickness;
|
|
|
|
|
2017-05-23 09:41:59 +00:00
|
|
|
for (size_t i = 0; i < values_.size(); ++i) {
|
2017-05-19 19:35:13 +00:00
|
|
|
current_height += kRowHeight;
|
|
|
|
|
|
|
|
if (y <= current_height)
|
|
|
|
return i;
|
|
|
|
}
|
2017-05-19 19:52:18 +00:00
|
|
|
|
2017-05-19 19:35:13 +00:00
|
|
|
return values_.size() - 1;
|
|
|
|
}
|
|
|
|
|
2017-05-19 19:52:18 +00:00
|
|
|
} // namespace electron
|