OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/tools_menu/tools_popup_controller.h" |
| 6 |
| 7 #import <QuartzCore/QuartzCore.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/metrics/user_metrics.h" |
| 12 #include "base/metrics/user_metrics_action.h" |
| 13 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 14 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 15 #import "ios/chrome/browser/ui/tools_menu/tools_menu_context.h" |
| 16 #import "ios/chrome/browser/ui/tools_menu/tools_menu_view_controller.h" |
| 17 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 18 |
| 19 using base::UserMetricsAction; |
| 20 |
| 21 NSString* const kToolsMenuTableViewId = @"kToolsMenuTableViewId"; |
| 22 |
| 23 namespace { |
| 24 |
| 25 const CGFloat kToolsPopupMenuWidth = 280.0; |
| 26 const CGFloat kToolsPopupMenuTrailingOffset = 4; |
| 27 |
| 28 // Inset for the shadows of the contained views. |
| 29 NS_INLINE UIEdgeInsets TabHistoryPopupMenuInsets() { |
| 30 return UIEdgeInsetsMake(9, 11, 12, 11); |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 @interface ToolsPopupController ()<ToolsPopupTableDelegate> { |
| 36 base::scoped_nsobject<ToolsMenuViewController> _toolsMenuViewController; |
| 37 // Container view of the menu items table. |
| 38 base::scoped_nsobject<UIView> _toolsTableViewContainer; |
| 39 } |
| 40 @end |
| 41 |
| 42 @implementation ToolsPopupController |
| 43 @synthesize isCurrentPageBookmarked = _isCurrentPageBookmarked; |
| 44 |
| 45 - (instancetype)initWithContext:(ToolsMenuContext*)context { |
| 46 DCHECK(context.displayView); |
| 47 self = [super initWithParentView:context.displayView]; |
| 48 if (self) { |
| 49 _toolsMenuViewController.reset([[ToolsMenuViewController alloc] init]); |
| 50 _toolsTableViewContainer.reset([[_toolsMenuViewController view] retain]); |
| 51 [_toolsTableViewContainer layer].cornerRadius = 2; |
| 52 [_toolsTableViewContainer layer].masksToBounds = YES; |
| 53 [_toolsMenuViewController initializeMenu:context]; |
| 54 |
| 55 UIEdgeInsets popupInsets = TabHistoryPopupMenuInsets(); |
| 56 CGFloat popupWidth = kToolsPopupMenuWidth; |
| 57 |
| 58 CGPoint origin = CGPointMake(CGRectGetMidX(context.sourceRect), |
| 59 CGRectGetMidY(context.sourceRect)); |
| 60 |
| 61 CGRect containerBounds = [context.displayView bounds]; |
| 62 CGFloat minY = CGRectGetMinY(context.sourceRect) - popupInsets.top; |
| 63 |
| 64 // The tools popup appears trailing- aligned, but because |
| 65 // kToolsPopupMenuTrailingOffset is smaller than the popupInsets's trailing |
| 66 // value, destination needs to be shifted a bit. |
| 67 CGFloat trailingShift = |
| 68 UIEdgeInsetsGetTrailing(popupInsets) - kToolsPopupMenuTrailingOffset; |
| 69 if (UseRTLLayout()) |
| 70 trailingShift = -trailingShift; |
| 71 |
| 72 CGPoint destination = CGPointMake( |
| 73 CGRectGetTrailingEdge(containerBounds) + trailingShift, minY); |
| 74 |
| 75 CGFloat availableHeight = CGRectGetHeight([context.displayView bounds]) - |
| 76 minY - popupInsets.bottom; |
| 77 CGFloat optimalHeight = |
| 78 [_toolsMenuViewController optimalHeight:availableHeight]; |
| 79 [self setOptimalSize:CGSizeMake(popupWidth, optimalHeight) |
| 80 atOrigin:destination]; |
| 81 |
| 82 CGRect bounds = [[self popupContainer] bounds]; |
| 83 CGRect frame = UIEdgeInsetsInsetRect(bounds, popupInsets); |
| 84 |
| 85 [_toolsTableViewContainer setFrame:frame]; |
| 86 [[self popupContainer] addSubview:_toolsTableViewContainer]; |
| 87 |
| 88 [_toolsMenuViewController setDelegate:self]; |
| 89 [self fadeInPopupFromSource:origin toDestination:destination]; |
| 90 |
| 91 // Insert |toolsButton| above |popupContainer| so it appears stationary. |
| 92 // Otherwise the tools button will animate with the tools popup. |
| 93 UIButton* toolsButton = [_toolsMenuViewController toolsButton]; |
| 94 if (toolsButton) { |
| 95 UIView* outsideAnimationView = [[self popupContainer] superview]; |
| 96 const CGFloat buttonWidth = 48; |
| 97 // |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 CGPoint buttonCenter = |
| 100 [context.displayView convertPoint:origin toView:outsideAnimationView]; |
| 101 CGRect frame = CGRectMake(buttonCenter.x - buttonWidth / 2.0, |
| 102 buttonCenter.y - buttonWidth / 2.0, buttonWidth, |
| 103 buttonWidth); |
| 104 [toolsButton setFrame:frame]; |
| 105 [toolsButton setImageEdgeInsets:context.toolsButtonInsets]; |
| 106 [outsideAnimationView addSubview:toolsButton]; |
| 107 } |
| 108 } |
| 109 return self; |
| 110 } |
| 111 |
| 112 - (void)dealloc { |
| 113 [_toolsTableViewContainer removeFromSuperview]; |
| 114 [_toolsMenuViewController setDelegate:nil]; |
| 115 [super dealloc]; |
| 116 } |
| 117 |
| 118 - (void)fadeInPopupFromSource:(CGPoint)source |
| 119 toDestination:(CGPoint)destination { |
| 120 [_toolsMenuViewController animateContentIn]; |
| 121 [super fadeInPopupFromSource:source toDestination:destination]; |
| 122 } |
| 123 |
| 124 - (void)dismissAnimatedWithCompletion:(void (^)(void))completion { |
| 125 [_toolsMenuViewController hideContent]; |
| 126 [super dismissAnimatedWithCompletion:completion]; |
| 127 } |
| 128 |
| 129 - (void)setIsCurrentPageBookmarked:(BOOL)value { |
| 130 _isCurrentPageBookmarked = value; |
| 131 [_toolsMenuViewController setIsCurrentPageBookmarked:value]; |
| 132 } |
| 133 |
| 134 // Informs tools popup menu whether the switch to reader mode is possible. |
| 135 - (void)setCanUseReaderMode:(BOOL)enabled { |
| 136 [_toolsMenuViewController setCanUseReaderMode:enabled]; |
| 137 } |
| 138 |
| 139 - (void)setCanUseDesktopUserAgent:(BOOL)enabled { |
| 140 [_toolsMenuViewController setCanUseDesktopUserAgent:enabled]; |
| 141 } |
| 142 |
| 143 - (void)setCanShowFindBar:(BOOL)enabled { |
| 144 [_toolsMenuViewController setCanShowFindBar:enabled]; |
| 145 } |
| 146 |
| 147 - (void)setCanShowShareMenu:(BOOL)enabled { |
| 148 [_toolsMenuViewController setCanShowShareMenu:enabled]; |
| 149 } |
| 150 |
| 151 - (void)setIsTabLoading:(BOOL)isTabLoading { |
| 152 [_toolsMenuViewController setIsTabLoading:isTabLoading]; |
| 153 } |
| 154 |
| 155 #pragma mark - ToolsPopupTableDelegate methods |
| 156 |
| 157 - (void)commandWasSelected:(int)commandID { |
| 158 // Record the corresponding metric. |
| 159 switch (commandID) { |
| 160 case IDC_TEMP_EDIT_BOOKMARK: |
| 161 base::RecordAction(UserMetricsAction("MobileMenuEditBookmark")); |
| 162 break; |
| 163 case IDC_BOOKMARK_PAGE: |
| 164 base::RecordAction(UserMetricsAction("MobileMenuAddToBookmarks")); |
| 165 break; |
| 166 case IDC_CLOSE_ALL_TABS: |
| 167 base::RecordAction(UserMetricsAction("MobileMenuCloseAllTabs")); |
| 168 break; |
| 169 case IDC_CLOSE_ALL_INCOGNITO_TABS: |
| 170 base::RecordAction(UserMetricsAction("MobileMenuCloseAllIncognitoTabs")); |
| 171 break; |
| 172 case IDC_FIND: |
| 173 base::RecordAction(UserMetricsAction("MobileMenuFindInPage")); |
| 174 break; |
| 175 case IDC_HELP_PAGE_VIA_MENU: |
| 176 base::RecordAction(UserMetricsAction("MobileMenuHelp")); |
| 177 break; |
| 178 case IDC_NEW_INCOGNITO_TAB: |
| 179 base::RecordAction(UserMetricsAction("MobileMenuNewIncognitoTab")); |
| 180 break; |
| 181 case IDC_NEW_TAB: |
| 182 base::RecordAction(UserMetricsAction("MobileMenuNewTab")); |
| 183 break; |
| 184 case IDC_OPTIONS: |
| 185 base::RecordAction(UserMetricsAction("MobileMenuSettings")); |
| 186 break; |
| 187 case IDC_RELOAD: |
| 188 base::RecordAction(UserMetricsAction("MobileMenuReload")); |
| 189 break; |
| 190 case IDC_SHARE_PAGE: |
| 191 base::RecordAction(UserMetricsAction("MobileMenuShare")); |
| 192 break; |
| 193 case IDC_REQUEST_DESKTOP_SITE: |
| 194 base::RecordAction(UserMetricsAction("MobileMenuRequestDesktopSite")); |
| 195 break; |
| 196 case IDC_READER_MODE: |
| 197 base::RecordAction(UserMetricsAction("MobileMenuRequestReaderMode")); |
| 198 break; |
| 199 case IDC_SHOW_BOOKMARK_MANAGER: |
| 200 base::RecordAction(UserMetricsAction("MobileMenuAllBookmarks")); |
| 201 break; |
| 202 case IDC_SHOW_HISTORY: |
| 203 base::RecordAction(UserMetricsAction("MobileMenuHistory")); |
| 204 break; |
| 205 case IDC_SHOW_OTHER_DEVICES: |
| 206 // "Open Tabs" is the original name of the "Other Devices" menu item. |
| 207 base::RecordAction(UserMetricsAction("MobileMenuOpenTabs")); |
| 208 break; |
| 209 case IDC_STOP: |
| 210 base::RecordAction(UserMetricsAction("MobileMenuStop")); |
| 211 break; |
| 212 case IDC_PRINT: |
| 213 base::RecordAction(UserMetricsAction("MobileMenuPrint")); |
| 214 break; |
| 215 case IDC_REPORT_AN_ISSUE: |
| 216 self.containerView.hidden = YES; |
| 217 base::RecordAction(UserMetricsAction("MobileMenuReportAnIssue")); |
| 218 break; |
| 219 case IDC_VIEW_SOURCE: |
| 220 // Debug only; no metric. |
| 221 break; |
| 222 case IDC_SHOW_TOOLS_MENU: |
| 223 // Do nothing when tapping the tools menu a second time. |
| 224 break; |
| 225 case IDC_SHOW_READING_LIST: |
| 226 // TODO(crbug.com/582957): Add metric here. |
| 227 break; |
| 228 default: |
| 229 NOTREACHED(); |
| 230 break; |
| 231 } |
| 232 |
| 233 // Close the menu. |
| 234 [self.delegate dismissPopupMenu:self]; |
| 235 } |
| 236 |
| 237 @end |
OLD | NEW |