Commit f23086a9 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

Allow more ViewGroup types as ViewAndroidDelegate's mContainerView

mContainerView is a ViewGroup, but some layout code assumed it was a
FrameLayout. Update that code to allow any kind of ViewGroup for which
the LayoutParams subclasses MarginLayoutParams (including FrameLayout,
RelativeLayout, etc).

It would be nice to add an assertion to setContainerView so that
failures are caught earlier, but I don't see a clean way to do so.

Bug: 1054106
Change-Id: If45a16c2864ef200461a08b783b425ea42346f2e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2067247
Commit-Queue: Bo <boliu@chromium.org>
Auto-Submit: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#744342}
parent 3328526a
...@@ -102,7 +102,7 @@ public class AwContentsAnchorViewTest { ...@@ -102,7 +102,7 @@ public class AwContentsAnchorViewTest {
// Add anchor view // Add anchor view
View anchorView = addAnchorView(); View anchorView = addAnchorView();
LayoutParams layoutParams = anchorView.getLayoutParams(); LayoutParams layoutParams = new LayoutParams(anchorView.getLayoutParams());
// Replace container view // Replace container view
FrameLayout updatedContainerView = updateContainerView(); FrameLayout updatedContainerView = updateContainerView();
...@@ -156,7 +156,7 @@ public class AwContentsAnchorViewTest { ...@@ -156,7 +156,7 @@ public class AwContentsAnchorViewTest {
// Add anchor view // Add anchor view
View anchorView = addAnchorView(); View anchorView = addAnchorView();
LayoutParams layoutParams = anchorView.getLayoutParams(); LayoutParams layoutParams = new LayoutParams(anchorView.getLayoutParams());
// Replace container view // Replace container view
FrameLayout updatedContainerView = updateContainerView(); FrameLayout updatedContainerView = updateContainerView();
...@@ -213,7 +213,7 @@ public class AwContentsAnchorViewTest { ...@@ -213,7 +213,7 @@ public class AwContentsAnchorViewTest {
float scaledDimension = (float) dimension * scale; float scaledDimension = (float) dimension * scale;
mViewDelegate.setViewPosition( mViewDelegate.setViewPosition(
anchorView, scaledCoords, scaledCoords, scaledDimension, scaledDimension, 10, 10); anchorView, scaledCoords, scaledCoords, scaledDimension, scaledDimension, 10, 10);
return anchorView.getLayoutParams(); return new LayoutParams(anchorView.getLayoutParams());
} }
private FrameLayout updateContainerView() { private FrameLayout updateContainerView() {
......
...@@ -14,8 +14,8 @@ import android.view.MotionEvent; ...@@ -14,8 +14,8 @@ import android.view.MotionEvent;
import android.view.PointerIcon; import android.view.PointerIcon;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
...@@ -131,17 +131,19 @@ public class ViewAndroidDelegate { ...@@ -131,17 +131,19 @@ public class ViewAndroidDelegate {
/** /**
* Set the anchor view to specified position and size (all units in px). * Set the anchor view to specified position and size (all units in px).
* @param view The anchor view that needs to be positioned. * @param anchorView The view that needs to be positioned. This must be the result of a previous
* call to {@link acquireView} which has not yet been removed via {@link removeView}.
* @param x X coordinate of the top left corner of the anchor view. * @param x X coordinate of the top left corner of the anchor view.
* @param y Y coordinate of the top left corner of the anchor view. * @param y Y coordinate of the top left corner of the anchor view.
* @param width The width of the anchor view. * @param width The width of the anchor view.
* @param height The height of the anchor view. * @param height The height of the anchor view.
*/ */
@CalledByNative @CalledByNative
public void setViewPosition( public void setViewPosition(View anchorView, float x, float y, float width, float height,
View view, float x, float y, float width, float height, int leftMargin, int topMargin) { int leftMargin, int topMargin) {
ViewGroup containerView = getContainerView(); ViewGroup containerView = getContainerView();
if (containerView == null) return; if (containerView == null) return;
assert anchorView.getParent() == containerView;
int widthInt = Math.round(width); int widthInt = Math.round(width);
int heightInt = Math.round(height); int heightInt = Math.round(height);
...@@ -155,10 +157,12 @@ public class ViewAndroidDelegate { ...@@ -155,10 +157,12 @@ public class ViewAndroidDelegate {
if (widthInt + startMargin > containerView.getWidth()) { if (widthInt + startMargin > containerView.getWidth()) {
widthInt = containerView.getWidth() - startMargin; widthInt = containerView.getWidth() - startMargin;
} }
LayoutParams lp = new LayoutParams(widthInt, heightInt); MarginLayoutParams mlp = (MarginLayoutParams) anchorView.getLayoutParams();
MarginLayoutParamsCompat.setMarginStart(lp, startMargin); mlp.width = widthInt;
lp.topMargin = topMargin; mlp.height = heightInt;
view.setLayoutParams(lp); MarginLayoutParamsCompat.setMarginStart(mlp, startMargin);
mlp.topMargin = topMargin;
anchorView.setLayoutParams(mlp);
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment