Skip to content

Commit cfefc1a

Browse files
committed
flatten callbacks bools
1 parent 051a0c5 commit cfefc1a

3 files changed

Lines changed: 129 additions & 40 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.cpp‎

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,54 @@ static std::string convertFontWeight(
4949
return convertRawProp(context, rawProps, "fontWeight", sourceValue, {});
5050
}
5151

52+
static AndroidTextInputEvents convertRawProp(
53+
const PropsParserContext& context,
54+
const RawProps& rawProps,
55+
const AndroidTextInputEvents& sourceValue) {
56+
using Offset = AndroidTextInputEvents::Offset;
57+
AndroidTextInputEvents result{};
58+
result.set(
59+
Offset::KeyPress,
60+
convertRawProp(
61+
context,
62+
rawProps,
63+
"onKeyPress",
64+
sourceValue[Offset::KeyPress],
65+
false));
66+
result.set(
67+
Offset::SelectionChange,
68+
convertRawProp(
69+
context,
70+
rawProps,
71+
"onSelectionChange",
72+
sourceValue[Offset::SelectionChange],
73+
false));
74+
result.set(
75+
Offset::ContentSizeChange,
76+
convertRawProp(
77+
context,
78+
rawProps,
79+
"onContentSizeChange",
80+
sourceValue[Offset::ContentSizeChange],
81+
false));
82+
result.set(
83+
Offset::Scroll,
84+
convertRawProp(
85+
context, rawProps, "onScroll", sourceValue[Offset::Scroll], false));
86+
return result;
87+
}
88+
89+
static void updateEventProp(
90+
folly::dynamic& result,
91+
const AndroidTextInputEvents& newEvents,
92+
const AndroidTextInputEvents& oldEvents,
93+
AndroidTextInputEvents::Offset offset,
94+
const char* name) {
95+
if (newEvents[offset] != oldEvents[offset]) {
96+
result[name] = newEvents[offset];
97+
}
98+
}
99+
52100
AndroidTextInputProps::AndroidTextInputProps(
53101
const PropsParserContext &context,
54102
const AndroidTextInputProps &sourceProps,
@@ -167,16 +215,7 @@ AndroidTextInputProps::AndroidTextInputProps(
167215
{false})),
168216
fontWeight(convertFontWeight(context, rawProps, sourceProps.fontWeight)),
169217
fontFamily(convertRawProp(context, rawProps, "fontFamily", sourceProps.fontFamily, {})),
170-
onKeyPress(convertRawProp(context, rawProps, "onKeyPress", sourceProps.onKeyPress, {false})),
171-
onSelectionChange(convertRawProp(context, rawProps,
172-
"onSelectionChange",
173-
sourceProps.onSelectionChange,
174-
{false})),
175-
onContentSizeChange(convertRawProp(context, rawProps,
176-
"onContentSizeChange",
177-
sourceProps.onContentSizeChange,
178-
{false})),
179-
onScroll(convertRawProp(context, rawProps, "onScroll", sourceProps.onScroll, {false})),
218+
textInputEvents(convertRawProp(context, rawProps, sourceProps.textInputEvents)),
180219
// See AndroidTextInputComponentDescriptor for usage
181220
// TODO T63008435: can these, and this feature, be removed entirely?
182221
hasPadding(hasValue(rawProps, sourceProps.hasPadding, "padding")),
@@ -208,6 +247,16 @@ AndroidTextInputProps::AndroidTextInputProps(
208247
hasPaddingEnd(hasValue(rawProps, sourceProps.hasPaddingEnd, "paddingEnd")) {
209248
}
210249

250+
#define TEXT_INPUT_EVENT_CASE(eventType) \
251+
case CONSTEXPR_RAW_PROPS_KEY_HASH("on" #eventType): { \
252+
bool res = false; \
253+
if (value.hasValue()) { \
254+
fromRawValue(context, value, res); \
255+
} \
256+
textInputEvents.set(AndroidTextInputEvents::Offset::eventType, res); \
257+
return; \
258+
}
259+
211260
void AndroidTextInputProps::setProp(
212261
const PropsParserContext& context,
213262
RawPropsPropNameHash hash,
@@ -252,10 +301,10 @@ void AndroidTextInputProps::setProp(
252301
RAW_SET_PROP_SWITCH_CASE_BASIC(textAlign);
253302
RAW_SET_PROP_SWITCH_CASE_BASIC(includeFontPadding);
254303
RAW_SET_PROP_SWITCH_CASE_BASIC(fontFamily);
255-
RAW_SET_PROP_SWITCH_CASE_BASIC(onKeyPress);
256-
RAW_SET_PROP_SWITCH_CASE_BASIC(onSelectionChange);
257-
RAW_SET_PROP_SWITCH_CASE_BASIC(onContentSizeChange);
258-
RAW_SET_PROP_SWITCH_CASE_BASIC(onScroll);
304+
TEXT_INPUT_EVENT_CASE(KeyPress);
305+
TEXT_INPUT_EVENT_CASE(SelectionChange);
306+
TEXT_INPUT_EVENT_CASE(ContentSizeChange);
307+
TEXT_INPUT_EVENT_CASE(Scroll);
259308

260309
case CONSTEXPR_RAW_PROPS_KEY_HASH("value"): {
261310
fromRawValue(context, value, this->value, {});
@@ -665,20 +714,25 @@ folly::dynamic AndroidTextInputProps::getDiffProps(
665714
: folly::dynamic(nullptr);
666715
}
667716

668-
if (onKeyPress != oldProps->onKeyPress) {
669-
result["onKeyPress"] = onKeyPress;
670-
}
671-
672-
if (onSelectionChange != oldProps->onSelectionChange) {
673-
result["onSelectionChange"] = onSelectionChange;
674-
}
675-
676-
if (onContentSizeChange != oldProps->onContentSizeChange) {
677-
result["onContentSizeChange"] = onContentSizeChange;
678-
}
679-
680-
if (onScroll != oldProps->onScroll) {
681-
result["onScroll"] = onScroll;
717+
if (textInputEvents != oldProps->textInputEvents) {
718+
using Offset = AndroidTextInputEvents::Offset;
719+
const auto& oldEvents = oldProps->textInputEvents;
720+
updateEventProp(
721+
result, textInputEvents, oldEvents, Offset::KeyPress, "onKeyPress");
722+
updateEventProp(
723+
result,
724+
textInputEvents,
725+
oldEvents,
726+
Offset::SelectionChange,
727+
"onSelectionChange");
728+
updateEventProp(
729+
result,
730+
textInputEvents,
731+
oldEvents,
732+
Offset::ContentSizeChange,
733+
"onContentSizeChange");
734+
updateEventProp(
735+
result, textInputEvents, oldEvents, Offset::Scroll, "onScroll");
682736
}
683737

684738
return result;

‎packages/react-native/ReactCommon/react/renderer/components/textinput/platform/android/react/renderer/components/androidtextinput/AndroidTextInputProps.h‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <react/renderer/attributedstring/conversions.h>
1717
#include <react/renderer/components/textinput/BaseTextInputProps.h>
1818
#include <react/renderer/components/textinput/basePrimitives.h>
19+
#include <cstdint>
1920
#include <unordered_map>
2021

2122
namespace facebook::react {
@@ -60,6 +61,35 @@ inline folly::dynamic toDynamic(const AndroidTextInputTextShadowOffsetStruct &va
6061
return dynamicValue;
6162
}
6263

64+
/*
65+
* Whether JS listens to these events. ReactTextInputManager only sets up the
66+
* native watchers for them when their props are sent. Stored in a uint8_t
67+
* because a std::bitset takes at least a full machine word, even for 4 bits.
68+
*/
69+
struct AndroidTextInputEvents {
70+
uint8_t bits{};
71+
72+
enum class Offset : uint8_t {
73+
KeyPress = 0,
74+
SelectionChange = 1,
75+
ContentSizeChange = 2,
76+
Scroll = 3,
77+
};
78+
79+
constexpr bool operator[](const Offset offset) const
80+
{
81+
return ((bits >> static_cast<uint8_t>(offset)) & 1) != 0;
82+
}
83+
84+
constexpr void set(const Offset offset, const bool value)
85+
{
86+
const auto mask = static_cast<uint8_t>(1 << static_cast<uint8_t>(offset));
87+
bits = static_cast<uint8_t>(value ? (bits | mask) : (bits & ~mask));
88+
}
89+
90+
bool operator==(const AndroidTextInputEvents &rhs) const = default;
91+
};
92+
6393
class AndroidTextInputProps final : public BaseTextInputProps {
6494
public:
6595
AndroidTextInputProps() = default;
@@ -109,14 +139,8 @@ class AndroidTextInputProps final : public BaseTextInputProps {
109139
std::string fontWeight{};
110140
std::string fontFamily{};
111141

112-
/*
113-
* Whether JS listens to these events. ReactTextInputManager only sets up the
114-
* native watchers for them when these props are sent.
115-
*/
116-
bool onKeyPress{false};
117-
bool onSelectionChange{false};
118-
bool onContentSizeChange{false};
119-
bool onScroll{false};
142+
// Named so it doesn't hide BaseViewProps::events.
143+
AndroidTextInputEvents textInputEvents{};
120144

121145
/**
122146
* Auxiliary information to detect if these props are set or not.

‎scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,13 +1187,10 @@ class facebook::react::AndroidTextInputProps : public facebook::react::BaseTextI
11871187
public bool hasPaddingTop;
11881188
public bool hasPaddingVertical;
11891189
public bool includeFontPadding;
1190-
public bool onContentSizeChange;
1191-
public bool onKeyPress;
1192-
public bool onScroll;
1193-
public bool onSelectionChange;
11941190
public bool secureTextEntry;
11951191
public bool selectTextOnFocus;
11961192
public bool showSoftInputOnFocus;
1193+
public facebook::react::AndroidTextInputEvents textInputEvents;
11971194
public facebook::react::AndroidTextInputTextShadowOffsetStruct textShadowOffset;
11981195
public facebook::react::Float fontSize;
11991196
public facebook::react::Float letterSpacing;
@@ -5569,6 +5566,20 @@ struct facebook::react::ActivePointer {
55695566
public facebook::react::PointerEvent event;
55705567
}
55715568

5569+
struct facebook::react::AndroidTextInputEvents {
5570+
public bool operator==(const facebook::react::AndroidTextInputEvents& rhs) const = default;
5571+
public constexpr bool operator[](const facebook::react::AndroidTextInputEvents::Offset offset) const;
5572+
public constexpr void set(const facebook::react::AndroidTextInputEvents::Offset offset, const bool value);
5573+
public uint8_t bits;
5574+
}
5575+
5576+
enum facebook::react::AndroidTextInputEvents::Offset : uint8_t {
5577+
ContentSizeChange,
5578+
KeyPress,
5579+
Scroll,
5580+
SelectionChange,
5581+
}
5582+
55725583
struct facebook::react::AndroidTextInputTextShadowOffsetStruct {
55735584
public double height;
55745585
public double width;

0 commit comments

Comments
 (0)