From 22b3faf1ad4fd36e86184a2bf233b0ecfe7eb51e Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Wed, 8 Mar 2017 05:56:27 -0800 Subject: [PATCH] iOS: Expose fontScale to JavaScript Summary: A related Android PR is #11008. Font scale was exposed through: - The `getContentSizeMultiplier` method - The `didUpdateContentSizeMultiplier` event These are now deprecated. The reason is that there was already an API that exposed font scale. However, it was Android only. We now expose font scale through that API on iOS as well. Specifically: - Font scale is now available as `PixelRatio.getFontScale()`. - The `change` event on the `Dimensions` object now fires when font scale changes. This change also adds support for `Dimensions.get('screen')` on iOS. Previously, only `Dimensions.get('window')` was available on iOS. The motivation is that, [according to this comment](https://github.com/facebook/react-native/pull/11008#issuecomment-275123609), we'd like to deprecate `window` dimensions in favor of `screen` dimensions in the future. **Test plan (required)** Verified that `PixelRatio.getFontScale()` and the `change` event work properly in a test app. Adam Comella Microsoft Corp. Closes https://github.com/facebook/react-native/pull/12268 Differential Revision: D4673642 Pulled By: mkonicek fbshipit-source-id: 2602204da6998a96216e06f5321f28f6603e4972 --- React/Modules/RCTUIManager.m | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index 288f3d61e..bc36c8e40 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -236,8 +236,13 @@ RCT_EXPORT_MODULE() - (void)didReceiveNewContentSizeMultiplier { // Report the event across the bridge. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + [_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions" + body:RCTExportedDimensions(_bridge)]; [_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateContentSizeMultiplier" body:@([_bridge.accessibilityManager multiplier])]; +#pragma clang diagnostic pop dispatch_async(RCTGetUIManagerQueue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:RCTUIManagerWillUpdateViewsDueToContentSizeMultiplierChangeNotification @@ -260,7 +265,7 @@ RCT_EXPORT_MODULE() #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" [_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions" - body:RCTExportedDimensions()]; + body:RCTExportedDimensions(_bridge)]; #pragma clang diagnostic pop } @@ -1541,23 +1546,26 @@ RCT_EXPORT_METHOD(clearJSResponder) constants[@"customBubblingEventTypes"] = bubblingEvents; constants[@"customDirectEventTypes"] = directEvents; - constants[@"Dimensions"] = RCTExportedDimensions(); + constants[@"Dimensions"] = RCTExportedDimensions(_bridge); return constants; } -static NSDictionary *RCTExportedDimensions() +static NSDictionary *RCTExportedDimensions(RCTBridge *bridge) { RCTAssertMainQueue(); // Don't use RCTScreenSize since it the interface orientation doesn't apply to it CGRect screenSize = [[UIScreen mainScreen] bounds]; + NSDictionary *dims = @{ + @"width": @(screenSize.size.width), + @"height": @(screenSize.size.height), + @"scale": @(RCTScreenScale()), + @"fontScale": @(bridge.accessibilityManager.multiplier) + }; return @{ - @"window": @{ - @"width": @(screenSize.size.width), - @"height": @(screenSize.size.height), - @"scale": @(RCTScreenScale()), - }, + @"window": dims, + @"screen": dims }; } @@ -1580,11 +1588,6 @@ RCT_EXPORT_METHOD(configureNextLayoutAnimation:(NSDictionary *)config }]; } -RCT_EXPORT_METHOD(getContentSizeMultiplier:(nonnull RCTResponseSenderBlock)callback) -{ - callback(@[@(_bridge.accessibilityManager.multiplier)]); -} - - (void)rootViewForReactTag:(NSNumber *)reactTag withCompletion:(void (^)(UIView *view))completion { RCTAssertMainQueue(); @@ -1656,6 +1659,12 @@ static UIView *_jsResponder; [self setSize:frame.size forView:view]; } +RCT_EXPORT_METHOD(getContentSizeMultiplier:(nonnull RCTResponseSenderBlock)callback) +{ + RCTLogWarn(@"`getContentSizeMultiplier` is deprecated. Instead, use `PixelRatio.getFontScale()` and listen to the `didUpdateDimensions` event."); + callback(@[@(_bridge.accessibilityManager.multiplier)]); +} + @end @implementation RCTBridge (RCTUIManager)