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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
14 #include "chrome/browser/extensions/extension_message_bubble_controller.h" | 14 #include "chrome/browser/extensions/extension_message_bubble_controller.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
17 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 17 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
18 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" | 18 #import "chrome/browser/ui/cocoa/extensions/browser_action_button.h" |
19 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" | 19 #import "chrome/browser/ui/cocoa/extensions/browser_actions_container_view.h" |
20 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" | 20 #import "chrome/browser/ui/cocoa/extensions/extension_popup_controller.h" |
21 #import "chrome/browser/ui/cocoa/extensions/toolbar_actions_bar_bubble_mac.h" | 21 #import "chrome/browser/ui/cocoa/extensions/toolbar_actions_bar_bubble_mac.h" |
22 #import "chrome/browser/ui/cocoa/extensions/toolbar_actions_bar_bubble_views_pre senter.h" | |
22 #import "chrome/browser/ui/cocoa/image_button_cell.h" | 23 #import "chrome/browser/ui/cocoa/image_button_cell.h" |
23 #import "chrome/browser/ui/cocoa/l10n_util.h" | 24 #import "chrome/browser/ui/cocoa/l10n_util.h" |
24 #import "chrome/browser/ui/cocoa/menu_button.h" | 25 #import "chrome/browser/ui/cocoa/menu_button.h" |
25 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" | 26 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
26 #include "chrome/browser/ui/extensions/extension_message_bubble_bridge.h" | 27 #include "chrome/browser/ui/extensions/extension_message_bubble_bridge.h" |
27 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
28 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" | 29 #include "chrome/browser/ui/toolbar/toolbar_action_view_controller.h" |
29 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 30 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
30 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_delegate.h" | 31 #include "chrome/browser/ui/toolbar/toolbar_actions_bar_delegate.h" |
31 #include "chrome/grit/theme_resources.h" | 32 #include "chrome/grit/theme_resources.h" |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 // Handles clicks for BrowserActionButtons. | 136 // Handles clicks for BrowserActionButtons. |
136 - (BOOL)browserActionClicked:(BrowserActionButton*)button; | 137 - (BOOL)browserActionClicked:(BrowserActionButton*)button; |
137 | 138 |
138 // Returns the associated ToolbarController. | 139 // Returns the associated ToolbarController. |
139 - (ToolbarController*)toolbarController; | 140 - (ToolbarController*)toolbarController; |
140 | 141 |
141 // Creates a message bubble with the given |delegate|. | 142 // Creates a message bubble with the given |delegate|. |
142 - (void)createMessageBubble: | 143 - (void)createMessageBubble: |
143 (std::unique_ptr<ToolbarActionsBarBubbleDelegate>)delegate; | 144 (std::unique_ptr<ToolbarActionsBarBubbleDelegate>)delegate; |
144 | 145 |
145 // Called when the window for the active bubble is closing, and sets the active | |
146 // bubble to nil. | |
147 - (void)bubbleWindowClosing:(NSNotification*)notification; | |
148 | |
149 // Sets the current focused view. Should only be used for the overflow | 146 // Sets the current focused view. Should only be used for the overflow |
150 // container. | 147 // container. |
151 - (void)setFocusedViewIndex:(NSInteger)index; | 148 - (void)setFocusedViewIndex:(NSInteger)index; |
152 | 149 |
153 @end | 150 @end |
154 | 151 |
155 namespace { | 152 namespace { |
156 | 153 |
157 // A bridge between the ToolbarActionsBar and the BrowserActionsController. | 154 // A bridge between the ToolbarActionsBar and the BrowserActionsController. |
158 class ToolbarActionsBarBridge : public ToolbarActionsBarDelegate { | 155 class ToolbarActionsBarBridge : public ToolbarActionsBarDelegate { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 browser_ = browser; | 261 browser_ = browser; |
265 isOverflow_ = mainController != nil; | 262 isOverflow_ = mainController != nil; |
266 | 263 |
267 toolbarActionsBarBridge_.reset(new ToolbarActionsBarBridge(self)); | 264 toolbarActionsBarBridge_.reset(new ToolbarActionsBarBridge(self)); |
268 ToolbarActionsBar* mainBar = | 265 ToolbarActionsBar* mainBar = |
269 mainController ? [mainController toolbarActionsBar] : nullptr; | 266 mainController ? [mainController toolbarActionsBar] : nullptr; |
270 toolbarActionsBar_.reset( | 267 toolbarActionsBar_.reset( |
271 new ToolbarActionsBar(toolbarActionsBarBridge_.get(), | 268 new ToolbarActionsBar(toolbarActionsBarBridge_.get(), |
272 browser_, | 269 browser_, |
273 mainBar)); | 270 mainBar)); |
271 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { | |
272 viewsBubblePresenter_ = | |
273 base::MakeUnique<ToolbarActionsBarBubbleViewsPresenter>(self); | |
274 } | |
274 | 275 |
275 containerView_ = container; | 276 containerView_ = container; |
276 [containerView_ setMinWidth:toolbarActionsBar_->GetMinimumWidth()]; | 277 [containerView_ setMinWidth:toolbarActionsBar_->GetMinimumWidth()]; |
277 [containerView_ setPostsFrameChangedNotifications:YES]; | 278 [containerView_ setPostsFrameChangedNotifications:YES]; |
278 [[NSNotificationCenter defaultCenter] | 279 [[NSNotificationCenter defaultCenter] |
279 addObserver:self | 280 addObserver:self |
280 selector:@selector(containerFrameChanged:) | 281 selector:@selector(containerFrameChanged:) |
281 name:NSViewFrameDidChangeNotification | 282 name:NSViewFrameDidChangeNotification |
282 object:containerView_]; | 283 object:containerView_]; |
283 [[NSNotificationCenter defaultCenter] | 284 [[NSNotificationCenter defaultCenter] |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
396 [buttons_ count] - toolbarActionsBar_->GetIconCount() : -1; | 397 [buttons_ count] - toolbarActionsBar_->GetIconCount() : -1; |
397 [self setFocusedViewIndex:index]; | 398 [self setFocusedViewIndex:index]; |
398 } | 399 } |
399 } | 400 } |
400 | 401 |
401 - (gfx::Size)sizeForOverflowWidth:(int)maxWidth { | 402 - (gfx::Size)sizeForOverflowWidth:(int)maxWidth { |
402 toolbarActionsBar_->SetOverflowRowWidth(maxWidth); | 403 toolbarActionsBar_->SetOverflowRowWidth(maxWidth); |
403 return [self preferredSize]; | 404 return [self preferredSize]; |
404 } | 405 } |
405 | 406 |
407 - (void)bubbleWindowClosing:(NSNotification*)notification { | |
408 activeBubble_ = nil; | |
409 toolbarActionsBar_->OnBubbleClosed(); | |
410 } | |
411 | |
406 - (void)updateMaxWidth { | 412 - (void)updateMaxWidth { |
407 const CGFloat ownMaxWidth = toolbarActionsBar_->GetMaximumWidth(); | 413 const CGFloat ownMaxWidth = toolbarActionsBar_->GetMaximumWidth(); |
408 containerView_.maxWidth = | 414 containerView_.maxWidth = |
409 (maxWidth_ ? std::min(maxWidth_, ownMaxWidth) : ownMaxWidth); | 415 (maxWidth_ ? std::min(maxWidth_, ownMaxWidth) : ownMaxWidth); |
410 } | 416 } |
411 | 417 |
412 - (void)setMaxWidth:(CGFloat)maxWidth { | 418 - (void)setMaxWidth:(CGFloat)maxWidth { |
413 maxWidth_ = maxWidth; | 419 maxWidth_ = maxWidth; |
414 [self updateMaxWidth]; | 420 [self updateMaxWidth]; |
415 } | 421 } |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
723 // We need to flip the y coordinate for Cocoa's view system. | 729 // We need to flip the y coordinate for Cocoa's view system. |
724 frame.origin.y = NSHeight([containerView_ frame]) - NSMaxY(frame); | 730 frame.origin.y = NSHeight([containerView_ frame]) - NSMaxY(frame); |
725 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) | 731 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) |
726 frame.origin.x = | 732 frame.origin.x = |
727 NSWidth([containerView_ frame]) - NSWidth(frame) - NSMinX(frame); | 733 NSWidth([containerView_ frame]) - NSWidth(frame) - NSMinX(frame); |
728 return frame; | 734 return frame; |
729 } | 735 } |
730 | 736 |
731 - (NSPoint)popupPointForView:(NSView*)view | 737 - (NSPoint)popupPointForView:(NSView*)view |
732 withBounds:(NSRect)bounds { | 738 withBounds:(NSRect)bounds { |
733 // Anchor point just above the center of the bottom. | 739 NSPoint anchor; |
734 int y = [view isFlipped] ? NSMaxY(bounds) - kBrowserActionBubbleYOffset : | 740 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { |
735 kBrowserActionBubbleYOffset; | 741 // Anchor to the bottom-right of the button. |
736 NSPoint anchor = NSMakePoint(NSMidX(bounds), y); | 742 anchor = NSMakePoint(NSMaxX(bounds), [view isFlipped] ? NSMaxY(bounds) : 0); |
743 } else { | |
744 // Anchor point just above the center of the bottom. | |
745 int y = [view isFlipped] ? NSMaxY(bounds) - kBrowserActionBubbleYOffset | |
746 : kBrowserActionBubbleYOffset; | |
747 anchor = NSMakePoint(NSMidX(bounds), y); | |
748 } | |
737 // Convert the point to the container view's frame, and adjust for animation. | 749 // Convert the point to the container view's frame, and adjust for animation. |
738 NSPoint anchorInContainer = | 750 NSPoint anchorInContainer = |
739 [containerView_ convertPoint:anchor fromView:view]; | 751 [containerView_ convertPoint:anchor fromView:view]; |
740 anchorInContainer.x -= NSMinX([containerView_ frame]) - | 752 anchorInContainer.x -= NSMinX([containerView_ frame]) - |
741 NSMinX([containerView_ animationEndFrame]); | 753 NSMinX([containerView_ animationEndFrame]); |
742 | 754 |
743 return [containerView_ convertPoint:anchorInContainer toView:nil]; | 755 return [containerView_ convertPoint:anchorInContainer toView:nil]; |
744 } | 756 } |
745 | 757 |
746 - (void)moveButton:(BrowserActionButton*)button | 758 - (void)moveButton:(BrowserActionButton*)button |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
799 anchorView = [[self toolbarController] appMenuButton]; | 811 anchorView = [[self toolbarController] appMenuButton]; |
800 } | 812 } |
801 } else { | 813 } else { |
802 anchorView = containerView_; | 814 anchorView = containerView_; |
803 } | 815 } |
804 | 816 |
805 DCHECK_GE([buttons_ count], 0u); | 817 DCHECK_GE([buttons_ count], 0u); |
806 NSPoint anchor = [self popupPointForView:anchorView | 818 NSPoint anchor = [self popupPointForView:anchorView |
807 withBounds:[anchorView bounds]]; | 819 withBounds:[anchorView bounds]]; |
808 | 820 |
809 anchor = ui::ConvertPointFromWindowToScreen([containerView_ window], anchor); | 821 NSWindow* parentWindow = [containerView_ window]; |
822 anchor = ui::ConvertPointFromWindowToScreen(parentWindow, anchor); | |
823 if (viewsBubblePresenter_) { | |
824 viewsBubblePresenter_->PresentAt(std::move(delegate), parentWindow, anchor, | |
825 anchoredToAction); | |
826 // No need to set |activeBubble_| as it's only used for Cocoa tests. | |
Devlin
2017/03/09 02:49:48
Maybe also comment that bubbleWindowClosing will b
tapted
2017/03/09 10:24:15
Done.
| |
827 return; | |
828 } | |
829 | |
810 activeBubble_ = [[ToolbarActionsBarBubbleMac alloc] | 830 activeBubble_ = [[ToolbarActionsBarBubbleMac alloc] |
811 initWithParentWindow:[containerView_ window] | 831 initWithParentWindow:[containerView_ window] |
812 anchorPoint:anchor | 832 anchorPoint:anchor |
813 anchoredToAction:anchoredToAction | 833 anchoredToAction:anchoredToAction |
814 delegate:std::move(delegate)]; | 834 delegate:std::move(delegate)]; |
815 [[NSNotificationCenter defaultCenter] | 835 [[NSNotificationCenter defaultCenter] |
816 addObserver:self | 836 addObserver:self |
817 selector:@selector(bubbleWindowClosing:) | 837 selector:@selector(bubbleWindowClosing:) |
818 name:NSWindowWillCloseNotification | 838 name:NSWindowWillCloseNotification |
819 object:[activeBubble_ window]]; | 839 object:[activeBubble_ window]]; |
820 [activeBubble_ showWindow:nil]; | 840 [activeBubble_ showWindow:nil]; |
821 } | 841 } |
822 | 842 |
823 - (void)bubbleWindowClosing:(NSNotification*)notification { | |
824 activeBubble_ = nil; | |
825 toolbarActionsBar_->OnBubbleClosed(); | |
826 } | |
827 | |
828 - (void)setFocusedViewIndex:(NSInteger)index { | 843 - (void)setFocusedViewIndex:(NSInteger)index { |
829 DCHECK(isOverflow_); | 844 DCHECK(isOverflow_); |
830 focusedViewIndex_ = index; | 845 focusedViewIndex_ = index; |
831 } | 846 } |
832 | 847 |
833 #pragma mark - | 848 #pragma mark - |
834 #pragma mark Testing Methods | 849 #pragma mark Testing Methods |
835 | 850 |
836 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index { | 851 - (BrowserActionButton*)buttonWithIndex:(NSUInteger)index { |
837 return index < [buttons_ count] ? [buttons_ objectAtIndex:index] : nil; | 852 return index < [buttons_ count] ? [buttons_ objectAtIndex:index] : nil; |
838 } | 853 } |
839 | 854 |
840 @end | 855 @end |
OLD | NEW |