Chromium Code Reviews| Index: ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| diff --git a/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm b/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| index 7e9eee5a5606d9af6c4433c4bfd420e8c146180b..68f0b001ff200ca8abac0a5c1ab2203671df6022 100644 |
| --- a/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| +++ b/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| @@ -10,6 +10,8 @@ |
| #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h" |
| #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| #import "ios/chrome/browser/ui/ntp/new_tab_page_view.h" |
| +#import "ios/chrome/browser/ui/rtl_geometry.h" |
| +#include "ios/chrome/browser/ui/ui_util.h" |
| #include "ios/chrome/grit/ios_strings.h" |
| #include "ui/base/l10n/l10n_util.h" |
| @@ -17,11 +19,27 @@ |
| #error "This file requires ARC support." |
| #endif |
| +@interface NTPViewController ()<UIScrollViewDelegate, NewTabPageBarDelegate> { |
| + @private |
|
marq (ping after 24h)
2017/04/05 12:22:49
no need for @private here.
justincohen
2017/04/05 19:28:24
Done.
|
| + BOOL _homeLoaded; |
| + BOOL _openTabsLoaded; |
| + BOOL _bookmarksLoaded; |
| + BOOL _incognitoLoaded; |
| +} |
| +@property(nonatomic, strong) NewTabPageView* ntpView; |
|
marq (ping after 24h)
2017/04/05 12:22:48
NTPView, or NewTabView, or something.
justincohen
2017/04/05 19:28:24
Done.
|
| +@property(nonatomic, retain) NSArray* tabBarItems; |
|
marq (ping after 24h)
2017/04/05 12:22:48
s/retain/strong/ in ARC-land.
justincohen
2017/04/05 19:28:24
Done.
|
| +@end |
| + |
| @implementation NTPViewController |
| +@synthesize ntpView = _ntpView; |
| +@synthesize ntpCommandHandler = _ntpCommandHandler; |
| +@synthesize tabBarItems = _tabBarItems; |
| + |
| #pragma mark - UIViewController |
| - (void)viewDidLoad { |
| + [super viewDidLoad]; |
| self.title = l10n_util::GetNSString(IDS_NEW_TAB_TITLE); |
| self.view.backgroundColor = [UIColor whiteColor]; |
| @@ -34,46 +52,144 @@ |
| scrollView.contentMode = UIViewContentModeScaleAspectFit; |
| scrollView.bounces = YES; |
| scrollView.scrollsToTop = NO; |
| + scrollView.delegate = self; |
| NewTabPageBar* tabBar = [[NewTabPageBar alloc] initWithFrame:CGRectZero]; |
| - NewTabPageView* ntpView = [[NewTabPageView alloc] initWithFrame:CGRectZero |
| - andScrollView:scrollView |
| - andTabBar:tabBar]; |
| - ntpView.translatesAutoresizingMaskIntoConstraints = NO; |
| - [self.view addSubview:ntpView]; |
| + tabBar.delegate = self; |
| + self.ntpView = [[NewTabPageView alloc] initWithFrame:CGRectZero |
| + andScrollView:scrollView |
| + andTabBar:tabBar]; |
| + self.ntpView.translatesAutoresizingMaskIntoConstraints = NO; |
| + [self.view addSubview:_ntpView]; |
|
marq (ping after 24h)
2017/04/05 12:22:48
s/_ntpView/self.ntpView everywhere, for consistenc
justincohen
2017/04/05 19:28:24
Done.
|
| [NSLayoutConstraint activateConstraints:@[ |
| - [ntpView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| - [ntpView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
| - [ntpView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| - [ntpView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
| + [_ntpView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| + [_ntpView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
| + [_ntpView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| + [_ntpView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
| ]]; |
| - // PLACEHOLDER: this logic should move out of the UIVC. |
| - NSString* mostVisited = l10n_util::GetNSString(IDS_IOS_NEW_TAB_MOST_VISITED); |
| - NSString* bookmarks = |
| - l10n_util::GetNSString(IDS_IOS_NEW_TAB_BOOKMARKS_PAGE_TITLE_MOBILE); |
| - NSString* openTabs = l10n_util::GetNSString(IDS_IOS_NEW_TAB_RECENT_TABS); |
| - |
| - NSMutableArray* tabBarItems = [NSMutableArray array]; |
| - |
| - NewTabPageBarItem* mostVisitedItem = [NewTabPageBarItem |
| - newTabPageBarItemWithTitle:mostVisited |
| - identifier:NewTabPage::kMostVisitedPanel |
| - image:[UIImage imageNamed:@"ntp_mv_search"]]; |
| - NewTabPageBarItem* bookmarksItem = [NewTabPageBarItem |
| - newTabPageBarItemWithTitle:bookmarks |
| - identifier:NewTabPage::kBookmarksPanel |
| - image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| - [tabBarItems addObject:bookmarksItem]; |
| - [tabBarItems addObject:mostVisitedItem]; |
| - |
| - NewTabPageBarItem* openTabsItem = [NewTabPageBarItem |
| - newTabPageBarItemWithTitle:openTabs |
| - identifier:NewTabPage::kOpenTabsPanel |
| - image:[UIImage imageNamed:@"ntp_opentabs"]]; |
| - [tabBarItems addObject:openTabsItem]; |
| - tabBar.items = tabBarItems; |
| + self.ntpView.tabBar.items = _tabBarItems; |
|
marq (ping after 24h)
2017/04/05 12:22:48
self.tabBarItems?
justincohen
2017/04/05 19:28:24
I could create a property and override the setter
marq (ping after 24h)
2017/04/06 14:30:13
I meant that you have a tabBarItems property, so i
justincohen
2017/04/06 18:25:09
Acknowledged.
|
| +} |
| + |
|
marq (ping after 24h)
2017/04/05 12:22:49
#pragma mark - NTPConsumer
justincohen
2017/04/05 19:28:24
Done.
|
| +- (void)setTabBarItems:(NSMutableArray*)items { |
| + _tabBarItems = items; |
|
marq (ping after 24h)
2017/04/05 12:22:49
Should this just directly set them on self.ntpView
justincohen
2017/04/05 19:28:24
No, that doesn't exist yet.
|
| +} |
| + |
| +- (void)viewDidLayoutSubviews { |
| + [super viewDidLayoutSubviews]; |
| + |
| + [self.ntpView layoutIfNeeded]; |
| + if (!_homeLoaded) { |
| + // PLACEHOLDER: This should come from the mediator. |
| + if (IsIPadIdiom()) { |
|
marq (ping after 24h)
2017/04/05 12:22:49
Please use size classes instead of IsIPadIdiom().
justincohen
2017/04/05 19:28:24
I could, but that would require a number of UI cha
marq (ping after 24h)
2017/04/06 14:30:13
We can hold off on any change for now; I think it'
|
| + CGRect itemFrame = [self.ntpView panelFrameForItemAtIndex:1]; |
| + CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
| + [self.ntpView.scrollView setContentOffset:point animated:NO]; |
| + } else { |
| + [self.ntpCommandHandler startNTPHomePanel]; |
| + } |
| + } |
| +} |
| + |
| +- (void)scrollViewDidScroll:(UIScrollView*)scrollView { |
| + // Position is used to track the exact X position of the scroll view, whereas |
| + // index is rounded to the panel that is most visible. |
| + CGFloat panelWidth = |
| + scrollView.contentSize.width / self.ntpView.tabBar.items.count; |
| + LayoutOffset position = |
| + LeadingContentOffsetForScrollView(scrollView) / panelWidth; |
| + NSUInteger index = round(position); |
| + |
| + // |scrollView| can be out of range when the frame changes. |
| + if (index >= self.ntpView.tabBar.items.count) |
| + return; |
| + |
| + NewTabPageBarItem* item = self.ntpView.tabBar.items[index]; |
| + if (item.identifier == NewTabPage::kBookmarksPanel && !_bookmarksLoaded) |
| + [self.ntpCommandHandler startNTPBookmarksPanel]; |
| + else if (item.identifier == NewTabPage::kMostVisitedPanel && !_homeLoaded) |
| + [self.ntpCommandHandler startNTPHomePanel]; |
| + else if (item.identifier == NewTabPage::kOpenTabsPanel && !_openTabsLoaded) |
| + [self.ntpCommandHandler startNTPOpenTabsPanel]; |
| + else if (item.identifier == NewTabPage::kIncognitoPanel && !_incognitoLoaded) |
| + [self.ntpCommandHandler startNTPIncognitoPanel]; |
| + if (item.identifier == NewTabPage::kMostVisitedPanel && !_homeLoaded) |
| + [self.ntpCommandHandler startNTPHomePanel]; |
| + |
| + // If index changed, follow same path as if a tab bar item was pressed. When |
| + // |index| == |position|, the panel is completely in view. |
| + if (index == position && self.ntpView.tabBar.selectedIndex != index) { |
| + NewTabPageBarItem* item = [self.ntpView.tabBar.items objectAtIndex:index]; |
| + DCHECK(item); |
| + self.ntpView.tabBar.selectedIndex = index; |
| + } |
| + [self.ntpView.tabBar updateColorsForScrollView:scrollView]; |
| + |
| + _ntpView.tabBar.overlayPercentage = |
| + scrollView.contentOffset.x / scrollView.contentSize.width; |
| +} |
| + |
| +- (void)newTabBarItemDidChange:(NewTabPageBarItem*)selectedItem |
| + changePanel:(BOOL)changePanel { |
| + if (IsIPadIdiom()) { |
| + NSUInteger index = [self.ntpView.tabBar.items indexOfObject:selectedItem]; |
| + CGRect itemFrame = [self.ntpView panelFrameForItemAtIndex:index]; |
| + CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
| + [self.ntpView.scrollView setContentOffset:point animated:YES]; |
| + } else { |
| + if (selectedItem.identifier == NewTabPage::kBookmarksPanel) { |
| + [self.ntpCommandHandler startNTPBookmarksPanel]; |
| + } else if (selectedItem.identifier == NewTabPage::kOpenTabsPanel) { |
| + [self.ntpCommandHandler startNTPOpenTabsPanel]; |
| + } |
| + } |
| +} |
| + |
| +- (void)addControllerToScrollView:(UIViewController*)controller { |
| + [self addChildViewController:controller]; |
| + [_ntpView.scrollView addSubview:controller.view]; |
| + [controller didMoveToParentViewController:self]; |
| +} |
| + |
| +- (void)addHomePanelViewController:(UIViewController*)controller { |
| + if (IsIPadIdiom()) { |
| + controller.view.frame = [_ntpView panelFrameForItemAtIndex:1]; |
| + } else { |
| + controller.view.frame = [_ntpView panelFrameForItemAtIndex:0]; |
| + } |
| + NewTabPageBarItem* item = self.ntpView.tabBar.items[1]; |
| + item.view = controller.view; |
| + [self addControllerToScrollView:controller]; |
| + _homeLoaded = YES; |
| +} |
| + |
| +- (void)addBookmarksViewController:(UIViewController*)controller { |
| + controller.view.frame = [_ntpView panelFrameForItemAtIndex:0]; |
| + NewTabPageBarItem* item = self.ntpView.tabBar.items[0]; |
| + item.view = controller.view; |
| + [self addControllerToScrollView:controller]; |
| + _bookmarksLoaded = YES; |
| +} |
| + |
| +- (void)addOpenTabsViewController:(UIViewController*)controller { |
| + controller.view.frame = [_ntpView panelFrameForItemAtIndex:2]; |
| + NewTabPageBarItem* item = self.ntpView.tabBar.items[2]; |
| + item.view = controller.view; |
| + [self addControllerToScrollView:controller]; |
| + _openTabsLoaded = YES; |
| +} |
| + |
| +- (void)addIncognitoViewController:(UIViewController*)controller { |
| + if (IsIPadIdiom()) { |
| + controller.view.frame = [_ntpView panelFrameForItemAtIndex:1]; |
| + } else { |
| + controller.view.frame = [_ntpView panelFrameForItemAtIndex:0]; |
| + } |
| + NewTabPageBarItem* item = self.ntpView.tabBar.items[1]; |
| + item.view = controller.view; |
| + [self addControllerToScrollView:controller]; |
| + _incognitoLoaded = YES; |
| } |
| @end |