-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathOverKeyboardViewManager.mm
More file actions
237 lines (203 loc) · 5.84 KB
/
OverKeyboardViewManager.mm
File metadata and controls
237 lines (203 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// OverKeyboardViewManager.mm
// react-native-keyboard-controller
//
// Created by Kiryl Ziusko on 11/09/2024.
//
#import "OverKeyboardViewManager.h"
#if __has_include("react_native_keyboard_controller-Swift.h")
#import "react_native_keyboard_controller-Swift.h"
#else
#import <react_native_keyboard_controller/react_native_keyboard_controller-Swift.h>
#endif
#ifdef RCT_NEW_ARCH_ENABLED
#import <React/RCTSurfaceTouchHandler.h>
#import <react/renderer/components/RNKC/EventEmitters.h>
#import <react/renderer/components/RNKC/Props.h>
#import <react/renderer/components/RNKC/RCTComponentViewHelpers.h>
#import <react/renderer/components/RNKC/RNKCOverKeyboardViewComponentDescriptor.h>
#import "RCTFabricComponentsPlugins.h"
#endif
#import <React/RCTTouchHandler.h>
#import <UIKit/UIKit.h>
#ifdef RCT_NEW_ARCH_ENABLED
using namespace facebook::react;
#endif
// MARK: Manager
@implementation OverKeyboardViewManager
RCT_EXPORT_MODULE(OverKeyboardViewManager)
// Expose the `visible` prop to React Native
RCT_EXPORT_VIEW_PROPERTY(visible, BOOL)
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
#ifndef RCT_NEW_ARCH_ENABLED
- (UIView *)view
{
return [[OverKeyboardView alloc] initWithBridge:self.bridge];
}
#endif
@end
// MARK: View
#ifdef RCT_NEW_ARCH_ENABLED
@interface OverKeyboardView () <RCTOverKeyboardViewViewProtocol>
@end
#endif
@implementation OverKeyboardView {
UIView *_contentView;
#ifdef RCT_NEW_ARCH_ENABLED
RCTSurfaceTouchHandler *_touchHandler;
#else
RCTTouchHandler *_touchHandler;
#endif
}
#ifdef RCT_NEW_ARCH_ENABLED
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<OverKeyboardViewComponentDescriptor>();
}
#endif
// Needed because of this: https://github.com/facebook/react-native/pull/37274
+ (void)load
{
[super load];
// Eagerly initialize the observer
(void)[UIWindow sharedKeyboardWindowObserver];
}
// MARK: Constructor
#ifdef RCT_NEW_ARCH_ENABLED
- (instancetype)init
{
if (self = [super init]) {
_touchHandler = [RCTSurfaceTouchHandler new];
_contentView = [[UIView alloc] initWithFrame:CGRectZero];
}
return self;
}
#else
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
self = [super initWithFrame:CGRectZero];
if (self) {
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:bridge];
_contentView = [[UIView alloc] initWithFrame:CGRectZero];
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(hide)
name:RCTJavaScriptWillStartLoadingNotification
object:nil];
return self;
}
#endif
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// MARK: lifecycle methods
- (void)didMoveToSuperview
{
if (self.superview == nil) {
[self hide];
}
}
// MARK: touch handling
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL canReceiveTouchEvents = ([self isUserInteractionEnabled] && ![self isHidden] && _visible);
if (!canReceiveTouchEvents) {
return nil;
}
// `hitSubview` is the topmost subview which was hit. The hit point can
// be outside the bounds of `view` (e.g., if -clipsToBounds is NO).
UIView *hitSubview = nil;
BOOL isPointInside = [self pointInside:point withEvent:event];
if (![self clipsToBounds] || isPointInside) {
// TODO: should we take zIndex into consideration?
// The default behaviour of UIKit is that if a view does not contain a point,
// then no subviews will be returned from hit testing, even if they contain
// the hit point. By doing hit testing directly on the subviews, we bypass
// the strict containment policy (i.e., UIKit guarantees that every ancestor
// of the hit view will return YES from -pointInside:withEvent:). See:
// - https://developer.apple.com/library/ios/qa/qa2013/qa1812.html
for (UIView *subview in [_contentView.subviews reverseObjectEnumerator]) {
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
hitSubview = [subview hitTest:convertedPoint withEvent:event];
if (hitSubview != nil) {
break;
}
}
}
return hitSubview;
}
// MARK: props updater
#ifdef RCT_NEW_ARCH_ENABLED
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &newViewProps = *std::static_pointer_cast<const OverKeyboardViewProps>(props);
if (newViewProps.visible != _visible) {
if (newViewProps.visible) {
[self show];
} else {
[self hide];
}
}
[super updateProps:props oldProps:oldProps];
}
#else
- (void)setVisible:(BOOL)visible
{
if (visible) {
[self show];
} else {
[self hide];
}
}
#endif
// MARK: child management
#ifdef RCT_NEW_ARCH_ENABLED
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView
index:(NSInteger)index
{
[_contentView insertSubview:childComponentView atIndex:index];
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView
index:(NSInteger)index
{
[childComponentView removeFromSuperview];
}
#else
- (void)addSubview:(UIView *)view
{
[_contentView addSubview:view];
}
#endif
- (void)show
{
if (_visible) {
return;
}
UIWindow *topWindow = [UIWindow topWindow];
_visible = true;
_contentView.frame = topWindow.bounds;
_contentView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_touchHandler attachToView:_contentView];
[topWindow addSubview:_contentView];
}
- (void)hide
{
if (!_visible) {
return;
}
_visible = false;
[_contentView removeFromSuperview];
[_touchHandler detachFromView:_contentView];
}
#ifdef RCT_NEW_ARCH_ENABLED
Class<RCTComponentViewProtocol> OverKeyboardViewCls(void)
{
return OverKeyboardView.class;
}
#endif
@end