From bdb4278523141ad747c3555e0837bdf5ac612a2e Mon Sep 17 00:00:00 2001 From: Oleksandr Melnykov Date: Thu, 3 Oct 2019 03:12:46 -0700 Subject: [PATCH] Fix Switch measurement on Android Summary: Fabric expects the measure method to return the size in density-independent pixels, but getMeasuredWidth and getMeasuredHeight return pixels on Android, so we have to convert these values before returning to C++. Check the method createUpdateLayoutMountItem in Binding.cpp: ``` local_ref createUpdateLayoutMountItem( const jni::global_ref &javaUIManager, const ShadowViewMutation &mutation) { ... int x = round(frame.origin.x * pointScaleFactor); int y = round(frame.origin.y * pointScaleFactor); int w = round(frame.size.width * pointScaleFactor); int h = round(frame.size.height * pointScaleFactor); auto layoutDirection = toInt(newChildShadowView.layoutMetrics.layoutDirection); return updateLayoutInstruction( javaUIManager, newChildShadowView.tag, x, y, w, h, layoutDirection); } return nullptr; } ``` We are interested in the next two lines: ``` int w = round(frame.size.width * pointScaleFactor); int h = round(frame.size.height * pointScaleFactor); ``` `frame.size.width` and `frame.size.height` are the values returned from the measure method in Java and they are multiplied by the screen density to get the size in pixels, which means Fabric expects these values to be DIPs. Reviewed By: shergin Differential Revision: D17626834 fbshipit-source-id: f9856b5d0796c75c26c84adf11e1652b22a1ddef --- .../facebook/react/views/switchview/ReactSwitchManager.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java index 8164a30a28d..45a43900029 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/switchview/ReactSwitchManager.java @@ -15,6 +15,7 @@ import androidx.annotation.Nullable; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.uimanager.LayoutShadowNode; +import com.facebook.react.uimanager.PixelUtil; import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.ThemedReactContext; import com.facebook.react.uimanager.UIManagerModule; @@ -194,6 +195,8 @@ public class ReactSwitchManager extends SimpleViewManager view.setShowText(false); int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(measureSpec, measureSpec); - return YogaMeasureOutput.make(view.getMeasuredWidth(), view.getMeasuredHeight()); + return YogaMeasureOutput.make( + PixelUtil.toDIPFromPixel(view.getMeasuredWidth()), + PixelUtil.toDIPFromPixel(view.getMeasuredHeight())); } }