Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add visionOS support #2025

Merged
merged 6 commits into from
Feb 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion RNScreens.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Pod::Spec.new do |s|
s.homepage = "https://github.com/software-mansion/react-native-screens"
s.license = "MIT"
s.author = { "author" => "author@domain.cn" }
s.platforms = { :ios => platform, :tvos => "11.0" }
s.platforms = { :ios => platform, :tvos => "11.0", :visionos => "1.0" }
s.source = { :git => "https://github.com/software-mansion/react-native-screens.git", :tag => "#{s.version}" }
s.source_files = source_files
s.requires_arc = true
Expand Down
2 changes: 2 additions & 0 deletions ios/RNSConvert.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ namespace react = facebook::react;
+ (NSDictionary *)gestureResponseDistanceDictFromCppStruct:
(const react::RNSScreenGestureResponseDistanceStruct &)gestureResponseDistance;

#if !TARGET_OS_VISION
+ (UITextAutocapitalizationType)UITextAutocapitalizationTypeFromCppEquivalent:
(react::RNSSearchBarAutoCapitalize)autoCapitalize;
#endif

+ (RNSSearchBarPlacement)RNSScreenSearchBarPlacementFromCppEquivalent:(react::RNSSearchBarPlacement)placement;

Expand Down
2 changes: 2 additions & 0 deletions ios/RNSConvert.mm
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ + (NSDictionary *)gestureResponseDistanceDictFromCppStruct:
};
}

#if !TARGET_OS_VISION
+ (UITextAutocapitalizationType)UITextAutocapitalizationTypeFromCppEquivalent:
(react::RNSSearchBarAutoCapitalize)autoCapitalize
{
Expand All @@ -137,6 +138,7 @@ + (UITextAutocapitalizationType)UITextAutocapitalizationTypeFromCppEquivalent:
return UITextAutocapitalizationTypeNone;
}
}
#endif

+ (RNSSearchBarPlacement)RNSScreenSearchBarPlacementFromCppEquivalent:(react::RNSSearchBarPlacement)placement
{
Expand Down
12 changes: 6 additions & 6 deletions ios/RNSScreen.mm
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ - (void)setActivityStateOrNil:(NSNumber *)activityStateOrNil
}
}

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
- (void)setStatusBarStyle:(RNSStatusBarStyle)statusBarStyle
{
_hasStatusBarStyleSet = YES;
Expand Down Expand Up @@ -604,7 +604,7 @@ - (BOOL)isTransparentModal
self.controller.modalPresentationStyle == UIModalPresentationOverCurrentContext;
}

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
/**
* Updates settings for sheet presentation controller.
* Note that this method should not be called inside `stackPresentation` setter, because on Paper we don't have
Expand Down Expand Up @@ -821,7 +821,7 @@ - (void)updateLayoutMetrics:(const react::LayoutMetrics &)layoutMetrics
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
[super finalizeUpdates:updateMask];
#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
[self updatePresentationStyle];
#endif // !TARGET_OS_TV
}
Expand All @@ -832,7 +832,7 @@ - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
- (void)didSetProps:(NSArray<NSString *> *)changedProps
{
[super didSetProps:changedProps];
#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
[self updatePresentationStyle];
#endif // !TARGET_OS_TV
}
Expand Down Expand Up @@ -1065,7 +1065,7 @@ - (BOOL)hasNestedStack

- (CGSize)getStatusBarHeightIsModal:(BOOL)isModal
{
#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
CGSize fallbackStatusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
Expand Down Expand Up @@ -1505,7 +1505,7 @@ @implementation RNSScreenManager
RCT_EXPORT_VIEW_PROPERTY(sheetExpandsWhenScrolledToEdge, BOOL);
#endif

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
// See:
// 1. https://github.com/software-mansion/react-native-screens/pull/1543
// 2. https://github.com/software-mansion/react-native-screens/pull/1596
Expand Down
10 changes: 5 additions & 5 deletions ios/RNSScreenStack.mm
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ - (UIViewController *)childViewControllerForHomeIndicatorAutoHidden

@end

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
@interface RNSScreenEdgeGestureRecognizer : UIScreenEdgePanGestureRecognizer
@end

Expand Down Expand Up @@ -150,7 +150,7 @@ - (void)initCommonProps
_presentedModals = [NSMutableArray new];
_controller = [RNSNavigationController new];
_controller.delegate = self;
#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
[self setupGestureHandlers];
#endif
// we have to initialize viewControllers with a non empty array for
Expand Down Expand Up @@ -731,7 +731,7 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
}
RNSScreenView *topScreen = _reactSubviews.lastObject;

#if TARGET_OS_TV
#if TARGET_OS_TV || TARGET_OS_VISION
[self cancelTouchesInParent];
return YES;
#else
Expand Down Expand Up @@ -774,7 +774,7 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
#endif // TARGET_OS_TV
}

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
- (void)setupGestureHandlers
{
// gesture recognizers for custom stack animations
Expand Down Expand Up @@ -951,7 +951,7 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
return [super hitTest:point withEvent:event];
}

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION

- (BOOL)isScrollViewPanGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
Expand Down
8 changes: 7 additions & 1 deletion ios/RNSScreenStackHeaderConfig.mm
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ + (void)setAnimatedConfig:(UIViewController *)vc withConfig:(RNSScreenStackHeade
[navbar setTitleTextAttributes:attrs];
}

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
if (@available(iOS 11.0, *)) {
if (config.largeTitle &&
(config.largeTitleFontFamily || config.largeTitleFontSize || config.largeTitleFontWeight ||
Expand Down Expand Up @@ -397,9 +397,12 @@ + (UINavigationBarAppearance *)buildAppearance:(UIViewController *)vc
if (config.titleFontFamily || config.titleFontSize || config.titleFontWeight || config.titleColor) {
NSMutableDictionary *attrs = [NSMutableDictionary new];

// Ignore changing header title color on visionOS
#if !TARGET_OS_VISION
if (config.titleColor) {
attrs[NSForegroundColorAttributeName] = config.titleColor;
}
#endif

NSString *family = config.titleFontFamily ?: nil;
NSNumber *size = config.titleFontSize ?: @17;
Expand All @@ -422,9 +425,12 @@ + (UINavigationBarAppearance *)buildAppearance:(UIViewController *)vc
config.largeTitleColor || config.titleColor) {
NSMutableDictionary *largeAttrs = [NSMutableDictionary new];

// Ignore changing header title color on visionOS
#if !TARGET_OS_VISION
if (config.largeTitleColor || config.titleColor) {
largeAttrs[NSForegroundColorAttributeName] = config.largeTitleColor ? config.largeTitleColor : config.titleColor;
}
#endif

NSString *largeFamily = config.largeTitleFontFamily ?: nil;
NSNumber *largeSize = config.largeTitleFontSize ?: @34;
Expand Down
2 changes: 1 addition & 1 deletion ios/RNSScreenWindowTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

+ (void)updateWindowTraits;

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
+ (void)assertViewControllerBasedStatusBarAppearenceSet;
#endif
+ (void)updateStatusBarAppearance;
Expand Down
58 changes: 9 additions & 49 deletions ios/RNSScreenWindowTraits.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@implementation RNSScreenWindowTraits

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
+ (void)assertViewControllerBasedStatusBarAppearenceSet
{
static dispatch_once_t once;
Expand All @@ -22,43 +22,18 @@ + (void)assertViewControllerBasedStatusBarAppearenceSet

+ (void)updateStatusBarAppearance
{
#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
[UIView animateWithDuration:0.4
animations:^{ // duration based on "Programming iOS 13" p. 311 implementation
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13, *)) {
UIWindow *firstWindow = [[[UIApplication sharedApplication] windows] firstObject];
if (firstWindow != nil) {
[[firstWindow rootViewController] setNeedsStatusBarAppearanceUpdate];
}
} else
#endif
{
[UIApplication.sharedApplication.keyWindow.rootViewController setNeedsStatusBarAppearanceUpdate];
}
[RCTKeyWindow().rootViewController setNeedsStatusBarAppearanceUpdate];
}];
#endif
}

+ (void)updateHomeIndicatorAutoHidden
{
#if !TARGET_OS_TV

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13, *)) {
UIWindow *firstWindow = [[[UIApplication sharedApplication] windows] firstObject];
if (firstWindow != nil) {
[[firstWindow rootViewController] setNeedsUpdateOfHomeIndicatorAutoHidden];
}
} else
#endif
{
if (@available(iOS 11.0, *)) {
[UIApplication.sharedApplication.keyWindow.rootViewController setNeedsUpdateOfHomeIndicatorAutoHidden];
}
}
[RCTKeyWindow().rootViewController setNeedsUpdateOfHomeIndicatorAutoHidden];
#endif
}

Expand Down Expand Up @@ -134,21 +109,10 @@ + (UIInterfaceOrientationMask)maskFromOrientation:(UIInterfaceOrientation)orient

+ (void)enforceDesiredDeviceOrientation
{
#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
dispatch_async(dispatch_get_main_queue(), ^{
UIInterfaceOrientationMask orientationMask = UIInterfaceOrientationMaskAllButUpsideDown;
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13, *)) {
UIWindow *firstWindow = [[[UIApplication sharedApplication] windows] firstObject];
if (firstWindow != nil) {
orientationMask = [firstWindow rootViewController].supportedInterfaceOrientations;
}
} else
#endif
{
orientationMask = UIApplication.sharedApplication.keyWindow.rootViewController.supportedInterfaceOrientations;
}
UIInterfaceOrientationMask orientationMask = [RCTKeyWindow().rootViewController supportedInterfaceOrientations];

UIInterfaceOrientation currentDeviceOrientation =
[RNSScreenWindowTraits interfaceOrientationFromDeviceOrientation:[[UIDevice currentDevice] orientation]];
UIInterfaceOrientation currentInterfaceOrientation = [RNSScreenWindowTraits interfaceOrientation];
Expand Down Expand Up @@ -211,19 +175,15 @@ + (void)updateWindowTraits
[RNSScreenWindowTraits updateHomeIndicatorAutoHidden];
}

#if !TARGET_OS_TV
#if !TARGET_OS_TV && !TARGET_OS_VISION
// based on
// https://stackoverflow.com/questions/57965701/statusbarorientation-was-deprecated-in-ios-13-0-when-attempting-to-get-app-ori/61249908#61249908
+ (UIInterfaceOrientation)interfaceOrientation
{
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
UIWindow *firstWindow = [[[UIApplication sharedApplication] windows] firstObject];
if (firstWindow == nil) {
return UIInterfaceOrientationUnknown;
}
UIWindowScene *windowScene = firstWindow.windowScene;
UIWindowScene *windowScene = RCTKeyWindow().windowScene;
if (windowScene == nil) {
return UIInterfaceOrientationUnknown;
}
Expand Down
2 changes: 2 additions & 0 deletions ios/RNSSearchBar.mm
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,11 @@ - (void)updateProps:(react::Props::Shared const &)props oldProps:(react::Props::
[self setPlaceholder:RCTNSStringFromStringNilIfEmpty(newScreenProps.placeholder)];
}

#if !TARGET_OS_VISION
if (oldScreenProps.autoCapitalize != newScreenProps.autoCapitalize) {
[self setAutoCapitalize:[RNSConvert UITextAutocapitalizationTypeFromCppEquivalent:newScreenProps.autoCapitalize]];
}
#endif

if (oldScreenProps.tintColor != newScreenProps.tintColor) {
[self setTintColor:RCTUIColorFromSharedColor(newScreenProps.tintColor)];
Expand Down
2 changes: 2 additions & 0 deletions ios/utils/RNSUIBarButtonItem.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#import <UIKit/UIKit.h>
okwasniewski marked this conversation as resolved.
Show resolved Hide resolved

@interface RNSUIBarButtonItem : UIBarButtonItem

@property (nonatomic) BOOL menuHidden;
Expand Down
10 changes: 10 additions & 0 deletions src/native-stack/views/HeaderConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { NativeStackNavigationOptions } from '../types';
import { useBackPressSubscription } from '../utils/useBackPressSubscription';
import { processFonts } from './FontProcessor';
import warnOnce from 'warn-once';

type Props = NativeStackNavigationOptions & {
route: Route<string>;
Expand Down Expand Up @@ -96,6 +97,15 @@ export default function HeaderConfig({
return searchBar;
}, [searchBar, createSubscription, clearSubscription]);

// @ts-ignore isVision is not yet in the type definitions (RN 0.74+)
const isVisionOS = Platform?.isVision;

warnOnce(
isVisionOS &&
(headerTitleStyle.color !== undefined || headerTintColor !== undefined),
'headerTitleStyle.color and headerTintColor are not supported on visionOS.'
);

return (
<ScreenStackHeaderConfig
backButtonInCustomView={backButtonInCustomView}
Expand Down
Loading