Skip to content

Commit

Permalink
Pass ActionMenuView as parent to right buttons instead of Toolbar (wi…
Browse files Browse the repository at this point in the history
…x#3091)

Motivation:
When searching for the button's concrete TextView by label, if the
title has the same text as the buttons, both views are returned.
  • Loading branch information
guyca authored Apr 22, 2018
1 parent 5786592 commit bee7e1a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void setBackButton(TitleBarLeftButtonParams leftButton) {

private void addButtonsToContextualMenu(List<ContextualMenuButtonParams> buttons, Menu menu) {
for (int i = 0; i < buttons.size(); i++) {
final TitleBarButton button = new ContextualMenuButton(menu, this, buttons.get(i), this);
final TitleBarButton button = new ContextualMenuButton(menu, getActionMenuView(), buttons.get(i), this);
addButtonInReverseOrder(buttons, i, button);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.reactnativenavigation.views;

import android.support.v7.widget.ActionMenuView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;

import com.reactnativenavigation.params.ContextualMenuButtonParams;

Expand All @@ -14,7 +14,7 @@ interface ContextualButtonClickListener {
void onClick(int index);
}

ContextualMenuButton(Menu menu, ViewGroup parent, ContextualMenuButtonParams contextualMenuButtonParams, ContextualButtonClickListener contextualButtonClickListener) {
ContextualMenuButton(Menu menu, ActionMenuView parent, ContextualMenuButtonParams contextualMenuButtonParams, ContextualButtonClickListener contextualButtonClickListener) {
super(menu, parent, contextualMenuButtonParams, null);
this.contextualMenuButtonParams = contextualMenuButtonParams;
this.contextualButtonClickListener = contextualButtonClickListener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected void setSubtitleTextColor(StyleParams params) {

private void addButtonsToTitleBar(String navigatorEventId, Menu menu) {
for (int i = 0; i < rightButtons.size(); i++) {
final TitleBarButton button = new TitleBarButton(menu, this, rightButtons.get(i), navigatorEventId);
final TitleBarButton button = new TitleBarButton(menu, getActionMenuView(), rightButtons.get(i), navigatorEventId);
addButtonInReverseOrder(rightButtons, i, button);
}
}
Expand Down Expand Up @@ -368,7 +368,7 @@ private void updateButtonColor(StyleParams.Color titleBarButtonColor) {
}

private void setButtonTextColor() {
final ActionMenuView buttonsContainer = ViewUtils.findChildByClass(this, ActionMenuView.class);
final ActionMenuView buttonsContainer = getActionMenuView();
if (buttonsContainer != null) {
for (int i = 0; i < buttonsContainer.getChildCount(); i++) {
if (buttonsContainer.getChildAt(i) instanceof TextView) {
Expand All @@ -378,6 +378,10 @@ private void setButtonTextColor() {
}
}

protected ActionMenuView getActionMenuView() {
return ViewUtils.findChildByClass(this, ActionMenuView.class);
}

private void setButtonsIconColor() {
final Menu menu = getMenu();
for (int i = 0; i < menu.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.reactnativenavigation.NavigationApplication;
Expand All @@ -21,29 +20,28 @@
class TitleBarButton implements MenuItem.OnMenuItemClickListener {

protected final Menu menu;
protected final ViewGroup parent;
private final ActionMenuView actionMenuView;
private TitleBarButtonParams buttonParams;
@Nullable protected String navigatorEventId;

TitleBarButton(Menu menu, ViewGroup parent, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
TitleBarButton(Menu menu, ActionMenuView actionMenuView, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
this.menu = menu;
this.parent = parent;
this.actionMenuView = actionMenuView;
this.buttonParams = buttonParams;
this.navigatorEventId = navigatorEventId;
}

MenuItem addToMenu(int index) {
void addToMenu(int index) {
MenuItem item = createMenuItem(index);
item.setShowAsAction(buttonParams.showAsAction.action);
item.setEnabled(buttonParams.enabled);
if (buttonParams.hasComponent()) {
item.setActionView(new TitleBarButtonComponent(parent.getContext(), buttonParams.componentName, buttonParams.componentProps));
item.setActionView(new TitleBarButtonComponent(actionMenuView.getContext(), buttonParams.componentName, buttonParams.componentProps));
}
setIcon(item, index);
setColor();
setFont();
item.setOnMenuItemClickListener(this);
return item;
}

private MenuItem createMenuItem(int index) {
Expand All @@ -66,10 +64,9 @@ private void setIcon(MenuItem item, int index) {
}

private void dontShowLabelOnLongPress(final int index) {
ViewUtils.runOnPreDraw(parent, new Runnable() {
ViewUtils.runOnPreDraw(actionMenuView, new Runnable() {
@Override
public void run() {
ActionMenuView actionMenuView = ViewUtils.findChildByClass(parent, ActionMenuView.class);
if (actionMenuView != null && actionMenuView.getChildAt(index) != null) {
actionMenuView.getChildAt(index).setOnLongClickListener(null);
}
Expand All @@ -94,7 +91,7 @@ private void setIconColor() {
}

private void setTextColor() {
ViewUtils.runOnPreDraw(parent, new Runnable() {
ViewUtils.runOnPreDraw(actionMenuView, new Runnable() {
@Override
public void run() {
ArrayList<View> outViews = findActualTextViewInMenuByLabel();
Expand All @@ -114,7 +111,7 @@ private void setFont() {
@NonNull
private ArrayList<View> findActualTextViewInMenuByLabel() {
ArrayList<View> outViews = new ArrayList<>();
parent.findViewsWithText(outViews, buttonParams.label, View.FIND_VIEWS_WITH_TEXT);
actionMenuView.findViewsWithText(outViews, buttonParams.label, View.FIND_VIEWS_WITH_TEXT);
return outViews;
}

Expand Down

0 comments on commit bee7e1a

Please sign in to comment.