1 | // Copyright (c) 2012 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 | package org.chromium.android_webview; |
6 | |
7 | import android.util.Pair; |
8 | import android.view.View.MeasureSpec; |
9 | import android.view.View; |
10 | |
11 | import org.chromium.content.browser.ContentViewCore; |
12 | |
13 | /** |
14 | * Helper methods used to manage the layout of the View that contains AwContents. |
15 | */ |
16 | public class AwLayoutSizer { |
17 | // These are used to prevent a re-layout if the content size changes within a dimension that is |
18 | // fixed by the view system. |
19 | private boolean mWidthMeasurementIsFixed; |
20 | private boolean mHeightMeasurementIsFixed; |
21 | |
22 | // Size of the rendered content, as reported by native. |
23 | private int mContentHeightCss; |
24 | private int mContentWidthCss; |
25 | |
26 | // Page scale factor. This is set to zero initially so that we don't attempt to do a layout if |
27 | // we get the content size change notification first and a page scale change second. |
28 | private double mPageScaleFactor = 0.0; |
29 | |
30 | // Whether to postpone layout requests. |
31 | private boolean mFreezeLayoutRequests; |
32 | // Did we try to request a layout since the last time mPostponeLayoutRequests was set to true. |
33 | private boolean mFrozenLayoutRequestPending; |
34 | |
35 | private double mDIPScale; |
36 | |
37 | // Callback object for interacting with the View. |
38 | private Delegate mDelegate; |
39 | |
40 | public interface Delegate { |
41 | void requestLayout(); |
42 | void setMeasuredDimension(int measuredWidth, int measuredHeight); |
43 | } |
44 | |
45 | /** |
46 | * Default constructor. Note: both setDelegate and setDIPScale must be called before the class |
47 | * is ready for use. |
48 | */ |
49 | public AwLayoutSizer() { |
50 | } |
51 | |
52 | public void setDelegate(Delegate delegate) { |
53 | mDelegate = delegate; |
54 | } |
55 | |
56 | public void setDIPScale(double dipScale) { |
57 | mDIPScale = dipScale; |
58 | } |
59 | |
60 | /** |
61 | * This is used to register the AwLayoutSizer to preferred content size change notifications in |
62 | * the AwWebContentsDelegate. |
63 | * NOTE: The preferred size notifications come in from the Renderer main thread and might be |
64 | * out of sync with the content size as seen by the InProcessViewRenderer (and Compositor). |
65 | */ |
66 | public AwWebContentsDelegateAdapter.PreferredSizeChangedListener |
67 | getPreferredSizeChangedListener() { |
68 | return new AwWebContentsDelegateAdapter.PreferredSizeChangedListener() { |
69 | @Override |
70 | public void updatePreferredSize(int widthCss, int heightCss) { |
71 | onContentSizeChanged(widthCss, heightCss); |
72 | } |
73 | }; |
74 | } |
75 | |
76 | /** |
77 | * Postpone requesting layouts till unfreezeLayoutRequests is called. |
78 | */ |
79 | public void freezeLayoutRequests() { |
80 | mFreezeLayoutRequests = true; |
81 | mFrozenLayoutRequestPending = false; |
82 | } |
83 | |
84 | /** |
85 | * Stop postponing layout requests and request layout if such a request would have been made |
86 | * had the freezeLayoutRequests method not been called before. |
87 | */ |
88 | public void unfreezeLayoutRequests() { |
89 | mFreezeLayoutRequests = false; |
90 | if (mFrozenLayoutRequestPending) { |
91 | mFrozenLayoutRequestPending = false; |
92 | mDelegate.requestLayout(); |
93 | } |
94 | } |
95 | |
96 | /** |
97 | * Update the contents size. |
98 | * This should be called whenever the content size changes (due to DOM manipulation or page |
99 | * load, for example). |
100 | * The width and height should be in CSS pixels. |
101 | */ |
102 | public void onContentSizeChanged(int widthCss, int heightCss) { |
103 | doUpdate(widthCss, heightCss, mPageScaleFactor); |
104 | } |
105 | |
106 | /** |
107 | * Update the contents page scale. |
108 | * This should be called whenever the content page scale factor changes (due to pinch zoom, for |
109 | * example). |
110 | */ |
111 | public void onPageScaleChanged(double pageScaleFactor) { |
112 | doUpdate(mContentWidthCss, mContentHeightCss, pageScaleFactor); |
113 | } |
114 | |
115 | private void doUpdate(int widthCss, int heightCss, double pageScaleFactor) { |
116 | // We want to request layout only if the size or scale change, however if any of the |
117 | // measurements are 'fixed', then changing the underlying size won't have any effect, so we |
118 | // ignore changes to dimensions that are 'fixed'. |
119 | boolean anyMeasurementNotFixed = !mWidthMeasurementIsFixed || !mHeightMeasurementIsFixed; |
120 | boolean layoutNeeded = (mContentWidthCss != widthCss && !mWidthMeasurementIsFixed) || |
121 | (mContentHeightCss != heightCss && !mHeightMeasurementIsFixed) || |
122 | (mPageScaleFactor != pageScaleFactor && anyMeasurementNotFixed); |
123 | |
124 | mContentWidthCss = widthCss; |
125 | mContentHeightCss = heightCss; |
126 | mPageScaleFactor = pageScaleFactor; |
127 | |
128 | if (layoutNeeded) { |
129 | if (mFreezeLayoutRequests) { |
130 | mFrozenLayoutRequestPending = true; |
131 | } else { |
132 | mDelegate.requestLayout(); |
133 | } |
134 | } |
135 | } |
136 | |
137 | /** |
138 | * Calculate the size of the view. |
139 | * This is designed to be used to implement the android.view.View#onMeasure() method. |
140 | */ |
141 | public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
142 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
143 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
144 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
145 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
146 | |
147 | int measuredHeight = heightSize; |
148 | int measuredWidth = widthSize; |
149 | |
150 | int contentHeightPix = (int) (mContentHeightCss * mPageScaleFactor * mDIPScale); |
151 | int contentWidthPix = (int) (mContentWidthCss * mPageScaleFactor * mDIPScale); |
152 | |
153 | // Always use the given size unless unspecified. This matches WebViewClassic behavior. |
154 | mWidthMeasurementIsFixed = (widthMode != MeasureSpec.UNSPECIFIED); |
155 | // Freeze the height if an exact size is given by the parent or if the content size has |
156 | // exceeded the maximum size specified by the parent. |
157 | // TODO(mkosiba): Actually we'd like the reduction in content size to cause the WebView to |
158 | // shrink back again but only as a result of a page load. |
159 | mHeightMeasurementIsFixed = (heightMode == MeasureSpec.EXACTLY) || |
160 | (heightMode == MeasureSpec.AT_MOST && contentHeightPix > heightSize); |
161 | |
162 | if (!mHeightMeasurementIsFixed) { |
163 | measuredHeight = contentHeightPix; |
164 | } |
165 | |
166 | if (!mWidthMeasurementIsFixed) { |
167 | measuredWidth = contentWidthPix; |
168 | } |
169 | |
170 | if (measuredHeight < contentHeightPix) { |
171 | measuredHeight |= View.MEASURED_STATE_TOO_SMALL; |
172 | } |
173 | |
174 | if (measuredWidth < contentWidthPix) { |
175 | measuredWidth |= View.MEASURED_STATE_TOO_SMALL; |
176 | } |
177 | |
178 | mDelegate.setMeasuredDimension(measuredWidth, measuredHeight); |
179 | } |
180 | } |