-
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathKeyboardBackgroundViewManager.mm
More file actions
175 lines (143 loc) · 4.65 KB
/
KeyboardBackgroundViewManager.mm
File metadata and controls
175 lines (143 loc) · 4.65 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
//
// KeyboardBackgroundViewManager.mm
// Pods
//
// Created by Kiryl Ziusko on 21/04/2025.
//
#import "KeyboardBackgroundViewManager.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/renderer/components/RNKC/EventEmitters.h>
#import <react/renderer/components/RNKC/Props.h>
#import <react/renderer/components/RNKC/RCTComponentViewHelpers.h>
#import <react/renderer/components/RNKC/RNKCKeyboardBackgroundViewComponentDescriptor.h>
#import "RCTFabricComponentsPlugins.h"
#endif
#import <React/RCTView.h>
#import <UIKit/UIKit.h>
#ifdef RCT_NEW_ARCH_ENABLED
using namespace facebook::react;
#endif
typedef NS_ENUM(NSInteger, KeyboardBackdropStyle) {
KeyboardBackdropStyleDark = 2030,
KeyboardBackdropStyleLight = 3901
};
@protocol KeyboardBackdropViewProtocol <NSObject>
@optional
- (instancetype)initWithFrame:(CGRect)frame style:(long long)style;
- (void)transitionToStyle:(long long)style;
@end
#pragma mark - Manager
@implementation KeyboardBackgroundViewManager
RCT_EXPORT_MODULE(KeyboardBackgroundViewManager)
+ (BOOL)requiresMainQueueSetup
{
return NO;
}
#ifndef RCT_NEW_ARCH_ENABLED
- (UIView *)view
{
RCTView *containerView = [[RCTView alloc] initWithFrame:CGRectZero];
containerView.clipsToBounds = YES;
KeyboardBackgroundView *backgroundView =
[[KeyboardBackgroundView alloc] initWithFrame:containerView.bounds];
backgroundView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[containerView addSubview:backgroundView];
return containerView;
}
#endif
@end
#pragma mark - View
#ifdef RCT_NEW_ARCH_ENABLED
@interface KeyboardBackgroundView () <RCTKeyboardBackgroundViewViewProtocol>
#else
@interface KeyboardBackgroundView ()
#endif
@end
@implementation KeyboardBackgroundView {
UIVisualEffectView *_backdropView;
}
#ifdef RCT_NEW_ARCH_ENABLED
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<KeyboardBackgroundViewComponentDescriptor>();
}
#endif
// Needed because of this: https://github.com/facebook/react-native/pull/37274
+ (void)load
{
[super load];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupBackdropView];
}
self.clipsToBounds = YES;
return self;
}
- (void)setupBackdropView
{
Class BackdropClass = NSClassFromString(@"UIKBBackdropView");
if (BackdropClass) {
long long style = (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark)
? KeyboardBackdropStyleDark
: KeyboardBackdropStyleLight;
id<KeyboardBackdropViewProtocol> backdrop =
(id<KeyboardBackdropViewProtocol>)[BackdropClass alloc];
backdrop = [backdrop initWithFrame:self.bounds style:style];
if ([backdrop isKindOfClass:[UIVisualEffectView class]]) {
_backdropView = (UIVisualEffectView *)backdrop;
_backdropView.layer.masksToBounds = YES;
_backdropView.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_backdropView.frame = self.bounds;
[self addSubview:_backdropView];
}
} else {
NSLog(@"KeyboardBackdropView class not found");
}
}
// MARK: child management
#ifdef RCT_NEW_ARCH_ENABLED
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView
index:(NSInteger)index
{
// preserve slot 0 for blur layer
[super mountChildComponentView:childComponentView index:index + 1];
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView
index:(NSInteger)index
{
// preserve slot 0 for blur layer
[super unmountChildComponentView:childComponentView index:index + 1];
}
#endif
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
{
[super traitCollectionDidChange:previousTraitCollection];
if (@available(iOS 12.0, *)) {
if ([previousTraitCollection
hasDifferentColorAppearanceComparedToTraitCollection:self.traitCollection]) {
long long style = (self.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark)
? KeyboardBackdropStyleDark
: KeyboardBackdropStyleLight;
if ([_backdropView respondsToSelector:@selector(transitionToStyle:)]) {
[(id<KeyboardBackdropViewProtocol>)_backdropView transitionToStyle:style];
}
}
}
}
#ifdef RCT_NEW_ARCH_ENABLED
Class<RCTComponentViewProtocol> KeyboardBackgroundViewCls(void)
{
return KeyboardBackgroundView.class;
}
#endif
@end