| 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_header_view.h" | 5 #import "ios/chrome/browser/ui/ntp/new_tab_page_header_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/browser/tabs/tab_model.h" | 9 #import "ios/chrome/browser/tabs/tab_model.h" |
| 10 #import "ios/chrome/browser/tabs/tab_model_observer.h" | 10 #import "ios/chrome/browser/tabs/tab_model_observer.h" |
| 11 #import "ios/chrome/browser/ui/image_util.h" | 11 #import "ios/chrome/browser/ui/image_util.h" |
| 12 #import "ios/chrome/browser/ui/ntp/google_landing_data_source.h" | 12 #import "ios/chrome/browser/ui/ntp/google_landing_data_source.h" |
| 13 #import "ios/chrome/browser/ui/ntp/new_tab_page_header_constants.h" | 13 #import "ios/chrome/browser/ui/ntp/new_tab_page_header_constants.h" |
| 14 #import "ios/chrome/browser/ui/ntp/new_tab_page_toolbar_controller.h" | 14 #import "ios/chrome/browser/ui/ntp/new_tab_page_toolbar_controller.h" |
| 15 #import "ios/chrome/browser/ui/uikit_ui_util.h" | 15 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
| 16 #import "ios/chrome/common/material_timing.h" | 16 #import "ios/chrome/common/material_timing.h" |
| 17 #include "ios/chrome/grit/ios_theme_resources.h" | 17 #include "ios/chrome/grit/ios_theme_resources.h" |
| 18 #import "ui/gfx/ios/uikit_util.h" | 18 #import "ui/gfx/ios/uikit_util.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const CGFloat kOmniboxImageBottomInset = 1; | 22 const CGFloat kOmniboxImageBottomInset = 1; |
| 23 const CGFloat kHintLabelSidePadding = 12; | 23 const CGFloat kHintLabelSidePadding = 12; |
| 24 const CGFloat kMaxConstraintConstantDiff = 5; | 24 const CGFloat kMaxConstraintConstantDiff = 5; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 @interface NewTabPageHeaderView () { | 28 @interface NewTabPageHeaderView ()<TabModelObserver> { |
| 29 base::scoped_nsobject<NewTabPageToolbarController> _toolbarController; | 29 base::scoped_nsobject<NewTabPageToolbarController> _toolbarController; |
| 30 base::scoped_nsobject<TabModel> _tabModel; |
| 30 base::scoped_nsobject<UIImageView> _searchBoxBorder; | 31 base::scoped_nsobject<UIImageView> _searchBoxBorder; |
| 31 base::scoped_nsobject<UIImageView> _shadow; | 32 base::scoped_nsobject<UIImageView> _shadow; |
| 32 } | 33 } |
| 33 | 34 |
| 34 @end | 35 @end |
| 35 | 36 |
| 36 @implementation NewTabPageHeaderView | 37 @implementation NewTabPageHeaderView |
| 37 | 38 |
| 38 - (instancetype)initWithFrame:(CGRect)frame { | 39 - (instancetype)initWithFrame:(CGRect)frame { |
| 39 self = [super initWithFrame:frame]; | 40 self = [super initWithFrame:frame]; |
| 40 if (self) { | 41 if (self) { |
| 41 self.clipsToBounds = YES; | 42 self.clipsToBounds = YES; |
| 42 } | 43 } |
| 43 return self; | 44 return self; |
| 44 } | 45 } |
| 45 | 46 |
| 46 - (void)dealloc { | 47 - (void)dealloc { |
| 48 [_tabModel removeObserver:self]; |
| 47 [super dealloc]; | 49 [super dealloc]; |
| 48 } | 50 } |
| 49 | 51 |
| 50 - (UIView*)toolBarView { | 52 - (UIView*)toolBarView { |
| 51 return [_toolbarController view]; | 53 return [_toolbarController view]; |
| 52 } | 54 } |
| 53 | 55 |
| 54 - (ToolbarController*)relinquishedToolbarController { | 56 - (ToolbarController*)relinquishedToolbarController { |
| 55 ToolbarController* relinquishedToolbarController = nil; | 57 ToolbarController* relinquishedToolbarController = nil; |
| 56 if ([[_toolbarController view] isDescendantOfView:self]) { | 58 if ([[_toolbarController view] isDescendantOfView:self]) { |
| 57 // Only relinquish the toolbar controller if it's in the hierarchy. | 59 // Only relinquish the toolbar controller if it's in the hierarchy. |
| 58 relinquishedToolbarController = _toolbarController.get(); | 60 relinquishedToolbarController = _toolbarController.get(); |
| 59 } | 61 } |
| 60 return relinquishedToolbarController; | 62 return relinquishedToolbarController; |
| 61 } | 63 } |
| 62 | 64 |
| 63 - (void)reparentToolbarController { | 65 - (void)reparentToolbarController { |
| 64 [self addSubview:[_toolbarController view]]; | 66 [self addSubview:[_toolbarController view]]; |
| 65 } | 67 } |
| 66 | 68 |
| 67 - (void)addToolbarWithDataSource:(id<GoogleLandingDataSource>)dataSource { | 69 - (void)addToolbarWithDataSource:(id<GoogleLandingDataSource>)dataSource { |
| 68 DCHECK(!_toolbarController); | 70 DCHECK(!_toolbarController); |
| 69 DCHECK(dataSource); | 71 DCHECK(dataSource); |
| 70 | 72 |
| 71 _toolbarController.reset([[NewTabPageToolbarController alloc] | 73 _toolbarController.reset([[NewTabPageToolbarController alloc] |
| 72 initWithToolbarDelegate:[dataSource toolbarDelegate] | 74 initWithToolbarDelegate:[dataSource toolbarDelegate] |
| 73 focuser:dataSource]); | 75 focuser:dataSource]); |
| 74 _toolbarController.get().readingListModel = [dataSource readingListModel]; | 76 _toolbarController.get().readingListModel = [dataSource readingListModel]; |
| 77 [_tabModel removeObserver:self]; |
| 78 _tabModel.reset([[dataSource tabModel] retain]); |
| 79 [self addTabModelObserver]; |
| 75 | 80 |
| 76 UIView* toolbarView = [_toolbarController view]; | 81 UIView* toolbarView = [_toolbarController view]; |
| 77 CGRect toolbarFrame = self.bounds; | 82 CGRect toolbarFrame = self.bounds; |
| 78 toolbarFrame.size.height = ntp_header::kToolbarHeight; | 83 toolbarFrame.size.height = ntp_header::kToolbarHeight; |
| 79 toolbarView.frame = toolbarFrame; | 84 toolbarView.frame = toolbarFrame; |
| 80 [toolbarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | 85 [toolbarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 81 [self hideToolbarViewsForNewTabPage]; | 86 [self hideToolbarViewsForNewTabPage]; |
| 82 | 87 |
| 83 [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; | 88 [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 84 [self addSubview:[_toolbarController view]]; | 89 [self addSubview:[_toolbarController view]]; |
| 85 } | 90 } |
| 86 | 91 |
| 87 - (void)hideToolbarViewsForNewTabPage { | 92 - (void)hideToolbarViewsForNewTabPage { |
| 88 [_toolbarController hideViewsForNewTabPage:YES]; | 93 [_toolbarController hideViewsForNewTabPage:YES]; |
| 89 }; | 94 }; |
| 90 | 95 |
| 91 - (void)setToolbarTabCount:(int)tabCount { | 96 - (void)addTabModelObserver { |
| 92 [_toolbarController setTabCount:tabCount]; | 97 [_tabModel addObserver:self]; |
| 98 [_toolbarController setTabCount:[_tabModel count]]; |
| 93 } | 99 } |
| 94 | 100 |
| 95 - (void)addViewsToSearchField:(UIView*)searchField { | 101 - (void)addViewsToSearchField:(UIView*)searchField { |
| 96 [searchField setBackgroundColor:[UIColor whiteColor]]; | 102 [searchField setBackgroundColor:[UIColor whiteColor]]; |
| 97 UIImage* searchBorderImage = | 103 UIImage* searchBorderImage = |
| 98 StretchableImageNamed(@"ntp_google_search_box", 12, 12); | 104 StretchableImageNamed(@"ntp_google_search_box", 12, 12); |
| 99 _searchBoxBorder.reset([[UIImageView alloc] initWithImage:searchBorderImage]); | 105 _searchBoxBorder.reset([[UIImageView alloc] initWithImage:searchBorderImage]); |
| 100 [_searchBoxBorder setFrame:[searchField bounds]]; | 106 [_searchBoxBorder setFrame:[searchField bounds]]; |
| 101 [_searchBoxBorder setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | 107 [_searchBoxBorder setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 102 UIViewAutoresizingFlexibleHeight]; | 108 UIViewAutoresizingFlexibleHeight]; |
| 103 [searchField insertSubview:_searchBoxBorder atIndex:0]; | 109 [searchField insertSubview:_searchBoxBorder atIndex:0]; |
| 104 | 110 |
| 105 UIImage* fullBleedShadow = NativeImage(IDR_IOS_TOOLBAR_SHADOW_FULL_BLEED); | 111 UIImage* fullBleedShadow = NativeImage(IDR_IOS_TOOLBAR_SHADOW_FULL_BLEED); |
| 106 _shadow.reset([[UIImageView alloc] initWithImage:fullBleedShadow]); | 112 _shadow.reset([[UIImageView alloc] initWithImage:fullBleedShadow]); |
| 107 CGRect shadowFrame = [searchField bounds]; | 113 CGRect shadowFrame = [searchField bounds]; |
| 108 shadowFrame.origin.y = | 114 shadowFrame.origin.y = |
| 109 searchField.bounds.size.height - kOmniboxImageBottomInset; | 115 searchField.bounds.size.height - kOmniboxImageBottomInset; |
| 110 shadowFrame.size.height = fullBleedShadow.size.height; | 116 shadowFrame.size.height = fullBleedShadow.size.height; |
| 111 [_shadow setFrame:shadowFrame]; | 117 [_shadow setFrame:shadowFrame]; |
| 112 [_shadow setUserInteractionEnabled:NO]; | 118 [_shadow setUserInteractionEnabled:NO]; |
| 113 [_shadow setAutoresizingMask:UIViewAutoresizingFlexibleWidth | | 119 [_shadow setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 114 UIViewAutoresizingFlexibleTopMargin]; | 120 UIViewAutoresizingFlexibleTopMargin]; |
| 115 [searchField addSubview:_shadow]; | 121 [searchField addSubview:_shadow]; |
| 116 [_shadow setAlpha:0]; | 122 [_shadow setAlpha:0]; |
| 117 } | 123 } |
| 118 | 124 |
| 125 - (void)tabModelDidChangeTabCount:(TabModel*)model { |
| 126 DCHECK(model == _tabModel); |
| 127 [_toolbarController setTabCount:[_tabModel count]]; |
| 128 } |
| 129 |
| 119 - (void)updateSearchField:(UIView*)searchField | 130 - (void)updateSearchField:(UIView*)searchField |
| 120 withInitialFrame:(CGRect)initialFrame | 131 withInitialFrame:(CGRect)initialFrame |
| 121 subviewConstraints:(NSArray*)constraints | 132 subviewConstraints:(NSArray*)constraints |
| 122 forOffset:(CGFloat)offset { | 133 forOffset:(CGFloat)offset { |
| 123 // The scroll offset at which point |searchField|'s frame should stop growing. | 134 // The scroll offset at which point |searchField|'s frame should stop growing. |
| 124 CGFloat maxScaleOffset = | 135 CGFloat maxScaleOffset = |
| 125 self.frame.size.height - ntp_header::kMinHeaderHeight; | 136 self.frame.size.height - ntp_header::kMinHeaderHeight; |
| 126 // The scroll offset at which point |searchField|'s frame should start | 137 // The scroll offset at which point |searchField|'s frame should start |
| 127 // growing. | 138 // growing. |
| 128 CGFloat startScaleOffset = maxScaleOffset - ntp_header::kAnimationDistance; | 139 CGFloat startScaleOffset = maxScaleOffset - ntp_header::kAnimationDistance; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 158 } | 169 } |
| 159 | 170 |
| 160 - (void)fadeOutShadow { | 171 - (void)fadeOutShadow { |
| 161 [UIView animateWithDuration:ios::material::kDuration1 | 172 [UIView animateWithDuration:ios::material::kDuration1 |
| 162 animations:^{ | 173 animations:^{ |
| 163 [_shadow setAlpha:0]; | 174 [_shadow setAlpha:0]; |
| 164 }]; | 175 }]; |
| 165 } | 176 } |
| 166 | 177 |
| 167 @end | 178 @end |
| OLD | NEW |