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 // Constraints for the container views. This may be overrided in |
marq (ping after 24h)
2017/02/16 12:55:56
overrided -> overridden. Yay, English!
| |
29 @property(nonatomic, strong) Constraints* contentConstraintsWithoutToolbar; | 31 // -updateViewConstraints in a subclass. |
30 @property(nonatomic, strong) Constraints* toolbarConstraints; | 32 @property(nonatomic, strong) Constraints* constraints; |
33 | |
34 // Height constraints for tabStripView and toolbarView. | |
35 @property(nonatomic, strong) NSLayoutConstraint* tabStripHeightConstraint; | |
36 @property(nonatomic, strong) NSLayoutConstraint* toolbarHeightConstraint; | |
31 | 37 |
32 // Cache for forwarding methods to child view controllers. | 38 // Cache for forwarding methods to child view controllers. |
33 @property(nonatomic, assign) SEL actionToForward; | 39 @property(nonatomic, assign) SEL actionToForward; |
34 @property(nonatomic, weak) UIResponder* forwardingTarget; | 40 @property(nonatomic, weak) UIResponder* forwardingTarget; |
35 | 41 |
36 // Contained view controller utility methods. | |
37 - (void)removeChildViewController:(UIViewController*)viewController; | |
38 | |
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 | |
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 constraints = _constraints; |
53 @synthesize tabStripHeightConstraint = _tabStripHeightConstraint; | |
54 @synthesize toolbarHeightConstraint = _toolbarHeightConstraint; | |
63 @synthesize actionToForward = _actionToForward; | 55 @synthesize actionToForward = _actionToForward; |
64 @synthesize forwardingTarget = _forwardingTarget; | 56 @synthesize forwardingTarget = _forwardingTarget; |
65 | 57 |
58 #pragma mark - UIViewController | |
59 | |
60 - (void)viewDidLoad { | |
61 [super viewDidLoad]; | |
62 self.tabStripView = [[UIView alloc] init]; | |
63 self.toolbarView = [[UIView alloc] init]; | |
64 self.contentView = [[UIView alloc] init]; | |
65 [self.view addSubview:self.tabStripView]; | |
66 [self.view addSubview:self.toolbarView]; | |
67 [self.view addSubview:self.contentView]; | |
68 self.tabStripView.translatesAutoresizingMaskIntoConstraints = NO; | |
69 self.toolbarView.translatesAutoresizingMaskIntoConstraints = NO; | |
70 self.contentView.translatesAutoresizingMaskIntoConstraints = NO; | |
71 self.view.backgroundColor = [UIColor blackColor]; | |
72 self.tabStripView.backgroundColor = [UIColor blackColor]; | |
73 self.toolbarView.backgroundColor = [UIColor blackColor]; | |
74 self.contentView.backgroundColor = [UIColor blackColor]; | |
75 | |
76 [self addChildViewController:self.tabStripViewController | |
77 toSubview:self.tabStripView]; | |
78 [self addChildViewController:self.toolbarViewController | |
79 toSubview:self.toolbarView]; | |
80 [self addChildViewController:self.contentViewController | |
81 toSubview:self.contentView]; | |
82 | |
83 self.tabStripHeightConstraint = | |
84 [self.tabStripView.heightAnchor constraintEqualToConstant:0.0f]; | |
85 self.toolbarHeightConstraint = | |
86 [self.toolbarView.heightAnchor constraintEqualToConstant:0.0f]; | |
87 if (self.toolbarViewController) { | |
88 self.toolbarHeightConstraint.constant = kToolbarHeight; | |
89 } | |
90 } | |
91 | |
92 - (void)updateViewConstraints { | |
93 if (!self.constraints) { | |
94 self.constraints = @[ | |
95 [self.tabStripView.topAnchor | |
96 constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor], | |
97 [self.tabStripView.leadingAnchor | |
98 constraintEqualToAnchor:self.view.leadingAnchor], | |
99 [self.tabStripView.trailingAnchor | |
100 constraintEqualToAnchor:self.view.trailingAnchor], | |
101 self.tabStripHeightConstraint, | |
102 [self.toolbarView.topAnchor | |
103 constraintEqualToAnchor:self.tabStripView.bottomAnchor], | |
104 [self.toolbarView.leadingAnchor | |
105 constraintEqualToAnchor:self.view.leadingAnchor], | |
106 [self.toolbarView.trailingAnchor | |
107 constraintEqualToAnchor:self.view.trailingAnchor], | |
108 self.toolbarHeightConstraint, | |
109 [self.contentView.topAnchor | |
110 constraintEqualToAnchor:self.toolbarView.bottomAnchor], | |
111 [self.contentView.leadingAnchor | |
112 constraintEqualToAnchor:self.view.leadingAnchor], | |
113 [self.contentView.trailingAnchor | |
114 constraintEqualToAnchor:self.view.trailingAnchor], | |
115 [self.contentView.bottomAnchor | |
116 constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor], | |
117 ]; | |
118 [NSLayoutConstraint activateConstraints:self.constraints]; | |
119 } | |
120 [super updateViewConstraints]; | |
121 } | |
122 | |
66 #pragma mark - Public properties | 123 #pragma mark - Public properties |
67 | 124 |
68 - (void)setContentViewController:(UIViewController*)contentViewController { | 125 - (void)setContentViewController:(UIViewController*)contentViewController { |
69 if (self.contentViewController == contentViewController) | 126 if (self.contentViewController == contentViewController) |
70 return; | 127 return; |
71 | 128 if ([self isViewLoaded]) { |
72 // Remove the current content view controller, if any. | 129 [self removeChildViewController:self.contentViewController]; |
73 [NSLayoutConstraint | 130 [self addChildViewController:contentViewController |
74 deactivateConstraints:self.contentConstraintsWithoutToolbar]; | 131 toSubview:self.contentView]; |
75 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithToolbar]; | 132 } |
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; | 133 _contentViewController = contentViewController; |
83 [self didAddContentViewController]; | |
84 [self.view setNeedsUpdateConstraints]; | |
85 [self.contentViewController didMoveToParentViewController:self]; | |
86 } | 134 } |
87 | 135 |
88 - (void)setToolbarViewController:(UIViewController*)toolbarViewController { | 136 - (void)setToolbarViewController:(UIViewController*)toolbarViewController { |
89 if (self.toolbarViewController == toolbarViewController) | 137 if (self.toolbarViewController == toolbarViewController) |
90 return; | 138 return; |
91 | 139 if ([self isViewLoaded]) { |
92 // Remove the current toolbar view controller, if any. | 140 [self removeChildViewController:self.toolbarViewController]; |
93 [NSLayoutConstraint deactivateConstraints:self.toolbarConstraints]; | 141 [self addChildViewController:toolbarViewController |
94 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithToolbar]; | 142 toSubview:self.toolbarView]; |
95 [self removeChildViewController:self.toolbarViewController]; | 143 } |
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; | 144 _toolbarViewController = toolbarViewController; |
102 [self didAddToolbarViewController]; | |
103 [self.view setNeedsUpdateConstraints]; | |
104 [self.toolbarViewController didMoveToParentViewController:self]; | |
105 } | 145 } |
106 | 146 |
107 #pragma mark - UIViewController | 147 - (void)setTabStripViewController:(UIViewController*)tabStripViewController { |
108 | 148 if (self.tabStripViewController == tabStripViewController) |
109 - (void)updateViewConstraints { | 149 return; |
110 if (self.toolbarViewController) { | 150 if ([self isViewLoaded]) { |
111 [NSLayoutConstraint activateConstraints:self.toolbarConstraints]; | 151 [self removeChildViewController:self.tabStripViewController]; |
112 [NSLayoutConstraint activateConstraints:self.contentConstraintsWithToolbar]; | 152 [self addChildViewController:tabStripViewController |
113 } else { | 153 toSubview:self.tabStripView]; |
114 [NSLayoutConstraint | |
115 activateConstraints:self.contentConstraintsWithoutToolbar]; | |
116 } | 154 } |
117 [super updateViewConstraints]; | 155 _tabStripViewController = tabStripViewController; |
118 } | 156 } |
119 | 157 |
120 - (UIViewController*)childViewControllerForStatusBarHidden { | 158 #pragma mark - ChildViewController helper methods |
121 return self.topmostViewController; | 159 |
160 - (void)addChildViewController:(UIViewController*)viewController | |
161 toSubview:(UIView*)subview { | |
162 if (!viewController || !subview) { | |
163 return; | |
164 } | |
165 [self addChildViewController:viewController]; | |
166 viewController.view.translatesAutoresizingMaskIntoConstraints = YES; | |
167 viewController.view.autoresizingMask = | |
168 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; | |
169 viewController.view.frame = subview.bounds; | |
170 [subview addSubview:viewController.view]; | |
171 [viewController didMoveToParentViewController:self]; | |
122 } | 172 } |
123 | 173 |
124 - (UIViewController*)childViewControllerForStatusBarStyle { | 174 - (void)removeChildViewController:(UIViewController*)viewController { |
125 return self.topmostViewController; | 175 if (viewController.parentViewController != self) |
176 return; | |
177 [viewController willMoveToParentViewController:nil]; | |
178 [viewController.view removeFromSuperview]; | |
179 [viewController removeFromParentViewController]; | |
126 } | 180 } |
127 | 181 |
128 #pragma mark - MenuPresentationDelegate | 182 #pragma mark - MenuPresentationDelegate |
129 | 183 |
130 - (CGRect)frameForMenuPresentation:(UIPresentationController*)presentation { | 184 - (CGRect)frameForMenuPresentation:(UIPresentationController*)presentation { |
131 CGSize menuSize = presentation.presentedView.frame.size; | 185 CGSize menuSize = presentation.presentedView.frame.size; |
132 CGRect menuRect; | 186 CGRect menuRect; |
133 menuRect.size = menuSize; | 187 menuRect.size = menuSize; |
134 | 188 |
135 CGRect menuOriginRect = [self rectForZoomWithKey:@"" inView:self.view]; | 189 CGRect menuOriginRect = [self rectForZoomWithKey:@"" inView:self.view]; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 | 246 |
193 #pragma mark - NSObject method forwarding | 247 #pragma mark - NSObject method forwarding |
194 | 248 |
195 - (id)forwardingTargetForSelector:(SEL)aSelector { | 249 - (id)forwardingTargetForSelector:(SEL)aSelector { |
196 if (aSelector == self.actionToForward) { | 250 if (aSelector == self.actionToForward) { |
197 return self.forwardingTarget; | 251 return self.forwardingTarget; |
198 } | 252 } |
199 return nil; | 253 return nil; |
200 } | 254 } |
201 | 255 |
202 #pragma mark - Private methods | 256 #pragma mark - TabStripActions |
203 | 257 |
204 - (void)removeChildViewController:(UIViewController*)viewController { | 258 - (void)toggleTabStrip:(id)sender { |
205 if (viewController.parentViewController != self) | 259 self.tabStripHeightConstraint.constant = |
206 return; | 260 self.tabStripHeightConstraint.constant > 0.0f ? 0.0f : kTabStripHeight; |
207 [viewController willMoveToParentViewController:nil]; | |
208 [viewController.view removeFromSuperview]; | |
209 [viewController removeFromParentViewController]; | |
210 } | |
211 | |
212 - (void)didAddContentViewController { | |
213 if (self.toolbarViewController) { | |
214 [self updateContentConstraintsWithToolbar]; | |
215 } else { | |
216 self.topmostViewController = self.contentViewController; | |
217 [self updateContentConstraintsWithoutToolbar]; | |
218 } | |
219 } | |
220 | |
221 - (void)didAddToolbarViewController { | |
222 [self updateToolbarConstraints]; | |
223 // If there's already a content view controller, update the constraints for | |
224 // that, too. | |
225 if (self.contentViewController) { | |
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 } | |
248 | |
249 @end | |
250 | |
251 @implementation TopToolbarTabViewController | |
marq (ping after 24h)
2017/02/16 12:55:56
I would like to keep TopToolbar and BottomToolbar
edchin
2017/02/16 16:12:05
I've implemented this in the updated patch.
| |
252 | |
253 - (void)didAddContentViewController { | |
254 [super didAddContentViewController]; | |
255 if (!self.toolbarViewController) { | |
256 self.topmostViewController = self.contentViewController; | |
257 } | |
258 } | |
259 | |
260 - (void)didAddToolbarViewController { | |
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], | |
274 ]; | |
275 } | |
276 | |
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 } | 261 } |
292 | 262 |
293 @end | 263 @end |
294 | 264 |
295 @implementation BottomToolbarTabViewController | 265 @implementation BottomToolbarTabViewController |
296 | 266 |
297 - (void)didAddContentViewController { | 267 // Override with different constraints. |
298 [super didAddContentViewController]; | 268 - (void)updateViewConstraints { |
299 self.topmostViewController = self.contentViewController; | 269 if (!self.constraints) { |
marq (ping after 24h)
2017/02/16 12:55:56
Instead of overriding -updateViewConstraints, I'd
edchin
2017/02/16 16:12:05
The benefit of DRY is lost to lack of full compreh
marq (ping after 24h)
2017/02/17 12:16:58
Right now you have this (pseudocode):
Base Class:
edchin
2017/02/17 19:48:09
Updated:
1) No more factoring out common constrai
| |
300 } | 270 self.constraints = @[ |
301 | 271 [self.tabStripView.topAnchor |
302 // Note that this class doesn't override -didAddToolbarViewController; in the | 272 constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor], |
303 // case where there is a toolbar view controller set but not a content view | 273 [self.tabStripView.leadingAnchor |
304 // controller, functionally there is no topmost view controller, so no | 274 constraintEqualToAnchor:self.view.leadingAnchor], |
305 // additional action needs to be taken. | 275 [self.tabStripView.trailingAnchor |
306 | 276 constraintEqualToAnchor:self.view.trailingAnchor], |
307 - (void)updateContentConstraintsWithToolbar { | 277 self.tabStripHeightConstraint, |
308 UIView* contentView = self.contentViewController.view; | 278 [self.contentView.topAnchor |
309 self.contentConstraintsWithToolbar = @[ | 279 constraintEqualToAnchor:self.tabStripView.bottomAnchor], |
310 [contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 280 [self.contentView.leadingAnchor |
311 [contentView.bottomAnchor | 281 constraintEqualToAnchor:self.view.leadingAnchor], |
312 constraintEqualToAnchor:self.toolbarViewController.view.topAnchor], | 282 [self.contentView.trailingAnchor |
313 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 283 constraintEqualToAnchor:self.view.trailingAnchor], |
314 [contentView.trailingAnchor | 284 [self.toolbarView.topAnchor |
315 constraintEqualToAnchor:self.view.trailingAnchor], | 285 constraintEqualToAnchor:self.contentView.bottomAnchor], |
316 ]; | 286 [self.toolbarView.leadingAnchor |
317 } | 287 constraintEqualToAnchor:self.view.leadingAnchor], |
318 | 288 [self.toolbarView.trailingAnchor |
319 - (void)updateToolbarConstraints { | 289 constraintEqualToAnchor:self.view.trailingAnchor], |
320 UIView* toolbarView = self.toolbarViewController.view; | 290 self.toolbarHeightConstraint, |
321 self.toolbarConstraints = @[ | 291 [self.toolbarView.bottomAnchor |
322 [toolbarView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 292 constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor], |
323 [toolbarView.heightAnchor constraintEqualToConstant:kToolbarHeight], | 293 ]; |
324 [toolbarView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 294 [NSLayoutConstraint activateConstraints:self.constraints]; |
325 [toolbarView.trailingAnchor | 295 } |
326 constraintEqualToAnchor:self.view.trailingAnchor], | 296 [super updateViewConstraints]; |
327 ]; | |
328 } | 297 } |
329 | 298 |
330 @end | 299 @end |
OLD | NEW |