|
@@ -0,0 +1,104 @@
|
|
1
|
+package com.reactnativenavigation.views;
|
|
2
|
+
|
|
3
|
+import android.app.Activity;
|
|
4
|
+import android.graphics.drawable.Drawable;
|
|
5
|
+import android.support.annotation.ColorInt;
|
|
6
|
+import android.support.annotation.NonNull;
|
|
7
|
+import android.view.Menu;
|
|
8
|
+import android.view.MenuItem;
|
|
9
|
+import android.view.View;
|
|
10
|
+import android.view.ViewTreeObserver;
|
|
11
|
+import android.widget.TextView;
|
|
12
|
+
|
|
13
|
+import com.reactnativenavigation.utils.ImageUtils;
|
|
14
|
+
|
|
15
|
+import java.util.ArrayList;
|
|
16
|
+
|
|
17
|
+public class Button {
|
|
18
|
+ public enum ShowAsAction {
|
|
19
|
+ IfRoom(MenuItem.SHOW_AS_ACTION_IF_ROOM),
|
|
20
|
+ Always(MenuItem.SHOW_AS_ACTION_ALWAYS),
|
|
21
|
+ Never(MenuItem.SHOW_AS_ACTION_NEVER),
|
|
22
|
+ WithText(MenuItem.SHOW_AS_ACTION_WITH_TEXT);
|
|
23
|
+
|
|
24
|
+ int action;
|
|
25
|
+ ShowAsAction(int action) {
|
|
26
|
+ this.action = action;
|
|
27
|
+ }
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ String label;
|
|
31
|
+ Drawable icon;
|
|
32
|
+ @ColorInt
|
|
33
|
+ int color;
|
|
34
|
+ ShowAsAction showAsAction;
|
|
35
|
+
|
|
36
|
+ public MenuItem addToMenu(Activity context, Menu menu, int index) {
|
|
37
|
+ MenuItem item = menu.add(Menu.NONE, Menu.NONE, index, label);
|
|
38
|
+ setShowAsAction(item);
|
|
39
|
+ setIcon(item);
|
|
40
|
+ setColor(context, item);
|
|
41
|
+ return item;
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ private void setShowAsAction(MenuItem item) {
|
|
45
|
+ item.setShowAsAction(showAsAction.action);
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ private void setIcon(MenuItem item) {
|
|
49
|
+ if (icon != null) {
|
|
50
|
+ item.setIcon(icon);
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ private void setColor(Activity context, MenuItem item) {
|
|
55
|
+ if (!hasColor()) {
|
|
56
|
+ return;
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ if (hasIcon()) {
|
|
60
|
+ setIconColor();
|
|
61
|
+ } else {
|
|
62
|
+ setTextColor(context);
|
|
63
|
+ }
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ private void setTextColor(Activity context) {
|
|
67
|
+ final View decorView = context.getWindow().getDecorView();
|
|
68
|
+ decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
69
|
+ @Override
|
|
70
|
+ public void onGlobalLayout() {
|
|
71
|
+ decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
|
72
|
+
|
|
73
|
+ ArrayList<View> outViews = findButtonTextView();
|
|
74
|
+ setTextColorInternal(outViews);
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ @NonNull
|
|
78
|
+ private ArrayList<View> findButtonTextView() {
|
|
79
|
+ ArrayList<View> outViews = new ArrayList<>();
|
|
80
|
+ decorView.findViewsWithText(outViews, label, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
|
|
81
|
+ return outViews;
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ private void setTextColorInternal(ArrayList<View> outViews) {
|
|
85
|
+ for (View button : outViews) {
|
|
86
|
+ ((TextView) button).setTextColor(color);
|
|
87
|
+ }
|
|
88
|
+ }
|
|
89
|
+ });
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ private void setIconColor() {
|
|
93
|
+ ImageUtils.tint(icon, color);
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ private boolean hasIcon() {
|
|
97
|
+ return icon != null;
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ private boolean hasColor() {
|
|
101
|
+ return color > 0;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+}
|