Skip to content

Commit 3a5caa7

Browse files
committed
menu popup ported
1 parent 9335fb6 commit 3a5caa7

15 files changed

+124
-31
lines changed

src/api/base/base.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ Base::~Base() {
3838
}
3939

4040

41-
void Base::Call(const std::string& method, const base::ListValue& arguments) {
41+
void Base::Call(const std::string& method, const base::ListValue& arguments,
42+
content::RenderViewHost* rvh) {
4243
NOTREACHED() << "Uncatched call in Base"
4344
<< " method:" << method
4445
<< " arguments:" << arguments;

src/api/base/base.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class DictionaryValue;
3131
class ListValue;
3232
}
3333

34+
namespace content {
35+
class RenderViewHost;
36+
}
37+
3438
namespace nw {
3539

3640
class ObjectManager;
@@ -44,7 +48,8 @@ class Base {
4448
virtual ~Base();
4549

4650
virtual void Call(const std::string& method,
47-
const base::ListValue& arguments);
51+
const base::ListValue& arguments,
52+
content::RenderViewHost* rvh = nullptr);
4853
virtual void CallSync(const std::string& method,
4954
const base::ListValue& arguments,
5055
base::ListValue* result);

src/api/menu/menu.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Menu::~Menu() {
3939
}
4040

4141
void Menu::Call(const std::string& method,
42-
const base::ListValue& arguments) {
42+
const base::ListValue& arguments,
43+
content::RenderViewHost* rvh) {
4344
if (method == "Append") {
4445
int object_id = 0;
4546
arguments.GetInteger(0, &object_id);
@@ -61,8 +62,7 @@ void Menu::Call(const std::string& method,
6162
arguments.GetInteger(0, &x);
6263
int y = 0;
6364
arguments.GetInteger(1, &y);
64-
// Popup(x, y, content::Shell::FromRenderViewHost(
65-
// object_manager()->render_view_host()));
65+
Popup(x, y, rvh);
6666
} else if (method == "EnableShowEvent") {
6767
arguments.GetBoolean(0, &enable_show_event_);
6868
} else {

src/api/menu/menu.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ class NwMenuModel : public SimpleMenuModel {
7878

7979
#endif
8080

81+
namespace aura {
82+
class Window;
83+
}
84+
8185
namespace content {
82-
class Shell;
86+
class RenderViewHost;
87+
class RenderFrameHost;
8388
}
8489

8590
namespace nw {
@@ -95,11 +100,13 @@ class Menu : public Base {
95100
~Menu() override;
96101

97102
void Call(const std::string& method,
98-
const base::ListValue& arguments) override;
103+
const base::ListValue& arguments,
104+
content::RenderViewHost* rvh = nullptr) override;
99105

100106
#if defined(OS_WIN) || defined(OS_LINUX)
101107
void UpdateKeys(views::FocusManager *focus_manager);
102108
ui::NwMenuModel* model() { return menu_model_.get(); }
109+
aura::Window* GetActiveNativeView(content::RenderFrameHost* rfh);
103110
#endif
104111

105112
bool enable_show_event() { return enable_show_event_; }
@@ -111,7 +118,7 @@ class Menu : public Base {
111118
void Append(MenuItem* menu_item);
112119
void Insert(MenuItem* menu_item, int pos);
113120
void Remove(MenuItem* menu_item, int pos);
114-
//void Popup(int x, int y, content::Shell*);
121+
void Popup(int x, int y, content::RenderViewHost*);
115122

116123
#if defined(OS_LINUX)
117124
std::vector<MenuItem*> menu_items;

src/api/menu/menu_views.cc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "base/strings/utf_string_conversions.h"
2525
#include "content/nw/src/api/object_manager.h"
2626
#include "content/nw/src/api/menuitem/menuitem.h"
27+
#include "content/public/browser/render_frame_host.h"
28+
#include "content/public/browser/render_view_host.h"
29+
#include "content/public/browser/render_widget_host_view.h"
2730
#include "content/public/browser/web_contents.h"
2831
#include "extensions/browser/app_window/app_window.h"
2932
#include "skia/ext/image_operations.h"
@@ -157,35 +160,35 @@ void Menu::Remove(MenuItem* menu_item, int pos) {
157160
menu_item->menu_ = NULL;
158161
}
159162

160-
#if 0 //FIXME
161-
void Menu::Popup(int x, int y, content::Shell* shell) {
163+
void Menu::Popup(int x, int y, content::RenderViewHost* rvh) {
162164
// Rebuild();
163165

164166
// Map point from document to screen.
165167
gfx::Point screen_point(x, y);
166168

167169
// Convert from content coordinates to window coordinates.
168170
// This code copied from chrome_web_contents_view_delegate_views.cc
169-
aura::Window* web_contents_window =
170-
shell->web_contents()->GetNativeView();
171-
aura::Window* root_window = web_contents_window->GetRootWindow();
171+
aura::Window* target_window = GetActiveNativeView(rvh->GetMainFrame());
172+
aura::Window* root_window = target_window->GetRootWindow();
173+
views::Widget* top_level_widget =
174+
views::Widget::GetTopLevelWidgetForNativeView(target_window);
172175
aura::client::ScreenPositionClient* screen_position_client =
173176
aura::client::GetScreenPositionClient(root_window);
174177
if (screen_position_client) {
175-
screen_position_client->ConvertPointToScreen(web_contents_window,
178+
screen_position_client->ConvertPointToScreen(target_window,
176179
&screen_point);
177180
}
178181
views::MenuRunner runner(menu_model_.get(), views::MenuRunner::CONTEXT_MENU);
179182
if (views::MenuRunner::MENU_DELETED ==
180-
runner.RunMenuAt(static_cast<nw::NativeWindowAura*>(shell->window())->window(),
183+
runner.RunMenuAt(top_level_widget,
181184
NULL,
182185
gfx::Rect(screen_point, gfx::Size()),
183186
views::MENU_ANCHOR_TOPRIGHT,
184187
ui::MENU_SOURCE_NONE))
185188
return;
186189
// menu_->RunMenuAt(screen_point, views::Menu2::ALIGN_TOPLEFT);
187190
}
188-
#endif
191+
189192
#if defined(OS_WIN)
190193
void Menu::Rebuild(const HMENU *parent_menu) {
191194
if (is_menu_modified_) {
@@ -269,4 +272,17 @@ void Menu::SetWindow(extensions::AppWindow* win) {
269272
}
270273
#endif
271274

275+
aura::Window* Menu::GetActiveNativeView(content::RenderFrameHost* rfh) {
276+
content::WebContents* web_contents =
277+
content::WebContents::FromRenderFrameHost(rfh);
278+
if (!web_contents) {
279+
LOG(ERROR) << "Menu: couldn't find WebContents";
280+
return NULL;
281+
}
282+
return web_contents->GetFullscreenRenderWidgetHostView()
283+
? web_contents->GetFullscreenRenderWidgetHostView()
284+
->GetNativeView()
285+
: web_contents->GetNativeView();
286+
}
287+
272288
} // namespace nw

src/api/menuitem/menuitem.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ MenuItem::~MenuItem() {
4242
}
4343

4444
void MenuItem::Call(const std::string& method,
45-
const base::ListValue& arguments) {
45+
const base::ListValue& arguments,
46+
content::RenderViewHost* rvh) {
4647
if (method == "SetLabel") {
4748
std::string label;
4849
arguments.GetString(0, &label);

src/api/menuitem/menuitem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class MenuItem : public Base {
5959
~MenuItem() override;
6060

6161
void Call(const std::string& method,
62-
const base::ListValue& arguments) override;
62+
const base::ListValue& arguments,
63+
content::RenderViewHost* rvh = nullptr) override;
6364

6465
#if defined(OS_WIN) || defined(OS_LINUX)
6566
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;

src/api/menuitem/menuitem_views.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "base/files/file_path.h"
2424
#include "base/strings/utf_string_conversions.h"
25+
#include "base/threading/thread_restrictions.h"
2526
#include "base/values.h"
2627
#include "content/nw/src/api/object_manager.h"
2728
#include "content/nw/src/api/menu/menu.h"
@@ -123,6 +124,7 @@ void MenuItem::SetLabel(const std::string& label) {
123124
}
124125

125126
void MenuItem::SetIcon(const std::string& icon) {
127+
base::ThreadRestrictions::ScopedAllowIO allow_io;
126128
is_modified_ = true;
127129
if (icon.empty()) {
128130
icon_ = gfx::Image();

src/api/nw_object_api.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bool NwObjectCallObjectMethodFunction::RunNWSync(base::ListValue* response, std:
6262
EXTENSION_FUNCTION_VALIDATE(args_->GetList(3, &arguments));
6363

6464
nw::ObjectManager* manager = nw::ObjectManager::Get(browser_context());
65-
manager->OnCallObjectMethod(id, type, method, *arguments);
65+
manager->OnCallObjectMethod(render_view_host(), id, type, method, *arguments);
6666
return true;
6767
}
6868

@@ -82,7 +82,7 @@ bool NwObjectCallObjectMethodSyncFunction::RunNWSync(base::ListValue* response,
8282
EXTENSION_FUNCTION_VALIDATE(args_->GetList(3, &arguments));
8383

8484
nw::ObjectManager* manager = nw::ObjectManager::Get(browser_context());
85-
manager->OnCallObjectMethodSync(id, type, method, *arguments, response);
85+
manager->OnCallObjectMethodSync(render_view_host(), id, type, method, *arguments, response);
8686
return true;
8787
}
8888

src/api/nw_shell_api.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ NwShellOpenItemFunction::~NwShellOpenItemFunction() {
1919

2020
bool NwShellOpenItemFunction::RunNWSync(base::ListValue* response, std::string* error) {
2121
nw::ObjectManager* manager = nw::ObjectManager::Get(browser_context());
22-
manager->OnCallStaticMethod("Shell", "OpenItem", *args_);
22+
manager->OnCallStaticMethod(render_view_host(), "Shell", "OpenItem", *args_);
2323
return true;
2424
}
2525

@@ -31,7 +31,7 @@ NwShellOpenExternalFunction::~NwShellOpenExternalFunction() {
3131

3232
bool NwShellOpenExternalFunction::RunNWSync(base::ListValue* response, std::string* error) {
3333
nw::ObjectManager* manager = nw::ObjectManager::Get(browser_context());
34-
manager->OnCallStaticMethod("Shell", "OpenExternal", *args_);
34+
manager->OnCallStaticMethod(render_view_host(), "Shell", "OpenExternal", *args_);
3535
return true;
3636
}
3737

@@ -43,7 +43,7 @@ NwShellShowItemInFolderFunction::~NwShellShowItemInFolderFunction() {
4343

4444
bool NwShellShowItemInFolderFunction::RunNWSync(base::ListValue* response, std::string* error) {
4545
nw::ObjectManager* manager = nw::ObjectManager::Get(browser_context());
46-
manager->OnCallStaticMethod("Shell", "ShowItemInFolder", *args_);
46+
manager->OnCallStaticMethod(render_view_host(), "Shell", "ShowItemInFolder", *args_);
4747
return true;
4848
}
4949

0 commit comments

Comments
 (0)