Move AutofillPopup from NativeWindow to WebContents (#12514)

This commit is contained in:
Cheng Zhao 2018-04-05 09:53:51 +09:00 committed by GitHub
parent 3a45d541f3
commit c75dd93b92
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 123 additions and 126 deletions

View file

@ -6,19 +6,22 @@
#include <utility>
#include <vector>
#if defined(ENABLE_OSR)
#include "atom/browser/osr/osr_render_widget_host_view.h"
#include "atom/browser/osr/osr_view_proxy.h"
#endif
#include "atom/browser/native_window_views.h"
#include "atom/browser/ui/autofill_popup.h"
#include "atom/common/api/api_messages.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 defined(ENABLE_OSR)
#include "atom/browser/osr/osr_render_widget_host_view.h"
#include "atom/browser/osr/osr_view_proxy.h"
#endif
namespace atom {
namespace {
@ -95,20 +98,17 @@ std::pair<int, int> CalculatePopupYAndHeight(
}
}
display::Display GetDisplayNearestPoint(
const gfx::Point& point,
gfx::NativeView container_view) {
display::Display GetDisplayNearestPoint(const gfx::Point& point) {
return display::Screen::GetScreen()->GetDisplayNearestPoint(point);
}
} // namespace
AutofillPopup::AutofillPopup(gfx::NativeView container_view)
: container_view_(container_view), view_(nullptr) {
AutofillPopup::AutofillPopup() {
bold_font_list_ =
gfx::FontList().DeriveWithWeight(gfx::Font::Weight::BOLD);
gfx::FontList().DeriveWithWeight(gfx::Font::Weight::BOLD);
smaller_font_list_ =
gfx::FontList().DeriveWithSizeDelta(kSmallerFontSizeDelta);
gfx::FontList().DeriveWithSizeDelta(kSmallerFontSizeDelta);
}
AutofillPopup::~AutofillPopup() {
@ -118,20 +118,17 @@ AutofillPopup::~AutofillPopup() {
void AutofillPopup::CreateView(
content::RenderFrameHost* frame_host,
bool offscreen,
views::Widget* parent_widget,
views::View* parent,
const gfx::RectF& r) {
frame_host_ = frame_host;
gfx::Rect lb(std::floor(r.x()), std::floor(r.y() + r.height()),
std::floor(r.width()), std::floor(r.height()));
gfx::Point menu_position(lb.origin());
popup_bounds_in_view_ = lb;
views::View::ConvertPointToScreen(
parent_widget->GetContentsView(), &menu_position);
popup_bounds_ = gfx::Rect(menu_position, lb.size());
element_bounds_ = popup_bounds_;
Hide();
view_ = new AutofillPopupView(this, parent_widget);
frame_host_ = frame_host;
element_bounds_ = gfx::ToEnclosedRect(r);
parent_ = parent;
parent_->AddObserver(this);
view_ = new AutofillPopupView(this, parent->GetWidget());
view_->Show();
#if defined(ENABLE_OSR)
@ -145,6 +142,10 @@ void AutofillPopup::CreateView(
}
void AutofillPopup::Hide() {
if (parent_) {
parent_->RemoveObserver(this);
parent_ = nullptr;
}
if (view_) {
view_->Hide();
view_ = nullptr;
@ -152,28 +153,31 @@ void AutofillPopup::Hide() {
}
void AutofillPopup::SetItems(const std::vector<base::string16>& values,
const std::vector<base::string16>& labels) {
const std::vector<base::string16>& labels) {
DCHECK(view_);
values_ = values;
labels_ = labels;
if (view_) {
view_->OnSuggestionsChanged();
}
UpdatePopupBounds();
view_->OnSuggestionsChanged();
if (view_) // could be hidden after the change
view_->DoUpdateBoundsAndRedrawPopup();
}
void AutofillPopup::AcceptSuggestion(int index) {
frame_host_->Send(new AtomAutofillFrameMsg_AcceptSuggestion(
frame_host_->GetRoutingID(), GetValueAt(index)));
frame_host_->GetRoutingID(), GetValueAt(index)));
}
void AutofillPopup::UpdatePopupBounds(int height_compensation) {
void AutofillPopup::UpdatePopupBounds() {
DCHECK(parent_);
gfx::Point origin(element_bounds_.origin());
views::View::ConvertPointToScreen(parent_, &origin);
gfx::Rect bounds(origin, element_bounds_.size());
int desired_width = GetDesiredPopupWidth();
int desired_height = GetDesiredPopupHeight();
bool is_rtl = false;
gfx::Point origin(element_bounds_.origin().x(),
element_bounds_.origin().y() - height_compensation);
gfx::Rect bounds(origin, element_bounds_.size());
gfx::Point top_left_corner_of_popup =
origin + gfx::Vector2d(bounds.width() - desired_width, -desired_height);
@ -184,9 +188,9 @@ void AutofillPopup::UpdatePopupBounds(int height_compensation) {
origin + gfx::Vector2d(desired_width, bounds.height() + desired_height);
display::Display top_left_display =
GetDisplayNearestPoint(top_left_corner_of_popup, container_view_);
GetDisplayNearestPoint(top_left_corner_of_popup);
display::Display bottom_right_display =
GetDisplayNearestPoint(bottom_right_corner_of_popup, container_view_);
GetDisplayNearestPoint(bottom_right_corner_of_popup);
std::pair<int, int> popup_x_and_width =
CalculatePopupXAndWidth(top_left_display, bottom_right_display,
@ -201,8 +205,15 @@ void AutofillPopup::UpdatePopupBounds(int height_compensation) {
popup_bounds_in_view_ = gfx::Rect(
popup_bounds_in_view_.origin(),
gfx::Size(popup_x_and_width.second, popup_y_and_height.second));
if (view_)
view_->DoUpdateBoundsAndRedrawPopup();
}
void AutofillPopup::OnViewBoundsChanged(views::View* view) {
UpdatePopupBounds();
view_->DoUpdateBoundsAndRedrawPopup();
}
void AutofillPopup::OnViewIsDeleting(views::View* view) {
Hide();
}
int AutofillPopup::GetDesiredPopupHeight() {

View file

@ -18,22 +18,28 @@ namespace atom {
class AutofillPopupView;
class AutofillPopup {
class AutofillPopup : public views::ViewObserver {
public:
explicit AutofillPopup(gfx::NativeView);
AutofillPopup();
~AutofillPopup();
void CreateView(content::RenderFrameHost* render_frame,
bool offscreen, views::Widget* widget, const gfx::RectF& bounds);
bool offscreen,
views::View* parent,
const gfx::RectF& bounds);
void Hide();
void SetItems(const std::vector<base::string16>& values,
const std::vector<base::string16>& labels);
void UpdatePopupBounds(int height_compensation);
void UpdatePopupBounds();
private:
friend class AutofillPopupView;
// views::ViewObserver:
void OnViewBoundsChanged(views::View* view) override;
void OnViewIsDeleting(views::View* view) override;
void AcceptSuggestion(int index);
int GetDesiredPopupHeight();
@ -48,9 +54,6 @@ class AutofillPopup {
base::string16 GetLabelAt(int i);
int LineFromY(int y) const;
// The native view that contains this
gfx::NativeView container_view_;
int selected_index_;
// Popup location
@ -70,10 +73,13 @@ class AutofillPopup {
// For sending the accepted suggestion to the render frame that
// asked to open the popup
content::RenderFrameHost* frame_host_;
content::RenderFrameHost* frame_host_ = nullptr;
// The popup view. The lifetime is managed by the owning Widget
AutofillPopupView* view_;
AutofillPopupView* view_ = nullptr;
// The parent view that the popup view shows on. Weak ref.
views::View* parent_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(AutofillPopup);
};