OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // ====== New Architecture ===== | |
6 // = This code is only used in the new iOS Chrome architecture. = | |
7 // ============================================================================ | |
8 | |
9 #import "ios/chrome/browser/ui/tab/tab_container_view_controller.h" | |
10 | |
11 #import "ios/chrome/browser/ui/ui_types.h" | |
12 | |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
14 #error "This file requires ARC support." | |
15 #endif | |
16 | |
17 namespace { | |
18 CGFloat kToolbarHeight = 56.0; | |
19 } | |
20 | |
21 @interface TabContainerViewController () | |
22 | |
23 // Whichever view controller is at the top of the screen. This view controller | |
24 // controls the status bar. | |
25 @property(nonatomic, weak) UIViewController* topmostViewController; | |
26 | |
27 @property(nonatomic, strong) Constraints* contentConstraintsWithToolbar; | |
28 @property(nonatomic, strong) Constraints* contentConstraintsWithoutToolbar; | |
29 @property(nonatomic, strong) Constraints* toolbarConstraints; | |
30 | |
31 // Cache for forwarding methods to child view controllers. | |
32 @property(nonatomic, assign) SEL actionToForward; | |
33 @property(nonatomic, weak) UIResponder* forwardingTarget; | |
34 | |
35 // Contained view controller utility methods. | |
36 - (void)removeChildViewController:(UIViewController*)viewController; | |
37 | |
38 // Called after a new content view controller is set, but before | |
39 // |-didMoveToParentViewController:| is called on that view controller. | |
40 - (void)didAddContentViewController; | |
41 | |
42 // Called after a new toolbar view controller is set, but before | |
43 // |-didMoveToParentViewController:| is called on that view controller. | |
44 - (void)didAddToolbarViewController; | |
45 | |
46 // Methods to populate the constraint properties. | |
47 - (void)updateContentConstraintsWithToolbar; | |
48 - (void)updateContentConstraintsWithoutToolbar; | |
49 - (void)updateToolbarConstraints; | |
50 | |
51 @end | |
52 | |
53 @implementation TabContainerViewController | |
54 | |
55 @synthesize contentViewController = _contentViewController; | |
56 @synthesize toolbarViewController = _toolbarViewController; | |
57 @synthesize topmostViewController = _topmostViewController; | |
58 @synthesize contentConstraintsWithToolbar = _contentConstraintsWithToolbar; | |
59 @synthesize contentConstraintsWithoutToolbar = | |
60 _contentConstraintsWithoutToolbar; | |
61 @synthesize toolbarConstraints = _toolbarConstraints; | |
62 @synthesize actionToForward = _actionToForward; | |
63 @synthesize forwardingTarget = _forwardingTarget; | |
64 | |
65 #pragma mark - Public properties | |
66 | |
67 - (void)setContentViewController:(UIViewController*)contentViewController { | |
68 if (self.contentViewController == contentViewController) | |
69 return; | |
70 | |
71 // Remove the current content view controller, if any. | |
72 [NSLayoutConstraint | |
73 deactivateConstraints:self.contentConstraintsWithoutToolbar]; | |
74 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithToolbar]; | |
75 [self removeChildViewController:self.contentViewController]; | |
76 | |
77 // Add the new content view controller. | |
78 [self addChildViewController:contentViewController]; | |
79 contentViewController.view.translatesAutoresizingMaskIntoConstraints = NO; | |
80 [self.view addSubview:contentViewController.view]; | |
81 _contentViewController = contentViewController; | |
82 [self didAddContentViewController]; | |
83 [self.view setNeedsUpdateConstraints]; | |
84 [self.contentViewController didMoveToParentViewController:self]; | |
85 } | |
86 | |
87 - (void)setToolbarViewController:(UIViewController*)toolbarViewController { | |
88 if (self.toolbarViewController == toolbarViewController) | |
89 return; | |
90 | |
91 // Remove the current toolbar view controller, if any. | |
92 [NSLayoutConstraint deactivateConstraints:self.toolbarConstraints]; | |
93 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithToolbar]; | |
94 [self removeChildViewController:self.toolbarViewController]; | |
95 | |
96 // Add the new toolbar view controller. | |
97 [self addChildViewController:toolbarViewController]; | |
98 toolbarViewController.view.translatesAutoresizingMaskIntoConstraints = NO; | |
99 [self.view addSubview:toolbarViewController.view]; | |
100 _toolbarViewController = toolbarViewController; | |
101 [self didAddToolbarViewController]; | |
102 [self.view setNeedsUpdateConstraints]; | |
103 [self.toolbarViewController didMoveToParentViewController:self]; | |
104 } | |
105 | |
106 #pragma mark - UIViewController | |
107 | |
108 - (void)updateViewConstraints { | |
109 if (self.toolbarViewController) { | |
110 [NSLayoutConstraint activateConstraints:self.toolbarConstraints]; | |
111 [NSLayoutConstraint activateConstraints:self.contentConstraintsWithToolbar]; | |
112 } else { | |
113 [NSLayoutConstraint | |
114 activateConstraints:self.contentConstraintsWithoutToolbar]; | |
115 } | |
116 [super updateViewConstraints]; | |
117 } | |
118 | |
119 - (UIViewController*)childViewControllerForStatusBarHidden { | |
120 return self.topmostViewController; | |
121 } | |
122 | |
123 - (UIViewController*)childViewControllerForStatusBarStyle { | |
124 return self.topmostViewController; | |
125 } | |
126 | |
127 #pragma mark - MenuPresentationDelegate | |
128 | |
129 - (CGRect)frameForMenuPresentation:(UIPresentationController*)presentation { | |
130 // Placeholder. | |
131 return CGRectMake(50, 50, 250, 300); | |
132 } | |
133 | |
134 #pragma mark - UIResponder | |
135 | |
136 // Before forwarding actions up the responder chain, give both contained | |
137 // view controllers a chance to handle them. | |
138 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { | |
139 self.actionToForward = nullptr; | |
140 self.forwardingTarget = nil; | |
141 for (UIResponder* responder in | |
142 @[ self.contentViewController, self.toolbarViewController ]) { | |
143 if ([responder canPerformAction:action withSender:sender]) { | |
144 self.actionToForward = action; | |
145 self.forwardingTarget = responder; | |
146 return YES; | |
147 } | |
148 } | |
149 return [super canPerformAction:action withSender:sender]; | |
150 } | |
151 | |
152 #pragma mark - NSObject method forwarding | |
153 | |
154 - (id)forwardingTargetForSelector:(SEL)aSelector { | |
155 if (aSelector == self.actionToForward) { | |
156 return self.forwardingTarget; | |
157 } | |
158 return nil; | |
159 } | |
160 | |
161 #pragma mark - Private methods | |
162 | |
163 - (void)removeChildViewController:(UIViewController*)viewController { | |
164 if (viewController.parentViewController != self) | |
165 return; | |
166 [viewController willMoveToParentViewController:nil]; | |
167 [viewController.view removeFromSuperview]; | |
168 [viewController removeFromParentViewController]; | |
169 } | |
170 | |
171 - (void)didAddContentViewController { | |
172 if (self.toolbarViewController) { | |
173 [self updateContentConstraintsWithToolbar]; | |
174 } else { | |
175 self.topmostViewController = self.contentViewController; | |
176 [self updateContentConstraintsWithoutToolbar]; | |
177 } | |
178 } | |
179 | |
180 - (void)didAddToolbarViewController { | |
181 [self updateToolbarConstraints]; | |
182 // If there's already a content view controller, update the constraints for | |
183 // that, too. | |
184 if (self.contentViewController) { | |
185 [self updateContentConstraintsWithToolbar]; | |
186 } | |
187 } | |
188 | |
189 - (void)updateContentConstraintsWithToolbar { | |
190 // Template method for subclasses to implement; | |
191 } | |
192 | |
193 - (void)updateToolbarConstraints { | |
194 // Template method for subclasses to implement; | |
195 } | |
196 | |
197 - (void)updateContentConstraintsWithoutToolbar { | |
198 UIView* contentView = self.contentViewController.view; | |
199 self.contentConstraintsWithoutToolbar = @[ | |
200 [contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | |
201 [contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | |
202 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
203 [contentView.trailingAnchor | |
204 constraintEqualToAnchor:self.view.trailingAnchor], | |
205 ]; | |
206 } | |
207 | |
208 @end | |
209 | |
210 @implementation TopToolbarTabViewController | |
211 | |
212 - (void)didAddContentViewController { | |
213 [super didAddContentViewController]; | |
214 if (!self.toolbarViewController) { | |
215 self.topmostViewController = self.contentViewController; | |
216 } | |
217 } | |
218 | |
219 - (void)didAddToolbarViewController { | |
220 [super didAddToolbarViewController]; | |
221 self.topmostViewController = self.toolbarViewController; | |
222 } | |
223 | |
224 - (void)updateContentConstraintsWithToolbar { | |
225 UIView* contentView = self.contentViewController.view; | |
226 self.contentConstraintsWithToolbar = @[ | |
227 [contentView.topAnchor | |
228 constraintEqualToAnchor:self.toolbarViewController.view.bottomAnchor], | |
229 [contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | |
230 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
231 [contentView.trailingAnchor | |
232 constraintEqualToAnchor:self.view.trailingAnchor], | |
233 ]; | |
234 } | |
235 | |
236 - (void)updateToolbarConstraints { | |
237 UIView* toolbarView = self.toolbarViewController.view; | |
238 self.toolbarConstraints = @[ | |
239 [toolbarView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | |
240 [toolbarView.heightAnchor constraintEqualToConstant:kToolbarHeight], | |
241 [toolbarView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
242 [toolbarView.trailingAnchor | |
243 constraintEqualToAnchor:self.view.trailingAnchor], | |
244 ]; | |
245 } | |
246 | |
247 @end | |
248 | |
249 @implementation BottomToolbarTabViewController | |
250 | |
251 - (void)didAddContentViewController { | |
252 [super didAddContentViewController]; | |
253 self.topmostViewController = self.contentViewController; | |
254 } | |
255 | |
256 // Note that this class doesn't override -didAddToolbarViewController; in the | |
257 // case where there is a toolbar view controller set but not a content view | |
258 // controller, functionally there is no topmost view controller, so no | |
259 // additional action needs to be taken. | |
260 | |
261 - (void)updateContentConstraintsWithToolbar { | |
262 UIView* contentView = self.contentViewController.view; | |
263 self.contentConstraintsWithToolbar = @[ | |
264 [contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | |
265 [contentView.bottomAnchor | |
266 constraintEqualToAnchor:self.toolbarViewController.view.topAnchor], | |
267 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
268 [contentView.trailingAnchor | |
269 constraintEqualToAnchor:self.view.trailingAnchor], | |
270 ]; | |
271 } | |
272 | |
273 - (void)updateToolbarConstraints { | |
274 UIView* toolbarView = self.toolbarViewController.view; | |
275 self.toolbarConstraints = @[ | |
276 [toolbarView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | |
277 [toolbarView.heightAnchor constraintEqualToConstant:kToolbarHeight], | |
278 [toolbarView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
279 [toolbarView.trailingAnchor | |
280 constraintEqualToAnchor:self.view.trailingAnchor], | |
281 ]; | |
282 } | |
283 | |
284 @end | |
OLD | NEW |