summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/cocoa')
-rw-r--r--chrome/browser/cocoa/bookmark_bar_bridge.h5
-rw-r--r--chrome/browser/cocoa/bookmark_bar_bridge.mm3
-rw-r--r--chrome/browser/cocoa/bookmark_bar_bridge_unittest.mm12
-rw-r--r--chrome/browser/cocoa/bookmark_bar_controller.h54
-rw-r--r--chrome/browser/cocoa/bookmark_bar_controller.mm123
-rw-r--r--chrome/browser/cocoa/bookmark_bar_controller_unittest.mm105
-rw-r--r--chrome/browser/cocoa/bookmark_bar_toolbar_view.h25
-rw-r--r--chrome/browser/cocoa/bookmark_bar_toolbar_view.mm21
-rw-r--r--chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm70
-rw-r--r--chrome/browser/cocoa/browser_window_cocoa.mm2
-rw-r--r--chrome/browser/cocoa/browser_window_controller.h4
-rw-r--r--chrome/browser/cocoa/browser_window_controller.mm80
-rw-r--r--chrome/browser/cocoa/browser_window_controller_unittest.mm106
-rw-r--r--chrome/browser/cocoa/toolbar_compressable.h19
-rw-r--r--chrome/browser/cocoa/toolbar_controller.h16
-rw-r--r--chrome/browser/cocoa/toolbar_controller.mm58
-rw-r--r--chrome/browser/cocoa/toolbar_controller_unittest.mm46
17 files changed, 558 insertions, 191 deletions
diff --git a/chrome/browser/cocoa/bookmark_bar_bridge.h b/chrome/browser/cocoa/bookmark_bar_bridge.h
index 269bbad..9c09e4a 100644
--- a/chrome/browser/cocoa/bookmark_bar_bridge.h
+++ b/chrome/browser/cocoa/bookmark_bar_bridge.h
@@ -19,7 +19,8 @@ class Browser;
class BookmarkBarBridge : public BookmarkModelObserver {
public:
- BookmarkBarBridge(BookmarkBarController* controller, BookmarkModel* model);
+ BookmarkBarBridge(BookmarkBarController* controller,
+ BookmarkModel* model);
virtual ~BookmarkBarBridge();
// Overridden from BookmarkModelObserver
@@ -46,7 +47,7 @@ class BookmarkBarBridge : public BookmarkModelObserver {
private:
BookmarkBarController* controller_; // weak; owns me
- BookmarkModel *model_; // weak; it is owned by a Profile.
+ BookmarkModel* model_; // weak; it is owned by a Profile.
DISALLOW_COPY_AND_ASSIGN(BookmarkBarBridge);
};
diff --git a/chrome/browser/cocoa/bookmark_bar_bridge.mm b/chrome/browser/cocoa/bookmark_bar_bridge.mm
index 5fd6fe9..e7ace5b 100644
--- a/chrome/browser/cocoa/bookmark_bar_bridge.mm
+++ b/chrome/browser/cocoa/bookmark_bar_bridge.mm
@@ -9,7 +9,8 @@
BookmarkBarBridge::BookmarkBarBridge(BookmarkBarController* controller,
BookmarkModel* model)
- : controller_(controller), model_(model) {
+ : controller_(controller),
+ model_(model) {
model_->AddObserver(this);
// Bookmark loading is async; it may may not have happened yet.
diff --git a/chrome/browser/cocoa/bookmark_bar_bridge_unittest.mm b/chrome/browser/cocoa/bookmark_bar_bridge_unittest.mm
index 38a872d..627775a 100644
--- a/chrome/browser/cocoa/bookmark_bar_bridge_unittest.mm
+++ b/chrome/browser/cocoa/bookmark_bar_bridge_unittest.mm
@@ -33,9 +33,10 @@ typedef std::pair<GURL,WindowOpenDisposition> OpenInfo;
@implementation FakeBookmarkBarController
-- (id)initWithProfile:(Profile*)profile {
- if ((self = [super initWithProfile:profile
+- (id)initWithBrowser:(Browser*)browser {
+ if ((self = [super initWithBrowser:browser
initialWidth:100 // arbitrary
+ compressDelegate:nil
resizeDelegate:nil
urlDelegate:self])) {
callbacks_.reset([[NSMutableArray alloc] init]);
@@ -99,8 +100,9 @@ class BookmarkBarBridgeTest : public PlatformTest {
// Call all the callbacks; make sure they are all redirected to the objc object.
TEST_F(BookmarkBarBridgeTest, TestRedirect) {
- Profile *profile = browser_test_helper_.profile();
- BookmarkModel *model = profile->GetBookmarkModel();
+ Browser* browser = browser_test_helper_.browser();
+ Profile* profile = browser_test_helper_.profile();
+ BookmarkModel* model = profile->GetBookmarkModel();
scoped_nsobject<NSView> parentView([[NSView alloc]
initWithFrame:NSMakeRect(0,0,100,100)]);
@@ -110,7 +112,7 @@ TEST_F(BookmarkBarBridgeTest, TestRedirect) {
[[NSView alloc] initWithFrame:NSMakeRect(0,0,100,100)]);
scoped_nsobject<FakeBookmarkBarController>
- controller([[FakeBookmarkBarController alloc] initWithProfile:profile]);
+ controller([[FakeBookmarkBarController alloc] initWithBrowser:browser]);
EXPECT_TRUE(controller.get());
scoped_ptr<BookmarkBarBridge> bridge(new BookmarkBarBridge(controller.get(),
model));
diff --git a/chrome/browser/cocoa/bookmark_bar_controller.h b/chrome/browser/cocoa/bookmark_bar_controller.h
index 1efcc92..87b13a3 100644
--- a/chrome/browser/cocoa/bookmark_bar_controller.h
+++ b/chrome/browser/cocoa/bookmark_bar_controller.h
@@ -11,16 +11,21 @@
#include "base/scoped_nsobject.h"
#include "base/scoped_ptr.h"
#include "chrome/browser/cocoa/bookmark_bar_bridge.h"
+#include "chrome/browser/cocoa/tab_strip_model_observer_bridge.h"
#include "webkit/glue/window_open_disposition.h"
+@class BackgroundGradientView;
@class BookmarkBarStateController;
class BookmarkModel;
class BookmarkNode;
@class BookmarkBarView;
+class Browser;
+@protocol ToolbarCompressable;
class GURL;
@class MenuButton;
class Profile;
class PrefService;
+@class ToolbarController;
@protocol ViewResizer;
// The interface for an object which can open URLs for a bookmark.
@@ -29,12 +34,19 @@ class PrefService;
disposition:(WindowOpenDisposition)disposition;
@end
+// An interface to allow mocking of a BookmarkBarController by the
+// BookmarkBarToolbarView.
+@protocol BookmarkBarFloating
+// Returns true if the bookmark bar should be drawn as if it's a disconnected
+// bookmark bar on the New Tag Page.
+- (BOOL)drawAsFloatingBar;
+@end
// A controller for the bookmark bar in the browser window. Handles showing
// and hiding based on the preference in the given profile.
-@interface BookmarkBarController : NSViewController {
+@interface BookmarkBarController : NSViewController<BookmarkBarFloating> {
@private
- Profile* profile_; // weak
+ Browser* browser_; // weak; owned by its window
BookmarkModel* bookmarkModel_; // weak; part of the profile owned by the
// top-level Browser object.
@@ -55,19 +67,23 @@ class PrefService;
// Set when using fullscreen mode.
BOOL barIsEnabled_;
- // Set to YES when the user elects to always show the bookmark bar.
- BOOL barShouldBeShown_;
-
// Bridge from Chrome-style C++ notifications (e.g. derived from
// BookmarkModelObserver)
scoped_ptr<BookmarkBarBridge> bridge_;
+ // Delegate that is alerted about whether it should be compressed because
+ // it's right next to us.
+ id<ToolbarCompressable> compressDelegate_; // weak
+
// Delegate that can resize us.
id<ViewResizer> resizeDelegate_; // weak
// Delegate that can open URLs for us.
id<BookmarkURLOpener> urlDelegate_; // weak
+ // Lets us get TabSelectedAt notifications.
+ scoped_ptr<TabStripModelObserverBridge> tabObserver_;
+
IBOutlet BookmarkBarView* buttonView_;
IBOutlet MenuButton* offTheSideButton_;
IBOutlet NSMenu* buttonContextMenu_;
@@ -75,26 +91,42 @@ class PrefService;
// Initializes the bookmark bar controller with the given browser
// profile and delegates.
-- (id)initWithProfile:(Profile*)profile
+- (id)initWithBrowser:(Browser*)browser
initialWidth:(float)initialWidth
+ compressDelegate:(id<ToolbarCompressable>)compressDelegate
resizeDelegate:(id<ViewResizer>)resizeDelegate
urlDelegate:(id<BookmarkURLOpener>)urlDelegate;
+// Returns the backdrop to the bookmark bar.
+- (BackgroundGradientView*)backgroundGradientView;
+
// Tell the bar to show itself if needed (e.g. if the kShowBookmarkBar
// is set). Called once after the controller is first created.
- (void)showIfNeeded;
-// Returns whether or not the bookmark bar is visible.
-- (BOOL)isBookmarkBarVisible;
-
-// Toggle the state of the bookmark bar.
-- (void)toggleBookmarkBar;
+// Update the visible state of the bookmark bar based on the current value of
+// -[BookmarkBarController isAlwaysVisible].
+- (void)updateVisibility;
// Turn on or off the bookmark bar and prevent or reallow its
// appearance. On disable, toggle off if shown. On enable, show only
// if needed. For fullscreen mode.
- (void)setBookmarkBarEnabled:(BOOL)enabled;
+// Returns YES if the bookmarks bar is currently visible, either because the
+// user has asked for it to always be visible, or because the current tab is the
+// New Tab page.
+- (BOOL)isVisible;
+
+// Returns true if the bookmark bar needs to be shown currently because a tab
+// that requires it is selected. The bookmark bar will have a different
+// appearance when it is shown if isAlwaysVisible returns NO.
+- (BOOL)isNewTabPage;
+
+// Returns true if the bookmark bar is visible for all tabs. (This corresponds
+// to the user having selected "Always show the bookmark bar")
+- (BOOL)isAlwaysVisible;
+
// Actions for manipulating bookmarks.
// From a button, ...
- (IBAction)openBookmark:(id)sender;
diff --git a/chrome/browser/cocoa/bookmark_bar_controller.mm b/chrome/browser/cocoa/bookmark_bar_controller.mm
index 9de155a..9b3ef1e 100644
--- a/chrome/browser/cocoa/bookmark_bar_controller.mm
+++ b/chrome/browser/cocoa/bookmark_bar_controller.mm
@@ -19,8 +19,10 @@
#import "chrome/browser/cocoa/bookmark_menu_cocoa_controller.h"
#import "chrome/browser/cocoa/event_utils.h"
#import "chrome/browser/cocoa/menu_button.h"
+#import "chrome/browser/cocoa/toolbar_controller.h"
#import "chrome/browser/cocoa/view_resizer.h"
#include "chrome/browser/profile.h"
+#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "grit/generated_resources.h"
@@ -45,13 +47,17 @@
- (void)addFolderNode:(const BookmarkNode*)node toMenu:(NSMenu*)menu;
- (void)tagEmptyMenu:(NSMenu*)menu;
- (void)clearMenuTagMap;
+- (int)preferredHeight;
@end
namespace {
-// Our height, when opened.
+// Our height, when opened in "always visible" mode.
const int kBookmarkBarHeight = 28;
+// Our height, when visible in "new tab page" mode.
+const int kNTPBookmarkBarHeight = 40;
+
// Magic numbers from Cole
const CGFloat kDefaultBookmarkWidth = 150.0;
const CGFloat kBookmarkVerticalPadding = 2.0;
@@ -61,18 +67,22 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
@implementation BookmarkBarController
-- (id)initWithProfile:(Profile*)profile
+- (id)initWithBrowser:(Browser*)browser
initialWidth:(float)initialWidth
+ compressDelegate:(id<ToolbarCompressable>)compressDelegate
resizeDelegate:(id<ViewResizer>)resizeDelegate
urlDelegate:(id<BookmarkURLOpener>)urlDelegate {
if ((self = [super initWithNibName:@"BookmarkBar"
bundle:mac_util::MainAppBundle()])) {
- profile_ = profile;
+ browser_ = browser;
initialWidth_ = initialWidth;
- bookmarkModel_ = profile->GetBookmarkModel();
+ bookmarkModel_ = browser_->profile()->GetBookmarkModel();
buttons_.reset([[NSMutableArray alloc] init]);
+ compressDelegate_ = compressDelegate;
resizeDelegate_ = resizeDelegate;
urlDelegate_ = urlDelegate;
+ tabObserver_.reset(
+ new TabStripModelObserverBridge(browser_->tabstrip_model(), self));
}
return self;
}
@@ -110,8 +120,13 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
DCHECK([offTheSideButton_ attachedMenu]);
}
+// Method is the same as [self view], but is provided to be explicit.
+- (BackgroundGradientView*)backgroundGradientView {
+ return (BackgroundGradientView*)[self view];
+}
+
- (void)showIfNeeded {
- if (profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar))
+ if (browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar))
[self showBookmarkBar:YES immediately:YES];
}
@@ -140,50 +155,48 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
[self checkHideOffTheSideButton];
}
-// Show or hide the bar based on the value of |show|. Handles
-// animating the resize of the content view. if |immediately| is YES,
-// make changes immediately instead of using an animator. If the bar
-// is disabled, do absolutely nothing. The routine which enables the
-// bar will show it if relevant using other mechanisms (the pref) to
-// determine desired state.
+// Show or hide the bar based on the value of |show|. Handles animating the
+// resize of the content view. if |immediately| is YES, make changes
+// immediately instead of using an animator. The routine which enables the bar
+// will show it if relevant using other mechanisms (the pref) to determine
+// desired state.
- (void)showBookmarkBar:(BOOL)show immediately:(BOOL)immediately {
- if (barIsEnabled_ && (barShouldBeShown_ != show)) {
- if ([self view]) {
- [[self view] setHidden:show ? NO : YES];
- [resizeDelegate_ resizeView:[self view]
- newHeight:(show ? kBookmarkBarHeight : 0)];
- }
- barShouldBeShown_ = show;
- if (show) {
- [self loaded:bookmarkModel_];
- }
- }
-}
+ BOOL compressed = [self isAlwaysVisible];
+ [compressDelegate_ setShouldBeCompressed:compressed];
-- (BOOL)isBookmarkBarVisible {
- return barShouldBeShown_;
+ CGFloat height = show ? [self preferredHeight] : 0;
+ [resizeDelegate_ resizeView:[self view] newHeight:height];
+ [[self view] setHidden:show ? NO : YES];
}
// We don't change a preference; we only change visibility.
// Preference changing (global state) is handled in
-// BrowserWindowCocoa::ToggleBookmarkBar().
-- (void)toggleBookmarkBar {
- [self showBookmarkBar:!barShouldBeShown_ immediately:YES];
+// BrowserWindowCocoa::ToggleBookmarkBar(). We simply update the visibility of
+// the bar based on the current value of the pref.
+- (void)updateVisibility {
+ [self showBookmarkBar:[self isVisible] immediately:YES];
}
- (void)setBookmarkBarEnabled:(BOOL)enabled {
- if (enabled) {
- // Enabling the bar; set enabled then show if needed.
- barIsEnabled_ = YES;
- if (profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar))
- [self showBookmarkBar:YES immediately:YES];
- } else {
- // Disabling the bar; hide if visible.
- if ([self isBookmarkBarVisible]) {
- [self showBookmarkBar:NO immediately:YES];
- }
- barIsEnabled_ = NO;
- }
+ barIsEnabled_ = enabled ? YES : NO;
+ [self updateVisibility];
+}
+
+- (BOOL)isVisible {
+ return ([self isAlwaysVisible] && barIsEnabled_) || [self isNewTabPage];
+}
+
+- (BOOL)isNewTabPage {
+ return browser_ && browser_->GetSelectedTabContents() &&
+ browser_->GetSelectedTabContents()->ShouldShowBookmarkBar();
+}
+
+- (BOOL)isAlwaysVisible {
+ return browser_ && browser_->profile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
+}
+
+- (BOOL)drawAsFloatingBar {
+ return ![self isAlwaysVisible] && [self isNewTabPage];
}
// Return nil if menuItem has no delegate.
@@ -228,6 +241,27 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
menuTagMap_.clear();
}
+- (int)preferredHeight {
+ return [self isAlwaysVisible] ? kBookmarkBarHeight : kNTPBookmarkBarHeight;
+}
+
+- (void)selectTabWithContents:(TabContents*)newContents
+ previousContents:(TabContents*)oldContents
+ atIndex:(NSInteger)index
+ userGesture:(bool)wasUserGesture {
+ // We need selectTabWithContents: for when we change from a tab that is the
+ // new tab page to a tab that isn't.
+ [self updateVisibility];
+}
+
+- (void)tabChangedWithContents:(TabContents*)contents
+ atIndex:(NSInteger)index
+ loadingOnly:(BOOL)loading {
+ // We need tabChangedWithContents: for when the user clicks a bookmark from
+ // the bookmark bar on the new tab page.
+ [self updateVisibility];
+}
+
// Recursively add the given bookmark node and all its children to
// menu, one menu item per node.
- (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu {
@@ -382,7 +416,7 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
// for the other platforms but can't find a way to trigger it in the
// UI.
BookmarkEditor::Show([[[self view] window] contentView],
- profile_,
+ browser_->profile(),
node->GetParent(),
node,
BookmarkEditor::SHOW_TREE,
@@ -433,7 +467,7 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
if (!parent)
parent = bookmarkModel_->GetBookmarkBarNode();
BookmarkEditor::Show([[[self view] window] contentView],
- profile_,
+ browser_->profile(),
parent,
nil,
BookmarkEditor::SHOW_TREE,
@@ -447,7 +481,7 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
BookmarkNameFolderController* controller =
[[BookmarkNameFolderController alloc]
initWithParentWindow:[[self view] window]
- profile:profile_
+ profile:browser_->profile()
node:node];
[controller runAsModalSheet];
@@ -610,8 +644,7 @@ const CGFloat kBookmarkHorizontalPadding = 1.0;
// TODO(jrg): for now this is brute force.
- (void)loaded:(BookmarkModel*)model {
DCHECK(model == bookmarkModel_);
- // Do nothing if not active or too early
- if ((barShouldBeShown_ == NO) || !model->IsLoaded())
+ if (!model->IsLoaded())
return;
// Else brute force nuke and build.
const BookmarkNode* node = model->GetBookmarkBarNode();
diff --git a/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm b/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm
index bc72e65..5da3d0a 100644
--- a/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm
+++ b/chrome/browser/cocoa/bookmark_bar_controller_unittest.mm
@@ -10,8 +10,10 @@
#import "chrome/browser/cocoa/bookmark_bar_view.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
+#import "chrome/browser/cocoa/toolbar_compressable.h"
#include "chrome/browser/cocoa/test_event_utils.h"
#import "chrome/browser/cocoa/view_resizer_pong.h"
+#include "chrome/common/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -76,6 +78,33 @@
@end
+// A BookmarkBarController that always beleives that it's on the new tab page.
+@interface AlwaysNewTabPageBookmarkBarController : BookmarkBarController {
+}
+@end
+
+@implementation AlwaysNewTabPageBookmarkBarController
+-(BOOL)isNewTabPage {
+ return YES;
+}
+@end
+
+@interface CompressablePong : NSObject<ToolbarCompressable> {
+@private
+ BOOL compressed_;
+}
+@property (readonly) BOOL compressed;
+@end
+
+@implementation CompressablePong
+
+@synthesize compressed = compressed_;
+
+- (void)setShouldBeCompressed:(BOOL)compressed {
+ compressed_ = compressed;
+}
+
+@end
namespace {
@@ -86,12 +115,14 @@ class BookmarkBarControllerTest : public PlatformTest {
public:
BookmarkBarControllerTest() {
resizeDelegate_.reset([[ViewResizerPong alloc] init]);
+ compressDelegate_.reset([[CompressablePong alloc] init]);
NSRect parent_frame = NSMakeRect(0, 0, 800, 50);
parent_view_.reset([[NSView alloc] initWithFrame:parent_frame]);
[parent_view_ setHidden:YES];
bar_.reset(
- [[BookmarkBarController alloc] initWithProfile:helper_.profile()
+ [[BookmarkBarController alloc] initWithBrowser:helper_.browser()
initialWidth:NSWidth(parent_frame)
+ compressDelegate:compressDelegate_.get()
resizeDelegate:resizeDelegate_.get()
urlDelegate:nil]);
@@ -118,10 +149,9 @@ class BookmarkBarControllerTest : public PlatformTest {
[[[bar view] superview] setFrame:frame];
// make sure it's open so certain things aren't no-ops
- [bar toggleBookmarkBar];
+ helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
}
-
// Return a menu item that points to the right URL.
NSMenuItem* ItemForBookmarkBarMenu(GURL& gurl) {
node_.reset(new BookmarkNode(gurl));
@@ -140,6 +170,7 @@ class BookmarkBarControllerTest : public PlatformTest {
scoped_nsobject<NSView> parent_view_;
BrowserTestHelper helper_;
scoped_nsobject<ViewResizerPong> resizeDelegate_;
+ scoped_nsobject<CompressablePong> compressDelegate_;
scoped_nsobject<BookmarkBarController> bar_;
scoped_nsobject<NSMenu> menu_;
scoped_nsobject<NSMenuItem> menu_item_;
@@ -147,45 +178,77 @@ class BookmarkBarControllerTest : public PlatformTest {
scoped_ptr<BookmarkNode> node_;
};
-TEST_F(BookmarkBarControllerTest, ShowHide) {
- // The test class opens the bar by default since many actions are
- // no-ops with it closed. Set back to closed as a baseline.
- if ([bar_ isBookmarkBarVisible])
- [bar_ toggleBookmarkBar];
-
- // Start hidden.
- EXPECT_FALSE([bar_ isBookmarkBarVisible]);
- EXPECT_TRUE([[bar_ view] isHidden]);
+TEST_F(BookmarkBarControllerTest, ShowWhenShowBookmarkBarTrue) {
+ helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
+ [bar_ updateVisibility];
- // Show and hide it by toggling.
- [bar_ toggleBookmarkBar];
- EXPECT_TRUE([bar_ isBookmarkBarVisible]);
+ EXPECT_TRUE([bar_ isVisible]);
EXPECT_FALSE([[bar_ view] isHidden]);
EXPECT_GT([resizeDelegate_ height], 0);
EXPECT_GT([[bar_ view] frame].size.height, 0);
+}
+
+TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarFalse) {
+ helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false);
+ [bar_ updateVisibility];
- [bar_ toggleBookmarkBar];
- EXPECT_FALSE([bar_ isBookmarkBarVisible]);
+ EXPECT_FALSE([bar_ isVisible]);
EXPECT_TRUE([[bar_ view] isHidden]);
EXPECT_EQ(0, [resizeDelegate_ height]);
EXPECT_EQ(0, [[bar_ view] frame].size.height);
}
+TEST_F(BookmarkBarControllerTest, HideWhenShowBookmarkBarTrueButDisabled) {
+ helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
+ [bar_ setBookmarkBarEnabled:NO];
+ [bar_ updateVisibility];
+
+ EXPECT_FALSE([bar_ isVisible]);
+ EXPECT_TRUE([[bar_ view] isHidden]);
+ EXPECT_EQ(0, [resizeDelegate_ height]);
+ EXPECT_EQ(0, [[bar_ view] frame].size.height);
+}
+
+TEST_F(BookmarkBarControllerTest, ShowOnNewTabPage) {
+ helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, false);
+
+ scoped_nsobject<BookmarkBarControllerTogglePong> bar;
+ bar.reset(
+ [[AlwaysNewTabPageBookmarkBarController alloc]
+ initWithBrowser:helper_.browser()
+ initialWidth:100 // arbitrary
+ compressDelegate:compressDelegate_.get()
+ resizeDelegate:resizeDelegate_.get()
+ urlDelegate:nil]);
+ InstallAndToggleBar(bar.get());
+
+ [bar setBookmarkBarEnabled:NO];
+ [bar updateVisibility];
+
+ EXPECT_TRUE([bar isVisible]);
+ EXPECT_FALSE([[bar view] isHidden]);
+ EXPECT_GT([resizeDelegate_ height], 0);
+ EXPECT_GT([[bar view] frame].size.height, 0);
+}
+
// Make sure we're watching for frame change notifications.
TEST_F(BookmarkBarControllerTest, FrameChangeNotification) {
scoped_nsobject<BookmarkBarControllerTogglePong> bar;
bar.reset(
[[BookmarkBarControllerTogglePong alloc]
- initWithProfile:helper_.profile()
+ initWithBrowser:helper_.browser()
initialWidth:100 // arbitrary
+ compressDelegate:compressDelegate_.get()
resizeDelegate:resizeDelegate_.get()
urlDelegate:nil]);
InstallAndToggleBar(bar.get());
- EXPECT_GT([bar toggles], 0);
+ // Send a frame did change notification for the pong's view.
+ [[NSNotificationCenter defaultCenter]
+ postNotificationName:NSViewFrameDidChangeNotification
+ object:[bar view]];
- // Hard to force toggles -- simple whacking the frame is inadequate.
- // TODO(jrg): find a way to set frame, force a toggle, and verify it.
+ EXPECT_GT([bar toggles], 0);
}
// Confirm off the side button only enabled when reasonable.
diff --git a/chrome/browser/cocoa/bookmark_bar_toolbar_view.h b/chrome/browser/cocoa/bookmark_bar_toolbar_view.h
new file mode 100644
index 0000000..2808cc5
--- /dev/null
+++ b/chrome/browser/cocoa/bookmark_bar_toolbar_view.h
@@ -0,0 +1,25 @@
+// 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.
+
+// The BookmarkBarToolbarView is responsible for drawing the background of the
+// BookmarkBar's toolbar in either of its two display modes - permanently
+// attached (slimline with a stroke at the bottom edge) or New Tab Page style
+// (padded with a round rect border and the New Tab Page theme behind).
+
+#ifndef CHROME_BROWSER_COCOA_BOOKMARK_BAR_TOOLBAR_VIEW_H_
+#define CHROME_BROWSER_COCOA_BOOKMARK_BAR_TOOLBAR_VIEW_H_
+
+#import <Cocoa/Cocoa.h>
+
+#import "chrome/browser/cocoa/background_gradient_view.h"
+
+@protocol BookmarkBarFloating;
+
+@interface BookmarkBarToolbarView : BackgroundGradientView {
+ @private
+ IBOutlet id<BookmarkBarFloating> controller_;
+}
+@end
+
+#endif // CHROME_BROWSER_COCOA_BOOKMARK_BAR_TOOLBAR_VIEW_H_
diff --git a/chrome/browser/cocoa/bookmark_bar_toolbar_view.mm b/chrome/browser/cocoa/bookmark_bar_toolbar_view.mm
new file mode 100644
index 0000000..e40607b
--- /dev/null
+++ b/chrome/browser/cocoa/bookmark_bar_toolbar_view.mm
@@ -0,0 +1,21 @@
+// 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/cocoa/bookmark_bar_toolbar_view.h"
+
+#import "chrome/browser/cocoa/bookmark_bar_controller.h"
+
+@implementation BookmarkBarToolbarView
+
+- (void)drawRect:(NSRect)rect {
+ if ([controller_ drawAsFloatingBar]) {
+ // TODO(erg): Deal with themes. http://crbug.com/17625
+ [[NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0] set];
+ NSRectFill(rect);
+ } else {
+ [super drawRect:rect];
+ }
+}
+
+@end // @implementation BookmarkBarToolbarView
diff --git a/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm b/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm
new file mode 100644
index 0000000..8a2d6ef
--- /dev/null
+++ b/chrome/browser/cocoa/bookmark_bar_toolbar_view_unittest.mm
@@ -0,0 +1,70 @@
+// 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/cocoa/bookmark_bar_controller.h"
+#import "chrome/browser/cocoa/bookmark_bar_toolbar_view.h"
+#import "chrome/browser/cocoa/cocoa_test_helper.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+// Allows us to inject our fake controller below.
+@interface BookmarkBarToolbarView (TestingAPI)
+-(void)setController:(id<BookmarkBarFloating>)controller;
+@end
+
+@implementation BookmarkBarToolbarView (TestingAPI)
+-(void)setController:(id<BookmarkBarFloating>)controller {
+ controller_ = controller;
+}
+@end
+
+// Allows us to control which way the view is rendered.
+@interface DrawFloatingFakeController :
+ NSObject<BookmarkBarFloating> {
+ BOOL drawAsFloating_;
+}
+@property(assign) BOOL drawAsFloatingBar;
+@end
+
+@implementation DrawFloatingFakeController
+@synthesize drawAsFloatingBar = drawAsFloating_;
+@end
+
+class BookmarkBarToolbarViewTest : public PlatformTest {
+ public:
+ BookmarkBarToolbarViewTest() {
+ controller_.reset([[DrawFloatingFakeController alloc] init]);
+ NSRect frame = NSMakeRect(0, 0, 100, 30);
+ view_.reset([[BookmarkBarToolbarView alloc] initWithFrame:frame]);
+ [cocoa_helper_.contentView() addSubview:view_.get()];
+ [view_.get() setController:controller_.get()];
+ }
+
+ CocoaTestHelper cocoa_helper_; // Inits Cocoa, creates window, etc...
+ scoped_nsobject<DrawFloatingFakeController> controller_;
+ scoped_nsobject<BookmarkBarToolbarView> view_;
+};
+
+// Test adding/removing from the view hierarchy, mostly to ensure nothing
+// leaks or crashes.
+TEST_F(BookmarkBarToolbarViewTest, AddRemove) {
+ EXPECT_EQ(cocoa_helper_.contentView(), [view_ superview]);
+ [view_.get() removeFromSuperview];
+ EXPECT_FALSE([view_ superview]);
+}
+
+// Test drawing (part 1), mostly to ensure nothing leaks or crashes.
+TEST_F(BookmarkBarToolbarViewTest, DisplayAsNormalBar) {
+ [controller_.get() setDrawAsFloatingBar:NO];
+ [view_ display];
+}
+
+// Test drawing (part 2), mostly to ensure nothing leaks or crashes.
+TEST_F(BookmarkBarToolbarViewTest, DisplayAsDisconnectedBar) {
+ [controller_.get() setDrawAsFloatingBar:YES];
+ [view_ display];
+}
diff --git a/chrome/browser/cocoa/browser_window_cocoa.mm b/chrome/browser/cocoa/browser_window_cocoa.mm
index 3afae7e..33809ad 100644
--- a/chrome/browser/cocoa/browser_window_cocoa.mm
+++ b/chrome/browser/cocoa/browser_window_cocoa.mm
@@ -333,7 +333,7 @@ void BrowserWindowCocoa::Observe(NotificationType type,
// Only the key window gets a direct toggle from the menu.
// Other windows hear about it from the notification.
case NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED:
- [controller_ toggleBookmarkBar];
+ [controller_ updateBookmarkBarVisibility];
break;
default:
NOTREACHED(); // we don't ask for anything else!
diff --git a/chrome/browser/cocoa/browser_window_controller.h b/chrome/browser/cocoa/browser_window_controller.h
index b4700b4..76223de 100644
--- a/chrome/browser/cocoa/browser_window_controller.h
+++ b/chrome/browser/cocoa/browser_window_controller.h
@@ -67,6 +67,7 @@ class TabStripModelObserverBridge;
scoped_ptr<StatusBubble> statusBubble_;
scoped_nsobject<DownloadShelfController> downloadShelfController_;
scoped_nsobject<ExtensionShelfController> extensionShelfController_;
+ scoped_nsobject<BookmarkBarController> bookmarkBarController_;
scoped_nsobject<BookmarkBubbleController> bookmarkBubbleController_;
scoped_nsobject<GTMTheme> theme_;
BOOL ownsBrowser_; // Only ever NO when testing
@@ -113,7 +114,8 @@ class TabStripModelObserverBridge;
- (BOOL)isBookmarkBarVisible;
-- (void)toggleBookmarkBar;
+// Called after the visibility perf changed.
+- (void)updateBookmarkBarVisibility;
- (BOOL)isDownloadShelfVisible;
diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm
index 43ffa11..542d5d2 100644
--- a/chrome/browser/cocoa/browser_window_controller.mm
+++ b/chrome/browser/cocoa/browser_window_controller.mm
@@ -181,8 +181,7 @@ willPositionSheet:(NSWindow*)sheet
commands:browser->command_updater()
profile:browser->profile()
browser:browser
- resizeDelegate:self
- bookmarkDelegate:self]);
+ resizeDelegate:self]);
// If we are a pop-up, we have a titlebar and no toolbar.
if (!browser_->SupportsWindowFeature(Browser::FEATURE_TOOLBAR) &&
browser_->SupportsWindowFeature(Browser::FEATURE_TITLEBAR)) {
@@ -190,6 +189,26 @@ willPositionSheet:(NSWindow*)sheet
}
[[[self window] contentView] addSubview:[toolbarController_ view]];
+ // Create a sub-controller for the bookmark bar.
+ bookmarkBarController_.reset(
+ [[BookmarkBarController alloc]
+ initWithBrowser:browser_.get()
+ initialWidth:NSWidth([[[self window] contentView] frame])
+ compressDelegate:toolbarController_.get()
+ resizeDelegate:self
+ urlDelegate:self]);
+
+ // Add bookmark bar to the view hierarchy. This also triggers the
+ // nib load. The bookmark bar is defined (in the nib) to be
+ // bottom-aligned to it's parent view (among other things), so
+ // position and resize properties don't need to be set.
+ [[[self window] contentView] addSubview:[bookmarkBarController_ view]];
+
+ // We don't want to try and show the bar before it gets placed in
+ // it's parent view, so this step shoudn't be inside the bookmark
+ // bar controller's awakeFromNib.
+ [bookmarkBarController_ showIfNeeded];
+
if (browser_->SupportsWindowFeature(Browser::FEATURE_EXTENSIONSHELF)) {
// Create the extension shelf.
extensionShelfController_.reset([[ExtensionShelfController alloc]
@@ -457,12 +476,14 @@ willPositionSheet:(NSWindow*)sheet
// relayout.
- (void)resizeView:(NSView*)view newHeight:(float)height {
// We should only ever be called for one of the following four views.
- // |downloadShelfController_| may be nil.
+ // |downloadShelfController_| may be nil. If we are asked to size the bookmark
+ // bar directly, its superview must be this controller's content view.
DCHECK(view);
DCHECK(view == [toolbarController_ view] ||
view == [infoBarContainerController_ view] ||
+ view == [extensionShelfController_ view] ||
view == [downloadShelfController_ view] ||
- view == [extensionShelfController_ view]);
+ view == [bookmarkBarController_ view]);
// Change the height of the view and call layoutViews. We set the height here
// without regard to where the view is on the screen or whether it needs to
@@ -816,12 +837,11 @@ willPositionSheet:(NSWindow*)sheet
}
- (BOOL)isBookmarkBarVisible {
- return [[toolbarController_ bookmarkBarController] isBookmarkBarVisible];
+ return [bookmarkBarController_ isVisible];
}
-- (void)toggleBookmarkBar {
- BookmarkBarController* bar = [toolbarController_ bookmarkBarController];
- [bar toggleBookmarkBar];
+- (void)updateBookmarkBarVisibility {
+ [bookmarkBarController_ updateVisibility];
}
- (BOOL)isDownloadShelfVisible {
@@ -858,8 +878,7 @@ willPositionSheet:(NSWindow*)sheet
if (fullscreen) {
// Disable showing of the bookmark bar. This does not toggle the
// preference.
- // TODO(jrg): Is this still necessary?
- [[toolbarController_ bookmarkBarController] setBookmarkBarEnabled:NO];
+ [bookmarkBarController_ setBookmarkBarEnabled:NO];
// Make room for more content area.
[[toolbarController_ view] removeFromSuperview];
// Hide the menubar, and allow it to un-hide when moving the mouse
@@ -869,8 +888,7 @@ willPositionSheet:(NSWindow*)sheet
} else {
SetSystemUIMode(kUIModeNormal, 0);
[[[self window] contentView] addSubview:[toolbarController_ view]];
- // TODO(jrg): Is this still necessary?
- [[toolbarController_ bookmarkBarController] setBookmarkBarEnabled:YES];
+ [bookmarkBarController_ setBookmarkBarEnabled:YES];
}
// Force a relayout.
@@ -973,6 +991,7 @@ willPositionSheet:(NSWindow*)sheet
// Update all the UI bits.
windowShim_->UpdateTitleBar();
+
#if 0
// TODO(pinkerton):Update as more things become window-specific
toolbar_->SetProfile(newContents->profile());
@@ -1224,6 +1243,16 @@ willPositionSheet:(NSWindow*)sheet
}
[toolbarView setFrame:toolbarFrame];
+ if ([bookmarkBarController_ isAlwaysVisible]) {
+ NSView* bookmarkBarView = [bookmarkBarController_ view];
+ [bookmarkBarView setHidden:NO];
+ NSRect bookmarkBarFrame = [bookmarkBarView frame];
+ bookmarkBarFrame.origin.y = maxY - NSHeight(bookmarkBarFrame);
+ bookmarkBarFrame.size.width = NSWidth(contentFrame);
+ [bookmarkBarView setFrame:bookmarkBarFrame];
+ maxY -= NSHeight(bookmarkBarFrame);
+ }
+
// Place the infobar container in place below the toolbar.
NSView* infoBarView = [infoBarContainerController_ view];
NSRect infoBarFrame = [infoBarView frame];
@@ -1232,6 +1261,23 @@ willPositionSheet:(NSWindow*)sheet
[infoBarView setFrame:infoBarFrame];
maxY -= NSHeight(infoBarFrame);
+ if (![bookmarkBarController_ isAlwaysVisible] &&
+ [bookmarkBarController_ isVisible]) {
+ NSView* bookmarkBarView = [bookmarkBarController_ view];
+ [bookmarkBarView setHidden:NO];
+ NSRect bookmarkBarFrame = [bookmarkBarView frame];
+ bookmarkBarFrame.origin.y = maxY - NSHeight(bookmarkBarFrame);
+ bookmarkBarFrame.size.width = NSWidth(contentFrame);
+ [bookmarkBarView setFrame:bookmarkBarFrame];
+ maxY -= NSHeight(bookmarkBarFrame);
+ }
+
+ if (![bookmarkBarController_ isVisible]) {
+ // If the bookmark bar is not visible in either mode, we need to hide it
+ // otherwise it'll render over other elements.
+ [[bookmarkBarController_ view] setHidden:YES];
+ }
+
// Place the extension shelf at the bottom of the view, if it exists.
if (extensionShelfController_.get()) {
NSView* extensionView = [extensionShelfController_ view];
@@ -1265,6 +1311,16 @@ willPositionSheet:(NSWindow*)sheet
positionFindBarView:[infoBarContainerController_ view]];
verticalOffsetForStatusBubble_ = minY;
+
+ // The bottom of the visible toolbar stack is the one that shows the
+ // divider stroke. If the bookmark bar is visible and not in new tab page
+ // mode, it is the bottom visible toolbar and so it must, otherwise the
+ // main toolbar is.
+ BOOL bookmarkToolbarShowsDivider = [bookmarkBarController_ isAlwaysVisible];
+ [[toolbarController_ backgroundGradientView]
+ setShowsDivider:!bookmarkToolbarShowsDivider];
+ [[bookmarkBarController_ backgroundGradientView]
+ setShowsDivider:bookmarkToolbarShowsDivider];
}
@end
diff --git a/chrome/browser/cocoa/browser_window_controller_unittest.mm b/chrome/browser/cocoa/browser_window_controller_unittest.mm
index f5b4d3e..5df2fdd 100644
--- a/chrome/browser/cocoa/browser_window_controller_unittest.mm
+++ b/chrome/browser/cocoa/browser_window_controller_unittest.mm
@@ -25,6 +25,8 @@
// Implementations are below.
- (NSView*)infoBarContainerView;
- (NSView*)toolbarView;
+- (NSView*)bookmarkView;
+- (BOOL)bookmarkBarVisible;
- (NSView*)extensionShelfView;
@end
@@ -37,6 +39,14 @@
return [toolbarController_ view];
}
+- (NSView*)bookmarkView {
+ return [bookmarkBarController_ view];
+}
+
+- (BOOL)bookmarkBarVisible {
+ return [bookmarkBarController_ isVisible];
+}
+
- (NSView*)extensionShelfView {
return [extensionShelfController_ view];
}
@@ -146,7 +156,13 @@ TEST_F(BrowserWindowControllerTest, TestTheme) {
TEST_F(BrowserWindowControllerTest, BookmarkBarControllerIndirection) {
EXPECT_FALSE([controller_ isBookmarkBarVisible]);
- [controller_ toggleBookmarkBar];
+
+ // Explicitly show the bar. Can't use bookmark_utils::ToggleWhenVisible()
+ // because of the notification issues.
+ browser_helper_.profile()->GetPrefs()->
+ SetBoolean(prefs::kShowBookmarkBar, true);
+
+ [controller_ updateBookmarkBarVisibility];
EXPECT_TRUE([controller_ isBookmarkBarVisible]);
}
@@ -186,7 +202,16 @@ void CheckViewPositions(BrowserWindowController* controller) {
EXPECT_EQ(NSMaxY(extension), NSMinY(download));
EXPECT_EQ(NSMaxY(download), NSMinY(contentArea));
EXPECT_EQ(NSMaxY(contentArea), NSMinY(infobar));
- EXPECT_EQ(NSMaxY(infobar), NSMinY(toolbar));
+
+ // Bookmark bar frame is random memory when hidden.
+ if ([controller bookmarkBarVisible]) {
+ NSRect bookmark = [[controller bookmarkView] frame];
+ EXPECT_EQ(NSMaxY(infobar), NSMinY(bookmark));
+ EXPECT_EQ(NSMaxY(bookmark), NSMinY(toolbar));
+ } else {
+ EXPECT_EQ(NSMaxY(infobar), NSMinY(toolbar));
+ }
+
EXPECT_EQ(NSMaxY(contentView), NSMaxY(toolbar));
}
} // end namespace
@@ -240,6 +265,83 @@ TEST_F(BrowserWindowControllerTest, TestResizeViews) {
CheckViewPositions(controller_);
}
+TEST_F(BrowserWindowControllerTest, TestResizeViewsWithBookmarkBar) {
+ // Force a display of the bookmark bar.
+ browser_helper_.profile()->GetPrefs()->
+ SetBoolean(prefs::kShowBookmarkBar, true);
+
+ TabStripView* tabstrip = [controller_ tabStripView];
+ NSView* contentView = [[tabstrip window] contentView];
+ NSView* toolbar = [controller_ toolbarView];
+ NSView* bookmark = [controller_ bookmarkView];
+ NSView* infobar = [controller_ infoBarContainerView];
+ NSView* extensionShelf = [controller_ extensionShelfView];
+
+ // We need to muck with the views a bit to put us in a consistent state before
+ // we start resizing. In particular, we need to move the tab strip to be
+ // immediately above the content area, since we layout views to be directly
+ // under the tab strip. We also explicitly set the contentView's frame to be
+ // 800x600.
+ [contentView setFrame:NSMakeRect(0, 0, 800, 600)];
+ NSRect tabstripFrame = [tabstrip frame];
+ tabstripFrame.origin.y = NSMaxY([contentView frame]);
+ [tabstrip setFrame:tabstripFrame];
+
+ // The download shelf is created lazily. Force-create it and set its initial
+ // height to 0.
+ NSView* download = [[controller_ downloadShelf] view];
+ [controller_ resizeView:download newHeight:0];
+
+ // Force a layout and check each view's frame.
+ [controller_ layoutSubviews];
+ CheckViewPositions(controller_);
+
+ // Add the bookmark bar and recheck.
+ [controller_ resizeView:bookmark newHeight:40];
+ CheckViewPositions(controller_);
+
+ // Add an extension shelf and recheck.
+ [controller_ resizeView:extensionShelf newHeight:40];
+ CheckViewPositions(controller_);
+
+ // Expand the infobar to 60px and recheck
+ [controller_ resizeView:infobar newHeight:60];
+ CheckViewPositions(controller_);
+
+ // Expand the toolbar to 64px and recheck
+ [controller_ resizeView:toolbar newHeight:64];
+ CheckViewPositions(controller_);
+
+ // Add a 30px download shelf and recheck
+ [controller_ resizeView:download newHeight:30];
+ CheckViewPositions(controller_);
+
+ // Remove the bookmark bar and recheck
+ browser_helper_.profile()->GetPrefs()->
+ SetBoolean(prefs::kShowBookmarkBar, false);
+ [controller_ resizeView:bookmark newHeight:0];
+ CheckViewPositions(controller_);
+
+ // Shrink the infobar to 0px and toolbar to 39px and recheck
+ [controller_ resizeView:infobar newHeight:0];
+ [controller_ resizeView:toolbar newHeight:39];
+ CheckViewPositions(controller_);
+}
+
+// Make sure, by default, the bookmark bar and the toolbar are the same width.
+TEST_F(BrowserWindowControllerTest, BookmarkBarIsSameWidth) {
+ // Set the pref to the bookmark bar is visible when the toolbar is
+ // first created.
+ browser_helper_.profile()->GetPrefs()->SetBoolean(
+ prefs::kShowBookmarkBar, true);
+
+ // Make sure the bookmark bar is the same width as the toolbar
+ NSView* bookmarkBarView = [controller_ bookmarkView];
+ NSView* toolbarView = [controller_ toolbarView];
+ EXPECT_EQ([toolbarView frame].size.width,
+ [bookmarkBarView frame].size.width);
+}
+
TEST_F(BrowserWindowControllerTest, TestTopLeftForBubble) {
NSPoint p = [controller_ topLeftForBubble];
NSRect all = [[controller_ window] frame];
diff --git a/chrome/browser/cocoa/toolbar_compressable.h b/chrome/browser/cocoa/toolbar_compressable.h
new file mode 100644
index 0000000..ddd21c4
--- /dev/null
+++ b/chrome/browser/cocoa/toolbar_compressable.h
@@ -0,0 +1,19 @@
+// 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_COCOA_TOOLBAR_COMPRESSABLE_H_
+#define CHROME_BROWSER_COCOA_TOOLBAR_COMPRESSABLE_H_
+
+#include "chrome/browser/tabs/tab_strip_model.h"
+
+#import <Cocoa/Cocoa.h>
+
+// Defines a protocol that allows one view to tell another view that it should
+// remove pixels from the bottom of the view. Only ToolbarController implements
+// this, but it's a protocol for unit testing reasons.
+@protocol ToolbarCompressable
+- (void)setShouldBeCompressed:(BOOL)compressed;
+@end
+
+#endif // CHROME_BROWSER_COCOA_TOOLBAR_COMPRESSABLE_H_
diff --git a/chrome/browser/cocoa/toolbar_controller.h b/chrome/browser/cocoa/toolbar_controller.h
index 71467433..6af4241 100644
--- a/chrome/browser/cocoa/toolbar_controller.h
+++ b/chrome/browser/cocoa/toolbar_controller.h
@@ -10,7 +10,7 @@
#include "base/scoped_ptr.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/command_observer_bridge.h"
-#import "chrome/browser/cocoa/bookmark_bar_controller.h"
+#import "chrome/browser/cocoa/toolbar_compressable.h"
#import "chrome/browser/cocoa/delayedmenu_button.h"
#import "chrome/browser/cocoa/view_resizer.h"
#include "chrome/common/pref_member.h"
@@ -18,6 +18,7 @@
@class AutocompleteTextField;
@class AutocompleteTextFieldEditor;
@class BackForwardMenuController;
+@class BackgroundGradientView;
class Browser;
class BubblePositioner;
class CommandUpdater;
@@ -39,7 +40,7 @@ class ToolbarView;
// the web content view.
@interface ToolbarController :
- NSViewController<CommandObserverProtocol, ViewResizer> {
+ NSViewController<CommandObserverProtocol, ToolbarCompressable> {
@private
ToolbarModel* toolbarModel_; // weak, one per window
CommandUpdater* commands_; // weak, one per window
@@ -48,9 +49,7 @@ class ToolbarView;
scoped_ptr<CommandObserverBridge> commandObserver_;
scoped_ptr<LocationBarViewMac> locationBarView_;
scoped_nsobject<AutocompleteTextFieldEditor> autocompleteTextFieldEditor_;
- scoped_nsobject<BookmarkBarController> bookmarkBarController_;
id<ViewResizer> resizeDelegate_; // weak
- id<BookmarkURLOpener> bookmarkBarDelegate_; // weak
scoped_nsobject<BackForwardMenuController> backMenuController_;
scoped_nsobject<BackForwardMenuController> forwardMenuController_;
@@ -98,8 +97,7 @@ class ToolbarView;
commands:(CommandUpdater*)commands
profile:(Profile*)profile
browser:(Browser*)browser
- resizeDelegate:(id<ViewResizer>)resizeDelegate
- bookmarkDelegate:(id<BookmarkURLOpener>)delegate;
+ resizeDelegate:(id<ViewResizer>)resizeDelegate;
// Get the C++ bridge object representing the location bar for this tab.
- (LocationBar*)locationBar;
@@ -110,6 +108,9 @@ class ToolbarView;
// returns nil if we don't want to override the custom field editor for |obj|.
- (id)customFieldEditorForObject:(id)obj;
+// Returns the backdrop to the toolbar.
+- (BackgroundGradientView*)backgroundGradientView;
+
// Make the location bar the first responder, if possible.
- (void)focusLocationBar;
@@ -132,9 +133,6 @@ class ToolbarView;
// [self view].
- (void)setHasToolbar:(BOOL)toolbar;
-// Return the bookmark bar controller.
-- (BookmarkBarController*)bookmarkBarController;
-
// The bookmark bubble (when you click the star) needs to know where to go.
// Somewhere near the star button seems like a good start.
- (NSRect)starButtonInWindowCoordinates;
diff --git a/chrome/browser/cocoa/toolbar_controller.mm b/chrome/browser/cocoa/toolbar_controller.mm
index c36d9e1..ed726f8 100644
--- a/chrome/browser/cocoa/toolbar_controller.mm
+++ b/chrome/browser/cocoa/toolbar_controller.mm
@@ -36,7 +36,7 @@ static NSString* const kStarredImageName = @"starred.pdf";
static const float kBaseToolbarHeight = 36.0;
// Overlap (in pixels) between the toolbar and the bookmark bar.
-static const float kBookmarkBarOverlap = 7.0;
+static const float kBookmarkBarOverlap = 6.0;
@interface ToolbarController(Private)
- (void)initCommandStatus:(CommandUpdater*)commands;
@@ -90,8 +90,7 @@ class PrefObserverBridge : public NotificationObserver {
commands:(CommandUpdater*)commands
profile:(Profile*)profile
browser:(Browser*)browser
- resizeDelegate:(id<ViewResizer>)resizeDelegate
- bookmarkDelegate:(id<BookmarkURLOpener>)delegate {
+ resizeDelegate:(id<ViewResizer>)resizeDelegate {
DCHECK(model && commands && profile);
if ((self = [super initWithNibName:@"Toolbar"
bundle:mac_util::MainAppBundle()])) {
@@ -100,7 +99,6 @@ class PrefObserverBridge : public NotificationObserver {
profile_ = profile;
browser_ = browser;
resizeDelegate_ = resizeDelegate;
- bookmarkBarDelegate_ = delegate;
hasToolbar_ = YES;
// Register for notificaotions about state changes for the toolbar buttons
@@ -146,24 +144,6 @@ class PrefObserverBridge : public NotificationObserver {
[self showOptionalHomeButton];
[self showOptionalPageWrenchButtons];
- // Create a sub-controller for the bookmark bar.
- bookmarkBarController_.reset([[BookmarkBarController alloc]
- initWithProfile:profile_
- initialWidth:NSWidth([[self view] frame])
- resizeDelegate:self
- urlDelegate:bookmarkBarDelegate_]);
-
- // Add bookmark bar to the view hierarchy. This also triggers the
- // nib load. The bookmark bar is defined (in the nib) to be
- // bottom-aligned to it's parent view (among other things), so
- // position and resize properties don't need to be set.
- [[self view] addSubview:[bookmarkBarController_ view]];
-
- // We don't want to try and show the bar before it gets placed in
- // it's parent view, so this step shoudn't be inside the bookmark
- // bar controller's awakeFromNib.
- [bookmarkBarController_ showIfNeeded];
-
// Create the controllers for the back/forward menus.
backMenuController_.reset([[BackForwardMenuController alloc]
initWithBrowser:browser_
@@ -235,22 +215,6 @@ class PrefObserverBridge : public NotificationObserver {
[self mouseMoved:event];
}
-- (void)resizeView:(NSView*)view newHeight:(float)height {
- DCHECK(view == [bookmarkBarController_ view]);
-
- // The bookmark bar is always rooted at the bottom of the toolbar view, with
- // width equal to the toolbar's width. The toolbar view is resized to
- // accomodate the new bookmark bar height.
- NSRect frame = NSMakeRect(0, 0, [[self view] bounds].size.width, height);
- [view setFrame:frame];
-
- float newToolbarHeight = kBaseToolbarHeight + height - kBookmarkBarOverlap;
- if (newToolbarHeight < kBaseToolbarHeight)
- newToolbarHeight = kBaseToolbarHeight;
-
- [resizeDelegate_ resizeView:[self view] newHeight:newToolbarHeight];
-}
-
- (LocationBar*)locationBar {
return locationBarView_.get();
}
@@ -349,14 +313,8 @@ class PrefObserverBridge : public NotificationObserver {
return locationBar_;
}
-- (BookmarkBarController*)bookmarkBarController {
- // Browser has a FEATURE_BOOKMARKBAR but it is ignored by Safari
- // when using window.open(); the logic seems to be "if no toolbar,
- // no bookmark bar".
- // TODO(jrg): investigate non-Mac Chrome behavior and possibly expand this.
- if (hasToolbar_ == NO)
- return nil;
- return bookmarkBarController_.get();
+- (BackgroundGradientView*)backgroundGradientView {
+ return (BackgroundGradientView*)[super view];
}
- (id)customFieldEditorForObject:(id)obj {
@@ -467,6 +425,14 @@ class PrefObserverBridge : public NotificationObserver {
fromView:starButton_];
}
+- (void)setShouldBeCompressed:(BOOL)compressed {
+ CGFloat newToolbarHeight = kBaseToolbarHeight;
+ if (compressed)
+ newToolbarHeight -= kBookmarkBarOverlap;
+
+ [resizeDelegate_ resizeView:[self view] newHeight:newToolbarHeight];
+}
+
- (NSString *)view:(NSView *)view
stringForToolTip:(NSToolTipTag)tag
point:(NSPoint)point
diff --git a/chrome/browser/cocoa/toolbar_controller_unittest.mm b/chrome/browser/cocoa/toolbar_controller_unittest.mm
index fbcb0af..b27eafd 100644
--- a/chrome/browser/cocoa/toolbar_controller_unittest.mm
+++ b/chrome/browser/cocoa/toolbar_controller_unittest.mm
@@ -60,8 +60,7 @@ class ToolbarControllerTest : public PlatformTest {
commands:browser->command_updater()
profile:helper_.profile()
browser:browser
- resizeDelegate:resizeDelegate_.get()
- bookmarkDelegate:nil]);
+ resizeDelegate:resizeDelegate_.get()]);
EXPECT_TRUE([bar_ view]);
NSView* parent = [cocoa_helper_.window() contentView];
[parent addSubview:[bar_ view]];
@@ -94,19 +93,12 @@ TEST_F(ToolbarControllerTest, InitialState) {
CompareState(updater, [bar_ toolbarViews]);
}
-// Make sure awakeFromNib created a bookmarkBarController
-TEST_F(ToolbarControllerTest, AwakeFromNibCreatesBMBController) {
- EXPECT_TRUE([bar_ bookmarkBarController]);
-}
-
// Make sure a "titlebar only" toolbar works
TEST_F(ToolbarControllerTest, TitlebarOnly) {
NSView* view = [bar_ view];
- EXPECT_TRUE([bar_ bookmarkBarController]);
[bar_ setHasToolbar:NO];
EXPECT_NE(view, [bar_ view]);
- EXPECT_FALSE([bar_ bookmarkBarController]);
// Simulate a popup going fullscreen and back.
NSView* superview = [view superview];
@@ -118,7 +110,6 @@ TEST_F(ToolbarControllerTest, TitlebarOnly) {
[bar_ setHasToolbar:YES];
EXPECT_EQ(view, [bar_ view]);
- EXPECT_TRUE([bar_ bookmarkBarController]);
// Leave it off to make sure that's fine
[bar_ setHasToolbar:NO];
@@ -240,31 +231,6 @@ TEST_F(ToolbarControllerTest, DontToggleWhenNoToolbar) {
EXPECT_TRUE(NSEqualRects(locationBarFrame, newLocationBarFrame));
}
-// Make sure, by default, the bookmark bar is the full width of the
-// toolbar.
-TEST_F(ToolbarControllerTest, BookmarkBarIsFullWidth) {
- // Set the pref to the bookmark bar is visible when the toolbar is
- // first created.
- helper_.profile()->GetPrefs()->SetBoolean(prefs::kShowBookmarkBar, true);
-
- // Create a new bar (after the pref is set)
- Browser* browser = helper_.browser();
- bar_.reset(
- [[ToolbarController alloc] initWithModel:browser->toolbar_model()
- commands:browser->command_updater()
- profile:helper_.profile()
- browser:browser
- resizeDelegate:resizeDelegate_.get()
- bookmarkDelegate:nil]);
- EXPECT_TRUE([bar_ view]);
-
- // Make sure the bookmark bar is the same width as the toolbar
- NSView* bookmarkBarView = [[bar_ bookmarkBarController] view];
- EXPECT_EQ([[bar_ view] frame].size.width,
- [bookmarkBarView frame].size.width);
- EXPECT_TRUE([bookmarkBarView isDescendantOf:[bar_ view]]);
-}
-
TEST_F(ToolbarControllerTest, StarButtonInWindowCoordinates) {
NSRect star = [bar_ starButtonInWindowCoordinates];
NSRect all = [[[bar_ view] window] frame];
@@ -329,4 +295,14 @@ TEST_F(ToolbarControllerTest, PopulateEncodingMenu) {
EXPECT_NE(0, [encodings numberOfItems]);
}
+TEST_F(ToolbarControllerTest, CorrectHeightWhenCompressed) {
+ [bar_ setShouldBeCompressed:YES];
+ EXPECT_EQ(30.0, [resizeDelegate_ height]);
+}
+
+TEST_F(ToolbarControllerTest, CorrectHeightWhenUnompressed) {
+ [bar_ setShouldBeCompressed:NO];
+ EXPECT_EQ(36.0, [resizeDelegate_ height]);
+}
+
} // namespace