@ng-native/components 0.22.1 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/android/build.gradle +22 -0
  2. package/android/src/main/java/dev/ngnative/components/ContextMenuAccessibility.kt +71 -0
  3. package/android/src/main/java/dev/ngnative/components/ContextMenuGesture.kt +161 -0
  4. package/android/src/main/java/dev/ngnative/components/ContextMenuPopup.kt +197 -0
  5. package/android/src/main/java/dev/ngnative/components/MenuHost.kt +45 -0
  6. package/android/src/main/java/dev/ngnative/components/MenuWindowCallback.kt +76 -0
  7. package/android/src/main/java/dev/ngnative/components/MenusModule.kt +85 -0
  8. package/android/src/main/java/dev/ngnative/components/Shortcut.kt +144 -0
  9. package/android/src/main/java/dev/ngnative/components/WindowWrapper.kt +22 -0
  10. package/dist/activity-indicator.d.ts +4 -1
  11. package/dist/activity-indicator.js +6 -2
  12. package/dist/activity-indicator.js.map +1 -1
  13. package/dist/context-menu.d.ts +52 -0
  14. package/dist/context-menu.js +101 -0
  15. package/dist/context-menu.js.map +1 -0
  16. package/dist/drag-and-drop.d.ts +52 -0
  17. package/dist/drag-and-drop.js +112 -0
  18. package/dist/drag-and-drop.js.map +1 -0
  19. package/dist/gesture-backend.d.ts +32 -2
  20. package/dist/gesture-backend.js.map +1 -1
  21. package/dist/gesture-dispatch.d.ts +8 -1
  22. package/dist/gesture-dispatch.js +60 -16
  23. package/dist/gesture-dispatch.js.map +1 -1
  24. package/dist/gesture-interaction.d.ts +13 -0
  25. package/dist/gesture-interaction.js +97 -0
  26. package/dist/gesture-interaction.js.map +1 -0
  27. package/dist/gesture-tracking.d.ts +18 -0
  28. package/dist/gesture-tracking.js +28 -0
  29. package/dist/gesture-tracking.js.map +1 -0
  30. package/dist/gestures-web.d.ts +3 -1
  31. package/dist/gestures-web.js +5 -2
  32. package/dist/gestures-web.js.map +1 -1
  33. package/dist/gestures.d.ts +1 -0
  34. package/dist/gestures.js +45 -3
  35. package/dist/gestures.js.map +1 -1
  36. package/dist/image.d.ts +1 -1
  37. package/dist/index.d.ts +12 -1
  38. package/dist/index.js +12 -1
  39. package/dist/index.js.map +1 -1
  40. package/dist/interaction-observer.d.ts +11 -0
  41. package/dist/interaction-observer.js +12 -0
  42. package/dist/interaction-observer.js.map +1 -0
  43. package/dist/keyboard-avoiding-view.js +3 -3
  44. package/dist/keyboard-avoiding-view.js.map +1 -1
  45. package/dist/markdown-stream.d.ts +3 -1
  46. package/dist/markdown-stream.js +14 -2
  47. package/dist/markdown-stream.js.map +1 -1
  48. package/dist/markdown.d.ts +2 -0
  49. package/dist/markdown.js +5 -3
  50. package/dist/markdown.js.map +1 -1
  51. package/dist/menu.d.ts +71 -0
  52. package/dist/menu.js +193 -0
  53. package/dist/menu.js.map +1 -0
  54. package/dist/modal.d.ts +30 -1
  55. package/dist/modal.js +67 -6
  56. package/dist/modal.js.map +1 -1
  57. package/dist/native-button.d.ts +54 -0
  58. package/dist/native-button.js +100 -0
  59. package/dist/native-button.js.map +1 -0
  60. package/dist/native-gesture.d.ts +9 -1
  61. package/dist/native-gesture.js +32 -3
  62. package/dist/native-gesture.js.map +1 -1
  63. package/dist/native-id.d.ts +10 -0
  64. package/dist/native-id.js +70 -0
  65. package/dist/native-id.js.map +1 -0
  66. package/dist/pressable.d.ts +31 -0
  67. package/dist/pressable.js +102 -9
  68. package/dist/pressable.js.map +1 -1
  69. package/dist/safe-area-provider.js +15 -8
  70. package/dist/safe-area-provider.js.map +1 -1
  71. package/dist/scroll-view-props.d.ts +7 -1
  72. package/dist/scroll-view-props.js +54 -3
  73. package/dist/scroll-view-props.js.map +1 -1
  74. package/dist/section-list.js +2 -2
  75. package/dist/section-list.js.map +1 -1
  76. package/dist/shortcut.d.ts +21 -0
  77. package/dist/shortcut.js +34 -0
  78. package/dist/shortcut.js.map +1 -0
  79. package/dist/sidebar-item.d.ts +53 -0
  80. package/dist/sidebar-item.js +152 -0
  81. package/dist/sidebar-item.js.map +1 -0
  82. package/dist/sidebar-section.d.ts +21 -0
  83. package/dist/sidebar-section.js +74 -0
  84. package/dist/sidebar-section.js.map +1 -0
  85. package/dist/sidebar.d.ts +47 -0
  86. package/dist/sidebar.js +110 -0
  87. package/dist/sidebar.js.map +1 -0
  88. package/dist/split-view.d.ts +63 -0
  89. package/dist/split-view.js +121 -0
  90. package/dist/split-view.js.map +1 -0
  91. package/dist/switch.d.ts +9 -1
  92. package/dist/switch.js +23 -2
  93. package/dist/switch.js.map +1 -1
  94. package/dist/text-input.d.ts +8 -3
  95. package/dist/text-input.js +10 -4
  96. package/dist/text-input.js.map +1 -1
  97. package/dist/transforms.d.ts +7 -0
  98. package/dist/transforms.js +12 -3
  99. package/dist/transforms.js.map +1 -1
  100. package/dist/tray-icon.d.ts +18 -0
  101. package/dist/tray-icon.js +46 -0
  102. package/dist/tray-icon.js.map +1 -0
  103. package/dist/view-base.d.ts +42 -1
  104. package/dist/view-base.js +67 -39
  105. package/dist/view-base.js.map +1 -1
  106. package/dist/virtual-list.js +12 -1
  107. package/dist/virtual-list.js.map +1 -1
  108. package/dist/written-props.js +25 -0
  109. package/dist/written-props.js.map +1 -1
  110. package/expo-module.config.json +6 -0
  111. package/ios/NgNativeButtonComponentView.mm +132 -0
  112. package/ios/NgNativeButtonMetrics.h +28 -0
  113. package/ios/NgNativeButtonMetrics.mm +68 -0
  114. package/ios/NgNativeButtonShadowNode.h +65 -0
  115. package/ios/NgNativeButtonShadowNode.mm +40 -0
  116. package/ios/NgNativeComponents.podspec +28 -0
  117. package/ios/NgNativeDragAndDrop.mm +342 -0
  118. package/ios/NgNativeDropItems.h +29 -0
  119. package/ios/NgNativeDropItems.mm +55 -0
  120. package/ios/NgNativeMenuBuilding.h +40 -0
  121. package/ios/NgNativeMenuBuilding.mm +217 -0
  122. package/ios/NgNativeMenus.mm +320 -0
  123. package/ios/NgNativeResponderHooks.h +53 -0
  124. package/ios/NgNativeResponderHooks.mm +215 -0
  125. package/ios/NgNativeSceneKeys.h +36 -0
  126. package/ios/NgNativeSceneKeys.mm +56 -0
  127. package/ios/NgNativeSplitView.mm +433 -0
  128. package/ios/NgNativeSplitViewDelegate.h +29 -0
  129. package/ios/NgNativeSplitViewDelegate.mm +122 -0
  130. package/ios/NgNativeSplitViewShadowNodes.h +115 -0
  131. package/ios/NgNativeSplitViewShadowNodes.mm +31 -0
  132. package/ios/NgNativeUndo.mm +107 -0
  133. package/ios/NgNativeUndoSteps.h +17 -0
  134. package/ios/NgNativeUndoSteps.mm +22 -0
  135. package/ios/NgNativeViewBinder.h +54 -0
  136. package/ios/NgNativeViewBinder.mm +225 -0
  137. package/ios/NgNativeWindows.mm +1126 -0
  138. package/package.json +13 -5
@@ -0,0 +1,1126 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTInvalidating.h>
3
+ #import <React/RCTSurfaceHostingProxyRootView.h>
4
+ #import <React/RCTSurfaceProtocol.h>
5
+ #import <UIKit/UIKit.h>
6
+ #import <objc/runtime.h>
7
+
8
+ #import "NgNativeResponderHooks.h"
9
+ #import "NgNativeSceneKeys.h"
10
+
11
+ /**
12
+ The `NgNativeWindows` module on an iPad (`NativeWindows` in @ng-native/device): each of the app's
13
+ windows is a UIWindowScene with a surface of its own, on React Native's one host, so every window
14
+ shares the one runtime and the one Angular app.
15
+
16
+ - `multipleWindows` is true on an iPad whose Info.plist supports several scenes (the config
17
+ plugin's `ios.multipleWindows`). Elsewhere it is false, and JavaScript leaves the module alone.
18
+ - The scene the app launched in shows the surface React Native started with, which the app keeps
19
+ for itself and never renders on. The first window opened takes that scene, with a surface of
20
+ its own; each later one asks iPadOS for a new scene through `requestSceneSessionActivation`,
21
+ with the `dev.ngnative.window` activity, which the app delegate's configuration hook gives this
22
+ file's scene delegate. `open` resolves the surface's id once the surface runs.
23
+ - `close` stops the surface and destroys the scene's session, `focus` activates it, and
24
+ `setTitle` names the scene in the app switcher and the window's title.
25
+ - iPadOS asks no app before it closes a window, so there is no `closeRequest`: a scene the user
26
+ closes sends `closed` once iPadOS discards its session. A scene iPadOS only disconnects, to
27
+ reclaim memory in the background, keeps its window and surface, and shows them again when it
28
+ reconnects. iOS ends an app itself, so `quit` and `answerQuit` do nothing and no `quitRequest`
29
+ is ever sent.
30
+ - Events on `ngNativeWindows`: `focus` as a scene becomes active, `resize` as a window's size
31
+ changes, `closed`, `newWindow` for a scene the user made (New Window, or the app opened again
32
+ with none), and `reopen` for a window's scene that connects with no `open` waiting for it (one
33
+ asked for before a reload). Each waits five seconds for the app to open a window in it. The app
34
+ answers `newWindow` with its main window, and where that is open already it brings it to the
35
+ front with `focus`, which closes the scene the user made.
36
+ - A reload keeps the windows with a key for the reloaded app to open again, for five seconds from
37
+ the latest reload, and closes the rest, as the macOS host does. An `open` still waiting for its
38
+ scene resolves null.
39
+ - A relaunch: iPadOS keeps the scene sessions and restores the one in front, and the app opens its
40
+ windows again by key (`AppWindows` keeps them). The key of the window each session had is kept
41
+ in the app's user defaults, so `open` with a key takes the scene kept for it: the restored one,
42
+ or a hidden one, where the window shows once iPadOS connects that scene. The scene the app
43
+ launched in waits five seconds for its own window. A scene connecting after the app has
44
+ launched comes here even when it was made for the app's scene delegate, so React Native never
45
+ starts a second time.
46
+ */
47
+
48
+ static NSString *const WindowActivity = @"dev.ngnative.window";
49
+ static NSString *const WindowRunnable = @"NgNativeWindow";
50
+ // In the app's user defaults: the key of the window each scene session had, by its identifier.
51
+ static NSString *const KeptKeys = @"dev.ngnative.windowKeys";
52
+ static const NSTimeInterval ClaimSeconds = 5;
53
+
54
+ /** What JavaScript is handed for React Native's host: no header of its own is needed. */
55
+ @protocol NgNativeReactHost <NSObject>
56
+ - (id<RCTSurfaceProtocol>)createSurfaceWithModuleName:(NSString *)moduleName initialProperties:(NSDictionary *)properties;
57
+ @end
58
+
59
+ static __weak id<NgNativeReactHost> reactHost;
60
+
61
+ /** A window's root view: its surface, which says when it first runs and when its size changes. */
62
+ @interface NgNativeWindowRootView : RCTSurfaceHostingProxyRootView
63
+ @property (nonatomic, copy, nullable) void (^onRunning)(void);
64
+ @property (nonatomic, copy, nullable) void (^onResize)(CGSize size, CGFloat scale);
65
+ - (void)ngn_reportSizeAgain;
66
+ @end
67
+
68
+ @implementation NgNativeWindowRootView {
69
+ CGSize _reported;
70
+ // Sizes are sent once the surface runs, when the app has the window: one sooner goes unheard.
71
+ BOOL _ran;
72
+ }
73
+
74
+ - (void)surface:(id<RCTSurfaceProtocol>)surface didChangeStage:(RCTSurfaceStage)stage
75
+ {
76
+ [super surface:surface didChangeStage:stage];
77
+ if (!RCTSurfaceStageIsRunning(stage)) {
78
+ return;
79
+ }
80
+ dispatch_async(dispatch_get_main_queue(), ^{
81
+ [self ngn_ran];
82
+ });
83
+ }
84
+
85
+ - (void)ngn_ran
86
+ {
87
+ void (^running)(void) = self.onRunning;
88
+ self.onRunning = nil;
89
+ _ran = YES;
90
+ if (running) {
91
+ running();
92
+ }
93
+ [self ngn_reportSize];
94
+ }
95
+
96
+ - (void)layoutSubviews
97
+ {
98
+ [super layoutSubviews];
99
+ [self ngn_reportSize];
100
+ }
101
+
102
+ /** Send the size again, though it has not changed: a new mount on the surface has not heard it. */
103
+ - (void)ngn_reportSizeAgain
104
+ {
105
+ _reported = CGSizeZero;
106
+ [self ngn_reportSize];
107
+ }
108
+
109
+ - (void)ngn_reportSize
110
+ {
111
+ CGSize size = self.bounds.size;
112
+ if (!_ran || CGSizeEqualToSize(size, _reported) || size.width <= 0 || size.height <= 0 || self.onResize == nil) {
113
+ return;
114
+ }
115
+ _reported = size;
116
+ self.onResize(size, self.window.screen.scale ?: UIScreen.mainScreen.scale);
117
+ }
118
+
119
+ @end
120
+
121
+ /** One open window. */
122
+ @interface NgNativeSceneWindow : NSObject
123
+ @property (nonatomic) NSInteger surface;
124
+ @property (nonatomic, weak) UIWindowScene *scene;
125
+ // The scene's session, which outlives a disconnect: the window shows again when it reconnects.
126
+ @property (nonatomic, strong) UISceneSession *session;
127
+ // Whether the scene is this file's; the scene the app launched in is the app's.
128
+ @property (nonatomic) BOOL own;
129
+ // The window, for a scene of this file's while it is connected; the app's scene keeps its own.
130
+ @property (nonatomic, strong, nullable) UIWindow *window;
131
+ @property (nonatomic, strong) NgNativeWindowRootView *rootView;
132
+ @property (nonatomic, copy, nullable) NSString *key;
133
+ @property (nonatomic, copy) NSString *title;
134
+ // A reload kept it for the reloaded app to open again.
135
+ @property (nonatomic) BOOL kept;
136
+ @end
137
+
138
+ @implementation NgNativeSceneWindow
139
+ @end
140
+
141
+ /** A scene with no window yet: one the user made, or one iPadOS restored. */
142
+ @interface NgNativeWaitingScene : NSObject
143
+ @property (nonatomic, weak) UIWindowScene *scene;
144
+ @property (nonatomic, strong) UIWindow *window;
145
+ // New Window, or the app opened again: closed once the app answers it with a window it has open.
146
+ @property (nonatomic) BOOL userMade;
147
+ @end
148
+
149
+ @implementation NgNativeWaitingScene
150
+ @end
151
+
152
+ @class NgNativeWindows;
153
+
154
+ /** The windows, kept across reloads, which make a new module. Main thread. */
155
+ @interface NgNativeWindowHost : NSObject
156
+ @property (nonatomic, weak, nullable) NgNativeWindows *module;
157
+ + (instancetype)shared;
158
+ - (void)send:(NSDictionary *)event;
159
+ - (void)discarded:(NSSet<UISceneSession *> *)sessions;
160
+ - (void)connected:(UIWindowScene *)scene options:(UISceneConnectionOptions *)options;
161
+ @end
162
+
163
+ @interface NgNativeWindows : NSObject <RCTBridgeModule, RCTInvalidating>
164
+ - (void)send:(NSDictionary *)event;
165
+ @end
166
+
167
+ /** The scene delegate of a window this file opens. */
168
+ @interface NgNativeWindowSceneDelegate : UIResponder <UIWindowSceneDelegate>
169
+ /** Whether the scene is the app's own: a window's scene iPadOS restored before the app launched. */
170
+ - (BOOL)hostsApp;
171
+ @end
172
+
173
+ #pragma mark - Hooks
174
+
175
+ static Class appSceneDelegateClass(void);
176
+ // On an app scene delegate that handed its scene to the windows: the scene is a window's, not the app's.
177
+ static char HandedToWindows;
178
+
179
+ static BOOL multipleWindows(void)
180
+ {
181
+ return UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad &&
182
+ UIApplication.sharedApplication.supportsMultipleScenes;
183
+ }
184
+
185
+ static BOOL isWindowActivity(NSSet<NSUserActivity *> *activities)
186
+ {
187
+ for (NSUserActivity *activity in activities) {
188
+ if ([activity.activityType isEqualToString:WindowActivity]) {
189
+ return YES;
190
+ }
191
+ }
192
+ return NO;
193
+ }
194
+
195
+ /**
196
+ Whether `scene` is the app's own: Expo's scene delegate has it, unless it handed it to the
197
+ windows, or this file's handed it to one because iPadOS restored it before the app launched.
198
+ */
199
+ static BOOL isAppScene(UIScene *scene)
200
+ {
201
+ id<UISceneDelegate> delegate = scene.delegate;
202
+ if (delegate == nil || objc_getAssociatedObject(delegate, &HandedToWindows) != nil) {
203
+ return NO;
204
+ }
205
+ return ![delegate isKindOfClass:[NgNativeWindowSceneDelegate class]] ||
206
+ [(NgNativeWindowSceneDelegate *)delegate hostsApp];
207
+ }
208
+
209
+ /** Whether a scene of the app's own is connected: the app has launched. */
210
+ static BOOL appSceneConnected(void)
211
+ {
212
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
213
+ if (isAppScene(scene)) {
214
+ return YES;
215
+ }
216
+ }
217
+ return NO;
218
+ }
219
+
220
+ /**
221
+ The app delegate's scene configurations: a window's scene, and once the app has launched any
222
+ other new scene (New Window, or the app opened again after its first scene closed), is this
223
+ file's; the first is the app's, as before. React Native starts once, in that first scene.
224
+ */
225
+ static void hookSceneConfiguration(Class delegate)
226
+ {
227
+ SEL selector = @selector(application:configurationForConnectingSceneSession:options:);
228
+ NgNativeHook(delegate, selector, "@@:@@@", ^id(IMP previous) {
229
+ return ^UISceneConfiguration *(id self, UIApplication *app, UISceneSession *session, UISceneConnectionOptions *options) {
230
+ if (multipleWindows() && (isWindowActivity(options.userActivities) || reactHost != nil || appSceneConnected())) {
231
+ UISceneConfiguration *configuration = [UISceneConfiguration configurationWithName:nil
232
+ sessionRole:session.role];
233
+ configuration.delegateClass = [NgNativeWindowSceneDelegate class];
234
+ return configuration;
235
+ }
236
+ if (previous != NULL) {
237
+ return ((UISceneConfiguration * (*)(id, SEL, id, id, id)) previous)(self, selector, app, session, options);
238
+ }
239
+ return session.configuration;
240
+ };
241
+ });
242
+ }
243
+
244
+ /**
245
+ The app's scene delegate, connecting a scene once React Native runs: iPadOS keeps the delegate a
246
+ session was made with, so the scene the app launched in, connecting again, or restored after
247
+ another window's scene launched the app, comes here. It goes to the windows, as this file's
248
+ scenes do, rather than starting React Native a second time.
249
+ */
250
+ static void hookAppSceneConnection(void)
251
+ {
252
+ Class appDelegate = appSceneDelegateClass();
253
+ if (appDelegate == Nil || appDelegate == [NgNativeWindowSceneDelegate class]) {
254
+ return;
255
+ }
256
+ SEL selector = @selector(scene:willConnectToSession:options:);
257
+ NgNativeHook(appDelegate, selector, "v@:@@@", ^id(IMP previous) {
258
+ return ^(id<UIWindowSceneDelegate> self, UIScene *scene, UISceneSession *session, UISceneConnectionOptions *options) {
259
+ if (!multipleWindows() || reactHost == nil || ![scene isKindOfClass:[UIWindowScene class]]) {
260
+ if (previous != NULL) {
261
+ ((void (*)(id, SEL, id, id, id))previous)(self, selector, scene, session, options);
262
+ }
263
+ return;
264
+ }
265
+ objc_setAssociatedObject(self, &HandedToWindows, @YES, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
266
+ [[NgNativeWindowHost shared] connected:(UIWindowScene *)scene options:options];
267
+ // A link that opened this scene, or an activity handed to it, goes where the delegate sends one.
268
+ if (options.URLContexts.count > 0 && [self respondsToSelector:@selector(scene:openURLContexts:)]) {
269
+ [self scene:scene openURLContexts:options.URLContexts];
270
+ }
271
+ for (NSUserActivity *activity in options.userActivities) {
272
+ if (![activity.activityType isEqualToString:WindowActivity] &&
273
+ [self respondsToSelector:@selector(scene:continueUserActivity:)]) {
274
+ [self scene:scene continueUserActivity:activity];
275
+ }
276
+ }
277
+ };
278
+ });
279
+ }
280
+
281
+ /** The app delegate's discarded sessions: the scenes the user closed. */
282
+ static void hookSceneDiscard(Class delegate)
283
+ {
284
+ SEL selector = @selector(application:didDiscardSceneSessions:);
285
+ NgNativeHook(delegate, selector, "v@:@@", ^id(IMP previous) {
286
+ return ^(id self, UIApplication *app, NSSet<UISceneSession *> *sessions) {
287
+ if (previous != NULL) {
288
+ ((void (*)(id, SEL, id, id))previous)(self, selector, app, sessions);
289
+ }
290
+ [[NgNativeWindowHost shared] discarded:sessions];
291
+ };
292
+ });
293
+ }
294
+
295
+ @implementation NgNativeWindowSceneDelegate (Install)
296
+
297
+ + (void)load
298
+ {
299
+ // React Native's host, as it starts: every window's surface is made on it.
300
+ Class host = NSClassFromString(@"RCTHost");
301
+ if (host != Nil) {
302
+ NgNativeHook(host, @selector(start), "v@:", ^id(IMP previous) {
303
+ return ^(id self) {
304
+ reactHost = self;
305
+ if (previous != NULL) {
306
+ ((void (*)(id, SEL))previous)(self, @selector(start));
307
+ }
308
+ };
309
+ });
310
+ }
311
+ // The app delegate's scene configurations, hooked as it becomes the delegate: UIApplication reads
312
+ // which methods its delegate has then, and a method added later is never called.
313
+ NgNativeHook([UIApplication class], @selector(setDelegate:), "v@:@", ^id(IMP previous) {
314
+ return ^(UIApplication *self, id<UIApplicationDelegate> delegate) {
315
+ static dispatch_once_t once;
316
+ if (delegate != nil) {
317
+ dispatch_once(&once, ^{
318
+ hookSceneConfiguration([delegate class]);
319
+ hookSceneDiscard([delegate class]);
320
+ hookAppSceneConnection();
321
+ });
322
+ }
323
+ if (previous != NULL) {
324
+ ((void (*)(id, SEL, id))previous)(self, @selector(setDelegate:), delegate);
325
+ }
326
+ };
327
+ });
328
+ }
329
+
330
+ @end
331
+
332
+ #pragma mark - The host
333
+
334
+ @implementation NgNativeWindowHost {
335
+ NSMutableDictionary<NSNumber *, NgNativeSceneWindow *> *_windows;
336
+ NSMutableArray<NgNativeWaitingScene *> *_waiting;
337
+ // What `open` is waiting on for a scene iPadOS is connecting, by the request's id.
338
+ NSMutableDictionary<NSString *, void (^)(UIWindowScene *_Nullable)> *_requests;
339
+ // The scene the app launched in, until a window takes it, and the root it showed then.
340
+ UIViewController *_launchRoot;
341
+ BOOL _launchTaken;
342
+ BOOL _attached;
343
+ // How many times the app has reloaded: a reload's claim time ends only its own windows.
344
+ NSUInteger _reloads;
345
+ }
346
+
347
+ + (instancetype)shared
348
+ {
349
+ static NgNativeWindowHost *shared;
350
+ static dispatch_once_t once;
351
+ dispatch_once(&once, ^{
352
+ shared = [NgNativeWindowHost new];
353
+ });
354
+ return shared;
355
+ }
356
+
357
+ - (instancetype)init
358
+ {
359
+ if (self = [super init]) {
360
+ _windows = [NSMutableDictionary dictionary];
361
+ _waiting = [NSMutableArray array];
362
+ _requests = [NSMutableDictionary dictionary];
363
+ NSNotificationCenter *center = NSNotificationCenter.defaultCenter;
364
+ [center addObserver:self selector:@selector(sceneActivated:) name:UISceneDidActivateNotification object:nil];
365
+ [center addObserver:self selector:@selector(sceneDeactivating:) name:UISceneWillDeactivateNotification object:nil];
366
+ [center addObserver:self selector:@selector(sceneDisconnected:) name:UISceneDidDisconnectNotification object:nil];
367
+ }
368
+ return self;
369
+ }
370
+
371
+ - (void)send:(NSDictionary *)event
372
+ {
373
+ [self.module send:event];
374
+ }
375
+
376
+ /** A module starts. After the first, the app has reloaded. */
377
+ - (void)attach:(NgNativeWindows *)module
378
+ {
379
+ self.module = module;
380
+ if (!_attached) {
381
+ _attached = YES;
382
+ [self forgetClosedSessions:nil];
383
+ return;
384
+ }
385
+ // The app that asked for these scenes is gone: each resolves null, and its scene waits for the
386
+ // reloaded app as one restored would.
387
+ NSArray<void (^)(UIWindowScene *_Nullable)> *requests = _requests.allValues;
388
+ [_requests removeAllObjects];
389
+ for (void (^waitingOn)(UIWindowScene *_Nullable) in requests) {
390
+ waitingOn(nil);
391
+ }
392
+ for (NgNativeSceneWindow *window in _windows.allValues) {
393
+ if (window.key != nil) {
394
+ window.kept = YES;
395
+ } else {
396
+ [self closeWindow:window destroy:YES];
397
+ }
398
+ }
399
+ NSUInteger reload = ++_reloads;
400
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ClaimSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
401
+ // A later reload's windows have the whole claim time from that reload.
402
+ if (reload != self->_reloads) {
403
+ return;
404
+ }
405
+ for (NgNativeSceneWindow *window in self->_windows.allValues) {
406
+ if (window.kept) {
407
+ [self closeWindow:window destroy:YES];
408
+ }
409
+ }
410
+ });
411
+ }
412
+
413
+ #pragma mark Keys
414
+
415
+ /** The key of the window each scene session had, by the session's identifier. */
416
+ - (NSDictionary *)kept
417
+ {
418
+ return [NSUserDefaults.standardUserDefaults dictionaryForKey:KeptKeys] ?: @{};
419
+ }
420
+
421
+ - (nullable NSString *)keyOf:(UISceneSession *)session
422
+ {
423
+ return NgNativeSessionKey(self.kept, session.persistentIdentifier);
424
+ }
425
+
426
+ /** Mark the session's scene as a window's, with `key`. */
427
+ - (void)keep:(UISceneSession *)session key:(nullable NSString *)key
428
+ {
429
+ NSDictionary *kept = NgNativeKeepSession(self.kept, session.persistentIdentifier, key);
430
+ [NSUserDefaults.standardUserDefaults setObject:kept forKey:KeptKeys];
431
+ }
432
+
433
+ /** Forget the sessions iPadOS no longer keeps, and those in `discarded`. */
434
+ - (void)forgetClosedSessions:(nullable NSSet<UISceneSession *> *)discarded
435
+ {
436
+ NSMutableSet<NSString *> *open = [NSMutableSet set];
437
+ for (UISceneSession *session in UIApplication.sharedApplication.openSessions) {
438
+ [open addObject:session.persistentIdentifier];
439
+ }
440
+ for (UISceneSession *session in discarded) {
441
+ [open removeObject:session.persistentIdentifier];
442
+ }
443
+ NSDictionary *kept = NgNativeKeepOpenSessions(self.kept, open);
444
+ [NSUserDefaults.standardUserDefaults setObject:kept forKey:KeptKeys];
445
+ }
446
+
447
+ - (nullable NgNativeSceneWindow *)windowForScene:(UIScene *)scene
448
+ {
449
+ for (NgNativeSceneWindow *window in _windows.allValues) {
450
+ if (window.scene == scene) {
451
+ return window;
452
+ }
453
+ }
454
+ return nil;
455
+ }
456
+
457
+ - (nullable NgNativeSceneWindow *)windowForSession:(UISceneSession *)session
458
+ {
459
+ for (NgNativeSceneWindow *window in _windows.allValues) {
460
+ if ([window.session.persistentIdentifier isEqualToString:session.persistentIdentifier]) {
461
+ return window;
462
+ }
463
+ }
464
+ return nil;
465
+ }
466
+
467
+ #pragma mark Opening
468
+
469
+ - (void)open:(NSDictionary *)request resolve:(void (^)(id _Nullable surface))resolve
470
+ {
471
+ NSString *key = [request[@"key"] isKindOfClass:[NSString class]] ? request[@"key"] : nil;
472
+ NSString *title = [request[@"title"] isKindOfClass:[NSString class]] ? request[@"title"] : @"";
473
+ if (reactHost == nil || !multipleWindows()) {
474
+ resolve(nil);
475
+ return;
476
+ }
477
+ // A window a reload kept goes back to the app that opens its key again.
478
+ for (NgNativeSceneWindow *window in _windows.allValues) {
479
+ if (key != nil && window.kept && [window.key isEqualToString:key]) {
480
+ window.kept = NO;
481
+ window.title = title;
482
+ window.scene.title = title;
483
+ resolve(@(window.surface));
484
+ // The reloaded app keeps a size heard before its mount on the surface, and hands it over.
485
+ [window.rootView ngn_reportSizeAgain];
486
+ return;
487
+ }
488
+ }
489
+ // The scene the app launched in or one waiting for a window, the one kept for this key before
490
+ // the others, then the hidden scene of the window with this key, then a new one.
491
+ UIWindowScene *launch = _launchTaken ? nil : [self launchScene];
492
+ [_waiting filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NgNativeWaitingScene *waiting, id _) {
493
+ return waiting.scene != nil;
494
+ }]];
495
+ NSMutableArray *keys = [NSMutableArray array];
496
+ if (launch != nil) {
497
+ [keys addObject:[self keyOf:launch.session] ?: NSNull.null];
498
+ }
499
+ for (NgNativeWaitingScene *waiting in _waiting) {
500
+ [keys addObject:[self keyOf:waiting.scene.session] ?: NSNull.null];
501
+ }
502
+ NSUInteger chosen = NgNativeSceneForKey(keys, key);
503
+ if (launch != nil && chosen == 0) {
504
+ _launchTaken = YES;
505
+ UIWindow *window = launch.keyWindow ?: launch.windows.firstObject;
506
+ _launchRoot = window.rootViewController;
507
+ [self show:window scene:launch session:launch.session own:NO key:key title:title resolve:resolve];
508
+ return;
509
+ }
510
+ if (launch != nil && [self keyOf:launch.session] != nil) {
511
+ // iPadOS restored the app in another window's scene: it waits for that window, as a scene
512
+ // waiting for one does, and goes if the app does not open it.
513
+ [self dropLaunchSceneUnlessTaken:launch];
514
+ }
515
+ if (chosen != NSNotFound) {
516
+ NgNativeWaitingScene *waiting = _waiting[chosen - (launch != nil ? 1 : 0)];
517
+ [_waiting removeObject:waiting];
518
+ [self show:waiting.window scene:waiting.scene session:waiting.scene.session own:YES key:key title:title resolve:resolve];
519
+ return;
520
+ }
521
+ UISceneSession *hidden = [self hiddenSessionFor:key];
522
+ if (hidden != nil) {
523
+ // The window shows once iPadOS connects its scene again, as one it disconnected does.
524
+ [self show:nil scene:nil session:hidden own:YES key:key title:title resolve:resolve];
525
+ return;
526
+ }
527
+ NSString *requestId = NSUUID.UUID.UUIDString;
528
+ __weak NgNativeWindowHost *weakSelf = self;
529
+ _requests[requestId] = ^(UIWindowScene *scene) {
530
+ NgNativeWindowHost *host = weakSelf;
531
+ if (host == nil || scene == nil) {
532
+ resolve(nil);
533
+ return;
534
+ }
535
+ UIWindow *window = [[UIWindow alloc] initWithWindowScene:scene];
536
+ [host show:window scene:scene session:scene.session own:YES key:key title:title resolve:resolve];
537
+ };
538
+ NSUserActivity *activity = [[NSUserActivity alloc] initWithActivityType:WindowActivity];
539
+ activity.userInfo = @{@"request" : requestId, @"key" : key ?: @""};
540
+ // Options from iOS 17, as focus: passes them; nil below, where the system picks the scene.
541
+ UISceneActivationRequestOptions *options = nil;
542
+ if (@available(iOS 17.0, *)) {
543
+ options = [UISceneActivationRequestOptions new];
544
+ options.requestingScene = [self activeScene];
545
+ }
546
+ #pragma clang diagnostic push
547
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
548
+ [UIApplication.sharedApplication requestSceneSessionActivation:nil
549
+ userActivity:activity
550
+ options:options
551
+ errorHandler:^(NSError *error) {
552
+ dispatch_async(dispatch_get_main_queue(), ^{
553
+ NgNativeWindowHost *host = weakSelf;
554
+ if (host == nil) {
555
+ return;
556
+ }
557
+ void (^waitingOn)(UIWindowScene *) = host->_requests[requestId];
558
+ [host->_requests removeObjectForKey:requestId];
559
+ if (waitingOn) {
560
+ waitingOn(nil);
561
+ }
562
+ });
563
+ }];
564
+ #pragma clang diagnostic pop
565
+ }
566
+
567
+ /** The scene the app launched in, the app's own, while it is connected. */
568
+ - (nullable UIWindowScene *)launchScene
569
+ {
570
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
571
+ if ([scene isKindOfClass:[UIWindowScene class]] && isAppScene(scene) && [self windowForScene:scene] == nil) {
572
+ return (UIWindowScene *)scene;
573
+ }
574
+ }
575
+ return nil;
576
+ }
577
+
578
+ - (nullable UIWindowScene *)activeScene
579
+ {
580
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
581
+ if (scene.activationState == UISceneActivationStateForegroundActive && [scene isKindOfClass:[UIWindowScene class]]) {
582
+ return (UIWindowScene *)scene;
583
+ }
584
+ }
585
+ return nil;
586
+ }
587
+
588
+ /** A session iPadOS keeps for the window with `key` with no scene connected: a hidden window. */
589
+ - (nullable UISceneSession *)hiddenSessionFor:(nullable NSString *)key
590
+ {
591
+ NSMutableArray<UISceneSession *> *sessions = [NSMutableArray array];
592
+ NSMutableArray<NSString *> *ids = [NSMutableArray array];
593
+ for (UISceneSession *session in UIApplication.sharedApplication.openSessions) {
594
+ if (session.scene == nil && [self windowForSession:session] == nil) {
595
+ [sessions addObject:session];
596
+ [ids addObject:session.persistentIdentifier];
597
+ }
598
+ }
599
+ NSUInteger found = NgNativeSessionForKey(self.kept, ids, key);
600
+ return found == NSNotFound ? nil : sessions[found];
601
+ }
602
+
603
+ /**
604
+ Once the claim time is up, a scene the app launched in that no window took shows a window that is
605
+ waiting for its hidden scene instead; with none waiting, the scene goes.
606
+ */
607
+ - (void)dropLaunchSceneUnlessTaken:(UIWindowScene *)launch
608
+ {
609
+ __weak UIWindowScene *weakLaunch = launch;
610
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ClaimSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
611
+ UIWindowScene *scene = weakLaunch;
612
+ if (scene == nil || self->_launchTaken || [self launchScene] != scene) {
613
+ return;
614
+ }
615
+ for (NgNativeSceneWindow *window in self->_windows.allValues) {
616
+ if (window.scene == nil) {
617
+ UISceneSession *hidden = window.session;
618
+ self->_launchTaken = YES;
619
+ window.own = NO;
620
+ window.session = scene.session;
621
+ [self keep:scene.session key:window.key];
622
+ [self reattach:window to:scene];
623
+ [self destroySession:hidden];
624
+ return;
625
+ }
626
+ }
627
+ [self destroyScene:scene];
628
+ });
629
+ }
630
+
631
+ /**
632
+ A surface of its own in `window`, resolved with its id once it runs. With no window, the surface
633
+ waits for its session's scene to connect.
634
+ */
635
+ - (void)show:(nullable UIWindow *)window
636
+ scene:(nullable UIWindowScene *)scene
637
+ session:(UISceneSession *)session
638
+ own:(BOOL)own
639
+ key:(nullable NSString *)key
640
+ title:(NSString *)title
641
+ resolve:(void (^)(id _Nullable surface))resolve
642
+ {
643
+ id<RCTSurfaceProtocol> surface = [reactHost createSurfaceWithModuleName:WindowRunnable initialProperties:@{}];
644
+ if (surface == nil) {
645
+ resolve(nil);
646
+ return;
647
+ }
648
+ NgNativeWindowRootView *rootView = [[NgNativeWindowRootView alloc] initWithSurface:surface];
649
+ rootView.backgroundColor = UIColor.systemBackgroundColor;
650
+ NgNativeSceneWindow *record = [NgNativeSceneWindow new];
651
+ record.surface = surface.rootViewTag.integerValue;
652
+ record.scene = scene;
653
+ record.session = session;
654
+ record.own = own;
655
+ record.window = own ? window : nil;
656
+ record.rootView = rootView;
657
+ record.key = key;
658
+ record.title = title;
659
+ _windows[@(record.surface)] = record;
660
+ // A scene iPadOS restores with the session goes back to the same window.
661
+ [self keep:session key:key];
662
+ scene.title = title;
663
+ __weak NgNativeWindowHost *weakSelf = self;
664
+ NSInteger surfaceId = record.surface;
665
+ rootView.onResize = ^(CGSize size, CGFloat scale) {
666
+ [weakSelf send:@{
667
+ @"type" : @"resize",
668
+ @"surface" : @(surfaceId),
669
+ @"width" : @(size.width),
670
+ @"height" : @(size.height),
671
+ @"scale" : @(scale),
672
+ }];
673
+ };
674
+ // The scene the window has once it runs: one it waited for, or the launch scene that adopted it.
675
+ __weak NgNativeSceneWindow *weakRecord = record;
676
+ rootView.onRunning = ^{
677
+ resolve(@(surfaceId));
678
+ UIWindowScene *shownIn = weakRecord.scene;
679
+ if (shownIn != nil && shownIn.activationState == UISceneActivationStateForegroundActive) {
680
+ [weakSelf send:@{@"type" : @"focus", @"surface" : @(surfaceId)}];
681
+ }
682
+ };
683
+ if (window != nil) {
684
+ [self put:rootView in:window];
685
+ return;
686
+ }
687
+ // Laid out the size of a window the app has, until its own scene connects, rather than at none.
688
+ UIWindowScene *sized = [self activeScene] ?: [self anyWindowScene];
689
+ if (sized != nil) {
690
+ rootView.frame = sized.coordinateSpace.bounds;
691
+ [rootView layoutIfNeeded];
692
+ }
693
+ }
694
+
695
+ - (nullable UIWindowScene *)anyWindowScene
696
+ {
697
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
698
+ if ([scene isKindOfClass:[UIWindowScene class]]) {
699
+ return (UIWindowScene *)scene;
700
+ }
701
+ }
702
+ return nil;
703
+ }
704
+
705
+ - (void)put:(NgNativeWindowRootView *)rootView in:(UIWindow *)window
706
+ {
707
+ UIViewController *controller = [UIViewController new];
708
+ controller.view = rootView;
709
+ window.rootViewController = controller;
710
+ [window makeKeyAndVisible];
711
+ }
712
+
713
+ #pragma mark Scenes iPadOS connects
714
+
715
+ - (void)connected:(UIWindowScene *)scene options:(UISceneConnectionOptions *)options
716
+ {
717
+ BOOL marked = NgNativeSessionHadWindow(self.kept, scene.session.persistentIdentifier);
718
+ [self keep:scene.session key:[self keyOf:scene.session]];
719
+ // A window whose scene iPadOS disconnected in the background: it shows again as it was.
720
+ NgNativeSceneWindow *detached = [self windowForSession:scene.session];
721
+ if (detached != nil) {
722
+ [self reattach:detached to:scene];
723
+ return;
724
+ }
725
+ for (NSUserActivity *activity in options.userActivities) {
726
+ NSString *requestId = activity.userInfo[@"request"];
727
+ if (![requestId isKindOfClass:[NSString class]]) {
728
+ continue;
729
+ }
730
+ void (^waitingOn)(UIWindowScene *) = _requests[requestId];
731
+ if (waitingOn) {
732
+ [_requests removeObjectForKey:requestId];
733
+ waitingOn(scene);
734
+ return;
735
+ }
736
+ }
737
+ // A scene the user made, or one of a window's: it waits for the app to open a window in it.
738
+ NgNativeWaitingScene *waiting = [NgNativeWaitingScene new];
739
+ waiting.scene = scene;
740
+ waiting.userMade = !marked && !isWindowActivity(options.userActivities);
741
+ waiting.window = [[UIWindow alloc] initWithWindowScene:scene];
742
+ UIViewController *blank = [UIViewController new];
743
+ blank.view.backgroundColor = UIColor.systemBackgroundColor;
744
+ waiting.window.rootViewController = blank;
745
+ [waiting.window makeKeyAndVisible];
746
+ [_waiting addObject:waiting];
747
+ [self send:@{@"type" : waiting.userMade ? @"newWindow" : @"reopen"}];
748
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ClaimSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
749
+ if ([self->_waiting containsObject:waiting]) {
750
+ [self->_waiting removeObject:waiting];
751
+ [self destroyScene:waiting.scene];
752
+ }
753
+ });
754
+ }
755
+
756
+ - (void)sceneActivated:(NSNotification *)note
757
+ {
758
+ NgNativeSceneWindow *window = [self windowForScene:note.object];
759
+ if (window != nil) {
760
+ [self send:@{@"type" : @"focus", @"surface" : @(window.surface)}];
761
+ }
762
+ }
763
+
764
+ - (void)sceneDeactivating:(NSNotification *)note
765
+ {
766
+ if ([self windowForScene:note.object] != nil) {
767
+ [self send:@{@"type" : @"focus", @"surface" : NSNull.null}];
768
+ }
769
+ }
770
+
771
+ /**
772
+ iPadOS disconnects a scene the user closed, and one in the background whose memory it takes back,
773
+ whose session it keeps: the window keeps its surface until the session is discarded.
774
+ */
775
+ - (void)sceneDisconnected:(NSNotification *)note
776
+ {
777
+ NgNativeSceneWindow *window = [self windowForScene:note.object];
778
+ if (window == nil) {
779
+ return;
780
+ }
781
+ window.window.hidden = YES;
782
+ window.window = nil;
783
+ }
784
+
785
+ - (void)reattach:(NgNativeSceneWindow *)record to:(UIWindowScene *)scene
786
+ {
787
+ record.scene = scene;
788
+ scene.title = record.title;
789
+ if (record.own) {
790
+ record.window = [[UIWindow alloc] initWithWindowScene:scene];
791
+ [self put:record.rootView in:record.window];
792
+ } else {
793
+ UIWindow *window = scene.keyWindow ?: scene.windows.firstObject;
794
+ if (window == nil) {
795
+ // A scene this file's delegate hosted for the app, connected again after the app launched:
796
+ // nothing made it a window, so the record makes one and hides it as it closes.
797
+ window = [[UIWindow alloc] initWithWindowScene:scene];
798
+ record.window = window;
799
+ }
800
+ _launchRoot = window.rootViewController;
801
+ [self put:record.rootView in:window];
802
+ }
803
+ // A scene already active, the launch scene adopting a window, sends no activation of its own.
804
+ // A surface still starting reports focus as it runs.
805
+ if (record.rootView.onRunning == nil && scene.activationState == UISceneActivationStateForegroundActive) {
806
+ [self send:@{@"type" : @"focus", @"surface" : @(record.surface)}];
807
+ }
808
+ }
809
+
810
+ /** The sessions of the scenes the user closed. */
811
+ - (void)discarded:(NSSet<UISceneSession *> *)sessions
812
+ {
813
+ [self forgetClosedSessions:sessions];
814
+ for (UISceneSession *session in sessions) {
815
+ NgNativeSceneWindow *window = [self windowForSession:session];
816
+ if (window != nil) {
817
+ [self send:@{@"type" : @"closed", @"surface" : @(window.surface)}];
818
+ [self closeWindow:window destroy:NO];
819
+ }
820
+ }
821
+ }
822
+
823
+ #pragma mark Closing
824
+
825
+ - (void)close:(NSInteger)surface
826
+ {
827
+ NgNativeSceneWindow *window = _windows[@(surface)];
828
+ if (window != nil) {
829
+ [self closeWindow:window destroy:YES];
830
+ }
831
+ }
832
+
833
+ - (void)closeWindow:(NgNativeSceneWindow *)window destroy:(BOOL)destroy
834
+ {
835
+ [_windows removeObjectForKey:@(window.surface)];
836
+ [window.rootView.surface stop];
837
+ UIWindowScene *scene = window.scene;
838
+ if (!window.own) {
839
+ // The scene the app launched in shows what it showed before, should it come back.
840
+ if (scene != nil && _launchRoot != nil) {
841
+ (scene.keyWindow ?: scene.windows.firstObject).rootViewController = _launchRoot;
842
+ }
843
+ _launchTaken = NO;
844
+ }
845
+ window.window.hidden = YES;
846
+ if (destroy) {
847
+ [self destroySession:window.session];
848
+ }
849
+ }
850
+
851
+ - (void)destroyScene:(nullable UIWindowScene *)scene
852
+ {
853
+ [self destroySession:scene.session];
854
+ }
855
+
856
+ - (void)destroySession:(nullable UISceneSession *)session
857
+ {
858
+ if (session == nil) {
859
+ return;
860
+ }
861
+ // iPadOS destroys no session whose scene has not connected since the app launched: that one is
862
+ // kept for no window, so once it connects it waits for one as a restored scene does, and goes.
863
+ __weak NgNativeWindowHost *weakSelf = self;
864
+ [UIApplication.sharedApplication requestSceneSessionDestruction:session
865
+ options:nil
866
+ errorHandler:^(NSError *error) {
867
+ dispatch_async(dispatch_get_main_queue(), ^{
868
+ // Unless a window opened in it since.
869
+ if ([weakSelf windowForSession:session] == nil) {
870
+ [weakSelf keep:session key:nil];
871
+ }
872
+ });
873
+ }];
874
+ }
875
+
876
+ /** The app answered a window the user made with one it has open: the scenes the user made go. */
877
+ - (void)dropUserMadeScenes
878
+ {
879
+ for (NgNativeWaitingScene *waiting in [_waiting copy]) {
880
+ if (waiting.userMade) {
881
+ [_waiting removeObject:waiting];
882
+ [self destroyScene:waiting.scene];
883
+ }
884
+ }
885
+ }
886
+
887
+ - (void)focus:(NSInteger)surface
888
+ {
889
+ UISceneSession *session = _windows[@(surface)].session;
890
+ if (session == nil) {
891
+ return;
892
+ }
893
+ [self dropUserMadeScenes];
894
+ #pragma clang diagnostic push
895
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
896
+ [UIApplication.sharedApplication requestSceneSessionActivation:session
897
+ userActivity:nil
898
+ options:nil
899
+ errorHandler:nil];
900
+ #pragma clang diagnostic pop
901
+ }
902
+
903
+ - (void)setTitle:(NSString *)title of:(NSInteger)surface
904
+ {
905
+ NgNativeSceneWindow *window = _windows[@(surface)];
906
+ window.title = title;
907
+ window.scene.title = title;
908
+ }
909
+
910
+ @end
911
+
912
+ #pragma mark - The scene delegate
913
+
914
+ /** The scene delegate the app's Info.plist names, which starts React Native in its scene. */
915
+ static Class appSceneDelegateClass(void)
916
+ {
917
+ NSDictionary *manifest = NSBundle.mainBundle.infoDictionary[@"UIApplicationSceneManifest"];
918
+ NSArray *configurations = manifest[@"UISceneConfigurations"][@"UIWindowSceneSessionRoleApplication"];
919
+ NSString *name = [configurations.firstObject isKindOfClass:[NSDictionary class]]
920
+ ? configurations.firstObject[@"UISceneDelegateClassName"]
921
+ : nil;
922
+ if (![name isKindOfClass:[NSString class]]) {
923
+ return Nil;
924
+ }
925
+ // A Swift class is found by its module-qualified name, or by its plain one where it names itself
926
+ // for Objective-C, as Expo's does.
927
+ return NSClassFromString(name) ?: NSClassFromString([name componentsSeparatedByString:@"."].lastObject);
928
+ }
929
+
930
+ @implementation NgNativeWindowSceneDelegate {
931
+ // The app's own delegate, for a window's scene iPadOS restored before the app had launched: that
932
+ // scene is the app's now, and everything it is told goes to that delegate.
933
+ id<UIWindowSceneDelegate> _app;
934
+ }
935
+
936
+ - (BOOL)hostsApp
937
+ {
938
+ return _app != nil;
939
+ }
940
+
941
+ - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions
942
+ {
943
+ if (reactHost == nil) {
944
+ Class appDelegate = appSceneDelegateClass();
945
+ if (appDelegate != Nil && appDelegate != [self class]) {
946
+ _app = [appDelegate new];
947
+ [_app scene:scene willConnectToSession:session options:connectionOptions];
948
+ return;
949
+ }
950
+ }
951
+ if ([scene isKindOfClass:[UIWindowScene class]]) {
952
+ [[NgNativeWindowHost shared] connected:(UIWindowScene *)scene options:connectionOptions];
953
+ }
954
+ // A link that opened this scene, or an activity handed to it, as the app's own scene hands on.
955
+ if (connectionOptions.URLContexts.count > 0) {
956
+ [self scene:scene openURLContexts:connectionOptions.URLContexts];
957
+ }
958
+ for (NSUserActivity *activity in connectionOptions.userActivities) {
959
+ if (![activity.activityType isEqualToString:WindowActivity]) {
960
+ [self scene:scene continueUserActivity:activity];
961
+ }
962
+ }
963
+ }
964
+
965
+ // UIKit reads which methods a scene delegate has as it is set, before `_app` is made, so each one
966
+ // the app's delegate may answer is here, handing on what it is told.
967
+ - (UIWindow *)window
968
+ {
969
+ return [_app respondsToSelector:@selector(window)] ? _app.window : nil;
970
+ }
971
+
972
+ - (void)sceneDidDisconnect:(UIScene *)scene
973
+ {
974
+ if ([_app respondsToSelector:_cmd]) {
975
+ [_app sceneDidDisconnect:scene];
976
+ }
977
+ }
978
+
979
+ - (void)sceneDidBecomeActive:(UIScene *)scene
980
+ {
981
+ if ([_app respondsToSelector:_cmd]) {
982
+ [_app sceneDidBecomeActive:scene];
983
+ }
984
+ }
985
+
986
+ - (void)sceneWillResignActive:(UIScene *)scene
987
+ {
988
+ if ([_app respondsToSelector:_cmd]) {
989
+ [_app sceneWillResignActive:scene];
990
+ }
991
+ }
992
+
993
+ - (void)sceneWillEnterForeground:(UIScene *)scene
994
+ {
995
+ if ([_app respondsToSelector:_cmd]) {
996
+ [_app sceneWillEnterForeground:scene];
997
+ }
998
+ }
999
+
1000
+ - (void)sceneDidEnterBackground:(UIScene *)scene
1001
+ {
1002
+ if ([_app respondsToSelector:_cmd]) {
1003
+ [_app sceneDidEnterBackground:scene];
1004
+ }
1005
+ }
1006
+
1007
+ // A link or a handed-off activity for a window's scene goes to the app delegate, as the app's own
1008
+ // scene delegate hands on its own: Expo's modules subscribe to the app delegate's.
1009
+ - (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
1010
+ {
1011
+ if (_app != nil) {
1012
+ if ([_app respondsToSelector:_cmd]) {
1013
+ [_app scene:scene openURLContexts:URLContexts];
1014
+ }
1015
+ return;
1016
+ }
1017
+ UIApplication *app = UIApplication.sharedApplication;
1018
+ id<UIApplicationDelegate> delegate = app.delegate;
1019
+ if (![delegate respondsToSelector:@selector(application:openURL:options:)]) {
1020
+ return;
1021
+ }
1022
+ for (UIOpenURLContext *context in URLContexts) {
1023
+ NSMutableDictionary<UIApplicationOpenURLOptionsKey, id> *options = [NSMutableDictionary dictionary];
1024
+ options[UIApplicationOpenURLOptionsOpenInPlaceKey] = @(context.options.openInPlace);
1025
+ options[UIApplicationOpenURLOptionsSourceApplicationKey] = context.options.sourceApplication;
1026
+ options[UIApplicationOpenURLOptionsAnnotationKey] = context.options.annotation;
1027
+ [delegate application:app openURL:context.URL options:options];
1028
+ }
1029
+ }
1030
+
1031
+ - (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity
1032
+ {
1033
+ if (_app != nil) {
1034
+ if ([_app respondsToSelector:_cmd]) {
1035
+ [_app scene:scene continueUserActivity:userActivity];
1036
+ }
1037
+ return;
1038
+ }
1039
+ UIApplication *app = UIApplication.sharedApplication;
1040
+ id<UIApplicationDelegate> delegate = app.delegate;
1041
+ if ([delegate respondsToSelector:@selector(application:continueUserActivity:restorationHandler:)]) {
1042
+ [delegate application:app
1043
+ continueUserActivity:userActivity
1044
+ restorationHandler:^(NSArray<id<UIUserActivityRestoring>> *restorable){
1045
+ }];
1046
+ }
1047
+ }
1048
+
1049
+ @end
1050
+
1051
+ #pragma mark - The module
1052
+
1053
+ @implementation NgNativeWindows
1054
+
1055
+ @synthesize callableJSModules = _callableJSModules;
1056
+
1057
+ RCT_EXPORT_MODULE()
1058
+
1059
+ + (BOOL)requiresMainQueueSetup
1060
+ {
1061
+ return YES;
1062
+ }
1063
+
1064
+ - (dispatch_queue_t)methodQueue
1065
+ {
1066
+ return dispatch_get_main_queue();
1067
+ }
1068
+
1069
+ - (instancetype)init
1070
+ {
1071
+ if (self = [super init]) {
1072
+ dispatch_async(dispatch_get_main_queue(), ^{
1073
+ if (multipleWindows()) {
1074
+ [[NgNativeWindowHost shared] attach:self];
1075
+ }
1076
+ });
1077
+ }
1078
+ return self;
1079
+ }
1080
+
1081
+ - (NSDictionary *)constantsToExport
1082
+ {
1083
+ return @{@"multipleWindows" : @(multipleWindows())};
1084
+ }
1085
+
1086
+ - (void)invalidate
1087
+ {
1088
+ }
1089
+
1090
+ - (void)send:(NSDictionary *)event
1091
+ {
1092
+ [_callableJSModules invokeModule:@"RCTDeviceEventEmitter" method:@"emit" withArgs:@[ @"ngNativeWindows", event ]];
1093
+ }
1094
+
1095
+ RCT_EXPORT_METHOD(open : (NSDictionary *)request resolve : (RCTPromiseResolveBlock)resolve reject : (RCTPromiseRejectBlock)reject)
1096
+ {
1097
+ [[NgNativeWindowHost shared] open:request resolve:^(id surface) {
1098
+ resolve(surface ?: NSNull.null);
1099
+ }];
1100
+ }
1101
+
1102
+ RCT_EXPORT_METHOD(close : (double)surface)
1103
+ {
1104
+ [[NgNativeWindowHost shared] close:(NSInteger)surface];
1105
+ }
1106
+
1107
+ RCT_EXPORT_METHOD(focus : (double)surface)
1108
+ {
1109
+ [[NgNativeWindowHost shared] focus:(NSInteger)surface];
1110
+ }
1111
+
1112
+ RCT_EXPORT_METHOD(setTitle : (double)surface title : (NSString *)title)
1113
+ {
1114
+ [[NgNativeWindowHost shared] setTitle:title of:(NSInteger)surface];
1115
+ }
1116
+
1117
+ // iOS ends an app itself: there is nothing to quit, and no quit to answer.
1118
+ RCT_EXPORT_METHOD(quit)
1119
+ {
1120
+ }
1121
+
1122
+ RCT_EXPORT_METHOD(answerQuit : (BOOL)proceed)
1123
+ {
1124
+ }
1125
+
1126
+ @end