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