| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "app/l10n_util_mac.h" | 5 #include "app/l10n_util_mac.h" |
| 6 #include "app/resource_bundle.h" | 6 #include "app/resource_bundle.h" |
| 7 #include "base/mac_util.h" | 7 #include "base/mac_util.h" |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_editor.h" | 9 #include "chrome/browser/bookmarks/bookmark_editor.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_model.h" | 10 #include "chrome/browser/bookmarks/bookmark_model.h" |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // Overlap (in pixels) between the toolbar and the bookmark bar (when showing in | 103 // Overlap (in pixels) between the toolbar and the bookmark bar (when showing in |
| 104 // normal mode). | 104 // normal mode). |
| 105 const CGFloat kBookmarkBarOverlap = 5.0; | 105 const CGFloat kBookmarkBarOverlap = 5.0; |
| 106 | 106 |
| 107 // Duration of the bookmark bar animations. | 107 // Duration of the bookmark bar animations. |
| 108 const NSTimeInterval kBookmarkBarAnimationDuration = 0.12; | 108 const NSTimeInterval kBookmarkBarAnimationDuration = 0.12; |
| 109 | 109 |
| 110 } // namespace | 110 } // namespace |
| 111 | 111 |
| 112 @interface BookmarkBarController(Private) | 112 @interface BookmarkBarController(Private) |
| 113 |
| 113 // Determines the appropriate state for the given situation. | 114 // Determines the appropriate state for the given situation. |
| 114 + (bookmarks::VisualState)visualStateToShowNormalBar:(BOOL)showNormalBar | 115 + (bookmarks::VisualState)visualStateToShowNormalBar:(BOOL)showNormalBar |
| 115 showDetachedBar:(BOOL)showDetachedBar; | 116 showDetachedBar:(BOOL)showDetachedBar; |
| 116 | 117 |
| 117 // Moves to the given next state (from the current state), possibly animating. | 118 // Moves to the given next state (from the current state), possibly animating. |
| 118 // If |animate| is NO, it will stop any running animation and jump to the given | 119 // If |animate| is NO, it will stop any running animation and jump to the given |
| 119 // state. If YES, it may either (depending on implementation) jump to the end of | 120 // state. If YES, it may either (depending on implementation) jump to the end of |
| 120 // the current animation and begin the next one, or stop the current animation | 121 // the current animation and begin the next one, or stop the current animation |
| 121 // mid-flight and animate to the next state. | 122 // mid-flight and animate to the next state. |
| 122 - (void)moveToVisualState:(bookmarks::VisualState)nextVisualState | 123 - (void)moveToVisualState:(bookmarks::VisualState)nextVisualState |
| (...skipping 12 matching lines...) Expand all Loading... |
| 135 | 136 |
| 136 // Show/hide the bookmark bar. | 137 // Show/hide the bookmark bar. |
| 137 // if |animate| is YES, the changes are made using the animator; otherwise they | 138 // if |animate| is YES, the changes are made using the animator; otherwise they |
| 138 // are made immediately. | 139 // are made immediately. |
| 139 - (void)showBookmarkBarWithAnimation:(BOOL)animate; | 140 - (void)showBookmarkBarWithAnimation:(BOOL)animate; |
| 140 | 141 |
| 141 // Handles animating the resize of the content view. Returns YES if it handled | 142 // Handles animating the resize of the content view. Returns YES if it handled |
| 142 // the animation, NO if not (and hence it should be done instantly). | 143 // the animation, NO if not (and hence it should be done instantly). |
| 143 - (BOOL)doBookmarkBarAnimation; | 144 - (BOOL)doBookmarkBarAnimation; |
| 144 | 145 |
| 146 // Returns the index in the model for a drag of the given button to the given |
| 147 // location; currently, only the x-coordinate of |point| is considered. I |
| 148 // reserve the right to check for errors, in which case this would return |
| 149 // negative value; callers should check for this. |
| 150 - (int)indexForDragOfButton:(BookmarkButton*)sourceButton |
| 151 toPoint:(NSPoint)point; |
| 152 |
| 145 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu; | 153 - (void)addNode:(const BookmarkNode*)child toMenu:(NSMenu*)menu; |
| 146 - (void)addFolderNode:(const BookmarkNode*)node toMenu:(NSMenu*)menu; | 154 - (void)addFolderNode:(const BookmarkNode*)node toMenu:(NSMenu*)menu; |
| 147 - (void)tagEmptyMenu:(NSMenu*)menu; | 155 - (void)tagEmptyMenu:(NSMenu*)menu; |
| 148 - (void)clearMenuTagMap; | 156 - (void)clearMenuTagMap; |
| 149 - (int)preferredHeight; | 157 - (int)preferredHeight; |
| 150 - (void)addNonBookmarkButtonsToView; | 158 - (void)addNonBookmarkButtonsToView; |
| 151 - (void)addButtonsToView; | 159 - (void)addButtonsToView; |
| 152 - (void)resizeButtons; | 160 - (void)resizeButtons; |
| 153 - (void)centerNoItemsLabel; | 161 - (void)centerNoItemsLabel; |
| 154 - (NSImage*)getFavIconForNode:(const BookmarkNode*)node; | 162 - (NSImage*)getFavIconForNode:(const BookmarkNode*)node; |
| 155 - (void)setNodeForBarMenu; | 163 - (void)setNodeForBarMenu; |
| 164 |
| 156 @end | 165 @end |
| 157 | 166 |
| 158 @implementation BookmarkBarController | 167 @implementation BookmarkBarController |
| 159 | 168 |
| 160 @synthesize visualState = visualState_; | 169 @synthesize visualState = visualState_; |
| 161 @synthesize lastVisualState = lastVisualState_; | 170 @synthesize lastVisualState = lastVisualState_; |
| 162 @synthesize delegate = delegate_; | 171 @synthesize delegate = delegate_; |
| 163 | 172 |
| 164 - (id)initWithBrowser:(Browser*)browser | 173 - (id)initWithBrowser:(Browser*)browser |
| 165 initialWidth:(float)initialWidth | 174 initialWidth:(float)initialWidth |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 for (size_t i = 0; i < [urls count]; ++i) { | 491 for (size_t i = 0; i < [urls count]; ++i) { |
| 483 bookmarkModel_->AddURL( | 492 bookmarkModel_->AddURL( |
| 484 node, | 493 node, |
| 485 node->GetChildCount(), | 494 node->GetChildCount(), |
| 486 base::SysNSStringToWide([titles objectAtIndex:i]), | 495 base::SysNSStringToWide([titles objectAtIndex:i]), |
| 487 GURL([[urls objectAtIndex:i] UTF8String])); | 496 GURL([[urls objectAtIndex:i] UTF8String])); |
| 488 } | 497 } |
| 489 return YES; | 498 return YES; |
| 490 } | 499 } |
| 491 | 500 |
| 492 - (BOOL)dragButton:(BookmarkButton*)sourceButton to:(NSPoint)point { | 501 - (int)indexForDragOfButton:(BookmarkButton*)sourceButton |
| 502 toPoint:(NSPoint)point { |
| 493 DCHECK([sourceButton isKindOfClass:[BookmarkButton class]]); | 503 DCHECK([sourceButton isKindOfClass:[BookmarkButton class]]); |
| 494 | 504 |
| 495 void* pointer = [[[sourceButton cell] representedObject] pointerValue]; | 505 void* pointer = [[[sourceButton cell] representedObject] pointerValue]; |
| 496 const BookmarkNode* sourceNode = static_cast<const BookmarkNode*>(pointer); | 506 const BookmarkNode* sourceNode = static_cast<const BookmarkNode*>(pointer); |
| 497 DCHECK(sourceNode); | 507 if (!sourceNode) { |
| 508 NOTREACHED(); |
| 509 return -1; |
| 510 } |
| 498 | 511 |
| 499 // Identify which buttons we are between. For now, assume a button | 512 // Identify which buttons we are between. For now, assume a button |
| 500 // location is at the center point of its view, and that an exact | 513 // location is at the center point of its view, and that an exact |
| 501 // match means "place before". | 514 // match means "place before". |
| 502 // TODO(jrg): revisit position info based on UI team feedback. | 515 // TODO(jrg): revisit position info based on UI team feedback. |
| 503 // dropLocation is in bar local coordinates. | 516 // dropLocation is in bar local coordinates. |
| 504 NSPoint dropLocation = [[self view] convertPoint:point | 517 NSPoint dropLocation = |
| 505 fromView:[[[self view] window] | 518 [[self view] convertPoint:point |
| 506 contentView]]; | 519 fromView:[[[self view] window] contentView]]; |
| 507 NSButton* buttonToTheRightOfDraggedButton = nil; | 520 NSButton* buttonToTheRightOfDraggedButton = nil; |
| 508 for (NSButton* button in buttons_.get()) { | 521 for (NSButton* button in buttons_.get()) { |
| 509 CGFloat midpoint = NSMidX([button frame]); | 522 CGFloat midpoint = NSMidX([button frame]); |
| 510 if (dropLocation.x <= midpoint) { | 523 if (dropLocation.x <= midpoint) { |
| 511 buttonToTheRightOfDraggedButton = button; | 524 buttonToTheRightOfDraggedButton = button; |
| 512 break; | 525 break; |
| 513 } | 526 } |
| 514 } | 527 } |
| 515 if (buttonToTheRightOfDraggedButton) { | 528 if (buttonToTheRightOfDraggedButton) { |
| 516 pointer = [[[buttonToTheRightOfDraggedButton cell] | 529 pointer = [[[buttonToTheRightOfDraggedButton cell] |
| 517 representedObject] pointerValue]; | 530 representedObject] pointerValue]; |
| 518 const BookmarkNode* afterNode = static_cast<const BookmarkNode*>(pointer); | 531 const BookmarkNode* afterNode = static_cast<const BookmarkNode*>(pointer); |
| 519 bookmarkModel_->Move(sourceNode, sourceNode->GetParent(), | 532 return afterNode->GetParent()->IndexOfChild(afterNode); |
| 520 afterNode->GetParent()->IndexOfChild(afterNode)); | 533 } |
| 534 |
| 535 // If nothing is to my right I am at the end! |
| 536 return sourceNode->GetParent()->GetChildCount(); |
| 537 } |
| 538 |
| 539 - (BOOL)dragButton:(BookmarkButton*)sourceButton to:(NSPoint)point { |
| 540 DCHECK([sourceButton isKindOfClass:[BookmarkButton class]]); |
| 541 |
| 542 void* pointer = [[[sourceButton cell] representedObject] pointerValue]; |
| 543 const BookmarkNode* sourceNode = static_cast<const BookmarkNode*>(pointer); |
| 544 DCHECK(sourceNode); |
| 545 |
| 546 int destIndex = [self indexForDragOfButton:sourceButton toPoint:point]; |
| 547 if (destIndex >= 0 && sourceNode) { |
| 548 bookmarkModel_->Move(sourceNode, sourceNode->GetParent(), destIndex); |
| 521 } else { | 549 } else { |
| 522 // If nothing is to my right I am at the end! | 550 NOTREACHED(); |
| 523 bookmarkModel_->Move(sourceNode, sourceNode->GetParent(), | |
| 524 sourceNode->GetParent()->GetChildCount()); | |
| 525 } | 551 } |
| 526 | 552 |
| 527 // Movement of a node triggers observers (like us) to rebuild the | 553 // Movement of a node triggers observers (like us) to rebuild the |
| 528 // bar so we don't have to do so explicitly. | 554 // bar so we don't have to do so explicitly. |
| 529 | 555 |
| 530 return YES; | 556 return YES; |
| 531 } | 557 } |
| 532 | 558 |
| 559 - (CGFloat)indicatorPosForDragOfButton:(BookmarkButton*)sourceButton |
| 560 toPoint:(NSPoint)point { |
| 561 CGFloat x = 0; |
| 562 int destIndex = [self indexForDragOfButton:sourceButton toPoint:point]; |
| 563 int numButtons = static_cast<int>([buttons_ count]); |
| 564 |
| 565 // If it's a drop strictly between existing buttons ... |
| 566 if (destIndex >= 0 && destIndex < numButtons) { |
| 567 // ... put the indicator right between the buttons. |
| 568 BookmarkButton* button = |
| 569 [buttons_ objectAtIndex:static_cast<NSUInteger>(destIndex)]; |
| 570 DCHECK(button); |
| 571 NSRect buttonFrame = [button frame]; |
| 572 x = buttonFrame.origin.x - 0.5 * bookmarks::kBookmarkHorizontalPadding; |
| 573 |
| 574 // If it's a drop at the end (past the last button, if there are any) ... |
| 575 } else if (destIndex == numButtons) { |
| 576 // and if it's past the last button ... |
| 577 if (numButtons > 0) { |
| 578 // ... find the last button, and put the indicator to its right. |
| 579 BookmarkButton* button = |
| 580 [buttons_ objectAtIndex:static_cast<NSUInteger>(destIndex - 1)]; |
| 581 DCHECK(button); |
| 582 NSRect buttonFrame = [button frame]; |
| 583 x = buttonFrame.origin.x + buttonFrame.size.width + |
| 584 0.5 * bookmarks::kBookmarkHorizontalPadding; |
| 585 |
| 586 // Otherwise, put it right at the beginning. |
| 587 } else { |
| 588 x = 0.5 * bookmarks::kBookmarkHorizontalPadding; |
| 589 } |
| 590 } else { |
| 591 NOTREACHED(); |
| 592 } |
| 593 |
| 594 return x; |
| 595 } |
| 596 |
| 533 - (int)currentTabContentsHeight { | 597 - (int)currentTabContentsHeight { |
| 534 return browser_->GetSelectedTabContents() ? | 598 return browser_->GetSelectedTabContents() ? |
| 535 browser_->GetSelectedTabContents()->view()->GetContainerSize().height() : | 599 browser_->GetSelectedTabContents()->view()->GetContainerSize().height() : |
| 536 0; | 600 0; |
| 537 } | 601 } |
| 538 | 602 |
| 539 - (ThemeProvider*)themeProvider { | 603 - (ThemeProvider*)themeProvider { |
| 540 return browser_->profile()->GetThemeProvider(); | 604 return browser_->profile()->GetThemeProvider(); |
| 541 } | 605 } |
| 542 | 606 |
| (...skipping 831 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1374 [[self animatableView] currentAnimationProgress]); | 1438 [[self animatableView] currentAnimationProgress]); |
| 1375 } | 1439 } |
| 1376 if ([self isAnimatingFromState:bookmarks::kDetachedState]) { | 1440 if ([self isAnimatingFromState:bookmarks::kDetachedState]) { |
| 1377 return static_cast<CGFloat>( | 1441 return static_cast<CGFloat>( |
| 1378 1 - [[self animatableView] currentAnimationProgress]); | 1442 1 - [[self animatableView] currentAnimationProgress]); |
| 1379 } | 1443 } |
| 1380 return 0; | 1444 return 0; |
| 1381 } | 1445 } |
| 1382 | 1446 |
| 1383 @end | 1447 @end |
| OLD | NEW |