Skip to content

Commit

Permalink
Add support for Platform.isMacCatalyst (#38187)
Browse files Browse the repository at this point in the history
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: #38187

Test Plan: It compiles

Reviewed By: cortinico

Differential Revision: D47664425

Pulled By: cipolleschi

fbshipit-source-id: 09f43694aa9b5f980204474f0e07779acd5ed2c7
  • Loading branch information
jacobp100 authored and facebook-github-bot committed Oct 4, 2023
1 parent 4676493 commit 027d520
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type PlatformConstantsIOS = {|
osVersion: string,
systemName: string,
interfaceIdiom: string,
isMacCatalyst?: boolean,
|};

export interface Spec extends TurboModule {
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native/Libraries/Utilities/Platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/Libraries/Utilities/Platform.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type IOSPlatform = {
prerelease: ?number,
|},
systemName: string,
isMacCatalyst?: boolean,
|},
// $FlowFixMe[unsafe-getters-setters]
get isPad(): boolean,
Expand All @@ -44,6 +45,8 @@ type IOSPlatform = {
get isTesting(): boolean,
// $FlowFixMe[unsafe-getters-setters]
get isDisableAnimations(): boolean,
// $FlowFixMe[unsafe-getters-setters]
get isMacCatalyst(): boolean,
select: <T>(spec: PlatformSelectSpec<T>) => T,
};

Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/Libraries/Utilities/Platform.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const Platform: PlatformType = {
prerelease: ?number,
|},
systemName: string,
isMacCatalyst?: boolean,
|} {
// $FlowFixMe[object-this-reference]
if (this.__constants == null) {
Expand Down Expand Up @@ -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: <T>(spec: PlatformSelectSpec<T>): T =>
// $FlowFixMe[incompatible-return]
'ios' in spec ? spec.ios : 'native' in spec ? spec.native : spec.default,
Expand Down
27 changes: 15 additions & 12 deletions packages/react-native/React/CoreModules/RCTPlatform.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ - (dispatch_queue_t)methodQueue
UIDevice *device = [UIDevice currentDevice];
auto versions = RCTGetReactNativeVersion();
constants = typedConstants<JS::NativePlatformConstantsIOS::Constants>({
.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<double>{}
: [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<double>{}
: [versions[@"prerelease"] doubleValue]}),
#if TARGET_OS_MACCATALYST
.isMacCatalyst = true,
#else
.isMacCatalyst = false,
#endif
});
});

Expand Down

0 comments on commit 027d520

Please sign in to comment.