Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: ios/chrome/browser/ui/strip/strip_container_view_controller.mm

Issue 2594023002: Simple strip container that can reveal/hide tab strip. (Closed)
Patch Set: Remove unncessary dependency. Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/strip/strip_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 kStripHeight = 200.0;
19 }
20
21 @interface StripContainerViewController ()
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;
marq (ping after 24h) 2016/12/21 17:26:15 I don't think you need this if we aren't going to
edchin 2016/12/22 10:17:17 This container implementation accounts for when th
marq (ping after 24h) 2016/12/22 11:33:07 You are of course correct.
26
27 @property(nonatomic, strong) Constraints* contentConstraintsWithStrip;
28 @property(nonatomic, strong) Constraints* contentConstraintsWithoutStrip;
29 @property(nonatomic, strong) Constraints* stripConstraints;
30
31 // Cache for forwarding methods to child view controllers.
32 @property(nonatomic, assign) SEL actionToForward;
33 @property(nonatomic, weak) UIResponder* forwardingTarget;
34
35 @property(nonatomic, strong) NSLayoutConstraint* stripHeightConstraint;
36
37 // Contained view controller utility methods.
38 - (void)removeChildViewController:(UIViewController*)viewController;
39
40 // Called after a new content view controller is set, but before
41 // |-didMoveToParentViewController:| is called on that view controller.
42 - (void)didAddContentViewController;
43
44 // Called after a new strip view controller is set, but before
45 // |-didMoveToParentViewController:| is called on that view controller.
46 - (void)didAddContentViewController;
marq (ping after 24h) 2016/12/21 17:26:15 -didAddStripViewController, right? Or is this erro
edchin 2016/12/22 10:17:17 Error in tab container as well. Will update that i
47
48 // Methods to populate the constraint properties.
49 - (void)updateContentConstraintsWithStrip;
50 - (void)updateContentConstraintsWithoutStrip;
51 - (void)updateStripConstraints;
52
53 @end
54
55 @implementation StripContainerViewController
56
57 @synthesize contentViewController = _contentViewController;
58 @synthesize stripViewController = _stripViewController;
59 @synthesize topmostViewController = _topmostViewController;
60 @synthesize contentConstraintsWithStrip = _contentConstraintsWithStrip;
61 @synthesize contentConstraintsWithoutStrip = _contentConstraintsWithoutStrip;
62 @synthesize stripConstraints = _stripConstraints;
63 @synthesize actionToForward = _actionToForward;
64 @synthesize forwardingTarget = _forwardingTarget;
65 @synthesize stripHeightConstraint = _stripHeightConstraint;
66
67 #pragma mark - Public properties
68
69 - (void)setContentViewController:(UIViewController*)contentViewController {
70 if (self.contentViewController == contentViewController)
71 return;
72
73 // Remove the current content view controller, if any.
74 [NSLayoutConstraint
75 deactivateConstraints:self.contentConstraintsWithoutStrip];
76 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithStrip];
77 [self removeChildViewController:self.contentViewController];
78
79 // Add the new content view controller.
80 [self addChildViewController:contentViewController];
81 contentViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
82 [self.view addSubview:contentViewController.view];
83 _contentViewController = contentViewController;
84 [self didAddContentViewController];
85 [self.view setNeedsUpdateConstraints];
86 [self.contentViewController didMoveToParentViewController:self];
87 }
88
89 - (void)setStripViewController:(UIViewController*)stripViewController {
90 if (self.stripViewController == stripViewController)
91 return;
92
93 // Remove the current strip view controller, if any.
94 [NSLayoutConstraint deactivateConstraints:self.stripConstraints];
95 [NSLayoutConstraint deactivateConstraints:self.contentConstraintsWithStrip];
96 [self removeChildViewController:self.stripViewController];
97
98 // Add the new strip view controller.
99 [self addChildViewController:stripViewController];
100 stripViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
101 [self.view addSubview:stripViewController.view];
102 _stripViewController = stripViewController;
103 [self didAddStripViewController];
104 [self.view setNeedsUpdateConstraints];
105 [self.stripViewController didMoveToParentViewController:self];
106 }
107
108 #pragma mark - UIViewController
109
110 - (void)updateViewConstraints {
111 if (self.stripViewController) {
112 [NSLayoutConstraint activateConstraints:self.stripConstraints];
113 [NSLayoutConstraint activateConstraints:self.contentConstraintsWithStrip];
114 } else {
115 [NSLayoutConstraint
116 activateConstraints:self.contentConstraintsWithoutStrip];
117 }
118 [super updateViewConstraints];
119 }
120
121 - (UIViewController*)childViewControllerForStatusBarHidden {
122 return self.topmostViewController;
123 }
124
125 - (UIViewController*)childViewControllerForStatusBarStyle {
126 return self.topmostViewController;
127 }
128
129 #pragma mark - UIResponder
130
131 // Before forwarding actions up the responder chain, give both contained
132 // view controllers a chance to handle them.
133 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
134 self.actionToForward = nullptr;
135 self.forwardingTarget = nil;
136 for (UIResponder* responder in
137 @[ self.contentViewController, self.stripViewController ]) {
138 if ([responder canPerformAction:action withSender:sender]) {
139 self.actionToForward = action;
140 self.forwardingTarget = responder;
141 return YES;
142 }
143 }
144 return [super canPerformAction:action withSender:sender];
145 }
146
147 #pragma mark - NSObject method forwarding
148
149 - (id)forwardingTargetForSelector:(SEL)aSelector {
150 if (aSelector == self.actionToForward) {
151 return self.forwardingTarget;
152 }
153 return nil;
154 }
155
156 #pragma mark - Private methods
157
158 - (void)removeChildViewController:(UIViewController*)viewController {
159 if (viewController.parentViewController != self)
160 return;
161 [viewController willMoveToParentViewController:nil];
162 [viewController.view removeFromSuperview];
163 [viewController removeFromParentViewController];
164 }
165
166 - (void)didAddContentViewController {
167 if (self.stripViewController) {
168 [self updateContentConstraintsWithStrip];
169 } else {
170 self.topmostViewController = self.contentViewController;
171 [self updateContentConstraintsWithoutStrip];
172 }
173 if (!self.stripViewController) {
174 self.topmostViewController = self.contentViewController;
175 }
176 }
177
178 - (void)didAddStripViewController {
179 [self updateStripConstraints];
180 // If there's already a content view controller, update the constraints for
181 // that, too.
182 if (self.contentViewController) {
183 [self updateContentConstraintsWithStrip];
184 }
185 self.topmostViewController = self.stripViewController;
186 }
187
188 - (void)updateContentConstraintsWithoutStrip {
189 UIView* contentView = self.contentViewController.view;
190 self.contentConstraintsWithoutStrip = @[
191 [contentView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
192 [contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
193 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
194 [contentView.trailingAnchor
195 constraintEqualToAnchor:self.view.trailingAnchor],
196 ];
197 }
198
199 - (void)updateContentConstraintsWithStrip {
200 UIView* contentView = self.contentViewController.view;
201 self.contentConstraintsWithStrip = @[
202 [contentView.topAnchor
203 constraintEqualToAnchor:self.stripViewController.view.bottomAnchor],
204 [contentView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
205 [contentView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
206 [contentView.trailingAnchor
207 constraintEqualToAnchor:self.view.trailingAnchor],
208 ];
209 }
210
211 - (void)updateStripConstraints {
212 UIView* stripView = self.stripViewController.view;
213 self.stripHeightConstraint =
214 [stripView.heightAnchor constraintEqualToConstant:0.0];
215 self.stripConstraints = @[
216 [stripView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
217 [stripView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
218 [stripView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
219 self.stripHeightConstraint,
220 ];
221 }
222
223 // Action for UIResponder chain that toggles visibility of tab strip.
marq (ping after 24h) 2016/12/21 17:26:15 Nit: all actions are by definition using the respo
edchin 2016/12/22 10:17:17 Done.
224 - (void)toggleTabStrip:(id)sender {
marq (ping after 24h) 2016/12/21 17:26:15 Add a ui/actions/tab_strip_actions.h file with a T
edchin 2016/12/22 10:17:17 Done.
225 self.stripHeightConstraint.constant =
226 self.stripHeightConstraint.constant > 0 ? 0.0 : kStripHeight;
227 [self.view setNeedsUpdateConstraints];
marq (ping after 24h) 2016/12/21 17:26:15 Is this needed? No constraints have been added or
edchin 2016/12/22 10:17:17 Done.
228 [self.view updateConstraintsIfNeeded];
229 }
230
231 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698