123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package com.reactnativenavigation.views;
-
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.TextView;
-
- import com.reactnativenavigation.NavigationApplication;
- import com.reactnativenavigation.params.TitleBarButtonParams;
- import com.reactnativenavigation.utils.ViewUtils;
-
- import java.util.ArrayList;
-
- class TitleBarButton implements MenuItem.OnMenuItemClickListener {
-
- protected final Menu menu;
- protected final View parent;
- TitleBarButtonParams buttonParams;
- @Nullable protected String navigatorEventId;
-
- TitleBarButton(Menu menu, View parent, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
- this.menu = menu;
- this.parent = parent;
- this.buttonParams = buttonParams;
- this.navigatorEventId = navigatorEventId;
- }
-
- MenuItem addToMenu(int index) {
- MenuItem item = menu.add(Menu.NONE, Menu.NONE, index, buttonParams.label);
- item.setShowAsAction(buttonParams.showAsAction.action);
- item.setEnabled(buttonParams.enabled);
- setIcon(item);
- setColor();
- item.setOnMenuItemClickListener(this);
- return item;
- }
-
- private void setIcon(MenuItem item) {
- if (hasIcon()) {
- item.setIcon(buttonParams.icon);
- }
- }
-
- private void setColor() {
- if (!hasColor()) {
- return;
- }
-
- if (hasIcon()) {
- setIconColor();
- } else {
- setTextColor();
- }
- }
-
- private void setIconColor() {
- ViewUtils.tintDrawable(buttonParams.icon, buttonParams.color.getColor(), buttonParams.enabled);
- }
-
- private void setTextColor() {
- ViewUtils.runOnPreDraw(parent, new Runnable() {
- @Override
- public void run() {
- ArrayList<View> outViews = findActualTextViewInMenuByLabel();
- setTextColorForFoundButtonViews(outViews);
- }
- });
- }
-
- @NonNull
- private ArrayList<View> findActualTextViewInMenuByLabel() {
- ArrayList<View> outViews = new ArrayList<>();
- parent.findViewsWithText(outViews, buttonParams.label, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
- return outViews;
- }
-
- private void setTextColorForFoundButtonViews(ArrayList<View> outViews) {
- for (View button : outViews) {
- ((TextView) button).setTextColor(buttonParams.getColor().getColor());
- }
- }
-
- private boolean hasIcon() {
- return buttonParams.icon != null;
- }
-
- private boolean hasColor() {
- return buttonParams.color.hasColor();
- }
-
- @Override
- public boolean onMenuItemClick(MenuItem item) {
- NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(buttonParams.eventId, navigatorEventId);
- return true;
- }
- }
|