introduce RCTComputeScreenScale

Summary:
Changelog: [Internal]

in this diff, we add a function that allows us to retrieve screen scale. enforcing thread safety will be dependent on the consumer to make sure it's used on main thread.

i'd like for this to be a more temporary solution, i think we should have a more scalable screen solution for consumers to handle the gambit of uiscreen use cases.

Reviewed By: sammy-SC

Differential Revision: D31163909

fbshipit-source-id: 78104e9ef982b47c60697461141afb4b06eede72
This commit is contained in:
Phillip Pan
2021-09-24 16:57:42 -07:00
committed by Facebook GitHub Bot
parent c744528da1
commit 99f706e5df
2 changed files with 16 additions and 6 deletions
+3
View File
@@ -40,6 +40,9 @@ RCT_EXTERN void RCTExecuteOnMainQueue(dispatch_block_t block);
// Please do not use this unless you know what you're doing.
RCT_EXTERN void RCTUnsafeExecuteOnMainQueueSync(dispatch_block_t block);
// Get screen scale, can be only used on main
RCT_EXTERN void RCTComputeScreenScale(void);
// Get screen metrics in a thread-safe way
RCT_EXTERN CGFloat RCTScreenScale(void);
RCT_EXTERN CGFloat RCTFontSizeMultiplier(void);
+13 -6
View File
@@ -296,16 +296,23 @@ static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, disp
}
}
static dispatch_once_t onceTokenScreenScale;
static CGFloat screenScale;
void RCTComputeScreenScale()
{
dispatch_once(&onceTokenScreenScale, ^{
screenScale = [UIScreen mainScreen].scale;
});
}
CGFloat RCTScreenScale()
{
static dispatch_once_t onceToken;
static CGFloat scale;
RCTUnsafeExecuteOnMainQueueOnceSync(&onceToken, ^{
scale = [UIScreen mainScreen].scale;
RCTUnsafeExecuteOnMainQueueOnceSync(&onceTokenScreenScale, ^{
screenScale = [UIScreen mainScreen].scale;
});
return scale;
return screenScale;
}
CGFloat RCTFontSizeMultiplier()