Skip to content

Commit

Permalink
Extract the UNSET = -1 constant
Browse files Browse the repository at this point in the history
  • Loading branch information
cubuspl42 committed Jan 3, 2024
1 parent 36ce142 commit 3353e5e
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.facebook.react.common

/**
* General-purpose integer constants
*/
object IntConstants {
/**
* Some types have built-in support for representing a "missing" or "unset" value, for example
* NaN in the case of floating point numbers or null in the case of object references. Integers
* don't have such a special value. When an integer represent an inherently non-negative value,
* we use a special negative value to mark it as "unset".
*/
const val UNSET: Int = -1
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.common.IntConstants;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -167,8 +169,6 @@ public static class TypefaceStyle {

public static final int BOLD = 700;
public static final int NORMAL = 400;
public static final int UNSET = -1;

private static final int MIN_WEIGHT = 1;
private static final int MAX_WEIGHT = 1000;

Expand All @@ -177,11 +177,11 @@ public static class TypefaceStyle {

public TypefaceStyle(int weight, boolean italic) {
mItalic = italic;
mWeight = weight == UNSET ? NORMAL : weight;
mWeight = weight == IntConstants.UNSET ? NORMAL : weight;
}

public TypefaceStyle(int style) {
if (style == UNSET) {
if (style == IntConstants.UNSET) {
style = Typeface.NORMAL;
}

Expand All @@ -194,12 +194,12 @@ public TypefaceStyle(int style) {
* existing weight bit in `style` will be used.
*/
public TypefaceStyle(int style, int weight) {
if (style == UNSET) {
if (style == IntConstants.UNSET) {
style = Typeface.NORMAL;
}

mItalic = (style & Typeface.ITALIC) != 0;
mWeight = weight == UNSET ? (style & Typeface.BOLD) != 0 ? BOLD : NORMAL : weight;
mWeight = weight == IntConstants.UNSET ? (style & Typeface.BOLD) != 0 ? BOLD : NORMAL : weight;
}

public int getNearestStyle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.text.style.MetricAffectingSpan;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.common.IntConstants;
import com.facebook.react.common.assets.ReactFontManager;

@Nullsafe(Nullsafe.Mode.LOCAL)
Expand Down Expand Up @@ -61,11 +62,11 @@ public void updateMeasureState(TextPaint paint) {
}

public int getStyle() {
return mStyle == ReactFontManager.TypefaceStyle.UNSET ? Typeface.NORMAL : mStyle;
return mStyle == IntConstants.UNSET ? Typeface.NORMAL : mStyle;
}

public int getWeight() {
return mWeight == ReactFontManager.TypefaceStyle.UNSET
return mWeight == IntConstants.UNSET
? ReactFontManager.TypefaceStyle.NORMAL
: mWeight;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.facebook.react.views.text

import com.facebook.react.common.assets.ReactFontManager
import com.facebook.react.common.IntConstants.UNSET

/**
* Interface for an entity providing effective text attributes of a text node/fragment
*/
internal interface EffectiveTextAttributeProvider : BasicTextAttributeProvider {
companion object {
const val UNSET = ReactFontManager.TypefaceStyle.UNSET
}

val textTransform: TextTransform

val effectiveLetterSpacing: Float
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.facebook.react.views.text

import com.facebook.react.views.text.ReactBaseTextShadowNode
import com.facebook.react.views.text.TextAttributes
import com.facebook.react.views.text.TextTransform
import com.facebook.react.common.IntConstants

/**
* Implementation of [EffectiveTextAttributeProvider] that provides effective text
Expand Down Expand Up @@ -37,7 +35,7 @@ internal class HierarchicTextAttributeProvider(
return if (parentTextAttributes == null || parentTextAttributes.effectiveFontSize != fontSize) {
fontSize
} else {
EffectiveTextAttributeProvider.UNSET
IntConstants.UNSET
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.IntConstants;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.assets.ReactFontManager;
import com.facebook.react.views.text.BasicTextAttributeProvider;
import com.facebook.react.views.text.HierarchicTextAttributeProvider;
import com.facebook.react.views.text.TextLayoutUtils;
import com.facebook.react.config.ReactFeatureFlags;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.LayoutShadowNode;
Expand Down Expand Up @@ -59,7 +56,6 @@ public abstract class ReactBaseTextShadowNode extends LayoutShadowNode implement
// character.
// https://en.wikipedia.org/wiki/Bi-directional_text#weak_characters
private static final String INLINE_VIEW_PLACEHOLDER = "0";
public static final int UNSET = ReactFontManager.TypefaceStyle.UNSET;

public static final String PROP_SHADOW_OFFSET = "textShadowOffset";
public static final String PROP_SHADOW_OFFSET_WIDTH = "width";
Expand Down Expand Up @@ -212,8 +208,8 @@ private static void buildSpannedFromShadowNodeDuplicated(
|| parentTextAttributes.getEffectiveFontSize() != effectiveFontSize) {
ops.add(new SetSpanOperation(start, end, new ReactAbsoluteSizeSpan(effectiveFontSize)));
}
if (textShadowNode.mFontStyle != UNSET
|| textShadowNode.mFontWeight != UNSET
if (textShadowNode.mFontStyle != IntConstants.UNSET
|| textShadowNode.mFontWeight != IntConstants.UNSET
|| textShadowNode.mFontFamily != null) {
ops.add(
new SetSpanOperation(
Expand Down Expand Up @@ -429,7 +425,7 @@ protected Spannable spannedFromShadowNode(
protected @Nullable AccessibilityRole mAccessibilityRole = null;
protected @Nullable Role mRole = null;

protected int mNumberOfLines = UNSET;
protected int mNumberOfLines = IntConstants.UNSET;
protected int mTextAlign = Gravity.NO_GRAVITY;
protected int mTextBreakStrategy = Layout.BREAK_STRATEGY_HIGH_QUALITY;
protected int mHyphenationFrequency = Layout.HYPHENATION_FREQUENCY_NONE;
Expand All @@ -451,9 +447,9 @@ protected Spannable spannedFromShadowNode(
* mFontStyle can be {@link Typeface#NORMAL} or {@link Typeface#ITALIC}. mFontWeight can be {@link
* Typeface#NORMAL} or {@link Typeface#BOLD}.
*/
protected int mFontStyle = UNSET;
protected int mFontStyle = IntConstants.UNSET;

protected int mFontWeight = UNSET;
protected int mFontWeight = IntConstants.UNSET;
/**
* NB: If a font family is used that does not have a style in a certain Android version (ie.
* monospace bold pre Android 5.0), that style (ie. bold) will not be inherited by nested Text
Expand Down Expand Up @@ -506,9 +502,9 @@ private int getTextAlign() {
return textAlign;
}

@ReactProp(name = ViewProps.NUMBER_OF_LINES, defaultInt = UNSET)
@ReactProp(name = ViewProps.NUMBER_OF_LINES, defaultInt = IntConstants.UNSET)
public void setNumberOfLines(int numberOfLines) {
mNumberOfLines = numberOfLines == 0 ? UNSET : numberOfLines;
mNumberOfLines = numberOfLines == 0 ? IntConstants.UNSET : numberOfLines;
markUpdated();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.react.bridge.ReactSoftExceptionLogger;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.IntConstants;
import com.facebook.react.uimanager.NativeViewHierarchyOptimizer;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactShadowNode;
Expand Down Expand Up @@ -80,7 +81,7 @@ public long measure(
int minimumFontSize =
(int) Math.max(mMinimumFontScale * initialFontSize, PixelUtil.toPixelFromDIP(4));
while (currentFontSize > minimumFontSize
&& (mNumberOfLines != UNSET && layout.getLineCount() > mNumberOfLines
&& (mNumberOfLines != IntConstants.UNSET && layout.getLineCount() > mNumberOfLines
|| heightMode != YogaMeasureMode.UNDEFINED && layout.getHeight() > height)) {
// TODO: We could probably use a smarter algorithm here. This will require 0(n)
// measurements
Expand Down Expand Up @@ -122,7 +123,7 @@ public long measure(
}

final int lineCount =
mNumberOfLines == UNSET
mNumberOfLines == IntConstants.UNSET
? layout.getLineCount()
: Math.min(mNumberOfLines, layout.getLineCount());

Expand Down Expand Up @@ -327,7 +328,7 @@ public void onCollectExtraUpdates(UIViewOperationQueue uiViewOperationQueue) {
ReactTextUpdate reactTextUpdate =
new ReactTextUpdate(
mPreparedSpannableText,
UNSET,
IntConstants.UNSET,
mContainsImages,
getPadding(Spacing.START),
getPadding(Spacing.TOP),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

package com.facebook.react.views.text;

import static com.facebook.react.views.text.TextAttributeProps.UNSET;

import android.text.Layout;
import android.text.Spannable;

import com.facebook.react.common.IntConstants;

/**
* Class that contains the data needed for a text update. Used by both <Text/> and <TextInput/>
* VisibleForTesting from {@link TextInputEventsTestCase}.
Expand Down Expand Up @@ -67,10 +67,10 @@ public ReactTextUpdate(
text,
jsEventCounter,
containsImages,
UNSET,
UNSET,
UNSET,
UNSET,
IntConstants.UNSET,
IntConstants.UNSET,
IntConstants.UNSET,
IntConstants.UNSET,
textAlign,
textBreakStrategy,
justificationMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

package com.facebook.react.views.text;

import static com.facebook.react.views.text.TextAttributeProps.UNSET;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
Expand All @@ -35,6 +33,7 @@
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.IntConstants;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.ReactCompoundView;
Expand Down Expand Up @@ -378,10 +377,10 @@ public void setText(ReactTextUpdate update) {
// In Fabric padding is set by the update of Layout Metrics and not as part of the "setText"
// operation
// TODO T56559197: remove this condition when we migrate 100% to Fabric
if (paddingLeft != UNSET
&& paddingTop != UNSET
&& paddingRight != UNSET
&& paddingBottom != UNSET) {
if (paddingLeft != IntConstants.UNSET
&& paddingTop != IntConstants.UNSET
&& paddingRight != IntConstants.UNSET
&& paddingBottom != IntConstants.UNSET) {

setPadding(
(int) Math.floor(paddingLeft),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.IntConstants;
import com.facebook.react.common.assets.ReactFontManager;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -45,7 +46,7 @@ public static int parseFontWeight(@Nullable String fontWeightString) {
return 900;
}
}
return ReactFontManager.TypefaceStyle.UNSET;
return IntConstants.UNSET;
}

public static int parseFontStyle(@Nullable String fontStyleString) {
Expand All @@ -57,7 +58,7 @@ public static int parseFontStyle(@Nullable String fontStyleString) {
return Typeface.NORMAL;
}
}
return ReactFontManager.TypefaceStyle.UNSET;
return IntConstants.UNSET;
}

public static @Nullable String parseFontVariant(@Nullable ReadableArray fontVariantArray) {
Expand Down
Loading

0 comments on commit 3353e5e

Please sign in to comment.