From baed197a7d1140b423e7e728b5b48b2b01069534 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Thu, 16 Apr 2015 14:50:00 -0700 Subject: [PATCH] [react_native] JS files from D1998038: [react_native] Calculate density independent screen size in JS to prevent loss of precision --- Libraries/Utilities/Dimensions.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Libraries/Utilities/Dimensions.js b/Libraries/Utilities/Dimensions.js index 80d564a2b..c7302c7c5 100644 --- a/Libraries/Utilities/Dimensions.js +++ b/Libraries/Utilities/Dimensions.js @@ -17,6 +17,26 @@ var invariant = require('invariant'); var dimensions = NativeModules.UIManager.Dimensions; +// We calculate the window dimensions in JS so that we don't encounter loss of +// precision in transferring the dimensions (which could be non-integers) over +// the bridge. +if (dimensions.windowPhysicalPixels) { + // parse/stringify => Clone hack + dimensions = JSON.parse(JSON.stringify(dimensions)); + + var windowPhysicalPixels = dimensions.windowPhysicalPixels; + dimensions.window = { + width: windowPhysicalPixels.width / windowPhysicalPixels.scale, + height: windowPhysicalPixels.height / windowPhysicalPixels.scale, + scale: windowPhysicalPixels.scale, + }; + // TODO(andrews): Remove modalFullscreenView + dimensions.modalFullscreenView = dimensions.window; + + // delete so no callers rely on this existing + delete dimensions.windowPhysicalPixels; +} + class Dimensions { /** * This should only be called from native code.