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 1375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1386 [noItemTextfield setHidden:NO]; | 1386 [noItemTextfield setHidden:NO]; |
1387 NSButton* importBookmarksButton = [buttonView_ importBookmarksButton]; | 1387 NSButton* importBookmarksButton = [buttonView_ importBookmarksButton]; |
1388 [importBookmarksButton setFrame:originalImportBookmarksRect_]; | 1388 [importBookmarksButton setFrame:originalImportBookmarksRect_]; |
1389 [importBookmarksButton setHidden:NO]; | 1389 [importBookmarksButton setHidden:NO]; |
1390 // Check each to see if they need to be shrunk or hidden. | 1390 // Check each to see if they need to be shrunk or hidden. |
1391 if ([self shrinkOrHideView:importBookmarksButton forMaxX:maxViewX]) | 1391 if ([self shrinkOrHideView:importBookmarksButton forMaxX:maxViewX]) |
1392 [self shrinkOrHideView:noItemTextfield forMaxX:maxViewX]; | 1392 [self shrinkOrHideView:noItemTextfield forMaxX:maxViewX]; |
1393 } | 1393 } |
1394 } | 1394 } |
1395 | 1395 |
1396 // Scans through all buttons from left to right, calculating from scratch where | |
1397 // they should be based on the preceding widths, until it finds the one | |
1398 // requested. | |
1399 // Returns NSZeroRect if there is no such button in the bookmark bar. | |
1400 // Enables you to work out where a button will end up when it is done animating. | |
1401 - (NSRect)finalRectOfButton:(BookmarkButton*)wantedButton { | |
1402 CGFloat left = bookmarks::kBookmarkHorizontalPadding; | |
1403 NSRect buttonFrame = NSZeroRect; | |
1404 | |
1405 for (NSButton* button in buttons_.get()) { | |
1406 // Hidden buttons get no space. | |
1407 if ([button isHidden]) | |
1408 continue; | |
1409 buttonFrame = [button frame]; | |
1410 buttonFrame.origin.x = left; | |
1411 left += buttonFrame.size.width + bookmarks::kBookmarkHorizontalPadding; | |
1412 if (button == wantedButton) | |
1413 return buttonFrame; | |
1414 } | |
1415 return NSZeroRect; | |
1416 } | |
1417 | |
1418 // Calculates the final position of the last button in the bar. | |
1419 // We can't just use [[self buttons] lastObject] frame] because the button | |
1420 // may be animating currently. | |
1421 - (NSRect)finalRectOfLastButton { | |
1422 return [self finalRectOfButton:[[self buttons] lastObject]]; | |
1423 } | |
1424 | |
1425 - (void)redistributeButtonsOnBarAsNeeded { | 1396 - (void)redistributeButtonsOnBarAsNeeded { |
1426 const BookmarkNode* node = bookmarkModel_->GetBookmarkBarNode(); | 1397 const BookmarkNode* node = bookmarkModel_->GetBookmarkBarNode(); |
1427 NSInteger barCount = node->GetChildCount(); | 1398 NSInteger barCount = node->GetChildCount(); |
1428 | 1399 |
1429 // Determine the current maximum extent of the visible buttons. | 1400 // Determine the current maximum extent of the visible buttons. |
1430 CGFloat maxViewX = NSMaxX([[self view] bounds]); | 1401 CGFloat maxViewX = NSMaxX([[self view] bounds]); |
1431 NSButton* otherBookmarksButton = otherBookmarksButton_.get(); | 1402 NSButton* otherBookmarksButton = otherBookmarksButton_.get(); |
1432 // If necessary, pull in the width to account for the Other Bookmarks button. | 1403 // If necessary, pull in the width to account for the Other Bookmarks button. |
1433 if (otherBookmarksButton_) | 1404 if (otherBookmarksButton_) |
1434 maxViewX = [otherBookmarksButton frame].origin.x - | 1405 maxViewX = [otherBookmarksButton frame].origin.x - |
1435 bookmarks::kBookmarkHorizontalPadding; | 1406 bookmarks::kBookmarkHorizontalPadding; |
1436 // If we're already overflowing, then we need to account for the chevron. | 1407 // If we're already overflowing, then we need to account for the chevron. |
1437 if (barCount > displayedButtonCount_) | 1408 if (barCount > displayedButtonCount_) |
1438 maxViewX = [offTheSideButton_ frame].origin.x - | 1409 maxViewX = [offTheSideButton_ frame].origin.x - |
1439 bookmarks::kBookmarkHorizontalPadding; | 1410 bookmarks::kBookmarkHorizontalPadding; |
1440 | 1411 |
1441 // As a result of pasting or dragging, the bar may now have more buttons | 1412 // As a result of pasting or dragging, the bar may now have more buttons |
1442 // than will fit so remove any which overflow. They will be shown in | 1413 // than will fit so remove any which overflow. They will be shown in |
1443 // the off-the-side folder. | 1414 // the off-the-side folder. |
1444 while (displayedButtonCount_ > 0) { | 1415 while (displayedButtonCount_ > 0) { |
1445 BookmarkButton* button = [buttons_ lastObject]; | 1416 BookmarkButton* button = [buttons_ lastObject]; |
1446 if (NSMaxX([self finalRectOfLastButton]) < maxViewX) | 1417 if (NSMaxX([button frame]) < maxViewX) |
1447 break; | 1418 break; |
1448 [buttons_ removeLastObject]; | 1419 [buttons_ removeLastObject]; |
1449 [button setDelegate:nil]; | 1420 [button setDelegate:nil]; |
1450 [button removeFromSuperview]; | 1421 [button removeFromSuperview]; |
1451 --displayedButtonCount_; | 1422 --displayedButtonCount_; |
1452 } | 1423 } |
1453 | 1424 |
1454 // As a result of cutting, deleting and dragging, the bar may now have room | 1425 // As a result of cutting, deleting and dragging, the bar may now have room |
1455 // for more buttons. | 1426 // for more buttons. |
1456 int xOffset = displayedButtonCount_ > 0 ? | 1427 int xOffset = displayedButtonCount_ > 0 ? |
1457 NSMaxX([self finalRectOfLastButton]) + | 1428 NSMaxX([[buttons_ lastObject] frame]) + |
1458 bookmarks::kBookmarkHorizontalPadding : 0; | 1429 bookmarks::kBookmarkHorizontalPadding : 0; |
1459 for (int i = displayedButtonCount_; i < barCount; ++i) { | 1430 for (int i = displayedButtonCount_; i < barCount; ++i) { |
1460 const BookmarkNode* child = node->GetChild(i); | 1431 const BookmarkNode* child = node->GetChild(i); |
1461 BookmarkButton* button = [self buttonForNode:child xOffset:&xOffset]; | 1432 BookmarkButton* button = [self buttonForNode:child xOffset:&xOffset]; |
1462 // If we're testing against the last possible button then account | 1433 // If we're testing against the last possible button then account |
1463 // for the chevron no longer needing to be shown. | 1434 // for the chevron no longer needing to be shown. |
1464 if (i == barCount + 1) | 1435 if (i == barCount + 1) |
1465 maxViewX += NSWidth([offTheSideButton_ frame]) + | 1436 maxViewX += NSWidth([offTheSideButton_ frame]) + |
1466 bookmarks::kBookmarkHorizontalPadding; | 1437 bookmarks::kBookmarkHorizontalPadding; |
1467 if (NSMaxX([button frame]) >= maxViewX) | 1438 if (NSMaxX([button frame]) >= maxViewX) |
(...skipping 1082 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2550 // to minimize touching the object passed in (likely a mock). | 2521 // to minimize touching the object passed in (likely a mock). |
2551 - (void)setButtonContextMenu:(id)menu { | 2522 - (void)setButtonContextMenu:(id)menu { |
2552 buttonContextMenu_ = menu; | 2523 buttonContextMenu_ = menu; |
2553 } | 2524 } |
2554 | 2525 |
2555 - (void)setIgnoreAnimations:(BOOL)ignore { | 2526 - (void)setIgnoreAnimations:(BOOL)ignore { |
2556 ignoreAnimations_ = ignore; | 2527 ignoreAnimations_ = ignore; |
2557 } | 2528 } |
2558 | 2529 |
2559 @end | 2530 @end |
OLD | NEW |