Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.h" | 5 #import "ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.h" |
| 6 | 6 |
| 7 #import "base/ios/crb_protocol_observers.h" | 7 #import "base/ios/crb_protocol_observers.h" |
| 8 #include "components/strings/grit/components_strings.h" | 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar.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" | 10 #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h" |
| 11 #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" | 11 #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| 12 #import "ios/chrome/browser/ui/ntp/new_tab_page_view.h" | 12 #import "ios/chrome/browser/ui/ntp/new_tab_page_view.h" |
| 13 #import "ios/chrome/browser/ui/rtl_geometry.h" | |
| 14 #include "ios/chrome/browser/ui/ui_util.h" | |
| 13 #include "ios/chrome/grit/ios_strings.h" | 15 #include "ios/chrome/grit/ios_strings.h" |
| 16 #import "ios/clean/chrome/browser/ui/commands/ntp_commands.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 15 | 18 |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 17 #error "This file requires ARC support." | 20 #error "This file requires ARC support." |
| 18 #endif | 21 #endif |
| 19 | 22 |
| 23 @interface NTPViewController ()<UIScrollViewDelegate, NewTabPageBarDelegate> { | |
| 24 BOOL _homeLoaded; | |
| 25 BOOL _openTabsLoaded; | |
| 26 BOOL _bookmarksLoaded; | |
| 27 } | |
| 28 @property(nonatomic, strong) NewTabPageView* NTPView; | |
| 29 @property(nonatomic, strong) NSArray* tabBarItems; | |
| 30 @end | |
| 31 | |
| 20 @implementation NTPViewController | 32 @implementation NTPViewController |
| 21 | 33 |
| 34 @synthesize NTPView = _NTPView; | |
| 35 @synthesize dispatcher = _dispatcher; | |
| 36 @synthesize tabBarItems = _tabBarItems; | |
| 37 | |
| 22 #pragma mark - UIViewController | 38 #pragma mark - UIViewController |
| 23 | 39 |
| 24 - (void)viewDidLoad { | 40 - (void)viewDidLoad { |
| 41 [super viewDidLoad]; | |
| 25 self.title = l10n_util::GetNSString(IDS_NEW_TAB_TITLE); | 42 self.title = l10n_util::GetNSString(IDS_NEW_TAB_TITLE); |
| 26 self.view.backgroundColor = [UIColor whiteColor]; | 43 self.view.backgroundColor = [UIColor whiteColor]; |
| 27 | 44 |
| 28 UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; | 45 UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; |
| 29 [scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | | 46 [scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | |
| 30 UIViewAutoresizingFlexibleHeight)]; | 47 UIViewAutoresizingFlexibleHeight)]; |
| 31 scrollView.pagingEnabled = YES; | 48 scrollView.pagingEnabled = YES; |
| 32 scrollView.showsHorizontalScrollIndicator = NO; | 49 scrollView.showsHorizontalScrollIndicator = NO; |
| 33 scrollView.showsVerticalScrollIndicator = NO; | 50 scrollView.showsVerticalScrollIndicator = NO; |
| 34 scrollView.contentMode = UIViewContentModeScaleAspectFit; | 51 scrollView.contentMode = UIViewContentModeScaleAspectFit; |
| 35 scrollView.bounces = YES; | 52 scrollView.bounces = YES; |
| 36 scrollView.scrollsToTop = NO; | 53 scrollView.scrollsToTop = NO; |
| 54 scrollView.delegate = self; | |
| 37 | 55 |
| 38 NewTabPageBar* tabBar = [[NewTabPageBar alloc] initWithFrame:CGRectZero]; | 56 NewTabPageBar* tabBar = [[NewTabPageBar alloc] initWithFrame:CGRectZero]; |
| 39 NewTabPageView* ntpView = [[NewTabPageView alloc] initWithFrame:CGRectZero | 57 tabBar.delegate = self; |
| 40 andScrollView:scrollView | 58 self.NTPView = [[NewTabPageView alloc] initWithFrame:CGRectZero |
| 41 andTabBar:tabBar]; | 59 andScrollView:scrollView |
| 42 ntpView.translatesAutoresizingMaskIntoConstraints = NO; | 60 andTabBar:tabBar]; |
| 43 [self.view addSubview:ntpView]; | 61 self.NTPView.translatesAutoresizingMaskIntoConstraints = NO; |
| 62 [self.view addSubview:self.NTPView]; | |
| 44 | 63 |
| 45 [NSLayoutConstraint activateConstraints:@[ | 64 [NSLayoutConstraint activateConstraints:@[ |
| 46 [ntpView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 65 [self.NTPView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| 47 [ntpView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 66 [self.NTPView.leadingAnchor |
| 48 [ntpView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 67 constraintEqualToAnchor:self.view.leadingAnchor], |
| 49 [ntpView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | 68 [self.NTPView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| 69 [self.NTPView.trailingAnchor | |
| 70 constraintEqualToAnchor:self.view.trailingAnchor], | |
| 50 ]]; | 71 ]]; |
| 51 | 72 |
| 52 // PLACEHOLDER: this logic should move out of the UIVC. | 73 self.NTPView.tabBar.items = _tabBarItems; |
|
lpromero
2017/04/06 13:01:06
Should you DCHECK that _tabBarItems is set? Becaus
justincohen
2017/04/06 18:25:11
Done.
| |
| 53 NSString* mostVisited = l10n_util::GetNSString(IDS_IOS_NEW_TAB_MOST_VISITED); | 74 } |
| 54 NSString* bookmarks = | |
| 55 l10n_util::GetNSString(IDS_IOS_NEW_TAB_BOOKMARKS_PAGE_TITLE_MOBILE); | |
| 56 NSString* openTabs = l10n_util::GetNSString(IDS_IOS_NEW_TAB_RECENT_TABS); | |
| 57 | 75 |
| 58 NSMutableArray* tabBarItems = [NSMutableArray array]; | 76 - (void)viewDidLayoutSubviews { |
| 77 [super viewDidLayoutSubviews]; | |
| 59 | 78 |
| 60 NewTabPageBarItem* mostVisitedItem = [NewTabPageBarItem | 79 if (!_homeLoaded) { |
| 61 newTabPageBarItemWithTitle:mostVisited | 80 // Make sure scrollView is properly set up. |
| 62 identifier:NewTabPage::kMostVisitedPanel | 81 [self.NTPView layoutIfNeeded]; |
| 63 image:[UIImage imageNamed:@"ntp_mv_search"]]; | 82 // PLACEHOLDER: This should come from the mediator. |
| 64 NewTabPageBarItem* bookmarksItem = [NewTabPageBarItem | 83 if (IsIPadIdiom()) { |
| 65 newTabPageBarItemWithTitle:bookmarks | 84 CGRect itemFrame = [self.NTPView panelFrameForItemAtIndex:1]; |
| 66 identifier:NewTabPage::kBookmarksPanel | 85 CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
| 67 image:[UIImage imageNamed:@"ntp_bookmarks"]]; | 86 [self.NTPView.scrollView setContentOffset:point animated:NO]; |
| 68 [tabBarItems addObject:bookmarksItem]; | 87 } else { |
| 69 [tabBarItems addObject:mostVisitedItem]; | 88 [self.dispatcher showNTPHomePanel]; |
| 89 } | |
| 90 } | |
| 91 } | |
| 70 | 92 |
| 71 NewTabPageBarItem* openTabsItem = [NewTabPageBarItem | 93 - (void)addControllerToScrollView:(UIViewController*)controller { |
| 72 newTabPageBarItemWithTitle:openTabs | 94 [self addChildViewController:controller]; |
| 73 identifier:NewTabPage::kOpenTabsPanel | 95 [self.NTPView.scrollView addSubview:controller.view]; |
| 74 image:[UIImage imageNamed:@"ntp_opentabs"]]; | 96 [controller didMoveToParentViewController:self]; |
| 75 [tabBarItems addObject:openTabsItem]; | 97 } |
| 76 tabBar.items = tabBarItems; | 98 |
| 99 - (void)addHomePanelViewController:(UIViewController*)controller { | |
| 100 if (IsIPadIdiom()) { | |
| 101 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:1]; | |
| 102 } else { | |
| 103 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:0]; | |
| 104 } | |
| 105 NewTabPageBarItem* item = self.NTPView.tabBar.items[1]; | |
|
rohitrao (ping after 24h)
2017/04/06 13:08:13
This is weird -- we get a set of items from the me
justincohen
2017/04/06 18:25:11
More of this logic will move into the mediator in
| |
| 106 item.view = controller.view; | |
| 107 [self addControllerToScrollView:controller]; | |
| 108 _homeLoaded = YES; | |
| 109 } | |
| 110 | |
| 111 - (void)addBookmarksViewController:(UIViewController*)controller { | |
| 112 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:0]; | |
| 113 NewTabPageBarItem* item = self.NTPView.tabBar.items[0]; | |
| 114 item.view = controller.view; | |
| 115 [self addControllerToScrollView:controller]; | |
| 116 _bookmarksLoaded = YES; | |
| 117 } | |
| 118 | |
| 119 - (void)addOpenTabsViewController:(UIViewController*)controller { | |
| 120 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:2]; | |
| 121 NewTabPageBarItem* item = self.NTPView.tabBar.items[2]; | |
| 122 item.view = controller.view; | |
| 123 [self addControllerToScrollView:controller]; | |
| 124 _openTabsLoaded = YES; | |
| 125 } | |
| 126 | |
| 127 #pragma mark - NTPConsumer | |
| 128 | |
| 129 - (void)setBarItems:(NSMutableArray*)items { | |
| 130 _tabBarItems = items; | |
|
lpromero
2017/04/06 13:01:06
This supposes it is called before the view is load
justincohen
2017/04/06 18:25:11
Correct.
| |
| 131 } | |
| 132 | |
| 133 #pragma mark - UIScrollViewDelegate | |
| 134 | |
| 135 - (void)scrollViewDidScroll:(UIScrollView*)scrollView { | |
|
lpromero
2017/04/06 13:01:06
Could this logic be in a separate object whose sin
marq (ping after 24h)
2017/04/06 14:30:14
+1
justincohen
2017/04/06 18:25:11
Acknowledged.
justincohen
2017/04/06 18:25:11
There's not that much more logic necessary in this
| |
| 136 // Position is used to track the exact X position of the scroll view, whereas | |
| 137 // index is rounded to the panel that is most visible. | |
| 138 CGFloat panelWidth = | |
| 139 scrollView.contentSize.width / self.NTPView.tabBar.items.count; | |
| 140 LayoutOffset position = | |
| 141 LeadingContentOffsetForScrollView(scrollView) / panelWidth; | |
| 142 NSUInteger index = round(position); | |
| 143 | |
| 144 // |scrollView| can be out of range when the frame changes. | |
| 145 if (index >= self.NTPView.tabBar.items.count) | |
| 146 return; | |
| 147 | |
| 148 NewTabPageBarItem* item = self.NTPView.tabBar.items[index]; | |
| 149 if (item.identifier == NewTabPage::kBookmarksPanel && !_bookmarksLoaded) | |
| 150 [self.dispatcher showNTPBookmarksPanel]; | |
| 151 else if (item.identifier == NewTabPage::kMostVisitedPanel && !_homeLoaded) | |
| 152 [self.dispatcher showNTPHomePanel]; | |
| 153 else if (item.identifier == NewTabPage::kOpenTabsPanel && !_openTabsLoaded) | |
| 154 [self.dispatcher showNTPOpenTabsPanel]; | |
| 155 | |
| 156 // If index changed, follow same path as if a tab bar item was pressed. When | |
| 157 // |index| == |position|, the panel is completely in view. | |
| 158 if (index == position && self.NTPView.tabBar.selectedIndex != index) { | |
| 159 NewTabPageBarItem* item = [self.NTPView.tabBar.items objectAtIndex:index]; | |
| 160 DCHECK(item); | |
| 161 self.NTPView.tabBar.selectedIndex = index; | |
| 162 } | |
| 163 [self.NTPView.tabBar updateColorsForScrollView:scrollView]; | |
| 164 | |
| 165 self.NTPView.tabBar.overlayPercentage = | |
| 166 scrollView.contentOffset.x / scrollView.contentSize.width; | |
| 167 } | |
| 168 | |
| 169 #pragma mark - NewTabPageBarDelegate | |
| 170 | |
| 171 - (void)newTabBarItemDidChange:(NewTabPageBarItem*)selectedItem | |
| 172 changePanel:(BOOL)changePanel { | |
|
lpromero
2017/04/06 13:01:06
This is unused. Will it stay unused?
justincohen
2017/04/06 18:25:11
This is not unused. It's set in |tabBar.delegate
| |
| 173 if (IsIPadIdiom()) { | |
| 174 NSUInteger index = [self.NTPView.tabBar.items indexOfObject:selectedItem]; | |
| 175 CGRect itemFrame = [self.NTPView panelFrameForItemAtIndex:index]; | |
| 176 CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); | |
| 177 [self.NTPView.scrollView setContentOffset:point animated:YES]; | |
| 178 } else { | |
| 179 if (selectedItem.identifier == NewTabPage::kBookmarksPanel) { | |
| 180 [self.dispatcher showNTPBookmarksPanel]; | |
| 181 } else if (selectedItem.identifier == NewTabPage::kOpenTabsPanel) { | |
| 182 [self.dispatcher showNTPOpenTabsPanel]; | |
| 183 } | |
| 184 } | |
| 77 } | 185 } |
| 78 | 186 |
| 79 @end | 187 @end |
| OLD | NEW |