SelectPopupDropdown.java revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright 2014 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
5package org.chromium.content.browser.input;
6
7import android.content.Context;
8import android.graphics.Rect;
9import android.view.View;
10import android.widget.AdapterView;
11import android.widget.PopupWindow;
12
13import org.chromium.content.browser.ContentViewCore;
14import org.chromium.content.browser.RenderCoordinates;
15import org.chromium.ui.DropdownAdapter;
16import org.chromium.ui.DropdownItem;
17import org.chromium.ui.DropdownPopupWindow;
18
19import java.util.List;
20
21/**
22 * Handles the dropdown popup for the <select> HTML tag support.
23 */
24public class SelectPopupDropdown implements SelectPopup {
25
26    private final ContentViewCore mContentViewCore;
27    private final Context mContext;
28
29    private DropdownPopupWindow mDropdownPopupWindow;
30    private int mInitialSelection = -1;
31    private boolean mAlreadySelectedItems = false;
32
33    public SelectPopupDropdown(ContentViewCore contentViewCore, List<SelectPopupItem> items,
34            Rect bounds, int[] selected) {
35        mContentViewCore = contentViewCore;
36        mContext = mContentViewCore.getContext();
37        mDropdownPopupWindow = new DropdownPopupWindow(mContext,
38                mContentViewCore.getViewAndroidDelegate());
39        mDropdownPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
40            @Override
41            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
42                int[] selectedIndices = {position};
43                mContentViewCore.selectPopupMenuItems(selectedIndices);
44                mAlreadySelectedItems = true;
45                hide();
46            }
47        });
48        if (selected.length > 0) {
49            mInitialSelection = selected[0];
50        }
51        DropdownItem[] dropdownItems = items.toArray(new DropdownItem[items.size()]);
52        mDropdownPopupWindow.setAdapter(new DropdownAdapter(mContext, dropdownItems, null));
53        RenderCoordinates renderCoordinates = mContentViewCore.getRenderCoordinates();
54        float anchorX = renderCoordinates.fromPixToDip(
55                renderCoordinates.fromLocalCssToPix(bounds.left));
56        float anchorY = renderCoordinates.fromPixToDip(
57                renderCoordinates.fromLocalCssToPix(bounds.top));
58        float anchorWidth = renderCoordinates.fromPixToDip(
59                renderCoordinates.fromLocalCssToPix(bounds.right)) - anchorX;
60        float anchorHeight = renderCoordinates.fromPixToDip(
61                renderCoordinates.fromLocalCssToPix(bounds.bottom)) - anchorY;
62        mDropdownPopupWindow.setAnchorRect(anchorX, anchorY, anchorWidth, anchorHeight);
63        mDropdownPopupWindow.setOnDismissListener(
64            new PopupWindow.OnDismissListener() {
65                @Override
66                public void onDismiss() {
67                    if (!mAlreadySelectedItems) {
68                        mContentViewCore.selectPopupMenuItems(null);
69                    }
70                }
71            });
72    }
73
74    @Override
75    public void show() {
76        mDropdownPopupWindow.show();
77        if (mInitialSelection >= 0) {
78            mDropdownPopupWindow.getListView().setSelection(mInitialSelection);
79        }
80    }
81
82    @Override
83    public void hide() {
84        mDropdownPopupWindow.dismiss();
85    }
86}
87