Skip to content

Commit 587d849

Browse files
generatedunixname89002005232357facebook-github-bot
authored andcommitted
Revert D70200854
Summary: This diff reverts D70200854 To revert for S494669 #bypass-github-export-checks bypass-github-export-checks Reviewed By: NickGerleman Differential Revision: D70347605 fbshipit-source-id: 454cb0a8f5a7562cd6602c32f4978b129821730c
1 parent 144f44f commit 587d849

File tree

4 files changed

+163
-41
lines changed

4 files changed

+163
-41
lines changed

packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#import "RCTInitializeUIKitProxies.h"
99
#import "RCTInitialAccessibilityValuesProxy.h"
10+
#import "RCTKeyWindowValuesProxy.h"
1011
#import "RCTTraitCollectionProxy.h"
1112
#import "RCTWindowSafeAreaProxy.h"
1213

@@ -17,5 +18,6 @@ void RCTInitializeUIKitProxies(void)
1718
[[RCTWindowSafeAreaProxy sharedInstance] startObservingSafeArea];
1819
[[RCTTraitCollectionProxy sharedInstance] startObservingTraitCollection];
1920
[[RCTInitialAccessibilityValuesProxy sharedInstance] recordAccessibilityValues];
21+
[[RCTKeyWindowValuesProxy sharedInstance] startObservingWindowSizeIfNecessary];
2022
});
2123
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface RCTKeyWindowValuesProxy : NSObject
13+
14+
+ (instancetype)sharedInstance;
15+
16+
@property (assign, readonly) CGSize windowSize;
17+
@property (assign, readonly) UIInterfaceOrientation currentInterfaceOrientation;
18+
19+
- (void)startObservingWindowSizeIfNecessary;
20+
21+
@end
22+
23+
NS_ASSUME_NONNULL_END
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTKeyWindowValuesProxy.h"
9+
#import <React/RCTAssert.h>
10+
#import <React/RCTUtils.h>
11+
#import <mutex>
12+
13+
#import <React/RCTConstants.h>
14+
15+
static NSString *const kFrameKeyPath = @"frame";
16+
17+
@implementation RCTKeyWindowValuesProxy {
18+
BOOL _isObserving;
19+
std::mutex _mutex;
20+
CGSize _currentWindowSize;
21+
UIInterfaceOrientation _currentInterfaceOrientation;
22+
}
23+
24+
+ (instancetype)sharedInstance
25+
{
26+
static RCTKeyWindowValuesProxy *sharedInstance = nil;
27+
static dispatch_once_t onceToken;
28+
dispatch_once(&onceToken, ^{
29+
sharedInstance = [RCTKeyWindowValuesProxy new];
30+
});
31+
return sharedInstance;
32+
}
33+
34+
- (instancetype)init
35+
{
36+
self = [super init];
37+
if (self) {
38+
_isObserving = NO;
39+
UIView *mainWindow = RCTKeyWindow();
40+
_currentWindowSize = mainWindow ? mainWindow.bounds.size : UIScreen.mainScreen.bounds.size;
41+
}
42+
return self;
43+
}
44+
45+
- (void)startObservingWindowSizeIfNecessary
46+
{
47+
// Accesing _isObserving must be done under the lock to avoid a race condition.
48+
// We can't hold the lock while calling RCTUnsafeExecuteOnMainQueueSync.
49+
// Therefore, reading/writing _isObserving is kept separate from calling RCTUnsafeExecuteOnMainQueueSync.
50+
{
51+
std::lock_guard<std::mutex> lock(_mutex);
52+
if (_isObserving) {
53+
return;
54+
}
55+
_isObserving = YES;
56+
}
57+
58+
// For backwards compatibility, we register for notifications from the main thread only.
59+
// On the new architecture, we are already on the main thread and RCTUnsafeExecuteOnMainQueueSync will simply call
60+
// the block.
61+
RCTUnsafeExecuteOnMainQueueSync(^{
62+
[RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil];
63+
});
64+
65+
[[NSNotificationCenter defaultCenter] addObserver:self
66+
selector:@selector(_interfaceOrientationDidChange)
67+
name:UIApplicationDidBecomeActiveNotification
68+
object:nil];
69+
}
70+
71+
- (void)observeValueForKeyPath:(NSString *)keyPath
72+
ofObject:(id)object
73+
change:(NSDictionary *)change
74+
context:(void *)context
75+
{
76+
if ([keyPath isEqualToString:kFrameKeyPath]) {
77+
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
78+
{
79+
std::lock_guard<std::mutex> lock(_mutex);
80+
_currentWindowSize = RCTKeyWindow().bounds.size;
81+
}
82+
}
83+
}
84+
85+
- (CGSize)windowSize
86+
{
87+
{
88+
std::lock_guard<std::mutex> lock(_mutex);
89+
if (_isObserving) {
90+
return _currentWindowSize;
91+
}
92+
}
93+
94+
__block CGSize size;
95+
RCTUnsafeExecuteOnMainQueueSync(^{
96+
size = RCTKeyWindow().bounds.size;
97+
});
98+
return size;
99+
}
100+
101+
- (UIInterfaceOrientation)currentInterfaceOrientation
102+
{
103+
{
104+
std::lock_guard<std::mutex> lock(_mutex);
105+
if (_isObserving) {
106+
return _currentInterfaceOrientation;
107+
}
108+
}
109+
110+
__block UIInterfaceOrientation interfaceOrientation;
111+
RCTUnsafeExecuteOnMainQueueSync(^{
112+
interfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation;
113+
});
114+
return interfaceOrientation;
115+
}
116+
117+
- (void)_interfaceOrientationDidChange
118+
{
119+
std::lock_guard<std::mutex> lock(_mutex);
120+
_currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation;
121+
}
122+
123+
@end

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

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#import <React/RCTEventDispatcherProtocol.h>
1515
#import <React/RCTInitializing.h>
1616
#import <React/RCTInvalidating.h>
17+
#import <React/RCTKeyWindowValuesProxy.h>
1718
#import <React/RCTUtils.h>
19+
#import <React/RCTWindowSafeAreaProxy.h>
1820
#import <atomic>
1921

2022
#import "CoreModulesPlugins.h"
@@ -31,34 +33,21 @@ @implementation RCTDeviceInfo {
3133
std::atomic<BOOL> _invalidated;
3234
}
3335

34-
static NSString *const kFrameKeyPath = @"frame";
35-
3636
@synthesize moduleRegistry = _moduleRegistry;
3737

3838
RCT_EXPORT_MODULE()
3939

4040
- (instancetype)init
4141
{
4242
if (self = [super init]) {
43-
[RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil];
43+
[[RCTKeyWindowValuesProxy sharedInstance] startObservingWindowSizeIfNecessary];
4444
}
4545
return self;
4646
}
4747

48-
- (void)observeValueForKeyPath:(NSString *)keyPath
49-
ofObject:(id)object
50-
change:(NSDictionary *)change
51-
context:(void *)context
52-
{
53-
if ([keyPath isEqualToString:kFrameKeyPath]) {
54-
[self interfaceFrameDidChange];
55-
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self];
56-
}
57-
}
58-
5948
+ (BOOL)requiresMainQueueSetup
6049
{
61-
return YES;
50+
return NO;
6251
}
6352

6453
- (dispatch_queue_t)methodQueue
@@ -92,7 +81,7 @@ - (void)initialize
9281

9382
#if TARGET_OS_IOS
9483

95-
_currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation;
84+
_currentInterfaceOrientation = [RCTKeyWindowValuesProxy sharedInstance].currentInterfaceOrientation;
9685

9786
[[NSNotificationCenter defaultCenter] addObserver:self
9887
selector:@selector(interfaceFrameDidChange)
@@ -131,8 +120,6 @@ - (void)_cleanupObservers
131120

132121
[[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil];
133122

134-
[RCTKeyWindow() removeObserver:self forKeyPath:kFrameKeyPath];
135-
136123
#if TARGET_OS_IOS
137124
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
138125
#endif
@@ -145,13 +132,8 @@ static BOOL RCTIsIPhoneNotched()
145132

146133
#if TARGET_OS_IOS
147134
dispatch_once(&onceToken, ^{
148-
RCTAssertMainQueue();
149-
150135
// 20pt is the top safeArea value in non-notched devices
151-
UIWindow *keyWindow = RCTKeyWindow();
152-
if (keyWindow) {
153-
isIPhoneNotched = keyWindow.safeAreaInsets.top > 20;
154-
}
136+
isIPhoneNotched = [RCTWindowSafeAreaProxy sharedInstance].currentSafeAreaInsets.top > 20;
155137
});
156138
#endif
157139

@@ -160,13 +142,11 @@ static BOOL RCTIsIPhoneNotched()
160142

161143
static NSDictionary *RCTExportedDimensions(CGFloat fontScale)
162144
{
163-
RCTAssertMainQueue();
164145
UIScreen *mainScreen = UIScreen.mainScreen;
165146
CGSize screenSize = mainScreen.bounds.size;
166-
UIView *mainWindow = RCTKeyWindow();
167147

168148
// We fallback to screen size if a key window is not found.
169-
CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize;
149+
CGSize windowSize = [RCTKeyWindowValuesProxy sharedInstance].windowSize;
170150

171151
NSDictionary<NSString *, NSNumber *> *dimsWindow = @{
172152
@"width" : @(windowSize.width),
@@ -202,20 +182,14 @@ - (NSDictionary *)_exportedDimensions
202182

203183
- (NSDictionary<NSString *, id> *)getConstants
204184
{
205-
__block NSDictionary<NSString *, id> *constants;
206-
__weak __typeof(self) weakSelf = self;
207-
RCTUnsafeExecuteOnMainQueueSync(^{
208-
constants = @{
209-
@"Dimensions" : [weakSelf _exportedDimensions],
210-
// Note:
211-
// This prop is deprecated and will be removed in a future release.
212-
// Please use this only for a quick and temporary solution.
213-
// Use <SafeAreaView> instead.
214-
@"isIPhoneX_deprecated" : @(RCTIsIPhoneNotched()),
215-
};
216-
});
217-
218-
return constants;
185+
return @{
186+
@"Dimensions" : [self _exportedDimensions],
187+
// Note:
188+
// This prop is deprecated and will be removed in a future release.
189+
// Please use this only for a quick and temporary solution.
190+
// Use <SafeAreaView> instead.
191+
@"isIPhoneX_deprecated" : @(RCTIsIPhoneNotched()),
192+
};
219193
}
220194

221195
- (void)didReceiveNewContentSizeMultiplier

0 commit comments

Comments
 (0)