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