diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-05 16:41:40 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-05 16:41:40 +0000 |
commit | 8026e219d127cf966081a033a4b54d2536678e0b (patch) | |
tree | 42ed84df866325a74293c46c0b4d4b645c37624b /chrome/browser | |
parent | 34dd163cc5ac76042d93937760ec4667e4ccb661 (diff) | |
download | chromium_src-8026e219d127cf966081a033a4b54d2536678e0b.zip chromium_src-8026e219d127cf966081a033a4b54d2536678e0b.tar.gz chromium_src-8026e219d127cf966081a033a4b54d2536678e0b.tar.bz2 |
Remove DelayedMenuButton by merging it into MenuButton.
XIB Changes:
* Switch the back/forward buttons to be MenuButtons instead of DelayedMenuButtons.
BUG=32094
TEST=Back/forward buttons and menu still work. Wrench menu still works.
Review URL: http://codereview.chromium.org/5995004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70512 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/ui/cocoa/back_forward_menu_controller.h | 8 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/back_forward_menu_controller.mm | 8 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/delayedmenu_button.h | 32 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/delayedmenu_button.mm | 137 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/delayedmenu_button_unittest.mm | 62 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/extensions/chevron_menu_button.mm | 8 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/extensions/extension_infobar_controller.mm | 1 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/menu_button.h | 26 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/menu_button.mm | 66 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/menu_button_unittest.mm | 90 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/toolbar_controller.h | 8 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/toolbar_controller.mm | 4 |
12 files changed, 175 insertions, 275 deletions
diff --git a/chrome/browser/ui/cocoa/back_forward_menu_controller.h b/chrome/browser/ui/cocoa/back_forward_menu_controller.h index 13bb585..7e58051 100644 --- a/chrome/browser/ui/cocoa/back_forward_menu_controller.h +++ b/chrome/browser/ui/cocoa/back_forward_menu_controller.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,7 +12,7 @@ #include "base/scoped_ptr.h" #include "chrome/browser/ui/toolbar/back_forward_menu_model.h" -@class DelayedMenuButton; +@class MenuButton; typedef BackForwardMenuModel::ModelType BackForwardMenuType; const BackForwardMenuType BACK_FORWARD_MENU_TYPE_BACK = @@ -26,7 +26,7 @@ const BackForwardMenuType BACK_FORWARD_MENU_TYPE_FORWARD = @interface BackForwardMenuController : NSObject { @private BackForwardMenuType type_; - DelayedMenuButton* button_; // Weak; comes from nib. + MenuButton* button_; // Weak; comes from nib. scoped_ptr<BackForwardMenuModel> model_; scoped_nsobject<NSMenu> backForwardMenu_; } @@ -36,7 +36,7 @@ const BackForwardMenuType BACK_FORWARD_MENU_TYPE_FORWARD = - (id)initWithBrowser:(Browser*)browser modelType:(BackForwardMenuType)type - button:(DelayedMenuButton*)button; + button:(MenuButton*)button; @end // @interface BackForwardMenuController diff --git a/chrome/browser/ui/cocoa/back_forward_menu_controller.mm b/chrome/browser/ui/cocoa/back_forward_menu_controller.mm index 9db04cf..96a9a18 100644 --- a/chrome/browser/ui/cocoa/back_forward_menu_controller.mm +++ b/chrome/browser/ui/cocoa/back_forward_menu_controller.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -7,8 +7,8 @@ #include "base/logging.h" #include "base/scoped_ptr.h" #include "base/sys_string_conversions.h" -#import "chrome/browser/ui/cocoa/delayedmenu_button.h" #import "chrome/browser/ui/cocoa/event_utils.h" +#import "chrome/browser/ui/cocoa/menu_button.h" #include "chrome/browser/ui/toolbar/back_forward_menu_model.h" #include "skia/ext/skia_utils_mac.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -26,7 +26,7 @@ using gfx::SkBitmapToNSImage; - (id)initWithBrowser:(Browser*)browser modelType:(BackForwardMenuType)type - button:(DelayedMenuButton*)button { + button:(MenuButton*)button { if ((self = [super init])) { type_ = type; button_ = button; @@ -37,7 +37,7 @@ using gfx::SkBitmapToNSImage; [backForwardMenu_ setDelegate:self]; [button_ setAttachedMenu:backForwardMenu_]; - [button_ setAttachedMenuEnabled:YES]; + [button_ setOpenMenuOnClick:NO]; } return self; } diff --git a/chrome/browser/ui/cocoa/delayedmenu_button.h b/chrome/browser/ui/cocoa/delayedmenu_button.h deleted file mode 100644 index 6363d30..0000000 --- a/chrome/browser/ui/cocoa/delayedmenu_button.h +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_UI_COCOA_DELAYEDMENU_BUTTON_H_ -#define CHROME_BROWSER_UI_COCOA_DELAYEDMENU_BUTTON_H_ -#pragma once - -#import <Cocoa/Cocoa.h> - -#include "base/scoped_nsobject.h" - -@interface DelayedMenuButton : NSButton { - NSMenu* attachedMenu_; // Strong (retained). - BOOL attachedMenuEnabled_; - scoped_nsobject<NSPopUpButtonCell> popUpCell_; -} - -// The menu to display. Note that it should have no (i.e., a blank) title and -// that the 0-th entry should be blank (and won't be displayed). (This is -// because we use a pulldown list, for which Cocoa uses the 0-th item as "title" -// in the button. This might change if we ever switch to a pop-up. Our direct -// use of the given NSMenu object means that the one can set and use NSMenu's -// delegate as usual.) -@property(retain, nonatomic) NSMenu* attachedMenu; - -// Is the menu enabled? (If not, don't act like a click-hold button.) -@property(assign, nonatomic) BOOL attachedMenuEnabled; - -@end // @interface DelayedMenuButton - -#endif // CHROME_BROWSER_UI_COCOA_DELAYEDMENU_BUTTON_H_ diff --git a/chrome/browser/ui/cocoa/delayedmenu_button.mm b/chrome/browser/ui/cocoa/delayedmenu_button.mm deleted file mode 100644 index 9a9d73d..0000000 --- a/chrome/browser/ui/cocoa/delayedmenu_button.mm +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#import "chrome/browser/ui/cocoa/delayedmenu_button.h" - -#include "base/logging.h" -#include "base/scoped_nsobject.h" -#import "chrome/browser/ui/cocoa/clickhold_button_cell.h" - -@interface DelayedMenuButton (Private) - -- (void)setupCell; -- (void)attachedMenuAction:(id)sender; - -@end // @interface DelayedMenuButton (Private) - -@implementation DelayedMenuButton - -// Overrides: - -+ (Class)cellClass { - return [ClickHoldButtonCell class]; -} - -- (id)init { - if ((self = [super init])) - [self setupCell]; - return self; -} - -- (id)initWithCoder:(NSCoder*)decoder { - if ((self = [super initWithCoder:decoder])) - [self setupCell]; - return self; -} - -- (id)initWithFrame:(NSRect)frameRect { - if ((self = [super initWithFrame:frameRect])) - [self setupCell]; - return self; -} - -- (void)dealloc { - [attachedMenu_ release]; - [super dealloc]; -} - -- (void)awakeFromNib { - [self setupCell]; -} - -- (void)setCell:(NSCell*)cell { - [super setCell:cell]; - [self setupCell]; -} - -// Accessors and mutators: - -@synthesize attachedMenu = attachedMenu_; - -// Don't synthesize for attachedMenuEnabled_; its mutator must do other things. -- (void)setAttachedMenuEnabled:(BOOL)enabled { - attachedMenuEnabled_ = enabled; - [[self cell] setEnableClickHold:attachedMenuEnabled_]; -} - -- (BOOL)attachedMenuEnabled { - return attachedMenuEnabled_; -} - -@end // @implementation DelayedMenuButton - -@implementation DelayedMenuButton (Private) - -// Set up the button's cell if we've reached a point where it's been set. -- (void)setupCell { - ClickHoldButtonCell* cell = [self cell]; - if (cell) { - DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); - [self setEnabled:NO]; // Make the controller put in a menu and - // enable it explicitly. This also takes - // care of |[cell setEnableClickHold:]|. - [cell setClickHoldAction:@selector(attachedMenuAction:)]; - [cell setClickHoldTarget:self]; - } -} - -// Display the menu. -- (void)attachedMenuAction:(id)sender { - // We shouldn't get here unless the menu is enabled. - DCHECK(attachedMenuEnabled_); - - // If we don't have a menu (in which case the person using this control is - // being bad), just wait for a mouse up. - if (!attachedMenu_) { - LOG(WARNING) << "No menu available."; - [NSApp nextEventMatchingMask:NSLeftMouseUpMask - untilDate:[NSDate distantFuture] - inMode:NSEventTrackingRunLoopMode - dequeue:YES]; - return; - } - - // TODO(viettrungluu): We have some fudge factors below to make things line up - // (approximately). I wish I knew how to get rid of them. (Note that our view - // is flipped, and that frame should be in our coordinates.) The y/height is - // very odd, since it doesn't seem to respond to changes the way that it - // should. I don't understand it. - NSRect frame = [self convertRect:[self frame] - fromView:[self superview]]; - frame.origin.x -= 2.0; - frame.size.height += 10.0; - - // Make our pop-up button cell and set things up. This is, as of 10.5, the - // official Apple-recommended hack. Later, perhaps |-[NSMenu - // popUpMenuPositioningItem:atLocation:inView:]| may be a better option. - // However, using a pulldown has the benefit that Cocoa automatically places - // the menu correctly even when we're at the edge of the screen (including - // "dragging upwards" when the button is close to the bottom of the screen). - // A |scoped_nsobject| local variable cannot be used here because - // Accessibility on 10.5 grabs the NSPopUpButtonCell without retaining it, and - // uses it later. (This is fixed in 10.6.) - if (!popUpCell_.get()) { - popUpCell_.reset([[NSPopUpButtonCell alloc] initTextCell:@"" - pullsDown:YES]); - } - DCHECK(popUpCell_.get()); - [popUpCell_ setMenu:attachedMenu_]; - [popUpCell_ selectItem:nil]; - [popUpCell_ attachPopUpWithFrame:frame - inView:self]; - [popUpCell_ performClickWithFrame:frame - inView:self]; -} - -@end // @implementation DelayedMenuButton (Private) diff --git a/chrome/browser/ui/cocoa/delayedmenu_button_unittest.mm b/chrome/browser/ui/cocoa/delayedmenu_button_unittest.mm deleted file mode 100644 index d1d7b77..0000000 --- a/chrome/browser/ui/cocoa/delayedmenu_button_unittest.mm +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#import <Cocoa/Cocoa.h> - -#include "base/scoped_nsobject.h" -#import "chrome/browser/ui/cocoa/clickhold_button_cell.h" -#import "chrome/browser/ui/cocoa/cocoa_test_helper.h" -#import "chrome/browser/ui/cocoa/delayedmenu_button.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "testing/platform_test.h" - -namespace { - -class DelayedMenuButtonTest : public CocoaTest { - public: - DelayedMenuButtonTest() { - NSRect frame = NSMakeRect(0, 0, 50, 30); - scoped_nsobject<DelayedMenuButton>button([[DelayedMenuButton alloc] - initWithFrame:frame]); - button_ = button.get(); - scoped_nsobject<ClickHoldButtonCell> cell( - [[ClickHoldButtonCell alloc] initTextCell:@"Testing"]); - [button_ setCell:cell.get()]; - [[test_window() contentView] addSubview:button_]; - } - - DelayedMenuButton* button_; -}; - -TEST_VIEW(DelayedMenuButtonTest, button_) - -// Test assigning and enabling a menu, again mostly to ensure nothing leaks or -// crashes. -TEST_F(DelayedMenuButtonTest, MenuAssign) { - scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]); - ASSERT_TRUE(menu.get()); - - [menu insertItemWithTitle:@"" action:nil keyEquivalent:@"" atIndex:0]; - [menu insertItemWithTitle:@"foo" action:nil keyEquivalent:@"" atIndex:1]; - [menu insertItemWithTitle:@"bar" action:nil keyEquivalent:@"" atIndex:2]; - [menu insertItemWithTitle:@"baz" action:nil keyEquivalent:@"" atIndex:3]; - - [button_ setAttachedMenu:menu]; - EXPECT_TRUE([button_ attachedMenu]); - - [button_ setAttachedMenuEnabled:YES]; - EXPECT_TRUE([button_ attachedMenuEnabled]); - - // TODO(viettrungluu): Display the menu. (Calling DelayedMenuButton's private - // |-attachedMenuAction:| method displays it fine, but the problem is - // getting rid of the menu. We can catch the - // |NSMenuDidBeginTrackingNotification| from |menu| fine, but then - // |-cancelTracking| doesn't dismiss it. I don't know why.) -} - -// TODO(viettrungluu): Test the two actions of the button (the normal one and -// displaying the menu, also making sure the latter drags correctly)? It would -// require "emulating" a mouse.... - -} // namespace diff --git a/chrome/browser/ui/cocoa/extensions/chevron_menu_button.mm b/chrome/browser/ui/cocoa/extensions/chevron_menu_button.mm index 04f1506..801bc46 100644 --- a/chrome/browser/ui/cocoa/extensions/chevron_menu_button.mm +++ b/chrome/browser/ui/cocoa/extensions/chevron_menu_button.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -12,4 +12,10 @@ return [ChevronMenuButtonCell class]; } +// Overrides: +- (void)configureCell { + [super configureCell]; + [self setOpenMenuOnClick:YES]; +} + @end diff --git a/chrome/browser/ui/cocoa/extensions/extension_infobar_controller.mm b/chrome/browser/ui/cocoa/extensions/extension_infobar_controller.mm index 1214370..833b1a7 100644 --- a/chrome/browser/ui/cocoa/extensions/extension_infobar_controller.mm +++ b/chrome/browser/ui/cocoa/extensions/extension_infobar_controller.mm @@ -138,6 +138,7 @@ class InfobarBridge : public ExtensionInfoBarDelegate::DelegateObserver, if ((self = [super initWithDelegate:delegate])) { window_ = window; dropdownButton_.reset([[MenuButton alloc] init]); + [dropdownButton_ setOpenMenuOnClick:YES]; ExtensionHost* extensionHost = delegate_->AsExtensionInfoBarDelegate()-> extension_host(); diff --git a/chrome/browser/ui/cocoa/menu_button.h b/chrome/browser/ui/cocoa/menu_button.h index d7b00f5..93b1116 100644 --- a/chrome/browser/ui/cocoa/menu_button.h +++ b/chrome/browser/ui/cocoa/menu_button.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -13,9 +13,20 @@ // This a button which displays a user-provided menu "attached" below it upon // being clicked or dragged (or clicked and held). It expects a // |ClickHoldButtonCell| as cell. +// +// There are two different behaviors of this button depending on the value of +// the |openMenuOnClick| property. If YES, the target-action mechanism will be +// handled internally to always show the menu when clicked. This behavior is +// used for the Wrench menu, for example. When the property is NO, the button +// can have a separate target-action but will open the menu when clicked and +// held. This is used for the toolbar back/forward buttons, which have a +// primary action and the menu as a secondary click-hold action. The default +// value is NO so that custom actions can be hooked up in Interface Builder. @interface MenuButton : NSButton { @private - IBOutlet NSMenu* attachedMenu_; + scoped_nsobject<NSMenu> attachedMenu_; + BOOL attachedMenuEnabled_; + BOOL openMenuOnClick_; scoped_nsobject<NSPopUpButtonCell> popUpCell_; } @@ -25,8 +36,17 @@ // in the button. This might change if we ever switch to a pop-up. Our direct // use of the given NSMenu object means that the one can set and use NSMenu's // delegate as usual.) -@property(assign, nonatomic) NSMenu* attachedMenu; +@property (retain, nonatomic) IBOutlet NSMenu* attachedMenu; + +// Whether or not to open the menu when the button is clicked. Otherwise, the +// menu will only be opened when clicked and held. +@property (assign, nonatomic) BOOL openMenuOnClick; @end // @interface MenuButton +// Available for subclasses. +@interface MenuButton (Protected) +- (void)configureCell; +@end + #endif // CHROME_BROWSER_UI_COCOA_MENU_BUTTON_H_ diff --git a/chrome/browser/ui/cocoa/menu_button.mm b/chrome/browser/ui/cocoa/menu_button.mm index d1b9e88..c66b8d2 100644 --- a/chrome/browser/ui/cocoa/menu_button.mm +++ b/chrome/browser/ui/cocoa/menu_button.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -9,16 +9,15 @@ #import "chrome/browser/ui/cocoa/clickhold_button_cell.h" @interface MenuButton (Private) - -- (void)resetToDefaults; - (void)showMenu:(BOOL)isDragging; - (void)clickShowMenu:(id)sender; - (void)dragShowMenu:(id)sender; - @end // @interface MenuButton (Private) @implementation MenuButton +@synthesize openMenuOnClick = openMenuOnClick_; + // Overrides: + (Class)cellClass { @@ -27,25 +26,57 @@ - (id)init { if ((self = [super init])) - [self resetToDefaults]; + [self configureCell]; return self; } - (id)initWithCoder:(NSCoder*)decoder { if ((self = [super initWithCoder:decoder])) - [self resetToDefaults]; + [self configureCell]; return self; } - (id)initWithFrame:(NSRect)frameRect { if ((self = [super initWithFrame:frameRect])) - [self resetToDefaults]; + [self configureCell]; return self; } +- (void)dealloc { + self.attachedMenu = nil; + [super dealloc]; +} + +- (void)awakeFromNib { + [self configureCell]; +} + +- (void)setCell:(NSCell*)cell { + [super setCell:cell]; + [self configureCell]; +} + // Accessors and mutators: -@synthesize attachedMenu = attachedMenu_; +- (NSMenu*)attachedMenu { + return attachedMenu_.get(); +} + +- (void)setAttachedMenu:(NSMenu*)menu { + attachedMenu_.reset([menu retain]); + [[self cell] setEnableClickHold:(menu != nil)]; +} + +- (void)setOpenMenuOnClick:(BOOL)enabled { + openMenuOnClick_ = enabled; + if (enabled) { + [[self cell] setClickHoldTimeout:0.0]; // Make menu trigger immediately. + [[self cell] setAction:@selector(clickShowMenu:)]; + [[self cell] setTarget:self]; + } else { + [[self cell] setClickHoldTimeout:0.25]; // Default value. + } +} @end // @implementation MenuButton @@ -53,15 +84,12 @@ // Reset various settings of the button and its associated |ClickHoldButtonCell| // to the standard state which provides reasonable defaults. -- (void)resetToDefaults { +- (void)configureCell { ClickHoldButtonCell* cell = [self cell]; DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); - [cell setEnableClickHold:YES]; - [cell setClickHoldTimeout:0.0]; // Make menu trigger immediately. - [cell setAction:@selector(clickShowMenu:)]; - [cell setTarget:self]; [cell setClickHoldAction:@selector(dragShowMenu:)]; [cell setClickHoldTarget:self]; + [cell setEnableClickHold:([self attachedMenu] != nil)]; } // Actually show the menu (in the correct location). |isDragging| indicates @@ -79,8 +107,11 @@ return; } - // TODO(viettrungluu): Remove silly fudge factors (same ones as in - // delayedmenu_button.mm). + // TODO(viettrungluu): We have some fudge factors below to make things line up + // (approximately). I wish I knew how to get rid of them. (Note that our view + // is flipped, and that frame should be in our coordinates.) The y/height is + // very odd, since it doesn't seem to respond to changes the way that it + // should. I don't understand it. NSRect frame = [self convertRect:[self frame] fromView:[self superview]]; frame.origin.x -= 2.0; @@ -111,11 +142,16 @@ // Called when the button is clicked and released. (Shouldn't happen with // timeout of 0, though there may be some strange pointing devices out there.) - (void)clickShowMenu:(id)sender { + // This should only be called if openMenuOnClick has been set (which hooks + // up this target-action). + DCHECK(openMenuOnClick_); [self showMenu:NO]; } // Called when the button is clicked and dragged/held. - (void)dragShowMenu:(id)sender { + // We shouldn't get here unless the menu is enabled. + DCHECK([self attachedMenu]); [self showMenu:YES]; } diff --git a/chrome/browser/ui/cocoa/menu_button_unittest.mm b/chrome/browser/ui/cocoa/menu_button_unittest.mm index ccfcb2c..8adceb4 100644 --- a/chrome/browser/ui/cocoa/menu_button_unittest.mm +++ b/chrome/browser/ui/cocoa/menu_button_unittest.mm @@ -1,12 +1,60 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/mac/cocoa_protocols.h" #include "base/scoped_nsobject.h" #import "chrome/browser/ui/cocoa/clickhold_button_cell.h" #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" #import "chrome/browser/ui/cocoa/menu_button.h" +@interface MenuButtonTestDelegate : NSObject<NSMenuDelegate> { + @private + scoped_nsobject<NSMenu> menu_; + BOOL open_; + BOOL didOpen_; +} +- (BOOL)isOpen; +- (BOOL)didOpen; +@end + +@implementation MenuButtonTestDelegate +- (id)initWithMenu:(NSMenu*)menu { + if ((self = [super init])) { + menu_.reset([menu retain]); + } + return self; +} + +- (BOOL)isOpen { + return open_; +} + +- (BOOL)didOpen { + return didOpen_; +} + +- (void)menuWillOpen:(NSMenu*)menu { + EXPECT_EQ(menu_.get(), menu); + EXPECT_FALSE(open_); + open_ = YES; + didOpen_ = YES; + NSArray* modes = [NSArray arrayWithObjects:NSEventTrackingRunLoopMode, + NSDefaultRunLoopMode, + nil]; + [menu performSelector:@selector(cancelTracking) + withObject:nil + afterDelay:1.5 + inModes:modes]; +} + +- (void)menuDidClose:(NSMenu*)menu { + EXPECT_EQ(menu_.get(), menu); + EXPECT_TRUE(open_); + open_ = NO; +} +@end + namespace { class MenuButtonTest : public CocoaTest { @@ -22,6 +70,15 @@ class MenuButtonTest : public CocoaTest { [[test_window() contentView] addSubview:button_]; } + NSMenu* CreateMenu() { + NSMenu* menu = [[NSMenu alloc] initWithTitle:@""]; + [menu insertItemWithTitle:@"" action:nil keyEquivalent:@"" atIndex:0]; + [menu insertItemWithTitle:@"foo" action:nil keyEquivalent:@"" atIndex:1]; + [menu insertItemWithTitle:@"bar" action:nil keyEquivalent:@"" atIndex:2]; + [menu insertItemWithTitle:@"baz" action:nil keyEquivalent:@"" atIndex:3]; + return menu; + } + MenuButton* button_; }; @@ -29,22 +86,33 @@ TEST_VIEW(MenuButtonTest, button_); // Test assigning a menu, again mostly to ensure nothing leaks or crashes. TEST_F(MenuButtonTest, MenuAssign) { - scoped_nsobject<NSMenu> menu([[NSMenu alloc] initWithTitle:@""]); + scoped_nsobject<NSMenu> menu(CreateMenu()); ASSERT_TRUE(menu.get()); - [menu insertItemWithTitle:@"" action:nil keyEquivalent:@"" atIndex:0]; - [menu insertItemWithTitle:@"foo" action:nil keyEquivalent:@"" atIndex:1]; - [menu insertItemWithTitle:@"bar" action:nil keyEquivalent:@"" atIndex:2]; - [menu insertItemWithTitle:@"baz" action:nil keyEquivalent:@"" atIndex:3]; - [button_ setAttachedMenu:menu]; EXPECT_TRUE([button_ attachedMenu]); +} + +TEST_F(MenuButtonTest, OpenOnClick) { + scoped_nsobject<NSMenu> menu(CreateMenu()); + ASSERT_TRUE(menu.get()); + + scoped_nsobject<MenuButtonTestDelegate> delegate( + [[MenuButtonTestDelegate alloc] initWithMenu:menu.get()]); + ASSERT_TRUE(delegate.get()); + + [menu setDelegate:delegate.get()]; + [button_ setAttachedMenu:menu]; + [button_ setOpenMenuOnClick:YES]; + + EXPECT_FALSE([delegate isOpen]); + EXPECT_FALSE([delegate didOpen]); - // TODO(viettrungluu): Display the menu. (The tough part is closing the menu, - // not opening it!) + // Should open the menu. + [button_ performClick:nil]; - // Since |button_| doesn't retain menu, we should probably unset it here. - [button_ setAttachedMenu:nil]; + EXPECT_TRUE([delegate didOpen]); + EXPECT_FALSE([delegate isOpen]); } } // namespace diff --git a/chrome/browser/ui/cocoa/toolbar_controller.h b/chrome/browser/ui/cocoa/toolbar_controller.h index 029f89d..4a815e6 100644 --- a/chrome/browser/ui/cocoa/toolbar_controller.h +++ b/chrome/browser/ui/cocoa/toolbar_controller.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -11,7 +11,6 @@ #include "base/scoped_ptr.h" #include "base/scoped_nsobject.h" #import "chrome/browser/ui/cocoa/command_observer_bridge.h" -#import "chrome/browser/ui/cocoa/delayedmenu_button.h" #import "chrome/browser/ui/cocoa/url_drop_target.h" #import "chrome/browser/ui/cocoa/view_resizer.h" #include "chrome/browser/prefs/pref_member.h" @@ -23,7 +22,6 @@ class Browser; @class BrowserActionsController; class CommandUpdater; -@class DelayedMenuButton; class LocationBar; class LocationBarViewMac; @class MenuButton; @@ -49,8 +47,8 @@ class WrenchMenuModel; // The ordering is important for unit tests. If new items are added or the // ordering is changed, make sure to update |-toolbarViews| and the // corresponding enum in the unit tests. - IBOutlet DelayedMenuButton* backButton_; - IBOutlet DelayedMenuButton* forwardButton_; + IBOutlet MenuButton* backButton_; + IBOutlet MenuButton* forwardButton_; IBOutlet ReloadButton* reloadButton_; IBOutlet NSButton* homeButton_; IBOutlet MenuButton* wrenchButton_; diff --git a/chrome/browser/ui/cocoa/toolbar_controller.mm b/chrome/browser/ui/cocoa/toolbar_controller.mm index 22d3dde..3147738 100644 --- a/chrome/browser/ui/cocoa/toolbar_controller.mm +++ b/chrome/browser/ui/cocoa/toolbar_controller.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -246,6 +246,8 @@ class NotificationBridge : public NotificationObserver { app::mac::GetCachedImageWithName(kWrenchButtonImageName)]; [self badgeWrenchMenuIfNeeded]; + [wrenchButton_ setOpenMenuOnClick:YES]; + [backButton_ setShowsBorderOnlyWhileMouseInside:YES]; [forwardButton_ setShowsBorderOnlyWhileMouseInside:YES]; [reloadButton_ setShowsBorderOnlyWhileMouseInside:YES]; |