Skip to content

Commit 8214368

Browse files
authored
Restore macOS <Image> and ActionSheetIOS. Cherry pick multi windows UIScene support from upstream. (#256)
* Fix manual merge error. * Fix missing getters from react-native-implementation.macos.js. Cherry pick facebook#28147 into fork to Make RCTKeyWindow multi-window aware and add UIScene support to RCTRedBox. * release _window now to ensure its UIKit ivars are dealloc'd on the main thread as the RCTRedBox can be dealloc'd on a background thread
1 parent dadfc30 commit 8214368

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

Libraries/Image/RCTImageView.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge
162162
name:UIApplicationDidEnterBackgroundNotification
163163
object:nil];
164164
_imageView = [[UIImageView alloc] init];
165+
#else
166+
_imageView = [[NSImageView alloc] init];
167+
#endif // TODO(macOS ISS#2323203)
165168
_imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
166169
[self addSubview:_imageView];
167-
#endif // TODO(macOS ISS#2323203)
168170
}
169171
return self;
170172
}

Libraries/react-native/react-native-implementation.macos.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,14 @@ module.exports = {
187187
get VirtualizedList() {
188188
return require('VirtualizedList');
189189
},
190-
/*
191190
get VirtualizedSectionList() {
192191
return require('VirtualizedSectionList');
193-
},*/
192+
},
194193

195194
// APIs
196-
/*
197195
get ActionSheetIOS() {
198196
return require('ActionSheetIOS');
199-
},*/
197+
},
200198
get Alert() {
201199
return require('Alert');
202200
},

React/Base/RCTUtils.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,12 @@ BOOL RCTRunningInAppExtension(void)
524524
}
525525

526526
// TODO: replace with a more robust solution
527-
return RCTSharedApplication().keyWindow;
527+
for (UIWindow *window in RCTSharedApplication().windows) {
528+
if (window.keyWindow) {
529+
return window;
530+
}
531+
}
532+
return nil;
528533
}
529534

530535
UIViewController *__nullable RCTPresentedViewController(void)
@@ -835,7 +840,7 @@ BOOL RCTIsLocalAssetURL(NSURL *__nullable imageURL)
835840
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
836841
image = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
837842
#else // [TODO(macOS ISS#2323203)
838-
image = [bundle imageForResource:imageName];
843+
image = [bundle imageForResource:imageName];
839844
#endif // ]TODO(macOS ISS#2323203)
840845
if (image) {
841846
RCTLogWarn(@"Image %@ not found in mainBundle, but found in %@", imageName, bundle);

React/Modules/RCTRedBox.m

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ - (void)loadExtraDataViewController;
2929
@end
3030

3131
#if !TARGET_OS_OSX // TODO(macOS ISS#2323203)
32-
@interface RCTRedBoxWindow : UIWindow <UITableViewDelegate, UITableViewDataSource>
32+
@interface RCTRedBoxWindow : NSObject <UITableViewDelegate, UITableViewDataSource>
33+
@property (nonatomic, strong) UIViewController *rootViewController;
3334
@property (nonatomic, weak) id<RCTRedBoxWindowActionDelegate> actionDelegate;
3435
@end
3536

@@ -42,19 +43,11 @@ @implementation RCTRedBoxWindow
4243

4344
- (instancetype)initWithFrame:(CGRect)frame
4445
{
45-
if ((self = [super initWithFrame:frame])) {
46-
#if TARGET_OS_TV
47-
self.windowLevel = UIWindowLevelAlert + 1000;
48-
#else
49-
self.windowLevel = UIWindowLevelStatusBar - 1;
50-
#endif
51-
self.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
52-
self.hidden = YES;
53-
54-
UIViewController *rootController = [UIViewController new];
55-
self.rootViewController = rootController;
56-
UIView *rootView = rootController.view;
57-
rootView.backgroundColor = [UIColor clearColor];
46+
if ((self = [super init])) {
47+
_rootViewController = [UIViewController new];
48+
UIView *rootView = _rootViewController.view;
49+
rootView.frame = frame;
50+
rootView.backgroundColor = [UIColor blackColor];
5851

5952
const CGFloat buttonHeight = 60;
6053

@@ -133,8 +126,8 @@ - (instancetype)initWithFrame:(CGRect)frame
133126
[extraButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateHighlighted];
134127
[extraButton addTarget:self action:@selector(showExtraDataViewController) forControlEvents:UIControlEventTouchUpInside];
135128

136-
CGFloat buttonWidth = self.bounds.size.width / 4;
137-
CGFloat bottomButtonHeight = self.bounds.size.height - buttonHeight - [self bottomSafeViewHeight];
129+
CGFloat buttonWidth = frame.size.width / 4;
130+
CGFloat bottomButtonHeight = frame.size.height - buttonHeight - [self bottomSafeViewHeight];
138131

139132
dismissButton.frame = CGRectMake(0, bottomButtonHeight, buttonWidth, buttonHeight);
140133
reloadButton.frame = CGRectMake(buttonWidth, bottomButtonHeight, buttonWidth, buttonHeight);
@@ -152,7 +145,7 @@ - (instancetype)initWithFrame:(CGRect)frame
152145

153146
UIView *bottomSafeView = [UIView new];
154147
bottomSafeView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
155-
bottomSafeView.frame = CGRectMake(0, self.bounds.size.height - [self bottomSafeViewHeight], self.bounds.size.width, [self bottomSafeViewHeight]);
148+
bottomSafeView.frame = CGRectMake(0, frame.size.height - [self bottomSafeViewHeight], frame.size.width, [self bottomSafeViewHeight]);
156149

157150
[rootView addSubview:bottomSafeView];
158151
}
@@ -190,30 +183,29 @@ - (void)showErrorMessage:(NSString *)message withStack:(NSArray<RCTJSStackFrame
190183
NSString *messageWithoutAnsi = [self stripAnsi:message];
191184

192185
// Show if this is a new message, or if we're updating the previous message
193-
if ((self.hidden && !isUpdate) || (!self.hidden && isUpdate && [_lastErrorMessage isEqualToString:messageWithoutAnsi])) {
186+
BOOL hidden = !self.rootViewController.isBeingPresented;
187+
if ((hidden && !isUpdate) || (!hidden && isUpdate && [_lastErrorMessage isEqualToString:messageWithoutAnsi])) {
194188
_lastStackTrace = stack;
195189
// message is displayed using UILabel, which is unable to render text of
196190
// unlimited length, so we truncate it
197191
_lastErrorMessage = [messageWithoutAnsi substringToIndex:MIN((NSUInteger)10000, messageWithoutAnsi.length)];
198192

199193
[_stackTraceTableView reloadData];
200194

201-
if (self.hidden) {
195+
if (hidden) {
202196
[_stackTraceTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
203197
atScrollPosition:UITableViewScrollPositionTop
204198
animated:NO];
199+
[RCTKeyWindow().rootViewController presentViewController:self.rootViewController
200+
animated:YES
201+
completion:nil];
205202
}
206-
207-
[self makeKeyAndVisible];
208-
[self becomeFirstResponder];
209203
}
210204
}
211205

212206
- (void)dismiss
213207
{
214-
self.hidden = YES;
215-
[self resignFirstResponder];
216-
[RCTSharedApplication().delegate.window makeKeyWindow];
208+
[self.rootViewController dismissViewControllerAnimated:YES completion:nil];
217209
}
218210

219211
- (void)reload
@@ -554,13 +546,13 @@ - (void)showErrorMessage:(NSString *)message withStack:(NSArray<RCTJSStackFrame
554546
if (!_visible) {
555547
_visible = YES;
556548
[_window center];
557-
if (!RCTRunningInTestEnvironment()) {
558-
[NSApp runModalForWindow:_window];
559-
}
560-
else {
561-
[NSApp activateIgnoringOtherApps:YES];
562-
[[NSApp mainWindow] makeKeyAndOrderFront:_window];
563-
}
549+
if (!RCTRunningInTestEnvironment()) {
550+
[NSApp runModalForWindow:_window];
551+
}
552+
else {
553+
[NSApp activateIgnoringOtherApps:YES];
554+
[[NSApp mainWindow] makeKeyAndOrderFront:_window];
555+
}
564556
}
565557
}
566558
}
@@ -881,6 +873,7 @@ - (void)loadExtraDataViewController {
881873
#else // ]TODO(macOS ISS#2323203)
882874
dispatch_async(dispatch_get_main_queue(), ^{
883875
[self->_window dismiss];
876+
self->_window = nil; // TODO(OSS Candidate ISS#2710739): release _window now to ensure its UIKit ivars are dealloc'd on the main thread as the RCTRedBox can be dealloc'd on a background thread.
884877
});
885878
#endif // TODO(macOS ISS#2323203)
886879
}

0 commit comments

Comments
 (0)