Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6a17131

Browse files
committed
Fix nwjs#2072: [WIN] context menu popup in wrong (screen) position
1 parent d3d5136 commit 6a17131

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/api/menu/menu_win.cc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#include "content/public/browser/web_contents.h"
2929
#include "content/public/browser/web_contents_view.h"
3030
#include "skia/ext/image_operations.h"
31+
#include "ui/aura/client/screen_position_client.h"
32+
#include "ui/aura/window.h"
33+
#include "ui/aura/window_tree_host.h"
3134
#include "ui/gfx/gdi_util.h"
3235
#include "ui/gfx/icon_util.h"
3336
#include "ui/views/controls/menu/menu_2.h"
@@ -144,12 +147,20 @@ void Menu::Popup(int x, int y, content::Shell* shell) {
144147
Rebuild();
145148

146149
// Map point from document to screen.
147-
POINT screen_point = { x, y };
148-
ClientToScreen((HWND)shell->web_contents()->GetView()->GetNativeView(),
149-
&screen_point);
150-
151-
menu_->RunMenuAt(gfx::Point(screen_point.x, screen_point.y),
152-
views::Menu2::ALIGN_TOPLEFT);
150+
gfx::Point screen_point(x, y);
151+
152+
// Convert from content coordinates to window coordinates.
153+
// This code copied from chrome_web_contents_view_delegate_views.cc
154+
aura::Window* web_contents_window =
155+
shell->web_contents()->GetView()->GetNativeView();
156+
aura::Window* root_window = web_contents_window->GetRootWindow();
157+
aura::client::ScreenPositionClient* screen_position_client =
158+
aura::client::GetScreenPositionClient(root_window);
159+
if (screen_position_client) {
160+
screen_position_client->ConvertPointToScreen(web_contents_window,
161+
&screen_point);
162+
}
163+
menu_->RunMenuAt(screen_point, views::Menu2::ALIGN_TOPLEFT);
153164
}
154165

155166
void Menu::Rebuild(const HMENU *parent_menu) {

0 commit comments

Comments
 (0)