OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #import "ios/web/public/web_state/ui/crw_generic_content_view.h" | 5 #import "ios/web/public/web_state/ui/crw_generic_content_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/scoped_nsobject.h" | 8 #include "base/mac/scoped_nsobject.h" |
9 | 9 |
10 @interface CRWGenericContentView () { | 10 @interface CRWGenericContentView () { |
| 11 // The size of the view's bounds at the last call to |-layoutSubviews|. |
| 12 CGSize _lastLayoutSize; |
11 // Backing objectect for |self.scrollView|. | 13 // Backing objectect for |self.scrollView|. |
12 base::scoped_nsobject<UIScrollView> _scrollView; | 14 base::scoped_nsobject<UIScrollView> _scrollView; |
13 // Backing object for |self.view|. | 15 // Backing object for |self.view|. |
14 base::scoped_nsobject<UIView> _view; | 16 base::scoped_nsobject<UIView> _view; |
15 } | 17 } |
16 | 18 |
17 @end | 19 @end |
18 | 20 |
19 @implementation CRWGenericContentView | 21 @implementation CRWGenericContentView |
20 | 22 |
21 - (instancetype)initWithView:(UIView*)view { | 23 - (instancetype)initWithView:(UIView*)view { |
22 self = [super initWithFrame:CGRectZero]; | 24 self = [super initWithFrame:CGRectZero]; |
23 if (self) { | 25 if (self) { |
24 DCHECK(view); | 26 DCHECK(view); |
| 27 _lastLayoutSize = CGSizeZero; |
25 _view.reset([view retain]); | 28 _view.reset([view retain]); |
26 _scrollView.reset([[UIScrollView alloc] initWithFrame:CGRectZero]); | 29 _scrollView.reset([[UIScrollView alloc] initWithFrame:CGRectZero]); |
27 [self addSubview:_scrollView]; | 30 [self addSubview:_scrollView]; |
28 [_scrollView addSubview:_view]; | 31 [_scrollView addSubview:_view]; |
29 [_scrollView setBackgroundColor:[_view backgroundColor]]; | 32 [_scrollView setBackgroundColor:[_view backgroundColor]]; |
30 } | 33 } |
31 return self; | 34 return self; |
32 } | 35 } |
33 | 36 |
34 - (instancetype)initWithCoder:(NSCoder*)decoder { | 37 - (instancetype)initWithCoder:(NSCoder*)decoder { |
(...skipping 17 matching lines...) Expand all Loading... |
52 | 55 |
53 - (UIView*)view { | 56 - (UIView*)view { |
54 return _view.get(); | 57 return _view.get(); |
55 } | 58 } |
56 | 59 |
57 #pragma mark Layout | 60 #pragma mark Layout |
58 | 61 |
59 - (void)layoutSubviews { | 62 - (void)layoutSubviews { |
60 [super layoutSubviews]; | 63 [super layoutSubviews]; |
61 | 64 |
| 65 // Early return if the bounds' size hasn't changed since the last layout. |
| 66 if (CGSizeEqualToSize(_lastLayoutSize, self.bounds.size)) |
| 67 return; |
| 68 _lastLayoutSize = self.bounds.size; |
| 69 |
62 // scrollView layout. | 70 // scrollView layout. |
63 self.scrollView.frame = self.bounds; | 71 self.scrollView.frame = self.bounds; |
64 | 72 |
65 // view layout. | 73 // view layout. |
66 CGRect contentRect = | 74 CGRect contentRect = |
67 UIEdgeInsetsInsetRect(self.bounds, self.scrollView.contentInset); | 75 UIEdgeInsetsInsetRect(self.bounds, self.scrollView.contentInset); |
68 CGSize viewSize = [self.view sizeThatFits:contentRect.size]; | 76 CGSize viewSize = [self.view sizeThatFits:contentRect.size]; |
69 self.view.frame = CGRectMake(0.0, 0.0, viewSize.width, viewSize.height); | 77 self.view.frame = CGRectMake(0.0, 0.0, viewSize.width, viewSize.height); |
70 | 78 |
71 // UIScrollViews only scroll vertically if the content size's height is | 79 // UIScrollViews only scroll vertically if the content size's height is |
72 // creater than that of its content rect. | 80 // greater than that of its content rect. |
73 if (viewSize.height <= CGRectGetHeight(contentRect)) { | 81 if (viewSize.height <= CGRectGetHeight(contentRect)) { |
74 CGFloat singlePixel = 1.0f / [[UIScreen mainScreen] scale]; | 82 CGFloat singlePixel = 1.0f / [[UIScreen mainScreen] scale]; |
75 viewSize.height = CGRectGetHeight(contentRect) + singlePixel; | 83 viewSize.height = CGRectGetHeight(contentRect) + singlePixel; |
76 } | 84 } |
77 self.scrollView.contentSize = viewSize; | 85 self.scrollView.contentSize = viewSize; |
78 } | 86 } |
79 | 87 |
80 - (BOOL)isViewAlive { | 88 - (BOOL)isViewAlive { |
81 return YES; | 89 return YES; |
82 } | 90 } |
83 | 91 |
84 @end | 92 @end |
OLD | NEW |