-
-
Notifications
You must be signed in to change notification settings - Fork 643
Expand file tree
/
Copy pathRNSSplitHostComponentView.mm
More file actions
454 lines (377 loc) · 16.9 KB
/
RNSSplitHostComponentView.mm
File metadata and controls
454 lines (377 loc) · 16.9 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
#import "RNSSplitHostComponentView.h"
#import <React/RCTAssert.h>
#import <React/RCTMountingTransactionObserving.h>
#import <React/UIView+React.h>
#import <react/renderer/components/rnscreens/ComponentDescriptors.h>
#import <react/renderer/components/rnscreens/RCTComponentViewHelpers.h>
#import "RNSConversions.h"
#import "RNSDefines.h"
#import "RNSSplitScreenComponentView.h"
#import "Swift-Bridging.h"
namespace react = facebook::react;
static const CGFloat epsilon = 1e-6;
#define COLUMN_METRIC_CHANGED(OLD, NEW, PROPERTY_NAME, EPSILON) \
(fabs((OLD).columnMetrics.PROPERTY_NAME - (NEW).columnMetrics.PROPERTY_NAME) > (EPSILON))
@interface RNSSplitHostComponentView () <RCTMountingTransactionObserving, RCTRNSSplitHostViewProtocol>
@end
@implementation RNSSplitHostComponentView {
RNSSplitHostComponentEventEmitter *_Nonnull _reactEventEmitter;
RNSSplitHostController *_Nonnull _controller;
NSMutableArray<RNSSplitScreenComponentView *> *_Nonnull _reactSubviews;
bool _hasModifiedReactSubviewsInCurrentTransaction;
bool _needsSplitAppearanceUpdate;
bool _needsSplitSecondaryScreenNavBarUpdate;
bool _needsSplitDisplayModeUpdate;
bool _needsSplitOrientationUpdate;
bool _needsSplitLayoutDirectionUpdate;
// We need this information to warn users about dynamic changes to behavior being currently unsupported.
bool _isShowSecondaryToggleButtonSet;
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self initState];
}
return self;
}
- (void)initState
{
[self resetProps];
_reactEventEmitter = [RNSSplitHostComponentEventEmitter new];
_hasModifiedReactSubviewsInCurrentTransaction = false;
_needsSplitAppearanceUpdate = false;
_needsSplitSecondaryScreenNavBarUpdate = false;
_needsSplitDisplayModeUpdate = false;
_needsSplitOrientationUpdate = false;
_needsSplitLayoutDirectionUpdate = false;
_reactSubviews = [NSMutableArray new];
}
- (void)resetProps
{
static const auto defaultProps = std::make_shared<const react::RNSSplitHostProps>();
_props = defaultProps;
_preferredSplitBehavior = UISplitViewControllerSplitBehaviorAutomatic;
_primaryEdge = UISplitViewControllerPrimaryEdgeLeading;
_preferredDisplayMode = UISplitViewControllerDisplayModeAutomatic;
_displayModeButtonVisibility = UISplitViewControllerDisplayModeButtonVisibilityAutomatic;
#if !TARGET_OS_TV
UISplitViewController *tempSplitVC = [[UISplitViewController alloc] init];
_primaryBackgroundStyle = tempSplitVC.primaryBackgroundStyle;
#endif // !TARGET_OS_TV
_presentsWithGesture = true;
_showSecondaryToggleButton = false;
_showInspector = false;
_minimumPrimaryColumnWidth = -1.0;
_maximumPrimaryColumnWidth = -1.0;
_preferredPrimaryColumnWidthOrFraction = -1.0;
_minimumSupplementaryColumnWidth = -1.0;
_maximumSupplementaryColumnWidth = -1.0;
_preferredSupplementaryColumnWidthOrFraction = -1.0;
#if RNS_IPHONE_OS_VERSION_AVAILABLE(26_0)
_minimumSecondaryColumnWidth = -1.0;
_preferredSecondaryColumnWidthOrFraction = -1.0;
_minimumInspectorColumnWidth = -1.0;
_maximumInspectorColumnWidth = -1.0;
_preferredInspectorColumnWidthOrFraction = -1.0;
#endif // RNS_IPHONE_OS_VERSION_AVAILABLE(26_0)
_hasCustomTopColumnForCollapsing = NO;
_topColumnForCollapsingColumn = UISplitViewControllerColumnPrimary;
_orientation = RNSOrientationInherit;
_layoutDirection = UITraitEnvironmentLayoutDirectionUnspecified;
_isShowSecondaryToggleButtonSet = false;
}
- (int)getNumberOfColumns
{
int numberOfColumns = 0;
for (RNSSplitScreenComponentView *component in _reactSubviews) {
if (component.columnType == RNSSplitScreenColumnTypeColumn) {
numberOfColumns++;
}
}
return numberOfColumns;
}
- (void)setupController
{
// Controller needs to know about the number of reactSubviews before its initialization to pass proper number of
// columns to the constructor. Therefore, we must delay it's creation until attaching it to window.
// At this point, children are already attached to the Host component, therefore, we may create SplitView controller.
if (_controller == nil) {
int numberOfColumns = [self getNumberOfColumns];
_controller = [[RNSSplitHostController alloc] initWithSplitHostComponentView:self numberOfColumns:numberOfColumns];
}
}
- (void)didMoveToWindow
{
[self setupController];
RCTAssert(_controller != nil, @"[RNScreens] Controller must not be nil while attaching to window");
[self requestSplitHostControllerForAppearanceUpdate];
[self reactAddControllerToClosestParent:_controller];
}
- (void)reactAddControllerToClosestParent:(UIViewController *)controller
{
if (!controller.parentViewController) {
UIView *parentView = (UIView *)self.reactSuperview;
while (parentView) {
if (parentView.reactViewController) {
[parentView.reactViewController addChildViewController:controller];
[self addSubview:controller.view];
[controller didMoveToParentViewController:parentView.reactViewController];
break;
}
parentView = (UIView *)parentView.reactSuperview;
}
return;
}
}
RNS_IGNORE_SUPER_CALL_BEGIN
- (nonnull NSMutableArray<RNSSplitScreenComponentView *> *)reactSubviews
{
RCTAssert(
_reactSubviews != nil,
@"[RNScreens] Attempt to work with non-initialized list of RNSSplitScreenComponentView subviews. (for: %@)",
self);
return _reactSubviews;
}
RNS_IGNORE_SUPER_CALL_END
- (nonnull RNSSplitHostController *)splitHostController
{
RCTAssert(_controller != nil, @"[RNScreens] Controller must not be nil");
return _controller;
}
#pragma mark - RCTComponentViewProtocol
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
RCTAssert(
[childComponentView isKindOfClass:RNSSplitScreenComponentView.class],
@"[RNScreens] Attempt to mount child of unsupported type: %@, expected %@",
childComponentView.class,
RNSSplitScreenComponentView.class);
auto *childScreen = static_cast<RNSSplitScreenComponentView *>(childComponentView);
childScreen.splitHost = self;
[_reactSubviews insertObject:childScreen atIndex:index];
_hasModifiedReactSubviewsInCurrentTransaction = true;
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
RCTAssert(
[childComponentView isKindOfClass:RNSSplitScreenComponentView.class],
@"[RNScreens] Attempt to unmount child of unsupported type: %@, expected %@",
childComponentView.class,
RNSSplitScreenComponentView.class);
auto *childScreen = static_cast<RNSSplitScreenComponentView *>(childComponentView);
childScreen.splitHost = nil;
[_reactSubviews removeObject:childScreen];
_hasModifiedReactSubviewsInCurrentTransaction = true;
}
+ (react::ComponentDescriptorProvider)componentDescriptorProvider
{
return react::concreteComponentDescriptorProvider<react::RNSSplitHostComponentDescriptor>();
}
+ (BOOL)shouldBeRecycled
{
// There won't be tens of instances of this component usually & it's easier for now.
// We could consider enabling it someday though.
return NO;
}
- (void)updateProps:(const facebook::react::Props::Shared &)props
oldProps:(const facebook::react::Props::Shared &)oldProps
{
const auto &oldComponentProps = *std::static_pointer_cast<const react::RNSSplitHostProps>(_props);
const auto &newComponentProps = *std::static_pointer_cast<const react::RNSSplitHostProps>(props);
if (oldComponentProps.preferredSplitBehavior != newComponentProps.preferredSplitBehavior) {
_needsSplitAppearanceUpdate = true;
_preferredSplitBehavior =
rnscreens::conversion::SplitViewPreferredSplitBehaviorFromHostProp(newComponentProps.preferredSplitBehavior);
}
if (oldComponentProps.primaryEdge != newComponentProps.primaryEdge) {
_needsSplitAppearanceUpdate = true;
_primaryEdge = rnscreens::conversion::SplitViewPrimaryEdgeFromHostProp(newComponentProps.primaryEdge);
}
if (oldComponentProps.preferredDisplayMode != newComponentProps.preferredDisplayMode) {
_needsSplitAppearanceUpdate = true;
_needsSplitDisplayModeUpdate = true;
_preferredDisplayMode =
rnscreens::conversion::SplitViewPreferredDisplayModeFromHostProp(newComponentProps.preferredDisplayMode);
}
#if !TARGET_OS_TV
if (oldComponentProps.primaryBackgroundStyle != newComponentProps.primaryBackgroundStyle) {
_needsSplitAppearanceUpdate = true;
_primaryBackgroundStyle =
rnscreens::conversion::SplitViewPrimaryBackgroundStyleFromHostProp(newComponentProps.primaryBackgroundStyle);
}
#endif // !TARGET_OS_TV
if (oldComponentProps.presentsWithGesture != newComponentProps.presentsWithGesture) {
_needsSplitAppearanceUpdate = true;
_presentsWithGesture = newComponentProps.presentsWithGesture;
}
if (oldComponentProps.showSecondaryToggleButton != newComponentProps.showSecondaryToggleButton) {
_needsSplitAppearanceUpdate = true;
_needsSplitSecondaryScreenNavBarUpdate = true;
_showSecondaryToggleButton = newComponentProps.showSecondaryToggleButton;
}
if (oldComponentProps.showInspector != newComponentProps.showInspector) {
_needsSplitAppearanceUpdate = true;
_showInspector = newComponentProps.showInspector;
}
if (oldComponentProps.displayModeButtonVisibility != newComponentProps.displayModeButtonVisibility) {
_needsSplitAppearanceUpdate = true;
_displayModeButtonVisibility = rnscreens::conversion::SplitViewDisplayModeButtonVisibilityFromHostProp(
newComponentProps.displayModeButtonVisibility);
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, minimumPrimaryColumnWidth, epsilon)) {
_needsSplitAppearanceUpdate = true;
_minimumPrimaryColumnWidth = newComponentProps.columnMetrics.minimumPrimaryColumnWidth;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, maximumPrimaryColumnWidth, epsilon)) {
_needsSplitAppearanceUpdate = true;
_maximumPrimaryColumnWidth = newComponentProps.columnMetrics.maximumPrimaryColumnWidth;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, preferredPrimaryColumnWidthOrFraction, epsilon)) {
_needsSplitAppearanceUpdate = true;
_preferredPrimaryColumnWidthOrFraction = newComponentProps.columnMetrics.preferredPrimaryColumnWidthOrFraction;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, minimumSupplementaryColumnWidth, epsilon)) {
_needsSplitAppearanceUpdate = true;
_minimumSupplementaryColumnWidth = newComponentProps.columnMetrics.minimumSupplementaryColumnWidth;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, maximumSupplementaryColumnWidth, epsilon)) {
_needsSplitAppearanceUpdate = true;
_maximumSupplementaryColumnWidth = newComponentProps.columnMetrics.maximumSupplementaryColumnWidth;
}
if (COLUMN_METRIC_CHANGED(
oldComponentProps, newComponentProps, preferredSupplementaryColumnWidthOrFraction, epsilon)) {
_needsSplitAppearanceUpdate = true;
_preferredSupplementaryColumnWidthOrFraction =
newComponentProps.columnMetrics.preferredSupplementaryColumnWidthOrFraction;
}
#if RNS_IPHONE_OS_VERSION_AVAILABLE(26_0)
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, minimumSecondaryColumnWidth, epsilon)) {
_needsSplitAppearanceUpdate = true;
_minimumSecondaryColumnWidth = newComponentProps.columnMetrics.minimumSecondaryColumnWidth;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, preferredSecondaryColumnWidthOrFraction, epsilon)) {
_needsSplitAppearanceUpdate = true;
_preferredSecondaryColumnWidthOrFraction = newComponentProps.columnMetrics.preferredSecondaryColumnWidthOrFraction;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, minimumInspectorColumnWidth, epsilon)) {
_needsSplitAppearanceUpdate = true;
_minimumInspectorColumnWidth = newComponentProps.columnMetrics.minimumInspectorColumnWidth;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, maximumInspectorColumnWidth, epsilon)) {
_needsSplitAppearanceUpdate = true;
_maximumInspectorColumnWidth = newComponentProps.columnMetrics.maximumInspectorColumnWidth;
}
if (COLUMN_METRIC_CHANGED(oldComponentProps, newComponentProps, preferredInspectorColumnWidthOrFraction, epsilon)) {
_needsSplitAppearanceUpdate = true;
_preferredInspectorColumnWidthOrFraction = newComponentProps.columnMetrics.preferredInspectorColumnWidthOrFraction;
}
#endif // RNS_IPHONE_OS_VERSION_AVAILABLE(26_0)
// No appearance update needed — topColumnForCollapsingColumn is read on-demand by the delegate callback
// splitViewController(_:topColumnForCollapsingToProposedTopColumn:), not pushed to the controller.
if (oldComponentProps.topColumnForCollapsing != newComponentProps.topColumnForCollapsing) {
auto column =
rnscreens::conversion::SplitViewTopColumnForCollapsingFromHostProp(newComponentProps.topColumnForCollapsing);
_hasCustomTopColumnForCollapsing = column.has_value();
_topColumnForCollapsingColumn = column.value_or(UISplitViewControllerColumnPrimary);
}
if (oldComponentProps.orientation != newComponentProps.orientation) {
_needsSplitOrientationUpdate = true;
_orientation = rnscreens::conversion::RNSOrientationFromRNSSplitHostOrientation(newComponentProps.orientation);
}
if (oldComponentProps.layoutDirection != newComponentProps.layoutDirection) {
_needsSplitLayoutDirectionUpdate = true;
_layoutDirection = rnscreens::conversion::UITraitEnvironmentLayoutDirectionFromSplitHostCppEquivalent(
newComponentProps.layoutDirection);
}
// This flag is set to true when showsSecondaryOnlyButton prop is assigned for the first time.
// This allows us to identify any subsequent changes to this prop,
// enabling us to warn users that dynamic changes are not supported.
_isShowSecondaryToggleButtonSet = true;
[super updateProps:props oldProps:oldProps];
}
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
[self requestSplitHostControllerForAppearanceUpdate];
[super finalizeUpdates:updateMask];
}
- (void)requestSplitHostControllerForAppearanceUpdate
{
if (_needsSplitAppearanceUpdate && _controller != nil) {
_needsSplitAppearanceUpdate = false;
[_controller setNeedsAppearanceUpdate];
}
if (_needsSplitDisplayModeUpdate && _controller != nil) {
_needsSplitDisplayModeUpdate = false;
[_controller setNeedsDisplayModeUpdate];
}
if (_needsSplitSecondaryScreenNavBarUpdate && _controller != nil) {
_needsSplitSecondaryScreenNavBarUpdate = false;
[_controller setNeedsSecondaryScreenNavBarUpdate];
}
if (_needsSplitOrientationUpdate && _controller != nil) {
_needsSplitOrientationUpdate = false;
[_controller setNeedsOrientationUpdate];
}
if (_needsSplitLayoutDirectionUpdate && _controller != nil) {
_needsSplitLayoutDirectionUpdate = false;
#if RNS_IPHONE_OS_VERSION_AVAILABLE(17_0)
if (@available(iOS 17.0, *)) {
_controller.traitOverrides.layoutDirection = _layoutDirection;
} else
#endif // RNS_IPHONE_OS_VERSION_AVAILABLE(17_0)
[_controller setNeedsLayoutDirectionUpdateBelowIOS17];
}
}
#pragma mark - RCTMountingTransactionObserving
- (void)mountingTransactionWillMount:(const facebook::react::MountingTransaction &)transaction
withSurfaceTelemetry:(const facebook::react::SurfaceTelemetry &)surfaceTelemetry
{
_hasModifiedReactSubviewsInCurrentTransaction = false;
[_controller reactMountingTransactionWillMount];
}
- (void)mountingTransactionDidMount:(const facebook::react::MountingTransaction &)transaction
withSurfaceTelemetry:(const facebook::react::SurfaceTelemetry &)surfaceTelemetry
{
if (_hasModifiedReactSubviewsInCurrentTransaction) {
[_controller setNeedsUpdateOfChildViewControllers];
}
[_controller reactMountingTransactionDidMount];
}
- (void)updateEventEmitter:(const facebook::react::EventEmitter::Shared &)eventEmitter
{
[super updateEventEmitter:eventEmitter];
[_reactEventEmitter updateEventEmitter:std::static_pointer_cast<const react::RNSSplitHostEventEmitter>(eventEmitter)];
}
#pragma mark - Commands
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
{
RCTRNSSplitHostHandleCommand(self, commandName, args);
}
- (void)showColumn:(NSString *)column
{
if (_controller == nil) {
RCTLogWarn(@"[RNScreens] showColumn called before SplitView controller was initialized. Command ignored.");
return;
}
[_controller showColumnNamed:column];
}
#pragma mark - Events
- (nonnull RNSSplitHostComponentEventEmitter *)reactEventEmitter
{
RCTAssert(_reactEventEmitter != nil, @"[RNScreens] Attempt to access uninitialized _reactEventEmitter");
return _reactEventEmitter;
}
#pragma mark - Dynamic frameworks support
// Needed because of this: https://github.com/facebook/react-native/pull/37274
#ifdef RCT_DYNAMIC_FRAMEWORKS
+ (void)load
{
[super load];
}
#endif // RCT_DYNAMIC_FRAMEWORKS
@end
Class<RCTComponentViewProtocol> RNSSplitHostCls(void)
{
return RNSSplitHostComponentView.class;
}
#undef COLUMN_METRIC_CHANGED