Skip to content

Commit

Permalink
Refactor Logbox into a Sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Saadnajmi committed Jul 31, 2024
1 parent 9e98a1e commit 5209d78
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 115 deletions.
2 changes: 2 additions & 0 deletions packages/react-native/React/Base/RCTUIKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,11 @@ NS_ASSUME_NONNULL_END
#if !TARGET_OS_OSX
typedef UIApplication RCTUIApplication;
typedef UIWindow RCTUIWindow;
typedef UIViewController RCTUIViewController;
#else
typedef NSApplication RCTUIApplication;
typedef NSWindow RCTUIWindow;
typedef NSViewController RCTUIViewController;
#endif

//
Expand Down
12 changes: 0 additions & 12 deletions packages/react-native/React/CoreModules/RCTLogBox.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,15 @@ - (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
}

if (strongSelf->_bridgelessSurfacePresenter) {
#if !TARGET_OS_OSX // [macOS]
strongSelf->_view = [[RCTLogBoxView alloc] initWithWindow:RCTKeyWindow()
surfacePresenter:strongSelf->_bridgelessSurfacePresenter];
#else // [macOS
strongSelf->_view = [[RCTLogBoxView alloc] initWithSurfacePresenter:strongSelf->_bridgelessSurfacePresenter];
#endif // macOS]
[strongSelf->_view show];
} else if (strongSelf->_bridge && strongSelf->_bridge.valid) {
if (strongSelf->_bridge.surfacePresenter) {
#if !TARGET_OS_OSX // [macOS]
strongSelf->_view = [[RCTLogBoxView alloc] initWithWindow:RCTKeyWindow()
surfacePresenter:strongSelf->_bridge.surfacePresenter];
#else // [macOS
strongSelf->_view = [[RCTLogBoxView alloc] initWithSurfacePresenter:strongSelf->_bridge.surfacePresenter];
#endif // macOS]
} else {
#if !TARGET_OS_OSX // [macOS]
strongSelf->_view = [[RCTLogBoxView alloc] initWithWindow:RCTKeyWindow() bridge:strongSelf->_bridge];
#else // [macOS
strongSelf->_view = [[RCTLogBoxView alloc] initWithBridge:self->_bridge];
#endif // macOS]
}
[strongSelf->_view show];
}
Expand Down
28 changes: 11 additions & 17 deletions packages/react-native/React/CoreModules/RCTLogBoxView.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,25 @@
#import <React/RCTUIKit.h> // [macOS]

#if !TARGET_OS_OSX // [macOS]

@interface RCTLogBoxView : UIWindow
#else // [macOS
@interface RCTLogBoxView : NSWindow
#endif // macOS]

#if !TARGET_OS_OSX // [macOS]
- (instancetype)initWithFrame:(CGRect)frame;
#endif // [macOS]

- (void)createRootViewController:(UIView *)view;
- (void)createRootViewController:(RCTUIView *)view; // [macOS]

- (instancetype)initWithWindow:(UIWindow *)window bridge:(RCTBridge *)bridge;
- (instancetype)initWithWindow:(UIWindow *)window surfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter;
- (instancetype)initWithWindow:(RCTUIWindow *)window bridge:(RCTBridge *)bridge; // [macOS]
- (instancetype)initWithWindow:(RCTUIWindow *)window surfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter; // [macOS]

- (void)show;

@end

#else // [macOS

@interface RCTLogBoxView : NSWindow

- (instancetype)initWithSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter;
- (instancetype)initWithBridge:(RCTBridge *)bridge;

#if TARGET_OS_OSX // [macOS
- (void)setHidden:(BOOL)hidden;

- (void)show;
#endif // macOS]

@end

#endif // macOS]

137 changes: 51 additions & 86 deletions packages/react-native/React/CoreModules/RCTLogBoxView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
#import <React/RCTSurface.h>
#import <React/RCTSurfaceHostingView.h>

#if !TARGET_OS_OSX // [macOS]

@implementation RCTLogBoxView {
RCTSurface *_surface;
#if TARGET_OS_OSX // [macOS
NSWindow *_window;
#endif // macOS]
}

#if !TARGET_OS_OSX // [macOS]
- (instancetype)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
Expand All @@ -25,39 +28,64 @@ - (instancetype)initWithFrame:(CGRect)frame
}
return self;
}
#endif // [macOS]

- (void)createRootViewController:(UIView *)view
- (void)createRootViewController:(RCTUIView *)view // [macOS]
{
UIViewController *_rootViewController = [UIViewController new];
RCTUIViewController *_rootViewController = [RCTUIViewController new]; // [macOS]
_rootViewController.view = view;
#if !TARGET_OS_OSX // [macOS]
_rootViewController.view.backgroundColor = [UIColor clearColor];
_rootViewController.modalPresentationStyle = UIModalPresentationFullScreen;
self.rootViewController = _rootViewController;
#else // [macOS
_rootViewController.view.wantsLayer = true;
_rootViewController.view.layer.backgroundColor = [NSColor clearColor].CGColor;
self.contentViewController = _rootViewController;
#endif // macOS]
}

- (instancetype)initWithWindow:(UIWindow *)window bridge:(RCTBridge *)bridge
- (instancetype)initWithWindow:(RCTUIWindow *)window bridge:(RCTBridge *)bridge // [macOS]
{
RCTErrorNewArchitectureValidation(RCTNotAllowedInFabricWithoutLegacy, @"RCTLogBoxView", nil);

#if !TARGET_OS_OSX // [macOS]
self = [super initWithWindowScene:window.windowScene];

self.windowLevel = UIWindowLevelStatusBar - 1;
self.backgroundColor = [UIColor clearColor];
#else // [macOS
NSRect bounds = NSMakeRect(0, 0, 600, 800);
self = [super initWithContentRect:bounds styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:YES];

self.level = NSStatusWindowLevel;
self.backgroundColor = [NSColor clearColor];

_window = window;
#endif // macOS]

_surface = [[RCTSurface alloc] initWithBridge:bridge moduleName:@"LogBox" initialProperties:@{}];
[_surface start];

#if TARGET_OS_OSX // [macOS
[_surface setSize:bounds.size]; // [macOS]
#endif // macOS]

if (![_surface synchronouslyWaitForStage:RCTSurfaceStageSurfaceDidInitialMounting timeout:1]) {
RCTLogInfo(@"Failed to mount LogBox within 1s");
}
[self createRootViewController:(UIView *)_surface.view];
[self createRootViewController:(RCTUIView *)_surface.view]; // [macOS]

return self;
}

- (instancetype)initWithWindow:(UIWindow *)window surfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
- (instancetype)initWithWindow:(RCTUIWindow *)window surfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter // [macOS]
{
#if !TARGET_OS_OSX // [macOS]
self = [super initWithWindowScene:window.windowScene];
#else // [macOS
self = [super initWithContentRect:NSMakeRect(0, 0, 600, 800) styleMask:NSWindowStyleMaskTitled backing:NSBackingStoreBuffered defer:YES];
_window = window;
#endif // macOS]

id<RCTSurfaceProtocol> surface = [surfacePresenter createFabricSurfaceForModuleName:@"LogBox" initialProperties:@{}];
[surface start];
Expand All @@ -69,107 +97,44 @@ - (instancetype)initWithWindow:(UIWindow *)window surfacePresenter:(id<RCTSurfac
return self;
}

#if !TARGET_OS_OSX // [macOS]
- (void)layoutSubviews
{
[super layoutSubviews];
[_surface setSize:self.frame.size];
}
#else // [macOS
- (void)layoutIfNeeded
{
[super layoutIfNeeded];
NSRect frame = NSMakeRect(self.frame.origin.x, self.frame.origin.y, 600, 800);
[self setFrame:frame display:NO];
}
#endif // macOS]

#if !TARGET_OS_OSX // [macOS]
- (void)dealloc
{
[RCTSharedApplication().delegate.window makeKeyWindow];
}
#endif // [macOS]

#if !TARGET_OS_OSX // [macOS]
- (void)show
{
[self becomeFirstResponder];
[self makeKeyAndVisible];
}

@end

#else // [macOS

@implementation RCTLogBoxView {
RCTSurface *_surface;
}

- (instancetype)initWithSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
{
NSRect bounds = NSMakeRect(0, 0, 600, 800);
if ((self = [self initWithContentRect:bounds
styleMask:NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:YES])) {
id<RCTSurfaceProtocol> surface = [surfacePresenter createFabricSurfaceForModuleName:@"LogBox"
initialProperties:@{}];
[surface start];
RCTSurfaceHostingView *rootView = [[RCTSurfaceHostingView alloc]
initWithSurface:surface
sizeMeasureMode:RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact];

self.contentView = rootView;
self.contentView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
}
return self;
}

- (instancetype)initWithBridge:(RCTBridge *)bridge
{
NSRect bounds = NSMakeRect(0, 0, 600, 800);
if ((self = [self initWithContentRect:bounds
styleMask:NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered
defer:YES])) {
_surface = [[RCTSurface alloc] initWithBridge:bridge moduleName:@"LogBox" initialProperties:@{}];

[_surface start];
[_surface setSize:bounds.size];

if (![_surface synchronouslyWaitForStage:RCTSurfaceStageSurfaceDidInitialMounting timeout:1]) {
RCTLogInfo(@"Failed to mount LogBox within 1s");
}

self.contentView = (NSView *)_surface.view;
self.contentView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
}
return self;
}

- (void)setHidden:(BOOL)hidden // [macOS
{
if (hidden) {
if (NSApp.modalWindow == self) {
[NSApp stopModal];
}
[self orderOut:nil];
}
} // macOS]

- (void)show
{
if (!RCTRunningInTestEnvironment()) {
// Run the modal loop outside of the dispatch queue because it is not reentrant.
[self performSelectorOnMainThread:@selector(_showModal) withObject:nil waitUntilDone:NO];
}
else {
[NSApp activateIgnoringOtherApps:YES];
[self makeKeyAndOrderFront:nil];
}
[_window beginSheet:self completionHandler:nil];
}

- (void)_showModal
- (void)setHidden:(BOOL)hidden
{
NSModalSession session = [NSApp beginModalSessionForWindow:self];

while ([NSApp runModalSession:session] == NSModalResponseContinue) {
// Spin the runloop so that the main dispatch queue is processed.
[[NSRunLoop currentRunLoop] limitDateForMode:NSDefaultRunLoopMode];
}

[NSApp endModalSession:session];
[_window endSheet:self];
}
#endif // [macOS]

@end

#endif // macOS]

0 comments on commit 5209d78

Please sign in to comment.