From 027d520d657ab0c3dd30913c589692a496ad36da Mon Sep 17 00:00:00 2001 From: Jacob Parker Date: Wed, 4 Oct 2023 10:13:08 -0700 Subject: [PATCH] Add support for Platform.isMacCatalyst (#38187) Summary: Add support for `Platform.isMacCatalyst` By default, Mac catalyst reports the idiom as an iPad, but you can check if it is Mac catalyst with a macro There is technically the possibility to have the idiom return as a Mac, but that's not the default and almost definitely doesn't work in RN ignore-github-export-checks ## Changelog: [IOS] [ADDED] Add support for `Platform.isMacCatalyst` Pull Request resolved: https://github.com/facebook/react-native/pull/38187 Test Plan: It compiles Reviewed By: cortinico Differential Revision: D47664425 Pulled By: cipolleschi fbshipit-source-id: 09f43694aa9b5f980204474f0e07779acd5ed2c7 --- .../Utilities/NativePlatformConstantsIOS.js | 1 + .../Libraries/Utilities/Platform.d.ts | 2 ++ .../Libraries/Utilities/Platform.flow.js | 3 +++ .../Libraries/Utilities/Platform.ios.js | 6 +++++ .../React/CoreModules/RCTPlatform.mm | 27 ++++++++++--------- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/react-native/Libraries/Utilities/NativePlatformConstantsIOS.js b/packages/react-native/Libraries/Utilities/NativePlatformConstantsIOS.js index 1a2cf4ad5069a0..a4586079d508c8 100644 --- a/packages/react-native/Libraries/Utilities/NativePlatformConstantsIOS.js +++ b/packages/react-native/Libraries/Utilities/NativePlatformConstantsIOS.js @@ -25,6 +25,7 @@ export type PlatformConstantsIOS = {| osVersion: string, systemName: string, interfaceIdiom: string, + isMacCatalyst?: boolean, |}; export interface Spec extends TurboModule { diff --git a/packages/react-native/Libraries/Utilities/Platform.d.ts b/packages/react-native/Libraries/Utilities/Platform.d.ts index 1ff836839110be..e80241e807d934 100644 --- a/packages/react-native/Libraries/Utilities/Platform.d.ts +++ b/packages/react-native/Libraries/Utilities/Platform.d.ts @@ -50,10 +50,12 @@ interface PlatformIOSStatic extends PlatformStatic { interfaceIdiom: string; osVersion: string; systemName: string; + isMacCatalyst?: boolean | undefined; }; OS: 'ios'; isPad: boolean; isTV: boolean; + isMacCatalyst?: boolean | undefined; Version: string; } diff --git a/packages/react-native/Libraries/Utilities/Platform.flow.js b/packages/react-native/Libraries/Utilities/Platform.flow.js index d4bc8147249e36..b0c23b4fe77cc5 100644 --- a/packages/react-native/Libraries/Utilities/Platform.flow.js +++ b/packages/react-native/Libraries/Utilities/Platform.flow.js @@ -35,6 +35,7 @@ type IOSPlatform = { prerelease: ?number, |}, systemName: string, + isMacCatalyst?: boolean, |}, // $FlowFixMe[unsafe-getters-setters] get isPad(): boolean, @@ -44,6 +45,8 @@ type IOSPlatform = { get isTesting(): boolean, // $FlowFixMe[unsafe-getters-setters] get isDisableAnimations(): boolean, + // $FlowFixMe[unsafe-getters-setters] + get isMacCatalyst(): boolean, select: (spec: PlatformSelectSpec) => T, }; diff --git a/packages/react-native/Libraries/Utilities/Platform.ios.js b/packages/react-native/Libraries/Utilities/Platform.ios.js index 53371634e7d9f7..4c5377395c0b3b 100644 --- a/packages/react-native/Libraries/Utilities/Platform.ios.js +++ b/packages/react-native/Libraries/Utilities/Platform.ios.js @@ -37,6 +37,7 @@ const Platform: PlatformType = { prerelease: ?number, |}, systemName: string, + isMacCatalyst?: boolean, |} { // $FlowFixMe[object-this-reference] if (this.__constants == null) { @@ -69,6 +70,11 @@ const Platform: PlatformType = { // $FlowFixMe[object-this-reference] return this.constants.isDisableAnimations ?? this.isTesting; }, + // $FlowFixMe[unsafe-getters-setters] + get isMacCatalyst(): boolean { + // $FlowFixMe[object-this-reference] + return this.constants.isMacCatalyst ?? false; + }, select: (spec: PlatformSelectSpec): T => // $FlowFixMe[incompatible-return] 'ios' in spec ? spec.ios : 'native' in spec ? spec.native : spec.default, diff --git a/packages/react-native/React/CoreModules/RCTPlatform.mm b/packages/react-native/React/CoreModules/RCTPlatform.mm index 7c76ffb1f8a32c..d6ceed27dfa320 100644 --- a/packages/react-native/React/CoreModules/RCTPlatform.mm +++ b/packages/react-native/React/CoreModules/RCTPlatform.mm @@ -65,18 +65,21 @@ - (dispatch_queue_t)methodQueue UIDevice *device = [UIDevice currentDevice]; auto versions = RCTGetReactNativeVersion(); constants = typedConstants({ - .forceTouchAvailable = RCTForceTouchAvailable() ? true : false, - .osVersion = [device systemVersion], - .systemName = [device systemName], - .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), - .isTesting = RCTRunningInTestEnvironment() ? true : false, - .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( - {.minor = [versions[@"minor"] doubleValue], - .major = [versions[@"major"] doubleValue], - .patch = [versions[@"patch"] doubleValue], - .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] - ? std::optional{} - : [versions[@"prerelease"] doubleValue]}), + .forceTouchAvailable = RCTForceTouchAvailable() ? true : false, .osVersion = [device systemVersion], + .systemName = [device systemName], .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), + .isTesting = RCTRunningInTestEnvironment() ? true : false, + .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( + {.minor = [versions[@"minor"] doubleValue], + .major = [versions[@"major"] doubleValue], + .patch = [versions[@"patch"] doubleValue], + .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] + ? std::optional{} + : [versions[@"prerelease"] doubleValue]}), +#if TARGET_OS_MACCATALYST + .isMacCatalyst = true, +#else + .isMacCatalyst = false, +#endif }); });