electron/shell/browser/ui/autofill_popup.cc
electron-roller[bot] 3da598015b
chore: bump chromium to 100.0.4894.0 (main) (#32852)
* chore: bump chromium in DEPS to 100.0.4880.0

* resolve conflicts

* chore: update patches

* fix patch

* PIP20: add a new DocumentOverlayWindowViews subtype

https://chromium-review.googlesource.com/c/chromium/src/+/3252789

* Clean up PictureInPictureWindowManager::EnterPictureInPicture()

https://chromium-review.googlesource.com/c/chromium/src/+/3424145

* Remove StoragePartitionId.

https://chromium-review.googlesource.com/c/chromium/src/+/2811120

* Remove FLoC code

https://chromium-review.googlesource.com/c/chromium/src/+/3424359

* media: Make AddSupportedKeySystems() Async

https://chromium-review.googlesource.com/c/chromium/src/+/3430502

* [Extensions] Move some l10n file util methods to //extensions/browser

https://chromium-review.googlesource.com/c/chromium/src/+/3408192

* chore: IWYU

* Reland "webhid: Grant permissions for policy-allowed devices"

https://chromium-review.googlesource.com/c/chromium/src/+/3444147

* Migrate base::Value::GetList() to base::Value::GetListDeprecated(): 2/N.

https://chromium-review.googlesource.com/c/chromium/src/+/3435727
https://chromium-review.googlesource.com/c/chromium/src/+/3440910
https://chromium-review.googlesource.com/c/chromium/src/+/3440088

* [text blink period] Cache blink period instead of fetching from defaults

https://chromium-review.googlesource.com/c/chromium/src/+/3419059

* chore: update picture-in-picture.patch

https://chromium-review.googlesource.com/c/chromium/src/+/3252789

* ci: update to Xcode 13.2.1

https://chromium-review.googlesource.com/c/chromium/src/+/3437552

* chore: bump chromium in DEPS to 100.0.4882.1

* chore: update patches

* chore: bump chromium in DEPS to 100.0.4884.0

* chore: update patches

* chore: bump chromium in DEPS to 100.0.4886.0

* chore: update patches

* Refactor DownloadManager to use StoragePartitionConfig

https://chromium-review.googlesource.com/c/chromium/src/+/3222011

* Remove ToWebInputElement() in favor of new WebNode::DynamicTo<> helpers.

https://chromium-review.googlesource.com/c/chromium/src/+/3433852

* refactor: autofill to use the color pipeline

https://bugs.chromium.org/p/chromium/issues/detail?id=1249558
https://bugs.chromium.org/p/chromium/issues/detail?id=1003612

* [ProcessSingleton] Add many more trace events to cover all scenarios

https://chromium-review.googlesource.com/c/chromium/src/+/3429325

* fixup! PIP20: add a new DocumentOverlayWindowViews subtype

* chore: bump chromium in DEPS to 100.0.4888.0

* chore: update patches

* chore: update picture-in-picture.patch

* fixup! refactor: autofill to use the color pipeline

* ci: fixup fix sync

(cherry picked from commit c1e3e395465739bce5ca8e1c5ec1f5bd72b99ebd)

* chore: bump chromium in DEPS to 100.0.4889.0

* chore: update patches

* chore: fix feat_add_data_transfer_to_requestsingleinstancelock.patch

* fixup! PIP20: add a new DocumentOverlayWindowViews subtype

* Remove remaining NativeTheme::GetSystemColor() machinery.

https://chromium-review.googlesource.com/c/chromium/src/+/3421719

* ci: fetch proper esbuild for macos

* ci: fixup fetch proper esbuild for macos

* fix: failing Node.js test on outdated CurrentValueSerializerFormatVersion

* chore: bump chromium in DEPS to 100.0.4892.0

* 3460365: Set V8 fatal error callbacks during Isolate initialization

https://chromium-review.googlesource.com/c/chromium/src/+/3460365

* 3454343: PIP20: use permanent top controls

https://chromium-review.googlesource.com/c/chromium/src/+/3454343

* 3465574: Move most of GTK color mixers to ui/color/.

https://chromium-review.googlesource.com/c/chromium/src/+/3465574

* chore: fixup patch indices

* 3445327: [locales] Remove locales reference

https://chromium-review.googlesource.com/c/chromium/src/+/3445327

* 3456548: [DBB][#7] Blue border falls back to all tab if cropped-to zero pixels

https://chromium-review.googlesource.com/c/chromium/src/+/3456548

* 3441196: Convert GuestView's remaining legacy IPC messages to Mojo

https://chromium-review.googlesource.com/c/chromium/src/+/3441196

* 3455491: Don't include run_loop.h in thread_task_runner_handle.h

https://chromium-review.googlesource.com/c/chromium/src/+/3455491

* fixup! 3454343: PIP20: use permanent top controls

* 3442501: Add missing includes of //base/observer_list.h

https://chromium-review.googlesource.com/c/chromium/src/+/3442501

* 3437552: mac: Deploy a new hermetic build of Xcode 13.2.1 13C100

https://chromium-review.googlesource.com/c/chromium/src/+/3437552

* chore: bump chromium in DEPS to 100.0.4894.0

* fixup! 3460365: Set V8 fatal error callbacks during Isolate initialization

* chore: update patches

* 3425231: Use DnsOverHttpsConfig where appropriate

https://chromium-review.googlesource.com/c/chromium/src/+/3425231

* test: disable test-heapsnapshot-near-heap-limit-worker.js

As a result of CLs linked in https://bugs.chromium.org/p/v8/issues/detail?id=12503,
heap snapshotting near the heap limit DCHECKS in Node.js specs. This will
likely require a larger refactor in Node.js so i've disabled the test for
now and opened an upstream issue on node-v8 issue
at https://github.com/nodejs/node-v8/issues/218.

* Port all usage of NativeTheme color IDs to color pipeline

https://bugs.chromium.org/p/chromium/issues/detail?id=1249558

* chore: update patches after rebase

* ci: use gen2 machine for more disk space

* ci: don't try to make root volume writeable

* ci: use older xcode/macos for tests

* fix: html fullscreen transitions stacking

(cherry picked from commit 5e10965cdd7b2a024def5fc568912cefd0f05b44)

* ci: speed up woa testing

(cherry picked from commit 75c33c48b032137794f5734348a9ee3daa60d9de)
(cherry picked from commit e81996234029669663bf0daaababd34684dcbb17)

* ci: disable flaky tests on WOA

* ci: run remote tests separately to isolate issue there

* tests: disable node test parallel/test-worker-debug for now

* revert: fix: html fullscreen transitions stacking

* tests: disable flaky test on macOS arm64

* fixup circleci config so build tools can find xcode version

* make sure the workspace is clean before job runs

(cherry picked from commit 75f713c9748ac1a356846c39f268886130554fd6)

* tests: disable flaky test on Linux

* ci: debug why windows i32 is crashing

* Revert "ci: debug why windows i32 is crashing"

This reverts commit 4c4bba87ea76f16ef3b304dadff59ad4d366f60f.

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Jeremy Rose <nornagon@nornagon.net>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2022-02-25 13:17:35 -05:00

346 lines
12 KiB
C++

// 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.
#include <algorithm>
#include <memory>
#include <vector>
#include "base/feature_list.h"
#include "base/i18n/rtl.h"
#include "components/autofill/core/common/autofill_features.h"
#include "electron/buildflags/buildflags.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/ui/autofill_popup.h"
#include "shell/common/api/api.mojom.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/text_utils.h"
#if BUILDFLAG(ENABLE_OSR)
#include "shell/browser/osr/osr_render_widget_host_view.h"
#include "shell/browser/osr/osr_view_proxy.h"
#endif
namespace electron {
void CalculatePopupXAndWidthHorizontallyCentered(
int popup_preferred_width,
const gfx::Rect& content_area_bounds,
const gfx::Rect& element_bounds,
bool is_rtl,
gfx::Rect* bubble_bounds) {
// The preferred horizontal starting point for the pop-up is at the horizontal
// center of the field.
int preferred_starting_point =
base::clamp(element_bounds.x() + (element_bounds.size().width() / 2),
content_area_bounds.x(), content_area_bounds.right());
// 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.
int amount_to_grow_in_unpreffered_direction =
std::max(0, popup_width - space_to_grow_in_preferred_direction);
bubble_bounds->set_width(popup_width);
if (is_rtl) {
// Note, in RTL the |pop_up_width| must be subtracted to achieve
// right-alignment of the pop-up with the element.
bubble_bounds->set_x(preferred_starting_point - popup_width +
amount_to_grow_in_unpreffered_direction);
} else {
bubble_bounds->set_x(preferred_starting_point -
amount_to_grow_in_unpreffered_direction);
}
}
void CalculatePopupXAndWidth(int popup_preferred_width,
const gfx::Rect& content_area_bounds,
const gfx::Rect& element_bounds,
bool is_rtl,
gfx::Rect* bubble_bounds) {
int right_growth_start = base::clamp(
element_bounds.x(), content_area_bounds.x(), content_area_bounds.right());
int left_growth_end =
base::clamp(element_bounds.right(), content_area_bounds.x(),
content_area_bounds.right());
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;
}
bubble_bounds->set_width(popup_width);
bubble_bounds->set_x(grow_left ? left_growth_end - popup_width
: right_growth_start);
}
void CalculatePopupYAndHeight(int popup_preferred_height,
const gfx::Rect& content_area_bounds,
const gfx::Rect& element_bounds,
gfx::Rect* bubble_bounds) {
int top_growth_end = base::clamp(element_bounds.y(), content_area_bounds.y(),
content_area_bounds.bottom());
int bottom_growth_start =
base::clamp(element_bounds.bottom(), content_area_bounds.y(),
content_area_bounds.bottom());
int top_available = top_growth_end - content_area_bounds.y();
int bottom_available = content_area_bounds.bottom() - bottom_growth_start;
bubble_bounds->set_height(popup_preferred_height);
bubble_bounds->set_y(top_growth_end);
if (bottom_available >= popup_preferred_height ||
bottom_available >= top_available) {
bubble_bounds->AdjustToFit(
gfx::Rect(bubble_bounds->x(), element_bounds.bottom(),
bubble_bounds->width(), bottom_available));
} else {
bubble_bounds->AdjustToFit(
gfx::Rect(bubble_bounds->x(), content_area_bounds.y(),
bubble_bounds->width(), top_available));
}
}
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) {
gfx::Rect bubble_bounds;
if (horizontally_centered) {
CalculatePopupXAndWidthHorizontallyCentered(
desired_size.width(), content_area_bounds, element_bounds, is_rtl,
&bubble_bounds);
} else {
CalculatePopupXAndWidth(desired_size.width(), content_area_bounds,
element_bounds, is_rtl, &bubble_bounds);
}
CalculatePopupYAndHeight(desired_size.height(), content_area_bounds,
element_bounds, &bubble_bounds);
return bubble_bounds;
}
AutofillPopup::AutofillPopup() {
bold_font_list_ = gfx::FontList().DeriveWithWeight(gfx::Font::Weight::BOLD);
smaller_font_list_ =
gfx::FontList().DeriveWithSizeDelta(kSmallerFontSizeDelta);
}
AutofillPopup::~AutofillPopup() {
Hide();
}
void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
content::RenderFrameHost* embedder_frame_host,
bool offscreen,
views::View* parent,
const gfx::RectF& r) {
Hide();
frame_host_ = frame_host;
element_bounds_ = gfx::ToEnclosedRect(r);
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());
parent_ = parent;
parent_->AddObserver(this);
view_ = new AutofillPopupView(this, parent->GetWidget());
#if BUILDFLAG(ENABLE_OSR)
if (offscreen) {
auto* rwhv = frame_host->GetView();
if (embedder_frame_host != nullptr) {
rwhv = embedder_frame_host->GetView();
}
auto* osr_rwhv = static_cast<OffScreenRenderWidgetHostView*>(rwhv);
view_->view_proxy_ = std::make_unique<OffscreenViewProxy>(view_);
osr_rwhv->AddViewProxy(view_->view_proxy_.get());
}
#endif
// Do this after OSR setup, we check for view_proxy_ when showing
view_->Show();
}
void AutofillPopup::Hide() {
if (parent_) {
parent_->RemoveObserver(this);
parent_ = nullptr;
}
if (view_) {
view_->Hide();
view_ = nullptr;
}
}
void AutofillPopup::SetItems(const std::vector<std::u16string>& values,
const std::vector<std::u16string>& labels) {
DCHECK(view_);
values_ = values;
labels_ = labels;
UpdatePopupBounds();
view_->OnSuggestionsChanged();
if (view_) // could be hidden after the change
view_->DoUpdateBoundsAndRedrawPopup();
}
void AutofillPopup::AcceptSuggestion(int index) {
mojo::AssociatedRemote<mojom::ElectronAutofillAgent> autofill_agent;
frame_host_->GetRemoteAssociatedInterfaces()->GetInterface(&autofill_agent);
autofill_agent->AcceptDataListSuggestion(GetValueAt(index));
}
void AutofillPopup::UpdatePopupBounds() {
DCHECK(parent_);
gfx::Point origin(element_bounds_.origin());
views::View::ConvertPointToScreen(parent_, &origin);
gfx::Rect bounds(origin, element_bounds_.size());
gfx::Rect window_bounds = parent_->GetBoundsInScreen();
gfx::Size preferred_size =
gfx::Size(GetDesiredPopupWidth(), GetDesiredPopupHeight());
if (base::FeatureList::IsEnabled(
autofill::features::kAutofillCenterAlignedSuggestions)) {
popup_bounds_ = CalculatePopupBounds(preferred_size, window_bounds, bounds,
base::i18n::IsRTL(), true);
CalculatePopupXAndWidthHorizontallyCentered(
preferred_size.width(), window_bounds, element_bounds_,
base::i18n::IsRTL(), &popup_bounds_);
} else {
popup_bounds_ = CalculatePopupBounds(preferred_size, window_bounds, bounds,
base::i18n::IsRTL(), false);
}
}
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());
}
void AutofillPopup::OnViewBoundsChanged(views::View* view) {
UpdatePopupBounds();
view_->DoUpdateBoundsAndRedrawPopup();
}
void AutofillPopup::OnViewIsDeleting(views::View* view) {
Hide();
}
int AutofillPopup::GetDesiredPopupHeight() {
return 2 * kPopupBorderThickness + values_.size() * kRowHeight;
}
int AutofillPopup::GetDesiredPopupWidth() {
int popup_width = element_bounds_.width();
for (size_t i = 0; i < values_.size(); ++i) {
int row_size =
kEndPadding + 2 * kPopupBorderThickness +
gfx::GetStringWidth(GetValueAt(i), GetValueFontListForRow(i)) +
gfx::GetStringWidth(GetLabelAt(i), GetLabelFontListForRow(i));
if (!GetLabelAt(i).empty())
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_;
}
ui::ColorId AutofillPopup::GetBackgroundColorIDForRow(int index) const {
return (view_ && index == view_->GetSelectedLine())
? ui::kColorResultsTableHoveredBackground
: ui::kColorResultsTableNormalBackground;
}
int AutofillPopup::GetLineCount() {
return values_.size();
}
std::u16string AutofillPopup::GetValueAt(int i) {
return values_.at(i);
}
std::u16string AutofillPopup::GetLabelAt(int i) {
return labels_.at(i);
}
int AutofillPopup::LineFromY(int y) const {
int current_height = kPopupBorderThickness;
for (size_t i = 0; i < values_.size(); ++i) {
current_height += kRowHeight;
if (y <= current_height)
return i;
}
return values_.size() - 1;
}
} // namespace electron