OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/content_suggestions/content_suggestions_collectio
n_utils.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos
t_visited_item.h" |
| 9 #include "ios/chrome/browser/ui/ui_util.h" |
| 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." |
| 13 #endif |
| 14 |
| 15 namespace { |
| 16 const CGFloat kSpacingIPhone = 16; |
| 17 const CGFloat kSpacingIPad = 24; |
| 18 |
| 19 const CGFloat kMaxSearchFieldFrameMargin = 200; |
| 20 const CGFloat kDoodleTopMarginIPadPortrait = 82; |
| 21 const CGFloat kDoodleTopMarginIPadLandscape = 82; |
| 22 const CGFloat kNTPSearchFieldBottomPadding = 16; |
| 23 |
| 24 // Height for the doodle frame when Google is not the default search engine. |
| 25 const CGFloat kNonGoogleSearchDoodleHeight = 60; |
| 26 // Height for the header view on tablet when Google is not the default search |
| 27 // engine. |
| 28 const CGFloat kNonGoogleSearchHeaderHeightIPad = 10; |
| 29 |
| 30 enum InterfaceOrientation { |
| 31 ALL = 0, |
| 32 IPHONE_LANDSCAPE = 1, |
| 33 }; |
| 34 |
| 35 // Returns the width necessary to fit |numberOfItem| items, with no padding on |
| 36 // the side. |
| 37 CGFloat widthForNumberOfItem(NSUInteger numberOfItem) { |
| 38 return (numberOfItem - 1) * content_suggestions::spacingBetweenTiles() + |
| 39 numberOfItem * [ContentSuggestionsMostVisitedCell defaultSize].width; |
| 40 } |
| 41 } |
| 42 |
| 43 namespace content_suggestions { |
| 44 |
| 45 NSUInteger numberOfTilesForWidth(CGFloat availableWidth) { |
| 46 if (availableWidth > widthForNumberOfItem(4)) |
| 47 return 4; |
| 48 if (availableWidth > widthForNumberOfItem(3)) |
| 49 return 3; |
| 50 if (availableWidth > widthForNumberOfItem(2)) |
| 51 return 2; |
| 52 |
| 53 return 1; |
| 54 } |
| 55 |
| 56 CGFloat spacingBetweenTiles() { |
| 57 return IsIPadIdiom() ? kSpacingIPad : kSpacingIPhone; |
| 58 } |
| 59 |
| 60 CGRect getOrientationFrame(const CGRect frames[], CGFloat width) { |
| 61 BOOL is_portrait = UIInterfaceOrientationIsPortrait( |
| 62 [[UIApplication sharedApplication] statusBarOrientation]); |
| 63 InterfaceOrientation orientation = |
| 64 (IsIPadIdiom() || is_portrait) ? ALL : IPHONE_LANDSCAPE; |
| 65 |
| 66 // Calculate width based on screen width and origin x. |
| 67 CGRect frame = frames[orientation]; |
| 68 frame.size.width = fmax(width - 2 * frame.origin.x, 50); |
| 69 return frame; |
| 70 } |
| 71 |
| 72 CGFloat centeredTilesMarginForWidth(CGFloat width) { |
| 73 NSUInteger columns = numberOfTilesForWidth(width - 2 * spacingBetweenTiles()); |
| 74 CGFloat whitespace = |
| 75 width - columns * [ContentSuggestionsMostVisitedCell defaultSize].width - |
| 76 (columns - 1) * spacingBetweenTiles(); |
| 77 CGFloat margin = AlignValueToPixel(whitespace / 2); |
| 78 DCHECK(margin >= spacingBetweenTiles()); |
| 79 return margin; |
| 80 } |
| 81 |
| 82 CGRect doodleFrame(CGFloat width, BOOL logoIsShowing) { |
| 83 const CGRect kDoodleFrame[2] = { |
| 84 CGRectMake(0, 66, 0, 120), CGRectMake(0, 56, 0, 120), |
| 85 }; |
| 86 CGRect doodleFrame = getOrientationFrame(kDoodleFrame, width); |
| 87 if (!IsIPadIdiom() && !logoIsShowing) |
| 88 doodleFrame.size.height = kNonGoogleSearchDoodleHeight; |
| 89 if (IsIPadIdiom()) { |
| 90 doodleFrame.origin.y = IsPortrait() ? kDoodleTopMarginIPadPortrait |
| 91 : kDoodleTopMarginIPadLandscape; |
| 92 } |
| 93 return doodleFrame; |
| 94 } |
| 95 |
| 96 CGRect searchFieldFrame(CGFloat width, BOOL logoIsShowing) { |
| 97 CGFloat y = CGRectGetMaxY(doodleFrame(width, logoIsShowing)); |
| 98 CGFloat margin = centeredTilesMarginForWidth(width); |
| 99 if (margin > kMaxSearchFieldFrameMargin) |
| 100 margin = kMaxSearchFieldFrameMargin; |
| 101 const CGRect kSearchFieldFrame[2] = { |
| 102 CGRectMake(margin, y + 32, 0, 50), CGRectMake(margin, y + 16, 0, 50), |
| 103 }; |
| 104 CGRect searchFieldFrame = getOrientationFrame(kSearchFieldFrame, width); |
| 105 if (IsIPadIdiom()) { |
| 106 CGFloat iPadTopMargin = IsPortrait() ? kDoodleTopMarginIPadPortrait |
| 107 : kDoodleTopMarginIPadLandscape; |
| 108 searchFieldFrame.origin.y += iPadTopMargin - 32; |
| 109 } |
| 110 return searchFieldFrame; |
| 111 } |
| 112 |
| 113 CGFloat heightForLogoHeader(CGFloat width, |
| 114 BOOL logoIsShowing, |
| 115 BOOL promoCanShow) { |
| 116 CGFloat headerHeight = CGRectGetMaxY(searchFieldFrame(width, logoIsShowing)) + |
| 117 kNTPSearchFieldBottomPadding; |
| 118 if (!IsIPadIdiom()) { |
| 119 return headerHeight; |
| 120 } |
| 121 if (!logoIsShowing) { |
| 122 return kNonGoogleSearchHeaderHeightIPad; |
| 123 } |
| 124 if (!promoCanShow) { |
| 125 UIInterfaceOrientation orient = |
| 126 [[UIApplication sharedApplication] statusBarOrientation]; |
| 127 const CGFloat kTopSpacingMaterialPortrait = 56; |
| 128 const CGFloat kTopSpacingMaterialLandscape = 32; |
| 129 headerHeight += UIInterfaceOrientationIsPortrait(orient) |
| 130 ? kTopSpacingMaterialPortrait |
| 131 : kTopSpacingMaterialLandscape; |
| 132 } |
| 133 |
| 134 return headerHeight; |
| 135 } |
| 136 |
| 137 } // namespace content_suggestions |
OLD | NEW |