-
-
Notifications
You must be signed in to change notification settings - Fork 980
/
RNGestureHandlerButtonViewManager.java
261 lines (227 loc) · 9.1 KB
/
RNGestureHandlerButtonViewManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.swmansion.gesturehandler.react;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import com.facebook.react.bridge.SoftAssertions;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
public class RNGestureHandlerButtonViewManager extends
ViewGroupManager<RNGestureHandlerButtonViewManager.ButtonViewGroup> {
static class ButtonViewGroup extends ViewGroup {
static TypedValue sResolveOutValue = new TypedValue();
static ButtonViewGroup sResponder;
int mBackgroundColor = Color.TRANSPARENT;
// Using object because of handling null representing no value set.
Integer mRippleColor;
Integer mRippleRadius;
boolean mUseForeground = false;
boolean mUseBorderless = false;
float mBorderRadius = 0;
boolean mNeedBackgroundUpdate = false;
public static final String SELECTABLE_ITEM_BACKGROUND = "selectableItemBackground";
public static final String SELECTABLE_ITEM_BACKGROUND_BORDERLESS = "selectableItemBackgroundBorderless";
public ButtonViewGroup(Context context) {
super(context);
setClickable(true);
setFocusable(true);
mNeedBackgroundUpdate = true;
}
@Override
public void setBackgroundColor(int color) {
mBackgroundColor = color;
mNeedBackgroundUpdate = true;
}
public void setRippleColor(Integer color) {
mRippleColor = color;
mNeedBackgroundUpdate = true;
}
public void setRippleRadius(Integer radius) {
mRippleRadius = radius;
mNeedBackgroundUpdate = true;
}
public void setBorderRadius(float borderRadius) {
mBorderRadius = borderRadius * getResources().getDisplayMetrics().density;
mNeedBackgroundUpdate = true;
}
private Drawable applyRippleEffectWhenNeeded(Drawable selectable) {
if (mRippleColor != null
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& selectable instanceof RippleDrawable) {
int[][] states = new int[][]{ new int[]{ android.R.attr.state_enabled } };
int[] colors = new int[]{ mRippleColor };
ColorStateList colorStateList = new ColorStateList(states, colors);
((RippleDrawable) selectable).setColor(colorStateList);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& mRippleRadius != null
&& selectable instanceof RippleDrawable) {
RippleDrawable rippleDrawable = (RippleDrawable) selectable;
rippleDrawable.setRadius((int) PixelUtil.toPixelFromDIP(mRippleRadius));
}
return selectable;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (super.onInterceptTouchEvent(ev)) {
return true;
}
// We call `onTouchEvent` to and wait until button changes state to `pressed`, if it's pressed
// we return true so that the gesture handler can activate
onTouchEvent(ev);
return isPressed();
}
private void updateBackground() {
if (!mNeedBackgroundUpdate) {
return;
}
mNeedBackgroundUpdate = false;
if (mBackgroundColor == Color.TRANSPARENT) {
// reset background
setBackground(null);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// reset foreground
setForeground(null);
}
if (mUseForeground && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
setForeground(applyRippleEffectWhenNeeded(createSelectableDrawable()));
if (mBackgroundColor != Color.TRANSPARENT) {
setBackgroundColor(mBackgroundColor);
}
} else if (mBackgroundColor == Color.TRANSPARENT && mRippleColor == null) {
setBackground(createSelectableDrawable());
} else {
PaintDrawable colorDrawable = new PaintDrawable(mBackgroundColor);
Drawable selectable = createSelectableDrawable();
if (mBorderRadius != 0) {
// Radius-connected lines below ought to be considered
// as a temporary solution. It do not allow to set
// different radius on each corner. However, I suppose it's fairly
// fine for button-related use cases.
// Therefore it might be used as long as:
// 1. ReactViewManager is not a generic class with a possibility to handle another ViewGroup
// 2. There's no way to force native behavior of ReactViewGroup's superclass's onTouchEvent
colorDrawable.setCornerRadius(mBorderRadius);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
&& selectable instanceof RippleDrawable) {
PaintDrawable mask = new PaintDrawable(Color.WHITE);
mask.setCornerRadius(mBorderRadius);
((RippleDrawable) selectable).setDrawableByLayerId(android.R.id.mask, mask);
}
}
applyRippleEffectWhenNeeded(selectable);
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[] { colorDrawable, selectable});
setBackground(layerDrawable);
}
}
public void setUseDrawableOnForeground(boolean useForeground) {
mUseForeground = useForeground;
mNeedBackgroundUpdate = true;
}
public void setUseBorderlessDrawable(boolean useBorderless) {
mUseBorderless = useBorderless;
}
private Drawable createSelectableDrawable() {
final int version = Build.VERSION.SDK_INT;
String identifier = mUseBorderless && version >= 21 ? SELECTABLE_ITEM_BACKGROUND_BORDERLESS
: SELECTABLE_ITEM_BACKGROUND;
int attrID = getAttrId(getContext(), identifier);
getContext().getTheme().resolveAttribute(attrID, sResolveOutValue, true);
if (version >= 21) {
return getResources().getDrawable(sResolveOutValue.resourceId, getContext().getTheme());
} else {
return getResources().getDrawable(sResolveOutValue.resourceId);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static int getAttrId(Context context, String attr) {
SoftAssertions.assertNotNull(attr);
if (SELECTABLE_ITEM_BACKGROUND.equals(attr)) {
return android.R.attr.selectableItemBackground;
} else if (SELECTABLE_ITEM_BACKGROUND_BORDERLESS.equals(attr)) {
return android.R.attr.selectableItemBackgroundBorderless;
} else {
return context.getResources().getIdentifier(attr, "attr", "android");
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// No-op
}
@Override
public void drawableHotspotChanged(float x, float y) {
if (sResponder == null || sResponder == this) {
super.drawableHotspotChanged(x, y);
}
}
@Override
public void setPressed(boolean pressed) {
if (pressed && sResponder == null) {
// first button to be pressed grabs button responder
sResponder = this;
}
if (!pressed || sResponder == this) {
// we set pressed state only for current responder
super.setPressed(pressed);
}
if (!pressed && sResponder == this) {
// if the responder is no longer pressed we release button responder
sResponder = null;
}
}
@Override
public void dispatchDrawableHotspotChanged(float x, float y) {
// by default viewgroup would pass hotspot change events
}
}
@Override
public String getName() {
return "RNGestureHandlerButton";
}
@Override
public ButtonViewGroup createViewInstance(ThemedReactContext context) {
return new ButtonViewGroup(context);
}
@TargetApi(Build.VERSION_CODES.M)
@ReactProp(name = "foreground")
public void setForeground(ButtonViewGroup view, boolean useDrawableOnForeground) {
view.setUseDrawableOnForeground(useDrawableOnForeground);
}
@ReactProp(name = "borderless")
public void setBorderless(ButtonViewGroup view, boolean useBorderlessDrawable) {
view.setUseBorderlessDrawable(useBorderlessDrawable);
}
@ReactProp(name = "enabled")
public void setEnabled(ButtonViewGroup view, boolean enabled) {
view.setEnabled(enabled);
}
@ReactProp(name = ViewProps.BORDER_RADIUS)
public void setBorderRadius(ButtonViewGroup view, float borderRadius) {
view.setBorderRadius(borderRadius);
}
@ReactProp(name = "rippleColor")
public void setRippleColor(ButtonViewGroup view, Integer rippleColor) {
view.setRippleColor(rippleColor);
}
@ReactProp(name = "rippleRadius")
public void setRippleRadius(ButtonViewGroup view, Integer rippleRadius) {
view.setRippleRadius(rippleRadius);
}
@Override
protected void onAfterUpdateTransaction(ButtonViewGroup view) {
view.updateBackground();
}
}