Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/input/SingleSelectPopup.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/input/SingleSelectPopup.java b/content/public/android/java/src/org/chromium/content/browser/input/SingleSelectPopup.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ebd6ec099420e399d4035a0f56c69fe57737db65 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/input/SingleSelectPopup.java |
| @@ -0,0 +1,154 @@ |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
|
Ted C
2014/04/18 01:28:05
2014
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser.input; |
| + |
| +import android.content.Context; |
| +import android.graphics.Rect; |
| +import android.view.View; |
| +import android.view.View.MeasureSpec; |
| +import android.view.View.OnLayoutChangeListener; |
| +import android.widget.AdapterView; |
| +import android.widget.LinearLayout; |
| +import android.widget.ListPopupWindow; |
| + |
| +import org.chromium.content.R; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.content.browser.RenderCoordinates; |
| +import org.chromium.ui.base.ViewAndroidDelegate; |
| + |
| +import java.util.List; |
| + |
| +/** |
| + * The Autofill suggestion popup that lists relevant suggestions. |
| + */ |
| +public class SingleSelectPopup extends ListPopupWindow implements AdapterView.OnItemClickListener { |
| + |
| + private static final int TEXT_PADDING_DP = 30; |
| + |
| + private final ContentViewCore mContentViewCore; |
| + private final Context mContext; |
| + private final ViewAndroidDelegate mViewAndroidDelegate; |
| + private final SingleSelectPopupDelegate mSelectDelegate; |
| + private View mAnchorView; |
| + private OnLayoutChangeListener mLayoutChangeListener; |
| + |
| + /** |
| + * An interface to handle a SingleSelectPopup object. |
| + */ |
| + public interface SingleSelectPopupDelegate { |
| + /** |
| + * Handles the selection of an option from a SingleSelectPopup. |
| + * @param index The index of the selected option. |
| + */ |
| + public void optionSelected(int index); |
| + } |
| + |
| + /** |
| + * Shows the popup with the given items. |
| + * @param contentViewCore The list of items to show. |
| + * @param selectCallback The delegate that gets notified when an option has been selected. |
| + */ |
| + public SingleSelectPopup(ContentViewCore contentViewCore, |
| + SingleSelectPopupDelegate selectCallback) { |
| + super(contentViewCore.getContext(), null, 0, R.style.SingleSelectPopupWindow); |
| + mContext = contentViewCore.getContext(); |
| + mViewAndroidDelegate = contentViewCore.getViewAndroidDelegate(); |
| + mContentViewCore = contentViewCore; |
| + mSelectDelegate = selectCallback; |
| + |
| + setModal(true); |
| + setOnItemClickListener(this); |
| + |
| + mAnchorView = mViewAndroidDelegate.acquireAnchorView(); |
| + mAnchorView.setId(R.id.single_select_popup_window); |
| + mAnchorView.setTag(this); |
| + |
| + mLayoutChangeListener = new OnLayoutChangeListener() { |
| + @Override |
| + public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| + int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| + if (v == mAnchorView) SingleSelectPopup.this.show(); |
| + } |
| + }; |
| + |
| + mAnchorView.addOnLayoutChangeListener(mLayoutChangeListener); |
| + setAnchorView(mAnchorView); |
| + } |
| + |
| + @Override |
| + public void show() { |
| + super.show(); |
| + getListView().setDividerHeight(0); |
| + } |
| + |
| + /** |
| + * Shows the popup with the given items. |
| + * @param items The list of items to show. |
| + * @param originRect The rect of the select element in css pixels. |
| + */ |
| + public void show(List<SelectPopupItem> items, Rect originRect) { |
| + setAdapter(new SingleSelectPopupAdapter(mContext, items)); |
| + show(); |
| + float horizontalMargin = mContext.getResources().getDimension( |
| + R.dimen.select_popup_horizontal_margin); |
| + float borderWidth = mContext.getResources().getDimension(R.dimen.select_popup_border_width); |
| + RenderCoordinates renderCoordinates = mContentViewCore.getRenderCoordinates(); |
| + float anchorX = renderCoordinates.fromPixToDip( |
| + renderCoordinates.fromLocalCssToPix(originRect.left)) - horizontalMargin; |
| + float anchorY = renderCoordinates.fromPixToDip( |
| + renderCoordinates.fromLocalCssToPix(originRect.top)); |
| + float anchorWidth = renderCoordinates.fromPixToDip( |
| + renderCoordinates.fromLocalCssToPix(originRect.right)) - anchorX + |
| + horizontalMargin * 2; |
| + anchorWidth = Math.max(getDesiredListWidth() + (horizontalMargin + borderWidth) * 2, |
| + anchorWidth); |
| + float anchorHeight = renderCoordinates.fromPixToDip( |
| + renderCoordinates.fromLocalCssToPix(originRect.bottom)) - anchorY; |
| + mViewAndroidDelegate.setAnchorViewPosition(mAnchorView, anchorX, anchorY, anchorWidth, |
| + anchorHeight); |
| + } |
| + |
| + /** |
| + * Overrides the default dismiss behavior to request the controller to dismiss the view. |
| + */ |
| + @Override |
| + public void dismiss() { |
| + hide(); |
| + } |
| + |
| + /** |
| + * Hides the popup and removes the anchor view from the ContainerView. |
| + */ |
| + public void hide() { |
| + super.dismiss(); |
| + mAnchorView.removeOnLayoutChangeListener(mLayoutChangeListener); |
| + mAnchorView.setTag(null); |
| + mViewAndroidDelegate.releaseAnchorView(mAnchorView); |
| + } |
| + |
| + private float getDesiredListWidth() { |
| + SingleSelectPopupAdapter adapter = (SingleSelectPopupAdapter) getListView().getAdapter(); |
| + int maxWidth = 0; |
| + View view = null; |
| + //FrameLayout fakeParent = new FrameLayout(mContext); |
| + for (int i = 0; i < adapter.getCount(); i++) { |
| + view = adapter.getView(i, view, null); |
| + LinearLayout.LayoutParams params = |
| + new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, |
| + LinearLayout.LayoutParams.WRAP_CONTENT); |
| + view.setLayoutParams(params); |
| + view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); |
| + int width = view.getMeasuredWidth(); |
| + maxWidth = Math.max(maxWidth, view.getMeasuredWidth()); |
| + } |
| + RenderCoordinates renderCoordinates = mContentViewCore.getRenderCoordinates(); |
| + return renderCoordinates.fromPixToDip(maxWidth); |
| + } |
| + |
| + @Override |
| + public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| + mSelectDelegate.optionSelected(position); |
| + } |
| +} |