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