Skip to content

Commit eb39c9c

Browse files
Initialize Xaml on demand (#15407)
* Initialize Xaml if requested by a module * Drop XamlHost component - Exposing this means both app and module authors can use any Xaml but we want to enable Xaml hosting only for module authors for 0.82 and for this we don’t need XamlHost. * Initialize `XamlApplication`, which loads Xaml framework (costly), when there’s at least one component requesting Xaml support through `ReactNativeComponentBuilder::XamlSupport` * Fix ReactWindows-Desktop build break `//vnect/Shared/Shared.vcxitems` is included by two project: 1. `//vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj` 2. `//vnext/Desktop/React.Windows.Desktop.vcxproj` (2) is a static library that breaks compiling `ReactNativeHost.cpp` as it includes `XamlApplication.h` which in turn includes `Xaml.XamlApplication.g.*` which requires `XamlApplication.idl` to be generating them. This won’t happen due to the project type of (2). Wrap `XamlApplication`-related code in a macro defined only for (1).
1 parent 2b35af8 commit eb39c9c

18 files changed

+41
-147
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "none",
3+
"comment": "Initialize Xaml on demand only for module authors",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "none"
7+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ void ReactCompositionViewComponentBuilder::SetUnmountChildComponentViewHandler(
227227
m_unmountChildComponentViewHandler = impl;
228228
}
229229

230+
bool ReactCompositionViewComponentBuilder::XamlSupport() const noexcept {
231+
return m_xamlSupport;
232+
}
233+
234+
void ReactCompositionViewComponentBuilder::XamlSupport(bool isRequired) noexcept {
235+
m_xamlSupport = isRequired;
236+
}
237+
230238
const UnmountChildComponentViewDelegate &ReactCompositionViewComponentBuilder::UnmountChildComponentViewHandler()
231239
const noexcept {
232240
return m_unmountChildComponentViewHandler;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ struct ReactCompositionViewComponentBuilder
3939
void SetUpdateEventEmitterHandler(UpdateEventEmitterDelegate impl) noexcept;
4040
void SetMountChildComponentViewHandler(MountChildComponentViewDelegate impl) noexcept;
4141
void SetUnmountChildComponentViewHandler(UnmountChildComponentViewDelegate impl) noexcept;
42+
bool XamlSupport() const noexcept;
43+
void XamlSupport(bool isRequired) noexcept;
4244

4345
public: // Composition::IReactCompositionViewComponentBuilder
4446
void SetViewComponentViewInitializer(const ViewComponentViewInitializer &initializer) noexcept;
@@ -108,6 +110,7 @@ struct ReactCompositionViewComponentBuilder
108110
winrt::Microsoft::ReactNative::Composition::Experimental::IVisualToMountChildrenIntoDelegate
109111
m_visualToMountChildrenIntoHandler;
110112
UpdateLayoutMetricsDelegate m_updateLayoutMetricsHandler;
113+
bool m_xamlSupport{false};
111114
};
112115

113116
} // namespace winrt::Microsoft::ReactNative::Composition

vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ bool WindowsComponentDescriptorRegistry::hasComponentProvider(const std::string
5454
return std::find(m_componentNames.begin(), m_componentNames.end(), name) != m_componentNames.end();
5555
}
5656

57+
bool WindowsComponentDescriptorRegistry::isXamlSupportRequired() const noexcept {
58+
return std::any_of(m_builderByName.cbegin(), m_builderByName.cend(), [](const auto &pair) -> bool {
59+
return winrt::get_self<winrt::Microsoft::ReactNative::Composition::ReactCompositionViewComponentBuilder>(
60+
pair.second)
61+
->XamlSupport();
62+
});
63+
}
64+
5765
void WindowsComponentDescriptorRegistry::Add(
5866
winrt::hstring componentName,
5967
winrt::Microsoft::ReactNative::ReactViewComponentProvider const &provider) noexcept {

vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ struct WindowsComponentDescriptorRegistry {
3434

3535
bool hasComponentProvider(const std::string &name) noexcept;
3636

37+
// Returns true if any component requested for XAML support.
38+
bool isXamlSupportRequired() const noexcept;
39+
3740
private:
3841
void add(const facebook::react::ComponentDescriptorProvider &provider) noexcept;
3942

vnext/Microsoft.ReactNative/IReactViewComponentBuilder.idl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ namespace Microsoft.ReactNative
124124
void SetUpdateEventEmitterHandler(UpdateEventEmitterDelegate impl);
125125
void SetMountChildComponentViewHandler(MountChildComponentViewDelegate impl);
126126
void SetUnmountChildComponentViewHandler(UnmountChildComponentViewDelegate impl);
127+
Boolean XamlSupport { get; set; };
127128
};
128129

129130
// [exclusiveto(ShadowNode)]

vnext/Microsoft.ReactNative/IXamlProvider.idl

Lines changed: 0 additions & 11 deletions
This file was deleted.

vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
DISABLE_XAML_GENERATED_MAIN;
125125
REACTWINDOWS_BUILD;
126126
RN_PLATFORM=windows;
127+
RNW_XAML_ISLAND;
127128
NOMINMAX;
128129
FOLLY_CFG_NO_COROUTINES;
129130
FOLLY_NO_CONFIG;

vnext/Microsoft.ReactNative/ReactNativeHost.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
#include <winrt/Windows.Foundation.Collections.h>
1414
#include "IReactContext.h"
1515
#include "ReactInstanceSettings.h"
16+
#ifdef RNW_XAML_ISLAND
17+
#include "XamlApplication.h"
18+
#endif // RNW_XAML_ISLAND
1619

1720
#include <Fabric/Composition/Modal/WindowsModalHostViewComponentView.h>
1821
#include <Fabric/WindowsComponentDescriptorRegistry.h>
@@ -102,6 +105,12 @@ IAsyncAction ReactNativeHost::ReloadInstance() noexcept {
102105
}
103106
}
104107

108+
#ifdef RNW_XAML_ISLAND
109+
if (componentregistry->isXamlSupportRequired()) {
110+
winrt::Microsoft::ReactNative::Xaml::implementation::XamlApplication::EnsureCreated();
111+
}
112+
#endif // RNW_XAML_ISLAND
113+
105114
ReactPropertyBag(m_instanceSettings.Properties()).Set(ReactNativeHostProperty(), get_weak());
106115

107116
Mso::React::ReactOptions reactOptions{};

vnext/Microsoft.ReactNative/XamlHost.cpp

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)