OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/browser_actions_controller.h" | 5 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 bool ToolbarActionsBarBridge::IsPopupRunning() const { | 231 bool ToolbarActionsBarBridge::IsPopupRunning() const { |
232 return [ExtensionPopupController popup] != nil; | 232 return [ExtensionPopupController popup] != nil; |
233 } | 233 } |
234 | 234 |
235 } // namespace | 235 } // namespace |
236 | 236 |
237 @implementation BrowserActionsController | 237 @implementation BrowserActionsController |
238 | 238 |
239 @synthesize containerView = containerView_; | 239 @synthesize containerView = containerView_; |
240 @synthesize browser = browser_; | 240 @synthesize browser = browser_; |
| 241 @synthesize isOverflow = isOverflow_; |
241 | 242 |
242 #pragma mark - | 243 #pragma mark - |
243 #pragma mark Public Methods | 244 #pragma mark Public Methods |
244 | 245 |
245 - (id)initWithBrowser:(Browser*)browser | 246 - (id)initWithBrowser:(Browser*)browser |
246 containerView:(BrowserActionsContainerView*)container | 247 containerView:(BrowserActionsContainerView*)container |
247 mainController:(BrowserActionsController*)mainController { | 248 mainController:(BrowserActionsController*)mainController { |
248 DCHECK(browser && container); | 249 DCHECK(browser && container); |
249 | 250 |
250 if ((self = [super init])) { | 251 if ((self = [super init])) { |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 [self resizeContainerToWidth:toolbarActionsBar_->GetPreferredSize().width()]; | 627 [self resizeContainerToWidth:toolbarActionsBar_->GetPreferredSize().width()]; |
627 } | 628 } |
628 | 629 |
629 - (void)actionButtonDragging:(NSNotification*)notification { | 630 - (void)actionButtonDragging:(NSNotification*)notification { |
630 suppressChevron_ = YES; | 631 suppressChevron_ = YES; |
631 if (![self chevronIsHidden]) | 632 if (![self chevronIsHidden]) |
632 [self setChevronHidden:YES inFrame:[containerView_ frame]]; | 633 [self setChevronHidden:YES inFrame:[containerView_ frame]]; |
633 | 634 |
634 // Determine what index the dragged button should lie in, alter the model and | 635 // Determine what index the dragged button should lie in, alter the model and |
635 // reposition the buttons. | 636 // reposition the buttons. |
636 CGFloat dragThreshold = ToolbarActionsBar::IconWidth(false) / 2; | |
637 BrowserActionButton* draggedButton = [notification object]; | 637 BrowserActionButton* draggedButton = [notification object]; |
638 NSRect draggedButtonFrame = [draggedButton frame]; | 638 NSRect draggedButtonFrame = [draggedButton frame]; |
| 639 // Find the mid-point. We flip the y-coordinates so that y = 0 is at the |
| 640 // top of the container to make row calculation more logical. |
| 641 NSPoint midPoint = |
| 642 NSMakePoint(NSMidX(draggedButtonFrame), |
| 643 NSMaxY([containerView_ bounds]) - NSMidY(draggedButtonFrame)); |
639 | 644 |
640 NSUInteger index = 0; | 645 // Calculate the row index and the index in the row. We bound the latter |
641 for (BrowserActionButton* button in buttons_.get()) { | 646 // because the view can go farther right than the right-most icon in the last |
642 CGFloat intersectionWidth = | 647 // row of the overflow menu. |
643 NSWidth(NSIntersectionRect(draggedButtonFrame, [button frame])); | 648 NSInteger rowIndex = midPoint.y / ToolbarActionsBar::IconHeight(); |
| 649 int icons_per_row = isOverflow_ ? |
| 650 toolbarActionsBar_->platform_settings().icons_per_overflow_menu_row : |
| 651 toolbarActionsBar_->GetIconCount(); |
| 652 NSInteger indexInRow = std::min(icons_per_row - 1, |
| 653 static_cast<int>(midPoint.x / ToolbarActionsBar::IconWidth(true))); |
644 | 654 |
645 NSUInteger maxIndex = | 655 // Find the desired index for the button. |
646 isOverflow_ ? [buttons_ count] : [self visibleButtonCount]; | 656 NSInteger maxIndex = [buttons_ count] - 1; |
647 if (intersectionWidth > dragThreshold && button != draggedButton && | 657 NSInteger offset = isOverflow_ ? |
648 ![button isAnimating] && index < maxIndex) { | 658 [buttons_ count] - toolbarActionsBar_->GetIconCount() : 0; |
649 toolbarActionsBar_->OnDragDrop( | 659 NSInteger index = |
650 [buttons_ indexOfObject:draggedButton], | 660 std::min(maxIndex, offset + rowIndex * icons_per_row + indexInRow); |
651 index, | 661 |
652 ToolbarActionsBar::DRAG_TO_SAME); | 662 toolbarActionsBar_->OnDragDrop([buttons_ indexOfObject:draggedButton], |
653 return; | 663 index, |
654 } | 664 ToolbarActionsBar::DRAG_TO_SAME); |
655 ++index; | |
656 } | |
657 } | 665 } |
658 | 666 |
659 - (void)actionButtonDragFinished:(NSNotification*)notification { | 667 - (void)actionButtonDragFinished:(NSNotification*)notification { |
660 suppressChevron_ = NO; | 668 suppressChevron_ = NO; |
661 [self redraw]; | 669 [self redraw]; |
662 } | 670 } |
663 | 671 |
664 - (NSRect)frameForIndex:(NSUInteger)index { | 672 - (NSRect)frameForIndex:(NSUInteger)index { |
665 const ToolbarActionsBar::PlatformSettings& platformSettings = | 673 const ToolbarActionsBar::PlatformSettings& platformSettings = |
666 toolbarActionsBar_->platform_settings(); | 674 toolbarActionsBar_->platform_settings(); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 - (ToolbarActionsBar*)toolbarActionsBar { | 811 - (ToolbarActionsBar*)toolbarActionsBar { |
804 return toolbarActionsBar_.get(); | 812 return toolbarActionsBar_.get(); |
805 } | 813 } |
806 | 814 |
807 + (BrowserActionsController*)fromToolbarActionsBarDelegate: | 815 + (BrowserActionsController*)fromToolbarActionsBarDelegate: |
808 (ToolbarActionsBarDelegate*)delegate { | 816 (ToolbarActionsBarDelegate*)delegate { |
809 return static_cast<ToolbarActionsBarBridge*>(delegate)->controller_for_test(); | 817 return static_cast<ToolbarActionsBarBridge*>(delegate)->controller_for_test(); |
810 } | 818 } |
811 | 819 |
812 @end | 820 @end |
OLD | NEW |