2012-12-04 13:12:04 -08:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.drawable.ColorDrawable;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.PopupWindow;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.RelativeLayout.LayoutParams;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A popup to show the inflated MenuPanel. This has an arrow pointing to the anchor.
|
|
|
|
*/
|
|
|
|
public class MenuPopup extends PopupWindow {
|
|
|
|
private Resources mResources;
|
|
|
|
|
2013-01-16 11:29:20 -08:00
|
|
|
private ImageView mArrowTop;
|
|
|
|
private ImageView mArrowBottom;
|
2012-12-04 13:12:04 -08:00
|
|
|
private RelativeLayout mPanel;
|
|
|
|
|
|
|
|
private int mYOffset;
|
|
|
|
private int mArrowMargin;
|
|
|
|
private int mPopupWidth;
|
2013-01-18 11:41:44 -08:00
|
|
|
private boolean mShowArrow;
|
2012-12-04 13:12:04 -08:00
|
|
|
|
|
|
|
public MenuPopup(Context context) {
|
|
|
|
super(context);
|
|
|
|
mResources = context.getResources();
|
|
|
|
|
|
|
|
setFocusable(true);
|
|
|
|
|
|
|
|
mYOffset = mResources.getDimensionPixelSize(R.dimen.menu_popup_offset);
|
|
|
|
mArrowMargin = mResources.getDimensionPixelSize(R.dimen.menu_popup_arrow_margin);
|
|
|
|
mPopupWidth = mResources.getDimensionPixelSize(R.dimen.menu_popup_width);
|
|
|
|
|
|
|
|
// Setting a null background makes the popup to not close on touching outside.
|
|
|
|
setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
|
|
|
setWindowLayoutMode(View.MeasureSpec.makeMeasureSpec(mPopupWidth, View.MeasureSpec.AT_MOST),
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
|
|
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
|
|
|
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.menu_popup, null);
|
|
|
|
setContentView(layout);
|
|
|
|
|
2013-01-16 11:29:20 -08:00
|
|
|
mArrowTop = (ImageView) layout.findViewById(R.id.menu_arrow_top);
|
|
|
|
mArrowBottom = (ImageView) layout.findViewById(R.id.menu_arrow_bottom);
|
2012-12-04 13:12:04 -08:00
|
|
|
mPanel = (RelativeLayout) layout.findViewById(R.id.menu_panel);
|
2013-01-18 11:41:44 -08:00
|
|
|
mShowArrow = true;
|
2013-04-25 15:19:31 -07:00
|
|
|
setAnimationStyle(R.style.PopupAnimation);
|
2012-12-04 13:12:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the panel with the menu to its content.
|
|
|
|
*
|
|
|
|
* @param view The panel view with the menu to be shown.
|
|
|
|
*/
|
|
|
|
public void setPanelView(View view) {
|
|
|
|
mPanel.removeAllViews();
|
|
|
|
mPanel.addView(view);
|
2013-01-16 11:29:20 -08:00
|
|
|
mPanel.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
2012-12-04 13:12:04 -08:00
|
|
|
}
|
|
|
|
|
2013-01-18 11:41:44 -08:00
|
|
|
/**
|
|
|
|
* Show/hide the arrow pointing to the anchor.
|
|
|
|
*
|
|
|
|
* @param show Show/hide the arrow.
|
|
|
|
*/
|
|
|
|
public void showArrowToAnchor(boolean show) {
|
|
|
|
mShowArrow = show;
|
|
|
|
}
|
|
|
|
|
2012-12-04 13:12:04 -08:00
|
|
|
/**
|
|
|
|
* A small little offset for the arrow to overlap the anchor.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void showAsDropDown(View anchor) {
|
2013-01-18 11:41:44 -08:00
|
|
|
if (!mShowArrow) {
|
|
|
|
mArrowTop.setVisibility(View.GONE);
|
|
|
|
mArrowBottom.setVisibility(View.GONE);
|
|
|
|
showAsDropDown(anchor, 0, -mYOffset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-04 13:12:04 -08:00
|
|
|
int[] anchorLocation = new int[2];
|
|
|
|
anchor.getLocationOnScreen(anchorLocation);
|
|
|
|
|
|
|
|
int screenWidth = mResources.getDisplayMetrics().widthPixels;
|
|
|
|
int arrowWidth = mResources.getDimensionPixelSize(R.dimen.menu_popup_arrow_width);
|
|
|
|
int arrowOffset = (anchor.getWidth() - arrowWidth)/2;
|
|
|
|
|
|
|
|
if (anchorLocation[0] + mPopupWidth <= screenWidth) {
|
|
|
|
// left align
|
2013-01-16 11:29:20 -08:00
|
|
|
((LayoutParams) mArrowTop.getLayoutParams()).rightMargin = mPopupWidth - anchor.getWidth() + arrowOffset;
|
|
|
|
((LayoutParams) mArrowBottom.getLayoutParams()).rightMargin = mPopupWidth - anchor.getWidth() + arrowOffset;
|
2012-12-04 13:12:04 -08:00
|
|
|
} else {
|
|
|
|
// right align
|
2013-01-16 11:29:20 -08:00
|
|
|
((LayoutParams) mArrowTop.getLayoutParams()).rightMargin = mArrowMargin;
|
|
|
|
((LayoutParams) mArrowBottom.getLayoutParams()).rightMargin = mArrowMargin;
|
2012-12-04 13:12:04 -08:00
|
|
|
}
|
|
|
|
|
2013-03-11 13:10:00 -07:00
|
|
|
// shown below anchor
|
|
|
|
mArrowTop.setVisibility(View.VISIBLE);
|
|
|
|
mArrowBottom.setVisibility(View.GONE);
|
|
|
|
showAsDropDown(anchor, 0, -mYOffset);
|
2012-12-04 13:12:04 -08:00
|
|
|
}
|
|
|
|
}
|