Skip to content

Commit

Permalink
Convert FabricUIManager.measure params to floats
Browse files Browse the repository at this point in the history
Summary: Convert FabricUIManager.measure params to floats. Currently we convert parameters to ints across the JNI boundary, and then back to floats several times in Java. This is unnecessary and actually makes measurements trickier. The new implementation uses floats across the JNI boundary and uses Float.POSITIVE_INFINITY to represent unconstrained values, which is consistent with Fabric C++ as well.

Reviewed By: shergin, mdvacca

Differential Revision: D15176108

fbshipit-source-id: cf849b3773007637f059279460163872f300a4aa
  • Loading branch information
JoshuaGross authored and facebook-github-bot committed May 3, 2019
1 parent a87af19 commit 4a1d532
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ private long measure(
String componentName,
ReadableMap localData,
ReadableMap props,
int minWidth,
int maxWidth,
int minHeight,
int maxHeight) {
float minWidth,
float maxWidth,
float minHeight,
float maxHeight) {

return mMountingManager.measure(
mReactApplicationContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@

public class LayoutMetricsConversions {

// Represents the Layout constraint mode "undefined" from React side.
public static final int REACT_CONSTRAINT_UNDEFINED = -2147483648;

public static float getMinSize(int viewMeasureSpec) {
int mode = View.MeasureSpec.getMode(viewMeasureSpec);
int size = View.MeasureSpec.getSize(viewMeasureSpec);
Expand All @@ -28,30 +25,27 @@ public static float getMaxSize(int viewMeasureSpec) {
int mode = View.MeasureSpec.getMode(viewMeasureSpec);
int size = View.MeasureSpec.getSize(viewMeasureSpec);

return mode == View.MeasureSpec.UNSPECIFIED ? REACT_CONSTRAINT_UNDEFINED : size;
// Infinity represents an "unconstrained" size
return mode == View.MeasureSpec.UNSPECIFIED ? Float.POSITIVE_INFINITY : size;
}

public static float getYogaSize(float minSize, float maxSize) {
float yogaSize;
if (minSize == maxSize) {
yogaSize = PixelUtil.toPixelFromDIP(maxSize);
} else if (maxSize == REACT_CONSTRAINT_UNDEFINED) {
yogaSize = 0;
return PixelUtil.toPixelFromDIP(maxSize);
} else if (Float.isInfinite(maxSize)) {
return Float.POSITIVE_INFINITY;
} else {
yogaSize = PixelUtil.toPixelFromDIP(maxSize);
return PixelUtil.toPixelFromDIP(maxSize);
}
return yogaSize;
}

public static YogaMeasureMode getYogaMeasureMode(float minSize, float maxSize) {
YogaMeasureMode yogaMeasureMode;
if (minSize == maxSize) {
yogaMeasureMode = YogaMeasureMode.EXACTLY;
} else if (maxSize == REACT_CONSTRAINT_UNDEFINED) {
yogaMeasureMode = YogaMeasureMode.UNDEFINED;
return YogaMeasureMode.EXACTLY;
} else if (Float.isInfinite(maxSize)) {
return YogaMeasureMode.UNDEFINED;
} else {
yogaMeasureMode = YogaMeasureMode.AT_MOST;
return YogaMeasureMode.AT_MOST;
}
return yogaMeasureMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,13 @@ Size SliderMeasurementsManager::measure(
jstring,
ReadableMap::javaobject,
ReadableMap::javaobject,
jint,
jint,
jint,
jint)>("measure");
jfloat,
jfloat,
jfloat,
jfloat)>("measure");

auto minimumSize = layoutConstraints.minimumSize;
auto maximumSize = layoutConstraints.maximumSize;
int minWidth = (int)minimumSize.width;
int minHeight = (int)minimumSize.height;
int maxWidth = (int)maximumSize.width;
int maxHeight = (int)maximumSize.height;

local_ref<JString> componentName = make_jstring("RCTSlider");

Expand All @@ -54,10 +50,10 @@ Size SliderMeasurementsManager::measure(
componentName.get(),
nullptr,
nullptr,
minWidth,
maxWidth,
minHeight,
maxHeight));
minimumSize.width,
maximumSize.width,
minimumSize.height,
maximumSize.height));

std::lock_guard<std::mutex> lock(mutex_);
cachedMeasurement_ = measurement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,13 @@ Size TextLayoutManager::measure(
jstring,
ReadableMap::javaobject,
ReadableMap::javaobject,
jint,
jint,
jint,
jint)>("measure");
jfloat,
jfloat,
jfloat,
jfloat)>("measure");

auto minimumSize = layoutConstraints.minimumSize;
auto maximumSize = layoutConstraints.maximumSize;
int minWidth = (int)minimumSize.width;
int minHeight = (int)minimumSize.height;
int maxWidth = (int)maximumSize.width;
int maxHeight = (int)maximumSize.height;

local_ref<JString> componentName = make_jstring("RCTText");
local_ref<ReadableNativeMap::javaobject> attributedStringRNM =
Expand All @@ -63,10 +59,10 @@ Size TextLayoutManager::measure(
componentName.get(),
attributedStringRM.get(),
paragraphAttributesRM.get(),
minWidth,
maxWidth,
minHeight,
maxHeight));
minimumSize.width,
maximumSize.width,
minimumSize.height,
maximumSize.height));
}

} // namespace react
Expand Down

0 comments on commit 4a1d532

Please sign in to comment.