Skip to content

Commit 9719891

Browse files
authored
feat(fabric): Add macOS host platform to ReactCommon (#2690)
1 parent f5843d4 commit 9719891

File tree

8 files changed

+279
-1
lines changed

8 files changed

+279
-1
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTPullToRefreshViewComponentView.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
7676
{
7777
// Prop updates are ignored by _refreshControl until after the initial layout, so just store them in _props until then
7878
if (_isBeforeInitialLayout) {
79-
_props = std::static_pointer_cast<const BaseViewProps>(props);
79+
_props = std::static_pointer_cast<const PullToRefreshViewProps>(props); // [macOS]
8080
return;
8181
}
8282

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// [macOS]
9+
10+
#pragma once
11+
12+
#include <react/renderer/components/view/BaseTouch.h>
13+
14+
namespace facebook::react {
15+
using HostPlatformTouch = BaseTouch;
16+
} // namespace facebook::react
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// [macOS]
9+
10+
#include <react/renderer/components/view/HostPlatformViewEventEmitter.h>
11+
12+
namespace facebook::react {
13+
14+
#pragma mark - Focus Events
15+
16+
void HostPlatformViewEventEmitter::onFocus() const {
17+
dispatchEvent("focus");
18+
}
19+
20+
void HostPlatformViewEventEmitter::onBlur() const {
21+
dispatchEvent("blur");
22+
}
23+
24+
} // namespace facebook::react
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// [macOS]
9+
10+
#pragma once
11+
12+
#include <react/renderer/components/view/BaseViewEventEmitter.h>
13+
14+
namespace facebook::react {
15+
16+
class HostPlatformViewEventEmitter : public BaseViewEventEmitter {
17+
public:
18+
using BaseViewEventEmitter::BaseViewEventEmitter;
19+
20+
#pragma mark - Focus Events
21+
22+
void onFocus() const;
23+
void onBlur() const;
24+
};
25+
26+
} // namespace facebook::react
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "HostPlatformViewProps.h"
9+
10+
#include <algorithm>
11+
12+
#include <react/featureflags/ReactNativeFeatureFlags.h>
13+
#include <react/renderer/components/view/conversions.h>
14+
#include <react/renderer/components/view/propsConversions.h>
15+
#include <react/renderer/core/graphicsConversions.h>
16+
#include <react/renderer/core/propsConversions.h>
17+
18+
namespace facebook::react {
19+
20+
HostPlatformViewProps::HostPlatformViewProps(
21+
const PropsParserContext& context,
22+
const HostPlatformViewProps& sourceProps,
23+
const RawProps& rawProps)
24+
: BaseViewProps(context, sourceProps, rawProps),
25+
macOSViewEvents(
26+
ReactNativeFeatureFlags::enableCppPropsIteratorSetter()
27+
? sourceProps.macOSViewEvents
28+
: convertRawProp(
29+
context,
30+
rawProps,
31+
sourceProps.macOSViewEvents,
32+
{})),
33+
focusable(
34+
ReactNativeFeatureFlags::enableCppPropsIteratorSetter()
35+
? sourceProps.focusable
36+
: convertRawProp(
37+
context,
38+
rawProps,
39+
"focusable",
40+
sourceProps.focusable,
41+
{})),
42+
enableFocusRing(
43+
ReactNativeFeatureFlags::enableCppPropsIteratorSetter()
44+
? sourceProps.enableFocusRing
45+
: convertRawProp(
46+
context,
47+
rawProps,
48+
"enableFocusRing",
49+
sourceProps.enableFocusRing,
50+
{})) {}
51+
52+
#define MACOS_VIEW_EVENT_CASE(eventType) \
53+
case CONSTEXPR_RAW_PROPS_KEY_HASH("on" #eventType): { \
54+
const auto offset = MacOSViewEvents::Offset::eventType; \
55+
MacOSViewEvents defaultViewEvents{}; \
56+
bool res = defaultViewEvents[offset]; \
57+
if (value.hasValue()) { \
58+
fromRawValue(context, value, res); \
59+
} \
60+
macOSViewEvents[offset] = res; \
61+
return; \
62+
}
63+
64+
void HostPlatformViewProps::setProp(
65+
const PropsParserContext& context,
66+
RawPropsPropNameHash hash,
67+
const char* propName,
68+
const RawValue& value) {
69+
// All Props structs setProp methods must always, unconditionally,
70+
// call all super::setProp methods, since multiple structs may
71+
// reuse the same values.
72+
BaseViewProps::setProp(context, hash, propName, value);
73+
74+
static auto defaults = HostPlatformViewProps{};
75+
76+
switch (hash) {
77+
RAW_SET_PROP_SWITCH_CASE_BASIC(focusable);
78+
RAW_SET_PROP_SWITCH_CASE_BASIC(enableFocusRing);
79+
MACOS_VIEW_EVENT_CASE(Focus);
80+
MACOS_VIEW_EVENT_CASE(Blur);
81+
82+
}
83+
}
84+
85+
86+
} // namespace facebook::react
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// [macOS]
9+
10+
#pragma once
11+
12+
#include <react/renderer/components/view/BaseViewProps.h>
13+
#include <react/renderer/components/view/primitives.h>
14+
#include <react/renderer/core/Props.h>
15+
#include <react/renderer/core/PropsParserContext.h>
16+
17+
#include "MacOSViewEvents.h"
18+
19+
namespace facebook::react {
20+
21+
class HostPlatformViewProps : public BaseViewProps {
22+
public:
23+
HostPlatformViewProps() = default;
24+
HostPlatformViewProps(
25+
const PropsParserContext& context,
26+
const HostPlatformViewProps& sourceProps,
27+
const RawProps& rawProps);
28+
29+
void setProp(
30+
const PropsParserContext& context,
31+
RawPropsPropNameHash hash,
32+
const char* propName,
33+
const RawValue& value);
34+
35+
MacOSViewEvents macOSViewEvents{};
36+
37+
#pragma mark - Props
38+
39+
bool focusable{false};
40+
bool enableFocusRing{true};
41+
42+
};
43+
} // namespace facebook::react
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/components/view/ViewProps.h>
11+
#include <react/renderer/core/ShadowNodeTraits.h>
12+
13+
namespace facebook::react::HostPlatformViewTraitsInitializer {
14+
15+
inline bool formsStackingContext(const ViewProps& props) {
16+
return false;
17+
}
18+
19+
inline bool formsView(const ViewProps& props) {
20+
return props.focusable;
21+
}
22+
23+
} // namespace facebook::react::HostPlatformViewTraitsInitializer
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/core/PropsParserContext.h>
11+
#include <react/renderer/core/propsConversions.h>
12+
13+
#include <bitset>
14+
15+
namespace facebook::react {
16+
17+
struct MacOSViewEvents {
18+
std::bitset<64> bits{};
19+
20+
enum class Offset : std::size_t {
21+
// Focus Events
22+
Focus = 0,
23+
Blur = 1,
24+
};
25+
26+
constexpr bool operator[](const Offset offset) const {
27+
return bits[static_cast<std::size_t >(offset)];
28+
}
29+
30+
std::bitset<64>::reference operator[](const Offset offset) {
31+
return bits[static_cast<std::size_t >(offset)];
32+
}
33+
};
34+
35+
inline static bool operator==(MacOSViewEvents const &lhs, MacOSViewEvents const &rhs) {
36+
return lhs.bits == rhs.bits;
37+
}
38+
39+
inline static bool operator!=(MacOSViewEvents const &lhs, MacOSViewEvents const &rhs) {
40+
return lhs.bits != rhs.bits;
41+
}
42+
43+
static inline MacOSViewEvents convertRawProp(
44+
const PropsParserContext &context,
45+
const RawProps &rawProps,
46+
const MacOSViewEvents &sourceValue,
47+
const MacOSViewEvents &defaultValue) {
48+
MacOSViewEvents result{};
49+
using Offset = MacOSViewEvents::Offset;
50+
51+
// Focus Events
52+
result[Offset::Focus] =
53+
convertRawProp(context, rawProps, "onFocus", sourceValue[Offset::Focus], defaultValue[Offset::Focus]);
54+
result[Offset::Blur] =
55+
convertRawProp(context, rawProps, "onBlur", sourceValue[Offset::Blur], defaultValue[Offset::Blur]);
56+
57+
return result;
58+
}
59+
60+
} // namespace facebook::react

0 commit comments

Comments
 (0)