-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
MyNativeView.js
289 lines (273 loc) · 8.93 KB
/
MyNativeView.js
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
import type {MyLegacyViewType} from './MyLegacyViewNativeComponent';
import type {MyNativeViewType} from './MyNativeViewNativeComponent';
import RNTMyLegacyNativeView from './MyLegacyViewNativeComponent';
import {
callNativeMethodToAddOverlays,
callNativeMethodToChangeBackgroundColor,
callNativeMethodToRemoveOverlays,
} from './MyLegacyViewNativeComponent';
import RNTMyNativeView, {
Commands as RNTMyNativeViewCommands,
} from './MyNativeViewNativeComponent';
import * as React from 'react';
import {useRef, useState} from 'react';
import {Button, Platform, Text, UIManager, View} from 'react-native';
const colors = [
'#0000FF',
'#FF0000',
'#00FF00',
'#003300',
'#330000',
'#000033',
];
const cornerRadiuses = [0, 20, 40, 60, 80, 100, 120];
class HSBA {
hue: number;
saturation: number;
brightness: number;
alpha: number;
constructor(
hue: number = 0.0,
saturation: number = 0.0,
brightness: number = 0.0,
alpha: number = 0.0,
) {
this.hue = hue;
this.saturation = saturation;
this.brightness = brightness;
this.alpha = alpha;
}
toString(): string {
return `h: ${this.hue}, s: ${this.saturation}, b: ${this.brightness}, a: ${this.alpha}`;
}
}
function beautify(number: number): string {
if (number % 1 === 0) {
return number.toFixed();
}
return number.toFixed(2);
}
type MeasureStruct = {
x: number,
y: number,
width: number,
height: number,
};
const MeasureStructZero: MeasureStruct = {
x: 0,
y: 0,
width: 0,
height: 0,
};
function getTextFor(measureStruct: MeasureStruct): string {
return `x: ${beautify(measureStruct.x)}, y: ${beautify(
measureStruct.y,
)}, width: ${beautify(measureStruct.width)}, height: ${beautify(
measureStruct.height,
)}`;
}
const opacityDecrementCounter = 0.2;
function computeNextOpacity(opacity: number): number {
if (parseFloat(opacity.toFixed(1)) > 0.0) {
return opacity - opacityDecrementCounter;
}
return 1.0;
}
// This is an example component that migrates to use the new architecture.
export default function MyNativeView(props: {}): React.Node {
const containerRef = useRef<React.ElementRef<typeof View> | null>(null);
const ref = useRef<React.ElementRef<MyNativeViewType> | null>(null);
const legacyRef = useRef<React.ElementRef<MyLegacyViewType> | null>(null);
const [currentBGColor, setCurrentBGColor] = useState<number>(0);
const [opacity, setOpacity] = useState(1.0);
const [arrayValues, setArrayValues] = useState([1, 2, 3]);
const [hsba, setHsba] = useState<HSBA>(new HSBA());
const [cornerRadiusIndex, setCornerRadiusIndex] = useState<number>(0);
const [legacyMeasure, setLegacyMeasure] =
useState<MeasureStruct>(MeasureStructZero);
const [legacyMeasureInWindow, setLegacyMeasureInWindow] =
useState<MeasureStruct>(MeasureStructZero);
const [legacyMeasureLayout, setLegacyMeasureLayout] =
useState<MeasureStruct>(MeasureStructZero);
const [legacyStyleEventCount, setLegacyStyleEventCount] = useState<number>(0);
return (
<View ref={containerRef} style={{flex: 1}}>
<Text style={{color: 'red'}}>Fabric View</Text>
<RNTMyNativeView
ref={ref}
style={{flex: 1}}
opacity={opacity}
values={arrayValues}
onIntArrayChanged={event => {
console.log(event.nativeEvent.values);
console.log(event.nativeEvent.boolValues);
console.log(event.nativeEvent.floats);
console.log(event.nativeEvent.doubles);
console.log(event.nativeEvent.yesNos);
console.log(event.nativeEvent.strings);
console.log(event.nativeEvent.latLons);
console.log(event.nativeEvent.multiArrays);
}}
onLegacyStyleEvent={event => {
setLegacyStyleEventCount(prevCount => prevCount + 1);
console.log(event.nativeEvent.string);
}}
/>
<Text style={{color: 'red'}}>Legacy View</Text>
<RNTMyLegacyNativeView
ref={legacyRef}
style={{flex: 1}}
opacity={opacity}
onColorChanged={event => {
const normalizedHue =
Platform.OS === 'android'
? event.nativeEvent.backgroundColor.hue
: event.nativeEvent.backgroundColor.hue * 360;
const normalizedAlpha =
Platform.OS === 'android'
? event.nativeEvent.backgroundColor.alpha
: event.nativeEvent.backgroundColor.alpha * 255;
setHsba(
new HSBA(
normalizedHue,
event.nativeEvent.backgroundColor.saturation,
event.nativeEvent.backgroundColor.brightness,
normalizedAlpha,
),
);
}}
/>
<Text style={{color: 'green', textAlign: 'center'}}>
HSBA: {hsba.toString()}
</Text>
<Text style={{color: 'green', textAlign: 'center'}}>
Constants From Interop Layer:{' '}
{UIManager.getViewManagerConfig('RNTMyLegacyNativeView').Constants.PI}
</Text>
<Text style={{color: 'green', textAlign: 'center'}}>
Opacity: {opacity.toFixed(1)}
</Text>
<Button
title="Change Background"
onPress={() => {
let nextBGColor =
currentBGColor + 1 >= colors.length ? 0 : currentBGColor + 1;
let newColor = colors[nextBGColor];
RNTMyNativeViewCommands.callNativeMethodToChangeBackgroundColor(
// $FlowFixMe[incompatible-call]
ref.current,
newColor,
);
callNativeMethodToChangeBackgroundColor(legacyRef.current, newColor);
setCurrentBGColor(nextBGColor);
}}
/>
<Button
title="Add Overlays"
onPress={() => {
let randomColorId = Math.floor(Math.random() * 5);
let overlayColors = [
colors[randomColorId],
colors[(randomColorId + 1) % 5],
];
RNTMyNativeViewCommands.callNativeMethodToAddOverlays(
// $FlowFixMe[incompatible-call]
ref.current,
overlayColors,
);
callNativeMethodToAddOverlays(legacyRef.current, overlayColors);
}}
/>
<Button
title="Remove Overlays"
onPress={() => {
RNTMyNativeViewCommands.callNativeMethodToRemoveOverlays(
// $FlowFixMe[incompatible-call]
ref.current,
);
callNativeMethodToRemoveOverlays(legacyRef.current);
}}
/>
<Button
title="Set Opacity"
onPress={() => {
setOpacity(computeNextOpacity(opacity));
setArrayValues([
Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100),
Math.floor(Math.random() * 100),
]);
}}
/>
<Button
title="Console.log Measure"
onPress={() => {
ref.current?.measure((x, y, width, height) => {
console.log(x, y, width, height);
});
legacyRef.current?.measure((x, y, width, height) => {
setLegacyMeasure({x, y, width, height});
});
legacyRef.current?.measureInWindow((x, y, width, height) => {
setLegacyMeasureInWindow({x, y, width, height});
});
if (containerRef.current) {
legacyRef.current?.measureLayout(
// $FlowFixMe[incompatible-call]
containerRef.current,
(x, y, width, height) => {
setLegacyMeasureLayout({x, y, width, height});
},
);
}
}}
/>
<Text style={{color: 'green', textAlign: 'center'}}>
> Interop Layer Measurements <
</Text>
<Text style={{color: 'green', textAlign: 'center'}}>
measure {getTextFor(legacyMeasure)}
</Text>
<Text style={{color: 'green', textAlign: 'center'}}>
InWindow {getTextFor(legacyMeasureInWindow)}
</Text>
<Text style={{color: 'green', textAlign: 'center'}}>
InLayout {getTextFor(legacyMeasureLayout)}
</Text>
<Button
title="Fire Legacy Style Event"
onPress={() => {
RNTMyNativeViewCommands.fireLagacyStyleEvent(
// $FlowFixMe[incompatible-call]
ref.current,
);
}}
/>
<Text style={{color: 'green', textAlign: 'center'}}>
Legacy Style Event Fired {legacyStyleEventCount} times
</Text>
<Button
title="Test setNativeProps"
onPress={() => {
const newCRIndex =
cornerRadiusIndex + 1 >= cornerRadiuses.length
? 0
: cornerRadiusIndex + 1;
setCornerRadiusIndex(newCRIndex);
legacyRef.current?.setNativeProps({
cornerRadius: cornerRadiuses[newCRIndex],
});
}}
/>
</View>
);
}