]> khamidou — GitHub mirrors - electron.git/commitdiff
Add role property for MenuItem
authorCheng Zhao <zcbenz@gmail.com>
Tue, 1 Sep 2015 11:48:11 +0000 (19:48 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 2 Sep 2015 01:42:17 +0000 (09:42 +0800)
atom/browser/api/atom_api_menu.cc
atom/browser/api/atom_api_menu.h
atom/browser/api/lib/menu-item.coffee
atom/browser/api/lib/menu.coffee
atom/browser/default_app/main.js
atom/browser/ui/atom_menu_model.cc
atom/browser/ui/atom_menu_model.h
atom/browser/ui/cocoa/atom_menu_controller.h
atom/browser/ui/cocoa/atom_menu_controller.mm

index 356a4d4ce49444b154e08be2816b0b0a4a4f3ece..9bd724a9612e0364ee01a0f54210a2ade57e4c73 100644 (file)
@@ -107,6 +107,10 @@ void Menu::SetSublabel(int index, const base::string16& sublabel) {
   model_->SetSublabel(index, sublabel);
 }
 
+void Menu::SetRole(int index, const base::string16& role) {
+  model_->SetRole(index, role);
+}
+
 void Menu::Clear() {
   model_->Clear();
 }
@@ -154,6 +158,7 @@ void Menu::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
       .SetMethod("setIcon", &Menu::SetIcon)
       .SetMethod("setSublabel", &Menu::SetSublabel)
+      .SetMethod("setRole", &Menu::SetRole)
       .SetMethod("clear", &Menu::Clear)
       .SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
       .SetMethod("getItemCount", &Menu::GetItemCount)
index 33aafbc45d3b03be147a7278d105630c0a09bf78..0d93c0d46be6db6c6073bee6ac63292853963cef 100644 (file)
@@ -73,6 +73,7 @@ class Menu : public mate::Wrappable,
                        Menu* menu);
   void SetIcon(int index, const gfx::Image& image);
   void SetSublabel(int index, const base::string16& sublabel);
+  void SetRole(int index, const base::string16& role);
   void Clear();
   int GetIndexOfCommandId(int command_id);
   int GetItemCount() const;
index d8e896f8ffa1a6ee7c3230bba07d2d69572781ce..7663b32996d8184b5bb9cd346fe2a374dd4cc858 100644 (file)
@@ -9,12 +9,13 @@ class MenuItem
   constructor: (options) ->
     Menu = require 'menu'
 
-    {click, @selector, @type, @label, @sublabel, @accelerator, @icon, @enabled, @visible, @checked, @submenu} = options
+    {click, @selector, @type, @role, @label, @sublabel, @accelerator, @icon, @enabled, @visible, @checked, @submenu} = options
 
     @type = 'submenu' if not @type? and @submenu?
     throw new Error('Invalid submenu') if @type is 'submenu' and @submenu?.constructor isnt Menu
 
     @overrideReadOnlyProperty 'type', 'normal'
+    @overrideReadOnlyProperty 'role'
     @overrideReadOnlyProperty 'accelerator'
     @overrideReadOnlyProperty 'icon'
     @overrideReadOnlyProperty 'submenu'
index af70af740aafe1c98bdf04c9d2686d24fbfd7a13..35335ffcebe5057b7b88c1ac807076246e22cd9d 100644 (file)
@@ -115,6 +115,7 @@ Menu::insert = (pos, item) ->
 
   @setSublabel pos, item.sublabel if item.sublabel?
   @setIcon pos, item.icon if item.icon?
+  @setRole pos, item.role if item.role?
 
   # Make menu accessable to items.
   item.overrideReadOnlyProperty 'menu', this
index fd3a6b596b6bca89f278bb96d52878007b1cd16f..99c4ad03320bbed673bd1f0d5afda8f589fa3253 100644 (file)
@@ -44,13 +44,14 @@ app.once('ready', function() {
         submenu: [
           {
             label: 'About Electron',
-            selector: 'orderFrontStandardAboutPanel:'
+            role: 'about'
           },
           {
             type: 'separator'
           },
           {
             label: 'Services',
+            role: 'services',
             submenu: []
           },
           {
@@ -59,16 +60,16 @@ app.once('ready', function() {
           {
             label: 'Hide Electron',
             accelerator: 'Command+H',
-            selector: 'hide:'
+            role: 'hide'
           },
           {
             label: 'Hide Others',
             accelerator: 'Command+Shift+H',
-            selector: 'hideOtherApplications:'
+            role: 'hideothers:'
           },
           {
             label: 'Show All',
-            selector: 'unhideAllApplications:'
+            role: 'unhide:'
           },
           {
             type: 'separator'
@@ -86,12 +87,12 @@ app.once('ready', function() {
           {
             label: 'Undo',
             accelerator: 'Command+Z',
-            selector: 'undo:'
+            role: 'undo'
           },
           {
             label: 'Redo',
             accelerator: 'Shift+Command+Z',
-            selector: 'redo:'
+            role: 'redo'
           },
           {
             type: 'separator'
@@ -99,22 +100,22 @@ app.once('ready', function() {
           {
             label: 'Cut',
             accelerator: 'Command+X',
-            selector: 'cut:'
+            role: 'cut'
           },
           {
             label: 'Copy',
             accelerator: 'Command+C',
-            selector: 'copy:'
+            role: 'copy'
           },
           {
             label: 'Paste',
             accelerator: 'Command+V',
-            selector: 'paste:'
+            role: 'paste'
           },
           {
             label: 'Select All',
             accelerator: 'Command+A',
-            selector: 'selectAll:'
+            role: 'selectall'
           },
         ]
       },
@@ -152,28 +153,30 @@ app.once('ready', function() {
       },
       {
         label: 'Window',
+        role: 'window',
         submenu: [
           {
             label: 'Minimize',
             accelerator: 'Command+M',
-            selector: 'performMiniaturize:'
+            role: 'minimize'
           },
           {
             label: 'Close',
             accelerator: 'Command+W',
-            selector: 'performClose:'
+            role: 'close'
           },
           {
             type: 'separator'
           },
           {
             label: 'Bring All to Front',
-            selector: 'arrangeInFront:'
+            role: 'front'
           },
         ]
       },
       {
         label: 'Help',
+        role: 'help',
         submenu: [
           {
             label: 'Learn More',
index 7d2d5e1b6a1fd115e51194d213085276865d29a9..9add7a22715e9de81fa0dab952f5b89257dae9af 100644 (file)
@@ -4,6 +4,8 @@
 
 #include "atom/browser/ui/atom_menu_model.h"
 
+#include "base/stl_util.h"
+
 namespace atom {
 
 AtomMenuModel::AtomMenuModel(Delegate* delegate)
@@ -14,6 +16,17 @@ AtomMenuModel::AtomMenuModel(Delegate* delegate)
 AtomMenuModel::~AtomMenuModel() {
 }
 
+void AtomMenuModel::SetRole(int index, const base::string16& role) {
+  roles_[index] = role;
+}
+
+base::string16 AtomMenuModel::GetRoleAt(int index) {
+  if (ContainsKey(roles_, index))
+    return roles_[index];
+  else
+    return base::string16();
+}
+
 void AtomMenuModel::MenuClosed() {
   ui::SimpleMenuModel::MenuClosed();
   FOR_EACH_OBSERVER(Observer, observers_, MenuClosed());
index 42e0e5dbc53a52890a481c802b0236ad03fa0453..fe19a8dc2518cbf77331cb9470ce02cf47a7db96 100644 (file)
@@ -5,6 +5,8 @@
 #ifndef ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_
 #define ATOM_BROWSER_UI_ATOM_MENU_MODEL_H_
 
+#include <map>
+
 #include "base/observer_list.h"
 #include "ui/base/models/simple_menu_model.h"
 
@@ -31,12 +33,16 @@ class AtomMenuModel : public ui::SimpleMenuModel {
   void AddObserver(Observer* obs) { observers_.AddObserver(obs); }
   void RemoveObserver(Observer* obs) { observers_.RemoveObserver(obs); }
 
+  void SetRole(int index, const base::string16& role);
+  base::string16 GetRoleAt(int index);
+
   // ui::SimpleMenuModel:
   void MenuClosed() override;
 
  private:
   Delegate* delegate_;  // weak ref.
 
+  std::map<int, base::string16> roles_;
   ObserverList<Observer> observers_;
 
   DISALLOW_COPY_AND_ASSIGN(AtomMenuModel);
index da10e1b0ba668e8df36c6733652d1303094df98d..f8c48aa5dcb5b9d0013f1de464ef163e48ccffe6 100644 (file)
@@ -59,17 +59,4 @@ class MenuModel;
 
 @end
 
-// Exposed only for unit testing, do not call directly.
-@interface AtomMenuController (PrivateExposedForTesting)
-- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item;
-@end
-
-// Protected methods that subclassers can override.
-@interface AtomMenuController (Protected)
-- (void)addItemToMenu:(NSMenu*)menu
-              atIndex:(NSInteger)index
-            fromModel:(ui::MenuModel*)model;
-- (NSMenu*)menuFromModel:(ui::MenuModel*)model;
-@end
-
 #endif  // ATOM_BROWSER_UI_COCOA_ATOM_MENU_CONTROLLER_H_
index d799a9f9b64dd6c7f042d6d95072864d11af2683..e3aa78aa248c06f6b591728df6b666bf2614edfd 100644 (file)
@@ -8,16 +8,36 @@
 #include "atom/browser/ui/atom_menu_model.h"
 #include "base/logging.h"
 #include "base/strings/sys_string_conversions.h"
+#include "base/strings/utf_string_conversions.h"
 #include "ui/base/accelerators/accelerator.h"
 #include "ui/base/accelerators/platform_accelerator_cocoa.h"
 #include "ui/base/l10n/l10n_util_mac.h"
 #include "ui/events/cocoa/cocoa_event_utils.h"
 #include "ui/gfx/image/image.h"
 
-@interface AtomMenuController (Private)
-- (void)addSeparatorToMenu:(NSMenu*)menu
-                   atIndex:(int)index;
-@end
+namespace {
+
+struct Role {
+  SEL selector;
+  const char* role;
+};
+Role kRolesMap[] = {
+  { @selector(orderFrontStandardAboutPanel:), "about" },
+  { @selector(hide:), "hide" },
+  { @selector(hideOtherApplications:), "hideothers" },
+  { @selector(unhideAllApplications:), "unhide" },
+  { @selector(arrangeInFront:), "front" },
+  { @selector(undo:), "undo" },
+  { @selector(redo:), "redo" },
+  { @selector(cut:), "cut" },
+  { @selector(copy:), "copy" },
+  { @selector(paste:), "paste" },
+  { @selector(selectAll:), "selectall" },
+  { @selector(performMiniaturize:), "minimize" },
+  { @selector(performClose:), "close" },
+};
+
+}  // namespace
 
 @implementation AtomMenuController
 
 // associated with the entry in the model identified by |modelIndex|.
 - (void)addItemToMenu:(NSMenu*)menu
               atIndex:(NSInteger)index
-            fromModel:(ui::MenuModel*)model {
+            fromModel:(ui::MenuModel*)ui_model {
+  atom::AtomMenuModel* model = static_cast<atom::AtomMenuModel*>(ui_model);
+
   base::string16 label16 = model->GetLabelAt(index);
   NSString* label = l10n_util::FixUpWindowsStyleLabel(label16);
   base::scoped_nsobject<NSMenuItem> item(
     [submenu setTitle:[item title]];
     [item setSubmenu:submenu];
 
-    // Hack to set window and help menu.
-    if ([[item title] isEqualToString:@"Window"] && [submenu numberOfItems] > 0)
+    // Set submenu's role.
+    base::string16 role = model->GetRoleAt(index);
+    if (role == base::ASCIIToUTF16("window"))
       [NSApp setWindowsMenu:submenu];
-    else if ([[item title] isEqualToString:@"Help"])
+    else if (role == base::ASCIIToUTF16("help"))
       [NSApp setHelpMenu:submenu];
-    if ([[item title] isEqualToString:@"Services"] &&
-        [submenu numberOfItems] == 0)
+    if (role == base::ASCIIToUTF16("services"))
       [NSApp setServicesMenu:submenu];
   } else {
     // The MenuModel works on indexes so we can't just set the command id as the
     // model. Setting the target to |self| allows this class to participate
     // in validation of the menu items.
     [item setTag:index];
-    [item setTarget:self];
     NSValue* modelObject = [NSValue valueWithPointer:model];
     [item setRepresentedObject:modelObject];  // Retains |modelObject|.
     ui::Accelerator accelerator;
             platformAccelerator->modifier_mask()];
       }
     }
+
+    // Set menu item's role.
+    base::string16 role = model->GetRoleAt(index);
+    if (role.empty()) {
+      [item setTarget:self];
+    } else {
+      for (const Role& pair : kRolesMap) {
+        if (role == base::ASCIIToUTF16(pair.role)) {
+          [item setAction:pair.selector];
+          break;
+        }
+      }
+    }
   }
   [menu insertItem:item atIndex:index];
 }