OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 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_view.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/objc_property_releaser.h" |
| 9 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar.h" |
| 10 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h" |
| 11 #import "ios/chrome/browser/ui/rtl_geometry.h" |
| 12 #include "ios/chrome/browser/ui/ui_util.h" |
| 13 |
| 14 @implementation NewTabPageView { |
| 15 @private |
| 16 // The objects pointed to by |tabBar_| and |scrollView_| are owned as |
| 17 // subviews already. |
| 18 __unsafe_unretained NewTabPageBar* tabBar_; // weak |
| 19 __unsafe_unretained UIScrollView* scrollView_; // weak |
| 20 |
| 21 base::mac::ObjCPropertyReleaser propertyReleaser_NewTabPageView_; |
| 22 } |
| 23 |
| 24 @synthesize scrollView = scrollView_; |
| 25 @synthesize tabBar = tabBar_; |
| 26 |
| 27 - (instancetype)initWithFrame:(CGRect)frame |
| 28 andScrollView:(UIScrollView*)scrollView |
| 29 andTabBar:(NewTabPageBar*)tabBar { |
| 30 self = [super initWithFrame:frame]; |
| 31 if (self) { |
| 32 propertyReleaser_NewTabPageView_.Init(self, [NewTabPageView class]); |
| 33 [self addSubview:scrollView]; |
| 34 [self addSubview:tabBar]; |
| 35 scrollView_ = scrollView; |
| 36 tabBar_ = tabBar; |
| 37 } |
| 38 return self; |
| 39 } |
| 40 |
| 41 - (instancetype)initWithFrame:(CGRect)frame { |
| 42 NOTREACHED(); |
| 43 return nil; |
| 44 } |
| 45 |
| 46 - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 47 NOTREACHED(); |
| 48 return nil; |
| 49 } |
| 50 |
| 51 - (void)setFrame:(CGRect)frame { |
| 52 // When transitioning the iPhone xib to an iPad idiom, the setFrame call below |
| 53 // can sometimes fire a scrollViewDidScroll event which changes the |
| 54 // selectedIndex underneath us. Save the selected index and remove the |
| 55 // delegate so scrollViewDidScroll isn't called. Then fix the scrollView |
| 56 // offset after updating the frame. |
| 57 NSUInteger selectedIndex = self.tabBar.selectedIndex; |
| 58 id<UIScrollViewDelegate> delegate = self.scrollView.delegate; |
| 59 self.scrollView.delegate = nil; |
| 60 [super setFrame:frame]; |
| 61 self.scrollView.delegate = delegate; |
| 62 |
| 63 // Set the scrollView content size. |
| 64 [self updateScrollViewContentSize]; |
| 65 |
| 66 // Set the frame of the laid out NTP panels on iPad. |
| 67 if (IsIPadIdiom()) { |
| 68 NSUInteger index = 0; |
| 69 CGFloat selectedItemXOffset = 0; |
| 70 for (NewTabPageBarItem* item in self.tabBar.items) { |
| 71 item.view.frame = [self panelFrameForItemAtIndex:index]; |
| 72 if (index == selectedIndex) |
| 73 selectedItemXOffset = CGRectGetMinX(item.view.frame); |
| 74 index++; |
| 75 } |
| 76 |
| 77 // Ensure the selected NTP panel is lined up correctly when the frame |
| 78 // changes. |
| 79 CGPoint point = CGPointMake(selectedItemXOffset, 0); |
| 80 [self.scrollView setContentOffset:point animated:NO]; |
| 81 } |
| 82 |
| 83 // Trigger a layout. The |-layoutIfNeeded| call is required because sometimes |
| 84 // |-layoutSubviews| is not successfully triggered when |-setNeedsLayout| is |
| 85 // called after frame changes due to autoresizing masks. |
| 86 [self setNeedsLayout]; |
| 87 [self layoutIfNeeded]; |
| 88 } |
| 89 |
| 90 - (void)layoutSubviews { |
| 91 [super layoutSubviews]; |
| 92 |
| 93 self.tabBar.hidden = !self.tabBar.items.count; |
| 94 if (self.tabBar.hidden) { |
| 95 self.scrollView.frame = self.bounds; |
| 96 } else { |
| 97 CGSize barSize = [self.tabBar sizeThatFits:self.bounds.size]; |
| 98 self.tabBar.frame = CGRectMake(CGRectGetMinX(self.bounds), |
| 99 CGRectGetMaxY(self.bounds) - barSize.height, |
| 100 barSize.width, barSize.height); |
| 101 self.scrollView.frame = CGRectMake( |
| 102 CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds), |
| 103 CGRectGetWidth(self.bounds), CGRectGetMinY(self.tabBar.frame)); |
| 104 } |
| 105 } |
| 106 |
| 107 - (void)updateScrollViewContentSize { |
| 108 CGSize contentSize = self.scrollView.bounds.size; |
| 109 // On iPhone, NTP doesn't scroll horizontally, as alternate panels are shown |
| 110 // modally. On iPad, panels are laid out side by side in the scroll view. |
| 111 if (IsIPadIdiom()) { |
| 112 contentSize.width *= self.tabBar.items.count; |
| 113 } |
| 114 self.scrollView.contentSize = contentSize; |
| 115 } |
| 116 |
| 117 - (CGRect)panelFrameForItemAtIndex:(NSUInteger)index { |
| 118 CGRect contentBounds = CGRectMake(0, 0, self.scrollView.contentSize.width, |
| 119 self.scrollView.contentSize.height); |
| 120 LayoutRect layout = |
| 121 LayoutRectForRectInBoundingRect(self.scrollView.bounds, contentBounds); |
| 122 layout.position.leading = layout.size.width * index; |
| 123 return LayoutRectGetRect(layout); |
| 124 } |
| 125 |
| 126 @end |
OLD | NEW |