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" |
14 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
15 | 17 |
16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
17 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
18 #endif | 20 #endif |
19 | 21 |
22 @interface NTPViewController ()<UIScrollViewDelegate, NewTabPageBarDelegate> { | |
23 @private | |
marq (ping after 24h)
2017/04/05 12:22:49
no need for @private here.
justincohen
2017/04/05 19:28:24
Done.
| |
24 BOOL _homeLoaded; | |
25 BOOL _openTabsLoaded; | |
26 BOOL _bookmarksLoaded; | |
27 BOOL _incognitoLoaded; | |
28 } | |
29 @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.
| |
30 @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.
| |
31 @end | |
32 | |
20 @implementation NTPViewController | 33 @implementation NTPViewController |
21 | 34 |
35 @synthesize ntpView = _ntpView; | |
36 @synthesize ntpCommandHandler = _ntpCommandHandler; | |
37 @synthesize tabBarItems = _tabBarItems; | |
38 | |
22 #pragma mark - UIViewController | 39 #pragma mark - UIViewController |
23 | 40 |
24 - (void)viewDidLoad { | 41 - (void)viewDidLoad { |
42 [super viewDidLoad]; | |
25 self.title = l10n_util::GetNSString(IDS_NEW_TAB_TITLE); | 43 self.title = l10n_util::GetNSString(IDS_NEW_TAB_TITLE); |
26 self.view.backgroundColor = [UIColor whiteColor]; | 44 self.view.backgroundColor = [UIColor whiteColor]; |
27 | 45 |
28 UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; | 46 UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero]; |
29 [scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | | 47 [scrollView setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | |
30 UIViewAutoresizingFlexibleHeight)]; | 48 UIViewAutoresizingFlexibleHeight)]; |
31 scrollView.pagingEnabled = YES; | 49 scrollView.pagingEnabled = YES; |
32 scrollView.showsHorizontalScrollIndicator = NO; | 50 scrollView.showsHorizontalScrollIndicator = NO; |
33 scrollView.showsVerticalScrollIndicator = NO; | 51 scrollView.showsVerticalScrollIndicator = NO; |
34 scrollView.contentMode = UIViewContentModeScaleAspectFit; | 52 scrollView.contentMode = UIViewContentModeScaleAspectFit; |
35 scrollView.bounces = YES; | 53 scrollView.bounces = YES; |
36 scrollView.scrollsToTop = NO; | 54 scrollView.scrollsToTop = NO; |
55 scrollView.delegate = self; | |
37 | 56 |
38 NewTabPageBar* tabBar = [[NewTabPageBar alloc] initWithFrame:CGRectZero]; | 57 NewTabPageBar* tabBar = [[NewTabPageBar alloc] initWithFrame:CGRectZero]; |
39 NewTabPageView* ntpView = [[NewTabPageView alloc] initWithFrame:CGRectZero | 58 tabBar.delegate = self; |
40 andScrollView:scrollView | 59 self.ntpView = [[NewTabPageView alloc] initWithFrame:CGRectZero |
41 andTabBar:tabBar]; | 60 andScrollView:scrollView |
42 ntpView.translatesAutoresizingMaskIntoConstraints = NO; | 61 andTabBar:tabBar]; |
43 [self.view addSubview:ntpView]; | 62 self.ntpView.translatesAutoresizingMaskIntoConstraints = NO; |
63 [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.
| |
44 | 64 |
45 [NSLayoutConstraint activateConstraints:@[ | 65 [NSLayoutConstraint activateConstraints:@[ |
46 [ntpView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 66 [_ntpView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
47 [ntpView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 67 [_ntpView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
48 [ntpView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 68 [_ntpView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
49 [ntpView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | 69 [_ntpView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
50 ]]; | 70 ]]; |
51 | 71 |
52 // PLACEHOLDER: this logic should move out of the UIVC. | 72 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.
| |
53 NSString* mostVisited = l10n_util::GetNSString(IDS_IOS_NEW_TAB_MOST_VISITED); | 73 } |
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 | 74 |
marq (ping after 24h)
2017/04/05 12:22:49
#pragma mark - NTPConsumer
justincohen
2017/04/05 19:28:24
Done.
| |
58 NSMutableArray* tabBarItems = [NSMutableArray array]; | 75 - (void)setTabBarItems:(NSMutableArray*)items { |
76 _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.
| |
77 } | |
59 | 78 |
60 NewTabPageBarItem* mostVisitedItem = [NewTabPageBarItem | 79 - (void)viewDidLayoutSubviews { |
61 newTabPageBarItemWithTitle:mostVisited | 80 [super viewDidLayoutSubviews]; |
62 identifier:NewTabPage::kMostVisitedPanel | |
63 image:[UIImage imageNamed:@"ntp_mv_search"]]; | |
64 NewTabPageBarItem* bookmarksItem = [NewTabPageBarItem | |
65 newTabPageBarItemWithTitle:bookmarks | |
66 identifier:NewTabPage::kBookmarksPanel | |
67 image:[UIImage imageNamed:@"ntp_bookmarks"]]; | |
68 [tabBarItems addObject:bookmarksItem]; | |
69 [tabBarItems addObject:mostVisitedItem]; | |
70 | 81 |
71 NewTabPageBarItem* openTabsItem = [NewTabPageBarItem | 82 [self.ntpView layoutIfNeeded]; |
72 newTabPageBarItemWithTitle:openTabs | 83 if (!_homeLoaded) { |
73 identifier:NewTabPage::kOpenTabsPanel | 84 // PLACEHOLDER: This should come from the mediator. |
74 image:[UIImage imageNamed:@"ntp_opentabs"]]; | 85 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'
| |
75 [tabBarItems addObject:openTabsItem]; | 86 CGRect itemFrame = [self.ntpView panelFrameForItemAtIndex:1]; |
76 tabBar.items = tabBarItems; | 87 CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
88 [self.ntpView.scrollView setContentOffset:point animated:NO]; | |
89 } else { | |
90 [self.ntpCommandHandler startNTPHomePanel]; | |
91 } | |
92 } | |
93 } | |
94 | |
95 - (void)scrollViewDidScroll:(UIScrollView*)scrollView { | |
96 // Position is used to track the exact X position of the scroll view, whereas | |
97 // index is rounded to the panel that is most visible. | |
98 CGFloat panelWidth = | |
99 scrollView.contentSize.width / self.ntpView.tabBar.items.count; | |
100 LayoutOffset position = | |
101 LeadingContentOffsetForScrollView(scrollView) / panelWidth; | |
102 NSUInteger index = round(position); | |
103 | |
104 // |scrollView| can be out of range when the frame changes. | |
105 if (index >= self.ntpView.tabBar.items.count) | |
106 return; | |
107 | |
108 NewTabPageBarItem* item = self.ntpView.tabBar.items[index]; | |
109 if (item.identifier == NewTabPage::kBookmarksPanel && !_bookmarksLoaded) | |
110 [self.ntpCommandHandler startNTPBookmarksPanel]; | |
111 else if (item.identifier == NewTabPage::kMostVisitedPanel && !_homeLoaded) | |
112 [self.ntpCommandHandler startNTPHomePanel]; | |
113 else if (item.identifier == NewTabPage::kOpenTabsPanel && !_openTabsLoaded) | |
114 [self.ntpCommandHandler startNTPOpenTabsPanel]; | |
115 else if (item.identifier == NewTabPage::kIncognitoPanel && !_incognitoLoaded) | |
116 [self.ntpCommandHandler startNTPIncognitoPanel]; | |
117 if (item.identifier == NewTabPage::kMostVisitedPanel && !_homeLoaded) | |
118 [self.ntpCommandHandler startNTPHomePanel]; | |
119 | |
120 // If index changed, follow same path as if a tab bar item was pressed. When | |
121 // |index| == |position|, the panel is completely in view. | |
122 if (index == position && self.ntpView.tabBar.selectedIndex != index) { | |
123 NewTabPageBarItem* item = [self.ntpView.tabBar.items objectAtIndex:index]; | |
124 DCHECK(item); | |
125 self.ntpView.tabBar.selectedIndex = index; | |
126 } | |
127 [self.ntpView.tabBar updateColorsForScrollView:scrollView]; | |
128 | |
129 _ntpView.tabBar.overlayPercentage = | |
130 scrollView.contentOffset.x / scrollView.contentSize.width; | |
131 } | |
132 | |
133 - (void)newTabBarItemDidChange:(NewTabPageBarItem*)selectedItem | |
134 changePanel:(BOOL)changePanel { | |
135 if (IsIPadIdiom()) { | |
136 NSUInteger index = [self.ntpView.tabBar.items indexOfObject:selectedItem]; | |
137 CGRect itemFrame = [self.ntpView panelFrameForItemAtIndex:index]; | |
138 CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); | |
139 [self.ntpView.scrollView setContentOffset:point animated:YES]; | |
140 } else { | |
141 if (selectedItem.identifier == NewTabPage::kBookmarksPanel) { | |
142 [self.ntpCommandHandler startNTPBookmarksPanel]; | |
143 } else if (selectedItem.identifier == NewTabPage::kOpenTabsPanel) { | |
144 [self.ntpCommandHandler startNTPOpenTabsPanel]; | |
145 } | |
146 } | |
147 } | |
148 | |
149 - (void)addControllerToScrollView:(UIViewController*)controller { | |
150 [self addChildViewController:controller]; | |
151 [_ntpView.scrollView addSubview:controller.view]; | |
152 [controller didMoveToParentViewController:self]; | |
153 } | |
154 | |
155 - (void)addHomePanelViewController:(UIViewController*)controller { | |
156 if (IsIPadIdiom()) { | |
157 controller.view.frame = [_ntpView panelFrameForItemAtIndex:1]; | |
158 } else { | |
159 controller.view.frame = [_ntpView panelFrameForItemAtIndex:0]; | |
160 } | |
161 NewTabPageBarItem* item = self.ntpView.tabBar.items[1]; | |
162 item.view = controller.view; | |
163 [self addControllerToScrollView:controller]; | |
164 _homeLoaded = YES; | |
165 } | |
166 | |
167 - (void)addBookmarksViewController:(UIViewController*)controller { | |
168 controller.view.frame = [_ntpView panelFrameForItemAtIndex:0]; | |
169 NewTabPageBarItem* item = self.ntpView.tabBar.items[0]; | |
170 item.view = controller.view; | |
171 [self addControllerToScrollView:controller]; | |
172 _bookmarksLoaded = YES; | |
173 } | |
174 | |
175 - (void)addOpenTabsViewController:(UIViewController*)controller { | |
176 controller.view.frame = [_ntpView panelFrameForItemAtIndex:2]; | |
177 NewTabPageBarItem* item = self.ntpView.tabBar.items[2]; | |
178 item.view = controller.view; | |
179 [self addControllerToScrollView:controller]; | |
180 _openTabsLoaded = YES; | |
181 } | |
182 | |
183 - (void)addIncognitoViewController:(UIViewController*)controller { | |
184 if (IsIPadIdiom()) { | |
185 controller.view.frame = [_ntpView panelFrameForItemAtIndex:1]; | |
186 } else { | |
187 controller.view.frame = [_ntpView panelFrameForItemAtIndex:0]; | |
188 } | |
189 NewTabPageBarItem* item = self.ntpView.tabBar.items[1]; | |
190 item.view = controller.view; | |
191 [self addControllerToScrollView:controller]; | |
192 _incognitoLoaded = YES; | |
77 } | 193 } |
78 | 194 |
79 @end | 195 @end |
OLD | NEW |