forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.mm
228 lines (191 loc) · 7.83 KB
/
AppDelegate.mm
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
//
// AppDelegate.m
// RNTester-macOS
//
// Created by Jeff Cruikshank on 6/5/17.
// Copyright © 2017 Facebook. All rights reserved.
//
#import "AppDelegate.h"
#ifndef RCT_USE_HERMES
#if __has_include(<reacthermes/HermesExecutorFactory.h>)
#define RCT_USE_HERMES 1
#else
#define RCT_USE_HERMES 0
#endif
#endif
#if RCT_USE_HERMES
#import <reacthermes/HermesExecutorFactory.h>
#else
#import <React/JSCExecutorFactory.h>
#endif
#import <React/CoreModulesPlugins.h>
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTCxxBridgeDelegate.h>
#import <React/RCTDataRequestHandler.h>
#import <React/RCTFileRequestHandler.h>
#import <React/RCTGIFImageDecoder.h>
#import <React/RCTHTTPRequestHandler.h>
#import <React/RCTImageLoader.h>
#import <React/RCTJSIExecutorRuntimeInstaller.h>
#import <React/RCTLinkingManager.h>
#import <React/RCTLocalAssetImageLoader.h>
#import <React/RCTNetworking.h>
#import <React/RCTPushNotificationManager.h>
#import <React/RCTTextAttributes.h>
#import <ReactCommon/RCTTurboModuleManager.h>
#import <ReactCommon/TurboModule.h>
#import <TurboModulesProvider.h>
#import "../NativeModuleExample/ScreenshotMacOS.h"
NSString *kBundleNameJS = @"RNTesterApp";
@interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate, NSUserNotificationCenterDelegate>
@end
@implementation AppDelegate {
NSMutableArray *_mainWindows;
RCTTurboModuleManager *_turboModuleManager;
std::shared_ptr<winrt::Microsoft::ReactNative::TurboModulesProvider> _turboModulesProvider;
}
- (void)awakeFromNib
{
[super awakeFromNib];
RCTEnableTurboModule(YES);
_bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
// Optionally set the global `fontSmoothing` setting.
// If not explicitly set, the default is subpixel-antialiased
[RCTTextAttributes setFontSmoothingDefault:RCTFontSmoothingSubpixelAntialiased];
}
- (void)applicationWillFinishLaunching:(NSNotification *)__unused aNotification
{
[NSUserNotificationCenter defaultUserNotificationCenter].delegate = self;
// initialize the url event listeners for Linking module
// note that you will need to add a URL type to your app’s info.plist
// this sample registers the rntester scheme
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:[RCTLinkingManager class]
andSelector:@selector(getUrlEventHandler:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
}
- (IBAction)newDocument:(id)__unused sender
{
if (_mainWindows == nil) {
_mainWindows = [NSMutableArray new];
}
NSWindowController *windowController =
[[NSStoryboard storyboardWithName:@"Main" bundle:nil] instantiateControllerWithIdentifier:@"MainWindow"];
[_mainWindows addObject:windowController];
[windowController showWindow:self];
}
#pragma mark - RCTBridgeDelegate Methods
- (NSURL *)sourceURLForBridge:(__unused RCTBridge *)bridge
{
NSString *jsBundlePath = [NSString stringWithFormat:@"packages/rn-tester/js/%@.macos", kBundleNameJS];
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:jsBundlePath];
}
#pragma mark - RCTCxxBridgeDelegate Methods
- (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
{
_turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
delegate:self
jsInvoker:bridge.jsCallInvoker];
[bridge setRCTTurboModuleRegistry:_turboModuleManager];
#if RCT_DEV
/**
* Eagerly initialize RCTDevMenu so CMD + d, CMD + i, and CMD + r work.
* This is a stop gap until we have a system to eagerly init Turbo Modules.
*/
[_turboModuleManager moduleForName:"RCTDevMenu"];
#endif
__weak __typeof(self) weakSelf = self;
#if RCT_USE_HERMES
return std::make_unique<facebook::react::HermesExecutorFactory>(
#else
return std::make_unique<facebook::react::JSCExecutorFactory>(
#endif
facebook::react::RCTJSIExecutorRuntimeInstaller([weakSelf, bridge](facebook::jsi::Runtime &runtime) {
if (!bridge) {
return;
}
__typeof(self) strongSelf = weakSelf;
if (strongSelf) {
facebook::react::RuntimeExecutor syncRuntimeExecutor =
[&](std::function<void(facebook::jsi::Runtime & runtime_)> &&callback) { callback(runtime); };
[strongSelf->_turboModuleManager installJSBindingWithRuntimeExecutor:syncRuntimeExecutor];
}
}));
}
#pragma mark RCTTurboModuleManagerDelegate Methods
- (Class)getModuleClassFromName:(const char *)name
{
return RCTCoreModulesClassProvider(name);
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
{
if (!_turboModulesProvider) {
_turboModulesProvider = std::make_shared<winrt::Microsoft::ReactNative::TurboModulesProvider>();
_turboModulesProvider->SetReactContext(winrt::Microsoft::ReactNative::CreateMacOSReactContext(jsInvoker));
_turboModulesProvider->AddModuleProvider(
L"ScreenshotManager", winrt::Microsoft::ReactNative::MakeModuleProvider<ScreenshotManagerCxx>());
}
return _turboModulesProvider->getModule(name, jsInvoker);
}
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return nullptr;
}
- (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
{
// Set up the default RCTImageLoader and RCTNetworking modules.
if (moduleClass == RCTImageLoader.class) {
return [[moduleClass alloc] initWithRedirectDelegate:nil
loadersProvider:^NSArray<id<RCTImageURLLoader>> *(__unused RCTModuleRegistry *moduleRegistry) {
return @[ [RCTLocalAssetImageLoader new] ];
}
decodersProvider:^NSArray<id<RCTImageDataDecoder>> *(__unused RCTModuleRegistry *moduleRegistry) {
return @[ [RCTGIFImageDecoder new] ];
}];
} else if (moduleClass == RCTNetworking.class) {
return [[moduleClass alloc]
initWithHandlersProvider:^NSArray<id<RCTURLRequestHandler>> *(__unused RCTModuleRegistry *moduleRegistry) {
return @[
[RCTHTTPRequestHandler new],
[RCTDataRequestHandler new],
[RCTFileRequestHandler new],
];
}];
}
// No custom initializer here.
return [moduleClass new];
}
#pragma mark - Push Notifications
// Required for the remoteNotificationsRegistered event.
- (void)application:(NSApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RCTPushNotificationManager didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
// Required for the remoteNotificationRegistrationError event.
- (void)application:(NSApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RCTPushNotificationManager didFailToRegisterForRemoteNotificationsWithError:error];
}
// Required for the remoteNotificationReceived event.
- (void)application:(NSApplication *)application didReceiveRemoteNotification:(NSDictionary<NSString *, id> *)userInfo
{
[RCTPushNotificationManager didReceiveRemoteNotification:userInfo];
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
didDeliverNotification:(NSUserNotification *)notification
{
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center
didActivateNotification:(NSUserNotification *)notification
{
[RCTPushNotificationManager didReceiveUserNotification:notification];
}
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
shouldPresentNotification:(NSUserNotification *)notification
{
return YES;
}
@end