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/clean/chrome/browser/ui/ntp/ntp_view_controller.h" |
| 6 |
| 7 #import "base/ios/crb_protocol_observers.h" |
| 8 #include "components/strings/grit/components_strings.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/ntp/new_tab_page_controller.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" |
| 15 #include "ios/chrome/grit/ios_strings.h" |
| 16 #import "ios/clean/chrome/browser/ui/commands/ntp_commands.h" |
| 17 #include "ui/base/l10n/l10n_util.h" |
| 18 |
| 19 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 20 #error "This file requires ARC support." |
| 21 #endif |
| 22 |
| 23 @interface NTPViewController ()<UIScrollViewDelegate, NewTabPageBarDelegate> |
| 24 @property(nonatomic, strong) NewTabPageView* NTPView; |
| 25 @property(nonatomic, strong) NSArray* tabBarItems; |
| 26 @end |
| 27 |
| 28 @implementation NTPViewController |
| 29 |
| 30 @synthesize bookmarksViewController = _bookmarksViewController; |
| 31 @synthesize dispatcher = _dispatcher; |
| 32 @synthesize homeViewController = _homeViewController; |
| 33 @synthesize NTPView = _NTPView; |
| 34 @synthesize recentTabsViewController = _recentTabsViewController; |
| 35 @synthesize tabBarItems = _tabBarItems; |
| 36 |
| 37 #pragma mark - UIViewController |
| 38 |
| 39 - (void)viewDidLoad { |
| 40 [super viewDidLoad]; |
| 41 self.title = l10n_util::GetNSString(IDS_NEW_TAB_TITLE); |
| 42 self.view.backgroundColor = [UIColor whiteColor]; |
| 43 |
| 44 UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; |
| 45 [scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | |
| 46 UIViewAutoresizingFlexibleHeight)]; |
| 47 scrollView.pagingEnabled = YES; |
| 48 scrollView.showsHorizontalScrollIndicator = NO; |
| 49 scrollView.showsVerticalScrollIndicator = NO; |
| 50 scrollView.contentMode = UIViewContentModeScaleAspectFit; |
| 51 scrollView.bounces = YES; |
| 52 scrollView.scrollsToTop = NO; |
| 53 scrollView.delegate = self; |
| 54 |
| 55 NewTabPageBar* tabBar = [[NewTabPageBar alloc] initWithFrame:CGRectZero]; |
| 56 tabBar.delegate = self; |
| 57 self.NTPView = [[NewTabPageView alloc] initWithFrame:CGRectZero |
| 58 andScrollView:scrollView |
| 59 andTabBar:tabBar]; |
| 60 self.NTPView.translatesAutoresizingMaskIntoConstraints = NO; |
| 61 [self.view addSubview:self.NTPView]; |
| 62 |
| 63 [NSLayoutConstraint activateConstraints:@[ |
| 64 [self.NTPView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| 65 [self.NTPView.leadingAnchor |
| 66 constraintEqualToAnchor:self.view.leadingAnchor], |
| 67 [self.NTPView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| 68 [self.NTPView.trailingAnchor |
| 69 constraintEqualToAnchor:self.view.trailingAnchor], |
| 70 ]]; |
| 71 |
| 72 DCHECK(self.tabBarItems); |
| 73 self.NTPView.tabBar.items = self.tabBarItems; |
| 74 } |
| 75 |
| 76 - (void)viewDidLayoutSubviews { |
| 77 [super viewDidLayoutSubviews]; |
| 78 |
| 79 if (!self.homeViewController) { |
| 80 // Make sure scrollView is properly set up. |
| 81 [self.NTPView layoutIfNeeded]; |
| 82 // PLACEHOLDER: This should come from the mediator. |
| 83 if (IsIPadIdiom()) { |
| 84 CGRect itemFrame = [self.NTPView panelFrameForItemAtIndex:1]; |
| 85 CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
| 86 [self.NTPView.scrollView setContentOffset:point animated:NO]; |
| 87 } else { |
| 88 [self.dispatcher showNTPHomePanel]; |
| 89 } |
| 90 } |
| 91 } |
| 92 |
| 93 - (void)setHomeViewController:(UIViewController*)controller { |
| 94 _homeViewController = controller; |
| 95 if (IsIPadIdiom()) { |
| 96 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:1]; |
| 97 } else { |
| 98 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:0]; |
| 99 } |
| 100 NewTabPageBarItem* item = self.NTPView.tabBar.items[1]; |
| 101 item.view = controller.view; |
| 102 [self addControllerToScrollView:controller]; |
| 103 } |
| 104 |
| 105 - (void)setBookmarksViewController:(UIViewController*)controller { |
| 106 _bookmarksViewController = controller; |
| 107 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:0]; |
| 108 NewTabPageBarItem* item = self.NTPView.tabBar.items[0]; |
| 109 item.view = controller.view; |
| 110 [self addControllerToScrollView:controller]; |
| 111 } |
| 112 |
| 113 - (void)setRecentTabsViewController:(UIViewController*)controller { |
| 114 _recentTabsViewController = controller; |
| 115 controller.view.frame = [self.NTPView panelFrameForItemAtIndex:2]; |
| 116 NewTabPageBarItem* item = self.NTPView.tabBar.items[2]; |
| 117 item.view = controller.view; |
| 118 [self addControllerToScrollView:_recentTabsViewController]; |
| 119 } |
| 120 |
| 121 #pragma mark - Private |
| 122 |
| 123 // Add the view controller to vc hierarchy and add the view to the scrollview. |
| 124 - (void)addControllerToScrollView:(UIViewController*)controller { |
| 125 [self addChildViewController:controller]; |
| 126 [self.NTPView.scrollView addSubview:controller.view]; |
| 127 [controller didMoveToParentViewController:self]; |
| 128 } |
| 129 |
| 130 #pragma mark - NTPConsumer |
| 131 |
| 132 - (void)setBarItems:(NSArray*)items { |
| 133 self.tabBarItems = items; |
| 134 } |
| 135 |
| 136 #pragma mark - UIScrollViewDelegate |
| 137 |
| 138 // TODO(crbug.com/709086) Move UIScrollViewDelegate to shared object. |
| 139 - (void)scrollViewDidScroll:(UIScrollView*)scrollView { |
| 140 // Position is used to track the exact X position of the scroll view, whereas |
| 141 // index is rounded to the panel that is most visible. |
| 142 CGFloat panelWidth = |
| 143 scrollView.contentSize.width / self.NTPView.tabBar.items.count; |
| 144 LayoutOffset position = |
| 145 LeadingContentOffsetForScrollView(scrollView) / panelWidth; |
| 146 NSUInteger index = round(position); |
| 147 |
| 148 // |scrollView| can be out of range when the frame changes. |
| 149 if (index >= self.NTPView.tabBar.items.count) |
| 150 return; |
| 151 |
| 152 NewTabPageBarItem* item = self.NTPView.tabBar.items[index]; |
| 153 if (item.identifier == NewTabPage::kBookmarksPanel && |
| 154 !self.bookmarksViewController) |
| 155 [self.dispatcher showNTPBookmarksPanel]; |
| 156 else if (item.identifier == NewTabPage::kMostVisitedPanel && |
| 157 !self.homeViewController) |
| 158 [self.dispatcher showNTPHomePanel]; |
| 159 else if (item.identifier == NewTabPage::kOpenTabsPanel && |
| 160 !self.recentTabsViewController) |
| 161 [self.dispatcher showNTPRecentTabsPanel]; |
| 162 |
| 163 // If index changed, follow same path as if a tab bar item was pressed. When |
| 164 // |index| == |position|, the panel is completely in view. |
| 165 if (index == position && self.NTPView.tabBar.selectedIndex != index) { |
| 166 NewTabPageBarItem* item = [self.NTPView.tabBar.items objectAtIndex:index]; |
| 167 DCHECK(item); |
| 168 self.NTPView.tabBar.selectedIndex = index; |
| 169 } |
| 170 [self.NTPView.tabBar updateColorsForScrollView:scrollView]; |
| 171 |
| 172 self.NTPView.tabBar.overlayPercentage = |
| 173 scrollView.contentOffset.x / scrollView.contentSize.width; |
| 174 } |
| 175 |
| 176 #pragma mark - NewTabPageBarDelegate |
| 177 |
| 178 - (void)newTabBarItemDidChange:(NewTabPageBarItem*)selectedItem |
| 179 changePanel:(BOOL)changePanel { |
| 180 if (IsIPadIdiom()) { |
| 181 NSUInteger index = [self.NTPView.tabBar.items indexOfObject:selectedItem]; |
| 182 CGRect itemFrame = [self.NTPView panelFrameForItemAtIndex:index]; |
| 183 CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
| 184 [self.NTPView.scrollView setContentOffset:point animated:YES]; |
| 185 } else { |
| 186 if (selectedItem.identifier == NewTabPage::kBookmarksPanel) { |
| 187 [self.dispatcher showNTPBookmarksPanel]; |
| 188 } else if (selectedItem.identifier == NewTabPage::kOpenTabsPanel) { |
| 189 [self.dispatcher showNTPRecentTabsPanel]; |
| 190 } |
| 191 } |
| 192 } |
| 193 |
| 194 @end |
OLD | NEW |