Skip to content

Commit

Permalink
[Cherry Pick] Remove some usages of APIs deprecated in iOS & removed …
Browse files Browse the repository at this point in the history
…in visionOS (#2034)
  • Loading branch information
Saadnajmi committed Jan 15, 2024
1 parent 6128427 commit 01efe83
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 59 deletions.
25 changes: 22 additions & 3 deletions packages/react-native/Libraries/Text/Text/RCTTextView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ - (BOOL)canBecomeKeyView

@end

@interface RCTTextView () <NSTextViewDelegate>
@end
#endif // macOS]

#if !TARGET_OS_OSX // [macOS]
@interface RCTTextView () <UIEditMenuInteractionDelegate>

@property (nonatomic, nullable) UIEditMenuInteraction *editMenuInteraction API_AVAILABLE(ios(16.0));
#else // [macOS
@interface RCTTextView () <NSTextViewDelegate>
#endif // macOS]

@end

#import <QuartzCore/QuartzCore.h>

@implementation RCTTextView {
Expand Down Expand Up @@ -351,6 +358,10 @@ - (void)enableContextMenu
{
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPress:)];
if (@available(iOS 16.0, *)) {
_editMenuInteraction = [[UIEditMenuInteraction alloc] initWithDelegate:self];
[self addInteraction:_editMenuInteraction];
}
[self addGestureRecognizer:_longPressGestureRecognizer];
}

Expand All @@ -362,8 +373,16 @@ - (void)disableContextMenu

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
#if !TARGET_OS_UIKITFORMAC
if (@available(iOS 16.0, *)) {
CGPoint location = [gesture locationInView:self];
UIEditMenuConfiguration *config = [UIEditMenuConfiguration configurationWithIdentifier:nil sourcePoint:location];
if (_editMenuInteraction) {
[_editMenuInteraction presentEditMenuWithConfiguration:config];
}
return;
}
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
UIMenuController *menuController = [UIMenuController sharedMenuController];

if (menuController.isMenuVisible) {
Expand Down
26 changes: 16 additions & 10 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, disp
void RCTComputeScreenScale(void)
{
dispatch_once(&onceTokenScreenScale, ^{
screenScale = [UIScreen mainScreen].scale;
screenScale = [UITraitCollection currentTraitCollection].displayScale;
});
}

CGFloat RCTScreenScale(void)
{
RCTUnsafeExecuteOnMainQueueOnceSync(&onceTokenScreenScale, ^{
screenScale = [UIScreen mainScreen].scale;
screenScale = [UITraitCollection currentTraitCollection].displayScale;
});

return screenScale;
Expand Down Expand Up @@ -600,12 +600,20 @@ BOOL RCTRunningInAppExtension(void)
return nil;
}

// TODO: replace with a more robust solution
for (UIWindow *window in RCTSharedApplication().windows) {
if (window.keyWindow) {
return window;
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState != UISceneActivationStateForegroundActive ||
![scene isKindOfClass:[UIWindowScene class]]) {
continue;
}
UIWindowScene *windowScene = (UIWindowScene *)scene;

for (UIWindow *window in windowScene.windows) {
if (window.isKeyWindow) {
return window;
}
}
}

return nil;
#else // [macOS
return [NSApp keyWindow];
Expand Down Expand Up @@ -634,12 +642,10 @@ BOOL RCTForceTouchAvailable(void)
static BOOL forceSupported;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
forceSupported =
[UITraitCollection class] && [UITraitCollection instancesRespondToSelector:@selector(forceTouchCapability)];
forceSupported = [UITraitCollection currentTraitCollection].forceTouchCapability == UIForceTouchCapabilityAvailable;
});

return forceSupported &&
(RCTKeyWindow() ?: [UIView new]).traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
return forceSupported;
}
#endif // [macOS]

Expand Down
24 changes: 1 addition & 23 deletions packages/react-native/React/CoreModules/RCTAlertController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,7 @@ @implementation RCTAlertController
- (UIWindow *)alertWindow
{
if (_alertWindow == nil) {
_alertWindow = [self getUIWindowFromScene];

if (_alertWindow == nil) {
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
if (keyWindow) {
_alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
} else {
// keyWindow is nil, so we cannot create and initialize _alertWindow
NSLog(@"Unable to create alert window: keyWindow is nil");
}
}
_alertWindow = [[UIWindow alloc] initWithWindowScene:RCTKeyWindow().windowScene];

if (_alertWindow) {
_alertWindow.rootViewController = [UIViewController new];
Expand Down Expand Up @@ -67,18 +57,6 @@ - (void)hide

_alertWindow = nil;
}

- (UIWindow *)getUIWindowFromScene
{
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
if (scene.activationState == UISceneActivationStateForegroundActive &&
[scene isKindOfClass:[UIWindowScene class]]) {
return [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
}
}

return nil;
}
#endif // [macOS]

@end
16 changes: 7 additions & 9 deletions packages/react-native/React/CoreModules/RCTDevLoadingView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ - (void)showMessage:(NSString *)message color:(RCTUIColor *)color backgroundColo

dispatch_async(dispatch_get_main_queue(), ^{
self->_showDate = [NSDate date];

if (!self->_window && !RCTRunningInTestEnvironment()) {
#if !TARGET_OS_OSX // [macOS]
CGSize screenSize = [UIScreen mainScreen].bounds.size;
UIWindow *window = RCTKeyWindow();
CGFloat windowWidth = window.bounds.size.width;

UIWindow *window = RCTSharedApplication().keyWindow;
self->_window =
[[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, window.safeAreaInsets.top + 10)];
self->_label =
[[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top - 10, screenSize.width, 20)];
self->_window = [[UIWindow alloc] initWithWindowScene:window.windowScene];
self->_window.frame = CGRectMake(0, 0, windowWidth, window.safeAreaInsets.top + 10);
self->_label = [[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top - 10, windowWidth, 20)];
[self->_window addSubview:self->_label];

self->_window.windowLevel = UIWindowLevelStatusBar + 1;
Expand Down Expand Up @@ -164,12 +164,10 @@ - (void)showMessage:(NSString *)message color:(RCTUIColor *)color backgroundColo

self->_window.backgroundColor = backgroundColor;
self->_window.hidden = NO;

UIWindowScene *scene = (UIWindowScene *)RCTSharedApplication().connectedScenes.anyObject;
self->_window.windowScene = scene;
#else // [macOS
self->_label.stringValue = message;
self->_label.textColor = color;

self->_label.backgroundColor = backgroundColor;
[self->_window orderFront:nil];
#endif // macOS]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@

using namespace facebook::react;

#if !TARGET_OS_OSX // [macOS]
@interface RCTParagraphComponentView () <UIEditMenuInteractionDelegate>

@property (nonatomic, nullable) UIEditMenuInteraction *editMenuInteraction API_AVAILABLE(ios(16.0));

@end
#endif // [macOS]

@implementation RCTParagraphComponentView {
ParagraphShadowNode::ConcreteState::Shared _state;
ParagraphAttributes _paragraphAttributes;
Expand Down Expand Up @@ -223,19 +231,36 @@ - (void)enableContextMenu
{
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPress:)];

if (@available(iOS 16.0, *)) {
_editMenuInteraction = [[UIEditMenuInteraction alloc] initWithDelegate:self];
[self addInteraction:_editMenuInteraction];
}
[self addGestureRecognizer:_longPressGestureRecognizer];
}

- (void)disableContextMenu
{
[self removeGestureRecognizer:_longPressGestureRecognizer];
if (@available(iOS 16.0, *)) {
[self removeInteraction:_editMenuInteraction];
_editMenuInteraction = nil;
}
_longPressGestureRecognizer = nil;
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
#if !TARGET_OS_UIKITFORMAC
if (@available(iOS 16.0, *)) {
CGPoint location = [gesture locationInView:self];
UIEditMenuConfiguration *config = [UIEditMenuConfiguration configurationWithIdentifier:nil sourcePoint:location];
if (_editMenuInteraction) {
[_editMenuInteraction presentEditMenuWithConfiguration:config];
}
return;
}
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
UIMenuController *menuController = [UIMenuController sharedMenuController];

if (menuController.isMenuVisible) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
if (oldViewProps.shouldRasterize != newViewProps.shouldRasterize) {
self.layer.shouldRasterize = newViewProps.shouldRasterize;
#if !TARGET_OS_OSX // [macOS]
self.layer.rasterizationScale = newViewProps.shouldRasterize ? [UIScreen mainScreen].scale : 1.0;
self.layer.rasterizationScale = newViewProps.shouldRasterize ? self.traitCollection.displayScale : 1.0;
#else // [macOS
self.layer.rasterizationScale = 1.0;
#endif // macOS]
Expand Down
3 changes: 1 addition & 2 deletions packages/react-native/React/UIUtils/RCTUIUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ RCTDimensions RCTGetDimensions(CGFloat fontScale)
UIScreen *mainScreen = UIScreen.mainScreen;
CGSize screenSize = mainScreen.bounds.size;

UIView *mainWindow;
mainWindow = RCTKeyWindow();
UIView *mainWindow = RCTKeyWindow();
// We fallback to screen size if a key window is not found.
CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize;
#else // [macOS
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/React/Views/RCTViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ - (RCTShadowView *)shadowView
{
view.layer.shouldRasterize = json ? [RCTConvert BOOL:json] : defaultView.layer.shouldRasterize;
view.layer.rasterizationScale =
view.layer.shouldRasterize ? [UIScreen mainScreen].scale : defaultView.layer.rasterizationScale;
view.layer.shouldRasterize ? view.traitCollection.displayScale : defaultView.layer.rasterizationScale;
}
#endif // [macOS]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,15 @@ - (NSString *)_fileNameForSelector:(SEL)selector
if (0 < identifier.length) {
fileName = [fileName stringByAppendingFormat:@"_%@", identifier];
}
CGFloat scale; // [macOS
#if !TARGET_OS_OSX
scale = [[UIScreen mainScreen] scale];
#else
scale = [[NSScreen mainScreen] backingScaleFactor];
#endif
if (scale > 1.0) { // macOS]
#if !TARGET_OS_OSX // [macOS]
UITraitCollection *currentTraitCollection = [UITraitCollection currentTraitCollection];
if (currentTraitCollection.displayScale > 1.0) {
fileName = [fileName stringByAppendingFormat:@"@%.fx", currentTraitCollection.displayScale];
#else // [macOS
CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
if (scale > 1.0) {
fileName = [fileName stringByAppendingFormat:@"@%.fx", scale];
#endif // macOS]
}
fileName = [fileName stringByAppendingPathExtension:@"png"];
return fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ - (BOOL)compareWithImage:(UIImage *)image
(CGBitmapInfo)kCGImageAlphaPremultipliedLast);

#if !TARGET_OS_OSX // [macOS]
CGFloat scaleFactor = [UIScreen mainScreen].scale;
CGFloat scaleFactor = [UITraitCollection currentTraitCollection].displayScale;
#else // [macOS
// The compareWithImage: method is used for integration test snapshot image comparison.
// The _snapshotView: method that creates snapshot images that are *not* scaled for the screen.
// By not using the screen scale factor in this method the test results are machine independent.
CGFloat scaleFactor = 1;
#endif // macOS]

CGContextScaleCTM(referenceImageContext, scaleFactor, scaleFactor);
CGContextScaleCTM(imageContext, scaleFactor, scaleFactor);

Expand Down

0 comments on commit 01efe83

Please sign in to comment.