From 9b9108f789a6b4e04383b16cba2047f03c746bcd Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 24 Jun 2015 19:51:11 +0800 Subject: [PATCH] Remove JS override of setMenu --- atom/browser/api/atom_api_window.cc | 18 +++++++++++++++--- atom/browser/api/atom_api_window.h | 7 ++----- atom/browser/api/lib/browser-window.coffee | 6 ------ 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 0ee56db..86399ce 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -409,8 +409,20 @@ void Window::SetOverlayIcon(const gfx::Image& overlay, window_->SetOverlayIcon(overlay, description); } -void Window::SetMenu(ui::SimpleMenuModel* menu) { - window_->SetMenu(menu); +void Window::SetMenu(v8::Isolate* isolate, v8::Local value) { + mate::Handle menu; + if (value->IsObject() && + mate::V8ToString(value->ToObject()->GetConstructorName()) == "Menu" && + mate::ConvertFromV8(isolate, value, &menu)) { + menu_.Reset(isolate, menu.ToV8()); + window_->SetMenu(menu->model()); + } else if (value->IsNull()) { + menu_.Reset(); + window_->SetMenu(nullptr); + } else { + isolate->ThrowException(v8::Exception::TypeError( + mate::StringToV8(isolate, "Invalid Menu"))); + } } void Window::SetAutoHideMenuBar(bool auto_hide) { @@ -515,7 +527,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("capturePage", &Window::CapturePage) .SetMethod("setProgressBar", &Window::SetProgressBar) .SetMethod("setOverlayIcon", &Window::SetOverlayIcon) - .SetMethod("_setMenu", &Window::SetMenu) + .SetMethod("setMenu", &Window::SetMenu) .SetMethod("setAutoHideMenuBar", &Window::SetAutoHideMenuBar) .SetMethod("isMenuBarAutoHide", &Window::IsMenuBarAutoHide) .SetMethod("setMenuBarVisibility", &Window::SetMenuBarVisibility) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 87eabaf..a0403dd 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -25,10 +25,6 @@ class Arguments; class Dictionary; } -namespace ui { -class SimpleMenuModel; -} - namespace atom { class NativeWindow; @@ -137,7 +133,7 @@ class Window : public mate::TrackableObject, void SetProgressBar(double progress); void SetOverlayIcon(const gfx::Image& overlay, const std::string& description); - void SetMenu(ui::SimpleMenuModel* menu); + void SetMenu(v8::Isolate* isolate, v8::Local menu); void SetAutoHideMenuBar(bool auto_hide); bool IsMenuBarAutoHide(); void SetMenuBarVisibility(bool visible); @@ -156,6 +152,7 @@ class Window : public mate::TrackableObject, v8::Global web_contents_; v8::Global devtools_web_contents_; + v8::Global menu_; scoped_ptr window_; diff --git a/atom/browser/api/lib/browser-window.coffee b/atom/browser/api/lib/browser-window.coffee index 1aa84bb..66e0670 100644 --- a/atom/browser/api/lib/browser-window.coffee +++ b/atom/browser/api/lib/browser-window.coffee @@ -27,12 +27,6 @@ BrowserWindow::_init = -> @on 'focus', (event) => app.emit 'browser-window-focus', event, this -BrowserWindow::setMenu = (menu) -> - throw new TypeError('Invalid menu') unless menu is null or menu?.constructor?.name is 'Menu' - - @menu = menu # Keep a reference of menu in case of GC. - @_setMenu menu - BrowserWindow.getFocusedWindow = -> windows = BrowserWindow.getAllWindows() return window for window in windows when window.isFocused() -- 2.7.4