Remove JS override of setMenu
authorCheng Zhao <zcbenz@gmail.com>
Wed, 24 Jun 2015 11:51:11 +0000 (19:51 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 24 Jun 2015 11:51:11 +0000 (19:51 +0800)
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/api/lib/browser-window.coffee

index 0ee56db..86399ce 100644 (file)
@@ -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<v8::Value> value) {
+  mate::Handle<Menu> 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)
index 87eabaf..a0403dd 100644 (file)
@@ -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<Window>,
   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<v8::Value> menu);
   void SetAutoHideMenuBar(bool auto_hide);
   bool IsMenuBarAutoHide();
   void SetMenuBarVisibility(bool visible);
@@ -156,6 +152,7 @@ class Window : public mate::TrackableObject<Window>,
 
   v8::Global<v8::Value> web_contents_;
   v8::Global<v8::Value> devtools_web_contents_;
+  v8::Global<v8::Value> menu_;
 
   scoped_ptr<NativeWindow> window_;
 
index 1aa84bb..66e0670 100644 (file)
@@ -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()