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.
5 package org.chromium.content.browser.input;
7 import android.content.Context;
8 import android.graphics.Rect;
9 import android.view.View;
10 import android.widget.AdapterView;
12 import org.chromium.content.browser.ContentViewCore;
13 import org.chromium.content.browser.RenderCoordinates;
14 import org.chromium.ui.DropdownAdapter;
15 import org.chromium.ui.DropdownItem;
16 import org.chromium.ui.DropdownPopupWindow;
18 import java.util.List;
21 * Handles the dropdown popup for the <select> HTML tag support.
23 public class SelectPopupDropdown implements SelectPopup {
25 private final ContentViewCore mContentViewCore;
26 private final Context mContext;
28 private DropdownPopupWindow mDropdownPopupWindow;
29 private int mInitialSelection = -1;
31 public SelectPopupDropdown(ContentViewCore contentViewCore, List<SelectPopupItem> items,
32 Rect bounds, int[] selected) {
33 mContentViewCore = contentViewCore;
34 mContext = mContentViewCore.getContext();
35 mDropdownPopupWindow = new DropdownPopupWindow(mContext,
36 mContentViewCore.getViewAndroidDelegate());
37 mDropdownPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
39 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
40 int[] selectedIndices = {position};
41 mContentViewCore.selectPopupMenuItems(selectedIndices);
45 if (selected.length > 0) {
46 mInitialSelection = selected[0];
48 DropdownItem[] dropdownItems = items.toArray(new DropdownItem[items.size()]);
49 mDropdownPopupWindow.setAdapter(new DropdownAdapter(mContext, dropdownItems, null));
50 RenderCoordinates renderCoordinates = mContentViewCore.getRenderCoordinates();
51 float anchorX = renderCoordinates.fromPixToDip(
52 renderCoordinates.fromLocalCssToPix(bounds.left));
53 float anchorY = renderCoordinates.fromPixToDip(
54 renderCoordinates.fromLocalCssToPix(bounds.top));
55 float anchorWidth = renderCoordinates.fromPixToDip(
56 renderCoordinates.fromLocalCssToPix(bounds.right)) - anchorX;
57 float anchorHeight = renderCoordinates.fromPixToDip(
58 renderCoordinates.fromLocalCssToPix(bounds.bottom)) - anchorY;
59 mDropdownPopupWindow.setAnchorRect(anchorX, anchorY, anchorWidth, anchorHeight);
64 mDropdownPopupWindow.show();
65 if (mInitialSelection >= 0) {
66 mDropdownPopupWindow.getListView().setSelection(mInitialSelection);
72 mDropdownPopupWindow.dismiss();