1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/autofill/autofill_field_promo_view_impl.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/views/autofill/popup/popup_view_utils.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/views/view_class_properties.h"
namespace {
views::View* GetContentsWebView(content::WebContents* web_contents) {
Browser* browser = chrome::FindBrowserWithTab(web_contents);
if (!browser) {
return nullptr;
}
BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
return browser_view ? browser_view->contents_web_view() : nullptr;
}
} // namespace
namespace autofill {
// static
base::WeakPtr<AutofillFieldPromoView> AutofillFieldPromoView::CreateAndShow(
content::WebContents* web_contents,
const gfx::RectF& element_bounds,
const ui::ElementIdentifier& promo_element_identifier) {
views::View* contents_web_view = GetContentsWebView(web_contents);
if (!contents_web_view) {
return nullptr;
}
base::WeakPtr<AutofillFieldPromoView> promo_view =
contents_web_view
->AddChildView(base::WrapUnique<AutofillFieldPromoViewImpl>(
new AutofillFieldPromoViewImpl(web_contents, element_bounds,
promo_element_identifier)))
->GetWeakPtr();
return promo_view;
}
AutofillFieldPromoViewImpl::AutofillFieldPromoViewImpl(
content::WebContents* web_contents,
const gfx::RectF& element_bounds,
const ui::ElementIdentifier& promo_element_identifier)
: web_contents_(web_contents) {
SetViewBounds(element_bounds);
SetProperty(views::kElementIdentifierKey, promo_element_identifier);
}
AutofillFieldPromoViewImpl::~AutofillFieldPromoViewImpl() = default;
bool AutofillFieldPromoViewImpl::OverlapsWithPictureInPictureWindow() const {
return BoundsOverlapWithPictureInPictureWindow(GetBoundsInScreen());
}
void AutofillFieldPromoViewImpl::Close() {
parent()->RemoveChildViewT(this);
}
base::WeakPtr<AutofillFieldPromoView> AutofillFieldPromoViewImpl::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
void AutofillFieldPromoViewImpl::SetViewBounds(
const gfx::RectF& element_bounds) {
// The type of `element_bounds` is changed from `gfx::RectF` to `gfx::Rect`.
// They need to have the same type as the web contents bounds, because they
// are later intersected with them.
gfx::Rect element_bounds_container_bounds_intersection =
gfx::ToEnclosingRect(element_bounds);
// The coordinates of the element bounds are represented in a coordinate
// system which has the origin in the top-left corner of the `web_contents_`.
//
// The coordinates of `web_contents_` are represented in a coordinate system
// which has the origin in the top-left corner of the screen.
//
// O1 - origin of the coordinate system of `web_contents_` (top-left corner
// of the screen) O2 - origin of the coordinate system of the element
// (top-left corner of `web_contents_`)
//
// O1
// *___________________________________________________________
// | THIS AREA IS THE SCREEN |
// | |
// | O2 |
// | *_____________________________________ |
// | | | |
// | | THIS AREA IS THE WEB CONTENTS | |
// | | | |
// | | _________ | |
// | | |_________| <-- ELEMENT | |
// | | | |
// | |_____________________________________| |
// | |
// |__________________________________________________________|
//
// In order to be able to intersect them, they need to be represented in the
// same coordinate system. Thus, the bounds of the `web_contents_` are
// translated so that O1 overlaps with O2.
gfx::Rect web_contents_translated_bounds =
web_contents_->GetContainerBounds();
web_contents_translated_bounds -=
web_contents_translated_bounds.OffsetFromOrigin();
// Some elements may be partially outside the visible content area. Thus, we
// take into consideration only the part of the element which is inside the
// visible content area (i.e. the intersection between the web content bounds
// and the element bounds). Otherwise, the view would be partially outside of
// the visible content area and the IPH could be displayed there.
element_bounds_container_bounds_intersection.Intersect(
web_contents_translated_bounds);
// The view is drawn such that it completely overlaps with the bottom part of
// the element (it cannot be below the element, because the IPH would then
// float in the air). Height is not relevant as long as it is greater than 0.
SetBounds(element_bounds_container_bounds_intersection.x(),
element_bounds_container_bounds_intersection.y() +
element_bounds_container_bounds_intersection.height() - 1,
element_bounds_container_bounds_intersection.width(), 1);
SetPaintToLayer();
}
BEGIN_METADATA(AutofillFieldPromoViewImpl)
END_METADATA
} // namespace autofill
|