@ng-native/components 0.22.2 → 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 +5 -0
  98. package/dist/transforms.js +7 -0
  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,342 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTInvalidating.h>
3
+ #import <UIKit/UIKit.h>
4
+ #import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
5
+
6
+ #import "NgNativeDropItems.h"
7
+ #import "NgNativeViewBinder.h"
8
+
9
+ /**
10
+ The `NgNativeDragAndDrop` module on iOS (`NativeDragAndDrop` in @ng-native/device), which adds
11
+ interactions to React Native's own views by `nativeID`, once each is mounted, and removes them as
12
+ it is unmounted, recycled or renamed. iPadOS only: on an iPhone every call does nothing.
13
+ - `setFileDrop(nativeID, takes)`: a UIDropInteraction that takes files, text and urls, sending
14
+ `dropEnter`, `dropLeave` and `dropPerform` with `{ nativeID, files, text, urls }` on
15
+ `ngNativeDragAndDrop`. A dropped file is copied into the app's temporary folder, since the OS
16
+ takes its own copy away when the drop ends.
17
+ - `setDragItem(nativeID, item)`: a UIDragInteraction offering `{ files, text, url }` from a long
18
+ press, or none with null.
19
+ */
20
+ @interface NgNativeDragAndDrop : NSObject <RCTBridgeModule, RCTInvalidating>
21
+ - (nullable NSDictionary *)dragItemFor:(NSString *)nativeID;
22
+ - (void)send:(NSString *)type nativeID:(NSString *)nativeID drop:(nullable NSDictionary *)drop;
23
+ - (nullable id)dropTargetFor:(NSString *)nativeID;
24
+ @end
25
+
26
+ static BOOL takesDrags(void)
27
+ {
28
+ return UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad;
29
+ }
30
+
31
+ static NSString *nonEmptyString(id value)
32
+ {
33
+ return [value isKindOfClass:[NSString class]] && [value length] > 0 ? value : nil;
34
+ }
35
+
36
+ #pragma mark - Drop
37
+
38
+ /** The delegate of one view's drop interaction, which it keeps. */
39
+ @interface NgNativeDropTarget : NSObject <UIDropInteractionDelegate>
40
+ @property (nonatomic, readonly) UIDropInteraction *interaction;
41
+ @end
42
+
43
+ @implementation NgNativeDropTarget {
44
+ NSString *_nativeID;
45
+ __weak NgNativeDragAndDrop *_module;
46
+ // A drop ends the drag without `dropLeave`.
47
+ BOOL _dropped;
48
+ }
49
+
50
+ - (instancetype)initWithNativeID:(NSString *)nativeID module:(NgNativeDragAndDrop *)module
51
+ {
52
+ if (self = [super init]) {
53
+ _nativeID = [nativeID copy];
54
+ _module = module;
55
+ _interaction = [[UIDropInteraction alloc] initWithDelegate:self];
56
+ }
57
+ return self;
58
+ }
59
+
60
+ - (BOOL)dropInteraction:(UIDropInteraction *)interaction canHandleSession:(id<UIDropSession>)session
61
+ {
62
+ return [session hasItemsConformingToTypeIdentifiers:@[ UTTypeItem.identifier ]];
63
+ }
64
+
65
+ - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnter:(id<UIDropSession>)session
66
+ {
67
+ _dropped = NO;
68
+ // iOS shows nothing of what is dragged until it is dropped.
69
+ [_module send:@"dropEnter" nativeID:_nativeID drop:nil];
70
+ }
71
+
72
+ - (UIDropProposal *)dropInteraction:(UIDropInteraction *)interaction sessionDidUpdate:(id<UIDropSession>)session
73
+ {
74
+ return [[UIDropProposal alloc] initWithDropOperation:UIDropOperationCopy];
75
+ }
76
+
77
+ - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidExit:(id<UIDropSession>)session
78
+ {
79
+ if (!_dropped) {
80
+ [_module send:@"dropLeave" nativeID:_nativeID drop:nil];
81
+ }
82
+ }
83
+
84
+ - (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session
85
+ {
86
+ _dropped = YES;
87
+ NSMutableArray<NSDictionary *> *files = [NSMutableArray array];
88
+ NSMutableArray<NSString *> *urls = [NSMutableArray array];
89
+ NSMutableArray<NSString *> *texts = [NSMutableArray array];
90
+ NSObject *lock = [NSObject new];
91
+ dispatch_group_t group = dispatch_group_create();
92
+ NSURL *folder = [[NSURL fileURLWithPath:NSTemporaryDirectory()]
93
+ URLByAppendingPathComponent:[NSString stringWithFormat:@"NgNativeDrops/%@", NSUUID.UUID.UUIDString]];
94
+
95
+ NSUInteger index = 0;
96
+ for (UIDragItem *item in session.items) {
97
+ NSItemProvider *provider = item.itemProvider;
98
+ NgNativeDropKind kind = NgNativeDropKindOf(provider);
99
+ NSString *type = NgNativeDropFileType(provider);
100
+ if (kind == NgNativeDropKindNone || (kind == NgNativeDropKindFile && type == nil)) {
101
+ continue;
102
+ }
103
+ dispatch_group_enter(group);
104
+ if (kind == NgNativeDropKindWebURL) {
105
+ [provider loadObjectOfClass:[NSURL class]
106
+ completionHandler:^(id<NSItemProviderReading> object, NSError *error) {
107
+ NSURL *url = [(NSObject *)object isKindOfClass:[NSURL class]] ? (NSURL *)object : nil;
108
+ if (url != nil) {
109
+ @synchronized(lock) {
110
+ [urls addObject:url.absoluteString];
111
+ }
112
+ }
113
+ dispatch_group_leave(group);
114
+ }];
115
+ } else if (kind == NgNativeDropKindText) {
116
+ [provider loadObjectOfClass:[NSString class]
117
+ completionHandler:^(id<NSItemProviderReading> object, NSError *error) {
118
+ if ([(NSObject *)object isKindOfClass:[NSString class]]) {
119
+ @synchronized(lock) {
120
+ [texts addObject:(NSString *)object];
121
+ }
122
+ }
123
+ dispatch_group_leave(group);
124
+ }];
125
+ } else {
126
+ NSString *name = NgNativeDropFileName(provider.suggestedName, type);
127
+ // A folder for each item, so two files of one name both arrive.
128
+ NSURL *itemFolder = [folder URLByAppendingPathComponent:[NSString stringWithFormat:@"%lu", (unsigned long)index]];
129
+ [provider loadFileRepresentationForTypeIdentifier:type
130
+ completionHandler:^(NSURL *url, NSError *error) {
131
+ // The OS removes `url` when this returns: keep a copy.
132
+ if (url != nil) {
133
+ NSFileManager *manager = NSFileManager.defaultManager;
134
+ NSURL *kept = [itemFolder URLByAppendingPathComponent:name];
135
+ if ([manager createDirectoryAtURL:itemFolder
136
+ withIntermediateDirectories:YES
137
+ attributes:nil
138
+ error:nil] &&
139
+ [manager copyItemAtURL:url toURL:kept error:nil]) {
140
+ @synchronized(lock) {
141
+ [files addObject:@{@"uri" : kept.absoluteString, @"name" : name}];
142
+ }
143
+ }
144
+ }
145
+ dispatch_group_leave(group);
146
+ }];
147
+ }
148
+ index++;
149
+ }
150
+
151
+ __weak NgNativeDragAndDrop *module = _module;
152
+ NSString *nativeID = _nativeID;
153
+ // Each binding of a view has a target of its own. A view that unmounted while the items loaded,
154
+ // and one bound since under the same `nativeID`, get nothing: the drop was not theirs.
155
+ __weak NgNativeDropTarget *target = self;
156
+ dispatch_group_notify(group, dispatch_get_main_queue(), ^{
157
+ if (target == nil || [module dropTargetFor:nativeID] != target) {
158
+ // Every file is copied by now, and no event will name one: nothing else removes them.
159
+ [NSFileManager.defaultManager removeItemAtURL:folder error:nil];
160
+ return;
161
+ }
162
+ NSString *text = texts.count > 0 ? [texts componentsJoinedByString:@"\n"] : nil;
163
+ [module send:@"dropPerform"
164
+ nativeID:nativeID
165
+ drop:@{@"files" : [files copy], @"urls" : [urls copy], @"text" : text ?: [NSNull null]}];
166
+ });
167
+ }
168
+
169
+ - (void)dropInteraction:(UIDropInteraction *)interaction sessionDidEnd:(id<UIDropSession>)session
170
+ {
171
+ _dropped = NO;
172
+ }
173
+
174
+ @end
175
+
176
+ #pragma mark - Drag
177
+
178
+ /** The delegate of one view's drag interaction, which it keeps. */
179
+ @interface NgNativeDragSource : NSObject <UIDragInteractionDelegate>
180
+ @property (nonatomic, readonly) UIDragInteraction *interaction;
181
+ @end
182
+
183
+ @implementation NgNativeDragSource {
184
+ NSString *_nativeID;
185
+ __weak NgNativeDragAndDrop *_module;
186
+ }
187
+
188
+ - (instancetype)initWithNativeID:(NSString *)nativeID module:(NgNativeDragAndDrop *)module
189
+ {
190
+ if (self = [super init]) {
191
+ _nativeID = [nativeID copy];
192
+ _module = module;
193
+ _interaction = [[UIDragInteraction alloc] initWithDelegate:self];
194
+ _interaction.enabled = YES;
195
+ }
196
+ return self;
197
+ }
198
+
199
+ - (NSArray<UIDragItem *> *)dragInteraction:(UIDragInteraction *)interaction
200
+ itemsForBeginningSession:(id<UIDragSession>)session
201
+ {
202
+ NSDictionary *item = [_module dragItemFor:_nativeID];
203
+ if (item == nil) {
204
+ return @[];
205
+ }
206
+ NSMutableArray<UIDragItem *> *items = [NSMutableArray array];
207
+ NSArray *files = [item[@"files"] isKindOfClass:[NSArray class]] ? item[@"files"] : @[];
208
+ for (id file in files) {
209
+ NSURL *url = nonEmptyString(file) ? [NSURL URLWithString:file] : nil;
210
+ NSItemProvider *provider = url.isFileURL ? [[NSItemProvider alloc] initWithContentsOfURL:url] : nil;
211
+ if (provider != nil) {
212
+ provider.suggestedName = url.lastPathComponent;
213
+ [items addObject:[[UIDragItem alloc] initWithItemProvider:provider]];
214
+ }
215
+ }
216
+ NSString *text = nonEmptyString(item[@"text"]);
217
+ if (text != nil) {
218
+ [items addObject:[[UIDragItem alloc] initWithItemProvider:[[NSItemProvider alloc] initWithObject:text]]];
219
+ }
220
+ NSString *link = nonEmptyString(item[@"url"]);
221
+ NSURL *url = link != nil ? [NSURL URLWithString:link] : nil;
222
+ if (url != nil) {
223
+ [items addObject:[[UIDragItem alloc] initWithItemProvider:[[NSItemProvider alloc] initWithObject:url]]];
224
+ }
225
+ return items;
226
+ }
227
+
228
+ @end
229
+
230
+ #pragma mark - Module
231
+
232
+ @implementation NgNativeDragAndDrop {
233
+ NSMutableDictionary<NSString *, NSDictionary *> *_dragItems;
234
+ NgNativeViewBinder *_dropViews;
235
+ NgNativeViewBinder *_dragViews;
236
+ }
237
+
238
+ @synthesize callableJSModules = _callableJSModules;
239
+
240
+ RCT_EXPORT_MODULE()
241
+
242
+ + (BOOL)requiresMainQueueSetup
243
+ {
244
+ return YES;
245
+ }
246
+
247
+ - (dispatch_queue_t)methodQueue
248
+ {
249
+ return dispatch_get_main_queue();
250
+ }
251
+
252
+ - (instancetype)init
253
+ {
254
+ if (self = [super init]) {
255
+ _dragItems = [NSMutableDictionary dictionary];
256
+ __weak NgNativeDragAndDrop *weakSelf = self;
257
+ _dropViews = [[NgNativeViewBinder alloc]
258
+ initWithAttach:^id(UIView *view, NSString *nativeID) {
259
+ NgNativeDragAndDrop *module = weakSelf;
260
+ if (module == nil) {
261
+ return nil;
262
+ }
263
+ NgNativeDropTarget *target = [[NgNativeDropTarget alloc] initWithNativeID:nativeID module:module];
264
+ [view addInteraction:target.interaction];
265
+ return target;
266
+ }
267
+ detach:^(UIView *view, NgNativeDropTarget *target) {
268
+ [view removeInteraction:target.interaction];
269
+ }];
270
+ _dragViews = [[NgNativeViewBinder alloc]
271
+ initWithAttach:^id(UIView *view, NSString *nativeID) {
272
+ NgNativeDragAndDrop *module = weakSelf;
273
+ if (module == nil) {
274
+ return nil;
275
+ }
276
+ NgNativeDragSource *source = [[NgNativeDragSource alloc] initWithNativeID:nativeID module:module];
277
+ [view addInteraction:source.interaction];
278
+ return source;
279
+ }
280
+ detach:^(UIView *view, NgNativeDragSource *source) {
281
+ [view removeInteraction:source.interaction];
282
+ }];
283
+ }
284
+ return self;
285
+ }
286
+
287
+ // A reload makes a new module: this one's interactions go with it.
288
+ - (void)invalidate
289
+ {
290
+ dispatch_async(dispatch_get_main_queue(), ^{
291
+ [self->_dropViews unbindAll];
292
+ [self->_dragViews unbindAll];
293
+ [self->_dragItems removeAllObjects];
294
+ });
295
+ }
296
+
297
+ RCT_EXPORT_METHOD(setFileDrop : (NSString *)nativeID takes : (BOOL)takes)
298
+ {
299
+ if (!takesDrags() || nonEmptyString(nativeID) == nil) {
300
+ return;
301
+ }
302
+ takes ? [_dropViews bind:nativeID] : [_dropViews unbind:nativeID];
303
+ }
304
+
305
+ RCT_EXPORT_METHOD(setDragItem : (NSString *)nativeID item : (nullable NSDictionary *)item)
306
+ {
307
+ if (!takesDrags() || nonEmptyString(nativeID) == nil) {
308
+ return;
309
+ }
310
+ if ([item isKindOfClass:[NSDictionary class]]) {
311
+ _dragItems[nativeID] = item;
312
+ [_dragViews bind:nativeID];
313
+ } else {
314
+ [_dragItems removeObjectForKey:nativeID];
315
+ [_dragViews unbind:nativeID];
316
+ }
317
+ }
318
+
319
+ - (id)dropTargetFor:(NSString *)nativeID
320
+ {
321
+ return [_dropViews tokenFor:nativeID];
322
+ }
323
+
324
+ - (NSDictionary *)dragItemFor:(NSString *)nativeID
325
+ {
326
+ return _dragItems[nativeID];
327
+ }
328
+
329
+ - (void)send:(NSString *)type nativeID:(NSString *)nativeID drop:(NSDictionary *)drop
330
+ {
331
+ NSMutableDictionary *event = [@{
332
+ @"type" : type,
333
+ @"nativeID" : nativeID,
334
+ @"files" : @[],
335
+ @"text" : [NSNull null],
336
+ @"urls" : @[],
337
+ } mutableCopy];
338
+ [event addEntriesFromDictionary:drop ?: @{}];
339
+ [_callableJSModules invokeModule:@"RCTDeviceEventEmitter" method:@"emit" withArgs:@[ @"ngNativeDragAndDrop", event ]];
340
+ }
341
+
342
+ @end
@@ -0,0 +1,29 @@
1
+ // What a dropped item on iOS is, and the name its copy is kept under, apart from the module so the
2
+ // unit tests reach them without React Native.
3
+ #pragma once
4
+
5
+ #import <Foundation/Foundation.h>
6
+
7
+ NS_ASSUME_NONNULL_BEGIN
8
+
9
+ typedef NS_ENUM(NSInteger, NgNativeDropKind) {
10
+ NgNativeDropKindNone,
11
+ // A file, from the Files app or another app's document, whatever its contents: `files`.
12
+ NgNativeDropKindFile,
13
+ // A link that is not a file: `urls`.
14
+ NgNativeDropKindWebURL,
15
+ // Text that is not a file, a selection dragged from another app: `text`.
16
+ NgNativeDropKindText,
17
+ };
18
+
19
+ NgNativeDropKind NgNativeDropKindOf(NSItemProvider *provider);
20
+
21
+ // The type a file item is loaded as: its content's type, not a URL's.
22
+ NSString *_Nullable NgNativeDropFileType(NSItemProvider *provider);
23
+
24
+ // The name a dropped file's copy takes: the last part of the name the dragging app suggests, never
25
+ // a path out of the item's folder, `file` where that leaves nothing, with the type's extension
26
+ // where it has none.
27
+ NSString *NgNativeDropFileName(NSString *_Nullable suggestedName, NSString *_Nullable type);
28
+
29
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,55 @@
1
+ #import "NgNativeDropItems.h"
2
+
3
+ #import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
4
+
5
+ /** A file item: a file URL, or a file the dragging app lets the app open in place (the Files app). */
6
+ static BOOL isFile(NSItemProvider *provider)
7
+ {
8
+ if ([provider hasItemConformingToTypeIdentifier:UTTypeFileURL.identifier]) {
9
+ return YES;
10
+ }
11
+ NSString *type = NgNativeDropFileType(provider);
12
+ return type != nil &&
13
+ [provider hasRepresentationConformingToTypeIdentifier:type fileOptions:NSItemProviderFileOptionOpenInPlace];
14
+ }
15
+
16
+ NgNativeDropKind NgNativeDropKindOf(NSItemProvider *provider)
17
+ {
18
+ if (isFile(provider)) {
19
+ return NgNativeDropKindFile;
20
+ }
21
+ if ([provider hasItemConformingToTypeIdentifier:UTTypeURL.identifier] &&
22
+ [provider canLoadObjectOfClass:[NSURL class]]) {
23
+ return NgNativeDropKindWebURL;
24
+ }
25
+ if ([provider hasItemConformingToTypeIdentifier:UTTypePlainText.identifier] &&
26
+ [provider canLoadObjectOfClass:[NSString class]]) {
27
+ return NgNativeDropKindText;
28
+ }
29
+ return provider.registeredTypeIdentifiers.firstObject != nil ? NgNativeDropKindFile : NgNativeDropKindNone;
30
+ }
31
+
32
+ NSString *_Nullable NgNativeDropFileType(NSItemProvider *provider)
33
+ {
34
+ for (NSString *identifier in provider.registeredTypeIdentifiers) {
35
+ if (![[UTType typeWithIdentifier:identifier] conformsToType:UTTypeURL]) {
36
+ return identifier;
37
+ }
38
+ }
39
+ return provider.registeredTypeIdentifiers.firstObject;
40
+ }
41
+
42
+ NSString *NgNativeDropFileName(NSString *_Nullable suggestedName, NSString *_Nullable type)
43
+ {
44
+ // The dragging app names the file: `../x` would write outside the item's folder.
45
+ NSString *name = suggestedName.lastPathComponent;
46
+ if (name.length == 0 || [name isEqualToString:@"."] || [name isEqualToString:@".."] ||
47
+ [name isEqualToString:@"/"]) {
48
+ name = @"file";
49
+ }
50
+ NSString *extension = type != nil ? [UTType typeWithIdentifier:type].preferredFilenameExtension : nil;
51
+ if (extension.length > 0 && name.pathExtension.length == 0) {
52
+ name = [name stringByAppendingPathExtension:extension];
53
+ }
54
+ return name;
55
+ }
@@ -0,0 +1,40 @@
1
+ // Menus as the app sends them (`serializeMenu` in @ng-native/device: an `id` per item, `label`,
2
+ // `shortcut`, `disabled`, `checked`, `destructive`, `subtitle`, `sfSymbol`, `inline`, `items`, and
3
+ // `{ separator: true }` between groups) made into UIKit menu elements. A context menu and the iPad
4
+ // menu bar share it.
5
+ #pragma once
6
+
7
+ #import <UIKit/UIKit.h>
8
+
9
+ NS_ASSUME_NONNULL_BEGIN
10
+
11
+ // What choosing an item does: its id, sent back to the app.
12
+ typedef void (^NgNativeMenuChoose)(NSString *itemId);
13
+
14
+ // `entries` as menu elements. A separator splits the items around it into inline groups, as UIKit
15
+ // draws dividers. With `keyCommands`, an item with a shortcut becomes a UIKeyCommand with
16
+ // `action`, its id in `propertyList`, as a menu bar item; otherwise each item is a UIAction that
17
+ // calls `choose`. Role items the OS does itself (no `handled`) are left out: UIKit's own menus
18
+ // already have them.
19
+ NSArray<UIMenuElement *> *NgNativeMenuElements(
20
+ NSArray *entries,
21
+ NgNativeMenuChoose choose,
22
+ SEL _Nullable keyCommandAction);
23
+
24
+ // A shortcut (`{ key, meta, ctrl, alt, shift }`) as UIKeyCommand's input and modifier flags. Nil
25
+ // for a key UIKit cannot name.
26
+ NSString *_Nullable NgNativeKeyInput(NSDictionary *shortcut, UIKeyModifierFlags *flags);
27
+
28
+ // A key command's input and modifier flags as one string, for telling chords apart.
29
+ NSString *NgNativeKeyChord(NSString *input, UIKeyModifierFlags flags);
30
+
31
+ // Adds to `chords` the chord of every key command in `element` and under it.
32
+ void NgNativeCollectKeyChords(UIMenuElement *element, NSMutableSet<NSString *> *chords);
33
+
34
+ // `elements` with each key command whose chord is in `taken` made a plain command with the same
35
+ // title, action and id but no keys, and the chords of the others added to `taken`. UIKit's main
36
+ // menu throws on a key command whose chord it already has, which an app item with a standard role
37
+ // (Find on mod+f) or two items with one chord would otherwise insert.
38
+ NSArray<UIMenuElement *> *NgNativeWithoutTakenKeys(NSArray<UIMenuElement *> *elements, NSMutableSet<NSString *> *taken);
39
+
40
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,217 @@
1
+ #import "NgNativeMenuBuilding.h"
2
+
3
+ static NSString *stringOf(id value)
4
+ {
5
+ return [value isKindOfClass:[NSString class]] ? value : nil;
6
+ }
7
+
8
+ static BOOL flagOf(id value)
9
+ {
10
+ return [value isKindOfClass:[NSNumber class]] && [value boolValue];
11
+ }
12
+
13
+ NSString *_Nullable NgNativeKeyInput(NSDictionary *shortcut, UIKeyModifierFlags *flags)
14
+ {
15
+ NSString *key = stringOf(shortcut[@"key"]);
16
+ if (key.length == 0) {
17
+ return nil;
18
+ }
19
+ UIKeyModifierFlags modifiers = 0;
20
+ if (flagOf(shortcut[@"meta"])) modifiers |= UIKeyModifierCommand;
21
+ if (flagOf(shortcut[@"ctrl"])) modifiers |= UIKeyModifierControl;
22
+ if (flagOf(shortcut[@"alt"])) modifiers |= UIKeyModifierAlternate;
23
+ if (flagOf(shortcut[@"shift"])) modifiers |= UIKeyModifierShift;
24
+ *flags = modifiers;
25
+ if (key.length == 1) {
26
+ return key.lowercaseString;
27
+ }
28
+ // KeyboardEvent.key names, as parseShortcut keeps them.
29
+ static NSDictionary<NSString *, NSString *> *named;
30
+ static dispatch_once_t once;
31
+ dispatch_once(&once, ^{
32
+ NSMutableDictionary *keys = [@{
33
+ @"ArrowUp" : UIKeyInputUpArrow,
34
+ @"ArrowDown" : UIKeyInputDownArrow,
35
+ @"ArrowLeft" : UIKeyInputLeftArrow,
36
+ @"ArrowRight" : UIKeyInputRightArrow,
37
+ @"Escape" : UIKeyInputEscape,
38
+ @"Enter" : @"\r",
39
+ @"Tab" : @"\t",
40
+ @"Backspace" : @"\b",
41
+ @"Space" : @" ",
42
+ @"PageUp" : UIKeyInputPageUp,
43
+ @"PageDown" : UIKeyInputPageDown,
44
+ @"Home" : UIKeyInputHome,
45
+ @"End" : UIKeyInputEnd,
46
+ @"Delete" : UIKeyInputDelete,
47
+ } mutableCopy];
48
+ NSArray<NSString *> *functionKeys = @[
49
+ UIKeyInputF1, UIKeyInputF2, UIKeyInputF3, UIKeyInputF4, UIKeyInputF5, UIKeyInputF6,
50
+ UIKeyInputF7, UIKeyInputF8, UIKeyInputF9, UIKeyInputF10, UIKeyInputF11, UIKeyInputF12,
51
+ ];
52
+ [functionKeys enumerateObjectsUsingBlock:^(NSString *input, NSUInteger index, BOOL *stop) {
53
+ keys[[NSString stringWithFormat:@"F%lu", (unsigned long)index + 1]] = input;
54
+ }];
55
+ named = keys;
56
+ });
57
+ return named[key];
58
+ }
59
+
60
+ static UIMenuElementAttributes attributesOf(NSDictionary *entry)
61
+ {
62
+ UIMenuElementAttributes attributes = 0;
63
+ if (flagOf(entry[@"disabled"])) attributes |= UIMenuElementAttributesDisabled;
64
+ if (flagOf(entry[@"destructive"])) attributes |= UIMenuElementAttributesDestructive;
65
+ return attributes;
66
+ }
67
+
68
+ static UIImage *_Nullable imageOf(NSDictionary *entry)
69
+ {
70
+ NSString *symbol = stringOf(entry[@"sfSymbol"]);
71
+ return symbol.length > 0 ? [UIImage systemImageNamed:symbol] : nil;
72
+ }
73
+
74
+ static UIMenuElement *_Nullable elementOf(NSDictionary *entry, NgNativeMenuChoose choose, SEL keyCommandAction)
75
+ {
76
+ NSString *itemId = stringOf(entry[@"id"]) ?: @"";
77
+ NSString *label = stringOf(entry[@"label"]) ?: @"";
78
+ NSArray *items = [entry[@"items"] isKindOfClass:[NSArray class]] ? entry[@"items"] : nil;
79
+ if (items != nil) {
80
+ UIMenuOptions options = flagOf(entry[@"inline"]) ? UIMenuOptionsDisplayInline : 0;
81
+ return [UIMenu menuWithTitle:label
82
+ image:imageOf(entry)
83
+ identifier:nil
84
+ options:options
85
+ children:NgNativeMenuElements(items, choose, keyCommandAction)];
86
+ }
87
+ NSString *role = stringOf(entry[@"menuRole"]);
88
+ if (role != nil && !flagOf(entry[@"handled"])) {
89
+ // Back and Forward have no UIKit command: the menu bar sends them as `navigate`.
90
+ if (keyCommandAction != NULL && ([role isEqualToString:@"back"] || [role isEqualToString:@"forward"])) {
91
+ itemId = [@"navigate:" stringByAppendingString:role];
92
+ } else {
93
+ // A role the OS fills itself, such as Copy: UIKit's own menus have it.
94
+ return nil;
95
+ }
96
+ }
97
+ NSDictionary *shortcut = [entry[@"shortcut"] isKindOfClass:[NSDictionary class]] ? entry[@"shortcut"] : nil;
98
+ UIKeyModifierFlags flags = 0;
99
+ NSString *input = shortcut != nil ? NgNativeKeyInput(shortcut, &flags) : nil;
100
+ UIMenuElement *element;
101
+ if (keyCommandAction != NULL) {
102
+ UICommand *command = input != nil
103
+ ? [UIKeyCommand commandWithTitle:label
104
+ image:imageOf(entry)
105
+ action:keyCommandAction
106
+ input:input
107
+ modifierFlags:flags
108
+ propertyList:itemId]
109
+ : [UICommand commandWithTitle:label image:imageOf(entry) action:keyCommandAction propertyList:itemId];
110
+ command.attributes = attributesOf(entry);
111
+ command.state = flagOf(entry[@"checked"]) ? UIMenuElementStateOn : UIMenuElementStateOff;
112
+ command.discoverabilityTitle = label;
113
+ element = command;
114
+ } else {
115
+ UIAction *action = [UIAction actionWithTitle:label
116
+ image:imageOf(entry)
117
+ identifier:nil
118
+ handler:^(__kindof UIAction *chosen) {
119
+ choose(itemId);
120
+ }];
121
+ action.attributes = attributesOf(entry);
122
+ action.state = flagOf(entry[@"checked"]) ? UIMenuElementStateOn : UIMenuElementStateOff;
123
+ element = action;
124
+ }
125
+ NSString *subtitle = stringOf(entry[@"subtitle"]);
126
+ if (subtitle.length > 0) {
127
+ element.subtitle = subtitle;
128
+ }
129
+ return element;
130
+ }
131
+
132
+ NSArray<UIMenuElement *> *NgNativeMenuElements(NSArray *entries, NgNativeMenuChoose choose, SEL keyCommandAction)
133
+ {
134
+ NSMutableArray<NSArray<UIMenuElement *> *> *groups = [NSMutableArray arrayWithObject:@[]];
135
+ for (id entry in entries) {
136
+ if (![entry isKindOfClass:[NSDictionary class]]) {
137
+ continue;
138
+ }
139
+ if (flagOf(entry[@"separator"])) {
140
+ if (groups.lastObject.count > 0) {
141
+ [groups addObject:@[]];
142
+ }
143
+ continue;
144
+ }
145
+ UIMenuElement *element = elementOf(entry, choose, keyCommandAction);
146
+ if (element != nil) {
147
+ groups[groups.count - 1] = [groups.lastObject arrayByAddingObject:element];
148
+ }
149
+ }
150
+ if (groups.lastObject.count == 0) {
151
+ [groups removeLastObject];
152
+ }
153
+ if (groups.count <= 1) {
154
+ return groups.firstObject ?: @[];
155
+ }
156
+ NSMutableArray<UIMenuElement *> *elements = [NSMutableArray array];
157
+ for (NSArray<UIMenuElement *> *group in groups) {
158
+ [elements addObject:[UIMenu menuWithTitle:@"" image:nil identifier:nil options:UIMenuOptionsDisplayInline children:group]];
159
+ }
160
+ return elements;
161
+ }
162
+
163
+ NSString *NgNativeKeyChord(NSString *input, UIKeyModifierFlags flags)
164
+ {
165
+ return [NSString stringWithFormat:@"%ld %@", (long)flags, input.lowercaseString];
166
+ }
167
+
168
+ void NgNativeCollectKeyChords(UIMenuElement *element, NSMutableSet<NSString *> *chords)
169
+ {
170
+ if ([element isKindOfClass:[UIKeyCommand class]]) {
171
+ UIKeyCommand *command = (UIKeyCommand *)element;
172
+ if (command.input != nil) {
173
+ [chords addObject:NgNativeKeyChord(command.input, command.modifierFlags)];
174
+ }
175
+ } else if ([element isKindOfClass:[UIMenu class]]) {
176
+ for (UIMenuElement *child in ((UIMenu *)element).children) {
177
+ NgNativeCollectKeyChords(child, chords);
178
+ }
179
+ }
180
+ }
181
+
182
+ /** A key command as a plain command, keeping everything but its keys. */
183
+ static UICommand *withoutKeys(UIKeyCommand *command)
184
+ {
185
+ UICommand *plain = [UICommand commandWithTitle:command.title
186
+ image:command.image
187
+ action:command.action
188
+ propertyList:command.propertyList];
189
+ plain.attributes = command.attributes;
190
+ plain.state = command.state;
191
+ plain.discoverabilityTitle = command.discoverabilityTitle;
192
+ plain.subtitle = command.subtitle;
193
+ return plain;
194
+ }
195
+
196
+ NSArray<UIMenuElement *> *NgNativeWithoutTakenKeys(NSArray<UIMenuElement *> *elements, NSMutableSet<NSString *> *taken)
197
+ {
198
+ NSMutableArray<UIMenuElement *> *out = [NSMutableArray arrayWithCapacity:elements.count];
199
+ for (UIMenuElement *element in elements) {
200
+ if ([element isKindOfClass:[UIKeyCommand class]]) {
201
+ UIKeyCommand *command = (UIKeyCommand *)element;
202
+ NSString *chord = command.input != nil ? NgNativeKeyChord(command.input, command.modifierFlags) : nil;
203
+ if (chord == nil || [taken containsObject:chord]) {
204
+ [out addObject:withoutKeys(command)];
205
+ continue;
206
+ }
207
+ [taken addObject:chord];
208
+ [out addObject:command];
209
+ } else if ([element isKindOfClass:[UIMenu class]]) {
210
+ UIMenu *menu = (UIMenu *)element;
211
+ [out addObject:[menu menuByReplacingChildren:NgNativeWithoutTakenKeys(menu.children, taken)]];
212
+ } else {
213
+ [out addObject:element];
214
+ }
215
+ }
216
+ return out;
217
+ }