Skip to content

Default dimensions scale value to 1.0 if window is nil #1970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -495,8 +495,10 @@ NS_ASSUME_NONNULL_END

#if !TARGET_OS_OSX
typedef UIApplication RCTUIApplication;
typedef UIWindow RCTUIWindow;
#else
typedef NSApplication RCTUIApplication;
typedef NSWindow RCTUIWindow;
#endif

//
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native/React/Base/RCTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ RCT_EXTERN BOOL RCTRunningInAppExtension(void);
#endif // [macOS]

// Returns the shared UIApplication instance, or nil if running in an App Extension
RCT_EXTERN UIApplication *__nullable RCTSharedApplication(void);
RCT_EXTERN RCTUIApplication *__nullable RCTSharedApplication(void); // [macOS]

#if !TARGET_OS_OSX // [macOS]
// Returns the current main window, useful if you need to access the root view
// or view controller
RCT_EXTERN UIWindow *__nullable RCTKeyWindow(void);
RCT_EXTERN RCTUIWindow *__nullable RCTKeyWindow(void); // [macOS]

#if !TARGET_OS_OSX // [macOS]
// Returns the presented view controller, useful if you need
// e.g. to present a modal view controller or alert over it
RCT_EXTERN UIViewController *__nullable RCTPresentedViewController(void);
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ BOOL RCTRunningInAppExtension(void)
}
#endif // [macOS]

UIApplication *__nullable RCTSharedApplication(void)
RCTUIApplication *__nullable RCTSharedApplication(void) // [macOS]
{
#if !TARGET_OS_OSX // [macOS]
if (RCTRunningInAppExtension()) {
Expand All @@ -593,7 +593,7 @@ BOOL RCTRunningInAppExtension(void)
#endif // macOS]
}

RCTPlatformWindow *__nullable RCTKeyWindow(void) // [macOS]
RCTUIWindow *__nullable RCTKeyWindow(void) // [macOS]
{
#if !TARGET_OS_OSX // [macOS]
if (RCTRunningInAppExtension()) {
Expand Down
18 changes: 5 additions & 13 deletions packages/react-native/React/CoreModules/RCTDeviceInfo.mm
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,10 @@ static BOOL RCTIsIPhoneNotched()
}


#if !TARGET_OS_OSX // [macOS]
NSDictionary *RCTExportedDimensions(CGFloat fontScale)
#else // [macOS
static NSDictionary *RCTExportedDimensions(RCTPlatformView *rootView)
#endif // macOS]
static NSDictionary *RCTExportedDimensions(CGFloat fontScale)
{
RCTAssertMainQueue();
#if !TARGET_OS_OSX // [macOS]
RCTDimensions dimensions = RCTGetDimensions(fontScale);
#else // [macOS
RCTDimensions dimensions = RCTGetDimensions(rootView);
#endif // macOS]
__typeof(dimensions.window) window = dimensions.window;
NSDictionary<NSString *, NSNumber *> *dimsWindow = @{
@"width" : @(window.width),
Expand All @@ -144,13 +136,13 @@ - (NSDictionary *)_exportedDimensions
RCTAccessibilityManager *accessibilityManager =
(RCTAccessibilityManager *)[_moduleRegistry moduleForName:"AccessibilityManager"];
RCTAssert(accessibilityManager, @"Failed to get exported dimensions: AccessibilityManager is nil");
CGFloat fontScale = accessibilityManager ? accessibilityManager.multiplier : 1.0;
#if !TARGET_OS_OSX // [macOS]
return RCTExportedDimensions(fontScale);
CGFloat fontScale = accessibilityManager ? accessibilityManager.multiplier : 1.0;
#else // [macOS
// TODO: Saad - get root view here
return RCTExportedDimensions(nil);
CGFloat fontScale = 1.0;
#endif // macOS]

return RCTExportedDimensions(fontScale);
}

- (NSDictionary<NSString *, id> *)constantsToExport
Expand Down
4 changes: 0 additions & 4 deletions packages/react-native/React/UIUtils/RCTUIUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ typedef struct {
} window, screen;
} RCTDimensions;
extern __attribute__((visibility("default")))
#if !TARGET_OS_OSX // [macOS]
RCTDimensions RCTGetDimensions(CGFloat fontScale);
#else // [macOS
RCTDimensions RCTGetDimensions(RCTPlatformView *rootView);
#endif // macOS]

#ifdef __cplusplus
}
Expand Down
31 changes: 9 additions & 22 deletions packages/react-native/React/UIUtils/RCTUIUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@

#import "RCTUtils.h"

#if !TARGET_OS_OSX // [macOS]
RCTDimensions RCTGetDimensions(CGFloat fontScale)
#else // [macOS
RCTDimensions RCTGetDimensions(RCTPlatformView *rootView)
#endif // macOS]
{
#if !TARGET_OS_OSX // [macOS]
UIScreen *mainScreen = UIScreen.mainScreen;
Expand All @@ -24,20 +20,11 @@ RCTDimensions RCTGetDimensions(RCTPlatformView *rootView)
// We fallback to screen size if a key window is not found.
CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize;
#else // [macOS
NSWindow *window = nil;
NSSize windowSize;
NSSize screenSize;
if (rootView != nil) {
window = [rootView window];
windowSize = [window frame].size;
} else {
// We don't have a root view so fall back to the app's key window
window = [NSApp keyWindow];
windowSize = [window frame].size;
}
screenSize = [[window screen] frame].size;
#endif

RCTUIWindow *window = RCTKeyWindow();
NSSize windowSize = window ? [window frame].size : CGSizeMake(0, 0);
NSSize screenSize = window ? [[window screen] frame].size : CGSizeMake(0, 0);
CGFloat scale = window ? [[window screen] backingScaleFactor] : 1.0; // Default scale to 1.0 if window is nil
#endif // macOS
RCTDimensions result;
#if !TARGET_OS_OSX // [macOS]
typeof(result.screen) dimsScreen = {
Expand All @@ -48,13 +35,13 @@ RCTDimensions RCTGetDimensions(RCTPlatformView *rootView)
typeof(result.screen) dimsScreen = {
.width = screenSize.width,
.height = screenSize.height,
.scale = [[window screen] backingScaleFactor],
.fontScale = 1};
.scale = scale,
.fontScale = fontScale};
typeof(result.window) dimsWindow = {
.width = windowSize.width,
.height = windowSize.height,
.scale = [[window screen] backingScaleFactor],
.fontScale = 1};
.scale = scale,
.fontScale = fontScale};
#endif // macOS]
result.screen = dimsScreen;
result.window = dimsWindow;
Expand Down