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/ntp/incognito_panel_controller.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #import "base/mac/scoped_nsobject.h" |
| 10 #include "components/google/core/browser/google_util.h" |
| 11 #include "components/strings/grit/components_strings.h" |
| 12 #include "ios/chrome/browser/application_context.h" |
| 13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 14 #import "ios/chrome/browser/ui/toolbar/web_toolbar_controller.h" |
| 15 #include "ios/chrome/browser/ui/ui_util.h" |
| 16 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 17 #import "ios/chrome/browser/ui/url_loader.h" |
| 18 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" |
| 19 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 20 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 21 #include "ios/web/public/referrer.h" |
| 22 #import "net/base/mac/url_conversions.h" |
| 23 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "url/gurl.h" |
| 25 |
| 26 namespace { |
| 27 // The URL for the the Learn More page shown on incognito new tab. |
| 28 // Taken from ntp_resource_cache.cc. |
| 29 const char kLearnMoreIncognitoUrl[] = |
| 30 "https://www.google.com/support/chrome/bin/answer.py?answer=95464"; |
| 31 |
| 32 GURL GetUrlWithLang(const GURL& url) { |
| 33 std::string locale = GetApplicationContext()->GetApplicationLocale(); |
| 34 return google_util::AppendGoogleLocaleParam(url, locale); |
| 35 } |
| 36 |
| 37 const CGFloat kDistanceToFadeToolbar = 50.0; |
| 38 const int kLinkColor = 0x03A9F4; |
| 39 } |
| 40 |
| 41 // The scrollview containing the views. Its content's size is constrained on its |
| 42 // superview's size. |
| 43 @interface IncognitoNTPView : UIScrollView |
| 44 |
| 45 // Returns an autoreleased label with the right format. |
| 46 - (UILabel*)labelWithString:(NSString*)string |
| 47 font:(UIFont*)font |
| 48 alpha:(float)alpha; |
| 49 |
| 50 // Triggers a navigation to the help page. |
| 51 - (void)learnMoreButtonPressed; |
| 52 |
| 53 @end |
| 54 |
| 55 @implementation IncognitoNTPView { |
| 56 base::WeakNSProtocol<id<UrlLoader>> _loader; |
| 57 base::scoped_nsobject<UIView> _containerView; |
| 58 |
| 59 // Constraint ensuring that |containerView| is at least as high as the |
| 60 // superview of the IncognitoNTPView, i.e. the Incognito panel. |
| 61 // This ensures that if the Incognito panel is higher than a compact |
| 62 // |containerView|, the |containerView|'s |topGuide| and |bottomGuide| are |
| 63 // forced to expand, centering the views in between them. |
| 64 base::scoped_nsobject<NSLayoutConstraint> _containerVerticalConstraint; |
| 65 |
| 66 // Constraint ensuring that |containerView| is as wide as the superview of the |
| 67 // the IncognitoNTPView, i.e. the Incognito panel. |
| 68 base::scoped_nsobject<NSLayoutConstraint> _containerHorizontalConstraint; |
| 69 } |
| 70 |
| 71 - (instancetype)initWithFrame:(CGRect)frame urlLoader:(id<UrlLoader>)loader { |
| 72 self = [super initWithFrame:frame]; |
| 73 if (self) { |
| 74 _loader.reset(loader); |
| 75 |
| 76 self.alwaysBounceVertical = YES; |
| 77 |
| 78 // Container in which all the subviews (image, labels, button) are added. |
| 79 _containerView.reset([[UIView alloc] initWithFrame:frame]); |
| 80 [_containerView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 81 |
| 82 // Incognito image. |
| 83 base::scoped_nsobject<UIImageView> incognitoImage([[UIImageView alloc] |
| 84 initWithImage:[UIImage imageNamed:@"incognito_icon"]]); |
| 85 [incognitoImage setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 86 [_containerView addSubview:incognitoImage]; |
| 87 |
| 88 // Title. |
| 89 UIFont* titleFont = |
| 90 [[MDFRobotoFontLoader sharedInstance] lightFontOfSize:24]; |
| 91 base::scoped_nsobject<UILabel> incognitoTabHeading( |
| 92 [[self labelWithString:l10n_util::GetNSString(IDS_NEW_TAB_OTR_HEADING) |
| 93 font:titleFont |
| 94 alpha:0.8] retain]); |
| 95 [_containerView addSubview:incognitoTabHeading]; |
| 96 |
| 97 // Description paragraph. |
| 98 UIFont* regularFont = |
| 99 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:14]; |
| 100 base::scoped_nsobject<UILabel> incognitoTabDescription([[self |
| 101 labelWithString:l10n_util::GetNSString(IDS_NEW_TAB_OTR_DESCRIPTION) |
| 102 font:regularFont |
| 103 alpha:0.7] retain]); |
| 104 [_containerView addSubview:incognitoTabDescription]; |
| 105 |
| 106 // Warning paragraph. |
| 107 base::scoped_nsobject<UILabel> incognitoTabWarning([[self |
| 108 labelWithString:l10n_util::GetNSString(IDS_NEW_TAB_OTR_MESSAGE_WARNING) |
| 109 font:regularFont |
| 110 alpha:0.7] retain]); |
| 111 [_containerView addSubview:incognitoTabWarning]; |
| 112 |
| 113 // Learn more button. |
| 114 base::scoped_nsobject<MDCButton> learnMore([[MDCFlatButton alloc] init]); |
| 115 UIColor* inkColor = |
| 116 [[[MDCPalette greyPalette] tint300] colorWithAlphaComponent:0.25]; |
| 117 [learnMore setInkColor:inkColor]; |
| 118 [learnMore setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 119 [learnMore setTitle:l10n_util::GetNSString(IDS_NEW_TAB_OTR_LEARN_MORE_LINK) |
| 120 forState:UIControlStateNormal]; |
| 121 [learnMore setTitleColor:UIColorFromRGB(kLinkColor) |
| 122 forState:UIControlStateNormal]; |
| 123 UIFont* buttonFont = |
| 124 [[MDFRobotoFontLoader sharedInstance] boldFontOfSize:14]; |
| 125 [[learnMore titleLabel] setFont:buttonFont]; |
| 126 [learnMore addTarget:self |
| 127 action:@selector(learnMoreButtonPressed) |
| 128 forControlEvents:UIControlEventTouchUpInside]; |
| 129 [_containerView addSubview:learnMore]; |
| 130 |
| 131 // |topGuide| and |bottomGuide| exist to vertically center the sibling views |
| 132 // located in between them. |
| 133 base::scoped_nsobject<UILayoutGuide> topGuide([[UILayoutGuide alloc] init]); |
| 134 base::scoped_nsobject<UILayoutGuide> bottomGuide( |
| 135 [[UILayoutGuide alloc] init]); |
| 136 [_containerView addLayoutGuide:topGuide]; |
| 137 [_containerView addLayoutGuide:bottomGuide]; |
| 138 |
| 139 NSDictionary* viewsDictionary = @{ |
| 140 @"topGuide" : topGuide.get(), |
| 141 @"image" : incognitoImage.get(), |
| 142 @"heading" : incognitoTabHeading.get(), |
| 143 @"description" : incognitoTabDescription.get(), |
| 144 @"warning" : incognitoTabWarning.get(), |
| 145 @"learnMoreButton" : learnMore.get(), |
| 146 @"bottomGuide" : bottomGuide.get(), |
| 147 }; |
| 148 NSArray* constraints = @[ |
| 149 @"V:|-0-[topGuide(>=12)]-[image]-24-[heading]-32-[description]", |
| 150 @"V:[description]-32-[warning]-32-[learnMoreButton]", |
| 151 @"V:[learnMoreButton]-[bottomGuide]-0-|", |
| 152 @"H:|-(>=24)-[heading]-(>=24)-|", |
| 153 @"H:|-(>=24)-[description(==416@999)]-(>=24)-|", |
| 154 @"H:|-(>=24)-[warning(==416@999)]-(>=24)-|" |
| 155 ]; |
| 156 ApplyVisualConstraintsWithOptions(constraints, viewsDictionary, |
| 157 LayoutOptionForRTLSupport(), |
| 158 _containerView); |
| 159 |
| 160 AddSameCenterXConstraint(_containerView, incognitoImage); |
| 161 AddSameCenterXConstraint(_containerView, incognitoTabHeading); |
| 162 AddSameCenterXConstraint(_containerView, incognitoTabDescription); |
| 163 AddSameCenterXConstraint(_containerView, incognitoTabWarning); |
| 164 AddSameCenterXConstraint(_containerView, learnMore); |
| 165 |
| 166 // The bottom guide is twice as big as the top guide. |
| 167 [_containerView addConstraint:[NSLayoutConstraint |
| 168 constraintWithItem:bottomGuide |
| 169 attribute:NSLayoutAttributeHeight |
| 170 relatedBy:NSLayoutRelationEqual |
| 171 toItem:topGuide |
| 172 attribute:NSLayoutAttributeHeight |
| 173 multiplier:2 |
| 174 constant:0]]; |
| 175 |
| 176 [self addSubview:_containerView]; |
| 177 |
| 178 // Constraints comunicating the size of the contentView to the scrollview. |
| 179 // See UIScrollView autolayout information at |
| 180 // https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-6_
0/index.html |
| 181 viewsDictionary = @{ @"containerView" : _containerView.get() }; |
| 182 constraints = @[ |
| 183 @"V:|-0-[containerView]-0-|", |
| 184 @"H:|-0-[containerView]-0-|", |
| 185 ]; |
| 186 ApplyVisualConstraintsWithOptions(constraints, viewsDictionary, |
| 187 LayoutOptionForRTLSupport(), self); |
| 188 } |
| 189 return self; |
| 190 } |
| 191 |
| 192 - (UILabel*)labelWithString:(NSString*)string |
| 193 font:(UIFont*)font |
| 194 alpha:(float)alpha { |
| 195 base::scoped_nsobject<NSMutableAttributedString> attributedString( |
| 196 [[NSMutableAttributedString alloc] initWithString:string]); |
| 197 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
| 198 [[NSMutableParagraphStyle alloc] init]); |
| 199 [paragraphStyle setLineSpacing:4]; |
| 200 [paragraphStyle setAlignment:NSTextAlignmentJustified]; |
| 201 [attributedString addAttribute:NSParagraphStyleAttributeName |
| 202 value:paragraphStyle |
| 203 range:NSMakeRange(0, string.length)]; |
| 204 base::scoped_nsobject<UILabel> label( |
| 205 [[UILabel alloc] initWithFrame:CGRectZero]); |
| 206 [label setTranslatesAutoresizingMaskIntoConstraints:NO]; |
| 207 [label setNumberOfLines:0]; |
| 208 [label setFont:font]; |
| 209 [label setAttributedText:attributedString]; |
| 210 [label setTextColor:[UIColor colorWithWhite:1.0 alpha:alpha]]; |
| 211 return label.autorelease(); |
| 212 } |
| 213 |
| 214 - (void)learnMoreButtonPressed { |
| 215 GURL gurl = GetUrlWithLang(GURL(kLearnMoreIncognitoUrl)); |
| 216 [_loader loadURL:gurl |
| 217 referrer:web::Referrer() |
| 218 transition:ui::PAGE_TRANSITION_LINK |
| 219 rendererInitiated:NO]; |
| 220 } |
| 221 |
| 222 #pragma mark - UIView overrides |
| 223 |
| 224 - (void)didMoveToSuperview { |
| 225 [super didMoveToSuperview]; |
| 226 _containerHorizontalConstraint.reset( |
| 227 [[NSLayoutConstraint constraintWithItem:_containerView.get() |
| 228 attribute:NSLayoutAttributeWidth |
| 229 relatedBy:NSLayoutRelationEqual |
| 230 toItem:[self superview] |
| 231 attribute:NSLayoutAttributeWidth |
| 232 multiplier:1 |
| 233 constant:0] retain]); |
| 234 _containerVerticalConstraint.reset( |
| 235 [[NSLayoutConstraint constraintWithItem:_containerView.get() |
| 236 attribute:NSLayoutAttributeHeight |
| 237 relatedBy:NSLayoutRelationGreaterThanOrEqual |
| 238 toItem:[self superview] |
| 239 attribute:NSLayoutAttributeHeight |
| 240 multiplier:1 |
| 241 constant:0] retain]); |
| 242 [[self superview] addConstraint:_containerHorizontalConstraint.get()]; |
| 243 [[self superview] addConstraint:_containerVerticalConstraint.get()]; |
| 244 } |
| 245 |
| 246 - (void)willMoveToSuperview:(UIView*)newSuperview { |
| 247 [[self superview] removeConstraint:_containerHorizontalConstraint.get()]; |
| 248 [[self superview] removeConstraint:_containerVerticalConstraint.get()]; |
| 249 _containerHorizontalConstraint.reset(); |
| 250 _containerVerticalConstraint.reset(); |
| 251 [super willMoveToSuperview:newSuperview]; |
| 252 } |
| 253 |
| 254 @end |
| 255 |
| 256 @interface IncognitoPanelController ()<UIScrollViewDelegate> |
| 257 // Calculate the background alpha for the toolbar based on how much |scrollView| |
| 258 // has scrolled up. |
| 259 - (CGFloat)toolbarAlphaForScrollView:(UIScrollView*)scrollView; |
| 260 @end |
| 261 |
| 262 @implementation IncognitoPanelController { |
| 263 // Delegate for updating the toolbar's background alpha. |
| 264 base::WeakNSProtocol<id<WebToolbarDelegate>> _webToolbarDelegate; |
| 265 |
| 266 // The view containing the scrollview. |
| 267 // The purpose of this view is to be used to set the size |
| 268 // of the contentView of the scrollview with constraints. |
| 269 base::scoped_nsobject<UIView> _view; |
| 270 |
| 271 // The scrollview containing the actual views. |
| 272 base::scoped_nsobject<IncognitoNTPView> _incognitoView; |
| 273 } |
| 274 |
| 275 // Property declared in NewTabPagePanelProtocol. |
| 276 @synthesize delegate = _delegate; |
| 277 |
| 278 - (id)initWithLoader:(id<UrlLoader>)loader |
| 279 browserState:(ios::ChromeBrowserState*)browserState |
| 280 webToolbarDelegate:(id<WebToolbarDelegate>)webToolbarDelegate { |
| 281 self = [super init]; |
| 282 if (self) { |
| 283 _view.reset([[UIView alloc] |
| 284 initWithFrame:[UIApplication sharedApplication].keyWindow.bounds]); |
| 285 [_view setAccessibilityIdentifier:@"NTP Incognito Panel"]; |
| 286 [_view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | |
| 287 UIViewAutoresizingFlexibleWidth]; |
| 288 _incognitoView.reset([[IncognitoNTPView alloc] |
| 289 initWithFrame:[UIApplication sharedApplication].keyWindow.bounds |
| 290 urlLoader:loader]); |
| 291 [_incognitoView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | |
| 292 UIViewAutoresizingFlexibleWidth]; |
| 293 |
| 294 if (IsIPadIdiom()) { |
| 295 [_incognitoView setBackgroundColor:[UIColor clearColor]]; |
| 296 } else { |
| 297 [_incognitoView |
| 298 setBackgroundColor:[UIColor colorWithWhite:34 / 255.0 alpha:1.0]]; |
| 299 } |
| 300 if (!IsIPadIdiom()) { |
| 301 [_incognitoView setDelegate:self]; |
| 302 _webToolbarDelegate.reset(webToolbarDelegate); |
| 303 [_webToolbarDelegate updateToolbarBackgroundAlpha:0]; |
| 304 } |
| 305 [_view addSubview:_incognitoView]; |
| 306 } |
| 307 return self; |
| 308 } |
| 309 |
| 310 - (CGFloat)toolbarAlphaForScrollView:(UIScrollView*)scrollView { |
| 311 CGFloat alpha = scrollView.contentOffset.y / kDistanceToFadeToolbar; |
| 312 return MAX(MIN(alpha, 1), 0); |
| 313 } |
| 314 |
| 315 - (void)dealloc { |
| 316 [_webToolbarDelegate updateToolbarBackgroundAlpha:1]; |
| 317 [_incognitoView setDelegate:nil]; |
| 318 [super dealloc]; |
| 319 } |
| 320 |
| 321 #pragma mark - |
| 322 #pragma mark NewTabPagePanelProtocol |
| 323 |
| 324 - (void)reload { |
| 325 } |
| 326 |
| 327 - (void)wasShown { |
| 328 CGFloat alpha = [self toolbarAlphaForScrollView:_incognitoView]; |
| 329 [_webToolbarDelegate updateToolbarBackgroundAlpha:alpha]; |
| 330 } |
| 331 |
| 332 - (void)wasHidden { |
| 333 [_webToolbarDelegate updateToolbarBackgroundAlpha:1]; |
| 334 } |
| 335 |
| 336 - (void)dismissModals { |
| 337 } |
| 338 |
| 339 - (void)dismissKeyboard { |
| 340 } |
| 341 |
| 342 - (void)setScrollsToTop:(BOOL)enable { |
| 343 } |
| 344 |
| 345 - (CGFloat)alphaForBottomShadow { |
| 346 return 0; |
| 347 } |
| 348 |
| 349 - (UIView*)view { |
| 350 return _view.get(); |
| 351 } |
| 352 |
| 353 #pragma mark - |
| 354 #pragma mark UIScrollViewDelegate methods |
| 355 |
| 356 - (void)scrollViewDidScroll:(UIScrollView*)scrollView { |
| 357 CGFloat alpha = [self toolbarAlphaForScrollView:_incognitoView]; |
| 358 [_webToolbarDelegate updateToolbarBackgroundAlpha:alpha]; |
| 359 } |
| 360 |
| 361 @end |
OLD | NEW |