Chromium Code Reviews| Index: ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java |
| diff --git a/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java b/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java |
| index 6d3694cc9bfe1304dd81fce41b8c3691be883f0a..fdb1b1e7a14af5f714b05d2b6921ab4ec9a0f903 100644 |
| --- a/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java |
| +++ b/ui/android/java/src/org/chromium/ui/base/ViewAndroidDelegate.java |
| @@ -5,30 +5,67 @@ |
| package org.chromium.ui.base; |
| import android.view.View; |
| +import android.view.ViewGroup; |
| +import android.widget.FrameLayout.LayoutParams; |
| + |
| +import org.chromium.base.ApiCompatibilityUtils; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| /** |
| - * Interface to acquire and release anchor views from the implementing View. |
| + * Class that acquire, position, and remove anchor views from the implementing View. |
|
no sievers
2016/07/18 22:14:16
nit: 'that acquires' or 'to acquire' etc.
Jinsuk Kim
2016/07/19 07:08:39
Done.
|
| */ |
| -public interface ViewAndroidDelegate { |
| - |
| - /** |
| - * @return An anchor view that can be used to anchor decoration views like Autofill popup. |
| - */ |
| - View acquireAnchorView(); |
| - |
| - /** |
| - * Set the anchor view to specified position and width (all units in dp). |
| - * @param view The anchor view that needs to be positioned. |
| - * @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 width The width of the anchor view. |
| - * @param height The height of the anchor view. |
| - */ |
| - void setAnchorViewPosition(View view, float x, float y, float width, float height); |
| - |
| - /** |
| - * Release given anchor view. |
| - * @param anchorView The anchor view that needs to be released. |
| - */ |
| - void releaseAnchorView(View anchorView); |
| +@JNINamespace("ui") |
| +public class ViewAndroidDelegate { |
| + private final ViewGroup mContainerView; |
|
no sievers
2016/07/18 22:14:16
Can this be in the implementation (and getContaine
Jinsuk Kim
2016/07/19 07:08:39
Done. You can see that the pattern (anonymous View
|
| + |
| + public ViewAndroidDelegate(ViewGroup containerView) { |
| + mContainerView = containerView; |
| + } |
| + |
| + @CalledByNative |
| + public View acquireView() { |
| + View anchorView = new View(getContainerView().getContext()); |
| + getContainerView().addView(anchorView); |
| + return anchorView; |
| + } |
| + |
| + @CalledByNative |
| + public void removeView(View anchorView) { |
| + getContainerView().removeView(anchorView); |
| + } |
| + |
| + @CalledByNative |
| + public void setViewPosition(View view, float x, float y, |
| + float width, float height, float scale, int leftMargin, int topMargin) { |
| + if (view.getParent() == null) { |
| + // Ignore. setAnchorViewPosition has been called after the anchor view has |
| + // already been released. |
| + return; |
| + } |
| + ViewGroup containerView = getContainerView(); |
| + assert view.getParent() == containerView; |
| + |
| + int scaledWidth = Math.round(width * scale); |
| + int scaledHeight = Math.round(height * scale); |
| + int startMargin; |
| + if (ApiCompatibilityUtils.isLayoutRtl(containerView)) { |
| + startMargin = containerView.getMeasuredWidth() - Math.round((width + x) * scale); |
| + } else { |
| + startMargin = leftMargin; |
| + } |
| + if (scaledWidth + startMargin > containerView.getWidth()) { |
| + scaledWidth = containerView.getWidth() - startMargin; |
| + } |
| + LayoutParams lp = new LayoutParams(scaledWidth, scaledHeight); |
| + ApiCompatibilityUtils.setMarginStart(lp, startMargin); |
| + lp.topMargin = topMargin; |
| + view.setLayoutParams(lp); |
| + } |
| + |
| + // Can be overriden by the inherited class to return its own container view in case |
| + // it allows dynamic container view switching. |
| + protected ViewGroup getContainerView() { |
| + return mContainerView; |
| + } |
| } |