OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" | 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" |
6 | 6 |
7 #include "base/mac/mac_util.h" | 7 #include "base/mac/mac_util.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
10 #include "chrome/browser/bookmarks/bookmark_editor.h" | 10 #include "chrome/browser/bookmarks/bookmark_editor.h" |
(...skipping 825 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 DCHECK([[self view] isKindOfClass:[BackgroundGradientView class]]); | 836 DCHECK([[self view] isKindOfClass:[BackgroundGradientView class]]); |
837 return (BackgroundGradientView*)[self view]; | 837 return (BackgroundGradientView*)[self view]; |
838 } | 838 } |
839 | 839 |
840 // (Private) Method is the same as [self view], but is provided to be explicit. | 840 // (Private) Method is the same as [self view], but is provided to be explicit. |
841 - (AnimatableView*)animatableView { | 841 - (AnimatableView*)animatableView { |
842 DCHECK([[self view] isKindOfClass:[AnimatableView class]]); | 842 DCHECK([[self view] isKindOfClass:[AnimatableView class]]); |
843 return (AnimatableView*)[self view]; | 843 return (AnimatableView*)[self view]; |
844 } | 844 } |
845 | 845 |
846 // Position the off-the-side chevron to the left of the otherBookmarks button. | 846 // Position the off-the-side chevron to the left of the otherBookmarks button, |
| 847 // unless it's hidden in which case it's right aligned on top of it. |
847 - (void)positionOffTheSideButton { | 848 - (void)positionOffTheSideButton { |
848 NSRect frame = [offTheSideButton_ frame]; | 849 NSRect frame = [offTheSideButton_ frame]; |
849 frame.size.height = bookmarks::kBookmarkFolderButtonHeight; | 850 frame.size.height = bookmarks::kBookmarkFolderButtonHeight; |
850 if (otherBookmarksButton_.get()) { | 851 if (otherBookmarksButton_.get() && ![otherBookmarksButton_ isHidden]) { |
851 frame.origin.x = ([otherBookmarksButton_ frame].origin.x - | 852 frame.origin.x = ([otherBookmarksButton_ frame].origin.x - |
852 (frame.size.width + | 853 (frame.size.width + |
853 bookmarks::kBookmarkHorizontalPadding)); | 854 bookmarks::kBookmarkHorizontalPadding)); |
| 855 } else { |
| 856 frame.origin.x = (NSMaxX([otherBookmarksButton_ frame]) - frame.size.width); |
854 } | 857 } |
855 [offTheSideButton_ setFrame:frame]; | 858 [offTheSideButton_ setFrame:frame]; |
856 } | 859 } |
857 | 860 |
858 // Configure the off-the-side button (e.g. specify the node range, | 861 // Configure the off-the-side button (e.g. specify the node range, |
859 // check if we should enable or disable it, etc). | 862 // check if we should enable or disable it, etc). |
860 - (void)configureOffTheSideButtonContentsAndVisibility { | 863 - (void)configureOffTheSideButtonContentsAndVisibility { |
861 // If deleting a button while off-the-side is open, buttons may be | 864 // If deleting a button while off-the-side is open, buttons may be |
862 // promoted from off-the-side to the bar. Accomodate. | 865 // promoted from off-the-side to the bar. Accomodate. |
863 if (folderController_ && | 866 if (folderController_ && |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1307 ++displayedButtonCount_; | 1310 ++displayedButtonCount_; |
1308 } | 1311 } |
1309 NSUInteger removalCount = | 1312 NSUInteger removalCount = |
1310 [buttons count] - (NSUInteger)displayedButtonCount_; | 1313 [buttons count] - (NSUInteger)displayedButtonCount_; |
1311 if (removalCount > 0) { | 1314 if (removalCount > 0) { |
1312 NSRange removalRange = NSMakeRange(displayedButtonCount_, removalCount); | 1315 NSRange removalRange = NSMakeRange(displayedButtonCount_, removalCount); |
1313 [buttons removeObjectsInRange:removalRange]; | 1316 [buttons removeObjectsInRange:removalRange]; |
1314 } | 1317 } |
1315 } | 1318 } |
1316 | 1319 |
| 1320 // Shows or hides the Other Bookmarks button as appropriate, and returns |
| 1321 // whether it ended up visible. |
| 1322 - (BOOL)setOtherBookmarksButtonVisibility { |
| 1323 if (!otherBookmarksButton_.get()) |
| 1324 return NO; |
| 1325 |
| 1326 BOOL visible = ![otherBookmarksButton_ bookmarkNode]->empty(); |
| 1327 [otherBookmarksButton_ setHidden:!visible]; |
| 1328 return visible; |
| 1329 } |
| 1330 |
1317 // Create the button for "Other Bookmarks" on the right of the bar. | 1331 // Create the button for "Other Bookmarks" on the right of the bar. |
1318 - (void)createOtherBookmarksButton { | 1332 - (void)createOtherBookmarksButton { |
1319 // Can't create this until the model is loaded, but only need to | 1333 // Can't create this until the model is loaded, but only need to |
1320 // create it once. | 1334 // create it once. |
1321 if (otherBookmarksButton_.get()) | 1335 if (otherBookmarksButton_.get()) { |
| 1336 [self setOtherBookmarksButtonVisibility]; |
1322 return; | 1337 return; |
| 1338 } |
1323 | 1339 |
1324 // TODO(jrg): remove duplicate code | 1340 // TODO(jrg): remove duplicate code |
1325 NSCell* cell = [self cellForBookmarkNode:bookmarkModel_->other_node()]; | 1341 NSCell* cell = [self cellForBookmarkNode:bookmarkModel_->other_node()]; |
1326 int ignored = 0; | 1342 int ignored = 0; |
1327 NSRect frame = [self frameForBookmarkButtonFromCell:cell xOffset:&ignored]; | 1343 NSRect frame = [self frameForBookmarkButtonFromCell:cell xOffset:&ignored]; |
1328 frame.origin.x = [[self buttonView] bounds].size.width - frame.size.width; | 1344 frame.origin.x = [[self buttonView] bounds].size.width - frame.size.width; |
1329 frame.origin.x -= bookmarks::kBookmarkHorizontalPadding; | 1345 frame.origin.x -= bookmarks::kBookmarkHorizontalPadding; |
1330 BookmarkButton* button = [[BookmarkButton alloc] initWithFrame:frame]; | 1346 BookmarkButton* button = [[BookmarkButton alloc] initWithFrame:frame]; |
1331 [button setDraggable:NO]; | 1347 [button setDraggable:NO]; |
1332 [button setActsOnMouseDown:YES]; | 1348 [button setActsOnMouseDown:YES]; |
1333 otherBookmarksButton_.reset(button); | 1349 otherBookmarksButton_.reset(button); |
1334 view_id_util::SetID(button, VIEW_ID_OTHER_BOOKMARKS); | 1350 view_id_util::SetID(button, VIEW_ID_OTHER_BOOKMARKS); |
1335 | 1351 |
1336 // Make sure this button, like all other BookmarkButtons, lives | 1352 // Make sure this button, like all other BookmarkButtons, lives |
1337 // until the end of the current event loop. | 1353 // until the end of the current event loop. |
1338 [[button retain] autorelease]; | 1354 [[button retain] autorelease]; |
1339 | 1355 |
1340 // Peg at right; keep same height as bar. | 1356 // Peg at right; keep same height as bar. |
1341 [button setAutoresizingMask:(NSViewMinXMargin)]; | 1357 [button setAutoresizingMask:(NSViewMinXMargin)]; |
1342 [button setCell:cell]; | 1358 [button setCell:cell]; |
1343 [button setDelegate:self]; | 1359 [button setDelegate:self]; |
1344 [button setTarget:self]; | 1360 [button setTarget:self]; |
1345 [button setAction:@selector(openBookmarkFolderFromButton:)]; | 1361 [button setAction:@selector(openBookmarkFolderFromButton:)]; |
1346 [buttonView_ addSubview:button]; | 1362 [buttonView_ addSubview:button]; |
1347 | 1363 |
| 1364 [self setOtherBookmarksButtonVisibility]; |
| 1365 |
1348 // Now that it's here, move the chevron over. | 1366 // Now that it's here, move the chevron over. |
1349 [self positionOffTheSideButton]; | 1367 [self positionOffTheSideButton]; |
1350 } | 1368 } |
1351 | 1369 |
1352 // Now that the model is loaded, set the bookmark bar root as the node | 1370 // Now that the model is loaded, set the bookmark bar root as the node |
1353 // represented by the bookmark bar (default, background) menu. | 1371 // represented by the bookmark bar (default, background) menu. |
1354 - (void)setNodeForBarMenu { | 1372 - (void)setNodeForBarMenu { |
1355 const BookmarkNode* node = bookmarkModel_->GetBookmarkBarNode(); | 1373 const BookmarkNode* node = bookmarkModel_->GetBookmarkBarNode(); |
1356 BookmarkMenu* menu = static_cast<BookmarkMenu*>([[self view] menu]); | 1374 BookmarkMenu* menu = static_cast<BookmarkMenu*>([[self view] menu]); |
1357 | 1375 |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1547 } | 1565 } |
1548 | 1566 |
1549 - (void)redistributeButtonsOnBarAsNeeded { | 1567 - (void)redistributeButtonsOnBarAsNeeded { |
1550 const BookmarkNode* node = bookmarkModel_->GetBookmarkBarNode(); | 1568 const BookmarkNode* node = bookmarkModel_->GetBookmarkBarNode(); |
1551 NSInteger barCount = node->child_count(); | 1569 NSInteger barCount = node->child_count(); |
1552 | 1570 |
1553 // Determine the current maximum extent of the visible buttons. | 1571 // Determine the current maximum extent of the visible buttons. |
1554 CGFloat maxViewX = NSMaxX([[self view] bounds]); | 1572 CGFloat maxViewX = NSMaxX([[self view] bounds]); |
1555 NSButton* otherBookmarksButton = otherBookmarksButton_.get(); | 1573 NSButton* otherBookmarksButton = otherBookmarksButton_.get(); |
1556 // If necessary, pull in the width to account for the Other Bookmarks button. | 1574 // If necessary, pull in the width to account for the Other Bookmarks button. |
1557 if (otherBookmarksButton_) | 1575 if ([self setOtherBookmarksButtonVisibility]) |
1558 maxViewX = [otherBookmarksButton frame].origin.x - | 1576 maxViewX = [otherBookmarksButton frame].origin.x - |
1559 bookmarks::kBookmarkHorizontalPadding; | 1577 bookmarks::kBookmarkHorizontalPadding; |
| 1578 |
| 1579 [self positionOffTheSideButton]; |
1560 // If we're already overflowing, then we need to account for the chevron. | 1580 // If we're already overflowing, then we need to account for the chevron. |
1561 if (barCount > displayedButtonCount_) | 1581 if (barCount > displayedButtonCount_) |
1562 maxViewX = [offTheSideButton_ frame].origin.x - | 1582 maxViewX = [offTheSideButton_ frame].origin.x - |
1563 bookmarks::kBookmarkHorizontalPadding; | 1583 bookmarks::kBookmarkHorizontalPadding; |
1564 | 1584 |
1565 // As a result of pasting or dragging, the bar may now have more buttons | 1585 // As a result of pasting or dragging, the bar may now have more buttons |
1566 // than will fit so remove any which overflow. They will be shown in | 1586 // than will fit so remove any which overflow. They will be shown in |
1567 // the off-the-side folder. | 1587 // the off-the-side folder. |
1568 while (displayedButtonCount_ > 0) { | 1588 while (displayedButtonCount_ > 0) { |
1569 BookmarkButton* button = [buttons_ lastObject]; | 1589 BookmarkButton* button = [buttons_ lastObject]; |
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2089 // If a context menu is open, close it. | 2109 // If a context menu is open, close it. |
2090 [self cancelMenuTracking]; | 2110 [self cancelMenuTracking]; |
2091 | 2111 |
2092 const BookmarkNode* newNode = newParent->GetChild(newIndex); | 2112 const BookmarkNode* newNode = newParent->GetChild(newIndex); |
2093 id<BookmarkButtonControllerProtocol> newController = | 2113 id<BookmarkButtonControllerProtocol> newController = |
2094 [self controllerForNode:newParent]; | 2114 [self controllerForNode:newParent]; |
2095 [newController addButtonForNode:newNode atIndex:newIndex]; | 2115 [newController addButtonForNode:newNode atIndex:newIndex]; |
2096 // If we go from 0 --> 1 bookmarks we may need to hide the | 2116 // If we go from 0 --> 1 bookmarks we may need to hide the |
2097 // "bookmarks go here" text container. | 2117 // "bookmarks go here" text container. |
2098 [self showOrHideNoItemContainerForNode:model->GetBookmarkBarNode()]; | 2118 [self showOrHideNoItemContainerForNode:model->GetBookmarkBarNode()]; |
| 2119 // Cope with chevron or "Other Bookmarks" buttons possibly changing state. |
| 2120 [self reconfigureBookmarkBar]; |
2099 } | 2121 } |
2100 | 2122 |
2101 // TODO(jrg): for now this is brute force. | 2123 // TODO(jrg): for now this is brute force. |
2102 - (void)nodeChanged:(BookmarkModel*)model | 2124 - (void)nodeChanged:(BookmarkModel*)model |
2103 node:(const BookmarkNode*)node { | 2125 node:(const BookmarkNode*)node { |
2104 [self loaded:model]; | 2126 [self loaded:model]; |
2105 } | 2127 } |
2106 | 2128 |
2107 - (void)nodeMoved:(BookmarkModel*)model | 2129 - (void)nodeMoved:(BookmarkModel*)model |
2108 oldParent:(const BookmarkNode*)oldParent oldIndex:(int)oldIndex | 2130 oldParent:(const BookmarkNode*)oldParent oldIndex:(int)oldIndex |
2109 newParent:(const BookmarkNode*)newParent newIndex:(int)newIndex { | 2131 newParent:(const BookmarkNode*)newParent newIndex:(int)newIndex { |
2110 const BookmarkNode* movedNode = newParent->GetChild(newIndex); | 2132 const BookmarkNode* movedNode = newParent->GetChild(newIndex); |
2111 id<BookmarkButtonControllerProtocol> oldController = | 2133 id<BookmarkButtonControllerProtocol> oldController = |
2112 [self controllerForNode:oldParent]; | 2134 [self controllerForNode:oldParent]; |
2113 id<BookmarkButtonControllerProtocol> newController = | 2135 id<BookmarkButtonControllerProtocol> newController = |
2114 [self controllerForNode:newParent]; | 2136 [self controllerForNode:newParent]; |
2115 if (newController == oldController) { | 2137 if (newController == oldController) { |
2116 [oldController moveButtonFromIndex:oldIndex toIndex:newIndex]; | 2138 [oldController moveButtonFromIndex:oldIndex toIndex:newIndex]; |
2117 } else { | 2139 } else { |
2118 [oldController removeButton:oldIndex animate:NO]; | 2140 [oldController removeButton:oldIndex animate:NO]; |
2119 [newController addButtonForNode:movedNode atIndex:newIndex]; | 2141 [newController addButtonForNode:movedNode atIndex:newIndex]; |
2120 } | 2142 } |
2121 // If the bar is one of the parents we may need to update the visibility | 2143 // If the bar is one of the parents we may need to update the visibility |
2122 // of the "bookmarks go here" presentation. | 2144 // of the "bookmarks go here" presentation. |
2123 [self showOrHideNoItemContainerForNode:model->GetBookmarkBarNode()]; | 2145 [self showOrHideNoItemContainerForNode:model->GetBookmarkBarNode()]; |
2124 // If we moved the only item on the "off the side" menu somewhere | 2146 // Cope with chevron or "Other Bookmarks" buttons possibly changing state. |
2125 // else, we may no longer need to show it. | 2147 [self reconfigureBookmarkBar]; |
2126 [self configureOffTheSideButtonContentsAndVisibility]; | |
2127 } | 2148 } |
2128 | 2149 |
2129 - (void)nodeRemoved:(BookmarkModel*)model | 2150 - (void)nodeRemoved:(BookmarkModel*)model |
2130 parent:(const BookmarkNode*)oldParent index:(int)index { | 2151 parent:(const BookmarkNode*)oldParent index:(int)index { |
2131 // If a context menu is open, close it. | 2152 // If a context menu is open, close it. |
2132 [self cancelMenuTracking]; | 2153 [self cancelMenuTracking]; |
2133 | 2154 |
2134 // Locate the parent node. The parent may not be showing, in which case | 2155 // Locate the parent node. The parent may not be showing, in which case |
2135 // we do nothing. | 2156 // we do nothing. |
2136 id<BookmarkButtonControllerProtocol> parentController = | 2157 id<BookmarkButtonControllerProtocol> parentController = |
(...skipping 591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2728 // to minimize touching the object passed in (likely a mock). | 2749 // to minimize touching the object passed in (likely a mock). |
2729 - (void)setButtonContextMenu:(id)menu { | 2750 - (void)setButtonContextMenu:(id)menu { |
2730 buttonContextMenu_ = menu; | 2751 buttonContextMenu_ = menu; |
2731 } | 2752 } |
2732 | 2753 |
2733 - (void)setIgnoreAnimations:(BOOL)ignore { | 2754 - (void)setIgnoreAnimations:(BOOL)ignore { |
2734 ignoreAnimations_ = ignore; | 2755 ignoreAnimations_ = ignore; |
2735 } | 2756 } |
2736 | 2757 |
2737 @end | 2758 @end |
OLD | NEW |