123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- package com.reactnativenavigation.views;
-
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.content.Context;
- import android.graphics.drawable.Drawable;
- import android.support.annotation.Nullable;
- import android.support.v7.widget.ActionMenuView;
- import android.support.v7.widget.Toolbar;
- import android.view.Menu;
- import android.view.View;
- import android.view.animation.AccelerateDecelerateInterpolator;
- import android.view.animation.AccelerateInterpolator;
- import android.widget.TextView;
-
- import com.reactnativenavigation.params.BaseTitleBarButtonParams;
- import com.reactnativenavigation.params.StyleParams;
- import com.reactnativenavigation.params.TitleBarButtonParams;
- import com.reactnativenavigation.params.TitleBarLeftButtonParams;
- import com.reactnativenavigation.utils.ViewUtils;
-
- import java.util.List;
-
- public class TitleBar extends Toolbar {
- private static final int TITLE_VISIBILITY_ANIMATION_DURATION = 320;
- private LeftButton leftButton;
- private ActionMenuView actionMenuView;
-
- public TitleBar(Context context) {
- super(context);
- }
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- super.onLayout(changed, l, t, r, b);
- }
-
- @Override
- public void onViewAdded(View child) {
- super.onViewAdded(child);
- if (child instanceof ActionMenuView) {
- actionMenuView = (ActionMenuView) child;
- }
- }
-
- public void setRightButtons(List<TitleBarButtonParams> rightButtons, String navigatorEventId) {
- Menu menu = getMenu();
- menu.clear();
- if (rightButtons == null) {
- return;
- }
- addButtonsToTitleBar(rightButtons, navigatorEventId, menu);
- }
-
- public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
- LeftButtonOnClickListener leftButtonOnClickListener,
- String navigatorEventId,
- boolean overrideBackPressInJs) {
- if (shouldSetLeftButton(leftButtonParams)) {
- createAndSetLeftButton(leftButtonParams, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
- } else if (hasLeftButton()) {
- if (leftButtonParams.hasIcon()) {
- updateLeftButton(leftButtonParams);
- } else {
- removeLeftButton();
- }
- }
- }
-
- private void removeLeftButton() {
- setNavigationIcon(null);
- leftButton = null;
- }
-
- public void setStyle(StyleParams params) {
- setVisibility(params.titleBarHidden ? GONE : VISIBLE);
- setTitleTextColor(params);
- setTitleTextFont(params);
- setSubtitleTextColor(params);
- colorOverflowButton(params);
- setBackground(params);
- centerTitle(params);
- }
-
- private void centerTitle(final StyleParams params) {
- final View titleView = getTitleView();
- ViewUtils.runOnPreDraw(titleView, new Runnable() {
- @Override
- public void run() {
- if (params.titleBarTitleTextCentered) {
- if (titleView != null) {
- int[] location = new int[2];
- titleView.getLocationOnScreen(location);
- titleView.setTranslationX(titleView.getTranslationX() + (-location[0] + ViewUtils.getScreenWidth() / 2 - titleView.getWidth() / 2));
- }
-
- }
-
- }
- });
- }
-
- private void colorOverflowButton(StyleParams params) {
- Drawable overflowIcon = actionMenuView.getOverflowIcon();
- if (shouldColorOverflowButton(params, overflowIcon)) {
- ViewUtils.tintDrawable(overflowIcon, params.titleBarButtonColor.getColor(), true);
- }
- }
-
- protected void setBackground(StyleParams params) {
- setTranslucent(params);
- }
-
- protected void setTranslucent(StyleParams params) {
- if (params.topBarTranslucent) {
- setBackground(new TranslucentDrawable());
- }
- }
-
- private boolean shouldColorOverflowButton(StyleParams params, Drawable overflowIcon) {
- return overflowIcon != null && params.titleBarButtonColor.hasColor();
- }
-
- protected void setTitleTextColor(StyleParams params) {
- if (params.titleBarTitleColor.hasColor()) {
- setTitleTextColor(params.titleBarTitleColor.getColor());
- }
- }
-
- protected void setTitleTextFont(StyleParams params) {
- if (params.titleBarTitleFont.hasFont()) {
- return;
- }
- View titleView = getTitleView();
- if (titleView instanceof TextView) {
- ((TextView) titleView).setTypeface(params.titleBarTitleFont.get());
- }
- }
-
- protected void setSubtitleTextColor(StyleParams params) {
- if (params.titleBarSubtitleColor.hasColor()) {
- setSubtitleTextColor(params.titleBarSubtitleColor.getColor());
- }
- }
-
- private void addButtonsToTitleBar(List<TitleBarButtonParams> rightButtons, String navigatorEventId, Menu menu) {
- for (int i = 0; i < rightButtons.size(); i++) {
- final TitleBarButton button = ButtonFactory.create(menu, this, rightButtons.get(i), navigatorEventId);
- addButtonInReverseOrder(rightButtons, i, button);
- }
- }
-
- protected void addButtonInReverseOrder(List<? extends BaseTitleBarButtonParams> buttons, int i, TitleBarButton button) {
- final int index = buttons.size() - i - 1;
- button.addToMenu(index);
- }
-
- private boolean hasLeftButton() {
- return leftButton != null;
- }
-
- private void updateLeftButton(TitleBarLeftButtonParams leftButtonParams) {
- leftButton.setIconState(leftButtonParams);
- }
-
- private boolean shouldSetLeftButton(TitleBarLeftButtonParams leftButtonParams) {
- return leftButton == null && leftButtonParams != null && leftButtonParams.iconState != null;
- }
-
- private void createAndSetLeftButton(TitleBarLeftButtonParams leftButtonParams,
- LeftButtonOnClickListener leftButtonOnClickListener,
- String navigatorEventId,
- boolean overrideBackPressInJs) {
- leftButton = new LeftButton(getContext(), leftButtonParams, leftButtonOnClickListener, navigatorEventId,
- overrideBackPressInJs);
- setNavigationOnClickListener(leftButton);
-
- if (leftButtonParams.icon != null) {
- setNavigationIcon(leftButtonParams.icon);
- } else {
- setNavigationIcon(leftButton);
- }
- }
-
- public void hide() {
- hide(null);
- }
-
- public void hide(@Nullable final Runnable onHidden) {
- animate()
- .alpha(0)
- .setDuration(200)
- .setInterpolator(new AccelerateInterpolator())
- .setListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- if (onHidden != null) {
- onHidden.run();
- }
- }
- });
- }
-
- public void show() {
- this.show(null);
- }
-
- public void show(final @Nullable Runnable onDisplayed) {
- setAlpha(0);
- animate()
- .alpha(1)
- .setDuration(200)
- .setInterpolator(new AccelerateDecelerateInterpolator())
- .setListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- if (onDisplayed != null) {
- onDisplayed.run();
- }
- }
- });
- }
-
- public void showTitle() {
- animateTitle(1);
- }
-
- public void hideTitle() {
- animateTitle(0);
- }
-
- private void animateTitle(int alpha) {
- View titleView = getTitleView();
- if (titleView != null) {
- titleView.animate()
- .alpha(alpha)
- .setDuration(TITLE_VISIBILITY_ANIMATION_DURATION);
- }
- }
-
- @Nullable
- protected View getTitleView() {
- return ViewUtils.findChildByClass(this, TextView.class, new ViewUtils.Matcher<TextView>() {
- @Override
- public boolean match(TextView child) {
- return child.getText().equals(getTitle());
- }
- });
- }
- }
|