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 5 package org.chromium.content.browser.input; 6 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; 12 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; 18 19 import java.util.List; 20 21 /** 22 * Handles the dropdown popup for the <select> HTML tag support. 23 */ 24 public class SelectPopupDropdown implements SelectPopup { 25 26 private final ContentViewCore mContentViewCore; 27 private final Context mContext; 28 private final DropdownPopupWindow mDropdownPopupWindow; 29 30 private int mInitialSelection = -1; 31 private boolean mSelectionNotified; 32 SelectPopupDropdown(ContentViewCore contentViewCore, List<SelectPopupItem> items, Rect bounds, int[] selected)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 notifySelection(new int[] {position}); 43 hide(); 44 } 45 }); 46 if (selected.length > 0) { 47 mInitialSelection = selected[0]; 48 } 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() { 63 @Override 64 public void onDismiss() { 65 notifySelection(null); 66 } 67 }); 68 } 69 notifySelection(int[] indicies)70 private void notifySelection(int[] indicies) { 71 if (mSelectionNotified) return; 72 mContentViewCore.selectPopupMenuItems(indicies); 73 mSelectionNotified = true; 74 } 75 76 @Override show()77 public void show() { 78 mDropdownPopupWindow.show(); 79 if (mInitialSelection >= 0) { 80 mDropdownPopupWindow.getListView().setSelection(mInitialSelection); 81 } 82 } 83 84 @Override hide()85 public void hide() { 86 mDropdownPopupWindow.dismiss(); 87 notifySelection(null); 88 } 89 } 90