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/most_visited_layout.h" |
| 6 |
| 7 #import "ios/chrome/browser/ui/ntp/new_tab_page_header_constants.h" |
| 8 #include "ios/chrome/browser/ui/ui_util.h" |
| 9 |
| 10 @implementation MostVisitedLayout |
| 11 |
| 12 - (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect { |
| 13 NSMutableArray* layoutAttributes = [ |
| 14 [[super layoutAttributesForElementsInRect:rect] mutableCopy] autorelease]; |
| 15 UICollectionViewLayoutAttributes* fixedHeaderAttributes = nil; |
| 16 NSIndexPath* fixedHeaderIndexPath = |
| 17 [NSIndexPath indexPathForItem:0 inSection:0]; |
| 18 |
| 19 for (UICollectionViewLayoutAttributes* attributes in layoutAttributes) { |
| 20 if ([attributes.representedElementKind |
| 21 isEqualToString:UICollectionElementKindSectionHeader] && |
| 22 attributes.indexPath.section == fixedHeaderIndexPath.section) { |
| 23 fixedHeaderAttributes = [self |
| 24 layoutAttributesForSupplementaryViewOfKind: |
| 25 UICollectionElementKindSectionHeader |
| 26 atIndexPath:fixedHeaderIndexPath]; |
| 27 attributes.zIndex = fixedHeaderAttributes.zIndex; |
| 28 attributes.frame = fixedHeaderAttributes.frame; |
| 29 } |
| 30 } |
| 31 |
| 32 // The fixed header's attributes are not updated if the header's default frame |
| 33 // is far enough away from |rect|, which can occur when the NTP is scrolled |
| 34 // up. |
| 35 if (!fixedHeaderAttributes && !IsIPadIdiom()) { |
| 36 UICollectionViewLayoutAttributes* fixedHeaderAttributes = |
| 37 [self layoutAttributesForSupplementaryViewOfKind: |
| 38 UICollectionElementKindSectionHeader |
| 39 atIndexPath:fixedHeaderIndexPath]; |
| 40 [layoutAttributes addObject:fixedHeaderAttributes]; |
| 41 } |
| 42 |
| 43 return layoutAttributes; |
| 44 } |
| 45 |
| 46 - (UICollectionViewLayoutAttributes*) |
| 47 layoutAttributesForSupplementaryViewOfKind:(NSString*)kind |
| 48 atIndexPath:(NSIndexPath*)indexPath { |
| 49 UICollectionViewLayoutAttributes* attributes = |
| 50 [super layoutAttributesForSupplementaryViewOfKind:kind |
| 51 atIndexPath:indexPath]; |
| 52 if ([kind isEqualToString:UICollectionElementKindSectionHeader] && |
| 53 indexPath.section == 0) { |
| 54 UICollectionView* collectionView = self.collectionView; |
| 55 CGPoint contentOffset = collectionView.contentOffset; |
| 56 CGFloat headerHeight = CGRectGetHeight(attributes.frame); |
| 57 CGPoint origin = attributes.frame.origin; |
| 58 |
| 59 // Keep the header in front of all other views. |
| 60 attributes.zIndex = 1000; |
| 61 |
| 62 // Prevent the fake omnibox from scrolling up off of the screen. |
| 63 CGFloat minY = headerHeight - ntp_header::kMinHeaderHeight; |
| 64 if (contentOffset.y > minY) |
| 65 origin.y = contentOffset.y - minY; |
| 66 attributes.frame = {origin, attributes.frame.size}; |
| 67 } |
| 68 return attributes; |
| 69 } |
| 70 |
| 71 - (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath: |
| 72 (NSIndexPath*)indexPath { |
| 73 UICollectionViewLayoutAttributes* currentItemAttributes = |
| 74 [super layoutAttributesForItemAtIndexPath:indexPath]; |
| 75 |
| 76 return currentItemAttributes; |
| 77 } |
| 78 |
| 79 - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBound { |
| 80 return YES; |
| 81 } |
| 82 |
| 83 - (CGSize)collectionViewContentSize { |
| 84 CGFloat collectionViewHeight = self.collectionView.bounds.size.height; |
| 85 CGFloat headerHeight = [self headerReferenceSize].height; |
| 86 |
| 87 // The minimum height for the collection view content should be the height of |
| 88 // the header plus the height of the collection view minus the height of the |
| 89 // NTP bottom bar. This allows the Most Visited cells to be scrolled up to the |
| 90 // top of the screen. |
| 91 CGFloat minimumHeight = collectionViewHeight + headerHeight - |
| 92 ntp_header::kScrolledToTopOmniboxBottomMargin; |
| 93 if (!IsIPadIdiom()) |
| 94 minimumHeight -= ntp_header::kToolbarHeight; |
| 95 |
| 96 CGSize contentSize = [super collectionViewContentSize]; |
| 97 if (contentSize.height < minimumHeight) { |
| 98 contentSize.height = minimumHeight; |
| 99 } |
| 100 return contentSize; |
| 101 } |
| 102 |
| 103 @end |
OLD | NEW |