1 // Copyright 2015 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.
5 #import "ios/web/web_state/ui/crw_web_controller_container_view.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
10 #pragma mark - CRWToolbarContainerView
12 // Class that manages the display of toolbars.
13 @interface CRWToolbarContainerView : UIView {
14 // Backing object for |self.toolbars|.
15 base::scoped_nsobject<NSMutableArray> _toolbars;
18 // The toolbars currently managed by this view.
19 @property(nonatomic, retain, readonly) NSMutableArray* toolbars;
21 // Adds |toolbar| as a subview and bottom aligns to any previously added
23 - (void)addToolbar:(UIView*)toolbar;
25 // Removes |toolbar| from the container view.
26 - (void)removeToolbar:(UIView*)toolbar;
30 @implementation CRWToolbarContainerView
32 #pragma mark Accessors
34 - (NSMutableArray*)toolbars {
36 _toolbars.reset([[NSMutableArray alloc] init]);
37 return _toolbars.get();
42 - (void)layoutSubviews {
43 [super layoutSubviews];
45 // Bottom-align the toolbars.
46 CGPoint toolbarOrigin =
47 CGPointMake(self.bounds.origin.x, CGRectGetMaxY(self.bounds));
48 for (UIView* toolbar in self.toolbars) {
49 CGSize toolbarSize = [toolbar sizeThatFits:self.bounds.size];
50 toolbarSize.width = self.bounds.size.width;
51 toolbarOrigin.y -= toolbarSize.height;
52 toolbar.frame = CGRectMake(toolbarOrigin.x, toolbarOrigin.y,
53 toolbarSize.width, toolbarSize.height);
57 - (CGSize)sizeThatFits:(CGSize)size {
58 CGSize boundingRectSize = CGSizeMake(size.width, CGFLOAT_MAX);
59 CGFloat necessaryHeight = 0.0f;
60 for (UIView* toolbar in self.toolbars)
61 necessaryHeight += [toolbar sizeThatFits:boundingRectSize].height;
62 return CGSizeMake(size.width, necessaryHeight);
67 - (void)addToolbar:(UIView*)toolbar {
69 DCHECK(![self.toolbars containsObject:toolbar]);
70 toolbar.translatesAutoresizingMaskIntoConstraints = NO;
71 [self.toolbars addObject:toolbar];
72 [self addSubview:toolbar];
75 - (void)removeToolbar:(UIView*)toolbar {
77 DCHECK([self.toolbars containsObject:toolbar]);
78 [self.toolbars removeObject:toolbar];
79 [toolbar removeFromSuperview];
84 #pragma mark - CRWWebControllerContainerView
86 @interface CRWWebControllerContainerView () {
87 // Backing object for |self.toolbarContainerView|.
88 base::scoped_nsobject<CRWToolbarContainerView> _toolbarContainerView;
91 // Container view that displays any added toolbars. It is always the top-most
92 // subview, and is bottom aligned with the CRWWebControllerContainerView.
93 @property(nonatomic, retain, readonly)
94 CRWToolbarContainerView* toolbarContainerView;
98 @implementation CRWWebControllerContainerView
100 - (instancetype)init {
105 - (instancetype)initWithFrame:(CGRect)frame {
106 self = [super initWithFrame:frame];
108 self.backgroundColor = [UIColor whiteColor];
109 self.autoresizingMask =
110 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
115 #pragma mark Accessors
117 - (void)setToolbarContainerView:(CRWToolbarContainerView*)toolbarContainerView {
118 if (![_toolbarContainerView isEqual:toolbarContainerView]) {
119 [_toolbarContainerView removeFromSuperview];
120 _toolbarContainerView.reset(toolbarContainerView);
121 [self addSubview:_toolbarContainerView];
125 - (UIView*)toolbarContainerView {
126 return _toolbarContainerView.get();
131 - (void)layoutSubviews {
132 [super layoutSubviews];
134 if (self.toolbarContainerView) {
135 [self bringSubviewToFront:self.toolbarContainerView];
136 CGSize toolbarContainerSize =
137 [self.toolbarContainerView sizeThatFits:self.bounds.size];
138 self.toolbarContainerView.frame =
139 CGRectMake(CGRectGetMinX(self.bounds),
140 CGRectGetMaxY(self.bounds) - toolbarContainerSize.height,
141 toolbarContainerSize.width, toolbarContainerSize.height);
145 #pragma mark Toolbars
147 - (void)addToolbar:(UIView*)toolbar {
148 // Create toolbar container if necessary.
149 if (!self.toolbarContainerView) {
150 self.toolbarContainerView =
151 [[CRWToolbarContainerView alloc] initWithFrame:CGRectZero];
153 // Add the toolbar to the container.
154 [self.toolbarContainerView addToolbar:toolbar];
155 [self setNeedsLayout];
158 - (void)addToolbars:(NSArray*)toolbars {
160 for (UIView* toolbar in toolbars)
161 [self addToolbar:toolbar];
164 - (void)removeToolbar:(UIView*)toolbar {
165 // Remove the toolbar from the container view.
166 [self.toolbarContainerView removeToolbar:toolbar];
167 // Reset the container if there are no more toolbars.
168 if ([self.toolbarContainerView.toolbars count])
169 [self setNeedsLayout];
171 self.toolbarContainerView = nil;
174 - (void)removeAllToolbars {
175 // Resetting the property will remove the toolbars from the hierarchy.
176 self.toolbarContainerView = nil;