@ng-native/components 0.22.2 → 0.24.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 (139) hide show
  1. package/android/build.gradle +22 -0
  2. package/android/src/main/java/dev/ngnative/components/ColorSchemeModule.kt +38 -0
  3. package/android/src/main/java/dev/ngnative/components/ContextMenuAccessibility.kt +71 -0
  4. package/android/src/main/java/dev/ngnative/components/ContextMenuGesture.kt +161 -0
  5. package/android/src/main/java/dev/ngnative/components/ContextMenuPopup.kt +197 -0
  6. package/android/src/main/java/dev/ngnative/components/MenuHost.kt +45 -0
  7. package/android/src/main/java/dev/ngnative/components/MenuWindowCallback.kt +76 -0
  8. package/android/src/main/java/dev/ngnative/components/MenusModule.kt +85 -0
  9. package/android/src/main/java/dev/ngnative/components/Shortcut.kt +144 -0
  10. package/android/src/main/java/dev/ngnative/components/WindowWrapper.kt +22 -0
  11. package/dist/activity-indicator.d.ts +4 -1
  12. package/dist/activity-indicator.js +6 -2
  13. package/dist/activity-indicator.js.map +1 -1
  14. package/dist/context-menu.d.ts +52 -0
  15. package/dist/context-menu.js +101 -0
  16. package/dist/context-menu.js.map +1 -0
  17. package/dist/drag-and-drop.d.ts +52 -0
  18. package/dist/drag-and-drop.js +112 -0
  19. package/dist/drag-and-drop.js.map +1 -0
  20. package/dist/gesture-backend.d.ts +32 -2
  21. package/dist/gesture-backend.js.map +1 -1
  22. package/dist/gesture-dispatch.d.ts +8 -1
  23. package/dist/gesture-dispatch.js +60 -16
  24. package/dist/gesture-dispatch.js.map +1 -1
  25. package/dist/gesture-interaction.d.ts +13 -0
  26. package/dist/gesture-interaction.js +97 -0
  27. package/dist/gesture-interaction.js.map +1 -0
  28. package/dist/gesture-tracking.d.ts +18 -0
  29. package/dist/gesture-tracking.js +28 -0
  30. package/dist/gesture-tracking.js.map +1 -0
  31. package/dist/gestures-web.d.ts +3 -1
  32. package/dist/gestures-web.js +5 -2
  33. package/dist/gestures-web.js.map +1 -1
  34. package/dist/gestures.d.ts +1 -0
  35. package/dist/gestures.js +45 -3
  36. package/dist/gestures.js.map +1 -1
  37. package/dist/image.d.ts +1 -1
  38. package/dist/index.d.ts +12 -1
  39. package/dist/index.js +12 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/interaction-observer.d.ts +11 -0
  42. package/dist/interaction-observer.js +12 -0
  43. package/dist/interaction-observer.js.map +1 -0
  44. package/dist/keyboard-avoiding-view.js +3 -3
  45. package/dist/keyboard-avoiding-view.js.map +1 -1
  46. package/dist/markdown-stream.d.ts +3 -1
  47. package/dist/markdown-stream.js +14 -2
  48. package/dist/markdown-stream.js.map +1 -1
  49. package/dist/markdown.d.ts +2 -0
  50. package/dist/markdown.js +5 -3
  51. package/dist/markdown.js.map +1 -1
  52. package/dist/menu.d.ts +71 -0
  53. package/dist/menu.js +193 -0
  54. package/dist/menu.js.map +1 -0
  55. package/dist/modal.d.ts +30 -1
  56. package/dist/modal.js +67 -6
  57. package/dist/modal.js.map +1 -1
  58. package/dist/native-button.d.ts +54 -0
  59. package/dist/native-button.js +99 -0
  60. package/dist/native-button.js.map +1 -0
  61. package/dist/native-gesture.d.ts +9 -1
  62. package/dist/native-gesture.js +32 -3
  63. package/dist/native-gesture.js.map +1 -1
  64. package/dist/native-id.d.ts +10 -0
  65. package/dist/native-id.js +70 -0
  66. package/dist/native-id.js.map +1 -0
  67. package/dist/pressable.d.ts +36 -0
  68. package/dist/pressable.js +106 -10
  69. package/dist/pressable.js.map +1 -1
  70. package/dist/safe-area-provider.js +15 -8
  71. package/dist/safe-area-provider.js.map +1 -1
  72. package/dist/scroll-view-props.d.ts +7 -1
  73. package/dist/scroll-view-props.js +54 -3
  74. package/dist/scroll-view-props.js.map +1 -1
  75. package/dist/section-list.js +2 -2
  76. package/dist/section-list.js.map +1 -1
  77. package/dist/shortcut.d.ts +21 -0
  78. package/dist/shortcut.js +34 -0
  79. package/dist/shortcut.js.map +1 -0
  80. package/dist/sidebar-item.d.ts +53 -0
  81. package/dist/sidebar-item.js +152 -0
  82. package/dist/sidebar-item.js.map +1 -0
  83. package/dist/sidebar-section.d.ts +21 -0
  84. package/dist/sidebar-section.js +74 -0
  85. package/dist/sidebar-section.js.map +1 -0
  86. package/dist/sidebar.d.ts +47 -0
  87. package/dist/sidebar.js +110 -0
  88. package/dist/sidebar.js.map +1 -0
  89. package/dist/split-view.d.ts +63 -0
  90. package/dist/split-view.js +121 -0
  91. package/dist/split-view.js.map +1 -0
  92. package/dist/switch.d.ts +9 -1
  93. package/dist/switch.js +23 -2
  94. package/dist/switch.js.map +1 -1
  95. package/dist/text-input.d.ts +8 -3
  96. package/dist/text-input.js +10 -4
  97. package/dist/text-input.js.map +1 -1
  98. package/dist/transforms.d.ts +5 -0
  99. package/dist/transforms.js +7 -0
  100. package/dist/transforms.js.map +1 -1
  101. package/dist/tray-icon.d.ts +18 -0
  102. package/dist/tray-icon.js +46 -0
  103. package/dist/tray-icon.js.map +1 -0
  104. package/dist/view-base.d.ts +42 -1
  105. package/dist/view-base.js +67 -39
  106. package/dist/view-base.js.map +1 -1
  107. package/dist/virtual-list.js +12 -1
  108. package/dist/virtual-list.js.map +1 -1
  109. package/dist/written-props.js +25 -0
  110. package/dist/written-props.js.map +1 -1
  111. package/expo-module.config.json +6 -0
  112. package/ios/NgNativeButtonComponentView.mm +132 -0
  113. package/ios/NgNativeButtonMetrics.h +28 -0
  114. package/ios/NgNativeButtonMetrics.mm +68 -0
  115. package/ios/NgNativeButtonShadowNode.h +65 -0
  116. package/ios/NgNativeButtonShadowNode.mm +40 -0
  117. package/ios/NgNativeComponents.podspec +28 -0
  118. package/ios/NgNativeDragAndDrop.mm +342 -0
  119. package/ios/NgNativeDropItems.h +29 -0
  120. package/ios/NgNativeDropItems.mm +55 -0
  121. package/ios/NgNativeMenuBuilding.h +40 -0
  122. package/ios/NgNativeMenuBuilding.mm +217 -0
  123. package/ios/NgNativeMenus.mm +320 -0
  124. package/ios/NgNativeResponderHooks.h +53 -0
  125. package/ios/NgNativeResponderHooks.mm +215 -0
  126. package/ios/NgNativeSceneKeys.h +36 -0
  127. package/ios/NgNativeSceneKeys.mm +56 -0
  128. package/ios/NgNativeSplitView.mm +433 -0
  129. package/ios/NgNativeSplitViewDelegate.h +29 -0
  130. package/ios/NgNativeSplitViewDelegate.mm +122 -0
  131. package/ios/NgNativeSplitViewShadowNodes.h +115 -0
  132. package/ios/NgNativeSplitViewShadowNodes.mm +31 -0
  133. package/ios/NgNativeUndo.mm +107 -0
  134. package/ios/NgNativeUndoSteps.h +17 -0
  135. package/ios/NgNativeUndoSteps.mm +22 -0
  136. package/ios/NgNativeViewBinder.h +54 -0
  137. package/ios/NgNativeViewBinder.mm +225 -0
  138. package/ios/NgNativeWindows.mm +1126 -0
  139. package/package.json +13 -5
@@ -0,0 +1,31 @@
1
+ #include "NgNativeSplitViewShadowNodes.h"
2
+
3
+ #import <react/renderer/core/propsConversions.h>
4
+
5
+ namespace facebook::react {
6
+
7
+ extern const char NgNativeSplitViewComponentName[] = "NgNativeSplitView";
8
+ extern const char NgNativeSidebarComponentName[] = "NgNativeSidebar";
9
+ extern const char NgNativeSplitContentComponentName[] = "NgNativeSplitContent";
10
+
11
+ NgNativeSidebarProps::NgNativeSidebarProps(
12
+ const PropsParserContext &context,
13
+ const NgNativeSidebarProps &sourceProps,
14
+ const RawProps &rawProps)
15
+ : ViewProps(context, sourceProps, rawProps),
16
+ collapsed(convertRawProp(context, rawProps, "collapsed", sourceProps.collapsed, false)),
17
+ paneMinWidth(convertRawProp(context, rawProps, "paneMinWidth", sourceProps.paneMinWidth, (Float)0)),
18
+ paneMaxWidth(convertRawProp(context, rawProps, "paneMaxWidth", sourceProps.paneMaxWidth, (Float)0))
19
+ {
20
+ }
21
+
22
+ void NgNativeSidebarEventEmitter::onSidebarCollapse(bool collapsed) const
23
+ {
24
+ dispatchEvent("sidebarCollapse", [collapsed](jsi::Runtime &runtime) {
25
+ auto payload = jsi::Object(runtime);
26
+ payload.setProperty(runtime, "collapsed", collapsed);
27
+ return payload;
28
+ });
29
+ }
30
+
31
+ } // namespace facebook::react
@@ -0,0 +1,107 @@
1
+ #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTInvalidating.h>
3
+ #import <UIKit/UIKit.h>
4
+
5
+ #import "NgNativeResponderHooks.h"
6
+ #import "NgNativeUndoSteps.h"
7
+
8
+ /**
9
+ The `NgNativeUndo` module on iOS (`NativeUndo` in @ng-native/device), over one NSUndoManager that
10
+ every window answers as its `undoManager`: shake to undo, mod+z and mod+shift+z from a hardware
11
+ keyboard, and Edit > Undo and Redo on an iPad reach the app's actions while no text input, or
12
+ other view with a history of its own, is first responder.
13
+ - `register(id, label)` registers an action named `label`. Undoing it sends
14
+ `{ type: 'undo', id }` on `ngNativeUndo` and registers its redo under the same id; redoing sends
15
+ `{ type: 'redo', id }`.
16
+ - `undo()`, `redo()` and `clear()` are Edit > Undo, Edit > Redo and removing every action.
17
+ */
18
+ @interface NgNativeUndo : NSObject <RCTBridgeModule, RCTInvalidating, NgNativeUndoHost>
19
+ @end
20
+
21
+ @implementation NgNativeUndo {
22
+ NSUndoManager *_manager;
23
+ }
24
+
25
+ @synthesize callableJSModules = _callableJSModules;
26
+
27
+ RCT_EXPORT_MODULE()
28
+
29
+ + (BOOL)requiresMainQueueSetup
30
+ {
31
+ return YES;
32
+ }
33
+
34
+ - (dispatch_queue_t)methodQueue
35
+ {
36
+ return dispatch_get_main_queue();
37
+ }
38
+
39
+ - (instancetype)init
40
+ {
41
+ if (self = [super init]) {
42
+ _manager = NgNativeMakeUndoManager();
43
+ dispatch_async(dispatch_get_main_queue(), ^{
44
+ NgNativeInstallResponderHooks();
45
+ NgNativeSetUndoHost(self);
46
+ });
47
+ }
48
+ return self;
49
+ }
50
+
51
+ - (void)invalidate
52
+ {
53
+ // A reload starts the history again; the app that registered it is gone. Only the app's own
54
+ // actions are this module's to remove.
55
+ dispatch_async(dispatch_get_main_queue(), ^{
56
+ [self->_manager removeAllActionsWithTarget:self];
57
+ });
58
+ }
59
+
60
+ - (NSUndoManager *)sharedUndoManager
61
+ {
62
+ return _manager;
63
+ }
64
+
65
+ - (void)send:(NSString *)type itemId:(NSString *)itemId
66
+ {
67
+ [_callableJSModules invokeModule:@"RCTDeviceEventEmitter"
68
+ method:@"emit"
69
+ withArgs:@[ @"ngNativeUndo", @{@"type" : type, @"id" : itemId} ]];
70
+ }
71
+
72
+ // One step: undoing it sends `undo` and registers the redo, which sends `redo` and registers the
73
+ // undo again. NSUndoManager puts a registration made while undoing on the redo stack.
74
+ - (void)registerStep:(NSString *)itemId label:(NSString *)label
75
+ {
76
+ NgNativeRegisterUndoStep(_manager, self, label, ^(NgNativeUndo *target) {
77
+ NSString *type = target->_manager.isUndoing ? @"undo" : @"redo";
78
+ [target send:type itemId:itemId];
79
+ [target registerStep:itemId label:label];
80
+ });
81
+ }
82
+
83
+ RCT_EXPORT_METHOD(register : (NSString *)itemId label : (NSString *)label)
84
+ {
85
+ [self registerStep:itemId label:label ?: @""];
86
+ }
87
+
88
+ RCT_EXPORT_METHOD(undo)
89
+ {
90
+ if (_manager.canUndo) {
91
+ [_manager undo];
92
+ }
93
+ }
94
+
95
+ RCT_EXPORT_METHOD(redo)
96
+ {
97
+ if (_manager.canRedo) {
98
+ [_manager redo];
99
+ }
100
+ }
101
+
102
+ RCT_EXPORT_METHOD(clear)
103
+ {
104
+ [_manager removeAllActionsWithTarget:self];
105
+ }
106
+
107
+ @end
@@ -0,0 +1,17 @@
1
+ // The undo manager behind `UndoStack` on iOS and how an action goes on it, apart from the module so
2
+ // the unit tests reach them without React Native.
3
+ #pragma once
4
+
5
+ #import <Foundation/Foundation.h>
6
+
7
+ NS_ASSUME_NONNULL_BEGIN
8
+
9
+ // An undo manager on which each of the app's actions is an undo step of its own, whenever it is
10
+ // registered: two in one pass of the run loop are still two steps.
11
+ NSUndoManager *NgNativeMakeUndoManager(void);
12
+
13
+ // Register `handler` as one step named `label`. One registered while undoing or redoing goes into
14
+ // the group NSUndoManager has open for that, on the other stack.
15
+ void NgNativeRegisterUndoStep(NSUndoManager *manager, id target, NSString *label, void (^handler)(id target));
16
+
17
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,22 @@
1
+ #import "NgNativeUndoSteps.h"
2
+
3
+ NSUndoManager *NgNativeMakeUndoManager(void)
4
+ {
5
+ NSUndoManager *manager = [NSUndoManager new];
6
+ // Each of the app's actions is a group of its own, whenever it is registered.
7
+ manager.groupsByEvent = NO;
8
+ return manager;
9
+ }
10
+
11
+ void NgNativeRegisterUndoStep(NSUndoManager *manager, id target, NSString *label, void (^handler)(id target))
12
+ {
13
+ BOOL own = !manager.isUndoing && !manager.isRedoing;
14
+ if (own) {
15
+ [manager beginUndoGrouping];
16
+ }
17
+ [manager registerUndoWithTarget:target handler:handler];
18
+ [manager setActionName:label];
19
+ if (own) {
20
+ [manager endUndoGrouping];
21
+ }
22
+ }
@@ -0,0 +1,54 @@
1
+ // React Native's views found by `nativeID`, for a module that adds a UIKit interaction to a view
2
+ // it does not own: a context menu, a drop or a drag. The directive names the view; the binder finds
3
+ // it once it is mounted and lets it go as it is unmounted, recycled for another element, or given
4
+ // another `nativeID`. React Native's view is never replaced or subclassed. A legacy module is
5
+ // handed no surface presenter in bridgeless mode, so the binder follows mounts by checking its
6
+ // views each time the main run loop finishes its work, while it has any. A view not found is looked
7
+ // for again at once after a bind or a detach, and then less and less often while it stays missing
8
+ // (a screen mounted but not in a window), up to every two seconds.
9
+ #pragma once
10
+
11
+ #import <UIKit/UIKit.h>
12
+
13
+ NS_ASSUME_NONNULL_BEGIN
14
+
15
+ // Called with the view found for `nativeID`; what it answers is handed back to `detach`.
16
+ typedef id _Nullable (^NgNativeViewAttach)(UIView *view, NSString *nativeID);
17
+ typedef void (^NgNativeViewDetach)(UIView *view, id token);
18
+
19
+ @interface NgNativeViewBinder : NSObject
20
+
21
+ - (instancetype)initWithAttach:(NgNativeViewAttach)attach detach:(NgNativeViewDetach)detach;
22
+ - (instancetype)init NS_UNAVAILABLE;
23
+
24
+ // The views searched for a missing `nativeID`: every window of every connected scene, unless set.
25
+ @property (nonatomic, copy, null_resettable) NSArray<UIView *> * (^roots)(void);
26
+ // How long the binder now waits between searches for a view it has not found, in seconds.
27
+ @property (nonatomic, readonly) NSTimeInterval searchInterval;
28
+ // How many searches it has made.
29
+ @property (nonatomic, readonly) NSUInteger searches;
30
+
31
+ // Keep the view with `nativeID` attached, once it is mounted. Main thread.
32
+ - (void)bind:(NSString *)nativeID;
33
+ // Detach the view with `nativeID`, and stop looking for it. Main thread.
34
+ - (void)unbind:(NSString *)nativeID;
35
+ // What attaching the view now bound to `nativeID` answered, or nil while none is: each attach
36
+ // answers its own, so a caller can tell the binding it started with from a later one of the same
37
+ // name. Main thread.
38
+ - (nullable id)tokenFor:(NSString *)nativeID;
39
+ // Detach every view. Main thread.
40
+ - (void)unbindAll;
41
+ // Check each view still is the one with its `nativeID`, and look for the ones not found yet. Main
42
+ // thread.
43
+ - (void)refresh;
44
+
45
+ @end
46
+
47
+ // The `nativeID` React Native's view has, or nil for any other view.
48
+ NSString *_Nullable NgNativeNativeIdOf(UIView *view);
49
+
50
+ // Whether a long press on `touched` is taken by a view on its way up to `menuView` (not
51
+ // `menuView` itself) whose `nativeID` is in `takers`: one with its own `(longPress)`.
52
+ BOOL NgNativeLongPressTaken(UIView *_Nullable touched, UIView *menuView, NSSet<NSString *> *takers);
53
+
54
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,225 @@
1
+ #import "NgNativeViewBinder.h"
2
+
3
+ #import <QuartzCore/QuartzCore.h>
4
+ #import <React/RCTViewComponentView.h>
5
+
6
+ /**
7
+ How long a binder waits between looks for views it has not found yet, in seconds: at first, and
8
+ at most, after looks that found nothing.
9
+ */
10
+ static const CFTimeInterval FirstSearchInterval = 0.25;
11
+ static const CFTimeInterval LongestSearchInterval = 2;
12
+
13
+ /** A view found for a `nativeID`, and what attaching it answered. */
14
+ @interface NgNativeBinding : NSObject
15
+ @property (nonatomic, weak) UIView *view;
16
+ @property (nonatomic, strong) id token;
17
+ @end
18
+
19
+ @implementation NgNativeBinding
20
+ @end
21
+
22
+ /**
23
+ Whether `binding` is a found view still mounted under `nativeID`: not unmounted (a recycled view is
24
+ taken out first), and not another element's now.
25
+ */
26
+ static BOOL isValid(id binding, NSString *nativeID)
27
+ {
28
+ if (![binding isKindOfClass:[NgNativeBinding class]]) {
29
+ return NO;
30
+ }
31
+ UIView *view = ((NgNativeBinding *)binding).view;
32
+ return view != nil && view.superview != nil && [NgNativeNativeIdOf(view) isEqualToString:nativeID];
33
+ }
34
+
35
+ NSString *NgNativeNativeIdOf(UIView *view)
36
+ {
37
+ return [view isKindOfClass:[RCTViewComponentView class]] ? ((RCTViewComponentView *)view).nativeId : nil;
38
+ }
39
+
40
+ BOOL NgNativeLongPressTaken(UIView *touched, UIView *menuView, NSSet<NSString *> *takers)
41
+ {
42
+ if (takers.count == 0) {
43
+ return NO;
44
+ }
45
+ for (UIView *view = touched; view != nil && view != menuView; view = view.superview) {
46
+ NSString *nativeID = NgNativeNativeIdOf(view);
47
+ if (nativeID != nil && [takers containsObject:nativeID]) {
48
+ return YES;
49
+ }
50
+ }
51
+ return NO;
52
+ }
53
+
54
+ /** Each view under `view` whose `nativeID` is in `wanted`, the first one found for each. */
55
+ static void collect(UIView *view, NSSet<NSString *> *wanted, NSMutableDictionary<NSString *, UIView *> *found)
56
+ {
57
+ NSString *nativeID = NgNativeNativeIdOf(view);
58
+ if (nativeID != nil && found[nativeID] == nil && [wanted containsObject:nativeID]) {
59
+ found[nativeID] = view;
60
+ }
61
+ for (UIView *subview in view.subviews) {
62
+ if (found.count == wanted.count) {
63
+ return;
64
+ }
65
+ collect(subview, wanted, found);
66
+ }
67
+ }
68
+
69
+ @implementation NgNativeViewBinder {
70
+ NgNativeViewAttach _attach;
71
+ NgNativeViewDetach _detach;
72
+ // Every `nativeID` asked for, with its view once found (NSNull until then).
73
+ NSMutableDictionary<NSString *, id> *_bindings;
74
+ // Runs `refresh` each time the main run loop finishes its work, so after every mount, while
75
+ // there is anything to bind.
76
+ CFRunLoopObserverRef _observer;
77
+ CFTimeInterval _searchedAt;
78
+ }
79
+
80
+ @synthesize roots = _roots;
81
+
82
+ - (instancetype)initWithAttach:(NgNativeViewAttach)attach detach:(NgNativeViewDetach)detach
83
+ {
84
+ if (self = [super init]) {
85
+ _attach = [attach copy];
86
+ _detach = [detach copy];
87
+ _bindings = [NSMutableDictionary dictionary];
88
+ _searchInterval = FirstSearchInterval;
89
+ }
90
+ return self;
91
+ }
92
+
93
+ - (NSArray<UIView *> * (^)(void))roots
94
+ {
95
+ if (_roots != nil) {
96
+ return _roots;
97
+ }
98
+ return ^NSArray<UIView *> * {
99
+ NSMutableArray<UIView *> *windows = [NSMutableArray array];
100
+ for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
101
+ if ([scene isKindOfClass:[UIWindowScene class]]) {
102
+ [windows addObjectsFromArray:((UIWindowScene *)scene).windows];
103
+ }
104
+ }
105
+ return windows;
106
+ };
107
+ }
108
+
109
+ /** Look at once, and often, for what changed. */
110
+ - (void)searchSoon
111
+ {
112
+ _searchedAt = 0;
113
+ _searchInterval = FirstSearchInterval;
114
+ }
115
+
116
+ - (void)dealloc
117
+ {
118
+ [self stopObserving];
119
+ }
120
+
121
+ - (void)bind:(NSString *)nativeID
122
+ {
123
+ if (_bindings[nativeID] == nil) {
124
+ _bindings[nativeID] = [NSNull null];
125
+ }
126
+ // Looked for as the run loop finishes this pass, after a mount already on its way.
127
+ [self searchSoon];
128
+ [self startObserving];
129
+ }
130
+
131
+ - (void)unbind:(NSString *)nativeID
132
+ {
133
+ [self detach:nativeID];
134
+ [_bindings removeObjectForKey:nativeID];
135
+ if (_bindings.count == 0) {
136
+ [self stopObserving];
137
+ }
138
+ }
139
+
140
+ - (id)tokenFor:(NSString *)nativeID
141
+ {
142
+ id binding = _bindings[nativeID];
143
+ return isValid(binding, nativeID) ? ((NgNativeBinding *)binding).token : nil;
144
+ }
145
+
146
+ - (void)unbindAll
147
+ {
148
+ for (NSString *nativeID in _bindings.allKeys) {
149
+ [self unbind:nativeID];
150
+ }
151
+ }
152
+
153
+ - (void)startObserving
154
+ {
155
+ if (_observer != NULL) {
156
+ return;
157
+ }
158
+ __weak NgNativeViewBinder *weakSelf = self;
159
+ _observer = CFRunLoopObserverCreateWithHandler(
160
+ kCFAllocatorDefault, kCFRunLoopBeforeWaiting, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
161
+ [weakSelf refresh];
162
+ });
163
+ CFRunLoopAddObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes);
164
+ }
165
+
166
+ - (void)stopObserving
167
+ {
168
+ if (_observer == NULL) {
169
+ return;
170
+ }
171
+ CFRunLoopObserverInvalidate(_observer);
172
+ CFRelease(_observer);
173
+ _observer = NULL;
174
+ }
175
+
176
+ - (void)detach:(NSString *)nativeID
177
+ {
178
+ id binding = _bindings[nativeID];
179
+ if (![binding isKindOfClass:[NgNativeBinding class]]) {
180
+ return;
181
+ }
182
+ _bindings[nativeID] = [NSNull null];
183
+ [self searchSoon];
184
+ UIView *view = ((NgNativeBinding *)binding).view;
185
+ id token = ((NgNativeBinding *)binding).token;
186
+ if (view != nil && token != nil) {
187
+ _detach(view, token);
188
+ }
189
+ }
190
+
191
+ - (void)refresh
192
+ {
193
+ NSMutableSet<NSString *> *missing = [NSMutableSet set];
194
+ for (NSString *nativeID in _bindings.allKeys) {
195
+ id binding = _bindings[nativeID];
196
+ if ([binding isKindOfClass:[NgNativeBinding class]]) {
197
+ if (isValid(binding, nativeID)) {
198
+ continue;
199
+ }
200
+ [self detach:nativeID];
201
+ }
202
+ [missing addObject:nativeID];
203
+ }
204
+ // A view not mounted yet is looked for at once after a change, then less often while it stays
205
+ // missing: a whole search of every window is not cheap.
206
+ CFTimeInterval now = CACurrentMediaTime();
207
+ if (missing.count == 0 || now - _searchedAt < _searchInterval) {
208
+ return;
209
+ }
210
+ _searchedAt = now;
211
+ _searches++;
212
+ NSMutableDictionary<NSString *, UIView *> *found = [NSMutableDictionary dictionary];
213
+ for (UIView *root in self.roots()) {
214
+ collect(root, missing, found);
215
+ }
216
+ _searchInterval = found.count == 0 ? MIN(_searchInterval * 2, LongestSearchInterval) : FirstSearchInterval;
217
+ [found enumerateKeysAndObjectsUsingBlock:^(NSString *nativeID, UIView *view, BOOL *stop) {
218
+ NgNativeBinding *binding = [NgNativeBinding new];
219
+ binding.view = view;
220
+ binding.token = self->_attach(view, nativeID);
221
+ self->_bindings[nativeID] = binding;
222
+ }];
223
+ }
224
+
225
+ @end