OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // ====== New Architecture ===== | 5 // ====== New Architecture ===== |
6 // = This code is only used in the new iOS Chrome architecture. = | 6 // = This code is only used in the new iOS Chrome architecture. = |
7 // ============================================================================ | 7 // ============================================================================ |
8 | 8 |
9 #import "ios/clean/chrome/browser/ui/tab/tab_container_view_controller.h" | 9 #import "ios/clean/chrome/browser/ui/tab/tab_container_view_controller.h" |
10 | 10 |
11 #import "base/mac/foundation_util.h" | |
12 #import "ios/clean/chrome/browser/ui/ui_types.h" | 11 #import "ios/clean/chrome/browser/ui/ui_types.h" |
13 | 12 |
14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
15 #error "This file requires ARC support." | 14 #error "This file requires ARC support." |
16 #endif | 15 #endif |
17 | 16 |
18 namespace { | 17 namespace { |
19 CGFloat kToolbarHeight = 44.0; | 18 CGFloat kToolbarHeight = 44.0f; |
| 19 CGFloat kTabStripHeight = 200.0f; |
20 } | 20 } |
21 | 21 |
22 @interface TabContainerViewController () | 22 @interface TabContainerViewController () |
23 | 23 |
24 // Whichever view controller is at the top of the screen. This view controller | 24 // Container views for child view controllers. The child view controller's |
25 // controls the status bar. | 25 // view is added as a subview that fills it's container view via autoresizing. |
26 @property(nonatomic, weak) UIViewController* topmostViewController; | 26 @property(nonatomic, strong) UIView* tabStripView; |
| 27 @property(nonatomic, strong) UIView* toolbarView; |
| 28 @property(nonatomic, strong) UIView* contentView; |
27 | 29 |
28 @property(nonatomic, strong) Constraints* contentConstraintsWithToolbar; | 30 // Height constraints for tabStripView and toolbarView. |
29 @property(nonatomic, strong) Constraints* contentConstraintsWithoutToolbar; | 31 @property(nonatomic, strong) NSLayoutConstraint* tabStripHeightConstraint; |
30 @property(nonatomic, strong) Constraints* toolbarConstraints; | 32 @property(nonatomic, strong) NSLayoutConstraint* toolbarHeightConstraint; |
31 | 33 |
32 // Cache for forwarding methods to child view controllers. | 34 // Cache for forwarding methods to child view controllers. |
33 @property(nonatomic, assign) SEL actionToForward; | 35 @property(nonatomic, assign) SEL actionToForward; |
34 @property(nonatomic, weak) UIResponder* forwardingTarget; | 36 @property(nonatomic, weak) UIResponder* forwardingTarget; |
35 | 37 |
36 // Contained view controller utility methods. | 38 // Abstract base method for subclasses to implement. |
37 - (void)removeChildViewController:(UIViewController*)viewController; | 39 // Returns constraints for tabStrip, toolbar, and content subviews. |
38 | 40 - (Constraints*)subviewConstraints; |
39 // Called after a new content view controller is set, but before | |
40 // |-didMoveToParentViewController:| is called on that view controller. | |
41 - (void)didAddContentViewController; | |
42 | |
43 // Called after a new toolbar view controller is set, but before | |
44 // |-didMoveToParentViewController:| is called on that view controller. | |
45 - (void)didAddToolbarViewController; | |
46 | |
47 // Methods to populate the constraint properties. | |
48 - (void)updateContentConstraintsWithToolbar; | |
49 - (void)updateContentConstraintsWithoutToolbar; | |
50 - (void)updateToolbarConstraints; | |
51 | 41 |
52 @end | 42 @end |
53 | 43 |
54 @implementation TabContainerViewController | 44 @implementation TabContainerViewController |
55 | 45 |
56 @synthesize contentViewController = _contentViewController; | 46 @synthesize contentViewController = _contentViewController; |
57 @synthesize toolbarViewController = _toolbarViewController; | 47 @synthesize toolbarViewController = _toolbarViewController; |
58 @synthesize topmostViewController = _topmostViewController; | 48 @synthesize tabStripViewController = _tabStripViewController; |
59 @synthesize contentConstraintsWithToolbar = _contentConstraintsWithToolbar; | 49 @synthesize tabStripView = _tabStripView; |
60 @synthesize contentConstraintsWithoutToolbar = | 50 @synthesize toolbarView = _toolbarView; |
61 _contentConstraintsWithoutToolbar; | 51 @synthesize contentView = _contentView; |
62 @synthesize toolbarConstraints = _toolbarConstraints; | 52 @synthesize tabStripHeightConstraint = _tabStripHeightConstraint; |
| 53 @synthesize toolbarHeightConstraint = _toolbarHeightConstraint; |
63 @synthesize actionToForward = _actionToForward; | 54 @synthesize actionToForward = _actionToForward; |
64 @synthesize forwardingTarget = _forwardingTarget; | 55 @synthesize forwardingTarget = _forwardingTarget; |
65 | 56 |
| 57 #pragma mark - UIViewController |
| 58 |
| 59 - (void)viewDidLoad { |
| 60 [super viewDidLoad]; |
| 61 self.tabStripView = [[UIView alloc] init]; |
| 62 self.toolbarView = [[UIView alloc] init]; |
| 63 self.contentView = [[UIView alloc] init]; |
| 64 [self.view addSubview:self.tabStripView]; |
| 65 [self.view addSubview:self.toolbarView]; |
| 66 [self.view addSubview:self.contentView]; |
| 67 self.tabStripView.translatesAutoresizingMaskIntoConstraints = NO; |
| 68 self.toolbarView.translatesAutoresizingMaskIntoConstraints = NO; |
| 69 self.contentView.translatesAutoresizingMaskIntoConstraints = NO; |
| 70 self.view.backgroundColor = [UIColor blackColor]; |
| 71 self.tabStripView.backgroundColor = [UIColor blackColor]; |
| 72 self.toolbarView.backgroundColor = [UIColor blackColor]; |
| 73 self.contentView.backgroundColor = [UIColor blackColor]; |
| 74 |
| 75 [self addChildViewController:self.tabStripViewController |
| 76 toSubview:self.tabStripView]; |
| 77 [self addChildViewController:self.toolbarViewController |
| 78 toSubview:self.toolbarView]; |
| 79 [self addChildViewController:self.contentViewController |
| 80 toSubview:self.contentView]; |
| 81 |
| 82 self.tabStripHeightConstraint = |
| 83 [self.tabStripView.heightAnchor constraintEqualToConstant:0.0f]; |
| 84 self.toolbarHeightConstraint = |
| 85 [self.toolbarView.heightAnchor constraintEqualToConstant:0.0f]; |
| 86 if (self.toolbarViewController) { |
| 87 self.toolbarHeightConstraint.constant = kToolbarHeight; |
| 88 } |
| 89 |
| 90 [NSLayoutConstraint activateConstraints:[self subviewConstraints]]; |
| 91 } |
| 92 |
66 #pragma mark - Public properties | 93 #pragma mark - Public properties |
67 | 94 |
68 - (void)setContentViewController:(UIViewController*)contentViewController { | 95 - (void)setContentViewController:(UIViewController*)contentViewController { |
69 if (self.contentViewController == contentViewController) | 96 if (self.contentViewController == contentViewController) |
70 return; | 97 return; |
71 | 98 if ([self isViewLoaded]) { |
72 // Remove the current content view controller, if any. | 99 [self removeChildViewController:self.contentViewController]; |
73 [NSLayoutConstraint | 100 [self addChildViewController:contentViewController |
74 deactivateConstraints:self.contentConstraintsWithoutToolbar]; | 101 toSubview:self.contentView]; |
75 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithToolbar]; | 102 } |
76 [self removeChildViewController:self.contentViewController]; | |
77 | |
78 // Add the new content view controller. | |
79 [self addChildViewController:contentViewController]; | |
80 contentViewController.view.translatesAutoresizingMaskIntoConstraints = NO; | |
81 [self.view addSubview:contentViewController.view]; | |
82 _contentViewController = contentViewController; | 103 _contentViewController = contentViewController; |
83 [self didAddContentViewController]; | |
84 [self.view setNeedsUpdateConstraints]; | |
85 [self.contentViewController didMoveToParentViewController:self]; | |
86 } | 104 } |
87 | 105 |
88 - (void)setToolbarViewController:(UIViewController*)toolbarViewController { | 106 - (void)setToolbarViewController:(UIViewController*)toolbarViewController { |
89 if (self.toolbarViewController == toolbarViewController) | 107 if (self.toolbarViewController == toolbarViewController) |
90 return; | 108 return; |
91 | 109 if ([self isViewLoaded]) { |
92 // Remove the current toolbar view controller, if any. | 110 [self removeChildViewController:self.toolbarViewController]; |
93 [NSLayoutConstraint deactivateConstraints:self.toolbarConstraints]; | 111 [self addChildViewController:toolbarViewController |
94 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithToolbar]; | 112 toSubview:self.toolbarView]; |
95 [self removeChildViewController:self.toolbarViewController]; | 113 } |
96 | |
97 // Add the new toolbar view controller. | |
98 [self addChildViewController:toolbarViewController]; | |
99 toolbarViewController.view.translatesAutoresizingMaskIntoConstraints = NO; | |
100 [self.view addSubview:toolbarViewController.view]; | |
101 _toolbarViewController = toolbarViewController; | 114 _toolbarViewController = toolbarViewController; |
102 [self didAddToolbarViewController]; | |
103 [self.view setNeedsUpdateConstraints]; | |
104 [self.toolbarViewController didMoveToParentViewController:self]; | |
105 } | 115 } |
106 | 116 |
107 #pragma mark - UIViewController | 117 - (void)setTabStripViewController:(UIViewController*)tabStripViewController { |
108 | 118 if (self.tabStripViewController == tabStripViewController) |
109 - (void)updateViewConstraints { | 119 return; |
110 if (self.toolbarViewController) { | 120 if ([self isViewLoaded]) { |
111 [NSLayoutConstraint activateConstraints:self.toolbarConstraints]; | 121 [self removeChildViewController:self.tabStripViewController]; |
112 [NSLayoutConstraint activateConstraints:self.contentConstraintsWithToolbar]; | 122 [self addChildViewController:tabStripViewController |
113 } else { | 123 toSubview:self.tabStripView]; |
114 [NSLayoutConstraint | |
115 activateConstraints:self.contentConstraintsWithoutToolbar]; | |
116 } | 124 } |
117 [super updateViewConstraints]; | 125 _tabStripViewController = tabStripViewController; |
118 } | 126 } |
119 | 127 |
120 - (UIViewController*)childViewControllerForStatusBarHidden { | 128 #pragma mark - ChildViewController helper methods |
121 return self.topmostViewController; | 129 |
| 130 - (void)addChildViewController:(UIViewController*)viewController |
| 131 toSubview:(UIView*)subview { |
| 132 if (!viewController || !subview) { |
| 133 return; |
| 134 } |
| 135 [self addChildViewController:viewController]; |
| 136 viewController.view.translatesAutoresizingMaskIntoConstraints = YES; |
| 137 viewController.view.autoresizingMask = |
| 138 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 139 viewController.view.frame = subview.bounds; |
| 140 [subview addSubview:viewController.view]; |
| 141 [viewController didMoveToParentViewController:self]; |
122 } | 142 } |
123 | 143 |
124 - (UIViewController*)childViewControllerForStatusBarStyle { | 144 - (void)removeChildViewController:(UIViewController*)viewController { |
125 return self.topmostViewController; | 145 if (viewController.parentViewController != self) |
| 146 return; |
| 147 [viewController willMoveToParentViewController:nil]; |
| 148 [viewController.view removeFromSuperview]; |
| 149 [viewController removeFromParentViewController]; |
126 } | 150 } |
127 | 151 |
128 #pragma mark - MenuPresentationDelegate | 152 #pragma mark - MenuPresentationDelegate |
129 | 153 |
130 - (CGRect)frameForMenuPresentation:(UIPresentationController*)presentation { | 154 - (CGRect)frameForMenuPresentation:(UIPresentationController*)presentation { |
131 CGSize menuSize = presentation.presentedView.frame.size; | 155 CGSize menuSize = presentation.presentedView.frame.size; |
132 CGRect menuRect; | 156 CGRect menuRect; |
133 menuRect.size = menuSize; | 157 menuRect.size = menuSize; |
134 | 158 |
135 CGRect menuOriginRect = [self rectForZoomWithKey:@"" inView:self.view]; | 159 CGRect menuOriginRect = [self rectForZoomWithKey:@"" inView:self.view]; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 216 |
193 #pragma mark - NSObject method forwarding | 217 #pragma mark - NSObject method forwarding |
194 | 218 |
195 - (id)forwardingTargetForSelector:(SEL)aSelector { | 219 - (id)forwardingTargetForSelector:(SEL)aSelector { |
196 if (aSelector == self.actionToForward) { | 220 if (aSelector == self.actionToForward) { |
197 return self.forwardingTarget; | 221 return self.forwardingTarget; |
198 } | 222 } |
199 return nil; | 223 return nil; |
200 } | 224 } |
201 | 225 |
202 #pragma mark - Private methods | 226 #pragma mark - TabStripActions |
203 | 227 |
204 - (void)removeChildViewController:(UIViewController*)viewController { | 228 - (void)toggleTabStrip:(id)sender { |
205 if (viewController.parentViewController != self) | 229 self.tabStripHeightConstraint.constant = |
206 return; | 230 self.tabStripHeightConstraint.constant > 0.0f ? 0.0f : kTabStripHeight; |
207 [viewController willMoveToParentViewController:nil]; | |
208 [viewController.view removeFromSuperview]; | |
209 [viewController removeFromParentViewController]; | |
210 } | 231 } |
211 | 232 |
212 - (void)didAddContentViewController { | 233 #pragma mark - Abstract methods to be overriden by subclass |
213 if (self.toolbarViewController) { | |
214 [self updateContentConstraintsWithToolbar]; | |
215 } else { | |
216 self.topmostViewController = self.contentViewController; | |
217 [self updateContentConstraintsWithoutToolbar]; | |
218 } | |
219 } | |
220 | 234 |
221 - (void)didAddToolbarViewController { | 235 - (Constraints*)subviewConstraints { |
222 [self updateToolbarConstraints]; | 236 [NSException |
223 // If there's already a content view controller, update the constraints for | 237 raise:NSInternalInconsistencyException |
224 // that, too. | 238 format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]; |
225 if (self.contentViewController) { | 239 return nil; |
226 [self updateContentConstraintsWithToolbar]; | |
227 } | |
228 } | |
229 | |
230 - (void)updateContentConstraintsWithToolbar { | |
231 // Template method for subclasses to implement; | |
232 } | |
233 | |
234 - (void)updateToolbarConstraints { | |
235 // Template method for subclasses to implement; | |
236 } | |
237 | |
238 - (void)updateContentConstraintsWithoutToolbar { | |
239 UIView* contentView = self.contentViewController.view; | |
240 self.contentConstraintsWithoutToolbar = @[ | |
241 [contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | |
242 [contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | |
243 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
244 [contentView.trailingAnchor | |
245 constraintEqualToAnchor:self.view.trailingAnchor], | |
246 ]; | |
247 } | 240 } |
248 | 241 |
249 @end | 242 @end |
250 | 243 |
251 @implementation TopToolbarTabViewController | 244 @implementation TopToolbarTabViewController |
252 | 245 |
253 - (void)didAddContentViewController { | 246 // Override with constraints that place the toolbar on top. |
254 [super didAddContentViewController]; | 247 - (Constraints*)subviewConstraints { |
255 if (!self.toolbarViewController) { | 248 return @[ |
256 self.topmostViewController = self.contentViewController; | 249 [self.tabStripView.topAnchor |
257 } | 250 constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor], |
258 } | 251 [self.tabStripView.leadingAnchor |
259 | 252 constraintEqualToAnchor:self.view.leadingAnchor], |
260 - (void)didAddToolbarViewController { | 253 [self.tabStripView.trailingAnchor |
261 [super didAddToolbarViewController]; | |
262 self.topmostViewController = self.toolbarViewController; | |
263 } | |
264 | |
265 - (void)updateContentConstraintsWithToolbar { | |
266 UIView* contentView = self.contentViewController.view; | |
267 self.contentConstraintsWithToolbar = @[ | |
268 [contentView.topAnchor | |
269 constraintEqualToAnchor:self.toolbarViewController.view.bottomAnchor], | |
270 [contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | |
271 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
272 [contentView.trailingAnchor | |
273 constraintEqualToAnchor:self.view.trailingAnchor], | 254 constraintEqualToAnchor:self.view.trailingAnchor], |
| 255 self.tabStripHeightConstraint, |
| 256 [self.toolbarView.topAnchor |
| 257 constraintEqualToAnchor:self.tabStripView.bottomAnchor], |
| 258 [self.toolbarView.leadingAnchor |
| 259 constraintEqualToAnchor:self.view.leadingAnchor], |
| 260 [self.toolbarView.trailingAnchor |
| 261 constraintEqualToAnchor:self.view.trailingAnchor], |
| 262 self.toolbarHeightConstraint, |
| 263 [self.contentView.topAnchor |
| 264 constraintEqualToAnchor:self.toolbarView.bottomAnchor], |
| 265 [self.contentView.leadingAnchor |
| 266 constraintEqualToAnchor:self.view.leadingAnchor], |
| 267 [self.contentView.trailingAnchor |
| 268 constraintEqualToAnchor:self.view.trailingAnchor], |
| 269 [self.contentView.bottomAnchor |
| 270 constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor], |
274 ]; | 271 ]; |
275 } | 272 } |
276 | 273 |
277 - (void)updateToolbarConstraints { | |
278 // HACK: This background is added so the status bar portion of the view is not | |
279 // transparent. This needs to be implemented properly for the top toolbar | |
280 // case. | |
281 self.view.backgroundColor = [UIColor lightGrayColor]; | |
282 UIView* toolbarView = self.toolbarViewController.view; | |
283 self.toolbarConstraints = @[ | |
284 [toolbarView.topAnchor constraintEqualToAnchor:self.view.topAnchor | |
285 constant:20.0], | |
286 [toolbarView.heightAnchor constraintEqualToConstant:kToolbarHeight], | |
287 [toolbarView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
288 [toolbarView.trailingAnchor | |
289 constraintEqualToAnchor:self.view.trailingAnchor], | |
290 ]; | |
291 } | |
292 | |
293 @end | 274 @end |
294 | 275 |
295 @implementation BottomToolbarTabViewController | 276 @implementation BottomToolbarTabViewController |
296 | 277 |
297 - (void)didAddContentViewController { | 278 // Override with constraints that place the toolbar on bottom. |
298 [super didAddContentViewController]; | 279 - (Constraints*)subviewConstraints { |
299 self.topmostViewController = self.contentViewController; | 280 return @[ |
300 } | 281 [self.tabStripView.topAnchor |
301 | 282 constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor], |
302 // Note that this class doesn't override -didAddToolbarViewController; in the | 283 [self.tabStripView.leadingAnchor |
303 // case where there is a toolbar view controller set but not a content view | 284 constraintEqualToAnchor:self.view.leadingAnchor], |
304 // controller, functionally there is no topmost view controller, so no | 285 [self.tabStripView.trailingAnchor |
305 // additional action needs to be taken. | |
306 | |
307 - (void)updateContentConstraintsWithToolbar { | |
308 UIView* contentView = self.contentViewController.view; | |
309 self.contentConstraintsWithToolbar = @[ | |
310 [contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | |
311 [contentView.bottomAnchor | |
312 constraintEqualToAnchor:self.toolbarViewController.view.topAnchor], | |
313 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
314 [contentView.trailingAnchor | |
315 constraintEqualToAnchor:self.view.trailingAnchor], | 286 constraintEqualToAnchor:self.view.trailingAnchor], |
| 287 self.tabStripHeightConstraint, |
| 288 [self.contentView.topAnchor |
| 289 constraintEqualToAnchor:self.tabStripView.bottomAnchor], |
| 290 [self.contentView.leadingAnchor |
| 291 constraintEqualToAnchor:self.view.leadingAnchor], |
| 292 [self.contentView.trailingAnchor |
| 293 constraintEqualToAnchor:self.view.trailingAnchor], |
| 294 [self.toolbarView.topAnchor |
| 295 constraintEqualToAnchor:self.contentView.bottomAnchor], |
| 296 [self.toolbarView.leadingAnchor |
| 297 constraintEqualToAnchor:self.view.leadingAnchor], |
| 298 [self.toolbarView.trailingAnchor |
| 299 constraintEqualToAnchor:self.view.trailingAnchor], |
| 300 self.toolbarHeightConstraint, |
| 301 [self.toolbarView.bottomAnchor |
| 302 constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor], |
316 ]; | 303 ]; |
317 } | 304 } |
318 | 305 |
319 - (void)updateToolbarConstraints { | |
320 UIView* toolbarView = self.toolbarViewController.view; | |
321 self.toolbarConstraints = @[ | |
322 [toolbarView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | |
323 [toolbarView.heightAnchor constraintEqualToConstant:kToolbarHeight], | |
324 [toolbarView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
325 [toolbarView.trailingAnchor | |
326 constraintEqualToAnchor:self.view.trailingAnchor], | |
327 ]; | |
328 } | |
329 | |
330 @end | 306 @end |
OLD | NEW |