Browse Source

Fix autofill popup position when the popup is spawned from a webview (#13184)

Heilig Benedek 6 years ago
parent
commit
90911a423a

+ 13 - 2
atom/browser/api/atom_api_web_contents.cc

@@ -1035,8 +1035,19 @@ void WebContents::ShowAutofillPopup(content::RenderFrameHost* frame_host,
                                     const std::vector<base::string16>& values,
                                     const std::vector<base::string16>& labels) {
   bool offscreen = IsOffScreen() || (embedder_ && embedder_->IsOffScreen());
-  CommonWebContentsDelegate::ShowAutofillPopup(offscreen, frame_host, bounds,
-                                               values, labels);
+  gfx::RectF popup_bounds(bounds);
+  content::RenderFrameHost* embedder_frame_host = nullptr;
+  if (embedder_) {
+    auto* embedder_view = embedder_->web_contents()->GetMainFrame()->GetView();
+    auto* view = web_contents()->GetMainFrame()->GetView();
+    auto offset = view->GetViewBounds().origin() -
+                  embedder_view->GetViewBounds().origin();
+    popup_bounds.Offset(offset.x(), offset.y());
+    embedder_frame_host = embedder_->web_contents()->GetMainFrame();
+  }
+
+  CommonWebContentsDelegate::ShowAutofillPopup(
+      frame_host, embedder_frame_host, offscreen, popup_bounds, values, labels);
 }
 #endif
 

+ 3 - 2
atom/browser/common_web_contents_delegate.h

@@ -92,8 +92,9 @@ class CommonWebContentsDelegate
 
   // Autofill related events.
 #if defined(TOOLKIT_VIEWS) && !defined(OS_MACOSX)
-  void ShowAutofillPopup(bool offscreen,
-                         content::RenderFrameHost* frame_host,
+  void ShowAutofillPopup(content::RenderFrameHost* frame_host,
+                         content::RenderFrameHost* embedder_frame_host,
+                         bool offscreen,
                          const gfx::RectF& bounds,
                          const std::vector<base::string16>& values,
                          const std::vector<base::string16>& labels);

+ 5 - 3
atom/browser/common_web_contents_delegate_views.cc

@@ -4,6 +4,7 @@
 
 #include "atom/browser/common_web_contents_delegate.h"
 
+#include "atom/browser/api/atom_api_web_contents_view.h"
 #include "atom/browser/native_window_views.h"
 #include "base/strings/string_util.h"
 #include "content/public/browser/native_web_keyboard_event.h"
@@ -28,8 +29,9 @@ void CommonWebContentsDelegate::HandleKeyboardEvent(
 }
 
 void CommonWebContentsDelegate::ShowAutofillPopup(
-    bool offscreen,
     content::RenderFrameHost* frame_host,
+    content::RenderFrameHost* embedder_frame_host,
+    bool offscreen,
     const gfx::RectF& bounds,
     const std::vector<base::string16>& values,
     const std::vector<base::string16>& labels) {
@@ -37,8 +39,8 @@ void CommonWebContentsDelegate::ShowAutofillPopup(
     return;
 
   auto* window = static_cast<NativeWindowViews*>(owner_window());
-  autofill_popup_->CreateView(frame_host, offscreen, window->content_view(),
-                              bounds);
+  autofill_popup_->CreateView(frame_host, embedder_frame_host, offscreen,
+                              window->content_view(), bounds);
   autofill_popup_->SetItems(values, labels);
 }
 

+ 13 - 2
atom/browser/ui/autofill_popup.cc

@@ -115,6 +115,7 @@ AutofillPopup::~AutofillPopup() {
 }
 
 void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
+                               content::RenderFrameHost* embedder_frame_host,
                                bool offscreen,
                                views::View* parent,
                                const gfx::RectF& r) {
@@ -123,6 +124,12 @@ void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
   frame_host_ = frame_host;
   element_bounds_ = gfx::ToEnclosedRect(r);
 
+  gfx::Vector2d height_offset(0, element_bounds_.height());
+  popup_bounds_in_view_ = element_bounds_ + height_offset;
+  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);
 
@@ -131,8 +138,12 @@ void AutofillPopup::CreateView(content::RenderFrameHost* frame_host,
 
 #if defined(ENABLE_OSR)
   if (offscreen) {
-    auto* osr_rwhv =
-        static_cast<OffScreenRenderWidgetHostView*>(frame_host_->GetView());
+    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_.reset(new OffscreenViewProxy(view_));
     osr_rwhv->AddViewProxy(view_->view_proxy_.get());
   }

+ 1 - 0
atom/browser/ui/autofill_popup.h

@@ -24,6 +24,7 @@ class AutofillPopup : public views::ViewObserver {
   ~AutofillPopup() override;
 
   void CreateView(content::RenderFrameHost* render_frame,
+                  content::RenderFrameHost* embedder_frame,
                   bool offscreen,
                   views::View* parent,
                   const gfx::RectF& bounds);