-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathMenuView.mm
More file actions
194 lines (164 loc) · 6.96 KB
/
Copy pathMenuView.mm
File metadata and controls
194 lines (164 loc) · 6.96 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
#ifdef RCT_NEW_ARCH_ENABLED
#if __has_include(<react_native_menu/react_native_menu-Swift.h>)
#import <react_native_menu/react_native_menu-Swift.h>
#else
#import <react_native_menu-Swift.h>
#endif
#import "MenuView.h"
#import <react/renderer/components/RNMenuViewSpec/ComponentDescriptors.h>
#import <react/renderer/components/RNMenuViewSpec/EventEmitters.h>
#import <react/renderer/components/RNMenuViewSpec/Props.h>
#import <react/renderer/components/RNMenuViewSpec/RCTComponentViewHelpers.h>
#import "RCTFabricComponentsPlugins.h"
using namespace facebook::react;
@interface MenuView () <RCTMenuViewViewProtocol>
@end
@implementation MenuView {
UIView <FabricViewImplementationProtocol> * _view;
}
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<MenuViewComponentDescriptor>();
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const MenuViewProps>();
_props = defaultProps;
if (@available(iOS 14.0, *)) {
_view = [[FabricMenuViewImplementation alloc] init];
} else {
_view = [[FabricActionSheetView alloc] init];
}
_view.onPressAction = ^(NSString *eventString) {
[self onPressAction:eventString];
};
_view.onOpenMenu = ^{
[self onOpenMenu];
};
_view.onCloseMenu = ^{
[self onCloseMenu];
};
self.contentView = _view;
}
return self;
}
- (std::shared_ptr<const MenuViewEventEmitter>)getEventEmitter
{
if (!self->_eventEmitter) {
return nullptr;
}
assert(std::dynamic_pointer_cast<MenuViewEventEmitter const>(self->_eventEmitter));
return std::static_pointer_cast<MenuViewEventEmitter const>(self->_eventEmitter);
}
- (void)onPressAction:(NSString * _Nonnull)eventString {
// If screen is already unmounted then there will be no event emitter
const auto eventEmitter = [self getEventEmitter];
if (eventEmitter != nullptr) {
eventEmitter->onPressAction(MenuViewEventEmitter::OnPressAction{
.event = [eventString UTF8String]
});
}
}
- (void)onCloseMenu {
// If screen is already unmounted then there will be no event emitter
const auto eventEmitter = [self getEventEmitter];
if (eventEmitter != nullptr) {
eventEmitter->onCloseMenu({});
}
}
- (void)onOpenMenu {
// If screen is already unmounted then there will be no event emitter
const auto eventEmitter = [self getEventEmitter];
if (eventEmitter != nullptr) {
eventEmitter->onOpenMenu({});
}
}
/**
Responsible for iterating through the C++ vector<struct> and convert each struct element to NSDictionary, then return it all in an NSArray
*/
- (NSMutableArray<NSDictionary *> *) convertActionsToObjC: (std::vector<MenuViewActionsStruct>) actions {
if (actions.size() == 0) {
return @[];
}
NSMutableArray<NSDictionary *> *actionsArray = [NSMutableArray arrayWithCapacity:actions.size()];
for (const MenuViewActionsStruct &action : actions) {
// first convert the subactions if they exist
NSMutableArray<NSDictionary *> *subactionsArray = [NSMutableArray arrayWithCapacity:actions.size()];
if (action.subactions.size() > 0) {
for (const MenuViewActionsSubactionsStruct &subaction : action.subactions) {
NSDictionary *subactionDict = @{
@"id": [NSString stringWithUTF8String:subaction.id.c_str()],
@"title": [NSString stringWithUTF8String:subaction.title.c_str()],
@"titleColor": @(subaction.titleColor),
@"subtitle": [NSString stringWithUTF8String:subaction.subtitle.c_str()],
@"state": [NSString stringWithUTF8String:subaction.state.c_str()],
@"image": [NSString stringWithUTF8String:subaction.image.c_str()],
@"imageColor": @(subaction.imageColor),
@"displayInline": @(subaction.displayInline),
@"attributes": @{
@"destructive": @(subaction.attributes.destructive),
@"disabled": @(subaction.attributes.disabled),
@"hidden": @(subaction.attributes.hidden),
},
};
[subactionsArray addObject:subactionDict];
}
}
// then convert the full actions object
NSDictionary *actionDict = @{
@"id": [NSString stringWithUTF8String:action.id.c_str()],
@"title": [NSString stringWithUTF8String:action.title.c_str()],
@"titleColor": @(action.titleColor),
@"subtitle": [NSString stringWithUTF8String:action.subtitle.c_str()],
@"state": [NSString stringWithUTF8String:action.state.c_str()],
@"image": [NSString stringWithUTF8String:action.image.c_str()],
@"imageColor": @(action.imageColor),
@"displayInline": @(action.displayInline),
@"preferredElementSize": [NSString stringWithUTF8String:action.preferredElementSize.c_str()],
@"attributes": @{
@"destructive": @(action.attributes.destructive),
@"disabled": @(action.attributes.disabled),
@"hidden": @(action.attributes.hidden),
},
@"subactions": subactionsArray,
};
[actionsArray addObject:actionDict];
}
return actionsArray;
}
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &oldViewProps = *std::static_pointer_cast<MenuViewProps const>(_props);
const auto &newViewProps = *std::static_pointer_cast<MenuViewProps const>(props);
if (oldViewProps.actionsHash != newViewProps.actionsHash) {
_view.actions = [self convertActionsToObjC: newViewProps.actions];
}
if (oldViewProps.title != newViewProps.title) {
_view.title = [NSString stringWithUTF8String:newViewProps.title.c_str()];
}
if (oldViewProps.themeVariant != newViewProps.themeVariant) {
_view.themeVariant = [NSString stringWithUTF8String:newViewProps.themeVariant.c_str()];
}
if (oldViewProps.shouldOpenOnLongPress != newViewProps.shouldOpenOnLongPress) {
_view.shouldOpenOnLongPress = newViewProps.shouldOpenOnLongPress;
}
if (oldViewProps.hitSlop.top != newViewProps.hitSlop.top ||
oldViewProps.hitSlop.bottom != newViewProps.hitSlop.bottom ||
oldViewProps.hitSlop.left != newViewProps.hitSlop.left ||
oldViewProps.hitSlop.right != newViewProps.hitSlop.right) {
_view.hitSlop = UIEdgeInsetsMake(
newViewProps.hitSlop.top,
newViewProps.hitSlop.left,
newViewProps.hitSlop.bottom,
newViewProps.hitSlop.right
);
}
[super updateProps:props oldProps:oldProps];
}
Class<RCTComponentViewProtocol> MenuViewCls(void)
{
return MenuView.class;
}
@end
#endif