Chromium Code Reviews| Index: chrome/browser/tab_contents/render_view_context_menu_mac.mm |
| =================================================================== |
| --- chrome/browser/tab_contents/render_view_context_menu_mac.mm (revision 97777) |
| +++ chrome/browser/tab_contents/render_view_context_menu_mac.mm (working copy) |
| @@ -13,6 +13,35 @@ |
| #import "chrome/browser/ui/cocoa/menu_controller.h" |
| #include "grit/generated_resources.h" |
| +namespace { |
| + |
| +// Retrieves an NSMenuItem which has the specified command_id. This function |
| +// traverses the given |model| in the depth-first order. When this function |
| +// finds an item whose command_id is the same as the given |command_id|, it |
| +// returns the NSMenuItem associated with the item. This function emulates |
| +// views::MenuItemViews::GetMenuItemByID() for Mac. |
| +NSMenuItem* GetMenuItemByID(ui::MenuModel* model, |
| + NSMenu* menu, |
| + int command_id) { |
| + for (int i = 0; i < model->GetItemCount(); ++i) { |
| + NSMenuItem* item = [menu itemAtIndex:i]; |
| + if (model->GetCommandIdAt(i) == command_id) |
| + return item; |
| + |
| + ui::MenuModel* submenu = model->GetSubmenuModelAt(i); |
| + if (submenu && [item hasSubmenu]) { |
| + NSMenuItem* subitem = GetMenuItemByID(submenu, |
| + [item submenu], |
| + command_id); |
| + if (subitem) |
| + return subitem; |
| + } |
| + } |
| + return nil; |
| +} |
| + |
| +} // namespace |
| + |
| // Obj-C bridge class that is the target of all items in the context menu. |
| // Relies on the tag being set to the command id. |
| @@ -103,3 +132,17 @@ |
| if (ok) |
| NSPerformService(@"Look Up in Dictionary", pboard); |
| } |
| + |
| +void RenderViewContextMenuMac::UpdateMenuItem(int command_id, |
| + bool enabled, |
| + const string16& title) { |
| + NSMenuItem* item = GetMenuItemByID(&menu_model_, [menuController_ menu], |
| + command_id); |
| + if (!item) |
| + return; |
| + |
| + // Update the returned NSMenuItem directly so we can update it immediately. |
| + [item setEnabled:enabled ? YES : NO]; |
|
Avi (use Gerrit)
2011/08/23 18:38:19
It's safe to just pass in enabled here; no need to
Hironori Bono
2011/08/25 11:18:58
Done. Thank you for noticing it. I misunderstood o
|
| + [item setTitle:SysUTF16ToNSString(title)]; |
| + [[item menu] itemChanged:item]; |
| +} |