From 38cf8567ad5be0425572f96e273a73a58a6020be Mon Sep 17 00:00:00 2001 From: Shawn Dempsey Date: Fri, 3 Nov 2023 16:27:03 -0700 Subject: [PATCH 1/3] [0.73] Default scale to 1.0 on if window is nil --- .../React/CoreModules/RCTDeviceInfo.mm | 7 ++--- .../react-native/React/UIUtils/RCTUIUtils.h | 2 +- .../react-native/React/UIUtils/RCTUIUtils.m | 28 +++++++------------ 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 85a544846a7be1..c035bdbde4b6ef 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -111,14 +111,14 @@ static BOOL RCTIsIPhoneNotched() #if !TARGET_OS_OSX // [macOS] NSDictionary *RCTExportedDimensions(CGFloat fontScale) #else // [macOS -static NSDictionary *RCTExportedDimensions(RCTPlatformView *rootView) +static NSDictionary *RCTExportedDimensions(void) #endif // macOS] { RCTAssertMainQueue(); #if !TARGET_OS_OSX // [macOS] RCTDimensions dimensions = RCTGetDimensions(fontScale); #else // [macOS - RCTDimensions dimensions = RCTGetDimensions(rootView); + RCTDimensions dimensions = RCTGetDimensions(); #endif // macOS] __typeof(dimensions.window) window = dimensions.window; NSDictionary *dimsWindow = @{ @@ -148,8 +148,7 @@ - (NSDictionary *)_exportedDimensions #if !TARGET_OS_OSX // [macOS] return RCTExportedDimensions(fontScale); #else // [macOS - // TODO: Saad - get root view here - return RCTExportedDimensions(nil); + return RCTExportedDimensions(); #endif // macOS] } diff --git a/packages/react-native/React/UIUtils/RCTUIUtils.h b/packages/react-native/React/UIUtils/RCTUIUtils.h index 7371e017e8fe79..0f9dcb31dbc9bc 100644 --- a/packages/react-native/React/UIUtils/RCTUIUtils.h +++ b/packages/react-native/React/UIUtils/RCTUIUtils.h @@ -25,7 +25,7 @@ extern __attribute__((visibility("default"))) #if !TARGET_OS_OSX // [macOS] RCTDimensions RCTGetDimensions(CGFloat fontScale); #else // [macOS -RCTDimensions RCTGetDimensions(RCTPlatformView *rootView); +RCTDimensions RCTGetDimensions(void); #endif // macOS] #ifdef __cplusplus diff --git a/packages/react-native/React/UIUtils/RCTUIUtils.m b/packages/react-native/React/UIUtils/RCTUIUtils.m index bfefcb3a40a82e..3cdafd50a3006f 100644 --- a/packages/react-native/React/UIUtils/RCTUIUtils.m +++ b/packages/react-native/React/UIUtils/RCTUIUtils.m @@ -12,7 +12,7 @@ #if !TARGET_OS_OSX // [macOS] RCTDimensions RCTGetDimensions(CGFloat fontScale) #else // [macOS -RCTDimensions RCTGetDimensions(RCTPlatformView *rootView) +RCTDimensions RCTGetDimensions(void) #endif // macOS] { #if !TARGET_OS_OSX // [macOS] @@ -24,20 +24,12 @@ 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; + NSWindow *window = [NSApp keyWindow]; + 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 + CGFloat fontScale = 1; #endif - RCTDimensions result; #if !TARGET_OS_OSX // [macOS] typeof(result.screen) dimsScreen = { @@ -48,13 +40,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; From 4f74cb46584fd4fca79578fd0ecad7467ddbef56 Mon Sep 17 00:00:00 2001 From: Shawn Dempsey Date: Fri, 3 Nov 2023 17:41:13 -0700 Subject: [PATCH 2/3] Unify RCTExportedDimensions between iOS & macOS --- packages/react-native/React/Base/RCTUIKit.h | 3 +++ packages/react-native/React/Base/RCTUtils.h | 2 +- .../React/CoreModules/RCTDeviceInfo.mm | 17 +++++------------ .../react-native/React/UIUtils/RCTUIUtils.h | 4 ---- .../react-native/React/UIUtils/RCTUIUtils.m | 9 ++------- 5 files changed, 11 insertions(+), 24 deletions(-) diff --git a/packages/react-native/React/Base/RCTUIKit.h b/packages/react-native/React/Base/RCTUIKit.h index 94614f55775bf0..3843813efd9789 100644 --- a/packages/react-native/React/Base/RCTUIKit.h +++ b/packages/react-native/React/Base/RCTUIKit.h @@ -335,6 +335,9 @@ NS_INLINE NSEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat botto // UIApplication #define UIApplication NSApplication +// UIWindow +#define UIWindow NSWindow + // UIImage @compatibility_alias UIImage NSImage; diff --git a/packages/react-native/React/Base/RCTUtils.h b/packages/react-native/React/Base/RCTUtils.h index c018f27f51f5af..4cb7bcbb1a7df9 100644 --- a/packages/react-native/React/Base/RCTUtils.h +++ b/packages/react-native/React/Base/RCTUtils.h @@ -95,11 +95,11 @@ RCT_EXTERN BOOL RCTRunningInAppExtension(void); // Returns the shared UIApplication instance, or nil if running in an App Extension RCT_EXTERN UIApplication *__nullable RCTSharedApplication(void); -#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); +#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/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index c035bdbde4b6ef..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(void) -#endif // macOS] +static NSDictionary *RCTExportedDimensions(CGFloat fontScale) { RCTAssertMainQueue(); -#if !TARGET_OS_OSX // [macOS] RCTDimensions dimensions = RCTGetDimensions(fontScale); -#else // [macOS - RCTDimensions dimensions = RCTGetDimensions(); -#endif // macOS] __typeof(dimensions.window) window = dimensions.window; NSDictionary *dimsWindow = @{ @"width" : @(window.width), @@ -144,12 +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 - return RCTExportedDimensions(); + 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 0f9dcb31dbc9bc..342a0081fdebb3 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(void); -#endif // macOS] #ifdef __cplusplus } diff --git a/packages/react-native/React/UIUtils/RCTUIUtils.m b/packages/react-native/React/UIUtils/RCTUIUtils.m index 3cdafd50a3006f..22e702b87e57f2 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(void) -#endif // macOS] { #if !TARGET_OS_OSX // [macOS] UIScreen *mainScreen = UIScreen.mainScreen; @@ -24,12 +20,11 @@ RCTDimensions RCTGetDimensions(void) // We fallback to screen size if a key window is not found. CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize; #else // [macOS - NSWindow *window = [NSApp keyWindow]; + NSWindow *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 - CGFloat fontScale = 1; -#endif +#endif // macOS] RCTDimensions result; #if !TARGET_OS_OSX // [macOS] typeof(result.screen) dimsScreen = { From 4f1bce182646d5f9ed8025816c49927f795c135d Mon Sep 17 00:00:00 2001 From: Shawn Dempsey Date: Mon, 6 Nov 2023 08:49:03 -0800 Subject: [PATCH 3/3] Use typedef instead of #define for NSWindow --- packages/react-native/React/Base/RCTUIKit.h | 5 ++--- packages/react-native/React/Base/RCTUtils.h | 4 ++-- packages/react-native/React/Base/RCTUtils.m | 4 ++-- packages/react-native/React/UIUtils/RCTUIUtils.m | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/react-native/React/Base/RCTUIKit.h b/packages/react-native/React/Base/RCTUIKit.h index 3843813efd9789..f58f24d1d2daec 100644 --- a/packages/react-native/React/Base/RCTUIKit.h +++ b/packages/react-native/React/Base/RCTUIKit.h @@ -335,9 +335,6 @@ NS_INLINE NSEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat botto // UIApplication #define UIApplication NSApplication -// UIWindow -#define UIWindow NSWindow - // UIImage @compatibility_alias UIImage NSImage; @@ -498,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 4cb7bcbb1a7df9..d391b64327178e 100644 --- a/packages/react-native/React/Base/RCTUtils.h +++ b/packages/react-native/React/Base/RCTUtils.h @@ -93,11 +93,11 @@ 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] // 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 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/UIUtils/RCTUIUtils.m b/packages/react-native/React/UIUtils/RCTUIUtils.m index 22e702b87e57f2..8d6ebc0ea5c2f4 100644 --- a/packages/react-native/React/UIUtils/RCTUIUtils.m +++ b/packages/react-native/React/UIUtils/RCTUIUtils.m @@ -20,11 +20,11 @@ RCTDimensions RCTGetDimensions(CGFloat fontScale) // We fallback to screen size if a key window is not found. CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize; #else // [macOS - NSWindow *window = RCTKeyWindow(); + 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] +#endif // macOS RCTDimensions result; #if !TARGET_OS_OSX // [macOS] typeof(result.screen) dimsScreen = {