diff --git a/change/react-native-windows-73f0d63e-63d7-4322-b402-83e1c0c092e8.json b/change/react-native-windows-73f0d63e-63d7-4322-b402-83e1c0c092e8.json new file mode 100644 index 00000000000..70df7564ea4 --- /dev/null +++ b/change/react-native-windows-73f0d63e-63d7-4322-b402-83e1c0c092e8.json @@ -0,0 +1,7 @@ +{ + "type": "none", + "comment": "Initialize Xaml on demand only for module authors", + "packageName": "react-native-windows", + "email": "suramaswamy@microsoft.com", + "dependentChangeType": "none" +} diff --git a/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.cpp b/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.cpp index 83b25e3c727..52c262423b5 100644 --- a/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.cpp @@ -227,6 +227,14 @@ void ReactCompositionViewComponentBuilder::SetUnmountChildComponentViewHandler( m_unmountChildComponentViewHandler = impl; } +bool ReactCompositionViewComponentBuilder::XamlSupport() const noexcept { + return m_xamlSupport; +} + +void ReactCompositionViewComponentBuilder::XamlSupport(bool isRequired) noexcept { + m_xamlSupport = isRequired; +} + const UnmountChildComponentViewDelegate &ReactCompositionViewComponentBuilder::UnmountChildComponentViewHandler() const noexcept { return m_unmountChildComponentViewHandler; diff --git a/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.h b/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.h index f9d9ab9a227..fa5ec618baf 100644 --- a/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.h +++ b/vnext/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.h @@ -39,6 +39,8 @@ struct ReactCompositionViewComponentBuilder void SetUpdateEventEmitterHandler(UpdateEventEmitterDelegate impl) noexcept; void SetMountChildComponentViewHandler(MountChildComponentViewDelegate impl) noexcept; void SetUnmountChildComponentViewHandler(UnmountChildComponentViewDelegate impl) noexcept; + bool XamlSupport() const noexcept; + void XamlSupport(bool isRequired) noexcept; public: // Composition::IReactCompositionViewComponentBuilder void SetViewComponentViewInitializer(const ViewComponentViewInitializer &initializer) noexcept; @@ -108,6 +110,7 @@ struct ReactCompositionViewComponentBuilder winrt::Microsoft::ReactNative::Composition::Experimental::IVisualToMountChildrenIntoDelegate m_visualToMountChildrenIntoHandler; UpdateLayoutMetricsDelegate m_updateLayoutMetricsHandler; + bool m_xamlSupport{false}; }; } // namespace winrt::Microsoft::ReactNative::Composition diff --git a/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.cpp b/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.cpp index b3c0fc9815e..e8f311702f2 100644 --- a/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.cpp +++ b/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.cpp @@ -54,6 +54,14 @@ bool WindowsComponentDescriptorRegistry::hasComponentProvider(const std::string return std::find(m_componentNames.begin(), m_componentNames.end(), name) != m_componentNames.end(); } +bool WindowsComponentDescriptorRegistry::isXamlSupportRequired() const noexcept { + return std::any_of(m_builderByName.cbegin(), m_builderByName.cend(), [](const auto &pair) -> bool { + return winrt::get_self( + pair.second) + ->XamlSupport(); + }); +} + void WindowsComponentDescriptorRegistry::Add( winrt::hstring componentName, winrt::Microsoft::ReactNative::ReactViewComponentProvider const &provider) noexcept { diff --git a/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.h b/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.h index f29dc581841..68ede8eb6e5 100644 --- a/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.h +++ b/vnext/Microsoft.ReactNative/Fabric/WindowsComponentDescriptorRegistry.h @@ -34,6 +34,9 @@ struct WindowsComponentDescriptorRegistry { bool hasComponentProvider(const std::string &name) noexcept; + // Returns true if any component requested for XAML support. + bool isXamlSupportRequired() const noexcept; + private: void add(const facebook::react::ComponentDescriptorProvider &provider) noexcept; diff --git a/vnext/Microsoft.ReactNative/IReactViewComponentBuilder.idl b/vnext/Microsoft.ReactNative/IReactViewComponentBuilder.idl index fa2197a0596..2b60aa7e6cd 100644 --- a/vnext/Microsoft.ReactNative/IReactViewComponentBuilder.idl +++ b/vnext/Microsoft.ReactNative/IReactViewComponentBuilder.idl @@ -124,6 +124,7 @@ namespace Microsoft.ReactNative void SetUpdateEventEmitterHandler(UpdateEventEmitterDelegate impl); void SetMountChildComponentViewHandler(MountChildComponentViewDelegate impl); void SetUnmountChildComponentViewHandler(UnmountChildComponentViewDelegate impl); + Boolean XamlSupport { get; set; }; }; // [exclusiveto(ShadowNode)] diff --git a/vnext/Microsoft.ReactNative/IXamlProvider.idl b/vnext/Microsoft.ReactNative/IXamlProvider.idl deleted file mode 100644 index eb20e3cc3fb..00000000000 --- a/vnext/Microsoft.ReactNative/IXamlProvider.idl +++ /dev/null @@ -1,11 +0,0 @@ - -#include "DocString.h" - -namespace Microsoft.ReactNative.Xaml { -[webhosthidden] -interface IXamlControl { - DOC_STRING( - "Native components that want to be Xaml-based can implement IXamlControl to allow their object to be parented to a XamlHost.") - Microsoft.UI.Xaml.UIElement GetXamlElement(); -}; -} // namespace Microsoft.ReactNative.Xaml diff --git a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj index c7e8956734c..f72f867ded9 100644 --- a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +++ b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj @@ -124,6 +124,7 @@ DISABLE_XAML_GENERATED_MAIN; REACTWINDOWS_BUILD; RN_PLATFORM=windows; + RNW_XAML_ISLAND; NOMINMAX; FOLLY_CFG_NO_COROUTINES; FOLLY_NO_CONFIG; diff --git a/vnext/Microsoft.ReactNative/ReactNativeHost.cpp b/vnext/Microsoft.ReactNative/ReactNativeHost.cpp index 543a0c7d441..a7b03a1388d 100644 --- a/vnext/Microsoft.ReactNative/ReactNativeHost.cpp +++ b/vnext/Microsoft.ReactNative/ReactNativeHost.cpp @@ -13,6 +13,9 @@ #include #include "IReactContext.h" #include "ReactInstanceSettings.h" +#ifdef RNW_XAML_ISLAND +#include "XamlApplication.h" +#endif // RNW_XAML_ISLAND #include #include @@ -102,6 +105,12 @@ IAsyncAction ReactNativeHost::ReloadInstance() noexcept { } } +#ifdef RNW_XAML_ISLAND + if (componentregistry->isXamlSupportRequired()) { + winrt::Microsoft::ReactNative::Xaml::implementation::XamlApplication::EnsureCreated(); + } +#endif // RNW_XAML_ISLAND + ReactPropertyBag(m_instanceSettings.Properties()).Set(ReactNativeHostProperty(), get_weak()); Mso::React::ReactOptions reactOptions{}; diff --git a/vnext/Shared/Shared.vcxitems b/vnext/Shared/Shared.vcxitems index 3568b832da0..2caa172e37d 100644 --- a/vnext/Shared/Shared.vcxitems +++ b/vnext/Shared/Shared.vcxitems @@ -139,11 +139,6 @@ true $(ReactNativeWindowsDir)Microsoft.ReactNative\XamlApplication.idl Code - - - true - $(ReactNativeWindowsDir)Microsoft.ReactNative\XamlApplication.idl - Code @@ -248,11 +243,6 @@ true $(MSBuildThisFileDirectory)..\Microsoft.ReactNative\XamlApplication.idl Code - - - true - $(MSBuildThisFileDirectory)..\Microsoft.ReactNative\XamlApplication.idl - Code $(MSBuildThisFileDirectory)..\Microsoft.ReactNative\IJSValueReader.idl @@ -612,7 +602,6 @@ - diff --git a/vnext/overrides.json b/vnext/overrides.json index eb3e0884bcb..28cba06257f 100644 --- a/vnext/overrides.json +++ b/vnext/overrides.json @@ -694,18 +694,6 @@ "file": "src-win/src/private/specs_DEPRECATED/modules/NativePlatformConstantsWindows.js", "baseFile": "packages/react-native/src/private/specs_DEPRECATED/modules/NativePlatformConstantsAndroid.js", "baseHash": "365c5df75b38b129d364af3f6700cb206ce5bd2a" - }, - { - "type": "platform", - "file": "src-win/Libraries/Components/Xaml/XamlHost.d.ts" - }, - { - "type": "platform", - "file": "src-win/Libraries/Components/Xaml/XamlHost.windows.js" - }, - { - "type": "platform", - "file": "src-win/src/private/specs_DEPRECATED/components/Xaml/XamlHostNativeComponent.js" } ] -} \ No newline at end of file +} diff --git a/vnext/src-win/Libraries/Components/Xaml/XamlHost.d.ts b/vnext/src-win/Libraries/Components/Xaml/XamlHost.d.ts deleted file mode 100644 index a69cfafe0be..00000000000 --- a/vnext/src-win/Libraries/Components/Xaml/XamlHost.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. - * Licensed under the MIT License. - * @format - * @flow - */ -/// -import type {ViewProps} from 'react-native'; -export interface XamlHostProps extends ViewProps { - label: string; -} -declare const _default: import('react-native/Libraries/Utilities/codegenNativeComponent').NativeComponentType; -export default _default; diff --git a/vnext/src-win/Libraries/Components/Xaml/XamlHost.windows.js b/vnext/src-win/Libraries/Components/Xaml/XamlHost.windows.js deleted file mode 100644 index 723810b17a4..00000000000 --- a/vnext/src-win/Libraries/Components/Xaml/XamlHost.windows.js +++ /dev/null @@ -1,7 +0,0 @@ -/** - * @format - * @flow - */ - -import XamlHost from '../../../src/private/specs_DEPRECATED/components/Xaml/XamlHostNativeComponent'; -export default XamlHost; diff --git a/vnext/src-win/index.windows.js b/vnext/src-win/index.windows.js index 8f368fe05fe..14ac2344540 100644 --- a/vnext/src-win/index.windows.js +++ b/vnext/src-win/index.windows.js @@ -372,9 +372,6 @@ module.exports = { get AppTheme() { return require('./Libraries/AppTheme/AppTheme').AppTheme; }, - get XamlHost() { - return require('./Libraries/Components/Xaml/XamlHost').default; - }, } as ReactNativePublicAPI; if (__DEV__) { diff --git a/vnext/src-win/index.windows.js.flow b/vnext/src-win/index.windows.js.flow index d601455ba73..6942181c0c3 100644 --- a/vnext/src-win/index.windows.js.flow +++ b/vnext/src-win/index.windows.js.flow @@ -469,7 +469,6 @@ export {HandledEventPhase } from './Libraries/Components/View/ViewPropTypes' export {default as ViewWindows} from './Libraries/Components/View/View'; export {AppTheme} from './Libraries/AppTheme/AppTheme'; -export {default as XamlHost} from './Libraries/Components/Xaml/XamlHost'; // End Windows Specific exports // #endregion diff --git a/vnext/src-win/src/private/specs_DEPRECATED/components/Xaml/XamlHostNativeComponent.js b/vnext/src-win/src/private/specs_DEPRECATED/components/Xaml/XamlHostNativeComponent.js deleted file mode 100644 index f1a9ed9c96a..00000000000 --- a/vnext/src-win/src/private/specs_DEPRECATED/components/Xaml/XamlHostNativeComponent.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. - * Licensed under the MIT License. - * @format - * @flow - */ - -'use strict'; - -import type {ViewProps} from 'react-native'; -import {codegenNativeComponent} from 'react-native'; -import type {HostComponent} from '../../../../../src/private/types/HostComponent'; - -type XamlHostProps = $ReadOnly<{| - ...ViewProps, -|}>; - -type NativeType = HostComponent; - -export default (codegenNativeComponent('XamlHost'): NativeType);