-
-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathKeyboardControllerView.mm
More file actions
309 lines (271 loc) · 12.3 KB
/
KeyboardControllerView.mm
File metadata and controls
309 lines (271 loc) · 12.3 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// KeyboardControllerView.mm
// KeyboardController
//
// Created by Kiryl Ziusko on 2.08.22.
// Copyright © 2022 Facebook. All rights reserved.
//
// This guard prevent the code from being compiled in the old architecture
#ifdef RCT_NEW_ARCH_ENABLED
#import "KeyboardControllerView.h"
#import "FocusedInputLayoutChangedEvent.h"
#import "FocusedInputSelectionChangedEvent.h"
#import "FocusedInputTextChangedEvent.h"
#import "KeyboardMoveEvent.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
#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/RNKCKeyboardControllerViewComponentDescriptor.h>
#import "KeyboardControllerModule-Header.h"
#import "RCTFabricComponentsPlugins.h"
using namespace facebook::react;
@interface KeyboardControllerView () <RCTKeyboardControllerViewViewProtocol>
@end
@implementation KeyboardControllerView {
KeyboardMovementObserver *keyboardObserver;
FocusedInputObserver *inputObserver;
CGSize _lastScreenSize;
}
- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args
{
RCTKeyboardControllerViewHandleCommand(self, commandName, args);
}
- (void)synchronizeFocusedInputLayout
{
[inputObserver syncUpLayout];
[KeyboardController.shared sendEvent:@"KeyboardController::layoutDidSynchronize" body:nil];
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<KeyboardControllerViewComponentDescriptor>();
}
// Needed because of this: https://github.com/facebook/react-native/pull/37274
+ (void)load
{
[super load];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const KeyboardControllerViewProps>();
_props = defaultProps;
inputObserver = [[FocusedInputObserver alloc]
initOnLayoutChangedHandler:^(NSDictionary *event) {
if (self->_eventEmitter) {
int target = [event[@"target"] integerValue];
int parentScrollViewTarget = [event[@"parentScrollViewTarget"] integerValue];
double absoluteY = [event[@"layout"][@"absoluteY"] doubleValue];
double absoluteX = [event[@"layout"][@"absoluteX"] doubleValue];
double y = [event[@"layout"][@"y"] doubleValue];
double x = [event[@"layout"][@"x"] doubleValue];
double width = [event[@"layout"][@"width"] doubleValue];
double height = [event[@"layout"][@"height"] doubleValue];
std::dynamic_pointer_cast<const facebook::react::KeyboardControllerViewEventEmitter>(
self->_eventEmitter)
->onFocusedInputLayoutChanged(
facebook::react::KeyboardControllerViewEventEmitter::
OnFocusedInputLayoutChanged{
.target = target,
.parentScrollViewTarget = parentScrollViewTarget,
.layout = facebook::react::KeyboardControllerViewEventEmitter::
OnFocusedInputLayoutChangedLayout{
.absoluteY = absoluteY,
.absoluteX = absoluteX,
.height = height,
.width = width,
.x = x,
.y = y}});
FocusedInputLayoutChangedEvent *inputChangedEvent =
[[FocusedInputLayoutChangedEvent alloc] initWithReactTag:@(self.tag) event:event];
[self postNotificationForEventDispatcherObserversWithEvent:inputChangedEvent];
}
}
onTextChangedHandler:^(NSString *text) {
if (self->_eventEmitter) {
std::dynamic_pointer_cast<const facebook::react::KeyboardControllerViewEventEmitter>(
self->_eventEmitter)
->onFocusedInputTextChanged(
facebook::react::KeyboardControllerViewEventEmitter::OnFocusedInputTextChanged{
.text = std::string([text UTF8String])});
FocusedInputTextChangedEvent *textChangedEvent =
[[FocusedInputTextChangedEvent alloc] initWithReactTag:@(self.tag) text:text];
[self postNotificationForEventDispatcherObserversWithEvent:textChangedEvent];
}
}
onSelectionChangedHandler:^(NSDictionary *event) {
if (self->_eventEmitter) {
int target = [event[@"target"] integerValue];
double startX = [event[@"selection"][@"start"][@"x"] doubleValue];
double startY = [event[@"selection"][@"start"][@"y"] doubleValue];
double endX = [event[@"selection"][@"end"][@"x"] doubleValue];
double endY = [event[@"selection"][@"end"][@"y"] doubleValue];
int start = [event[@"selection"][@"start"][@"position"] integerValue];
int end = [event[@"selection"][@"end"][@"position"] integerValue];
std::dynamic_pointer_cast<const facebook::react::KeyboardControllerViewEventEmitter>(
self->_eventEmitter)
->onFocusedInputSelectionChanged(
facebook::react::KeyboardControllerViewEventEmitter::
OnFocusedInputSelectionChanged{
.target = target,
.selection = facebook::react::KeyboardControllerViewEventEmitter::
OnFocusedInputSelectionChangedSelection{
.start =
facebook::react::KeyboardControllerViewEventEmitter::
OnFocusedInputSelectionChangedSelectionStart{
.x = startX, .y = startY, .position = start},
.end = facebook::react::KeyboardControllerViewEventEmitter::
OnFocusedInputSelectionChangedSelectionEnd{
.x = endX, .y = endY, .position = end}}});
FocusedInputSelectionChangedEvent *selectionChangedEvent =
[[FocusedInputSelectionChangedEvent alloc] initWithReactTag:@(self.tag)
event:event];
[self postNotificationForEventDispatcherObserversWithEvent:selectionChangedEvent];
}
}
onFocusDidSet:^(NSDictionary *data) {
[KeyboardController.shared sendEvent:@"KeyboardController::focusDidSet" body:data];
}];
keyboardObserver = [[KeyboardMovementObserver alloc]
initWithHandler:^(
NSString *event,
NSNumber *height,
NSNumber *progress,
NSNumber *duration,
NSNumber *target) {
if (self->_eventEmitter) {
// TODO: use reflection to reduce code duplication?
if ([event isEqualToString:@"onKeyboardMoveStart"]) {
std::dynamic_pointer_cast<const facebook::react::KeyboardControllerViewEventEmitter>(
self->_eventEmitter)
->onKeyboardMoveStart(
facebook::react::KeyboardControllerViewEventEmitter::OnKeyboardMoveStart{
.height = [height doubleValue],
.progress = [progress doubleValue],
.duration = [duration intValue],
.target = [target intValue]});
}
if ([event isEqualToString:@"onKeyboardMove"]) {
std::dynamic_pointer_cast<const facebook::react::KeyboardControllerViewEventEmitter>(
self->_eventEmitter)
->onKeyboardMove(
facebook::react::KeyboardControllerViewEventEmitter::OnKeyboardMove{
.height = [height doubleValue],
.progress = [progress doubleValue],
.duration = [duration intValue],
.target = [target intValue]});
}
if ([event isEqualToString:@"onKeyboardMoveEnd"]) {
std::dynamic_pointer_cast<const facebook::react::KeyboardControllerViewEventEmitter>(
self->_eventEmitter)
->onKeyboardMoveEnd(
facebook::react::KeyboardControllerViewEventEmitter::OnKeyboardMoveEnd{
.height = [height doubleValue],
.progress = [progress doubleValue],
.duration = [duration intValue],
.target = [target intValue]});
}
if ([event isEqualToString:@"onKeyboardMoveInteractive"]) {
std::dynamic_pointer_cast<const facebook::react::KeyboardControllerViewEventEmitter>(
self->_eventEmitter)
->onKeyboardMoveInteractive(facebook::react::KeyboardControllerViewEventEmitter::
OnKeyboardMoveInteractive{
.height = [height doubleValue],
.progress = [progress doubleValue],
.duration = [duration intValue],
.target = [target intValue]});
}
}
KeyboardMoveEvent *keyboardMoveEvent =
[[KeyboardMoveEvent alloc] initWithReactTag:@(self.tag)
event:event
height:height
progress:progress
duration:duration
target:target];
[self postNotificationForEventDispatcherObserversWithEvent:keyboardMoveEvent];
}
onNotify:^(NSString *event, NSDictionary *data) {
[KeyboardController.shared sendEvent:event body:data];
}
onRequestAnimation:^() {
// no-op for fabric
}
onCancelAnimation:^(){
// no-op for fabric
}];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
UIWindow *window = self.window;
if (!window) {
return;
}
CGSize screenSize = window.bounds.size;
if (CGSizeEqualToSize(screenSize, _lastScreenSize)) {
return;
}
_lastScreenSize = screenSize;
NSDictionary *data = @{@"width" : @(screenSize.width), @"height" : @(screenSize.height)};
[KeyboardController.shared sendEvent:@"KeyboardController::windowDidResize" body:data];
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &oldViewProps = *std::static_pointer_cast<const KeyboardControllerViewProps>(_props);
const auto &newViewProps = *std::static_pointer_cast<const KeyboardControllerViewProps>(props);
if (newViewProps.enabled != oldViewProps.enabled) {
if (newViewProps.enabled) {
[self mount];
} else {
[self unmount];
}
}
[super updateProps:props oldProps:oldProps];
}
- (void)willMoveToSuperview:(UIView *)newSuperview
{
if (newSuperview == nil) {
[self unmount];
} else {
[self mount];
}
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
if (self.window) {
[keyboardObserver.keyboardTrackingView attachToTopmostViewToWindow:self.window];
}
}
- (void)mount
{
[inputObserver mount];
[keyboardObserver mount];
}
- (void)unmount
{
[inputObserver unmount];
[keyboardObserver unmount];
}
// TODO: once Fabric implements proper NativeAnimationDriver, this should be removed.
- (void)postNotificationForEventDispatcherObserversWithEvent:(NSObject<RCTEvent> *)event
{
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:event, @"event", nil];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"RCTNotifyEventDispatcherObserversOfEvent_DEPRECATED"
object:nil
userInfo:userInfo];
}
Class<RCTComponentViewProtocol> KeyboardControllerViewCls(void)
{
return KeyboardControllerView.class;
}
@end
#endif