From 074ca1e65a7a94d2b935929d12b9e5c30ad5c921 Mon Sep 17 00:00:00 2001 From: Shawn Dempsey <96719+shwanton@users.noreply.github.com> Date: Mon, 6 Nov 2023 22:53:31 -0800 Subject: [PATCH] [0.73] Default scale to 1.0 on if window is nil (#1975) --- packages/react-native/React/Base/RCTUIKit.h | 2 ++ packages/react-native/React/Base/RCTUtils.h | 6 ++-- packages/react-native/React/Base/RCTUtils.m | 4 +-- .../React/CoreModules/RCTDeviceInfo.mm | 18 +++-------- .../react-native/React/UIUtils/RCTUIUtils.h | 4 --- .../react-native/React/UIUtils/RCTUIUtils.m | 31 ++++++------------- 6 files changed, 21 insertions(+), 44 deletions(-) diff --git a/packages/react-native/React/Base/RCTUIKit.h b/packages/react-native/React/Base/RCTUIKit.h index 94614f55775bf0..f58f24d1d2daec 100644 --- a/packages/react-native/React/Base/RCTUIKit.h +++ b/packages/react-native/React/Base/RCTUIKit.h @@ -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 // diff --git a/packages/react-native/React/Base/RCTUtils.h b/packages/react-native/React/Base/RCTUtils.h index c018f27f51f5af..d391b64327178e 100644 --- a/packages/react-native/React/Base/RCTUtils.h +++ b/packages/react-native/React/Base/RCTUtils.h @@ -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); diff --git a/packages/react-native/React/Base/RCTUtils.m b/packages/react-native/React/Base/RCTUtils.m index 1fd3ceeb9151b5..923d6a277ad0d4 100644 --- a/packages/react-native/React/Base/RCTUtils.m +++ b/packages/react-native/React/Base/RCTUtils.m @@ -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()) { @@ -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()) { diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 85a544846a7be1..b39e14e5639f81 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -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 *dimsWindow = @{ @"width" : @(window.width), @@ -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 *)constantsToExport diff --git a/packages/react-native/React/UIUtils/RCTUIUtils.h b/packages/react-native/React/UIUtils/RCTUIUtils.h index bdd75663c5a655..d585bb270067c4 100644 --- a/packages/react-native/React/UIUtils/RCTUIUtils.h +++ b/packages/react-native/React/UIUtils/RCTUIUtils.h @@ -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] #if !TARGET_OS_OSX // [macOS] // Get font size multiplier for font base size (Large) by content size category diff --git a/packages/react-native/React/UIUtils/RCTUIUtils.m b/packages/react-native/React/UIUtils/RCTUIUtils.m index 5897dfa1ba7938..2058e19fd26543 100644 --- a/packages/react-native/React/UIUtils/RCTUIUtils.m +++ b/packages/react-native/React/UIUtils/RCTUIUtils.m @@ -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; @@ -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 = { @@ -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;