@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,320 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTInvalidating.h>
3
+ #import <UIKit/UIKit.h>
4
+
5
+ #import "NgNativeMenuBuilding.h"
6
+ #import "NgNativeResponderHooks.h"
7
+ #import "NgNativeViewBinder.h"
8
+
9
+ /**
10
+ The `NgNativeMenus` module on iOS (`NativeMenus` in @ng-native/device):
11
+ - `setMenuBar(groups)`: on an iPad, the app's groups in the main menu through UIMenuBuilder, the
12
+ iPadOS menu bar and the hold-Cmd overlay. A group with a standard `menuRole` adds its items to
13
+ that menu, another is a menu of its own after View. Role items the OS does itself stay its own.
14
+ - `setShortcuts(shortcuts)`: a UIKeyCommand for each, from a hardware keyboard, on the app
15
+ delegate, so they work wherever focus is.
16
+ - `setTakesLongPress(nativeID, takes)`: the view with that `nativeID` takes a long press itself,
17
+ with a `(longPress)` of its own, so a long press on it or inside it opens no context menu of a
18
+ view around it. iOS tells a long press from a secondary click only by the gesture, so an iPad
19
+ pointer's secondary click there opens none either.
20
+ - `setContextMenu(nativeID, entries)`: a UIContextMenuInteraction on React Native's own view with
21
+ that `nativeID`, added once the view is mounted, its menu read as it opens, and removed with
22
+ null, or as the view is unmounted, recycled or renamed. A long press opens it, and a secondary
23
+ click from an iPad pointer.
24
+ - Events on `ngNativeMenus`: `menuSelect` for a menu bar item, `shortcut` with
25
+ `textInputFocused`, `navigate` for the Back and Forward roles, and `contextMenuSelect` with the
26
+ view's `nativeID` and the item's `id`, since React Native's view sends no event it does not know.
27
+ - `menuBar` is true on an iPad and false on an iPhone; `appName` is the bundle's display name.
28
+ */
29
+ @interface NgNativeMenus : NSObject <RCTBridgeModule, RCTInvalidating, NgNativeMenusHost>
30
+ - (nullable NSArray *)contextMenuFor:(NSString *)nativeID;
31
+ - (BOOL)longPressTakenAt:(CGPoint)location in:(UIView *)view;
32
+ - (void)send:(NSDictionary *)event;
33
+ @end
34
+
35
+ /** The delegate of one view's context menu interaction, which it keeps. */
36
+ @interface NgNativeContextMenuTarget : NSObject <UIContextMenuInteractionDelegate>
37
+ @property (nonatomic, readonly) UIContextMenuInteraction *interaction;
38
+ @end
39
+
40
+ @implementation NgNativeContextMenuTarget {
41
+ NSString *_nativeID;
42
+ __weak NgNativeMenus *_module;
43
+ }
44
+
45
+ - (instancetype)initWithNativeID:(NSString *)nativeID module:(NgNativeMenus *)module
46
+ {
47
+ if (self = [super init]) {
48
+ _nativeID = [nativeID copy];
49
+ _module = module;
50
+ _interaction = [[UIContextMenuInteraction alloc] initWithDelegate:self];
51
+ }
52
+ return self;
53
+ }
54
+
55
+ - (UIContextMenuConfiguration *)contextMenuInteraction:(UIContextMenuInteraction *)interaction
56
+ configurationForMenuAtLocation:(CGPoint)location
57
+ {
58
+ // A long press a view inside takes itself, with a `(longPress)` of its own, opens no menu here.
59
+ if ([_module longPressTakenAt:location in:interaction.view]) {
60
+ return nil;
61
+ }
62
+ // The menu as it is when it opens.
63
+ NSArray *entries = [_module contextMenuFor:_nativeID];
64
+ if (entries.count == 0) {
65
+ return nil;
66
+ }
67
+ NSString *nativeID = _nativeID;
68
+ __weak NgNativeMenus *module = _module;
69
+ return [UIContextMenuConfiguration
70
+ configurationWithIdentifier:nil
71
+ previewProvider:nil
72
+ actionProvider:^UIMenu *(NSArray<UIMenuElement *> *suggested) {
73
+ NSArray<UIMenuElement *> *elements = NgNativeMenuElements(
74
+ entries,
75
+ ^(NSString *itemId) {
76
+ [module send:@{@"type" : @"contextMenuSelect", @"nativeID" : nativeID, @"id" : itemId}];
77
+ },
78
+ NULL);
79
+ return [UIMenu menuWithTitle:@"" children:elements];
80
+ }];
81
+ }
82
+
83
+ @end
84
+
85
+ static NSString *const NavigatePrefix = @"navigate:";
86
+
87
+ static UIMenuIdentifier standardMenu(NSString *role)
88
+ {
89
+ static NSDictionary<NSString *, UIMenuIdentifier> *menus;
90
+ static dispatch_once_t once;
91
+ dispatch_once(&once, ^{
92
+ menus = @{
93
+ @"app" : UIMenuApplication,
94
+ @"file" : UIMenuFile,
95
+ @"edit" : UIMenuEdit,
96
+ @"view" : UIMenuView,
97
+ @"window" : UIMenuWindow,
98
+ @"help" : UIMenuHelp,
99
+ };
100
+ });
101
+ return [role isKindOfClass:[NSString class]] ? menus[role] : nil;
102
+ }
103
+
104
+ @implementation NgNativeMenus {
105
+ NSArray *_menuBar;
106
+ NSArray<UIKeyCommand *> *_shortcuts;
107
+ NSMutableDictionary<NSString *, NSArray *> *_contextMenus;
108
+ NgNativeViewBinder *_contextMenuViews;
109
+ // The views inside a context menu's that take a long press themselves, by `nativeID`.
110
+ NSMutableSet<NSString *> *_longPressTakers;
111
+ }
112
+
113
+ @synthesize callableJSModules = _callableJSModules;
114
+
115
+ RCT_EXPORT_MODULE()
116
+
117
+ + (BOOL)requiresMainQueueSetup
118
+ {
119
+ return YES;
120
+ }
121
+
122
+ - (dispatch_queue_t)methodQueue
123
+ {
124
+ return dispatch_get_main_queue();
125
+ }
126
+
127
+ - (instancetype)init
128
+ {
129
+ if (self = [super init]) {
130
+ _menuBar = @[];
131
+ _shortcuts = @[];
132
+ _contextMenus = [NSMutableDictionary dictionary];
133
+ _longPressTakers = [NSMutableSet set];
134
+ __weak NgNativeMenus *weakSelf = self;
135
+ _contextMenuViews = [[NgNativeViewBinder alloc]
136
+ initWithAttach:^id(UIView *view, NSString *nativeID) {
137
+ NgNativeMenus *module = weakSelf;
138
+ if (module == nil) {
139
+ return nil;
140
+ }
141
+ NgNativeContextMenuTarget *target = [[NgNativeContextMenuTarget alloc] initWithNativeID:nativeID
142
+ module:module];
143
+ [view addInteraction:target.interaction];
144
+ return target;
145
+ }
146
+ detach:^(UIView *view, NgNativeContextMenuTarget *target) {
147
+ [view removeInteraction:target.interaction];
148
+ }];
149
+ dispatch_async(dispatch_get_main_queue(), ^{
150
+ NgNativeInstallResponderHooks();
151
+ NgNativeSetMenusHost(self);
152
+ });
153
+ }
154
+ return self;
155
+ }
156
+
157
+ // A reload makes a new module: this one's menus go with it, the menu bar's groups among them,
158
+ // which UIKit keeps until it is asked to build the menu bar again.
159
+ - (void)invalidate
160
+ {
161
+ dispatch_async(dispatch_get_main_queue(), ^{
162
+ [self->_contextMenuViews unbindAll];
163
+ [self->_contextMenus removeAllObjects];
164
+ [self->_longPressTakers removeAllObjects];
165
+ self->_menuBar = @[];
166
+ self->_shortcuts = @[];
167
+ [UIMenuSystem.mainSystem setNeedsRebuild];
168
+ });
169
+ }
170
+
171
+ - (NSDictionary *)constantsToExport
172
+ {
173
+ NSDictionary *info = NSBundle.mainBundle.infoDictionary;
174
+ return @{
175
+ @"menuBar" : @(UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad),
176
+ @"appName" : info[@"CFBundleDisplayName"] ?: info[@"CFBundleName"] ?: @"",
177
+ };
178
+ }
179
+
180
+ RCT_EXPORT_METHOD(setMenuBar : (NSArray *)groups)
181
+ {
182
+ _menuBar = [groups isKindOfClass:[NSArray class]] ? groups : @[];
183
+ [UIMenuSystem.mainSystem setNeedsRebuild];
184
+ }
185
+
186
+ RCT_EXPORT_METHOD(setShortcuts : (NSArray *)shortcuts)
187
+ {
188
+ NSMutableArray<UIKeyCommand *> *commands = [NSMutableArray array];
189
+ for (NSDictionary *shortcut in [shortcuts isKindOfClass:[NSArray class]] ? shortcuts : @[]) {
190
+ if (![shortcut isKindOfClass:[NSDictionary class]] || ![shortcut[@"chord"] isKindOfClass:[NSDictionary class]]) {
191
+ continue;
192
+ }
193
+ UIKeyModifierFlags flags = 0;
194
+ NSString *input = NgNativeKeyInput(shortcut[@"chord"], &flags);
195
+ NSString *itemId = shortcut[@"id"];
196
+ if (input == nil || ![itemId isKindOfClass:[NSString class]]) {
197
+ continue;
198
+ }
199
+ NSString *label = [shortcut[@"label"] isKindOfClass:[NSString class]] ? shortcut[@"label"] : @"";
200
+ UIKeyCommand *command = [UIKeyCommand commandWithTitle:label
201
+ image:nil
202
+ action:@selector(ngn_shortcut:)
203
+ input:input
204
+ modifierFlags:flags
205
+ propertyList:itemId];
206
+ command.discoverabilityTitle = label.length > 0 ? label : nil;
207
+ [commands addObject:command];
208
+ }
209
+ _shortcuts = commands;
210
+ }
211
+
212
+ RCT_EXPORT_METHOD(setContextMenu : (NSString *)nativeID entries : (nullable NSArray *)entries)
213
+ {
214
+ if (![nativeID isKindOfClass:[NSString class]] || nativeID.length == 0) {
215
+ return;
216
+ }
217
+ if ([entries isKindOfClass:[NSArray class]] && entries.count > 0) {
218
+ _contextMenus[nativeID] = entries;
219
+ [_contextMenuViews bind:nativeID];
220
+ } else {
221
+ [_contextMenus removeObjectForKey:nativeID];
222
+ [_contextMenuViews unbind:nativeID];
223
+ }
224
+ }
225
+
226
+ RCT_EXPORT_METHOD(setTakesLongPress : (NSString *)nativeID takes : (BOOL)takes)
227
+ {
228
+ if (![nativeID isKindOfClass:[NSString class]] || nativeID.length == 0) {
229
+ return;
230
+ }
231
+ if (takes) {
232
+ [_longPressTakers addObject:nativeID];
233
+ } else {
234
+ [_longPressTakers removeObject:nativeID];
235
+ }
236
+ }
237
+
238
+ - (NSArray *)contextMenuFor:(NSString *)nativeID
239
+ {
240
+ return _contextMenus[nativeID];
241
+ }
242
+
243
+ - (BOOL)longPressTakenAt:(CGPoint)location in:(UIView *)view
244
+ {
245
+ return NgNativeLongPressTaken([view hitTest:location withEvent:nil], view, _longPressTakers);
246
+ }
247
+
248
+ - (void)send:(NSDictionary *)event
249
+ {
250
+ [_callableJSModules invokeModule:@"RCTDeviceEventEmitter" method:@"emit" withArgs:@[ @"ngNativeMenus", event ]];
251
+ }
252
+
253
+ #pragma mark - NgNativeMenusHost
254
+
255
+ - (NSArray<UIKeyCommand *> *)shortcutKeyCommands
256
+ {
257
+ return _shortcuts;
258
+ }
259
+
260
+ - (void)buildMenuBar:(id<UIMenuBuilder>)builder
261
+ {
262
+ if (builder.system != UIMenuSystem.mainSystem || UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPad) {
263
+ return;
264
+ }
265
+ UIMenuIdentifier after = UIMenuView;
266
+ NSUInteger index = 0;
267
+ // The chords UIKit's menus have, and each app item's as it goes in: a second one would throw.
268
+ NSMutableSet<NSString *> *taken = [NSMutableSet set];
269
+ UIMenu *root = [builder menuForIdentifier:UIMenuRoot];
270
+ if (root != nil) {
271
+ NgNativeCollectKeyChords(root, taken);
272
+ }
273
+ for (NSDictionary *group in _menuBar) {
274
+ index++;
275
+ if (![group isKindOfClass:[NSDictionary class]] || ![group[@"items"] isKindOfClass:[NSArray class]]) {
276
+ continue;
277
+ }
278
+ NSArray<UIMenuElement *> *elements = NgNativeWithoutTakenKeys(
279
+ NgNativeMenuElements(group[@"items"], ^(NSString *itemId) {
280
+ }, @selector(ngn_menuBarItem:)),
281
+ taken);
282
+ if (elements.count == 0) {
283
+ continue;
284
+ }
285
+ UIMenuIdentifier identifier = [NSString stringWithFormat:@"dev.ngnative.menu.%lu", (unsigned long)index];
286
+ UIMenuIdentifier standard = standardMenu(group[@"menuRole"]);
287
+ if (standard != nil && [builder menuForIdentifier:standard] != nil) {
288
+ UIMenu *section = [UIMenu menuWithTitle:@""
289
+ image:nil
290
+ identifier:identifier
291
+ options:UIMenuOptionsDisplayInline
292
+ children:elements];
293
+ [builder insertChildMenu:section atEndOfMenuForIdentifier:standard];
294
+ } else {
295
+ NSString *label = [group[@"label"] isKindOfClass:[NSString class]] ? group[@"label"] : @"";
296
+ UIMenu *menu = [UIMenu menuWithTitle:label image:nil identifier:identifier options:0 children:elements];
297
+ [builder insertSiblingMenu:menu afterMenuForIdentifier:after];
298
+ after = identifier;
299
+ }
300
+ }
301
+ }
302
+
303
+ - (void)commandChosen:(UICommand *)command shortcut:(BOOL)shortcut
304
+ {
305
+ NSString *itemId = [command.propertyList isKindOfClass:[NSString class]] ? command.propertyList : nil;
306
+ if (itemId == nil) {
307
+ return;
308
+ }
309
+ NSDictionary *event;
310
+ if ([itemId hasPrefix:NavigatePrefix]) {
311
+ event = @{@"type" : @"navigate", @"direction" : [itemId substringFromIndex:NavigatePrefix.length]};
312
+ } else if (shortcut) {
313
+ event = @{@"type" : @"shortcut", @"id" : itemId, @"textInputFocused" : @(NgNativeTextInputFocused())};
314
+ } else {
315
+ event = @{@"type" : @"menuSelect", @"id" : itemId};
316
+ }
317
+ [self send:event];
318
+ }
319
+
320
+ @end
@@ -0,0 +1,53 @@
1
+ // What the app delegate and UIApplication answer in the responder chain on iOS: the key commands
2
+ // for the app's shortcuts (NgNativeMenus), the iPad menu bar through UIMenuBuilder, and the Undo
3
+ // and Redo commands for the shared undo manager (NgNativeUndo). The app delegate is Expo's and
4
+ // UIApplication is UIKit's, so the hooks are added to their classes at run time. Each calls what
5
+ // the class had before (its own method or the one it inherited), and does only that while no
6
+ // module is there to answer.
7
+ #pragma once
8
+
9
+ #import <UIKit/UIKit.h>
10
+
11
+ NS_ASSUME_NONNULL_BEGIN
12
+
13
+ @protocol NgNativeMenusHost <NSObject>
14
+ // The key commands for every shortcut outside the menu bar.
15
+ - (NSArray<UIKeyCommand *> *)shortcutKeyCommands;
16
+ // Adds the app's groups to the main menu.
17
+ - (void)buildMenuBar:(id<UIMenuBuilder>)builder;
18
+ // A menu bar item or a shortcut was chosen: the command's `propertyList` is its id.
19
+ - (void)commandChosen:(UICommand *)command shortcut:(BOOL)shortcut;
20
+ @end
21
+
22
+ @protocol NgNativeUndoHost <NSObject>
23
+ - (NSUndoManager *)sharedUndoManager;
24
+ @end
25
+
26
+ // The actions of the app's menu bar items and shortcuts, which UIApplication answers at the end of
27
+ // the responder chain.
28
+ @interface UIApplication (NgNative)
29
+ - (void)ngn_menuBarItem:(UICommand *)command;
30
+ - (void)ngn_shortcut:(UIKeyCommand *)command;
31
+ @end
32
+
33
+ // Install the hooks, once, on the main thread. Each forwards to the host while it is set. The app
34
+ // delegate's are installed once there is an app delegate, so a call before then tries again.
35
+ void NgNativeInstallResponderHooks(void);
36
+ void NgNativeSetMenusHost(id<NgNativeMenusHost> _Nullable host);
37
+ void NgNativeSetUndoHost(id<NgNativeUndoHost> _Nullable host);
38
+
39
+ // Whether the first responder in the key window edits text.
40
+ BOOL NgNativeTextInputFocused(void);
41
+
42
+ // Whether a window answers the app's shared undo manager while `firstResponder` is first: with
43
+ // none, or one of React Native's views or a view controller. A text input, or another view that
44
+ // keeps an undo history of its own (a canvas), keeps its window's.
45
+ BOOL NgNativeAnswersSharedUndo(UIResponder *_Nullable firstResponder);
46
+
47
+ // Replaces `selector` on `cls` with the block `make` answers, which receives the implementation it
48
+ // replaces to call: the class's own, the one it inherited, or NULL where it had none, and `types`
49
+ // is the method's type encoding then. A method the class only inherited is added to the class, so
50
+ // its superclass is untouched.
51
+ void NgNativeHook(Class cls, SEL selector, const char *types, id (^make)(IMP _Nullable previous));
52
+
53
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,215 @@
1
+ #import "NgNativeResponderHooks.h"
2
+
3
+ #import <React/RCTViewComponentView.h>
4
+ #import <objc/runtime.h>
5
+
6
+ // Edit > Undo and Redo's actions, which UIKit's menus name and no public header declares.
7
+ @protocol NgNativeUndoActions
8
+ - (void)undo:(id)sender;
9
+ - (void)redo:(id)sender;
10
+ @end
11
+
12
+ static __weak id<NgNativeMenusHost> menusHost;
13
+ static __weak id<NgNativeUndoHost> undoHost;
14
+
15
+ void NgNativeSetMenusHost(id<NgNativeMenusHost> host)
16
+ {
17
+ menusHost = host;
18
+ }
19
+
20
+ void NgNativeSetUndoHost(id<NgNativeUndoHost> host)
21
+ {
22
+ undoHost = host;
23
+ }
24
+
25
+ /** The first responder, which answers `ngn_reportFirstResponder:` sent to no one in particular. */
26
+ static __weak UIResponder *reported;
27
+
28
+ @interface UIResponder (NgNativeFirstResponder)
29
+ - (void)ngn_reportFirstResponder:(id)sender;
30
+ @end
31
+
32
+ @implementation UIResponder (NgNativeFirstResponder)
33
+
34
+ - (void)ngn_reportFirstResponder:(id)sender
35
+ {
36
+ reported = self;
37
+ }
38
+
39
+ @end
40
+
41
+ static UIResponder *firstResponder(void)
42
+ {
43
+ reported = nil;
44
+ [UIApplication.sharedApplication sendAction:@selector(ngn_reportFirstResponder:) to:nil from:nil forEvent:nil];
45
+ UIResponder *found = reported;
46
+ reported = nil;
47
+ return found;
48
+ }
49
+
50
+ BOOL NgNativeTextInputFocused(void)
51
+ {
52
+ return [firstResponder() conformsToProtocol:@protocol(UITextInput)];
53
+ }
54
+
55
+ static UIWindow *keyWindow(void)
56
+ {
57
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
58
+ if ([scene isKindOfClass:[UIWindowScene class]]) {
59
+ for (UIWindow *window in ((UIWindowScene *)scene).windows) {
60
+ if (window.isKeyWindow) {
61
+ return window;
62
+ }
63
+ }
64
+ }
65
+ }
66
+ return nil;
67
+ }
68
+
69
+ void NgNativeHook(Class cls, SEL selector, const char *types, id (^make)(IMP _Nullable previous))
70
+ {
71
+ Method method = class_getInstanceMethod(cls, selector);
72
+ IMP previous = method != NULL ? method_getImplementation(method) : NULL;
73
+ IMP replacement = imp_implementationWithBlock(make(previous));
74
+ const char *encoding = method != NULL ? method_getTypeEncoding(method) : types;
75
+ if (!class_addMethod(cls, selector, replacement, encoding)) {
76
+ method_setImplementation(method, replacement);
77
+ }
78
+ }
79
+
80
+ BOOL NgNativeAnswersSharedUndo(UIResponder *firstResponder)
81
+ {
82
+ if (firstResponder == nil) {
83
+ return YES;
84
+ }
85
+ if ([firstResponder conformsToProtocol:@protocol(UITextInput)]) {
86
+ return NO;
87
+ }
88
+ if ([firstResponder isKindOfClass:[UIView class]]) {
89
+ return [firstResponder isKindOfClass:[RCTViewComponentView class]] ||
90
+ [firstResponder isKindOfClass:[UIWindow class]];
91
+ }
92
+ return YES;
93
+ }
94
+
95
+ @implementation UIApplication (NgNative)
96
+
97
+ - (void)ngn_menuBarItem:(UICommand *)command
98
+ {
99
+ [menusHost commandChosen:command shortcut:NO];
100
+ }
101
+
102
+ - (void)ngn_shortcut:(UIKeyCommand *)command
103
+ {
104
+ [menusHost commandChosen:command shortcut:YES];
105
+ }
106
+
107
+ @end
108
+
109
+ /** The app delegate's hooks: the shortcuts' key commands, and the app's groups in the menu bar. */
110
+ static void hookAppDelegate(Class delegate)
111
+ {
112
+ NgNativeHook(delegate, @selector(keyCommands), "@@:", ^id(IMP previous) {
113
+ return ^NSArray<UIKeyCommand *> *(UIResponder *self) {
114
+ NSArray<UIKeyCommand *> *own =
115
+ previous != NULL ? ((NSArray * (*)(id, SEL)) previous)(self, @selector(keyCommands)) : nil;
116
+ NSArray<UIKeyCommand *> *shortcuts = [menusHost shortcutKeyCommands];
117
+ if (shortcuts.count == 0) {
118
+ return own;
119
+ }
120
+ return own.count > 0 ? [own arrayByAddingObjectsFromArray:shortcuts] : shortcuts;
121
+ };
122
+ });
123
+
124
+ NgNativeHook(delegate, @selector(buildMenuWithBuilder:), "v@:@", ^id(IMP previous) {
125
+ return ^(UIResponder *self, id<UIMenuBuilder> builder) {
126
+ if (previous != NULL) {
127
+ ((void (*)(id, SEL, id))previous)(self, @selector(buildMenuWithBuilder:), builder);
128
+ }
129
+ [menusHost buildMenuBar:builder];
130
+ };
131
+ });
132
+ }
133
+
134
+ /**
135
+ Undo or Redo on UIApplication, at the end of the responder chain, while the undo module is there:
136
+ the key window's undo manager's, which is the app's, or a focused text input's own. Without the
137
+ module, what UIApplication did before, if anything.
138
+ */
139
+ static void hookUndoAction(SEL selector, BOOL redo)
140
+ {
141
+ NgNativeHook([UIApplication class], selector, "v@:@", ^id(IMP previous) {
142
+ return ^(UIResponder *self, id sender) {
143
+ if ([undoHost sharedUndoManager] == nil) {
144
+ if (previous != NULL) {
145
+ ((void (*)(id, SEL, id))previous)(self, selector, sender);
146
+ }
147
+ return;
148
+ }
149
+ NSUndoManager *manager = keyWindow().undoManager;
150
+ if (redo ? manager.canRedo : manager.canUndo) {
151
+ redo ? [manager redo] : [manager undo];
152
+ }
153
+ };
154
+ });
155
+ }
156
+
157
+ void NgNativeInstallResponderHooks(void)
158
+ {
159
+ static BOOL delegateHooked = NO;
160
+ if (!delegateHooked) {
161
+ Class delegate = [UIApplication.sharedApplication.delegate class];
162
+ if (delegate != nil && [delegate isSubclassOfClass:[UIResponder class]]) {
163
+ hookAppDelegate(delegate);
164
+ delegateHooked = YES;
165
+ }
166
+ }
167
+
168
+ static dispatch_once_t once;
169
+ dispatch_once(&once, ^{
170
+ Method undoBefore = class_getInstanceMethod([UIApplication class], @selector(undo:));
171
+ BOOL hadUndo = undoBefore != NULL;
172
+ hookUndoAction(@selector(undo:), NO);
173
+ hookUndoAction(@selector(redo:), YES);
174
+
175
+ // Undo and Redo named for the action, and disabled with none, or with no module to undo when
176
+ // UIApplication had no Undo of its own.
177
+ NgNativeHook([UIApplication class], @selector(validateCommand:), "v@:@", ^id(IMP previous) {
178
+ return ^(UIResponder *self, UICommand *command) {
179
+ if (previous != NULL) {
180
+ ((void (*)(id, SEL, id))previous)(self, @selector(validateCommand:), command);
181
+ }
182
+ BOOL undo = command.action == @selector(undo:);
183
+ if (!undo && command.action != @selector(redo:)) {
184
+ return;
185
+ }
186
+ if ([undoHost sharedUndoManager] == nil) {
187
+ if (!hadUndo) {
188
+ command.attributes |= UIMenuElementAttributesDisabled;
189
+ }
190
+ return;
191
+ }
192
+ NSUndoManager *manager = keyWindow().undoManager;
193
+ command.title = undo ? manager.undoMenuItemTitle : manager.redoMenuItemTitle;
194
+ BOOL can = undo ? manager.canUndo : manager.canRedo;
195
+ command.attributes = can ? (command.attributes & ~UIMenuElementAttributesDisabled)
196
+ : (command.attributes | UIMenuElementAttributesDisabled);
197
+ };
198
+ });
199
+
200
+ // Undo and Redo outside a text input: shake to undo, mod+z, and Edit > Undo on an iPad all
201
+ // reach the window's undo manager, which is then the one the app's actions are in. While a
202
+ // text input, or a view of another kind with a history of its own, is first responder the
203
+ // window answers its own, so typing keeps a history of its own, as on macOS, and the app's
204
+ // history holds only the app's actions. Without the module, each window has its own again.
205
+ NgNativeHook([UIWindow class], @selector(undoManager), "@@:", ^id(IMP previous) {
206
+ return ^NSUndoManager *(UIWindow *self) {
207
+ NSUndoManager *shared = [undoHost sharedUndoManager];
208
+ if (shared != nil && NgNativeAnswersSharedUndo(firstResponder())) {
209
+ return shared;
210
+ }
211
+ return previous != NULL ? ((NSUndoManager * (*)(id, SEL)) previous)(self, @selector(undoManager)) : nil;
212
+ };
213
+ });
214
+ });
215
+ }
@@ -0,0 +1,36 @@
1
+ // Which scene a window opens in on an iPad, by the key of the window each scene session had, apart
2
+ // from the module so the unit tests reach it without React Native. iPadOS keeps a session across
3
+ // launches, so the host keeps each one's key in the app's user defaults, by the session's
4
+ // persistent identifier, and a scene iPadOS restores goes back to the window it had. A session's
5
+ // own `userInfo` is not used: iPadOS can lose a change to it when the app is killed soon after.
6
+ #pragma once
7
+
8
+ #import <Foundation/Foundation.h>
9
+
10
+ NS_ASSUME_NONNULL_BEGIN
11
+
12
+ // Whether one of the app's windows had the session's scene: one iPadOS restores or connects again,
13
+ // not a New Window the user made. `kept` maps a session's identifier to its window's key, or to an
14
+ // empty string for a window with no key.
15
+ BOOL NgNativeSessionHadWindow(NSDictionary *_Nullable kept, NSString *session);
16
+
17
+ // The key of the window the session's scene had, or nil for none.
18
+ NSString *_Nullable NgNativeSessionKey(NSDictionary *_Nullable kept, NSString *session);
19
+
20
+ // `kept` with the session's scene marked as a window's, with `key`, or no key when it is nil.
21
+ NSDictionary *NgNativeKeepSession(NSDictionary *_Nullable kept, NSString *session, NSString *_Nullable key);
22
+
23
+ // `kept` without the sessions iPadOS no longer keeps: only those in `open`.
24
+ NSDictionary *NgNativeKeepOpenSessions(NSDictionary *_Nullable kept, NSSet<NSString *> *open);
25
+
26
+ // Which of the scenes waiting for a window, given the key each one's session had (NSNull for
27
+ // none), a window with `key` opens in: the one kept for its key, else the first kept for none. A
28
+ // scene kept for another key waits for that key. NSNotFound where none fits.
29
+ NSUInteger NgNativeSceneForKey(NSArray *sceneKeys, NSString *_Nullable key);
30
+
31
+ // Which of `sessions`, iPadOS's sessions with no scene connected, had the window with `key`: a
32
+ // window iPadOS lists as hidden, which the app opens there rather than in a new scene. NSNotFound
33
+ // for none, and always for a window with no key.
34
+ NSUInteger NgNativeSessionForKey(NSDictionary *_Nullable kept, NSArray<NSString *> *sessions, NSString *_Nullable key);
35
+
36
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,56 @@
1
+ #import "NgNativeSceneKeys.h"
2
+
3
+ BOOL NgNativeSessionHadWindow(NSDictionary *kept, NSString *session)
4
+ {
5
+ return [kept[session] isKindOfClass:[NSString class]];
6
+ }
7
+
8
+ NSString *NgNativeSessionKey(NSDictionary *kept, NSString *session)
9
+ {
10
+ id key = kept[session];
11
+ return [key isKindOfClass:[NSString class]] && [key length] > 0 ? key : nil;
12
+ }
13
+
14
+ NSDictionary *NgNativeKeepSession(NSDictionary *kept, NSString *session, NSString *key)
15
+ {
16
+ NSMutableDictionary *next = [kept mutableCopy] ?: [NSMutableDictionary dictionary];
17
+ next[session] = key ?: @"";
18
+ return next;
19
+ }
20
+
21
+ NSDictionary *NgNativeKeepOpenSessions(NSDictionary *kept, NSSet<NSString *> *open)
22
+ {
23
+ NSMutableDictionary *next = [NSMutableDictionary dictionary];
24
+ for (id session in kept) {
25
+ if ([open containsObject:session]) {
26
+ next[session] = kept[session];
27
+ }
28
+ }
29
+ return next;
30
+ }
31
+
32
+ NSUInteger NgNativeSceneForKey(NSArray *sceneKeys, NSString *key)
33
+ {
34
+ NSUInteger unkeyed = NSNotFound;
35
+ for (NSUInteger index = 0; index < sceneKeys.count; index++) {
36
+ id kept = sceneKeys[index];
37
+ if (![kept isKindOfClass:[NSString class]]) {
38
+ if (unkeyed == NSNotFound) {
39
+ unkeyed = index;
40
+ }
41
+ } else if (key != nil && [kept isEqualToString:key]) {
42
+ return index;
43
+ }
44
+ }
45
+ return unkeyed;
46
+ }
47
+
48
+ NSUInteger NgNativeSessionForKey(NSDictionary *kept, NSArray<NSString *> *sessions, NSString *key)
49
+ {
50
+ for (NSUInteger index = 0; index < sessions.count; index++) {
51
+ if ([NgNativeSessionKey(kept, sessions[index]) isEqualToString:key]) {
52
+ return index;
53
+ }
54
+ }
55
+ return NSNotFound;
56
+ }