2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-03-18 02:47:51 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-03-18 02:47:51 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTDeviceInfo.h"
|
|
|
|
|
2019-08-22 00:28:16 +03:00
|
|
|
#import <FBReactNativeSpec/FBReactNativeSpec.h>
|
|
|
|
#import <React/RCTAccessibilityManager.h>
|
|
|
|
#import <React/RCTAssert.h>
|
2019-12-11 21:02:12 +03:00
|
|
|
#import <React/RCTConstants.h>
|
2019-08-22 00:28:16 +03:00
|
|
|
#import <React/RCTEventDispatcher.h>
|
|
|
|
#import <React/RCTUIUtils.h>
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
|
|
|
|
#import "CoreModulesPlugins.h"
|
|
|
|
|
|
|
|
using namespace facebook::react;
|
|
|
|
|
|
|
|
@interface RCTDeviceInfo () <NativeDeviceInfoSpec>
|
|
|
|
@end
|
2017-03-18 02:47:51 +03:00
|
|
|
|
|
|
|
@implementation RCTDeviceInfo {
|
|
|
|
#if !TARGET_OS_TV
|
|
|
|
UIInterfaceOrientation _currentInterfaceOrientation;
|
2019-04-11 00:08:42 +03:00
|
|
|
NSDictionary *_currentInterfaceDimensions;
|
2017-03-18 02:47:51 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2017-08-07 16:45:24 +03:00
|
|
|
+ (BOOL)requiresMainQueueSetup
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2017-03-18 02:47:51 +03:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
|
|
|
{
|
|
|
|
_bridge = bridge;
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(didReceiveNewContentSizeMultiplier)
|
|
|
|
name:RCTAccessibilityManagerDidUpdateMultiplierNotification
|
|
|
|
object:_bridge.accessibilityManager];
|
|
|
|
#if !TARGET_OS_TV
|
|
|
|
_currentInterfaceOrientation = [RCTSharedApplication() statusBarOrientation];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(interfaceOrientationDidChange)
|
|
|
|
name:UIApplicationDidChangeStatusBarOrientationNotification
|
|
|
|
object:nil];
|
2019-04-11 00:08:42 +03:00
|
|
|
|
|
|
|
_currentInterfaceDimensions = RCTExportedDimensions(_bridge);
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2020-03-09 08:58:01 +03:00
|
|
|
selector:@selector(interfaceFrameDidChange)
|
|
|
|
name:UIApplicationDidBecomeActiveNotification
|
|
|
|
object:nil];
|
2019-12-11 21:02:12 +03:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2020-03-09 08:58:01 +03:00
|
|
|
selector:@selector(interfaceFrameDidChange)
|
|
|
|
name:RCTUserInterfaceStyleDidChangeNotification
|
|
|
|
object:nil];
|
2019-12-11 21:02:12 +03:00
|
|
|
|
2017-03-18 02:47:51 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
static BOOL RCTIsIPhoneX()
|
|
|
|
{
|
2018-07-06 05:10:28 +03:00
|
|
|
static BOOL isIPhoneX = NO;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
RCTAssertMainQueue();
|
|
|
|
|
2018-09-17 07:38:47 +03:00
|
|
|
CGSize screenSize = [UIScreen mainScreen].nativeBounds.size;
|
|
|
|
CGSize iPhoneXScreenSize = CGSizeMake(1125, 2436);
|
|
|
|
CGSize iPhoneXMaxScreenSize = CGSizeMake(1242, 2688);
|
|
|
|
CGSize iPhoneXRScreenSize = CGSizeMake(828, 1792);
|
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
isIPhoneX = CGSizeEqualToSize(screenSize, iPhoneXScreenSize) ||
|
|
|
|
CGSizeEqualToSize(screenSize, iPhoneXMaxScreenSize) || CGSizeEqualToSize(screenSize, iPhoneXRScreenSize);
|
2018-07-06 05:10:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
return isIPhoneX;
|
|
|
|
}
|
|
|
|
|
2017-03-18 02:47:51 +03:00
|
|
|
static NSDictionary *RCTExportedDimensions(RCTBridge *bridge)
|
|
|
|
{
|
|
|
|
RCTAssertMainQueue();
|
2018-01-29 22:07:37 +03:00
|
|
|
RCTDimensions dimensions = RCTGetDimensions(bridge.accessibilityManager.multiplier);
|
2020-03-09 08:58:01 +03:00
|
|
|
__typeof(dimensions.window) window = dimensions.window;
|
2019-04-11 00:08:42 +03:00
|
|
|
NSDictionary<NSString *, NSNumber *> *dimsWindow = @{
|
2020-03-09 08:58:01 +03:00
|
|
|
@"width" : @(window.width),
|
|
|
|
@"height" : @(window.height),
|
|
|
|
@"scale" : @(window.scale),
|
|
|
|
@"fontScale" : @(window.fontScale)
|
2018-01-29 22:07:37 +03:00
|
|
|
};
|
2019-08-22 00:28:16 +03:00
|
|
|
__typeof(dimensions.screen) screen = dimensions.screen;
|
2019-04-11 00:08:42 +03:00
|
|
|
NSDictionary<NSString *, NSNumber *> *dimsScreen = @{
|
2020-03-09 08:58:01 +03:00
|
|
|
@"width" : @(screen.width),
|
|
|
|
@"height" : @(screen.height),
|
|
|
|
@"scale" : @(screen.scale),
|
|
|
|
@"fontScale" : @(screen.fontScale)
|
2018-01-29 22:07:37 +03:00
|
|
|
};
|
2020-03-09 08:58:01 +03:00
|
|
|
return @{@"window" : dimsWindow, @"screen" : dimsScreen};
|
2017-03-18 02:47:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport
|
2019-02-05 04:41:36 +03:00
|
|
|
{
|
|
|
|
return [self getConstants];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary<NSString *, id> *)getConstants
|
2017-03-18 02:47:51 +03:00
|
|
|
{
|
2017-10-03 08:49:59 +03:00
|
|
|
return @{
|
2020-03-09 08:58:01 +03:00
|
|
|
@"Dimensions" : RCTExportedDimensions(_bridge),
|
2018-07-06 05:10:28 +03:00
|
|
|
// Note:
|
2018-07-08 23:06:38 +03:00
|
|
|
// This prop is deprecated and will be removed in a future release.
|
2018-07-06 05:10:28 +03:00
|
|
|
// Please use this only for a quick and temporary solution.
|
|
|
|
// Use <SafeAreaView> instead.
|
2020-03-09 08:58:01 +03:00
|
|
|
@"isIPhoneX_deprecated" : @(RCTIsIPhoneX()),
|
2017-10-03 08:49:59 +03:00
|
|
|
};
|
2017-03-18 02:47:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didReceiveNewContentSizeMultiplier
|
|
|
|
{
|
2017-06-21 04:42:46 +03:00
|
|
|
RCTBridge *bridge = _bridge;
|
|
|
|
RCTExecuteOnMainQueue(^{
|
2020-03-09 08:58:01 +03:00
|
|
|
// Report the event across the bridge.
|
2017-03-18 02:47:51 +03:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2020-03-09 08:58:01 +03:00
|
|
|
[bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions" body:RCTExportedDimensions(bridge)];
|
2017-03-18 02:47:51 +03:00
|
|
|
#pragma clang diagnostic pop
|
2017-06-21 04:42:46 +03:00
|
|
|
});
|
2017-03-18 02:47:51 +03:00
|
|
|
}
|
|
|
|
|
2017-06-22 01:04:28 +03:00
|
|
|
#if !TARGET_OS_TV
|
|
|
|
|
2017-03-18 02:47:51 +03:00
|
|
|
- (void)interfaceOrientationDidChange
|
|
|
|
{
|
2019-08-22 00:28:16 +03:00
|
|
|
__weak __typeof(self) weakSelf = self;
|
2017-06-21 04:42:46 +03:00
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
[weakSelf _interfaceOrientationDidChange];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_interfaceOrientationDidChange
|
|
|
|
{
|
2017-03-18 02:47:51 +03:00
|
|
|
UIInterfaceOrientation nextOrientation = [RCTSharedApplication() statusBarOrientation];
|
|
|
|
|
|
|
|
// Update when we go from portrait to landscape, or landscape to portrait
|
|
|
|
if ((UIInterfaceOrientationIsPortrait(_currentInterfaceOrientation) &&
|
|
|
|
!UIInterfaceOrientationIsPortrait(nextOrientation)) ||
|
|
|
|
(UIInterfaceOrientationIsLandscape(_currentInterfaceOrientation) &&
|
|
|
|
!UIInterfaceOrientationIsLandscape(nextOrientation))) {
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2020-03-09 08:58:01 +03:00
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions" body:RCTExportedDimensions(_bridge)];
|
2017-03-18 02:47:51 +03:00
|
|
|
#pragma clang diagnostic pop
|
2020-03-09 08:58:01 +03:00
|
|
|
}
|
2017-03-18 02:47:51 +03:00
|
|
|
|
|
|
|
_currentInterfaceOrientation = nextOrientation;
|
|
|
|
}
|
|
|
|
|
2019-04-11 00:08:42 +03:00
|
|
|
- (void)interfaceFrameDidChange
|
|
|
|
{
|
2019-08-22 00:28:16 +03:00
|
|
|
__weak __typeof(self) weakSelf = self;
|
2019-04-11 00:08:42 +03:00
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
[weakSelf _interfaceFrameDidChange];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_interfaceFrameDidChange
|
|
|
|
{
|
|
|
|
NSDictionary *nextInterfaceDimensions = RCTExportedDimensions(_bridge);
|
|
|
|
|
|
|
|
if (!([nextInterfaceDimensions isEqual:_currentInterfaceDimensions])) {
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2020-03-09 08:58:01 +03:00
|
|
|
[_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions" body:nextInterfaceDimensions];
|
2019-04-11 00:08:42 +03:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
}
|
|
|
|
|
|
|
|
_currentInterfaceDimensions = nextInterfaceDimensions;
|
|
|
|
}
|
|
|
|
|
2017-06-22 01:04:28 +03:00
|
|
|
#endif // TARGET_OS_TV
|
|
|
|
|
2019-09-20 20:48:50 +03:00
|
|
|
- (std::shared_ptr<TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<CallInvoker>)jsInvoker
|
Codemod all getTurboModuleWithJsInvoker methods to accept a native CallInvoker
Summary:
To make iOS TurboModules integrate with the bridge's onBatchComplete event, they need to use a native CallInvoker. This call invoker is created by the `NativeToJsBridge`, and ObjCTurboModule will use this native CallInvoker to dispatch TurboModule method calls. This diff makes sure that ObjCTurboModules are created with that native CallInvoker.
## Script
```
var withSpaces = (...args) => args.join('\s*')
var regexString = withSpaces(
'-',
'\(',
'std::shared_ptr',
'<',
'(?<turboModuleClass>(facebook::react::|react::|::|)TurboModule)',
'>',
'\)',
'getTurboModuleWithJsInvoker',
':',
'\(',
'std::shared_ptr',
'<',
'(?<callInvokerClass>(facebook::react::|react::|::|)CallInvoker)',
'>',
'\)',
'(?<jsInvokerInstance>[A-Za-z0-9]+)',
'perfLogger',
':',
'\(',
'id',
'<',
'RCTTurboModulePerformanceLogger',
'>',
'\)',
'(?<perfLoggerInstance>[A-Za-z0-9]+)',
'{',
'return',
'std::make_shared',
'<',
'(?<specName>(facebook::react::|react::|::|)Native[%A-Za-z0-9]+SpecJSI)',
'>',
'\(',
'self',
',',
'\k<jsInvokerInstance>',
',',
'\k<perfLoggerInstance>',
'\)',
';',
'}',
)
var replaceString = `- (std::shared_ptr<$<turboModuleClass>>)
getTurboModuleWithJsInvoker:(std::shared_ptr<$<callInvokerClass>>)$<jsInvokerInstance>
nativeInvoker:(std::shared_ptr<$<callInvokerClass>>)nativeInvoker
perfLogger:(id<RCTTurboModulePerformanceLogger>)$<perfLoggerInstance>
{
return std::make_shared<$<specName>>(self, $<jsInvokerInstance>, nativeInvoker, $<perfLoggerInstance>);
}`
const exec = require('../lib/exec');
const abspath = require('../lib/abspath');
const relpath = require('../lib/relpath');
const readFile = (filename) => require('fs').readFileSync(filename, 'utf8');
const writeFile = (filename, content) => require('fs').writeFileSync(filename, content);
function main() {
const tmFiles = exec('cd ~/fbsource && xbgs -n 10000 -l getTurboModuleWithJsInvoker:').split('\n').filter(Boolean);
tmFiles
.filter((filename) => !filename.includes('microsoft-fork-of-react-native'))
.map(abspath)
.forEach((filename) => {
const source = readFile(filename);
const newSource = source.replace(new RegExp(regexString, 'g'), replaceString);
if (source == newSource) {
console.log(relpath(filename));
}
writeFile(filename, newSource);
});
}
if (!module.parent) {
main();
}
```
Changelog: [Internal]
Reviewed By: fkgozali
Differential Revision: D20809202
fbshipit-source-id: 5d39b3cacdaa5681b70ce1803351d0432dd74550
2020-04-03 12:24:31 +03:00
|
|
|
nativeInvoker:(std::shared_ptr<CallInvoker>)nativeInvoker
|
Add a perfLogger argument to getTurboModuleWithJSInvoker:
Summary:
## Purpose
We must modify the `getTurboModuleWithJsInvoker:` method of all our NativeModules to also accept a `id<RCTTurboModulePerformanceLogger>` object. This performance logger object should then be forwarded to the `Native*SpecJSI` constructor.
## Script
Run the following script via Node:
```
var withSpaces = (...args) => args.join('\s*')
var regexString = withSpaces(
'-',
'\(',
'std::shared_ptr',
'<',
'(?<turboModuleClass>(facebook::react::|react::|::|)TurboModule)',
'>',
'\)',
'getTurboModuleWithJsInvoker',
':',
'\(',
'std::shared_ptr',
'<',
'(?<callInvokerClass>(facebook::react::|react::|::|)CallInvoker)',
'>',
'\)',
'jsInvoker',
'{',
'return',
'std::make_shared',
'<',
'(?<specName>(facebook::react::|react::|::|)Native[A-Za-z0-9]+SpecJSI)',
'>',
'\(',
'(?<arg1>[A-Za-z0-9]+)',
',',
'(?<arg2>[A-Za-z0-9]+)',
'\)',
';',
'}',
)
var replaceString = `- (std::shared_ptr<$<turboModuleClass>>)
getTurboModuleWithJsInvoker:(std::shared_ptr<$<callInvokerClass>>)jsInvoker
perfLogger:(id<RCTTurboModulePerformanceLogger>)perfLogger
{
return std::make_shared<$<specName>>($<arg1>, $<arg2>, perfLogger);
}`
const exec = (cmd) => require('child_process').execSync(cmd, { encoding: 'utf8' });
const abspath = (filename) => `${process.env.HOME}/${filename}`;
const relpath = (filename) => filename.replace(process.env.HOME + '/', '');
const readFile = (filename) => require('fs').readFileSync(filename, 'utf8');
const writeFile = (filename, content) => require('fs').writeFileSync(filename, content);
function main() {
const tmFiles = exec('cd ~/fbsource && xbgs -n 10000 -l getTurboModuleWithJsInvoker:').split('\n').filter(Boolean);
tmFiles
.filter((filename) => !filename.includes('microsoft-fork-of-react-native'))
.map(abspath)
.forEach((filename) => {
const source = readFile(filename);
const newSource = source.replace(new RegExp(regexString, 'g'), replaceString);
if (source == newSource) {
console.log(relpath(filename));
}
writeFile(filename, newSource);
});
}
if (!module.parent) {
main();
}
```
Also, run: `pushd ~/fbsource && js1 build oss-native-modules-specs -p ios && js1 build oss-native-modules-specs -p android && popd;`
Changelog: [Internal]
Reviewed By: fkgozali
Differential Revision: D20478718
fbshipit-source-id: 89ee27ed8a0338a66a9b2dbb716168a4c4582c44
2020-03-18 20:56:41 +03:00
|
|
|
perfLogger:(id<RCTTurboModulePerformanceLogger>)perfLogger
|
2019-08-22 00:28:16 +03:00
|
|
|
{
|
Codemod all getTurboModuleWithJsInvoker methods to accept a native CallInvoker
Summary:
To make iOS TurboModules integrate with the bridge's onBatchComplete event, they need to use a native CallInvoker. This call invoker is created by the `NativeToJsBridge`, and ObjCTurboModule will use this native CallInvoker to dispatch TurboModule method calls. This diff makes sure that ObjCTurboModules are created with that native CallInvoker.
## Script
```
var withSpaces = (...args) => args.join('\s*')
var regexString = withSpaces(
'-',
'\(',
'std::shared_ptr',
'<',
'(?<turboModuleClass>(facebook::react::|react::|::|)TurboModule)',
'>',
'\)',
'getTurboModuleWithJsInvoker',
':',
'\(',
'std::shared_ptr',
'<',
'(?<callInvokerClass>(facebook::react::|react::|::|)CallInvoker)',
'>',
'\)',
'(?<jsInvokerInstance>[A-Za-z0-9]+)',
'perfLogger',
':',
'\(',
'id',
'<',
'RCTTurboModulePerformanceLogger',
'>',
'\)',
'(?<perfLoggerInstance>[A-Za-z0-9]+)',
'{',
'return',
'std::make_shared',
'<',
'(?<specName>(facebook::react::|react::|::|)Native[%A-Za-z0-9]+SpecJSI)',
'>',
'\(',
'self',
',',
'\k<jsInvokerInstance>',
',',
'\k<perfLoggerInstance>',
'\)',
';',
'}',
)
var replaceString = `- (std::shared_ptr<$<turboModuleClass>>)
getTurboModuleWithJsInvoker:(std::shared_ptr<$<callInvokerClass>>)$<jsInvokerInstance>
nativeInvoker:(std::shared_ptr<$<callInvokerClass>>)nativeInvoker
perfLogger:(id<RCTTurboModulePerformanceLogger>)$<perfLoggerInstance>
{
return std::make_shared<$<specName>>(self, $<jsInvokerInstance>, nativeInvoker, $<perfLoggerInstance>);
}`
const exec = require('../lib/exec');
const abspath = require('../lib/abspath');
const relpath = require('../lib/relpath');
const readFile = (filename) => require('fs').readFileSync(filename, 'utf8');
const writeFile = (filename, content) => require('fs').writeFileSync(filename, content);
function main() {
const tmFiles = exec('cd ~/fbsource && xbgs -n 10000 -l getTurboModuleWithJsInvoker:').split('\n').filter(Boolean);
tmFiles
.filter((filename) => !filename.includes('microsoft-fork-of-react-native'))
.map(abspath)
.forEach((filename) => {
const source = readFile(filename);
const newSource = source.replace(new RegExp(regexString, 'g'), replaceString);
if (source == newSource) {
console.log(relpath(filename));
}
writeFile(filename, newSource);
});
}
if (!module.parent) {
main();
}
```
Changelog: [Internal]
Reviewed By: fkgozali
Differential Revision: D20809202
fbshipit-source-id: 5d39b3cacdaa5681b70ce1803351d0432dd74550
2020-04-03 12:24:31 +03:00
|
|
|
return std::make_shared<NativeDeviceInfoSpecJSI>(self, jsInvoker, nativeInvoker, perfLogger);
|
2019-08-22 00:28:16 +03:00
|
|
|
}
|
2017-03-18 02:47:51 +03:00
|
|
|
|
|
|
|
@end
|
2019-08-22 00:28:16 +03:00
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
Class RCTDeviceInfoCls(void)
|
|
|
|
{
|
2019-08-22 00:28:16 +03:00
|
|
|
return RCTDeviceInfo.class;
|
|
|
|
}
|