forked from software-mansion/react-native-svg
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderableViewManager.java
More file actions
1761 lines (1474 loc) · 53.8 KB
/
Copy pathRenderableViewManager.java
File metadata and controls
1761 lines (1474 loc) · 53.8 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 static com.facebook.react.uimanager.ViewProps.ALIGN_CONTENT;
import static com.facebook.react.uimanager.ViewProps.ALIGN_ITEMS;
import static com.facebook.react.uimanager.ViewProps.ALIGN_SELF;
import static com.facebook.react.uimanager.ViewProps.BORDER_BOTTOM_WIDTH;
import static com.facebook.react.uimanager.ViewProps.BORDER_END_WIDTH;
import static com.facebook.react.uimanager.ViewProps.BORDER_LEFT_WIDTH;
import static com.facebook.react.uimanager.ViewProps.BORDER_RIGHT_WIDTH;
import static com.facebook.react.uimanager.ViewProps.BORDER_START_WIDTH;
import static com.facebook.react.uimanager.ViewProps.BORDER_TOP_WIDTH;
import static com.facebook.react.uimanager.ViewProps.BORDER_WIDTH;
import static com.facebook.react.uimanager.ViewProps.BOTTOM;
import static com.facebook.react.uimanager.ViewProps.COLLAPSABLE;
import static com.facebook.react.uimanager.ViewProps.DISPLAY;
import static com.facebook.react.uimanager.ViewProps.END;
import static com.facebook.react.uimanager.ViewProps.FLEX;
import static com.facebook.react.uimanager.ViewProps.FLEX_BASIS;
import static com.facebook.react.uimanager.ViewProps.FLEX_DIRECTION;
import static com.facebook.react.uimanager.ViewProps.FLEX_GROW;
import static com.facebook.react.uimanager.ViewProps.FLEX_SHRINK;
import static com.facebook.react.uimanager.ViewProps.FLEX_WRAP;
import static com.facebook.react.uimanager.ViewProps.HEIGHT;
import static com.facebook.react.uimanager.ViewProps.JUSTIFY_CONTENT;
import static com.facebook.react.uimanager.ViewProps.LEFT;
import static com.facebook.react.uimanager.ViewProps.MARGIN;
import static com.facebook.react.uimanager.ViewProps.MARGIN_BOTTOM;
import static com.facebook.react.uimanager.ViewProps.MARGIN_END;
import static com.facebook.react.uimanager.ViewProps.MARGIN_HORIZONTAL;
import static com.facebook.react.uimanager.ViewProps.MARGIN_LEFT;
import static com.facebook.react.uimanager.ViewProps.MARGIN_RIGHT;
import static com.facebook.react.uimanager.ViewProps.MARGIN_START;
import static com.facebook.react.uimanager.ViewProps.MARGIN_TOP;
import static com.facebook.react.uimanager.ViewProps.MARGIN_VERTICAL;
import static com.facebook.react.uimanager.ViewProps.MAX_HEIGHT;
import static com.facebook.react.uimanager.ViewProps.MAX_WIDTH;
import static com.facebook.react.uimanager.ViewProps.MIN_HEIGHT;
import static com.facebook.react.uimanager.ViewProps.MIN_WIDTH;
import static com.facebook.react.uimanager.ViewProps.OVERFLOW;
import static com.facebook.react.uimanager.ViewProps.PADDING;
import static com.facebook.react.uimanager.ViewProps.PADDING_BOTTOM;
import static com.facebook.react.uimanager.ViewProps.PADDING_END;
import static com.facebook.react.uimanager.ViewProps.PADDING_HORIZONTAL;
import static com.facebook.react.uimanager.ViewProps.PADDING_LEFT;
import static com.facebook.react.uimanager.ViewProps.PADDING_RIGHT;
import static com.facebook.react.uimanager.ViewProps.PADDING_START;
import static com.facebook.react.uimanager.ViewProps.PADDING_TOP;
import static com.facebook.react.uimanager.ViewProps.PADDING_VERTICAL;
import static com.facebook.react.uimanager.ViewProps.POSITION;
import static com.facebook.react.uimanager.ViewProps.RIGHT;
import static com.facebook.react.uimanager.ViewProps.START;
import static com.facebook.react.uimanager.ViewProps.TOP;
import static com.facebook.react.uimanager.ViewProps.WIDTH;
import static com.horcrux.svg.RenderableView.CAP_ROUND;
import static com.horcrux.svg.RenderableView.FILL_RULE_NONZERO;
import static com.horcrux.svg.RenderableView.JOIN_ROUND;
import android.graphics.Matrix;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.JavaOnlyMap;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.LayoutShadowNode;
import com.facebook.react.uimanager.MatrixMathHelper;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.TransformHelper;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.ViewManagerDelegate;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.annotations.ReactPropGroup;
import com.facebook.react.viewmanagers.RNSVGCircleManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGCircleManagerInterface;
import com.facebook.react.viewmanagers.RNSVGClipPathManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGClipPathManagerInterface;
import com.facebook.react.viewmanagers.RNSVGDefsManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGDefsManagerInterface;
import com.facebook.react.viewmanagers.RNSVGEllipseManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGEllipseManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFeBlendManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFeBlendManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFeColorMatrixManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFeColorMatrixManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFeCompositeManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFeCompositeManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFeFloodManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFeFloodManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFeGaussianBlurManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFeGaussianBlurManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFeMergeManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFeMergeManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFeOffsetManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFeOffsetManagerInterface;
import com.facebook.react.viewmanagers.RNSVGFilterManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGFilterManagerInterface;
import com.facebook.react.viewmanagers.RNSVGForeignObjectManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGForeignObjectManagerInterface;
import com.facebook.react.viewmanagers.RNSVGGroupManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGGroupManagerInterface;
import com.facebook.react.viewmanagers.RNSVGImageManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGImageManagerInterface;
import com.facebook.react.viewmanagers.RNSVGLineManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGLineManagerInterface;
import com.facebook.react.viewmanagers.RNSVGLinearGradientManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGLinearGradientManagerInterface;
import com.facebook.react.viewmanagers.RNSVGMarkerManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGMarkerManagerInterface;
import com.facebook.react.viewmanagers.RNSVGMaskManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGMaskManagerInterface;
import com.facebook.react.viewmanagers.RNSVGPathManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGPathManagerInterface;
import com.facebook.react.viewmanagers.RNSVGPatternManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGPatternManagerInterface;
import com.facebook.react.viewmanagers.RNSVGRadialGradientManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGRadialGradientManagerInterface;
import com.facebook.react.viewmanagers.RNSVGRectManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGRectManagerInterface;
import com.facebook.react.viewmanagers.RNSVGSymbolManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGSymbolManagerInterface;
import com.facebook.react.viewmanagers.RNSVGTSpanManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGTSpanManagerInterface;
import com.facebook.react.viewmanagers.RNSVGTextManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGTextManagerInterface;
import com.facebook.react.viewmanagers.RNSVGTextPathManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGTextPathManagerInterface;
import com.facebook.react.viewmanagers.RNSVGUseManagerDelegate;
import com.facebook.react.viewmanagers.RNSVGUseManagerInterface;
import com.horcrux.svg.events.SvgLoadEvent;
import com.horcrux.svg.events.SvgOnLayoutEvent;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** ViewManager for DefinitionView RNSVG views */
class VirtualViewManager<V extends VirtualView> extends ViewGroupManager<VirtualView> {
protected final SVGClass svgClass;
protected final String mClassName;
protected VirtualViewManager(SVGClass svgclass) {
svgClass = svgclass;
mClassName = svgclass.toString();
}
protected ViewManagerDelegate<V> mDelegate;
protected ViewManagerDelegate getDelegate() {
return mDelegate;
}
static class RenderableShadowNode extends LayoutShadowNode {
@SuppressWarnings({"unused", "EmptyMethod"})
@ReactPropGroup(
names = {
ALIGN_SELF,
ALIGN_ITEMS,
COLLAPSABLE,
FLEX,
FLEX_BASIS,
FLEX_DIRECTION,
FLEX_GROW,
FLEX_SHRINK,
FLEX_WRAP,
JUSTIFY_CONTENT,
OVERFLOW,
ALIGN_CONTENT,
DISPLAY,
/* position */
POSITION,
RIGHT,
TOP,
BOTTOM,
LEFT,
START,
END,
/* dimensions */
WIDTH,
HEIGHT,
MIN_WIDTH,
MAX_WIDTH,
MIN_HEIGHT,
MAX_HEIGHT,
/* margins */
MARGIN,
MARGIN_VERTICAL,
MARGIN_HORIZONTAL,
MARGIN_LEFT,
MARGIN_RIGHT,
MARGIN_TOP,
MARGIN_BOTTOM,
MARGIN_START,
MARGIN_END,
/* paddings */
PADDING,
PADDING_VERTICAL,
PADDING_HORIZONTAL,
PADDING_LEFT,
PADDING_RIGHT,
PADDING_TOP,
PADDING_BOTTOM,
PADDING_START,
PADDING_END,
BORDER_WIDTH,
BORDER_START_WIDTH,
BORDER_END_WIDTH,
BORDER_TOP_WIDTH,
BORDER_BOTTOM_WIDTH,
BORDER_LEFT_WIDTH,
BORDER_RIGHT_WIDTH,
})
public void ignoreLayoutProps(int index, Dynamic value) {}
}
@Override
public LayoutShadowNode createShadowNodeInstance() {
return new RenderableShadowNode();
}
@Override
public Class<? extends LayoutShadowNode> getShadowNodeClass() {
return RenderableShadowNode.class;
}
private static final MatrixMathHelper.MatrixDecompositionContext sMatrixDecompositionContext =
new MatrixMathHelper.MatrixDecompositionContext();
private static final double[] sTransformDecompositionArray = new double[16];
private static final int PERSPECTIVE_ARRAY_INVERTED_CAMERA_DISTANCE_INDEX = 2;
private static final float CAMERA_DISTANCE_NORMALIZATION_MULTIPLIER = 5;
private static float sanitizeFloatPropertyValue(float value) {
if (value >= -Float.MAX_VALUE && value <= Float.MAX_VALUE) {
return value;
}
if (value < -Float.MAX_VALUE || value == Float.NEGATIVE_INFINITY) {
return -Float.MAX_VALUE;
}
if (value > Float.MAX_VALUE || value == Float.POSITIVE_INFINITY) {
return Float.MAX_VALUE;
}
if (Float.isNaN(value)) {
return 0;
}
// Shouldn't be possible to reach this point.
throw new IllegalStateException("Invalid float property value: " + value);
}
protected void setTransformProperty(VirtualView view, ReadableArray transforms) {
if (transforms == null) {
view.setTranslationX(PixelUtil.toPixelFromDIP(0));
view.setTranslationY(PixelUtil.toPixelFromDIP(0));
view.setRotation(0);
view.setRotationX(0);
view.setRotationY(0);
view.setScaleX(1);
view.setScaleY(1);
view.setCameraDistance(0);
return;
}
sMatrixDecompositionContext.reset();
TransformHelper.processTransform(
transforms, sTransformDecompositionArray, view.getWidth(), view.getHeight(), null, false);
MatrixMathHelper.decomposeMatrix(sTransformDecompositionArray, sMatrixDecompositionContext);
view.setTranslationX(
PixelUtil.toPixelFromDIP(
sanitizeFloatPropertyValue((float) sMatrixDecompositionContext.translation[0])));
view.setTranslationY(
PixelUtil.toPixelFromDIP(
sanitizeFloatPropertyValue((float) sMatrixDecompositionContext.translation[1])));
view.setRotation(
sanitizeFloatPropertyValue((float) sMatrixDecompositionContext.rotationDegrees[2]));
view.setRotationX(
sanitizeFloatPropertyValue((float) sMatrixDecompositionContext.rotationDegrees[0]));
view.setRotationY(
sanitizeFloatPropertyValue((float) sMatrixDecompositionContext.rotationDegrees[1]));
view.setScaleX(sanitizeFloatPropertyValue((float) sMatrixDecompositionContext.scale[0]));
view.setScaleY(sanitizeFloatPropertyValue((float) sMatrixDecompositionContext.scale[1]));
double[] perspectiveArray = sMatrixDecompositionContext.perspective;
if (perspectiveArray.length > PERSPECTIVE_ARRAY_INVERTED_CAMERA_DISTANCE_INDEX) {
float invertedCameraDistance =
(float) perspectiveArray[PERSPECTIVE_ARRAY_INVERTED_CAMERA_DISTANCE_INDEX];
if (invertedCameraDistance == 0) {
// Default camera distance, before scale multiplier (1280)
invertedCameraDistance = 0.00078125f;
}
float cameraDistance = -1 / invertedCameraDistance;
float scale = DisplayMetricsHolder.getScreenDisplayMetrics().density;
// The following converts the matrix's perspective to a camera distance
// such that the camera perspective looks the same on Android and iOS.
// The native Android implementation removed the screen density from the
// calculation, so squaring and a normalization value of
// sqrt(5) produces an exact replica with iOS.
// For more information, see https://github.com/facebook/react-native/pull/18302
float normalizedCameraDistance =
scale * scale * cameraDistance * CAMERA_DISTANCE_NORMALIZATION_MULTIPLIER;
view.setCameraDistance(normalizedCameraDistance);
}
}
@Nonnull
public String getName() {
return mClassName;
}
@ReactProp(name = "mask")
public void setMask(V node, String mask) {
node.setMask(mask);
}
@ReactProp(name = "markerStart")
public void setMarkerStart(V node, String markerStart) {
node.setMarkerStart(markerStart);
}
@ReactProp(name = "markerMid")
public void setMarkerMid(V node, String markerMid) {
node.setMarkerMid(markerMid);
}
@ReactProp(name = "markerEnd")
public void setMarkerEnd(V node, String markerEnd) {
node.setMarkerEnd(markerEnd);
}
@ReactProp(name = "clipPath")
public void setClipPath(V node, String clipPath) {
node.setClipPath(clipPath);
}
@ReactProp(name = "clipRule")
public void setClipRule(V node, int clipRule) {
node.setClipRule(clipRule);
}
@ReactProp(name = "opacity", defaultFloat = 1f)
public void setOpacity(@Nonnull V node, float opacity) {
node.setOpacity(opacity);
}
@ReactProp(name = "responsible")
public void setResponsible(V node, boolean responsible) {
node.setResponsible(responsible);
}
@ReactProp(name = ViewProps.POINTER_EVENTS)
public void setPointerEvents(V view, @Nullable String pointerEventsStr) {
if (pointerEventsStr == null) {
view.setPointerEvents(PointerEvents.AUTO);
} else {
PointerEvents pointerEvents =
PointerEvents.valueOf(pointerEventsStr.toUpperCase(Locale.US).replace("-", "_"));
view.setPointerEvents(pointerEvents);
}
}
@ReactProp(name = "name")
public void setName(V node, String name) {
node.setName(name);
}
@ReactProp(name = "display")
public void setDisplay(V node, String display) {
node.setDisplay(display);
}
@ReactProp(name = "matrix")
public void setMatrix(V node, Dynamic matrixArray) {
node.setMatrix(matrixArray);
}
public void setMatrix(V view, @Nullable ReadableArray value) {
view.setMatrix(value);
}
@Override
public void setTransform(VirtualView node, @Nullable ReadableArray matrix) {
setTransformProperty(node, matrix);
Matrix m = node.getMatrix();
node.mMatrix = m;
node.mInvertible = m.invert(node.mInvMatrix);
}
@ReactProp(name = "transform")
public void setTransform(V node, Dynamic matrix) {
if (matrix.getType() != ReadableType.Array) {
return;
}
ReadableArray ma = matrix.asArray();
setTransform(node, ma);
}
private void invalidateSvgView(V node) {
SvgView view = node.getSvgView();
if (view != null) {
view.invalidate();
}
if (node instanceof TextView) {
((TextView) node).getTextContainer().clearChildCache();
}
}
@Override
protected void addEventEmitters(
@Nonnull ThemedReactContext reactContext, @Nonnull VirtualView view) {
super.addEventEmitters(reactContext, view);
view.setOnHierarchyChangeListener(
new ViewGroup.OnHierarchyChangeListener() {
@Override
public void onChildViewAdded(View view, View view1) {
if (view instanceof VirtualView) {
invalidateSvgView((V) view);
}
}
@Override
public void onChildViewRemoved(View view, View view1) {
if (view instanceof VirtualView) {
invalidateSvgView((V) view);
}
}
});
}
/**
* Callback that will be triggered after all properties are updated in current update transaction
* (all @ReactProp handlers for properties updated in current transaction have been called). If
* you want to override this method you should call super.onAfterUpdateTransaction from it as the
* parent class of the ViewManager may rely on callback being executed.
*/
@Override
protected void onAfterUpdateTransaction(@Nonnull VirtualView node) {
super.onAfterUpdateTransaction(node);
invalidateSvgView((V) node);
}
protected enum SVGClass {
RNSVGGroup,
RNSVGPath,
RNSVGText,
RNSVGTSpan,
RNSVGTextPath,
RNSVGImage,
RNSVGCircle,
RNSVGEllipse,
RNSVGLine,
RNSVGRect,
RNSVGClipPath,
RNSVGDefs,
RNSVGUse,
RNSVGSymbol,
RNSVGLinearGradient,
RNSVGRadialGradient,
RNSVGPattern,
RNSVGMask,
RNSVGFilter,
RNSVGFeBlend,
RNSVGFeColorMatrix,
RNSVGFeComposite,
RNSVGFeFlood,
RNSVGFeGaussianBlur,
RNSVGFeMerge,
RNSVGFeOffset,
RNSVGMarker,
RNSVGForeignObject,
}
@Nonnull
@Override
protected VirtualView createViewInstance(@Nonnull ThemedReactContext reactContext) {
switch (svgClass) {
case RNSVGGroup:
return new GroupView(reactContext);
case RNSVGPath:
return new PathView(reactContext);
case RNSVGCircle:
return new CircleView(reactContext);
case RNSVGEllipse:
return new EllipseView(reactContext);
case RNSVGLine:
return new LineView(reactContext);
case RNSVGRect:
return new RectView(reactContext);
case RNSVGText:
return new TextView(reactContext);
case RNSVGTSpan:
return new TSpanView(reactContext);
case RNSVGTextPath:
return new TextPathView(reactContext);
case RNSVGImage:
return new ImageView(reactContext);
case RNSVGClipPath:
return new ClipPathView(reactContext);
case RNSVGDefs:
return new DefsView(reactContext);
case RNSVGUse:
return new UseView(reactContext);
case RNSVGSymbol:
return new SymbolView(reactContext);
case RNSVGLinearGradient:
return new LinearGradientView(reactContext);
case RNSVGRadialGradient:
return new RadialGradientView(reactContext);
case RNSVGPattern:
return new PatternView(reactContext);
case RNSVGMask:
return new MaskView(reactContext);
case RNSVGFilter:
return new FilterView(reactContext);
case RNSVGFeBlend:
return new FeBlendView(reactContext);
case RNSVGFeColorMatrix:
return new FeColorMatrixView(reactContext);
case RNSVGFeComposite:
return new FeCompositeView(reactContext);
case RNSVGFeFlood:
return new FeFloodView(reactContext);
case RNSVGFeGaussianBlur:
return new FeGaussianBlurView(reactContext);
case RNSVGFeMerge:
return new FeMergeView(reactContext);
case RNSVGFeOffset:
return new FeOffsetView(reactContext);
case RNSVGMarker:
return new MarkerView(reactContext);
case RNSVGForeignObject:
return new ForeignObjectView(reactContext);
default:
throw new IllegalStateException("Unexpected type " + svgClass.toString());
}
}
private static final SparseArray<RenderableView> mTagToRenderableView = new SparseArray<>();
private static final SparseArray<Runnable> mTagToRunnable = new SparseArray<>();
static void setRenderableView(int tag, RenderableView svg) {
mTagToRenderableView.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 RenderableView getRenderableViewByTag(int tag) {
return mTagToRenderableView.get(tag);
}
@Override
public void onDropViewInstance(@Nonnull VirtualView view) {
super.onDropViewInstance(view);
mTagToRenderableView.remove(view.getId());
}
}
/** ViewManager for Renderable RNSVG views */
class RenderableViewManager<T extends RenderableView> extends VirtualViewManager<T> {
RenderableViewManager(SVGClass svgclass) {
super(svgclass);
}
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
Map<String, Object> eventTypes = new HashMap<>();
eventTypes.put(SvgOnLayoutEvent.EVENT_NAME, MapBuilder.of("registrationName", "onSvgLayout"));
return eventTypes;
}
static class GroupViewManagerAbstract<U extends GroupView> extends RenderableViewManager<U> {
GroupViewManagerAbstract(SVGClass svgClass) {
super(svgClass);
}
@ReactProp(name = "font")
public void setFont(U node, Dynamic font) {
node.setFont(font);
}
@ReactProp(name = "fontSize")
public void setFontSize(U node, Dynamic fontSize) {
JavaOnlyMap map = new JavaOnlyMap();
switch (fontSize.getType()) {
case Number:
map.putDouble("fontSize", fontSize.asDouble());
break;
case String:
map.putString("fontSize", fontSize.asString());
break;
default:
return;
}
node.setFont(map);
}
@ReactProp(name = "fontWeight")
public void setFontWeight(U node, Dynamic fontWeight) {
JavaOnlyMap map = new JavaOnlyMap();
switch (fontWeight.getType()) {
case Number:
map.putDouble("fontWeight", fontWeight.asDouble());
break;
case String:
map.putString("fontWeight", fontWeight.asString());
break;
default:
return;
}
node.setFont(map);
}
}
static class GroupViewManager extends GroupViewManagerAbstract<GroupView>
implements RNSVGGroupManagerInterface<GroupView> {
GroupViewManager() {
super(SVGClass.RNSVGGroup);
mDelegate = new RNSVGGroupManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGGroup";
}
static class PathViewManager extends RenderableViewManager<PathView>
implements RNSVGPathManagerInterface<PathView> {
PathViewManager() {
super(SVGClass.RNSVGPath);
mDelegate = new RNSVGPathManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGPath";
@ReactProp(name = "d")
public void setD(PathView node, String d) {
node.setD(d);
}
}
static class TextViewManagerAbstract<K extends TextView> extends GroupViewManagerAbstract<K> {
TextViewManagerAbstract(SVGClass svgClass) {
super(svgClass);
}
@ReactProp(name = "inlineSize")
public void setInlineSize(K node, Dynamic inlineSize) {
node.setInlineSize(inlineSize);
}
@ReactProp(name = "textLength")
public void setTextLength(K node, Dynamic length) {
node.setTextLength(length);
}
@ReactProp(name = "lengthAdjust")
public void setLengthAdjust(K node, @Nullable String adjustment) {
node.setLengthAdjust(adjustment);
}
@ReactProp(name = "alignmentBaseline")
public void setMethod(K node, @Nullable String alignment) {
node.setMethod(alignment);
}
@ReactProp(name = "baselineShift")
public void setBaselineShift(K node, Dynamic baselineShift) {
node.setBaselineShift(baselineShift);
}
@ReactProp(name = "verticalAlign")
public void setVerticalAlign(K node, @Nullable Dynamic verticalAlign) {
node.setVerticalAlign(verticalAlign);
}
@ReactProp(name = "rotate")
public void setRotate(K node, Dynamic rotate) {
node.setRotate(rotate);
}
@ReactProp(name = "dx")
public void setDx(K node, Dynamic deltaX) {
node.setDeltaX(deltaX);
}
@ReactProp(name = "dy")
public void setDy(K node, Dynamic deltaY) {
node.setDeltaY(deltaY);
}
@ReactProp(name = "x")
public void setX(K node, Dynamic positionX) {
node.setPositionX(positionX);
}
@ReactProp(name = "y")
public void setY(K node, Dynamic positionY) {
node.setPositionY(positionY);
}
@ReactProp(name = "font")
public void setFont(K node, Dynamic font) {
node.setFont(font);
}
public void setAlignmentBaseline(K view, @Nullable String value) {
view.setMethod(value);
}
}
static class TextViewManager extends TextViewManagerAbstract<TextView>
implements RNSVGTextManagerInterface<TextView> {
TextViewManager() {
super(SVGClass.RNSVGText);
mDelegate = new RNSVGTextManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGText";
TextViewManager(SVGClass svgClass) {
super(svgClass);
mDelegate = new RNSVGTextManagerDelegate(this);
}
}
static class TSpanViewManager extends TextViewManagerAbstract<TSpanView>
implements RNSVGTSpanManagerInterface<TSpanView> {
TSpanViewManager() {
super(SVGClass.RNSVGTSpan);
mDelegate = new RNSVGTSpanManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGTSpan";
TSpanViewManager(SVGClass svgClass) {
super(svgClass);
mDelegate = new RNSVGTSpanManagerDelegate(this);
}
@ReactProp(name = "content")
public void setContent(TSpanView node, @Nullable String content) {
node.setContent(content);
}
}
static class TextPathViewManager extends TextViewManagerAbstract<TextPathView>
implements RNSVGTextPathManagerInterface<TextPathView> {
TextPathViewManager() {
super(SVGClass.RNSVGTextPath);
mDelegate = new RNSVGTextPathManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGTextPath";
TextPathViewManager(SVGClass svgClass) {
super(svgClass);
mDelegate = new RNSVGTextPathManagerDelegate(this);
}
@ReactProp(name = "href")
public void setHref(TextPathView node, String href) {
node.setHref(href);
}
@ReactProp(name = "startOffset")
public void setStartOffset(TextPathView node, Dynamic startOffset) {
node.setStartOffset(startOffset);
}
@ReactProp(name = "method")
public void setMethod(TextPathView node, @Nullable String method) {
node.setMethod(method);
}
@Override
public void setMidLine(TextPathView view, @Nullable String value) {
view.setSharp(value);
}
@ReactProp(name = "spacing")
public void setSpacing(TextPathView node, @Nullable String spacing) {
node.setSpacing(spacing);
}
@ReactProp(name = "side")
public void setSide(TextPathView node, @Nullable String side) {
node.setSide(side);
}
@ReactProp(name = "midLine")
public void setSharp(TextPathView node, @Nullable String midLine) {
node.setSharp(midLine);
}
}
static class ImageViewManager extends RenderableViewManager<ImageView>
implements RNSVGImageManagerInterface<ImageView> {
ImageViewManager() {
super(SVGClass.RNSVGImage);
mDelegate = new RNSVGImageManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGImage";
@ReactProp(name = "x")
public void setX(ImageView node, Dynamic x) {
node.setX(x);
}
@ReactProp(name = "y")
public void setY(ImageView node, Dynamic y) {
node.setY(y);
}
@ReactProp(name = "width")
public void setWidth(ImageView node, Dynamic width) {
node.setWidth(width);
}
@ReactProp(name = "height")
public void setHeight(ImageView node, Dynamic height) {
node.setHeight(height);
}
@ReactProp(name = "src", customType = "ImageSource")
public void setSrc(ImageView node, @Nullable ReadableMap src) {
node.setSrc(src);
}
@ReactProp(name = "align")
public void setAlign(ImageView node, String align) {
node.setAlign(align);
}
@ReactProp(name = "meetOrSlice")
public void setMeetOrSlice(ImageView node, int meetOrSlice) {
node.setMeetOrSlice(meetOrSlice);
}
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
Map<String, Object> eventTypes = new HashMap<>();
eventTypes.put(SvgLoadEvent.EVENT_NAME, MapBuilder.of("registrationName", "onLoad"));
return eventTypes;
}
}
static class CircleViewManager extends RenderableViewManager<CircleView>
implements RNSVGCircleManagerInterface<CircleView> {
CircleViewManager() {
super(SVGClass.RNSVGCircle);
mDelegate = new RNSVGCircleManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGCircle";
@ReactProp(name = "cx")
public void setCx(CircleView node, Dynamic cx) {
node.setCx(cx);
}
@ReactProp(name = "cy")
public void setCy(CircleView node, Dynamic cy) {
node.setCy(cy);
}
@ReactProp(name = "r")
public void setR(CircleView node, Dynamic r) {
node.setR(r);
}
}
static class EllipseViewManager extends RenderableViewManager<EllipseView>
implements RNSVGEllipseManagerInterface<EllipseView> {
EllipseViewManager() {
super(SVGClass.RNSVGEllipse);
mDelegate = new RNSVGEllipseManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGEllipse";
@ReactProp(name = "cx")
public void setCx(EllipseView node, Dynamic cx) {
node.setCx(cx);
}
@ReactProp(name = "cy")
public void setCy(EllipseView node, Dynamic cy) {
node.setCy(cy);
}
@ReactProp(name = "rx")
public void setRx(EllipseView node, Dynamic rx) {
node.setRx(rx);
}
@ReactProp(name = "ry")
public void setRy(EllipseView node, Dynamic ry) {
node.setRy(ry);
}
}
static class LineViewManager extends RenderableViewManager<LineView>
implements RNSVGLineManagerInterface<LineView> {
LineViewManager() {
super(SVGClass.RNSVGLine);
mDelegate = new RNSVGLineManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGLine";
@ReactProp(name = "x1")
public void setX1(LineView node, Dynamic x1) {
node.setX1(x1);
}
@ReactProp(name = "y1")
public void setY1(LineView node, Dynamic y1) {
node.setY1(y1);
}
@ReactProp(name = "x2")
public void setX2(LineView node, Dynamic x2) {
node.setX2(x2);
}
@ReactProp(name = "y2")
public void setY2(LineView node, Dynamic y2) {
node.setY2(y2);
}
}
static class RectViewManager extends RenderableViewManager<RectView>
implements RNSVGRectManagerInterface<RectView> {
RectViewManager() {
super(SVGClass.RNSVGRect);
mDelegate = new RNSVGRectManagerDelegate(this);
}
public static final String REACT_CLASS = "RNSVGRect";
@ReactProp(name = "x")
public void setX(RectView node, Dynamic x) {
node.setX(x);
}
@ReactProp(name = "y")
public void setY(RectView node, Dynamic y) {
node.setY(y);
}
@ReactProp(name = "width")
public void setWidth(RectView node, Dynamic width) {
node.setWidth(width);
}
@ReactProp(name = "height")
public void setHeight(RectView node, Dynamic height) {
node.setHeight(height);
}
@ReactProp(name = "rx")
public void setRx(RectView node, Dynamic rx) {
node.setRx(rx);
}
@ReactProp(name = "ry")
public void setRy(RectView node, Dynamic ry) {
node.setRy(ry);
}
}
static class ClipPathViewManager extends GroupViewManagerAbstract<ClipPathView>