-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathSvgViewManager.java
361 lines (299 loc) · 9.84 KB
/
SvgViewManager.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Copyright (c) 2015-present, Horcrux.
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.horcrux.svg;
import android.graphics.Rect;
import android.util.SparseArray;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewManagerDelegate;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.viewmanagers.RNSVGSvgViewAndroidManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGSvgViewAndroidManagerInterface;
import com.facebook.react.views.view.ReactViewGroup;
import com.facebook.react.views.view.ReactViewManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Locale;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* ViewManager for RNSVGSvgView React views. Renders as a {@link SvgView} and handles invalidating
* the native view on view updates happening in the underlying tree.
*/
class SvgViewManager extends ReactViewManager
implements RNSVGSvgViewAndroidManagerInterface<SvgView> {
public static final String REACT_CLASS = "RNSVGSvgViewAndroid";
private static final SparseArray<SvgView> mTagToSvgView = new SparseArray<>();
private static final SparseArray<Runnable> mTagToRunnable = new SparseArray<>();
private final ViewManagerDelegate<SvgView> mDelegate;
protected ViewManagerDelegate getDelegate() {
return mDelegate;
}
public SvgViewManager() {
mDelegate = new RNSVGSvgViewAndroidManagerDelegate(this);
}
static void setSvgView(int tag, SvgView svg) {
mTagToSvgView.put(tag, svg);
Runnable task = mTagToRunnable.get(tag);
if (task != null) {
task.run();
mTagToRunnable.delete(tag);
}
}
static void runWhenViewIsAvailable(int tag, Runnable task) {
mTagToRunnable.put(tag, task);
}
static @Nullable SvgView getSvgViewByTag(int tag) {
return mTagToSvgView.get(tag);
}
@Nonnull
@Override
public String getName() {
return REACT_CLASS;
}
@Nonnull
@Override
public ReactViewGroup createViewInstance(ThemedReactContext reactContext) {
return new SvgView(reactContext);
}
@Override
public void updateExtraData(ReactViewGroup root, Object extraData) {
super.updateExtraData(root, extraData);
root.invalidate();
}
@Override
public void onDropViewInstance(@Nonnull ReactViewGroup view) {
super.onDropViewInstance(view);
mTagToSvgView.remove(view.getId());
}
@Override
public boolean needsCustomLayoutForChildren() {
return true;
}
@ReactProp(name = "tintColor", customType = "Color")
@Override
public void setTintColor(SvgView node, Integer tintColor) {
node.setTintColor(tintColor);
}
@ReactProp(name = "color", customType = "Color")
@Override
public void setColor(SvgView node, Integer color) {
node.setTintColor(color);
}
@ReactProp(name = "minX")
@Override
public void setMinX(SvgView node, float minX) {
node.setMinX(minX);
}
@ReactProp(name = "minY")
@Override
public void setMinY(SvgView node, float minY) {
node.setMinY(minY);
}
@ReactProp(name = "vbWidth")
@Override
public void setVbWidth(SvgView node, float vbWidth) {
node.setVbWidth(vbWidth);
}
@ReactProp(name = "vbHeight")
@Override
public void setVbHeight(SvgView node, float vbHeight) {
node.setVbHeight(vbHeight);
}
@ReactProp(name = "bbWidth")
public void setBbWidth(SvgView node, Dynamic bbWidth) {
node.setBbWidth(bbWidth);
}
@ReactProp(name = "bbHeight")
public void setBbHeight(SvgView node, Dynamic bbHeight) {
node.setBbHeight(bbHeight);
}
@ReactProp(name = "align")
@Override
public void setAlign(SvgView node, String align) {
node.setAlign(align);
}
@ReactProp(name = "meetOrSlice")
@Override
public void setMeetOrSlice(SvgView node, int meetOrSlice) {
node.setMeetOrSlice(meetOrSlice);
}
@Override
public void setBbWidth(SvgView view, @Nullable String value) {
view.setBbWidth(value);
}
public void setBbWidth(SvgView view, @Nullable Double value) {
view.setBbWidth(value);
}
@Override
public void setBbHeight(SvgView view, @Nullable String value) {
view.setBbHeight(value);
}
public void setBbHeight(SvgView view, @Nullable Double value) {
view.setBbHeight(value);
}
@ReactProp(name = ViewProps.POINTER_EVENTS)
public void setPointerEvents(SvgView view, @Nullable String pointerEventsStr) {
try {
Class<?> superclass = view.getClass().getSuperclass();
if (superclass != null) {
Method method = superclass.getDeclaredMethod("setPointerEvents", PointerEvents.class);
method.setAccessible(true);
method.invoke(
view, PointerEvents.valueOf(pointerEventsStr.toUpperCase(Locale.US).replace("-", "_")));
}
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}
@Override
public void setHasTVPreferredFocus(SvgView view, boolean value) {
super.setTVPreferredFocus(view, value);
}
@Override
public void setBorderTopEndRadius(SvgView view, float value) {
super.setBorderRadius(view, 6, value);
}
@Override
public void setBorderBottomStartRadius(SvgView view, float value) {
super.setBorderRadius(view, 7, value);
}
@Override
public void setBorderBottomColor(SvgView view, @Nullable Integer value) {
super.setBorderColor(view, 4, value);
}
@Override
public void setNextFocusDown(SvgView view, int value) {
super.nextFocusDown(view, value);
}
@Override
public void setBorderRightColor(SvgView view, @Nullable Integer value) {
super.setBorderColor(view, 2, value);
}
@Override
public void setNextFocusRight(SvgView view, int value) {
super.nextFocusRight(view, value);
}
@Override
public void setBorderLeftColor(SvgView view, @Nullable Integer value) {
super.setBorderColor(view, 1, value);
}
@Override
public void setBorderColor(SvgView view, @Nullable Integer value) {
super.setBorderColor(view, 0, value);
}
@Override
public void setRemoveClippedSubviews(SvgView view, boolean value) {
super.setRemoveClippedSubviews(view, value);
}
@Override
public void setNextFocusForward(SvgView view, int value) {
super.nextFocusForward(view, value);
}
@Override
public void setNextFocusUp(SvgView view, int value) {
super.nextFocusUp(view, value);
}
@Override
public void setAccessible(SvgView view, boolean value) {
super.setAccessible(view, value);
}
@Override
public void setBorderStartColor(SvgView view, @Nullable Integer value) {
super.setBorderColor(view, 5, value);
}
@Override
public void setBorderBottomEndRadius(SvgView view, float value) {
super.setBorderRadius(view, 8, value);
}
@Override
public void setBorderEndColor(SvgView view, @Nullable Integer value) {
super.setBorderColor(view, 6, value);
}
@Override
public void setFocusable(SvgView view, boolean value) {
super.setFocusable(view, value);
}
@Override
public void setNativeBackgroundAndroid(SvgView view, @Nullable ReadableMap value) {
super.setNativeBackground(view, value);
}
@Override
public void setBorderTopStartRadius(SvgView view, float value) {
super.setBorderRadius(view, 5, value);
}
@Override
public void setNativeForegroundAndroid(SvgView view, @Nullable ReadableMap value) {
super.setNativeForeground(view, value);
}
@Override
public void setBackfaceVisibility(SvgView view, @Nullable String value) {
super.setBackfaceVisibility(view, value);
}
@Override
public void setBorderStyle(SvgView view, @Nullable String value) {
super.setBorderStyle(view, value);
}
@Override
public void setNeedsOffscreenAlphaCompositing(SvgView view, boolean value) {
super.setNeedsOffscreenAlphaCompositing(view, value);
}
@Override
public void setHitSlop(SvgView view, @Nullable ReadableMap hitSlopMap) {
// we don't call super here since its signature changed in RN 0.69 and we want backwards
// compatibility
if (hitSlopMap != null) {
view.setHitSlopRect(
new Rect(
hitSlopMap.hasKey("left")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("left"))
: 0,
hitSlopMap.hasKey("top")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("top"))
: 0,
hitSlopMap.hasKey("right")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("right"))
: 0,
hitSlopMap.hasKey("bottom")
? (int) PixelUtil.toPixelFromDIP(hitSlopMap.getDouble("bottom"))
: 0));
}
}
@Override
public void setBorderTopColor(SvgView view, @Nullable Integer value) {
super.setBorderColor(view, 3, value);
}
@Override
public void setNextFocusLeft(SvgView view, int value) {
super.nextFocusLeft(view, value);
}
@Override
public void setBorderRadius(SvgView view, double value) {
super.setBorderRadius(view, 0, (float) value);
}
@Override
public void setBorderTopLeftRadius(SvgView view, double value) {
super.setBorderRadius(view, 1, (float) value);
}
@Override
public void setBorderTopRightRadius(SvgView view, double value) {
super.setBorderRadius(view, 2, (float) value);
}
@Override
public void setBorderBottomRightRadius(SvgView view, double value) {
super.setBorderRadius(view, 3, (float) value);
}
@Override
public void setBorderBottomLeftRadius(SvgView view, double value) {
super.setBorderRadius(view, 4, (float) value);
}
}