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_header_view.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/browser/tabs/tab_model.h" |
| 10 #import "ios/chrome/browser/tabs/tab_model_observer.h" |
| 11 #import "ios/chrome/browser/ui/image_util.h" |
| 12 #import "ios/chrome/browser/ui/ntp/new_tab_page_header_constants.h" |
| 13 #import "ios/chrome/browser/ui/ntp/new_tab_page_toolbar_controller.h" |
| 14 #import "ios/chrome/common/material_timing.h" |
| 15 #include "ios/chrome/grit/ios_theme_resources.h" |
| 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #import "ui/gfx/ios/uikit_util.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 const CGFloat kOmniboxImageBottomInset = 1; |
| 22 const CGFloat kHintLabelSidePadding = 12; |
| 23 const CGFloat kMaxConstraintConstantDiff = 5; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 @interface NewTabPageHeaderView ()<TabModelObserver> { |
| 28 base::scoped_nsobject<NewTabPageToolbarController> _toolbarController; |
| 29 base::scoped_nsobject<TabModel> _tabModel; |
| 30 base::scoped_nsobject<UIImageView> _searchBoxBorder; |
| 31 base::scoped_nsobject<UIImageView> _shadow; |
| 32 } |
| 33 |
| 34 @end |
| 35 |
| 36 @implementation NewTabPageHeaderView |
| 37 |
| 38 - (instancetype)initWithFrame:(CGRect)frame { |
| 39 self = [super initWithFrame:frame]; |
| 40 if (self) { |
| 41 self.clipsToBounds = YES; |
| 42 } |
| 43 return self; |
| 44 } |
| 45 |
| 46 - (void)dealloc { |
| 47 [_tabModel removeObserver:self]; |
| 48 [super dealloc]; |
| 49 } |
| 50 |
| 51 - (UIView*)toolBarView { |
| 52 return [_toolbarController view]; |
| 53 } |
| 54 |
| 55 - (ToolbarController*)relinquishedToolbarController { |
| 56 ToolbarController* relinquishedToolbarController = nil; |
| 57 if ([[_toolbarController view] isDescendantOfView:self]) { |
| 58 // Only relinquish the toolbar controller if it's in the hierarchy. |
| 59 relinquishedToolbarController = _toolbarController.get(); |
| 60 } |
| 61 return relinquishedToolbarController; |
| 62 } |
| 63 |
| 64 - (void)reparentToolbarController { |
| 65 [self addSubview:[_toolbarController view]]; |
| 66 } |
| 67 |
| 68 - (void)addToolbarWithDelegate:(id<WebToolbarDelegate>)toolbarDelegate |
| 69 focuser:(id<OmniboxFocuser>)focuser |
| 70 tabModel:(TabModel*)tabModel |
| 71 readingListModel:(ReadingListModel*)readingListModel { |
| 72 DCHECK(!_toolbarController); |
| 73 DCHECK(focuser); |
| 74 |
| 75 _toolbarController.reset([[NewTabPageToolbarController alloc] |
| 76 initWithToolbarDelegate:toolbarDelegate |
| 77 focuser:focuser]); |
| 78 _toolbarController.get().readingListModel = readingListModel; |
| 79 [_tabModel removeObserver:self]; |
| 80 _tabModel.reset([tabModel retain]); |
| 81 [self addTabModelObserver]; |
| 82 |
| 83 UIView* toolbarView = [_toolbarController view]; |
| 84 CGRect toolbarFrame = self.bounds; |
| 85 toolbarFrame.size.height = ntp_header::kToolbarHeight; |
| 86 toolbarView.frame = toolbarFrame; |
| 87 [toolbarView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 88 [self hideToolbarViewsForNewTabPage]; |
| 89 |
| 90 [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; |
| 91 [self addSubview:[_toolbarController view]]; |
| 92 } |
| 93 |
| 94 - (void)hideToolbarViewsForNewTabPage { |
| 95 [_toolbarController hideViewsForNewTabPage:YES]; |
| 96 }; |
| 97 |
| 98 - (void)addTabModelObserver { |
| 99 [_tabModel addObserver:self]; |
| 100 [_toolbarController setTabCount:[_tabModel count]]; |
| 101 } |
| 102 |
| 103 - (void)addViewsToSearchField:(UIView*)searchField { |
| 104 [searchField setBackgroundColor:[UIColor whiteColor]]; |
| 105 UIImage* searchBorderImage = |
| 106 StretchableImageNamed(@"ntp_google_search_box", 12, 12); |
| 107 _searchBoxBorder.reset([[UIImageView alloc] initWithImage:searchBorderImage]); |
| 108 [_searchBoxBorder setFrame:[searchField bounds]]; |
| 109 [_searchBoxBorder setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 110 UIViewAutoresizingFlexibleHeight]; |
| 111 [searchField insertSubview:_searchBoxBorder atIndex:0]; |
| 112 |
| 113 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 114 UIImage* fullBleedShadow = |
| 115 rb.GetNativeImageNamed(IDR_IOS_TOOLBAR_SHADOW_FULL_BLEED).ToUIImage(); |
| 116 _shadow.reset([[UIImageView alloc] initWithImage:fullBleedShadow]); |
| 117 CGRect shadowFrame = [searchField bounds]; |
| 118 shadowFrame.origin.y = |
| 119 searchField.bounds.size.height - kOmniboxImageBottomInset; |
| 120 shadowFrame.size.height = fullBleedShadow.size.height; |
| 121 [_shadow setFrame:shadowFrame]; |
| 122 [_shadow setUserInteractionEnabled:NO]; |
| 123 [_shadow setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 124 UIViewAutoresizingFlexibleTopMargin]; |
| 125 [searchField addSubview:_shadow]; |
| 126 [_shadow setAlpha:0]; |
| 127 } |
| 128 |
| 129 - (void)tabModelDidChangeTabCount:(TabModel*)model { |
| 130 DCHECK(model == _tabModel); |
| 131 [_toolbarController setTabCount:[_tabModel count]]; |
| 132 } |
| 133 |
| 134 - (void)updateSearchField:(UIView*)searchField |
| 135 withInitialFrame:(CGRect)initialFrame |
| 136 subviewConstraints:(NSArray*)constraints |
| 137 forOffset:(CGFloat)offset { |
| 138 // The scroll offset at which point |searchField|'s frame should stop growing. |
| 139 CGFloat maxScaleOffset = |
| 140 self.frame.size.height - ntp_header::kMinHeaderHeight; |
| 141 // The scroll offset at which point |searchField|'s frame should start |
| 142 // growing. |
| 143 CGFloat startScaleOffset = maxScaleOffset - ntp_header::kAnimationDistance; |
| 144 CGFloat percent = 0; |
| 145 if (offset > startScaleOffset) { |
| 146 CGFloat animatingOffset = offset - startScaleOffset; |
| 147 percent = MIN(1, MAX(0, animatingOffset / ntp_header::kAnimationDistance)); |
| 148 } |
| 149 |
| 150 // Calculate the amount to grow the width and height of |searchField| so that |
| 151 // its frame covers the entire toolbar area. |
| 152 CGFloat maxXInset = ui::AlignValueToUpperPixel( |
| 153 (initialFrame.size.width - self.bounds.size.width) / 2 - 1); |
| 154 CGFloat maxYOffset = ui::AlignValueToUpperPixel( |
| 155 (ntp_header::kToolbarHeight - initialFrame.size.height) / 2 + |
| 156 kOmniboxImageBottomInset - 0.5); |
| 157 CGRect searchFieldFrame = CGRectInset(initialFrame, maxXInset * percent, 0); |
| 158 searchFieldFrame.origin.y += maxYOffset * percent; |
| 159 searchFieldFrame.size.height += 2 * maxYOffset * percent; |
| 160 [searchField setFrame:CGRectIntegral(searchFieldFrame)]; |
| 161 [_searchBoxBorder setAlpha:(1 - percent)]; |
| 162 [_shadow setAlpha:percent]; |
| 163 |
| 164 // Adjust the position of the search field's subviews by adjusting their |
| 165 // constraint constant value. |
| 166 CGFloat constantDiff = percent * kMaxConstraintConstantDiff; |
| 167 for (NSLayoutConstraint* constraint in constraints) { |
| 168 if (constraint.constant > 0) |
| 169 constraint.constant = constantDiff + kHintLabelSidePadding; |
| 170 else |
| 171 constraint.constant = -constantDiff; |
| 172 } |
| 173 } |
| 174 |
| 175 - (void)fadeOutShadow { |
| 176 [UIView animateWithDuration:ios::material::kDuration1 |
| 177 animations:^{ |
| 178 [_shadow setAlpha:0]; |
| 179 }]; |
| 180 } |
| 181 |
| 182 @end |
OLD | NEW |