From 6d822b4ca3e282e8a5a56c99018bd0cab2c8ec21 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Fri, 11 Aug 2023 14:35:47 -0700 Subject: [PATCH] make notch detection more future proof (#38940) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38940 ## Changelog: [Internal] - make notch detection more future proof current implementation of `isIPhoneX_deprecated` is not completely correct and is already 2 iphones behind. i update the implementation so we don't have to change it with every new iphone that comes out. Reviewed By: cipolleschi Differential Revision: D48254737 fbshipit-source-id: a7f45d70b06ab6c6d4fe036885c8a623fed0958e --- .../React/CoreModules/RCTDeviceInfo.mm | 23 +++++-------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 78e53899778..117ccf29f77 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -71,30 +71,19 @@ RCT_EXPORT_MODULE() object:nil]; } -static BOOL RCTIsIPhoneX() +static BOOL RCTIsIPhoneNotched() { - static BOOL isIPhoneX = NO; + static BOOL isIPhoneNotched = NO; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ RCTAssertMainQueue(); - CGSize screenSize = [UIScreen mainScreen].nativeBounds.size; - CGSize iPhoneXScreenSize = CGSizeMake(1125, 2436); - CGSize iPhoneXMaxScreenSize = CGSizeMake(1242, 2688); - CGSize iPhoneXRScreenSize = CGSizeMake(828, 1792); - CGSize iPhone12ScreenSize = CGSizeMake(1170, 2532); - CGSize iPhone12MiniScreenSize = CGSizeMake(1080, 2340); - CGSize iPhone12ProMaxScreenSize = CGSizeMake(1284, 2778); - - isIPhoneX = CGSizeEqualToSize(screenSize, iPhoneXScreenSize) || - CGSizeEqualToSize(screenSize, iPhoneXMaxScreenSize) || CGSizeEqualToSize(screenSize, iPhoneXRScreenSize) || - CGSizeEqualToSize(screenSize, iPhone12ScreenSize) || CGSizeEqualToSize(screenSize, iPhone12MiniScreenSize) || - CGSizeEqualToSize(screenSize, iPhone12ProMaxScreenSize); - ; + // 20pt is the top safeArea value in non-notched devices + isIPhoneNotched = RCTSharedApplication().keyWindow.safeAreaInsets.top > 20; }); - return isIPhoneX; + return isIPhoneNotched; } static NSDictionary *RCTExportedDimensions(RCTModuleRegistry *moduleRegistry, RCTBridge *bridge) @@ -142,7 +131,7 @@ static NSDictionary *RCTExportedDimensions(RCTModuleRegistry *moduleRegistry, RC // This prop is deprecated and will be removed in a future release. // Please use this only for a quick and temporary solution. // Use instead. - @"isIPhoneX_deprecated" : @(RCTIsIPhoneX()), + @"isIPhoneX_deprecated" : @(RCTIsIPhoneNotched()), }; });