Bug 943908 - Show tooltip when long-pressing action bar items. r=wesj

This commit is contained in:
Alexandru Chiriac 2014-05-19 15:01:02 -07:00
parent 003b7c22ba
commit 70480368dc
6 changed files with 88 additions and 9 deletions

View File

@ -6,11 +6,14 @@ package org.mozilla.gecko;
import org.mozilla.gecko.widget.GeckoPopupMenu;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
class ActionModeCompat implements GeckoPopupMenu.OnMenuItemClickListener,
GeckoPopupMenu.OnMenuItemLongClickListener,
View.OnClickListener {
private final String LOGTAG = "GeckoActionModeCompat";
@ -93,9 +96,31 @@ class ActionModeCompat implements GeckoPopupMenu.OnMenuItemClickListener,
return false;
}
/* GeckoPopupMenu.onMenuItemLongClickListener */
@Override
public boolean onMenuItemLongClick(MenuItem item) {
showTooltip(item);
return true;
}
/* View.OnClickListener*/
@Override
public void onClick(View v) {
mPresenter.endActionModeCompat();
}
private void showTooltip(MenuItem item) {
// Computes the tooltip toast screen position (shown when long-tapping the menu item) with regards to the
// menu item's position (i.e below the item and slightly to the left)
int[] location = new int[2];
final View view = item.getActionView();
view.getLocationOnScreen(location);
int xOffset = location[0] - view.getWidth();
int yOffset = location[1] + view.getHeight() / 2;
Toast toast = Toast.makeText(view.getContext(), item.getTitle(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, xOffset, yOffset);
toast.show();
}
}

View File

@ -71,6 +71,7 @@ class ActionModeCompatView extends LinearLayout implements GeckoMenu.ActionItemB
public void initForMode(final ActionModeCompat mode) {
mTitleView.setOnClickListener(mode);
mPopupMenu.setOnMenuItemClickListener(mode);
mPopupMenu.setOnMenuItemLongClickListener(mode);
}
public CharSequence getTitle() {

View File

@ -353,10 +353,15 @@ public abstract class GeckoApp
}
@Override
public boolean onMenuItemSelected(MenuItem item) {
public boolean onMenuItemClick(MenuItem item) {
return onOptionsItemSelected(item);
}
@Override
public boolean onMenuItemLongClick(MenuItem item) {
return false;
}
@Override
public void openMenu() {
openOptionsMenu();

View File

@ -15,7 +15,6 @@ import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionProvider;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
@ -46,11 +45,14 @@ public class GeckoMenu extends ListView
private static final AssertBehavior THREAD_ASSERT_BEHAVIOR = AppConstants.RELEASE_BUILD ? AssertBehavior.NONE : AssertBehavior.THROW;
/*
* A callback for a menu item selected event.
* A callback for a menu item click/long click event.
*/
public static interface Callback {
// Called when a menu item is selected, with the actual menu item as the argument.
public boolean onMenuItemSelected(MenuItem item);
// Called when a menu item is clicked, with the actual menu item as the argument.
public boolean onMenuItemClick(MenuItem item);
// Called when a menu item is long-clicked, with the actual menu item as the argument.
public boolean onMenuItemLongClick(MenuItem item);
}
/*
@ -222,6 +224,13 @@ public class GeckoMenu extends ListView
handleMenuItemClick(menuItem);
}
});
((MenuItemActionBar) actionView).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
handleMenuItemLongClick(menuItem);
return true;
}
});
} else if (actionView instanceof MenuItemActionView) {
((MenuItemActionView) actionView).setMenuItemClickListener(new View.OnClickListener() {
@Override
@ -229,6 +238,13 @@ public class GeckoMenu extends ListView
handleMenuItemClick(menuItem);
}
});
((MenuItemActionView) actionView).setMenuItemLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
handleMenuItemLongClick(menuItem);
return true;
}
});
}
return added;
@ -556,7 +572,17 @@ public class GeckoMenu extends ListView
showMenu(subMenu);
} else {
close();
mCallback.onMenuItemSelected(item);
mCallback.onMenuItemClick(item);
}
}
private void handleMenuItemLongClick(GeckoMenuItem item) {
if(!item.isEnabled()) {
return;
}
if(mCallback != null) {
mCallback.onMenuItemLongClick(item);
}
}

View File

@ -12,7 +12,6 @@ import org.mozilla.gecko.R;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
@ -103,6 +102,11 @@ public class MenuItemActionView extends LinearLayout
mMenuButton.setOnClickListener(listener);
}
public void setMenuItemLongClickListener(View.OnLongClickListener listener) {
mMenuItem.setOnLongClickListener(listener);
mMenuButton.setOnLongClickListener(listener);
}
public void setActionButtonClickListener(View.OnClickListener listener) {
mActionButtonListener = listener;

View File

@ -33,6 +33,11 @@ public class GeckoPopupMenu implements GeckoMenu.Callback,
public boolean onMenuItemClick(MenuItem item);
}
// An interface for listeners for menu item long click events.
public static interface OnMenuItemLongClickListener {
public boolean onMenuItemLongClick(MenuItem item);
}
private View mAnchor;
private MenuPopup mMenuPopup;
@ -43,6 +48,7 @@ public class GeckoPopupMenu implements GeckoMenu.Callback,
private OnDismissListener mDismissListener;
private OnMenuItemClickListener mClickListener;
private OnMenuItemLongClickListener mLongClickListener;
public GeckoPopupMenu(Context context) {
initialize(context, null);
@ -117,6 +123,10 @@ public class GeckoPopupMenu implements GeckoMenu.Callback,
mClickListener = listener;
}
public void setOnMenuItemLongClickListener(OnMenuItemLongClickListener listener) {
mLongClickListener = listener;
}
/**
* Show the inflated menu.
*/
@ -138,10 +148,18 @@ public class GeckoPopupMenu implements GeckoMenu.Callback,
}
@Override
public boolean onMenuItemSelected(MenuItem item) {
if (mClickListener != null)
public boolean onMenuItemClick(MenuItem item) {
if (mClickListener != null) {
return mClickListener.onMenuItemClick(item);
}
return false;
}
@Override
public boolean onMenuItemLongClick(MenuItem item) {
if (mLongClickListener != null) {
return mLongClickListener.onMenuItemLongClick(item);
}
return false;
}