| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/ntp/new_tab_page_toolbar_controller.h" | 5 #import "ios/chrome/browser/ui/ntp/new_tab_page_toolbar_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsobject.h" | |
| 9 #include "base/metrics/user_metrics.h" | 8 #include "base/metrics/user_metrics.h" |
| 10 #include "base/metrics/user_metrics_action.h" | 9 #include "base/metrics/user_metrics_action.h" |
| 11 #include "components/strings/grit/components_strings.h" | 10 #include "components/strings/grit/components_strings.h" |
| 12 #include "components/toolbar/toolbar_model.h" | 11 #include "components/toolbar/toolbar_model.h" |
| 13 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" | 12 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 14 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" | 13 #import "ios/chrome/browser/ui/commands/generic_chrome_command.h" |
| 15 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" | 14 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 16 #import "ios/chrome/browser/ui/ntp/google_landing_data_source.h" | 15 #import "ios/chrome/browser/ui/ntp/google_landing_data_source.h" |
| 17 #import "ios/chrome/browser/ui/rtl_geometry.h" | 16 #import "ios/chrome/browser/ui/rtl_geometry.h" |
| 18 #include "ios/chrome/browser/ui/toolbar/toolbar_resource_macros.h" | 17 #include "ios/chrome/browser/ui/toolbar/toolbar_resource_macros.h" |
| 19 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" | 18 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" |
| 20 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 19 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 22 | 21 |
| 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 23 #error "This file requires ARC support." |
| 24 #endif |
| 25 |
| 23 using base::UserMetricsAction; | 26 using base::UserMetricsAction; |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 27 const CGFloat kButtonYOffset = 4.0; | 30 const CGFloat kButtonYOffset = 4.0; |
| 28 const CGFloat kBackButtonLeading = 0; | 31 const CGFloat kBackButtonLeading = 0; |
| 29 const CGFloat kForwardButtonLeading = 48; | 32 const CGFloat kForwardButtonLeading = 48; |
| 30 const CGFloat kOmniboxFocuserLeading = 96; | 33 const CGFloat kOmniboxFocuserLeading = 96; |
| 31 const CGSize kBackButtonSize = {48, 48}; | 34 const CGSize kBackButtonSize = {48, 48}; |
| 32 const CGSize kForwardButtonSize = {48, 48}; | 35 const CGSize kForwardButtonSize = {48, 48}; |
| 33 const CGSize kOmniboxFocuserSize = {128, 48}; | 36 const CGSize kOmniboxFocuserSize = {128, 48}; |
| 34 | 37 |
| 35 enum { | 38 enum { |
| 36 NTPToolbarButtonNameBack = NumberOfToolbarButtonNames, | 39 NTPToolbarButtonNameBack = NumberOfToolbarButtonNames, |
| 37 NTPToolbarButtonNameForward, | 40 NTPToolbarButtonNameForward, |
| 38 NumberOfNTPToolbarButtonNames, | 41 NumberOfNTPToolbarButtonNames, |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 } // namespace | 44 } // namespace |
| 42 | 45 |
| 43 @interface NewTabPageToolbarController () { | 46 @interface NewTabPageToolbarController () { |
| 44 base::scoped_nsobject<UIButton> _backButton; | 47 UIButton* _backButton; |
| 45 base::scoped_nsobject<UIButton> _forwardButton; | 48 UIButton* _forwardButton; |
| 46 base::scoped_nsobject<UIButton> _omniboxFocuser; | 49 UIButton* _omniboxFocuser; |
| 47 } | 50 } |
| 48 | 51 |
| 49 // |YES| if the google landing toolbar can show the forward arrow. | 52 // |YES| if the google landing toolbar can show the forward arrow. |
| 50 @property(nonatomic, assign) BOOL canGoForward; | 53 @property(nonatomic, assign) BOOL canGoForward; |
| 51 | 54 |
| 52 // |YES| if the google landing toolbar can show the back arrow. | 55 // |YES| if the google landing toolbar can show the back arrow. |
| 53 @property(nonatomic, assign) BOOL canGoBack; | 56 @property(nonatomic, assign) BOOL canGoBack; |
| 54 | 57 |
| 55 @end | 58 @end |
| 56 | 59 |
| 57 @implementation NewTabPageToolbarController | 60 @implementation NewTabPageToolbarController |
| 58 | 61 |
| 59 @synthesize dispatcher = _dispatcher; | 62 @synthesize dispatcher = _dispatcher; |
| 60 @synthesize canGoForward = _canGoForward; | 63 @synthesize canGoForward = _canGoForward; |
| 61 @synthesize canGoBack = _canGoBack; | 64 @synthesize canGoBack = _canGoBack; |
| 62 | 65 |
| 63 - (instancetype)init { | 66 - (instancetype)init { |
| 64 self = [super initWithStyle:ToolbarControllerStyleLightMode]; | 67 self = [super initWithStyle:ToolbarControllerStyleLightMode]; |
| 65 if (self) { | 68 if (self) { |
| 66 [self.backgroundView setHidden:YES]; | 69 [self.backgroundView setHidden:YES]; |
| 67 | 70 |
| 68 CGFloat boundingWidth = self.view.bounds.size.width; | 71 CGFloat boundingWidth = self.view.bounds.size.width; |
| 69 LayoutRect backButtonLayout = | 72 LayoutRect backButtonLayout = |
| 70 LayoutRectMake(kBackButtonLeading, boundingWidth, kButtonYOffset, | 73 LayoutRectMake(kBackButtonLeading, boundingWidth, kButtonYOffset, |
| 71 kBackButtonSize.width, kBackButtonSize.height); | 74 kBackButtonSize.width, kBackButtonSize.height); |
| 72 _backButton.reset( | 75 _backButton = |
| 73 [[UIButton alloc] initWithFrame:LayoutRectGetRect(backButtonLayout)]); | 76 [[UIButton alloc] initWithFrame:LayoutRectGetRect(backButtonLayout)]; |
| 74 [_backButton | 77 [_backButton |
| 75 setAutoresizingMask:UIViewAutoresizingFlexibleTrailingMargin() | | 78 setAutoresizingMask:UIViewAutoresizingFlexibleTrailingMargin() | |
| 76 UIViewAutoresizingFlexibleBottomMargin]; | 79 UIViewAutoresizingFlexibleBottomMargin]; |
| 77 LayoutRect forwardButtonLayout = | 80 LayoutRect forwardButtonLayout = |
| 78 LayoutRectMake(kForwardButtonLeading, boundingWidth, kButtonYOffset, | 81 LayoutRectMake(kForwardButtonLeading, boundingWidth, kButtonYOffset, |
| 79 kForwardButtonSize.width, kForwardButtonSize.height); | 82 kForwardButtonSize.width, kForwardButtonSize.height); |
| 80 _forwardButton.reset([[UIButton alloc] | 83 _forwardButton = |
| 81 initWithFrame:LayoutRectGetRect(forwardButtonLayout)]); | 84 [[UIButton alloc] initWithFrame:LayoutRectGetRect(forwardButtonLayout)]; |
| 82 [_forwardButton | 85 [_forwardButton |
| 83 setAutoresizingMask:UIViewAutoresizingFlexibleTrailingMargin() | | 86 setAutoresizingMask:UIViewAutoresizingFlexibleTrailingMargin() | |
| 84 UIViewAutoresizingFlexibleBottomMargin]; | 87 UIViewAutoresizingFlexibleBottomMargin]; |
| 85 LayoutRect omniboxFocuserLayout = | 88 LayoutRect omniboxFocuserLayout = |
| 86 LayoutRectMake(kOmniboxFocuserLeading, boundingWidth, kButtonYOffset, | 89 LayoutRectMake(kOmniboxFocuserLeading, boundingWidth, kButtonYOffset, |
| 87 kOmniboxFocuserSize.width, kOmniboxFocuserSize.height); | 90 kOmniboxFocuserSize.width, kOmniboxFocuserSize.height); |
| 88 _omniboxFocuser.reset([[UIButton alloc] | 91 _omniboxFocuser = [[UIButton alloc] |
| 89 initWithFrame:LayoutRectGetRect(omniboxFocuserLayout)]); | 92 initWithFrame:LayoutRectGetRect(omniboxFocuserLayout)]; |
| 90 [_omniboxFocuser | 93 [_omniboxFocuser |
| 91 setAccessibilityLabel:l10n_util::GetNSString(IDS_ACCNAME_LOCATION)]; | 94 setAccessibilityLabel:l10n_util::GetNSString(IDS_ACCNAME_LOCATION)]; |
| 92 | 95 |
| 93 [_omniboxFocuser setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | 96 [_omniboxFocuser setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 94 | 97 |
| 95 [self.view addSubview:_backButton]; | 98 [self.view addSubview:_backButton]; |
| 96 [self.view addSubview:_forwardButton]; | 99 [self.view addSubview:_forwardButton]; |
| 97 [self.view addSubview:_omniboxFocuser]; | 100 [self.view addSubview:_omniboxFocuser]; |
| 98 [_backButton setImageEdgeInsets:UIEdgeInsetsMakeDirected(0, 0, 0, -10)]; | 101 [_backButton setImageEdgeInsets:UIEdgeInsetsMakeDirected(0, 0, 0, -10)]; |
| 99 [_forwardButton setImageEdgeInsets:UIEdgeInsetsMakeDirected(0, -7, 0, 0)]; | 102 [_forwardButton setImageEdgeInsets:UIEdgeInsetsMakeDirected(0, -7, 0, 0)]; |
| 100 | 103 |
| 101 // Set up the button images. | 104 // Set up the button images. |
| 102 [self setUpButton:_backButton | 105 [self setUpButton:_backButton |
| 103 withImageEnum:NTPToolbarButtonNameBack | 106 withImageEnum:NTPToolbarButtonNameBack |
| 104 forInitialState:UIControlStateDisabled | 107 forInitialState:UIControlStateDisabled |
| 105 hasDisabledImage:YES | 108 hasDisabledImage:YES |
| 106 synchronously:NO]; | 109 synchronously:NO]; |
| 107 [self setUpButton:_forwardButton | 110 [self setUpButton:_forwardButton |
| 108 withImageEnum:NTPToolbarButtonNameForward | 111 withImageEnum:NTPToolbarButtonNameForward |
| 109 forInitialState:UIControlStateDisabled | 112 forInitialState:UIControlStateDisabled |
| 110 hasDisabledImage:YES | 113 hasDisabledImage:YES |
| 111 synchronously:NO]; | 114 synchronously:NO]; |
| 112 | 115 |
| 113 base::scoped_nsobject<UILongPressGestureRecognizer> backLongPress( | 116 UILongPressGestureRecognizer* backLongPress = |
| 114 [[UILongPressGestureRecognizer alloc] | 117 [[UILongPressGestureRecognizer alloc] |
| 115 initWithTarget:self | 118 initWithTarget:self |
| 116 action:@selector(handleLongPress:)]); | 119 action:@selector(handleLongPress:)]; |
| 117 [_backButton addGestureRecognizer:backLongPress]; | 120 [_backButton addGestureRecognizer:backLongPress]; |
| 118 base::scoped_nsobject<UILongPressGestureRecognizer> forwardLongPress( | 121 UILongPressGestureRecognizer* forwardLongPress = |
| 119 [[UILongPressGestureRecognizer alloc] | 122 [[UILongPressGestureRecognizer alloc] |
| 120 initWithTarget:self | 123 initWithTarget:self |
| 121 action:@selector(handleLongPress:)]); | 124 action:@selector(handleLongPress:)]; |
| 122 [_forwardButton addGestureRecognizer:forwardLongPress]; | 125 [_forwardButton addGestureRecognizer:forwardLongPress]; |
| 123 [_backButton setTag:IDC_BACK]; | 126 [_backButton setTag:IDC_BACK]; |
| 124 [_forwardButton setTag:IDC_FORWARD]; | 127 [_forwardButton setTag:IDC_FORWARD]; |
| 125 | 128 |
| 126 [_omniboxFocuser addTarget:self | 129 [_omniboxFocuser addTarget:self |
| 127 action:@selector(focusOmnibox:) | 130 action:@selector(focusOmnibox:) |
| 128 forControlEvents:UIControlEventTouchUpInside]; | 131 forControlEvents:UIControlEventTouchUpInside]; |
| 129 | 132 |
| 130 SetA11yLabelAndUiAutomationName(_backButton, IDS_ACCNAME_BACK, @"Back"); | 133 SetA11yLabelAndUiAutomationName(_backButton, IDS_ACCNAME_BACK, @"Back"); |
| 131 SetA11yLabelAndUiAutomationName(_forwardButton, IDS_ACCNAME_FORWARD, | 134 SetA11yLabelAndUiAutomationName(_forwardButton, IDS_ACCNAME_FORWARD, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 143 if (imageEnum < NumberOfToolbarButtonNames) | 146 if (imageEnum < NumberOfToolbarButtonNames) |
| 144 return [super imageShouldFlipForRightToLeftLayoutDirection:imageEnum]; | 147 return [super imageShouldFlipForRightToLeftLayoutDirection:imageEnum]; |
| 145 if (imageEnum == NTPToolbarButtonNameBack || | 148 if (imageEnum == NTPToolbarButtonNameBack || |
| 146 imageEnum == NTPToolbarButtonNameForward) { | 149 imageEnum == NTPToolbarButtonNameForward) { |
| 147 return YES; | 150 return YES; |
| 148 } | 151 } |
| 149 return NO; | 152 return NO; |
| 150 } | 153 } |
| 151 | 154 |
| 152 - (int)imageEnumForButton:(UIButton*)button { | 155 - (int)imageEnumForButton:(UIButton*)button { |
| 153 if (button == _backButton.get()) | 156 if (button == _backButton) |
| 154 return NTPToolbarButtonNameBack; | 157 return NTPToolbarButtonNameBack; |
| 155 if (button == _forwardButton.get()) | 158 if (button == _forwardButton) |
| 156 return NTPToolbarButtonNameForward; | 159 return NTPToolbarButtonNameForward; |
| 157 return [super imageEnumForButton:button]; | 160 return [super imageEnumForButton:button]; |
| 158 } | 161 } |
| 159 | 162 |
| 160 - (int)imageIdForImageEnum:(int)index | 163 - (int)imageIdForImageEnum:(int)index |
| 161 style:(ToolbarControllerStyle)style | 164 style:(ToolbarControllerStyle)style |
| 162 forState:(ToolbarButtonUIState)state { | 165 forState:(ToolbarButtonUIState)state { |
| 163 DCHECK(style < ToolbarControllerStyleMaxStyles); | 166 DCHECK(style < ToolbarControllerStyleMaxStyles); |
| 164 DCHECK(state < NumberOfToolbarButtonUIStates); | 167 DCHECK(state < NumberOfToolbarButtonUIStates); |
| 165 | 168 |
| 166 if (index >= NumberOfNTPToolbarButtonNames) | 169 if (index >= NumberOfNTPToolbarButtonNames) |
| 167 NOTREACHED(); | 170 NOTREACHED(); |
| 168 if (index < NumberOfToolbarButtonNames) | 171 if (index < NumberOfToolbarButtonNames) |
| 169 return [super imageIdForImageEnum:index style:style forState:state]; | 172 return [super imageIdForImageEnum:index style:style forState:state]; |
| 170 | 173 |
| 171 index -= NumberOfToolbarButtonNames; | 174 index -= NumberOfToolbarButtonNames; |
| 172 | 175 |
| 173 const int numberOfAddedNames = | 176 const int numberOfAddedNames = |
| 174 NumberOfNTPToolbarButtonNames - NumberOfToolbarButtonNames; | 177 NumberOfNTPToolbarButtonNames - NumberOfToolbarButtonNames; |
| 175 // Name, style [light, dark], UIControlState [normal, pressed, disabled] | 178 // Name, style [light, dark], UIControlState [normal, pressed, disabled] |
| 176 static int | 179 static int |
| 177 buttonImageIds[numberOfAddedNames][2][NumberOfToolbarButtonUIStates] = { | 180 buttonImageIds[numberOfAddedNames][2][NumberOfToolbarButtonUIStates] = { |
| 178 TOOLBAR_IDR_THREE_STATE(BACK), TOOLBAR_IDR_THREE_STATE(FORWARD), | 181 TOOLBAR_IDR_THREE_STATE(BACK), TOOLBAR_IDR_THREE_STATE(FORWARD), |
| 179 }; | 182 }; |
| 180 return buttonImageIds[index][style][state]; | 183 return buttonImageIds[index][style][state]; |
| 181 } | 184 } |
| 182 | 185 |
| 183 - (IBAction)recordUserMetrics:(id)sender { | 186 - (IBAction)recordUserMetrics:(id)sender { |
| 184 if (sender == _backButton.get()) { | 187 if (sender == _backButton) { |
| 185 base::RecordAction(UserMetricsAction("MobileToolbarBack")); | 188 base::RecordAction(UserMetricsAction("MobileToolbarBack")); |
| 186 } else if (sender == _forwardButton.get()) { | 189 } else if (sender == _forwardButton) { |
| 187 base::RecordAction(UserMetricsAction("MobileToolbarForward")); | 190 base::RecordAction(UserMetricsAction("MobileToolbarForward")); |
| 188 } else { | 191 } else { |
| 189 [super recordUserMetrics:sender]; | 192 [super recordUserMetrics:sender]; |
| 190 } | 193 } |
| 191 } | 194 } |
| 192 | 195 |
| 193 - (void)handleLongPress:(UILongPressGestureRecognizer*)gesture { | 196 - (void)handleLongPress:(UILongPressGestureRecognizer*)gesture { |
| 194 if (gesture.state != UIGestureRecognizerStateBegan) | 197 if (gesture.state != UIGestureRecognizerStateBegan) |
| 195 return; | 198 return; |
| 196 | 199 |
| 197 if (gesture.view == _backButton.get()) { | 200 if (gesture.view == _backButton) { |
| 198 base::scoped_nsobject<GenericChromeCommand> command( | 201 GenericChromeCommand* command = |
| 199 [[GenericChromeCommand alloc] initWithTag:IDC_SHOW_BACK_HISTORY]); | 202 [[GenericChromeCommand alloc] initWithTag:IDC_SHOW_BACK_HISTORY]; |
| 200 [_backButton chromeExecuteCommand:command]; | 203 [_backButton chromeExecuteCommand:command]; |
| 201 } else if (gesture.view == _forwardButton.get()) { | 204 } else if (gesture.view == _forwardButton) { |
| 202 base::scoped_nsobject<GenericChromeCommand> command( | 205 GenericChromeCommand* command = |
| 203 [[GenericChromeCommand alloc] initWithTag:IDC_SHOW_FORWARD_HISTORY]); | 206 [[GenericChromeCommand alloc] initWithTag:IDC_SHOW_FORWARD_HISTORY]; |
| 204 [_forwardButton chromeExecuteCommand:command]; | 207 [_forwardButton chromeExecuteCommand:command]; |
| 205 } | 208 } |
| 206 } | 209 } |
| 207 | 210 |
| 208 - (void)hideViewsForNewTabPage:(BOOL)hide { | 211 - (void)hideViewsForNewTabPage:(BOOL)hide { |
| 209 [super hideViewsForNewTabPage:hide]; | 212 [super hideViewsForNewTabPage:hide]; |
| 210 // Show the back/forward buttons if there is forward history. | 213 // Show the back/forward buttons if there is forward history. |
| 211 BOOL forwardEnabled = self.canGoForward; | 214 BOOL forwardEnabled = self.canGoForward; |
| 212 [_backButton setHidden:!forwardEnabled && hide]; | 215 [_backButton setHidden:!forwardEnabled && hide]; |
| 213 [_backButton setEnabled:self.canGoBack]; | 216 [_backButton setEnabled:self.canGoBack]; |
| 214 [_forwardButton setHidden:!forwardEnabled && hide]; | 217 [_forwardButton setHidden:!forwardEnabled && hide]; |
| 215 } | 218 } |
| 216 | 219 |
| 217 - (void)focusOmnibox:(id)sender { | 220 - (void)focusOmnibox:(id)sender { |
| 218 [self.dispatcher focusFakebox]; | 221 [self.dispatcher focusFakebox]; |
| 219 } | 222 } |
| 220 | 223 |
| 221 - (IBAction)stackButtonTouchDown:(id)sender { | 224 - (IBAction)stackButtonTouchDown:(id)sender { |
| 222 [self.dispatcher prepareToEnterTabSwitcher:self]; | 225 [self.dispatcher prepareToEnterTabSwitcher:self]; |
| 223 } | 226 } |
| 224 | 227 |
| 225 @end | 228 @end |
| OLD | NEW |