| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "ios/chrome/browser/ui/tools_menu/tools_popup_controller.h" | 5 #import "ios/chrome/browser/ui/tools_menu/tools_popup_controller.h" |
| 6 | 6 |
| 7 #import <QuartzCore/QuartzCore.h> | 7 #import <QuartzCore/QuartzCore.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/metrics/user_metrics.h" | 11 #include "base/metrics/user_metrics.h" |
| 12 #include "base/metrics/user_metrics_action.h" | 12 #include "base/metrics/user_metrics_action.h" |
| 13 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" | 13 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 14 #include "ios/chrome/browser/ui/rtl_geometry.h" | 14 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 15 #import "ios/chrome/browser/ui/tools_menu/tools_menu_context.h" | 15 #import "ios/chrome/browser/ui/tools_menu/tools_menu_configuration.h" |
| 16 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" | 16 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" |
| 17 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 17 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 18 | 18 |
| 19 using base::UserMetricsAction; | 19 using base::UserMetricsAction; |
| 20 | 20 |
| 21 NSString* const kToolsMenuTableViewId = @"kToolsMenuTableViewId"; | 21 NSString* const kToolsMenuTableViewId = @"kToolsMenuTableViewId"; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const CGFloat kToolsPopupMenuWidth = 280.0; | 25 const CGFloat kToolsPopupMenuWidth = 280.0; |
| 26 const CGFloat kToolsPopupMenuTrailingOffset = 4; | 26 const CGFloat kToolsPopupMenuTrailingOffset = 4; |
| 27 | 27 |
| 28 // Inset for the shadows of the contained views. | 28 // Inset for the shadows of the contained views. |
| 29 NS_INLINE UIEdgeInsets TabHistoryPopupMenuInsets() { | 29 NS_INLINE UIEdgeInsets TabHistoryPopupMenuInsets() { |
| 30 return UIEdgeInsetsMake(9, 11, 12, 11); | 30 return UIEdgeInsetsMake(9, 11, 12, 11); |
| 31 } | 31 } |
| 32 | 32 |
| 33 } // namespace | 33 } // namespace |
| 34 | 34 |
| 35 @interface ToolsPopupController ()<ToolsPopupTableDelegate> { | 35 @interface ToolsPopupController ()<ToolsPopupTableDelegate> { |
| 36 base::scoped_nsobject<ToolsMenuViewController> _toolsMenuViewController; | 36 base::scoped_nsobject<ToolsMenuViewController> _toolsMenuViewController; |
| 37 // Container view of the menu items table. | 37 // Container view of the menu items table. |
| 38 base::scoped_nsobject<UIView> _toolsTableViewContainer; | 38 base::scoped_nsobject<UIView> _toolsTableViewContainer; |
| 39 } | 39 } |
| 40 @end | 40 @end |
| 41 | 41 |
| 42 @implementation ToolsPopupController | 42 @implementation ToolsPopupController |
| 43 @synthesize isCurrentPageBookmarked = _isCurrentPageBookmarked; | 43 @synthesize isCurrentPageBookmarked = _isCurrentPageBookmarked; |
| 44 | 44 |
| 45 - (instancetype)initWithContext:(ToolsMenuContext*)context { | 45 - (instancetype)initWithConfiguration:(ToolsMenuConfiguration*)configuration { |
| 46 DCHECK(context.displayView); | 46 DCHECK(configuration.displayView); |
| 47 self = [super initWithParentView:context.displayView]; | 47 self = [super initWithParentView:configuration.displayView]; |
| 48 if (self) { | 48 if (self) { |
| 49 _toolsMenuViewController.reset([[ToolsMenuViewController alloc] init]); | 49 _toolsMenuViewController.reset([[ToolsMenuViewController alloc] init]); |
| 50 _toolsTableViewContainer.reset([[_toolsMenuViewController view] retain]); | 50 _toolsTableViewContainer.reset([[_toolsMenuViewController view] retain]); |
| 51 [_toolsTableViewContainer layer].cornerRadius = 2; | 51 [_toolsTableViewContainer layer].cornerRadius = 2; |
| 52 [_toolsTableViewContainer layer].masksToBounds = YES; | 52 [_toolsTableViewContainer layer].masksToBounds = YES; |
| 53 [_toolsMenuViewController initializeMenu:context]; | 53 [_toolsMenuViewController initializeMenuWithConfiguration:configuration]; |
| 54 | 54 |
| 55 UIEdgeInsets popupInsets = TabHistoryPopupMenuInsets(); | 55 UIEdgeInsets popupInsets = TabHistoryPopupMenuInsets(); |
| 56 CGFloat popupWidth = kToolsPopupMenuWidth; | 56 CGFloat popupWidth = kToolsPopupMenuWidth; |
| 57 | 57 |
| 58 CGPoint origin = CGPointMake(CGRectGetMidX(context.sourceRect), | 58 CGPoint origin = CGPointMake(CGRectGetMidX(configuration.sourceRect), |
| 59 CGRectGetMidY(context.sourceRect)); | 59 CGRectGetMidY(configuration.sourceRect)); |
| 60 | 60 |
| 61 CGRect containerBounds = [context.displayView bounds]; | 61 CGRect containerBounds = [configuration.displayView bounds]; |
| 62 CGFloat minY = CGRectGetMinY(context.sourceRect) - popupInsets.top; | 62 CGFloat minY = CGRectGetMinY(configuration.sourceRect) - popupInsets.top; |
| 63 | 63 |
| 64 // The tools popup appears trailing- aligned, but because | 64 // The tools popup appears trailing- aligned, but because |
| 65 // kToolsPopupMenuTrailingOffset is smaller than the popupInsets's trailing | 65 // kToolsPopupMenuTrailingOffset is smaller than the popupInsets's trailing |
| 66 // value, destination needs to be shifted a bit. | 66 // value, destination needs to be shifted a bit. |
| 67 CGFloat trailingShift = | 67 CGFloat trailingShift = |
| 68 UIEdgeInsetsGetTrailing(popupInsets) - kToolsPopupMenuTrailingOffset; | 68 UIEdgeInsetsGetTrailing(popupInsets) - kToolsPopupMenuTrailingOffset; |
| 69 if (UseRTLLayout()) | 69 if (UseRTLLayout()) |
| 70 trailingShift = -trailingShift; | 70 trailingShift = -trailingShift; |
| 71 | 71 |
| 72 CGPoint destination = CGPointMake( | 72 CGPoint destination = CGPointMake( |
| 73 CGRectGetTrailingEdge(containerBounds) + trailingShift, minY); | 73 CGRectGetTrailingEdge(containerBounds) + trailingShift, minY); |
| 74 | 74 |
| 75 CGFloat availableHeight = CGRectGetHeight([context.displayView bounds]) - | 75 CGFloat availableHeight = |
| 76 minY - popupInsets.bottom; | 76 CGRectGetHeight([configuration.displayView bounds]) - minY - |
| 77 popupInsets.bottom; |
| 77 CGFloat optimalHeight = | 78 CGFloat optimalHeight = |
| 78 [_toolsMenuViewController optimalHeight:availableHeight]; | 79 [_toolsMenuViewController optimalHeight:availableHeight]; |
| 79 [self setOptimalSize:CGSizeMake(popupWidth, optimalHeight) | 80 [self setOptimalSize:CGSizeMake(popupWidth, optimalHeight) |
| 80 atOrigin:destination]; | 81 atOrigin:destination]; |
| 81 | 82 |
| 82 CGRect bounds = [[self popupContainer] bounds]; | 83 CGRect bounds = [[self popupContainer] bounds]; |
| 83 CGRect frame = UIEdgeInsetsInsetRect(bounds, popupInsets); | 84 CGRect frame = UIEdgeInsetsInsetRect(bounds, popupInsets); |
| 84 | 85 |
| 85 [_toolsTableViewContainer setFrame:frame]; | 86 [_toolsTableViewContainer setFrame:frame]; |
| 86 [[self popupContainer] addSubview:_toolsTableViewContainer]; | 87 [[self popupContainer] addSubview:_toolsTableViewContainer]; |
| 87 | 88 |
| 88 [_toolsMenuViewController setDelegate:self]; | 89 [_toolsMenuViewController setDelegate:self]; |
| 89 [self fadeInPopupFromSource:origin toDestination:destination]; | 90 [self fadeInPopupFromSource:origin toDestination:destination]; |
| 90 | 91 |
| 91 // Insert |toolsButton| above |popupContainer| so it appears stationary. | 92 // Insert |toolsButton| above |popupContainer| so it appears stationary. |
| 92 // Otherwise the tools button will animate with the tools popup. | 93 // Otherwise the tools button will animate with the tools popup. |
| 93 UIButton* toolsButton = [_toolsMenuViewController toolsButton]; | 94 UIButton* toolsButton = [_toolsMenuViewController toolsButton]; |
| 94 if (toolsButton) { | 95 if (toolsButton) { |
| 95 UIView* outsideAnimationView = [[self popupContainer] superview]; | 96 UIView* outsideAnimationView = [[self popupContainer] superview]; |
| 96 const CGFloat buttonWidth = 48; | 97 const CGFloat buttonWidth = 48; |
| 97 // |origin| is the center of the tools menu icon in the toolbar; use | 98 // |origin| is the center of the tools menu icon in the toolbar; use |
| 98 // that to determine where the tools button should be placed. | 99 // that to determine where the tools button should be placed. |
| 99 CGPoint buttonCenter = | 100 CGPoint buttonCenter = |
| 100 [context.displayView convertPoint:origin toView:outsideAnimationView]; | 101 [configuration.displayView convertPoint:origin |
| 102 toView:outsideAnimationView]; |
| 101 CGRect frame = CGRectMake(buttonCenter.x - buttonWidth / 2.0, | 103 CGRect frame = CGRectMake(buttonCenter.x - buttonWidth / 2.0, |
| 102 buttonCenter.y - buttonWidth / 2.0, buttonWidth, | 104 buttonCenter.y - buttonWidth / 2.0, buttonWidth, |
| 103 buttonWidth); | 105 buttonWidth); |
| 104 [toolsButton setFrame:frame]; | 106 [toolsButton setFrame:frame]; |
| 105 [toolsButton setImageEdgeInsets:context.toolsButtonInsets]; | 107 [toolsButton setImageEdgeInsets:configuration.toolsButtonInsets]; |
| 106 [outsideAnimationView addSubview:toolsButton]; | 108 [outsideAnimationView addSubview:toolsButton]; |
| 107 } | 109 } |
| 108 } | 110 } |
| 109 return self; | 111 return self; |
| 110 } | 112 } |
| 111 | 113 |
| 112 - (void)dealloc { | 114 - (void)dealloc { |
| 113 [_toolsTableViewContainer removeFromSuperview]; | 115 [_toolsTableViewContainer removeFromSuperview]; |
| 114 [_toolsMenuViewController setDelegate:nil]; | 116 [_toolsMenuViewController setDelegate:nil]; |
| 115 [super dealloc]; | 117 [super dealloc]; |
| 116 } | 118 } |
| 117 | 119 |
| 118 - (void)fadeInPopupFromSource:(CGPoint)source | 120 - (void)fadeInPopupFromSource:(CGPoint)source |
| 119 toDestination:(CGPoint)destination { | 121 toDestination:(CGPoint)destination { |
| 120 [_toolsMenuViewController animateContentIn]; | 122 [_toolsMenuViewController animateContentIn]; |
| 121 [super fadeInPopupFromSource:source toDestination:destination]; | 123 [super fadeInPopupFromSource:source toDestination:destination]; |
| 122 } | 124 } |
| 123 | 125 |
| 124 - (void)dismissAnimatedWithCompletion:(void (^)(void))completion { | 126 - (void)dismissAnimatedWithCompletion:(void (^)(void))completion { |
| 125 [_toolsMenuViewController hideContent]; | 127 [_toolsMenuViewController hideContent]; |
| 126 [super dismissAnimatedWithCompletion:completion]; | 128 [super dismissAnimatedWithCompletion:completion]; |
| 127 } | 129 } |
| 128 | 130 |
| 129 - (void)setIsCurrentPageBookmarked:(BOOL)value { | 131 - (void)setIsCurrentPageBookmarked:(BOOL)value { |
| 130 _isCurrentPageBookmarked = value; | 132 _isCurrentPageBookmarked = value; |
| 131 [_toolsMenuViewController setIsCurrentPageBookmarked:value]; | 133 [_toolsMenuViewController setIsCurrentPageBookmarked:value]; |
| 132 } | 134 } |
| 133 | 135 |
| 134 // Informs tools popup menu whether the switch to reader mode is possible. | |
| 135 - (void)setCanUseReaderMode:(BOOL)enabled { | 136 - (void)setCanUseReaderMode:(BOOL)enabled { |
| 136 [_toolsMenuViewController setCanUseReaderMode:enabled]; | 137 [_toolsMenuViewController setCanUseReaderMode:enabled]; |
| 137 } | 138 } |
| 138 | 139 |
| 139 - (void)setCanUseDesktopUserAgent:(BOOL)enabled { | |
| 140 [_toolsMenuViewController setCanUseDesktopUserAgent:enabled]; | |
| 141 } | |
| 142 | |
| 143 - (void)setCanShowFindBar:(BOOL)enabled { | 140 - (void)setCanShowFindBar:(BOOL)enabled { |
| 144 [_toolsMenuViewController setCanShowFindBar:enabled]; | 141 [_toolsMenuViewController setCanShowFindBar:enabled]; |
| 145 } | 142 } |
| 146 | 143 |
| 147 - (void)setCanShowShareMenu:(BOOL)enabled { | 144 - (void)setCanShowShareMenu:(BOOL)enabled { |
| 148 [_toolsMenuViewController setCanShowShareMenu:enabled]; | 145 [_toolsMenuViewController setCanShowShareMenu:enabled]; |
| 149 } | 146 } |
| 150 | 147 |
| 151 - (void)setIsTabLoading:(BOOL)isTabLoading { | 148 - (void)setIsTabLoading:(BOOL)isTabLoading { |
| 152 [_toolsMenuViewController setIsTabLoading:isTabLoading]; | 149 [_toolsMenuViewController setIsTabLoading:isTabLoading]; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 break; | 183 break; |
| 187 case IDC_RELOAD: | 184 case IDC_RELOAD: |
| 188 base::RecordAction(UserMetricsAction("MobileMenuReload")); | 185 base::RecordAction(UserMetricsAction("MobileMenuReload")); |
| 189 break; | 186 break; |
| 190 case IDC_SHARE_PAGE: | 187 case IDC_SHARE_PAGE: |
| 191 base::RecordAction(UserMetricsAction("MobileMenuShare")); | 188 base::RecordAction(UserMetricsAction("MobileMenuShare")); |
| 192 break; | 189 break; |
| 193 case IDC_REQUEST_DESKTOP_SITE: | 190 case IDC_REQUEST_DESKTOP_SITE: |
| 194 base::RecordAction(UserMetricsAction("MobileMenuRequestDesktopSite")); | 191 base::RecordAction(UserMetricsAction("MobileMenuRequestDesktopSite")); |
| 195 break; | 192 break; |
| 193 case IDC_REQUEST_MOBILE_SITE: |
| 194 base::RecordAction(UserMetricsAction("MobileMenuRequestMobileSite")); |
| 195 break; |
| 196 case IDC_READER_MODE: | 196 case IDC_READER_MODE: |
| 197 base::RecordAction(UserMetricsAction("MobileMenuRequestReaderMode")); | 197 base::RecordAction(UserMetricsAction("MobileMenuRequestReaderMode")); |
| 198 break; | 198 break; |
| 199 case IDC_SHOW_BOOKMARK_MANAGER: | 199 case IDC_SHOW_BOOKMARK_MANAGER: |
| 200 base::RecordAction(UserMetricsAction("MobileMenuAllBookmarks")); | 200 base::RecordAction(UserMetricsAction("MobileMenuAllBookmarks")); |
| 201 break; | 201 break; |
| 202 case IDC_SHOW_HISTORY: | 202 case IDC_SHOW_HISTORY: |
| 203 base::RecordAction(UserMetricsAction("MobileMenuHistory")); | 203 base::RecordAction(UserMetricsAction("MobileMenuHistory")); |
| 204 break; | 204 break; |
| 205 case IDC_SHOW_OTHER_DEVICES: | 205 case IDC_SHOW_OTHER_DEVICES: |
| (...skipping 24 matching lines...) Expand all Loading... |
| 230 default: | 230 default: |
| 231 NOTREACHED(); | 231 NOTREACHED(); |
| 232 break; | 232 break; |
| 233 } | 233 } |
| 234 | 234 |
| 235 // Close the menu. | 235 // Close the menu. |
| 236 [self.delegate dismissPopupMenu:self]; | 236 [self.delegate dismissPopupMenu:self]; |
| 237 } | 237 } |
| 238 | 238 |
| 239 @end | 239 @end |
| OLD | NEW |