Skip to content

Commit

Permalink
DeviceInfo: Simplify RCTExportedDimensions's API
Browse files Browse the repository at this point in the history
Summary:
RCTExportedDimensions doesn't need access to the ModuleRegistry, or the bridge. It just uses those two things to get the fontScale.

We could make RCTExportedDimensions easier to understand, by making it do fewer things (i.e: computing the fontScale up front, and passing it into RCTExportedDimensions). Let's just do that.

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D48237715

fbshipit-source-id: b3af648d88276846742d0e1192d33d180ee49dbb
  • Loading branch information
RSNara authored and Saadnajmi committed Jan 29, 2024
1 parent 84299f7 commit 0e0d2b8
Showing 1 changed file with 20 additions and 40 deletions.
60 changes: 20 additions & 40 deletions packages/react-native/React/CoreModules/RCTDeviceInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ @implementation RCTDeviceInfo {
#endif // [macOS]
}

@synthesize bridge = _bridge;
@synthesize moduleRegistry = _moduleRegistry;

RCT_EXPORT_MODULE()
Expand Down Expand Up @@ -63,7 +62,7 @@ - (void)initialize
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];

_currentInterfaceDimensions = RCTExportedDimensions(_moduleRegistry, _bridge);
_currentInterfaceDimensions = [self _exportedDimensions];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interfaceOrientationDidChange)
Expand Down Expand Up @@ -104,27 +103,10 @@ static BOOL RCTIsIPhoneX()
return isIPhoneX;
}

#if !TARGET_OS_OSX // [macOS]
static NSDictionary *RCTExportedDimensions(RCTModuleRegistry *moduleRegistry, RCTBridge *bridge)
#else // [macOS
NSDictionary *RCTExportedDimensions(RCTPlatformView *rootView, RCTBridge *bridge)
#endif // macOS]
static NSDictionary *RCTExportedDimensions(CGFloat fontScale)
{
RCTAssertMainQueue();

#if !TARGET_OS_OSX // [macOS]
RCTDimensions dimensions;
if (moduleRegistry) {
RCTAccessibilityManager *accessibilityManager =
(RCTAccessibilityManager *)[moduleRegistry moduleForName:"AccessibilityManager"];
dimensions = RCTGetDimensions(accessibilityManager ? accessibilityManager.multiplier : 1.0);
} else {
RCTAssert(false, @"ModuleRegistry must be set to properly init dimensions. Bridge exists: %d", bridge != nil);
}
#else // [macOS
RCTDimensions dimensions = RCTGetDimensions(rootView);
#endif // macOS]

RCTDimensions dimensions = RCTGetDimensions(fontScale);
__typeof(dimensions.window) window = dimensions.window;
NSDictionary<NSString *, NSNumber *> *dimsWindow = @{
@"width" : @(window.width),
Expand All @@ -142,6 +124,15 @@ static BOOL RCTIsIPhoneX()
return @{@"window" : dimsWindow, @"screen" : dimsScreen};
}

- (NSDictionary *)_exportedDimensions
{
RCTAssert(_moduleRegistry, @"ModuleRegistry must be set to properly init dimensions");
RCTAccessibilityManager *accessibilityManager =
(RCTAccessibilityManager *)[_moduleRegistry moduleForName:"AccessibilityManager"];
CGFloat fontScale = accessibilityManager ? accessibilityManager.multiplier : 1.0;
return RCTExportedDimensions(fontScale);
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
Expand All @@ -150,15 +141,10 @@ static BOOL RCTIsIPhoneX()
- (NSDictionary<NSString *, id> *)getConstants
{
__block NSDictionary<NSString *, id> *constants;
RCTModuleRegistry *moduleRegistry = _moduleRegistry;
RCTBridge *bridge = _bridge;
__weak __typeof(self) weakSelf = self;
RCTUnsafeExecuteOnMainQueueSync(^{
constants = @{
#if !TARGET_OS_OSX // [macOS]
@"Dimensions" : RCTExportedDimensions(moduleRegistry, bridge),
#else // [macOS
@"Dimensions": RCTExportedDimensions(nil, bridge),
#endif // macOS]
@"Dimensions" : [weakSelf _exportedDimensions],
// Note:
// This prop is deprecated and will be removed in a future release.
// Please use this only for a quick and temporary solution.
Expand All @@ -172,19 +158,14 @@ static BOOL RCTIsIPhoneX()

- (void)didReceiveNewContentSizeMultiplier
{
__weak __typeof(self) weakSelf = self;
RCTModuleRegistry *moduleRegistry = _moduleRegistry;
RCTBridge *bridge = _bridge;
RCTExecuteOnMainQueue(^{
// Report the event across the bridge.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[moduleRegistry moduleForName:"EventDispatcher"]
sendDeviceEventWithName:@"didUpdateDimensions"
#if !TARGET_OS_OSX // [macOS]
body:RCTExportedDimensions(moduleRegistry, bridge)];
#else // [macOS
body:RCTExportedDimensions(nil, bridge)];
#endif // macOS]
[[moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateDimensions"
body:[weakSelf _exportedDimensions]];
#pragma clang diagnostic pop
});
}
Expand Down Expand Up @@ -222,9 +203,8 @@ - (void)_interfaceOrientationDidChange
if ((isOrientationChanging || isResizingOrChangingToFullscreen) && RCTIsAppActive()) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[_moduleRegistry moduleForName:"EventDispatcher"]
sendDeviceEventWithName:@"didUpdateDimensions"
body:RCTExportedDimensions(_moduleRegistry, _bridge)];
[[_moduleRegistry moduleForName:"EventDispatcher"] sendDeviceEventWithName:@"didUpdateDimensions"
body:[self _exportedDimensions]];
// We only want to track the current _currentInterfaceOrientation and _isFullscreen only
// when it happens and only when it is published.
_currentInterfaceOrientation = nextOrientation;
Expand All @@ -243,7 +223,7 @@ - (void)interfaceFrameDidChange

- (void)_interfaceFrameDidChange
{
NSDictionary *nextInterfaceDimensions = RCTExportedDimensions(_moduleRegistry, _bridge);
NSDictionary *nextInterfaceDimensions = [self _exportedDimensions];

// update and publish the even only when the app is in active state
if (!([nextInterfaceDimensions isEqual:_currentInterfaceDimensions]) && RCTIsAppActive()) {
Expand Down

0 comments on commit 0e0d2b8

Please sign in to comment.