|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.menu; |
|
7 |
|
8 import org.mozilla.gecko.R; |
|
9 |
|
10 import android.content.Context; |
|
11 import android.graphics.Color; |
|
12 import android.graphics.drawable.ColorDrawable; |
|
13 import android.view.LayoutInflater; |
|
14 import android.view.View; |
|
15 import android.view.ViewGroup; |
|
16 import android.widget.LinearLayout; |
|
17 import android.widget.PopupWindow; |
|
18 |
|
19 /** |
|
20 * A popup to show the inflated MenuPanel. |
|
21 */ |
|
22 public class MenuPopup extends PopupWindow { |
|
23 private LinearLayout mPanel; |
|
24 |
|
25 private int mYOffset; |
|
26 private int mPopupWidth; |
|
27 |
|
28 public MenuPopup(Context context) { |
|
29 super(context); |
|
30 |
|
31 setFocusable(true); |
|
32 |
|
33 mYOffset = context.getResources().getDimensionPixelSize(R.dimen.menu_popup_offset); |
|
34 mPopupWidth = context.getResources().getDimensionPixelSize(R.dimen.menu_popup_width); |
|
35 |
|
36 // Setting a null background makes the popup to not close on touching outside. |
|
37 setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
|
38 setWindowLayoutMode(View.MeasureSpec.makeMeasureSpec(mPopupWidth, View.MeasureSpec.AT_MOST), |
|
39 ViewGroup.LayoutParams.WRAP_CONTENT); |
|
40 |
|
41 LayoutInflater inflater = LayoutInflater.from(context); |
|
42 mPanel = (LinearLayout) inflater.inflate(R.layout.menu_popup, null); |
|
43 setContentView(mPanel); |
|
44 |
|
45 setAnimationStyle(R.style.PopupAnimation); |
|
46 } |
|
47 |
|
48 /** |
|
49 * Adds the panel with the menu to its content. |
|
50 * |
|
51 * @param view The panel view with the menu to be shown. |
|
52 */ |
|
53 public void setPanelView(View view) { |
|
54 view.setLayoutParams(new LinearLayout.LayoutParams(mPopupWidth, |
|
55 LinearLayout.LayoutParams.WRAP_CONTENT)); |
|
56 |
|
57 mPanel.removeAllViews(); |
|
58 mPanel.addView(view); |
|
59 } |
|
60 |
|
61 /** |
|
62 * A small little offset. |
|
63 */ |
|
64 @Override |
|
65 public void showAsDropDown(View anchor) { |
|
66 showAsDropDown(anchor, 0, -mYOffset); |
|
67 } |
|
68 } |