Skip to content

Commit

Permalink
Adds GetReactRootView method to XamlUIService (#10403)
Browse files Browse the repository at this point in the history
* Adds GetReactRootView method to XamlUIService

We emulate this behavior in our app, it's probably better to expose the
core functionality to walk the shadow node tree (rather than using
VisualTreeHelpers).

* Change files

* Return ReactRootView from XamlUIService API instead of generic FrameworkElement
  • Loading branch information
rozele committed Oct 27, 2022
1 parent 6943715 commit 4920197
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Adds GetReactRootView method to XamlUIService",
"packageName": "react-native-windows",
"email": "erozell@outlook.com",
"dependentChangeType": "patch"
}
15 changes: 15 additions & 0 deletions vnext/Microsoft.ReactNative/XamlUIService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ void XamlUIService::DispatchEvent(
m_context->CallJSFunction("RCTEventEmitter", "receiveEvent", std::move(params));
}

winrt::Microsoft::ReactNative::ReactRootView XamlUIService::GetReactRootView(
xaml::FrameworkElement const &view) noexcept {
if (auto uiManager = ::Microsoft::ReactNative::GetNativeUIManager(*m_context).lock()) {
const auto reactTag = ::Microsoft::ReactNative::GetTag(view);
auto shadowNode = uiManager->getHost()->FindParentRootShadowNode(reactTag);
if (!shadowNode)
return nullptr;

return static_cast<::Microsoft::ReactNative::ShadowNodeBase *>(shadowNode)
->GetView()
.as<winrt::Microsoft::ReactNative::ReactRootView>();
}
return nullptr;
}

/*static*/ ReactPropertyId<XamlUIService> XamlUIService::XamlUIServiceProperty() noexcept {
static ReactPropertyId<XamlUIService> uiManagerProperty{L"ReactNative.UIManager", L"XamlUIManager"};
return uiManagerProperty;
Expand Down
2 changes: 2 additions & 0 deletions vnext/Microsoft.ReactNative/XamlUIService.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct XamlUIService : XamlUIServiceT<XamlUIService> {
hstring const &eventName,
JSValueArgWriter const &eventDataArgWriter) noexcept;

winrt::Microsoft::ReactNative::ReactRootView GetReactRootView(xaml::FrameworkElement const &view) noexcept;

static void SetXamlRoot(IReactPropertyBag const &properties, xaml::XamlRoot const &xamlRoot) noexcept;
static void SetAccessibleRoot(
IReactPropertyBag const &properties,
Expand Down
4 changes: 4 additions & 0 deletions vnext/Microsoft.ReactNative/XamlUIService.idl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import "IReactContext.idl";
import "ReactRootView.idl";

#include "NamespaceRedirect.h"
#include "DocString.h"
Expand All @@ -24,6 +25,9 @@ namespace Microsoft.ReactNative
DOC_STRING("Dispatches an event to a JS component.")
void DispatchEvent(XAML_NAMESPACE.FrameworkElement view, String eventName, JSValueArgWriter eventDataArgWriter);

DOC_STRING("Gets the @ReactRootView view for a given element.")
ReactRootView GetReactRootView(XAML_NAMESPACE.FrameworkElement view);

DOC_STRING(
"Sets the @Windows.UI.Xaml.XamlRoot element for the app. "
"This must be manually provided to the @ReactInstanceSettings object when using XAML Islands "
Expand Down

0 comments on commit 4920197

Please sign in to comment.