Skip to content

Commit 0a07004

Browse files
authored
[0.72][Cherry Pick] Remove some usages of APIs deprecated in iOS & removed in visionOS (#2036)
1 parent 1724498 commit 0a07004

File tree

10 files changed

+84
-57
lines changed

10 files changed

+84
-57
lines changed

packages/react-native/Libraries/Text/Text/RCTTextView.m

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ - (BOOL)canBecomeKeyView
3737

3838
@end
3939

40-
@interface RCTTextView () <NSTextViewDelegate>
41-
@end
40+
#endif // macOS]
41+
42+
#if !TARGET_OS_OSX // [macOS]
43+
@interface RCTTextView () <UIEditMenuInteractionDelegate>
4244

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

50+
@end
51+
4552
#import <QuartzCore/QuartzCore.h>
4653

4754
@implementation RCTTextView {
@@ -358,6 +365,10 @@ - (void)enableContextMenu
358365
{
359366
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
360367
action:@selector(handleLongPress:)];
368+
if (@available(iOS 16.0, *)) {
369+
_editMenuInteraction = [[UIEditMenuInteraction alloc] initWithDelegate:self];
370+
[self addInteraction:_editMenuInteraction];
371+
}
361372
[self addGestureRecognizer:_longPressGestureRecognizer];
362373
}
363374

@@ -369,8 +380,16 @@ - (void)disableContextMenu
369380

370381
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
371382
{
372-
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
373383
#if !TARGET_OS_UIKITFORMAC
384+
if (@available(iOS 16.0, *)) {
385+
CGPoint location = [gesture locationInView:self];
386+
UIEditMenuConfiguration *config = [UIEditMenuConfiguration configurationWithIdentifier:nil sourcePoint:location];
387+
if (_editMenuInteraction) {
388+
[_editMenuInteraction presentEditMenuWithConfiguration:config];
389+
}
390+
return;
391+
}
392+
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
374393
UIMenuController *menuController = [UIMenuController sharedMenuController];
375394

376395
if (menuController.isMenuVisible) {

packages/react-native/React/Base/RCTUtils.m

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,14 @@ static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, disp
305305
void RCTComputeScreenScale()
306306
{
307307
dispatch_once(&onceTokenScreenScale, ^{
308-
screenScale = [UIScreen mainScreen].scale;
308+
screenScale = [UITraitCollection currentTraitCollection].displayScale;
309309
});
310310
}
311311

312312
CGFloat RCTScreenScale()
313313
{
314314
RCTUnsafeExecuteOnMainQueueOnceSync(&onceTokenScreenScale, ^{
315-
screenScale = [UIScreen mainScreen].scale;
315+
screenScale = [UITraitCollection currentTraitCollection].displayScale;
316316
});
317317

318318
return screenScale;
@@ -600,12 +600,20 @@ BOOL RCTRunningInAppExtension(void)
600600
return nil;
601601
}
602602

603-
// TODO: replace with a more robust solution
604-
for (UIWindow *window in RCTSharedApplication().windows) {
605-
if (window.keyWindow) {
606-
return window;
603+
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
604+
if (scene.activationState != UISceneActivationStateForegroundActive ||
605+
![scene isKindOfClass:[UIWindowScene class]]) {
606+
continue;
607+
}
608+
UIWindowScene *windowScene = (UIWindowScene *)scene;
609+
610+
for (UIWindow *window in windowScene.windows) {
611+
if (window.isKeyWindow) {
612+
return window;
613+
}
607614
}
608615
}
616+
609617
return nil;
610618
}
611619

@@ -630,12 +638,10 @@ BOOL RCTForceTouchAvailable(void)
630638
static BOOL forceSupported;
631639
static dispatch_once_t onceToken;
632640
dispatch_once(&onceToken, ^{
633-
forceSupported =
634-
[UITraitCollection class] && [UITraitCollection instancesRespondToSelector:@selector(forceTouchCapability)];
641+
forceSupported = [UITraitCollection currentTraitCollection].forceTouchCapability == UIForceTouchCapabilityAvailable;
635642
});
636643

637-
return forceSupported &&
638-
(RCTKeyWindow() ?: [UIView new]).traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
644+
return forceSupported;
639645
}
640646
#endif // [macOS]
641647

packages/react-native/React/CoreModules/RCTAlertController.m

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,7 @@ @implementation RCTAlertController
2323
- (UIWindow *)alertWindow
2424
{
2525
if (_alertWindow == nil) {
26-
_alertWindow = [self getUIWindowFromScene];
27-
28-
if (_alertWindow == nil) {
29-
UIWindow *keyWindow = RCTSharedApplication().keyWindow;
30-
if (keyWindow) {
31-
_alertWindow = [[UIWindow alloc] initWithFrame:keyWindow.bounds];
32-
} else {
33-
// keyWindow is nil, so we cannot create and initialize _alertWindow
34-
NSLog(@"Unable to create alert window: keyWindow is nil");
35-
}
36-
}
26+
_alertWindow = [[UIWindow alloc] initWithWindowScene:RCTKeyWindow().windowScene];
3727

3828
if (_alertWindow) {
3929
_alertWindow.rootViewController = [UIViewController new];
@@ -78,19 +68,6 @@ - (void)hide
7868

7969
_alertWindow = nil;
8070
}
81-
82-
- (UIWindow *)getUIWindowFromScene
83-
{
84-
if (@available(iOS 13.0, *)) {
85-
for (UIScene *scene in RCTSharedApplication().connectedScenes) {
86-
if (scene.activationState == UISceneActivationStateForegroundActive &&
87-
[scene isKindOfClass:[UIWindowScene class]]) {
88-
return [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
89-
}
90-
}
91-
}
92-
return nil;
93-
}
9471
#endif // [macOS]
9572

9673
@end

packages/react-native/React/CoreModules/RCTDevLoadingView.mm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ - (void)showMessage:(NSString *)message color:(RCTUIColor *)color backgroundColo
139139

140140
dispatch_async(dispatch_get_main_queue(), ^{
141141
self->_showDate = [NSDate date];
142+
142143
if (!self->_window && !RCTRunningInTestEnvironment()) {
143144
#if !TARGET_OS_OSX // [macOS]
144-
CGSize screenSize = [UIScreen mainScreen].bounds.size;
145+
UIWindow *window = RCTKeyWindow();
146+
CGFloat windowWidth = window.bounds.size.width;
145147

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

153153
self->_window.windowLevel = UIWindowLevelStatusBar + 1;
@@ -186,6 +186,7 @@ - (void)showMessage:(NSString *)message color:(RCTUIColor *)color backgroundColo
186186
#else // [macOS
187187
self->_label.stringValue = message;
188188
self->_label.textColor = color;
189+
189190
self->_label.backgroundColor = backgroundColor;
190191
[self->_window orderFront:nil];
191192
#endif // macOS]

packages/react-native/React/Fabric/Mounting/ComponentViews/Text/RCTParagraphComponentView.mm

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727

2828
using namespace facebook::react;
2929

30+
#if !TARGET_OS_OSX // [macOS]
31+
@interface RCTParagraphComponentView () <UIEditMenuInteractionDelegate>
32+
33+
@property (nonatomic, nullable) UIEditMenuInteraction *editMenuInteraction API_AVAILABLE(ios(16.0));
34+
35+
@end
36+
#endif // [macOS]
37+
3038
@implementation RCTParagraphComponentView {
3139
ParagraphShadowNode::ConcreteState::Shared _state;
3240
ParagraphAttributes _paragraphAttributes;
@@ -223,19 +231,36 @@ - (void)enableContextMenu
223231
{
224232
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
225233
action:@selector(handleLongPress:)];
234+
235+
if (@available(iOS 16.0, *)) {
236+
_editMenuInteraction = [[UIEditMenuInteraction alloc] initWithDelegate:self];
237+
[self addInteraction:_editMenuInteraction];
238+
}
226239
[self addGestureRecognizer:_longPressGestureRecognizer];
227240
}
228241

229242
- (void)disableContextMenu
230243
{
231244
[self removeGestureRecognizer:_longPressGestureRecognizer];
245+
if (@available(iOS 16.0, *)) {
246+
[self removeInteraction:_editMenuInteraction];
247+
_editMenuInteraction = nil;
248+
}
232249
_longPressGestureRecognizer = nil;
233250
}
234251

235252
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
236253
{
237-
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
238254
#if !TARGET_OS_UIKITFORMAC
255+
if (@available(iOS 16.0, *)) {
256+
CGPoint location = [gesture locationInView:self];
257+
UIEditMenuConfiguration *config = [UIEditMenuConfiguration configurationWithIdentifier:nil sourcePoint:location];
258+
if (_editMenuInteraction) {
259+
[_editMenuInteraction presentEditMenuWithConfiguration:config];
260+
}
261+
return;
262+
}
263+
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
239264
UIMenuController *menuController = [UIMenuController sharedMenuController];
240265

241266
if (menuController.isMenuVisible) {

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
255255
if (oldViewProps.shouldRasterize != newViewProps.shouldRasterize) {
256256
self.layer.shouldRasterize = newViewProps.shouldRasterize;
257257
#if !TARGET_OS_OSX // [macOS]
258-
self.layer.rasterizationScale = newViewProps.shouldRasterize ? [UIScreen mainScreen].scale : 1.0;
258+
self.layer.rasterizationScale = newViewProps.shouldRasterize ? self.traitCollection.displayScale : 1.0;
259259
#else // [macOS
260260
self.layer.rasterizationScale = 1.0;
261261
#endif // macOS]

packages/react-native/React/UIUtils/RCTUIUtils.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ RCTDimensions RCTGetDimensions(CGFloat fontScale)
1515
UIScreen *mainScreen = UIScreen.mainScreen;
1616
CGSize screenSize = mainScreen.bounds.size;
1717

18-
UIView *mainWindow;
19-
mainWindow = RCTKeyWindow();
18+
UIView *mainWindow = RCTKeyWindow();
2019
// We fallback to screen size if a key window is not found.
2120
CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize;
2221

packages/react-native/React/Views/RCTViewManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ - (RCTShadowView *)shadowView
235235
{
236236
view.layer.shouldRasterize = json ? [RCTConvert BOOL:json] : defaultView.layer.shouldRasterize;
237237
view.layer.rasterizationScale =
238-
view.layer.shouldRasterize ? [UIScreen mainScreen].scale : defaultView.layer.rasterizationScale;
238+
view.layer.shouldRasterize ? view.traitCollection.displayScale : defaultView.layer.rasterizationScale;
239239
}
240240
#endif // [macOS]
241241

packages/rn-tester/RCTTest/FBSnapshotTestCase/FBSnapshotTestController.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,15 @@ - (NSString *)_fileNameForSelector:(SEL)selector
240240
if (0 < identifier.length) {
241241
fileName = [fileName stringByAppendingFormat:@"_%@", identifier];
242242
}
243-
CGFloat scale; // [macOS
244-
#if !TARGET_OS_OSX
245-
scale = [[UIScreen mainScreen] scale];
246-
#else
247-
scale = [[NSScreen mainScreen] backingScaleFactor];
248-
#endif
249-
if (scale > 1.0) { // macOS]
243+
#if !TARGET_OS_OSX // [macOS]
244+
UITraitCollection *currentTraitCollection = [UITraitCollection currentTraitCollection];
245+
if (currentTraitCollection.displayScale > 1.0) {
246+
fileName = [fileName stringByAppendingFormat:@"@%.fx", currentTraitCollection.displayScale];
247+
#else // [macOS
248+
CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
249+
if (scale > 1.0) {
250250
fileName = [fileName stringByAppendingFormat:@"@%.fx", scale];
251+
#endif // macOS]
251252
}
252253
fileName = [fileName stringByAppendingPathExtension:@"png"];
253254
return fileName;

packages/rn-tester/RCTTest/FBSnapshotTestCase/UIImage+Compare.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,13 @@ - (BOOL)compareWithImage:(UIImage *)image
4545
(CGBitmapInfo)kCGImageAlphaPremultipliedLast);
4646

4747
#if !TARGET_OS_OSX // [macOS]
48-
CGFloat scaleFactor = [UIScreen mainScreen].scale;
48+
CGFloat scaleFactor = [UITraitCollection currentTraitCollection].displayScale;
4949
#else // [macOS
5050
// The compareWithImage: method is used for integration test snapshot image comparison.
5151
// The _snapshotView: method that creates snapshot images that are *not* scaled for the screen.
5252
// By not using the screen scale factor in this method the test results are machine independent.
5353
CGFloat scaleFactor = 1;
5454
#endif // macOS]
55-
5655
CGContextScaleCTM(referenceImageContext, scaleFactor, scaleFactor);
5756
CGContextScaleCTM(imageContext, scaleFactor, scaleFactor);
5857

0 commit comments

Comments
 (0)