SelectPopupDropdown.java revision 010d83a9304c5a91596085d917d248abff47903a
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; 11 12import org.chromium.content.browser.ContentViewCore; 13import org.chromium.content.browser.RenderCoordinates; 14import org.chromium.ui.DropdownAdapter; 15import org.chromium.ui.DropdownItem; 16import org.chromium.ui.DropdownPopupWindow; 17 18import java.util.List; 19 20/** 21 * Handles the dropdown popup for the <select> HTML tag support. 22 */ 23public class SelectPopupDropdown implements SelectPopup { 24 25 private final ContentViewCore mContentViewCore; 26 private final Context mContext; 27 28 private DropdownPopupWindow mDropdownPopupWindow; 29 private int mInitialSelection = -1; 30 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() { 38 @Override 39 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 40 int[] selectedIndices = {position}; 41 mContentViewCore.selectPopupMenuItems(selectedIndices); 42 hide(); 43 } 44 }); 45 if (selected.length > 0) { 46 mInitialSelection = selected[0]; 47 } 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); 60 } 61 62 @Override 63 public void show() { 64 mDropdownPopupWindow.show(); 65 if (mInitialSelection >= 0) { 66 mDropdownPopupWindow.getListView().setSelection(mInitialSelection); 67 } 68 } 69 70 @Override 71 public void hide() { 72 mDropdownPopupWindow.dismiss(); 73 } 74} 75