Skip to content

Commit be45aa5

Browse files
authored
[Fabric] Move to using winrt objects for ComponentViews (#12580)
* Move to using winrt objects for ComponentViews * Change files * fix
1 parent c7910ee commit be45aa5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+869
-508
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Move to using winrt objects for ComponentViews",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#include "DocString.h"
5+
6+
namespace Microsoft.ReactNative
7+
{
8+
9+
[experimental]
10+
[webhosthidden]
11+
unsealed runtimeclass ComponentView {
12+
Int32 Tag { get; };
13+
};
14+
15+
} // namespace Microsoft.ReactNative
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import "ComponentView.idl";
5+
import "Theme.idl";
6+
import "ViewProps.idl";
7+
8+
#include "DocString.h"
9+
10+
namespace Microsoft.ReactNative.Composition
11+
{
12+
13+
[experimental]
14+
[webhosthidden]
15+
unsealed runtimeclass ComponentView : Microsoft.ReactNative.ComponentView {
16+
Theme Theme;
17+
};
18+
19+
[experimental]
20+
[webhosthidden]
21+
unsealed runtimeclass ViewComponentView : ComponentView {
22+
Microsoft.ReactNative.ViewProps ViewProps { get; };
23+
};
24+
25+
[experimental]
26+
[webhosthidden]
27+
[default_interface]
28+
unsealed runtimeclass SwitchComponentView : ComponentView {
29+
};
30+
31+
[experimental]
32+
[webhosthidden]
33+
[default_interface]
34+
unsealed runtimeclass RootComponentView : ViewComponentView {
35+
};
36+
37+
[experimental]
38+
[webhosthidden]
39+
[default_interface]
40+
unsealed runtimeclass AbiCompositionViewComponentView : ComponentView {
41+
};
42+
43+
[experimental]
44+
[webhosthidden]
45+
[default_interface]
46+
unsealed runtimeclass ActivityIndicatorComponentView : ComponentView {
47+
};
48+
49+
[experimental]
50+
[webhosthidden]
51+
[default_interface]
52+
unsealed runtimeclass ImageComponentView : ComponentView {
53+
};
54+
55+
[experimental]
56+
[webhosthidden]
57+
[default_interface]
58+
unsealed runtimeclass ParagraphComponentView : ComponentView {
59+
};
60+
61+
[experimental]
62+
[webhosthidden]
63+
[default_interface]
64+
unsealed runtimeclass ScrollViewComponentView : ComponentView {
65+
};
66+
67+
[experimental]
68+
[webhosthidden]
69+
[default_interface]
70+
unsealed runtimeclass UnimplementedNativeViewComponentView : ComponentView {
71+
};
72+
73+
[experimental]
74+
[webhosthidden]
75+
[default_interface]
76+
unsealed runtimeclass WindowsTextInputComponentView : ComponentView {
77+
};
78+
79+
} // namespace Microsoft.ReactNative

vnext/Microsoft.ReactNative/Fabric/AbiViewProps.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,10 @@ float UserViewProps::Opacity() noexcept {
4545
return m_viewProps ? m_viewProps->opacity : 1.0f;
4646
}
4747

48+
ViewProps::ViewProps(facebook::react::SharedViewProps props) noexcept : m_props(props) {}
49+
50+
float ViewProps::Opacity() noexcept {
51+
return m_props->opacity;
52+
}
53+
4854
} // namespace winrt::Microsoft::ReactNative::implementation

vnext/Microsoft.ReactNative/Fabric/AbiViewProps.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ struct UserViewProps : ViewPropsT<UserViewProps> {
4545
::Microsoft::ReactNative::AbiViewProps const *m_viewProps{nullptr};
4646
};
4747

48+
struct ViewProps : ViewPropsT<ViewProps> {
49+
ViewProps(facebook::react::SharedViewProps props) noexcept;
50+
51+
float Opacity() noexcept;
52+
53+
private:
54+
facebook::react::SharedViewProps m_props;
55+
};
56+
4857
} // namespace winrt::Microsoft::ReactNative::implementation

vnext/Microsoft.ReactNative/Fabric/ComponentView.h

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@
1313

1414
#include <Fabric/Composition/Theme.h>
1515
#include <winrt/Microsoft.ReactNative.Composition.Input.h>
16+
#include <winrt/Microsoft.ReactNative.h>
1617

17-
namespace Microsoft::ReactNative {
18+
#include "ComponentView.g.h"
19+
20+
namespace winrt::Microsoft::ReactNative::Composition::implementation {
21+
struct RootComponentView;
22+
}
23+
24+
namespace winrt::Microsoft::ReactNative::implementation {
1825

1926
enum class RNComponentViewUpdateMask : std::uint_fast8_t {
2027
None = 0,
@@ -34,8 +41,6 @@ enum class ClipState : std::uint_fast8_t {
3441

3542
DEFINE_ENUM_FLAG_OPERATORS(RNComponentViewUpdateMask);
3643

37-
struct RootComponentView;
38-
3944
struct BringIntoViewOptions {
4045
bool AnimationDesired{false};
4146
// NaN will bring the element fully into view aligned to the nearest edge of the viewport
@@ -47,11 +52,11 @@ struct BringIntoViewOptions {
4752
float VerticalOffset{0};
4853
};
4954

50-
struct IComponentView {
55+
struct ComponentView : public ComponentViewT<ComponentView> {
5156
virtual std::vector<facebook::react::ComponentDescriptorProvider>
5257
supplementalComponentDescriptorProviders() noexcept = 0;
53-
virtual void mountChildComponentView(IComponentView &childComponentView, uint32_t index) noexcept = 0;
54-
virtual void unmountChildComponentView(IComponentView &childComponentView, uint32_t index) noexcept = 0;
58+
virtual void mountChildComponentView(ComponentView &childComponentView, uint32_t index) noexcept = 0;
59+
virtual void unmountChildComponentView(ComponentView &childComponentView, uint32_t index) noexcept = 0;
5560
virtual void updateProps(
5661
facebook::react::Props::Shared const &props,
5762
facebook::react::Props::Shared const &oldProps) noexcept = 0;
@@ -66,16 +71,17 @@ struct IComponentView {
6671
virtual void prepareForRecycle() noexcept = 0;
6772
virtual facebook::react::Props::Shared props() noexcept = 0;
6873
virtual void handleCommand(std::string const &commandName, folly::dynamic const &arg) noexcept = 0;
69-
virtual RootComponentView *rootComponentView() noexcept = 0;
70-
virtual void parent(IComponentView *parent) noexcept = 0;
71-
virtual IComponentView *parent() const noexcept = 0;
74+
virtual winrt::Microsoft::ReactNative::Composition::implementation::RootComponentView *
75+
rootComponentView() noexcept = 0;
76+
virtual void parent(ComponentView *parent) noexcept = 0;
77+
virtual ComponentView *parent() const noexcept = 0;
7278
virtual void theme(winrt::Microsoft::ReactNative::Composition::implementation::Theme *theme) noexcept = 0;
7379
virtual winrt::Microsoft::ReactNative::Composition::implementation::Theme *theme() const noexcept = 0;
7480
virtual void onThemeChanged() noexcept = 0;
75-
virtual const std::vector<IComponentView *> &children() const noexcept = 0;
81+
virtual const std::vector<ComponentView *> &children() const noexcept = 0;
7682
// Run fn on all children of this node until fn returns true
7783
// returns true if the fn ever returned true
78-
virtual bool runOnChildren(bool forward, Mso::Functor<bool(IComponentView &)> &fn) noexcept = 0;
84+
virtual bool runOnChildren(bool forward, Mso::Functor<bool(ComponentView &)> &fn) noexcept = 0;
7985
virtual RECT getClientRect() const noexcept = 0;
8086
// The offset from this elements parent to its children (accounts for things like scroll position)
8187
virtual facebook::react::Point getClientOffset() const noexcept = 0;
@@ -105,7 +111,7 @@ struct IComponentView {
105111
virtual bool focusable() const noexcept = 0;
106112
virtual facebook::react::SharedViewEventEmitter eventEmitterAtPoint(facebook::react::Point pt) noexcept = 0;
107113
virtual facebook::react::SharedViewEventEmitter eventEmitter() noexcept = 0;
108-
virtual facebook::react::Tag tag() const noexcept = 0;
114+
virtual facebook::react::Tag Tag() const noexcept = 0;
109115
// By default, hitTests according the pointerEvents prop on the Component.
110116
// If ignorePointerEvents = true, all Components are treated as valid targets
111117
virtual facebook::react::Tag hitTest(
@@ -124,6 +130,9 @@ struct IComponentView {
124130

125131
// Run fn on all nodes of the component view tree starting from this one until fn returns true
126132
// returns true if the fn ever returned true
127-
bool walkTree(IComponentView &view, bool forward, Mso::Functor<bool(IComponentView &)> &fn) noexcept;
133+
bool walkTree(
134+
ComponentView &view,
135+
bool forward,
136+
Mso::Functor<bool(const winrt::Microsoft::ReactNative::ComponentView &)> &fn) noexcept;
128137

129-
} // namespace Microsoft::ReactNative
138+
} // namespace winrt::Microsoft::ReactNative::implementation

vnext/Microsoft.ReactNative/Fabric/Composition/AbiCompositionViewComponentView.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,27 @@
1313
#include "RootComponentView.h"
1414
#include "Unicode.h"
1515

16-
namespace Microsoft::ReactNative {
16+
namespace winrt::Microsoft::ReactNative::Composition::implementation {
1717

1818
AbiCompositionViewComponentView::AbiCompositionViewComponentView(
1919
winrt::Microsoft::ReactNative::ReactContext const &reactContext,
2020
const winrt::Microsoft::ReactNative::Composition::ICompositionContext &compContext,
2121
facebook::react::Tag tag,
2222
winrt::Microsoft::ReactNative::IReactViewComponentBuilder builder)
2323
: Super(compContext, tag, reactContext, CompositionComponentViewFeatures::Default), m_builder(builder) {
24-
static auto const defaultProps = std::make_shared<AbiViewProps const>();
24+
static auto const defaultProps = std::make_shared<::Microsoft::ReactNative::AbiViewProps const>();
2525
m_props = defaultProps;
2626
m_handle = Builder().CreateView(reactContext.Handle(), compContext);
2727
m_visual = Builder().CreateVisual(m_handle);
2828
OuterVisual().InsertAt(m_visual, 0);
2929
}
3030

31-
std::shared_ptr<AbiCompositionViewComponentView> AbiCompositionViewComponentView::Create(
31+
winrt::Microsoft::ReactNative::ComponentView AbiCompositionViewComponentView::Create(
3232
winrt::Microsoft::ReactNative::ReactContext const &reactContext,
3333
const winrt::Microsoft::ReactNative::Composition::ICompositionContext &compContext,
3434
facebook::react::Tag tag,
3535
winrt::Microsoft::ReactNative::IReactViewComponentBuilder builder) noexcept {
36-
return std::shared_ptr<AbiCompositionViewComponentView>(
37-
new AbiCompositionViewComponentView(reactContext, compContext, tag, builder));
36+
return winrt::make<AbiCompositionViewComponentView>(reactContext, compContext, tag, builder);
3837
}
3938

4039
winrt::Microsoft::ReactNative::Composition::ReactCompositionViewComponentBuilder &
@@ -43,22 +42,22 @@ AbiCompositionViewComponentView::Builder() noexcept {
4342
}
4443

4544
void AbiCompositionViewComponentView::mountChildComponentView(
46-
IComponentView &childComponentView,
45+
winrt::Microsoft::ReactNative::implementation::ComponentView &childComponentView,
4746
uint32_t index) noexcept {
4847
assert(false);
4948
}
5049

5150
void AbiCompositionViewComponentView::unmountChildComponentView(
52-
IComponentView &childComponentView,
51+
winrt::Microsoft::ReactNative::implementation::ComponentView &childComponentView,
5352
uint32_t index) noexcept {
5453
assert(false);
5554
}
5655

5756
void AbiCompositionViewComponentView::updateProps(
5857
facebook::react::Props::Shared const &props,
5958
facebook::react::Props::Shared const &oldProps) noexcept {
60-
const auto &oldViewProps = *std::static_pointer_cast<const AbiViewProps>(m_props);
61-
const auto &newViewProps = *std::static_pointer_cast<const AbiViewProps>(props);
59+
const auto &oldViewProps = *std::static_pointer_cast<const ::Microsoft::ReactNative::AbiViewProps>(m_props);
60+
const auto &newViewProps = *std::static_pointer_cast<const ::Microsoft::ReactNative::AbiViewProps>(props);
6261

6362
if (oldViewProps.testId != newViewProps.testId) {
6463
m_visual.Comment(winrt::to_hstring(newViewProps.testId));
@@ -71,7 +70,7 @@ void AbiCompositionViewComponentView::updateProps(
7170

7271
Builder().UpdateProps(m_handle, newViewProps.UserProps());
7372

74-
m_props = std::static_pointer_cast<AbiViewProps const>(props);
73+
m_props = std::static_pointer_cast<::Microsoft::ReactNative::AbiViewProps const>(props);
7574
}
7675

7776
void AbiCompositionViewComponentView::updateLayoutMetrics(
@@ -96,11 +95,12 @@ void AbiCompositionViewComponentView::updateLayoutMetrics(
9695
void AbiCompositionViewComponentView::updateState(
9796
facebook::react::State::Shared const &state,
9897
facebook::react::State::Shared const &oldState) noexcept {
99-
m_state = winrt::make<AbiComponentState>(state);
98+
m_state = winrt::make<::Microsoft::ReactNative::AbiComponentState>(state);
10099
Builder().UpdateState(m_handle, m_state);
101100
}
102101

103-
void AbiCompositionViewComponentView::finalizeUpdates(RNComponentViewUpdateMask updateMask) noexcept {
102+
void AbiCompositionViewComponentView::finalizeUpdates(
103+
winrt::Microsoft::ReactNative::implementation::RNComponentViewUpdateMask updateMask) noexcept {
104104
Super::finalizeUpdates(updateMask);
105105
Builder().FinalizeUpdates(m_handle);
106106
}
@@ -192,18 +192,18 @@ facebook::react::Tag AbiCompositionViewComponentView::hitTest(
192192
ptLocal.x >= 0 && ptLocal.x <= m_layoutMetrics.frame.size.width && ptLocal.y >= 0 &&
193193
ptLocal.y <= m_layoutMetrics.frame.size.height) {
194194
localPt = ptLocal;
195-
return tag();
195+
return Tag();
196196
}
197197

198198
return -1;
199199
}
200200

201201
winrt::IInspectable AbiCompositionViewComponentView::EnsureUiaProvider() noexcept {
202202
if (m_uiaProvider == nullptr) {
203-
m_uiaProvider = winrt::make<winrt::Microsoft::ReactNative::implementation::CompositionDynamicAutomationProvider>(
204-
shared_from_this());
203+
m_uiaProvider =
204+
winrt::make<winrt::Microsoft::ReactNative::implementation::CompositionDynamicAutomationProvider>(*get_strong());
205205
}
206206
return m_uiaProvider;
207207
}
208208

209-
} // namespace Microsoft::ReactNative
209+
} // namespace winrt::Microsoft::ReactNative::Composition::implementation

vnext/Microsoft.ReactNative/Fabric/Composition/AbiCompositionViewComponentView.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,29 @@
1111

1212
#include <Fabric/Composition/ReactCompositionViewComponentBuilder.h>
1313

14+
#include "Composition.AbiCompositionViewComponentView.g.h"
1415
#include <react/components/rnwcore/ShadowNodes.h>
1516

16-
namespace Microsoft::ReactNative {
17+
namespace winrt::Microsoft::ReactNative::Composition::implementation {
1718

18-
struct AbiCompositionViewComponentView : CompositionBaseComponentView {
19-
using Super = CompositionBaseComponentView;
19+
struct AbiCompositionViewComponentView
20+
: public AbiCompositionViewComponentViewT<AbiCompositionViewComponentView, CompositionBaseComponentView> {
21+
using Super = AbiCompositionViewComponentViewT<AbiCompositionViewComponentView, CompositionBaseComponentView>;
2022

21-
[[nodiscard]] static std::shared_ptr<AbiCompositionViewComponentView> Create(
23+
[[nodiscard]] static winrt::Microsoft::ReactNative::ComponentView Create(
2224
winrt::Microsoft::ReactNative::ReactContext const &reactContext,
2325
const winrt::Microsoft::ReactNative::Composition::ICompositionContext &compContext,
2426
facebook::react::Tag tag,
2527
winrt::Microsoft::ReactNative::IReactViewComponentBuilder builder) noexcept;
2628

2729
winrt::IInspectable EnsureUiaProvider() noexcept override;
2830

29-
void mountChildComponentView(IComponentView &childComponentView, uint32_t index) noexcept override;
30-
void unmountChildComponentView(IComponentView &childComponentView, uint32_t index) noexcept override;
31+
void mountChildComponentView(
32+
winrt::Microsoft::ReactNative::implementation::ComponentView &childComponentView,
33+
uint32_t index) noexcept override;
34+
void unmountChildComponentView(
35+
winrt::Microsoft::ReactNative::implementation::ComponentView &childComponentView,
36+
uint32_t index) noexcept override;
3137

3238
void updateState(facebook::react::State::Shared const &state, facebook::react::State::Shared const &oldState) noexcept
3339
override;
@@ -37,7 +43,8 @@ struct AbiCompositionViewComponentView : CompositionBaseComponentView {
3743
void updateLayoutMetrics(
3844
facebook::react::LayoutMetrics const &layoutMetrics,
3945
facebook::react::LayoutMetrics const &oldLayoutMetrics) noexcept override;
40-
void finalizeUpdates(RNComponentViewUpdateMask updateMask) noexcept override;
46+
void finalizeUpdates(
47+
winrt::Microsoft::ReactNative::implementation::RNComponentViewUpdateMask updateMask) noexcept override;
4148
void prepareForRecycle() noexcept override;
4249
bool focusable() const noexcept override;
4350
void onKeyDown(
@@ -69,20 +76,20 @@ struct AbiCompositionViewComponentView : CompositionBaseComponentView {
6976
const noexcept override;
7077
winrt::Microsoft::ReactNative::Composition::IVisual Visual() const noexcept override;
7178

72-
private:
7379
AbiCompositionViewComponentView(
7480
winrt::Microsoft::ReactNative::ReactContext const &reactContext,
7581
const winrt::Microsoft::ReactNative::Composition::ICompositionContext &compContext,
7682
facebook::react::Tag tag,
7783
winrt::Microsoft::ReactNative::IReactViewComponentBuilder builder);
7884

85+
private:
7986
winrt::Microsoft::ReactNative::Composition::ReactCompositionViewComponentBuilder &Builder() noexcept;
8087

8188
winrt::IInspectable m_handle;
8289
winrt::Microsoft::ReactNative::IReactViewComponentBuilder m_builder;
8390
winrt::Microsoft::ReactNative::IComponentState m_state;
84-
std::shared_ptr<AbiViewProps const> m_props;
91+
std::shared_ptr<::Microsoft::ReactNative::AbiViewProps const> m_props;
8592
winrt::Microsoft::ReactNative::Composition::IVisual m_visual{nullptr};
8693
};
8794

88-
} // namespace Microsoft::ReactNative
95+
} // namespace winrt::Microsoft::ReactNative::Composition::implementation

0 commit comments

Comments
 (0)