]> khamidou — GitHub mirrors - electron.git/commitdiff
win: Update states when setting checked property.
authorCheng Zhao <zcbenz@gmail.com>
Mon, 26 May 2014 03:34:36 +0000 (11:34 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 26 May 2014 03:34:36 +0000 (11:34 +0800)
atom/browser/api/atom_api_menu.cc
atom/browser/api/atom_api_menu.h
atom/browser/api/atom_api_menu_gtk.cc
atom/browser/api/atom_api_menu_gtk.h
atom/browser/api/atom_api_menu_win.cc
atom/browser/api/atom_api_menu_win.h
atom/browser/api/lib/menu.coffee
atom/browser/native_window_win.h

index 2eb1a816adf88b7d0ab5330f00cb49ea9205f8d2..a76d77b0f3e461bc712120648a1f9952978233f2 100644 (file)
@@ -44,7 +44,9 @@ v8::Handle<v8::Value> CallDelegate(v8::Handle<v8::Value> default_value,
 
 }  // namespace
 
-Menu::Menu() : model_(new ui::SimpleMenuModel(this)) {
+Menu::Menu()
+    : model_(new ui::SimpleMenuModel(this)),
+      parent_(NULL) {
 }
 
 Menu::~Menu() {
@@ -167,6 +169,7 @@ void Menu::InsertSubMenuAt(int index,
                            int command_id,
                            const base::string16& label,
                            Menu* menu) {
+  menu->parent_ = this;
   model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
 }
 
@@ -231,6 +234,9 @@ void Menu::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
 #if defined(OS_WIN) || defined(TOOLKIT_GTK)
       .SetMethod("_attachToWindow", &Menu::AttachToWindow)
+#endif
+#if defined(OS_WIN)
+      .SetMethod("_updateStates", &Menu::UpdateStates)
 #endif
       .SetMethod("_popup", &Menu::Popup);
 }
index a9e9e926ba0bf2577877d3c4330c8bdb8280be99..a309e1b1eb4055e369f5455adc8f809ace3ac4d7 100644 (file)
@@ -54,6 +54,7 @@ class Menu : public mate::Wrappable,
   virtual void Popup(Window* window) = 0;
 
   scoped_ptr<ui::SimpleMenuModel> model_;
+  Menu* parent_;
 
  private:
   void InsertItemAt(int index, int command_id, const base::string16& label);
@@ -80,8 +81,12 @@ class Menu : public mate::Wrappable,
   bool IsEnabledAt(int index) const;
   bool IsVisibleAt(int index) const;
 
+#if defined(OS_WIN)
+  virtual void UpdateStates() = 0;
+#endif
+
 #if defined(OS_WIN) || defined(TOOLKIT_GTK)
-  void AttachToWindow(Window* window);
+  virtual void AttachToWindow(Window* window) = 0;
 #endif
 
   DISALLOW_COPY_AND_ASSIGN(Menu);
index 86076bcc416014247dc2727f7407befbe6b83b0e..acaca4b2f8e8c9626788a565ff4923b87ebf13f0 100644 (file)
@@ -37,7 +37,7 @@ void MenuGtk::Popup(Window* window) {
   menu_gtk_->PopupAsContext(point, triggering_event_time);
 }
 
-void Menu::AttachToWindow(Window* window) {
+void MenuGtk::AttachToWindow(Window* window) {
   static_cast<NativeWindowGtk*>(window->window())->SetMenu(model_.get());
 }
 
index 3c39fb730b7bd7939d6f0a7af834eb6d8fe8fe5c..eb6c94c9932bf620fcce65664b001b0288aa306d 100644 (file)
@@ -19,6 +19,7 @@ class MenuGtk : public Menu,
 
  protected:
   virtual void Popup(Window* window) OVERRIDE;
+  virtual void AttachToWindow(Window* window) OVERRIDE;
 
  private:
   scoped_ptr<::MenuGtk> menu_gtk_;
index d5a24ceadb77a4a510c1b3884324af667513f2f8..6bf515c7bdd5e7ee35c6b91cf01205a709ebb47f 100644 (file)
@@ -15,17 +15,28 @@ namespace atom {
 
 namespace api {
 
-MenuWin::MenuWin() {
+MenuWin::MenuWin() : menu_(NULL) {
 }
 
 void MenuWin::Popup(Window* window) {
   gfx::Point cursor = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint();
-  menu_.reset(new atom::Menu2(model_.get()));
+  popup_menu_.reset(new atom::Menu2(model_.get()));
+  menu_ = popup_menu_.get();
   menu_->RunContextMenuAt(cursor);
 }
 
-void Menu::AttachToWindow(Window* window) {
-  static_cast<NativeWindowWin*>(window->window())->SetMenu(model_.get());
+void MenuWin::UpdateStates() {
+  MenuWin* top = this;
+  while (top->parent_)
+    top = static_cast<MenuWin*>(top->parent_);
+  if (top->menu_)
+    top->menu_->UpdateStates();
+}
+
+void MenuWin::AttachToWindow(Window* window) {
+  NativeWindowWin* nw = static_cast<NativeWindowWin*>(window->window());
+  nw->SetMenu(model_.get());
+  menu_ = nw->menu();
 }
 
 // static
index 965aa5983141bab32649b3f1da932d791b906149..1dd66c488ee8731e5237777efe91d81d3b18ea6e 100644 (file)
@@ -19,9 +19,12 @@ class MenuWin : public Menu {
 
  protected:
   virtual void Popup(Window* window) OVERRIDE;
+  virtual void UpdateStates() OVERRIDE;
+  virtual void AttachToWindow(Window* window) OVERRIDE;
 
  private:
-  scoped_ptr<atom::Menu2> menu_;
+  atom::Menu2* menu_;  // Weak ref, could be window menu or popup menu.
+  scoped_ptr<atom::Menu2> popup_menu_;
 
   DISALLOW_COPY_AND_ASSIGN(MenuWin);
 };
index a119d9fdabf1e66b94d7d892102a73e490729dd5..0e7b4c263f9c91defa0558062da71950272a2481 100644 (file)
@@ -64,7 +64,18 @@ Menu::insert = (pos, item) ->
 
   switch item.type
     when 'normal' then @insertItem pos, item.commandId, item.label
-    when 'checkbox' then @insertCheckItem pos, item.commandId, item.label
+    when 'checkbox'
+      # Update states when clicked on Windows.
+      if process.platform is 'win32'
+        v8Util.setHiddenValue item, 'checked', item.checked
+        Object.defineProperty item, 'checked',
+          enumerable: true
+          get: -> v8Util.getHiddenValue item, 'checked'
+          set: (val) =>
+            v8Util.setHiddenValue item, 'checked', val
+            @_updateStates() if process.platform is 'win32'
+
+      @insertCheckItem pos, item.commandId, item.label
     when 'separator' then @insertSeparator pos
     when 'submenu' then @insertSubMenu pos, item.commandId, item.label, item.submenu
     when 'radio'
@@ -83,6 +94,9 @@ Menu::insert = (pos, item) ->
             v8Util.setHiddenValue otherItem, 'checked', false
           v8Util.setHiddenValue item, 'checked', true
 
+          # Update states when clicked on Windows.
+          @_updateStates() if process.platform is 'win32'
+
       @insertRadioItem pos, item.commandId, item.label, item.groupId
 
   @setSublabel pos, item.sublabel if item.sublabel?
index 47984c4c61f4dbc826d577d72144b8524a0fb3a0..8d25fcba8c87d7013f17cf34ab3134d8e1a4530b 100644 (file)
@@ -81,6 +81,7 @@ class NativeWindowWin : public NativeWindow,
   void SetMenu(ui::MenuModel* menu_model);
 
   views::Widget* window() const { return window_.get(); }
+  atom::Menu2* menu() const { return menu_.get(); }
   SkRegion* draggable_region() { return draggable_region_.get(); }
 
  protected: