From f27182f191b134f37441a4c2fdb29d4bbb63794f Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Thu, 17 Nov 2016 17:35:16 -0800 Subject: [PATCH 1/8] Android: Add UIManager.getContentSizeMultiplier This gives apps the ability to find out the current scaling factor for fonts on the device. --- .../java/com/facebook/react/uimanager/UIManagerModule.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index ffccabcc67d49e..43f130b6c868b1 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -498,6 +498,12 @@ public void configureNextLayoutAnimation( mUIImplementation.configureNextLayoutAnimation(config, success, error); } + @ReactMethod + public void getContentSizeMultiplier(Callback callback) { + float scale = getReactApplicationContext().getResources().getConfiguration().fontScale; + callback.invoke(scale); + } + /** * To implement the transactional requirement mentioned in the class javadoc, we only commit * UI changes to the actual view hierarchy once a batch of JS->Java calls have been completed. From 2f896a7cc3acd7d70428c6963e0a6467d793ff97 Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Tue, 7 Feb 2017 12:26:57 -0800 Subject: [PATCH 2/8] Revert "Android: Add UIManager.getContentSizeMultiplier" This reverts commit 29c8093e4f34b4a6b3111672b7a33fa3d6bf99e2. Instead of exposing new getContentSizeMultiplier method, expose this information on Dimensions.get('screen').fontScale. --- .../java/com/facebook/react/uimanager/UIManagerModule.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 43f130b6c868b1..ffccabcc67d49e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -498,12 +498,6 @@ public void configureNextLayoutAnimation( mUIImplementation.configureNextLayoutAnimation(config, success, error); } - @ReactMethod - public void getContentSizeMultiplier(Callback callback) { - float scale = getReactApplicationContext().getResources().getConfiguration().fontScale; - callback.invoke(scale); - } - /** * To implement the transactional requirement mentioned in the class javadoc, we only commit * UI changes to the actual view hierarchy once a batch of JS->Java calls have been completed. From 0f5ea42ac7dc246cd282db075e9e15f6b59ccea6 Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Tue, 7 Feb 2017 13:38:50 -0800 Subject: [PATCH 3/8] Correctly export fontScale to JS Here's a summary of the changes in this commit: - Breaking change: UIManager was exporting DisplayMetrics.scaledDensity under the name fontScale. However, scaledDensity is actually density * fontScale. We now correctly export just fontScale under the name fontScale. - The didUpdateDimensions event now fires when fontScale changes. --- .../com/facebook/react/ReactRootView.java | 25 ++-------- .../react/uimanager/UIManagerModule.java | 50 +++++++++++++++++-- .../uimanager/UIManagerModuleConstants.java | 8 +-- .../UIManagerModuleConstantsHelper.java | 6 ++- 4 files changed, 60 insertions(+), 29 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java index b6172aee234cba..46ad7c6c47f658 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactRootView.java @@ -394,27 +394,10 @@ private void emitOrientationChanged(final int newRotation) { } private void emitUpdateDimensionsEvent() { - DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); - DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics(); - - WritableMap windowDisplayMetricsMap = Arguments.createMap(); - windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels); - windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels); - windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density); - windowDisplayMetricsMap.putDouble("fontScale", windowDisplayMetrics.scaledDensity); - windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi); - - WritableMap screenDisplayMetricsMap = Arguments.createMap(); - screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels); - screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels); - screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density); - screenDisplayMetricsMap.putDouble("fontScale", screenDisplayMetrics.scaledDensity); - screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi); - - WritableMap dimensionsMap = Arguments.createMap(); - dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap); - dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap); - sendEvent("didUpdateDimensions", dimensionsMap); + mReactInstanceManager + .getCurrentReactContext() + .getNativeModule(UIManagerModule.class) + .emitUpdateDimensionsEvent(); } private void sendEvent(String eventName, @Nullable WritableMap params) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index ffccabcc67d49e..e45b9b01c97ba4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -17,9 +17,12 @@ import android.content.ComponentCallbacks2; import android.content.res.Configuration; +import android.util.DisplayMetrics; import com.facebook.common.logging.FLog; +import com.facebook.react.ReactApplication; import com.facebook.react.animation.Animation; +import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.GuardedRunnable; import com.facebook.react.bridge.LifecycleEventListener; @@ -32,8 +35,10 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.WritableMap; import com.facebook.react.common.ReactConstants; import com.facebook.react.module.annotations.ReactModule; +import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.uimanager.debug.NotThreadSafeViewHierarchyUpdateDebugListener; import com.facebook.react.uimanager.events.EventDispatcher; import com.facebook.systrace.Systrace; @@ -88,6 +93,7 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements private final Map mModuleConstants; private final UIImplementation mUIImplementation; private final MemoryTrimCallback mMemoryTrimCallback = new MemoryTrimCallback(); + private float mFontScale; private int mNextRootViewTag = 1; private int mBatchId = 0; @@ -100,7 +106,8 @@ public UIManagerModule( super(reactContext); DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(reactContext); mEventDispatcher = new EventDispatcher(reactContext); - mModuleConstants = createConstants(viewManagerList, lazyViewManagersEnabled); + mFontScale = getReactApplicationContext().getResources().getConfiguration().fontScale; + mModuleConstants = createConstants(viewManagerList, lazyViewManagersEnabled, mFontScale); mUIImplementation = uiImplementationProvider .createUIImplementation(reactContext, viewManagerList, mEventDispatcher); @@ -133,6 +140,10 @@ public void initialize() { @Override public void onHostResume() { mUIImplementation.onHostResume(); + + if (mFontScale != getReactApplicationContext().getResources().getConfiguration().fontScale) { + emitUpdateDimensionsEvent(); + } } @Override @@ -156,13 +167,15 @@ public void onCatalystInstanceDestroy() { private static Map createConstants( List viewManagerList, - boolean lazyViewManagersEnabled) { + boolean lazyViewManagersEnabled, + float fontScale) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateUIManagerConstants"); try { return UIManagerModuleConstantsHelper.createConstants( viewManagerList, - lazyViewManagersEnabled); + lazyViewManagersEnabled, + fontScale); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_END); @@ -541,6 +554,37 @@ public void sendAccessibilityEvent(int tag, int eventType) { mUIImplementation.sendAccessibilityEvent(tag, eventType); } + public void emitUpdateDimensionsEvent() { + mFontScale = getReactApplicationContext().getResources().getConfiguration().fontScale; + DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); + DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics(); + + WritableMap windowDisplayMetricsMap = Arguments.createMap(); + windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels); + windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels); + windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density); + windowDisplayMetricsMap.putDouble("fontScale", mFontScale); + windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi); + + WritableMap screenDisplayMetricsMap = Arguments.createMap(); + screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels); + screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels); + screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density); + screenDisplayMetricsMap.putDouble("fontScale", mFontScale); + screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi); + + WritableMap dimensionsMap = Arguments.createMap(); + dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap); + dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap); + sendEvent("didUpdateDimensions", dimensionsMap); + } + + private void sendEvent(String eventName, @Nullable WritableMap params) { + getReactApplicationContext() + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(eventName, params); + } + /** * Schedule a block to be executed on the UI thread. Useful if you need to execute * view logic after all currently queued view updates have completed. diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java index 506c0063acabed..6bc9b8870b58f4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java @@ -16,6 +16,8 @@ import android.view.accessibility.AccessibilityEvent; import android.widget.ImageView; +import com.facebook.react.ReactApplication; +import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.common.MapBuilder; import com.facebook.react.uimanager.events.TouchEventType; @@ -81,7 +83,7 @@ .build(); } - public static Map getConstants() { + public static Map getConstants(float fontScale) { HashMap constants = new HashMap(); constants.put( "UIView", @@ -109,7 +111,7 @@ public static Map getConstants() { "scale", displayMetrics.density, "fontScale", - displayMetrics.scaledDensity, + fontScale, "densityDpi", displayMetrics.densityDpi), "screenPhysicalPixels", @@ -121,7 +123,7 @@ public static Map getConstants() { "scale", screenDisplayMetrics.density, "fontScale", - screenDisplayMetrics.scaledDensity, + fontScale, "densityDpi", screenDisplayMetrics.densityDpi))); diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java index 9c1afe2b262587..dbbd283d0e3943 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.Map; +import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.common.MapBuilder; import com.facebook.systrace.Systrace; import com.facebook.systrace.SystraceMessage; @@ -42,8 +43,9 @@ */ /* package */ static Map createConstants( List viewManagers, - boolean lazyViewManagersEnabled) { - Map constants = UIManagerModuleConstants.getConstants(); + boolean lazyViewManagersEnabled, + float fontScale) { + Map constants = UIManagerModuleConstants.getConstants(fontScale); Map bubblingEventTypesConstants = UIManagerModuleConstants.getBubblingEventTypeConstants(); Map directEventTypesConstants = UIManagerModuleConstants.getDirectEventTypeConstants(); From 340f7caa49829bc8c9f12005c08817abbca2a55f Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Tue, 7 Feb 2017 18:29:18 -0800 Subject: [PATCH 4/8] Begin fixing buck errors - Removed some unnecessary imports to fix some buck errors. - Added a missing dependency to a BUCK file. Unfortunately, it introduced a dependency cycle. I'm not sure how to break it yet. --- .../main/java/com/facebook/react/uimanager/UIManagerModule.java | 1 - .../com/facebook/react/uimanager/UIManagerModuleConstants.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index e45b9b01c97ba4..30e572027e8caf 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -20,7 +20,6 @@ import android.util.DisplayMetrics; import com.facebook.common.logging.FLog; -import com.facebook.react.ReactApplication; import com.facebook.react.animation.Animation; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java index 6bc9b8870b58f4..2b7d15a0b8b4d8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java @@ -16,8 +16,6 @@ import android.view.accessibility.AccessibilityEvent; import android.widget.ImageView; -import com.facebook.react.ReactApplication; -import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.common.MapBuilder; import com.facebook.react.uimanager.events.TouchEventType; From 3ce2b47729c052bc7a0a15a80552dcd603502079 Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Wed, 8 Feb 2017 23:36:15 -0800 Subject: [PATCH 5/8] Don't set mFontScale in emitUpdateDimensionsEvent emitUpdateDimensionsEvent shouldn't have side effects. Instead, set it in onHostResume. --- .../java/com/facebook/react/uimanager/UIManagerModule.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 30e572027e8caf..df619cbafd2cc5 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -140,7 +140,9 @@ public void initialize() { public void onHostResume() { mUIImplementation.onHostResume(); - if (mFontScale != getReactApplicationContext().getResources().getConfiguration().fontScale) { + float fontScale = getReactApplicationContext().getResources().getConfiguration().fontScale; + if (mFontScale != fontScale) { + mFontScale = fontScale; emitUpdateDimensionsEvent(); } } @@ -554,7 +556,6 @@ public void sendAccessibilityEvent(int tag, int eventType) { } public void emitUpdateDimensionsEvent() { - mFontScale = getReactApplicationContext().getResources().getConfiguration().fontScale; DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics(); From e8fc3295f20ce25d344bb473c1595de07500774e Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Wed, 8 Feb 2017 23:49:55 -0800 Subject: [PATCH 6/8] Share construction of the dimensions object The same helper function, getDimensionsConstants, is now used both when constructing the UIManager's initial constants object and when firing the didUpdateDimensions event. --- .../react/uimanager/UIManagerModule.java | 26 +-------- .../uimanager/UIManagerModuleConstants.java | 55 ++++++++++--------- 2 files changed, 30 insertions(+), 51 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index df619cbafd2cc5..d082314de7a25a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -17,11 +17,9 @@ import android.content.ComponentCallbacks2; import android.content.res.Configuration; -import android.util.DisplayMetrics; import com.facebook.common.logging.FLog; import com.facebook.react.animation.Animation; -import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.GuardedRunnable; import com.facebook.react.bridge.LifecycleEventListener; @@ -555,28 +553,8 @@ public void sendAccessibilityEvent(int tag, int eventType) { mUIImplementation.sendAccessibilityEvent(tag, eventType); } - public void emitUpdateDimensionsEvent() { - DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); - DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics(); - - WritableMap windowDisplayMetricsMap = Arguments.createMap(); - windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels); - windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels); - windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density); - windowDisplayMetricsMap.putDouble("fontScale", mFontScale); - windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi); - - WritableMap screenDisplayMetricsMap = Arguments.createMap(); - screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels); - screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels); - screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density); - screenDisplayMetricsMap.putDouble("fontScale", mFontScale); - screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi); - - WritableMap dimensionsMap = Arguments.createMap(); - dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap); - dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap); - sendEvent("didUpdateDimensions", dimensionsMap); +public void emitUpdateDimensionsEvent() { + sendEvent("didUpdateDimensions", UIManagerModuleConstants.getDimensionsConstants(mFontScale)); } private void sendEvent(String eventName, @Nullable WritableMap params) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java index 2b7d15a0b8b4d8..7dc5602ae4b7c7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstants.java @@ -16,6 +16,8 @@ import android.view.accessibility.AccessibilityEvent; import android.widget.ImageView; +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.WritableMap; import com.facebook.react.common.MapBuilder; import com.facebook.react.uimanager.events.TouchEventType; @@ -95,35 +97,9 @@ public static Map getConstants(float fontScale) { "ScaleAspectCenter", ImageView.ScaleType.CENTER_INSIDE.ordinal()))); - DisplayMetrics displayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); - DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics(); constants.put( "Dimensions", - MapBuilder.of( - "windowPhysicalPixels", - MapBuilder.of( - "width", - displayMetrics.widthPixels, - "height", - displayMetrics.heightPixels, - "scale", - displayMetrics.density, - "fontScale", - fontScale, - "densityDpi", - displayMetrics.densityDpi), - "screenPhysicalPixels", - MapBuilder.of( - "width", - screenDisplayMetrics.widthPixels, - "height", - screenDisplayMetrics.heightPixels, - "scale", - screenDisplayMetrics.density, - "fontScale", - fontScale, - "densityDpi", - screenDisplayMetrics.densityDpi))); + getDimensionsConstants(fontScale)); constants.put( "StyleConstants", @@ -157,4 +133,29 @@ public static Map getConstants(float fontScale) { return constants; } + + public static WritableMap getDimensionsConstants(float fontScale) { + DisplayMetrics windowDisplayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics(); + DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics(); + + WritableMap windowDisplayMetricsMap = Arguments.createMap(); + windowDisplayMetricsMap.putInt("width", windowDisplayMetrics.widthPixels); + windowDisplayMetricsMap.putInt("height", windowDisplayMetrics.heightPixels); + windowDisplayMetricsMap.putDouble("scale", windowDisplayMetrics.density); + windowDisplayMetricsMap.putDouble("fontScale", fontScale); + windowDisplayMetricsMap.putDouble("densityDpi", windowDisplayMetrics.densityDpi); + + WritableMap screenDisplayMetricsMap = Arguments.createMap(); + screenDisplayMetricsMap.putInt("width", screenDisplayMetrics.widthPixels); + screenDisplayMetricsMap.putInt("height", screenDisplayMetrics.heightPixels); + screenDisplayMetricsMap.putDouble("scale", screenDisplayMetrics.density); + screenDisplayMetricsMap.putDouble("fontScale", fontScale); + screenDisplayMetricsMap.putDouble("densityDpi", screenDisplayMetrics.densityDpi); + + WritableMap dimensionsMap = Arguments.createMap(); + dimensionsMap.putMap("windowPhysicalPixels", windowDisplayMetricsMap); + dimensionsMap.putMap("screenPhysicalPixels", screenDisplayMetricsMap); + + return dimensionsMap; + } } From 55a9467df36281f7854b77ffcefe4afe861bfa24 Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Mon, 13 Feb 2017 14:35:47 -0800 Subject: [PATCH 7/8] Add 'change' event for subscribing to Dimensions updates with a cross platform interface --- Libraries/Utilities/Dimensions.js | 47 ++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/Libraries/Utilities/Dimensions.js b/Libraries/Utilities/Dimensions.js index b4e71b554aa10f..ed3d1358d1bf8e 100644 --- a/Libraries/Utilities/Dimensions.js +++ b/Libraries/Utilities/Dimensions.js @@ -11,12 +11,15 @@ */ 'use strict'; +var EventEmitter = require('EventEmitter'); var Platform = require('Platform'); var UIManager = require('UIManager'); var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter'); var invariant = require('fbjs/lib/invariant'); +var eventEmitter = new EventEmitter(); +var dimensionsInitialized = false; var dimensions = {}; class Dimensions { /** @@ -60,6 +63,15 @@ class Dimensions { } Object.assign(dimensions, dims); + if (dimensionsInitialized) { + // Don't fire 'change' the first time the dimensions are set. + eventEmitter.emit('change', { + window: dimensions.window, + screen: dimensions.screen + }); + } else { + dimensionsInitialized = true; + } } /** @@ -81,11 +93,44 @@ class Dimensions { invariant(dimensions[dim], 'No dimension set for key ' + dim); return dimensions[dim]; } + + /** + * Add an event handler. Supported events: + * + * - `change`: Fires when a property within the `Dimensions` object changes. The argument + * to the event handler is an object with `window` and `screen` properties whose values + * are the same as the return values of `Dimensions.get('window')` and + * `Dimensions.get('screen')`, respectively. + */ + static addEventListener( + type: string, + handler: Function + ) { + invariant( + 'change' === type, + 'Trying to subscribe to unknown event: "%s"', type + ); + eventEmitter.addListener(type, handler); + } + + /** + * Remove an event handler. + */ + static removeEventListener( + type: string, + handler: Function + ) { + invariant( + 'change' === type, + 'Trying to remove listener for unknown event: "%s"', type + ); + eventEmitter.removeListener(type, handler); + } } Dimensions.set(UIManager.Dimensions); RCTDeviceEventEmitter.addListener('didUpdateDimensions', function(update) { - Dimensions.set(update); + Dimensions.set(update); }); module.exports = Dimensions; From f5c2fff62c107770538bc8bba640ea060a0d1adc Mon Sep 17 00:00:00 2001 From: Adam Comella Date: Mon, 13 Feb 2017 14:41:57 -0800 Subject: [PATCH 8/8] Tweak formatting --- Libraries/Utilities/Dimensions.js | 2 +- .../main/java/com/facebook/react/uimanager/UIManagerModule.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Libraries/Utilities/Dimensions.js b/Libraries/Utilities/Dimensions.js index ed3d1358d1bf8e..2204ba4fcc331a 100644 --- a/Libraries/Utilities/Dimensions.js +++ b/Libraries/Utilities/Dimensions.js @@ -130,7 +130,7 @@ class Dimensions { Dimensions.set(UIManager.Dimensions); RCTDeviceEventEmitter.addListener('didUpdateDimensions', function(update) { - Dimensions.set(update); + Dimensions.set(update); }); module.exports = Dimensions; diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index d082314de7a25a..1d05aa2679332e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -553,7 +553,7 @@ public void sendAccessibilityEvent(int tag, int eventType) { mUIImplementation.sendAccessibilityEvent(tag, eventType); } -public void emitUpdateDimensionsEvent() { + public void emitUpdateDimensionsEvent() { sendEvent("didUpdateDimensions", UIManagerModuleConstants.getDimensionsConstants(mFontScale)); }