Skip to content

Commit

Permalink
Move textAlignVertical into TextAttributes
Browse files Browse the repository at this point in the history
Summary:
# Changelog:
[Internal] - 

This is a generally cross-platform attribute, which is supported not only on Android, and conceptually does arguably belong in TextAttributes.

Differential Revision: D57181633
  • Loading branch information
rshest authored and facebook-github-bot committed May 10, 2024
1 parent c647950 commit 4439f06
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ void TextAttributes::apply(TextAttributes textAttributes) {
? textAttributes.accessibilityRole
: accessibilityRole;
role = textAttributes.role.has_value() ? textAttributes.role : role;

textAlignVertical = textAttributes.textAlignVertical.has_value()
? textAttributes.textAlignVertical
: textAlignVertical;
}

#pragma mark - Operators
Expand All @@ -123,6 +127,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
allowFontScaling,
dynamicTypeRamp,
alignment,
textAlignVertical,
baseWritingDirection,
lineBreakStrategy,
textDecorationColor,
Expand All @@ -146,6 +151,7 @@ bool TextAttributes::operator==(const TextAttributes& rhs) const {
rhs.allowFontScaling,
rhs.dynamicTypeRamp,
rhs.alignment,
rhs.textAlignVertical,
rhs.baseWritingDirection,
rhs.lineBreakStrategy,
rhs.textDecorationColor,
Expand Down Expand Up @@ -228,6 +234,8 @@ SharedDebugStringConvertibleList TextAttributes::getDebugProps() const {
debugStringConvertibleItem("layoutDirection", layoutDirection),
debugStringConvertibleItem("accessibilityRole", accessibilityRole),
debugStringConvertibleItem("role", role),

debugStringConvertibleItem("textAlignVertical", textAlignVertical),
};
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class TextAttributes : public DebugStringConvertible {
std::optional<AccessibilityRole> accessibilityRole{};
std::optional<Role> role{};

std::optional<TextAlignmentVertical> textAlignVertical{};

#pragma mark - Operations

void apply(TextAttributes textAttributes);
Expand Down Expand Up @@ -123,6 +125,7 @@ struct hash<facebook::react::TextAttributes> {
textAttributes.textTransform,
textAttributes.lineHeight,
textAttributes.alignment,
textAttributes.textAlignVertical,
textAttributes.baseWritingDirection,
textAttributes.lineBreakStrategy,
textAttributes.textDecorationColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,52 @@ inline std::string toString(const TextAlignment& textAlignment) {
return "auto";
}

inline void fromRawValue(
const PropsParserContext& /*context*/,
const RawValue& value,
TextAlignmentVertical& result) {
react_native_expect(value.hasType<std::string>());
if (value.hasType<std::string>()) {
auto string = (std::string)value;
if (string == "auto") {
result = TextAlignmentVertical::Auto;
} else if (string == "top") {
result = TextAlignmentVertical::Top;
} else if (string == "bottom") {
result = TextAlignmentVertical::Bottom;
} else if (string == "center") {
result = TextAlignmentVertical::Center;
} else {
LOG(ERROR) << "Unsupported TextAlignment value: " << string;
react_native_expect(false);
// sane default for prod
result = TextAlignmentVertical::Auto;
}
return;
}

LOG(ERROR) << "Unsupported TextAlignmentVertical type";
// sane default for prod
result = TextAlignmentVertical::Auto;
}

inline std::string toString(const TextAlignmentVertical& textAlignment) {
switch (textAlignment) {
case TextAlignmentVertical::Auto:
return "auto";
case TextAlignmentVertical::Top:
return "top";
case TextAlignmentVertical::Bottom:
return "bottom";
case TextAlignmentVertical::Center:
return "center";
}

LOG(ERROR) << "Unsupported TextAlignmentVertical value";
// sane default for prod
return "auto";
}

inline void fromRawValue(
const PropsParserContext& context,
const RawValue& value,
Expand Down Expand Up @@ -893,6 +939,10 @@ inline folly::dynamic toDynamic(const TextAttributes& textAttributes) {
_textAttributes(
"accessibilityRole", toString(*textAttributes.accessibilityRole));
}
if (textAttributes.textAlignVertical.has_value()) {
_textAttributes(
"textAlignVertical", toString(*textAttributes.textAlignVertical));
}
return _textAttributes;
}

Expand Down Expand Up @@ -975,6 +1025,7 @@ constexpr static MapBuffer::Key TA_KEY_ACCESSIBILITY_ROLE = 24;
constexpr static MapBuffer::Key TA_KEY_LINE_BREAK_STRATEGY = 25;
constexpr static MapBuffer::Key TA_KEY_ROLE = 26;
constexpr static MapBuffer::Key TA_KEY_TEXT_TRANSFORM = 27;
constexpr static MapBuffer::Key TA_KEY_ALIGNMENT_VERTICAL = 28;

// constants for ParagraphAttributes serialization
constexpr static MapBuffer::Key PA_KEY_MAX_NUMBER_OF_LINES = 0;
Expand Down Expand Up @@ -1141,6 +1192,10 @@ inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) {
if (textAttributes.role.has_value()) {
builder.putInt(TA_KEY_ROLE, static_cast<int32_t>(*textAttributes.role));
}
if (textAttributes.textAlignVertical.has_value()) {
builder.putString(
TA_KEY_ALIGNMENT_VERTICAL, toString(*textAttributes.textAlignVertical));
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ enum class TextAlignment {
Justified // Fully-justified. The last line in a paragraph is natural-aligned.
};

enum class TextAlignmentVertical {
Auto,
Top,
Bottom,
Center,
};

enum class WritingDirection {
Natural, // Determines direction using the Unicode Bidi Algorithm rules P2 and
// P3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ static TextAttributes convertRawProp(
"textTransform",
sourceTextAttributes.textTransform,
defaultTextAttributes.textTransform);
textAttributes.textAlignVertical = convertRawProp(
context,
rawProps,
"textAlignVertical",
sourceTextAttributes.textAlignVertical,
defaultTextAttributes.textAlignVertical);

// Paragraph
textAttributes.lineHeight = convertRawProp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ AndroidTextInputProps::AndroidTextInputProps(
convertRawProp(context, rawProps, "fontWeight", sourceProps.fontWeight, {})),
fontFamily(CoreFeatures::enablePropIteratorSetter? sourceProps.fontFamily :
convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})),
textAlignVertical(CoreFeatures::enablePropIteratorSetter? sourceProps.textAlignVertical : convertRawProp(context, rawProps,
"textAlignVertical",
sourceProps.textAlignVertical,
{})),
// See AndroidTextInputComponentDescriptor for usage
// TODO T63008435: can these, and this feature, be removed entirely?
hasPadding(CoreFeatures::enablePropIteratorSetter? sourceProps.hasPadding : hasValue(rawProps, sourceProps.hasPadding, "padding")),
Expand Down Expand Up @@ -250,7 +246,6 @@ void AndroidTextInputProps::setProp(
RAW_SET_PROP_SWITCH_CASE_BASIC(includeFontPadding);
RAW_SET_PROP_SWITCH_CASE_BASIC(fontWeight);
RAW_SET_PROP_SWITCH_CASE_BASIC(fontFamily);
RAW_SET_PROP_SWITCH_CASE_BASIC(textAlignVertical);

case CONSTEXPR_RAW_PROPS_KEY_HASH("value"): {
fromRawValue(context, value, this->value, {});
Expand Down Expand Up @@ -348,7 +343,6 @@ folly::dynamic AndroidTextInputProps::getDynamic() const {
props["includeFontPadding"] = includeFontPadding;
props["fontWeight"] = fontWeight;
props["fontFamily"] = fontFamily;
props["textAlignVertical"] = textAlignVertical;
props["cursorColor"] = toAndroidRepr(cursorColor);
props["mostRecentEventCount"] = mostRecentEventCount;
props["text"] = text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ class AndroidTextInputProps final : public BaseTextInputProps {
bool includeFontPadding{false};
std::string fontWeight{};
std::string fontFamily{};
std::string textAlignVertical{};

/**
* Auxiliary information to detect if these props are set or not.
Expand Down

0 comments on commit 4439f06

Please sign in to comment.