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;
11 import android.widget.PopupWindow;
13 import org.chromium.content.browser.ContentViewCore;
14 import org.chromium.content.browser.RenderCoordinates;
15 import org.chromium.ui.DropdownAdapter;
16 import org.chromium.ui.DropdownItem;
17 import org.chromium.ui.DropdownPopupWindow;
19 import java.util.List;
22 * Handles the dropdown popup for the <select> HTML tag support.
24 public class SelectPopupDropdown implements SelectPopup {
26 private final ContentViewCore mContentViewCore;
27 private final Context mContext;
28 private final DropdownPopupWindow mDropdownPopupWindow;
30 private int mInitialSelection = -1;
31 private boolean mSelectionNotified;
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() {
41 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
42 notifySelection(new int[] {position});
46 if (selected.length > 0) {
47 mInitialSelection = selected[0];
49 DropdownItem[] dropdownItems = items.toArray(new DropdownItem[items.size()]);
50 mDropdownPopupWindow.setAdapter(new DropdownAdapter(mContext, dropdownItems, null));
51 RenderCoordinates renderCoordinates = mContentViewCore.getRenderCoordinates();
52 float anchorX = renderCoordinates.fromPixToDip(
53 renderCoordinates.fromLocalCssToPix(bounds.left));
54 float anchorY = renderCoordinates.fromPixToDip(
55 renderCoordinates.fromLocalCssToPix(bounds.top));
56 float anchorWidth = renderCoordinates.fromPixToDip(
57 renderCoordinates.fromLocalCssToPix(bounds.right)) - anchorX;
58 float anchorHeight = renderCoordinates.fromPixToDip(
59 renderCoordinates.fromLocalCssToPix(bounds.bottom)) - anchorY;
60 mDropdownPopupWindow.setAnchorRect(anchorX, anchorY, anchorWidth, anchorHeight);
61 mDropdownPopupWindow.setOnDismissListener(
62 new PopupWindow.OnDismissListener() {
64 public void onDismiss() {
65 notifySelection(null);
70 private void notifySelection(int[] indicies) {
71 if (mSelectionNotified) return;
72 mContentViewCore.selectPopupMenuItems(indicies);
73 mSelectionNotified = true;
78 mDropdownPopupWindow.show();
79 if (mInitialSelection >= 0) {
80 mDropdownPopupWindow.getListView().setSelection(mInitialSelection);
86 mDropdownPopupWindow.dismiss();
87 notifySelection(null);