Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Unified Diff: chrome/browser/cocoa/tab_controller.mm

Issue 500030: Factor tab context menu into a shared model and fix mac and win to use it. Im... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/cocoa/tab_controller.mm
===================================================================
--- chrome/browser/cocoa/tab_controller.mm (revision 34596)
+++ chrome/browser/cocoa/tab_controller.mm (working copy)
@@ -4,6 +4,7 @@
#include "app/l10n_util_mac.h"
#include "base/mac_util.h"
+#import "chrome/browser/cocoa/menu_controller.h"
#import "chrome/browser/cocoa/tab_controller.h"
#import "chrome/browser/cocoa/tab_controller_target.h"
#import "chrome/browser/cocoa/tab_view.h"
@@ -17,6 +18,53 @@
@synthesize target = target_;
@synthesize action = action_;
+namespace TabControllerInternal {
+
+// A C++ delegate that handles enabling/disabling menu items and handling when
+// a menu command is chosen. Also fixes up the menu item label for "pin/unpin
+// tab".
+class MenuDelegate : public menus::SimpleMenuModel::Delegate {
+ public:
+ explicit MenuDelegate(id<TabControllerTarget> target, TabController* owner)
+ : target_(target), owner_(owner) { }
+
+ // Overridden from menus::SimpleMenuModel::Delegate
+ virtual bool IsCommandIdChecked(int command_id) const { return false; }
+ virtual bool IsCommandIdEnabled(int command_id) const {
+ TabStripModel::ContextMenuCommand command =
+ static_cast<TabStripModel::ContextMenuCommand>(command_id);
+ return [target_ isCommandEnabled:command forController:owner_];
+ }
+ virtual bool GetAcceleratorForCommandId(
+ int command_id,
+ menus::Accelerator* accelerator) { return false; }
+ virtual void ExecuteCommand(int command_id) {
+ TabStripModel::ContextMenuCommand command =
+ static_cast<TabStripModel::ContextMenuCommand>(command_id);
+ [target_ commandDispatch:command forController:owner_];
+ }
+
+ virtual bool IsLabelForCommandIdDynamic(int command_id) const {
+ return command_id == TabStripModel::CommandTogglePinned;
+ }
+ virtual string16 GetLabelForCommandId(int command_id) const {
+ // Display "Pin Tab" when the tab is not pinned and "Unpin Tab" when it is
+ // (this is not a checkmark menu item, per Apple's HIG).
+ if (command_id == TabStripModel::CommandTogglePinned) {
+ return l10n_util::GetStringUTF16(
+ [owner_ pinned] ? IDS_TAB_CXMENU_UNPIN_TAB_MAC
+ : IDS_TAB_CXMENU_PIN_TAB_MAC);
+ }
+ return string16();
+ }
+
+ private:
+ id<TabControllerTarget> target_; // weak
+ TabController* owner_; // weak, owns me
+};
+
+} // namespace
+
// The min widths match the windows values and are sums of left + right
// padding, of which we have no comparable constants (we draw using paths, not
// images). The selected tab width includes the close button width.
@@ -79,6 +127,19 @@
[self internalSetSelected:selected_];
}
+// Called when Cocoa wants to display the context menu. Lazily instantiate
+// the menu based off of the cross-platform model. Re-create the menu and
+// model every time to get the correct labels and enabling.
+- (NSMenu*)menu {
+ contextMenuDelegate_.reset(
+ new TabControllerInternal::MenuDelegate(target_, self));
+ contextMenuModel_.reset(new TabMenuModel(contextMenuDelegate_.get()));
+ contextMenuController_.reset(
+ [[MenuController alloc] initWithModel:contextMenuModel_.get()
+ useWithPopUpButtonCell:NO]);
+ return [contextMenuController_ menu];
+}
+
- (IBAction)closeTab:(id)sender {
if ([[self target] respondsToSelector:@selector(closeTab:)]) {
[[self target] performSelector:@selector(closeTab:)
@@ -86,22 +147,6 @@
}
}
-// Dispatches the command in the tag to the registered target object.
-- (IBAction)commandDispatch:(id)sender {
- TabStripModel::ContextMenuCommand command =
- static_cast<TabStripModel::ContextMenuCommand>([sender tag]);
- [[self target] commandDispatch:command forController:self];
-}
-
-// Called for each menu item on its target, which would be this controller.
-// Returns YES if the menu item should be enabled. We ask the tab's
-// target for the proper answer.
-- (BOOL)validateMenuItem:(NSMenuItem*)item {
- TabStripModel::ContextMenuCommand command =
- static_cast<TabStripModel::ContextMenuCommand>([item tag]);
- return [[self target] isCommandEnabled:command forController:self];
-}
-
- (void)setTitle:(NSString*)title {
[[self view] setToolTip:title];
[super setTitle:title];
@@ -259,16 +304,4 @@
return NO;
}
-// Delegate method for context menu. Called before the menu is displayed to
-// update the menu. We need to display "Pin Tab" when the tab is not pinned and
-// "Unpin Tab" when it is (this is not a checkmark menu item, per Apple's HIG).
-- (void)menuNeedsUpdate:(NSMenu*)menu {
- NSMenuItem* togglePinned =
- [menu itemWithTag:TabStripModel::CommandTogglePinned];
- NSString* menuItemText = l10n_util::GetNSStringWithFixup(
- [self pinned] ? IDS_TAB_CXMENU_UNPIN_TAB_MAC
- : IDS_TAB_CXMENU_PIN_TAB_MAC);
- [togglePinned setTitle:menuItemText];
-}
-
@end

Powered by Google App Engine
This is Rietveld 408576698