Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2001,6 +2001,7 @@ namespace JS {
RCTRequired<NSString *> osVersion;
RCTRequired<NSString *> systemName;
RCTRequired<NSString *> interfaceIdiom;
RCTRequired<bool> isCatalyst;
};

/** Initialize with a set of values */
Expand Down Expand Up @@ -3431,6 +3432,8 @@ inline JS::NativePlatformConstantsIOS::Constants::Builder::Builder(const Input i
d[@"systemName"] = systemName;
auto interfaceIdiom = i.interfaceIdiom.get();
d[@"interfaceIdiom"] = interfaceIdiom;
auto isCatalyst = i.isCatalyst.get();
d[@"isCatalyst"] = @(isCatalyst);
return d;
}) {}
inline JS::NativePlatformConstantsIOS::Constants::Builder::Builder(Constants i) : _factory(^{
Expand Down
1 change: 1 addition & 0 deletions Libraries/Utilities/NativePlatformConstantsIOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Spec extends TurboModule {
osVersion: string,
systemName: string,
interfaceIdiom: string,
isCatalyst: boolean,
|};
}

Expand Down
4 changes: 4 additions & 0 deletions Libraries/Utilities/Platform.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const Platform = {
get isTV(): boolean {
return this.constants.uiMode === 'tv';
},
// $FlowFixMe[unsafe-getters-setters]
get isCatalyst(): boolean {
return false;
},
select: <A, N, D>(spec: PlatformSelectSpec<A, N, D>): A | N | D =>
'android' in spec
? spec.android
Expand Down
5 changes: 5 additions & 0 deletions Libraries/Utilities/Platform.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const Platform = {
prerelease: ?number,
|},
systemName: string,
isCatalyst: boolean,
|} {
if (this.__constants == null) {
this.__constants = NativePlatformConstantsIOS.getConstants();
Expand Down Expand Up @@ -67,6 +68,10 @@ const Platform = {
}
return false;
},
// $FlowFixMe[unsafe-getters-setters]
get isCatalyst(): boolean {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer this to called isMacCatalyst() as it's more clear and more inline with how Apple markets the technology.

My general concern is that this will force all platforms to implement a isMacCatalyst() method, which is very specific, so I wonder if we should instead go with a method called isDesktop() to be more inclusive for our other platforms. That way react-native-macos and react-native-windows can return true for this method as well.

Thoughts @alloy?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that, I’m just a little concerned about the boundaries. Would this return true on iPad OS or what is the definition of a desktop machine?

@appden appden Sep 18, 2020 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the purpose is it would return true for macOS (AppKit or UIKit) and Windows. 😄

Although lots of people use iPad as a desktop, I think the general expectation is that it is not classified as a desktop. If that's a controversial opinion, then we can consider another name. We already have isPad() and isTV() methods that describe the type of device rather than particular OS, so I think isDesktop() would fit nicely in there.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, perhaps desktops and iPads together are ‘workstations’? In any case, seems like we have enough ways to narrow down the type, so I’m good with ‘desktop’ 👍

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just include it inside Platform.OS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@EhsanSarshar because Mac Catalyst isn't a platform itself, it's running the iOS app on macOS, so its behavior should mostly follow iOS except for some cases where we want it to be desktop.

Also, Platform.OS is handled by the packager (Metro) during build time, not here in React Native. See this PR for the implementation of Platform.select with mac: react/metro#495.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charpeni Will you update this to be isDesktop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

return this.constants.isCatalyst;
},
select: <D, N, I>(spec: PlatformSelectSpec<D, N, I>): D | N | I =>
'ios' in spec ? spec.ios : 'native' in spec ? spec.native : spec.default,
};
Expand Down
6 changes: 6 additions & 0 deletions Libraries/Utilities/__tests__/Platform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ describe('Platform', () => {
expect(PlatformAndroid.select(obj)).toEqual(obj.default);
});
});

describe('isCatalyst', () => {
it('should return false for android', () => {
expect(PlatformAndroid.isCatalyst).toBe(false);
});
});
});
9 changes: 9 additions & 0 deletions React/CoreModules/RCTPlatform.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
}
}

static BOOL isCatalyst() {
#if TARGET_OS_MACCATALYST
return YES;
#else
return NO;
#endif
}

@interface RCTPlatform () <NativePlatformConstantsIOSSpec>
@end

Expand Down Expand Up @@ -68,6 +76,7 @@ - (dispatch_queue_t)methodQueue
.systemName = [device systemName],
.interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]),
.isTesting = RCTRunningInTestEnvironment() ? true : false,
.isCatalyst = isCatalyst() ? true : false,
.reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder(
{.minor = [versions[@"minor"] doubleValue],
.major = [versions[@"major"] doubleValue],
Expand Down